lmdb 0.8.0 → 0.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5673437b11f0c87befc6f0003aaa1c25f8c20ec3270147396d45c8af38fb0a78
4
- data.tar.gz: 407eadc211c71c38cc8d1e4523f2639abb964e187452635af7b603ce5e3eb565
3
+ metadata.gz: 0eca18739acfd5adf30147b8d4d5561215aacb85efafdfa4b92a4669c919913f
4
+ data.tar.gz: 5e5c34291a740b95637f9f64a280e3171e9a977e7c4c24ef4f98b45441ac794e
5
5
  SHA512:
6
- metadata.gz: '0596fdb966d1373d03a739901ca441fb3df42b7257566b07a8ee513c1b30ea7b9b8fcfa1ae6f5a1efcbae7a5b91c7983f6d83fc1e6ffa6d63fba6c9572623c39'
7
- data.tar.gz: cbfd03a44df15275c6bf66d10501e5d86d6a6bf3bee949588ae7d9b89cd9e03bf57ddb7f3502f56d7128ddf500658d502ea0436bc4868c455a0d139c7525a74c
6
+ metadata.gz: 9523366f92b3f4a0835bd2786bb3438da3d5e5c94460afdf71cc4652fd4dd4f00dd431f553a8078ab8948eddf0d09bb2dbea78546ca87465f552db94249bbfcc
7
+ data.tar.gz: d5c548be5e79e730e62c408ac987fab8b59f44ba41bab54db69b032f1fccd8b632c483450db301058b8c07c69c88a0f8d8403cb39701380480ad810e69e5209e
data/CHANGES CHANGED
@@ -1,3 +1,25 @@
1
+ 0.8.1
2
+
3
+ * added `env.transaction?` for conditional transactions
4
+ * removed private method `db.maybe_txn`
5
+
6
+ 0.8.0
7
+
8
+ * overhauled internals to use newer `TypedData` conventions
9
+ * made pseudo-transactions for behaviour parity ofr read-only transactions
10
+ * rewrote `with_transaction` and `transaction_finish` etc
11
+ * made `env.database(:symbol_for_a_name)` not segfault
12
+ * added `env['foo']` (or `env[:foo]`) accessor for existing databases
13
+ * added `env.reader_check` to wipe out stale readers
14
+ * added `env.reader_list` to list them
15
+ * added unit tests for GC torture
16
+ * added unit tests for nested pseudo-transactions
17
+
18
+ 0.7.5
19
+
20
+ * changed embedding regime for upstream lmdb
21
+ * added --vendor-lmdb build flag (`gem install lmdb -- --vendor-lmdb`)
22
+
1
23
  0.7.0
2
24
 
3
25
  * Nesting read-only transactions finally works, by making it a noop
data/lib/lmdb/database.rb CHANGED
@@ -1,4 +1,22 @@
1
1
  module LMDB
2
+ class Environment
3
+
4
+ # Start a new transaction if there isn't already one going.
5
+ #
6
+ # @param readonly [false, true]
7
+ # @param block [Proc] the block to run
8
+ # @yieldparam [LMDB::Transaction] the transaction handle
9
+ # @yieldreturn [Object] your pick
10
+ # @return [Object] whatever the block returns
11
+ #
12
+ def transaction? readonly = false, &block
13
+ raise ArgumentError, 'no block for conditional transaction' unless block
14
+
15
+ t = active_txn
16
+ t ? block.call(t) : transaction(!!readonly, &block)
17
+ end
18
+ end
19
+
2
20
  class Database
3
21
  include Enumerable
4
22
 
@@ -10,9 +28,8 @@ module LMDB
10
28
  # key, value = record
11
29
  # puts "at #{key}: #{value}"
12
30
  # end
13
- def each
14
- maybe_txn true do
15
- # env.transaction do
31
+ def each &block
32
+ env.transaction? true do
16
33
  cursor do |c|
17
34
  while i = c.next
18
35
  yield(i)
@@ -55,12 +72,12 @@ module LMDB
55
72
  # @yield key [String] the next key in the database.
56
73
  # @return [Enumerator] in lieu of a block.
57
74
  def each_key(&block)
58
- return enum_for :each_key unless block_given?
59
- maybe_txn true do
60
- #env.transaction do
75
+ return enum_for :each_key unless block
76
+
77
+ env.transaction? true do
61
78
  cursor do |c|
62
79
  while (rec = c.next true)
63
- yield rec.first
80
+ block.call rec.first
64
81
  end
65
82
  end
66
83
  end
@@ -73,24 +90,25 @@ module LMDB
73
90
  # @yield value [String] the next value associated with the key.
74
91
  # @return [Enumerator] in lieu of a block.
75
92
  def each_value(key, &block)
76
- return enum_for :each_value, key unless block_given?
93
+ return enum_for :each_value, key unless block
77
94
 
78
- value = get(key) or return
79
- unless dupsort?
80
- yield value
81
- return
82
- end
95
+ op = -> txn do
96
+ value = get(key) or return
97
+ unless dupsort?
98
+ block.call value
99
+ return
100
+ end
83
101
 
84
- maybe_txn true do
85
- # env.transaction do
86
102
  cursor do |c|
87
103
  rec = c.set key
88
104
  while rec
89
- yield rec.last
105
+ block.call rec.last
90
106
  rec = c.next_range key
91
107
  end
92
108
  end
93
109
  end
110
+
111
+ env.transaction? true, &op
94
112
  end
95
113
 
96
114
  # Return the cardinality (number of duplicates) of a given
@@ -99,8 +117,7 @@ module LMDB
99
117
  # @return [Integer] The number of entries under the key.
100
118
  def cardinality(key)
101
119
  ret = 0
102
- maybe_txn true do
103
- # env.transaction do
120
+ env.transaction? true do
104
121
  if get key
105
122
  if dupsort?
106
123
  cursor do |c|
@@ -118,18 +135,20 @@ module LMDB
118
135
  # Test if the database has a given key (or, if opened in
119
136
  # +:dupsort+, value)
120
137
  def has?(key, value = nil)
121
- v = get(key) or return false
122
- return true if value.nil? or value.to_s == v
123
- return false unless dupsort?
124
-
125
- # warn "checking dupsort value `#{value.inspect}` (#{value.class})"
126
-
127
- ret = false
128
- # read-only txn was having trouble being nested inside a read-write
129
- # maybe_txn(true) { cursor { |c| ret = !!c.set(key, value) } }
130
- env.transaction(true) { cursor { |c| ret = !!c.set(key, value) } }
138
+ env.transaction? true do
139
+ if v = get(key)
140
+ if value.nil? or value.to_s == v
141
+ true
142
+ elsif !dupsort?
143
+ false
144
+ else
145
+ ret = false
146
+ cursor { |c| ret = !!c.set(key, value) }
131
147
 
132
- ret
148
+ ret
149
+ end
150
+ end
151
+ end
133
152
  end
134
153
 
135
154
  # Conditionally put a value into the database.
@@ -148,7 +167,7 @@ module LMDB
148
167
 
149
168
  flags = { (dupsort? ? :nodupdata : :nooverwrite) => true }
150
169
 
151
- env.transaction do |txn|
170
+ env.transaction? do |txn|
152
171
  put(key, value, **options.merge(flags)) unless has?(key, value)
153
172
  end
154
173
  end
@@ -164,7 +183,7 @@ module LMDB
164
183
  # @return [void]
165
184
  #
166
185
  def delete?(key, value = nil)
167
- env.transaction { |txn| delete(key, value) if has?(key, value) }
186
+ env.transaction? { |txn| delete(key, value) if has?(key, value) }
168
187
  end
169
188
 
170
189
  # Return how many records there are in this database.
@@ -179,26 +198,5 @@ module LMDB
179
198
  def empty?
180
199
  stat[:entries] == 0
181
200
  end
182
-
183
- private
184
-
185
- # having trouble with read-only transactions embedded in
186
- # read-write for some reason; can't pin it down to test it yet so
187
- # going to do this (djt; 2020-02-10)
188
- #
189
- # 2025-11-14: this miiiigght no longer be necessary?? like as of
190
- # whenever i implemented that short-circuiting code
191
- #
192
- def maybe_txn(readonly, &block)
193
- if t = env.active_txn
194
- # warn "reusing #{t.readonly? ? 'read-only ' : ''}txn #{t.inspect}"
195
- yield t
196
- else
197
- env.transaction !!readonly do |t|
198
- # warn "new #{t.readonly? ? 'read-only ' : ''}txn #{t.inspect}"
199
- yield t
200
- end
201
- end
202
- end
203
201
  end
204
202
  end
data/lib/lmdb/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module LMDB
2
- VERSION = '0.8.0'.freeze
2
+ VERSION = '0.8.1'.freeze
3
3
  end
data/spec/lmdb_spec.rb CHANGED
@@ -200,6 +200,16 @@ describe LMDB do
200
200
  end
201
201
  expect(env2).to eq(env)
202
202
  end
203
+
204
+ it 'should do conditional transactions' do
205
+ subject.transaction? true do |t1|
206
+ # note this second one is not readonly which would be
207
+ # illegal if it was opening a transaction for real
208
+ subject.transaction? do |t2|
209
+ expect(t2).to eq(t1)
210
+ end
211
+ end
212
+ end
203
213
  end
204
214
  end
205
215
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lmdb
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.8.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Mendler