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
-
#[] ⇒ Database
Accesses an existing database.
- #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.
-
#databases ⇒ Array
Returns a list of the names of all databases in the environment, or an empty list if there are no named databases but only the defaut database.
-
#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.
- #reader_check ⇒ Object
- #reader_list ⇒ Object
-
#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
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 965
static VALUE environment_new(int argc, VALUE *argv, VALUE klass) {
VALUE path, option_hash;
#ifdef RB_SCAN_ARGS_KEYWORDS
rb_scan_args_kw(RB_SCAN_ARGS_LAST_HASH_KEYWORDS,
argc, argv, "1:", &path, &option_hash);
#else
rb_scan_args(argc, argv, "1:", &path, &option_hash);
#endif
EnvironmentOptions options = {
// .flags = MDB_NOTLS,
.flags = 0,
.maxreaders = -1,
.maxdbs = 128,
.mapsize = 0,
.mode = 0755,
};
if (!NIL_P(option_hash))
rb_hash_foreach(option_hash, (int (*)(ANYARGS))environment_options,
(VALUE)&options);
MDB_env* env;
check(mdb_env_create(&env));
Environment* environment;
VALUE venv = TypedData_Make_Struct(cEnvironment, Environment,
&lmdb_environment_type, environment);
environment->env = env;
environment->thread_txn_hash = rb_hash_new();
environment->txn_thread_hash = rb_hash_new();
environment->rw_txn_thread = Qnil;
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
#[] ⇒ Database
Returns a database handle.
1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 1367
static VALUE environment_database_aref(VALUE self, VALUE name) {
// If they pass nil explicitly, treat it as zero arguments (anonymous database)
int argc = NIL_P(name) ? 0 : 1;
// Package it up into an argv array
VALUE argv[1];
argv[0] = name;
// Call your existing function directly
return environment_database(argc, argv, self);
}
|
#active_txn ⇒ Transaction
1118 1119 1120 1121 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 1118
static VALUE environment_active_txn(VALUE self) {
ENVIRONMENT(self, &lmdb_environment_type, environment);
return rb_hash_aref(environment->thread_txn_hash, rb_thread_current());
}
|
#clear_flags(flags) ⇒ Object
1104 1105 1106 1107 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 1104
static VALUE environment_clear_flags(int argc, VALUE* argv, VALUE self) {
environment_change_flags(argc, argv, self, 0);
return Qnil;
}
|
#close ⇒ Object
758 759 760 761 762 763 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 758
static VALUE environment_close(VALUE self) {
ENVIRONMENT(self, &lmdb_environment_type, environment);
mdb_env_close(environment->env);
environment->env = 0;
return Qnil;
}
|
#copy(path) ⇒ Object
877 878 879 880 881 882 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 877
static VALUE environment_copy(VALUE self, VALUE path) {
ENVIRONMENT(self, &lmdb_environment_type, 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
1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 1318
static VALUE environment_database(int argc, VALUE *argv, VALUE self) {
ENVIRONMENT(self, &lmdb_environment_type, environment);
if (!active_txn(self))
return call_with_transaction(self, self, "database", argc, argv, 0);
VALUE name = Qnil;
VALUE option_hash = Qnil;
rb_scan_args_kw(RB_SCAN_ARGS_LAST_HASH_KEYWORDS,
argc, argv, "01:", &name, &option_hash);
int flags = 0;
if (!NIL_P(option_hash))
rb_hash_foreach(option_hash, (int (*)(ANYARGS))database_flags,
(VALUE)&flags);
MDB_dbi dbi;
if (name && !NIL_P(name)) {
if (RB_TYPE_P(name, T_SYMBOL))
name = rb_sym2str(name);
Check_Type(name, T_STRING);
}
check(mdb_dbi_open(need_txn(self), NIL_P(name) ? 0 : StringValueCStr(name),
flags, &dbi));
Database* database;
VALUE vdb = TypedData_Make_Struct(cDatabase, Database,
&lmdb_database_type, database);
database->dbi = dbi;
database->env = self;
return vdb;
}
|
#databases ⇒ Array
Returns The names of all databases in the environment.
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 1387
static VALUE environment_databases(VALUE self) {
ENVIRONMENT(self, &lmdb_environment_type, environment);
if (!active_txn(self))
return call_with_transaction(self, self, "databases", 0, 0, MDB_RDONLY);
MDB_dbi dbi;
MDB_cursor *cursor;
MDB_txn *txn = need_txn(self);
MDB_val key;
check(mdb_dbi_open(txn, NULL, 0, &dbi));
check(mdb_cursor_open(txn, dbi, &cursor));
VALUE ret = rb_ary_new();
while (mdb_cursor_get(cursor, &key, NULL, MDB_NEXT_NODUP) == MDB_SUCCESS) {
char *intern_db_name;
MDB_dbi db;
VALUE db_name;
if (memchr(key.mv_data, '\0', key.mv_size))
continue;
intern_db_name = malloc(key.mv_size + 1);
memcpy(intern_db_name, key.mv_data, key.mv_size);
intern_db_name[key.mv_size] = '\0';
if (mdb_dbi_open(txn, intern_db_name, 0, &db) == MDB_SUCCESS) {
mdb_dbi_close(environment->env, db);
db_name = rb_str_new(key.mv_data, key.mv_size);
rb_ary_push(ret, db_name);
}
xfree(intern_db_name);
}
mdb_cursor_close(cursor);
return ret;
}
|
#flags ⇒ Array
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 1031
static VALUE environment_flags(VALUE self) {
unsigned int flags;
ENVIRONMENT(self, &lmdb_environment_type, 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
845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 845
static VALUE environment_info(VALUE self) {
MDB_envinfo info;
ENVIRONMENT(self, &lmdb_environment_type, 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)), SIZET2NUM((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
1056 1057 1058 1059 1060 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 1056
static VALUE environment_set_mapsize(VALUE self, VALUE size) {
ENVIRONMENT(self, &lmdb_environment_type, environment);
check(mdb_env_set_mapsize(environment->env, NUM2LONG(size)));
return Qnil;
}
|
#path ⇒ String
1049 1050 1051 1052 1053 1054 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 1049
static VALUE environment_path(VALUE self) {
const char* path;
ENVIRONMENT(self, &lmdb_environment_type, environment);
check(mdb_env_get_path(environment->env, &path));
return rb_str_new2(path);
}
|
#reader_check ⇒ Object
782 783 784 785 786 787 788 789 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 782
static VALUE environment_reader_check(VALUE self) {
ENVIRONMENT(self, &lmdb_environment_type, environment);
int dead = 0;
mdb_reader_check(environment->env, &dead);
return INT2NUM(dead);
}
|
#reader_list ⇒ Object
771 772 773 774 775 776 777 778 779 780 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 771
static VALUE environment_reader_list(VALUE self) {
ENVIRONMENT(self, &lmdb_environment_type, environment);
VALUE ret = rb_ary_new();
mdb_reader_list(environment->env, reader_list_callback, (void*)ret);
//return Qnil;
return ret;
}
|
#set_flags(flags) ⇒ Object
1089 1090 1091 1092 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 1089
static VALUE environment_set_flags(int argc, VALUE* argv, VALUE self) {
environment_change_flags(argc, argv, self, 1);
return Qnil;
}
|
#stat ⇒ Hash
827 828 829 830 831 832 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 827
static VALUE environment_stat(VALUE self) {
ENVIRONMENT(self, &lmdb_environment_type, environment);
MDB_stat stat;
check(mdb_env_stat(environment->env, &stat));
return stat2hash(&stat);
}
|
#sync(force) ⇒ Object
897 898 899 900 901 902 903 904 905 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 897
static VALUE environment_sync(int argc, VALUE *argv, VALUE self) {
ENVIRONMENT(self, &lmdb_environment_type, environment);
VALUE force;
rb_scan_args(argc, argv, "01", &force);
check(mdb_env_sync(environment->env, RTEST(force)));
return Qnil;
}
|
#transaction(readonly) {|txn| ... } ⇒ Object
1221 1222 1223 1224 1225 1226 1227 1228 1229 |
# File 'ext/lmdb_ext/lmdb_ext.c', line 1221
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);
}
|