| 1 | // -------------------------------------------------------------------------- |
|---|
| 2 | // |
|---|
| 3 | // File |
|---|
| 4 | // Name: BackupStoreRefCountDatabase.h |
|---|
| 5 | // Purpose: Main backup store information storage |
|---|
| 6 | // Created: 2003/08/28 |
|---|
| 7 | // |
|---|
| 8 | // -------------------------------------------------------------------------- |
|---|
| 9 | |
|---|
| 10 | #ifndef BACKUPSTOREREFCOUNTDATABASE__H |
|---|
| 11 | #define BACKUPSTOREREFCOUNTDATABASE__H |
|---|
| 12 | |
|---|
| 13 | #include <memory> |
|---|
| 14 | #include <string> |
|---|
| 15 | #include <vector> |
|---|
| 16 | |
|---|
| 17 | #include "BackupStoreAccountDatabase.h" |
|---|
| 18 | #include "FileStream.h" |
|---|
| 19 | |
|---|
| 20 | class BackupStoreCheck; |
|---|
| 21 | class BackupStoreContext; |
|---|
| 22 | |
|---|
| 23 | // set packing to one byte |
|---|
| 24 | #ifdef STRUCTURE_PACKING_FOR_WIRE_USE_HEADERS |
|---|
| 25 | #include "BeginStructPackForWire.h" |
|---|
| 26 | #else |
|---|
| 27 | BEGIN_STRUCTURE_PACKING_FOR_WIRE |
|---|
| 28 | #endif |
|---|
| 29 | |
|---|
| 30 | typedef struct |
|---|
| 31 | { |
|---|
| 32 | uint32_t mMagicValue; // also the version number |
|---|
| 33 | uint32_t mAccountID; |
|---|
| 34 | } refcount_StreamFormat; |
|---|
| 35 | |
|---|
| 36 | // Use default packing |
|---|
| 37 | #ifdef STRUCTURE_PACKING_FOR_WIRE_USE_HEADERS |
|---|
| 38 | #include "EndStructPackForWire.h" |
|---|
| 39 | #else |
|---|
| 40 | END_STRUCTURE_PACKING_FOR_WIRE |
|---|
| 41 | #endif |
|---|
| 42 | |
|---|
| 43 | // -------------------------------------------------------------------------- |
|---|
| 44 | // |
|---|
| 45 | // Class |
|---|
| 46 | // Name: BackupStoreRefCountDatabase |
|---|
| 47 | // Purpose: Backup store reference count database storage |
|---|
| 48 | // Created: 2009/06/01 |
|---|
| 49 | // |
|---|
| 50 | // -------------------------------------------------------------------------- |
|---|
| 51 | class BackupStoreRefCountDatabase |
|---|
| 52 | { |
|---|
| 53 | friend class BackupStoreCheck; |
|---|
| 54 | friend class BackupStoreContext; |
|---|
| 55 | friend class HousekeepStoreAccount; |
|---|
| 56 | |
|---|
| 57 | public: |
|---|
| 58 | ~BackupStoreRefCountDatabase(); |
|---|
| 59 | private: |
|---|
| 60 | // Creation through static functions only |
|---|
| 61 | BackupStoreRefCountDatabase(const BackupStoreAccountDatabase::Entry& |
|---|
| 62 | rAccount); |
|---|
| 63 | // No copying allowed |
|---|
| 64 | BackupStoreRefCountDatabase(const BackupStoreRefCountDatabase &); |
|---|
| 65 | |
|---|
| 66 | public: |
|---|
| 67 | // Create a new database for a new account. This method will refuse |
|---|
| 68 | // to overwrite any existing file. |
|---|
| 69 | static void CreateNew(const BackupStoreAccountDatabase::Entry& rAccount) |
|---|
| 70 | { |
|---|
| 71 | Create(rAccount, false); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | // Load it from the store |
|---|
| 75 | static std::auto_ptr<BackupStoreRefCountDatabase> Load(const |
|---|
| 76 | BackupStoreAccountDatabase::Entry& rAccount, bool ReadOnly); |
|---|
| 77 | |
|---|
| 78 | typedef uint32_t refcount_t; |
|---|
| 79 | |
|---|
| 80 | // Data access functions |
|---|
| 81 | refcount_t GetRefCount(int64_t ObjectID) const; |
|---|
| 82 | int64_t GetLastObjectIDUsed() const; |
|---|
| 83 | |
|---|
| 84 | // Data modification functions |
|---|
| 85 | void AddReference(int64_t ObjectID); |
|---|
| 86 | // RemoveReference returns false if refcount drops to zero |
|---|
| 87 | bool RemoveReference(int64_t ObjectID); |
|---|
| 88 | |
|---|
| 89 | private: |
|---|
| 90 | // Create a new database for an existing account. Used during |
|---|
| 91 | // account checking if opening the old database throws an exception. |
|---|
| 92 | // This method will overwrite any existing file. |
|---|
| 93 | static void CreateForRegeneration(const |
|---|
| 94 | BackupStoreAccountDatabase::Entry& rAccount) |
|---|
| 95 | { |
|---|
| 96 | Create(rAccount, true); |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | static void Create(const BackupStoreAccountDatabase::Entry& rAccount, |
|---|
| 100 | bool AllowOverwrite); |
|---|
| 101 | |
|---|
| 102 | static std::string GetFilename(const BackupStoreAccountDatabase::Entry& |
|---|
| 103 | rAccount); |
|---|
| 104 | IOStream::pos_type GetSize() const |
|---|
| 105 | { |
|---|
| 106 | return mapDatabaseFile->GetPosition() + |
|---|
| 107 | mapDatabaseFile->BytesLeftToRead(); |
|---|
| 108 | } |
|---|
| 109 | IOStream::pos_type GetEntrySize() const |
|---|
| 110 | { |
|---|
| 111 | return sizeof(refcount_t); |
|---|
| 112 | } |
|---|
| 113 | IOStream::pos_type GetOffset(int64_t ObjectID) const |
|---|
| 114 | { |
|---|
| 115 | return ((ObjectID - 1) * GetEntrySize()) + |
|---|
| 116 | sizeof(refcount_StreamFormat); |
|---|
| 117 | } |
|---|
| 118 | void SetRefCount(int64_t ObjectID, refcount_t NewRefCount); |
|---|
| 119 | |
|---|
| 120 | // Location information |
|---|
| 121 | BackupStoreAccountDatabase::Entry mAccount; |
|---|
| 122 | std::string mFilename; |
|---|
| 123 | bool mReadOnly; |
|---|
| 124 | bool mIsModified; |
|---|
| 125 | std::auto_ptr<FileStream> mapDatabaseFile; |
|---|
| 126 | }; |
|---|
| 127 | |
|---|
| 128 | #endif // BACKUPSTOREREFCOUNTDATABASE__H |
|---|