Class: GDBM

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
gdbm.c,
gdbm.c

Overview

Summary

Ruby extension for GNU dbm (gdbm) – a simple database engine for storing key-value pairs on disk.

Description

GNU dbm is a library for simple databases. A database is a file that stores key-value pairs. Gdbm allows the user to store, retrieve, and delete data by key. It furthermore allows a non-sorted traversal of all key-value pairs. A gdbm database thus provides the same functionality as a hash. As with objects of the Hash class, elements can be accessed with []. Furthermore, GDBM mixes in the Enumerable module, thus providing convenient methods such as #find, #collect, #map, etc.

A process is allowed to open several different databases at the same time. A process can open a database as a “reader” or a “writer”. Whereas a reader has only read-access to the database, a writer has read- and write-access. A database can be accessed either by any number of readers or by exactly one writer at the same time.

Examples

  1. Opening/creating a database, and filling it with some entries:

    require 'gdbm'
    
    gdbm = GDBM.new("fruitstore.db")
    gdbm["ananas"]    = "3"
    gdbm["banana"]    = "8"
    gdbm["cranberry"] = "4909"
    gdbm.close
    
  2. Reading out a database:

    require 'gdbm'
    
    gdbm = GDBM.new("fruitstore.db")
    gdbm.each_pair do |key, value|
      print "#{key}: #{value}\n"
    end
    gdbm.close
    

    produces

    banana: 8
    ananas: 3
    cranberry: 4909
    

Constant Summary collapse

READER =

open database as a reader

flag for #new and #open
WRITER =

open database as a writer

flag for #new and #open
WRCREAT =

open database as a writer; if the database does not exist, create a new one

flag for #new and #open
NEWDB =

open database as a writer; overwrite any existing databases

flag for #new and #open
FAST =

flag for #new and #open. this flag is obsolete for gdbm >= 1.8

INT2FIX(GDBM_FAST)
SYNC =

flag for #new and #open. only for gdbm >= 1.8

INT2FIX(GDBM_SYNC)
NOLOCK =

flag for #new and #open

INT2FIX(GDBM_NOLOCK)
VERSION =

version of the gdbm library

rb_str_new2(gdbm_version)

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#new(filename, mode = 0666, flags = nil) ⇒ Object

Creates a new GDBM instance by opening a gdbm file named filename. If the file does not exist, a new file with file mode mode will be created. flags may be one of the following:

  • READER - open as a reader

  • WRITER - open as a writer

  • WRCREAT - open as a writer; if the database does not exist, create a new one

  • NEWDB - open as a writer; overwrite any existing databases

The values WRITER, WRCREAT and NEWDB may be combined with the following values by bitwise or:

  • SYNC - cause all database operations to be synchronized to the disk

  • NOLOCK - do not lock the database file

If no flags are specified, the GDBM object will try to open the database file as a writer and will create it if it does not already exist (cf. flag WRCREAT). If this fails (for instance, if another process has already opened the database as a reader), it will try to open the database file as a reader (cf. flag READER).


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
# File 'gdbm.c', line 210

static VALUE
fgdbm_initialize(int argc, VALUE *argv, VALUE obj)
{
    VALUE file, vmode, vflags;
    GDBM_FILE dbm;
    struct dbmdata *dbmp;
    int mode, flags = 0;

    TypedData_Get_Struct(obj, struct dbmdata, &dbm_type, dbmp);
    if (rb_scan_args(argc, argv, "12", &file, &vmode, &vflags) == 1) {
        mode = 0666;            /* default value */
    }
    else if (NIL_P(vmode)) {
        mode = -1;              /* return nil if DB does not exist */
    }
    else {
        mode = NUM2INT(vmode);
    }

    if (!NIL_P(vflags))
        flags = NUM2INT(vflags);

    FilePathValue(file);

#ifdef GDBM_CLOEXEC
    /* GDBM_CLOEXEC is available since gdbm 1.10. */
    flags |= GDBM_CLOEXEC;
#endif

    if (flags & RUBY_GDBM_RW_BIT) {
        flags &= ~RUBY_GDBM_RW_BIT;
        dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE,
                        flags, mode, MY_FATAL_FUNC);
    }
    else {
        dbm = 0;
        if (mode >= 0)
            dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE,
                            GDBM_WRCREAT|flags, mode, MY_FATAL_FUNC);
        if (!dbm)
            dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE,
                            GDBM_WRITER|flags, 0, MY_FATAL_FUNC);
        if (!dbm)
            dbm = gdbm_open(RSTRING_PTR(file), MY_BLOCK_SIZE,
                            GDBM_READER|flags, 0, MY_FATAL_FUNC);
    }

    if (dbm) {
        rb_fd_fix_cloexec(gdbm_fdesc(dbm));
    }

    if (!dbm) {
        if (mode == -1) return Qnil;

        if (gdbm_errno == GDBM_FILE_OPEN_ERROR ||
            gdbm_errno == GDBM_CANT_BE_READER ||
            gdbm_errno == GDBM_CANT_BE_WRITER)
            rb_sys_fail_str(file);
        else
            rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
    }

    if (dbmp->di_dbm)
	gdbm_close(dbmp->di_dbm);
    dbmp->di_dbm = dbm;
    dbmp->di_size = -1;

    return obj;
}

