Class: TDB

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
ext/tdb/tdb.c,
lib/tdb.rb,
ext/tdb/tdb.c

Overview

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

Defined Under Namespace

Modules: MT Classes: ERR

Constant Summary collapse

HASHES =

Available hash functions, the key is the name of the hash and the value is a pointer for internal for usage.

hashes
DEFAULT =

just a readability place holder

UINT2NUM(TDB_DEFAULT)
CLEAR_IF_FIRST =

clear database if we are the only one with it open

UINT2NUM(TDB_CLEAR_IF_FIRST)
INTERNAL =

don’t store on disk, use in-memory database

UINT2NUM(TDB_INTERNAL)
NOLOCK =

don’t do any locking

UINT2NUM(TDB_NOLOCK)
NOMMAP =

don’t use mmap

UINT2NUM(TDB_NOMMAP)
CONVERT =

convert endian (internal use)

UINT2NUM(TDB_CONVERT)
BIGENDIAN =

header is big-endian (internal use)

UINT2NUM(TDB_BIGENDIAN)
NOSYNC =

don’t use synchronous transactions

UINT2NUM(TDB_NOSYNC)
SEQNUM =

maintain a sequence number

UINT2NUM(TDB_SEQNUM)
VOLATILE =

Activate the per-hashchain freelist, default 5

UINT2NUM(TDB_VOLATILE)
ALLOW_NESTING =

Allow transactions to nest

UINT2NUM(TDB_ALLOW_NESTING)
DISALLOW_NESTING =

Disallow transactions to nest

UINT2NUM(TDB_DISALLOW_NESTING)
INCOMPATIBLE_HASH =

Better hashing, but can’t be opened by tdb < 1.2.6.

UINT2NUM(TDB_INCOMPATIBLE_HASH)

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object

:call-seq:

TDB.new(“/path/to/file”) -> TDB TDB.new(“/path/to/file”, :hash_size => 666) -> TDB TDB.new(“/path/to/file”, :hash => :murmur2) -> TDB TDB.new(“/path/to/file”, :open_flags => IO::RDONLY) -> TDB TDB.new(“/path/to/file”, :tdb_flags => TDB::NOSYNC) -> TDB

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)



240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
# File 'ext/tdb/tdb.c', line 240

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;
}

Instance Method Details

#[](key) ⇒ Object



359
360
361
362
363
364
365
366
367
368
# File 'ext/tdb/tdb.c', line 359

static VALUE fetch(VALUE self, VALUE key)
{
	struct fetch_parse_args f;

	f.tdb = db(self, 1);
	TO_TDB_DATA(f.as.key, key);
	f.value = Qnil;

	return my_tbr(nogvl_parse_record, &f);
}

#[]=(key, val) ⇒ Object



408
409
410
411
# File 'ext/tdb/tdb.c', line 408

static VALUE store(VALUE self, VALUE key, VALUE val)
{
	return rbtdb_store(self, key, val, 0, 0);
}

#clearObject

clears out the database



625
626
627
628
629
630
631
# File 'ext/tdb/tdb.c', line 625

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;
}

#closeObject



281
282
283
284
285
286
287
288
289
290
291
# File 'ext/tdb/tdb.c', line 281

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;
}

#closed?Boolean

Returns:

  • (Boolean)


293
294
295
296
297
298
# File 'ext/tdb/tdb.c', line 293

static VALUE closed(VALUE self)
{
	struct tdb_context *tdb = db(self, 0);

	return tdb ? Qfalse : Qtrue;
}

#delete(key) ⇒ Object



538
539
540
541
542
543
544
545
546
# File 'ext/tdb/tdb.c', line 538

static VALUE delete(VALUE self, VALUE key)
{
	VALUE rc = fetch(self, key);

	if (! NIL_P(rc))
		if (nuke(self, key) == Qfalse)
			return Qnil;
	return rc;
}

#eachObject



503
504
505
506
507
508
509
510
511
512
513
514
# File 'ext/tdb/tdb.c', line 503

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;
}

#fetch(key) ⇒ Object



359
360
361
362
363
364
365
366
367
368
# File 'ext/tdb/tdb.c', line 359

