]>
Commit | Line | Data |
---|---|---|
1 | // -*- mode: cpp; mode: fold -*- | |
2 | // Description /*{{{*/ | |
3 | // $Id: cachedb.h,v 1.4 2004/05/08 19:41:01 mdz Exp $ | |
4 | /* ###################################################################### | |
5 | ||
6 | CacheDB | |
7 | ||
8 | Simple uniform interface to a cache database. | |
9 | ||
10 | ##################################################################### */ | |
11 | /*}}}*/ | |
12 | #ifndef CACHEDB_H | |
13 | #define CACHEDB_H | |
14 | ||
15 | #ifdef __GNUG__ | |
16 | #pragma interface "cachedb.h" | |
17 | #endif | |
18 | ||
19 | #include <db.h> | |
20 | #include <string> | |
21 | #include <apt-pkg/debfile.h> | |
22 | #include <inttypes.h> | |
23 | #include <sys/stat.h> | |
24 | #include <errno.h> | |
25 | ||
26 | #include "contents.h" | |
27 | ||
28 | class CacheDB | |
29 | { | |
30 | protected: | |
31 | ||
32 | // Database state/access | |
33 | DBT Key; | |
34 | DBT Data; | |
35 | char TmpKey[600]; | |
36 | DB *Dbp; | |
37 | bool DBLoaded; | |
38 | bool ReadOnly; | |
39 | string DBFile; | |
40 | ||
41 | // Generate a key for the DB of a given type | |
42 | inline void InitQuery(const char *Type) | |
43 | { | |
44 | memset(&Key,0,sizeof(Key)); | |
45 | memset(&Data,0,sizeof(Data)); | |
46 | Key.data = TmpKey; | |
47 | Key.size = snprintf(TmpKey,sizeof(TmpKey),"%s:%s",Type,FileName.c_str()); | |
48 | } | |
49 | ||
50 | inline bool Get() | |
51 | { | |
52 | return Dbp->get(Dbp,0,&Key,&Data,0) == 0; | |
53 | }; | |
54 | inline bool Put(const void *In,unsigned long Length) | |
55 | { | |
56 | if (ReadOnly == true) | |
57 | return true; | |
58 | Data.size = Length; | |
59 | Data.data = (void *)In; | |
60 | if (DBLoaded == true && (errno = Dbp->put(Dbp,0,&Key,&Data,0)) != 0) | |
61 | { | |
62 | DBLoaded = false; | |
63 | return false; | |
64 | } | |
65 | return true; | |
66 | } | |
67 | ||
68 | // Stat info stored in the DB, Fixed types since it is written to disk. | |
69 | enum FlagList {FlControl = (1<<0),FlMD5=(1<<1),FlContents=(1<<2)}; | |
70 | struct StatStore | |
71 | { | |
72 | time_t mtime; | |
73 | uint32_t Flags; | |
74 | } CurStat; | |
75 | struct StatStore OldStat; | |
76 | ||
77 | // 'set' state | |
78 | string FileName; | |
79 | struct stat FileStat; | |
80 | FileFd *Fd; | |
81 | debDebFile *DebFile; | |
82 | ||
83 | public: | |
84 | ||
85 | // Data collection helpers | |
86 | debDebFile::MemControlExtract Control; | |
87 | ContentsExtract Contents; | |
88 | ||
89 | // Runtime statistics | |
90 | struct Stats | |
91 | { | |
92 | double Bytes; | |
93 | double MD5Bytes; | |
94 | unsigned long Packages; | |
95 | unsigned long Misses; | |
96 | unsigned long DeLinkBytes; | |
97 | ||
98 | inline void Add(const Stats &S) {Bytes += S.Bytes; MD5Bytes += S.MD5Bytes; | |
99 | Packages += S.Packages; Misses += S.Misses; DeLinkBytes += S.DeLinkBytes;}; | |
100 | Stats() : Bytes(0), MD5Bytes(0), Packages(0), Misses(0), DeLinkBytes(0) {}; | |
101 | } Stats; | |
102 | ||
103 | bool ReadyDB(string DB); | |
104 | inline bool DBFailed() {return Dbp != 0 && DBLoaded == false;}; | |
105 | inline bool Loaded() {return DBLoaded == true;}; | |
106 | ||
107 | bool SetFile(string FileName,struct stat St,FileFd *Fd); | |
108 | bool LoadControl(); | |
109 | bool LoadContents(bool GenOnly); | |
110 | bool GetMD5(string &MD5Res,bool GenOnly); | |
111 | bool Finish(); | |
112 | ||
113 | bool Clean(); | |
114 | ||
115 | CacheDB(string DB) : Dbp(0), DebFile(0) {ReadyDB(DB);}; | |
116 | ~CacheDB() {ReadyDB(string()); delete DebFile;}; | |
117 | }; | |
118 | ||
119 | #endif |