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
41 #include "StoreIOBuffer.h"
45 #include "comm/forward.h"
47 #include "RemovalPolicy.h"
50 #include "esi/Element.h"
74 extern StoreIoStats store_io_stats
;
76 /// maximum number of entries per cache_dir
77 enum { SwapFilenMax
= 0xFFFFFF }; // keep in sync with StoreEntry::swap_filen
82 class StoreEntry
: public hash_link
86 static DeferredRead::DeferrableRead DeferReader
;
87 bool checkDeferRead(int fd
) const;
89 virtual const char *getMD5Text() const;
91 StoreEntry(const char *url
, const char *log_url
);
92 virtual ~StoreEntry();
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();
111 void setPrivateKey();
113 void releaseRequest();
114 void negativeCache();
115 void cacheNegatively(); /** \todo argh, why both? */
116 void invokeHandlers();
118 void cacheInMemory(); ///< start or continue storing in memory cache
120 bool swapOutAble() const;
121 void swapOutFileClose(int how
);
122 const char *url() const;
124 int checkNegativeHit() 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;
132 void hashInsert(const cache_key
*);
133 void registerAbort(STABH
* cb
, void *);
135 void setMemStatus(mem_status_t
);
136 void timestampsSet();
137 void unregisterAbort();
138 void destroyMemObject();
141 void delayAwareRead(const Comm::ConnectionPointer
&conn
, char *buf
, int len
, AsyncCall::Pointer callback
);
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;
150 /** What store does this entry belong too ? */
151 virtual RefCount
<SwapDir
> store() const;
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 */
161 uint64_t swap_file_sz
;
164 /* END OF ON-DISK STORE_META_STD */
166 /// unique ID inside a cache_dir for swapped out entries; -1 for others
167 sfileno swap_filen
:25; // keep in sync with SwapFilenMax
171 unsigned short lock_count
; /* Assume < 65536! */
173 mem_status_t mem_status
:3;
175 ping_status_t ping_status
:3;
177 store_status_t store_status
:3;
179 swap_status_t swap_status
:3;
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
);
187 virtual bool isNull() {
191 void *operator new(size_t byteCount
);
192 void operator delete(void *address
);
193 void setReleaseFlag();
196 ESIElement::Pointer cachedESITree
;
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;
211 virtual void release();
214 static MemAllocator
*pool
;
216 bool validLength() const;
217 bool hasOneOfEtags(const String
&reqETags
, const bool allowWeakMatch
) const;
220 std::ostream
&operator <<(std::ostream
&os
, const StoreEntry
&e
);
222 /// \ingroup StoreAPI
223 class NullStoreEntry
:public StoreEntry
227 static NullStoreEntry
*getInstance();
232 const char *getMD5Text() const;
233 _SQUID_INLINE_ HttpReply
const *getReply() const;
234 void write (StoreIOBuffer
) {}
236 bool isEmpty () const {return true;}
238 virtual size_t bytesWanted(Range
<size_t> const aRange
) const { assert (aRange
.size()); return aRange
.end
- 1;}
240 void operator delete(void *address
);
244 store_client_t
storeClientType() const {return STORE_MEM_CLIENT
;}
246 char const *getSerialisedMetaData();
247 bool swapoutPossible() {return false;}
252 static NullStoreEntry _instance
;
255 /// \ingroup StoreAPI
256 typedef void (*STOREGETCLIENT
) (StoreEntry
*, void *cbdata
);
261 * Abstract base class that will replace the whole store and swapdir interface.
263 class Store
: public RefCountable
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
);
276 /** Handle pending callbacks - called by the event loop. */
277 virtual int callback() = 0;
279 /** create the resources needed for this store to operate */
280 virtual void create();
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
286 virtual void diskFull();
288 /** Retrieve a store entry from the store */
289 virtual StoreEntry
* get(const cache_key
*) = 0;
291 /** \todo imeplement the async version */
292 virtual void get(String
const key
, STOREGETCLIENT callback
, void *cbdata
) = 0;
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
298 virtual void init() = 0;
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.
304 virtual uint64_t maxSize() const = 0;
306 /** The minimum size the store will shrink to via normal housekeeping */
307 virtual uint64_t minSize() const = 0;
309 /** current store size */
310 virtual uint64_t currentSize() const = 0;
312 /** the total number of objects stored */
313 virtual uint64_t currentCount() const = 0;
315 /** the maximum object size that can be stored, -1 if unlimited */
316 virtual int64_t maxObjectSize() const = 0;
319 * Output stats to the provided store entry.
320 \todo make these calls asynchronous
322 virtual void stat(StoreEntry
&) const = 0;
324 /** Sync the store prior to shutdown */
327 /** remove a Store entry from the store */
328 virtual void unlink (StoreEntry
&);
330 /* search in the store */
331 virtual StoreSearch
*search(String
const url
, HttpRequest
*) = 0;
333 /* pulled up from SwapDir for migration.... probably do not belong here */
334 virtual void reference(StoreEntry
&) = 0; /* Reference this object */
336 /// Undo reference(), returning false iff idle e should be destroyed
337 virtual bool dereference(StoreEntry
&e
) = 0;
339 virtual void maintain() = 0; /* perform regular maintenance should be private and self registered ... */
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
) {}
347 static RefCount
<Store
> CurrentRoot
;
350 /// \ingroup StoreAPI
351 typedef RefCount
<Store
> StorePointer
;
353 /// \ingroup StoreAPI
354 SQUIDCEXTERN
size_t storeEntryInUse();
356 /// \ingroup StoreAPI
357 SQUIDCEXTERN
const char *storeEntryFlags(const StoreEntry
*);
359 /// \ingroup StoreAPI
360 extern void storeEntryReplaceObject(StoreEntry
*, HttpReply
*);
362 /// \ingroup StoreAPI
363 SQUIDCEXTERN StoreEntry
*storeGetPublic(const char *uri
, const HttpRequestMethod
& method
);
365 /// \ingroup StoreAPI
366 SQUIDCEXTERN StoreEntry
*storeGetPublicByRequest(HttpRequest
* request
);
368 /// \ingroup StoreAPI
369 SQUIDCEXTERN StoreEntry
*storeGetPublicByRequestMethod(HttpRequest
* request
, const HttpRequestMethod
& method
);
371 /// \ingroup StoreAPI
372 SQUIDCEXTERN StoreEntry
*storeCreateEntry(const char *, const char *, request_flags
, const HttpRequestMethod
&);
374 /// \ingroup StoreAPI
375 SQUIDCEXTERN
void storeInit(void);
377 /// \ingroup StoreAPI
378 SQUIDCEXTERN
void storeConfigure(void);
380 /// \ingroup StoreAPI
381 SQUIDCEXTERN
void storeFreeMemory(void);
383 /// \ingroup StoreAPI
384 SQUIDCEXTERN
int expiresMoreThan(time_t, time_t);
386 /// \ingroup StoreAPI
387 SQUIDCEXTERN
void storeAppendPrintf(StoreEntry
*, const char *,...) PRINTF_FORMAT_ARG2
;
389 /// \ingroup StoreAPI
390 extern void storeAppendVPrintf(StoreEntry
*, const char *, va_list ap
);
392 /// \ingroup StoreAPI
393 SQUIDCEXTERN
int storeTooManyDiskFilesOpen(void);
396 /// \ingroup StoreAPI
397 SQUIDCEXTERN
void storeHeapPositionUpdate(StoreEntry
*, SwapDir
*);
399 /// \ingroup StoreAPI
400 SQUIDCEXTERN
void storeSwapFileNumberSet(StoreEntry
* e
, sfileno filn
);
402 /// \ingroup StoreAPI
403 SQUIDCEXTERN
void storeFsInit(void);
405 /// \ingroup StoreAPI
406 SQUIDCEXTERN
void storeFsDone(void);
408 /// \ingroup StoreAPI
409 SQUIDCEXTERN
void storeReplAdd(const char *, REMOVALPOLICYCREATE
*);
411 /// \ingroup StoreAPI
412 extern FREE destroyStoreEntry
;
416 \todo should be a subclass of Packer perhaps ?
418 SQUIDCEXTERN
void packerToStoreInit(Packer
* p
, StoreEntry
* e
);
420 /// \ingroup StoreAPI
421 SQUIDCEXTERN
void storeGetMemSpace(int size
);
427 #endif /* SQUID_STORE_H */