Class Method Details

.open(filename, mode = 0666, flags = nil) ⇒ Object .open(filename, mode = 0666, flags = nil) {|gdbm| ... } ⇒ Object

If called without a block, this is synonymous to GDBM::new. If a block is given, the new GDBM instance will be passed to the block as a parameter, and the corresponding database file will be closed after the execution of the block code has been finished.

Example for an open call with a block:

require 'gdbm'
GDBM.open("fruitstore.db") do |gdbm|
  gdbm.each_pair do |key, value|
    print "#{key}: #{value}\n"
  end
end

Overloads:

  • .open(filename, mode = 0666, flags = nil) {|gdbm| ... } ⇒ Object

    Yields:

    • (gdbm)

299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'gdbm.c', line 299

static VALUE
fgdbm_s_open(int argc, VALUE *argv, VALUE klass)
{
    VALUE obj = fgdbm_s_alloc(klass);

    if (NIL_P(fgdbm_initialize(argc, argv, obj))) {
        return Qnil;
    }

    if (rb_block_given_p()) {
        return rb_ensure(rb_yield, obj, fgdbm_close, obj);
    }

    return obj;
}

Instance Method Details

#[](key) ⇒ Object

Retrieves the value corresponding to key.


410
411
412
413
414
# File 'gdbm.c', line 410

static VALUE
fgdbm_aref(VALUE obj, VALUE keystr)
{
    return rb_gdbm_fetch3(obj, keystr);
}

#[]=(key) ⇒ Object #store(key, value) ⇒ Object

Associates the value value with the specified key.


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
# File 'gdbm.c', line 717

static VALUE
fgdbm_store(VALUE obj, VALUE keystr, VALUE valstr)
{
    datum key, val;
    struct dbmdata *dbmp;
    GDBM_FILE dbm;

    rb_gdbm_modify(obj);
    ExportStringValue(keystr);
    ExportStringValue(valstr);

    key.dptr = RSTRING_PTR(keystr);
    key.dsize = RSTRING_LENINT(keystr);

    val.dptr = RSTRING_PTR(valstr);
    val.dsize = RSTRING_LENINT(valstr);

    GetDBM2(obj, dbmp, dbm);
    dbmp->di_size = -1;
    if (gdbm_store(dbm, key, val, GDBM_REPLACE)) {
        if (errno == EPERM) rb_sys_fail(0);
        rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
    }

    return valstr;
}

#cachesize=(size) ⇒ Object

Sets the size of the internal bucket cache to size.


1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
# File 'gdbm.c', line 1108

static VALUE
fgdbm_set_cachesize(VALUE obj, VALUE val)
{
    struct dbmdata *dbmp;
    GDBM_FILE dbm;
    int optval;

    GetDBM2(obj, dbmp, dbm);
    optval = FIX2INT(val);
    if (gdbm_setopt(dbm, GDBM_CACHESIZE, &optval, sizeof(optval)) == -1) {
        rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
    }
    return val;
}

#clearObject

Removes all the key-value pairs within gdbm.


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
# File 'gdbm.c', line 648

static VALUE
fgdbm_clear(VALUE obj)
{
    datum key, nextkey;
    struct dbmdata *dbmp;
    GDBM_FILE dbm;

    rb_gdbm_modify(obj);
    GetDBM2(obj, dbmp, dbm);
    dbmp->di_size = -1;

#if 0
    while (key = gdbm_firstkey(dbm), key.dptr) {
        if (gdbm_delete(dbm, key)) {
            free(key.dptr);
            rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
        }
        free(key.dptr);
    }
#else
    while (key = gdbm_firstkey(dbm), key.dptr) {
        for (; key.dptr; key = nextkey) {
            nextkey = gdbm_nextkey(dbm, key);
            if (gdbm_delete(dbm, key)) {
                free(key.dptr);
                if (nextkey.dptr) free(nextkey.dptr);
                rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
            }
            free(key.dptr);
        }
    }
#endif
    dbmp->di_size = 0;

    return obj;
}

#closenil

Closes the associated database file.

Returns:

  • (nil)

149
150
151
152
153
154
155
156
157
158
159
# File 'gdbm.c', line 149

static VALUE
fgdbm_close(VALUE obj)
{
    struct dbmdata *dbmp;

    GetDBM(obj, dbmp);
    gdbm_close(dbmp->di_dbm);
    dbmp->di_dbm = 0;

    return Qnil;
}

