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.
37 \defgroup StoreAPI Store API
43 /// \todo protect the 'ostream' file includes via configure file tests as per squid conventions.
46 #include "StoreIOBuffer.h"
51 #include "RemovalPolicy.h"
54 #include "ESIElement.h"
70 class StoreEntry
: public hash_link
74 static DeferredRead::DeferrableRead DeferReader
;
75 bool checkDeferRead(int fd
) const;
77 virtual const char *getMD5Text() const;
79 StoreEntry(const char *url
, const char *log_url
);
80 virtual ~StoreEntry(){}
82 virtual HttpReply
const *getReply() const;
83 virtual void write (StoreIOBuffer
);
84 virtual _SQUID_INLINE_
bool isEmpty() const;
85 virtual bool isAccepting() const;
86 virtual size_t bytesWanted(Range
<size_t> const) const;
87 virtual void complete();
88 virtual store_client_t
storeClientType() const;
89 virtual char const *getSerialisedMetaData();
90 virtual void replaceHttpReply(HttpReply
*);
91 virtual bool swapoutPossible();
92 virtual void trimMemory();
100 void releaseRequest();
101 void negativeCache();
102 void cacheNegatively(); /** \todo argh, why both? */
103 void invokeHandlers();
106 bool swapOutAble() const;
107 void swapOutFileClose();
108 const char *url() const;
110 int checkNegativeHit() const;
112 int validToSend() const;
113 int keepInMemory() const;
114 void createMemObject(const char *, const char *);
115 void dump(int debug_lvl
) const;
117 void hashInsert(const cache_key
*);
118 void registerAbort(STABH
* cb
, void *);
120 void setMemStatus(mem_status_t
);
121 void timestampsSet();
122 void unregisterAbort();
123 void destroyMemObject();
126 void delayAwareRead(int fd
, char *buf
, int len
, AsyncCall::Pointer callback
);
128 void setNoDelay (bool const);
129 bool modifiedSince(HttpRequest
* request
) const;
131 /** What store does this entry belong too ? */
132 virtual RefCount
<Store
> store() const;
135 RemovalPolicyNode repl
;
136 /* START OF ON-DISK STORE_META_STD TLV field */
141 uint64_t swap_file_sz
;
144 /* END OF ON-DISK STORE_META_STD */
151 u_short lock_count
; /* Assume < 65536! */
153 mem_status_t mem_status
:
156 ping_status_t ping_status
:
159 store_status_t store_status
:
162 swap_status_t swap_status
:
166 static size_t inUseCount();
167 static void getPublicByRequestMethod(StoreClient
* aClient
, HttpRequest
* request
, const HttpRequestMethod
& method
);
168 static void getPublicByRequest(StoreClient
* aClient
, HttpRequest
* request
);
169 static void getPublic(StoreClient
* aClient
, const char *uri
, const HttpRequestMethod
& method
);
171 virtual bool isNull()
176 void *operator new(size_t byteCount
);
177 void operator delete(void *address
);
178 void setReleaseFlag();
181 ESIElement::Pointer cachedESITree
;
183 /** append bytes to the buffer */
184 virtual void append(char const *, int len
);
185 /** disable sending content to the clients */
186 virtual void buffer();
187 /** flush any buffered content */
188 virtual void flush();
189 /** reduce the memory lock count on the entry */
190 virtual int unlock();
191 /** increate the memory lock count on the entry */
192 virtual int64_t objectLen() const;
193 virtual int64_t contentLen() const;
196 virtual void release();
199 static MemAllocator
*pool
;
201 bool validLength() const;
204 /// \ingroup StoreAPI
205 class NullStoreEntry
:public StoreEntry
209 static NullStoreEntry
*getInstance();
215 const char *getMD5Text() const;
216 _SQUID_INLINE_ HttpReply
const *getReply() const;
217 void write (StoreIOBuffer
){}
219 bool isEmpty () const {return true;}
221 virtual size_t bytesWanted(Range
<size_t> const aRange
) const { assert (aRange
.size());return aRange
.end
- 1;}
223 void operator delete(void *address
);
227 store_client_t
storeClientType() const{return STORE_MEM_CLIENT
;}
229 char const *getSerialisedMetaData();
230 bool swapoutPossible() {return false;}
235 static NullStoreEntry _instance
;
238 /// \ingroup StoreAPI
239 typedef void (*STOREGETCLIENT
) (StoreEntry
*, void *cbdata
);
244 * Abstract base class that will replace the whole store and swapdir interface.
246 class Store
: public RefCountable
250 /** The root store */
251 static _SQUID_INLINE_ Store
&Root();
252 static void Root(Store
*);
253 static void Root(RefCount
<Store
>);
254 static void Stats(StoreEntry
* output
);
255 static void Maintain(void *unused
);
259 /** Handle pending callbacks - called by the event loop. */
260 virtual int callback() = 0;
262 /** create the resources needed for this store to operate */
263 virtual void create();
266 * Notify this store that its disk is full.
267 \todo XXX move into a protected api call between store files and their stores, rather than a top level api call
269 virtual void diskFull();
271 /** Retrieve a store entry from the store */
272 virtual StoreEntry
* get
273 (const cache_key
*) = 0;
275 /** \todo imeplement the async version */
277 (String
const key
, STOREGETCLIENT callback
, void *cbdata
) = 0;
279 /* prepare the store for use. The store need not be usable immediately,
280 * it should respond to readable() and writable() with true as soon
281 * as it can provide those services
283 virtual void init() = 0;
286 * The maximum size the store will support in normal use. Inaccuracy is permitted,
287 * but may throw estimates for memory etc out of whack.
289 virtual size_t maxSize() const = 0;
291 /** The minimum size the store will shrink to via normal housekeeping */
292 virtual size_t minSize() const = 0;
295 * Output stats to the provided store entry.
296 \todo make these calls asynchronous
298 virtual void stat(StoreEntry
&) const = 0;
300 /** Sync the store prior to shutdown */
303 /** remove a Store entry from the store */
304 virtual void unlink (StoreEntry
&);
306 /* search in the store */
307 virtual StoreSearch
*search(String
const url
, HttpRequest
*) = 0;
309 /* pulled up from SwapDir for migration.... probably do not belong here */
310 virtual void reference(StoreEntry
&) = 0; /* Reference this object */
312 virtual void dereference(StoreEntry
&) = 0; /* Unreference this object */
314 virtual void maintain() = 0; /* perform regular maintenance should be private and self registered ... */
316 /* These should really be private */
317 virtual void updateSize(int64_t size
, int sign
) = 0;
320 static RefCount
<Store
> CurrentRoot
;
323 /// \ingroup StoreAPI
324 typedef RefCount
<Store
> StorePointer
;
326 /// \ingroup StoreAPI
327 SQUIDCEXTERN
size_t storeEntryInUse();
329 /// \ingroup StoreAPI
330 SQUIDCEXTERN
const char *storeEntryFlags(const StoreEntry
*);
332 /// \ingroup StoreAPI
333 extern void storeEntryReplaceObject(StoreEntry
*, HttpReply
*);
335 /// \ingroup StoreAPI
336 SQUIDCEXTERN StoreEntry
*storeGetPublic(const char *uri
, const HttpRequestMethod
& method
);
338 /// \ingroup StoreAPI
339 SQUIDCEXTERN StoreEntry
*storeGetPublicByRequest(HttpRequest
* request
);
341 /// \ingroup StoreAPI
342 SQUIDCEXTERN StoreEntry
*storeGetPublicByRequestMethod(HttpRequest
* request
, const HttpRequestMethod
& method
);
344 /// \ingroup StoreAPI
345 SQUIDCEXTERN StoreEntry
*storeCreateEntry(const char *, const char *, request_flags
, const HttpRequestMethod
&);
347 /// \ingroup StoreAPI
348 SQUIDCEXTERN
void storeInit(void);
350 /// \ingroup StoreAPI
351 extern void storeRegisterWithCacheManager(CacheManager
& manager
);
353 /// \ingroup StoreAPI
354 SQUIDCEXTERN
void storeConfigure(void);
356 /// \ingroup StoreAPI
357 SQUIDCEXTERN
void storeFreeMemory(void);
359 /// \ingroup StoreAPI
360 SQUIDCEXTERN
int expiresMoreThan(time_t, time_t);
363 /// \ingroup StoreAPI
364 SQUIDCEXTERN
void storeAppendPrintf(StoreEntry
*, const char *,...) PRINTF_FORMAT_ARG2
;
366 /// \ingroup StoreAPI
367 SQUIDCEXTERN
void storeAppendPrintf();
370 /// \ingroup StoreAPI
371 SQUIDCEXTERN
void storeAppendVPrintf(StoreEntry
*, const char *, va_list ap
);
373 /// \ingroup StoreAPI
374 SQUIDCEXTERN
int storeTooManyDiskFilesOpen(void);
376 /// \ingroup StoreAPI
377 SQUIDCEXTERN
void storeHeapPositionUpdate(StoreEntry
*, SwapDir
*);
379 /// \ingroup StoreAPI
380 SQUIDCEXTERN
void storeSwapFileNumberSet(StoreEntry
* e
, sfileno filn
);
382 /// \ingroup StoreAPI
383 SQUIDCEXTERN
void storeFsInit(void);
385 /// \ingroup StoreAPI
386 SQUIDCEXTERN
void storeFsDone(void);
388 /// \ingroup StoreAPI
389 SQUIDCEXTERN
void storeReplAdd(const char *, REMOVALPOLICYCREATE
*);
391 /// \ingroup StoreAPI
392 extern FREE destroyStoreEntry
;
396 \todo should be a subclass of Packer perhaps ?
398 SQUIDCEXTERN
void packerToStoreInit(Packer
* p
, StoreEntry
* e
);
404 #endif /* SQUID_STORE_H */