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