#closed?Boolean

Returns true if the associated database file has been closed.

Returns:

  • (Boolean)

167
168
169
170
171
172
173
174
175
176
177
# File 'gdbm.c', line 167

static VALUE
fgdbm_closed(VALUE obj)
{
    struct dbmdata *dbmp;

    TypedData_Get_Struct(obj, struct dbmdata, &dbm_type, dbmp);
    if (dbmp->di_dbm == 0)
        return Qtrue;

    return Qfalse;
}

#delete(key) ⇒ nil

Removes the key-value-pair with the specified key from this database and returns the corresponding value. Returns nil if the database is empty.

Returns:

  • (nil)

566
567
568
569
570
571
572
573
574
# File 'gdbm.c', line 566

static VALUE
fgdbm_delete(VALUE obj, VALUE keystr)
{
    VALUE valstr;

    valstr = fgdbm_fetch(obj, keystr, Qnil);
    rb_gdbm_delete(obj, keystr);
    return valstr;
}

#delete_if {|key, value| ... } ⇒ Object #reject! {|key, value| ... } ⇒ Object

Deletes every key-value pair from gdbm for which block evaluates to true.

Overloads:

  • #delete_if {|key, value| ... } ⇒ Object

    Yields:

  • #reject! {|key, value| ... } ⇒ Object

    Yields:


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
# File 'gdbm.c', line 607

static VALUE
fgdbm_delete_if(VALUE obj)
{
    struct dbmdata *dbmp;
    GDBM_FILE dbm;
    VALUE keystr, valstr;
    VALUE ret, ary = rb_ary_tmp_new(0);
    long i;
    int status = 0, n;

    rb_gdbm_modify(obj);
    GetDBM2(obj, dbmp, dbm);
    n = dbmp->di_size;
    dbmp->di_size = -1;

    for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
         keystr = rb_gdbm_nextkey(dbm, keystr)) {

	OBJ_FREEZE(keystr);
        valstr = rb_gdbm_fetch2(dbm, keystr);
        ret = rb_protect(rb_yield, rb_assoc_new(rb_str_dup(keystr), valstr), &status);
        if (status != 0) break;
        if (RTEST(ret)) rb_ary_push(ary, keystr);
        GetDBM2(obj, dbmp, dbm);
    }

    for (i = 0; i < RARRAY_LEN(ary); i++)
        rb_gdbm_delete(obj, RARRAY_AREF(ary, i));
    if (status) rb_jump_tag(status);
    if (n > 0) dbmp->di_size = n - (int)RARRAY_LEN(ary);
    rb_ary_clear(ary);

    return obj;
}

#each_pair {|key, value| ... } ⇒ Object

Executes block for each key in the database, passing the key and the corresponding value as a parameter.

Yields:


903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
# File 'gdbm.c', line 903

static VALUE
fgdbm_each_pair(VALUE obj)
{
    GDBM_FILE dbm;
    struct dbmdata *dbmp;
    VALUE keystr;

    RETURN_ENUMERATOR(obj, 0, 0);

    GetDBM2(obj, dbmp, dbm);
    for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
         keystr = rb_gdbm_nextkey(dbm, keystr)) {

        rb_yield(rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr)));
        GetDBM2(obj, dbmp, dbm);
    }

    return obj;
}

#each_key {|key| ... } ⇒ Object

Executes block for each key in the database, passing the key as a parameter.

Yields:


877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
# File 'gdbm.c', line 877

static VALUE
fgdbm_each_key(VALUE obj)
{
    struct dbmdata *dbmp;
    GDBM_FILE dbm;
    VALUE keystr;

    RETURN_ENUMERATOR(obj, 0, 0);

    GetDBM2(obj, dbmp, dbm);
    for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
         keystr = rb_gdbm_nextkey(dbm, keystr)) {

        rb_yield(keystr);
        GetDBM2(obj, dbmp, dbm);
    }
    return obj;
}

#each_pair {|key, value| ... } ⇒ Object

Executes block for each key in the database, passing the key and the corresponding value as a parameter.

Yields:


903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
# File 'gdbm.c', line 903

static VALUE
fgdbm_each_pair(VALUE obj)
{
    GDBM_FILE dbm;
    struct dbmdata *dbmp;
    VALUE keystr;

    RETURN_ENUMERATOR(obj, 0, 0);

    GetDBM2(obj, dbmp, dbm);
    for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
         keystr = rb_gdbm_nextkey(dbm, keystr)) {

        rb_yield(rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr)));
        GetDBM2(obj, dbmp, dbm);
    }

    return obj;
}

#each_value {|value| ... } ⇒ Object

Executes block for each key in the database, passing the corresponding value as a parameter.

Yields:

  • (value)

851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
# File 'gdbm.c', line 851

