DBM
class DBM
Parent:ObjectIncluded modules:Enumerable
介绍
DBM类为Unix风格的dbm或数据库管理器库提供封装。
Dbm数据库没有表或列; 它们是简单的键值数据存储,就像Ruby Hash,除了不驻留在RAM中。键和值必须是字符串。
所使用的确切库取决于Ruby是如何编译的。它可以是以下任何一种:
- 最初的ndbm库在4.3BSD中发布。它基于Unix版本7中的dbm库,但具有不同的API以支持进程中的多个数据库。
- Berkeley DB版本1至5,也被称为BDB和Sleepycat DB,现在归甲骨文公司所有。
- Berkeley DB 1.x,仍然可以在4.4BSD衍生产品(FreeBSD,OpenBSD等)中找到。
- gdbm,dbm的GNU实现。
- QDBM,这些 DBM 实现的 dbm.All 的另一个开源重新实现有可用自己的 Ruby 接口,提供更丰富(但不同)APIs.CautionsBefore 您决定使用 DBM,也有一些问题,你应该考虑:
- 每个dbm的实现都有自己的文件格式。通常,dbm库不会读取彼此的文件。这使得dbm文件成为数据交换的不错的选择。
- 即使运行相同的操作系统和相同的dbm实现,数据库文件格式也可能取决于CPU体系结构。例如,文件可能不能在PowerPC和386之间,或在32和64位Linux之间移植。
- 不同版本的Berkeley DB使用不同的文件格式。因此,对操作系统的更改可能会破坏对现有文件的DBM访问。
数据大小限制因实现而异。原始的Berkeley DB仅限于2GB的数据。 Dbm库有时还会限制键/值对的总大小,以及散列为相同值的所有键的总大小。这些限制可能只有512个字节。也就是说,gdbm和Berkeley DB的最新版本取消了这些限制。鉴于上述注意事项,DBM不是长期存储重要数据的好选择。它可能最适合用作处理大量数据的Hash的快速和简单替代。示例 'dbm'
- “ndbm (4.3BSD)”
- “Berkeley DB 4.8.30: (April 9, 2010)”
- “Berkeley DB (unknown)” (4.4BSD, maybe)
- “GDBM version 1.8.3. 10/15/2002 (built Jul 1 2011 12:32:45)”
- “QDBM 1.8.78”
WRCREAT
指示dbm_open()应以读取/写入模式打开数据库,如果数据库尚不存在,则创建该数据库
WRITER
指示dbm_open()应以读/写模式打开数据库
公共类方法
new(filename[, mode, flags]) → dbm Show source
用指定名称打开一个dbm数据库,该名称可以包含一个目录路径。任何需要的文件扩展名都将由dbm库自动提供。例如,Berkeley DB追加'.db',而GNU gdbm使用两个扩展名为'.dir'和'.pag'的物理文件。
该模式应该是一个整数,如Unix chmod。
标志应该是READER,WRITER,WRCREAT或NEWDB之一。
static VALUE
fdbm_initialize(int argc, VALUE *argv, VALUE obj)
{
VALUE file, vmode, vflags;
DBM *dbm;
struct dbmdata *dbmp;
int mode, flags = 0;
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 not exist */
}
else {
mode = NUM2INT(vmode
}
if (!NIL_P(vflags))
flags = NUM2INT(vflags
FilePathValue(file
/*
* Note:
* gdbm 1.10 works with O_CLOEXEC. gdbm 1.9.1 silently ignore it.
*/
#ifndef O_CLOEXEC
# define O_CLOEXEC 0
#endif
if (flags & RUBY_DBM_RW_BIT) {
flags &= ~RUBY_DBM_RW_BIT;
dbm = dbm_open(RSTRING_PTR(file), flags|O_CLOEXEC, mode
}
else {
dbm = 0;
if (mode >= 0) {
dbm = dbm_open(RSTRING_PTR(file), O_RDWR|O_CREAT|O_CLOEXEC, mode
}
if (!dbm) {
dbm = dbm_open(RSTRING_PTR(file), O_RDWR|O_CLOEXEC, 0
}
if (!dbm) {
dbm = dbm_open(RSTRING_PTR(file), O_RDONLY|O_CLOEXEC, 0
}
}
if (dbm) {
/*
* History of dbm_pagfno() and dbm_dirfno() in ndbm and its compatibles.
* (dbm_pagfno() and dbm_dirfno() is not standardized.)
*
* 1986: 4.3BSD provides ndbm.
* It provides dbm_pagfno() and dbm_dirfno() as macros.
* 1991: gdbm-1.5 provides them as functions.
* They returns a same descriptor.
* (Earlier releases may have the functions too.)
* 1991: Net/2 provides Berkeley DB.
* It doesn't provide dbm_pagfno() and dbm_dirfno().
* 1992: 4.4BSD Alpha provides Berkeley DB with dbm_dirfno() as a function.
* dbm_pagfno() is a macro as DBM_PAGFNO_NOT_AVAILABLE.
* 1997: Berkeley DB 2.0 is released by Sleepycat Software, Inc.
* It defines dbm_pagfno() and dbm_dirfno() as macros.
* 2011: gdbm-1.9 creates a separate dir file.
* dbm_pagfno() and dbm_dirfno() returns different descriptors.
*/
#if defined(HAVE_DBM_PAGFNO)
rb_fd_fix_cloexec(dbm_pagfno(dbm)
#endif
#if defined(HAVE_DBM_DIRFNO)
rb_fd_fix_cloexec(dbm_dirfno(dbm)
#endif
#if defined(RUBYDBM_DB_HEADER) && defined(HAVE_TYPE_DBC)
/* Disable Berkeley DB error messages such as:
* DB->put: attempt to modify a read-only database */
((DBC*)dbm)->dbp->set_errfile(((DBC*)dbm)->dbp, NULL
#endif
}
if (!dbm) {
if (mode == -1) return Qnil;
rb_sys_fail_str(file
}
dbmp = ALLOC(struct dbmdata
DATA_PTR(obj) = dbmp;
dbmp->di_dbm = dbm;
dbmp->di_size = -1;
return obj;
}
open(filename[, mode, flags]) → dbm Show source
open(filename[, mode, flags]) {|dbm| block}
打开一个dbm数据库并在给定块的情况下产生它。另见DBM.new
。
static VALUE
fdbm_s_open(int argc, VALUE *argv, VALUE klass)
{
VALUE obj = fdbm_alloc(klass
if (NIL_P(fdbm_initialize(argc, argv, obj))) {
return Qnil;
}
if (rb_block_given_p()) {
return rb_ensure(rb_yield, obj, fdbm_close, obj
}
return obj;
}
公共实例方法
dbmkey→字符串值或零显示源
通过查找提供的密钥字符串,从数据库中返回一个值。如果找不到密钥,则返回nil。
static VALUE
fdbm_aref(VALUE obj, VALUE keystr)
{
return fdbm_fetch(obj, keystr, Qnil
}
dbmkey = value Show source
将指定的字符串值存储在数据库中,通过提供的字符串键进行索引。
static VALUE
fdbm_store(VALUE obj, VALUE keystr, VALUE valstr)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
fdbm_modify(obj
keystr = rb_obj_as_string(keystr
valstr = rb_obj_as_string(valstr
key.dptr = RSTRING_PTR(keystr
key.dsize = RSTRING_DSIZE(keystr
val.dptr = RSTRING_PTR(valstr
val.dsize = RSTRING_DSIZE(valstr
GetDBM2(obj, dbmp, dbm
dbmp->di_size = -1;
if (dbm_store(dbm, key, val, DBM_REPLACE)) {
dbm_clearerr(dbm
if (errno == EPERM) rb_sys_fail(0
rb_raise(rb_eDBMError, "dbm_store failed"
}
return valstr;
}
clear Show source
从数据库中删除所有数据。
static VALUE
fdbm_clear(VALUE obj)
{
datum key;
struct dbmdata *dbmp;
DBM *dbm;
fdbm_modify(obj
GetDBM2(obj, dbmp, dbm
dbmp->di_size = -1;
while (key = dbm_firstkey(dbm), key.dptr) {
if (dbm_delete(dbm, key)) {
rb_raise(rb_eDBMError, "dbm_delete failed"
}
}
dbmp->di_size = 0;
return obj;
}
close Show source
关闭数据库。
static VALUE
fdbm_close(VALUE obj)
{
struct dbmdata *dbmp;
GetDBM(obj, dbmp
dbm_close(dbmp->di_dbm
dbmp->di_dbm = 0;
return Qnil;
}
closed? → true or false Show source
如果数据库关闭,则返回true,否则返回false。
static VALUE
fdbm_closed(VALUE obj)
{
struct dbmdata *dbmp;
TypedData_Get_Struct(obj, struct dbmdata, &dbm_type, dbmp
if (dbmp == 0)
return Qtrue;
if (dbmp->di_dbm == 0)
return Qtrue;
return Qfalse;
}
delete(key) Show source
从数据库中删除一个条目。
static VALUE
fdbm_delete(VALUE obj, VALUE keystr)
{
datum key, value;
struct dbmdata *dbmp;
DBM *dbm;
VALUE valstr;
long len;
fdbm_modify(obj
ExportStringValue(keystr
len = RSTRING_LEN(keystr
if (TOO_LONG(len)) goto not_found;
key.dptr = RSTRING_PTR(keystr
key.dsize = (DSIZE_TYPE)len;
GetDBM2(obj, dbmp, dbm
value = dbm_fetch(dbm, key
if (value.dptr == 0) {
not_found:
if (rb_block_given_p()) return rb_yield(keystr
return Qnil;
}
/* need to save value before dbm_delete() */
valstr = rb_tainted_str_new(value.dptr, value.dsize
if (dbm_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| block} → self Show source
删除代码块返回true的所有条目。返回自身。
static VALUE
fdbm_delete_if(VALUE obj)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
VALUE keystr, valstr;
VALUE ret, ary = rb_ary_tmp_new(0
int status = 0;
long i, n;
fdbm_modify(obj
GetDBM2(obj, dbmp, dbm
n = dbmp->di_size;
dbmp->di_size = -1;
for (key = dbm_firstkey(dbm key.dptr; key = dbm_nextkey(dbm)) {
val = dbm_fetch(dbm, key
keystr = rb_tainted_str_new(key.dptr, key.dsize
OBJ_FREEZE(keystr
valstr = rb_tainted_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
key.dptr = RSTRING_PTR(keystr
key.dsize = (DSIZE_TYPE)RSTRING_LEN(keystr
if (dbm_delete(dbm, key)) {
rb_raise(rb_eDBMError, "dbm_delete failed"
}
}
if (status) rb_jump_tag(status
if (n > 0) dbmp->di_size = n - RARRAY_LEN(ary
rb_ary_clear(ary
return obj;
}
each_pair {|key,value| block} → self Show source
为数据库中的每个键值对调用一次块。返回自身。
static VALUE
fdbm_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 = dbm_firstkey(dbm key.dptr; key = dbm_nextkey(dbm)) {
val = dbm_fetch(dbm, key
keystr = rb_tainted_str_new(key.dptr, key.dsize
valstr = rb_tainted_str_new(val.dptr, val.dsize
rb_yield(rb_assoc_new(keystr, valstr)
GetDBM2(obj, dbmp, dbm
}
return obj;
}
each_key {|key| block} → self Show source
为数据库中的每个键字符串调用一次块。返回自身。
static VALUE
fdbm_each_key(VALUE obj)
{
datum key;
struct dbmdata *dbmp;
DBM *dbm;
RETURN_ENUMERATOR(obj, 0, 0
GetDBM2(obj, dbmp, dbm
for (key = dbm_firstkey(dbm key.dptr; key = dbm_nextkey(dbm)) {
rb_yield(rb_tainted_str_new(key.dptr, key.dsize)
GetDBM2(obj, dbmp, dbm
}
return obj;
}
each_pair {|key,value| block} → self Show source
为数据库中的每个键值对调用一次块。返回自身。
static VALUE
fdbm_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 = dbm_firstkey(dbm key.dptr; key = dbm_nextkey(dbm)) {
val = dbm_fetch(dbm, key
keystr = rb_tainted_str_new(key.dptr, key.dsize
valstr = rb_tainted_str_new(val.dptr, val.dsize
rb_yield(rb_assoc_new(keystr, valstr)
GetDBM2(obj, dbmp, dbm
}
return obj;
}
each_value {|value| block} → self Show source
为数据库中的每个值字符串调用一次块。返回自身。
static VALUE
fdbm_each_value(VALUE obj)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
RETURN_ENUMERATOR(obj, 0, 0
GetDBM2(obj, dbmp, dbm
for (key = dbm_firstkey(dbm key.dptr; key = dbm_nextkey(dbm)) {
val = dbm_fetch(dbm, key
rb_yield(rb_tainted_str_new(val.dptr, val.dsize)
GetDBM2(obj, dbmp, dbm
}
return obj;
}
empty? Show source
如果数据库为空,则返回true,否则返回false。
static VALUE
fdbm_empty_p(VALUE obj)
{
datum key;
struct dbmdata *dbmp;
DBM *dbm;
GetDBM2(obj, dbmp, dbm
if (dbmp->di_size < 0) {
dbm = dbmp->di_dbm;
for (key = dbm_firstkey(dbm key.dptr; key = dbm_nextkey(dbm)) {
return Qfalse;
}
}
else {
if (dbmp->di_size)
return Qfalse;
}
return Qtrue;
}
fetch(key, ifnone) → value Show source
通过查找提供的密钥字符串,从数据库中返回一个值。如果找不到密钥,则返回ifnone
。如果ifnone
没有给出,则引发IndexError。
static VALUE
fdbm_fetch_m(int argc, VALUE *argv, VALUE obj)
{
VALUE keystr, valstr, ifnone;
rb_scan_args(argc, argv, "11", &keystr, &ifnone
valstr = fdbm_fetch(obj, keystr, ifnone
if (argc == 1 && !rb_block_given_p() && NIL_P(valstr))
rb_raise(rb_eIndexError, "key not found"
return valstr;
}
has_key?(key) → boolean Show source
如果数据库包含指定的键,则返回true,否则返回false。
static VALUE
fdbm_has_key(VALUE obj, VALUE keystr)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
long len;
ExportStringValue(keystr
len = RSTRING_LEN(keystr
if (TOO_LONG(len)) return Qfalse;
key.dptr = RSTRING_PTR(keystr
key.dsize = (DSIZE_TYPE)len;
GetDBM2(obj, dbmp, dbm
val = dbm_fetch(dbm, key
if (val.dptr) return Qtrue;
return Qfalse;
}
has_value?(value) → boolean Show source
如果数据库包含指定的字符串值,则返回true,否则返回false。
static VALUE
fdbm_has_value(VALUE obj, VALUE valstr)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
long len;
ExportStringValue(valstr
len = RSTRING_LEN(valstr
if (TOO_LONG(len)) return Qfalse;
val.dptr = RSTRING_PTR(valstr
val.dsize = (DSIZE_TYPE)len;
GetDBM2(obj, dbmp, dbm
for (key = dbm_firstkey(dbm key.dptr; key = dbm_nextkey(dbm)) {
val = dbm_fetch(dbm, key
if ((DSIZE_TYPE)val.dsize == (DSIZE_TYPE)RSTRING_LEN(valstr) &&
memcmp(val.dptr, RSTRING_PTR(valstr), val.dsize) == 0)
return Qtrue;
}
return Qfalse;
}
include?(key) → boolean Show source
如果数据库包含指定的键,则返回true,否则返回false。
static VALUE
fdbm_has_key(VALUE obj, VALUE keystr)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
long len;
ExportStringValue(keystr
len = RSTRING_LEN(keystr
if (TOO_LONG(len)) return Qfalse;
key.dptr = RSTRING_PTR(keystr
key.dsize = (DSIZE_TYPE)len;
GetDBM2(obj, dbmp, dbm
val = dbm_fetch(dbm, key
if (val.dptr) return Qtrue;
return Qfalse;
}
invert → hash Show source
通过使用数据库中的每个值作为关键字创建一个哈希(不是DBM数据库),并将相应的键用作其值。
static VALUE
fdbm_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 = dbm_firstkey(dbm key.dptr; key = dbm_nextkey(dbm)) {
val = dbm_fetch(dbm, key
keystr = rb_tainted_str_new(key.dptr, key.dsize
valstr = rb_tainted_str_new(val.dptr, val.dsize
rb_hash_aset(hash, valstr, keystr
}
return hash;
}
key(value) → string Show source
返回指定值的键。
static VALUE
fdbm_key(VALUE obj, VALUE valstr)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
long len;
ExportStringValue(valstr
len = RSTRING_LEN(valstr
if (TOO_LONG(len)) return Qnil;
val.dptr = RSTRING_PTR(valstr
val.dsize = (DSIZE_TYPE)len;
GetDBM2(obj, dbmp, dbm
for (key = dbm_firstkey(dbm key.dptr; key = dbm_nextkey(dbm)) {
val = dbm_fetch(dbm, key
if ((long)val.dsize == RSTRING_LEN(valstr) &&
memcmp(val.dptr, RSTRING_PTR(valstr), val.dsize) == 0) {
return rb_tainted_str_new(key.dptr, key.dsize
}
}
return Qnil;
}
key?(key) → boolean Show source
如果数据库包含指定的键,则返回true,否则返回false。
static VALUE
fdbm_has_key(VALUE obj, VALUE keystr)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
long len;
ExportStringValue(keystr
len = RSTRING_LEN(keystr
if (TOO_LONG(len)) return Qfalse;
key.dptr = RSTRING_PTR(keystr
key.dsize = (DSIZE_TYPE)len;
GetDBM2(obj, dbmp, dbm
val = dbm_fetch(dbm, key
if (val.dptr) return Qtrue;
return Qfalse;
}
keys → array Show source
返回数据库中所有字符串键的数组。
static VALUE
fdbm_keys(VALUE obj)
{
datum key;
struct dbmdata *dbmp;
DBM *dbm;
VALUE ary;
GetDBM2(obj, dbmp, dbm
ary = rb_ary_new(
for (key = dbm_firstkey(dbm key.dptr; key = dbm_nextkey(dbm)) {
rb_ary_push(ary, rb_tainted_str_new(key.dptr, key.dsize)
}
return ary;
}
length → integer Show source
返回数据库中条目的数量。
static VALUE
fdbm_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 = dbm_firstkey(dbm key.dptr; key = dbm_nextkey(dbm)) {
i++;
}
dbmp->di_size = i;
return INT2FIX(i
}
member?(key) → boolean Show source
如果数据库包含指定的键,则返回true,否则返回false。
static VALUE
fdbm_has_key(VALUE obj, VALUE keystr)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
long len;
ExportStringValue(keystr
len = RSTRING_LEN(keystr
if (TOO_LONG(len)) return Qfalse;
key.dptr = RSTRING_PTR(keystr
key.dsize = (DSIZE_TYPE)len;
GetDBM2(obj, dbmp, dbm
val = dbm_fetch(dbm, key
if (val.dptr) return Qtrue;
return Qfalse;
}
reject {|key,value| block} → Hash Show source
将数据库的内容转换为内存中的哈希,然后使用指定的代码块调用Hash#reject,返回一个新的哈希。
static VALUE
fdbm_reject(VALUE obj)
{
return rb_hash_delete_if(fdbm_to_hash(obj)
}
reject! {|key, value| block} → self Show source
删除代码块返回true的所有条目。返回自身。
static VALUE
fdbm_delete_if(VALUE obj)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
VALUE keystr, valstr;
VALUE ret, ary = rb_ary_tmp_new(0
int status = 0;
long i, n;
fdbm_modify(obj
GetDBM2(obj, dbmp, dbm
n = dbmp->di_size;
dbmp->di_size = -1;
for (key = dbm_firstkey(dbm key.dptr; key = dbm_nextkey(dbm)) {
val = dbm_fetch(dbm, key
keystr = rb_tainted_str_new(key.dptr, key.dsize
OBJ_FREEZE(keystr
valstr = rb_tainted_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
key.dptr = RSTRING_PTR(keystr
key.dsize = (DSIZE_TYPE)RSTRING_LEN(keystr
if (dbm_delete(dbm, key)) {
rb_raise(rb_eDBMError, "dbm_delete failed"
}
}
if (status) rb_jump_tag(status
if (n > 0) dbmp->di_size = n - RARRAY_LEN(ary
rb_ary_clear(ary
return obj;
}
replace(obj) Show source
用指定对象的内容替换数据库的内容。接受实现#each_pair方法的任何对象,包括Hash和DBM对象。
static VALUE
fdbm_replace(VALUE obj, VALUE other)
{
fdbm_clear(obj
rb_block_call(other, rb_intern("each_pair"), 0, 0, update_i, obj
return obj;
}
select {|key, value| block} → array Show source
返回由代码块返回true的键值对组成的新数组。
static VALUE
fdbm_select(VALUE obj)
{
VALUE new = rb_ary_new(
datum key, val;
DBM *dbm;
struct dbmdata *dbmp;
GetDBM2(obj, dbmp, dbm
for (key = dbm_firstkey(dbm key.dptr; key = dbm_nextkey(dbm)) {
VALUE assoc, v;
val = dbm_fetch(dbm, key
assoc = rb_assoc_new(rb_tainted_str_new(key.dptr, key.dsize),
rb_tainted_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() → key, value()
从数据库中删除一个键值对,并返回它。如果数据库为空,则返回nil。无法保证值的排除/返回顺序。
static VALUE
fdbm_shift(VALUE obj)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
VALUE keystr, valstr;
fdbm_modify(obj
GetDBM2(obj, dbmp, dbm
dbmp->di_size = -1;
key = dbm_firstkey(dbm
if (!key.dptr) return Qnil;
val = dbm_fetch(dbm, key
keystr = rb_tainted_str_new(key.dptr, key.dsize
valstr = rb_tainted_str_new(val.dptr, val.dsize
dbm_delete(dbm, key
return rb_assoc_new(keystr, valstr
}
size → integer Show source
返回数据库中条目的数量。
static VALUE
fdbm_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 = dbm_firstkey(dbm key.dptr; key = dbm_nextkey(dbm)) {
i++;
}
dbmp->di_size = i;
return INT2FIX(i
}
store(key, value) → value Show source
将指定的字符串值存储在数据库中,通过提供的字符串键进行索引。
static VALUE
fdbm_store(VALUE obj, VALUE keystr, VALUE valstr)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
fdbm_modify(obj
keystr = rb_obj_as_string(keystr
valstr = rb_obj_as_string(valstr
key.dptr = RSTRING_PTR(keystr
key.dsize = RSTRING_DSIZE(keystr
val.dptr = RSTRING_PTR(valstr
val.dsize = RSTRING_DSIZE(valstr
GetDBM2(obj, dbmp, dbm
dbmp->di_size = -1;
if (dbm_store(dbm, key, val, DBM_REPLACE)) {
dbm_clearerr(dbm
if (errno == EPERM) rb_sys_fail(0
rb_raise(rb_eDBMError, "dbm_store failed"
}
return valstr;
}
to_a → array Show source
将数据库的内容转换为一个包含键值数组的数组,并将其返回。
static VALUE
fdbm_to_a(VALUE obj)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
VALUE ary;
GetDBM2(obj, dbmp, dbm
ary = rb_ary_new(
for (key = dbm_firstkey(dbm key.dptr; key = dbm_nextkey(dbm)) {
val = dbm_fetch(dbm, key
rb_ary_push(ary, rb_assoc_new(rb_tainted_str_new(key.dptr, key.dsize),
rb_tainted_str_new(val.dptr, val.dsize))
}
return ary;
}
to_hash → hash Show source
将数据库的内容转换为内存中的哈希对象,并将其返回。
static VALUE
fdbm_to_hash(VALUE obj)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
VALUE hash;
GetDBM2(obj, dbmp, dbm
hash = rb_hash_new(
for (key = dbm_firstkey(dbm key.dptr; key = dbm_nextkey(dbm)) {
val = dbm_fetch(dbm, key
rb_hash_aset(hash, rb_tainted_str_new(key.dptr, key.dsize),
rb_tainted_str_new(val.dptr, val.dsize)
}
return hash;
}
update(obj) Show source
使用指定对象的多个值更新数据库。接受实现#each_pair方法的任何对象,包括Hash和DBM对象。
static VALUE
fdbm_update(VALUE obj, VALUE other)
{
rb_block_call(other, rb_intern("each_pair"), 0, 0, update_i, obj
return obj;
}
value?(value) → boolean Show source
如果数据库包含指定的字符串值,则返回true,否则返回false。
static VALUE
fdbm_has_value(VALUE obj, VALUE valstr)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
long len;
ExportStringValue(valstr
len = RSTRING_LEN(valstr
if (TOO_LONG(len)) return Qfalse;
val.dptr = RSTRING_PTR(valstr
val.dsize = (DSIZE_TYPE)len;
GetDBM2(obj, dbmp, dbm
for (key = dbm_firstkey(dbm key.dptr; key = dbm_nextkey(dbm)) {
val = dbm_fetch(dbm, key
if ((DSIZE_TYPE)val.dsize == (DSIZE_TYPE)RSTRING_LEN(valstr) &&
memcmp(val.dptr, RSTRING_PTR(valstr), val.dsize) == 0)
return Qtrue;
}
return Qfalse;
}
values → array Show source
返回数据库中所有字符串值的数组。
static VALUE
fdbm_values(VALUE obj)
{
datum key, val;
struct dbmdata *dbmp;
DBM *dbm;
VALUE ary;
GetDBM2(obj, dbmp, dbm
ary = rb_ary_new(
for (key = dbm_firstkey(dbm key.dptr; key = dbm_nextkey(dbm)) {
val = dbm_fetch(dbm, key
rb_ary_push(ary, rb_tainted_str_new(val.dptr, val.dsize)
}
return ary;
}
values_at(key, ...) → Array Show source
返回包含与给定键相关的值的数组。
static VALUE
fdbm_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, fdbm_fetch(obj, argv[i], Qnil)
}
return new;
}