]> git.ipfire.org Git - thirdparty/squid.git/blame - src/Store.h
Move max_size and n_disk_objects to specific SwapDirs, remove updateSize().
[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 StoreSearch;
7d3c4ca1 61class SwapDir;
c8f4eac4 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);
6d8d05b5 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();
9487bae9 113 void cacheInMemory(); ///< start or continue storing in memory cache
c07cbbf4 114 void swapOut();
115 bool swapOutAble() const;
aa1a691e 116 void swapOutFileClose(int how);
3900307b 117 const char *url() const;
118 int checkCachable();
119 int checkNegativeHit() const;
120 int locked() const;
121 int validToSend() const;
9487bae9 122 bool memoryCachable() const; ///< may be cached in memory
3900307b 123 void createMemObject(const char *, const char *);
9487bae9 124 void hideMemObject(); ///< no mem_obj for callers until createMemObject
3900307b 125 void dump(int debug_lvl) const;
126 void hashDelete();
127 void hashInsert(const cache_key *);
128 void registerAbort(STABH * cb, void *);
129 void reset();
130 void setMemStatus(mem_status_t);
131 void timestampsSet();
132 void unregisterAbort();
133 void destroyMemObject();
134 int checkTooSmall();
528b2c61 135
65517dc8 136 void delayAwareRead(int fd, char *buf, int len, AsyncCall::Pointer callback);
43ae1d95 137
a46d2c0e 138 void setNoDelay (bool const);
190154cf 139 bool modifiedSince(HttpRequest * request) const;
79c8035e
AR
140 /// has ETag matching at least one of the If-Match etags
141 bool hasIfMatchEtag(const HttpRequest &request) const;
142 /// has ETag matching at least one of the If-None-Match etags
143 bool hasIfNoneMatchEtag(const HttpRequest &request) const;
63be0a78 144
145 /** What store does this entry belong too ? */
7d3c4ca1 146 virtual RefCount<SwapDir> store() const;
a46d2c0e 147
e6ccf245 148 MemObject *mem_obj;
9487bae9 149 MemObject *hidden_mem_obj; ///< mem_obj created before URLs were known
e6ccf245 150 RemovalPolicyNode repl;
151 /* START OF ON-DISK STORE_META_STD TLV field */
152 time_t timestamp;
153 time_t lastref;
154 time_t expires;
155 time_t lastmod;
47f6e231 156 uint64_t swap_file_sz;
e6ccf245 157 u_short refcount;
158 u_short flags;
159 /* END OF ON-DISK STORE_META_STD */
62e76326 160
d1398b75
HN
161 sfileno swap_filen:25;
162
163 sdirno swap_dirn:7;
62e76326 164
e6ccf245 165 u_short lock_count; /* Assume < 65536! */
62e76326 166
d1398b75 167 mem_status_t mem_status:3;
62e76326 168
d1398b75 169 ping_status_t ping_status:3;
62e76326 170
d1398b75 171 store_status_t store_status:3;
62e76326 172
d1398b75 173 swap_status_t swap_status:3;
62e76326 174
e6ccf245 175public:
176 static size_t inUseCount();
60745f24 177 static void getPublicByRequestMethod(StoreClient * aClient, HttpRequest * request, const HttpRequestMethod& method);
190154cf 178 static void getPublicByRequest(StoreClient * aClient, HttpRequest * request);
60745f24 179 static void getPublic(StoreClient * aClient, const char *uri, const HttpRequestMethod& method);
528b2c61 180
26ac0430 181 virtual bool isNull() {
62e76326 182 return false;
63be0a78 183 };
62e76326 184
35db0b4e 185 void *operator new(size_t byteCount);
e6ccf245 186 void operator delete(void *address);
43ae1d95 187 void setReleaseFlag();
f41735ea 188#if USE_SQUID_ESI
43ae1d95 189
190 ESIElement::Pointer cachedESITree;
191#endif
63be0a78 192 /** append bytes to the buffer */
c21ad0f5 193 virtual void append(char const *, int len);
63be0a78 194 /** disable sending content to the clients */
c21ad0f5 195 virtual void buffer();
63be0a78 196 /** flush any buffered content */
c21ad0f5 197 virtual void flush();
63be0a78 198 /** reduce the memory lock count on the entry */
c21ad0f5 199 virtual int unlock();
63be0a78 200 /** increate the memory lock count on the entry */
47f6e231 201 virtual int64_t objectLen() const;
202 virtual int64_t contentLen() const;
c21ad0f5 203
63be0a78 204 virtual void lock();
5f33b71d 205 virtual void release();
62e76326 206
e6ccf245 207private:
04eb0689 208 static MemAllocator *pool;
528b2c61 209
210 bool validLength() const;
79c8035e 211 bool hasOneOfEtags(const String &reqETags, const bool allowWeakMatch) const;
e6ccf245 212};
213
2c4cd1ad
AR
214std::ostream &operator <<(std::ostream &os, const StoreEntry &e);
215
63be0a78 216/// \ingroup StoreAPI
3b13a8fd 217class NullStoreEntry:public StoreEntry
e6ccf245 218{
62e76326 219
e6ccf245 220public:
221 static NullStoreEntry *getInstance();
26ac0430 222 bool isNull() {
62e76326 223 return true;
e6ccf245 224 }
62e76326 225
332dafa2 226 const char *getMD5Text() const;
528b2c61 227 _SQUID_INLINE_ HttpReply const *getReply() const;
26ac0430 228 void write (StoreIOBuffer) {}
62e76326 229
528b2c61 230 bool isEmpty () const {return true;}
62e76326 231
e1381638 232 virtual size_t bytesWanted(Range<size_t> const aRange) const { assert (aRange.size()); return aRange.end - 1;}
62e76326 233
e6ccf245 234 void operator delete(void *address);
26ac0430 235 void complete() {}
62e76326 236
237private:
26ac0430 238 store_client_t storeClientType() const {return STORE_MEM_CLIENT;}
62e76326 239
528b2c61 240 char const *getSerialisedMetaData();
241 bool swapoutPossible() {return false;}
62e76326 242
528b2c61 243 void trimMemory() {}
244
62e76326 245
e6ccf245 246 static NullStoreEntry _instance;
247};
248
63be0a78 249/// \ingroup StoreAPI
c8f4eac4 250typedef void (*STOREGETCLIENT) (StoreEntry *, void *cbdata);
251
c21ad0f5 252
63be0a78 253/**
254 \ingroup StoreAPI
255 * Abstract base class that will replace the whole store and swapdir interface.
256 */
c8f4eac4 257class Store : public RefCountable
258{
259
260public:
63be0a78 261 /** The root store */
88e13392 262 static _SQUID_INLINE_ Store &Root();
c8f4eac4 263 static void Root(Store *);
264 static void Root(RefCount<Store>);
265 static void Stats(StoreEntry * output);
266 static void Maintain(void *unused);
267
268 virtual ~Store() {}
269
63be0a78 270 /** Handle pending callbacks - called by the event loop. */
c8f4eac4 271 virtual int callback() = 0;
63be0a78 272
273 /** create the resources needed for this store to operate */
c8f4eac4 274 virtual void create();
63be0a78 275
276 /**
277 * Notify this store that its disk is full.
278 \todo XXX move into a protected api call between store files and their stores, rather than a top level api call
c8f4eac4 279 */
280 virtual void diskFull();
c8f4eac4 281
63be0a78 282 /** Retrieve a store entry from the store */
b21767da 283 virtual StoreEntry * get(const cache_key *) = 0;
c8f4eac4 284
63be0a78 285 /** \todo imeplement the async version */
b21767da 286 virtual void get(String const key , STOREGETCLIENT callback, void *cbdata) = 0;
c8f4eac4 287
288 /* prepare the store for use. The store need not be usable immediately,
289 * it should respond to readable() and writable() with true as soon
290 * as it can provide those services
291 */
292 virtual void init() = 0;
293
63be0a78 294 /**
295 * The maximum size the store will support in normal use. Inaccuracy is permitted,
296 * but may throw estimates for memory etc out of whack.
297 */
12e11a5c 298 virtual uint64_t maxSize() const = 0;
c8f4eac4 299
63be0a78 300 /** The minimum size the store will shrink to via normal housekeeping */
12e11a5c 301 virtual uint64_t minSize() const = 0;
c8f4eac4 302
57f583f1
DK
303 /** current store size */
304 virtual uint64_t currentSize() const = 0;
39c1e1d9
DK
305
306 /** the total number of objects stored */
307 virtual uint64_t currentCount() const = 0;
308
af2fda07
DK
309 /** the maximum object size that can be stored, -1 if unlimited */
310 virtual int64_t maxObjectSize() const = 0;
311
63be0a78 312 /**
313 * Output stats to the provided store entry.
314 \todo make these calls asynchronous
315 */
316 virtual void stat(StoreEntry &) const = 0;
c8f4eac4 317
63be0a78 318 /** Sync the store prior to shutdown */
319 virtual void sync();
c8f4eac4 320
63be0a78 321 /** remove a Store entry from the store */
c8f4eac4 322 virtual void unlink (StoreEntry &);
323
324 /* search in the store */
30abd221 325 virtual StoreSearch *search(String const url, HttpRequest *) = 0;
c8f4eac4 326
327 /* pulled up from SwapDir for migration.... probably do not belong here */
328 virtual void reference(StoreEntry &) = 0; /* Reference this object */
329
330 virtual void dereference(StoreEntry &) = 0; /* Unreference this object */
331
332 virtual void maintain() = 0; /* perform regular maintenance should be private and self registered ... */
333
9487bae9
AR
334 // XXX: This method belongs to Store::Root/StoreController, but it is here
335 // because test cases use non-StoreController derivatives as Root
336 /// called when the entry is no longer needed by any transaction
337 virtual void handleIdleEntry(StoreEntry &e) {}
338
c8f4eac4 339private:
340 static RefCount<Store> CurrentRoot;
341};
342
63be0a78 343/// \ingroup StoreAPI
c8f4eac4 344typedef RefCount<Store> StorePointer;
345
63be0a78 346/// \ingroup StoreAPI
43ae1d95 347SQUIDCEXTERN size_t storeEntryInUse();
63be0a78 348
349/// \ingroup StoreAPI
e6ccf245 350SQUIDCEXTERN const char *storeEntryFlags(const StoreEntry *);
63be0a78 351
352/// \ingroup StoreAPI
528b2c61 353extern void storeEntryReplaceObject(StoreEntry *, HttpReply *);
e6ccf245 354
63be0a78 355/// \ingroup StoreAPI
60745f24 356SQUIDCEXTERN StoreEntry *storeGetPublic(const char *uri, const HttpRequestMethod& method);
63be0a78 357
358/// \ingroup StoreAPI
190154cf 359SQUIDCEXTERN StoreEntry *storeGetPublicByRequest(HttpRequest * request);
63be0a78 360
361/// \ingroup StoreAPI
60745f24 362SQUIDCEXTERN StoreEntry *storeGetPublicByRequestMethod(HttpRequest * request, const HttpRequestMethod& method);
63be0a78 363
364/// \ingroup StoreAPI
60745f24 365SQUIDCEXTERN StoreEntry *storeCreateEntry(const char *, const char *, request_flags, const HttpRequestMethod&);
63be0a78 366
367/// \ingroup StoreAPI
e6ccf245 368SQUIDCEXTERN void storeInit(void);
63be0a78 369
63be0a78 370/// \ingroup StoreAPI
e6ccf245 371SQUIDCEXTERN void storeConfigure(void);
63be0a78 372
373/// \ingroup StoreAPI
e6ccf245 374SQUIDCEXTERN void storeFreeMemory(void);
63be0a78 375
376/// \ingroup StoreAPI
e6ccf245 377SQUIDCEXTERN int expiresMoreThan(time_t, time_t);
63be0a78 378
63be0a78 379/// \ingroup StoreAPI
380SQUIDCEXTERN void storeAppendPrintf(StoreEntry *, const char *,...) PRINTF_FORMAT_ARG2;
63be0a78 381
382/// \ingroup StoreAPI
daacd51f 383extern void storeAppendVPrintf(StoreEntry *, const char *, va_list ap);
63be0a78 384
385/// \ingroup StoreAPI
e6ccf245 386SQUIDCEXTERN int storeTooManyDiskFilesOpen(void);
63be0a78 387
e1f7507e 388class SwapDir;
63be0a78 389/// \ingroup StoreAPI
e6ccf245 390SQUIDCEXTERN void storeHeapPositionUpdate(StoreEntry *, SwapDir *);
63be0a78 391
392/// \ingroup StoreAPI
e6ccf245 393SQUIDCEXTERN void storeSwapFileNumberSet(StoreEntry * e, sfileno filn);
63be0a78 394
395/// \ingroup StoreAPI
e6ccf245 396SQUIDCEXTERN void storeFsInit(void);
63be0a78 397
398/// \ingroup StoreAPI
e6ccf245 399SQUIDCEXTERN void storeFsDone(void);
63be0a78 400
401/// \ingroup StoreAPI
e6ccf245 402SQUIDCEXTERN void storeReplAdd(const char *, REMOVALPOLICYCREATE *);
63be0a78 403
404/// \ingroup StoreAPI
c8f4eac4 405extern FREE destroyStoreEntry;
e6ccf245 406
63be0a78 407/**
408 \ingroup StoreAPI
409 \todo should be a subclass of Packer perhaps ?
410 */
25b6a907 411SQUIDCEXTERN void packerToStoreInit(Packer * p, StoreEntry * e);
412
6d3c2758
HN
413/// \ingroup StoreAPI
414SQUIDCEXTERN void storeGetMemSpace(int size);
415
32d002cb 416#if _USE_INLINE_
528b2c61 417#include "Store.cci"
418#endif
419
e6ccf245 420#endif /* SQUID_STORE_H */