summaryrefslogtreecommitdiff
path: root/lib/backupstore/BackupStoreRefCountDatabase.h
blob: 6c16516ed8d0a463d24b37c78b9771b39001c4d5 (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
// --------------------------------------------------------------------------
//
// 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 "BackupStoreConstants.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, bool ReadOnly, bool Temporary,
		std::auto_ptr<FileStream> apDatabaseFile);
	// No copying allowed
	BackupStoreRefCountDatabase(const BackupStoreRefCountDatabase &);
	
public:
	// Create a blank database, using a temporary file that you must
	// Discard() or Commit() to make permanent.
	static std::auto_ptr<BackupStoreRefCountDatabase> Create
		(const BackupStoreAccountDatabase::Entry& rAccount);
	void Commit();
	void Discard();

	// 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);
	int ReportChangesTo(BackupStoreRefCountDatabase& rOldRefs,
		int64_t ignore_object_id = 0);

private:
	static std::string GetFilename(const BackupStoreAccountDatabase::Entry&
		rAccount, bool Temporary);

	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;
	bool mIsTemporaryFile;
	std::auto_ptr<FileStream> mapDatabaseFile;

	bool NeedsCommitOrDiscard()
	{
		return mapDatabaseFile.get() && mIsModified && mIsTemporaryFile;
	}
};

#endif // BACKUPSTOREREFCOUNTDATABASE__H