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