summaryrefslogtreecommitdiff
path: root/lib/backupstore/BackupStoreRefCountDatabase.h
blob: 93c79afb721af3b8c433b73a449d93cd62ddad8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
// --------------------------------------------------------------------------
//
// File
//		Name:    BackupStoreRefCountDatabase.h
//		Purpose: Main backup store information storage
//		Created: 2003/08/28
//
// --------------------------------------------------------------------------

#ifndef BACKUPSTOREREFCOUNTDATABASE__H
#define BACKUPSTOREREFCOUNTDATABASE__H

#include <memory>
#include <string>
#include <vector>

#include "BackupStoreAccountDatabase.h"
#include "FileStream.h"

class BackupStoreCheck;
class BackupStoreContext;

// set packing to one byte
#ifdef STRUCTURE_PACKING_FOR_WIRE_USE_HEADERS
#include "BeginStructPackForWire.h"
#else
BEGIN_STRUCTURE_PACKING_FOR_WIRE
#endif

typedef struct
{
	uint32_t mMagicValue;	// also the version number
	uint32_t mAccountID;
} refcount_StreamFormat;

// Use default packing
#ifdef STRUCTURE_PACKING_FOR_WIRE_USE_HEADERS
#include "EndStructPackForWire.h"
#else
END_STRUCTURE_PACKING_FOR_WIRE
#endif

// --------------------------------------------------------------------------
//
// Class
//		Name:    BackupStoreRefCountDatabase
//		Purpose: Backup store reference count database storage
//		Created: 2009/06/01
//
// --------------------------------------------------------------------------
class BackupStoreRefCountDatabase
{
	friend class BackupStoreCheck;
	friend class BackupStoreContext;
	friend class HousekeepStoreAccount;

public:
	~BackupStoreRefCountDatabase();
private:
	// Creation through static functions only
	BackupStoreRefCountDatabase(const BackupStoreAccountDatabase::Entry&
		rAccount);
	// No copying allowed
	BackupStoreRefCountDatabase(const BackupStoreRefCountDatabase &);
	
public:
	// Create a new database for a new account. This method will refuse
	// to overwrite any existing file.
	static void CreateNew(const BackupStoreAccountDatabase::Entry& rAccount)
	{
		Create(rAccount, false);
	}
	
	// Load it from the store
	static std::auto_ptr<BackupStoreRefCountDatabase> Load(const
		BackupStoreAccountDatabase::Entry& rAccount, bool ReadOnly);
	
	typedef uint32_t refcount_t;

	// Data access functions
	refcount_t GetRefCount(int64_t ObjectID) const;
	int64_t GetLastObjectIDUsed() const;
	
	// Data modification functions
	void AddReference(int64_t ObjectID);
	// RemoveReference returns false if refcount drops to zero
	bool RemoveReference(int64_t ObjectID);

private:
	// Create a new database for an existing account. Used during
	// account checking if opening the old database throws an exception.
	// This method will overwrite any existing file.
	static void CreateForRegeneration(const
		BackupStoreAccountDatabase::Entry& rAccount)
	{
		Create(rAccount, true);
	}

	static void Create(const BackupStoreAccountDatabase::Entry& rAccount,
		bool AllowOverwrite);

	static std::string GetFilename(const BackupStoreAccountDatabase::Entry&
		rAccount);
	IOStream::pos_type GetSize() const
	{
		return mapDatabaseFile->GetPosition() +
			mapDatabaseFile->BytesLeftToRead();
	}
	IOStream::pos_type GetEntrySize() const
	{
		return sizeof(refcount_t);
	}
	IOStream::pos_type GetOffset(int64_t ObjectID) const
	{
		return ((ObjectID - 1) * GetEntrySize()) +
			sizeof(refcount_StreamFormat);
	}
	void SetRefCount(int64_t ObjectID, refcount_t NewRefCount);
	
	// Location information
	BackupStoreAccountDatabase::Entry mAccount;
	std::string mFilename;
	bool mReadOnly;
	bool mIsModified;
	std::auto_ptr<FileStream> mapDatabaseFile;
};

#endif // BACKUPSTOREREFCOUNTDATABASE__H