static VALUE
fgdbm_each_value(VALUE obj)
{
    struct dbmdata *dbmp;
    GDBM_FILE dbm;
    VALUE keystr;

    RETURN_ENUMERATOR(obj, 0, 0);

    GetDBM2(obj, dbmp, dbm);
    for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
         keystr = rb_gdbm_nextkey(dbm, keystr)) {

        rb_yield(rb_gdbm_fetch2(dbm, keystr));
        GetDBM2(obj, dbmp, dbm);
    }
    return obj;
}

#empty?Boolean

Returns true if the database is empty.

Returns:

  • (Boolean)

821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
# File 'gdbm.c', line 821

static VALUE
fgdbm_empty_p(VALUE obj)
{
    datum key;
    struct dbmdata *dbmp;
    GDBM_FILE dbm;

    GetDBM(obj, dbmp);
    if (dbmp->di_size < 0) {
        dbm = dbmp->di_dbm;

        key = gdbm_firstkey(dbm);
        if (key.dptr) {
            free(key.dptr);
            return Qfalse;
        }
        return Qtrue;
    }

    if (dbmp->di_size == 0) return Qtrue;
    return Qfalse;
}

#fastmode=(boolean) ⇒ Boolean

Turns the database's fast mode on or off. If fast mode is turned on, gdbm does not wait for writes to be flushed to the disk before continuing.

This option is obsolete for gdbm >= 1.8 since fast mode is turned on by default. See also: #syncmode=

Returns:

  • (Boolean)

1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
# File 'gdbm.c', line 1133

static VALUE
fgdbm_set_fastmode(VALUE obj, VALUE val)
{
    struct dbmdata *dbmp;
    GDBM_FILE dbm;
    int optval;

    GetDBM2(obj, dbmp, dbm);
    optval = 0;
    if (RTEST(val))
        optval = 1;

    if (gdbm_setopt(dbm, GDBM_FASTMODE, &optval, sizeof(optval)) == -1) {
        rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
    }
    return val;
}

#fetch(key[, default]) ⇒ Object

Retrieves the value corresponding to key. If there is no value associated with key, default will be returned instead.


423
424
425
426
427
428
429
430
431
432
433
434
# File 'gdbm.c', line 423

static VALUE
fgdbm_fetch_m(int argc, VALUE *argv, VALUE obj)
{
    VALUE keystr, valstr, ifnone;

    rb_scan_args(argc, argv, "11", &keystr, &ifnone);
    valstr = fgdbm_fetch(obj, keystr, ifnone);
    if (argc == 1 && !rb_block_given_p() && NIL_P(valstr))
        rb_raise(rb_eIndexError, "key not found");

    return valstr;
}

#include?(k) ⇒ Boolean #has_key?(k) ⇒ Boolean #member?(k) ⇒ Boolean #key?(k) ⇒ Boolean

Returns true if the given key k exists within the database. Returns false otherwise.

Overloads:

  • #include?(k) ⇒ Boolean

    Returns:

    • (Boolean)
  • #has_key?(k) ⇒ Boolean

    Returns:

    • (Boolean)
  • #member?(k) ⇒ Boolean

    Returns:

    • (Boolean)
  • #key?(k) ⇒ Boolean

    Returns:

    • (Boolean)

983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
# File 'gdbm.c', line 983

static VALUE
fgdbm_has_key(VALUE obj, VALUE keystr)
{
    datum key;
    struct dbmdata *dbmp;
    GDBM_FILE dbm;
    long len;

    ExportStringValue(keystr);
    len = RSTRING_LENINT(keystr);
    if (TOO_LONG(len)) return Qfalse;
    key.dptr = RSTRING_PTR(keystr);
    key.dsize = (int)len;

    GetDBM2(obj, dbmp, dbm);
    if (gdbm_exists(dbm, key))
        return Qtrue;
    return Qfalse;
}

#has_value?(v) ⇒ Boolean #value?(v) ⇒ Boolean

Returns true if the given value v exists within the database. Returns false otherwise.

Overloads:

  • #has_value?(v) ⇒ Boolean

    Returns:

    • (Boolean)
  • #value?(v) ⇒ Boolean

    Returns:

    • (Boolean)

1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
# File 'gdbm.c', line 1011

static VALUE
fgdbm_has_value(VALUE obj, VALUE valstr)
{
    struct dbmdata *dbmp;
    GDBM_FILE dbm;
    VALUE keystr, valstr2;

    ExportStringValue(valstr);
    GetDBM2(obj, dbmp, dbm);
    for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
         keystr = rb_gdbm_nextkey(dbm, keystr)) {

        valstr2 = rb_gdbm_fetch2(dbm, keystr);

        if (!NIL_P(valstr2) &&
            (int)RSTRING_LEN(valstr) == (int)RSTRING_LEN(valstr2) &&
            memcmp(RSTRING_PTR(valstr), RSTRING_PTR(valstr2),
                   (int)RSTRING_LEN(valstr)) == 0) {
            return Qtrue;
        }
    }
    return Qfalse;
}

