2020-11-30 05:57:26 +00:00
|
|
|
/* vim: set noet sta sw=4 ts=4 : */
|
|
|
|
|
|
|
|
|
|
#include "mdbx_ext.h"
|
|
|
|
|
|
|
|
|
|
/* Shortcut for fetching current DB variables.
|
|
|
|
|
*/
|
|
|
|
|
#define UNWRAP_DB( val, db ) \
|
|
|
|
|
rmdbx_db_t *db; \
|
|
|
|
|
TypedData_Get_Struct( val, rmdbx_db_t, &rmdbx_db_data, db );
|
|
|
|
|
|
|
|
|
|
|
2020-12-23 01:10:19 +00:00
|
|
|
VALUE rmdbx_cDatabase;
|
2020-11-30 05:57:26 +00:00
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Ruby allocation hook.
|
|
|
|
|
*/
|
|
|
|
|
static const rb_data_type_t rmdbx_db_data = {
|
|
|
|
|
.wrap_struct_name = "MDBX::Database::Data",
|
|
|
|
|
.function = { .dfree = rmdbx_free },
|
|
|
|
|
.flags = RUBY_TYPED_FREE_IMMEDIATELY
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Allocate a DB environment onto the stack.
|
|
|
|
|
*/
|
|
|
|
|
VALUE
|
|
|
|
|
rmdbx_alloc( VALUE klass )
|
|
|
|
|
{
|
2020-12-04 19:07:34 +00:00
|
|
|
rmdbx_db_t *new = RB_ALLOC( rmdbx_db_t );
|
|
|
|
|
return TypedData_Make_Struct( klass, rmdbx_db_t, &rmdbx_db_data, new );
|
2020-12-01 07:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Ensure all database file descriptors are collected and
|
|
|
|
|
* removed.
|
|
|
|
|
*/
|
|
|
|
|
void
|
2020-12-23 01:10:19 +00:00
|
|
|
rmdbx_close_all( rmdbx_db_t *db )
|
2020-12-01 07:58:49 +00:00
|
|
|
{
|
|
|
|
|
if ( db->cursor ) mdbx_cursor_close( db->cursor );
|
|
|
|
|
if ( db->txn ) mdbx_txn_abort( db->txn );
|
|
|
|
|
if ( db->dbi ) mdbx_dbi_close( db->env, db->dbi );
|
|
|
|
|
if ( db->env ) mdbx_env_close( db->env );
|
2020-12-23 01:10:19 +00:00
|
|
|
db->state.open = 0;
|
2020-11-30 05:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-01-25 00:36:40 +00:00
|
|
|
/*
|
|
|
|
|
* Close any open database handle. Will be automatically
|
|
|
|
|
* re-opened on next transaction. This is primarily useful for
|
|
|
|
|
* switching between subdatabases.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
rmdbx_close_dbi( rmdbx_db_t *db )
|
|
|
|
|
{
|
|
|
|
|
if ( ! db->dbi ) return;
|
|
|
|
|
mdbx_dbi_close( db->env, db->dbi );
|
|
|
|
|
db->dbi = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-11-30 05:57:26 +00:00
|
|
|
/*
|
|
|
|
|
* Cleanup a previously allocated DB environment.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
rmdbx_free( void *db )
|
|
|
|
|
{
|
2020-12-01 07:58:49 +00:00
|
|
|
if ( db ) {
|
2020-12-16 08:29:50 +00:00
|
|
|
rmdbx_close_all( db );
|
2021-02-14 09:47:04 +00:00
|
|
|
xfree( db );
|
2020-12-01 07:58:49 +00:00
|
|
|
}
|
2020-11-30 05:57:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
2020-12-16 08:29:50 +00:00
|
|
|
* Cleanly close an opened database from Ruby.
|
2020-11-30 05:57:26 +00:00
|
|
|
*/
|
|
|
|
|
VALUE
|
|
|
|
|
rmdbx_close( VALUE self )
|
|
|
|
|
{
|
|
|
|
|
UNWRAP_DB( self, db );
|
2020-12-16 08:29:50 +00:00
|
|
|
rmdbx_close_all( db );
|
2020-11-30 05:57:26 +00:00
|
|
|
return Qtrue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-12-23 01:10:19 +00:00
|
|
|
/*
|
|
|
|
|
* call-seq:
|
2021-02-14 09:47:04 +00:00
|
|
|
* db.closed? => false
|
2020-12-23 01:10:19 +00:00
|
|
|
*
|
|
|
|
|
* Predicate: return true if the database environment is closed.
|
|
|
|
|
*/
|
|
|
|
|
VALUE
|
|
|
|
|
rmdbx_closed_p( VALUE self )
|
|
|
|
|
{
|
|
|
|
|
UNWRAP_DB( self, db );
|
|
|
|
|
return db->state.open == 1 ? Qfalse : Qtrue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-01-25 00:36:40 +00:00
|
|
|
/*
|
|
|
|
|
* call-seq:
|
2021-02-14 09:47:04 +00:00
|
|
|
* db.in_transaction? => false
|
2021-01-25 00:36:40 +00:00
|
|
|
*
|
|
|
|
|
* Predicate: return true if a transaction (or snapshot)
|
|
|
|
|
* is currently open.
|
|
|
|
|
*/
|
|
|
|
|
VALUE
|
|
|
|
|
rmdbx_in_transaction_p( VALUE self )
|
|
|
|
|
{
|
|
|
|
|
UNWRAP_DB( self, db );
|
|
|
|
|
return db->txn ? Qtrue : Qfalse;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-12-17 01:09:43 +00:00
|
|
|
/*
|
|
|
|
|
* Open the DB environment handle.
|
2021-02-14 09:47:04 +00:00
|
|
|
*
|
2020-12-17 01:09:43 +00:00
|
|
|
*/
|
|
|
|
|
VALUE
|
|
|
|
|
rmdbx_open_env( VALUE self )
|
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
UNWRAP_DB( self, db );
|
|
|
|
|
rmdbx_close_all( db );
|
|
|
|
|
|
|
|
|
|
/* Allocate an mdbx environment.
|
|
|
|
|
*/
|
|
|
|
|
rc = mdbx_env_create( &db->env );
|
|
|
|
|
if ( rc != MDBX_SUCCESS )
|
|
|
|
|
rb_raise( rmdbx_eDatabaseError, "mdbx_env_create: (%d) %s", rc, mdbx_strerror(rc) );
|
|
|
|
|
|
|
|
|
|
/* Set the maximum number of named databases for the environment. */
|
2020-12-23 01:10:19 +00:00
|
|
|
mdbx_env_set_maxdbs( db->env, db->settings.max_collections );
|
|
|
|
|
|
|
|
|
|
/* Customize the maximum number of simultaneous readers. */
|
|
|
|
|
if ( db->settings.max_readers )
|
|
|
|
|
mdbx_env_set_maxreaders( db->env, db->settings.max_readers );
|
2020-12-17 01:09:43 +00:00
|
|
|
|
2020-12-23 01:10:19 +00:00
|
|
|
/* Set an upper boundary (in bytes) for the database map size. */
|
2021-02-14 09:47:04 +00:00
|
|
|
if ( db->settings.max_size )
|
2020-12-23 01:10:19 +00:00
|
|
|
mdbx_env_set_geometry( db->env, -1, -1, db->settings.max_size, -1, -1, -1 );
|
|
|
|
|
|
|
|
|
|
rc = mdbx_env_open( db->env, db->path, db->settings.env_flags, db->settings.mode );
|
2020-12-17 01:09:43 +00:00
|
|
|
if ( rc != MDBX_SUCCESS ) {
|
2020-12-23 01:10:19 +00:00
|
|
|
rmdbx_close_all( db );
|
2020-12-17 01:09:43 +00:00
|
|
|
rb_raise( rmdbx_eDatabaseError, "mdbx_env_open: (%d) %s", rc, mdbx_strerror(rc) );
|
|
|
|
|
}
|
2020-12-23 01:10:19 +00:00
|
|
|
db->state.open = 1;
|
2020-12-17 01:09:43 +00:00
|
|
|
|
|
|
|
|
return Qtrue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-11-30 05:57:26 +00:00
|
|
|
/*
|
2020-12-23 01:10:19 +00:00
|
|
|
* Open a new database transaction. If a transaction is already
|
|
|
|
|
* open, this is a no-op.
|
2020-12-01 03:32:26 +00:00
|
|
|
*
|
|
|
|
|
* +rwflag+ must be either MDBX_TXN_RDONLY or MDBX_TXN_READWRITE.
|
|
|
|
|
*/
|
|
|
|
|
void
|
2020-12-23 01:10:19 +00:00
|
|
|
rmdbx_open_txn( rmdbx_db_t *db, int rwflag )
|
2020-12-01 03:32:26 +00:00
|
|
|
{
|
2020-12-23 01:10:19 +00:00
|
|
|
if ( db->txn ) return;
|
2020-12-01 03:32:26 +00:00
|
|
|
|
2020-12-23 01:10:19 +00:00
|
|
|
int rc = mdbx_txn_begin( db->env, NULL, rwflag, &db->txn);
|
2020-12-01 03:32:26 +00:00
|
|
|
if ( rc != MDBX_SUCCESS ) {
|
2020-12-23 01:10:19 +00:00
|
|
|
rmdbx_close_all( db );
|
2020-12-01 03:32:26 +00:00
|
|
|
rb_raise( rmdbx_eDatabaseError, "mdbx_txn_begin: (%d) %s", rc, mdbx_strerror(rc) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( db->dbi == 0 ) {
|
|
|
|
|
// FIXME: dbi_flags
|
2020-12-16 08:29:50 +00:00
|
|
|
rc = mdbx_dbi_open( db->txn, db->subdb, MDBX_CREATE, &db->dbi );
|
2020-12-01 03:32:26 +00:00
|
|
|
if ( rc != MDBX_SUCCESS ) {
|
2020-12-23 01:10:19 +00:00
|
|
|
rmdbx_close_all( db );
|
2020-12-01 03:32:26 +00:00
|
|
|
rb_raise( rmdbx_eDatabaseError, "mdbx_dbi_open: (%d) %s", rc, mdbx_strerror(rc) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-12-23 01:10:19 +00:00
|
|
|
/*
|
|
|
|
|
* Close any existing database transaction. If there is no
|
2021-01-25 00:36:40 +00:00
|
|
|
* active transaction, this is a no-op. If there is a long
|
|
|
|
|
* running transaction open, this is a no-op.
|
2020-12-23 01:10:19 +00:00
|
|
|
*
|
|
|
|
|
* +txnflag must either be RMDBX_TXN_ROLLBACK or RMDBX_TXN_COMMIT.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
rmdbx_close_txn( rmdbx_db_t *db, int txnflag )
|
|
|
|
|
{
|
2021-01-25 00:36:40 +00:00
|
|
|
if ( ! db->txn || db->state.retain_txn > -1 ) return;
|
2020-12-23 01:10:19 +00:00
|
|
|
|
|
|
|
|
switch ( txnflag ) {
|
|
|
|
|
case RMDBX_TXN_COMMIT:
|
|
|
|
|
mdbx_txn_commit( db->txn );
|
|
|
|
|
default:
|
|
|
|
|
mdbx_txn_abort( db->txn );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
db->txn = 0;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-01-25 00:36:40 +00:00
|
|
|
/*
|
|
|
|
|
* call-seq:
|
|
|
|
|
* db.open_transaction( mode )
|
|
|
|
|
*
|
|
|
|
|
* Open a new long-running transaction. If +mode+ is true,
|
|
|
|
|
* it is opened read/write.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
VALUE
|
|
|
|
|
rmdbx_rb_opentxn( VALUE self, VALUE mode )
|
|
|
|
|
{
|
|
|
|
|
UNWRAP_DB( self, db );
|
|
|
|
|
|
|
|
|
|
rmdbx_open_txn( db, RTEST(mode) ? MDBX_TXN_READWRITE : MDBX_TXN_RDONLY );
|
|
|
|
|
db->state.retain_txn = RTEST(mode) ? 1 : 0;
|
|
|
|
|
|
|
|
|
|
return Qtrue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* call-seq:
|
|
|
|
|
* db.close_transaction( mode )
|
|
|
|
|
*
|
|
|
|
|
* Close a long-running transaction. If +write+ is true,
|
|
|
|
|
* the transaction is committed. Otherwise, rolled back.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
VALUE
|
|
|
|
|
rmdbx_rb_closetxn( VALUE self, VALUE write )
|
|
|
|
|
{
|
|
|
|
|
UNWRAP_DB( self, db );
|
|
|
|
|
|
|
|
|
|
db->state.retain_txn = -1;
|
|
|
|
|
rmdbx_close_txn( db, RTEST(write) ? RMDBX_TXN_COMMIT : RMDBX_TXN_ROLLBACK );
|
|
|
|
|
|
|
|
|
|
return Qtrue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-12-16 08:29:50 +00:00
|
|
|
/*
|
|
|
|
|
* call-seq:
|
2020-12-17 01:09:43 +00:00
|
|
|
* db.clear
|
2020-12-16 08:29:50 +00:00
|
|
|
*
|
2021-02-14 09:47:04 +00:00
|
|
|
* Empty the current collection on disk. If collections are not enabled
|
|
|
|
|
* or the database handle is set to the top-level (main) db - this
|
|
|
|
|
* deletes *all data* on disk. Fair warning, this is not recoverable!
|
2020-12-16 08:29:50 +00:00
|
|
|
*/
|
|
|
|
|
VALUE
|
2020-12-17 01:09:43 +00:00
|
|
|
rmdbx_clear( VALUE self )
|
2020-12-16 08:29:50 +00:00
|
|
|
{
|
|
|
|
|
UNWRAP_DB( self, db );
|
2020-12-17 01:09:43 +00:00
|
|
|
|
2020-12-23 01:10:19 +00:00
|
|
|
rmdbx_open_txn( db, MDBX_TXN_READWRITE );
|
2020-12-16 08:29:50 +00:00
|
|
|
int rc = mdbx_drop( db->txn, db->dbi, true );
|
|
|
|
|
|
2020-12-22 06:24:18 +00:00
|
|
|
if ( rc != MDBX_SUCCESS )
|
2020-12-16 08:29:50 +00:00
|
|
|
rb_raise( rmdbx_eDatabaseError, "mdbx_drop: (%d) %s", rc, mdbx_strerror(rc) );
|
2020-12-17 01:09:43 +00:00
|
|
|
|
2020-12-23 01:10:19 +00:00
|
|
|
rmdbx_close_txn( db, RMDBX_TXN_COMMIT );
|
2020-12-17 01:09:43 +00:00
|
|
|
|
2020-12-22 06:24:18 +00:00
|
|
|
/* Refresh the environment handles. */
|
|
|
|
|
rmdbx_open_env( self );
|
2020-12-17 01:09:43 +00:00
|
|
|
|
2020-12-16 08:29:50 +00:00
|
|
|
return Qnil;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Given a ruby +arg+, convert and return a structure
|
|
|
|
|
* suitable for usage as a key for mdbx. All keys are explicitly
|
|
|
|
|
* converted to strings.
|
|
|
|
|
*/
|
|
|
|
|
MDBX_val
|
|
|
|
|
rmdbx_key_for( VALUE arg )
|
|
|
|
|
{
|
|
|
|
|
MDBX_val rv;
|
|
|
|
|
|
|
|
|
|
arg = rb_funcall( arg, rb_intern("to_s"), 0 );
|
|
|
|
|
rv.iov_len = RSTRING_LEN( arg );
|
|
|
|
|
rv.iov_base = StringValuePtr( arg );
|
|
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-12-01 03:32:26 +00:00
|
|
|
/*
|
|
|
|
|
* Given a ruby +arg+, convert and return a structure
|
2020-12-16 08:29:50 +00:00
|
|
|
* suitable for usage as a value for mdbx.
|
2020-12-01 03:32:26 +00:00
|
|
|
*/
|
|
|
|
|
MDBX_val
|
2020-12-16 08:29:50 +00:00
|
|
|
rmdbx_val_for( VALUE self, VALUE arg )
|
2020-12-01 03:32:26 +00:00
|
|
|
{
|
|
|
|
|
MDBX_val rv;
|
2020-12-16 08:29:50 +00:00
|
|
|
VALUE serialize_proc;
|
|
|
|
|
|
|
|
|
|
serialize_proc = rb_iv_get( self, "@serializer" );
|
|
|
|
|
if ( ! NIL_P( serialize_proc ) )
|
|
|
|
|
arg = rb_funcall( serialize_proc, rb_intern("call"), 1, arg );
|
2020-12-01 03:32:26 +00:00
|
|
|
|
|
|
|
|
rv.iov_len = RSTRING_LEN( arg );
|
|
|
|
|
rv.iov_base = StringValuePtr( arg );
|
|
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-12-17 01:09:43 +00:00
|
|
|
/* call-seq:
|
2021-02-14 09:47:04 +00:00
|
|
|
* db.keys => [ 'key1', 'key2', ... ]
|
2020-12-17 01:09:43 +00:00
|
|
|
*
|
|
|
|
|
* Return an array of all keys in the current collection.
|
|
|
|
|
*/
|
|
|
|
|
VALUE
|
|
|
|
|
rmdbx_keys( VALUE self )
|
|
|
|
|
{
|
|
|
|
|
UNWRAP_DB( self, db );
|
|
|
|
|
VALUE rv = rb_ary_new();
|
|
|
|
|
MDBX_val key, data;
|
|
|
|
|
int rc;
|
|
|
|
|
|
2020-12-23 01:10:19 +00:00
|
|
|
if ( ! db->state.open ) rb_raise( rmdbx_eDatabaseError, "Closed database." );
|
2020-12-17 01:09:43 +00:00
|
|
|
|
2020-12-23 01:10:19 +00:00
|
|
|
rmdbx_open_txn( db, MDBX_TXN_RDONLY );
|
2020-12-17 01:09:43 +00:00
|
|
|
rc = mdbx_cursor_open( db->txn, db->dbi, &db->cursor);
|
|
|
|
|
|
|
|
|
|
if ( rc != MDBX_SUCCESS ) {
|
|
|
|
|
rmdbx_close( self );
|
|
|
|
|
rb_raise( rmdbx_eDatabaseError, "Unable to open cursor: (%d) %s", rc, mdbx_strerror(rc) );
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-22 06:24:18 +00:00
|
|
|
rc = mdbx_cursor_get( db->cursor, &key, &data, MDBX_FIRST );
|
|
|
|
|
if ( rc == MDBX_SUCCESS ) {
|
2020-12-17 01:09:43 +00:00
|
|
|
rb_ary_push( rv, rb_str_new( key.iov_base, key.iov_len ) );
|
2020-12-22 06:24:18 +00:00
|
|
|
while ( mdbx_cursor_get( db->cursor, &key, &data, MDBX_NEXT ) == 0 ) {
|
|
|
|
|
rb_ary_push( rv, rb_str_new( key.iov_base, key.iov_len ) );
|
|
|
|
|
}
|
2020-12-17 01:09:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mdbx_cursor_close( db->cursor );
|
|
|
|
|
db->cursor = NULL;
|
2020-12-23 01:10:19 +00:00
|
|
|
rmdbx_close_txn( db, RMDBX_TXN_ROLLBACK );
|
2020-12-17 01:09:43 +00:00
|
|
|
return rv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-12-01 03:32:26 +00:00
|
|
|
/* call-seq:
|
2021-02-14 09:47:04 +00:00
|
|
|
* db[ 'key' ] => value
|
2020-12-01 03:32:26 +00:00
|
|
|
*
|
|
|
|
|
* Convenience method: return a single value for +key+ immediately.
|
|
|
|
|
*/
|
|
|
|
|
VALUE
|
|
|
|
|
rmdbx_get_val( VALUE self, VALUE key )
|
|
|
|
|
{
|
|
|
|
|
int rc;
|
2020-12-16 08:29:50 +00:00
|
|
|
VALUE deserialize_proc;
|
2020-12-01 03:32:26 +00:00
|
|
|
UNWRAP_DB( self, db );
|
|
|
|
|
|
2020-12-23 01:10:19 +00:00
|
|
|
if ( ! db->state.open ) rb_raise( rmdbx_eDatabaseError, "Closed database." );
|
2020-12-01 03:32:26 +00:00
|
|
|
|
2020-12-23 01:10:19 +00:00
|
|
|
rmdbx_open_txn( db, MDBX_TXN_RDONLY );
|
2020-12-01 03:32:26 +00:00
|
|
|
|
2020-12-16 08:29:50 +00:00
|
|
|
MDBX_val ckey = rmdbx_key_for( key );
|
2020-12-01 03:32:26 +00:00
|
|
|
MDBX_val data;
|
|
|
|
|
rc = mdbx_get( db->txn, db->dbi, &ckey, &data );
|
2020-12-23 01:10:19 +00:00
|
|
|
rmdbx_close_txn( db, RMDBX_TXN_ROLLBACK );
|
2020-12-01 03:32:26 +00:00
|
|
|
|
|
|
|
|
switch ( rc ) {
|
|
|
|
|
case MDBX_SUCCESS:
|
2020-12-16 08:29:50 +00:00
|
|
|
deserialize_proc = rb_iv_get( self, "@deserializer" );
|
2020-12-22 06:24:18 +00:00
|
|
|
VALUE rv = rb_str_new( data.iov_base, data.iov_len );
|
|
|
|
|
if ( ! NIL_P( deserialize_proc ) )
|
|
|
|
|
return rb_funcall( deserialize_proc, rb_intern("call"), 1, rv );
|
|
|
|
|
return rv;
|
2020-12-01 03:32:26 +00:00
|
|
|
|
|
|
|
|
case MDBX_NOTFOUND:
|
|
|
|
|
return Qnil;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
rmdbx_close( self );
|
|
|
|
|
rb_raise( rmdbx_eDatabaseError, "Unable to fetch value: (%d) %s", rc, mdbx_strerror(rc) );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* call-seq:
|
2021-02-14 09:47:04 +00:00
|
|
|
* db[ 'key' ] = value
|
2020-12-01 03:32:26 +00:00
|
|
|
*
|
|
|
|
|
* Convenience method: set a single value for +key+
|
|
|
|
|
*/
|
|
|
|
|
VALUE
|
|
|
|
|
rmdbx_put_val( VALUE self, VALUE key, VALUE val )
|
|
|
|
|
{
|
|
|
|
|
int rc;
|
|
|
|
|
UNWRAP_DB( self, db );
|
|
|
|
|
|
2020-12-23 01:10:19 +00:00
|
|
|
if ( ! db->state.open ) rb_raise( rmdbx_eDatabaseError, "Closed database." );
|
2020-12-01 03:32:26 +00:00
|
|
|
|
2020-12-23 01:10:19 +00:00
|
|
|
rmdbx_open_txn( db, MDBX_TXN_READWRITE );
|
2020-12-16 08:29:50 +00:00
|
|
|
|
|
|
|
|
MDBX_val ckey = rmdbx_key_for( key );
|
2020-12-01 03:32:26 +00:00
|
|
|
|
|
|
|
|
// FIXME: DUPSORT is enabled -- different api?
|
|
|
|
|
// See: MDBX_NODUPDATA / MDBX_NOOVERWRITE
|
2020-12-01 04:17:11 +00:00
|
|
|
if ( NIL_P(val) ) { /* remove if set to nil */
|
|
|
|
|
rc = mdbx_del( db->txn, db->dbi, &ckey, NULL );
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
MDBX_val old;
|
2020-12-16 08:29:50 +00:00
|
|
|
MDBX_val data = rmdbx_val_for( self, val );
|
2020-12-01 04:17:11 +00:00
|
|
|
rc = mdbx_replace( db->txn, db->dbi, &ckey, &data, &old, 0 );
|
|
|
|
|
}
|
2020-12-01 03:32:26 +00:00
|
|
|
|
2020-12-23 01:10:19 +00:00
|
|
|
rmdbx_close_txn( db, RMDBX_TXN_COMMIT );
|
2020-12-01 03:32:26 +00:00
|
|
|
|
|
|
|
|
switch ( rc ) {
|
|
|
|
|
case MDBX_SUCCESS:
|
|
|
|
|
return val;
|
2020-12-17 01:09:43 +00:00
|
|
|
case MDBX_NOTFOUND:
|
|
|
|
|
return Qnil;
|
2020-12-01 03:32:26 +00:00
|
|
|
default:
|
2020-12-01 04:17:11 +00:00
|
|
|
rb_raise( rmdbx_eDatabaseError, "Unable to store value: (%d) %s", rc, mdbx_strerror(rc) );
|
2020-12-01 03:32:26 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-12-23 01:10:19 +00:00
|
|
|
/*
|
|
|
|
|
* call-seq:
|
2021-02-14 09:47:04 +00:00
|
|
|
* db.statistics => (hash of stats)
|
2020-12-23 01:10:19 +00:00
|
|
|
*
|
|
|
|
|
* Returns a hash populated with various metadata for the opened
|
|
|
|
|
* database.
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
VALUE
|
|
|
|
|
rmdbx_stats( VALUE self )
|
|
|
|
|
{
|
|
|
|
|
UNWRAP_DB( self, db );
|
|
|
|
|
if ( ! db->state.open ) rb_raise( rmdbx_eDatabaseError, "Closed database." );
|
|
|
|
|
|
|
|
|
|
return rmdbx_gather_stats( db );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-12-16 08:29:50 +00:00
|
|
|
/*
|
2021-02-14 09:47:04 +00:00
|
|
|
* call-seq:
|
|
|
|
|
* db.collection( 'collection_name' ) => db
|
|
|
|
|
* db.collection( nil ) => db (main)
|
|
|
|
|
*
|
|
|
|
|
* Gets or sets the sub-database "collection" that read/write
|
|
|
|
|
* operations apply to.
|
2021-01-25 00:36:40 +00:00
|
|
|
* Passing +nil+ sets the database to the main, top-level namespace.
|
|
|
|
|
* If a block is passed, the collection automatically reverts to the
|
|
|
|
|
* prior collection when it exits.
|
|
|
|
|
*
|
|
|
|
|
* db.collection( 'collection_name' ) do
|
|
|
|
|
* [ ... ]
|
2021-02-14 09:47:04 +00:00
|
|
|
* end => reverts to the previous collection name
|
2020-12-16 08:29:50 +00:00
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
VALUE
|
|
|
|
|
rmdbx_set_subdb( int argc, VALUE *argv, VALUE self )
|
|
|
|
|
{
|
|
|
|
|
UNWRAP_DB( self, db );
|
2021-01-25 00:36:40 +00:00
|
|
|
VALUE subdb, block;
|
|
|
|
|
char *prev_db = NULL;
|
2020-12-16 08:29:50 +00:00
|
|
|
|
2021-01-25 00:36:40 +00:00
|
|
|
rb_scan_args( argc, argv, "01&", &subdb, &block );
|
2020-12-16 08:29:50 +00:00
|
|
|
if ( argc == 0 ) {
|
|
|
|
|
if ( db->subdb == NULL ) return Qnil;
|
2020-12-17 01:09:43 +00:00
|
|
|
return rb_str_new_cstr( db->subdb );
|
2020-12-16 08:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
2021-02-14 09:47:04 +00:00
|
|
|
/* Provide a friendlier error message if max_collections is 0. */
|
|
|
|
|
if ( db->settings.max_collections == 0 )
|
|
|
|
|
rb_raise( rmdbx_eDatabaseError, "Unable to change collection: collections are not enabled." );
|
|
|
|
|
|
2021-01-25 00:36:40 +00:00
|
|
|
/* All transactions must be closed when switching database handles. */
|
2021-02-14 09:47:04 +00:00
|
|
|
if ( db->txn )
|
|
|
|
|
rb_raise( rmdbx_eDatabaseError, "Unable to change collection: transaction open" );
|
2021-01-25 00:36:40 +00:00
|
|
|
|
|
|
|
|
/* Retain the prior database collection if a
|
|
|
|
|
* block was passed. */
|
|
|
|
|
if ( rb_block_given_p() ) {
|
|
|
|
|
if ( db->subdb != NULL ) {
|
|
|
|
|
prev_db = (char *) malloc( strlen(db->subdb) + 1 );
|
|
|
|
|
strcpy( prev_db, db->subdb );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-16 08:29:50 +00:00
|
|
|
rb_iv_set( self, "@collection", subdb );
|
|
|
|
|
db->subdb = NIL_P( subdb ) ? NULL : StringValueCStr( subdb );
|
2021-01-25 00:36:40 +00:00
|
|
|
rmdbx_close_dbi( db );
|
2020-12-16 08:29:50 +00:00
|
|
|
|
2021-01-25 00:36:40 +00:00
|
|
|
/*
|
2020-12-17 01:09:43 +00:00
|
|
|
FIXME: Immediate transaction write to auto-create new env?
|
|
|
|
|
Fetching from here at the moment causes an error if you
|
2021-01-25 00:36:40 +00:00
|
|
|
haven't written anything to the new collection yet.
|
2020-12-17 01:09:43 +00:00
|
|
|
*/
|
2021-01-25 00:36:40 +00:00
|
|
|
|
|
|
|
|
/* Revert to the previous collection after the block is done. */
|
|
|
|
|
if ( rb_block_given_p() ) {
|
|
|
|
|
rb_yield( self );
|
|
|
|
|
if ( db->subdb != prev_db ) {
|
|
|
|
|
rb_iv_set( self, "@collection", prev_db ? rb_str_new_cstr(prev_db) : Qnil );
|
|
|
|
|
db->subdb = prev_db;
|
|
|
|
|
rmdbx_close_dbi( db );
|
|
|
|
|
}
|
2021-02-14 09:47:04 +00:00
|
|
|
xfree( prev_db );
|
2020-12-16 08:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-11-30 05:57:26 +00:00
|
|
|
/*
|
2021-01-25 00:36:40 +00:00
|
|
|
* Open an existing (or create a new) mdbx database at filesystem
|
|
|
|
|
* +path+. In block form, the database is automatically closed.
|
|
|
|
|
*
|
2020-11-30 05:57:26 +00:00
|
|
|
* MDBX::Database.open( path ) -> db
|
|
|
|
|
* MDBX::Database.open( path, options ) -> db
|
|
|
|
|
* MDBX::Database.open( path, options ) do |db|
|
|
|
|
|
* db...
|
2021-01-25 00:36:40 +00:00
|
|
|
* end
|
2020-11-30 05:57:26 +00:00
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
VALUE
|
|
|
|
|
rmdbx_database_initialize( int argc, VALUE *argv, VALUE self )
|
|
|
|
|
{
|
|
|
|
|
VALUE path, opts, opt;
|
|
|
|
|
rb_scan_args( argc, argv, "11", &path, &opts );
|
|
|
|
|
|
|
|
|
|
/* Ensure options is a hash if it was passed in.
|
|
|
|
|
*/
|
|
|
|
|
if ( NIL_P(opts) ) {
|
|
|
|
|
opts = rb_hash_new();
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
Check_Type( opts, T_HASH );
|
|
|
|
|
}
|
|
|
|
|
rb_hash_freeze( opts );
|
|
|
|
|
|
2020-12-23 01:10:19 +00:00
|
|
|
/* Initialize the DB vals.
|
|
|
|
|
*/
|
|
|
|
|
UNWRAP_DB( self, db );
|
|
|
|
|
db->env = NULL;
|
|
|
|
|
db->dbi = 0;
|
|
|
|
|
db->txn = NULL;
|
|
|
|
|
db->cursor = NULL;
|
|
|
|
|
db->path = StringValueCStr( path );
|
|
|
|
|
db->subdb = NULL;
|
2021-01-25 00:36:40 +00:00
|
|
|
db->state.open = 0;
|
|
|
|
|
db->state.retain_txn = -1;
|
2020-12-23 01:10:19 +00:00
|
|
|
db->settings.env_flags = MDBX_ENV_DEFAULTS;
|
|
|
|
|
db->settings.mode = 0644;
|
|
|
|
|
db->settings.max_collections = 0;
|
|
|
|
|
db->settings.max_readers = 0;
|
2021-02-14 09:47:04 +00:00
|
|
|
db->settings.max_size = 0;
|
2020-12-23 01:10:19 +00:00
|
|
|
|
2020-11-30 05:57:26 +00:00
|
|
|
/* Options setup, overrides.
|
|
|
|
|
*/
|
|
|
|
|
opt = rb_hash_aref( opts, ID2SYM( rb_intern("mode") ) );
|
2020-12-23 01:10:19 +00:00
|
|
|
if ( ! NIL_P(opt) ) db->settings.mode = FIX2INT( opt );
|
2020-12-17 01:09:43 +00:00
|
|
|
opt = rb_hash_aref( opts, ID2SYM( rb_intern("max_collections") ) );
|
2020-12-23 01:10:19 +00:00
|
|
|
if ( ! NIL_P(opt) ) db->settings.max_collections = FIX2INT( opt );
|
|
|
|
|
opt = rb_hash_aref( opts, ID2SYM( rb_intern("max_readers") ) );
|
|
|
|
|
if ( ! NIL_P(opt) ) db->settings.max_readers = FIX2INT( opt );
|
|
|
|
|
opt = rb_hash_aref( opts, ID2SYM( rb_intern("max_size") ) );
|
|
|
|
|
if ( ! NIL_P(opt) ) db->settings.max_size = NUM2LONG( opt );
|
2020-11-30 05:57:26 +00:00
|
|
|
opt = rb_hash_aref( opts, ID2SYM( rb_intern("nosubdir") ) );
|
2020-12-23 01:10:19 +00:00
|
|
|
if ( RTEST(opt) ) db->settings.env_flags = db->settings.env_flags | MDBX_NOSUBDIR;
|
2020-11-30 05:57:26 +00:00
|
|
|
opt = rb_hash_aref( opts, ID2SYM( rb_intern("readonly") ) );
|
2020-12-23 01:10:19 +00:00
|
|
|
if ( RTEST(opt) ) db->settings.env_flags = db->settings.env_flags | MDBX_RDONLY;
|
2020-11-30 05:57:26 +00:00
|
|
|
opt = rb_hash_aref( opts, ID2SYM( rb_intern("exclusive") ) );
|
2020-12-23 01:10:19 +00:00
|
|
|
if ( RTEST(opt) ) db->settings.env_flags = db->settings.env_flags | MDBX_EXCLUSIVE;
|
2020-11-30 05:57:26 +00:00
|
|
|
opt = rb_hash_aref( opts, ID2SYM( rb_intern("compat") ) );
|
2020-12-23 01:10:19 +00:00
|
|
|
if ( RTEST(opt) ) db->settings.env_flags = db->settings.env_flags | MDBX_ACCEDE;
|
2020-11-30 05:57:26 +00:00
|
|
|
opt = rb_hash_aref( opts, ID2SYM( rb_intern("writemap") ) );
|
2020-12-23 01:10:19 +00:00
|
|
|
if ( RTEST(opt) ) db->settings.env_flags = db->settings.env_flags | MDBX_WRITEMAP;
|
2020-11-30 05:57:26 +00:00
|
|
|
opt = rb_hash_aref( opts, ID2SYM( rb_intern("no_threadlocal") ) );
|
2020-12-23 01:10:19 +00:00
|
|
|
if ( RTEST(opt) ) db->settings.env_flags = db->settings.env_flags | MDBX_NOTLS;
|
2020-11-30 05:57:26 +00:00
|
|
|
opt = rb_hash_aref( opts, ID2SYM( rb_intern("no_readahead") ) );
|
2020-12-23 01:10:19 +00:00
|
|
|
if ( RTEST(opt) ) db->settings.env_flags = db->settings.env_flags | MDBX_NORDAHEAD;
|
2020-11-30 05:57:26 +00:00
|
|
|
opt = rb_hash_aref( opts, ID2SYM( rb_intern("no_memory_init") ) );
|
2020-12-23 01:10:19 +00:00
|
|
|
if ( RTEST(opt) ) db->settings.env_flags = db->settings.env_flags | MDBX_NOMEMINIT;
|
2020-11-30 05:57:26 +00:00
|
|
|
opt = rb_hash_aref( opts, ID2SYM( rb_intern("coalesce") ) );
|
2020-12-23 01:10:19 +00:00
|
|
|
if ( RTEST(opt) ) db->settings.env_flags = db->settings.env_flags | MDBX_COALESCE;
|
2020-11-30 05:57:26 +00:00
|
|
|
opt = rb_hash_aref( opts, ID2SYM( rb_intern("lifo_reclaim") ) );
|
2020-12-23 01:10:19 +00:00
|
|
|
if ( RTEST(opt) ) db->settings.env_flags = db->settings.env_flags | MDBX_LIFORECLAIM;
|
2020-11-30 05:57:26 +00:00
|
|
|
opt = rb_hash_aref( opts, ID2SYM( rb_intern("no_metasync") ) );
|
2020-12-23 01:10:19 +00:00
|
|
|
if ( RTEST(opt) ) db->settings.env_flags = db->settings.env_flags | MDBX_NOMETASYNC;
|
2020-12-01 03:32:26 +00:00
|
|
|
|
|
|
|
|
/* Duplicate keys, on mdbx_dbi_open, maybe set here? */
|
|
|
|
|
/* MDBX_DUPSORT = UINT32_C(0x04), */
|
2020-11-30 05:57:26 +00:00
|
|
|
|
|
|
|
|
/* Set instance variables.
|
|
|
|
|
*/
|
|
|
|
|
rb_iv_set( self, "@path", path );
|
|
|
|
|
rb_iv_set( self, "@options", opts );
|
|
|
|
|
|
2020-12-17 01:09:43 +00:00
|
|
|
rmdbx_open_env( self );
|
2020-11-30 05:57:26 +00:00
|
|
|
return self;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
* Initialization for the MDBX::Database class.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
rmdbx_init_database()
|
|
|
|
|
{
|
|
|
|
|
rmdbx_cDatabase = rb_define_class_under( rmdbx_mMDBX, "Database", rb_cData );
|
|
|
|
|
|
2020-12-22 06:24:18 +00:00
|
|
|
#ifdef FOR_RDOC
|
|
|
|
|
rmdbx_mMDBX = rb_define_module( "MDBX" );
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-11-30 05:57:26 +00:00
|
|
|
rb_define_alloc_func( rmdbx_cDatabase, rmdbx_alloc );
|
|
|
|
|
|
2020-12-01 03:32:26 +00:00
|
|
|
rb_define_protected_method( rmdbx_cDatabase, "initialize", rmdbx_database_initialize, -1 );
|
2020-12-16 08:29:50 +00:00
|
|
|
rb_define_method( rmdbx_cDatabase, "collection", rmdbx_set_subdb, -1 );
|
2020-11-30 05:57:26 +00:00
|
|
|
rb_define_method( rmdbx_cDatabase, "close", rmdbx_close, 0 );
|
2020-12-22 06:24:18 +00:00
|
|
|
rb_define_method( rmdbx_cDatabase, "reopen", rmdbx_open_env, 0 );
|
2020-11-30 05:57:26 +00:00
|
|
|
rb_define_method( rmdbx_cDatabase, "closed?", rmdbx_closed_p, 0 );
|
2021-01-25 00:36:40 +00:00
|
|
|
rb_define_method( rmdbx_cDatabase, "in_transaction?", rmdbx_in_transaction_p, 0 );
|
2020-12-17 01:09:43 +00:00
|
|
|
rb_define_method( rmdbx_cDatabase, "clear", rmdbx_clear, 0 );
|
|
|
|
|
rb_define_method( rmdbx_cDatabase, "keys", rmdbx_keys, 0 );
|
2020-12-01 03:32:26 +00:00
|
|
|
rb_define_method( rmdbx_cDatabase, "[]", rmdbx_get_val, 1 );
|
|
|
|
|
rb_define_method( rmdbx_cDatabase, "[]=", rmdbx_put_val, 2 );
|
2020-11-30 05:57:26 +00:00
|
|
|
|
2021-01-25 00:36:40 +00:00
|
|
|
/* Manually open/close transactions from ruby. */
|
|
|
|
|
rb_define_protected_method( rmdbx_cDatabase, "open_transaction", rmdbx_rb_opentxn, 1 );
|
|
|
|
|
rb_define_protected_method( rmdbx_cDatabase, "close_transaction", rmdbx_rb_closetxn, 1 );
|
|
|
|
|
|
2020-12-23 01:10:19 +00:00
|
|
|
rb_define_protected_method( rmdbx_cDatabase, "raw_stats", rmdbx_stats, 0 );
|
|
|
|
|
|
2020-11-30 05:57:26 +00:00
|
|
|
rb_require( "mdbx/database" );
|
|
|
|
|
}
|
|
|
|
|
|