Multiple changes.

- Alter the behavior of #clear, so it doesn't destroy collection
   environments, but just empties them.
 - Add #drop, which explictly -does- destroy a collection environment.
 - Run all cursor methods through rb_protect, to ensure proper
   cursor cleanup in the event of an exception mid iteration.
 - Fix the block form of collections to support multiple scopes.
 - Switching to a collection now automatically creates its environment.
 - Add include? and has_key?, for presence checks without allocating
   value memory or requiring deserialization.

FossilOrigin-Name: e1ed7bf613981607bb3b57ce7dd3e58b94ea3046e140b6dc37440da8d2909f94
This commit is contained in:
Mahlon E. Smith 2021-04-08 22:27:47 +00:00
parent 1e04b12efa
commit ca34f9fdc5
7 changed files with 365 additions and 114 deletions

View file

@ -128,16 +128,43 @@ class MDBX::Database
attr_accessor :deserializer
alias_method :size, :length
alias_method :each, :each_pair
alias_method :has_key?, :include?
### Gets or sets the sub-database "collection" that read/write
### operations apply to. If a block is passed, the collection
### automatically reverts to the prior collection when it exits.
###
### db.collection #=> (collection name, or nil if in main)
### db.collection( 'collection_name' ) #=> db
###
### db.collection( 'collection_name' ) do
### [ ... ]
### end # reverts to the previous collection name
###
def collection( name=nil )
current = self.get_subdb
return current unless name
self.set_subdb( name.to_s )
yield( self ) if block_given?
return self
ensure
self.set_subdb( current ) if name && block_given?
end
alias_method :namespace, :collection
### Switch to the top-level collection.
###
def main
return self.collection( nil )
return self.set_subdb( nil )
end
alias_method :namespace, :collection
alias_method :size, :length
alias_method :each, :each_pair
#
# Transaction methods