static VALUE fetch(VALUE self, VALUE key)
{
	struct fetch_parse_args f;

	f.tdb = db(self, 1);
	TO_TDB_DATA(f.as.key, key);
	f.value = Qnil;

	return my_tbr(nogvl_parse_record, &f);
}

#has_key?(key) ⇒ Boolean

Returns:

  • (Boolean)


445
446
447
448
449
450
451
452
453
# File 'ext/tdb/tdb.c', line 445

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);
}

#include?(key) ⇒ Boolean

Returns:

  • (Boolean)


445
446
447
448
449
450
451
452
453
# File 'ext/tdb/tdb.c', line 445

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);
}

#insert(key, val) ⇒ Object



418
419
420
421
# File 'ext/tdb/tdb.c', line 418

static VALUE insert(VALUE self, VALUE key, VALUE val)
{
	return rbtdb_store(self, key, val, TDB_INSERT, 1);
}

#insert!(key, val) ⇒ Object



413
414
415
416
# File 'ext/tdb/tdb.c', line 413

static VALUE insert_bang(VALUE self, VALUE key, VALUE val)
{
	return rbtdb_store(self, key, val, TDB_INSERT, 0);
}

#key?(key) ⇒ Boolean

Returns:

  • (Boolean)


445
446
447
448
449
450
451
452
453
# File 'ext/tdb/tdb.c', line 445

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);
}

#lockallObject



548
549
550
551
552
553
554
555
# File 'ext/tdb/tdb.c', line 548

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;
}

#lockall_markObject



606
607
608
609
610
611
612
# File 'ext/tdb/tdb.c', line 606

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;
}

#lockall_readObject



578
579
580
581
582
583
584
# File 'ext/tdb/tdb.c', line 578

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;
}

#lockall_unmarkObject



614
615
616
617
618
619
620
# File 'ext/tdb/tdb.c', line 614

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;
}

#member?(key) ⇒ Boolean

Returns:

  • (Boolean)


445
446
447
448
449
450
451
452
453
# File 'ext/tdb/tdb.c', line 445

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);
}

#modify(key, val) ⇒ Object



428
429
430
431
# File 'ext/tdb/tdb.c', line 428

static VALUE modify(VALUE self, VALUE key, VALUE val)
{
	return rbtdb_store(self, key, val, TDB_MODIFY, 1);
}

#modify!(key, val) ⇒ Object



423
424
425
426
# File 'ext/tdb/tdb.c', line 423

static VALUE modify_bang(VALUE self, VALUE key, VALUE val)
{
	return rbtdb_store(self, key, val, TDB_MODIFY, 0);
}

#nuke!(key) ⇒ Object



528
529
530
531
532
533
534
535
536
# File 'ext/tdb/tdb.c', line 528

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);
}

#repackObject

repacks a database to reduce fragmentation, available with tdb 1.2.x+



635
636
637
638
639
640
641
# File 'ext/tdb/tdb.c', line 635

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;
}

#store(key, val) ⇒ Object



408
409
410
411
# File 'ext/tdb/tdb.c', line 408

static VALUE store(VALUE self, VALUE key, VALUE val)
{
	return rbtdb_store(self, key, val, 0, 0);
}

#threadsafe!Object

makes the current TDB object thread-safe



7
8
9
# File 'lib/tdb.rb', line 7

def threadsafe!
  extend MT
end

#threadsafe?Boolean

will return true when TDB::MT is included in TDB or the TDB object is extended by TDB

Returns:

  • (Boolean)


13
14
15
# File 'lib/tdb.rb', line 13

def threadsafe?
  false
end

#trylockallObject



557
558
559
560
561
562
563
564
565
566
567
568
# File 'ext/tdb/tdb.c', line 557

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;
}

#trylockall_readObject



586
587
588
589
590
591
592
593
594
595
596
# File 'ext/tdb/tdb.c', line 586

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;
}

#unlockallObject



570
571
572
573
574
575
576
# File 'ext/tdb/tdb.c', line 570

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;
}

#unlockall_readObject



598
599
600
601
602
603
604
# File 'ext/tdb/tdb.c', line 598

static VALUE unlockall_read(VALUE self)
{
	struct tdb_context *tdb = db(self, 1);
	if ((int)my_tbr((rb_blocking_function_t *)tdb_unlockall_read, tdb))
		my_raise(tdb);
	return Qtrue;
}