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/hashes.h>
16 #include <apt-pkg/debfile.h>
35 // Database state/access
44 // Generate a key for the DB of a given type
45 void _InitQuery(const char *Type
)
47 memset(&Key
,0,sizeof(Key
));
48 memset(&Data
,0,sizeof(Data
));
50 Key
.size
= snprintf(TmpKey
,sizeof(TmpKey
),"%s:%s",FileName
.c_str(), Type
);
53 void InitQueryStats() {
56 void InitQuerySource() {
59 void InitQueryControl() {
62 void InitQueryContent() {
68 return Dbp
->get(Dbp
,0,&Key
,&Data
,0) == 0;
70 inline bool Put(const void *In
,unsigned long const &Length
)
75 Data
.data
= (void *)In
;
76 if (DBLoaded
== true && (errno
= Dbp
->put(Dbp
,0,&Key
,&Data
,0)) != 0)
89 bool GetFileStat(bool const &doStat
= false);
92 bool LoadContents(bool const &GenOnly
);
94 bool GetHashes(bool const GenOnly
, unsigned int const DoHashes
);
96 // Stat info stored in the DB, Fixed types since it is written to disk.
97 enum FlagList
{FlControl
= (1<<0),FlMD5
=(1<<1),FlContents
=(1<<2),
98 FlSize
=(1<<3), FlSHA1
=(1<<4), FlSHA256
=(1<<5),
99 FlSHA512
=(1<<6), FlSource
=(1<<7),
112 struct StatStore OldStat
;
115 std::string FileName
;
121 // Data collection helpers
122 debDebFile::MemControlExtract Control
;
123 ContentsExtract Contents
;
125 HashStringList HashesList
;
127 // Runtime statistics
135 unsigned long Packages
;
136 unsigned long Misses
;
137 unsigned long long DeLinkBytes
;
139 inline void Add(const Stats
&S
) {
141 MD5Bytes
+= S
.MD5Bytes
;
142 SHA1Bytes
+= S
.SHA1Bytes
;
143 SHA256Bytes
+= S
.SHA256Bytes
;
144 SHA512Bytes
+= S
.SHA512Bytes
;
145 Packages
+= S
.Packages
;
147 DeLinkBytes
+= S
.DeLinkBytes
;
149 Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0),
150 SHA512Bytes(0),Packages(0), Misses(0), DeLinkBytes(0) {};
153 bool ReadyDB(std::string
const &DB
);
154 inline bool DBFailed() {return Dbp
!= 0 && DBLoaded
== false;};
155 inline bool Loaded() {return DBLoaded
== true;};
157 inline unsigned long long GetFileSize(void) {return CurStat
.FileSize
;}
159 bool SetFile(std::string
const &FileName
,struct stat St
,FileFd
*Fd
);
161 // terrible old overloaded interface
162 bool GetFileInfo(std::string
const &FileName
,
163 bool const &DoControl
,
164 bool const &DoContents
,
165 bool const &GenContentsOnly
,
167 unsigned int const DoHashes
,
168 bool const &checkMtime
= false);
174 CacheDB(std::string
const &DB
) : Dbp(0), Fd(NULL
), DebFile(0) {TmpKey
[0]='\0'; ReadyDB(DB
);};
175 ~CacheDB() {ReadyDB(std::string()); delete DebFile
;};