Class

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
Constants

open database as a reader

open database as a writer

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

open database as a writer; overwrite any existing databases

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

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

flag for new and open

version of the gdbm library

Class Methods

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).

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
Instance Methods

Retrieves the value corresponding to key.

Associates the value value with the specified key.

Sets the size of the internal bucket cache to size.

Removes all the key-value pairs within gdbm.

Closes the associated database file.

Returns true if the associated database file has been closed.

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

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

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

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

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

Returns true if the database is empty.

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=

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

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

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

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

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.

Returns an array of all keys of this database.

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

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

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.

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

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

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.

An alias for length

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.

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 an array of all key-value pairs contained in the database.

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

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.

Returns an array of all values of this database.

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