forked from membase/ep-engine
/
sqlite-kvstore.hh
140 lines (115 loc) · 2.96 KB
/
sqlite-kvstore.hh
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
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#ifndef SQLITE_BASE_H
#define SQLITE_BASE_H 1
#include <map>
#include <vector>
#ifdef USE_SYSTEM_LIBSQLITE3
#include <sqlite3.h>
#else
#include "embedded/sqlite3.h"
#endif
#include "sqlite-pst.hh"
#include "sqlite-strategies.hh"
#include "item.hh"
class EventuallyPersistentEngine;
class EPStats;
class StrategicSqlite3 {
public:
/**
* Construct an instance of sqlite with the given database name.
*/
StrategicSqlite3(EventuallyPersistentEngine &theEngine, SqliteStrategy *s);
/**
* Cleanup.
*/
~StrategicSqlite3() {
close();
}
/**
* Reset database to a clean state.
*/
void reset();
/**
* Begin a transaction (if not already in one).
*/
void begin() {
if(!intransaction) {
execute("begin");
intransaction = true;
}
}
/**
* Commit a transaction (unless not currently in one).
*
* Returns false if the commit fails.
*/
bool commit() {
if(intransaction) {
// If commit returns -1, we're still in a transaction.
intransaction = (execute("commit") == -1);
}
// !intransaction == not in a transaction == committed
return !intransaction;
}
/**
* Rollback a transaction (unless not currently in one).
*/
void rollback() {
if(intransaction) {
intransaction = false;
execute("rollback");
}
}
/**
* Overrides set().
*/
void set(const Item &item, Callback<std::pair<bool, int64_t> > &cb);
/**
* Overrides get().
*/
void get(const std::string &key, uint64_t rowid, Callback<GetValue> &cb);
/**
* Overrides del().
*/
void del(const std::string &key, uint16_t vbucket,
Callback<bool> &cb);
bool delVBucket(uint16_t vbucket);
bool setVBState(uint16_t vbucket, const std::string &to);
std::map<uint16_t, std::string> listPersistedVbuckets(void);
/**
* Overrides dump
*/
void dump(Callback<GetValue> &cb);
private:
/**
* Shortcut to execute a simple query.
*
* @param query a simple query with no bindings to execute directly
*/
int execute(const char *query) {
PreparedStatement st(db, query);
return st.execute();
}
void insert(const Item &itm, Callback<std::pair<bool, int64_t> > &cb);
void update(const Item &itm, Callback<std::pair<bool, int64_t> > &cb);
int64_t lastRowId();
EventuallyPersistentEngine &engine;
EPStats &stats;
/**
* Direct access to the DB.
*/
sqlite3 *db;
void open() {
assert(strategy);
db = strategy->open();
intransaction = false;
}
void close() {
strategy->close();
intransaction = false;
db = NULL;
}
SqliteStrategy *strategy;
bool intransaction;
};
#endif /* SQLITE_BASE_H */