Move utility conditional transactions to a helper wrapper.

FossilOrigin-Name: ae7078a55b83a71f41a6e5d076a6a5ad1483aafac10be5d77afcd4a537a6f663
This commit is contained in:
Mahlon E. Smith 2021-03-15 07:39:01 +00:00
parent 755fca55a1
commit 00036862df
3 changed files with 41 additions and 40 deletions

View file

@ -201,24 +201,18 @@ class MDBX::Database
### pairs.
###
def to_a
in_txn = self.in_transaction?
self.snapshot unless in_txn
return self.each_pair.to_a
ensure
self.abort unless in_txn
return self.conditional_snapshot do
self.each_pair.to_a
end
end
### Return the entirety of database contents as a Hash.
###
def to_h
in_txn = self.in_transaction?
self.snapshot unless in_txn
return self.each_pair.to_h
ensure
self.abort unless in_txn
return self.conditional_snapshot do
self.each_pair.to_h
end
end
@ -267,12 +261,9 @@ class MDBX::Database
### Returns a new Array containing all keys in the collection.
###
def keys
in_txn = self.in_transaction?
self.snapshot unless in_txn
return self.each_key.to_a
ensure
self.abort unless in_txn
return self.conditional_snapshot do
self.each_key.to_a
end
end
@ -280,41 +271,32 @@ class MDBX::Database
### keys. Any given keys that are not found are ignored.
###
def slice( *keys )
in_txn = self.in_transaction?
self.snapshot unless in_txn
return keys.each_with_object( {} ) do |key, acc|
val = self[ key ]
acc[ key ] = val if val
return self.conditional_snapshot do
keys.each_with_object( {} ) do |key, acc|
val = self[ key ]
acc[ key ] = val if val
end
end
ensure
self.abort unless in_txn
end
### Returns a new Array containing all values in the collection.
###
def values
in_txn = self.in_transaction?
self.snapshot unless in_txn
return self.each_value.to_a
ensure
self.abort unless in_txn
return self.conditional_snapshot do
self.each_value.to_a
end
end
### Returns a new Array containing values for the given +keys+.
###
def values_at( *keys )
in_txn = self.in_transaction?
self.snapshot unless in_txn
return keys.each_with_object( [] ) do |key, acc|
acc << self[ key ]
return self.conditional_snapshot do
keys.each_with_object( [] ) do |key, acc|
acc << self[ key ]
end
end
ensure
self.abort unless in_txn
end
@ -347,5 +329,23 @@ class MDBX::Database
return stats
end
#########
protected
#########
### Yield and return the block, opening a snapshot first if
### there isn't already a transaction in progress. Closes
### the snapshot if this method opened it.
###
def conditional_snapshot
in_txn = self.in_transaction?
self.snapshot unless in_txn
return yield
ensure
self.abort unless in_txn
end
end # class MDBX::Database