1
/************************************************
6
$Date: 2007-02-13 08:01:19 +0900 (Tue, 13 Feb 2007) $
7
modified at: Mon Jan 24 15:59:52 JST 1994
9
Documentation by Peter Adolphs < futzilogik at users dot sourceforge dot net >
11
************************************************/
20
* Document-class: GDBM
24
* Ruby extension for GNU dbm (gdbm) -- a simple database engine for storing
25
* key-value pairs on disk.
29
* GNU dbm is a library for simple databases. A database is a file that stores
30
* key-value pairs. Gdbm allows the user to store, retrieve, and delete data by
31
* key. It furthermore allows a non-sorted traversal of all key-value pairs.
32
* A gdbm database thus provides the same functionality as a hash. As
33
* with objects of the Hash class, elements can be accessed with <tt>[]</tt>.
34
* Furthermore, GDBM mixes in the Enumerable module, thus providing convenient
35
* methods such as #find, #collect, #map, etc.
37
* A process is allowed to open several different databases at the same time.
38
* A process can open a database as a "reader" or a "writer". Whereas a reader
39
* has only read-access to the database, a writer has read- and write-access.
40
* A database can be accessed either by any number of readers or by exactly one
41
* writer at the same time.
45
* 1. Opening/creating a database, and filling it with some entries:
49
* gdbm = GDBM.new("fruitstore.db")
50
* gdbm["ananas"] = "3"
51
* gdbm["banana"] = "8"
52
* gdbm["cranberry"] = "4909"
55
* 2. Reading out a database:
59
* gdbm = GDBM.new("fruitstore.db")
60
* gdbm.each_pair do |key, value|
61
* print "#{key}: #{value}\n"
73
* * http://www.gnu.org/software/gdbm/
75
static VALUE rb_cGDBM, rb_eGDBMError, rb_eGDBMFatalError;
77
#define RUBY_GDBM_RW_BIT 0x20000000
79
#define MY_BLOCK_SIZE (2048)
80
#define MY_FATAL_FUNC rb_gdbm_fatal
85
rb_raise(rb_eGDBMFatalError, "%s", msg);
96
rb_raise(rb_eRuntimeError, "closed GDBM file");
99
#define GetDBM(obj, dbmp) do {\
100
Data_Get_Struct(obj, struct dbmdata, dbmp);\
101
if (dbmp == 0) closed_dbm();\
102
if (dbmp->di_dbm == 0) closed_dbm();\
105
#define GetDBM2(obj, data, dbm) {\
107
(dbm) = dbmp->di_dbm;\
112
struct dbmdata *dbmp;
115
if (dbmp->di_dbm) gdbm_close(dbmp->di_dbm);
124
* Closes the associated database file.
130
struct dbmdata *dbmp;
133
gdbm_close(dbmp->di_dbm);
141
* gdbm.closed? -> true or false
143
* Returns true if the associated database file has been closed.
149
struct dbmdata *dbmp;
151
Data_Get_Struct(obj, struct dbmdata, dbmp);
154
if (dbmp->di_dbm == 0)
160
static VALUE fgdbm_s_alloc _((VALUE));
166
return Data_Wrap_Struct(klass, 0, free_dbm, 0);
171
* GDBM.new(filename, mode = 0666, flags = nil)
173
* Creates a new GDBM instance by opening a gdbm file named _filename_.
174
* If the file does not exist, a new file with file mode _mode_ will be
175
* created. _flags_ may be one of the following:
176
* * *READER* - open as a reader
177
* * *WRITER* - open as a writer
178
* * *WRCREAT* - open as a writer; if the database does not exist, create a new one
179
* * *NEWDB* - open as a writer; overwrite any existing databases
181
* The values *WRITER*, *WRCREAT* and *NEWDB* may be combined with the following
182
* values by bitwise or:
183
* * *SYNC* - cause all database operations to be synchronized to the disk
184
* * *NOLOCK* - do not lock the database file
186
* If no _flags_ are specified, the GDBM object will try to open the database
187
* file as a writer and will create it if it does not already exist
188
* (cf. flag <tt>WRCREAT</tt>). If this fails (for instance, if another process
189
* has already opened the database as a reader), it will try to open the
190
* database file as a reader (cf. flag <tt>READER</tt>).
193
fgdbm_initialize(argc, argv, obj)
198
VALUE file, vmode, vflags;
200
struct dbmdata *dbmp;
203
if (rb_scan_args(argc, argv, "12", &file, &vmode, &vflags) == 1) {
204
mode = 0666; /* default value */
206
else if (NIL_P(vmode)) {
207
mode = -1; /* return nil if DB does not exist */
210
mode = NUM2INT(vmode);
214
flags = NUM2INT(vflags);
216
SafeStringValue(file);
218
if (flags & RUBY_GDBM_RW_BIT) {
219
flags &= ~RUBY_GDBM_RW_BIT;
220
dbm = gdbm_open(RSTRING(file)->ptr, MY_BLOCK_SIZE,
221
flags, mode, MY_FATAL_FUNC);
226
dbm = gdbm_open(RSTRING(file)->ptr, MY_BLOCK_SIZE,
227
GDBM_WRCREAT|flags, mode, MY_FATAL_FUNC);
229
dbm = gdbm_open(RSTRING(file)->ptr, MY_BLOCK_SIZE,
230
GDBM_WRITER|flags, 0, MY_FATAL_FUNC);
232
dbm = gdbm_open(RSTRING(file)->ptr, MY_BLOCK_SIZE,
233
GDBM_READER|flags, 0, MY_FATAL_FUNC);
237
if (mode == -1) return Qnil;
239
if (gdbm_errno == GDBM_FILE_OPEN_ERROR ||
240
gdbm_errno == GDBM_CANT_BE_READER ||
241
gdbm_errno == GDBM_CANT_BE_WRITER)
242
rb_sys_fail(RSTRING(file)->ptr);
244
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
247
dbmp = ALLOC(struct dbmdata);
248
free_dbm(DATA_PTR(obj));
249
DATA_PTR(obj) = dbmp;
258
* GDBM.open(filename, mode = 0666, flags = nil)
259
* GDBM.open(filename, mode = 0666, flags = nil) { |gdbm| ... }
261
* If called without a block, this is synonymous to GDBM::new.
262
* If a block is given, the new GDBM instance will be passed to the block
263
* as a parameter, and the corresponding database file will be closed
264
* after the execution of the block code has been finished.
266
* Example for an open call with a block:
269
* GDBM.open("fruitstore.db") do |gdbm|
270
* gdbm.each_pair do |key, value|
271
* print "#{key}: #{value}\n"
276
fgdbm_s_open(argc, argv, klass)
281
VALUE obj = Data_Wrap_Struct(klass, 0, free_dbm, 0);
283
if (NIL_P(fgdbm_initialize(argc, argv, obj))) {
287
if (rb_block_given_p()) {
288
return rb_ensure(rb_yield, obj, fgdbm_close, obj);
295
rb_gdbm_fetch(dbm, key)
302
val = gdbm_fetch(dbm, key);
306
str = rb_obj_alloc(rb_cString);
307
RSTRING(str)->len = val.dsize;
308
RSTRING(str)->aux.capa = val.dsize;
309
RSTRING(str)->ptr = REALLOC_N(val.dptr,char,val.dsize+1);
310
RSTRING(str)->ptr[val.dsize] = '\0';
317
rb_gdbm_fetch2(dbm, keystr)
324
key.dptr = RSTRING(keystr)->ptr;
325
key.dsize = RSTRING(keystr)->len;
327
return rb_gdbm_fetch(dbm, key);
331
rb_gdbm_fetch3(obj, keystr)
334
struct dbmdata *dbmp;
337
GetDBM2(obj, dbmp, dbm);
338
return rb_gdbm_fetch2(dbm, keystr);
342
rb_gdbm_firstkey(dbm)
348
key = gdbm_firstkey(dbm);
352
str = rb_obj_alloc(rb_cString);
353
RSTRING(str)->len = key.dsize;
354
RSTRING(str)->aux.capa = key.dsize;
355
RSTRING(str)->ptr = REALLOC_N(key.dptr,char,key.dsize+1);
356
RSTRING(str)->ptr[RSTRING(str)->len] = '\0';
363
rb_gdbm_nextkey(dbm, keystr)
370
key.dptr = RSTRING(keystr)->ptr;
371
key.dsize = RSTRING(keystr)->len;
372
key2 = gdbm_nextkey(dbm, key);
376
str = rb_obj_alloc(rb_cString);
377
RSTRING(str)->len = key2.dsize;
378
RSTRING(str)->aux.capa = key2.dsize;
379
RSTRING(str)->ptr = REALLOC_N(key2.dptr,char,key2.dsize+1);
380
RSTRING(str)->ptr[RSTRING(str)->len] = '\0';
387
fgdbm_fetch(obj, keystr, ifnone)
388
VALUE obj, keystr, ifnone;
392
valstr = rb_gdbm_fetch3(obj, keystr);
394
if (ifnone == Qnil && rb_block_given_p())
395
return rb_yield(keystr);
405
* Retrieves the _value_ corresponding to _key_.
408
fgdbm_aref(obj, keystr)
411
return rb_gdbm_fetch3(obj, keystr);
416
* gdbm.fetch(key [, default]) -> value
418
* Retrieves the _value_ corresponding to _key_. If there is no value
419
* associated with _key_, _default_ will be returned instead.
422
fgdbm_fetch_m(argc, argv, obj)
427
VALUE keystr, valstr, ifnone;
429
rb_scan_args(argc, argv, "11", &keystr, &ifnone);
430
valstr = fgdbm_fetch(obj, keystr, ifnone);
431
if (argc == 1 && !rb_block_given_p() && NIL_P(valstr))
432
rb_raise(rb_eIndexError, "key not found");
439
* gdbm.index(value) -> key
441
* Returns the _key_ for a given _value_. If several keys may map to the
442
* same value, the key that is found first will be returned.
445
fgdbm_index(obj, valstr)
448
struct dbmdata *dbmp;
450
VALUE keystr, valstr2;
453
GetDBM2(obj, dbmp, dbm);
454
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
455
keystr = rb_gdbm_nextkey(dbm, keystr)) {
457
valstr2 = rb_gdbm_fetch2(dbm, keystr);
458
if (!NIL_P(valstr2) &&
459
RSTRING(valstr)->len == RSTRING(valstr2)->len &&
460
memcmp(RSTRING(valstr)->ptr, RSTRING(valstr2)->ptr,
461
RSTRING(valstr)->len) == 0) {
469
fgdbm_indexes(argc, argv, obj)
477
new = rb_ary_new2(argc);
478
for (i=0; i<argc; i++) {
479
rb_ary_push(new, rb_gdbm_fetch3(obj, argv[i]));
487
* gdbm.select { |value| block } -> array
489
* Returns a new array of all values of the database for which _block_
493
fgdbm_select(argc, argv, obj)
498
VALUE new = rb_ary_new2(argc);
501
if (rb_block_given_p()) {
503
struct dbmdata *dbmp;
507
rb_raise(rb_eArgError, "wrong number arguments(%d for 0)", argc);
509
GetDBM2(obj, dbmp, dbm);
510
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
511
keystr = rb_gdbm_nextkey(dbm, keystr)) {
512
VALUE assoc = rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr));
513
VALUE v = rb_yield(assoc);
516
rb_ary_push(new, assoc);
518
GetDBM2(obj, dbmp, dbm);
522
rb_warn("GDBM#select(index..) is deprecated; use GDBM#values_at");
524
for (i=0; i<argc; i++) {
525
rb_ary_push(new, rb_gdbm_fetch3(obj, argv[i]));
534
* gdbm.values_at(key, ...) -> array
536
* Returns an array of the values associated with each specified _key_.
539
fgdbm_values_at(argc, argv, obj)
544
VALUE new = rb_ary_new2(argc);
547
for (i=0; i<argc; i++) {
548
rb_ary_push(new, rb_gdbm_fetch3(obj, argv[i]));
559
if (OBJ_FROZEN(obj)) rb_error_frozen("GDBM");
563
rb_gdbm_delete(obj, keystr)
567
struct dbmdata *dbmp;
572
key.dptr = RSTRING(keystr)->ptr;
573
key.dsize = RSTRING(keystr)->len;
575
GetDBM2(obj, dbmp, dbm);
576
if (!gdbm_exists(dbm, key)) {
580
if (gdbm_delete(dbm, key)) {
582
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
584
else if (dbmp->di_size >= 0) {
592
* gdbm.delete(key) -> value or nil
594
* Removes the key-value-pair with the specified _key_ from this database and
595
* returns the corresponding _value_. Returns nil if the database is empty.
598
fgdbm_delete(obj, keystr)
603
valstr = fgdbm_fetch(obj, keystr, Qnil);
604
rb_gdbm_delete(obj, keystr);
610
* gdbm.shift -> (key, value) or nil
612
* Removes a key-value-pair from this database and returns it as a
613
* two-item array [ _key_, _value_ ]. Returns nil if the database is empty.
619
struct dbmdata *dbmp;
621
VALUE keystr, valstr;
624
GetDBM2(obj, dbmp, dbm);
625
keystr = rb_gdbm_firstkey(dbm);
626
if (NIL_P(keystr)) return Qnil;
627
valstr = rb_gdbm_fetch2(dbm, keystr);
628
rb_gdbm_delete(obj, keystr);
630
return rb_assoc_new(keystr, valstr);
635
* gdbm.delete_if { |key, value| block } -> gdbm
636
* gdbm.reject! { |key, value| block } -> gdbm
638
* Deletes every key-value pair from _gdbm_ for which _block_ evaluates to true.
644
struct dbmdata *dbmp;
646
VALUE keystr, valstr;
647
VALUE ret, ary = rb_ary_new();
648
int i, status = 0, n;
651
GetDBM2(obj, dbmp, dbm);
655
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
656
keystr = rb_gdbm_nextkey(dbm, keystr)) {
658
valstr = rb_gdbm_fetch2(dbm, keystr);
659
ret = rb_protect(rb_yield, rb_assoc_new(keystr, valstr), &status);
660
if (status != 0) break;
661
if (RTEST(ret)) rb_ary_push(ary, keystr);
662
GetDBM2(obj, dbmp, dbm);
665
for (i = 0; i < RARRAY(ary)->len; i++)
666
rb_gdbm_delete(obj, RARRAY(ary)->ptr[i]);
667
if (status) rb_jump_tag(status);
668
if (n > 0) dbmp->di_size = n - RARRAY(ary)->len;
677
* Removes all the key-value pairs within _gdbm_.
684
struct dbmdata *dbmp;
688
GetDBM2(obj, dbmp, dbm);
692
while (key = gdbm_firstkey(dbm), key.dptr) {
693
if (gdbm_delete(dbm, key)) {
695
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
700
while (key = gdbm_firstkey(dbm), key.dptr) {
701
for (; key.dptr; key = nextkey) {
702
nextkey = gdbm_nextkey(dbm, key);
703
if (gdbm_delete(dbm, key)) {
705
if (nextkey.dptr) free(nextkey.dptr);
706
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
719
* gdbm.invert -> hash
721
* Returns a hash created by using _gdbm_'s values as keys, and the keys
728
struct dbmdata *dbmp;
730
VALUE keystr, valstr;
731
VALUE hash = rb_hash_new();
733
GetDBM2(obj, dbmp, dbm);
734
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
735
keystr = rb_gdbm_nextkey(dbm, keystr)) {
736
valstr = rb_gdbm_fetch2(dbm, keystr);
738
rb_hash_aset(hash, valstr, keystr);
743
static VALUE each_pair _((VALUE));
749
return rb_funcall(obj, rb_intern("each_pair"), 0, 0);
752
static VALUE fgdbm_store _((VALUE,VALUE,VALUE));
758
Check_Type(pair, T_ARRAY);
759
if (RARRAY(pair)->len < 2) {
760
rb_raise(rb_eArgError, "pair must be [key, value]");
762
fgdbm_store(dbm, RARRAY(pair)->ptr[0], RARRAY(pair)->ptr[1]);
768
* gdbm.update(other) -> gdbm
770
* Adds the key-value pairs of _other_ to _gdbm_, overwriting entries with
771
* duplicate keys with those from _other_. _other_ must have an each_pair
775
fgdbm_update(obj, other)
778
rb_iterate(each_pair, other, update_i, obj);
784
* gdbm.replace(other) -> gdbm
786
* Replaces the content of _gdbm_ with the key-value pairs of _other_.
787
* _other_ must have an each_pair method.
790
fgdbm_replace(obj, other)
794
rb_iterate(each_pair, other, update_i, obj);
800
* gdbm[key]= value -> value
801
* gdbm.store(key, value) -> value
803
* Associates the value _value_ with the specified _key_.
806
fgdbm_store(obj, keystr, valstr)
807
VALUE obj, keystr, valstr;
810
struct dbmdata *dbmp;
817
key.dptr = RSTRING(keystr)->ptr;
818
key.dsize = RSTRING(keystr)->len;
820
val.dptr = RSTRING(valstr)->ptr;
821
val.dsize = RSTRING(valstr)->len;
823
GetDBM2(obj, dbmp, dbm);
825
if (gdbm_store(dbm, key, val, GDBM_REPLACE)) {
826
if (errno == EPERM) rb_sys_fail(0);
827
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
835
* gdbm.length -> fixnum
836
* gdbm.size -> fixnum
838
* Returns the number of key-value pairs in this database.
845
struct dbmdata *dbmp;
849
GetDBM2(obj, dbmp, dbm);
850
if (dbmp->di_size > 0) return INT2FIX(dbmp->di_size);
852
for (key = gdbm_firstkey(dbm); key.dptr; key = nextkey) {
853
nextkey = gdbm_nextkey(dbm, key);
864
* gdbm.empty? -> true or false
866
* Returns true if the database is empty.
873
struct dbmdata *dbmp;
877
if (dbmp->di_size < 0) {
880
key = gdbm_firstkey(dbm);
888
if (dbmp->di_size == 0) return Qtrue;
894
* gdbm.each_value { |value| block } -> gdbm
896
* Executes _block_ for each key in the database, passing the corresponding
897
* _value_ as a parameter.
900
fgdbm_each_value(obj)
903
struct dbmdata *dbmp;
907
GetDBM2(obj, dbmp, dbm);
908
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
909
keystr = rb_gdbm_nextkey(dbm, keystr)) {
911
rb_yield(rb_gdbm_fetch2(dbm, keystr));
912
GetDBM2(obj, dbmp, dbm);
919
* gdbm.each_key { |key| block } -> gdbm
921
* Executes _block_ for each key in the database, passing the
922
* _key_ as a parameter.
928
struct dbmdata *dbmp;
932
GetDBM2(obj, dbmp, dbm);
933
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
934
keystr = rb_gdbm_nextkey(dbm, keystr)) {
937
GetDBM2(obj, dbmp, dbm);
944
* gdbm.each_pair { |key, value| block } -> gdbm
946
* Executes _block_ for each key in the database, passing the _key_ and the
947
* correspoding _value_ as a parameter.
954
struct dbmdata *dbmp;
957
GetDBM2(obj, dbmp, dbm);
958
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
959
keystr = rb_gdbm_nextkey(dbm, keystr)) {
961
rb_yield(rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr)));
962
GetDBM2(obj, dbmp, dbm);
972
* Returns an array of all keys of this database.
978
struct dbmdata *dbmp;
982
GetDBM2(obj, dbmp, dbm);
984
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
985
keystr = rb_gdbm_nextkey(dbm, keystr)) {
987
rb_ary_push(ary, keystr);
995
* gdbm.values -> array
997
* Returns an array of all values of this database.
1004
struct dbmdata *dbmp;
1008
GetDBM2(obj, dbmp, dbm);
1010
for (key = gdbm_firstkey(dbm); key.dptr; key = nextkey) {
1011
nextkey = gdbm_nextkey(dbm, key);
1012
valstr = rb_gdbm_fetch(dbm, key);
1014
rb_ary_push(ary, valstr);
1022
* gdbm.has_key?(k) -> true or false
1023
* gdbm.key?(k) -> true or false
1025
* Returns true if the given key _k_ exists within the database.
1026
* Returns false otherwise.
1029
fgdbm_has_key(obj, keystr)
1033
struct dbmdata *dbmp;
1036
StringValue(keystr);
1037
key.dptr = RSTRING(keystr)->ptr;
1038
key.dsize = RSTRING(keystr)->len;
1040
GetDBM2(obj, dbmp, dbm);
1041
if (gdbm_exists(dbm, key))
1048
* gdbm.has_value?(v) -> true or false
1049
* gdbm.value?(v) -> true or false
1051
* Returns true if the given value _v_ exists within the database.
1052
* Returns false otherwise.
1055
fgdbm_has_value(obj, valstr)
1058
struct dbmdata *dbmp;
1060
VALUE keystr, valstr2;
1062
StringValue(valstr);
1063
GetDBM2(obj, dbmp, dbm);
1064
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
1065
keystr = rb_gdbm_nextkey(dbm, keystr)) {
1067
valstr2 = rb_gdbm_fetch2(dbm, keystr);
1069
if (!NIL_P(valstr2) &&
1070
RSTRING(valstr)->len == RSTRING(valstr2)->len &&
1071
memcmp(RSTRING(valstr)->ptr, RSTRING(valstr2)->ptr,
1072
RSTRING(valstr)->len) == 0) {
1081
* gdbm.to_a -> array
1083
* Returns an array of all key-value pairs contained in the database.
1089
struct dbmdata *dbmp;
1093
GetDBM2(obj, dbmp, dbm);
1095
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
1096
keystr = rb_gdbm_nextkey(dbm, keystr)) {
1098
rb_ary_push(ary, rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr)));
1106
* gdbm.reorganize -> gdbm
1108
* Reorganizes the database file. This operation removes reserved space of
1109
* elements that have already been deleted. It is only useful after a lot of
1110
* deletions in the database.
1113
fgdbm_reorganize(obj)
1116
struct dbmdata *dbmp;
1119
rb_gdbm_modify(obj);
1120
GetDBM2(obj, dbmp, dbm);
1121
gdbm_reorganize(dbm);
1129
* Unless the _gdbm_ object has been opened with the *SYNC* flag, it is not
1130
* guarenteed that database modification operations are immediately applied to
1131
* the database file. This method ensures that all recent modifications
1132
* to the database are written to the file. Blocks until all writing operations
1133
* to the disk have been finished.
1139
struct dbmdata *dbmp;
1142
rb_gdbm_modify(obj);
1143
GetDBM2(obj, dbmp, dbm);
1150
* gdbm.cachesize = size -> size
1152
* Sets the size of the internal bucket cache to _size_.
1155
fgdbm_set_cachesize(obj, val)
1158
struct dbmdata *dbmp;
1162
GetDBM2(obj, dbmp, dbm);
1163
optval = FIX2INT(val);
1164
if (gdbm_setopt(dbm, GDBM_CACHESIZE, &optval, sizeof(optval)) == -1) {
1165
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
1172
* gdbm.fastmode = boolean -> boolean
1174
* Turns the database's fast mode on or off. If fast mode is turned on, gdbm
1175
* does not wait for writes to be flushed to the disk before continuing.
1177
* This option is obsolete for gdbm >= 1.8 since fast mode is turned on by
1178
* default. See also: #syncmode=
1181
fgdbm_set_fastmode(obj, val)
1184
struct dbmdata *dbmp;
1188
GetDBM2(obj, dbmp, dbm);
1193
if (gdbm_setopt(dbm, GDBM_FASTMODE, &optval, sizeof(optval)) == -1) {
1194
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
1201
* gdbm.syncmode = boolean -> boolean
1203
* Turns the database's synchronization mode on or off. If the synchronization
1204
* mode is turned on, the database's in-memory state will be synchronized to
1205
* disk after every database modification operation. If the synchronization
1206
* mode is turned off, GDBM does not wait for writes to be flushed to the disk
1207
* before continuing.
1209
* This option is only available for gdbm >= 1.8 where syncmode is turned off
1210
* by default. See also: #fastmode=
1213
fgdbm_set_syncmode(obj, val)
1216
#if !defined(GDBM_SYNCMODE)
1217
fgdbm_set_fastmode(obj, RTEST(val) ? Qfalse : Qtrue);
1220
struct dbmdata *dbmp;
1224
GetDBM2(obj, dbmp, dbm);
1229
if (gdbm_setopt(dbm, GDBM_FASTMODE, &optval, sizeof(optval)) == -1) {
1230
rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
1238
* gdbm.to_hash -> hash
1240
* Returns a hash of all key-value pairs contained in the database.
1246
struct dbmdata *dbmp;
1250
GetDBM2(obj, dbmp, dbm);
1251
hash = rb_hash_new();
1252
for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
1253
keystr = rb_gdbm_nextkey(dbm, keystr)) {
1255
rb_hash_aset(hash, keystr, rb_gdbm_fetch2(dbm, keystr));
1263
* gdbm.reject { |key, value| block } -> hash
1265
* Returns a hash copy of _gdbm_ where all key-value pairs from _gdbm_ for
1266
* which _block_ evaluates to true are removed. See also: #delete_if
1272
return rb_hash_delete_if(fgdbm_to_hash(obj));
1278
rb_cGDBM = rb_define_class("GDBM", rb_cObject);
1279
rb_eGDBMError = rb_define_class("GDBMError", rb_eStandardError);
1280
rb_eGDBMFatalError = rb_define_class("GDBMFatalError", rb_eException);
1281
rb_include_module(rb_cGDBM, rb_mEnumerable);
1283
rb_define_alloc_func(rb_cGDBM, fgdbm_s_alloc);
1284
rb_define_singleton_method(rb_cGDBM, "open", fgdbm_s_open, -1);
1286
rb_define_method(rb_cGDBM, "initialize", fgdbm_initialize, -1);
1287
rb_define_method(rb_cGDBM, "close", fgdbm_close, 0);
1288
rb_define_method(rb_cGDBM, "closed?", fgdbm_closed, 0);
1289
rb_define_method(rb_cGDBM, "[]", fgdbm_aref, 1);
1290
rb_define_method(rb_cGDBM, "fetch", fgdbm_fetch_m, -1);
1291
rb_define_method(rb_cGDBM, "[]=", fgdbm_store, 2);
1292
rb_define_method(rb_cGDBM, "store", fgdbm_store, 2);
1293
rb_define_method(rb_cGDBM, "index", fgdbm_index, 1);
1294
rb_define_method(rb_cGDBM, "indexes", fgdbm_indexes, -1);
1295
rb_define_method(rb_cGDBM, "indices", fgdbm_indexes, -1);
1296
rb_define_method(rb_cGDBM, "select", fgdbm_select, -1);
1297
rb_define_method(rb_cGDBM, "values_at", fgdbm_values_at, -1);
1298
rb_define_method(rb_cGDBM, "length", fgdbm_length, 0);
1299
rb_define_method(rb_cGDBM, "size", fgdbm_length, 0);
1300
rb_define_method(rb_cGDBM, "empty?", fgdbm_empty_p, 0);
1301
rb_define_method(rb_cGDBM, "each", fgdbm_each_pair, 0);
1302
rb_define_method(rb_cGDBM, "each_value", fgdbm_each_value, 0);
1303
rb_define_method(rb_cGDBM, "each_key", fgdbm_each_key, 0);
1304
rb_define_method(rb_cGDBM, "each_pair", fgdbm_each_pair, 0);
1305
rb_define_method(rb_cGDBM, "keys", fgdbm_keys, 0);
1306
rb_define_method(rb_cGDBM, "values", fgdbm_values, 0);
1307
rb_define_method(rb_cGDBM, "shift", fgdbm_shift, 0);
1308
rb_define_method(rb_cGDBM, "delete", fgdbm_delete, 1);
1309
rb_define_method(rb_cGDBM, "delete_if", fgdbm_delete_if, 0);
1310
rb_define_method(rb_cGDBM, "reject!", fgdbm_delete_if, 0);
1311
rb_define_method(rb_cGDBM, "reject", fgdbm_reject, 0);
1312
rb_define_method(rb_cGDBM, "clear", fgdbm_clear, 0);
1313
rb_define_method(rb_cGDBM, "invert", fgdbm_invert, 0);
1314
rb_define_method(rb_cGDBM, "update", fgdbm_update, 1);
1315
rb_define_method(rb_cGDBM, "replace", fgdbm_replace, 1);
1316
rb_define_method(rb_cGDBM, "reorganize", fgdbm_reorganize, 0);
1317
rb_define_method(rb_cGDBM, "sync", fgdbm_sync, 0);
1318
/* rb_define_method(rb_cGDBM, "setopt", fgdbm_setopt, 2); */
1319
rb_define_method(rb_cGDBM, "cachesize=", fgdbm_set_cachesize, 1);
1320
rb_define_method(rb_cGDBM, "fastmode=", fgdbm_set_fastmode, 1);
1321
rb_define_method(rb_cGDBM, "syncmode=", fgdbm_set_syncmode, 1);
1323
rb_define_method(rb_cGDBM, "include?", fgdbm_has_key, 1);
1324
rb_define_method(rb_cGDBM, "has_key?", fgdbm_has_key, 1);
1325
rb_define_method(rb_cGDBM, "member?", fgdbm_has_key, 1);
1326
rb_define_method(rb_cGDBM, "has_value?", fgdbm_has_value, 1);
1327
rb_define_method(rb_cGDBM, "key?", fgdbm_has_key, 1);
1328
rb_define_method(rb_cGDBM, "value?", fgdbm_has_value, 1);
1330
rb_define_method(rb_cGDBM, "to_a", fgdbm_to_a, 0);
1331
rb_define_method(rb_cGDBM, "to_hash", fgdbm_to_hash, 0);
1333
/* flag for #new and #open: open database as a reader */
1334
rb_define_const(rb_cGDBM, "READER", INT2FIX(GDBM_READER|RUBY_GDBM_RW_BIT));
1335
/* flag for #new and #open: open database as a writer */
1336
rb_define_const(rb_cGDBM, "WRITER", INT2FIX(GDBM_WRITER|RUBY_GDBM_RW_BIT));
1337
/* flag for #new and #open: open database as a writer; if the database does not exist, create a new one */
1338
rb_define_const(rb_cGDBM, "WRCREAT", INT2FIX(GDBM_WRCREAT|RUBY_GDBM_RW_BIT));
1339
/* flag for #new and #open: open database as a writer; overwrite any existing databases */
1340
rb_define_const(rb_cGDBM, "NEWDB", INT2FIX(GDBM_NEWDB|RUBY_GDBM_RW_BIT));
1342
/* flag for #new and #open. this flag is obsolete for gdbm >= 1.8 */
1343
rb_define_const(rb_cGDBM, "FAST", INT2FIX(GDBM_FAST));
1344
/* this flag is obsolete in gdbm 1.8.
1345
On gdbm 1.8, fast mode is default behavior. */
1347
/* gdbm version 1.8 specific */
1348
#if defined(GDBM_SYNC)
1349
/* flag for #new and #open. only for gdbm >= 1.8 */
1350
rb_define_const(rb_cGDBM, "SYNC", INT2FIX(GDBM_SYNC));
1352
#if defined(GDBM_NOLOCK)
1353
/* flag for #new and #open */
1354
rb_define_const(rb_cGDBM, "NOLOCK", INT2FIX(GDBM_NOLOCK));
1356
/* version of the gdbm library*/
1357
rb_define_const(rb_cGDBM, "VERSION", rb_str_new2(gdbm_version));