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 ##################################################################### */
16 #pragma interface "cachedb.h"
21 #include <apt-pkg/debfile.h>
32 // Database state/access
41 // Generate a key for the DB of a given type
42 inline void InitQuery(const char *Type
)
44 memset(&Key
,0,sizeof(Key
));
45 memset(&Data
,0,sizeof(Data
));
47 Key
.size
= snprintf(TmpKey
,sizeof(TmpKey
),"%s:%s",FileName
.c_str(), Type
);
52 return Dbp
->get(Dbp
,0,&Key
,&Data
,0) == 0;
54 inline bool Put(const void *In
,unsigned long Length
)
59 Data
.data
= (void *)In
;
60 if (DBLoaded
== true && (errno
= Dbp
->put(Dbp
,0,&Key
,&Data
,0)) != 0)
71 bool LoadContents(bool GenOnly
);
72 bool GetMD5(bool GenOnly
);
73 bool GetSHA1(bool GenOnly
);
74 bool GetSHA256(bool GenOnly
);
76 // Stat info stored in the DB, Fixed types since it is written to disk.
77 enum FlagList
{FlControl
= (1<<0),FlMD5
=(1<<1),FlContents
=(1<<2),
78 FlSize
=(1<<3), FlSHA1
=(1<<4), FlSHA256
=(1<<5)};
88 struct StatStore OldStat
;
97 // Data collection helpers
98 debDebFile::MemControlExtract Control
;
99 ContentsExtract Contents
;
104 // Runtime statistics
111 unsigned long Packages
;
112 unsigned long Misses
;
113 unsigned long DeLinkBytes
;
115 inline void Add(const Stats
&S
) {
116 Bytes
+= S
.Bytes
; MD5Bytes
+= S
.MD5Bytes
; SHA1Bytes
+= S
.SHA1Bytes
;
117 SHA256Bytes
+= S
.SHA256Bytes
;
118 Packages
+= S
.Packages
; Misses
+= S
.Misses
; DeLinkBytes
+= S
.DeLinkBytes
;};
119 Stats() : Bytes(0), MD5Bytes(0), SHA1Bytes(0), SHA256Bytes(0), Packages(0), Misses(0), DeLinkBytes(0) {};
122 bool ReadyDB(string DB
);
123 inline bool DBFailed() {return Dbp
!= 0 && DBLoaded
== false;};
124 inline bool Loaded() {return DBLoaded
== true;};
126 inline off_t
GetFileSize(void) {return CurStat
.FileSize
;}
128 bool SetFile(string FileName
,struct stat St
,FileFd
*Fd
);
129 bool GetFileInfo(string FileName
, bool DoControl
, bool DoContents
,
130 bool GenContentsOnly
, bool DoMD5
, bool DoSHA1
, bool DoSHA256
);
135 CacheDB(string DB
) : Dbp(0), Fd(NULL
), DebFile(0) {ReadyDB(DB
);};
136 ~CacheDB() {ReadyDB(string()); delete DebFile
;};