-
Notifications
You must be signed in to change notification settings - Fork 1
/
ptnk.cpp
697 lines (572 loc) · 14.1 KB
/
ptnk.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
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
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
#include "ptnk.h"
#include <errno.h>
#include <fcntl.h>
#include <memory>
using std::unique_ptr;
#define PTNK_READ_BUF_SIZE 4096
// #define PTNK_LOG_ALL_CAPI
#ifdef PTNK_LOG_ALL_CAPI
#include <stdio.h>
#define LOG_OUTF(...) do { fprintf(stdout, "ptnk CAPI: " __VA_ARGS__); fflush(stdout); } while(0)
#else
#define LOG_OUTF(...) do {} while(0)
#endif
struct ptnk_db
{
//! pointer to C++ impl.
ptnk::DB* impl;
//! error code
int ptnk_errno;
//! buffer used in read operations (mostly value)
ptnk::Buffer read_buf;
//! buffer used in read operations (mostly key)
ptnk::Buffer read_buf2;
ptnk_db(ptnk::DB* impl_)
: impl(impl_),
ptnk_errno(0),
read_buf(PTNK_READ_BUF_SIZE),
read_buf2(PTNK_READ_BUF_SIZE)
{ /* NOP */ }
~ptnk_db()
{
delete impl;
}
};
struct ptnk_tx
{
//! pointer to C++ impl.
ptnk::DB::Tx* impl;
//! error code
int ptnk_errno;
//! buffer used in read operations (mostly value)
ptnk::Buffer read_buf;
//! buffer used in read operations (mostly key)
ptnk::Buffer read_buf2;
ptnk_tx(ptnk::DB::Tx* impl_)
: impl(impl_),
ptnk_errno(0)
{ /* NOP */}
~ptnk_tx()
{
delete impl;
}
};
struct ptnk_cur
{
//! pointer to the C++ impl.
ptnk::DB::Tx::cursor_t* impl;
//! pointer to tx
ptnk_tx* tx;
//! buffer used in read operations (mostly value)
ptnk::Buffer read_buf;
//! buffer used in read operations (mostly key)
ptnk::Buffer read_buf2;
ptnk_cur(ptnk::DB::Tx::cursor_t* impl_, ptnk_tx* tx_)
: impl(impl_),
tx(tx_),
read_buf(4096),
read_buf2(4096)
{ /* NOP */ }
~ptnk_cur()
{
ptnk::DB::Tx::curClose(impl);
}
};
#define COMMON_CATCH_BLOCKS(handle) \
catch(std::exception& e) \
{ \
handle->ptnk_errno = EINVAL; \
std::cerr << __func__ << ": " << e.what() << std::endl; \
return 0; \
} \
catch(...) \
{ \
handle->ptnk_errno = EINVAL; \
std::cerr << __func__ << ": unknown exception caught" << std::endl; \
return 0; \
}
#define COMMON_CATCH_BLOCKS_DATUM(handle) \
catch(std::exception& e) \
{ \
handle->ptnk_errno = EINVAL; \
std::cerr << __func__ << ": " << e.what() << std::endl; \
ptnk_datum ret = {NULL, PTNK_ERR_TAG}; \
return ret; \
} \
catch(...) \
{ \
handle->ptnk_errno = EINVAL; \
std::cerr << __func__ << ": unknown exception caught" << std::endl; \
ptnk_datum ret = {NULL, PTNK_ERR_TAG}; \
return ret; \
}
#define COMMON_CATCH_BLOCKS_PTR(handle) \
catch(std::exception& e) \
{ \
handle->ptnk_errno = EINVAL; \
std::cerr << __func__ << ": " << e.what() << std::endl; \
return NULL; \
} \
catch(...) \
{ \
handle->ptnk_errno = EINVAL; \
std::cerr << __func__ << ": unknown exception caught" << std::endl; \
return NULL; \
}
inline
ptnk::BufferCRef
datum2CRef(ptnk_datum_t d)
{
return ptnk::BufferCRef(d.dptr, d.dsize);
}
ptnk_db_t*
ptnk_open(const char* filename, ptnk_opts_t opts, int mode)
try
{
LOG_OUTF("ptnk_open(filename = %s, opts = %d, mode = 0%o);\n", filename, opts, mode);
return new ptnk_db_t(new ptnk::DB(filename, opts, mode));;
}
catch(std::exception& e)
{
std::cerr << __func__ << ": " << e.what() << std::endl;
return NULL;
}
catch(...)
{
std::cerr << __func__ << ": unknown exception caught" << std::endl;;
return NULL;
}
ptnk_db_t*
ptnk_open_dbm(const char* file, int flags, int mode)
{
LOG_OUTF("ptnk_open_dbm(file = %s, flags = %d, mode = 0%o);\n", file, flags, mode);
ptnk_opts_t opts = 0;
if(flags & O_RDWR) opts |= ptnk::OWRITER;
if(flags & O_CREAT) opts |= ptnk::OCREATE;
if(flags & O_TRUNC) opts |= ptnk::OTRUNCATE;
if(flags & O_SYNC) opts |= ptnk::OAUTOSYNC;
return ptnk_open(file, opts, mode);
}
void
ptnk_close(ptnk_db_t* db)
{
LOG_OUTF("ptnk_close(db = %p);\n", db);
if(!db)
{
std::cerr << "ptnk_close: db is NULL" << std::endl;
return;
}
delete db;
}
int
ptnk_drop_db(const char* file)
try
{
LOG_OUTF("ptnk_drop_db(file = %s);\n", file);
ptnk::DB::drop(file);
return 1;
}
catch(std::exception& e)
{
std::cerr << __func__ << ": " << e.what() << std::endl;
return 0;
}
catch(...)
{
std::cerr << __func__ << ": unknown exception caught" << std::endl;
return 0;
}
int
ptnk_put(ptnk_db_t* db, ptnk_datum_t key, ptnk_datum_t value, int mode)
try
{
LOG_OUTF("ptnk_put(db = %p, key = {%p, %u}, value = {%p, %u}, mode = %d);\n", db, key.dptr, key.dsize, value.dptr, value.dsize, mode);
PTNK_ASSERT(db->impl);
db->impl->put(datum2CRef(key), datum2CRef(value), (ptnk::put_mode_t)mode);
return 1;
}
catch(ptnk::ptnk_duplicate_key_error&)
{
db->ptnk_errno = PTNK_EDUPKEY;
return 0;
}
COMMON_CATCH_BLOCKS(db)
int
ptnk_put_cstr(ptnk_db_t* db, const char* key, const char* value, int mode)
try
{
LOG_OUTF("ptnk_put_cstr(db = %p, key = %s, value = %s, mode = %d);\n", db, key, value, mode);
ptnk_datum_t k = {const_cast<char*>(key), static_cast<int>(::strlen(key))};
ptnk_datum_t v = {const_cast<char*>(value), static_cast<int>(::strlen(value))};
return ptnk_put(db, k, v, (ptnk::put_mode_t)mode);
}
COMMON_CATCH_BLOCKS(db)
ptnk_datum_t
ptnk_get(ptnk_db_t* db, ptnk_datum_t key)
try
{
LOG_OUTF("ptnk_get(db = %p, key = {%p, %u});\n", db, key.dptr, key.dsize);
db->impl->get(datum2CRef(key), &db->read_buf);
ptnk_datum_t ret = {db->read_buf.get(), static_cast<int>(db->read_buf.valsize())};
return ret;
}
COMMON_CATCH_BLOCKS_DATUM(db)
const char*
ptnk_get_cstr(ptnk_db_t* db, const char* key)
try
{
LOG_OUTF("ptnk_get_cstr(db = %p, key = %s);\n", db, key);
db->impl->get(ptnk::cstr2ref(key), &db->read_buf);
if(db->read_buf.valsize() >= 0)
{
db->read_buf.makeNullTerm();
LOG_OUTF("ptnk_get ret: %s\n", db->read_buf.get());
return db->read_buf.get();
}
else
{
LOG_OUTF("ptnk_get ret: (NULL)\n");
return NULL;
}
}
COMMON_CATCH_BLOCKS_PTR(db)
int
ptnk_error(ptnk_db_t* db)
{
return db->ptnk_errno;
}
int
ptnk_clearerr(ptnk_db_t* db)
{
db->ptnk_errno = 0;
return 1;
}
ptnk_tx_t*
ptnk_tx_begin(ptnk_db_t* db)
try
{
LOG_OUTF("ptnk_tx_begin(db = %p);\n", db);
return new ptnk_tx_t(db->impl->newTransaction());
}
COMMON_CATCH_BLOCKS_PTR(db)
int
ptnk_tx_end(ptnk_tx_t* tx, int commit)
try
{
LOG_OUTF("ptnk_tx_end(tx = %p, commit = %d);\n", tx, commit);
unique_ptr<ptnk_tx_t> tx_(tx);
if(commit)
{
return tx_->impl->tryCommit() ? 1 : 0;
}
else
{
// commit not tried at all
return 0;
}
}
COMMON_CATCH_BLOCKS(tx)
int
ptnk_tx_error(ptnk_tx_t* tx)
{
return tx->ptnk_errno;
}
int
ptnk_tx_put(ptnk_tx_t* tx, ptnk_datum_t key, ptnk_datum_t value, int mode)
try
{
LOG_OUTF("ptnk_tx_put(tx = %p, key = {%p, %u}, value = {%p, %u}, mode = %d);\n", tx, key.dptr, key.dsize, value.dptr, value.dsize, mode);
PTNK_ASSERT(tx->impl);
tx->impl->put(datum2CRef(key), datum2CRef(value), (ptnk::put_mode_t)mode);
return 1;
}
catch(ptnk::ptnk_duplicate_key_error&)
{
tx->ptnk_errno = PTNK_EDUPKEY;
return 0;
}
COMMON_CATCH_BLOCKS(tx)
int
ptnk_tx_put_cstr(ptnk_tx_t* tx, const char* key, const char* value, int mode)
try
{
LOG_OUTF("ptnk_tx_put_cstr(tx = %p, key = %s, value = %s, mode = %d);\n", tx, key, value, mode);
PTNK_ASSERT(tx->impl);
tx->impl->put(ptnk::cstr2ref(key), ptnk::cstr2ref(value), (ptnk::put_mode_t)mode);
return 1;
}
catch(ptnk::ptnk_duplicate_key_error&)
{
tx->ptnk_errno = PTNK_EDUPKEY;
return 0;
}
COMMON_CATCH_BLOCKS(tx)
ptnk_datum_t
ptnk_tx_get(ptnk_tx_t* tx, ptnk_datum_t key)
try
{
LOG_OUTF("ptnk_tx_get(tx = %p, key = {%p, %u});\n", tx, key.dptr, key.dsize);
tx->impl->get(datum2CRef(key), &tx->read_buf);
ptnk_datum_t ret = {tx->read_buf.get(), static_cast<int>(tx->read_buf.valsize())};
return ret;
}
COMMON_CATCH_BLOCKS_DATUM(tx)
const char*
ptnk_tx_get_cstr(ptnk_tx_t* tx, const char* key)
try
{
LOG_OUTF("ptnk_tx_get_cstr(tx = %p, key = %s);\n", tx, key);
tx->impl->get(ptnk::cstr2ref(key), &tx->read_buf);
if(tx->read_buf.valsize() >= 0)
{
tx->read_buf.makeNullTerm();
return tx->read_buf.get();
}
else
{
return NULL;
}
}
COMMON_CATCH_BLOCKS_PTR(tx)
ptnk_table_t*
ptnk_table_open(ptnk_datum_t tableid)
{
LOG_OUTF("ptnk_table_open(tableid = {%p, %d});\n", tableid.dptr, tableid.dsize);
return static_cast<ptnk_table_t*>(new ptnk::TableOffCache(datum2CRef(tableid)));
}
ptnk_table_t*
ptnk_table_open_cstr(const char* tableid)
{
LOG_OUTF("ptnk_table_open_cstr(tableid = %s);\n", tableid);
return static_cast<ptnk_table_t*>(new ptnk::TableOffCache(ptnk::cstr2ref(tableid)));
}
void
ptnk_table_close(ptnk_table_t* table)
{
LOG_OUTF("ptnk_table_close(table = %p);\n", table);
delete static_cast<ptnk::TableOffCache*>(table);
}
int
ptnk_tx_table_put(ptnk_tx_t* tx, ptnk_table_t* table, ptnk_datum_t key, ptnk_datum_t value, int mode)
try
{
LOG_OUTF("ptnk_tx_put(tx = %p, table = %p, key = {%p, %u}, value = {%p, %u}, mode = %d);\n", tx, table, key.dptr, key.dsize, value.dptr, value.dsize, mode);
PTNK_ASSERT(tx->impl);
ptnk::TableOffCache* toc = static_cast<ptnk::TableOffCache*>(table);
tx->impl->put(toc, datum2CRef(key), datum2CRef(value), (ptnk::put_mode_t)mode);
return 1;
}
catch(ptnk::ptnk_duplicate_key_error&)
{
tx->ptnk_errno = PTNK_EDUPKEY;
return 0;
}
COMMON_CATCH_BLOCKS(tx)
int
ptnk_tx_table_put_cstr(ptnk_tx_t* tx, ptnk_table_t* table, const char* key, const char* value, int mode)
try
{
LOG_OUTF("ptnk_tx_put_cstr(tx = %p, table = %p, key = %s, value = %s, mode = %d);\n", tx, table, key, value, mode);
PTNK_ASSERT(tx->impl);
ptnk::TableOffCache* toc = static_cast<ptnk::TableOffCache*>(table);
tx->impl->put(toc, ptnk::cstr2ref(key), ptnk::cstr2ref(value), (ptnk::put_mode_t)mode);
return 1;
}
catch(ptnk::ptnk_duplicate_key_error&)
{
tx->ptnk_errno = PTNK_EDUPKEY;
return 0;
}
COMMON_CATCH_BLOCKS(tx)
ptnk_datum_t
ptnk_tx_table_get(ptnk_tx_t* tx, ptnk_table_t* table, ptnk_datum_t key)
try
{
LOG_OUTF("ptnk_tx_table_get(tx = %p, table = %p, key = {%p, %u});\n", tx, table, key.dptr, key.dsize);
ptnk::TableOffCache* toc = static_cast<ptnk::TableOffCache*>(table);
tx->impl->get(toc, datum2CRef(key), &tx->read_buf);
ptnk_datum_t ret = {tx->read_buf.get(), static_cast<int>(tx->read_buf.valsize())};
return ret;
}
COMMON_CATCH_BLOCKS_DATUM(tx)
const char*
ptnk_tx_table_get_cstr(ptnk_tx_t* tx, ptnk_table_t* table, const char* key)
try
{
LOG_OUTF("ptnk_tx_table_get_cstr(tx = %p, key = %s);\n", tx, key);
ptnk::TableOffCache* toc = static_cast<ptnk::TableOffCache*>(table);
tx->impl->get(toc, ptnk::cstr2ref(key), &tx->read_buf);
if(tx->read_buf.valsize() >= 0)
{
tx->read_buf.makeNullTerm();
return tx->read_buf.get();
}
else
{
return NULL;
}
}
COMMON_CATCH_BLOCKS_PTR(tx)
ptnk_cur_t*
ptnk_cur_front(ptnk_tx_t* tx, ptnk_table_t* table)
try
{
LOG_OUTF("ptnk_cur_front(tx = %p, table = %p);\n", tx, table);
ptnk::TableOffCache* toc = static_cast<ptnk::TableOffCache*>(table);
ptnk::DB::Tx::cursor_t* ptnkcur = tx->impl->curFront(toc);
if(ptnkcur)
{
return new ptnk_cur_t(ptnkcur, tx);
}
else
{
return NULL;
}
}
COMMON_CATCH_BLOCKS_PTR(tx)
ptnk_cur_t*
ptnk_query(ptnk_tx_t* tx, ptnk_table_t* table, ptnk_datum_t key, int query_type)
try
{
LOG_OUTF("ptnk_query(tx = %p, table = %p, key = {%p, %d}, query_type = %d);\n", tx, table, key.dptr, key.dsize, query_type);
ptnk::query_t q = {datum2CRef(key), (ptnk::query_type_t)query_type};
ptnk::TableOffCache* toc = static_cast<ptnk::TableOffCache*>(table);
ptnk::DB::Tx::cursor_t* ptnkcur = tx->impl->curQuery(toc, q);
if(ptnkcur)
{
return new ptnk_cur_t(ptnkcur, tx);
}
else
{
return NULL;
}
}
COMMON_CATCH_BLOCKS_PTR(tx)
int
ptnk_cur_next(ptnk_cur_t* cur)
try
{
LOG_OUTF("ptnk_cur_next(cur = %p);\n", cur);
if(cur->tx->impl->curNext(cur->impl))
{
return 1;
}
else
{
// no more next record
return 0;
}
}
COMMON_CATCH_BLOCKS(cur->tx)
int
ptnk_cur_get(ptnk_datum_t* key, ptnk_datum_t* value, ptnk_cur_t* cur)
try
{
LOG_OUTF("ptnk_cur_get(key = %p, value = %p, cur = %p);\n", key, value, cur);
cur->tx->impl->curGet(&cur->read_buf2, &cur->read_buf, cur->impl);
key->dptr = cur->read_buf2.get();
key->dsize = cur->read_buf2.valsize();
value->dptr = cur->read_buf.get();
value->dsize = cur->read_buf.valsize();
return 1;
}
COMMON_CATCH_BLOCKS(cur->tx)
int
ptnk_cur_get_cstr(const char** key, const char** value, ptnk_cur_t* cur)
try
{
LOG_OUTF("ptnk_cur_get(key = %p, value = %p, cur = %p);\n", key, value, cur);
cur->tx->impl->curGet(&cur->read_buf2, &cur->read_buf, cur->impl);
// FIXME: handle null cases?
cur->read_buf.makeNullTerm();
cur->read_buf2.makeNullTerm();
*key = cur->read_buf2.get();
*value = cur->read_buf.get();
return 1;
}
COMMON_CATCH_BLOCKS(cur->tx)
int
ptnk_cur_put(ptnk_cur_t* cur, ptnk_datum_t value)
try
{
LOG_OUTF("ptnk_cur_put(cur = %p, value = {%p, %d});\n", cur, value.dptr, value.dsize);
cur->tx->impl->curPut(cur->impl, datum2CRef(value));
return 1;
}
COMMON_CATCH_BLOCKS(cur->tx);
int
ptnk_cur_delete(ptnk_cur_t* cur)
try
{
LOG_OUTF("ptnk_cur_delete(cur = %p);\n", cur);
if(cur->tx->impl->curDelete(cur->impl))
{
return 1;
}
else
{
return 0;
}
return 1;
}
COMMON_CATCH_BLOCKS(cur->tx);
void ptnk_cur_close(ptnk_cur_t* cur)
{
if(cur) delete cur;
}
int
ptnk_tx_table_create(ptnk_tx_t* tx, ptnk_datum_t table)
try
{
LOG_OUTF("ptnk_table_create(tx = %p, table = {%p, %u});\n", tx, table.dptr, table.dsize);
tx->impl->tableCreate(datum2CRef(table));
return 1;
}
COMMON_CATCH_BLOCKS(tx)
int
ptnk_tx_table_create_cstr(ptnk_tx_t* tx, const char* table)
try
{
LOG_OUTF("ptnk_table_create_cstr(tx = %p, table = %s);\n", tx, table);
tx->impl->tableCreate(ptnk::cstr2ref(table));
return 1;
}
COMMON_CATCH_BLOCKS(tx)
int
ptnk_tx_table_drop(ptnk_tx_t* tx, ptnk_datum_t table)
try
{
LOG_OUTF("ptnk_table_drop(tx = %p, table = {%p, %u});\n", tx, table.dptr, table.dsize);
tx->impl->tableDrop(datum2CRef(table));
return 1;
}
COMMON_CATCH_BLOCKS(tx)
int
ptnk_tx_table_drop_cstr(ptnk_tx_t* tx, const char* table)
try
{
LOG_OUTF("ptnk_table_drop_cstr(tx = %p, table = %s);\n", tx, table);
tx->impl->tableDrop(ptnk::cstr2ref(table));
return 1;
}
COMMON_CATCH_BLOCKS(tx)
const char*
ptnk_tx_table_get_name_cstr(ptnk_tx_t* tx, int idx)
try
{
LOG_OUTF("ptnk_tx_table_get_name_cstr(tx = %p, idx = %d);\n", tx, idx);
tx->impl->tableGetName(idx, &tx->read_buf);
tx->read_buf.makeNullTerm();
if(tx->read_buf.valsize() >= 0)
{
tx->read_buf.makeNullTerm();
LOG_OUTF("ptnk_tx_table_get_name_cstr ret: %s\n", tx->read_buf.get());
return tx->read_buf.get();
}
else
{
LOG_OUTF("ptnk_tx_table_get_name_cstr ret: (NULL)\n");
return NULL;
}
}
COMMON_CATCH_BLOCKS_PTR(tx)