forked from wormtable/wormtable
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_vcfdbmodule.c
2576 lines (2379 loc) · 74.6 KB
/
_vcfdbmodule.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
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <Python.h>
#include <structmember.h>
#include <db.h>
#if PY_MAJOR_VERSION >= 3
#define IS_PY3K
#endif
#define ELEMENT_TYPE_CHAR 0
#define ELEMENT_TYPE_INT 1
#define ELEMENT_TYPE_FLOAT 2
#define ELEMENT_TYPE_ENUM 3
#define NUM_ELEMENTS_VARIABLE 0
#define MAX_NUM_ELEMENTS 255
#define MAX_ROW_SIZE 65536
#define MODULE_DOC \
"Low level Berkeley DB interface for vcfdb"
static PyObject *BerkeleyDatabaseError;
/* TODO:
* 1) We need much better error reporting for the parsers. These should have a
* top level variadic function that takes arguments and set the Python exception
* appropriately.
* 2) It seems we have a memory leak somewhere. If we run the test cases in a
* loop the memory usage keeps increasing. We need to track this down.
* 3) The numerical types need a little cleaning up and thought put into. We
* now have single and double floating point sizes and should export constants
* to tell what they are. There should also be some constants telling range
* of integers and so on.
* 4) Integer limits are not correct and need to be fixed, and then tested
* properly.
* 5) Insert check for duplicate column names and columns: this is a nasty bug!
*/
typedef struct Column_t {
PyObject_HEAD
PyObject *name;
PyObject *description;
PyObject *enum_values;
int element_type;
int element_size;
int num_elements;
int fixed_region_offset;
void **input_elements; /* pointer to each elements in input format */
void *element_buffer; /* parsed input elements in native CPU format */
int num_buffered_elements;
int (*string_to_native)(struct Column_t*, char *); /* DEPRECATED */
int (*python_to_native)(struct Column_t*, PyObject *);
int (*verify_elements)(struct Column_t*);
int (*pack_elements)(struct Column_t*, void *);
int (*unpack_elements)(struct Column_t*, void *);
PyObject *(*native_to_python)(struct Column_t *, int);
} Column;
typedef struct {
PyObject_HEAD
DB *primary_db;
PyObject *filename;
PyObject *columns;
Py_ssize_t cache_size;
int fixed_region_size;
int key_size;
} BerkeleyDatabase;
typedef struct {
PyObject_HEAD
BerkeleyDatabase *database;
/* arrays for double buffering */
void *key_buffer;
void *data_buffer;
int num_records;
int current_data_offset;
int current_key_offset;
int current_record_size;
DBT *key_dbts;
DBT *data_dbts;
/* end of arrays */
int max_num_records;
int data_buffer_size;
int key_buffer_size;
u_int64_t record_id;
} WriteBuffer;
typedef struct {
PyObject_HEAD
BerkeleyDatabase *database;
DB *secondary_db;
PyObject *filename;
PyObject *columns;
Py_ssize_t cache_size;
} Index;
typedef struct {
PyObject_HEAD
BerkeleyDatabase *database;
Index *index;
PyObject *columns;
DBC *cursor;
void *min_key;
uint32_t min_key_size;
void *max_key;
uint32_t max_key_size;
} RowIterator;
static void
handle_bdb_error(int err)
{
PyErr_SetString(BerkeleyDatabaseError, db_strerror(err));
}
#ifndef WORDS_BIGENDIAN
/*
* Copies n bytes of source into destination, swapping the order of the
* bytes.
*
* TODO rename to byteswap_copy
*/
static void
bigendian_copy(void* dest, void *source, size_t n)
{
size_t j = 0;
unsigned char *dest_c = (unsigned char *) dest;
unsigned char *source_c = (unsigned char *) source;
for (j = 0; j < n; j++) {
dest_c[j] = source_c[n - j - 1];
}
}
#endif
/*==========================================================
* Column object
*==========================================================
*/
/**************************************
*
* Native values to Python conversion.
*
*************************************/
static PyObject *
Column_native_to_python_int(Column *self, int index)
{
PyObject *ret = NULL;
int64_t *elements = (int64_t *) self->element_buffer;
int64_t missing_value = (-1) * (1ll << (8 * self->element_size - 1));
if (elements[index] == missing_value) {
Py_INCREF(Py_None);
ret = Py_None;
} else {
ret = PyLong_FromLongLong((long long) elements[index]);
if (ret == NULL) {
PyErr_NoMemory();
}
}
return ret;
}
static PyObject *
Column_native_to_python_float(Column *self, int index)
{
PyObject *ret = NULL;
double *elements = (double *) self->element_buffer;
/* TODO figure out if this is portable */
if (isnan(elements[index])) {
Py_INCREF(Py_None);
ret = Py_None;
} else {
ret = PyFloat_FromDouble(elements[index]);
if (ret == NULL) {
PyErr_NoMemory();
}
}
return ret;
}
static PyObject *
Column_native_to_python_char(Column *self, int index)
{
PyObject *ret = NULL;
int j = self->num_buffered_elements - 1;
char *str = (char *) self->element_buffer;
if (self->num_elements != NUM_ELEMENTS_VARIABLE) {
/* check for shortened fixed-length strings, which will be padded
* with NULLs */
while (str[j] == '\0' && j >= 0) {
j--;
}
}
ret = PyBytes_FromStringAndSize(str, j + 1);
if (ret == NULL) {
PyErr_NoMemory();
}
return ret;
}
/**************************************
*
* Floating point packing and unpacking. This is based on the implementation
* of SortedFloat and SortedDouble from Berkeley DB Java edition. See
* com.sleepycat.bind.tuple.TupleInput for the source of the bit
* manipulations below.
*
*************************************/
static void
pack_float(float value, void *dest)
{
int32_t float_bits;
memcpy(&float_bits, &value, sizeof(float));
float_bits ^= (float_bits < 0) ? 0xffffffff: 0x80000000;
#ifdef WORDS_BIGENDIAN
memcpy(dest, &float_bits, sizeof(float));
#else
bigendian_copy(dest, &float_bits, sizeof(float));
#endif
}
static double
unpack_float(void *src)
{
int32_t float_bits;
float value;
#ifdef WORDS_BIGENDIAN
memcpy(&float_bits, src, sizeof(float));
#else
bigendian_copy(&float_bits, src, sizeof(float));
#endif
float_bits ^= (float_bits < 0) ? 0x80000000: 0xffffffff;
memcpy(&value, &float_bits, sizeof(float));
return (double) value;
}
static void
pack_double(double value, void *dest)
{
int64_t double_bits;
memcpy(&double_bits, &value, sizeof(double));
double_bits ^= (double_bits < 0) ? 0xffffffffffffffffLL: 0x8000000000000000LL;
#ifdef WORDS_BIGENDIAN
memcpy(dest, &double_bits, sizeof(double));
#else
bigendian_copy(dest, &double_bits, sizeof(double));
#endif
}
static double
unpack_double(void *src)
{
int64_t double_bits;
double value;
#ifdef WORDS_BIGENDIAN
memcpy(&double_bits, src, sizeof(double));
#else
bigendian_copy(&double_bits, src, sizeof(double));
#endif
double_bits ^= (double_bits < 0) ? 0x8000000000000000LL: 0xffffffffffffffffLL;
memcpy(&value, &double_bits, sizeof(double));
return (double) value;
}
/**************************************
*
* Unpacking from a row to the element buffer.
*
*************************************/
static int
Column_unpack_elements_int(Column *self, void *source)
{
int j;
int ret = -1;
void *v = source;
void *dest;
int64_t *elements = (int64_t *) self->element_buffer;
int64_t tmp;
for (j = 0; j < self->num_buffered_elements; j++) {
dest = &tmp;
#ifdef WORDS_BIGENDIAN
dest += 8 - self->element_size;
memcpy(dest, v, self->element_size);
#else
bigendian_copy(dest, v, self->element_size);
#endif
v += self->element_size;
/* flip the sign bit */
tmp ^= 1LL << (self->element_size * 8 - 1);
/* TODO fix this to work for all int sizes */
switch (self->element_size) {
case 1:
elements[j] = (int8_t) tmp;
break;
case 2:
elements[j] = (int16_t) tmp;
break;
case 4:
elements[j] = (int32_t) tmp;
break;
case 8:
elements[j] = (int64_t) tmp;
break;
default:
Py_FatalError("Complete int sizes not yet supported");
}
}
ret = 0;
return ret;
}
static int
Column_unpack_elements_float(Column *self, void *source)
{
int j;
int ret = -1;
void *v = source;
double *elements = (double *) self->element_buffer;
/* TODO Tidy this up and make it consistent with the pack definition */
for (j = 0; j < self->num_buffered_elements; j++) {
if (self->element_size == 4) {
elements[j] = unpack_float(v);
} else {
elements[j] = unpack_double(v);
}
v += self->element_size;
}
ret = 0;
return ret;
}
static int
Column_unpack_elements_char(Column *self, void *source)
{
/*
char v[1024];
memcpy(v, source, self->num_buffered_elements);
v[self->num_buffered_elements] = 0;
printf("unpacked: '%s': %d\n", v, self->num_buffered_elements);
*/
memcpy(self->element_buffer, source, self->num_buffered_elements);
return 0;
}
/**************************************
*
* Packing native values from the element_buffer to a row.
*
*************************************/
static int
Column_pack_elements_int(Column *self, void *dest)
{
int j;
int ret = -1;
void *v = dest;
void *src;
int64_t *elements = (int64_t *) self->element_buffer;
int64_t u;
for (j = 0; j < self->num_buffered_elements; j++) {
//printf("\npacking :%ld\n", elements[j]);
u = elements[j];
/* flip the sign bit */
u ^= 1LL << (self->element_size * 8 - 1);
src = &u;
#ifdef WORDS_BIGENDIAN
memcpy(v, src + (8 - self->element_size), self->element_size);
#else
bigendian_copy(v, src, self->element_size);
#endif
v += self->element_size;
}
ret = 0;
return ret;
}
static int
Column_pack_elements_float(Column *self, void *dest)
{
int j;
int ret = -1;
void *v = dest;
double *elements = (double *) self->element_buffer;
/* TODO tidy this up */
for (j = 0; j < self->num_buffered_elements; j++) {
if (self->element_size == 4) {
pack_float((float) elements[j], v);
} else if (self->element_size == 8) {
pack_double(elements[j], v);
} else {
assert(0);
}
v += self->element_size;
}
ret = 0;
return ret;
}
static int
Column_pack_elements_char(Column *self, void *dest)
{
int ret = -1;
/*
char v[1024];
memcpy(v, self->element_buffer, self->num_buffered_elements);
v[self->num_buffered_elements] = 0;
printf("packed: '%s': %d\n", v, self->num_buffered_elements);
*/
memcpy(dest, self->element_buffer, self->num_buffered_elements);
ret = 0;
return ret;
}
/**************************************
*
* Verify elements in the buffer.
*
*************************************/
static int
Column_verify_elements_int(Column *self)
{
int j;
int ret = -1;
int64_t *elements = (int64_t *) self->element_buffer;
/* TODO check this - probably not totally right */
/* This seems to be wrong - must get the correct formula */
int64_t min_value = (-1) * (1ll << (8 * self->element_size - 1)) + 1;
int64_t max_value = (1ll << (8 * self->element_size - 1)) - 1;
for (j = 0; j < self->num_buffered_elements; j++) {
if (elements[j] < min_value || elements[j] > max_value) {
PyErr_SetString(PyExc_OverflowError, "Value out of bounds");
goto out;
}
}
ret = 0;
out:
return ret;
}
static int
Column_verify_elements_float(Column *self)
{
return 0;
}
static int
Column_verify_elements_char(Column *self)
{
return 0;
}
/**************************************
*
* Python input element parsing.
*
*************************************/
/*
* Takes a Python sequence and places pointers to the Python
* elements into the input_elements list. Checks for various
* errors in the format of this sequence.
*/
static int
Column_parse_python_sequence(Column *self, PyObject *elements)
{
int ret = -1;
int j, num_elements;
PyObject *seq = NULL;
PyObject *v;
self->num_buffered_elements = 0;
if (self->num_elements == 1) {
self->input_elements[0] = elements;
num_elements = 1;
} else {
seq = PySequence_Fast(elements, "Sequence required");
if (seq == NULL) {
goto out;
}
num_elements = PySequence_Fast_GET_SIZE(seq);
if (self->num_elements == NUM_ELEMENTS_VARIABLE) {
if (num_elements > MAX_NUM_ELEMENTS) {
PyErr_SetString(PyExc_ValueError, "too many elements");
goto out;
}
} else {
if (num_elements != self->num_elements) {
PyErr_SetString(PyExc_ValueError, "incorrect number of elements");
goto out;
}
}
for (j = 0; j < num_elements; j++) {
v = PySequence_Fast_GET_ITEM(seq, j);
self->input_elements[j] = v;
}
}
if (self->num_elements != NUM_ELEMENTS_VARIABLE) {
if (num_elements != self->num_elements) {
PyErr_SetString(PyExc_ValueError, "incorrect number of elements");
goto out;
}
}
self->num_buffered_elements = num_elements;
ret = 0;
out:
Py_XDECREF(seq);
return ret;
}
static int
Column_python_to_native_int(Column *self, PyObject *elements)
{
int ret = -1;
int64_t *native= (int64_t *) self->element_buffer;
PyObject *v;
int j;
if (Column_parse_python_sequence(self, elements) < 0) {
goto out;
}
for (j = 0; j < self->num_buffered_elements; j++) {
v = (PyObject *) self->input_elements[j];
if (!PyNumber_Check(v)) {
PyErr_SetString(PyExc_TypeError, "Must be numeric");
goto out;
}
native[j] = (int64_t) PyLong_AsLongLong(v);
if (native[j] == -1) {
/* PyLong_AsLongLong return -1 and raises OverFlowError if
* the value cannot be represented as a long long
*/
if (PyErr_Occurred()) {
goto out;
}
}
}
ret = 0;
out:
return ret;
}
static int
Column_python_to_native_float(Column *self, PyObject *elements)
{
int ret = -1;
double *native = (double *) self->element_buffer;
PyObject *v;
int j;
if (Column_parse_python_sequence(self, elements) < 0) {
goto out;
}
for (j = 0; j < self->num_buffered_elements; j++) {
v = (PyObject *) self->input_elements[j];
if (!PyNumber_Check(v)) {
PyErr_SetString(PyExc_TypeError, "Must be float");
goto out;
}
native[j] = (double) PyFloat_AsDouble(v);
}
ret = 0;
out:
return ret;
}
static int
Column_python_to_native_char(Column *self, PyObject *elements)
{
int ret = -1;
char *s;
Py_ssize_t max_length = self->num_elements == NUM_ELEMENTS_VARIABLE?
MAX_NUM_ELEMENTS: self->num_elements;
Py_ssize_t length;
/* Elements must be a single Python bytes object */
if (!PyBytes_Check(elements)) {
PyErr_SetString(PyExc_TypeError, "Must be bytes");
goto out;
}
if (PyBytes_AsStringAndSize(elements, &s, &length) < 0) {
PyErr_SetString(PyExc_ValueError, "Error in string conversion");
goto out;
}
if (length > max_length) {
PyErr_SetString(PyExc_ValueError, "String too long");
goto out;
}
memcpy(self->element_buffer, s, length);
self->num_buffered_elements = length;
ret = 0;
out:
return ret;
}
/**************************************
*
* String input element parsing.
*
*************************************/
/*
* Takes a string sequence and places pointers to the start
* of each individual element into the input_elements list.
* Checks for various errors in the format of this sequence.
*/
static int
Column_parse_string_sequence(Column *self, char *s)
{
int ret = -1;
int j, num_elements, delimiter;
self->num_buffered_elements = 0;
if (self->num_elements == 1) {
self->input_elements[0] = s;
num_elements = 1;
} else {
j = 0;
num_elements = 0;
delimiter = -1;
if (s[0] == '\0') {
PyErr_SetString(PyExc_ValueError, "Empty value");
goto out;
}
/* TODO this needs lots of error checking! */
while (s[j] != '\0') {
if (s[j] == ',' || s[j] == ';') {
delimiter = j;
}
if (j == delimiter + 1) {
/* this is the start of a new element */
self->input_elements[num_elements] = &s[j];
num_elements++;
}
j++;
}
}
if (self->num_elements != NUM_ELEMENTS_VARIABLE) {
if (num_elements != self->num_elements) {
PyErr_SetString(PyExc_ValueError, "incorrect number of elements");
goto out;
}
}
self->num_buffered_elements = num_elements;
ret = 0;
out:
return ret;
}
static int
Column_string_to_native_int(Column *self, char *string)
{
int ret = -1;
int64_t *native= (int64_t *) self->element_buffer;
char *v, *tail;
int j;
if (Column_parse_string_sequence(self, string) < 0) {
goto out;
}
for (j = 0; j < self->num_buffered_elements; j++) {
v = (char *) self->input_elements[j];
errno = 0;
native[j] = (int64_t) strtoll(v, &tail, 0);
if (errno) {
PyErr_SetString(PyExc_ValueError, "Element overflow");
goto out;
}
if (v == tail) {
PyErr_SetString(PyExc_ValueError, "Element parse error");
goto out;
}
if (*tail != '\0') {
if (!(isspace(*tail) || *tail == ',' || *tail == ';')) {
PyErr_SetString(PyExc_ValueError, "Element parse error");
goto out;
}
}
}
ret = 0;
out:
return ret;
}
static int
Column_string_to_native_float(Column *self, char *string)
{
int ret = -1;
double *native= (double *) self->element_buffer;
char *v, *tail;
int j;
if (Column_parse_string_sequence(self, string) < 0) {
goto out;
}
for (j = 0; j < self->num_buffered_elements; j++) {
v = (char *) self->input_elements[j];
errno = 0;
native[j] = (double) strtod(v, &tail);
if (errno) {
PyErr_SetString(PyExc_ValueError, "Element overflow");
goto out;
}
if (v == tail) {
PyErr_SetString(PyExc_ValueError, "Element parse error");
goto out;
}
if (*tail != '\0') {
if (!(isspace(*tail) || *tail == ',' || *tail == ';')) {
PyErr_SetString(PyExc_ValueError, "Element parse error");
goto out;
}
}
}
ret = 0;
out:
return ret;
}
static int
Column_string_to_native_char(Column *self, char *string)
{
size_t n = strlen(string);
memcpy(self->element_buffer, string, n);
return n;
}
/*
* TODO We need to support lists of enumeration values - this will need to
* copy the parsing code above.
*/
static int
Column_string_to_native_enum(Column *self, char *string)
{
int ret = -1;
unsigned long value;
unsigned long max_value = 1l << (8 * self->element_size);
PyObject *v = PyDict_GetItemString(self->enum_values, string);
if (v == NULL) {
value = PyDict_Size(self->enum_values) + 1;
if (value > max_value) {
PyErr_SetString(PyExc_ValueError, "Enum value too large");
goto out;
}
v = PyLong_FromUnsignedLong(value);
if (v == NULL) {
PyErr_NoMemory();
goto out;
}
if (PyDict_SetItemString(self->enum_values, string, v) < 0) {
Py_DECREF(v);
goto out;
}
Py_DECREF(v);
} else {
if (!PyLong_Check(v)) {
PyErr_SetString(PyExc_ValueError, "Enum value not a long");
goto out;
}
value = PyLong_AsUnsignedLong(v);
}
#ifdef WORDS_BIGENDIAN
printf("bigendian enums not supported\n");
abort();
#else
bigendian_copy(self->element_buffer, &value, self->element_size);
#endif
//printf("%s -> %ld\n", string, value);
ret = 1;
out:
return ret;
}
/*
* Packs the address and number of elements in a variable length column at the
* specified pointer.
*
* TODO Error checking here - this is a major opportunity for buffer overflows.
*/
static int
Column_pack_variable_elements_address(Column *self, void *dest,
uint32_t offset, uint32_t num_elements)
{
int ret = -1;
uint16_t off = (uint16_t) offset;
uint8_t n = (uint8_t) num_elements;
void *v = dest;
/* TODO these are internal errors so should have a different
* exception
*/
if (offset >= MAX_ROW_SIZE) {
PyErr_SetString(PyExc_ValueError, "Row overflow");
goto out;
}
if (num_elements > MAX_NUM_ELEMENTS) {
PyErr_SetString(PyExc_ValueError, "too many elements");
goto out;
}
#if WORDS_BIGENDIAN
memcpy(v, &off, sizeof(off));
memcpy(v + sizeof(off), &n, sizeof(n));
#else
bigendian_copy(v, &off, sizeof(off));
bigendian_copy(v + sizeof(off), &n, sizeof(n));
#endif
ret = 0;
out:
return ret;
}
/*
* Unpacks the address and number of elements in a variable length column at the
* specified pointer.
*
* TODO Error checking here - this is a major opportunity for buffer overflows.
*/
static int
Column_unpack_variable_elements_address(Column *self, void *src,
uint32_t *offset, uint32_t *num_elements)
{
int ret = -1;
void *v = src;
uint16_t off = 0;
uint8_t n = 0;
#if WORDS_BIGENDIAN
memcpy(&off, v, sizeof(off));
memcpy(&n, v + sizeof(off), sizeof(n));
#else
bigendian_copy(&off, v, sizeof(off));
bigendian_copy(&n, v + sizeof(off), sizeof(n));
#endif
/* These should really be considered to be internal
* fatal errors, as they should only happen on database
* corruption
*/
if (off >= MAX_ROW_SIZE) {
PyErr_SetString(PyExc_ValueError, "Row overflow");
goto out;
}
if (n > MAX_NUM_ELEMENTS) {
PyErr_SetString(PyExc_ValueError, "too many elements");
goto out;
}
*offset = (uint32_t) off;
*num_elements = (uint32_t) n;
ret = 0;
out:
return ret;
}
/*
* Inserts the values in the element buffer into the specified row which
* is currently of the specified size, and return the number of bytes
* used in the variable region. Returns -1 in the case of an error with
* the appropriate Python exception set.
*/
static int
Column_update_row(Column *self, void *row, uint32_t row_size)
{
int ret = -1;
void *dest;
int bytes_added = 0;
uint32_t num_elements = (uint32_t) self->num_buffered_elements;
int data_size = num_elements * self->element_size;
if (self->verify_elements(self) < 0) {
goto out;
}
dest = row + self->fixed_region_offset;
if (self->num_elements == NUM_ELEMENTS_VARIABLE) {
bytes_added = data_size;
if (row_size + bytes_added > MAX_ROW_SIZE) {
PyErr_SetString(PyExc_ValueError, "Row overflow");
goto out;
}
if (Column_pack_variable_elements_address(self, dest, row_size,
num_elements) < 0) {
goto out;
}
//printf("set to offset %d, with %d bytes\n", row_size, num_elements);
dest = row + row_size;
}
self->pack_elements(self, dest);
ret = bytes_added;
out:
return ret;
}
/*
* Extracts elements from the specified row and inserts them into the
* element buffer.
*/
static int
Column_extract_elements(Column *self, void *row)
{
int ret = -1;
void *src;
uint32_t offset, num_elements;
src = row + self->fixed_region_offset;
num_elements = self->num_elements;
if (self->num_elements == NUM_ELEMENTS_VARIABLE) {
if (Column_unpack_variable_elements_address(self, src, &offset,
&num_elements) < 0) {
goto out;
}
src = row + offset;
}
self->num_buffered_elements = num_elements;
ret = self->unpack_elements(self, src);
out:
return ret;
}
/* Copies the data values from the specified source to the specified
* destination
*/
static int
Column_copy_row(Column *self, void *dest, void *src)
{
int ret = -1;
uint32_t len, num_elements, offset;
void *v = src + self->fixed_region_offset;
offset = 0;
num_elements = self->num_elements;
if (self->num_elements == NUM_ELEMENTS_VARIABLE) {
if (Column_unpack_variable_elements_address(self, v, &offset,
&num_elements) < 0) {
goto out;
}
v = src + offset;
}
len = self->element_size * num_elements;
memcpy(dest, v, len);
ret = len;
out:
return ret;
}
/*
* Converts the native values in the element buffer to the appropriate
* Python types, and returns the result.
*/
static PyObject *
Column_get_python_elements(Column *self)
{
PyObject *ret = NULL;
PyObject *u, *t;
Py_ssize_t j;
/* TODO Missing value handling is a mess FIXME!!! */
if (self->element_type == ELEMENT_TYPE_CHAR) {
ret = self->native_to_python(self, 0);
if (ret == NULL) {
goto out;
}
} else {
if (self->num_buffered_elements == 0) {
Py_INCREF(Py_None);
ret = Py_None;
} else {
if (self->num_elements == 1) {
ret = self->native_to_python(self, 0);
if (ret == NULL) {
goto out;
}
} else {
t = PyTuple_New(self->num_buffered_elements);
if (t == NULL) {
PyErr_NoMemory();
goto out;
}
for (j = 0; j < self->num_buffered_elements; j++) {
u = self->native_to_python(self, j);
if (u == NULL) {
Py_DECREF(t);
PyErr_NoMemory();
goto out;
}
PyTuple_SET_ITEM(t, j, u);
}
ret = t;
}
}
}
out:
return ret;
}