]> git.ipfire.org Git - thirdparty/squid.git/blame - src/Store.h
Shared Rock::DirMap version 6.
[thirdparty/squid.git] / src / Store.h
CommitLineData
e6ccf245 1/*
262a0e14 2 * $Id$
e6ccf245 3 *
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
26ac0430 21 *
e6ccf245 22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26ac0430 26 *
e6ccf245 27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 *
31 */
e6ccf245 32#ifndef SQUID_STORE_H
33#define SQUID_STORE_H
34
63be0a78 35/**
36 \defgroup StoreAPI Store API
37 \ingroup FileSystems
38 */
39
c21ad0f5 40#include "squid.h"
528b2c61 41#include "StoreIOBuffer.h"
a46d2c0e 42#include "Range.h"
c8f4eac4 43#include "RefCount.h"
a46d2c0e 44#include "CommRead.h"
25b6a907 45#include "Packer.h"
aa839030 46#include "RemovalPolicy.h"
528b2c61 47
f41735ea 48#if USE_SQUID_ESI
f99c2cfe 49#include "esi/Element.h"
43ae1d95 50#endif
51
27e059d4
AJ
52#if HAVE_OSTREAM
53#include <ostream>
54#endif
55
56
65517dc8 57class AsyncCall;
e6ccf245 58class StoreClient;
528b2c61 59class MemObject;
c8f4eac4 60class Store;
c8f4eac4 61class StoreSearch;
62
8822ebee
AR
63typedef struct {
64
65 struct {
66 int calls;
67 int select_fail;
68 int create_fail;
69 int success;
70 } create;
71} StoreIoStats;
72
73extern StoreIoStats store_io_stats;
74
63be0a78 75/**
76 \ingroup StoreAPI
77 */
62e76326 78class StoreEntry : public hash_link
79{
80
332dafa2 81public:
a46d2c0e 82 static DeferredRead::DeferrableRead DeferReader;
83 bool checkDeferRead(int fd) const;
62e76326 84
528b2c61 85 virtual const char *getMD5Text() const;
c8f4eac4 86 StoreEntry();
87 StoreEntry(const char *url, const char *log_url);
26ac0430 88 virtual ~StoreEntry() {}
43ae1d95 89
528b2c61 90 virtual HttpReply const *getReply() const;
91 virtual void write (StoreIOBuffer);
92 virtual _SQUID_INLINE_ bool isEmpty() const;
aa18a4ca 93 virtual bool isAccepting() const;
a46d2c0e 94 virtual size_t bytesWanted(Range<size_t> const) const;
528b2c61 95 virtual void complete();
96 virtual store_client_t storeClientType() const;
97 virtual char const *getSerialisedMetaData();
4a56ee8d 98 virtual void replaceHttpReply(HttpReply *);
528b2c61 99 virtual bool swapoutPossible();
100 virtual void trimMemory();
bfb55b6f 101 void abort();
c8f4eac4 102 void unlink();
5ed72359 103 void makePublic();
104 void makePrivate();
d88e3c49 105 void setPublicKey();
106 void setPrivateKey();
107 void expireNow();
108 void releaseRequest();
109 void negativeCache();
63be0a78 110 void cacheNegatively(); /** \todo argh, why both? */
d88e3c49 111 void invokeHandlers();
112 void purgeMem();
c07cbbf4 113 void swapOut();
114 bool swapOutAble() const;
115 void swapOutFileClose();
3900307b 116 const char *url() const;
117 int checkCachable();
118 int checkNegativeHit() const;
119 int locked() const;
120 int validToSend() const;
121 int keepInMemory() const;
122 void createMemObject(const char *, const char *);
123 void dump(int debug_lvl) const;
124 void hashDelete();
125 void hashInsert(const cache_key *);
126 void registerAbort(STABH * cb, void *);
127 void reset();
128 void setMemStatus(mem_status_t);
129 void timestampsSet();
130 void unregisterAbort();
131 void destroyMemObject();
132 int checkTooSmall();
528b2c61 133
65517dc8 134 void delayAwareRead(int fd, char *buf, int len, AsyncCall::Pointer callback);
43ae1d95 135
a46d2c0e 136 void setNoDelay (bool const);
190154cf 137 bool modifiedSince(HttpRequest * request) const;
79c8035e
AR
138 /// has ETag matching at least one of the If-Match etags
139 bool hasIfMatchEtag(const HttpRequest &request) const;
140 /// has ETag matching at least one of the If-None-Match etags
141 bool hasIfNoneMatchEtag(const HttpRequest &request) const;
63be0a78 142
143 /** What store does this entry belong too ? */
c8f4eac4 144 virtual RefCount<Store> store() const;
a46d2c0e 145
e6ccf245 146 MemObject *mem_obj;
147 RemovalPolicyNode repl;
148 /* START OF ON-DISK STORE_META_STD TLV field */
149 time_t timestamp;
150 time_t lastref;
151 time_t expires;
152 time_t lastmod;
47f6e231 153 uint64_t swap_file_sz;
e6ccf245 154 u_short refcount;
155 u_short flags;
156 /* END OF ON-DISK STORE_META_STD */
62e76326 157
d1398b75
HN
158 sfileno swap_filen:25;
159
160 sdirno swap_dirn:7;
62e76326 161
e6ccf245 162 u_short lock_count; /* Assume < 65536! */
62e76326 163
d1398b75 164 mem_status_t mem_status:3;
62e76326 165
d1398b75 166 ping_status_t ping_status:3;
62e76326 167
d1398b75 168 store_status_t store_status:3;
62e76326 169
d1398b75 170 swap_status_t swap_status:3;
62e76326 171
e6ccf245 172public:
173 static size_t inUseCount();
60745f24 174 static void getPublicByRequestMethod(StoreClient * aClient, HttpRequest * request, const HttpRequestMethod& method);
190154cf 175 static void getPublicByRequest(StoreClient * aClient, HttpRequest * request);
60745f24 176 static void getPublic(StoreClient * aClient, const char *uri, const HttpRequestMethod& method);
528b2c61 177
26ac0430 178 virtual bool isNull() {
62e76326 179 return false;
63be0a78 180 };
62e76326 181
35db0b4e 182 void *operator new(size_t byteCount);
e6ccf245 183 void operator delete(void *address);
43ae1d95 184 void setReleaseFlag();
f41735ea 185#if USE_SQUID_ESI
43ae1d95 186
187 ESIElement::Pointer cachedESITree;
188#endif
63be0a78 189 /** append bytes to the buffer */
c21ad0f5 190 virtual void append(char const *, int len);
63be0a78 191 /** disable sending content to the clients */
c21ad0f5 192 virtual void buffer();
63be0a78 193 /** flush any buffered content */
c21ad0f5 194 virtual void flush();
63be0a78 195 /** reduce the memory lock count on the entry */
c21ad0f5 196 virtual int unlock();
63be0a78 197 /** increate the memory lock count on the entry */
47f6e231 198 virtual int64_t objectLen() const;
199 virtual int64_t contentLen() const;
c21ad0f5 200
63be0a78 201 virtual void lock();
5f33b71d 202 virtual void release();
62e76326 203
e6ccf245 204private:
04eb0689 205 static MemAllocator *pool;
528b2c61 206
207 bool validLength() const;
79c8035e 208 bool hasOneOfEtags(const String &reqETags, const bool allowWeakMatch) const;
e6ccf245 209};
210
2c4cd1ad
AR
211std::ostream &operator <<(std::ostream &os, const StoreEntry &e);
212
63be0a78 213/// \ingroup StoreAPI
3b13a8fd 214class NullStoreEntry:public StoreEntry
e6ccf245 215{
62e76326 216
e6ccf245 217public:
218 static NullStoreEntry *getInstance();
26ac0430 219 bool isNull() {
62e76326 220 return true;
e6ccf245 221 }
62e76326 222
332dafa2 223 const char *getMD5Text() const;
528b2c61 224 _SQUID_INLINE_ HttpReply const *getReply() const;
26ac0430 225 void write (StoreIOBuffer) {}
62e76326 226
528b2c61 227 bool isEmpty () const {return true;}
62e76326 228
e1381638 229 virtual size_t bytesWanted(Range<size_t> const aRange) const { assert (aRange.size()); return aRange.end - 1;}
62e76326 230
e6ccf245 231 void operator delete(void *address);
26ac0430 232 void complete() {}
62e76326 233
234private:
26ac0430 235 store_client_t storeClientType() const {return STORE_MEM_CLIENT;}
62e76326 236
528b2c61 237 char const *getSerialisedMetaData();
238 bool swapoutPossible() {return false;}
62e76326 239
528b2c61 240 void trimMemory() {}
241
62e76326 242
e6ccf245 243 static NullStoreEntry _instance;
244};
245
63be0a78 246/// \ingroup StoreAPI
c8f4eac4 247typedef void (*STOREGETCLIENT) (StoreEntry *, void *cbdata);
248
c21ad0f5 249
63be0a78 250/**
251 \ingroup StoreAPI
252 * Abstract base class that will replace the whole store and swapdir interface.
253 */
c8f4eac4 254class Store : public RefCountable
255{
256
257public:
63be0a78 258 /** The root store */
88e13392 259 static _SQUID_INLINE_ Store &Root();
c8f4eac4 260 static void Root(Store *);
261 static void Root(RefCount<Store>);
262 static void Stats(StoreEntry * output);
263 static void Maintain(void *unused);
264
265 virtual ~Store() {}
266
63be0a78 267 /** Handle pending callbacks - called by the event loop. */
c8f4eac4 268 virtual int callback() = 0;
63be0a78 269
270 /** create the resources needed for this store to operate */
c8f4eac4 271 virtual void create();
63be0a78 272
273 /**
274 * Notify this store that its disk is full.
275 \todo XXX move into a protected api call between store files and their stores, rather than a top level api call
c8f4eac4 276 */
277 virtual void diskFull();
c8f4eac4 278
63be0a78 279 /** Retrieve a store entry from the store */
b21767da 280 virtual StoreEntry * get(const cache_key *) = 0;
c8f4eac4 281
63be0a78 282 /** \todo imeplement the async version */
b21767da 283 virtual void get(String const key , STOREGETCLIENT callback, void *cbdata) = 0;
c8f4eac4 284
285 /* prepare the store for use. The store need not be usable immediately,
286 * it should respond to readable() and writable() with true as soon
287 * as it can provide those services
288 */
289 virtual void init() = 0;
290
63be0a78 291 /**
292 * The maximum size the store will support in normal use. Inaccuracy is permitted,
293 * but may throw estimates for memory etc out of whack.
294 */
12e11a5c 295 virtual uint64_t maxSize() const = 0;
c8f4eac4 296
63be0a78 297 /** The minimum size the store will shrink to via normal housekeeping */
12e11a5c 298 virtual uint64_t minSize() const = 0;
c8f4eac4 299
63be0a78 300 /**
301 * Output stats to the provided store entry.
302 \todo make these calls asynchronous
303 */
304 virtual void stat(StoreEntry &) const = 0;
c8f4eac4 305
63be0a78 306 /** Sync the store prior to shutdown */
307 virtual void sync();
c8f4eac4 308
63be0a78 309 /** remove a Store entry from the store */
c8f4eac4 310 virtual void unlink (StoreEntry &);
311
312 /* search in the store */
30abd221 313 virtual StoreSearch *search(String const url, HttpRequest *) = 0;
c8f4eac4 314
315 /* pulled up from SwapDir for migration.... probably do not belong here */
316 virtual void reference(StoreEntry &) = 0; /* Reference this object */
317
318 virtual void dereference(StoreEntry &) = 0; /* Unreference this object */
319
320 virtual void maintain() = 0; /* perform regular maintenance should be private and self registered ... */
321
322 /* These should really be private */
47f6e231 323 virtual void updateSize(int64_t size, int sign) = 0;
c8f4eac4 324
325private:
326 static RefCount<Store> CurrentRoot;
327};
328
63be0a78 329/// \ingroup StoreAPI
c8f4eac4 330typedef RefCount<Store> StorePointer;
331
63be0a78 332/// \ingroup StoreAPI
43ae1d95 333SQUIDCEXTERN size_t storeEntryInUse();
63be0a78 334
335/// \ingroup StoreAPI
e6ccf245 336SQUIDCEXTERN const char *storeEntryFlags(const StoreEntry *);
63be0a78 337
338/// \ingroup StoreAPI
528b2c61 339extern void storeEntryReplaceObject(StoreEntry *, HttpReply *);
e6ccf245 340
63be0a78 341/// \ingroup StoreAPI
60745f24 342SQUIDCEXTERN StoreEntry *storeGetPublic(const char *uri, const HttpRequestMethod& method);
63be0a78 343
344/// \ingroup StoreAPI
190154cf 345SQUIDCEXTERN StoreEntry *storeGetPublicByRequest(HttpRequest * request);
63be0a78 346
347/// \ingroup StoreAPI
60745f24 348SQUIDCEXTERN StoreEntry *storeGetPublicByRequestMethod(HttpRequest * request, const HttpRequestMethod& method);
63be0a78 349
350/// \ingroup StoreAPI
60745f24 351SQUIDCEXTERN StoreEntry *storeCreateEntry(const char *, const char *, request_flags, const HttpRequestMethod&);
63be0a78 352
353/// \ingroup StoreAPI
e6ccf245 354SQUIDCEXTERN void storeInit(void);
63be0a78 355
63be0a78 356/// \ingroup StoreAPI
e6ccf245 357SQUIDCEXTERN void storeConfigure(void);
63be0a78 358
359/// \ingroup StoreAPI
e6ccf245 360SQUIDCEXTERN void storeFreeMemory(void);
63be0a78 361
362/// \ingroup StoreAPI
e6ccf245 363SQUIDCEXTERN int expiresMoreThan(time_t, time_t);
63be0a78 364
63be0a78 365/// \ingroup StoreAPI
366SQUIDCEXTERN void storeAppendPrintf(StoreEntry *, const char *,...) PRINTF_FORMAT_ARG2;
63be0a78 367
368/// \ingroup StoreAPI
daacd51f 369extern void storeAppendVPrintf(StoreEntry *, const char *, va_list ap);
63be0a78 370
371/// \ingroup StoreAPI
e6ccf245 372SQUIDCEXTERN int storeTooManyDiskFilesOpen(void);
63be0a78 373
e1f7507e 374class SwapDir;
63be0a78 375/// \ingroup StoreAPI
e6ccf245 376SQUIDCEXTERN void storeHeapPositionUpdate(StoreEntry *, SwapDir *);
63be0a78 377
378/// \ingroup StoreAPI
e6ccf245 379SQUIDCEXTERN void storeSwapFileNumberSet(StoreEntry * e, sfileno filn);
63be0a78 380
381/// \ingroup StoreAPI
e6ccf245 382SQUIDCEXTERN void storeFsInit(void);
63be0a78 383
384/// \ingroup StoreAPI
e6ccf245 385SQUIDCEXTERN void storeFsDone(void);
63be0a78 386
387/// \ingroup StoreAPI
e6ccf245 388SQUIDCEXTERN void storeReplAdd(const char *, REMOVALPOLICYCREATE *);
63be0a78 389
390/// \ingroup StoreAPI
c8f4eac4 391extern FREE destroyStoreEntry;
e6ccf245 392
63be0a78 393/**
394 \ingroup StoreAPI
395 \todo should be a subclass of Packer perhaps ?
396 */
25b6a907 397SQUIDCEXTERN void packerToStoreInit(Packer * p, StoreEntry * e);
398
6d3c2758
HN
399/// \ingroup StoreAPI
400SQUIDCEXTERN void storeGetMemSpace(int size);
401
32d002cb 402#if _USE_INLINE_
528b2c61 403#include "Store.cci"
404#endif
405
e6ccf245 406#endif /* SQUID_STORE_H */