- Notifications
You must be signed in to change notification settings - Fork 31.7k
/
Copy path_bsddb.c
10376 lines (8777 loc) · 273 KB
/
_bsddb.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
/*----------------------------------------------------------------------
Copyright (c) 1999-2001, Digital Creations, Fredericksburg, VA, USA
and Andrew Kuchling. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
o Redistributions of source code must retain the above copyright
notice, this list of conditions, and the disclaimer that follows.
o Redistributions in binary form must reproduce the above copyright
notice, this list of conditions, and the following disclaimer in
the documentation and/or other materials provided with the
distribution.
o Neither the name of Digital Creations nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY DIGITAL CREATIONS AND CONTRIBUTORS *AS
IS* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL DIGITAL
CREATIONS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
DAMAGE.
------------------------------------------------------------------------*/
/*
* Handwritten code to wrap version 3.x of the Berkeley DB library,
* written to replace a SWIG-generated file. It has since been updated
* to compile with Berkeley DB versions 3.2 through 4.2.
*
* This module was started by Andrew Kuchling to remove the dependency
* on SWIG in a package by Gregory P. Smith who based his work on a
* similar package by Robin Dunn <robin@alldunn.com> which wrapped
* Berkeley DB 2.7.x.
*
* Development of this module then returned full circle back to Robin Dunn
* who worked on behalf of Digital Creations to complete the wrapping of
* the DB 3.x API and to build a solid unit test suite. Robin has
* since gone onto other projects (wxPython).
*
* Gregory P. Smith <greg@krypto.org> was once again the maintainer.
*
* Since January 2008, new maintainer is Jesus Cea <jcea@jcea.es>.
* Jesus Cea licenses this code to PSF under a Contributor Agreement.
*
* Use the pybsddb-users@lists.sf.net mailing list for all questions.
* Things can change faster than the header of this file is updated. This
* file is shared with the PyBSDDB project at SourceForge:
*
* http://pybsddb.sf.net
*
* This file should remain backward compatible with Python 2.1, but see PEP
* 291 for the most current backward compatibility requirements:
*
* http://www.python.org/peps/pep-0291.html
*
* This module contains 7 types:
*
* DB (Database)
* DBCursor (Database Cursor)
* DBEnv (database environment)
* DBTxn (An explicit database transaction)
* DBLock (A lock handle)
* DBSequence (Sequence)
* DBSite (Site)
*
* More datatypes added:
*
* DBLogCursor (Log Cursor)
*
*/
/* --------------------------------------------------------------------- */
/*
* Portions of this module, associated unit tests and build scripts are the
* result of a contract with The Written Word (http://thewrittenword.com/)
* Many thanks go out to them for causing me to raise the bar on quality and
* functionality, resulting in a better bsddb3 package for all of us to use.
*
* --Robin
*/
/* --------------------------------------------------------------------- */
#include<stddef.h>/* for offsetof() */
#include<Python.h>
#defineCOMPILING_BSDDB_C
#include"bsddb.h"
#undef COMPILING_BSDDB_C
staticchar*rcs_id="$Id$";
/* --------------------------------------------------------------------- */
/* Various macro definitions */
#if (PY_VERSION_HEX<0x02050000)
typedefintPy_ssize_t;
#endif
#if (PY_VERSION_HEX<0x02060000) /* really: before python trunk r63675 */
/* This code now uses PyBytes* API function names instead of PyString*.
* These #defines map to their equivalent on earlier python versions. */
#definePyBytes_FromStringAndSize PyString_FromStringAndSize
#definePyBytes_FromString PyString_FromString
#definePyBytes_AsStringAndSize PyString_AsStringAndSize
#definePyBytes_Check PyString_Check
#definePyBytes_GET_SIZE PyString_GET_SIZE
#definePyBytes_AS_STRING PyString_AS_STRING
#endif
#if (PY_VERSION_HEX >= 0x03000000)
#defineNUMBER_Check PyLong_Check
#defineNUMBER_AsLong PyLong_AsLong
#defineNUMBER_FromLong PyLong_FromLong
#else
#defineNUMBER_Check PyInt_Check
#defineNUMBER_AsLong PyInt_AsLong
#defineNUMBER_FromLong PyInt_FromLong
#endif
#ifdefWITH_THREAD
/* These are for when calling Python --> C */
#defineMYDB_BEGIN_ALLOW_THREADS Py_BEGIN_ALLOW_THREADS;
#defineMYDB_END_ALLOW_THREADS Py_END_ALLOW_THREADS;
/* and these are for calling C --> Python */
#defineMYDB_BEGIN_BLOCK_THREADS \
PyGILState_STATE __savestate = PyGILState_Ensure();
#defineMYDB_END_BLOCK_THREADS \
PyGILState_Release(__savestate);
#else
/* Compiled without threads - avoid all this cruft */
#defineMYDB_BEGIN_ALLOW_THREADS
#defineMYDB_END_ALLOW_THREADS
#defineMYDB_BEGIN_BLOCK_THREADS
#defineMYDB_END_BLOCK_THREADS
#endif
/* --------------------------------------------------------------------- */
/* Exceptions */
staticPyObject*DBError; /* Base class, all others derive from this */
staticPyObject*DBCursorClosedError; /* raised when trying to use a closed cursor object */
staticPyObject*DBKeyEmptyError; /* DB_KEYEMPTY: also derives from KeyError */
staticPyObject*DBKeyExistError; /* DB_KEYEXIST */
staticPyObject*DBLockDeadlockError; /* DB_LOCK_DEADLOCK */
staticPyObject*DBLockNotGrantedError; /* DB_LOCK_NOTGRANTED */
staticPyObject*DBNotFoundError; /* DB_NOTFOUND: also derives from KeyError */
staticPyObject*DBOldVersionError; /* DB_OLD_VERSION */
staticPyObject*DBRunRecoveryError; /* DB_RUNRECOVERY */
staticPyObject*DBVerifyBadError; /* DB_VERIFY_BAD */
staticPyObject*DBNoServerError; /* DB_NOSERVER */
#if (DBVER<52)
staticPyObject*DBNoServerHomeError; /* DB_NOSERVER_HOME */
staticPyObject*DBNoServerIDError; /* DB_NOSERVER_ID */
#endif
staticPyObject*DBPageNotFoundError; /* DB_PAGE_NOTFOUND */
staticPyObject*DBSecondaryBadError; /* DB_SECONDARY_BAD */
staticPyObject*DBInvalidArgError; /* EINVAL */
staticPyObject*DBAccessError; /* EACCES */
staticPyObject*DBNoSpaceError; /* ENOSPC */
staticPyObject*DBNoMemoryError; /* DB_BUFFER_SMALL */
staticPyObject*DBAgainError; /* EAGAIN */
staticPyObject*DBBusyError; /* EBUSY */
staticPyObject*DBFileExistsError; /* EEXIST */
staticPyObject*DBNoSuchFileError; /* ENOENT */
staticPyObject*DBPermissionsError; /* EPERM */
staticPyObject*DBRepHandleDeadError; /* DB_REP_HANDLE_DEAD */
#if (DBVER >= 44)
staticPyObject*DBRepLockoutError; /* DB_REP_LOCKOUT */
#endif
#if (DBVER >= 46)
staticPyObject*DBRepLeaseExpiredError; /* DB_REP_LEASE_EXPIRED */
#endif
#if (DBVER >= 47)
staticPyObject*DBForeignConflictError; /* DB_FOREIGN_CONFLICT */
#endif
staticPyObject*DBRepUnavailError; /* DB_REP_UNAVAIL */
#if (DBVER<48)
#defineDB_GID_SIZE DB_XIDDATASIZE
#endif
/* --------------------------------------------------------------------- */
/* Structure definitions */
#ifPYTHON_API_VERSION<1010
#error "Python 2.1 or later required"
#endif
/* Defaults for moduleFlags in DBEnvObject and DBObject. */
#defineDEFAULT_GET_RETURNS_NONE 1
#defineDEFAULT_CURSOR_SET_RETURNS_NONE 1 /* 0 in pybsddb < 4.2, python < 2.4 */
/* See comment in Python 2.6 "object.h" */
#ifndefstaticforward
#definestaticforward static
#endif
#ifndefstatichere
#definestatichere static
#endif
staticforwardPyTypeObjectDB_Type, DBCursor_Type, DBEnv_Type, DBTxn_Type,
DBLock_Type, DBLogCursor_Type;
staticforwardPyTypeObjectDBSequence_Type;
#if (DBVER >= 52)
staticforwardPyTypeObjectDBSite_Type;
#endif
#ifndefPy_TYPE
/* for compatibility with Python 2.5 and earlier */
#definePy_TYPE(ob) (((PyObject*)(ob))->ob_type)
#endif
#defineDBObject_Check(v) (Py_TYPE(v) == &DB_Type)
#defineDBCursorObject_Check(v) (Py_TYPE(v) == &DBCursor_Type)
#defineDBLogCursorObject_Check(v) (Py_TYPE(v) == &DBLogCursor_Type)
#defineDBEnvObject_Check(v) (Py_TYPE(v) == &DBEnv_Type)
#defineDBTxnObject_Check(v) (Py_TYPE(v) == &DBTxn_Type)
#defineDBLockObject_Check(v) (Py_TYPE(v) == &DBLock_Type)
#defineDBSequenceObject_Check(v) (Py_TYPE(v) == &DBSequence_Type)
#if (DBVER >= 52)
#defineDBSiteObject_Check(v) (Py_TYPE(v) == &DBSite_Type)
#endif
#if (DBVER<46)
#define_DBC_close(dbc) dbc->c_close(dbc)
#define_DBC_count(dbc,a,b) dbc->c_count(dbc,a,b)
#define_DBC_del(dbc,a) dbc->c_del(dbc,a)
#define_DBC_dup(dbc,a,b) dbc->c_dup(dbc,a,b)
#define_DBC_get(dbc,a,b,c) dbc->c_get(dbc,a,b,c)
#define_DBC_pget(dbc,a,b,c,d) dbc->c_pget(dbc,a,b,c,d)
#define_DBC_put(dbc,a,b,c) dbc->c_put(dbc,a,b,c)
#else
#define_DBC_close(dbc) dbc->close(dbc)
#define_DBC_count(dbc,a,b) dbc->count(dbc,a,b)
#define_DBC_del(dbc,a) dbc->del(dbc,a)
#define_DBC_dup(dbc,a,b) dbc->dup(dbc,a,b)
#define_DBC_get(dbc,a,b,c) dbc->get(dbc,a,b,c)
#define_DBC_pget(dbc,a,b,c,d) dbc->pget(dbc,a,b,c,d)
#define_DBC_put(dbc,a,b,c) dbc->put(dbc,a,b,c)
#endif
/* --------------------------------------------------------------------- */
/* Utility macros and functions */
#defineINSERT_IN_DOUBLE_LINKED_LIST(backlink,object) \
{ \
object->sibling_next=backlink; \
object->sibling_prev_p=&(backlink); \
backlink=object; \
if (object->sibling_next) { \
object->sibling_next->sibling_prev_p=&(object->sibling_next); \
} \
}
#defineEXTRACT_FROM_DOUBLE_LINKED_LIST(object) \
{ \
if (object->sibling_next) { \
object->sibling_next->sibling_prev_p=object->sibling_prev_p; \
} \
*(object->sibling_prev_p)=object->sibling_next; \
}
#defineEXTRACT_FROM_DOUBLE_LINKED_LIST_MAYBE_NULL(object) \
{ \
if (object->sibling_next) { \
object->sibling_next->sibling_prev_p=object->sibling_prev_p; \
} \
if (object->sibling_prev_p) { \
*(object->sibling_prev_p)=object->sibling_next; \
} \
}
#defineINSERT_IN_DOUBLE_LINKED_LIST_TXN(backlink,object) \
{ \
object->sibling_next_txn=backlink; \
object->sibling_prev_p_txn=&(backlink); \
backlink=object; \
if (object->sibling_next_txn) { \
object->sibling_next_txn->sibling_prev_p_txn= \
&(object->sibling_next_txn); \
} \
}
#defineEXTRACT_FROM_DOUBLE_LINKED_LIST_TXN(object) \
{ \
if (object->sibling_next_txn) { \
object->sibling_next_txn->sibling_prev_p_txn= \
object->sibling_prev_p_txn; \
} \
*(object->sibling_prev_p_txn)=object->sibling_next_txn; \
}
#defineRETURN_IF_ERR() \
if (makeDBError(err)) { \
return NULL; \
}
#defineRETURN_NONE() Py_INCREF(Py_None); return Py_None;
#define_CHECK_OBJECT_NOT_CLOSED(nonNull, pyErrObj, name) \
if ((nonNull) == NULL) { \
PyObject *errTuple = NULL; \
errTuple = Py_BuildValue("(is)", 0, #name " object has been closed"); \
if (errTuple) { \
PyErr_SetObject((pyErrObj), errTuple); \
Py_DECREF(errTuple); \
} \
return NULL; \
}
#defineCHECK_DB_NOT_CLOSED(dbobj) \
_CHECK_OBJECT_NOT_CLOSED(dbobj->db, DBError, DB)
#defineCHECK_ENV_NOT_CLOSED(env) \
_CHECK_OBJECT_NOT_CLOSED(env->db_env, DBError, DBEnv)
#defineCHECK_CURSOR_NOT_CLOSED(curs) \
_CHECK_OBJECT_NOT_CLOSED(curs->dbc, DBCursorClosedError, DBCursor)
#defineCHECK_LOGCURSOR_NOT_CLOSED(logcurs) \
_CHECK_OBJECT_NOT_CLOSED(logcurs->logc, DBCursorClosedError, DBLogCursor)
#defineCHECK_SEQUENCE_NOT_CLOSED(curs) \
_CHECK_OBJECT_NOT_CLOSED(curs->sequence, DBError, DBSequence)
#if (DBVER >= 52)
#defineCHECK_SITE_NOT_CLOSED(db_site) \
_CHECK_OBJECT_NOT_CLOSED(db_site->site, DBError, DBSite)
#endif
#defineCHECK_DBFLAG(mydb, flag) (((mydb)->flags & (flag)) || \
(((mydb)->myenvobj != NULL) && ((mydb)->myenvobj->flags & (flag))))
#defineCLEAR_DBT(dbt) (memset(&(dbt), 0, sizeof(dbt)))
#defineFREE_DBT(dbt) if ((dbt.flags & (DB_DBT_MALLOC|DB_DBT_REALLOC)) && \
dbt.data != NULL) { free(dbt.data); dbt.data = NULL; }
staticintmakeDBError(interr);
/* Return the access method type of the DBObject */
staticint_DB_get_type(DBObject*self)
{
DBTYPEtype;
interr;
err=self->db->get_type(self->db, &type);
if (makeDBError(err)) {
return-1;
}
returntype;
}
/* Create a DBT structure (containing key and data values) from Python
strings. Returns 1 on success, 0 on an error. */
staticintmake_dbt(PyObject*obj, DBT*dbt)
{
CLEAR_DBT(*dbt);
if (obj==Py_None) {
/* no need to do anything, the structure has already been zeroed */
}
elseif (!PyArg_Parse(obj, "s#", &dbt->data, &dbt->size)) {
PyErr_SetString(PyExc_TypeError,
#if (PY_VERSION_HEX<0x03000000)
"Data values must be of type string or None.");
#else
"Data values must be of type bytes or None.");
#endif
return0;
}
return1;
}
/* Recno and Queue DBs can have integer keys. This function figures out
what's been given, verifies that it's allowed, and then makes the DBT.
Caller MUST call FREE_DBT(key) when done. */
staticint
make_key_dbt(DBObject*self, PyObject*keyobj, DBT*key, int*pflags)
{
db_recno_trecno;
inttype;
CLEAR_DBT(*key);
if (keyobj==Py_None) {
type=_DB_get_type(self);
if (type==-1)
return0;
if (type==DB_RECNO||type==DB_QUEUE) {
PyErr_SetString(
PyExc_TypeError,
"None keys not allowed for Recno and Queue DB's");
return0;
}
/* no need to do anything, the structure has already been zeroed */
}
elseif (PyBytes_Check(keyobj)) {
/* verify access method type */
type=_DB_get_type(self);
if (type==-1)
return0;
if (type==DB_RECNO||type==DB_QUEUE) {
PyErr_SetString(
PyExc_TypeError,
#if (PY_VERSION_HEX<0x03000000)
"String keys not allowed for Recno and Queue DB's");
#else
"Bytes keys not allowed for Recno and Queue DB's");
#endif
return0;
}
/*
* NOTE(gps): I don't like doing a data copy here, it seems
* wasteful. But without a clean way to tell FREE_DBT if it
* should free key->data or not we have to. Other places in
* the code check for DB_THREAD and forceably set DBT_MALLOC
* when we otherwise would leave flags 0 to indicate that.
*/
key->data=malloc(PyBytes_GET_SIZE(keyobj));
if (key->data==NULL) {
PyErr_SetString(PyExc_MemoryError, "Key memory allocation failed");
return0;
}
memcpy(key->data, PyBytes_AS_STRING(keyobj),
PyBytes_GET_SIZE(keyobj));
key->flags=DB_DBT_REALLOC;
key->size=PyBytes_GET_SIZE(keyobj);
}
elseif (NUMBER_Check(keyobj)) {
/* verify access method type */
type=_DB_get_type(self);
if (type==-1)
return0;
if (type==DB_BTREE&&pflags!=NULL) {
/* if BTREE then an Integer key is allowed with the
* DB_SET_RECNO flag */
*pflags |= DB_SET_RECNO;
}
elseif (type!=DB_RECNO&&type!=DB_QUEUE) {
PyErr_SetString(
PyExc_TypeError,
"Integer keys only allowed for Recno and Queue DB's");
return0;
}
/* Make a key out of the requested recno, use allocated space so DB
* will be able to realloc room for the real key if needed. */
recno=NUMBER_AsLong(keyobj);
key->data=malloc(sizeof(db_recno_t));
if (key->data==NULL) {
PyErr_SetString(PyExc_MemoryError, "Key memory allocation failed");
return0;
}
key->ulen=key->size=sizeof(db_recno_t);
memcpy(key->data, &recno, sizeof(db_recno_t));
key->flags=DB_DBT_REALLOC;
}
else {
PyErr_Format(PyExc_TypeError,
#if (PY_VERSION_HEX<0x03000000)
"String or Integer object expected for key, %s found",
#else
"Bytes or Integer object expected for key, %s found",
#endif
Py_TYPE(keyobj)->tp_name);
return0;
}
return1;
}
/* Add partial record access to an existing DBT data struct.
If dlen and doff are set, then the DB_DBT_PARTIAL flag will be set
and the data storage/retrieval will be done using dlen and doff. */
staticintadd_partial_dbt(DBT*d, intdlen, intdoff) {
/* if neither were set we do nothing (-1 is the default value) */
if ((dlen==-1) && (doff==-1)) {
return1;
}
if ((dlen<0) || (doff<0)) {
PyErr_SetString(PyExc_TypeError, "dlen and doff must both be >= 0");
return0;
}
d->flags=d->flags | DB_DBT_PARTIAL;
d->dlen= (unsigned int) dlen;
d->doff= (unsigned int) doff;
return1;
}
/* a safe strcpy() without the zeroing behaviour and semantics of strncpy. */
/* TODO: make this use the native libc strlcpy() when available (BSD) */
unsigned intour_strlcpy(char*dest, constchar*src, unsigned intn)
{
unsigned intsrclen, copylen;
srclen=strlen(src);
if (n <= 0)
returnsrclen;
copylen= (srclen>n-1) ? n-1 : srclen;
/* populate dest[0] thru dest[copylen-1] */
memcpy(dest, src, copylen);
/* guarantee null termination */
dest[copylen] =0;
returnsrclen;
}
/* Callback used to save away more information about errors from the DB
* library. */
staticchar_db_errmsg[1024];
staticvoid_db_errorCallback(constDB_ENV*db_env,
constchar*prefix, constchar*msg)
{
our_strlcpy(_db_errmsg, msg, sizeof(_db_errmsg));
}
/*
** We need these functions because some results
** are undefined if pointer is NULL. Some other
** give None instead of "".
**
** This functions are static and will be
** -I hope- inlined.
*/
staticconstchar*DummyString="This string is a simple placeholder";
staticPyObject*Build_PyString(constchar*p,ints)
{
if (!p) {
p=DummyString;
assert(s==0);
}
returnPyBytes_FromStringAndSize(p,s);
}
staticPyObject*BuildValue_S(constvoid*p,ints)
{
if (!p) {
p=DummyString;
assert(s==0);
}
returnPyBytes_FromStringAndSize(p, s);
}
staticPyObject*BuildValue_SS(constvoid*p1,ints1,constvoid*p2,ints2)
{
PyObject*a, *b, *r;
if (!p1) {
p1=DummyString;
assert(s1==0);
}
if (!p2) {
p2=DummyString;
assert(s2==0);
}
if (!(a=PyBytes_FromStringAndSize(p1, s1))) {
returnNULL;
}
if (!(b=PyBytes_FromStringAndSize(p2, s2))) {
Py_DECREF(a);
returnNULL;
}
r=PyTuple_Pack(2, a, b) ;
Py_DECREF(a);
Py_DECREF(b);
returnr;
}
staticPyObject*BuildValue_IS(inti,constvoid*p,ints)
{
PyObject*a, *r;
if (!p) {
p=DummyString;
assert(s==0);
}
if (!(a=PyBytes_FromStringAndSize(p, s))) {
returnNULL;
}
r=Py_BuildValue("iO", i, a);
Py_DECREF(a);
returnr;
}
staticPyObject*BuildValue_LS(longl,constvoid*p,ints)
{
PyObject*a, *r;
if (!p) {
p=DummyString;
assert(s==0);
}
if (!(a=PyBytes_FromStringAndSize(p, s))) {
returnNULL;
}
r=Py_BuildValue("lO", l, a);
Py_DECREF(a);
returnr;
}
/* make a nice exception object to raise for errors. */
staticintmakeDBError(interr)
{
charerrTxt[2048]; /* really big, just in case... */
PyObject*errObj=NULL;
PyObject*errTuple=NULL;
intexceptionRaised=0;
unsigned intbytes_left;
switch (err) {
case0: /* successful, no error */
return0;
caseDB_KEYEMPTY: errObj=DBKeyEmptyError; break;
caseDB_KEYEXIST: errObj=DBKeyExistError; break;
caseDB_LOCK_DEADLOCK: errObj=DBLockDeadlockError; break;
caseDB_LOCK_NOTGRANTED: errObj=DBLockNotGrantedError; break;
caseDB_NOTFOUND: errObj=DBNotFoundError; break;
caseDB_OLD_VERSION: errObj=DBOldVersionError; break;
caseDB_RUNRECOVERY: errObj=DBRunRecoveryError; break;
caseDB_VERIFY_BAD: errObj=DBVerifyBadError; break;
caseDB_NOSERVER: errObj=DBNoServerError; break;
#if (DBVER<52)
caseDB_NOSERVER_HOME: errObj=DBNoServerHomeError; break;
caseDB_NOSERVER_ID: errObj=DBNoServerIDError; break;
#endif
caseDB_PAGE_NOTFOUND: errObj=DBPageNotFoundError; break;
caseDB_SECONDARY_BAD: errObj=DBSecondaryBadError; break;
caseDB_BUFFER_SMALL: errObj=DBNoMemoryError; break;
caseENOMEM: errObj=PyExc_MemoryError; break;
caseEINVAL: errObj=DBInvalidArgError; break;
caseEACCES: errObj=DBAccessError; break;
caseENOSPC: errObj=DBNoSpaceError; break;
caseEAGAIN: errObj=DBAgainError; break;
caseEBUSY : errObj=DBBusyError; break;
caseEEXIST: errObj=DBFileExistsError; break;
caseENOENT: errObj=DBNoSuchFileError; break;
caseEPERM : errObj=DBPermissionsError; break;
caseDB_REP_HANDLE_DEAD : errObj=DBRepHandleDeadError; break;
#if (DBVER >= 44)
caseDB_REP_LOCKOUT : errObj=DBRepLockoutError; break;
#endif
#if (DBVER >= 46)
caseDB_REP_LEASE_EXPIRED : errObj=DBRepLeaseExpiredError; break;
#endif
#if (DBVER >= 47)
caseDB_FOREIGN_CONFLICT : errObj=DBForeignConflictError; break;
#endif
caseDB_REP_UNAVAIL : errObj=DBRepUnavailError; break;
default: errObj=DBError; break;
}
if (errObj!=NULL) {
bytes_left=our_strlcpy(errTxt, db_strerror(err), sizeof(errTxt));
/* Ensure that bytes_left never goes negative */
if (_db_errmsg[0] &&bytes_left< (sizeof(errTxt) -4)) {
bytes_left=sizeof(errTxt) -bytes_left-4-1;
assert(bytes_left >= 0);
strcat(errTxt, " -- ");
strncat(errTxt, _db_errmsg, bytes_left);
}
_db_errmsg[0] =0;
errTuple=Py_BuildValue("(is)", err, errTxt);
if (errTuple==NULL) {
Py_DECREF(errObj);
return !0;
}
PyErr_SetObject(errObj, errTuple);
Py_DECREF(errTuple);
}
return ((errObj!=NULL) ||exceptionRaised);
}
/* set a type exception */
staticvoidmakeTypeError(char*expected, PyObject*found)
{
PyErr_Format(PyExc_TypeError, "Expected %s argument, %s found.",
expected, Py_TYPE(found)->tp_name);
}
/* verify that an obj is either None or a DBTxn, and set the txn pointer */
staticintcheckTxnObj(PyObject*txnobj, DB_TXN**txn)
{
if (txnobj==Py_None||txnobj==NULL) {
*txn=NULL;
return1;
}
if (DBTxnObject_Check(txnobj)) {
*txn= ((DBTxnObject*)txnobj)->txn;
return1;
}
else
makeTypeError("DBTxn", txnobj);
return0;
}
/* Delete a key from a database
Returns 0 on success, -1 on an error. */
staticint_DB_delete(DBObject*self, DB_TXN*txn, DBT*key, intflags)
{
interr;
MYDB_BEGIN_ALLOW_THREADS;
err=self->db->del(self->db, txn, key, 0);
MYDB_END_ALLOW_THREADS;
if (makeDBError(err)) {
return-1;
}
return0;
}
/* Store a key into a database
Returns 0 on success, -1 on an error. */
staticint_DB_put(DBObject*self, DB_TXN*txn, DBT*key, DBT*data, intflags)
{
interr;
MYDB_BEGIN_ALLOW_THREADS;
err=self->db->put(self->db, txn, key, data, flags);
MYDB_END_ALLOW_THREADS;
if (makeDBError(err)) {
return-1;
}
return0;
}
/* Get a key/data pair from a cursor */
staticPyObject*_DBCursor_get(DBCursorObject*self, intextra_flags,
PyObject*args, PyObject*kwargs, char*format)
{
interr;
PyObject*retval=NULL;
DBTkey, data;
intdlen=-1;
intdoff=-1;
intflags=0;
staticchar*kwnames[] = { "flags", "dlen", "doff", NULL };
if (!PyArg_ParseTupleAndKeywords(args, kwargs, format, kwnames,
&flags, &dlen, &doff))
returnNULL;
CHECK_CURSOR_NOT_CLOSED(self);
flags |= extra_flags;
CLEAR_DBT(key);
CLEAR_DBT(data);
if (!add_partial_dbt(&data, dlen, doff))
returnNULL;
MYDB_BEGIN_ALLOW_THREADS;
err=_DBC_get(self->dbc, &key, &data, flags);
MYDB_END_ALLOW_THREADS;
if ((err==DB_NOTFOUND||err==DB_KEYEMPTY)
&&self->mydb->moduleFlags.getReturnsNone) {
Py_INCREF(Py_None);
retval=Py_None;
}
elseif (makeDBError(err)) {
retval=NULL;
}
else { /* otherwise, success! */
/* if Recno or Queue, return the key as an Int */
switch (_DB_get_type(self->mydb)) {
case-1:
retval=NULL;
break;
caseDB_RECNO:
caseDB_QUEUE:
retval=BuildValue_IS(*((db_recno_t*)key.data), data.data, data.size);
break;
caseDB_HASH:
caseDB_BTREE:
default:
retval=BuildValue_SS(key.data, key.size, data.data, data.size);
break;
}
}
returnretval;
}
/* add an integer to a dictionary using the given name as a key */
staticvoid_addIntToDict(PyObject*dict, char*name, intvalue)
{
PyObject*v=NUMBER_FromLong((long) value);
if (!v||PyDict_SetItemString(dict, name, v))
PyErr_Clear();
Py_XDECREF(v);
}
/* The same, when the value is a time_t */
staticvoid_addTimeTToDict(PyObject*dict, char*name, time_tvalue)
{
PyObject*v;
/* if the value fits in regular int, use that. */
#ifdefPY_LONG_LONG
if (sizeof(time_t) >sizeof(long))
v=PyLong_FromLongLong((PY_LONG_LONG) value);
else
#endif
v=NUMBER_FromLong((long) value);
if (!v||PyDict_SetItemString(dict, name, v))
PyErr_Clear();
Py_XDECREF(v);
}
/* add an db_seq_t to a dictionary using the given name as a key */
staticvoid_addDb_seq_tToDict(PyObject*dict, char*name, db_seq_tvalue)
{
PyObject*v=PyLong_FromLongLong(value);
if (!v||PyDict_SetItemString(dict, name, v))
PyErr_Clear();
Py_XDECREF(v);
}
staticvoid_addDB_lsnToDict(PyObject*dict, char*name, DB_LSNvalue)
{
PyObject*v=Py_BuildValue("(ll)",value.file,value.offset);
if (!v||PyDict_SetItemString(dict, name, v))
PyErr_Clear();
Py_XDECREF(v);
}
/* --------------------------------------------------------------------- */
/* Allocators and deallocators */
staticDBObject*
newDBObject(DBEnvObject*arg, intflags)
{
DBObject*self;
DB_ENV*db_env=NULL;
interr;
self=PyObject_New(DBObject, &DB_Type);
if (self==NULL)
returnNULL;
self->flags=0;
self->setflags=0;
self->myenvobj=NULL;
self->db=NULL;
self->children_cursors=NULL;
self->children_sequences=NULL;
self->associateCallback=NULL;
self->btCompareCallback=NULL;
self->dupCompareCallback=NULL;
self->primaryDBType=0;
Py_INCREF(Py_None);
self->private_obj=Py_None;
self->in_weakreflist=NULL;
/* keep a reference to our python DBEnv object */
if (arg) {
Py_INCREF(arg);
self->myenvobj=arg;
db_env=arg->db_env;
INSERT_IN_DOUBLE_LINKED_LIST(self->myenvobj->children_dbs,self);
} else {
self->sibling_prev_p=NULL;
self->sibling_next=NULL;
}
self->txn=NULL;
self->sibling_prev_p_txn=NULL;
self->sibling_next_txn=NULL;
if (self->myenvobj) {
self->moduleFlags=self->myenvobj->moduleFlags;
}
else {
self->moduleFlags.getReturnsNone=DEFAULT_GET_RETURNS_NONE;
self->moduleFlags.cursorSetReturnsNone=DEFAULT_CURSOR_SET_RETURNS_NONE;
}
MYDB_BEGIN_ALLOW_THREADS;
err=db_create(&self->db, db_env, flags);
if (self->db!=NULL) {
self->db->set_errcall(self->db, _db_errorCallback);
self->db->app_private= (void*)self;
}
MYDB_END_ALLOW_THREADS;
/* TODO add a weakref(self) to the self->myenvobj->open_child_weakrefs
* list so that a DBEnv can refuse to close without aborting any open
* DBTxns and closing any open DBs first. */
if (makeDBError(err)) {
if (self->myenvobj) {
Py_CLEAR(self->myenvobj);
}
Py_DECREF(self);
self=NULL;
}
returnself;
}
/* Forward declaration */
staticPyObject*DB_close_internal(DBObject*self, intflags, intdo_not_close);
staticvoid
DB_dealloc(DBObject*self)
{
PyObject*dummy;
if (self->db!=NULL) {
dummy=DB_close_internal(self, 0, 0);
/*
** Raising exceptions while doing
** garbage collection is a fatal error.
*/
if (dummy)
Py_DECREF(dummy);
else
PyErr_Clear();
}
if (self->in_weakreflist!=NULL) {
PyObject_ClearWeakRefs((PyObject*) self);
}
if (self->myenvobj) {
Py_CLEAR(self->myenvobj);
}
if (self->associateCallback!=NULL) {
Py_CLEAR(self->associateCallback);
}
if (self->btCompareCallback!=NULL) {
Py_CLEAR(self->btCompareCallback);
}
if (self->dupCompareCallback!=NULL) {
Py_CLEAR(self->dupCompareCallback);
}
Py_DECREF(self->private_obj);
PyObject_Del(self);
}