#include?(k) ⇒ Boolean #has_key?(k) ⇒ Boolean #member?(k) ⇒ Boolean #key?(k) ⇒ Boolean

Returns true if the given key k exists within the database. Returns false otherwise.

Overloads:

  • #include?(k) ⇒ Boolean

    Returns:

    • (Boolean)
  • #has_key?(k) ⇒ Boolean

    Returns:

    • (Boolean)
  • #member?(k) ⇒ Boolean

    Returns:

    • (Boolean)
  • #key?(k) ⇒ Boolean

    Returns:

    • (Boolean)

983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
# File 'gdbm.c', line 983

static VALUE
fgdbm_has_key(VALUE obj, VALUE keystr)
{
    datum key;
    struct dbmdata *dbmp;
    GDBM_FILE dbm;
    long len;

    ExportStringValue(keystr);
    len = RSTRING_LENINT(keystr);
    if (TOO_LONG(len)) return Qfalse;
    key.dptr = RSTRING_PTR(keystr);
    key.dsize = (int)len;

    GetDBM2(obj, dbmp, dbm);
    if (gdbm_exists(dbm, key))
        return Qtrue;
    return Qfalse;
}

#index(value) ⇒ Object

:nodoc:


467
468
469
470
471
472
# File 'gdbm.c', line 467

static VALUE
fgdbm_index(VALUE obj, VALUE value)
{
    rb_warn("GDBM#index is deprecated; use GDBM#key");
    return fgdbm_key(obj, value);
}

#invertHash

Returns a hash created by using gdbm's values as keys, and the keys as values.

Returns:

  • (Hash)

692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
# File 'gdbm.c', line 692

static VALUE
fgdbm_invert(VALUE obj)
{
    struct dbmdata *dbmp;
    GDBM_FILE dbm;
    VALUE keystr, valstr;
    VALUE hash = rb_hash_new();

    GetDBM2(obj, dbmp, dbm);
    for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
         keystr = rb_gdbm_nextkey(dbm, keystr)) {
        valstr = rb_gdbm_fetch2(dbm, keystr);

        rb_hash_aset(hash, valstr, keystr);
    }
    return hash;
}

#key(value) ⇒ Object

Returns the key for a given value. If several keys may map to the same value, the key that is found first will be returned.


443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'gdbm.c', line 443

static VALUE
fgdbm_key(VALUE obj, VALUE valstr)
{
    struct dbmdata *dbmp;
    GDBM_FILE dbm;
    VALUE keystr, valstr2;

    ExportStringValue(valstr);
    GetDBM2(obj, dbmp, dbm);
    for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
         keystr = rb_gdbm_nextkey(dbm, keystr)) {

        valstr2 = rb_gdbm_fetch2(dbm, keystr);
        if (!NIL_P(valstr2) &&
            (int)RSTRING_LEN(valstr) == (int)RSTRING_LEN(valstr2) &&
            memcmp(RSTRING_PTR(valstr), RSTRING_PTR(valstr2),
                   (int)RSTRING_LEN(valstr)) == 0) {
            return keystr;
        }
    }
    return Qnil;
}

#include?(k) ⇒ Boolean #has_key?(k) ⇒ Boolean #member?(k) ⇒ Boolean #key?(k) ⇒ Boolean

Returns true if the given key k exists within the database. Returns false otherwise.

Overloads:

  • #include?(k) ⇒ Boolean

    Returns:

    • (Boolean)
  • #has_key?(k) ⇒ Boolean

    Returns:

    • (Boolean)
  • #member?(k) ⇒ Boolean

    Returns:

    • (Boolean)
  • #key?(k) ⇒ Boolean

    Returns:

    • (Boolean)

983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
# File 'gdbm.c', line 983

static VALUE
fgdbm_has_key(VALUE obj, VALUE keystr)
{
    datum key;
    struct dbmdata *dbmp;
    GDBM_FILE dbm;
    long len;

    ExportStringValue(keystr);
    len = RSTRING_LENINT(keystr);
    if (TOO_LONG(len)) return Qfalse;
    key.dptr = RSTRING_PTR(keystr);
    key.dsize = (int)len;

    GetDBM2(obj, dbmp, dbm);
    if (gdbm_exists(dbm, key))
        return Qtrue;
    return Qfalse;
}

#keysArray

Returns an array of all keys of this database.

Returns:

  • (Array)

929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
# File 'gdbm.c', line 929

static VALUE
fgdbm_keys(VALUE obj)
{
    struct dbmdata *dbmp;
    GDBM_FILE dbm;
    VALUE keystr, ary;

    GetDBM2(obj, dbmp, dbm);
    ary = rb_ary_new();
    for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
         keystr = rb_gdbm_nextkey(dbm, keystr)) {

        rb_ary_push(ary, keystr);
    }

    return ary;
}

