-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathktplugdbvoid.cc
347 lines (337 loc) · 10.8 KB
/
ktplugdbvoid.cc
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*************************************************************************************************
* A pluggable database of no operation
* Copyright (C) 2009-2012 FAL Labs
* This file is part of Kyoto Tycoon.
* This program is free software: you can redistribute it and/or modify it under the terms of
* the GNU General Public License as published by the Free Software Foundation, either version
* 3 of the License, or any later version.
* This program 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.
* See the GNU General Public License for more details.
* You should have received a copy of the GNU General Public License along with this program.
* If not, see <http://www.gnu.org/licenses/>.
*************************************************************************************************/
#include <ktplugdb.h>
namespace kc = kyotocabinet;
namespace kt = kyototycoon;
// pluggable database of no operation
class VoidDB : public kt::PluggableDB {
public:
class Cursor;
private:
class ScopedVisitor;
public:
// cursor to indicate a record
class Cursor : public BasicDB::Cursor {
friend class VoidDB;
public:
// constructor
explicit Cursor(VoidDB* db) : db_(db) {
_assert_(db);
}
// destructor
virtual ~Cursor() {
_assert_(true);
}
// accept a visitor to the current record
bool accept(Visitor* visitor, bool writable = true, bool step = false) {
_assert_(visitor);
db_->set_error(_KCCODELINE_, Error::NOREC, "no record");
return false;
}
// jump the cursor to the first record for forward scan
bool jump() {
_assert_(true);
db_->set_error(_KCCODELINE_, Error::NOREC, "no record");
return false;
}
// jump the cursor to a record for forward scan
bool jump(const char* kbuf, size_t ksiz) {
_assert_(kbuf && ksiz <= kc::MEMMAXSIZ);
db_->set_error(_KCCODELINE_, Error::NOREC, "no record");
return false;
}
// jump the cursor to a record for forward scan
bool jump(const std::string& key) {
_assert_(true);
db_->set_error(_KCCODELINE_, Error::NOREC, "no record");
return false;
}
// jump the cursor to the last record for backward scan
bool jump_back() {
_assert_(true);
db_->set_error(_KCCODELINE_, Error::NOREC, "no record");
return false;
}
// jump the cursor to a record for backward scan
bool jump_back(const char* kbuf, size_t ksiz) {
_assert_(kbuf && ksiz <= kc::MEMMAXSIZ);
db_->set_error(_KCCODELINE_, Error::NOREC, "no record");
return false;
}
// jump the cursor to a record for backward scan
bool jump_back(const std::string& key) {
_assert_(true);
db_->set_error(_KCCODELINE_, Error::NOREC, "no record");
return false;
}
// step the cursor to the next record
bool step() {
_assert_(true);
db_->set_error(_KCCODELINE_, Error::NOREC, "no record");
return false;
}
// step the cursor to the previous record
bool step_back() {
_assert_(true);
db_->set_error(_KCCODELINE_, Error::NOREC, "no record");
return false;
}
// get the database object
VoidDB* db() {
_assert_(true);
return db_;
}
private:
Cursor(const Cursor&);
Cursor& operator =(const Cursor&);
VoidDB* db_;
};
// default constructor
explicit VoidDB() :
mlock_(), error_(), logger_(NULL), logkinds_(0), mtrigger_(NULL), path_("") {
_assert_(true);
}
// destructor
~VoidDB() {
_assert_(true);
}
// get the last happened error
Error error() const {
_assert_(true);
return error_;
}
// set the error information
void set_error(const char* file, int32_t line, const char* func,
Error::Code code, const char* message) {
_assert_(file && line > 0 && func && message);
error_->set(code, message);
if (logger_) {
Logger::Kind kind = code == Error::BROKEN || code == Error::SYSTEM ?
Logger::ERROR : Logger::INFO;
if (kind & logkinds_)
report(file, line, func, kind, "%d: %s: %s", code, Error::codename(code), message);
}
}
// open a database file
bool open(const std::string& path, uint32_t mode = OWRITER | OCREATE) {
_assert_(true);
kc::ScopedRWLock lock(&mlock_, true);
path_.append(path);
trigger_meta(MetaTrigger::OPEN, "open");
return true;
}
// close the database file
bool close() {
_assert_(true);
kc::ScopedRWLock lock(&mlock_, true);
path_.clear();
trigger_meta(MetaTrigger::CLOSE, "close");
return true;
}
// accept a visitor to a record
bool accept(const char* kbuf, size_t ksiz, Visitor* visitor, bool writable = true) {
_assert_(kbuf && ksiz <= kc::MEMMAXSIZ && visitor);
kc::ScopedRWLock lock(&mlock_, false);
size_t vsiz;
visitor->visit_empty(kbuf, ksiz, &vsiz);
return true;
}
// accept a visitor to multiple records at once
bool accept_bulk(const std::vector<std::string>& keys, Visitor* visitor,
bool writable = true) {
_assert_(visitor);
kc::ScopedRWLock lock(&mlock_, writable);
ScopedVisitor svis(visitor);
std::vector<std::string>::const_iterator kit = keys.begin();
std::vector<std::string>::const_iterator kitend = keys.end();
while (kit != kitend) {
size_t vsiz;
visitor->visit_empty(kit->data(), kit->size(), &vsiz);
++kit;
}
return true;
}
// iterate to accept a visitor for each record
bool iterate(Visitor *visitor, bool writable = true, ProgressChecker* checker = NULL) {
_assert_(visitor);
kc::ScopedRWLock lock(&mlock_, true);
ScopedVisitor svis(visitor);
trigger_meta(MetaTrigger::ITERATE, "iterate");
return true;
}
// scan each record in parallel
bool scan_parallel(Visitor *visitor, size_t thnum, ProgressChecker* checker = NULL) {
_assert_(visitor);
kc::ScopedRWLock lock(&mlock_, false);
ScopedVisitor svis(visitor);
trigger_meta(MetaTrigger::ITERATE, "scan_parallel");
return true;
}
// synchronize updated contents with the file and the device
bool synchronize(bool hard = false, FileProcessor* proc = NULL,
ProgressChecker* checker = NULL) {
_assert_(true);
kc::ScopedRWLock lock(&mlock_, false);
bool err = false;
if (proc && !proc->process(path_, 0, 0)) {
set_error(_KCCODELINE_, Error::LOGIC, "postprocessing failed");
err = true;
}
trigger_meta(MetaTrigger::SYNCHRONIZE, "synchronize");
return !err;
}
// occupy database by locking and do something meanwhile
bool occupy(bool writable = true, FileProcessor* proc = NULL) {
_assert_(true);
kc::ScopedRWLock lock(&mlock_, writable);
bool err = false;
if (proc && !proc->process(path_, 0, 0)) {
set_error(_KCCODELINE_, Error::LOGIC, "processing failed");
err = true;
}
trigger_meta(MetaTrigger::OCCUPY, "occupy");
return !err;
}
// begin transaction
bool begin_transaction(bool hard = false) {
_assert_(true);
kc::ScopedRWLock lock(&mlock_, true);
trigger_meta(MetaTrigger::BEGINTRAN, "begin_transaction");
return true;
}
// try to begin transaction
bool begin_transaction_try(bool hard = false) {
_assert_(true);
kc::ScopedRWLock lock(&mlock_, true);
trigger_meta(MetaTrigger::BEGINTRAN, "begin_transaction_try");
return true;
}
// end transaction
bool end_transaction(bool commit = true) {
_assert_(true);
kc::ScopedRWLock lock(&mlock_, true);
trigger_meta(commit ? MetaTrigger::COMMITTRAN : MetaTrigger::ABORTTRAN, "end_transaction");
return true;
}
// remove all records
bool clear() {
_assert_(true);
kc::ScopedRWLock lock(&mlock_, true);
trigger_meta(MetaTrigger::CLEAR, "clear");
return true;
}
// get the number of records
int64_t count() {
_assert_(true);
kc::ScopedRWLock lock(&mlock_, false);
return 0;
}
// get the size of the database file
int64_t size() {
_assert_(true);
kc::ScopedRWLock lock(&mlock_, false);
return 0;
}
// get the path of the database file
std::string path() {
_assert_(true);
kc::ScopedRWLock lock(&mlock_, false);
return path_;
}
// get the miscellaneous status information
bool status(std::map<std::string, std::string>* strmap) {
_assert_(strmap);
kc::ScopedRWLock lock(&mlock_, true);
(*strmap)["type"] = kc::strprintf("%u", (unsigned)TYPEMISC);
(*strmap)["path"] = path_;
(*strmap)["count"] = "0";
(*strmap)["size"] = "0";
return true;
}
// create a cursor object
Cursor* cursor() {
_assert_(true);
return new Cursor(this);
}
// write a log message
void log(const char* file, int32_t line, const char* func, Logger::Kind kind,
const char* message) {
_assert_(file && line > 0 && func && message);
kc::ScopedRWLock lock(&mlock_, false);
if (!logger_) return;
logger_->log(file, line, func, kind, message);
}
// set the internal logger
bool tune_logger(Logger* logger, uint32_t kinds = Logger::WARN | Logger::ERROR) {
_assert_(logger);
kc::ScopedRWLock lock(&mlock_, true);
logger_ = logger;
logkinds_ = kinds;
return true;
}
// set the internal meta operation trigger
bool tune_meta_trigger(MetaTrigger* trigger) {
_assert_(trigger);
kc::ScopedRWLock lock(&mlock_, true);
mtrigger_ = trigger;
return true;
}
protected:
// report a message for debugging
void report(const char* file, int32_t line, const char* func, Logger::Kind kind,
const char* format, ...) {
_assert_(file && line > 0 && func && format);
if (!logger_ || !(kind & logkinds_)) return;
std::string message;
kc::strprintf(&message, "%s: ", path_.empty() ? "-" : path_.c_str());
va_list ap;
va_start(ap, format);
kc::vstrprintf(&message, format, ap);
va_end(ap);
logger_->log(file, line, func, kind, message.c_str());
}
// trigger a meta database operation
void trigger_meta(MetaTrigger::Kind kind, const char* message) {
_assert_(message);
if (mtrigger_) mtrigger_->trigger(kind, message);
}
private:
// scoped visitor
class ScopedVisitor {
public:
explicit ScopedVisitor(Visitor* visitor) : visitor_(visitor) {
_assert_(visitor);
visitor_->visit_before();
}
~ScopedVisitor() {
_assert_(true);
visitor_->visit_after();
}
private:
Visitor* visitor_;
};
VoidDB(const VoidDB&);
VoidDB& operator =(const VoidDB&);
kc::RWLock mlock_;
kc::TSD<kc::BasicDB::Error> error_;
kc::BasicDB::Logger* logger_;
uint32_t logkinds_;
MetaTrigger* mtrigger_;
std::string path_;
};
// initializer called by the main server
extern "C" void* ktdbinit() {
return new VoidDB;
}
// END OF FILE