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

@ -148,7 +148,7 @@ rmdbx_gather_reader_stats(
{ {
VALUE readers = rb_ary_new(); VALUE readers = rb_ary_new();
mdbx_reader_list( db->env, reader_list_callback, (void*)readers ); mdbx_reader_list( db->env, reader_list_callback, (void*)readers );
rb_hash_aset( stat, ID2SYM(rb_intern("readers")), readers ); rb_hash_aset( stat, ID2SYM(rb_intern("readers")), readers );
return; return;

View file

@ -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|
val = self[ key ]
return keys.each_with_object( {} ) do |key, acc| acc[ key ] = val if val
val = self[ key ] end
acc[ key ] = val if val
end end
ensure
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|
acc << self[ key ]
return keys.each_with_object( [] ) do |key, acc| end
acc << self[ key ]
end end
ensure
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

View file

@ -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