-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraildbmodule.c
714 lines (651 loc) · 20.4 KB
/
traildbmodule.c
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
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
#include <Python.h>
#include <traildb.h>
#include <stdio.h>
#include <stdint.h>
// Structures for each type we have.
// Forward declarations (some structures refer to each other).
struct sTrailDBObject;
struct sTrailDBCursorObject;
struct sTrailDBTrailsIteratorObject;
struct sTrailDBEventObject;
struct sTrailDBFiltersAttrsObject;
typedef struct sTrailDBObject {
PyObject_HEAD
tdb* t;
uint64_t num_trails;
uint64_t num_fields;
PyObject* field_attrs;
} TrailDBObject;
typedef struct sTrailDBCursorObject {
PyObject_HEAD
TrailDBObject* t;
tdb_cursor* c;
uint64_t timestamp;
uint64_t trail_id;
int exhausted;
} TrailDBCursorObject;
typedef struct sTrailDBTrailsIteratorObject {
PyObject_HEAD
uint64_t trail_id;
TrailDBObject* t;
TrailDBCursorObject* c;
} TrailDBTrailsIteratorObject;
typedef struct sTrailDBEventObject {
PyObject_HEAD
TrailDBObject* t;
uint64_t timestamp;
uint64_t trail_id;
tdb_item* items;
} TrailDBEventObject;
typedef struct sTrailDBFiltersAttrsObject {
PyObject_HEAD
PyDictObject* attrs;
} TrailDBFieldAttrsObject;
// Declarations for all our functions.
// init functions
static int
ctraildb_TrailDB_init(PyObject *self_pobj, PyObject *args, PyObject* kwds);
static int
ctraildb_TrailDBCursor_init(PyObject *self_pobj, PyObject *args, PyObject* kwds);
// del functions
static void ctraildb_TrailDB_del(PyObject *self_pobj);
static void ctraildb_TrailDBCursor_del(PyObject *self_pobj);
static void ctraildb_TrailDBTrailsIterator_del(PyObject* self_pobj);
static void ctraildb_TrailDBEvent_del(PyObject* self_pobj);
static void ctraildb_TrailDBFieldAttrs_del(PyObject* self);
// iterating
static PyObject *
ctraildb_TrailDBTrailsIterator_iter(PyObject* self_pobj);
static PyObject *
ctraildb_TrailDBTrailsIterator_iternext(PyObject* self_pobj);
static PyObject *
ctraildb_TrailDBCursor_iter(PyObject* self_pobj);
static PyObject *
ctraildb_TrailDBCursor_iternext(PyObject* self_pobj);
// methods for TrailDB
static PyObject *
ctraildb_TrailDB_trails(PyObject* self_pobj, PyObject* dummy);
static PyObject *
ctraildb_TrailDB_get_uuid(PyObject* self_pobj, PyObject* arg);
static PyObject *
ctraildb_TrailDB_get_trail_id(PyObject* self_pobj, PyObject* arg);
static PyObject *
ctraildb_TrailDB_num_trails(PyObject* self_pobj, void* dummy);
static Py_ssize_t ctraildb_TrailDB_sq_length(PyObject* self_pobj);
static PyObject *
ctraildb_TrailDB_cursor(PyObject* self_pobj, PyObject* arg);
// methods for TrailDBCursor
static PyObject *
ctraildb_TrailDBCursor_get_trail(PyObject* self_pobj, PyObject* trail_id_pobj);
static PyObject *
ctraildb_TrailDBEvent_getattro(PyObject* self_pobj, PyObject* attr_name);
// misc functions
static signed long
load_to_attr_cache(TrailDBEventObject* self, PyObject* attr_name);
// Static definitions
static PyGetSetDef ctraildb_TrailDB_getsets[] = {
{ .name = "num_trails",
.get = ctraildb_TrailDB_num_trails },
NULL
};
static PyMethodDef ctraildb_TrailDBCursor_methods[] = {
{ .ml_name = "get_trail",
.ml_flags = METH_O,
.ml_meth = ctraildb_TrailDBCursor_get_trail,
.ml_doc = "Set the cursor to a certain trail in the TrailDB"
},
NULL,
};
static PyTypeObject TrailDBCursorType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "ctraildb.TrailDBCursor",
.tp_doc = "TrailDB cursor",
.tp_basicsize = sizeof(TrailDBCursorObject),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_new = PyType_GenericNew,
.tp_init = ctraildb_TrailDBCursor_init,
.tp_dealloc = ctraildb_TrailDBCursor_del,
.tp_iter = ctraildb_TrailDBCursor_iter,
.tp_iternext = ctraildb_TrailDBCursor_iternext,
.tp_methods = ctraildb_TrailDBCursor_methods,
};
static PyTypeObject TrailDBTrailsIteratorType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "ctraildb.TrailDBTrailsIterator",
.tp_doc = "Iterator over all trails in a TrailDB",
.tp_basicsize = sizeof(TrailDBTrailsIteratorObject),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_new = PyType_GenericNew,
.tp_dealloc = ctraildb_TrailDBTrailsIterator_del,
.tp_iter = ctraildb_TrailDBTrailsIterator_iter,
.tp_iternext = ctraildb_TrailDBTrailsIterator_iternext,
};
static PyTypeObject TrailDBEventType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "ctraildb.TrailDBEvent",
.tp_doc = "Event in a TrailDB",
.tp_basicsize = sizeof(TrailDBEventObject),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_new = PyType_GenericNew,
.tp_dealloc = ctraildb_TrailDBEvent_del,
.tp_getattro = ctraildb_TrailDBEvent_getattro,
};
static PyTypeObject TrailDBFieldAttrsType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "ctraildb.TrailDBFieldAttrs",
.tp_doc = "Dictionary of attribute names to field indices (internal)",
.tp_basicsize = sizeof(TrailDBFieldAttrsObject),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_new = PyType_GenericNew,
.tp_dealloc = ctraildb_TrailDBFieldAttrs_del,
.tp_getattro = PyObject_GenericGetAttr,
.tp_setattro = PyObject_GenericSetAttr,
.tp_dictoffset = -sizeof(PyDictObject*),
};
static PyMethodDef ctraildb_TrailDB_methods[] = {
{ .ml_name = "trails",
.ml_meth = ctraildb_TrailDB_trails,
.ml_flags = METH_NOARGS,
.ml_doc = "Return an iterator over all trails in the TrailDB." },
{ .ml_name = "get_uuid",
.ml_flags = METH_O,
.ml_meth = ctraildb_TrailDB_get_uuid,
.ml_doc = "Get the UUID of some Trail ID." },
{ .ml_name = "get_trail_id",
.ml_flags = METH_O,
.ml_meth = ctraildb_TrailDB_get_trail_id,
.ml_doc = "Get the ID of some UUID. Raises IndexError if it's not in TrailDB." },
{ .ml_name = "cursor",
.ml_flags = METH_NOARGS,
.ml_meth = ctraildb_TrailDB_cursor,
.ml_doc = "Creates a cursor for this TrailDB." },
NULL
};
static PySequenceMethods TrailDBSequenceMethods = {
.sq_length = ctraildb_TrailDB_sq_length,
};
static PyTypeObject TrailDBObjectType = {
PyVarObject_HEAD_INIT(NULL, 0)
.tp_name = "ctraildb.TrailDB",
.tp_doc = "TrailDB handle",
.tp_basicsize = sizeof(TrailDBObject),
.tp_itemsize = 0,
.tp_flags = Py_TPFLAGS_DEFAULT,
.tp_new = PyType_GenericNew,
.tp_init = ctraildb_TrailDB_init,
.tp_dealloc = ctraildb_TrailDB_del,
.tp_getset = ctraildb_TrailDB_getsets,
.tp_methods = ctraildb_TrailDB_methods,
.tp_as_sequence = &TrailDBSequenceMethods,
};
static PyObject *
ctraildb_TrailDBEvent_getattro(PyObject* self_pobj, PyObject* attr_name)
{
signed long field_id = -1;
TrailDBEventObject* self = (TrailDBEventObject*) self_pobj;
PyObject* ret = PyObject_GetAttr(self->t->field_attrs, attr_name);
if (!ret) {
if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
PyErr_Clear();
field_id = load_to_attr_cache(self, attr_name);
if (field_id == -1) {
return NULL;
}
} else {
return NULL;
}
} else {
field_id = PyLong_AsLong(ret);
if (field_id == -1 && PyErr_Occurred()) {
return NULL;
}
}
switch (field_id) {
case 0: // uuid
{
const uint8_t* uuid = tdb_get_uuid(self->t->t, self->trail_id);
uint8_t hexuuid[32];
tdb_uuid_hex(uuid, hexuuid);
return PyBytes_FromStringAndSize((char*) hexuuid, 32);
}
case 1: // time
return PyLong_FromUnsignedLongLong((unsigned long long) self->timestamp);
break;
default:
{
field_id -= 3;
size_t len;
const char* val = tdb_get_item_value(self->t->t, self->items[field_id], &len);
return PyBytes_FromStringAndSize(val, len);
}
}
return ret;
}
static signed long
load_to_attr_cache(TrailDBEventObject* self, PyObject* attr_name)
{
#if PY_MAJOR_VERSION >= 3
const char* attr_name_cstr = PyUnicode_AsUTF8(attr_name);
if (!attr_name_cstr) {
return -1;
}
#else
const char* attr_name_cstr = PyBytes_AsString(attr_name);
if (!attr_name_cstr) {
return -1;
}
#endif
if (!strcmp(attr_name_cstr, "uuid")) {
return 0;
}
if (!strcmp(attr_name_cstr, "time")) {
return 1;
}
TrailDBObject* t = self->t;
for (int i = 0; i < (int) t->num_fields-1; ++i) {
if (!strcmp(tdb_get_field_name(t->t, (tdb_field) i), attr_name_cstr)) {
PyObject* val = PyLong_FromLong((long) (i+2));
if (!val) {
return -1;
}
if (PyObject_SetAttr(self->t->field_attrs, attr_name, val) < 0) {
return -1;
}
return (signed long) (i+2);
}
}
char errstr[500];
snprintf(errstr, 499, "No such field in TrailDB: '%s'", attr_name_cstr);
errstr[499] = 0;
PyErr_SetString(PyExc_AttributeError, errstr);
return -1;
}
static void ctraildb_TrailDBEvent_del(PyObject* self_pobj)
{
TrailDBEventObject* self = (TrailDBEventObject*) self_pobj;
if (self->items) {
PyMem_Free(self->items);
self->items = NULL;
}
if (self->t) {
Py_DECREF(self->t);
self->t = NULL;
}
PyObject_Del(self_pobj);
}
static void ctraildb_TrailDBFieldAttrs_del(PyObject* self_pobj)
{
TrailDBFieldAttrsObject* self = (TrailDBFieldAttrsObject*) self_pobj;
if (self->attrs) {
Py_DECREF(self->attrs);
}
PyObject_Del(self);
}
static void
ctraildb_TrailDB_del(PyObject *self_pobj)
{
TrailDBObject* self = (TrailDBObject*) self_pobj;
tdb* t = NULL;
if (self->t) {
t = self->t;
self->t = NULL;
}
if (self->field_attrs) {
Py_DECREF(self->field_attrs);
self->field_attrs = NULL;
}
PyObject_Del(self_pobj);
tdb_close(t);
}
static int
ctraildb_TrailDB_init(PyObject *self_pobj, PyObject *args, PyObject* kwds)
{
static char* kwlist[] = {"path", NULL};
TrailDBObject* self = (TrailDBObject*) self_pobj;
const char* traildb_name;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "s", kwlist, &traildb_name))
return -1;
tdb* t = tdb_init();
if (!t) {
PyErr_SetString(PyExc_MemoryError, "Not enough memory to allocate 'tdb' object.");
return -1;
}
tdb_error err = tdb_open(t, traildb_name);
if (err != TDB_ERR_OK) {
char errstr[500];
snprintf(errstr, 499, "Cannot open TrailDB '%s': %s", traildb_name, tdb_error_str(err));
errstr[499] = 0;
PyErr_SetString(PyExc_OSError, errstr);
return -1;
}
TrailDBFieldAttrsObject* field_attrs = PyObject_New(TrailDBFieldAttrsObject, (PyTypeObject*) &TrailDBFieldAttrsType);
if (!field_attrs) {
tdb_close(t);
return -1;
}
field_attrs->attrs = (PyDictObject*) PyDict_New();
if (!field_attrs->attrs) {
Py_DECREF(field_attrs);
tdb_close(t);
return -1;
}
self->t = t;
self->num_trails = tdb_num_trails(t);
self->num_fields = tdb_num_fields(t)+1;
self->field_attrs = (PyObject*) field_attrs;
return 0;
}
static PyObject *
ctraildb_TrailDB_num_trails(PyObject* self_pobj, void* dummy)
{
TrailDBObject* self = (TrailDBObject*) self_pobj;
if (!self->t) {
PyErr_SetString(PyExc_ValueError, "Cannot read num_trails: TrailDB has been closed.");
return NULL;
}
uint64_t count = tdb_num_trails(self->t);
return Py_BuildValue("K", (unsigned long long) count);
}
static PyObject *
ctraildb_TrailDB_trails(PyObject* self_pobj, PyObject* dummy)
{
TrailDBObject* self = (TrailDBObject*) self_pobj;
if (!self->t) {
PyErr_SetString(PyExc_ValueError, "Cannot read num_trails: TrailDB has been closed.");
return NULL;
}
TrailDBTrailsIteratorObject* pobj = (TrailDBTrailsIteratorObject*) PyObject_New(TrailDBTrailsIteratorObject, (PyTypeObject*) &TrailDBTrailsIteratorType);
if (!pobj)
return NULL;
TrailDBCursorObject* c = (TrailDBCursorObject*) PyObject_New(TrailDBCursorObject, &TrailDBCursorType);
if (!c) {
Py_DECREF(pobj);
return NULL;
}
tdb_cursor* cur = tdb_cursor_new(self->t);
if (!cur) {
Py_DECREF(pobj);
Py_DECREF(c);
return NULL;
}
Py_INCREF(self);
c->t = self;
c->c = cur;
c->exhausted = 1;
c->trail_id = 0;
pobj->t = self;
Py_INCREF(self);
pobj->c = c;
return (PyObject*) pobj;
}
static PyObject *
ctraildb_TrailDB_get_uuid(PyObject* self_pobj, PyObject* arg)
{
TrailDBObject* self = (TrailDBObject*) self_pobj;
signed long trail_id = PyLong_AsLong(arg);
if (trail_id == -1 && PyErr_Occurred()) {
return NULL;
}
if (trail_id < 0 || trail_id >= (signed long) self->num_trails) {
PyErr_SetString(PyExc_ValueError, "Trail ID is outside of range.");
return NULL;
}
const uint8_t* uuid = tdb_get_uuid(self->t, trail_id);
uint8_t hexuuid[32];
tdb_uuid_hex(uuid, hexuuid);
return PyBytes_FromStringAndSize((char*) hexuuid, 32);
}
static PyObject *
ctraildb_TrailDB_get_trail_id(PyObject* self_pobj, PyObject* arg)
{
TrailDBObject* self = (TrailDBObject*) self_pobj;
char* uuid_raw;
Py_ssize_t uuid_len;
if (PyBytes_AsStringAndSize(arg, &uuid_raw, &uuid_len) < 0) {
return NULL;
}
if (uuid_len != 32) {
PyErr_SetString(PyExc_ValueError, "Given UUID does not is not 32 bytes in its hex-encoded form.");
return NULL;
}
uint8_t uuid16[16];
tdb_error err = tdb_uuid_raw((uint8_t*) uuid_raw, uuid16);
if (err != TDB_ERR_OK) {
PyErr_SetString(PyExc_ValueError, "Given UUID is not valid hex string.");
return NULL;
}
uint64_t trail_id;
err = tdb_get_trail_id(self->t, uuid16, &trail_id);
if (err != TDB_ERR_OK) {
PyErr_SetString(PyExc_IndexError, "Cannot find UUID in TrailDB.");
return NULL;
}
return PyLong_FromUnsignedLongLong((unsigned long long) trail_id);
}
static PyObject *
ctraildb_TrailDB_cursor(PyObject* self_pobj, PyObject* arg)
{
TrailDBObject* self = (TrailDBObject*) self_pobj;
TrailDBCursorObject* c = PyObject_New(TrailDBCursorObject, &TrailDBCursorType);
if (!c)
return NULL;
tdb_cursor* cur = tdb_cursor_new(self->t);
if (!cur) {
Py_DECREF(c);
PyErr_SetString(PyExc_MemoryError, "Not enough memory to allocate 'TrailDBCursor' object.");
return NULL;
}
Py_INCREF(self);
c->t = self;
c->c = cur;
c->exhausted = 1;
c->trail_id = 0;
return (PyObject*) c;
}
// Function implementations.
static Py_ssize_t ctraildb_TrailDB_sq_length(PyObject* self_pobj)
{
TrailDBObject* self = (TrailDBObject*) self_pobj;
if (!self->t) {
PyErr_SetString(PyExc_ValueError, "Cannot read num_trails: TrailDB has been closed.");
return -1;
}
return tdb_num_trails(self->t);
}
static PyObject *
ctraildb_TrailDBTrailsIterator_iter(PyObject* self_pobj)
{
TrailDBTrailsIteratorObject* self = (TrailDBTrailsIteratorObject*) self_pobj;
self->trail_id = 0;
self->c->trail_id = 0;
Py_INCREF(self);
return (PyObject*) self;
}
static PyObject *
ctraildb_TrailDBTrailsIterator_iternext(PyObject* self_pobj)
{
TrailDBTrailsIteratorObject* self = (TrailDBTrailsIteratorObject*) self_pobj;
if (self->trail_id < self->t->num_trails) {
self->c->trail_id = self->trail_id;
self->trail_id++;
const uint8_t* uuid = tdb_get_uuid(self->t->t, self->c->trail_id);
uint8_t hexuuid[32];
tdb_uuid_hex(uuid, hexuuid);
#if PY_MAJOR_VERSION >= 3
return Py_BuildValue("y#O", (char*) hexuuid, 32, (PyObject*) self->c);
#else
return Py_BuildValue("s#O", (char*) hexuuid, 32, (PyObject*) self->c);
#endif
}
return NULL;
}
static void
ctraildb_TrailDBTrailsIterator_del(PyObject* self_pobj)
{
TrailDBTrailsIteratorObject* self = (TrailDBTrailsIteratorObject*) self_pobj;
if (self->c) {
Py_DECREF(self->c);
self->c = NULL;
}
if (self->t) {
Py_DECREF(self->t);
self->t = NULL;
}
PyObject_Del(self_pobj);
}
static void
ctraildb_TrailDBCursor_del(PyObject *self_pobj)
{
TrailDBCursorObject* self = (TrailDBCursorObject*) self_pobj;
if (self->c) {
tdb_cursor_free(self->c);
self->c = NULL;
}
if (self->t) {
Py_DECREF(self->t);
self->t = NULL;
}
PyObject_Del(self_pobj);
}
static int
ctraildb_TrailDBCursor_init(PyObject *self_pobj, PyObject *args, PyObject* kwds)
{
static char* kwlist[] = {"traildb", NULL};
TrailDBCursorObject* self = (TrailDBCursorObject*) self_pobj;
TrailDBObject* tobj;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "O!", kwlist, &TrailDBObjectType, &tobj))
return -1;
tdb_cursor* c = tdb_cursor_new(tobj->t);
if (!c) {
PyErr_SetString(PyExc_MemoryError, "Not enough memory to allocate 'TrailDBCursor' object.");
return -1;
}
Py_INCREF(tobj);
self->t = tobj;
self->c = c;
self->exhausted = 1;
self->trail_id = 0;
return 0;
}
static PyObject *
ctraildb_TrailDBCursor_iter(PyObject* self_pobj)
{
TrailDBCursorObject* self = (TrailDBCursorObject*) self_pobj;
self->exhausted = 0;
if (self->trail_id >= self->t->num_trails) {
PyErr_SetString(PyExc_KeyError, "Attempt to iterate a TrailDBCursor beyond allowed trail ID range.");
return NULL;
}
tdb_error err = tdb_get_trail(self->c, self->trail_id);
if (err != TDB_ERR_OK) {
char errstr[500];
snprintf(errstr, 499, "tdb_get_trail() failed: %s", tdb_error_str(err));
errstr[499] = 0;
PyErr_SetString(PyExc_OSError, errstr);
return NULL;
}
Py_INCREF(self_pobj);
return self_pobj;
}
static PyObject *
ctraildb_TrailDBCursor_iternext(PyObject* self_pobj)
{
TrailDBCursorObject* self = (TrailDBCursorObject*) self_pobj;
const tdb_event* ev = tdb_cursor_next(self->c);
if (ev) {
TrailDBEventObject* evobj = (TrailDBEventObject*) PyObject_New(TrailDBEventObject, &TrailDBEventType);
if (!evobj) {
return NULL;
}
#if PY_MAJOR_VERSION >= 3
evobj->items = PyMem_Calloc(self->t->num_fields, sizeof(tdb_item));
#else
evobj->items = PyMem_Malloc(self->t->num_fields * sizeof(tdb_item));
#endif
if (!evobj->items) {
Py_DECREF(evobj);
PyErr_SetString(PyExc_MemoryError, "Cannot allocate TDB event.");
return NULL;
}
memcpy(evobj->items, ev->items, self->t->num_fields*sizeof(tdb_item));
evobj->trail_id = self->trail_id;
evobj->timestamp = ev->timestamp;
evobj->t = self->t;
Py_INCREF(self->t);
return (PyObject*) evobj;
} else {
return NULL;
}
}
static PyObject *
ctraildb_TrailDBCursor_get_trail(PyObject* self_pobj, PyObject* trail_id_pobj)
{
TrailDBCursorObject* self = (TrailDBCursorObject*) self_pobj;
long long tid = PyLong_AsLongLong(trail_id_pobj);
if (tid == -1 && PyErr_Occurred()) {
return NULL;
}
self->trail_id = tid;
Py_RETURN_NONE;
}
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef ctraildbmodule = {
PyModuleDef_HEAD_INIT,
.m_name = "ctraildb",
.m_doc = NULL,
.m_size = -1,
};
#endif
#if PY_MAJOR_VERSION >= 3
PyMODINIT_FUNC
PyInit_ctraildb(void)
#else
PyMODINIT_FUNC
initctraildb(void)
#endif
{
PyObject* m;
#if PY_MAJOR_VERSION >= 3
if (PyType_Ready(&TrailDBObjectType) < 0)
return NULL;
if (PyType_Ready(&TrailDBCursorType) < 0)
return NULL;
if (PyType_Ready(&TrailDBEventType) < 0)
return NULL;
if (PyType_Ready(&TrailDBFieldAttrsType) < 0)
return NULL;
#else
if (PyType_Ready(&TrailDBObjectType) < 0)
return;
if (PyType_Ready(&TrailDBCursorType) < 0)
return;
if (PyType_Ready(&TrailDBEventType) < 0)
return;
if (PyType_Ready(&TrailDBFieldAttrsType) < 0)
return;
#endif
#if PY_MAJOR_VERSION >= 3
m = PyModule_Create(&ctraildbmodule);
if (m == NULL)
return NULL;
#else
m = Py_InitModule3("ctraildb", NULL, NULL);
if (m == NULL)
return;
#endif
Py_INCREF(&TrailDBObjectType);
Py_INCREF(&TrailDBCursorType);
Py_INCREF(&TrailDBEventType);
Py_INCREF(&TrailDBFieldAttrsType);
PyModule_AddObject(m, "TrailDB", (PyObject*) &TrailDBObjectType);
PyModule_AddObject(m, "TrailDBCursor", (PyObject*) &TrailDBCursorType);
PyModule_AddObject(m, "TrailDBEvent", (PyObject*) &TrailDBEventType);
PyModule_AddObject(m, "TrailDBFieldAttrs", (PyObject*) &TrailDBFieldAttrsType);
#if PY_MAJOR_VERSION >= 3
return m;
#endif
}