]> git.ipfire.org Git - thirdparty/squid.git/blame - src/Store.h
SourceFormat: enforcement
[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
63be0a78 63/**
64 \ingroup StoreAPI
65 */
62e76326 66class StoreEntry : public hash_link
67{
68
332dafa2 69public:
a46d2c0e 70 static DeferredRead::DeferrableRead DeferReader;
71 bool checkDeferRead(int fd) const;
62e76326 72
528b2c61 73 virtual const char *getMD5Text() const;
c8f4eac4 74 StoreEntry();
75 StoreEntry(const char *url, const char *log_url);
26ac0430 76 virtual ~StoreEntry() {}
43ae1d95 77
528b2c61 78 virtual HttpReply const *getReply() const;
79 virtual void write (StoreIOBuffer);
80 virtual _SQUID_INLINE_ bool isEmpty() const;
aa18a4ca 81 virtual bool isAccepting() const;
a46d2c0e 82 virtual size_t bytesWanted(Range<size_t> const) const;
528b2c61 83 virtual void complete();
84 virtual store_client_t storeClientType() const;
85 virtual char const *getSerialisedMetaData();
4a56ee8d 86 virtual void replaceHttpReply(HttpReply *);
528b2c61 87 virtual bool swapoutPossible();
88 virtual void trimMemory();
bfb55b6f 89 void abort();
c8f4eac4 90 void unlink();
5ed72359 91 void makePublic();
92 void makePrivate();
d88e3c49 93 void setPublicKey();
94 void setPrivateKey();
95 void expireNow();
96 void releaseRequest();
97 void negativeCache();
63be0a78 98 void cacheNegatively(); /** \todo argh, why both? */
d88e3c49 99 void invokeHandlers();
100 void purgeMem();
c07cbbf4 101 void swapOut();
102 bool swapOutAble() const;
103 void swapOutFileClose();
3900307b 104 const char *url() const;
105 int checkCachable();
106 int checkNegativeHit() const;
107 int locked() const;
108 int validToSend() const;
109 int keepInMemory() const;
110 void createMemObject(const char *, const char *);
111 void dump(int debug_lvl) const;
112 void hashDelete();
113 void hashInsert(const cache_key *);
114 void registerAbort(STABH * cb, void *);
115 void reset();
116 void setMemStatus(mem_status_t);
117 void timestampsSet();
118 void unregisterAbort();
119 void destroyMemObject();
120 int checkTooSmall();
528b2c61 121
65517dc8 122 void delayAwareRead(int fd, char *buf, int len, AsyncCall::Pointer callback);
43ae1d95 123
a46d2c0e 124 void setNoDelay (bool const);
190154cf 125 bool modifiedSince(HttpRequest * request) const;
63be0a78 126
127 /** What store does this entry belong too ? */
c8f4eac4 128 virtual RefCount<Store> store() const;
a46d2c0e 129
e6ccf245 130 MemObject *mem_obj;
131 RemovalPolicyNode repl;
132 /* START OF ON-DISK STORE_META_STD TLV field */
133 time_t timestamp;
134 time_t lastref;
135 time_t expires;
136 time_t lastmod;
47f6e231 137 uint64_t swap_file_sz;
e6ccf245 138 u_short refcount;
139 u_short flags;
140 /* END OF ON-DISK STORE_META_STD */
62e76326 141
d1398b75
HN
142 sfileno swap_filen:25;
143
144 sdirno swap_dirn:7;
62e76326 145
e6ccf245 146 u_short lock_count; /* Assume < 65536! */
62e76326 147
d1398b75 148 mem_status_t mem_status:3;
62e76326 149
d1398b75 150 ping_status_t ping_status:3;
62e76326 151
d1398b75 152 store_status_t store_status:3;
62e76326 153
d1398b75 154 swap_status_t swap_status:3;
62e76326 155
e6ccf245 156public:
157 static size_t inUseCount();
60745f24 158 static void getPublicByRequestMethod(StoreClient * aClient, HttpRequest * request, const HttpRequestMethod& method);
190154cf 159 static void getPublicByRequest(StoreClient * aClient, HttpRequest * request);
60745f24 160 static void getPublic(StoreClient * aClient, const char *uri, const HttpRequestMethod& method);
528b2c61 161
26ac0430 162 virtual bool isNull() {
62e76326 163 return false;
63be0a78 164 };
62e76326 165
35db0b4e 166 void *operator new(size_t byteCount);
e6ccf245 167 void operator delete(void *address);
43ae1d95 168 void setReleaseFlag();
f41735ea 169#if USE_SQUID_ESI
43ae1d95 170
171 ESIElement::Pointer cachedESITree;
172#endif
63be0a78 173 /** append bytes to the buffer */
c21ad0f5 174 virtual void append(char const *, int len);
63be0a78 175 /** disable sending content to the clients */
c21ad0f5 176 virtual void buffer();
63be0a78 177 /** flush any buffered content */
c21ad0f5 178 virtual void flush();
63be0a78 179 /** reduce the memory lock count on the entry */
c21ad0f5 180 virtual int unlock();
63be0a78 181 /** increate the memory lock count on the entry */
47f6e231 182 virtual int64_t objectLen() const;
183 virtual int64_t contentLen() const;
c21ad0f5 184
63be0a78 185 virtual void lock();
5f33b71d 186 virtual void release();
62e76326 187
e6ccf245 188private:
04eb0689 189 static MemAllocator *pool;
528b2c61 190
191 bool validLength() const;
e6ccf245 192};
193
63be0a78 194/// \ingroup StoreAPI
3b13a8fd 195class NullStoreEntry:public StoreEntry
e6ccf245 196{
62e76326 197
e6ccf245 198public:
199 static NullStoreEntry *getInstance();
26ac0430 200 bool isNull() {
62e76326 201 return true;
e6ccf245 202 }
62e76326 203
332dafa2 204 const char *getMD5Text() const;
528b2c61 205 _SQUID_INLINE_ HttpReply const *getReply() const;
26ac0430 206 void write (StoreIOBuffer) {}
62e76326 207
528b2c61 208 bool isEmpty () const {return true;}
62e76326 209
e1381638 210 virtual size_t bytesWanted(Range<size_t> const aRange) const { assert (aRange.size()); return aRange.end - 1;}
62e76326 211
e6ccf245 212 void operator delete(void *address);
26ac0430 213 void complete() {}
62e76326 214
215private:
26ac0430 216 store_client_t storeClientType() const {return STORE_MEM_CLIENT;}
62e76326 217
528b2c61 218 char const *getSerialisedMetaData();
219 bool swapoutPossible() {return false;}
62e76326 220
528b2c61 221 void trimMemory() {}
222
62e76326 223
e6ccf245 224 static NullStoreEntry _instance;
225};
226
63be0a78 227/// \ingroup StoreAPI
c8f4eac4 228typedef void (*STOREGETCLIENT) (StoreEntry *, void *cbdata);
229
c21ad0f5 230
63be0a78 231/**
232 \ingroup StoreAPI
233 * Abstract base class that will replace the whole store and swapdir interface.
234 */
c8f4eac4 235class Store : public RefCountable
236{
237
238public:
63be0a78 239 /** The root store */
88e13392 240 static _SQUID_INLINE_ Store &Root();
c8f4eac4 241 static void Root(Store *);
242 static void Root(RefCount<Store>);
243 static void Stats(StoreEntry * output);
244 static void Maintain(void *unused);
245
246 virtual ~Store() {}
247
63be0a78 248 /** Handle pending callbacks - called by the event loop. */
c8f4eac4 249 virtual int callback() = 0;
63be0a78 250
251 /** create the resources needed for this store to operate */
c8f4eac4 252 virtual void create();
63be0a78 253
254 /**
255 * Notify this store that its disk is full.
256 \todo XXX move into a protected api call between store files and their stores, rather than a top level api call
c8f4eac4 257 */
258 virtual void diskFull();
c8f4eac4 259
63be0a78 260 /** Retrieve a store entry from the store */
b21767da 261 virtual StoreEntry * get(const cache_key *) = 0;
c8f4eac4 262
63be0a78 263 /** \todo imeplement the async version */
b21767da 264 virtual void get(String const key , STOREGETCLIENT callback, void *cbdata) = 0;
c8f4eac4 265
266 /* prepare the store for use. The store need not be usable immediately,
267 * it should respond to readable() and writable() with true as soon
268 * as it can provide those services
269 */
270 virtual void init() = 0;
271
63be0a78 272 /**
273 * The maximum size the store will support in normal use. Inaccuracy is permitted,
274 * but may throw estimates for memory etc out of whack.
275 */
c8f4eac4 276 virtual size_t maxSize() const = 0;
277
63be0a78 278 /** The minimum size the store will shrink to via normal housekeeping */
c8f4eac4 279 virtual size_t minSize() const = 0;
280
63be0a78 281 /**
282 * Output stats to the provided store entry.
283 \todo make these calls asynchronous
284 */
285 virtual void stat(StoreEntry &) const = 0;
c8f4eac4 286
63be0a78 287 /** Sync the store prior to shutdown */
288 virtual void sync();
c8f4eac4 289
63be0a78 290 /** remove a Store entry from the store */
c8f4eac4 291 virtual void unlink (StoreEntry &);
292
293 /* search in the store */
30abd221 294 virtual StoreSearch *search(String const url, HttpRequest *) = 0;
c8f4eac4 295
296 /* pulled up from SwapDir for migration.... probably do not belong here */
297 virtual void reference(StoreEntry &) = 0; /* Reference this object */
298
299 virtual void dereference(StoreEntry &) = 0; /* Unreference this object */
300
301 virtual void maintain() = 0; /* perform regular maintenance should be private and self registered ... */
302
303 /* These should really be private */
47f6e231 304 virtual void updateSize(int64_t size, int sign) = 0;
c8f4eac4 305
306private:
307 static RefCount<Store> CurrentRoot;
308};
309
63be0a78 310/// \ingroup StoreAPI
c8f4eac4 311typedef RefCount<Store> StorePointer;
312
63be0a78 313/// \ingroup StoreAPI
43ae1d95 314SQUIDCEXTERN size_t storeEntryInUse();
63be0a78 315
316/// \ingroup StoreAPI
e6ccf245 317SQUIDCEXTERN const char *storeEntryFlags(const StoreEntry *);
63be0a78 318
319/// \ingroup StoreAPI
528b2c61 320extern void storeEntryReplaceObject(StoreEntry *, HttpReply *);
e6ccf245 321
63be0a78 322/// \ingroup StoreAPI
60745f24 323SQUIDCEXTERN StoreEntry *storeGetPublic(const char *uri, const HttpRequestMethod& method);
63be0a78 324
325/// \ingroup StoreAPI
190154cf 326SQUIDCEXTERN StoreEntry *storeGetPublicByRequest(HttpRequest * request);
63be0a78 327
328/// \ingroup StoreAPI
60745f24 329SQUIDCEXTERN StoreEntry *storeGetPublicByRequestMethod(HttpRequest * request, const HttpRequestMethod& method);
63be0a78 330
331/// \ingroup StoreAPI
60745f24 332SQUIDCEXTERN StoreEntry *storeCreateEntry(const char *, const char *, request_flags, const HttpRequestMethod&);
63be0a78 333
334/// \ingroup StoreAPI
e6ccf245 335SQUIDCEXTERN void storeInit(void);
63be0a78 336
63be0a78 337/// \ingroup StoreAPI
e6ccf245 338SQUIDCEXTERN void storeConfigure(void);
63be0a78 339
340/// \ingroup StoreAPI
e6ccf245 341SQUIDCEXTERN void storeFreeMemory(void);
63be0a78 342
343/// \ingroup StoreAPI
e6ccf245 344SQUIDCEXTERN int expiresMoreThan(time_t, time_t);
63be0a78 345
63be0a78 346/// \ingroup StoreAPI
347SQUIDCEXTERN void storeAppendPrintf(StoreEntry *, const char *,...) PRINTF_FORMAT_ARG2;
63be0a78 348
349/// \ingroup StoreAPI
e6ccf245 350SQUIDCEXTERN void storeAppendVPrintf(StoreEntry *, const char *, va_list ap);
63be0a78 351
352/// \ingroup StoreAPI
e6ccf245 353SQUIDCEXTERN int storeTooManyDiskFilesOpen(void);
63be0a78 354
e1f7507e 355class SwapDir;
63be0a78 356/// \ingroup StoreAPI
e6ccf245 357SQUIDCEXTERN void storeHeapPositionUpdate(StoreEntry *, SwapDir *);
63be0a78 358
359/// \ingroup StoreAPI
e6ccf245 360SQUIDCEXTERN void storeSwapFileNumberSet(StoreEntry * e, sfileno filn);
63be0a78 361
362/// \ingroup StoreAPI
e6ccf245 363SQUIDCEXTERN void storeFsInit(void);
63be0a78 364
365/// \ingroup StoreAPI
e6ccf245 366SQUIDCEXTERN void storeFsDone(void);
63be0a78 367
368/// \ingroup StoreAPI
e6ccf245 369SQUIDCEXTERN void storeReplAdd(const char *, REMOVALPOLICYCREATE *);
63be0a78 370
371/// \ingroup StoreAPI
c8f4eac4 372extern FREE destroyStoreEntry;
e6ccf245 373
63be0a78 374/**
375 \ingroup StoreAPI
376 \todo should be a subclass of Packer perhaps ?
377 */
25b6a907 378SQUIDCEXTERN void packerToStoreInit(Packer * p, StoreEntry * e);
379
6d3c2758
HN
380/// \ingroup StoreAPI
381SQUIDCEXTERN void storeGetMemSpace(int size);
382
528b2c61 383#ifdef _USE_INLINE_
384#include "Store.cci"
385#endif
386
e6ccf245 387#endif /* SQUID_STORE_H */