Checkpoint.

- Fix a couple of edge cases while switching between collections.
  - Raise error if attempting to switch collections mid-transaction.
  - Add logic for re-entrancy with long-running transactions.
  - Revert to the prior collection if passing a block to #collection.
  - Add a predicate to tell if you're currently within a long-running transaction.
  - Add separate commit/rollback to long-running transactions.

FossilOrigin-Name: 711239e6fc2f25479a26fb54805da1e5db792f97f28a8b5724e0b38eb11cdb07
This commit is contained in:
Mahlon E. Smith 2021-01-25 00:36:40 +00:00
parent ceb92fad16
commit 81ee69295c
7 changed files with 239 additions and 29 deletions

View file

@ -18,8 +18,6 @@ class MDBX::Database
### db[ 'key' ] #=> value
### end
###
### FIXME: document all options!
###
def self::open( *args, &block )
db = new( *args )
@ -49,9 +47,11 @@ class MDBX::Database
attr_reader :path
# A Proc for automatically serializing values.
# Defaults to +Marshal.dump+.
attr_accessor :serializer
# A Proc for automatically deserializing values.
# Defaults to +Marshal.load+.
attr_accessor :deserializer
@ -65,6 +65,56 @@ class MDBX::Database
alias_method :namespace, :collection
### Open a new mdbx read/write transaction. In block form,
### the transaction is automatically committed.
###
### Raising a MDBX::Rollback exception from within the block
### automatically rolls the transaction back.
###
def transaction( commit: true, &block )
self.open_transaction( commit )
yield self if block_given?
return self
rescue MDBX::Rollback
commit = false
self.rollback
rescue
commit = false
self.rollback
raise
ensure
if block_given?
commit ? self.commit : self.rollback
end
end
### Open a new mdbx read only snapshot. In block form,
### the snapshot is automatically closed.
###
def snapshot( &block )
self.transaction( commit: false, &block )
end
### Close any open transactions, abandoning all changes.
###
def rollback
return self.close_transaction( false )
end
alias_method :abort, :rollback
### Close any open transactions, writing all changes.
###
def commit
return self.close_transaction( true )
end
alias_method :save, :commit
### Return a hash of various metadata for the current database.
###
def statistics