#lengthFixnum #sizeFixnum

Returns the number of key-value pairs in this database.

Overloads:

  • #lengthFixnum

    Returns:

    • (Fixnum)
  • #sizeFixnum

    Returns:

    • (Fixnum)

794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
# File 'gdbm.c', line 794

static VALUE
fgdbm_length(VALUE obj)
{
    datum key, nextkey;
    struct dbmdata *dbmp;
    GDBM_FILE dbm;
    int i = 0;

    GetDBM2(obj, dbmp, dbm);
    if (dbmp->di_size > 0) return INT2FIX(dbmp->di_size);

    for (key = gdbm_firstkey(dbm); key.dptr; key = nextkey) {
        nextkey = gdbm_nextkey(dbm, key);
        free(key.dptr);
        i++;
    }
    dbmp->di_size = i;

    return INT2FIX(i);
}

#include?(k) ⇒ Boolean #has_key?(k) ⇒ Boolean #member?(k) ⇒ Boolean #key?(k) ⇒ Boolean

Returns true if the given key k exists within the database. Returns false otherwise.

Overloads:

  • #include?(k) ⇒ Boolean

    Returns:

    • (Boolean)
  • #has_key?(k) ⇒ Boolean

    Returns:

    • (Boolean)
  • #member?(k) ⇒ Boolean

    Returns:

    • (Boolean)
  • #key?(k) ⇒ Boolean

    Returns:

    • (Boolean)

983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
# File 'gdbm.c', line 983

static VALUE
fgdbm_has_key(VALUE obj, VALUE keystr)
{
    datum key;
    struct dbmdata *dbmp;
    GDBM_FILE dbm;
    long len;

    ExportStringValue(keystr);
    len = RSTRING_LENINT(keystr);
    if (TOO_LONG(len)) return Qfalse;
    key.dptr = RSTRING_PTR(keystr);
    key.dsize = (int)len;

    GetDBM2(obj, dbmp, dbm);
    if (gdbm_exists(dbm, key))
        return Qtrue;
    return Qfalse;
}

#reject {|key, value| ... } ⇒ Hash

Returns a hash copy of gdbm where all key-value pairs from gdbm for which block evaluates to true are removed. See also: #delete_if

Yields:

Returns:

  • (Hash)

1218
1219
1220
1221
1222
# File 'gdbm.c', line 1218

static VALUE
fgdbm_reject(VALUE obj)
{
    return rb_hash_delete_if(fgdbm_to_hash(obj));
}

#delete_if {|key, value| ... } ⇒ Object #reject! {|key, value| ... } ⇒ Object

Deletes every key-value pair from gdbm for which block evaluates to true.

Overloads:

  • #delete_if {|key, value| ... } ⇒ Object

    Yields:

  • #reject! {|key, value| ... } ⇒ Object

    Yields:


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
# File 'gdbm.c', line 607

static VALUE
fgdbm_delete_if(VALUE obj)
{
    struct dbmdata *dbmp;
    GDBM_FILE dbm;
    VALUE keystr, valstr;
    VALUE ret, ary = rb_ary_tmp_new(0);
    long i;
    int status = 0, n;

    rb_gdbm_modify(obj);
    GetDBM2(obj, dbmp, dbm);
    n = dbmp->di_size;
    dbmp->di_size = -1;

    for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
         keystr = rb_gdbm_nextkey(dbm, keystr)) {

	OBJ_FREEZE(keystr);
        valstr = rb_gdbm_fetch2(dbm, keystr);
        ret = rb_protect(rb_yield, rb_assoc_new(rb_str_dup(keystr), valstr), &status);
        if (status != 0) break;
        if (RTEST(ret)) rb_ary_push(ary, keystr);
        GetDBM2(obj, dbmp, dbm);
    }

    for (i = 0; i < RARRAY_LEN(ary); i++)
        rb_gdbm_delete(obj, RARRAY_AREF(ary, i));
    if (status) rb_jump_tag(status);
    if (n > 0) dbmp->di_size = n - (int)RARRAY_LEN(ary);
    rb_ary_clear(ary);

    return obj;
}

#reorganizeObject

Reorganizes the database file. This operation removes reserved space of elements that have already been deleted. It is only useful after a lot of deletions in the database.


1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
# File 'gdbm.c', line 1067

static VALUE
fgdbm_reorganize(VALUE obj)
{
    struct dbmdata *dbmp;
    GDBM_FILE dbm;

    rb_gdbm_modify(obj);
    GetDBM2(obj, dbmp, dbm);
    gdbm_reorganize(dbm);
    rb_fd_fix_cloexec(gdbm_fdesc(dbm));
    return obj;
}

