cachedb.h 2.8 KB

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