1 // -*- mode: cpp; mode: fold -*-
3 // $Id: cachedb.h,v 1.4 2004/05/08 19:41:01 mdz Exp $
4 /* ######################################################################
8 Simple uniform interface to a cache database.
10 ##################################################################### */
15 #include <apt-pkg/debfile.h>
34 // Database state/access
43 // Generate a key for the DB of a given type
44 void _InitQuery(const char *Type
)
46 memset(&Key
,0,sizeof(Key
));
47 memset(&Data
,0,sizeof(Data
));
49 Key
.size
= snprintf(TmpKey
,sizeof(TmpKey
),"%s:%s",FileName
.c_str(), Type
);
52 void InitQueryStats() {
55 void InitQuerySource() {
58 void InitQueryControl() {
61 void InitQueryContent() {
67 return Dbp
->get(Dbp
,0,&Key
,&Data
,0) == 0;
69 inline bool Put(const void *In
,unsigned long const &Length
)
74 Data
.data
= (void *)In
;
75 if (DBLoaded
== true && (errno
= Dbp
->put(Dbp
,0,&Key
,&Data
,0)) != 0)
88 bool GetFileStat(bool const &doStat
= false);
91 bool LoadContents(bool const &GenOnly
);
93 bool GetMD5(bool const &GenOnly
);
94 bool GetSHA1(bool const &GenOnly
);
95 bool GetSHA256(bool const &GenOnly
);
96 bool GetSHA512(bool const &GenOnly
);
98 // Stat info stored in the DB, Fixed types since it is written to disk.
99 enum FlagList
{FlControl
= (1<<0),FlMD5
=(1<<1),FlContents
=(1<<2),
100 FlSize
=(1<<3), FlSHA1
=(1<<4), FlSHA256
=(1<<5),
101 FlSHA512
=(1<<6), FlSource
=(1<<7),
114 struct StatStore OldStat
;
117 std::string FileName
;
123 // Data collection helpers
124 debDebFile::MemControlExtract Control
;
125 ContentsExtract Contents
;
130 std::string SHA256Res
;
131 std::string SHA512Res
;
133 // Runtime statistics
141 unsigned long Packages
;
142 unsigned long Misses
;
143 unsigned long long DeLinkBytes
;
145 inline void Add(const Stats
&S
) {
147 MD5Bytes
+= S
.MD5Bytes
;
148 SHA1Bytes
+= S
.SHA1Bytes
;
149 SHA256Bytes
+= S
.SHA256Bytes
;
150 SHA512Bytes
+= S
.SHA512Bytes
;
151 Packages
+= S
.Packages
;
153 DeLinkBytes
+= S
.DeLinkBytes
;
155 Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0),
156 SHA512Bytes(0),Packages(0), Misses(0), DeLinkBytes(0) {};
159 bool ReadyDB(std::string
const &DB
);
160 inline bool DBFailed() {return Dbp
!= 0 && DBLoaded
== false;};
161 inline bool Loaded() {return DBLoaded
== true;};
163 inline unsigned long long GetFileSize(void) {return CurStat
.FileSize
;}
165 bool SetFile(std::string
const &FileName
,struct stat St
,FileFd
*Fd
);
167 // terrible old overloaded interface
168 bool GetFileInfo(std::string
const &FileName
,
169 bool const &DoControl
,
170 bool const &DoContents
,
171 bool const &GenContentsOnly
,
172 bool const &DoSource
,
175 bool const &DoSHA256
,
176 bool const &DoSHA512
,
177 bool const &checkMtime
= false);
183 CacheDB(std::string
const &DB
) : Dbp(0), Fd(NULL
), DebFile(0) {TmpKey
[0]='\0'; ReadyDB(DB
);};
184 ~CacheDB() {ReadyDB(std::string()); delete DebFile
;};