-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmlbdb_stub.c
2340 lines (2010 loc) · 66.3 KB
/
mlbdb_stub.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
/*
* (c) 2010 Anastasia Gornostaeva <[email protected]>
*/
#include <caml/alloc.h>
#include <caml/mlvalues.h>
#include <caml/fail.h>
#include <caml/memory.h>
#include <caml/custom.h>
#include <caml/signals.h>
#include <caml/callback.h>
#include <db.h>
#include <string.h>
#include <errno.h>
#include "db_helper.h"
#include "db_flags.h"
#define DB_ENV_val(v) (*((DB_ENV **) Data_custom_val(v)))
#define DB_val(v) (*((DB **) Data_custom_val(v)))
#define DB_TXN_val(v) (*((DB_TXN **) Data_custom_val(v)))
#define DBC_val(v) (*((DBC **) Data_custom_val(v)))
#define DB_SEQ_val(v) (*((DB_SEQUENCE **) Data_custom_val(v)))
#define DB_MPOOLFILE_val(v) (*((DB_MPOOLFILE **) Data_custom_val(v)))
#define DB_LOGC_val(v) (*((DB_LOGC **) Data_custom_val(v)))
#ifdef CHECK_HANDLE
#define TEST_HANDLE(v) \
if(v == NULL) caml_invalid_argument(#v ": null pointer")
#else
#define TEST_HANDLE(v)
#endif
#define Some_val(v) (Field(v,0))
//+ add call TEST_HANDLE
#define DB_TXN_opt_val(v) (Is_block(v)) ? DB_TXN_val(Some_val(v)) : NULL
static void finalize_dbenv(value block) {
// DB_ENV *dbenv = DB_ENV_val(block);
printf("finalizing dbenv\n");
/*
if(dbenv != NULL) {
(void) dbenv->close(dbenv, 0);
DB_ENV_val(block) = NULL;
}
else
printf("dbenv is NULL\n");
*/
}
static struct custom_operations dbenv_ops = {
"caml_dbenv",
finalize_dbenv,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default
};
static void finalize_db(value block) {
// DB *db = DB_val(block);
printf("finalizing db\n");
/*
if(db != NULL) {
//? how to check retval
(void) db->close(db, 0);
DB_val(block) = NULL;
}
else
printf("db is NULL\n");
*/
}
static struct custom_operations db_ops = {
"caml_db",
finalize_db,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default
};
static void finalize_dbc(value block) {
// DBC *dbc = DBC_val(block);
printf("finalizing dbc\n");
/*
if(dbc != NULL) {
(void) dbc->close(dbc);
DBC_val(block) = NULL;
}
else
printf("dbc is NULL\n");
*/
}
static struct custom_operations dbc_ops = {
"caml_dbc",
finalize_dbc,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default
};
static struct custom_operations dbtxn_ops = {
"caml_dbtxn",
custom_finalize_default,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default
};
static void finalize_dbmpf(value block) {
// DB_MPOOLFILE *mpf = DB_MPOOLFILE_val(block);
printf("finalizing dbmpf\n");
/*
if(mpf != NULL) {
(void) mpf->close(mpf, 0);
DB_MPOOLFILE_val(block) = NULL;
}
else
printf("mpf is NULL\n");
*/
}
enum OCAML_TYPE_KEY {
OCAML_TYPE_KEY_RECNO = 0,
OCAML_TYPE_KEY_STRING = 1
};
struct _app_data {
enum OCAML_TYPE_KEY keytype;
value vcallbacks;
};
typedef struct _app_data AppData;
static struct custom_operations dbmpf_ops = {
"caml_dbmpf",
finalize_dbmpf,
custom_compare_default,
custom_hash_default,
custom_serialize_default,
custom_deserialize_default
};
enum db_cb_values {
V_ERRCALL = 0, // must be first item
V_DB_ASSOCIATE,
V_DB_ASSOCIATE_FOREIGN,
V_DB_DUP_COMPARE,
TOTAL_DB_VALUES
};
enum dbenv_cb_values {
V__ERRCALL = 0, // must be first item
V_DBENV_EVENT_NOTIFY,
TOTAL_DBENV_VALUES
};
static void ensure_vcallbacks(AppData *app_data, int size) {
int i;
if(app_data->vcallbacks == Val_unit) {
app_data->vcallbacks = caml_alloc(size, 0);
caml_register_generational_global_root(&(app_data->vcallbacks));
for(i = 0; i < size; i++)
Store_field(app_data->vcallbacks, i, Val_unit);
}
return;
}
static value *create_vcallbacks(int size) {
int i;
value *app_data = malloc(sizeof(value));
*(value *) app_data = caml_alloc(size, 0);
caml_register_generational_global_root(app_data);
for(i = 0; i < size; i++)
Store_field(*(value *) app_data, i, Val_unit);
return app_data;
}
static void check_retval(int ret) {
static value *mlbdb_error_exn = NULL;
static value *mlbdb_keyempty_exn = NULL;
static value *mlbdb_keyexists_exn = NULL;
static value *mlbdb_deadlock_exn = NULL;
static value *mlbdb_locknotgranted_exn = NULL;
static value *mlbdb_runrecovery_exn = NULL;
static value *mlbdb_secondarybad_exn = NULL;
char* msg;
if(ret != 0)
switch(ret) {
case EINVAL:
msg = db_strerror(ret);
caml_invalid_argument(msg);
case DB_NOTFOUND:
caml_raise_not_found();
case DB_KEYEMPTY:
if (mlbdb_keyempty_exn == NULL) {
mlbdb_keyempty_exn = caml_named_value("dbkeyempty");
if (mlbdb_keyempty_exn == NULL)
caml_invalid_argument("init: Exception Error is not initialized");
}
caml_raise_constant(*mlbdb_keyempty_exn);
case DB_KEYEXIST:
mlbdb_keyexists_exn = caml_named_value("dbkeyexists");
if (mlbdb_keyexists_exn == NULL)
caml_invalid_argument("init: Exception Error is not initialized");
caml_raise_constant(*mlbdb_keyexists_exn);
case DB_LOCK_DEADLOCK:
if (mlbdb_deadlock_exn == NULL) {
mlbdb_deadlock_exn = caml_named_value("dbdeadlock");
if (mlbdb_deadlock_exn == NULL)
caml_invalid_argument("init: Exception Error is not initialized");
}
caml_raise_constant(*mlbdb_deadlock_exn);
case DB_LOCK_NOTGRANTED:
if (mlbdb_locknotgranted_exn == NULL) {
mlbdb_locknotgranted_exn = caml_named_value( "dblocknotgranted");
if (mlbdb_locknotgranted_exn == NULL)
caml_invalid_argument("init: Exception Error is not initialized");
}
caml_raise_constant(*mlbdb_locknotgranted_exn);
case DB_RUNRECOVERY:
if (mlbdb_runrecovery_exn == NULL) {
mlbdb_runrecovery_exn = caml_named_value( "dbrunrecovery");
if (mlbdb_runrecovery_exn == NULL)
caml_invalid_argument("init: Exception RunRecovery is not initialized");
}
caml_raise_constant(*mlbdb_runrecovery_exn);
case DB_SECONDARY_BAD:
if (mlbdb_secondarybad_exn == NULL) {
mlbdb_secondarybad_exn = caml_named_value("dbsecondarybad");
if (mlbdb_secondarybad_exn == NULL)
caml_invalid_argument("init: Exception SecondaryBad is not initialized");
}
caml_raise_constant(*mlbdb_secondarybad_exn);
default:
if (mlbdb_error_exn == NULL) {
mlbdb_error_exn = caml_named_value("dberror");
if (mlbdb_error_exn == NULL)
caml_invalid_argument("init: Exception Error is not initialized");
}
msg = db_strerror(ret);
caml_raise_with_string(*mlbdb_error_exn, msg);
}
return;
}
/*
static value Val_flags(int flags, int *tbl) {
CAMLparam0();
CAMLlocal2(li, cons);
int i;
size_t len = sizeof(tbl)/sizeof(tbl[0]);
li = Val_emptylist;
for(i = 0; i < len; i++) {
if(flags & tbl[i]) {
cons = caml_alloc(2, 0);
Store_field(cons, 0, i);
Store_field(cons, 1, li);
li = cons;
}
}
CAMLreturn(li);
}
*/
#define Size(v) sizeof(v)/sizeof(*v)
static inline u_int32_t Variant_val(value vflag, map_flag tbl[], size_t len) {
int i;
for(i=0; i<len; i++) {
if(vflag == tbl[i].variant)
return tbl[i].flag;
}
caml_invalid_argument("Bad flag");
}
static int Variants_val(value vflags, map_flag tbl[], size_t len) {
value head;
u_int32_t flags = 0;
while(vflags != Val_emptylist) {
head = Field(vflags, 0);
flags |= Variant_val(head, tbl, len);
vflags = Field(vflags, 1);
}
return flags;
}
#define DBFlags_val(vflags, tbl) \
(Is_block(vflags)) ? Variants_val(Some_val(vflags),tbl, Size(tbl)) : 0
static value Val_variants(value vflags, map_flag tbl[], size_t len) {
CAMLparam1(vflags);
CAMLlocal2(li, cons);
int i;
li = Val_emptylist;
for(i = 0; i < len; i++) {
if(vflags & tbl[i].flag) {
cons = caml_alloc(2, 0);
Store_field(cons, 0, tbl[i].variant);
Store_field(cons, 1, li);
li = cons;
}
}
CAMLreturn(li);
}
static int ml2enum(value v, int *tbl, size_t len) {
int i = Int_val(v);
if(i < len)
return tbl[i];
else
caml_invalid_argument("ml2enum");
}
static value enum2ml(int v, int *tbl, size_t len) {
int i;
for(i = 0; i < len && tbl[i] != v; i++);
if(i < len)
return Val_int(i);
else
caml_invalid_argument("enum2ml");
}
static void dbt_of_value(DB *db, DBT *dbt, value v) {
dbt->flags = DB_DBT_APPMALLOC;
if(((AppData *) db->app_private)->keytype == OCAML_TYPE_KEY_RECNO) {
dbt->data = malloc(sizeof(db_recno_t));
*(db_recno_t *) dbt->data = Long_val(v);
dbt->size = sizeof(db_recno_t);
} else {
int len = caml_string_length(v);
dbt->data = malloc(len);
memcpy(dbt->data, String_val(v), len);
dbt->size = len;
}
return;
}
static int associate_stub(DB *db, const DBT *key, const DBT *data, DBT *result) {
CAMLparam0();
CAMLlocal5(vdb, vkey, vdata, vret, head);
AppData *app_data;
int rlen;
vdb = caml_alloc_custom(&db_ops, sizeof(DB*), 0, 1);
DB_val(vdb) = db;
if(((AppData *) db->s_primary->app_private)->keytype == OCAML_TYPE_KEY_RECNO)
vkey = Val_long(* (db_recno_t *) key->data);
else {
vkey = caml_alloc_string(key->size);
memcpy(String_val(vkey), key->data, key->size);
}
vdata = caml_alloc_string(data->size);
memcpy(String_val(vdata), data->data, data->size);
app_data = db->app_private;
vret = caml_callback3(Field(app_data->vcallbacks, V_DB_ASSOCIATE),
vdb, vkey, vdata);
memset(result, 0, sizeof(DBT));
for(head = vret, rlen = 0; head != Val_emptylist;
head = Field(head, 1), rlen++);
if(rlen == 0)
CAMLreturnT(int, DB_DONOTINDEX);
else if(rlen == 1) {
dbt_of_value(db, result, Field(vret, 0));
CAMLreturnT(int, 0);
} else {
DBT *dbt;
result->flags = DB_DBT_MULTIPLE | DB_DBT_APPMALLOC;
result->size = rlen;
result->data = malloc(sizeof(DBT) * rlen);
head = vret;
for(dbt = result->data; head != Val_emptylist; dbt++) {
dbt_of_value(db, dbt, Field(head, 0));
head = Field(head, 1);
}
CAMLreturnT(int, 0);
}
}
static int dbpriority_enum[] = {
DB_PRIORITY_UNCHANGED,
DB_PRIORITY_VERY_LOW,
DB_PRIORITY_LOW,
DB_PRIORITY_DEFAULT,
DB_PRIORITY_HIGH,
DB_PRIORITY_VERY_HIGH
};
#define DBPriority_val(v) ml2enum(v, dbpriority_enum, Size(dbpriority_enum))
#define Val_dbpriority(v) enum2ml(v, dbpriority_enum, Size(dbpriority_enum))
CAMLprim value ml_db_associate(value vdb, value vtxn, value vsecondary,
value vcallback, value vflags, value unit) {
CAMLparam5(vdb, vtxn, vsecondary, vcallback, vflags);
CAMLlocal1(vres);
DB *db = DB_val(vdb);
DB *secondary = DB_val(vsecondary);
DB_TXN *txn = DB_TXN_opt_val(vtxn);
int (*callback)(DB *secondary, const DBT *key, const DBT *data, DBT *result);
u_int32_t flags = DBFlags_val(vflags, flags_db_associate);
AppData *app_data;
int ret;
TEST_HANDLE(db);
TEST_HANDLE(secondary);
app_data = secondary->app_private;
if(Is_block(vcallback)) {
ensure_vcallbacks(app_data, TOTAL_DB_VALUES);
Store_field(app_data->vcallbacks, V_DB_ASSOCIATE, Some_val(vcallback));
callback = associate_stub;
} else {
if(app_data->vcallbacks != Val_unit)
Store_field(app_data->vcallbacks, V_DB_ASSOCIATE, Val_unit);
callback = NULL;
}
ret = db->associate(db, txn, secondary, callback, flags);
check_retval(ret);
vres = caml_alloc_tuple(2);
Store_field(vres, 0, vsecondary);
Store_field(vres, 1, vdb);
CAMLreturn(vres);
}
CAMLprim value ml_db_associate_byte(value *argv, int argn) {
return ml_db_associate(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
}
static int associate_foreign_stub(DB *db,
const DBT *key, DBT *data, const DBT *fkey,
int *changed) {
CAMLparam0();
CAMLlocal1(vres);
CAMLlocalN(vargs, 5);
vres = caml_alloc_custom(&db_ops, sizeof(DB*), 0, 1);
DB_val(vres) = db;
vargs[0] = vres;
vres = caml_alloc_string(key->size);
memcpy(String_val(vres), key->data, key->size);
vargs[1] = vres;
vres = caml_alloc_string(data->size);
memcpy(String_val(vres), data->data, data->size);
vargs[2] = vres;
vres = caml_alloc_string(fkey->size);
memcpy(String_val(vres), fkey->data, fkey->size);
vargs[3] = vres;
caml_callbackN(Field(db->app_private, V_DB_ASSOCIATE_FOREIGN), 5, vargs);
/*
memset(result, 0, sizeof(DBT));
result->data = String_val(vret);
result->size = caml_string_length(vret);
*/
CAMLreturnT(int, 0);
}
CAMLprim value ml_db_associate_foreign(value vdb, value vsecondary,
value vcallback, value vflags,
value unit) {
DB *db = DB_val(vdb);
DB *secondary = DB_val(vsecondary);
u_int32_t flags = DBFlags_val(vflags, flags_db_associate_foreign);
int (*callback)(DB *, const DBT *, DBT *, const DBT *, int *) = NULL;
AppData *app_data;
int ret;
TEST_HANDLE(db);
TEST_HANDLE(secondary);
app_data = secondary->app_private;
if(Is_block(vcallback)) {
ensure_vcallbacks(app_data, TOTAL_DB_VALUES);
callback = &associate_foreign_stub;
Store_field(app_data->vcallbacks, V_DB_ASSOCIATE_FOREIGN,
Some_val(vcallback));
}
ret = db->associate_foreign(db, secondary, callback, flags);
check_retval(ret);
return Val_unit;
}
static int dup_compare_stub(DB* db, const DBT *dbt1, const DBT *dbt2) {
CAMLparam0();
CAMLlocal4(vret, vdb, vdata1, vdata2);
vdb = caml_alloc_custom(&db_ops, sizeof(DB*), 0, 1);
DB_val(vdb) = db;
vdata1 = caml_alloc_string(dbt1->size);
memcpy(String_val(vdata1), dbt1->data, dbt1->size);
vdata2 = caml_alloc_string(dbt2->size);
memcpy(String_val(vdata2), dbt2->data, dbt2->size);
vret = caml_callback3(Field(db->app_private, V_DB_DUP_COMPARE),
vdb, vdata1, vdata2);
CAMLreturnT(int, Int_val(vret));
}
CAMLprim value ml_db_set_dup_compare(value vdb, value vcallback) {
DB *db = DB_val(vdb);
AppData *app_data;
int ret;
TEST_HANDLE(db);
app_data = db->app_private;
ensure_vcallbacks(app_data, TOTAL_DB_VALUES);
Store_field(app_data->vcallbacks, V_DB_DUP_COMPARE, vcallback);
ret = db->set_dup_compare(db, dup_compare_stub);
check_retval(ret);
return Val_unit;
}
static void errcall_stub(const DB_ENV *dbenv,
const char *errpfx, const char *msg) {
CAMLparam0();
CAMLlocal3(vdbenv, vstr1, vstr2);
vdbenv = caml_alloc_custom(&dbenv_ops, sizeof(DB_ENV*), 0, 1);
DB_ENV_val(vdbenv) = (DB_ENV*)dbenv;
vstr1 = caml_copy_string(errpfx);
vstr2 = caml_copy_string(msg);
caml_callback3(Field(dbenv->app_private, V_ERRCALL),
vdbenv, vstr1, vstr2);
CAMLreturn0;
}
//? Hm, where i should save the callback?
// The callback stub does not see DB->app_private because db handle is not
// orovided in the callback
/*
CAMLprim value ml_db_set_errcall(value vdb, value vcallback) {
CAMLparam2(vdb, vcallback);
DB *db = DB_val(vdb);
AppData *app_data;
TEST_HANDLE(db);
app_data = db->app_private;
ensure_vcallbacks(app_data, TOTAL_DB_VALUES);
Store_field(db->app_private, V_ERRCALL, vcallback);
db->set_errcall(db, &errcall_stub);
CAMLreturn(Val_unit);
}
*/
CAMLprim value ml_dbenv_set_errcall(value vdbenv, value vcallback) {
DB_ENV *dbenv = DB_ENV_val(vdbenv);
void (*callback) (const DB_ENV*, const char*, const char*) = NULL;
TEST_HANDLE(dbenv);
if(Is_block(vcallback)) {
if(dbenv->app_private == NULL)
dbenv->app_private = create_vcallbacks(TOTAL_DBENV_VALUES);
Store_field(dbenv->app_private, V_ERRCALL, Some_val(vcallback));
callback = errcall_stub;
} else {
if(dbenv->app_private != NULL)
Store_field(dbenv->app_private, V_ERRCALL, Val_unit);
}
dbenv->set_errcall(dbenv, callback);
return Val_unit;
}
CAMLprim value ml_db_close(value vdb, value vflags, value unit) {
DB *db = DB_val(vdb);
u_int32_t flags = DBFlags_val(vflags, flags_db_close);
int ret;
TEST_HANDLE(db);
if(db->app_private != NULL) {
caml_remove_generational_global_root(db->app_private);
free(db->app_private);
}
ret = db->close(db, flags);
DB_val(vdb) = NULL;
check_retval(ret);
return Val_unit;
}
CAMLprim value ml_db_create(value vdbenv_opt, value vkeytype) {
CAMLparam2(vdbenv_opt, vkeytype);
CAMLlocal1(vres);
DB *db = NULL;
DB_ENV *dbenv = NULL;
AppData *app_data;
int ret;
if(Is_block(vdbenv_opt))
dbenv = DB_ENV_val(Some_val(vdbenv_opt));
ret = db_create(&db, dbenv, 0);
if(ret != 0 && db != NULL) {
db->close(db, 0);
db = NULL;
}
check_retval(ret);
app_data = (AppData*) malloc(sizeof(AppData));
app_data->vcallbacks = Val_unit;
switch(Int_val(vkeytype)) {
case 0: app_data->keytype = OCAML_TYPE_KEY_RECNO; break;
case 1: app_data->keytype = OCAML_TYPE_KEY_STRING; break;
default: caml_invalid_argument("Unknown key type");
}
db->app_private = (void *) app_data;
vres = caml_alloc_custom(&db_ops, sizeof(DB*), 0, 1);
DB_val(vres) = db;
CAMLreturn(vres);
}
static int dbtype_enum[] = {
DB_BTREE,
DB_HASH,
DB_RECNO,
DB_QUEUE,
DB_UNKNOWN
};
CAMLprim value ml_db_open(value vdb, value vtxn, value vfname,
value vdatabase, value vdbtype,
value vflags, value vmode, value unit) {
DB *db = DB_val(vdb);
DB_TXN *txn = DB_TXN_opt_val(vtxn);
DBTYPE dbtype = ml2enum(vdbtype, dbtype_enum, Size(dbtype_enum));
char *database = NULL;
u_int32_t flags = DBFlags_val(vflags, flags_db_open);
int mode = 0;
int ret;
TEST_HANDLE(db);
if(Is_block(vdatabase))
database = String_val(Some_val(vdatabase));
if(Is_block(vmode))
mode = Int_val(Some_val(vmode));
ret = db->open(db, txn, String_val(vfname), database, dbtype, flags, mode);
check_retval(ret);
return Val_unit;
}
CAMLprim value ml_db_open_byte(value *argv, int argn) {
return ml_db_open(argv[0], argv[1], argv[2], argv[3], argv[4],
argv[5], argv[6], argv[7]);
}
CAMLprim value ml_db_del(value vdb, value vtxn, value vkey, value vflags,
value unit) {
DB *db = DB_val(vdb);
DB_TXN *txn = DB_TXN_opt_val(vtxn);
DBT key;
u_int32_t flags = DBFlags_val(vflags, flags_db_del);
int ret;
TEST_HANDLE(db);
memset(&key, 0, sizeof(DBT));
key.data = String_val(vkey);
key.size = caml_string_length(vkey);
ret = db->del(db, txn, &key, flags);
check_retval(ret);
return Val_unit;
}
CAMLprim value ml_db_exists(value vdb, value vtxn, value vkey, value vflags,
value unit) {
DB *db = DB_val(vdb);
DB_TXN *txn = DB_TXN_opt_val(vtxn);
DBT key;
u_int32_t flags = DBFlags_val(vflags, flags_db_exists);
int ret;
TEST_HANDLE(db);
memset(&key, 0, sizeof(DBT));
key.data = String_val(vkey);
key.size = caml_string_length(vkey);
ret = db->exists(db, txn, &key, flags);
if(ret == DB_NOTFOUND)
return Val_false;
else {
check_retval(ret);
return Val_true;
}
}
CAMLprim value ml_db_get(value vdb, value vtxn, value vkey, value vflags,
value unit) {
CAMLparam5(vdb, vtxn, vkey, vflags, unit);
CAMLlocal1(vres);
DB *db = DB_val(vdb);
DB_TXN *txn = DB_TXN_opt_val(vtxn);
u_int32_t flags = DBFlags_val(vflags, flags_db_get);
DBT key, data;
int ret;
db_recno_t recno;
TEST_HANDLE(db);
memset(&key, 0, sizeof(DBT));
if(Is_long(vkey)) {
key.data = &recno;
key.size = sizeof(db_recno_t);
recno = Long_val(vkey);
} else {
key.data = String_val(vkey);
key.size = caml_string_length(vkey);
}
memset(&data, 0, sizeof(DBT));
//! DB_THREAD mandates memory allocation flag on data DBT
if(DB_IS_THREADED(db))
data.flags = DB_DBT_MALLOC;
ret = db->get(db, txn, &key, &data, flags);
check_retval(ret);
vres = caml_alloc_string(data.size);
memcpy(String_val(vres), data.data, data.size);
if(DB_IS_THREADED(db) && data.flags & DB_DBT_MALLOC &&
data.data != NULL)
free(data.data);
CAMLreturn(vres);
}
CAMLprim value ml_db_pget(value vsecondary, value vtxn, value vkey, value vdata,
value vflags, value unit) {
CAMLparam5(vsecondary, vtxn, vkey, vflags, unit);
CAMLlocal3(vres, vstr1, vstr2);
DB *db = DB_val(Field(vsecondary, 0));
DB *primary = DB_val(Field(vsecondary, 1));
DB_TXN *txn = DB_TXN_opt_val(vtxn);
u_int32_t flags = DBFlags_val(vflags, flags_db_get);
DBT key, pkey, data;
int ret;
TEST_HANDLE(db);
TEST_HANDLE(primary);
memset(&key, 0, sizeof(DBT));
if(Is_long(vkey)) {
u_int32_t i = Long_val(vkey);
key.data = &i;
key.size = sizeof(u_int32_t);
} else {
key.data = String_val(vkey);
key.size = caml_string_length(vkey);
}
memset(&pkey, 0, sizeof(DBT));
memset(&data, 0, sizeof(DBT));
if(DB_IS_THREADED(primary)) {
pkey.flags = DB_DBT_MALLOC;
data.flags = DB_DBT_MALLOC;
}
if(Is_block(vdata)) {
data.data = String_val(Some_val(vdata));
data.size = caml_string_length(Some_val(vdata));
}
ret = db->pget(db, txn, &key, &pkey, &data, flags);
check_retval(ret);
if(((AppData *) primary->app_private)->keytype == OCAML_TYPE_KEY_RECNO)
vstr1 = Val_long(*(db_recno_t*) pkey.data);
else {
vstr1 = caml_alloc_string(pkey.size);
memcpy(String_val(vstr1), pkey.data, pkey.size);
}
vstr2 = caml_alloc_string(data.size);
memcpy(String_val(vstr2), data.data, data.size);
vres = caml_alloc_tuple(2);
Store_field(vres, 0, vstr1);
Store_field(vres, 1, vstr2);
if(pkey.flags &DB_DBT_MALLOC && pkey.data != NULL)
free(pkey.data);
if(data.flags &DB_DBT_MALLOC && data.data != NULL)
free(data.data);
CAMLreturn(vres);
}
CAMLprim value ml_db_pget_byte(value *argv, int argn) {
return ml_db_pget(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
}
CAMLprim value ml_db_join(value vdb, value vcursors, value vflags, value unit) {
CAMLparam4(vdb, vcursors, vflags, unit);
CAMLlocal2(head, vres);
DB *db = DB_val(vdb);
DBC **cursorlist;
DBC *newcursor;
u_int32_t flags = DBFlags_val(vflags, flags_db_join);
int i, ret, clen;
TEST_HANDLE(db);
head = vcursors;
for(clen = 0; head != Val_emptylist; clen++)
head = Field(head, 1);
cursorlist = (DBC**)caml_stat_alloc(sizeof(DBC) * clen);
for(i = 0; vcursors != Val_emptylist; i++) {
head = Field(vcursors, 0);
cursorlist[i] = DBC_val(head);
TEST_HANDLE(cursorlist[i]);
vcursors = Field(vcursors, 1);
}
cursorlist[i] = NULL;
ret = db->join(db, cursorlist, &newcursor, flags);
caml_stat_free(cursorlist);
check_retval(ret);
vres = caml_alloc_custom(&dbc_ops, sizeof(DBC*), 0, 1);
DBC_val(vres) = newcursor;
CAMLreturn(vres);
}
CAMLprim value ml_db_get_multiple(value vdb) {
DB *db = DB_val(vdb);
int ret;
TEST_HANDLE(db);
ret = db->get_multiple(db);
check_retval(ret);
return Val_bool(ret);
}
CAMLprim value ml_db_set_encrypt(value vdb, value vpasswd, value vflags,
value unit) {
DB *db = DB_val(vdb);
u_int32_t flags = DBFlags_val(vflags, flags_db_encrypt);
int ret;
TEST_HANDLE(db);
ret = db->set_encrypt(db, String_val(vpasswd), flags);
check_retval(ret);
return Val_unit;
}
// make optional return
CAMLprim value ml_db_get_env(value vdb) {
CAMLparam1(vdb);
CAMLlocal1(vres);
DB *db = DB_val(vdb);
DB_ENV *dbenv = db->get_env(db);
vres = caml_alloc_custom(&dbenv_ops, sizeof(DB_ENV*), 0, 1);
DB_ENV_val(vres) = dbenv;
CAMLreturn(vres);
}
CAMLprim value ml_db_get_errpfx(value vdb) {
CAMLparam1(vdb);
DB *db = DB_val(vdb);
const char *str;
TEST_HANDLE(db);
db->get_errpfx(db, &str);
CAMLreturn(caml_copy_string(str));
}
CAMLprim value ml_db_set_errpfx(value vdb, value vstr) {
DB *db = DB_val(vdb);
TEST_HANDLE(db);
db->set_errpfx(db, String_val(vstr));
return Val_unit;
}
CAMLprim value ml_db_get_transactional(value vdb) {
DB *db = DB_val(vdb);
int ret;
TEST_HANDLE(db);
ret = db->get_transactional(db);
return Val_bool(ret);
}
CAMLprim value ml_db_get_type(value vdb) {
DB *db = DB_val(vdb);
DBTYPE dbtype;
int ret;
TEST_HANDLE(db);
ret = db->get_type(db, &dbtype);
check_retval(ret);
return enum2ml(dbtype, dbtype_enum, Size(dbtype_enum));
}
CAMLprim value ml_db_put(value vdb, value vtxn, value vkey, value vdata,
value vflags, value unit) {
CAMLparam5(vdb, vtxn, vkey, vdata, vflags);
CAMLlocal1(vres);
DB *db = DB_val(vdb);
DB_TXN *txn = DB_TXN_opt_val(vtxn);
DBT key, data;
u_int32_t flags = DBFlags_val(vflags, flags_db_put);
int ret;
db_recno_t recno;
void *saved_key_data;
TEST_HANDLE(db);
memset(&key, 0, sizeof(DBT));
if(Is_long(vkey)) {
key.data = &recno;
key.size = sizeof(recno);
recno = Long_val(vkey);
} else {
key.data = String_val(vkey);
key.size = caml_string_length(vkey);
}
//! DB_THREAD mandates memory allocation flag on key DBT
if(DB_IS_THREADED(db)) {
key.flags = DB_DBT_MALLOC;
saved_key_data = key.data;
}
memset(&data, 0, sizeof(DBT));
data.data = String_val(vdata);
data.size = caml_string_length(vdata);
ret = db->put(db, txn, &key, &data, flags);
check_retval(ret);
if(Is_long(vkey)) {
vres = Val_long((*(db_recno_t*)key.data));
} else {
vres = caml_alloc_string(key.size);
memcpy(String_val(vres), key.data, key.size);
}
if(DB_IS_THREADED(db) && key.flags & DB_DBT_MALLOC &&
key.data != saved_key_data)
free(key.data);
CAMLreturn(vres);
}
CAMLprim value ml_db_put_byte(value *argv, int argn) {
return ml_db_put(argv[0], argv[1], argv[2], argv[3], argv[4], argv[5]);
}
CAMLprim value ml_db_remove(value vdb, value vdatabase, value vfile) {
DB *db = DB_val(vdb);
char* database = NULL;
int ret;
TEST_HANDLE(db);
if(Is_block(vdatabase))
database = String_val(Some_val(vdatabase));
ret = db->remove(db, String_val(vfile), database, 0);
check_retval(ret);
return Val_unit;
}
CAMLprim value ml_db_rename(value vdb, value vfile, value vdatabase,
value vnewfile) {
DB *db = DB_val(vdb);
char* database = NULL;
int ret;
TEST_HANDLE(db);
if(Is_block(vdatabase))
database = String_val(Some_val(vdatabase));
ret = db->rename(db, String_val(vfile), database, String_val(vnewfile), 0);
check_retval(ret);
return Val_unit;
}
CAMLprim value ml_db_get_cachesize(value vdb) {
CAMLparam1(vdb);
CAMLlocal3(vres, vr1, vr2);
DB *db = DB_val(vdb);
u_int32_t gbytes, bytes;
int ncache;