Class: LMDB::Database
- Inherits:
-
Object
- Object
- LMDB::Database
- Includes:
- Enumerable
- Defined in:
- lib/lmdb/database.rb,
ext/lmdb_ext/lmdb_ext.c
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #clear ⇒ Object
- #cursor ⇒ Object
- #delete(*args) ⇒ Object
- #drop ⇒ Object
- #each ⇒ Object
- #get(vkey) ⇒ Object
- #put(*args) ⇒ Object
- #size ⇒ Object
- #stat ⇒ Object
Instance Method Details
#[](key) ⇒ Object
13 14 15 |
# File 'lib/lmdb/database.rb', line 13 def [](key) get(key) end |
#[]=(key, value) ⇒ Object
17 18 19 20 |
# File 'lib/lmdb/database.rb', line 17 def []=(key, value) put(key, value) value end |
#clear ⇒ Object
417 418 419 420 421 422 423 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 417
static VALUE database_clear(VALUE self) {
DATABASE(self, database);
if (!active_txn(database->env))
return call_with_transaction(database->env, self, "clear", 0, 0, 0);
check(mdb_drop(need_txn(database->env), database->dbi, 0));
return Qnil;
}
|
#cursor ⇒ Object
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 525
static VALUE database_cursor(VALUE self) {
DATABASE(self, database);
if (!active_txn(database->env))
return call_with_transaction(database->env, self, "cursor", 0, 0, 0);
MDB_cursor* cur;
check(mdb_cursor_open(need_txn(database->env), database->dbi, &cur));
Cursor* cursor;
VALUE vcur = Data_Make_Struct(cCursor, Cursor, cursor_mark, cursor_free, cursor);
cursor->cur = cur;
cursor->db = self;
if (rb_block_given_p()) {
int exception;
VALUE ret = rb_protect(rb_yield, vcur, &exception);
if (exception) {
cursor_close(vcur);
rb_jump_tag(exception);
}
cursor_close(vcur);
return ret;
}
return vcur;
}
|
#delete(*args) ⇒ Object
473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 473
static VALUE database_delete(int argc, VALUE *argv, VALUE self) {
DATABASE(self, database);
if (!active_txn(database->env))
return call_with_transaction(database->env, self, "delete", argc, argv, 0);
VALUE vkey, vval;
rb_scan_args(argc, argv, "11", &vkey, &vval);
vkey = StringValue(vkey);
MDB_val key;
key.mv_size = RSTRING_LEN(vkey);
key.mv_data = RSTRING_PTR(vkey);
if (NIL_P(vval)) {
check(mdb_del(need_txn(database->env), database->dbi, &key, 0));
} else {
VALUE vval = StringValue(vval);
MDB_val value;
value.mv_size = RSTRING_LEN(vval);
value.mv_data = RSTRING_PTR(vval);
check(mdb_del(need_txn(database->env), database->dbi, &key, &value));
}
return Qnil;
}
|
#drop ⇒ Object
409 410 411 412 413 414 415 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 409
static VALUE database_drop(VALUE self) {
DATABASE(self, database);
if (!active_txn(database->env))
return call_with_transaction(database->env, self, "drop", 0, 0, 0);
check(mdb_drop(need_txn(database->env), database->dbi, 1));
return Qnil;
}
|
#each ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/lmdb/database.rb', line 5 def each cursor do |c| while i = c.next yield(i) end end end |
#get(vkey) ⇒ Object
425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 425
static VALUE database_get(VALUE self, VALUE vkey) {
DATABASE(self, database);
if (!active_txn(database->env))
return call_with_transaction(database->env, self, "get", 1, &vkey, MDB_RDONLY);
vkey = StringValue(vkey);
MDB_val key, value;
key.mv_size = RSTRING_LEN(vkey);
key.mv_data = RSTRING_PTR(vkey);
int ret = mdb_get(need_txn(database->env), database->dbi, &key, &value);
if (ret == MDB_NOTFOUND)
return Qnil;
check(ret);
return rb_str_new(value.mv_data, value.mv_size);
}
|
#put(*args) ⇒ Object
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 448
static VALUE database_put(int argc, VALUE *argv, VALUE self) {
DATABASE(self, database);
if (!active_txn(database->env))
return call_with_transaction(database->env, self, "put", argc, argv, 0);
VALUE vkey, vval, option_hash;
rb_scan_args(argc, argv, "2:", &vkey, &vval, &option_hash);
int flags = 0;
if (!NIL_P(option_hash))
rb_hash_foreach(option_hash, database_put_flags, (VALUE)&flags);
vkey = StringValue(vkey);
vval = StringValue(vval);
MDB_val key, value;
key.mv_size = RSTRING_LEN(vkey);
key.mv_data = RSTRING_PTR(vkey);
value.mv_size = RSTRING_LEN(vval);
value.mv_data = RSTRING_PTR(vval);
check(mdb_put(need_txn(database->env), database->dbi, &key, &value, flags));
return Qnil;
}
|
#size ⇒ Object
22 23 24 |
# File 'lib/lmdb/database.rb', line 22 def size stat[:entries] end |
#stat ⇒ Object
399 400 401 402 403 404 405 406 407 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 399
static VALUE database_stat(VALUE self) {
DATABASE(self, database);
if (!active_txn(database->env))
return call_with_transaction(database->env, self, "stat", 0, 0, MDB_RDONLY);
MDB_stat stat;
check(mdb_stat(need_txn(database->env), database->dbi, &stat));
return stat2hash(&stat);
}
|