Don't inadvertantly close an open transaction while using hash convenience methods.
FossilOrigin-Name: 6454408d0a2347681d8030e1788f94904aa69e7502ba695f099d964a638b8d49
This commit is contained in:
parent
6c289674dd
commit
54a9409066
4 changed files with 53 additions and 23 deletions
|
|
@ -7,4 +7,3 @@ ext/mdbx_ext/database.c
|
||||||
ext/mdbx_ext/stats.c
|
ext/mdbx_ext/stats.c
|
||||||
lib/mdbx.rb
|
lib/mdbx.rb
|
||||||
lib/mdbx/database.rb
|
lib/mdbx/database.rb
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ require 'mdbx_ext'
|
||||||
module MDBX
|
module MDBX
|
||||||
|
|
||||||
# The version of this gem.
|
# The version of this gem.
|
||||||
VERSION = '0.1.0'
|
VERSION = '0.1.1'
|
||||||
|
|
||||||
end # module MDBX
|
end # module MDBX
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -201,18 +201,24 @@ class MDBX::Database
|
||||||
### pairs.
|
### pairs.
|
||||||
###
|
###
|
||||||
def to_a
|
def to_a
|
||||||
self.snapshot do
|
in_txn = self.in_transaction?
|
||||||
|
self.snapshot unless in_txn
|
||||||
|
|
||||||
return self.each_pair.to_a
|
return self.each_pair.to_a
|
||||||
end
|
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
|
||||||
self.snapshot do
|
in_txn = self.in_transaction?
|
||||||
|
self.snapshot unless in_txn
|
||||||
|
|
||||||
return self.each_pair.to_h
|
return self.each_pair.to_h
|
||||||
end
|
ensure
|
||||||
|
self.abort unless in_txn
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -261,9 +267,12 @@ 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
|
||||||
self.snapshot do
|
in_txn = self.in_transaction?
|
||||||
|
self.snapshot unless in_txn
|
||||||
|
|
||||||
return self.each_key.to_a
|
return self.each_key.to_a
|
||||||
end
|
ensure
|
||||||
|
self.abort unless in_txn
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -271,32 +280,41 @@ 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 )
|
||||||
self.snapshot do
|
in_txn = self.in_transaction?
|
||||||
|
self.snapshot unless in_txn
|
||||||
|
|
||||||
return 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
|
||||||
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
|
||||||
self.snapshot do
|
in_txn = self.in_transaction?
|
||||||
|
self.snapshot unless in_txn
|
||||||
|
|
||||||
return self.each_value.to_a
|
return self.each_value.to_a
|
||||||
end
|
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 )
|
||||||
self.snapshot do
|
in_txn = self.in_transaction?
|
||||||
|
self.snapshot unless in_txn
|
||||||
|
|
||||||
return keys.each_with_object( [] ) do |key, acc|
|
return keys.each_with_object( [] ) do |key, acc|
|
||||||
acc << self[ key ]
|
acc << self[ key ]
|
||||||
end
|
end
|
||||||
end
|
ensure
|
||||||
|
self.abort unless in_txn
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -312,6 +312,19 @@ RSpec.describe( MDBX::Database ) do
|
||||||
end
|
end
|
||||||
expect( db[ 1 ] ).to be_falsey
|
expect( db[ 1 ] ).to be_falsey
|
||||||
end
|
end
|
||||||
|
|
||||||
|
it "doesn't inadvertantly close transactions when using hash-alike methods" do
|
||||||
|
expect( db.in_transaction? ).to be_falsey
|
||||||
|
db.transaction
|
||||||
|
expect( db.in_transaction? ).to be_truthy
|
||||||
|
db.to_a
|
||||||
|
db.to_h
|
||||||
|
db.keys
|
||||||
|
db.slice( :woop )
|
||||||
|
db.values
|
||||||
|
db.values_at( :woop )
|
||||||
|
expect( db.in_transaction? ).to be_truthy
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue