-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathriba.cpp
394 lines (332 loc) · 10.3 KB
/
riba.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
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
#include <cstdlib>
#include <cmath>
#include <string>
#include <cctype>
#include <vector>
#include <sstream>
#include <iostream>
#include <stdexcept>
#include <leveldb/db.h>
#include <leveldb/write_batch.h>
#include "riba.h"
namespace {
/* A single leveldb instance */
leveldb::DB* ldb = NULL;
/* A batch of updates */
bool should_batch = false;
leveldb::WriteBatch lbatch;
/* A DB snapshot */
bool exists_snapshot = false;
leveldb::ReadOptions loptions;
void clear_db_state()
{
if(!ldb) return;
// Clear any pending batches
if(should_batch)
{
should_batch = false;
lbatch.Clear();
}
// Clear any snapshots
if(exists_snapshot)
{
exists_snapshot = false;
ldb->ReleaseSnapshot(loptions.snapshot);
}
}
bool get_confirmation(const std::string& question,
const std::string& yes,
const std::string& no)
{
std::string answer;
std::cout << question;
std::cin >> answer;
return (answer == yes);
}
/* Is this slice a string? */
bool is_string(const char* str, size_t len)
{
if(!str) return false;
for(size_t i=0; i<len; i++)
{
if( !isprint( str[i]) )
return false;
}
return true;
}
char from_hex(char hbyte)
{
char outbyte = -1;
if(isalpha(hbyte))
{
hbyte = tolower(hbyte);
outbyte = 10 + (hbyte - 'a');
}
else if(isdigit(hbyte))
{
outbyte = hbyte - '0';
}
return outbyte;
}
// Note: The caller needs to free the underlying memory after
// using the returned slice
leveldb::Slice make_slice(const char* sdata)
{
int i=0, len = strlen(sdata);
// If the data is an integer, handle it accordingly
for(i=0; i<len && isdigit(sdata[i]); i++);
if(i == len)
{
std::stringstream ss;
int *number = (int*)malloc(sizeof(int));
ss << sdata;
ss >> (*number);
return leveldb::Slice((const char*)number, sizeof(int));
}
// If the data is a hex string, create the appropriate bytes
if(len > 2 && sdata[0] == '0' && (sdata[1] == 'x' || sdata[1] == 'X'))
{
int hlen = 0, j = 0;
char* hdata;
sdata += 2, len -=2; // Skip past 0x
hlen = (int)ceil(len/2.0);
hdata = (char*)malloc(hlen);
memset(hdata, 0, hlen);
for(int i=len-1; i>=0; i-=2)
{
char nibble_low = from_hex(sdata[i]);
char nibble_high = (i>0) ? from_hex(sdata[i-1]) : 0;
// If neither character is a valid hex character, bail out
if(nibble_low == -1 || nibble_high == -1)
{
free(hdata);
throw(std::runtime_error("Please enter a valid hex string"));
}
hdata[j++] = nibble_high*16 + nibble_low;
}
return leveldb::Slice(hdata, hlen);
}
// Else the data is just a string
return leveldb::Slice( strdup(sdata) );
}
std::string to_print(const char* stream, size_t len)
{
static char hexmap[] = { '0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
if(is_string(stream, len))
return std::string(stream, len);
// Treat it as a byte stream
std::string outs("0x");
for(int i=len-1; i>=0; i--)
{
char hb = stream[i];
outs += hexmap[(hb & 0xf0) >> 4];
outs += hexmap[hb & 0x0f];
}
return outs;
}
std::ostream& operator <<(std::ostream& os, leveldb::Slice slice)
{
os << to_print(slice.data(), slice.size());
return os;
}
}
void leveldb_open(const char* name)
{
leveldb::Options options;
options.create_if_missing = true;
// Close the previous running instance
if(ldb)
{
leveldb_close();
}
leveldb::Status s = leveldb::DB::Open(options, name, &ldb);
if(!s.ok())
{
std::cerr << "open: " << s.ToString() << std::endl;
ldb = NULL;
}
}
void leveldb_close()
{
if(ldb)
{
clear_db_state();
delete ldb;
ldb = NULL;
}
}
void leveldb_get(const char* key)
{
if(!ldb) return;
std::string value;
leveldb::ReadOptions options = leveldb::ReadOptions();
leveldb::Slice key_slice = make_slice(key);
if(exists_snapshot)
{
options.snapshot = loptions.snapshot;
}
leveldb::Status s = ldb->Get(options, key_slice, &value);
if(s.ok())
std::cout << to_print(value.c_str(), value.size()) << std::endl;
else
std::cerr << s.ToString() << std::endl;
free(const_cast<char*>(key_slice.data()));
}
void leveldb_put(const char* key, const char* value)
{
if(!ldb) return;
leveldb::Slice key_slice = make_slice(key);
leveldb::Slice value_slice = make_slice(value);
if(should_batch)
{
lbatch.Put(key_slice, value_slice);
}
else
{
leveldb::Status s = ldb->Put(leveldb::WriteOptions(), key_slice, value_slice);
if(!s.ok())
std::cerr << s.ToString() << std::endl;
}
free(const_cast<char*>(key_slice.data()));
free(const_cast<char*>(value_slice.data()));
}
void leveldb_delete(const char* key)
{
if(!ldb) return;
leveldb::Slice key_slice = make_slice(key);
if(should_batch)
{
lbatch.Delete(key_slice);
return;
}
else
{
leveldb::Status s = ldb->Delete(leveldb::WriteOptions(), key_slice);
if(!s.ok())
std::cerr << s.ToString() << std::endl;
}
free(const_cast<char*>(key_slice.data()));
}
void leveldb_start_batch()
{
if(!ldb || should_batch) return;
should_batch = true;
lbatch.Clear();
}
void leveldb_commit_batch()
{
if(!ldb || !should_batch) return;
leveldb::Status s = ldb->Write(leveldb::WriteOptions(), &lbatch);
if(!s.ok())
{
std::cerr << s.ToString() << ". Batch cleared" << std::endl;
}
should_batch = false;
lbatch.Clear();
}
void leveldb_snap()
{
if(!ldb || exists_snapshot) return;
exists_snapshot = true;
loptions.snapshot = ldb->GetSnapshot();
}
void leveldb_unsnap()
{
if(!ldb) return;
if(exists_snapshot)
{
ldb->ReleaseSnapshot(loptions.snapshot);
exists_snapshot = false;
}
}
void leveldb_print()
{
if(!ldb) return;
leveldb::Iterator* it = ldb->NewIterator(leveldb::ReadOptions());
for(it->SeekToFirst(); it->Valid(); it->Next())
{
std::cout << it->key() << " : " << it->value() << std::endl;
}
delete it;
}
void leveldb_compact()
{
if(!ldb) return;
ldb->CompactRange(NULL, NULL);
}
// Currently an inefficient way of printing out the total number of elements
// in the database, until a more efficient method can be found
void leveldb_count()
{
if(!ldb) return;
size_t count = 0;
leveldb::Iterator* it = ldb->NewIterator(leveldb::ReadOptions());
for(it->SeekToFirst(); it->Valid(); it->Next(), count++);
std::cout << count << std::endl;
delete it;
}
void leveldb_exit(void)
{
std::string exitString("A batch with uncommitted updates might exist. Really exit? [y/n] ");
if(should_batch && get_confirmation(exitString, "y", "n")==false)
{
return;
}
leveldb_close();
exit(0);
}
void leveldb_help(void)
{
std::cout << "The following list of commands are supported in riba:" << std::endl;
std::cout << "open 'dir' - Open/Create a database at the specified directory" << std::endl;
std::cout << "close - Close the database" << std::endl;
std::cout << "get <key> - Get value for <key>" << std::endl;
std::cout << "put <key> <value> - Insert key-value pair" << std::endl;
std::cout << "delete <key> - Delete value at <key>" << std::endl;
std::cout << "batch - Start a new batch" << std::endl;
std::cout << "commit - Commit the batch of operations" << std::endl;
std::cout << "snap - Take a snapshot of the database" << std::endl;
std::cout << "unsnap - Delete the snapshot" << std::endl;
std::cout << "print - Print the contents of the database" << std::endl;
std::cout << "count - Print the number of elements in the database" << std::endl;
std::cout << "help - Print this help" << std::endl;
std::cout << "about - About riba" << std::endl;
std::cout << "exit - Exit" << std::endl;
}
void leveldb_about(void)
{
std::cout <<
"\n riba - A REPL for leveldb"
"\n"
"\n"
" ... M \n"
" .M.M M MM M MMMMMMMMM. . \n"
" MMMM. . M M. M MMM.. ...MMMM \n"
" M MMM . M M M..M MM ..MMM \n"
" MMM..M M.M M.M. M.M. MMMM.. MMM \n"
"MMM.M M .MMMM ..M.M M. MMM MMMMMMMMMM. MM. \n"
"M M M MM M.M .M M.M .MM . MMMMMMMM M .MM \n"
" .MM M..MM..M M.M .M M . M MMMMM . MM \n"
" .MM M. M . .M MM . .MM M . .M \n"
" M M M M M MMN M. M MM \n"
" M .M . M..M. MMM M. M. MM .MM \n"
" .M . M..M.M . M M ...M. MM.\n"
" .M M. M M. M M. M . MM \n"
" M MMMMM M . . MMM . . . MMMMM \n"
" .M M M M MMMMMMM \n"
" M M. M. . M ..MMMM \n"
" .M. M M MM \n"
" MM . M . MMM. \n"
" M M. .M..MMMMM MMMM \n"
" MMMM M. . M M MMM . M M \n"
" M .M MM M.... . .M \n"
" .M MM .MM. MMMM.. M \n"
" M .MMM .MMM. \n"
" .MMMM MMMM \n"
" MMMMMM ..MMMMMM \n"
" MMM..M. MMMMMMMMMMMMMMMMMMMMM \n"
" MMM..M M M. .. .. \n"
" MM.MM. \n"
" MM \n";
}