html
Class GDBM
In: gdbm/gdbm.c
Parent: Object
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
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
Links
www.gnu.org/software/gdbm/
Methods
1 of 7 2010-09-02 5:18 PM
Class: GDBM http://ruby-doc.org/stdlib/libdoc/gdbm/rdoc/classes/GDBM.html
[] []= cachesize= clear close closed? delete delete_if each each_key each_pair
each_value empty? fastmode= fetch has_key? has_value? include? index invert key?
keys length member? new open reject reject! reorganize replace select shift size
store sync syncmode= to_a to_hash update value? values values_at
Included Modules
Enumerable
Constants
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:
The values WRITER, WRCREAT and NEWDB may be combined with the following values by
bitwise or:
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).
[Source]
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.
2 of 7 2010-09-02 5:18 PM
Class: GDBM http://ruby-doc.org/stdlib/libdoc/gdbm/rdoc/classes/GDBM.html
require 'gdbm'
GDBM.open("fruitstore.db") do |gdbm|
gdbm.each_pair do |key, value|
print "#{key}: #{value}\n"
end
end
[Source]
gdbm[key] ? value
[Source]
[Source]
[Source]
gdbm.clear ? gdbm
[Source]
gdbm.close ? nil
[Source]
[Source]
Removes the key-value-pair with the specified key from this database and returns the
corresponding value. Returns nil if the database is empty.
[Source]
Deletes every key-value pair from gdbm for which block evaluates to true.
[Source]
3 of 7 2010-09-02 5:18 PM
Class: GDBM http://ruby-doc.org/stdlib/libdoc/gdbm/rdoc/classes/GDBM.html
Executes block for each key in the database, passing the key and the correspoding value as a
parameter.
[Source]
Executes block for each key in the database, passing the key as a parameter.
[Source]
Executes block for each key in the database, passing the key and the correspoding value as a
parameter.
[Source]
Executes block for each key in the database, passing the corresponding value as a parameter.
[Source]
[Source]
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=
[Source]
Retrieves the value corresponding to key. If there is no value associated with key, default will be
returned instead.
[Source]
Returns true if the given key k exists within the database. Returns false otherwise.
[Source]
Returns true if the given value v exists within the database. Returns false otherwise.
[Source]
4 of 7 2010-09-02 5:18 PM
Class: GDBM http://ruby-doc.org/stdlib/libdoc/gdbm/rdoc/classes/GDBM.html
Returns true if the given key k exists within the database. Returns false otherwise.
[Source]
gdbm.index(value) ? key
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.
[Source]
gdbm.invert ? hash
Returns a hash created by using gdbm‘s values as keys, and the keys as values.
[Source]
Returns true if the given key k exists within the database. Returns false otherwise.
[Source]
gdbm.keys ? array
[Source]
gdbm.length ? fixnum
gdbm.size ? fixnum
[Source]
Returns true if the given key k exists within the database. Returns false otherwise.
[Source]
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
[Source]
Deletes every key-value pair from gdbm for which block evaluates to true.
[Source]
gdbm.reorganize ? gdbm
5 of 7 2010-09-02 5:18 PM
Class: GDBM http://ruby-doc.org/stdlib/libdoc/gdbm/rdoc/classes/GDBM.html
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.
[Source]
gdbm.replace(other) ? gdbm
Replaces the content of gdbm with the key-value pairs of other. other must have an each_pair
method.
[Source]
Returns a new array of all values of the database for which block evaluates to true.
[Source]
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.
[Source]
gdbm.length ? fixnum
gdbm.size ? fixnum
[Source]
[Source]
gdbm.sync ? gdbm
Unless the gdbm object has been opened with the SYNC flag, it is not guarenteed 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.
[Source]
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=
[Source]
gdbm.to_a ? array
6 of 7 2010-09-02 5:18 PM
Class: GDBM http://ruby-doc.org/stdlib/libdoc/gdbm/rdoc/classes/GDBM.html
[Source]
gdbm.to_hash ? hash
[Source]
gdbm.update(other) ? gdbm
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.
[Source]
Returns true if the given value v exists within the database. Returns false otherwise.
[Source]
gdbm.values ? array
[Source]
[Source]
[Validate]
7 of 7 2010-09-02 5:18 PM