forked from meganz/sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.cpp
More file actions
120 lines (102 loc) · 2.76 KB
/
db.cpp
File metadata and controls
120 lines (102 loc) · 2.76 KB
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
/**
* @file db.cpp
* @brief Database access interface
*
* (c) 2013-2014 by Mega Limited, Auckland, New Zealand
*
* This file is part of the MEGA SDK - Client Access Engine.
*
* Applications using the MEGA API must present a valid application key
* and comply with the the rules set forth in the Terms of Service.
*
* The MEGA SDK is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* @copyright Simplified (2-clause) BSD License.
*
* You should have received a copy of the license along with this
* program.
*/
#include "mega/db.h"
#include "mega/utils.h"
#include "mega/logging.h"
namespace mega {
DbTable::DbTable(PrnGen &rng, bool checkAlwaysTransacted)
: rng(rng), mCheckAlwaysTransacted(checkAlwaysTransacted)
{
nextid = 0;
}
// add or update record from string
bool DbTable::put(uint32_t index, string* data)
{
return put(index, (char*)data->data(), unsigned(data->size()));
}
// add or update record with padding and encryption
bool DbTable::put(uint32_t type, Cacheable* record, SymmCipher* key)
{
string data;
if (!record->serialize(&data))
{
//Don't return false if there are errors in the serialization
//to let the SDK continue and save the rest of records
LOG_warn << "Serialization failed: " << type;
return true;
}
PaddedCBC::encrypt(rng, &data, key);
if (!record->dbid)
{
record->dbid = (nextid += IDSPACING) | type;
}
return put(record->dbid, &data);
}
// get next record, decrypt and unpad
bool DbTable::next(uint32_t* type, string* data, SymmCipher* key)
{
if (next(type, data))
{
if (!*type)
{
return true;
}
if (*type > nextid)
{
nextid = *type & - IDSPACING;
}
return PaddedCBC::decrypt(data, key);
}
return false;
}
DBTableTransactionCommitter *DbTable::getTransactionCommitter() const
{
return mTransactionCommitter;
}
void DbTable::checkTransaction()
{
if (mCheckAlwaysTransacted)
{
assert(mTransactionCommitter); // if this fails, we should have started a DBTableTransactionCommitter higher in the call stack
if (mTransactionCommitter)
{
mTransactionCommitter->beginOnce();
}
}
}
void DbTable::resetCommitter()
{
if (mTransactionCommitter)
{
mTransactionCommitter->reset();
mCheckAlwaysTransacted = false;
mTransactionCommitter = nullptr;
}
}
void DbTable::checkCommitter(DBTableTransactionCommitter* committer)
{
assert(!committer || committer == mTransactionCommitter);
}
DbAccess::DbAccess()
{
currentDbVersion = LEGACY_DB_VERSION;
}
} // namespace