Class: SDBM
| Relationships & Source Files | |
| Super Chains via Extension / Inclusion / Inheritance | |
|
Instance Chain:
self,
Enumerable
|
|
| Inherits: | Object |
| Defined in: | ext/sdbm/init.c, ext/sdbm/init.c |
Overview
SDBM provides a simple file-based key-value store, which can only store String keys and values.
Note that Ruby comes with the source code for SDBM, while the DBM and GDBM standard libraries rely on external libraries and headers.
Examples
Insert values:
require 'sdbm'
SDBM.open 'my_database' do |db|
db['apple'] = 'fruit'
db['pear'] = 'fruit'
db['carrot'] = 'vegetable'
db['tomato'] = 'vegetable'
end
Bulk update:
require 'sdbm'
SDBM.open 'my_database' do |db|
db.update('peach' => 'fruit', 'tomato' => 'fruit')
end
Retrieve values:
require 'sdbm'
SDBM.open 'my_database' do |db|
db.each do |key, value|
puts "Key: #{key}, Value: #{value}"
end
end
Outputs:
Key: apple, Value: fruit
Key: pear, Value: fruit
Key: carrot, Value: vegetable
Key: peach, Value: fruit
Key: tomato, Value: fruit
Class Method Summary
-
.new(filename, mode = 0666)
constructor
Creates a new database handle by opening the given
filename. -
.open(filename, mode = 0666)
If called without a block, this is the same as .new.
Instance Attribute Summary
-
#closed? ⇒ Boolean
readonly
Returns
trueif the database is closed. -
#empty? ⇒ Boolean
readonly
Returns
trueif the database is empty.
Instance Method Summary
-
#[](key) ⇒ value?
Returns the
valuein the database associated with the given #key string. -
#[]=(key, value) ⇒ value
(also: #store)
Stores a new
valuein the database with the given #key as an index. -
#clear ⇒ self
Deletes all data from the database.
- #close
-
#delete(key) ⇒ value?
Deletes the key-value pair corresponding to the given #key.
-
#delete_if {|key, value| ... } ⇒ self
Alias for #reject!.
-
#each
(also: #each_pair)
Iterates over each key-value pair in the database.
-
#each_key
Iterates over each #key in the database.
-
#each_pair
Alias for #each.
-
#each_value
Iterates over each
valuein the database. -
#fetch(key) ⇒ value?
Returns the
valuein the database associated with the given #key string. -
#has_key?(key) ⇒ Boolean
Alias for #key?.
-
#has_value?(key) ⇒ Boolean
Alias for #value?.
-
#include?(key) ⇒ Boolean
Alias for #key?.
-
#invert ⇒ Hash
Returns a Hash in which the key-value pairs have been inverted.
-
#key(value) ⇒ key
Returns the
keyassociated with the givenvalue. -
#key?(key) ⇒ Boolean
(also: #has_key?, #include?, #member?)
Returns
trueif the database contains the given #key. -
#keys ⇒ Array
Returns a new Array containing the keys in the database.
-
#length ⇒ Integer
(also: #size)
Returns the number of keys in the database.
-
#member?(key) ⇒ Boolean
Alias for #key?.
-
#reject {|key, value| ... } ⇒ Hash
Creates a new Hash using the key-value pairs from the database, then calls
Hash#rejectwith the given block, which returns a Hash with only the key-value pairs for which the block returnsfalse. -
#reject! {|key, value| ... } ⇒ self
(also: #delete_if)
Iterates over the key-value pairs in the database, deleting those for which the block returns
true. -
#replace(pairs) ⇒ self
Empties the database, then inserts the given key-value pairs.
-
#select {|key, value| ... } ⇒ Array
Returns a new Array of key-value pairs for which the block returns
true. -
#shift ⇒ Array?
Removes a key-value pair from the database and returns them as an Array.
-
#size ⇒ Integer
Alias for #length.
-
#store(key, value) ⇒ value
Alias for #[]=.
-
#to_a ⇒ Array
Returns a new Array containing each key-value pair in the database.
-
#to_hash ⇒ Hash
Returns a new Hash containing each key-value pair in the database.
-
#update(pairs) ⇒ self
Insert or update key-value pairs.
-
#value?(key) ⇒ Boolean
(also: #has_value?)
Returns
trueif the database contains the givenvalue. -
#values ⇒ Array
Returns a new Array containing the values in the database.
-
#values_at(key, ...) ⇒ Array
Returns an Array of values corresponding to the given keys.
- #index(value) Internal use only
Constructor Details
.new(filename, mode = 0666)
Creates a new database handle by opening the given filename. SDBM actually uses two physical files, with extensions ‘.dir’ and ‘.pag’. These extensions will automatically be appended to the filename.
If the file does not exist, a new file will be created using the given mode, unless mode is explicitly set to nil. In the latter case, no database will be created.
If the file exists, it will be opened in read/write mode. If this fails, it will be opened in read-only mode.
# File 'ext/sdbm/init.c', line 178
static VALUE
fsdbm_initialize(int argc, VALUE *argv, VALUE obj)
{
VALUE file, vmode;
DBM *dbm;
struct dbmdata *dbmp;
int mode;
TypedData_Get_Struct(obj, struct dbmdata, &sdbm_type, dbmp);
if (rb_scan_args(argc, argv, "11", &file, &vmode) == 1) {
mode = 0666; /* default value */
}
else if (NIL_P(vmode)) {
mode = -1; /* return nil if DB not exist */
}
else {
mode = NUM2INT(vmode);
}
FilePathValue(file);
dbm = 0;
if (mode >= 0)
dbm = sdbm_open(RSTRING_PTR(file), O_RDWR|O_CREAT, mode);
if (!dbm)
dbm = sdbm_open(RSTRING_PTR(file), O_RDWR, 0);
if (!dbm)
dbm = sdbm_open(RSTRING_PTR(file), O_RDONLY, 0);
if (!dbm) {
if (mode == -1) return Qnil;
rb_sys_fail_str(file);
}
if (dbmp->di_dbm)
sdbm_close(dbmp->di_dbm);
dbmp->di_dbm = dbm;
dbmp->di_size = -1;
return obj;
}
Class Method Details
.open(filename, mode = 0666)
.open(filename, mode = 0666) {|sdbm| ... }
If called without a block, this is the same as .new.
If a block is given, the new database will be passed to the block and will be safely closed after the block has executed.
Example:
require 'sdbm'
SDBM.open('my_database') do |db|
db['hello'] = 'world'
end
# File 'ext/sdbm/init.c', line 237
static VALUE
fsdbm_s_open(int argc, VALUE *argv, VALUE klass)
{
VALUE obj = fsdbm_alloc(klass);
if (NIL_P(fsdbm_initialize(argc, argv, obj))) {
return Qnil;
}
if (rb_block_given_p()) {
return rb_ensure(rb_yield, obj, fsdbm_close, obj);
}
return obj;
}
Instance Attribute Details
#closed? ⇒ Boolean (readonly)
Returns true if the database is closed.
# File 'ext/sdbm/init.c', line 144
static VALUE
fsdbm_closed(VALUE obj)
{
struct dbmdata *dbmp;
TypedData_Get_Struct(obj, struct dbmdata, &sdbm_type, dbmp);
if (dbmp->di_dbm == 0)
return Qtrue;
return Qfalse;
}
#empty? ⇒ Boolean (readonly)
Returns true if the database is empty.
# File 'ext/sdbm/init.c', line 732
static VALUE
fsdbm_empty_p(VALUE obj)
{
datum key;
struct dbmdata *dbmp;
DBM *dbm;
GetDBM(obj, dbmp);
if (dbmp->di_size < 0) {
dbm = dbmp->di_dbm;
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
return Qfalse;
}
}
else {
if (dbmp->di_size)
return Qfalse;
}
return Qtrue;
}
Instance Method Details
#[](key) ⇒ value?
Returns the value in the database associated with the given #key string.
If no value is found, returns nil.
# File 'ext/sdbm/init.c', line 282
static VALUE
fsdbm_aref(VALUE obj, VALUE keystr)
{
return fsdbm_fetch(obj, keystr, Qnil);
}
#[]=(key, value) ⇒ value
#store(key, value) ⇒ value
Also known as: #store
value
#store(key, value) ⇒ value
# File 'ext/sdbm/init.c', line 619
static VALUE
fsdbm_store(VALUE obj, VALUE keystr, VALUE valstr)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
if (valstr == Qnil) {
fsdbm_delete(obj, keystr);
return Qnil;
}
fdbm_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 (sdbm_store(dbm, key, val, DBM_REPLACE)) {
#ifdef HAVE_DBM_CLAERERR
sdbm_clearerr(dbm);
#endif
if (errno == EPERM) rb_sys_fail(0);
rb_raise(rb_eDBMError, "sdbm_store failed");
}
return valstr;
}
#clear ⇒ self
Deletes all data from the database.
# File 'ext/sdbm/init.c', line 552
static VALUE
fsdbm_clear(VALUE obj)
{
datum key;
struct dbmdata *dbmp;
DBM *dbm;
fdbm_modify(obj);
GetDBM2(obj, dbmp, dbm);
dbmp->di_size = -1;
while (key = sdbm_firstkey(dbm), key.dptr) {
if (sdbm_delete(dbm, key)) {
rb_raise(rb_eDBMError, "sdbm_delete failed");
}
}
dbmp->di_size = 0;
return obj;
}
#close
[ GitHub ]# File 'ext/sdbm/init.c', line 126
static VALUE
fsdbm_close(VALUE obj)
{
struct dbmdata *dbmp;
GetDBM(obj, dbmp);
sdbm_close(dbmp->di_dbm);
dbmp->di_dbm = 0;
return Qnil;
}
#delete(key) ⇒ value?
#delete(key) {|key, value| ... }
value?
#delete(key) {|key, value| ... }
# File 'ext/sdbm/init.c', line 433
static VALUE
fsdbm_delete(VALUE obj, VALUE keystr)
{
datum key, value;
struct dbmdata *dbmp;
DBM *dbm;
VALUE valstr;
fdbm_modify(obj);
ExportStringValue(keystr);
key.dptr = RSTRING_PTR(keystr);
key.dsize = RSTRING_LENINT(keystr);
GetDBM2(obj, dbmp, dbm);
dbmp->di_size = -1;
value = sdbm_fetch(dbm, key);
if (value.dptr == 0) {
if (rb_block_given_p()) return rb_yield(keystr);
return Qnil;
}
/* need to save value before sdbm_delete() */
valstr = rb_external_str_new(value.dptr, value.dsize);
if (sdbm_delete(dbm, key)) {
dbmp->di_size = -1;
rb_raise(rb_eDBMError, "dbm_delete failed");
}
else if (dbmp->di_size >= 0) {
dbmp->di_size--;
}
return valstr;
}
#delete_if {|key, value| ... } ⇒ self
#reject! {|key, value| ... } ⇒ self
self
#reject! {|key, value| ... } ⇒ self
Alias for #reject!.
#each
#each {|key, value| ... }
#each_pair
#each_pair {|key, value| ... }
Also known as: #each_pair
Iterates over each key-value pair in the database.
If no block is given, returns an Enumerator.
# File 'ext/sdbm/init.c', line 818
static VALUE
fsdbm_each_pair(VALUE obj)
{
datum key, val;
DBM *dbm;
struct dbmdata *dbmp;
VALUE keystr, valstr;
RETURN_ENUMERATOR(obj, 0, 0);
GetDBM2(obj, dbmp, dbm);
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
val = sdbm_fetch(dbm, key);
keystr = rb_external_str_new(key.dptr, key.dsize);
valstr = rb_external_str_new(val.dptr, val.dsize);
rb_yield(rb_assoc_new(keystr, valstr));
GetDBM2(obj, dbmp, dbm);
}
return obj;
}
#each_key
#each_key {|key| ... }
Iterates over each #key in the database.
If no block is given, returns an Enumerator.
# File 'ext/sdbm/init.c', line 790
static VALUE
fsdbm_each_key(VALUE obj)
{
datum key;
struct dbmdata *dbmp;
DBM *dbm;
RETURN_ENUMERATOR(obj, 0, 0);
GetDBM2(obj, dbmp, dbm);
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
rb_yield(rb_external_str_new(key.dptr, key.dsize));
GetDBM2(obj, dbmp, dbm);
}
return obj;
}
#each
#each {|key, value| ... }
#each_pair
#each_pair {|key, value| ... }
Alias for #each.
#each_value
#each_value {|value| ... }
Iterates over each value in the database.
If no block is given, returns an Enumerator.
# File 'ext/sdbm/init.c', line 763
static VALUE
fsdbm_each_value(VALUE obj)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
RETURN_ENUMERATOR(obj, 0, 0);
GetDBM2(obj, dbmp, dbm);
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
val = sdbm_fetch(dbm, key);
rb_yield(rb_external_str_new(val.dptr, val.dsize));
GetDBM2(obj, dbmp, dbm);
}
return obj;
}
#fetch(key) ⇒ value?
#fetch(key) {|key| ... }
value?
#fetch(key) {|key| ... }
Returns the value in the database associated with the given #key string.
If a block is provided, the block will be called when there is no value associated with the given #key. The #key will be passed in as an argument to the block.
If no block is provided and no value is associated with the given #key, then an IndexError will be raised.
# File 'ext/sdbm/init.c', line 302
static VALUE
fsdbm_fetch_m(int argc, VALUE *argv, VALUE obj)
{
VALUE keystr, valstr, ifnone;
rb_scan_args(argc, argv, "11", &keystr, &ifnone);
valstr = fsdbm_fetch(obj, keystr, ifnone);
if (argc == 1 && !rb_block_given_p() && NIL_P(valstr))
rb_raise(rb_eIndexError, "key not found");
return valstr;
}
#include?(key) ⇒ Boolean
#key?(key) ⇒ Boolean
#member?(key) ⇒ Boolean
#has_key?(key) ⇒ Boolean
Boolean
#key?(key) ⇒ Boolean
#member?(key) ⇒ Boolean
#has_key?(key) ⇒ Boolean
Alias for #key?.
#value?(key) ⇒ Boolean
#has_value?(key) ⇒ Boolean
Boolean
#has_value?(key) ⇒ Boolean
Alias for #value?.
#include?(key) ⇒ Boolean
#key?(key) ⇒ Boolean
#member?(key) ⇒ Boolean
#has_key?(key) ⇒ Boolean
Boolean
#key?(key) ⇒ Boolean
#member?(key) ⇒ Boolean
#has_key?(key) ⇒ Boolean
Alias for #key?.
#index(value)
# File 'ext/sdbm/init.c', line 347
static VALUE
fsdbm_index(VALUE hash, VALUE value)
{
rb_warn("SDBM#index is deprecated; use SDBM#key");
return fsdbm_key(hash, value);
}
#invert ⇒ Hash
# File 'ext/sdbm/init.c', line 588
static VALUE
fsdbm_invert(VALUE obj)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
VALUE keystr, valstr;
VALUE hash = rb_hash_new();
GetDBM2(obj, dbmp, dbm);
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
val = sdbm_fetch(dbm, key);
keystr = rb_external_str_new(key.dptr, key.dsize);
valstr = rb_external_str_new(val.dptr, val.dsize);
rb_hash_aset(hash, valstr, keystr);
}
return hash;
}
#key(value) ⇒ key
Returns the key associated with the given value. If more than one key corresponds to the given value, then the first key to be found will be returned. If no keys are found, nil will be returned.
# File 'ext/sdbm/init.c', line 323
static VALUE
fsdbm_key(VALUE obj, VALUE valstr)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
ExportStringValue(valstr);
val.dptr = RSTRING_PTR(valstr);
val.dsize = RSTRING_LENINT(valstr);
GetDBM2(obj, dbmp, dbm);
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
val = sdbm_fetch(dbm, key);
if (val.dsize == RSTRING_LEN(valstr) &&
memcmp(val.dptr, RSTRING_PTR(valstr), val.dsize) == 0)
return rb_external_str_new(key.dptr, key.dsize);
}
return Qnil;
}
#include?(key) ⇒ Boolean
#key?(key) ⇒ Boolean
#member?(key) ⇒ Boolean
#has_key?(key) ⇒ Boolean
Also known as: #has_key?, #include?, #member?
Boolean
#key?(key) ⇒ Boolean
#member?(key) ⇒ Boolean
#has_key?(key) ⇒ Boolean
Returns true if the database contains the given #key.
# File 'ext/sdbm/init.c', line 896
static VALUE
fsdbm_has_key(VALUE obj, VALUE keystr)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
ExportStringValue(keystr);
key.dptr = RSTRING_PTR(keystr);
key.dsize = RSTRING_LENINT(keystr);
GetDBM2(obj, dbmp, dbm);
val = sdbm_fetch(dbm, key);
if (val.dptr) return Qtrue;
return Qfalse;
}
#keys ⇒ Array
Returns a new Array containing the keys in the database.
# File 'ext/sdbm/init.c', line 846
static VALUE
fsdbm_keys(VALUE obj)
{
datum key;
struct dbmdata *dbmp;
DBM *dbm;
VALUE ary;
GetDBM2(obj, dbmp, dbm);
ary = rb_ary_new();
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
rb_ary_push(ary, rb_external_str_new(key.dptr, key.dsize));
}
return ary;
}
#length ⇒ Integer
#size ⇒ Integer
Also known as: #size
Integer
#size ⇒ Integer
Returns the number of keys in the database.
# File 'ext/sdbm/init.c', line 707
static VALUE
fsdbm_length(VALUE obj)
{
datum key;
struct dbmdata *dbmp;
DBM *dbm;
int i = 0;
GetDBM2(obj, dbmp, dbm);
if (dbmp->di_size > 0) return INT2FIX(dbmp->di_size);
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
i++;
}
dbmp->di_size = i;
return INT2FIX(i);
}
#include?(key) ⇒ Boolean
#key?(key) ⇒ Boolean
#member?(key) ⇒ Boolean
#has_key?(key) ⇒ Boolean
Boolean
#key?(key) ⇒ Boolean
#member?(key) ⇒ Boolean
#has_key?(key) ⇒ Boolean
Alias for #key?.
#reject {|key, value| ... } ⇒ Hash
Creates a new Hash using the key-value pairs from the database, then calls Hash#reject with the given block, which returns a Hash with only the key-value pairs for which the block returns false.
# File 'ext/sdbm/init.c', line 1009
static VALUE
fsdbm_reject(VALUE obj)
{
return rb_hash_delete_if(fsdbm_to_hash(obj));
}
#delete_if {|key, value| ... } ⇒ self
#reject! {|key, value| ... } ⇒ self
Also known as: #delete_if
self
#reject! {|key, value| ... } ⇒ self
Iterates over the key-value pairs in the database, deleting those for which the block returns true.
# File 'ext/sdbm/init.c', line 506
static VALUE
fsdbm_delete_if(VALUE obj)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
VALUE keystr, valstr;
VALUE ret, ary = rb_ary_new();
long i;
int status = 0, n;
fdbm_modify(obj);
GetDBM2(obj, dbmp, dbm);
n = dbmp->di_size;
dbmp->di_size = -1;
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
val = sdbm_fetch(dbm, key);
keystr = rb_external_str_new(key.dptr, key.dsize);
valstr = rb_external_str_new(val.dptr, val.dsize);
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++) {
keystr = RARRAY_AREF(ary, i);
ExportStringValue(keystr);
key.dptr = RSTRING_PTR(keystr);
key.dsize = RSTRING_LENINT(keystr);
if (sdbm_delete(dbm, key)) {
rb_raise(rb_eDBMError, "sdbm_delete failed");
}
}
if (status) rb_jump_tag(status);
if (n > 0) dbmp->di_size = n - RARRAY_LENINT(ary);
return obj;
}
#replace(pairs) ⇒ self
Empties the database, then inserts the given key-value pairs.
This method will work with any object which implements an each_pair method, such as a Hash.
# File 'ext/sdbm/init.c', line 692
static VALUE
fsdbm_replace(VALUE obj, VALUE other)
{
fsdbm_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 key-value pairs for which the block returns true.
Example:
require 'sdbm'
SDBM.open 'my_database' do |db|
db['apple'] = 'fruit'
db['pear'] = 'fruit'
db['spinach'] = 'vegetable'
veggies = db.select do |key, value|
value == 'vegetable'
end #=> [["apple", "fruit"], ["pear", "fruit"]]
end
# File 'ext/sdbm/init.c', line 373
static VALUE
fsdbm_select(VALUE obj)
{
VALUE new = rb_ary_new();
datum key, val;
DBM *dbm;
struct dbmdata *dbmp;
GetDBM2(obj, dbmp, dbm);
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
VALUE assoc, v;
val = sdbm_fetch(dbm, key);
assoc = rb_assoc_new(rb_external_str_new(key.dptr, key.dsize),
rb_external_str_new(val.dptr, val.dsize));
v = rb_yield(assoc);
if (RTEST(v)) {
rb_ary_push(new, assoc);
}
GetDBM2(obj, dbmp, dbm);
}
return new;
}
#shift ⇒ Array?
Removes a key-value pair from the database and returns them as an Array. If the database is empty, returns nil.
# File 'ext/sdbm/init.c', line 475
static VALUE
fsdbm_shift(VALUE obj)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
VALUE keystr, valstr;
fdbm_modify(obj);
GetDBM2(obj, dbmp, dbm);
key = sdbm_firstkey(dbm);
if (!key.dptr) return Qnil;
val = sdbm_fetch(dbm, key);
keystr = rb_external_str_new(key.dptr, key.dsize);
valstr = rb_external_str_new(val.dptr, val.dsize);
sdbm_delete(dbm, key);
if (dbmp->di_size >= 0) {
dbmp->di_size--;
}
return rb_assoc_new(keystr, valstr);
}
#length ⇒ Integer
#size ⇒ Integer
Integer
#size ⇒ Integer
Alias for #length.
#[]=(key, value) ⇒ value
#store(key, value) ⇒ value
value
#store(key, value) ⇒ value
Alias for #[]=.
#to_a ⇒ Array
# File 'ext/sdbm/init.c', line 957
static VALUE
fsdbm_to_a(VALUE obj)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
VALUE ary;
GetDBM2(obj, dbmp, dbm);
ary = rb_ary_new();
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
val = sdbm_fetch(dbm, key);
rb_ary_push(ary, rb_assoc_new(rb_external_str_new(key.dptr, key.dsize),
rb_external_str_new(val.dptr, val.dsize)));
}
return ary;
}
#to_hash ⇒ Hash
Returns a new Hash containing each key-value pair in the database.
# File 'ext/sdbm/init.c', line 982
static VALUE
fsdbm_to_hash(VALUE obj)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
VALUE hash;
GetDBM2(obj, dbmp, dbm);
hash = rb_hash_new();
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
val = sdbm_fetch(dbm, key);
rb_hash_aset(hash, rb_external_str_new(key.dptr, key.dsize),
rb_external_str_new(val.dptr, val.dsize));
}
return hash;
}
#update(pairs) ⇒ self
Insert or update key-value pairs.
This method will work with any object which implements an each_pair method, such as a Hash.
# File 'ext/sdbm/init.c', line 676
static VALUE
fsdbm_update(VALUE obj, VALUE other)
{
rb_block_call(other, rb_intern("each_pair"), 0, 0, update_i, obj);
return obj;
}
#value?(key) ⇒ Boolean
#has_value?(key) ⇒ Boolean
Also known as: #has_value?
Boolean
#has_value?(key) ⇒ Boolean
Returns true if the database contains the given value.
# File 'ext/sdbm/init.c', line 920
static VALUE
fsdbm_has_value(VALUE obj, VALUE valstr)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
ExportStringValue(valstr);
val.dptr = RSTRING_PTR(valstr);
val.dsize = RSTRING_LENINT(valstr);
GetDBM2(obj, dbmp, dbm);
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
val = sdbm_fetch(dbm, key);
if (val.dsize == RSTRING_LENINT(valstr) &&
memcmp(val.dptr, RSTRING_PTR(valstr), val.dsize) == 0)
return Qtrue;
}
return Qfalse;
}
#values ⇒ Array
Returns a new Array containing the values in the database.
# File 'ext/sdbm/init.c', line 869
static VALUE
fsdbm_values(VALUE obj)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
VALUE ary;
GetDBM2(obj, dbmp, dbm);
ary = rb_ary_new();
for (key = sdbm_firstkey(dbm); key.dptr; key = sdbm_nextkey(dbm)) {
val = sdbm_fetch(dbm, key);
rb_ary_push(ary, rb_external_str_new(val.dptr, val.dsize));
}
return ary;
}
#values_at(key, ...) ⇒ Array
Returns an Array of values corresponding to the given keys.
# File 'ext/sdbm/init.c', line 402
static VALUE
fsdbm_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, fsdbm_fetch(obj, argv[i], Qnil));
}
return new;
}