Move utility conditional transactions to a helper wrapper.
FossilOrigin-Name: ae7078a55b83a71f41a6e5d076a6a5ad1483aafac10be5d77afcd4a537a6f663
This commit is contained in:
parent
755fca55a1
commit
00036862df
3 changed files with 41 additions and 40 deletions
|
|
@ -201,24 +201,18 @@ class MDBX::Database
|
||||||
### pairs.
|
### pairs.
|
||||||
###
|
###
|
||||||
def to_a
|
def to_a
|
||||||
in_txn = self.in_transaction?
|
return self.conditional_snapshot do
|
||||||
self.snapshot unless in_txn
|
self.each_pair.to_a
|
||||||
|
end
|
||||||
return self.each_pair.to_a
|
|
||||||
ensure
|
|
||||||
self.abort unless in_txn
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
### Return the entirety of database contents as a Hash.
|
### Return the entirety of database contents as a Hash.
|
||||||
###
|
###
|
||||||
def to_h
|
def to_h
|
||||||
in_txn = self.in_transaction?
|
return self.conditional_snapshot do
|
||||||
self.snapshot unless in_txn
|
self.each_pair.to_h
|
||||||
|
end
|
||||||
return self.each_pair.to_h
|
|
||||||
ensure
|
|
||||||
self.abort unless in_txn
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -267,12 +261,9 @@ class MDBX::Database
|
||||||
### Returns a new Array containing all keys in the collection.
|
### Returns a new Array containing all keys in the collection.
|
||||||
###
|
###
|
||||||
def keys
|
def keys
|
||||||
in_txn = self.in_transaction?
|
return self.conditional_snapshot do
|
||||||
self.snapshot unless in_txn
|
self.each_key.to_a
|
||||||
|
end
|
||||||
return self.each_key.to_a
|
|
||||||
ensure
|
|
||||||
self.abort unless in_txn
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -280,41 +271,32 @@ class MDBX::Database
|
||||||
### keys. Any given keys that are not found are ignored.
|
### keys. Any given keys that are not found are ignored.
|
||||||
###
|
###
|
||||||
def slice( *keys )
|
def slice( *keys )
|
||||||
in_txn = self.in_transaction?
|
return self.conditional_snapshot do
|
||||||
self.snapshot unless in_txn
|
keys.each_with_object( {} ) do |key, acc|
|
||||||
|
|
||||||
return keys.each_with_object( {} ) do |key, acc|
|
|
||||||
val = self[ key ]
|
val = self[ key ]
|
||||||
acc[ key ] = val if val
|
acc[ key ] = val if val
|
||||||
end
|
end
|
||||||
ensure
|
end
|
||||||
self.abort unless in_txn
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
### Returns a new Array containing all values in the collection.
|
### Returns a new Array containing all values in the collection.
|
||||||
###
|
###
|
||||||
def values
|
def values
|
||||||
in_txn = self.in_transaction?
|
return self.conditional_snapshot do
|
||||||
self.snapshot unless in_txn
|
self.each_value.to_a
|
||||||
|
end
|
||||||
return self.each_value.to_a
|
|
||||||
ensure
|
|
||||||
self.abort unless in_txn
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
### Returns a new Array containing values for the given +keys+.
|
### Returns a new Array containing values for the given +keys+.
|
||||||
###
|
###
|
||||||
def values_at( *keys )
|
def values_at( *keys )
|
||||||
in_txn = self.in_transaction?
|
return self.conditional_snapshot do
|
||||||
self.snapshot unless in_txn
|
keys.each_with_object( [] ) do |key, acc|
|
||||||
|
|
||||||
return keys.each_with_object( [] ) do |key, acc|
|
|
||||||
acc << self[ key ]
|
acc << self[ key ]
|
||||||
end
|
end
|
||||||
ensure
|
end
|
||||||
self.abort unless in_txn
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -347,5 +329,23 @@ class MDBX::Database
|
||||||
return stats
|
return stats
|
||||||
end
|
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
|
end # class MDBX::Database
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -324,6 +324,7 @@ RSpec.describe( MDBX::Database ) do
|
||||||
db.values
|
db.values
|
||||||
db.values_at( :woop )
|
db.values_at( :woop )
|
||||||
expect( db.in_transaction? ).to be_truthy
|
expect( db.in_transaction? ).to be_truthy
|
||||||
|
db.abort
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue