Strictly speaking, WvPam is not a stream. So move wvpam.cc to utils.
[wvapps.git] / funfs / wvdiskcache.h
blobebcacfdcd37260d0e0c57bea87e89cb482c30048
1 /*
2 * Worldvisions Weaver Software:
3 * Copyright (C) 1997-2003 Net Integration Technologies, Inc.
5 * WvDiskCache uses GDBM hash to help keep track of the least recently used
6 * files in an on-disk cache of file with an enforced maximum size.
7 */
9 #ifndef __WVDISKCACHE_H
10 #define __WVDISKCACHE_H
12 #include <wvautoconf.h>
14 #ifdef WITH_GDBM
16 #include <wvcallback.h>
17 #include <wvstring.h>
18 #include <wvgdbmhash.h>
19 #include <wvbuf.h>
21 struct DCFileInfo
23 DCFileInfo() {};
24 DCFileInfo(size_t _size, WvStringParm _next, WvStringParm _prev)
25 : size(_size), next(_next), prev(_prev) {};
26 DCFileInfo(const DCFileInfo &other)
28 size = other.size;
29 next = other.next;
30 prev = other.prev;
32 size_t size;
33 WvString next;
34 WvString prev;
38 template <>
39 class DatumAdapter<DCFileInfo> : public DatumAdapterBase
41 public:
42 typedef DCFileInfo ReturnType;
43 operator DCFileInfo ()
44 { return convert(mydatum); }
45 static ReturnType convert(datum convertme)
47 WvInPlaceBuf buf(convertme.dptr, convertme.dsize, convertme.dsize);
48 DCFileInfo fileinfo;
49 fileinfo.size = atoi(buf.getstr(buf.strchr('\0')));
50 fileinfo.next = buf.getstr(buf.strchr('\0'));
51 fileinfo.prev = buf.getstr(buf.strchr('\0'));
52 return fileinfo;
54 DatumAdapter(DCFileInfo fileinfo)
56 dynbuf.zap();
57 dynbuf.putstr(fileinfo.size); dynbuf.putch('\0');
58 dynbuf.putstr(fileinfo.next); dynbuf.putch('\0');
59 dynbuf.putstr(fileinfo.prev); dynbuf.putch('\0');
60 mydatum.dsize = dynbuf.used();
61 mydatum.dptr = (char *)dynbuf.get(dynbuf.used());
63 DatumAdapter(const DatumAdapter<DCFileInfo> &other)
65 // I don't know what's going on here....
66 // My theory is that gcc 3.x is crazy.
67 // Where is my implicit conversion to a base class?
68 dynbuf.merge(*((WvDynBufBase<unsigned char> *)&other.dynbuf),
69 other.dynbuf.used());
71 private:
72 WvDynBuf dynbuf;
76 typedef WvCallback<void, WvStringParm> DiskCacheCallback;
78 class WvDiskCache
80 public:
81 WvDiskCache(WvStringParm hashpath, int megabytes);
82 void add(WvStringParm path, size_t size);
83 void remove(WvString path);
84 void set_remove_callback(const DiskCacheCallback &_callback)
85 { callback = _callback; }
86 void set_max(int megabytes);
87 int current_size() { return cur_size / 1024; };
88 int count() { return xcount; };
89 protected:
90 void purge();
91 size_t max_size, cur_size;
92 int xcount;
93 WvString head, tail;
94 DiskCacheCallback callback;
95 WvGdbmHash<WvString, DCFileInfo> backend;
98 #endif
100 #endif