#replace(other) ⇒ Object

Replaces the content of gdbm with the key-value pairs of other. other must have an each_pair method.


779
780
781
782
783
784
785
# File 'gdbm.c', line 779

static VALUE
fgdbm_replace(VALUE obj, VALUE other)
{
    fgdbm_clear(obj);
    rb_block_call(other, rb_intern("each_pair"), 0, 0, update_i, obj);
    return obj;
}

#select {|key, value| ... } ⇒ Array

Returns a new array of all key-value pairs of the database for which block evaluates to true.

Yields:

Returns:

  • (Array)

481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
# File 'gdbm.c', line 481

static VALUE
fgdbm_select(VALUE obj)
{
    VALUE new = rb_ary_new();
    GDBM_FILE dbm;
    struct dbmdata *dbmp;
    VALUE keystr;

    GetDBM2(obj, dbmp, dbm);
    for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
         keystr = rb_gdbm_nextkey(dbm, keystr)) {
        VALUE assoc = rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr));
        VALUE v = rb_yield(assoc);

        if (RTEST(v)) {
            rb_ary_push(new, assoc);
        }
        GetDBM2(obj, dbmp, dbm);
    }

    return new;
}

#shiftnil

Removes a key-value-pair from this database and returns it as a two-item array [ key, value ]. Returns nil if the database is empty.

Returns:

  • (nil)

583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
# File 'gdbm.c', line 583

static VALUE
fgdbm_shift(VALUE obj)
{
    struct dbmdata *dbmp;
    GDBM_FILE dbm;
    VALUE keystr, valstr;

    rb_gdbm_modify(obj);
    GetDBM2(obj, dbmp, dbm);
    keystr = rb_gdbm_firstkey(dbm);
    if (NIL_P(keystr)) return Qnil;
    valstr = rb_gdbm_fetch2(dbm, keystr);
    rb_gdbm_delete(obj, keystr);

    return rb_assoc_new(keystr, valstr);
}

#lengthFixnum #sizeFixnum

Returns the number of key-value pairs in this database.

Overloads:

  • #lengthFixnum

    Returns:

    • (Fixnum)
  • #sizeFixnum

    Returns:

    • (Fixnum)

794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
# File 'gdbm.c', line 794

static VALUE
fgdbm_length(VALUE obj)
{
    datum key, nextkey;
    struct dbmdata *dbmp;
    GDBM_FILE dbm;
    int i = 0;

    GetDBM2(obj, dbmp, dbm);
    if (dbmp->di_size > 0) return INT2FIX(dbmp->di_size);

    for (key = gdbm_firstkey(dbm); key.dptr; key = nextkey) {
        nextkey = gdbm_nextkey(dbm, key);
        free(key.dptr);
        i++;
    }
    dbmp->di_size = i;

    return INT2FIX(i);
}

#[]=(key) ⇒ Object #store(key, value) ⇒ Object

Associates the value value with the specified key.


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
# File 'gdbm.c', line 717

static VALUE
fgdbm_store(VALUE obj, VALUE keystr, VALUE valstr)
{
    datum key, val;
    struct dbmdata *dbmp;
    GDBM_FILE dbm;

    rb_gdbm_modify(obj);
    ExportStringValue(keystr);
    ExportStringValue(valstr);

    key.dptr = RSTRING_PTR(keystr);
    key.dsize = RSTRING_LENINT(keystr);

    val.dptr = RSTRING_PTR(valstr);
    val.dsize = RSTRING_LENINT(valstr);

    GetDBM2(obj, dbmp, dbm);
    dbmp->di_size = -1;
    if (gdbm_store(dbm, key, val, GDBM_REPLACE)) {
        if (errno == EPERM) rb_sys_fail(0);
        rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
    }

    return valstr;
}

#syncObject

Unless the gdbm object has been opened with the SYNC flag, it is not guaranteed that database modification operations are immediately applied to the database file. This method ensures that all recent modifications to the database are written to the file. Blocks until all writing operations to the disk have been finished.


1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
# File 'gdbm.c', line 1090

static VALUE
fgdbm_sync(VALUE obj)
{
    struct dbmdata *dbmp;
    GDBM_FILE dbm;

    rb_gdbm_modify(obj);
    GetDBM2(obj, dbmp, dbm);
    gdbm_sync(dbm);
    return obj;
}

#syncmode=(boolean) ⇒ Boolean

Turns the database's synchronization mode on or off. If the synchronization mode is turned on, the database's in-memory state will be synchronized to disk after every database modification operation. If the synchronization mode is turned off, GDBM does not wait for writes to be flushed to the disk before continuing.

This option is only available for gdbm >= 1.8 where syncmode is turned off by default. See also: #fastmode=

Returns:

  • (Boolean)

1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
# File 'gdbm.c', line 1164

