Object
<code>
tdb = TDB.new("/path/to/file", flags => IO::RDWR|IO::CREAT)
tdb.store("HELLO", "world")
tdb.fetch("HELLO") -> "world"
tdb.delete("HELLO") -> "world"
</code>
: Initializes a TDB context. It takes several options.
:hash_size - the number of buckets, this is the most important tuning parameter when creating large databases. This parameter only affects the creation of new databases.
:open_flags - a bit mask of IO flags passed directly to open(2), File.open-compatible flags are accepted.
:hash - any of the hashes described in Hash_Functions. This must remain the same for all clients.
:tdb_flags - a bitmask of any combination of TDB::CLEAR_IF_FIRST, TDB::INTERNAL, TDB::NOLOCK, TDB::NOMMAP, TDB::CONVERT, TDB::BIGENDIAN, TDB::NOSYNC, TDB::SEQNUM, TDB::VOLATILE, TDB::ALLOW_NESTING, TDB::DISALLOW_NESTING, TDB::INCOMPATIBLE_HASH
:mode - octal mode mask passed to open(2)
static VALUE init(int argc, VALUE *argv, VALUE self)
{
struct tdb_context *tdb = db(self, 0);
VALUE path, opts;
struct open_args o;
if (tdb)
rb_raise(rb_eRuntimeError, "TDB already initialized");
rb_scan_args(argc, argv, "11", &path, &opts);
set_args(self, &o, opts);
if (NIL_P(path))
o.tdb_flags |= TDB_INTERNAL;
else
o.name = StringValuePtr(path);
tdb = (struct tdb_context *)my_tbr(nogvl_open, &o);
if (!tdb) {
switch (errno) {
case ENOMEM:
case EMFILE:
case ENFILE:
rb_gc();
tdb = (struct tdb_context *)my_tbr(nogvl_open, &o);
}
if (!tdb)
rb_sys_fail("tdb_open_ex");
}
DATA_PTR(self) = tdb;
return self;
}
clears out the database
static VALUE clear(VALUE self)
{
struct tdb_context *tdb = db(self, 1);
if ((int)my_tbr((rb_blocking_function_t *)tdb_wipe_all, tdb))
my_raise(tdb);
return self;
}
static VALUE tdbclose(VALUE self)
{
struct tdb_context *tdb = db(self, 1);
DATA_PTR(self) = NULL;
if ((int)my_tbr(nogvl_close, tdb) == -1)
rb_sys_fail("tdb_close");
return Qnil;
}
static VALUE closed(VALUE self)
{
struct tdb_context *tdb = db(self, 0);
return tdb ? Qfalse : Qtrue;
}
static VALUE delete(int argc, VALUE *argv, VALUE self)
{
VALUE rc = fetch(argc, argv, self);
if (! NIL_P(rc))
if (nuke(self, argv[0]) == Qfalse)
return Qnil;
return rc;
}
static VALUE each(VALUE self)
{
struct traverse_args t;
t.tdb = db(self, 1);
t.state = 0;
my_tbr(nogvl_traverse, &t);
if (t.state)
rb_jump_tag(t.state);
return self;
}
static VALUE fetch(int argc, VALUE *argv, VALUE self)
{
struct fetch_parse_args f;
VALUE key;
rb_scan_args(argc, argv, "11", &key, &f.value);
if (NIL_P(f.value)) {
f.value = rb_str_new(0, 0);
} else {
StringValue(f.value);
rb_str_set_len(f.value, 0);
}
f.tdb = db(self, 1);
TO_TDB_DATA(f.as.key, key);
return my_tbr(nogvl_parse_record, &f);
}
static VALUE has_key(VALUE self, VALUE key)
{
struct exists_args e;
e.tdb = db(self, 1);
TO_TDB_DATA(e.key, key);
return my_tbr(nogvl_exists, &e);
}
static VALUE has_key(VALUE self, VALUE key)
{
struct exists_args e;
e.tdb = db(self, 1);
TO_TDB_DATA(e.key, key);
return my_tbr(nogvl_exists, &e);
}
static VALUE insert(VALUE self, VALUE key, VALUE val)
{
return rbtdb_store(self, key, val, TDB_INSERT, 1);
}
static VALUE insert_bang(VALUE self, VALUE key, VALUE val)
{
return rbtdb_store(self, key, val, TDB_INSERT, 0);
}
static VALUE has_key(VALUE self, VALUE key)
{
struct exists_args e;
e.tdb = db(self, 1);
TO_TDB_DATA(e.key, key);
return my_tbr(nogvl_exists, &e);
}
static VALUE lockall(VALUE self)
{
struct tdb_context *tdb = db(self, 1);
if ((int)my_tbr((rb_blocking_function_t *)tdb_lockall, tdb))
my_raise(tdb);
return Qtrue;
}
static VALUE lockall_mark(VALUE self)
{
struct tdb_context *tdb = db(self, 1);
if ((int)my_tbr((rb_blocking_function_t *)tdb_lockall_mark, tdb))
my_raise(tdb);
return Qtrue;
}
static VALUE lockall_read(VALUE self)
{
struct tdb_context *tdb = db(self, 1);
if ((int)my_tbr((rb_blocking_function_t *)tdb_lockall_read, tdb))
my_raise(tdb);
return Qtrue;
}
static VALUE lockall_unmark(VALUE self)
{
struct tdb_context *tdb = db(self, 1);
if ((int)my_tbr((rb_blocking_function_t *)tdb_lockall_unmark, tdb))
my_raise(tdb);
return Qtrue;
}
static VALUE has_key(VALUE self, VALUE key)
{
struct exists_args e;
e.tdb = db(self, 1);
TO_TDB_DATA(e.key, key);
return my_tbr(nogvl_exists, &e);
}
static VALUE modify(VALUE self, VALUE key, VALUE val)
{
return rbtdb_store(self, key, val, TDB_MODIFY, 1);
}
static VALUE modify_bang(VALUE self, VALUE key, VALUE val)
{
return rbtdb_store(self, key, val, TDB_MODIFY, 0);
}
static VALUE nuke(VALUE self, VALUE key)
{
struct delete_args d;
d.tdb = db(self, 1);
TO_TDB_DATA(d.key, key);
return my_tbr(nogvl_delete, &d);
}
repacks a database to reduce fragmentation, available with tdb 1.2.x+
static VALUE repack(VALUE self)
{
struct tdb_context *tdb = db(self, 1);
if ((int)my_tbr((rb_blocking_function_t *)tdb_repack, tdb))
my_raise(tdb);
return self;
}
makes the current TDB object thread-safe (DANGEROUS) Do not use this method yet, it has problems
static VALUE trylockall(VALUE self)
{
struct tdb_context *tdb = db(self, 1);
void *fn = tdb_lockall_nonblock;
if ((int)my_tbr((rb_blocking_function_t *)fn, tdb)) {
if (tdb_error(tdb) == TDB_ERR_LOCK)
return Qfalse;
my_raise(tdb);
}
return Qtrue;
}
static VALUE trylockall_read(VALUE self)
{
struct tdb_context *tdb = db(self, 1);
void *fn = tdb_lockall_read_nonblock;
if ((int)my_tbr((rb_blocking_function_t *)fn, tdb)) {
if (tdb_error(tdb) == TDB_ERR_LOCK)
return Qfalse;
my_raise(tdb);
}
return Qtrue;
}
static VALUE unlockall(VALUE self)
{
struct tdb_context *tdb = db(self, 1);
if ((int)my_tbr((rb_blocking_function_t *)tdb_unlockall, tdb))
my_raise(tdb);
return Qtrue;
}
Originally generated with the Darkfish Rdoc Generator 2, modified by wrongdoc.