-
Notifications
You must be signed in to change notification settings - Fork 1
/
engine.h
221 lines (174 loc) · 8.09 KB
/
engine.h
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
#pragma once
#include "txn.h"
#include "varstr.h"
#include "engine_internal.h"
#include "../benchmarks/record/encoder.h"
#if __clang__
#include <experimental/coroutine>
using std::experimental::coroutine_handle;
using std::experimental::noop_coroutine;
using std::experimental::suspend_always;
using std::experimental::suspend_never;
#else
#include <coroutine>
using std::coroutine_handle;
using std::noop_coroutine;
using std::suspend_always;
using std::suspend_never;
#endif
namespace ermia {
extern bool is_loading;
extern std::mutex tlog_lock;
extern std::mutex receiver_fd_map_lock;
extern TATAS spinlock;
extern Spinlock myspinlock;
extern std::unordered_map<uint32_t, int> receiver_fd_map;
extern std::unordered_map<uint32_t, int> sender_idx_map;
// Get "my" own log
dlog::tls_log *GetLog();
// Get a log with a specified log id
dlog::tls_log *GetLog(uint32_t logid);
class Table;
class Engine {
private:
void LogIndexCreation(bool primary, FID table_fid, FID index_fid, const std::string &index_name);
void CreateIndex(const char *table_name, const std::string &index_name, bool is_primary);
public:
Engine();
~Engine();
// All supported index types
static const uint16_t kIndexConcurrentMasstree = 0x1;
// Create a table without any index (at least yet)
TableDescriptor *CreateTable(const char *name);
// Create the primary index for a table
inline void CreateMasstreePrimaryIndex(const char *table_name, const std::string &index_name) {
CreateIndex(table_name, index_name, true);
}
// Create a secondary masstree index
inline void CreateMasstreeSecondaryIndex(const char *table_name, const std::string &index_name) {
CreateIndex(table_name, index_name, false);
}
inline transaction *NewTransaction(uint64_t txn_flags, str_arena &arena, transaction *buf, uint32_t coro_batch_idx = 0) {
// Reset the arena here - can't rely on the benchmark/user code to do it
arena.reset();
new (buf) transaction(txn_flags, arena, coro_batch_idx);
return buf;
}
inline rc_t Commit(transaction *t) {
pcontext::lock();
rc_t rc = t->commit();
pcontext::unlock();
return rc;
}
inline void Abort(transaction *t) {
pcontext::lock();
t->Abort();
t->uninitialize();
pcontext::unlock();
}
};
// User-facing table abstraction, operates on OIDs only
class Table {
private:
TableDescriptor *td;
public:
rc_t Insert(transaction &t, varstr *value, OID *out_oid);
PROMISE(rc_t) Update(transaction &t, OID oid, varstr &value);
rc_t SyncUpdate(transaction &t, OID oid, varstr &value);
rc_t Read(transaction &t, OID oid, varstr *out_value);
PROMISE(rc_t) Remove(transaction &t, OID oid);
};
// User-facing concurrent Masstree
class ConcurrentMasstreeIndex : public OrderedIndex {
friend struct sm_log_recover_impl;
friend class sm_chkpt_mgr;
private:
ConcurrentMasstree masstree_;
struct SearchRangeCallback {
SearchRangeCallback(OrderedIndex::ScanCallback &upcall)
: upcall(&upcall), return_code(rc_t{RC_FALSE}) {}
~SearchRangeCallback() {}
inline bool Invoke(const ConcurrentMasstree::string_type &k,
const varstr &v) {
return upcall->Invoke(k.data(), k.length(), v);
}
OrderedIndex::ScanCallback *upcall;
rc_t return_code;
};
struct XctSearchRangeCallback
: public ConcurrentMasstree::low_level_search_range_callback {
XctSearchRangeCallback(transaction *t, SearchRangeCallback *caller_callback)
: t(t), caller_callback(caller_callback) {}
virtual void
on_resp_node(const typename ConcurrentMasstree::node_opaque_t *n,
uint64_t version);
virtual bool invoke(const ConcurrentMasstree *btr_ptr,
const typename ConcurrentMasstree::string_type &k,
dbtuple *v,
const typename ConcurrentMasstree::node_opaque_t *n,
uint64_t version);
private:
transaction *const t;
SearchRangeCallback *const caller_callback;
};
struct PurgeTreeWalker : public ConcurrentMasstree::tree_walk_callback {
virtual void
on_node_begin(const typename ConcurrentMasstree::node_opaque_t *n);
virtual void on_node_success();
virtual void on_node_failure();
private:
std::vector<std::pair<typename ConcurrentMasstree::value_type, bool>>
spec_values;
};
static rc_t DoNodeRead(transaction *t,
const ConcurrentMasstree::node_opaque_t *node,
uint64_t version);
public:
ConcurrentMasstreeIndex(const char *table_name, bool primary)
: OrderedIndex(table_name, primary) {}
ConcurrentMasstree &GetMasstree() { return masstree_; }
inline void *GetTable() override { return masstree_.get_table(); }
// A multi-get interface using coroutines
void simple_coro_MultiGet(transaction *t, std::vector<varstr *> &keys,
std::vector<varstr *> &values,
std::vector<coroutine_handle<>> &handles);
// A multi-ops interface using coroutines
static void simple_coro_MultiOps(std::vector<rc_t> &rcs,
std::vector<coroutine_handle<ermia::coro::generator<rc_t>::promise_type>> &handles);
ermia::coro::generator<rc_t> coro_GetRecord(transaction *t, const varstr &key, varstr &value, OID *out_oid = nullptr);
ermia::coro::generator<rc_t> coro_GetRecordSV(transaction *t, const varstr &key, varstr &value, OID *out_oid = nullptr);
ermia::coro::generator<rc_t> coro_UpdateRecord(transaction *t, const varstr &key, varstr &value);
ermia::coro::generator<rc_t> coro_InsertRecord(transaction *t, const varstr &key, varstr &value, OID *out_oid = nullptr);
ermia::coro::generator<bool> coro_InsertOID(transaction *t, const varstr &key, OID oid);
ermia::coro::generator<rc_t> coro_Scan(transaction *t, const varstr &start_key, const varstr *end_key,
ScanCallback &callback, uint32_t max_keys = ~uint32_t{0});
ermia::coro::task<rc_t> flat_GetRecord(transaction *t, const varstr &key, varstr &value, OID *out_oid = nullptr);
ermia::coro::task<rc_t> task_GetRecord(transaction *t, const varstr &key, varstr &value, OID *out_oid = nullptr);
ermia::coro::task<rc_t> task_UpdateRecord(transaction *t, const varstr &key, varstr &value);
ermia::coro::task<rc_t> task_InsertRecord(transaction *t, const varstr &key, varstr &value, OID *out_oid = nullptr);
ermia::coro::task<bool> task_InsertOID(transaction *t, const varstr &key, OID oid);
ermia::coro::task<rc_t> task_Scan(transaction *t, const varstr &start_key, const varstr *end_key,
ScanCallback &callback, uint32_t max_keys = ~uint32_t{0});
PROMISE(void) GetRecord(transaction *t, rc_t &rc, const varstr &key, varstr &value, OID *out_oid = nullptr) override;
PROMISE(rc_t) UpdateRecord(transaction *t, const varstr &key, varstr &value) override;
PROMISE(rc_t) InsertRecord(transaction *t, const varstr &key, varstr &value, OID *out_oid = nullptr) override;
PROMISE(rc_t) InsertColdRecord(transaction *t, const varstr &key, varstr &value, OID *out_oid = nullptr) override;
PROMISE(rc_t) RemoveRecord(transaction *t, const varstr &key) override;
PROMISE(bool) InsertOID(transaction *t, const varstr &key, OID oid) override;
PROMISE(rc_t) Scan(transaction *t, const varstr &start_key, const varstr *end_key,
ScanCallback &callback) override;
PROMISE(rc_t) ReverseScan(transaction *t, const varstr &start_key,
const varstr *end_key, ScanCallback &callback) override;
inline size_t Size() override { return masstree_.size(); }
std::map<std::string, uint64_t> Clear() override;
inline void SetArrays(bool primary) override { masstree_.set_arrays(table_descriptor, primary); }
inline PROMISE(void)
GetOID(const varstr &key, rc_t &rc, TXN::xid_context *xc, OID &out_oid,
ConcurrentMasstree::versioned_node_t *out_sinfo = nullptr) override {
bool found = AWAIT masstree_.search(key, out_oid, xc->begin_epoch, out_sinfo);
volatile_write(rc._val, found ? RC_TRUE : RC_FALSE);
}
private:
PROMISE(bool) InsertIfAbsent(transaction *t, const varstr &key, OID oid) override;
};
} // namespace ermia