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/context.rb
CHANGED
|
@@ -2,12 +2,41 @@
|
|
|
2
2
|
|
|
3
3
|
module XitDB
|
|
4
4
|
# Owns one captured root or one transaction's logical root.
|
|
5
|
+
#
|
|
6
|
+
# A path is a sequence of map keys or array positions from the root.
|
|
7
|
+
# Views share this context so they agree on lifetime, writes, and stream locks.
|
|
8
|
+
# The explicit copy operation is available through a view's context.
|
|
9
|
+
#
|
|
10
|
+
# @see #copy_from
|
|
5
11
|
class Context
|
|
12
|
+
# A weakly retained observation of a view's path and membership generation.
|
|
6
13
|
Location = Struct.new(:path, :membership, :expired)
|
|
7
14
|
private_constant :Location
|
|
8
15
|
|
|
16
|
+
# @!attribute [r] database
|
|
17
|
+
# Handle that owns this context's storage.
|
|
18
|
+
# @return [Database]
|
|
19
|
+
# @!attribute [r] profile
|
|
20
|
+
# Rules for interpreting this context's values.
|
|
21
|
+
# @return [Profiles::Portable, Profiles::Ruby]
|
|
22
|
+
# @!attribute [r] transaction
|
|
23
|
+
# Writer whose lifetime this context shares, or nil for snapshots.
|
|
24
|
+
# @return [Core::Transaction, nil]
|
|
25
|
+
# @!attribute [r] root
|
|
26
|
+
# Stored logical root, updated only after a successful mutation.
|
|
27
|
+
# @return [Core::Slot]
|
|
28
|
+
# @!attribute [r] reference_proof
|
|
29
|
+
# Captured trailer bytes used to validate safe reference sharing.
|
|
30
|
+
# @return [String, nil]
|
|
9
31
|
attr_reader :database, :profile, :transaction, :root, :reference_proof
|
|
10
32
|
|
|
33
|
+
# Capture the root, readable byte boundary, and optional transaction for a family of views.
|
|
34
|
+
# @param database [Database]
|
|
35
|
+
# @param root [Core::Slot] logical root
|
|
36
|
+
# @param bound [Integer] exclusive byte boundary of the captured commit
|
|
37
|
+
# @param transaction [Core::Transaction, nil] active writer, or nil for a snapshot
|
|
38
|
+
# @param readonly [Boolean] forbid writes even when a transaction exists
|
|
39
|
+
# @param reference_proof [String, nil] captured trailer bytes proving the file generation
|
|
11
40
|
def initialize(database:, root:, bound:, transaction: nil, readonly: false, reference_proof: nil)
|
|
12
41
|
@database = database
|
|
13
42
|
@profile = database.profile
|
|
@@ -24,11 +53,16 @@ module XitDB
|
|
|
24
53
|
).freeze
|
|
25
54
|
end
|
|
26
55
|
|
|
56
|
+
# Require an open database and, for writable views, an active transaction.
|
|
57
|
+
# @return [void]
|
|
27
58
|
def check!
|
|
28
59
|
database.core.check_open!
|
|
29
60
|
transaction&.check_active!
|
|
30
61
|
end
|
|
31
62
|
|
|
63
|
+
# Keep storage open for one read operation and validate this context.
|
|
64
|
+
# @yield perform the read
|
|
65
|
+
# @return [Object] block result
|
|
32
66
|
def read
|
|
33
67
|
database.core.with_operation do
|
|
34
68
|
check!
|
|
@@ -36,6 +70,9 @@ module XitDB
|
|
|
36
70
|
end
|
|
37
71
|
end
|
|
38
72
|
|
|
73
|
+
# Require a writable context and permit entry into a logical mutation.
|
|
74
|
+
# @return [void]
|
|
75
|
+
# @raise [ReadOnlyError] if this context is read-only
|
|
39
76
|
def writable!
|
|
40
77
|
check!
|
|
41
78
|
raise ReadOnlyError, "view is read-only" if @readonly || !transaction
|
|
@@ -43,22 +80,38 @@ module XitDB
|
|
|
43
80
|
transaction.check_logical_mutation!
|
|
44
81
|
end
|
|
45
82
|
|
|
46
|
-
|
|
83
|
+
# Retrieve the active writer after checking context lifetime and permissions.
|
|
84
|
+
# @return [Core::Transaction]
|
|
85
|
+
# @raise [ReadOnlyError] if no writable transaction exists
|
|
86
|
+
def writable_transaction!
|
|
47
87
|
check!
|
|
48
88
|
raise ReadOnlyError, "view is read-only" if @readonly
|
|
49
89
|
|
|
50
90
|
transaction || raise(ReadOnlyError, "view is read-only")
|
|
51
91
|
end
|
|
52
92
|
|
|
93
|
+
# Interpret a slot as a collection under the selected profile.
|
|
94
|
+
# @param slot [Core::Slot]
|
|
95
|
+
# @param path [Array<Object>] diagnostic and view location
|
|
96
|
+
# @return [Profiles::Portable::Adapter, Ruby::Containers]
|
|
97
|
+
# @raise [ExpiredViewError] if the slot no longer names a collection
|
|
53
98
|
def collection(slot, path:)
|
|
54
99
|
profile.container(slot, context: self, path: path) || raise(ExpiredViewError, "view location is not a collection")
|
|
55
100
|
end
|
|
56
101
|
|
|
102
|
+
# Return the exclusive byte boundary this context may read.
|
|
103
|
+
# @return [Integer] transaction length or captured commit boundary
|
|
57
104
|
def read_bound
|
|
58
105
|
check!
|
|
59
106
|
transaction ? transaction.length : @bound
|
|
60
107
|
end
|
|
61
108
|
|
|
109
|
+
# Check whether a destination can safely reuse this context's stored references.
|
|
110
|
+
#
|
|
111
|
+
# Matching paths alone are insufficient. Storage identity, profile, header, and
|
|
112
|
+
# commit proof must agree; transaction references require the same transaction.
|
|
113
|
+
# @param destination [Context]
|
|
114
|
+
# @return [Boolean]
|
|
62
115
|
def reference_compatible?(destination)
|
|
63
116
|
check!
|
|
64
117
|
destination.check!
|
|
@@ -76,6 +129,9 @@ module XitDB
|
|
|
76
129
|
false
|
|
77
130
|
end
|
|
78
131
|
|
|
132
|
+
# Create a storage cursor with this context's lifetime and readable boundary.
|
|
133
|
+
# @param slot [Core::Slot]
|
|
134
|
+
# @return [Core::Cursor]
|
|
79
135
|
def cursor(slot)
|
|
80
136
|
check!
|
|
81
137
|
if transaction
|
|
@@ -85,6 +141,10 @@ module XitDB
|
|
|
85
141
|
end
|
|
86
142
|
end
|
|
87
143
|
|
|
144
|
+
# Follow selectors from the logical root to a stored slot.
|
|
145
|
+
# @param path [Array<Object>]
|
|
146
|
+
# @return [Core::Slot]
|
|
147
|
+
# @raise [ExpiredViewError] if any selected location is absent
|
|
88
148
|
def resolve(path)
|
|
89
149
|
check!
|
|
90
150
|
slot = root
|
|
@@ -95,16 +155,33 @@ module XitDB
|
|
|
95
155
|
slot
|
|
96
156
|
end
|
|
97
157
|
|
|
98
|
-
|
|
158
|
+
# Bind a public view to a snapshot reference or live transaction location.
|
|
159
|
+
# @param slot [Core::Slot] resolved value; snapshot views retain this immutable reference
|
|
160
|
+
# @param path [Array<Object>]
|
|
161
|
+
# @param kind [Symbol] logical collection kind
|
|
162
|
+
# @return [Views::Base]
|
|
163
|
+
def view(slot, path:, kind:)
|
|
99
164
|
klass = { map: Views::Map, sorted_map: Views::SortedMap, array: Views::Array,
|
|
100
165
|
linked_array: Views::LinkedArray, set: Views::Set, sorted_set: Views::SortedSet }.fetch(kind)
|
|
101
|
-
klass.new(context: self, path: path, kind: kind)
|
|
166
|
+
klass.new(context: self, path: path, kind: kind, slot: slot)
|
|
102
167
|
end
|
|
103
168
|
|
|
169
|
+
# Report whether a successful mutation published a different logical root.
|
|
170
|
+
# @return [Boolean]
|
|
104
171
|
def changed?
|
|
105
172
|
@changed
|
|
106
173
|
end
|
|
107
174
|
|
|
175
|
+
# Copy a view into this transaction, including across files or profiles.
|
|
176
|
+
#
|
|
177
|
+
# Assignment can share compatible references. This operation explicitly copies
|
|
178
|
+
# logical contents and can transform unsupported values and keys.
|
|
179
|
+
# @param view [Views::Base] source collection
|
|
180
|
+
# @param at [Array<Object>] destination selectors; an empty path replaces the root
|
|
181
|
+
# @param transform [#call, nil] callback receiving value:, path:, and role:
|
|
182
|
+
# @return [Object] destination's decoded value
|
|
183
|
+
# @raise [TypeError] if the source, path, or transform is invalid
|
|
184
|
+
# @raise [KeyCollisionError] if transformed keys collide
|
|
108
185
|
def copy_from(view, at:, transform: nil)
|
|
109
186
|
writable!
|
|
110
187
|
raise TypeError, "copy source must be a view" unless Object.instance_method(:is_a?).bind_call(view, Views::Base)
|
|
@@ -125,9 +202,20 @@ module XitDB
|
|
|
125
202
|
end
|
|
126
203
|
|
|
127
204
|
# Publish the logical root only after the complete operation succeeds.
|
|
205
|
+
# Change a collection and rebuild ancestor references only after success.
|
|
206
|
+
#
|
|
207
|
+
# A checkpoint records temporary writes. Failure restores that checkpoint,
|
|
208
|
+
# so a partially encoded value cannot become the context's root.
|
|
209
|
+
# @param path [Array<Object>] collection location
|
|
210
|
+
# @param targets [Array<Array<Object>>, nil] exact affected entry paths
|
|
211
|
+
# @param positions [Range, nil] affected array positions
|
|
212
|
+
# @yield [cursor] change the selected storage slot
|
|
213
|
+
# @yieldparam cursor [Core::Cursor]
|
|
214
|
+
# @return [Object] block result
|
|
215
|
+
# @raise [ConcurrentModificationError] if the change overlaps an active stream
|
|
128
216
|
def mutate(path, targets: nil, positions: nil)
|
|
129
217
|
writable!
|
|
130
|
-
transaction = writable_transaction
|
|
218
|
+
transaction = writable_transaction!
|
|
131
219
|
transaction.with_logical_mutation do
|
|
132
220
|
check_stream_mutation!(path, targets: targets, positions: positions)
|
|
133
221
|
checkpoint = transaction.checkpoint
|
|
@@ -163,6 +251,11 @@ module XitDB
|
|
|
163
251
|
end
|
|
164
252
|
end
|
|
165
253
|
|
|
254
|
+
# Reserve a byte location while a reader or writer block is active.
|
|
255
|
+
# @param path [Array<Object>]
|
|
256
|
+
# @param write [Boolean] whether this stream writes
|
|
257
|
+
# @return [Array] registration token for release_stream
|
|
258
|
+
# @raise [ConcurrentModificationError] if an overlapping stream includes a writer
|
|
166
259
|
def register_stream(path, write:)
|
|
167
260
|
check!
|
|
168
261
|
key = path_key(path)
|
|
@@ -178,10 +271,16 @@ module XitDB
|
|
|
178
271
|
registration
|
|
179
272
|
end
|
|
180
273
|
|
|
274
|
+
# Release the exact reservation token when a stream block ends.
|
|
275
|
+
# @param registration [Array]
|
|
276
|
+
# @return [void]
|
|
181
277
|
def release_stream(registration)
|
|
182
278
|
@streams&.delete_if { |item| item.equal?(registration) }
|
|
183
279
|
end
|
|
184
280
|
|
|
281
|
+
# Track a transaction view's location without keeping the view alive.
|
|
282
|
+
# @param path [Array<Object>]
|
|
283
|
+
# @return [Location, nil] nil for a snapshot context
|
|
185
284
|
def observe(path)
|
|
186
285
|
check!
|
|
187
286
|
locations = @locations
|
|
@@ -192,10 +291,17 @@ module XitDB
|
|
|
192
291
|
location
|
|
193
292
|
end
|
|
194
293
|
|
|
294
|
+
# Expire observed views at or below a replaced location.
|
|
295
|
+
# @param path [Array<Object>]
|
|
296
|
+
# @return [void]
|
|
195
297
|
def replace_location(path)
|
|
196
298
|
each_location(path) { |location| location.expired = true }
|
|
197
299
|
end
|
|
198
300
|
|
|
301
|
+
# Expire child array views whose positions can have shifted.
|
|
302
|
+
# @param path [Array<Object>] parent collection
|
|
303
|
+
# @param start [Integer] first affected position
|
|
304
|
+
# @return [void]
|
|
199
305
|
def invalidate_positions(path, start)
|
|
200
306
|
each_location(path) do |location|
|
|
201
307
|
index = location.path[path.size]
|
|
@@ -203,14 +309,25 @@ module XitDB
|
|
|
203
309
|
end
|
|
204
310
|
end
|
|
205
311
|
|
|
312
|
+
# Advance the generation checked by iterators at this collection.
|
|
313
|
+
# @param path [Array<Object>]
|
|
314
|
+
# @return [void]
|
|
206
315
|
def change_membership(path)
|
|
207
316
|
each_location(path) { |location| location.membership += 1 if location.path.size == path.size }
|
|
208
317
|
end
|
|
209
318
|
|
|
319
|
+
# Expire descendants while retaining a view of the collection itself.
|
|
320
|
+
# @param path [Array<Object>]
|
|
321
|
+
# @return [void]
|
|
210
322
|
def invalidate_children(path)
|
|
211
323
|
each_location(path) { |location| location.expired = true if location.path.size > path.size }
|
|
212
324
|
end
|
|
213
325
|
|
|
326
|
+
# Reject a mutation whose paths or shifted positions affect an active byte stream.
|
|
327
|
+
# @param path [Array<Object>]
|
|
328
|
+
# @param targets [Array<Array<Object>>, nil]
|
|
329
|
+
# @param positions [Range, nil]
|
|
330
|
+
# @return [void]
|
|
214
331
|
def check_stream_mutation!(path, targets: nil, positions: nil)
|
|
215
332
|
streams = @streams
|
|
216
333
|
return if !streams || streams.empty?
|
|
@@ -226,10 +343,14 @@ module XitDB
|
|
|
226
343
|
|
|
227
344
|
private
|
|
228
345
|
|
|
346
|
+
# Translate exact mutation targets into profile-aware identity paths.
|
|
347
|
+
# @return [Array<Array<Object>>]
|
|
229
348
|
def mutation_keys(path, targets, positions)
|
|
230
349
|
(targets || (positions ? [] : [path])).map { |target| path_key(target) }
|
|
231
350
|
end
|
|
232
351
|
|
|
352
|
+
# Freeze the source structure and require a map when replacing the root.
|
|
353
|
+
# @return [Array(Context, Object)] source context and adapter
|
|
233
354
|
def capture_copy_source(view, at)
|
|
234
355
|
view.check!
|
|
235
356
|
source_slot = view.slot
|
|
@@ -245,6 +366,8 @@ module XitDB
|
|
|
245
366
|
[source, source_adapter]
|
|
246
367
|
end
|
|
247
368
|
|
|
369
|
+
# Validate destination selectors and own mutable key values before copying.
|
|
370
|
+
# @return [Array<Object>] normalized destination path
|
|
248
371
|
def copy_location(at)
|
|
249
372
|
# @type var location: path
|
|
250
373
|
location = []
|
|
@@ -255,7 +378,7 @@ module XitDB
|
|
|
255
378
|
raise TypeError, "copy parent must be a collection" unless adapter
|
|
256
379
|
raise TypeError, "cannot assign a collection to a set member" if set_kinds.include?(adapter.kind)
|
|
257
380
|
|
|
258
|
-
selector = copy_array_position(selector, adapter.size) if array_kinds.include?(adapter.kind)
|
|
381
|
+
selector = copy_array_position!(selector, adapter.size) if array_kinds.include?(adapter.kind)
|
|
259
382
|
adapter.lookup(selector)
|
|
260
383
|
location << case selector
|
|
261
384
|
when Time then selector.dup.freeze
|
|
@@ -267,7 +390,9 @@ module XitDB
|
|
|
267
390
|
location
|
|
268
391
|
end
|
|
269
392
|
|
|
270
|
-
|
|
393
|
+
# Normalize a copy position and validate native array growth bounds.
|
|
394
|
+
# @return [Integer]
|
|
395
|
+
def copy_array_position!(selector, size)
|
|
271
396
|
selector = Integer.try_convert(selector) || raise(TypeError, "array position must be an Integer")
|
|
272
397
|
long_max = (1 << (([0].pack("l!").bytesize * 8) - 1)) - 1
|
|
273
398
|
raise RangeError, "array position is too large" unless ((-long_max - 1)..long_max).cover?(selector)
|
|
@@ -279,6 +404,8 @@ module XitDB
|
|
|
279
404
|
selector
|
|
280
405
|
end
|
|
281
406
|
|
|
407
|
+
# Guard both source and destination transactions while performing a logical copy.
|
|
408
|
+
# @return [void]
|
|
282
409
|
def perform_copy(source, source_adapter, location, parent, transform)
|
|
283
410
|
operation = lambda do
|
|
284
411
|
mutate(location.empty? ? [] : parent, targets: [location]) do |cursor|
|
|
@@ -293,6 +420,8 @@ module XitDB
|
|
|
293
420
|
end
|
|
294
421
|
end
|
|
295
422
|
|
|
423
|
+
# Encode copied contents and replace the root or selected child entry.
|
|
424
|
+
# @return [void]
|
|
296
425
|
def assign_copy(cursor, source, source_adapter, location, parent, transform)
|
|
297
426
|
copied = Conversion.new(source, self, transform: transform).copy(source.root, adapter: source_adapter)
|
|
298
427
|
if location.empty?
|
|
@@ -308,6 +437,8 @@ module XitDB
|
|
|
308
437
|
end
|
|
309
438
|
end
|
|
310
439
|
|
|
440
|
+
# Expire replaced views and update membership after a successful copy.
|
|
441
|
+
# @return [void]
|
|
311
442
|
def refresh_copy_location(location, parent, present)
|
|
312
443
|
if location.empty?
|
|
313
444
|
invalidate_children([])
|
|
@@ -318,28 +449,43 @@ module XitDB
|
|
|
318
449
|
end
|
|
319
450
|
end
|
|
320
451
|
|
|
452
|
+
# Test whether an active stream uses an affected array position or ancestor.
|
|
453
|
+
# @return [Boolean]
|
|
321
454
|
def position_conflict?(parent, active, positions)
|
|
322
455
|
return false unless parent && positions && overlapping_paths?(parent, active)
|
|
323
456
|
|
|
324
457
|
active.size <= parent.size || positions.cover?(active[parent.size])
|
|
325
458
|
end
|
|
326
459
|
|
|
460
|
+
# Test whether either identity path is a prefix of the other.
|
|
461
|
+
# @return [Boolean]
|
|
327
462
|
def overlapping_paths?(left, right)
|
|
328
463
|
left.first(right.size).eql?(right) || right.first(left.size).eql?(left)
|
|
329
464
|
end
|
|
330
465
|
|
|
466
|
+
# Visit live descendant observations and discard expired weak references.
|
|
467
|
+
# @yield [location]
|
|
468
|
+
# @yieldparam location [Location]
|
|
469
|
+
# @return [void]
|
|
331
470
|
def each_location(path)
|
|
332
|
-
key
|
|
471
|
+
# @type var key: path?
|
|
472
|
+
key = nil
|
|
333
473
|
retained = ObjectSpace::WeakMap.new
|
|
334
474
|
@locations&.each_key do |location|
|
|
335
475
|
next if location.expired
|
|
336
476
|
|
|
337
|
-
|
|
477
|
+
if location.path.size >= path.size
|
|
478
|
+
matched_key = key || path_key(path)
|
|
479
|
+
key = matched_key
|
|
480
|
+
yield location if location.path.first(matched_key.size).eql?(matched_key)
|
|
481
|
+
end
|
|
338
482
|
retained[location] = true unless location.expired
|
|
339
483
|
end
|
|
340
484
|
@locations = retained
|
|
341
485
|
end
|
|
342
486
|
|
|
487
|
+
# Represent each selector using its parent collection's key identity rules.
|
|
488
|
+
# @return [Array<Object>]
|
|
343
489
|
def path_key(path)
|
|
344
490
|
slot = root
|
|
345
491
|
path.each_with_index.map do |selector, index|
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module XitDB
|
|
4
|
+
class Conversion
|
|
5
|
+
# A bounded byte range, including tagged bytes and Ruby String payloads.
|
|
6
|
+
class ByteSource
|
|
7
|
+
# Captured byte length and optional format label for this source.
|
|
8
|
+
#
|
|
9
|
+
# @!attribute [r] size
|
|
10
|
+
# Number of bytes visible through this source.
|
|
11
|
+
# @return [Integer]
|
|
12
|
+
# @!attribute [r] format_tag
|
|
13
|
+
# Optional two-byte label attached to the stored byte value.
|
|
14
|
+
# @return [String, nil]
|
|
15
|
+
attr_reader :size, :format_tag
|
|
16
|
+
|
|
17
|
+
# Capture a bounded byte source without loading its payload.
|
|
18
|
+
# @param context [Context]
|
|
19
|
+
# @param slot [Core::Slot]
|
|
20
|
+
def initialize(context, slot)
|
|
21
|
+
@source = Core::Bytes.open(context.database.core, slot, bound: context.read_bound)
|
|
22
|
+
@start = 0
|
|
23
|
+
@size = @source.size
|
|
24
|
+
@format_tag = @source.format_tag
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Read a checked byte range relative to this source's start.
|
|
28
|
+
# @param position [Integer]
|
|
29
|
+
# @param length [Integer]
|
|
30
|
+
# @return [String]
|
|
31
|
+
# @raise [InvalidValueError] if the requested range exceeds this source
|
|
32
|
+
def read(position, length)
|
|
33
|
+
unless position >= 0 && length >= 0 && position + length <= size
|
|
34
|
+
raise InvalidValueError, "truncated byte payload"
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
@source.read(@start + position, length)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Decode a minimal base-128 unsigned integer and report the next byte position.
|
|
41
|
+
# @param position [Integer]
|
|
42
|
+
# @return [Array(Integer, Integer)] integer and next position
|
|
43
|
+
def unsigned(position)
|
|
44
|
+
value = shift = 0
|
|
45
|
+
while true
|
|
46
|
+
part = read(position, 1).getbyte(0) || raise(InvalidValueError, "truncated byte payload")
|
|
47
|
+
position += 1
|
|
48
|
+
value |= (part & 0x7f) << shift
|
|
49
|
+
if part < 0x80
|
|
50
|
+
raise InvalidValueError, "nonminimal unsigned field" if shift.positive? && part.zero?
|
|
51
|
+
|
|
52
|
+
break
|
|
53
|
+
end
|
|
54
|
+
shift += 7
|
|
55
|
+
raise InvalidValueError, "byte field length exceeds storage range" if shift >= 70
|
|
56
|
+
end
|
|
57
|
+
[value, position]
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# Create a bounded subsource that shares the same underlying storage.
|
|
61
|
+
# @param position [Integer]
|
|
62
|
+
# @param length [Integer]
|
|
63
|
+
# @return [ByteSource]
|
|
64
|
+
def slice(position, length)
|
|
65
|
+
read(position + length, 0)
|
|
66
|
+
output = dup
|
|
67
|
+
output.instance_variable_set(:@size, length)
|
|
68
|
+
output.instance_variable_set(:@format_tag, nil)
|
|
69
|
+
output.instance_variable_set(:@start, @start + position)
|
|
70
|
+
output
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Validate UTF-8 in chunks and select UTF-8 or binary for a portable byte payload.
|
|
74
|
+
# @return [Encoding]
|
|
75
|
+
def portable_encoding
|
|
76
|
+
converter = Encoding::Converter.new(Encoding::UTF_8, Encoding::UTF_16LE)
|
|
77
|
+
each_chunk { |chunk| converter.convert(chunk) }
|
|
78
|
+
converter.finish
|
|
79
|
+
Encoding::UTF_8
|
|
80
|
+
rescue Encoding::InvalidByteSequenceError, Encoding::UndefinedConversionError
|
|
81
|
+
Encoding::BINARY
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Append this source to a destination transaction in bounded chunks.
|
|
85
|
+
# @param context [Context]
|
|
86
|
+
# @param prefix [String] optional bytes preceding the source payload
|
|
87
|
+
# @param format_tag [String, nil]
|
|
88
|
+
# @return [Core::Slot]
|
|
89
|
+
def write_to(context, prefix: "".b, format_tag: @format_tag)
|
|
90
|
+
Core::Bytes.append(context.writable_transaction!, size: size, prefix: prefix, format_tag: format_tag,
|
|
91
|
+
representation: :external) do |position, length|
|
|
92
|
+
read(position, length)
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
private
|
|
97
|
+
|
|
98
|
+
# Visit bounded pieces without retaining the complete byte value.
|
|
99
|
+
# @yield [chunk]
|
|
100
|
+
# @yieldparam chunk [String]
|
|
101
|
+
# @return [void]
|
|
102
|
+
def each_chunk
|
|
103
|
+
position = 0
|
|
104
|
+
while position < size
|
|
105
|
+
chunk = read(position, [size - position, Core::Bytes::CHUNK_SIZE].min)
|
|
106
|
+
yield chunk
|
|
107
|
+
position += chunk.bytesize
|
|
108
|
+
end
|
|
109
|
+
end
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
end
|
data/lib/xitdb/conversion.rb
CHANGED
|
@@ -2,13 +2,30 @@
|
|
|
2
2
|
|
|
3
3
|
module XitDB
|
|
4
4
|
# Copies logical entries with one iterator per ancestor and no node cache.
|
|
5
|
+
#
|
|
6
|
+
# Profiles interpret source values and construct destination values. An
|
|
7
|
+
# explicit stack avoids Ruby call-stack limits on deeply nested input.
|
|
8
|
+
# @api private
|
|
5
9
|
class Conversion
|
|
10
|
+
# Bind logical value readers and writers, with an optional application transform.
|
|
11
|
+
# @param source [Context]
|
|
12
|
+
# @param destination [Context] writable destination
|
|
13
|
+
# @param transform [#call, nil] receives value:, path:, and role:
|
|
6
14
|
def initialize(source, destination, transform: nil)
|
|
7
15
|
@source = source
|
|
8
16
|
@destination = destination
|
|
9
17
|
@transform = transform
|
|
10
18
|
end
|
|
11
19
|
|
|
20
|
+
# Copy one value with an explicit stack of ancestors rather than recursive calls.
|
|
21
|
+
#
|
|
22
|
+
# Only the active traversal path retains iterators. Repeated references can be
|
|
23
|
+
# copied more than once; a reference to an active ancestor is rejected.
|
|
24
|
+
# @param slot [Core::Slot] source value
|
|
25
|
+
# @param path [Array<Object>] source location for diagnostics and transforms
|
|
26
|
+
# @param adapter [Object, nil] optional selected container adapter
|
|
27
|
+
# @return [Core::Slot] encoded destination value
|
|
28
|
+
# @raise [CyclicValueError] if stored values form an ancestor cycle
|
|
12
29
|
def copy(slot, path: [], adapter: nil)
|
|
13
30
|
# @type var stack: ::Array[conversion_frame]
|
|
14
31
|
stack = []
|
|
@@ -45,6 +62,8 @@ module XitDB
|
|
|
45
62
|
|
|
46
63
|
private
|
|
47
64
|
|
|
65
|
+
# Copy a scalar immediately, or push a container frame after checking for a cycle.
|
|
66
|
+
# @return [Core::Slot, nil] nil when the container needs further traversal
|
|
48
67
|
def begin_value(stored, location, selected, stack, active)
|
|
49
68
|
adapter = selected || @source.profile.container(stored, context: @source, path: location)
|
|
50
69
|
return copy_scalar(stored, location) unless adapter
|
|
@@ -60,6 +79,8 @@ module XitDB
|
|
|
60
79
|
nil
|
|
61
80
|
end
|
|
62
81
|
|
|
82
|
+
# Stream compatible bytes directly or decode, transform, and encode a scalar.
|
|
83
|
+
# @return [Core::Slot]
|
|
63
84
|
def copy_scalar(stored, location)
|
|
64
85
|
bytes = @source.profile.conversion_bytes(stored, context: @source) unless @transform
|
|
65
86
|
if bytes
|
|
@@ -79,11 +100,15 @@ module XitDB
|
|
|
79
100
|
@destination.profile.encode(value, context: @destination, path: location)
|
|
80
101
|
end
|
|
81
102
|
|
|
103
|
+
# Assign a completed child to the current destination adapter.
|
|
104
|
+
# @return [void]
|
|
82
105
|
def assign_parent(stack, stored)
|
|
83
106
|
parent = stack.last || raise(CorruptionError, "missing conversion parent")
|
|
84
107
|
parent[3].assign(parent[4], stored)
|
|
85
108
|
end
|
|
86
109
|
|
|
110
|
+
# Finish a container, remove its active identity, and attach it to its parent.
|
|
111
|
+
# @return [Core::Slot]
|
|
87
112
|
def finish_frame(frame, stack, active)
|
|
88
113
|
stored = frame[3].slot
|
|
89
114
|
active.delete(frame[0])
|
|
@@ -93,12 +118,16 @@ module XitDB
|
|
|
93
118
|
end
|
|
94
119
|
|
|
95
120
|
# Callback StopIteration must abort the operation, so rescue only #next.
|
|
121
|
+
# Read the next source entry, rescuing exhaustion only from the iterator.
|
|
122
|
+
# @return [Array, nil] nil at the end
|
|
96
123
|
def next_entry(frame)
|
|
97
124
|
frame[2].next
|
|
98
125
|
rescue StopIteration
|
|
99
126
|
nil
|
|
100
127
|
end
|
|
101
128
|
|
|
129
|
+
# Transform a key, detect collisions, and choose the next child to copy.
|
|
130
|
+
# @return [Array(Core::Slot, Array), nil] nil for set members
|
|
102
131
|
def select_child(frame, entry)
|
|
103
132
|
selector, child = entry
|
|
104
133
|
child_path = frame[1] + [selector]
|
|
@@ -123,6 +152,8 @@ module XitDB
|
|
|
123
152
|
end
|
|
124
153
|
end
|
|
125
154
|
|
|
155
|
+
# Apply the key callback and reject unsupported destination key classes.
|
|
156
|
+
# @return [Object] transformed selector
|
|
126
157
|
def transform_key(value, path)
|
|
127
158
|
converted = transform(value, path, :key)
|
|
128
159
|
case converted
|
|
@@ -133,6 +164,8 @@ module XitDB
|
|
|
133
164
|
end
|
|
134
165
|
end
|
|
135
166
|
|
|
167
|
+
# Call application code with an owned immutable path and explicit key/value role.
|
|
168
|
+
# @return [Object] unchanged input when no callback exists
|
|
136
169
|
def transform(value, path, role)
|
|
137
170
|
callback = @transform
|
|
138
171
|
return value unless callback
|
|
@@ -145,78 +178,7 @@ module XitDB
|
|
|
145
178
|
end.freeze
|
|
146
179
|
callback.call(value: value, path: immutable_path, role: role)
|
|
147
180
|
end
|
|
148
|
-
|
|
149
|
-
# A bounded byte range, including tagged bytes and Ruby String payloads.
|
|
150
|
-
class ByteSource
|
|
151
|
-
attr_reader :size, :format_tag
|
|
152
|
-
|
|
153
|
-
def initialize(context, slot)
|
|
154
|
-
@source = Core::Bytes.open(context.database.core, slot, bound: context.read_bound)
|
|
155
|
-
@start = 0
|
|
156
|
-
@size = @source.size
|
|
157
|
-
@format_tag = @source.format_tag
|
|
158
|
-
end
|
|
159
|
-
|
|
160
|
-
def read(position, length)
|
|
161
|
-
unless position >= 0 && length >= 0 && position + length <= size
|
|
162
|
-
raise InvalidValueError, "truncated byte payload"
|
|
163
|
-
end
|
|
164
|
-
|
|
165
|
-
@source.read(@start + position, length)
|
|
166
|
-
end
|
|
167
|
-
|
|
168
|
-
def unsigned(position)
|
|
169
|
-
value = shift = 0
|
|
170
|
-
while true
|
|
171
|
-
part = read(position, 1).getbyte(0) || raise(InvalidValueError, "truncated byte payload")
|
|
172
|
-
position += 1
|
|
173
|
-
value |= (part & 0x7f) << shift
|
|
174
|
-
if part < 0x80
|
|
175
|
-
raise InvalidValueError, "nonminimal unsigned field" if shift.positive? && part.zero?
|
|
176
|
-
|
|
177
|
-
break
|
|
178
|
-
end
|
|
179
|
-
shift += 7
|
|
180
|
-
raise InvalidValueError, "byte field length exceeds storage range" if shift >= 70
|
|
181
|
-
end
|
|
182
|
-
[value, position]
|
|
183
|
-
end
|
|
184
|
-
|
|
185
|
-
def slice(position, length)
|
|
186
|
-
read(position + length, 0)
|
|
187
|
-
output = dup
|
|
188
|
-
output.instance_variable_set(:@size, length)
|
|
189
|
-
output.instance_variable_set(:@format_tag, nil)
|
|
190
|
-
output.instance_variable_set(:@start, @start + position)
|
|
191
|
-
output
|
|
192
|
-
end
|
|
193
|
-
|
|
194
|
-
def portable_encoding
|
|
195
|
-
converter = Encoding::Converter.new(Encoding::UTF_8, Encoding::UTF_16LE)
|
|
196
|
-
each_chunk { |chunk| converter.convert(chunk) }
|
|
197
|
-
converter.finish
|
|
198
|
-
Encoding::UTF_8
|
|
199
|
-
rescue Encoding::InvalidByteSequenceError, Encoding::UndefinedConversionError
|
|
200
|
-
Encoding::BINARY
|
|
201
|
-
end
|
|
202
|
-
|
|
203
|
-
def write_to(context, prefix: "".b, format_tag: @format_tag)
|
|
204
|
-
Core::Bytes.append(context.writable_transaction, size: size, prefix: prefix, format_tag: format_tag,
|
|
205
|
-
representation: :external) do |position, length|
|
|
206
|
-
read(position, length)
|
|
207
|
-
end
|
|
208
|
-
end
|
|
209
|
-
|
|
210
|
-
private
|
|
211
|
-
|
|
212
|
-
def each_chunk
|
|
213
|
-
position = 0
|
|
214
|
-
while position < size
|
|
215
|
-
chunk = read(position, [size - position, Core::Bytes::CHUNK_SIZE].min)
|
|
216
|
-
yield chunk
|
|
217
|
-
position += chunk.bytesize
|
|
218
|
-
end
|
|
219
|
-
end
|
|
220
|
-
end
|
|
221
181
|
end
|
|
222
182
|
end
|
|
183
|
+
|
|
184
|
+
require_relative "conversion/byte_source"
|