Multiple changes.

- Provide a method to reopen the database environment if closed.
 - Add #keys, which returns an array of keys for the current collection.
 - Finish collection/namespace implementation.
 - Add various aliases (collection->namespace, etc)
 - Add #clear, which destroys data for a the current collection.
   If called in the main db, destroys all.  Don't close the environment
   after a #clear, just the dbi handle.
 - Various bugfixes.

FossilOrigin-Name: 8b00d59e8c5269266f3c6c0d10056a1c0fe1bb583ffbe099a6ed0dac3baf3a66
This commit is contained in:
Mahlon E. Smith 2020-12-17 01:09:43 +00:00
parent d92ba7c5eb
commit 5ed44e52fa
6 changed files with 194 additions and 47 deletions

View file

@ -10,7 +10,7 @@ RSpec.describe( MDBX::Database ) do
to raise_exception( NoMethodError, /private/ )
end
it "knows the db handle open/close state" do
it "knows the env handle open/close state" do
db = described_class.open( TEST_DATABASE.to_s )
expect( db.closed? ).to be_falsey
db.close
@ -33,29 +33,95 @@ RSpec.describe( MDBX::Database ) do
db.close
end
it "can be reopened" do
db.close
expect( db ).to be_closed
db.reopen
expect( db ).to_not be_closed
end
it "knows its own path" do
expect( db.path ).to match( %r|data/testdb$| )
expect( db.path ).to match( %r|tmp/testdb$| )
end
it "fails if opened again within the same process" do
# This is a function of libmdbx internals, just testing
# here for behaviorals.
# here for behavior.
expect {
described_class.open( TEST_DATABASE.to_s )
}.
to raise_exception( MDBX::DatabaseError, /environment is already used/ )
end
it "can remove a key by setting its value to nil" do
db[ 'test' ] = "hi"
expect( db['test'] ).to eq( 'hi' )
db[ 'test' ] = nil
expect( db['test'] ).to be_nil
expect { db[ 'test' ] = nil }.to_not raise_exception
end
it "can return an array of its keys" do
db[ 'key1' ] = true
db[ 'key2' ] = true
db[ 'key3' ] = true
expect( db.keys ).to include( 'key1', 'key2', 'key3' )
end
end
context 'collections' do
let!( :db ) { described_class.open( TEST_DATABASE.to_s, max_collections: 5 ) }
after( :each ) do
db.close
end
it "fail if the max_collections option is not specified when opening" do
db.close
db = described_class.open( TEST_DATABASE.to_s )
db.collection( 'bucket' )
expect{ db['key'] = true }.to raise_exception( /MDBX_DBS_FULL/ )
end
it "disallows regular key/val storage for namespace keys" do
db.collection( 'bucket' )
db[ 'okay' ] = 1
db.collection( nil )
expect{ db['bucket'] = 1 }.to raise_exception( /MDBX_INCOMPATIBLE/ )
end
it "defaults to the top-level namespace" do
expect( db.collection ).to be_nil
end
it "can set a collection namespace" do
expect( db.collection ).to be_nil
db[ 'key' ] = true
db.collection( 'bucket' )
expect( db.collection ).to eq( 'bucket' )
# TODO: set/retrieve data
db[ 'key' ] = false
db.main
expect( db.collection ).to be_nil
expect( db['key'] ).to be_truthy
end
it "can be cleared of contents" do
db.collection( 'bucket' )
10.times {|i| db[i] = true }
expect( db[2] ).to be_truthy
db.clear
db.main
expect( db['bucket'] ).to be_nil
end
end
end