-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlite3_map_impl.h
More file actions
659 lines (559 loc) · 18 KB
/
Copy pathsqlite3_map_impl.h
File metadata and controls
659 lines (559 loc) · 18 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
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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
// Copyright (c) 2015-2016 The Pebblecoin developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#pragma once
#include "sqlite3_map.h"
namespace sqlite3 {
extern "C" {
#include "sqlite3.h"
}
}
#include <boost/utility/value_init.hpp>
//#define SQLITE3_MAP_DEBUG
#ifdef SQLITE3_MAP_DEBUG
#include <iostream>
inline void log_cursor(void *pCursor, const std::string& msg) {
std::cout << "-- cursor " << pCursor << " -- " << msg << std::endl;
}
inline void log_cursor(void *pCursor, const std::string& msg, const std::string& msg2) {
std::cout << "-- cursor " << pCursor << " -- " << msg << " " << msg2 << std::endl;
}
#endif
namespace sqlite3 {
template <class K, class V>
sqlite3_map<K, V>::sqlite3_map(const char *filename,
std::function<K(const std::string&)> load_key,
std::function<std::string(const K&)> store_key,
std::function<V(const std::string&)> load_value,
std::function<std::string(const V&)> store_value)
: pDb(nullptr)
, which_db(0)
, load_key(load_key)
, store_key(store_key)
, load_value(load_value)
, store_value(store_value)
, autocommit_per_modification(false)
, autocommit_on_close(true)
, db_filename(nullptr)
, _opt_count_stmt(nullptr)
, _opt_count_key_stmt(nullptr)
, _opt_insert_stmt(nullptr)
{
reopen(filename);
}
template <class K, class V>
sqlite3_map<K, V>::sqlite3_map(sqlite3_map&& o)
// temporary in-mem database with the same serializers...
: sqlite3_map(nullptr, o.load_key, o.store_key, o.load_value, o.store_value)
{
// swap 'em
swap(*this, o);
}
template <class K, class V>
sqlite3_map<K, V>& sqlite3_map<K, V>::operator=(sqlite3_map&& o)
{
swap(*this, o);
return *this;
}
template <class K, class V>
sqlite3_map<K, V>::~sqlite3_map()
{
close();
}
template <class K, class V>
void sqlite3_map<K, V>::close()
{
if (!pDb)
return;
if (autocommit_on_close) {
commit();
}
// clear optimized queries
_opt_count_stmt.reset(nullptr);
_opt_count_key_stmt.reset(nullptr);
_opt_insert_stmt.reset(nullptr);
checked(sqlite3_close(pDb), "close, maybe not all iterators destructed?");
pDb = nullptr;
++which_db; // invalidate previous iterators
}
template <class K, class V>
void sqlite3_map<K, V>::reopen(const char *filename)
{
close();
checked(sqlite3_open(filename == nullptr ? ":memory:" : filename, &pDb), "open db");
checked_exec("CREATE TABLE IF NOT EXISTS the_table (key BLOB PRIMARY KEY, value BLOB);");
// begin transaction to disable autocommit
checked_exec("BEGIN TRANSACTION;");
db_filename = filename;
// initialized optimized queries
_opt_count_stmt = prepare_stmt("SELECT COUNT(*) FROM the_table;");
_opt_count_key_stmt = prepare_stmt("SELECT COUNT(*) FROM the_table WHERE key = ?");
_opt_insert_stmt = prepare_stmt("INSERT OR REPLACE INTO the_table (key, value) VALUES (?, ?);");
}
template <class K, class V>
void sqlite3_map<K, V>::set_autocommit(bool per_modification, bool on_close)
{
autocommit_per_modification = per_modification;
autocommit_on_close = on_close;
}
template <class K, class V>
void sqlite3_map<K, V>::get_autocommit(bool& per_modification, bool& on_close) const
{
per_modification = autocommit_per_modification;
on_close = autocommit_on_close;
}
template <class K, class V>
void sqlite3_map<K, V>::commit()
{
checked_exec("COMMIT TRANSACTION;");
checked_exec("BEGIN TRANSACTION;");
}
template <class K, class V>
void sqlite3_map<K, V>::rollback()
{
checked_exec("ROLLBACK TRANSACTION;");
checked_exec("BEGIN TRANSACTION;");
}
template <class K, class V>
V sqlite3_map<K, V>::load(const K& key) const
{
auto it = find(key);
if (it == end()) {
return boost::value_initialized<V>();
}
return it->second;
}
template <class K, class V>
void sqlite3_map<K, V>::store(const K& key, const V& value)
{
auto key_str = store_key(key);
auto val_str = store_value(value);
#ifdef SQLITE3_MAP_DEBUG
log_cursor(_opt_insert_stmt.get(), "binding key", key_str);
#endif
checked(sqlite3_bind_blob(_opt_insert_stmt.get(), 1, key_str.data(), key_str.size(), SQLITE_STATIC),
"bind store() key");
#ifdef SQLITE3_MAP_DEBUG
log_cursor(_opt_insert_stmt.get(), "binding value", val_str);
#endif
checked(sqlite3_bind_blob(_opt_insert_stmt.get(), 2, val_str.data(), val_str.size(), SQLITE_STATIC),
"bind store() value");
checked_exec(_opt_insert_stmt);
checked(sqlite3_reset(_opt_insert_stmt.get()), "reset insert stmt");
if (autocommit_per_modification) {
commit();
}
}
template <class K, class V>
std::pair<typename sqlite3_map<K, V>::iterator, bool> sqlite3_map<K, V>::insert(const value_type& v)
{
auto it = find(v.first);
if (it != end()) {
return std::make_pair(std::move(it), false);
}
store(v.first, v.second);
return std::make_pair(find(v.first), true);
}
template <class K, class V>
size_t sqlite3_map<K, V>::count(const K& key) const
{
// bind key blob value
auto key_blob = store_key(key);
checked(sqlite3_bind_blob(_opt_count_key_stmt.get(), 1, key_blob.data(), key_blob.size(), SQLITE_STATIC),
"bind find() key");
// execute it
if (sqlite3_step(_opt_count_key_stmt.get()) != SQLITE_ROW) {
throw std::runtime_error("select count(*) didn't return data");
}
// return result
size_t res = ((size_t)sqlite3_column_int64(_opt_count_key_stmt.get(), 0)) > 0;
// reset query
checked(sqlite3_reset(_opt_count_key_stmt.get()));
return res;
}
template <class K, class V>
bool sqlite3_map<K, V>::contains(const K& key) const
{
return count(key) > 0;
}
template <class K, class V>
bool sqlite3_map<K, V>::empty() const
{
return size() == 0;
}
template <class K, class V>
size_t sqlite3_map<K, V>::size() const
{
#ifdef SQLITE3_MAP_DEBUG
log_cursor(_opt_count_stmt.get(), "sqlite3_step()...");
#endif
if (sqlite3_step(_opt_count_stmt.get()) != SQLITE_ROW) {
throw std::runtime_error("select count(*) didn't return data");
}
size_t res = (size_t)sqlite3_column_int64(_opt_count_stmt.get(), 0);
checked(sqlite3_reset(_opt_count_stmt.get()), "reset count table stmt");
return res;
}
template <class K, class V>
void sqlite3_map<K, V>::clear()
{
checked_exec("DROP TABLE the_table");
checked_exec("CREATE TABLE IF NOT EXISTS the_table (key BLOB PRIMARY KEY, value BLOB);");
if (autocommit_per_modification) {
commit();
}
}
template <class K, class V>
size_t sqlite3_map<K, V>::erase(const K& key)
{
auto it = find(key);
if (it == end()) {
return 0;
}
erase(it);
return 1;
}
template <class K, class V>
typename sqlite3_map<K, V>::iterator sqlite3_map<K, V>::erase(iterator pos)
{
if (pos == end()) {
return pos;
}
auto res = pos.erase();
if (autocommit_per_modification) {
commit();
}
return res;
}
/// ------------------------------------------------------
template <class K, class V>
void sqlite3_map<K, V>::checked(int rc, const std::string& op) const {
if (!(rc == SQLITE_OK || rc == SQLITE_DONE)) {
throw std::runtime_error("sqlite3 error " + std::to_string(rc) + (op.empty() ? "" : " when " + op)
+ ": " + std::string(sqlite3_errmsg(pDb)));
}
}
template <class K, class V>
void sqlite3_map<K, V>::checked_exec(stmt_ptr& stmt) const {
// ignore all values returned, execute statement until it's done
int rc;
#ifdef SQLITE3_MAP_DEBUG
log_cursor(stmt.get(), "sqlite3_step()...");
#endif
while ((rc = sqlite3_step(stmt.get())) == SQLITE_ROW) {
#ifdef SQLITE3_MAP_DEBUG
log_cursor(stmt.get(), "sqlite3_step()...");
#endif
};
checked(rc, "step exec");
}
template <class K, class V>
void sqlite3_map<K, V>::checked_exec(const char *query) const {
auto stmt = prepare_stmt(query);
return checked_exec(stmt);
}
template <class K, class V>
typename sqlite3_map<K, V>::stmt_ptr sqlite3_map<K, V>::prepare_stmt(const char *query) const
{
auto destroy = [this](sqlite3_stmt *pStmt) {
this->checked(sqlite3_finalize(pStmt));
};
if (query == nullptr) {
#ifdef SQLITE3_MAP_DEBUG
log_cursor(nullptr, "prepared nullptr statement");
#endif
return stmt_ptr(nullptr, destroy);
}
sqlite3_stmt *pStmt;
checked(sqlite3_prepare_v2(pDb, query, strlen(query), &pStmt, nullptr));
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt, "prepared statement", query);
#endif
return stmt_ptr(pStmt, destroy);
}
template <class K, class V>
typename sqlite3_map<K, V>::stmt_ptr sqlite3_map<K, V>::prepare_find_key_blob(const std::string& key_blob) const
{
auto pStmt = prepare_stmt("SELECT key, value FROM the_table WHERE key = ?;");
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt.get(), "binding key", key_blob);
#endif
checked(sqlite3_bind_blob(pStmt.get(), 1, key_blob.data(), key_blob.size(), SQLITE_TRANSIENT),
"bind find() key");
return pStmt;
}
/// ------------------------------------------------------
/// iterator
template <class K, class V>
sqlite3_map<K, V>::iterator::iterator(const sqlite3_map<K, V>& parent, stmt_ptr&& the_pStmt)
: parent(parent)
, which_db(parent.which_db)
, pStmt(std::move(the_pStmt))
, is_past_the_end(false)
, cached_key_valid(false)
, cached_value_valid(false)
, cached_pair_valid(false)
{
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt.get(), "construct...");
#endif
if (pStmt.get() == nullptr) {
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt.get(), "construct null/past-the-end with which_db", std::to_string(which_db));
#endif
is_past_the_end = true;
return;
}
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt.get(), "construct w/ query, which_db", std::to_string(which_db));
#endif
// start executing query
++(*this);
}
template <class K, class V>
sqlite3_map<K, V>::iterator::iterator(const iterator& o)
// same parent & which_db
: parent(o.parent)
, which_db(o.which_db)
// new statement
, pStmt(nullptr)
, is_past_the_end(false)
, cached_key_valid(false)
, cached_value_valid(false)
, cached_pair_valid(false)
{
#ifdef SQLITE3_MAP_DEBUG
log_cursor(o.pStmt.get(), "copy construct *from* this ptr, with which_db", std::to_string(which_db));
#endif
if (!o.valid()) {
// remain invalid
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt.get(), "copy construct, remaining invalid");
#endif
is_past_the_end = true;
return;
}
// seek to where the other is
pStmt = parent.prepare_find_key_blob(o.getKey());
// start executing query
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt.get(), "copy constructed *to* this ptr, seeking to other");
#endif
++(*this);
}
template <class K, class V>
sqlite3_map<K, V>::iterator::iterator(iterator&& o)
// start w/ placeholder empty statement
: iterator(o.parent, o.parent.prepare_stmt(nullptr))
{
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt.get(), "move construct start");
#endif
// steal the others' values
swap(*this, o);
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt.get(), "move construct done");
#endif
}
template <class K, class V>
typename sqlite3_map<K, V>::iterator::iterator& sqlite3_map<K, V>::iterator::operator=(iterator o)
{
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt.get(), "assign start");
#endif
swap(*this, o);
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt.get(), "assign done");
#endif
return *this;
}
template <class K, class V>
sqlite3_map<K, V>::iterator::~iterator()
{
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt.get(), "destruct");
#endif
}
// ------------------------------------------------------
template <class K, class V>
typename sqlite3_map<K, V>::iterator& sqlite3_map<K, V>::iterator::operator++() {
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt.get(), "iterator++");
#endif
if (!valid()) {
throw std::runtime_error("incrementing invalid iterator");
}
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt.get(), "sqlite3_step()...");
#endif
int rc = sqlite3_step(pStmt.get());
if (rc == SQLITE_DONE) {
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt.get(), "step rc done, past the end!");
#endif
is_past_the_end = true;
} else if (rc == SQLITE_ROW) {
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt.get(), "step rc row!");
#endif
// wait before retrieving data
} else {
throw std::runtime_error("statement step error");
}
invalidateCache();
return *this;
}
template <class K, class V>
typename sqlite3_map<K, V>::value_type sqlite3_map<K, V>::iterator::operator++(int) {
auto res = *(*this);
++(*this);
return res;
}
template <class K, class V>
const typename sqlite3_map<K, V>::value_type& sqlite3_map<K, V>::iterator::operator*() const {
return getPair();
}
template <class K, class V>
const typename sqlite3_map<K, V>::value_type* sqlite3_map<K, V>::iterator::operator->() const {
return &getPair();
}
// ------------------------------------------------------
template <class K, class V>
bool sqlite3_map<K, V>::iterator::operator==(const iterator& i2) const {
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt.get(), "eq LHS");
log_cursor(i2.pStmt.get(), "eq RHS");
#endif
if (which_db != i2.which_db) { // different database
return false;
}
bool thisValid = valid();
bool thatValid = i2.valid();
// any invalid iterators are equal to each other
if (!thisValid && !thatValid) {
return true;
}
// if one is invalid but not the other, then they are inequal
if (thisValid != thatValid) {
return false;
}
// both valid - compare keys
return getKey() == i2.getKey();
}
template <class K, class V>
bool sqlite3_map<K, V>::iterator::operator!=(const iterator& i2) const {
return !this->operator==(i2);
}
/// assumes the iterator is valid
template <class K, class V>
std::string& sqlite3_map<K, V>::iterator::getKey() const {
if (!cached_key_valid) {
if (!valid()) {
throw std::runtime_error("loading key for invalid iterator");
}
cached_key = std::string((const char *)sqlite3_column_blob(pStmt.get(), 0),
sqlite3_column_bytes(pStmt.get(), 0));
cached_key_valid = true;
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt.get(), "loaded key", cached_key);
#endif
}
return cached_key;
}
template <class K, class V>
std::string& sqlite3_map<K, V>::iterator::getValue() const {
if (!cached_value_valid) {
if (!valid()) {
throw std::runtime_error("loading value for invalid iterator");
}
cached_value = std::string((const char *)sqlite3_column_blob(pStmt.get(), 1),
sqlite3_column_bytes(pStmt.get(), 1));
cached_value_valid = true;
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt.get(), "loaded value", cached_value);
#endif
}
return cached_value;
}
template <class K, class V>
typename sqlite3_map<K, V>::value_type& sqlite3_map<K, V>::iterator::getPair() const {
if (!cached_pair_valid) {
cached_pair = value_type(parent.load_key(getKey()), parent.load_value(getValue()));
cached_pair_valid = true;
}
return cached_pair;
}
template <class K, class V>
void sqlite3_map<K, V>::iterator::invalidateCache() const {
cached_key_valid = false;
cached_value_valid = false;
cached_pair_valid = false;
}
// ------------------------------------------------------
template <class K, class V>
typename sqlite3_map<K, V>::iterator sqlite3_map<K, V>::iterator::erase()
{
if (!valid()) {
throw std::runtime_error("deleting invalid iterator");
}
auto delStmt = parent.prepare_stmt("DELETE FROM the_table WHERE key = ?;");
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt.get(), "binding key", getKey());
#endif
parent.checked(sqlite3_bind_blob(delStmt.get(), 1, getKey().data(), getKey().size(), SQLITE_STATIC),
"bind erase() key");
parent.checked_exec(delStmt);
invalidateCache();
is_past_the_end = true; // done with this
// return invalid iterator (i.e. copy of this)
return *this;
}
template <class K, class V>
bool sqlite3_map<K, V>::iterator::valid() const
{
if (which_db != parent.which_db) { // different database
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt.get(), "valid(): invalid which_db from parent", std::to_string(which_db));
#endif
return false;
}
if (is_past_the_end) {
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt.get(), "valid(): is past the end");
#endif
return false;
}
if (pStmt.get() == nullptr) {
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt.get(), "valid(): is nullptr");
#endif
return false;
}
#ifdef SQLITE3_MAP_DEBUG
log_cursor(pStmt.get(), "valid(): is valid");
#endif
return true;
}
/// ------------------------------------------------------
/// funcs using iterators
template <class K, class V>
typename sqlite3_map<K, V>::iterator sqlite3_map<K, V>::cbegin() const {
return iterator(*this, prepare_stmt("SELECT key, value FROM the_table;"));
}
template <class K, class V>
typename sqlite3_map<K, V>::iterator sqlite3_map<K, V>::cend() const {
return iterator(*this, prepare_stmt(nullptr));
}
template <class K, class V>
typename sqlite3_map<K, V>::iterator sqlite3_map<K, V>::find(const K& key) const {
// get it to >= key
#ifdef SQLITE3_MAP_DEBUG
log_cursor((void *)0x666, "find() making iterator w/ prepare_find_key_blob...");
#endif
auto it = iterator(*this, prepare_find_key_blob(store_key(key)));
#ifdef SQLITE3_MAP_DEBUG
log_cursor(it.pStmt.get(), "find() made iterator...");
#endif
return it;
}
}