2 * $Id: Store.h,v 1.44 2008/02/26 21:49:34 amosjeffries Exp $
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
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.
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.
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.
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.
36 \defgroup StoreAPI Store API
42 /// \todo protect the 'ostream' file includes via configure file tests as per squid conventions.
45 #include "StoreIOBuffer.h"
50 #include "RemovalPolicy.h"
53 #include "ESIElement.h"
62 typedef unsigned int ping_status_t
;
67 class StoreEntry
: public hash_link
71 static DeferredRead::DeferrableRead DeferReader
;
72 bool checkDeferRead(int fd
) const;
74 virtual const char *getMD5Text() const;
76 StoreEntry(const char *url
, const char *log_url
);
77 virtual ~StoreEntry(){}
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();
97 void releaseRequest();
99 void cacheNegatively(); /** \todo argh, why both? */
100 void invokeHandlers();
103 bool swapOutAble() const;
104 void swapOutFileClose();
105 const char *url() const;
107 int checkNegativeHit() const;
109 int validToSend() const;
110 int keepInMemory() const;
111 void createMemObject(const char *, const char *);
112 void dump(int debug_lvl
) const;
114 void hashInsert(const cache_key
*);
115 void registerAbort(STABH
* cb
, void *);
117 void setMemStatus(mem_status_t
);
118 void timestampsSet();
119 void unregisterAbort();
120 void destroyMemObject();
123 void delayAwareRead(int fd
, char *buf
, int len
, AsyncCall::Pointer callback
);
125 void setNoDelay (bool const);
126 bool modifiedSince(HttpRequest
* request
) const;
128 /** What store does this entry belong too ? */
129 virtual RefCount
<Store
> store() const;
132 RemovalPolicyNode repl
;
133 /* START OF ON-DISK STORE_META_STD TLV field */
138 uint64_t swap_file_sz
;
141 /* END OF ON-DISK STORE_META_STD */
143 sfileno swap_filen
:25;
146 u_short lock_count
; /* Assume < 65536! */
148 mem_status_t mem_status
:3;
150 ping_status_t ping_status
:3;
152 store_status_t store_status
:3;
154 swap_status_t swap_status
:3;
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
);
162 virtual bool isNull()
167 void *operator new(size_t byteCount
);
168 void operator delete(void *address
);
169 void setReleaseFlag();
172 ESIElement::Pointer cachedESITree
;
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;
187 virtual void release();
190 static MemAllocator
*pool
;
192 bool validLength() const;
195 /// \ingroup StoreAPI
196 class NullStoreEntry
:public StoreEntry
200 static NullStoreEntry
*getInstance();
206 const char *getMD5Text() const;
207 _SQUID_INLINE_ HttpReply
const *getReply() const;
208 void write (StoreIOBuffer
){}
210 bool isEmpty () const {return true;}
212 virtual size_t bytesWanted(Range
<size_t> const aRange
) const { assert (aRange
.size());return aRange
.end
- 1;}
214 void operator delete(void *address
);
218 store_client_t
storeClientType() const{return STORE_MEM_CLIENT
;}
220 char const *getSerialisedMetaData();
221 bool swapoutPossible() {return false;}
226 static NullStoreEntry _instance
;
229 /// \ingroup StoreAPI
230 typedef void (*STOREGETCLIENT
) (StoreEntry
*, void *cbdata
);
235 * Abstract base class that will replace the whole store and swapdir interface.
237 class Store
: public RefCountable
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
);
250 /** Handle pending callbacks - called by the event loop. */
251 virtual int callback() = 0;
253 /** create the resources needed for this store to operate */
254 virtual void create();
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
260 virtual void diskFull();
262 /** Retrieve a store entry from the store */
263 virtual StoreEntry
* get
264 (const cache_key
*) = 0;
266 /** \todo imeplement the async version */
268 (String
const key
, STOREGETCLIENT callback
, void *cbdata
) = 0;
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
274 virtual void init() = 0;
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.
280 virtual size_t maxSize() const = 0;
282 /** The minimum size the store will shrink to via normal housekeeping */
283 virtual size_t minSize() const = 0;
286 * Output stats to the provided store entry.
287 \todo make these calls asynchronous
289 virtual void stat(StoreEntry
&) const = 0;
291 /** Sync the store prior to shutdown */
294 /** remove a Store entry from the store */
295 virtual void unlink (StoreEntry
&);
297 /* search in the store */
298 virtual StoreSearch
*search(String
const url
, HttpRequest
*) = 0;
300 /* pulled up from SwapDir for migration.... probably do not belong here */
301 virtual void reference(StoreEntry
&) = 0; /* Reference this object */
303 virtual void dereference(StoreEntry
&) = 0; /* Unreference this object */
305 virtual void maintain() = 0; /* perform regular maintenance should be private and self registered ... */
307 /* These should really be private */
308 virtual void updateSize(int64_t size
, int sign
) = 0;
311 static RefCount
<Store
> CurrentRoot
;
314 /// \ingroup StoreAPI
315 typedef RefCount
<Store
> StorePointer
;
317 /// \ingroup StoreAPI
318 SQUIDCEXTERN
size_t storeEntryInUse();
320 /// \ingroup StoreAPI
321 SQUIDCEXTERN
const char *storeEntryFlags(const StoreEntry
*);
323 /// \ingroup StoreAPI
324 extern void storeEntryReplaceObject(StoreEntry
*, HttpReply
*);
326 /// \ingroup StoreAPI
327 SQUIDCEXTERN StoreEntry
*storeGetPublic(const char *uri
, const HttpRequestMethod
& method
);
329 /// \ingroup StoreAPI
330 SQUIDCEXTERN StoreEntry
*storeGetPublicByRequest(HttpRequest
* request
);
332 /// \ingroup StoreAPI
333 SQUIDCEXTERN StoreEntry
*storeGetPublicByRequestMethod(HttpRequest
* request
, const HttpRequestMethod
& method
);
335 /// \ingroup StoreAPI
336 SQUIDCEXTERN StoreEntry
*storeCreateEntry(const char *, const char *, request_flags
, const HttpRequestMethod
&);
338 /// \ingroup StoreAPI
339 SQUIDCEXTERN
void storeInit(void);
341 /// \ingroup StoreAPI
342 extern void storeRegisterWithCacheManager(void);
344 /// \ingroup StoreAPI
345 SQUIDCEXTERN
void storeConfigure(void);
347 /// \ingroup StoreAPI
348 SQUIDCEXTERN
void storeFreeMemory(void);
350 /// \ingroup StoreAPI
351 SQUIDCEXTERN
int expiresMoreThan(time_t, time_t);
354 /// \ingroup StoreAPI
355 SQUIDCEXTERN
void storeAppendPrintf(StoreEntry
*, const char *,...) PRINTF_FORMAT_ARG2
;
357 /// \ingroup StoreAPI
358 SQUIDCEXTERN
void storeAppendPrintf();
361 /// \ingroup StoreAPI
362 SQUIDCEXTERN
void storeAppendVPrintf(StoreEntry
*, const char *, va_list ap
);
364 /// \ingroup StoreAPI
365 SQUIDCEXTERN
int storeTooManyDiskFilesOpen(void);
368 /// \ingroup StoreAPI
369 SQUIDCEXTERN
void storeHeapPositionUpdate(StoreEntry
*, SwapDir
*);
371 /// \ingroup StoreAPI
372 SQUIDCEXTERN
void storeSwapFileNumberSet(StoreEntry
* e
, sfileno filn
);
374 /// \ingroup StoreAPI
375 SQUIDCEXTERN
void storeFsInit(void);
377 /// \ingroup StoreAPI
378 SQUIDCEXTERN
void storeFsDone(void);
380 /// \ingroup StoreAPI
381 SQUIDCEXTERN
void storeReplAdd(const char *, REMOVALPOLICYCREATE
*);
383 /// \ingroup StoreAPI
384 extern FREE destroyStoreEntry
;
388 \todo should be a subclass of Packer perhaps ?
390 SQUIDCEXTERN
void packerToStoreInit(Packer
* p
, StoreEntry
* e
);
396 #endif /* SQUID_STORE_H */