static VALUE
fgdbm_set_syncmode(VALUE obj, VALUE val)
{
#if !defined(GDBM_SYNCMODE)
    fgdbm_set_fastmode(obj, RTEST(val) ? Qfalse : Qtrue);
    return val;
#else
    struct dbmdata *dbmp;
    GDBM_FILE dbm;
    int optval;

    GetDBM2(obj, dbmp, dbm);
    optval = 0;
    if (RTEST(val))
        optval = 1;

    if (gdbm_setopt(dbm, GDBM_FASTMODE, &optval, sizeof(optval)) == -1) {
        rb_raise(rb_eGDBMError, "%s", gdbm_strerror(gdbm_errno));
    }
    return val;
#endif
}

#to_aArray

Returns an array of all key-value pairs contained in the database.

Returns:

  • (Array)

1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
# File 'gdbm.c', line 1041

static VALUE
fgdbm_to_a(VALUE obj)
{
    struct dbmdata *dbmp;
    GDBM_FILE dbm;
    VALUE keystr, ary;

    GetDBM2(obj, dbmp, dbm);
    ary = rb_ary_new();
    for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
         keystr = rb_gdbm_nextkey(dbm, keystr)) {

        rb_ary_push(ary, rb_assoc_new(keystr, rb_gdbm_fetch2(dbm, keystr)));
    }

    return ary;
}

#to_hashHash

Returns a hash of all key-value pairs contained in the database.

Returns:

  • (Hash)

1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
# File 'gdbm.c', line 1193

static VALUE
fgdbm_to_hash(VALUE obj)
{
    struct dbmdata *dbmp;
    GDBM_FILE dbm;
    VALUE keystr, hash;

    GetDBM2(obj, dbmp, dbm);
    hash = rb_hash_new();
    for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
         keystr = rb_gdbm_nextkey(dbm, keystr)) {

        rb_hash_aset(hash, keystr, rb_gdbm_fetch2(dbm, keystr));
    }

    return hash;
}

#update(other) ⇒ Object

Adds the key-value pairs of other to gdbm, overwriting entries with duplicate keys with those from other. other must have an each_pair method.


765
766
767
768
769
770
# File 'gdbm.c', line 765

static VALUE
fgdbm_update(VALUE obj, VALUE other)
{
    rb_block_call(other, rb_intern("each_pair"), 0, 0, update_i, obj);
    return obj;
}

#has_value?(v) ⇒ Boolean #value?(v) ⇒ Boolean

Returns true if the given value v exists within the database. Returns false otherwise.

Overloads:

  • #has_value?(v) ⇒ Boolean

    Returns:

    • (Boolean)
  • #value?(v) ⇒ Boolean

    Returns:

    • (Boolean)

1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
# File 'gdbm.c', line 1011

static VALUE
fgdbm_has_value(VALUE obj, VALUE valstr)
{
    struct dbmdata *dbmp;
    GDBM_FILE dbm;
    VALUE keystr, valstr2;

    ExportStringValue(valstr);
    GetDBM2(obj, dbmp, dbm);
    for (keystr = rb_gdbm_firstkey(dbm); RTEST(keystr);
         keystr = rb_gdbm_nextkey(dbm, keystr)) {

        valstr2 = rb_gdbm_fetch2(dbm, keystr);

        if (!NIL_P(valstr2) &&
            (int)RSTRING_LEN(valstr) == (int)RSTRING_LEN(valstr2) &&
            memcmp(RSTRING_PTR(valstr), RSTRING_PTR(valstr2),
                   (int)RSTRING_LEN(valstr)) == 0) {
            return Qtrue;
        }
    }
    return Qfalse;
}

#valuesArray

Returns an array of all values of this database.

Returns:

  • (Array)

953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
# File 'gdbm.c', line 953

static VALUE
fgdbm_values(VALUE obj)
{
    datum key, nextkey;
    struct dbmdata *dbmp;
    GDBM_FILE dbm;
    VALUE valstr, ary;

    GetDBM2(obj, dbmp, dbm);
    ary = rb_ary_new();
    for (key = gdbm_firstkey(dbm); key.dptr; key = nextkey) {
        nextkey = gdbm_nextkey(dbm, key);
        valstr = rb_gdbm_fetch(dbm, key);
        free(key.dptr);
        rb_ary_push(ary, valstr);
    }

    return ary;
}

#values_at(key, ...) ⇒ Array

Returns an array of the values associated with each specified key.

Returns:

  • (Array)

510
511
512
513
514
515
516
517
518
519
520
521
# File 'gdbm.c', line 510

static VALUE
fgdbm_values_at(int argc, VALUE *argv, VALUE obj)
{
    VALUE new = rb_ary_new2(argc);
    int i;

    for (i=0; i<argc; i++) {
        rb_ary_push(new, rb_gdbm_fetch3(obj, argv[i]));
    }

    return new;
}