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