Class: LMDB::Environment
- Inherits:
-
Object
- Object
- LMDB::Environment
- Defined in:
- ext/lmdb_ext/lmdb_ext.c,
ext/lmdb_ext/lmdb_ext.c
Overview
The Environment is the root object for all LMDB operations.
An LMDB “environment” is a collection of one or more “databases” (key-value tables), along with transactions to modify those databases and cursors to iterate through them.
An environment – and its collection of databases – is normally stored in a directory. That directory will contain two files:
-
data.mdb
: all the records in all the databases in the environment -
lock.mdb
: state of transactions that may be going on in the environment.
An environment can contain multiple databases. Each of the databases has a string name (“mydatabase”, “db.3.1982”). You use the database name to open the database within the environment.
Class Method Summary collapse
-
.new(path, opts) {|env| ... } ⇒ Environment
Open an LMDB database environment.
Instance Method Summary collapse
- #active_txn ⇒ Transaction
-
#clear_flags(flags) ⇒ Object
Clear one or more flags in the environment.
-
#close ⇒ Object
Close an environment, completing all IOs and cleaning up database state if needed.
-
#copy(path) ⇒ Object
Create a copy (snapshot) of an environment.
-
#database(name, options) ⇒ Database
Opens a database within the environment.
-
#flags ⇒ Array
Return the flags that are set in this environment.
-
#info ⇒ Hash
Return useful information about an environment.
- #mapsize=(size) ⇒ Object
-
#path ⇒ String
Return the path to the database environment files.
-
#set_flags(flags) ⇒ Object
Set one or more flags in the environment.
-
#stat ⇒ Hash
Return useful statistics about an environment.
-
#sync(force) ⇒ Object
Flush the data buffers to disk.
-
#transaction(readonly) {|txn| ... } ⇒ Object
Begin a transaction.
Class Method Details
.new(path, opts) {|env| ... } ⇒ Environment
471 472 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 499 500 501 502 503 504 505 506 507 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 471
static VALUE environment_new(int argc, VALUE *argv, VALUE klass) {
VALUE path, option_hash;
rb_scan_args(argc, argv, "1:", &path, &option_hash);
EnvironmentOptions options = {
.flags = MDB_NOTLS,
.maxreaders = -1,
.maxdbs = 128,
.mapsize = 0,
.mode = 0755,
};
if (!NIL_P(option_hash))
rb_hash_foreach(option_hash, environment_options, (VALUE)&options);
MDB_env* env;
check(mdb_env_create(&env));
Environment* environment;
VALUE venv = Data_Make_Struct(cEnvironment, Environment, environment_mark, environment_free, environment);
environment->env = env;
environment->thread_txn_hash = rb_hash_new();
environment->txn_thread_hash = rb_hash_new();
if (options.maxreaders > 0)
check(mdb_env_set_maxreaders(env, options.maxreaders));
if (options.mapsize > 0)
check(mdb_env_set_mapsize(env, options.mapsize));
check(mdb_env_set_maxdbs(env, options.maxdbs <= 0 ? 1 : options.maxdbs));
VALUE expanded_path = rb_file_expand_path(path, Qnil);
check(mdb_env_open(env, StringValueCStr(expanded_path), options.flags, options.mode));
if (rb_block_given_p())
return rb_ensure(rb_yield, venv, environment_close, venv);
return venv;
}
|
Instance Method Details
#active_txn ⇒ Transaction
613 614 615 616 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 613
static VALUE environment_active_txn(VALUE self) {
ENVIRONMENT(self, environment);
return rb_hash_aref(environment->thread_txn_hash, rb_thread_current());
}
|
#clear_flags(flags) ⇒ Object
599 600 601 602 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 599
static VALUE environment_clear_flags(int argc, VALUE* argv, VALUE self) {
environment_change_flags(argc, argv, self, 0);
return Qnil;
}
|
#close ⇒ Object
300 301 302 303 304 305 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 300
static VALUE environment_close(VALUE self) {
ENVIRONMENT(self, environment);
mdb_env_close(environment->env);
environment->env = 0;
return Qnil;
}
|
#copy(path) ⇒ Object
383 384 385 386 387 388 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 383
static VALUE environment_copy(VALUE self, VALUE path) {
ENVIRONMENT(self, environment);
VALUE expanded_path = rb_file_expand_path(path, Qnil);
check(mdb_env_copy(environment->env, StringValueCStr(expanded_path)));
return Qnil;
}
|
#database(name, options) ⇒ Database
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 742
static VALUE environment_database(int argc, VALUE *argv, VALUE self) {
ENVIRONMENT(self, environment);
if (!active_txn(self))
return call_with_transaction(self, self, "database", argc, argv, 0);
VALUE name, option_hash;
rb_scan_args(argc, argv, "01:", &name, &option_hash);
int flags = 0;
if (!NIL_P(option_hash))
rb_hash_foreach(option_hash, database_flags, (VALUE)&flags);
MDB_dbi dbi;
check(mdb_dbi_open(need_txn(self), NIL_P(name) ? 0 : StringValueCStr(name), flags, &dbi));
Database* database;
VALUE vdb = Data_Make_Struct(cDatabase, Database, database_mark, free, database);
database->dbi = dbi;
database->env = self;
return vdb;
}
|
#flags ⇒ Array
526 527 528 529 530 531 532 533 534 535 536 537 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 526
static VALUE environment_flags(VALUE self) {
unsigned int flags;
ENVIRONMENT(self, environment);
check(mdb_env_get_flags(environment->env, &flags));
VALUE ret = rb_ary_new();
#define FLAG(const, name) if (flags & MDB_##const) rb_ary_push(ret, ID2SYM(rb_intern(#name)));
#include "env_flags.h"
#undef FLAG
return ret;
}
|
#info ⇒ Hash
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 351
static VALUE environment_info(VALUE self) {
MDB_envinfo info;
ENVIRONMENT(self, environment);
check(mdb_env_info(environment->env, &info));
VALUE ret = rb_hash_new();
#define INFO_SET(name) rb_hash_aset(ret, ID2SYM(rb_intern(#name)), INT2NUM((size_t)info.me_##name));
INFO_SET(mapaddr);
INFO_SET(mapsize);
INFO_SET(last_pgno);
INFO_SET(last_txnid);
INFO_SET(maxreaders);
INFO_SET(numreaders);
#undef INFO_SET
return ret;
}
|
#mapsize=(size) ⇒ Object
551 552 553 554 555 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 551
static VALUE environment_set_mapsize(VALUE self, VALUE size) {
ENVIRONMENT(self, environment);
check(mdb_env_set_mapsize(environment->env, NUM2LONG(size)));
return Qnil;
}
|
#path ⇒ String
544 545 546 547 548 549 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 544
static VALUE environment_path(VALUE self) {
const char* path;
ENVIRONMENT(self, environment);
check(mdb_env_get_path(environment->env, &path));
return rb_str_new2(path);
}
|
#set_flags(flags) ⇒ Object
584 585 586 587 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 584
static VALUE environment_set_flags(int argc, VALUE* argv, VALUE self) {
environment_change_flags(argc, argv, self, 1);
return Qnil;
}
|
#stat ⇒ Hash
333 334 335 336 337 338 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 333
static VALUE environment_stat(VALUE self) {
ENVIRONMENT(self, environment);
MDB_stat stat;
check(mdb_env_stat(environment->env, &stat));
return stat2hash(&stat);
}
|
#sync(force) ⇒ Object
403 404 405 406 407 408 409 410 411 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 403
static VALUE environment_sync(int argc, VALUE *argv, VALUE self) {
ENVIRONMENT(self, environment);
VALUE force;
rb_scan_args(argc, argv, "01", &force);
check(mdb_env_sync(environment->env, RTEST(force)));
return Qnil;
}
|
#transaction(readonly) {|txn| ... } ⇒ Object
685 686 687 688 689 690 691 692 693 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 685
static VALUE environment_transaction(int argc, VALUE *argv, VALUE self) {
rb_need_block();
VALUE readonly;
rb_scan_args(argc, argv, "01", &readonly);
unsigned int flags = RTEST(readonly) ? MDB_RDONLY : 0;
return with_transaction(self, rb_yield, Qnil, flags);
}
|