xitdb 0.1.0 → 0.2.0
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 +4 -4
- data/CHANGELOG.md +16 -0
- data/README.md +15 -3
- data/docs/benchmarks.md +4 -1
- data/docs/core.md +15 -2
- data/docs/learning.md +211 -0
- data/docs/native-profile-performance.md +254 -0
- data/docs/ruby-profile-v1.md +2 -0
- data/docs/ruby-profile-v2.md +279 -0
- data/docs/ruby-profile-v3.md +53 -0
- data/docs/ruby-profile-v4.md +67 -0
- data/docs/ruby.md +26 -3
- data/examples/indexed_users.rb +3 -0
- data/examples/portable.rb +3 -0
- data/examples/ruby.rb +2 -0
- data/lib/xitdb/collection_declaration.rb +39 -0
- data/lib/xitdb/context.rb +154 -8
- data/lib/xitdb/conversion/byte_source.rb +112 -0
- data/lib/xitdb/conversion.rb +35 -73
- data/lib/xitdb/core/array_builder.rb +74 -0
- data/lib/xitdb/core/array_list.rb +92 -8
- data/lib/xitdb/core/b_tree.rb +127 -19
- data/lib/xitdb/core/byte_value.rb +33 -0
- data/lib/xitdb/core/bytes/source.rb +59 -0
- data/lib/xitdb/core/bytes.rb +54 -58
- data/lib/xitdb/core/compaction.rb +49 -7
- data/lib/xitdb/core/crc64.rb +8 -1
- data/lib/xitdb/core/cursor.rb +108 -18
- data/lib/xitdb/core/database.rb +163 -29
- data/lib/xitdb/core/file_store.rb +43 -1
- data/lib/xitdb/core/format.rb +39 -98
- data/lib/xitdb/core/hash_map.rb +119 -17
- data/lib/xitdb/core/hasher.rb +33 -2
- data/lib/xitdb/core/header.rb +98 -0
- data/lib/xitdb/core/linked_array_list.rb +70 -1
- data/lib/xitdb/core/memory_store.rb +23 -224
- data/lib/xitdb/core/offset_map.rb +22 -1
- data/lib/xitdb/core/record.rb +64 -0
- data/lib/xitdb/core/replication.rb +108 -25
- data/lib/xitdb/core/slot.rb +123 -0
- data/lib/xitdb/core/sorted_map/tree.rb +317 -0
- data/lib/xitdb/core/sorted_map.rb +78 -256
- data/lib/xitdb/core/store.rb +286 -0
- data/lib/xitdb/core/transaction.rb +84 -6
- data/lib/xitdb/core.rb +12 -0
- data/lib/xitdb/database.rb +194 -15
- data/lib/xitdb/declarations.rb +33 -33
- data/lib/xitdb/errors.rb +15 -0
- data/lib/xitdb/history.rb +21 -0
- data/lib/xitdb/input_guard.rb +15 -1
- data/lib/xitdb/profiles/portable/adapter.rb +380 -0
- data/lib/xitdb/profiles/portable.rb +129 -243
- data/lib/xitdb/profiles/ruby.rb +206 -24
- data/lib/xitdb/revision.rb +17 -0
- data/lib/xitdb/ruby/codec.rb +61 -54
- data/lib/xitdb/ruby/codec_registry.rb +79 -0
- data/lib/xitdb/ruby/containers.rb +355 -82
- data/lib/xitdb/ruby/encoded_value.rb +38 -0
- data/lib/xitdb/ruby/indexed_page_decoder.rb +72 -0
- data/lib/xitdb/ruby/indexed_page_encoder.rb +58 -0
- data/lib/xitdb/ruby/input.rb +55 -0
- data/lib/xitdb/ruby/key_encoding.rb +43 -5
- data/lib/xitdb/ruby/ordered_keys.rb +46 -3
- data/lib/xitdb/ruby/packed_array.rb +179 -0
- data/lib/xitdb/ruby/packed_map.rb +272 -0
- data/lib/xitdb/ruby/packing_state.rb +84 -0
- data/lib/xitdb/ruby/page_array.rb +218 -0
- data/lib/xitdb/ruby/page_cell.rb +20 -0
- data/lib/xitdb/ruby/page_codec.rb +46 -0
- data/lib/xitdb/ruby/page_decoder.rb +172 -0
- data/lib/xitdb/ruby/page_draft.rb +24 -0
- data/lib/xitdb/ruby/page_encoder.rb +149 -0
- data/lib/xitdb/ruby/page_reference.rb +26 -0
- data/lib/xitdb/ruby/page_row.rb +108 -0
- data/lib/xitdb/ruby/pages.rb +94 -0
- data/lib/xitdb/ruby/scalars.rb +175 -33
- data/lib/xitdb/ruby/wire/reader.rb +138 -0
- data/lib/xitdb/ruby/wire.rb +36 -90
- data/lib/xitdb/ruby.rb +43 -3
- data/lib/xitdb/snapshot.rb +8 -0
- data/lib/xitdb/streams/handle.rb +46 -0
- data/lib/xitdb/streams/reader.rb +122 -0
- data/lib/xitdb/streams/writer.rb +43 -0
- data/lib/xitdb/streams.rb +8 -140
- data/lib/xitdb/uint.rb +17 -0
- data/lib/xitdb/value_walker.rb +86 -16
- data/lib/xitdb/version.rb +2 -1
- data/lib/xitdb/views/array.rb +94 -74
- data/lib/xitdb/views/base.rb +144 -12
- data/lib/xitdb/views/linked_array.rb +7 -1
- data/lib/xitdb/views/map.rb +71 -3
- data/lib/xitdb/views/set.rb +13 -1
- data/lib/xitdb/views/slice.rb +101 -0
- data/lib/xitdb/views/sorted.rb +25 -37
- data/lib/xitdb/views/sorted_map.rb +39 -0
- data/lib/xitdb/views/sorted_set.rb +10 -0
- data/lib/xitdb.rb +3 -0
- data/sig/core.rbs +65 -34
- data/sig/public.rbs +24 -19
- data/sig/ruby.rbs +260 -42
- metadata +42 -1
data/lib/xitdb/views/base.rb
CHANGED
|
@@ -1,30 +1,67 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module XitDB
|
|
4
|
+
# Collection handles that resolve values through an owning snapshot or transaction.
|
|
5
|
+
#
|
|
6
|
+
# Views load entries on demand. Their lifetime ends when the database closes;
|
|
7
|
+
# transaction views also expire when their transaction ends.
|
|
4
8
|
module Views
|
|
5
9
|
# Distinguishes omitted arguments from stored nil values.
|
|
10
|
+
# @api private
|
|
6
11
|
class MissingArgument
|
|
12
|
+
# Freeze the sentinel used to distinguish an omitted argument from nil.
|
|
13
|
+
# @api private
|
|
7
14
|
def initialize
|
|
8
15
|
freeze
|
|
9
16
|
end
|
|
10
17
|
end
|
|
11
18
|
|
|
12
19
|
# Tracks a collection location and its owning snapshot or transaction.
|
|
20
|
+
#
|
|
21
|
+
# Application code obtains views by reading collections from a root.
|
|
22
|
+
# Use {#to_ruby} to produce independent ordinary Ruby containers.
|
|
13
23
|
class Base
|
|
14
24
|
include Enumerable
|
|
15
25
|
|
|
26
|
+
# Largest signed native long accepted by Ruby-style index conversions.
|
|
16
27
|
LONG_MAX = (1 << (([0].pack("l!").bytesize * 8) - 1)) - 1
|
|
17
28
|
private_constant :LONG_MAX
|
|
18
29
|
|
|
30
|
+
# @!attribute [r] context
|
|
31
|
+
# Owner used for lifetime checks and explicit cross-file copying.
|
|
32
|
+
# @return [Context]
|
|
33
|
+
# @!attribute [r] path
|
|
34
|
+
# Immutable selectors from the context root to this collection.
|
|
35
|
+
# @return [::Array<Object>]
|
|
36
|
+
# @!attribute [r] kind
|
|
37
|
+
# Logical map, array, or set variant.
|
|
38
|
+
# @return [Symbol]
|
|
19
39
|
attr_reader :context, :path, :kind
|
|
20
40
|
|
|
21
|
-
|
|
41
|
+
# Bind a view to a collection location; callers obtain views by reading values.
|
|
42
|
+
# @api private
|
|
43
|
+
# @param context [Context] snapshot or transaction owner
|
|
44
|
+
# @param path [::Array<Object>] selectors from the logical root
|
|
45
|
+
# @param kind [Symbol] logical collection kind
|
|
46
|
+
# @param slot [Core::Slot, nil] resolved reference retained until the logical root changes
|
|
47
|
+
def initialize(context:, path:, kind:, slot: nil)
|
|
22
48
|
@context = context
|
|
23
|
-
@path = path.map
|
|
49
|
+
@path = path.map do |selector|
|
|
50
|
+
case selector
|
|
51
|
+
when String then String.new(selector).freeze
|
|
52
|
+
else selector
|
|
53
|
+
end
|
|
54
|
+
end.freeze
|
|
24
55
|
@kind = kind
|
|
56
|
+
@stored_slot = slot
|
|
57
|
+
@stored_root = context.root
|
|
25
58
|
@location = context.observe(@path)
|
|
26
59
|
end
|
|
27
60
|
|
|
61
|
+
# Reject closed handles, ended transactions, and replaced ancestor locations.
|
|
62
|
+
# @api private
|
|
63
|
+
# @return [void]
|
|
64
|
+
# @raise [ClosedError, ExpiredViewError] if this view is no longer usable
|
|
28
65
|
def check!
|
|
29
66
|
context.check!
|
|
30
67
|
return unless @location&.expired
|
|
@@ -32,25 +69,52 @@ module XitDB
|
|
|
32
69
|
raise ExpiredViewError, "view ancestor was replaced"
|
|
33
70
|
end
|
|
34
71
|
|
|
72
|
+
# Resolve the collection's stored reference at its current location.
|
|
73
|
+
# @api private
|
|
74
|
+
# @return [Core::Slot]
|
|
35
75
|
def slot
|
|
36
76
|
context.read do
|
|
37
77
|
check!
|
|
38
|
-
|
|
78
|
+
stored = @stored_slot
|
|
79
|
+
return stored if stored && (!context.transaction || @stored_root.equal?(context.root))
|
|
80
|
+
|
|
81
|
+
stored = context.resolve(path)
|
|
82
|
+
unless frozen?
|
|
83
|
+
@stored_root = context.root
|
|
84
|
+
@stored_slot = stored
|
|
85
|
+
end
|
|
86
|
+
stored
|
|
39
87
|
end
|
|
40
88
|
end
|
|
41
89
|
|
|
90
|
+
# Select the profile's collection operations for this stored value.
|
|
91
|
+
# @api private
|
|
92
|
+
# @return [Profiles::Portable::Adapter, Ruby::Containers]
|
|
42
93
|
def adapter
|
|
43
94
|
context.collection(slot, path: path)
|
|
44
95
|
end
|
|
45
96
|
|
|
97
|
+
# Count entries in the collection. Uncounted hash layouts may need traversal.
|
|
98
|
+
# @return [Integer] nonnegative entry count
|
|
46
99
|
def size
|
|
47
100
|
context.read { adapter.size }
|
|
48
101
|
end
|
|
49
102
|
|
|
103
|
+
# Report whether the collection has no entries.
|
|
104
|
+
# @return [Boolean]
|
|
50
105
|
def empty?
|
|
51
106
|
size.zero?
|
|
52
107
|
end
|
|
53
108
|
|
|
109
|
+
# Visit map key/value pairs, array values, or set members.
|
|
110
|
+
#
|
|
111
|
+
# Adding or deleting entries during iteration raises ConcurrentModificationError.
|
|
112
|
+
# Transaction iteration reads replacement values from their live locations.
|
|
113
|
+
# @yield [key_or_value, value] maps yield two arguments; other collections yield one
|
|
114
|
+
# @yieldparam key_or_value [Object]
|
|
115
|
+
# @yieldparam value [Object] map value only
|
|
116
|
+
# @return [self, Enumerator]
|
|
117
|
+
# @raise [ConcurrentModificationError] if membership changes during iteration
|
|
54
118
|
def each
|
|
55
119
|
return enum_for(:each) unless block_given?
|
|
56
120
|
|
|
@@ -63,14 +127,26 @@ module XitDB
|
|
|
63
127
|
end
|
|
64
128
|
end
|
|
65
129
|
|
|
130
|
+
# Return a stored reference that a compatible destination can share.
|
|
131
|
+
# @api private
|
|
132
|
+
# @return [Core::Slot]
|
|
66
133
|
def slot_for_assignment(**)
|
|
67
134
|
slot
|
|
68
135
|
end
|
|
69
136
|
|
|
137
|
+
# Copy the complete collection into ordinary Ruby containers.
|
|
138
|
+
#
|
|
139
|
+
# The resulting Hash, Array, or Set no longer needs the database handle.
|
|
140
|
+
# This reads every nested entry and needs memory for the complete result.
|
|
141
|
+
# @return [::Hash, ::Array, ::Set] independent materialized value
|
|
142
|
+
# @raise [CyclicValueError] if stored references form a cycle
|
|
70
143
|
def to_ruby
|
|
71
144
|
context.read { ValueWalker.materialize(self) }
|
|
72
145
|
end
|
|
73
146
|
|
|
147
|
+
# Compare fully materialized values using Ruby equality.
|
|
148
|
+
# @param other [Object] Ruby value or another collection view
|
|
149
|
+
# @return [Boolean]
|
|
74
150
|
def ==(other)
|
|
75
151
|
context.read do
|
|
76
152
|
case other
|
|
@@ -80,12 +156,24 @@ module XitDB
|
|
|
80
156
|
end
|
|
81
157
|
end
|
|
82
158
|
|
|
159
|
+
# Describe the view's profile and bounded location without loading its contents.
|
|
160
|
+
# @return [String]
|
|
83
161
|
def inspect
|
|
84
162
|
location = path.first(4).map { |selector| selector.is_a?(String) ? selector.byteslice(0, 32) : selector }
|
|
85
163
|
suffix = path.size > 4 ? "..." : ""
|
|
86
164
|
"#<#{self.class} profile=#{context.profile.class.name} path=#{location.inspect}#{suffix}>"
|
|
87
165
|
end
|
|
88
166
|
|
|
167
|
+
# Read an untagged byte value through a block-scoped stream.
|
|
168
|
+
#
|
|
169
|
+
# Reads can request small chunks without loading the complete value.
|
|
170
|
+
# The reader expires after the block; overlapping writes are blocked while open.
|
|
171
|
+
# @param key [Object] map key or array position
|
|
172
|
+
# @yield [reader]
|
|
173
|
+
# @yieldparam reader [Streams::Reader]
|
|
174
|
+
# @return [Object] block result
|
|
175
|
+
# @raise [KeyError] if the selected entry is absent
|
|
176
|
+
# @raise [InvalidValueError] if the entry is not untagged bytes
|
|
89
177
|
def open_bytes(key)
|
|
90
178
|
raise ArgumentError, "open_bytes requires a block" unless block_given?
|
|
91
179
|
|
|
@@ -94,7 +182,7 @@ module XitDB
|
|
|
94
182
|
registration = nil
|
|
95
183
|
reader = context.read do
|
|
96
184
|
selector = stream_selector(key)
|
|
97
|
-
if %i[array linked_array].include?(kind) && !integer_argument(selector).between?(0, size - 1)
|
|
185
|
+
if %i[array linked_array].include?(kind) && !integer_argument!(selector).between?(0, size - 1)
|
|
98
186
|
raise KeyError, "byte value not found"
|
|
99
187
|
end
|
|
100
188
|
|
|
@@ -110,6 +198,16 @@ module XitDB
|
|
|
110
198
|
context.release_stream(registration) if registration
|
|
111
199
|
end
|
|
112
200
|
|
|
201
|
+
# Replace a byte value after the writer block completes successfully.
|
|
202
|
+
#
|
|
203
|
+
# Data first goes to a temporary file. An exception leaves the entry unchanged.
|
|
204
|
+
# The writer expires after the block. The enclosing transaction must still commit.
|
|
205
|
+
# @param key [Object] map key or array position
|
|
206
|
+
# @yield [writer]
|
|
207
|
+
# @yieldparam writer [Streams::Writer]
|
|
208
|
+
# @return [Object] block result
|
|
209
|
+
# @raise [ReadOnlyError] unless the view belongs to an active writable transaction
|
|
210
|
+
# @raise [ConcurrentModificationError] if another active stream overlaps
|
|
113
211
|
def write_bytes(key)
|
|
114
212
|
raise ArgumentError, "write_bytes requires a block" unless block_given?
|
|
115
213
|
|
|
@@ -117,10 +215,10 @@ module XitDB
|
|
|
117
215
|
context.writable!
|
|
118
216
|
selector = stream_selector(key)
|
|
119
217
|
if %i[array linked_array].include?(kind)
|
|
120
|
-
selector = integer_argument(selector)
|
|
218
|
+
selector = integer_argument!(selector)
|
|
121
219
|
raise IndexError, "array index outside list" if selector.negative?
|
|
122
220
|
|
|
123
|
-
validate_growth(selector + 1)
|
|
221
|
+
validate_growth!(selector + 1)
|
|
124
222
|
end
|
|
125
223
|
context.check_stream_mutation!(path, **stream_mutation(selector))
|
|
126
224
|
Tempfile.create("xitdb-bytes", binmode: true) do |spool|
|
|
@@ -136,9 +234,9 @@ module XitDB
|
|
|
136
234
|
edit(**stream_mutation(selector)) do |collection|
|
|
137
235
|
spool.rewind
|
|
138
236
|
stored = Core::Bytes.copy(spool, size: spool.size, database: context.database.core,
|
|
139
|
-
transaction: context.writable_transaction)
|
|
237
|
+
transaction: context.writable_transaction!)
|
|
140
238
|
if %i[array linked_array].include?(kind)
|
|
141
|
-
position = integer_argument(selector)
|
|
239
|
+
position = integer_argument!(selector)
|
|
142
240
|
collection.assign(collection.size, Core::Slot.null) while collection.size < position
|
|
143
241
|
end
|
|
144
242
|
collection.assign(selector, stored)
|
|
@@ -151,7 +249,9 @@ module XitDB
|
|
|
151
249
|
|
|
152
250
|
protected
|
|
153
251
|
|
|
154
|
-
|
|
252
|
+
# Clear storage only when the profile reports that a change is needed.
|
|
253
|
+
# @return [Boolean] whether entries or profile metadata changed
|
|
254
|
+
def clear_entries
|
|
155
255
|
collection = adapter
|
|
156
256
|
needed = if defined?(Ruby::Containers) && collection.is_a?(Ruby::Containers)
|
|
157
257
|
collection.clear_needed?
|
|
@@ -170,6 +270,11 @@ module XitDB
|
|
|
170
270
|
true
|
|
171
271
|
end
|
|
172
272
|
|
|
273
|
+
# Apply adapter changes inside a context mutation checkpoint.
|
|
274
|
+
# @param options [::Hash] stream conflict locations passed to Context#mutate
|
|
275
|
+
# @yield [collection]
|
|
276
|
+
# @yieldparam collection [Profiles::Portable::Adapter, Ruby::Containers]
|
|
277
|
+
# @return [void]
|
|
173
278
|
def edit(**options)
|
|
174
279
|
context.mutate(path, **options) do |cursor|
|
|
175
280
|
collection = context.collection(cursor.slot, path: path)
|
|
@@ -178,6 +283,9 @@ module XitDB
|
|
|
178
283
|
end
|
|
179
284
|
end
|
|
180
285
|
|
|
286
|
+
# Validate and copy a stream key so caller mutation cannot move its location.
|
|
287
|
+
# @param key [Object]
|
|
288
|
+
# @return [Object] normalized selector
|
|
181
289
|
def stream_selector(key)
|
|
182
290
|
check!
|
|
183
291
|
case kind
|
|
@@ -189,7 +297,8 @@ module XitDB
|
|
|
189
297
|
context.profile.key_bytes(key)
|
|
190
298
|
end
|
|
191
299
|
case key
|
|
192
|
-
when String
|
|
300
|
+
when String then String.new(key).freeze
|
|
301
|
+
when Time then key.dup.freeze
|
|
193
302
|
else key
|
|
194
303
|
end
|
|
195
304
|
when :array, :linked_array then normalize_index(key)
|
|
@@ -197,22 +306,32 @@ module XitDB
|
|
|
197
306
|
end
|
|
198
307
|
end
|
|
199
308
|
|
|
309
|
+
# Describe which key or array positions a stream assignment can affect.
|
|
310
|
+
# @param selector [Object]
|
|
311
|
+
# @return [::Hash] conflict check options
|
|
200
312
|
def stream_mutation(selector)
|
|
201
313
|
if %i[array linked_array].include?(kind)
|
|
202
|
-
position = integer_argument(selector)
|
|
314
|
+
position = integer_argument!(selector)
|
|
203
315
|
{ positions: [size, position].min..position }
|
|
204
316
|
else
|
|
205
317
|
{ targets: [entry_path(selector)] }
|
|
206
318
|
end
|
|
207
319
|
end
|
|
208
320
|
|
|
209
|
-
|
|
321
|
+
# Apply Ruby's implicit integer conversion and native long bounds.
|
|
322
|
+
# @param value [Object]
|
|
323
|
+
# @return [Integer]
|
|
324
|
+
# @raise [TypeError, RangeError] if conversion or bounds validation fails
|
|
325
|
+
def integer_argument!(value)
|
|
210
326
|
result = ::Integer.try_convert(value) || raise(TypeError, "no implicit conversion into Integer")
|
|
211
327
|
raise RangeError, "integer too big to convert into long" unless result.between?(-LONG_MAX - 1, LONG_MAX)
|
|
212
328
|
|
|
213
329
|
result
|
|
214
330
|
end
|
|
215
331
|
|
|
332
|
+
# Retain the current structure before later transaction changes.
|
|
333
|
+
# @param readonly [Boolean] forbid writes through the captured context
|
|
334
|
+
# @return [Context] still subject to the owning transaction's lifetime
|
|
216
335
|
def captured_context(readonly: false)
|
|
217
336
|
stored = slot
|
|
218
337
|
context.transaction&.freeze
|
|
@@ -220,10 +339,18 @@ module XitDB
|
|
|
220
339
|
transaction: context.transaction, readonly: readonly, reference_proof: context.reference_proof)
|
|
221
340
|
end
|
|
222
341
|
|
|
342
|
+
# Extend this collection's path with one child selector.
|
|
343
|
+
# @param selector [Object]
|
|
344
|
+
# @return [::Array<Object>]
|
|
223
345
|
def entry_path(selector)
|
|
224
346
|
path + [selector]
|
|
225
347
|
end
|
|
226
348
|
|
|
349
|
+
# Decode one entry per read operation and check the membership generation.
|
|
350
|
+
# @param entries [#call] creates a storage iterator
|
|
351
|
+
# @param selectors_only [Boolean] skip value decoding
|
|
352
|
+
# @yield [*values] decoded arguments for one entry
|
|
353
|
+
# @return [self]
|
|
227
354
|
def each_entries(entries, selectors_only: false)
|
|
228
355
|
# @type var iterator: Enumerator[entry, adapter]?
|
|
229
356
|
iterator = nil
|
|
@@ -256,6 +383,11 @@ module XitDB
|
|
|
256
383
|
self
|
|
257
384
|
end
|
|
258
385
|
|
|
386
|
+
# Select iterator arguments and resolve live transaction replacements.
|
|
387
|
+
# @param selector [Object]
|
|
388
|
+
# @param stored [Core::Slot]
|
|
389
|
+
# @param selectors_only [Boolean]
|
|
390
|
+
# @return [::Array<Object>] arguments for the consumer block
|
|
259
391
|
def decode_entry(selector, stored, selectors_only:)
|
|
260
392
|
return [selector] if selectors_only || %i[set sorted_set].include?(kind)
|
|
261
393
|
|
|
@@ -4,9 +4,15 @@ module XitDB
|
|
|
4
4
|
module Views
|
|
5
5
|
# Provides array operations backed by the linked positional tree.
|
|
6
6
|
class LinkedArray < Array
|
|
7
|
+
# Capture two read-only slices before and after a split position.
|
|
8
|
+
#
|
|
9
|
+
# The original array is unchanged. Empty sides are valid slices.
|
|
10
|
+
# @param index [Integer] split boundary; negative values count from the end
|
|
11
|
+
# @return [::Array(Slice, Slice)] left and right selections
|
|
12
|
+
# @raise [IndexError] unless the normalized boundary is between zero and size
|
|
7
13
|
def split(index)
|
|
8
14
|
context.read do
|
|
9
|
-
index = integer_argument(index)
|
|
15
|
+
index = integer_argument!(index)
|
|
10
16
|
index += size if index.negative?
|
|
11
17
|
raise IndexError, "array split outside list" unless index.between?(0, size)
|
|
12
18
|
|
data/lib/xitdb/views/map.rb
CHANGED
|
@@ -3,10 +3,18 @@
|
|
|
3
3
|
module XitDB
|
|
4
4
|
module Views
|
|
5
5
|
# Provides key-based reads and writes for persistent maps.
|
|
6
|
+
#
|
|
7
|
+
# Portable maps require String keys. Ruby-profile maps accept typed keys
|
|
8
|
+
# and preserve insertion order. Missing keys and stored nil both read as nil;
|
|
9
|
+
# use {#key?} or {#fetch} when their difference matters.
|
|
6
10
|
class Map < Base
|
|
11
|
+
# Sentinel for an omitted default, distinct from an explicit nil.
|
|
7
12
|
UNSET = MissingArgument.new.freeze
|
|
8
13
|
private_constant :UNSET
|
|
9
14
|
|
|
15
|
+
# Read a value, decoding a scalar or returning a nested collection view.
|
|
16
|
+
# @param key [Object] key supported by the selected profile
|
|
17
|
+
# @return [Object, nil] stored value, or nil when absent
|
|
10
18
|
def [](key)
|
|
11
19
|
context.read do
|
|
12
20
|
stored = adapter.lookup(key)
|
|
@@ -14,6 +22,15 @@ module XitDB
|
|
|
14
22
|
end
|
|
15
23
|
end
|
|
16
24
|
|
|
25
|
+
# Read a key with an explicit missing-key policy.
|
|
26
|
+
#
|
|
27
|
+
# A fallback block takes precedence over the default argument.
|
|
28
|
+
# @param key [Object]
|
|
29
|
+
# @param default [Object] fallback when absent; omitted means raise
|
|
30
|
+
# @yield [key] compute a fallback for an absent key
|
|
31
|
+
# @yieldparam key [Object]
|
|
32
|
+
# @return [Object]
|
|
33
|
+
# @raise [KeyError] if absent without a default or block
|
|
17
34
|
def fetch(key, default = UNSET)
|
|
18
35
|
found, value = context.read do
|
|
19
36
|
present = key?(key)
|
|
@@ -27,13 +44,23 @@ module XitDB
|
|
|
27
44
|
else return default
|
|
28
45
|
end
|
|
29
46
|
|
|
47
|
+
case key
|
|
48
|
+
when String then key = String.new(key)
|
|
49
|
+
end
|
|
30
50
|
raise KeyError, "key not found: #{key.inspect}"
|
|
31
51
|
end
|
|
32
52
|
|
|
53
|
+
# Follow keys and array positions through nested values.
|
|
54
|
+
# @param keys [Array<Object>] successive selectors; at least one is required
|
|
55
|
+
# @return [Object, nil] selected value, or nil when a step is absent
|
|
56
|
+
# @raise [TypeError] if an intermediate value cannot be traversed
|
|
33
57
|
def dig(*keys)
|
|
34
58
|
context.read { dig_value(keys) }
|
|
35
59
|
end
|
|
36
60
|
|
|
61
|
+
# Resolve selectors one at a time, delegating to a scalar object's dig when supported.
|
|
62
|
+
# @param keys [Array<Object>]
|
|
63
|
+
# @return [Object, nil]
|
|
37
64
|
def dig_value(keys)
|
|
38
65
|
raise ArgumentError, "at least one key is required" if keys.empty?
|
|
39
66
|
|
|
@@ -43,7 +70,7 @@ module XitDB
|
|
|
43
70
|
case value
|
|
44
71
|
when nil then return nil
|
|
45
72
|
when Map then value = value[key]
|
|
46
|
-
when Array then value = value[integer_argument(key)]
|
|
73
|
+
when Array then value = value[integer_argument!(key)]
|
|
47
74
|
when Object
|
|
48
75
|
raise TypeError, "#{value.class} does not have #dig method" unless value.respond_to?(:dig)
|
|
49
76
|
|
|
@@ -56,10 +83,17 @@ module XitDB
|
|
|
56
83
|
end
|
|
57
84
|
private :dig_value
|
|
58
85
|
|
|
86
|
+
# Test presence independently of the stored value, including a stored nil.
|
|
87
|
+
# @param key [Object]
|
|
88
|
+
# @return [Boolean]
|
|
59
89
|
def key?(key)
|
|
60
90
|
context.read { !adapter.lookup(key).nil? }
|
|
61
91
|
end
|
|
62
92
|
|
|
93
|
+
# Visit keys in this map's profile-defined order.
|
|
94
|
+
# @yield [key]
|
|
95
|
+
# @yieldparam key [Object]
|
|
96
|
+
# @return [self, Enumerator] self with a block, otherwise an enumerator
|
|
63
97
|
def each_key(&block)
|
|
64
98
|
return enum_for(:each_key) unless block
|
|
65
99
|
|
|
@@ -67,6 +101,10 @@ module XitDB
|
|
|
67
101
|
self
|
|
68
102
|
end
|
|
69
103
|
|
|
104
|
+
# Visit values without building an intermediate Ruby Array.
|
|
105
|
+
# @yield [value]
|
|
106
|
+
# @yieldparam value [Object]
|
|
107
|
+
# @return [self, Enumerator]
|
|
70
108
|
def each_value
|
|
71
109
|
return enum_for(:each_value) unless block_given?
|
|
72
110
|
|
|
@@ -74,14 +112,27 @@ module XitDB
|
|
|
74
112
|
self
|
|
75
113
|
end
|
|
76
114
|
|
|
115
|
+
# Collect the keys into an independent Ruby Array.
|
|
116
|
+
# @return [::Array<Object>]
|
|
77
117
|
def keys
|
|
78
118
|
context.read { each_key.to_a }
|
|
79
119
|
end
|
|
80
120
|
|
|
121
|
+
# Collect values into a Ruby Array; nested collections remain database views.
|
|
122
|
+
# @return [::Array<Object>]
|
|
123
|
+
# @see Base#to_ruby
|
|
81
124
|
def values
|
|
82
125
|
context.read { each_value.to_a }
|
|
83
126
|
end
|
|
84
127
|
|
|
128
|
+
# Insert or replace one entry in the active transaction.
|
|
129
|
+
#
|
|
130
|
+
# Replacing a collection expires views of that location and its children.
|
|
131
|
+
# @param key [Object] key supported by the profile
|
|
132
|
+
# @param value [Object] supported scalar, container, declaration, or compatible view
|
|
133
|
+
# @return [Object] assigned value
|
|
134
|
+
# @raise [ReadOnlyError] if this view is not writable
|
|
135
|
+
# @raise [ForeignValueError] if an assigned view cannot share this storage
|
|
85
136
|
def []=(key, value)
|
|
86
137
|
check!
|
|
87
138
|
context.writable!
|
|
@@ -97,6 +148,14 @@ module XitDB
|
|
|
97
148
|
value
|
|
98
149
|
end
|
|
99
150
|
|
|
151
|
+
# Remove an entry, returning its value or a fallback for an absent key.
|
|
152
|
+
#
|
|
153
|
+
# A removed collection remains a captured view until its transaction ends.
|
|
154
|
+
# @param key [Object]
|
|
155
|
+
# @yield [key] optional fallback when the key is absent
|
|
156
|
+
# @yieldparam key [Object]
|
|
157
|
+
# @return [Object, nil]
|
|
158
|
+
# @raise [ReadOnlyError] if this view is not writable
|
|
100
159
|
def delete(key)
|
|
101
160
|
check!
|
|
102
161
|
context.writable!
|
|
@@ -108,7 +167,7 @@ module XitDB
|
|
|
108
167
|
context.mutate(path, targets: [path + [key]]) do |cursor|
|
|
109
168
|
# Capture before deletion, with loaders inside the mutation checkpoint.
|
|
110
169
|
stored = adapter.lookup(key) || raise(KeyError, "key no longer exists")
|
|
111
|
-
context.writable_transaction
|
|
170
|
+
context.writable_transaction!.freeze
|
|
112
171
|
captured = Context.new(database: context.database, root: stored, bound: context.read_bound,
|
|
113
172
|
transaction: context.transaction)
|
|
114
173
|
value = context.profile.decode(stored, context: captured, path: [])
|
|
@@ -121,16 +180,25 @@ module XitDB
|
|
|
121
180
|
value
|
|
122
181
|
end
|
|
123
182
|
|
|
183
|
+
# Remove every entry and expire child views.
|
|
184
|
+
# @return [self]
|
|
185
|
+
# @raise [ReadOnlyError] if this view is not writable
|
|
124
186
|
def clear
|
|
125
187
|
check!
|
|
126
188
|
context.writable!
|
|
127
|
-
return self unless clear_entries
|
|
189
|
+
return self unless clear_entries
|
|
128
190
|
|
|
129
191
|
context.invalidate_children(path)
|
|
130
192
|
context.change_membership(path)
|
|
131
193
|
self
|
|
132
194
|
end
|
|
133
195
|
|
|
196
|
+
# Assign all entries from a Ruby Hash as one logical mutation.
|
|
197
|
+
#
|
|
198
|
+
# An encoding failure leaves the map unchanged. Duplicate keys replace values.
|
|
199
|
+
# @param hash [::Hash] entries to assign
|
|
200
|
+
# @return [self]
|
|
201
|
+
# @raise [TypeError] if the argument is not an exact Ruby Hash
|
|
134
202
|
def merge!(hash)
|
|
135
203
|
check!
|
|
136
204
|
context.writable!
|
data/lib/xitdb/views/set.rb
CHANGED
|
@@ -4,10 +4,17 @@ module XitDB
|
|
|
4
4
|
module Views
|
|
5
5
|
# Provides membership reads and writes for persistent sets.
|
|
6
6
|
class Set < Base
|
|
7
|
+
# Test whether a member is present using the profile's key identity rules.
|
|
8
|
+
# @param value [Object] supported set member
|
|
9
|
+
# @return [Boolean]
|
|
7
10
|
def include?(value)
|
|
8
11
|
context.read { !adapter.lookup(value).nil? }
|
|
9
12
|
end
|
|
10
13
|
|
|
14
|
+
# Add a member once; adding a present member leaves membership unchanged.
|
|
15
|
+
# @param value [Object]
|
|
16
|
+
# @return [self]
|
|
17
|
+
# @raise [ReadOnlyError] unless this view is writable
|
|
11
18
|
def add(value)
|
|
12
19
|
check!
|
|
13
20
|
context.writable!
|
|
@@ -18,6 +25,9 @@ module XitDB
|
|
|
18
25
|
self
|
|
19
26
|
end
|
|
20
27
|
|
|
28
|
+
# Remove a member if present.
|
|
29
|
+
# @param value [Object]
|
|
30
|
+
# @return [self]
|
|
21
31
|
def delete(value)
|
|
22
32
|
check!
|
|
23
33
|
context.writable!
|
|
@@ -28,10 +38,12 @@ module XitDB
|
|
|
28
38
|
self
|
|
29
39
|
end
|
|
30
40
|
|
|
41
|
+
# Remove all members.
|
|
42
|
+
# @return [self]
|
|
31
43
|
def clear
|
|
32
44
|
check!
|
|
33
45
|
context.writable!
|
|
34
|
-
return self unless clear_entries
|
|
46
|
+
return self unless clear_entries
|
|
35
47
|
|
|
36
48
|
context.change_membership(path)
|
|
37
49
|
self
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module XitDB
|
|
4
|
+
module Views
|
|
5
|
+
# A captured, read-only range of an array.
|
|
6
|
+
#
|
|
7
|
+
# Later writes leave the selection's captured structure unchanged.
|
|
8
|
+
# The selection still expires with its transaction or database handle.
|
|
9
|
+
class Slice < Array
|
|
10
|
+
# Bind a selection to a captured array structure.
|
|
11
|
+
# @api private
|
|
12
|
+
# @param offset [Integer] first backing position
|
|
13
|
+
# @param count [Integer] visible entries
|
|
14
|
+
def initialize(offset:, count:, **)
|
|
15
|
+
super(**)
|
|
16
|
+
@offset = offset
|
|
17
|
+
@count = count
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Wrap the underlying adapter with this slice's visible bounds.
|
|
21
|
+
# @api private
|
|
22
|
+
# @return [Selection]
|
|
23
|
+
def adapter
|
|
24
|
+
Selection.new(super, @offset, @count)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Create storage for just the selected range in a compatible destination.
|
|
28
|
+
# @api private
|
|
29
|
+
# @param context [Context]
|
|
30
|
+
# @param path [::Array<Object>] destination location
|
|
31
|
+
# @return [Core::Slot]
|
|
32
|
+
def slot_for_assignment(context:, path:)
|
|
33
|
+
context.collection(slot, path: path).slice(@offset, @count)
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
protected
|
|
37
|
+
|
|
38
|
+
# Return the start position within the original backing array.
|
|
39
|
+
# @return [Integer]
|
|
40
|
+
def slice_offset
|
|
41
|
+
@offset
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# Translate a slice-relative selector to its backing array position.
|
|
45
|
+
# @param selector [Integer]
|
|
46
|
+
# @return [::Array<Object>]
|
|
47
|
+
def entry_path(selector)
|
|
48
|
+
path + [@offset + selector]
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# Limits the adapter's visible entries to the captured range.
|
|
52
|
+
# @api private
|
|
53
|
+
class Selection
|
|
54
|
+
# @return [Integer] number of entries visible in this selection
|
|
55
|
+
attr_reader :size
|
|
56
|
+
|
|
57
|
+
# Restrict a storage adapter to a fixed range without decoding entries.
|
|
58
|
+
# @param source [Profiles::Portable::Adapter, Ruby::Containers]
|
|
59
|
+
# @param offset [Integer]
|
|
60
|
+
# @param count [Integer]
|
|
61
|
+
def initialize(source, offset, count)
|
|
62
|
+
@source = source
|
|
63
|
+
@offset = offset
|
|
64
|
+
@size = count
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Preserve the backing array's logical collection kind.
|
|
68
|
+
# @return [Symbol]
|
|
69
|
+
def kind
|
|
70
|
+
@source.kind
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Translate a selection-relative position into the backing adapter.
|
|
74
|
+
# @param index [Integer]
|
|
75
|
+
# @return [Core::Slot, nil]
|
|
76
|
+
def lookup(index)
|
|
77
|
+
@source.lookup(@offset + index)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Visit selected slots with positions starting at zero.
|
|
81
|
+
# @yield [index, slot]
|
|
82
|
+
# @yieldparam index [Integer]
|
|
83
|
+
# @yieldparam slot [Core::Slot]
|
|
84
|
+
# @return [self, Enumerator]
|
|
85
|
+
def each_entry
|
|
86
|
+
return enum_for(:each_entry) unless block_given?
|
|
87
|
+
return self if size.zero?
|
|
88
|
+
|
|
89
|
+
index = 0
|
|
90
|
+
@source.each_from_index(@offset) do |_selector, stored|
|
|
91
|
+
yield index, stored
|
|
92
|
+
index += 1
|
|
93
|
+
break if index == size
|
|
94
|
+
end
|
|
95
|
+
self
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
private_constant :Selection
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
end
|