-
Notifications
You must be signed in to change notification settings - Fork 4
/
BerkeleyDB.xs
6269 lines (5632 loc) · 159 KB
/
BerkeleyDB.xs
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
/*
BerkeleyDB.xs -- Perl 5 interface to Berkeley DB version 2, 3, 4, 5 & 6
written by Paul Marquess <pmqs@cpan.org>
All comments/suggestions/problems are welcome
Copyright (c) 1997-2016 Paul Marquess. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.
*/
#ifdef __cplusplus
extern "C" {
#endif
#define PERL_NO_GET_CONTEXT
#define PERL_POLLUTE
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
/* XSUB.h defines a macro called abort */
/* This clashes with the txn abort method in Berkeley DB 4.x */
/* This is a problem with ActivePerl (at least) */
#ifdef _WIN32
# ifdef abort
# undef abort
# endif
# ifdef fopen
# undef fopen
# endif
# ifdef fclose
# undef fclose
# endif
# ifdef rename
# undef rename
# endif
# ifdef open
# undef open
# endif
#endif
#ifndef SvUTF8_off
# define SvUTF8_off(x)
#endif
#if PERL_REVISION == 5 && (PERL_VERSION < 8 || (PERL_VERSION == 8 && PERL_SUBVERSION < 4 ))
# ifdef SvPVbyte_force
# undef SvPVbyte_force
# endif
# define SvPVbyte_force(sv,lp) SvPV_force(sv,lp)
#endif
/* Being the Berkeley DB we prefer the <sys/cdefs.h> (which will be
* shortly #included by the <db.h>) __attribute__ to the possibly
* already defined __attribute__, for example by GNUC or by Perl. */
#undef __attribute__
#ifdef USE_PERLIO
# define GetFILEptr(sv) PerlIO_exportFILE(IoIFP(sv_2io(sv)), NULL)
#else
# define GetFILEptr(sv) IoIFP(sv_2io(sv))
#endif
#include <db.h>
/* Check the version of Berkeley DB */
#ifndef DB_VERSION_MAJOR
#ifdef HASHMAGIC
#error db.h is from Berkeley DB 1.x - need at least Berkeley DB 2.6.4
#else
#error db.h is not for Berkeley DB at all.
#endif
#endif
#if (DB_VERSION_MAJOR == 2 && DB_VERSION_MINOR < 6) ||\
(DB_VERSION_MAJOR == 2 && DB_VERSION_MINOR == 6 && DB_VERSION_PATCH < 4)
# error db.h is from Berkeley DB 2.0-2.5 - need at least Berkeley DB 2.6.4
#endif
#if DB_VERSION_MAJOR > 3 || (DB_VERSION_MAJOR == 2 && DB_VERSION_MINOR >= 2)
# define AT_LEAST_DB_2_2
#endif
#if (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 0)
# define IS_DB_3_0_x
#endif
#if DB_VERSION_MAJOR >= 3
# define AT_LEAST_DB_3
#endif
#if DB_VERSION_MAJOR > 3 || (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR >= 1)
# define AT_LEAST_DB_3_1
#endif
#if DB_VERSION_MAJOR > 3 || (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR >= 2)
# define AT_LEAST_DB_3_2
#endif
#if DB_VERSION_MAJOR > 3 || \
(DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR > 2) ||\
(DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR == 2 && DB_VERSION_PATCH >= 6)
# define AT_LEAST_DB_3_2_6
#endif
#if DB_VERSION_MAJOR > 3 || (DB_VERSION_MAJOR == 3 && DB_VERSION_MINOR >= 3)
# define AT_LEAST_DB_3_3
#endif
#if DB_VERSION_MAJOR >= 4
# define AT_LEAST_DB_4
#endif
#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 1)
# define AT_LEAST_DB_4_1
#endif
#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 2)
# define AT_LEAST_DB_4_2
#endif
#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 3)
# define AT_LEAST_DB_4_3
#endif
#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 4)
# define AT_LEAST_DB_4_4
#endif
#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 5)
# define AT_LEAST_DB_4_5
#endif
#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 6)
# define AT_LEAST_DB_4_6
#endif
#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 7)
# define AT_LEAST_DB_4_7
#endif
#if DB_VERSION_MAJOR > 4 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR >= 8)
# define AT_LEAST_DB_4_8
#endif
#if DB_VERSION_MAJOR > 5 || (DB_VERSION_MAJOR == 5 && DB_VERSION_MINOR >= 1)
# define AT_LEAST_DB_5_1
#endif
#if DB_VERSION_MAJOR > 5 || (DB_VERSION_MAJOR == 5 && DB_VERSION_MINOR >= 2)
# define AT_LEAST_DB_5_2
#endif
#if DB_VERSION_MAJOR > 5 || (DB_VERSION_MAJOR == 5 && DB_VERSION_MINOR >= 3)
# define AT_LEAST_DB_5_3
#endif
#if DB_VERSION_MAJOR >= 6
# define AT_LEAST_DB_6_0
#endif
#if DB_VERSION_MAJOR > 6 || (DB_VERSION_MAJOR == 6 && DB_VERSION_MINOR >= 2)
# define AT_LEAST_DB_6_2
#endif
#ifdef __cplusplus
}
#endif
#define DBM_FILTERING
#define STRICT_CLOSE
/* #define ALLOW_RECNO_OFFSET */
/* #define TRACE */
#if DB_VERSION_MAJOR == 2 && ! defined(DB_LOCK_DEADLOCK)
# define DB_LOCK_DEADLOCK EAGAIN
#endif /* DB_VERSION_MAJOR == 2 */
#if DB_VERSION_MAJOR == 2
# define DB_QUEUE 4
#endif /* DB_VERSION_MAJOR == 2 */
#if DB_VERSION_MAJOR == 2
# define BackRef internal
#else
# if DB_VERSION_MAJOR == 3 || (DB_VERSION_MAJOR == 4 && DB_VERSION_MINOR == 0)
# define BackRef cj_internal
# else
# define BackRef api_internal
# endif
#endif
#ifdef AT_LEAST_DB_3_2
# define DB_callback DB * db,
#else
# define DB_callback
#endif
#if DB_VERSION_MAJOR > 2
typedef struct {
int db_lorder;
size_t db_cachesize;
size_t db_pagesize;
void *(*db_malloc) __P((size_t));
u_int32_t bt_maxkey;
u_int32_t bt_minkey;
#ifdef AT_LEAST_DB_6_0
int (*bt_compare)
__P((DB_callback const DBT *, const DBT *, size_t *));
int (*dup_compare)
__P((DB_callback const DBT *, const DBT *, size_t *));
#else
int (*bt_compare)
__P((DB_callback const DBT *, const DBT *));
int (*dup_compare)
__P((DB_callback const DBT *, const DBT *));
#endif
size_t (*bt_prefix)
__P((DB_callback const DBT *, const DBT *));
u_int32_t h_ffactor;
u_int32_t h_nelem;
u_int32_t (*h_hash)
__P((DB_callback const void *, u_int32_t));
int re_pad;
int re_delim;
u_int32_t re_len;
char *re_source;
#define DB_DELIMITER 0x0001
#define DB_FIXEDLEN 0x0008
#define DB_PAD 0x0010
u_int32_t flags;
u_int32_t q_extentsize;
u_int32_t heapsize_gbytes;
u_int32_t heapsize_bytes;
u_int32_t blob_threshold;
char *blob_dir;
} DB_INFO ;
#endif /* DB_VERSION_MAJOR > 2 */
typedef struct {
int Status ;
/* char ErrBuff[1000] ; */
SV * ErrPrefix ;
SV * ErrHandle ;
#ifdef AT_LEAST_DB_4_3
SV * MsgHandle ;
#endif
DB_ENV * Env ;
int open_dbs ;
int TxnMgrStatus ;
int active ;
bool txn_enabled ;
bool opened ;
bool cds_enabled;
} BerkeleyDB_ENV_type ;
typedef struct {
DBTYPE type ;
bool recno_or_queue ;
char * filename ;
BerkeleyDB_ENV_type * parent_env ;
DB * dbp ;
SV * compare ;
bool in_compare ;
SV * dup_compare ;
bool in_dup_compare ;
SV * prefix ;
bool in_prefix ;
SV * hash ;
bool in_hash ;
#ifdef AT_LEAST_DB_3_3
SV * associated ;
bool secondary_db ;
#endif
#ifdef AT_LEAST_DB_4_8
SV * associated_foreign ;
SV * bt_compress ;
SV * bt_uncompress ;
#endif
bool primary_recno_or_queue ;
int Status ;
DB_INFO * info ;
DBC * cursor ;
DB_TXN * txn ;
int open_cursors ;
#ifdef AT_LEAST_DB_4_3
int open_sequences ;
#endif
u_int32_t partial ;
u_int32_t dlen ;
u_int32_t doff ;
int active ;
bool cds_enabled;
#ifdef ALLOW_RECNO_OFFSET
int array_base ;
#endif
#ifdef DBM_FILTERING
SV * filter_fetch_key ;
SV * filter_store_key ;
SV * filter_fetch_value ;
SV * filter_store_value ;
int filtering ;
#endif
} BerkeleyDB_type;
typedef struct {
DBTYPE type ;
bool recno_or_queue ;
char * filename ;
DB * dbp ;
SV * compare ;
SV * dup_compare ;
SV * prefix ;
SV * hash ;
#ifdef AT_LEAST_DB_3_3
SV * associated ;
bool secondary_db ;
#endif
#ifdef AT_LEAST_DB_4_8
SV * associated_foreign ;
#endif
bool primary_recno_or_queue ;
int Status ;
DB_INFO * info ;
DBC * cursor ;
DB_TXN * txn ;
BerkeleyDB_type * parent_db ;
u_int32_t partial ;
u_int32_t dlen ;
u_int32_t doff ;
int active ;
bool cds_enabled;
#ifdef ALLOW_RECNO_OFFSET
int array_base ;
#endif
#ifdef DBM_FILTERING
SV * filter_fetch_key ;
SV * filter_store_key ;
SV * filter_fetch_value ;
SV * filter_store_value ;
int filtering ;
#endif
} BerkeleyDB_Cursor_type;
typedef struct {
int Status ;
#ifdef AT_LEAST_DB_6_0
DB_STREAM * stream ;
#endif
int active ;
u_int32_t partial ;
u_int32_t dlen ;
u_int32_t doff ;
#ifdef DBM_FILTERING
SV * filter_fetch_key ;
SV * filter_store_key ;
SV * filter_fetch_value ;
SV * filter_store_value ;
int filtering ;
#endif
} BerkeleyDB_DbStream_type ;
typedef struct {
BerkeleyDB_ENV_type * env ;
} BerkeleyDB_TxnMgr_type ;
#if 1
typedef struct {
int Status ;
DB_TXN * txn ;
int active ;
} BerkeleyDB_Txn_type ;
#else
typedef DB_TXN BerkeleyDB_Txn_type ;
#endif
#ifdef AT_LEAST_DB_4_3
typedef struct {
int active;
BerkeleyDB_type *db;
DB_SEQUENCE *seq;
} BerkeleyDB_Sequence_type;
#else
typedef int BerkeleyDB_Sequence_type;
typedef SV* db_seq_t;
#endif
#ifndef DB_DBT_APPMALLOC
#define DB_DBT_APPMALLOC 0
#endif
typedef BerkeleyDB_ENV_type * BerkeleyDB__Env ;
typedef BerkeleyDB_ENV_type * BerkeleyDB__Env__Raw ;
typedef BerkeleyDB_ENV_type * BerkeleyDB__Env__Inner ;
typedef BerkeleyDB_type * BerkeleyDB ;
typedef void * BerkeleyDB__Raw ;
typedef BerkeleyDB_type * BerkeleyDB__Common ;
typedef BerkeleyDB_type * BerkeleyDB__Common__Raw ;
typedef BerkeleyDB_type * BerkeleyDB__Common__Inner ;
typedef BerkeleyDB_type * BerkeleyDB__Hash ;
typedef BerkeleyDB_type * BerkeleyDB__Hash__Raw ;
typedef BerkeleyDB_type * BerkeleyDB__Btree ;
typedef BerkeleyDB_type * BerkeleyDB__Btree__Raw ;
typedef BerkeleyDB_type * BerkeleyDB__Recno ;
typedef BerkeleyDB_type * BerkeleyDB__Recno__Raw ;
typedef BerkeleyDB_type * BerkeleyDB__Queue ;
typedef BerkeleyDB_type * BerkeleyDB__Queue__Raw ;
typedef BerkeleyDB_type * BerkeleyDB__Heap ;
typedef BerkeleyDB_type * BerkeleyDB__Heap__Raw ;
typedef BerkeleyDB_Cursor_type BerkeleyDB__Cursor_type ;
typedef BerkeleyDB_Cursor_type * BerkeleyDB__Cursor ;
typedef BerkeleyDB_Cursor_type * BerkeleyDB__Cursor__Raw ;
typedef BerkeleyDB_TxnMgr_type * BerkeleyDB__TxnMgr ;
typedef BerkeleyDB_TxnMgr_type * BerkeleyDB__TxnMgr__Raw ;
typedef BerkeleyDB_TxnMgr_type * BerkeleyDB__TxnMgr__Inner ;
typedef BerkeleyDB_Txn_type * BerkeleyDB__Txn ;
typedef BerkeleyDB_Txn_type * BerkeleyDB__Txn__Raw ;
typedef BerkeleyDB_Txn_type * BerkeleyDB__Txn__Inner ;
#ifdef AT_LEAST_DB_4_3
typedef BerkeleyDB_Sequence_type * BerkeleyDB__Sequence ;
#else
typedef int * BerkeleyDB__Sequence ;
#endif
//#ifdef AT_LEAST_DB_6_0
typedef BerkeleyDB_DbStream_type BerkeleyDB__DbStream_type ;
typedef BerkeleyDB_DbStream_type * BerkeleyDB__DbStream ;
typedef BerkeleyDB_DbStream_type * BerkeleyDB__DbStream__Raw ;
//#else
//typedef int BerkeleyDB__DbStream_type ;
//typedef int * BerkeleyDB__DbStream ;
//typedef int * BerkeleyDB__DbStream__Raw ;
//#endif
#if 0
typedef DB_LOG * BerkeleyDB__Log ;
typedef DB_LOCKTAB * BerkeleyDB__Lock ;
#endif
typedef DBT DBTKEY ;
typedef DBT DBT_OPT ;
typedef DBT DBT_B ;
typedef DBT DBTKEY_B ;
typedef DBT DBTKEY_B4Blob ;
typedef DBT DBTKEY_Br ;
typedef DBT DBTKEY_Bpr ;
typedef DBT DBTKEY_seq ;
typedef DBT DBT_Blob ;
typedef DBT DBTVALUE ;
typedef void * PV_or_NULL ;
typedef PerlIO * IO_or_NULL ;
typedef int DualType ;
typedef SV SVnull;
static void
hash_delete(char * hash, char * key);
#ifdef TRACE
# define Trace(x) do { printf("# "); printf x; fflush(stdout); } while (0) ;
#else
# define Trace(x)
#endif
#ifdef ALLOW_RECNO_OFFSET
# define RECNO_BASE db->array_base
#else
# define RECNO_BASE 1
#endif
#if DB_VERSION_MAJOR == 2
# define flagSet_DB2(i, f) i |= f
#else
# define flagSet_DB2(i, f)
#endif
#if DB_VERSION_MAJOR == 2 && DB_VERSION_MINOR < 5
# define flagSet(bitmask) (flags & (bitmask))
#else
# define flagSet(bitmask) ((flags & DB_OPFLAGS_MASK) == (bitmask))
#endif
#ifdef DB_GET_BOTH_RANGE
# define flagSetBoth() (flagSet(DB_GET_BOTH) || flagSet(DB_GET_BOTH_RANGE))
#else
# define flagSetBoth() (flagSet(DB_GET_BOTH))
#endif
#ifndef AT_LEAST_DB_4
typedef int db_timeout_t ;
#endif
#ifdef AT_LEAST_DB_5_2
# define isHeapDb(db) ((db)->type == DB_HEAP)
/* __heap_exists is not exported by db.h, so include prototype here */
int __heap_exist __P((void));
#else
# define isHeapDb(db) (0)
# define DB_HEAP_RID_SZ 1
#endif
#ifndef AT_LEAST_DB_6_0
typedef int db_off_t;
#endif
#define ERR_BUFF "BerkeleyDB::Error"
#define ZMALLOC(to, typ) ((to = (typ *)safemalloc(sizeof(typ))), \
Zero(to,1,typ))
#define DBT_clear(x) Zero(&x, 1, DBT) ;
#if 1
#define getInnerObject(x) (*av_fetch((AV*)SvRV(x), 0, FALSE))
#else
#define getInnerObject(x) ((SV*)SvRV(sv))
#endif
#define my_sv_setpvn(sv, d, s) do { \
s ? sv_setpvn(sv, d, s) : sv_setpv(sv, ""); \
SvUTF8_off(sv); \
} while(0)
#define GetValue_iv(h,k) (((sv = readHash(h, k)) && sv != &PL_sv_undef) \
? SvIV(sv) : 0)
#define SetValue_iv(i, k) if ((sv = readHash(hash, k)) && sv != &PL_sv_undef) \
i = SvIV(sv)
#define SetValue_io(i, k) if ((sv = readHash(hash, k)) && sv != &PL_sv_undef) \
i = GetFILEptr(sv)
#define SetValue_sv(i, k) if ((sv = readHash(hash, k)) && sv != &PL_sv_undef) \
i = sv
#define SetValue_pv(i, k,t) if ((sv = readHash(hash, k)) && sv != &PL_sv_undef) \
i = (t)SvPV(sv,PL_na)
#define SetValue_pvx(i, k, t) if ((sv = readHash(hash, k)) && sv != &PL_sv_undef) \
i = (t)SvPVX(sv)
#define SetValue_ov(i,k,t) if ((sv = readHash(hash, k)) && sv != &PL_sv_undef) {\
IV tmp = SvIV(getInnerObject(sv)) ; \
i = INT2PTR(t, tmp) ; \
}
#define SetValue_ovx(i,k,t) if ((sv = readHash(hash, k)) && sv != &PL_sv_undef) {\
HV * hv = (HV *)GetInternalObject(sv); \
SV ** svp = hv_fetch(hv, "db", 2, FALSE);\
IV tmp = SvIV(*svp); \
i = INT2PTR(t, tmp) ; \
}
#define SetValue_ovX(i,k,t) if ((sv = readHash(hash, k)) && sv != &PL_sv_undef) {\
IV tmp = SvIV(GetInternalObject(sv));\
i = INT2PTR(t, tmp) ; \
}
#define LastDBerror DB_RUNRECOVERY
#define setDUALerrno(var, err) \
sv_setnv(var, (double)err) ; \
sv_setpv(var, ((err) ? db_strerror(err) : "")) ;\
SvNOK_on(var);
#define OutputValue(arg, name) \
{ if (RETVAL == 0) { \
my_sv_setpvn(arg, name.data, name.size) ; \
DBM_ckFilter(arg, filter_fetch_value,"filter_fetch_value") ; \
} \
}
#define OutputValue_B(arg, name) \
{ if (RETVAL == 0) { \
if (db->type == DB_BTREE && \
flagSet(DB_GET_RECNO)){ \
sv_setiv(arg, (I32)(*(I32*)name.data) - RECNO_BASE); \
} \
else { \
my_sv_setpvn(arg, name.data, name.size) ; \
} \
DBM_ckFilter(arg, filter_fetch_value, "filter_fetch_value"); \
} \
}
#define OutputKey(arg, name) \
{ if (RETVAL == 0) \
{ \
if (!db->recno_or_queue) { \
my_sv_setpvn(arg, name.data, name.size); \
} \
else \
sv_setiv(arg, (I32)*(I32*)name.data - RECNO_BASE); \
if (! isHeapDb(db)) \
DBM_ckFilter(arg, filter_fetch_key, "filter_fetch_key") ; \
} \
}
#define OutputKeyBlob(arg, name) \
{ if (RETVAL) \
{ \
my_sv_setpvn(arg, name.data, name.size) ; \
DBM_ckFilter(arg, filter_fetch_key, "filter_fetch_key") ; \
} \
}
#ifdef AT_LEAST_DB_4_3
#define InputKey_seq(arg, var) \
{ \
SV* my_sv = arg ; \
/* DBM_ckFilter(my_sv, filter_store_key, "filter_store_key"); */ \
DBT_clear(var) ; \
SvGETMAGIC(arg) ; \
if (seq->db->recno_or_queue) { \
Value = GetRecnoKey(seq->db, SvIV(my_sv)) ; \
var.data = & Value; \
var.size = (int)sizeof(db_recno_t); \
} \
else { \
STRLEN len; \
var.data = SvPV(my_sv, len); \
var.size = (int)len; \
} \
}
#define OutputKey_seq(arg, name) \
{ if (RETVAL == 0) \
{ \
if (!seq->db->recno_or_queue) { \
my_sv_setpvn(arg, name.data, name.size); \
} \
else \
sv_setiv(arg, (I32)*(I32*)name.data - RECNO_BASE); \
} \
}
#else
#define InputKey_seq(arg, var)
#define OutputKey_seq(arg, name)
#endif
#define OutputKey_B(arg, name) \
{ if (RETVAL == 0) \
{ \
if (db->recno_or_queue \
|| (db->type == DB_BTREE && \
flagSet(DB_GET_RECNO))){ \
sv_setiv(arg, (I32)(*(I32*)name.data) - RECNO_BASE); \
} \
else { \
my_sv_setpvn(arg, name.data, name.size); \
} \
DBM_ckFilter(arg, filter_fetch_key, "filter_fetch_key") ; \
} \
}
#define OutputKey_Br(arg, name) \
{ if (RETVAL == 0) \
{ \
if (db->recno_or_queue || db->primary_recno_or_queue \
|| (db->type == DB_BTREE && \
flagSet(DB_GET_RECNO))){ \
sv_setiv(arg, (I32)(*(I32*)name.data) - RECNO_BASE); \
} \
else { \
my_sv_setpvn(arg, name.data, name.size); \
} \
DBM_ckFilter(arg, filter_fetch_key, "filter_fetch_key") ; \
} \
}
#define OutputKey_Bpr(arg, name) \
{ if (RETVAL == 0) \
{ \
if (db->primary_recno_or_queue \
|| (db->type == DB_BTREE && \
flagSet(DB_GET_RECNO))){ \
sv_setiv(arg, (I32)(*(I32*)name.data) - RECNO_BASE); \
} \
else { \
my_sv_setpvn(arg, name.data, name.size); \
} \
DBM_ckFilter(arg, filter_fetch_key, "filter_fetch_key") ; \
} \
}
#define SetPartial(data,db) \
data.flags = db->partial ; \
data.dlen = db->dlen ; \
data.doff = db->doff ;
#define ckActive(active, type) \
{ \
if (!active) \
softCrash("%s is already closed", type) ; \
}
#define ckActive_Environment(a) ckActive(a, "Environment")
#define ckActive_TxnMgr(a) ckActive(a, "Transaction Manager")
#define ckActive_Transaction(a) ckActive(a, "Transaction")
#define ckActive_DbStream(a) ckActive(a, "DB_STREAM")
#define ckActive_Database(a) ckActive(a, "Database")
#define ckActive_Cursor(a) ckActive(a, "Cursor")
#ifdef AT_LEAST_DB_4_3
#define ckActive_Sequence(a) ckActive(a, "Sequence")
#else
#define ckActive_Sequence(a)
#endif
#define dieIfEnvOpened(e, m) if (e->opened) softCrash("Cannot call method BerkeleyDB::Env::%s after environment has been opened", m);
#define isSTDOUT_ERR(f) ((f) == stdout || (f) == stderr)
/* Internal Global Data */
#define MY_CXT_KEY "BerkeleyDB::_guts" XS_VERSION
typedef struct {
db_recno_t x_Value;
db_recno_t x_zero;
DBTKEY x_empty;
#ifndef AT_LEAST_DB_3_2
BerkeleyDB x_CurrentDB;
#endif
} my_cxt_t;
START_MY_CXT
#define Value (MY_CXT.x_Value)
#define zero (MY_CXT.x_zero)
#define empty (MY_CXT.x_empty)
#ifdef AT_LEAST_DB_3_2
# define CurrentDB ((BerkeleyDB)db->BackRef)
#else
# define CurrentDB (MY_CXT.x_CurrentDB)
#endif
#ifdef AT_LEAST_DB_3_2
# define getCurrentDB ((BerkeleyDB)db->BackRef)
# define saveCurrentDB(db)
#else
# define getCurrentDB (MY_CXT.x_CurrentDB)
# define saveCurrentDB(db) (MY_CXT.x_CurrentDB) = db
#endif
#if 0
static char ErrBuff[1000] ;
#endif
#ifdef AT_LEAST_DB_3_3
# if PERL_REVISION == 5 && PERL_VERSION <= 4
/* saferealloc in perl5.004 will croak if it is given a NULL pointer*/
void *
MyRealloc(void * ptr, size_t size)
{
if (ptr == NULL )
return safemalloc(size) ;
else
return saferealloc(ptr, size) ;
}
# else
# define MyRealloc saferealloc
# endif
#endif
static char *
my_strdup(const char *s)
{
if (s == NULL)
return NULL ;
{
MEM_SIZE l = strlen(s) + 1;
char *s1 = (char *)safemalloc(l);
Copy(s, s1, (MEM_SIZE)l, char);
return s1;
}
}
#if DB_VERSION_MAJOR == 2
static char *
db_strerror(int err)
{
if (err == 0)
return "" ;
if (err > 0)
return Strerror(err) ;
switch (err) {
case DB_INCOMPLETE:
return ("DB_INCOMPLETE: Sync was unable to complete");
case DB_KEYEMPTY:
return ("DB_KEYEMPTY: Non-existent key/data pair");
case DB_KEYEXIST:
return ("DB_KEYEXIST: Key/data pair already exists");
case DB_LOCK_DEADLOCK:
return (
"DB_LOCK_DEADLOCK: Locker killed to resolve a deadlock");
case DB_LOCK_NOTGRANTED:
return ("DB_LOCK_NOTGRANTED: Lock not granted");
case DB_LOCK_NOTHELD:
return ("DB_LOCK_NOTHELD: Lock not held by locker");
case DB_NOTFOUND:
return ("DB_NOTFOUND: No matching key/data pair found");
case DB_RUNRECOVERY:
return ("DB_RUNRECOVERY: Fatal error, run database recovery");
default:
return "Unknown Error" ;
}
}
#endif /* DB_VERSION_MAJOR == 2 */
#ifdef TRACE
#if DB_VERSION_MAJOR > 2
static char *
my_db_strerror(int err)
{
#ifdef dTHX
dTHX;
#endif
static char buffer[1000] ;
SV * sv = perl_get_sv(ERR_BUFF, FALSE) ;
sprintf(buffer, "%d: %s", err, db_strerror(err)) ;
if (err && sv) {
strcat(buffer, ", ") ;
strcat(buffer, SvPVX(sv)) ;
}
return buffer;
}
#endif
#endif
static void
close_everything(void)
{
#ifdef dTHX
dTHX;
#endif
Trace(("close_everything\n")) ;
/* Abort All Transactions */
{
BerkeleyDB__Txn__Raw tid ;
HE * he ;
I32 len ;
HV * hv = perl_get_hv("BerkeleyDB::Term::Txn", TRUE);
int all = 0 ;
int closed = 0 ;
(void)hv_iterinit(hv) ;
Trace(("BerkeleyDB::Term::close_all_txns dirty=%d\n", PL_dirty)) ;
while ( (he = hv_iternext(hv)) ) {
tid = * (BerkeleyDB__Txn__Raw *) hv_iterkey(he, &len) ;
Trace((" Aborting Transaction [%p] in [%p] Active [%d]\n", tid->txn, tid, tid->active));
if (tid->active) {
#ifdef AT_LEAST_DB_4
tid->txn->abort(tid->txn) ;
#else
txn_abort(tid->txn);
#endif
++ closed ;
}
tid->active = FALSE ;
++ all ;
}
Trace(("End of BerkeleyDB::Term::close_all_txns aborted %d of %d transactios\n",closed, all)) ;
}
#ifdef AT_LEAST_DB_6_0
/* Close All DB_STREAM */
{
BerkeleyDB__DbStream db ;
HE * he ;
I32 len ;
HV * hv = perl_get_hv("BerkeleyDB::Term::DbStream", TRUE);
int all = 0 ;
int closed = 0 ;
(void) hv_iterinit(hv) ;
Trace(("BerkeleyDB::DbStream::close_all_dbstream \n")) ;
while ( (he = hv_iternext(hv)) ) {
db = * (BerkeleyDB__DbStream*) hv_iterkey(he, &len) ;
Trace((" Closing DbStream [%p] in [%p] Active [%d]\n", db->stream, db, db->active));
if (db->active) {
(db->stream->close)(db->stream, 0);
++ closed ;
}
db->active = FALSE ;
++ all ;
}
Trace(("End of BerkeleyDB::Term::close_all_dbstream closed %d of %d streams\n",closed, all)) ;
}
#endif
/* Close All Cursors */
{
BerkeleyDB__Cursor db ;
HE * he ;
I32 len ;
HV * hv = perl_get_hv("BerkeleyDB::Term::Cursor", TRUE);
int all = 0 ;
int closed = 0 ;
(void) hv_iterinit(hv) ;
Trace(("BerkeleyDB::Term::close_all_cursors \n")) ;
while ( (he = hv_iternext(hv)) ) {
db = * (BerkeleyDB__Cursor*) hv_iterkey(he, &len) ;
Trace((" Closing Cursor [%p] in [%p] Active [%d]\n", db->cursor, db, db->active));
if (db->active) {
((db->cursor)->c_close)(db->cursor) ;
++ closed ;
}
db->active = FALSE ;
++ all ;
}
Trace(("End of BerkeleyDB::Term::close_all_cursors closed %d of %d cursors\n",closed, all)) ;
}
/* Close All Databases */
{
BerkeleyDB db ;
HE * he ;
I32 len ;
HV * hv = perl_get_hv("BerkeleyDB::Term::Db", TRUE);
int all = 0 ;
int closed = 0 ;
(void)hv_iterinit(hv) ;
Trace(("BerkeleyDB::Term::close_all_dbs\n" )) ;
while ( (he = hv_iternext(hv)) ) {
db = * (BerkeleyDB*) hv_iterkey(he, &len) ;
Trace((" Closing Database [%p] in [%p] Active [%d]\n", db->dbp, db, db->active));
if (db->active) {
(db->dbp->close)(db->dbp, 0) ;
++ closed ;
}
db->active = FALSE ;
++ all ;
}
Trace(("End of BerkeleyDB::Term::close_all_dbs closed %d of %d dbs\n",closed, all)) ;
}
/* Close All Environments */
{
BerkeleyDB__Env env ;
HE * he ;
I32 len ;
HV * hv = perl_get_hv("BerkeleyDB::Term::Env", TRUE);
int all = 0 ;
int closed = 0 ;
(void)hv_iterinit(hv) ;
Trace(("BerkeleyDB::Term::close_all_envs\n")) ;
while ( (he = hv_iternext(hv)) ) {
env = * (BerkeleyDB__Env*) hv_iterkey(he, &len) ;
Trace((" Closing Environment [%p] in [%p] Active [%d]\n", env->Env, env, env->active));
if (env->active) {
#if DB_VERSION_MAJOR == 2
db_appexit(env->Env) ;
#else
(env->Env->close)(env->Env, 0) ;
#endif
++ closed ;
}
env->active = FALSE ;
++ all ;
}
Trace(("End of BerkeleyDB::Term::close_all_envs closed %d of %d dbs\n",closed, all)) ;
}
Trace(("end close_everything\n")) ;
}
static void
destroyDB(BerkeleyDB db)
{
#ifdef dTHX
dTHX;
#endif
if (! PL_dirty && db->active) {
if (db->parent_env && db->parent_env->open_dbs)
-- db->parent_env->open_dbs ;
-- db->open_cursors ;
((db->dbp)->close)(db->dbp, 0) ;
}