-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.cpp
185 lines (166 loc) · 4.46 KB
/
db.cpp
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#include "db.h"
#include "spooky.h"
#include "errors.h"
class GilUnlocker {
PyThreadState* const m_threadState;
public:
GilUnlocker() : m_threadState(PyGILState_Check() ? PyEval_SaveThread() : nullptr) { }
~GilUnlocker() {
if(m_threadState != nullptr)
PyEval_RestoreThread(m_threadState);
}
};
MDB_txn* txn_begin(MDB_env* const mdb, const bool write) {
GilUnlocker gil;
const unsigned int flags = write ? 0 : MDB_RDONLY;
MDB_txn* txn = nullptr;
int mapsizePatience = 10;
while(true) {
int error = mdb_txn_begin(mdb, nullptr, flags, &txn);
switch(error) {
case 0:
return txn;
case MDB_MAP_RESIZED:
if (mapsizePatience > 0) {
mapsizePatience -= 1;
error = mdb_env_set_mapsize(mdb, 0);
if(error != 0)
throw MdbError(error);
MDB_envinfo info;
mdb_env_info(mdb, &info);
continue;
} else {
throw MdbError(error);
}
default:
throw MdbError(error);
}
}
}
void txn_commit(MDB_txn* const txn) {
GilUnlocker gil;
const int error = mdb_txn_commit(txn);
if(error != 0)
throw MdbError(error);
}
void txn_abort(MDB_txn* const txn) {
GilUnlocker gil;
mdb_txn_abort(txn); // This doesn't return any errors.
}
void open_db(MDB_txn* const txn, const char* const name, unsigned int flags, MDB_dbi* const dbi) {
GilUnlocker gil;
const int error = mdb_dbi_open(txn, name, flags | MDB_CREATE, dbi);
if(error != 0)
throw MdbError(error);
}
void put(
MDB_txn* const txn,
const MDB_dbi dbi,
MDB_val* const key,
MDB_val* const value,
unsigned int flags
) {
GilUnlocker gil;
const int error = mdb_put(txn, dbi, key, value, flags);
if(error != 0)
throw MdbError(error);
}
bool get(
MDB_txn* const txn,
const MDB_dbi dbi,
MDB_val* const key,
MDB_val* const value
) {
GilUnlocker gil;
const int error = mdb_get(txn, dbi, key, value);
switch(error) {
case MDB_SUCCESS:
return true;
case MDB_NOTFOUND:
return false;
default:
throw MdbError(error);
}
}
uint64_t putImmutable(
MDB_txn* const txn,
const MDB_dbi dbi,
MDB_val* const mdbVal,
const unsigned char typeCode,
const bool readonly
) {
GilUnlocker gil;
uint64_t key = SpookyHash::hash64(
mdbVal->mv_data,
mdbVal->mv_size,
typeCode);
MDB_val mdbKey = { .mv_size = sizeof(key), .mv_data = &key };
if(readonly) {
// In a readonly transaction, we turn the put() into a get() to check whether
// the value is there.
const int error = mdb_get(txn, dbi, &mdbKey, mdbVal);
switch(error) {
case 0:
// We could check here for hash collisions, but we won't.
break;
case MDB_NOTFOUND:
throw OocError(OocError::ImmutableValueNotFound);
default:
throw MdbError(error);
}
} else {
put(txn, dbi, &mdbKey, mdbVal);
}
return key;
}
void del(MDB_txn* const txn, MDB_dbi dbi, MDB_val* key) {
GilUnlocker gil;
const int error = mdb_del(txn, dbi, key, nullptr);
if(error != 0)
throw MdbError(error);
}
MDB_cursor* cursor_open(MDB_txn* const txn, const MDB_dbi dbi) {
MDB_cursor* result;
const int error = mdb_cursor_open(txn, dbi, &result);
if(error != 0)
throw MdbError(error);
return result;
}
void cursor_close(MDB_cursor* const cursor) {
mdb_cursor_close(cursor);
// Apparently this never fails?
}
bool cursor_get(
MDB_cursor* const cursor,
MDB_val* const key,
MDB_val* const data,
const MDB_cursor_op op
) {
GilUnlocker gil;
const int error = mdb_cursor_get(cursor, key, data, op);
switch(error) {
case MDB_SUCCESS:
return true;
case MDB_NOTFOUND:
return false;
default:
throw MdbError(error);
}
}
void cursor_put(
MDB_cursor* const cursor,
MDB_val* const key,
MDB_val* const data,
const unsigned int flags
) {
GilUnlocker gil;
const int error = mdb_cursor_put(cursor, key, data, flags);
if(error != MDB_SUCCESS)
throw MdbError(error);
}
void cursor_del(MDB_cursor* const cursor, const unsigned int flags) {
GilUnlocker gil;
const int error = mdb_cursor_del(cursor, flags);
if(error != MDB_SUCCESS)
throw MdbError(error);
}