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
443 444 445 446 447 448 449 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 443
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
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 550
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;
++database->refcount;
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
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 499
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
435 436 437 438 439 440 441 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 435
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
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 451
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
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 474
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
425 426 427 428 429 430 431 432 433 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 425
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);
}
|