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 inline 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
);
54 return Dbp
->get(Dbp
,0,&Key
,&Data
,0) == 0;
56 inline bool Put(const void *In
,unsigned long const &Length
)
61 Data
.data
= (void *)In
;
62 if (DBLoaded
== true && (errno
= Dbp
->put(Dbp
,0,&Key
,&Data
,0)) != 0)
75 bool GetFileStat(bool const &doStat
= false);
78 bool LoadContents(bool const &GenOnly
);
80 bool GetMD5(bool const &GenOnly
);
81 bool GetSHA1(bool const &GenOnly
);
82 bool GetSHA256(bool const &GenOnly
);
83 bool GetSHA512(bool const &GenOnly
);
85 // Stat info stored in the DB, Fixed types since it is written to disk.
86 enum FlagList
{FlControl
= (1<<0),FlMD5
=(1<<1),FlContents
=(1<<2),
87 FlSize
=(1<<3), FlSHA1
=(1<<4), FlSHA256
=(1<<5),
88 FlSHA512
=(1<<6), FlSource
=(1<<7),
101 struct StatStore OldStat
;
104 std::string FileName
;
110 // Data collection helpers
111 debDebFile::MemControlExtract Control
;
112 ContentsExtract Contents
;
117 std::string SHA256Res
;
118 std::string SHA512Res
;
120 // Runtime statistics
128 unsigned long Packages
;
129 unsigned long Misses
;
130 unsigned long long DeLinkBytes
;
132 inline void Add(const Stats
&S
) {
134 MD5Bytes
+= S
.MD5Bytes
;
135 SHA1Bytes
+= S
.SHA1Bytes
;
136 SHA256Bytes
+= S
.SHA256Bytes
;
137 SHA512Bytes
+= S
.SHA512Bytes
;
138 Packages
+= S
.Packages
;
140 DeLinkBytes
+= S
.DeLinkBytes
;
142 Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0),
143 SHA512Bytes(0),Packages(0), Misses(0), DeLinkBytes(0) {};
146 bool ReadyDB(std::string
const &DB
);
147 inline bool DBFailed() {return Dbp
!= 0 && DBLoaded
== false;};
148 inline bool Loaded() {return DBLoaded
== true;};
150 inline unsigned long long GetFileSize(void) {return CurStat
.FileSize
;}
152 bool SetFile(std::string
const &FileName
,struct stat St
,FileFd
*Fd
);
154 // terrible old overloaded interface
155 bool GetFileInfo(std::string
const &FileName
,
156 bool const &DoControl
,
157 bool const &DoContents
,
158 bool const &GenContentsOnly
,
159 bool const &DoSource
,
162 bool const &DoSHA256
,
163 bool const &DoSHA512
,
164 bool const &checkMtime
= false);
170 CacheDB(std::string
const &DB
) : Dbp(0), Fd(NULL
), DebFile(0) {TmpKey
[0]='\0'; ReadyDB(DB
);};
171 ~CacheDB() {ReadyDB(std::string()); delete DebFile
;};