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.
Files changed (101) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +16 -0
  3. data/README.md +15 -3
  4. data/docs/benchmarks.md +4 -1
  5. data/docs/core.md +15 -2
  6. data/docs/learning.md +211 -0
  7. data/docs/native-profile-performance.md +254 -0
  8. data/docs/ruby-profile-v1.md +2 -0
  9. data/docs/ruby-profile-v2.md +279 -0
  10. data/docs/ruby-profile-v3.md +53 -0
  11. data/docs/ruby-profile-v4.md +67 -0
  12. data/docs/ruby.md +26 -3
  13. data/examples/indexed_users.rb +3 -0
  14. data/examples/portable.rb +3 -0
  15. data/examples/ruby.rb +2 -0
  16. data/lib/xitdb/collection_declaration.rb +39 -0
  17. data/lib/xitdb/context.rb +154 -8
  18. data/lib/xitdb/conversion/byte_source.rb +112 -0
  19. data/lib/xitdb/conversion.rb +35 -73
  20. data/lib/xitdb/core/array_builder.rb +74 -0
  21. data/lib/xitdb/core/array_list.rb +92 -8
  22. data/lib/xitdb/core/b_tree.rb +127 -19
  23. data/lib/xitdb/core/byte_value.rb +33 -0
  24. data/lib/xitdb/core/bytes/source.rb +59 -0
  25. data/lib/xitdb/core/bytes.rb +54 -58
  26. data/lib/xitdb/core/compaction.rb +49 -7
  27. data/lib/xitdb/core/crc64.rb +8 -1
  28. data/lib/xitdb/core/cursor.rb +108 -18
  29. data/lib/xitdb/core/database.rb +163 -29
  30. data/lib/xitdb/core/file_store.rb +43 -1
  31. data/lib/xitdb/core/format.rb +39 -98
  32. data/lib/xitdb/core/hash_map.rb +119 -17
  33. data/lib/xitdb/core/hasher.rb +33 -2
  34. data/lib/xitdb/core/header.rb +98 -0
  35. data/lib/xitdb/core/linked_array_list.rb +70 -1
  36. data/lib/xitdb/core/memory_store.rb +23 -224
  37. data/lib/xitdb/core/offset_map.rb +22 -1
  38. data/lib/xitdb/core/record.rb +64 -0
  39. data/lib/xitdb/core/replication.rb +108 -25
  40. data/lib/xitdb/core/slot.rb +123 -0
  41. data/lib/xitdb/core/sorted_map/tree.rb +317 -0
  42. data/lib/xitdb/core/sorted_map.rb +78 -256
  43. data/lib/xitdb/core/store.rb +286 -0
  44. data/lib/xitdb/core/transaction.rb +84 -6
  45. data/lib/xitdb/core.rb +12 -0
  46. data/lib/xitdb/database.rb +194 -15
  47. data/lib/xitdb/declarations.rb +33 -33
  48. data/lib/xitdb/errors.rb +15 -0
  49. data/lib/xitdb/history.rb +21 -0
  50. data/lib/xitdb/input_guard.rb +15 -1
  51. data/lib/xitdb/profiles/portable/adapter.rb +380 -0
  52. data/lib/xitdb/profiles/portable.rb +129 -243
  53. data/lib/xitdb/profiles/ruby.rb +206 -24
  54. data/lib/xitdb/revision.rb +17 -0
  55. data/lib/xitdb/ruby/codec.rb +61 -54
  56. data/lib/xitdb/ruby/codec_registry.rb +79 -0
  57. data/lib/xitdb/ruby/containers.rb +355 -82
  58. data/lib/xitdb/ruby/encoded_value.rb +38 -0
  59. data/lib/xitdb/ruby/indexed_page_decoder.rb +72 -0
  60. data/lib/xitdb/ruby/indexed_page_encoder.rb +58 -0
  61. data/lib/xitdb/ruby/input.rb +55 -0
  62. data/lib/xitdb/ruby/key_encoding.rb +43 -5
  63. data/lib/xitdb/ruby/ordered_keys.rb +46 -3
  64. data/lib/xitdb/ruby/packed_array.rb +179 -0
  65. data/lib/xitdb/ruby/packed_map.rb +272 -0
  66. data/lib/xitdb/ruby/packing_state.rb +84 -0
  67. data/lib/xitdb/ruby/page_array.rb +218 -0
  68. data/lib/xitdb/ruby/page_cell.rb +20 -0
  69. data/lib/xitdb/ruby/page_codec.rb +46 -0
  70. data/lib/xitdb/ruby/page_decoder.rb +172 -0
  71. data/lib/xitdb/ruby/page_draft.rb +24 -0
  72. data/lib/xitdb/ruby/page_encoder.rb +149 -0
  73. data/lib/xitdb/ruby/page_reference.rb +26 -0
  74. data/lib/xitdb/ruby/page_row.rb +108 -0
  75. data/lib/xitdb/ruby/pages.rb +94 -0
  76. data/lib/xitdb/ruby/scalars.rb +175 -33
  77. data/lib/xitdb/ruby/wire/reader.rb +138 -0
  78. data/lib/xitdb/ruby/wire.rb +36 -90
  79. data/lib/xitdb/ruby.rb +43 -3
  80. data/lib/xitdb/snapshot.rb +8 -0
  81. data/lib/xitdb/streams/handle.rb +46 -0
  82. data/lib/xitdb/streams/reader.rb +122 -0
  83. data/lib/xitdb/streams/writer.rb +43 -0
  84. data/lib/xitdb/streams.rb +8 -140
  85. data/lib/xitdb/uint.rb +17 -0
  86. data/lib/xitdb/value_walker.rb +86 -16
  87. data/lib/xitdb/version.rb +2 -1
  88. data/lib/xitdb/views/array.rb +94 -74
  89. data/lib/xitdb/views/base.rb +144 -12
  90. data/lib/xitdb/views/linked_array.rb +7 -1
  91. data/lib/xitdb/views/map.rb +71 -3
  92. data/lib/xitdb/views/set.rb +13 -1
  93. data/lib/xitdb/views/slice.rb +101 -0
  94. data/lib/xitdb/views/sorted.rb +25 -37
  95. data/lib/xitdb/views/sorted_map.rb +39 -0
  96. data/lib/xitdb/views/sorted_set.rb +10 -0
  97. data/lib/xitdb.rb +3 -0
  98. data/sig/core.rbs +65 -34
  99. data/sig/public.rbs +24 -19
  100. data/sig/ruby.rbs +260 -42
  101. metadata +42 -1
@@ -2,31 +2,63 @@
2
2
 
3
3
  module XitDB
4
4
  module Core
5
- # A counted B-tree for indexed collections.
5
+ # Counted tree algorithms used by linked lists and sorted collections.
6
+ #
7
+ # Leaf nodes hold slots. Branch nodes hold child pointers and the number of elements below each pointer.
8
+ # @api private
6
9
  class BTree
10
+ # Bytes storing the element count and root node offset.
7
11
  HEADER_SIZE = 16
12
+ # Maximum occupied slots in one leaf or branch node.
8
13
  CAPACITY = 16
14
+ # Number of slots placed in the left half when a full node splits.
9
15
  SPLIT_COUNT = 8
16
+ # Bytes in an encoded leaf, including its two-byte node header.
10
17
  LEAF_SIZE = 146
18
+ # Bytes in an encoded branch, including sixteen unsigned subtree counts.
11
19
  BRANCH_SIZE = 274
12
- # A mutable node used while copying a B-tree path.
20
+ # A mutable in-memory node used while rebuilding a stored tree path.
21
+ # @api private
13
22
  class Node
23
+ # @!attribute [rw] kind
24
+ # Zero for a leaf, or one for a branch.
25
+ # @return [Integer]
26
+ # @!attribute [rw] slots
27
+ # Leaf entries or branch pointer slots.
28
+ # @return [Array<Slot>]
29
+ # @!attribute [rw] counts
30
+ # Number of elements under each branch pointer.
31
+ # @return [Array<Integer>]
14
32
  attr_accessor :kind, :slots, :counts
15
33
 
34
+ # Builds a mutable leaf or branch during a copied tree update.
35
+ # @param kind [Integer] zero for a leaf, or one for a branch
36
+ # @param slots [Array<Slot>] leaf values or branch pointer slots
37
+ # @param counts [Array<Integer>] element count beneath each branch pointer
38
+ # @return [Node]
16
39
  def initialize(kind, slots, counts)
17
40
  @kind = kind
18
41
  @slots = slots
19
42
  @counts = counts
20
43
  end
21
44
 
45
+ # Counts leaf slots or sums the element counts of child branches.
46
+ # @return [Integer]
22
47
  def size
23
48
  kind.zero? ? slots.size : counts.sum
24
49
  end
25
50
  end
26
51
  private_constant :Node
27
52
 
53
+ # View locating this collection.
54
+ # @return [Cursor]
28
55
  attr_reader :cursor
29
56
 
57
+ # Opens a counted tree and initializes an empty writable cursor when needed.
58
+ # @param cursor [Cursor] view locating this collection or value
59
+ # @param tag [Symbol, Integer] storage type tag
60
+ # @return [BTree]
61
+ # @raise [InvalidValueError] if the cursor holds an incompatible collection tag
30
62
  def initialize(cursor, tag: :linked_array_list)
31
63
  @cursor = cursor
32
64
  @tag = tag
@@ -42,10 +74,15 @@ module XitDB
42
74
  end
43
75
  end
44
76
 
77
+ # Returns the total number of elements below the root.
78
+ # @return [Integer]
45
79
  def size
46
80
  header.first
47
81
  end
48
82
 
83
+ # Reads an element by index, counting negative indexes from the end.
84
+ # @param index [Integer] zero-based element index; negative indexes count from the end
85
+ # @return [Slot, nil]
49
86
  def at(index)
50
87
  count, pointer = header
51
88
  rank = resolve_index(index, count)
@@ -55,10 +92,16 @@ module XitDB
55
92
  node.slots.fetch(rank)
56
93
  end
57
94
 
95
+ # Inserts a slot from index zero through size and shifts later elements.
96
+ # @param index [Integer] insertion position from zero through size; negative indexes are rejected
97
+ # @param slot [Slot] stored value or record descriptor
98
+ # @return [self]
99
+ # @raise [ReadOnlyError] if the collection has no writable transaction
100
+ # @raise [IndexError] if the index or range is outside the allowed list positions
58
101
  def insert(index, slot)
59
- validate_slot(slot)
102
+ validate_slot!(slot)
60
103
  count, pointer = header
61
- integer_index(index)
104
+ ensure_integer_index!(index)
62
105
  raise IndexError, "array index outside list" unless index.between?(0, count)
63
106
  raise RangeError, "array size exceeds unsigned 64-bit range" if count == Format::UINT64_MAX
64
107
 
@@ -84,8 +127,17 @@ module XitDB
84
127
  self
85
128
  end
86
129
 
130
+ # Replaces an existing element while preserving optional child ownership.
131
+ # @param index [Integer] zero-based element index; supported negative indexes count from the end
132
+ # @param slot [Slot] stored value or record descriptor
133
+ # @param owner [Cursor, nil] child view to preserve during replacement
134
+ # @param collection [Boolean] whether this is an internal copied collection update
135
+ # @param propagate [Boolean] whether to update parent collection paths immediately
136
+ # @return [self]
137
+ # @raise [ReadOnlyError] if the collection has no writable transaction
138
+ # @raise [IndexError] if the index or range is outside the allowed list positions
87
139
  def replace(index, slot, owner: nil, collection: false, propagate: true)
88
- validate_slot(slot)
140
+ validate_slot!(slot)
89
141
  count, pointer = header
90
142
  rank = resolve_index(index, count)
91
143
  raise IndexError, "array index outside list" unless rank
@@ -104,6 +156,9 @@ module XitDB
104
156
  self
105
157
  end
106
158
 
159
+ # Removes the indexed slot and joins small neighboring nodes when possible.
160
+ # @param index [Integer] zero-based element index; supported negative indexes count from the end
161
+ # @return [Slot, nil]
107
162
  def delete_at(index)
108
163
  count, pointer = header
109
164
  rank = resolve_index(index, count)
@@ -125,9 +180,14 @@ module XitDB
125
180
  end
126
181
  end
127
182
 
183
+ # Creates detached trees that share unchanged nodes around the split index.
184
+ # @param index [Integer] zero-based element index; supported negative indexes count from the end
185
+ # @return [Array<BTree>]
186
+ # @raise [ReadOnlyError] if the collection has no writable transaction
187
+ # @raise [IndexError] if the index or range is outside the allowed list positions
128
188
  def split(index)
129
189
  count, pointer = header
130
- integer_index(index)
190
+ ensure_integer_index!(index)
131
191
  index += count if index.negative?
132
192
  raise IndexError, "array split outside list" unless index.between?(0, count)
133
193
 
@@ -136,10 +196,16 @@ module XitDB
136
196
  end
137
197
  end
138
198
 
199
+ # Creates a detached tree for a range without replacing the source root.
200
+ # @param start [Integer] first selected index; negative indexes count from the end
201
+ # @param length [Integer] number of elements in the selected range
202
+ # @return [BTree]
203
+ # @raise [ReadOnlyError] if the collection has no writable transaction
204
+ # @raise [IndexError] if the index or range is outside the allowed list positions
139
205
  def slice(start, length)
140
206
  count, pointer = header
141
- integer_index(start)
142
- integer_index(length)
207
+ ensure_integer_index!(start)
208
+ ensure_integer_index!(length)
143
209
  start += count if start.negative?
144
210
  unless start.between?(0, count) && length >= 0 && length <= count - start
145
211
  raise IndexError, "array slice outside list"
@@ -152,6 +218,11 @@ module XitDB
152
218
  end
153
219
  end
154
220
 
221
+ # Creates a detached tree joining two lists from the same database.
222
+ # @param other [BTree] list from the same database to append to this list
223
+ # @return [BTree]
224
+ # @raise [ReadOnlyError] if the collection has no writable transaction
225
+ # @raise [ForeignValueError] if the other list belongs to another database
155
226
  def concat(other)
156
227
  raise TypeError, "expected BTree" unless other.is_a?(BTree)
157
228
  unless cursor.database.equal?(other.cursor.database)
@@ -169,11 +240,18 @@ module XitDB
169
240
  end
170
241
  end
171
242
 
243
+ # Walks leaves in order from an index, continuing through recorded ancestor paths.
244
+ # @param index [Integer] zero-based element index; supported negative indexes count from the end
245
+ # @return [self, Enumerator<Slot>]
246
+ # @yield [slot] receives each visible element
247
+ # @yieldparam slot [Slot] stored element
248
+ # @yieldreturn [Object] ignored
249
+ # @note Without a block, returns an Enumerator.
172
250
  def each_from_index(index, &block)
173
251
  return enum_for(:each_from_index, index) unless block_given?
174
252
 
175
253
  count, pointer = header
176
- integer_index(index)
254
+ ensure_integer_index!(index)
177
255
  index += count if index.negative?
178
256
  return self unless index >= 0 && index < count
179
257
 
@@ -196,6 +274,7 @@ module XitDB
196
274
 
197
275
  private
198
276
 
277
+ # Pops exhausted traversal frames until a following sibling is available.
199
278
  def next_frame(path)
200
279
  until path.empty?
201
280
  candidate = path.pop || raise(CorruptionError, "missing B-tree path")
@@ -204,6 +283,7 @@ module XitDB
204
283
  nil
205
284
  end
206
285
 
286
+ # Removes an empty child or merges it with a neighbor when both fit.
207
287
  def rebalance_child(transaction, node, parent, index, old_pointer)
208
288
  if node.slots.empty?
209
289
  parent.slots.delete_at(index)
@@ -215,6 +295,7 @@ module XitDB
215
295
  end
216
296
  end
217
297
 
298
+ # Selects an adjacent node and returns their merged node and replacement index.
218
299
  def merge_neighbor(node, parent, index)
219
300
  first = index + 1 < parent.slots.size ? index : index - 1
220
301
  neighbor_index = first == index ? first + 1 : first
@@ -228,6 +309,7 @@ module XitDB
228
309
  [node, first]
229
310
  end
230
311
 
312
+ # Removes redundant single-child root levels after deletion.
231
313
  def collapse_root(transaction, node, old_pointer, count)
232
314
  unless node.kind == 1 && node.slots.size == 1
233
315
  node = Node.new(0, [], []) if node.slots.empty?
@@ -248,20 +330,24 @@ module XitDB
248
330
  end
249
331
  end
250
332
 
251
- def integer_index(index)
333
+ # Checks that an index is an Integer.
334
+ def ensure_integer_index!(index)
252
335
  raise TypeError, "array index must be an Integer" unless index.is_a?(Integer)
253
336
  end
254
337
 
338
+ # Normalizes a negative index and rejects positions outside the tree.
255
339
  def resolve_index(index, count)
256
- integer_index(index)
340
+ ensure_integer_index!(index)
257
341
  index += count if index.negative?
258
342
  index if index >= 0 && index < count
259
343
  end
260
344
 
345
+ # Reads a range inside the supplied or current cursor boundary.
261
346
  def read(offset, length, bound: cursor.bound)
262
347
  cursor.database.read_at(offset, length, bound: bound)
263
348
  end
264
349
 
350
+ # Reads root count and pointer, treating an empty slot as an empty tree.
265
351
  def header
266
352
  slot = cursor.slot
267
353
  return [0, nil] if slot.tag == :none
@@ -270,6 +356,7 @@ module XitDB
270
356
  Format.pair(read(slot.value, HEADER_SIZE), "Q>Q>")
271
357
  end
272
358
 
359
+ # Decodes a leaf or branch and checks its expected subtree count.
273
360
  def read_node(pointer, expected, bound: cursor.bound)
274
361
  kind, num = read_node_header(pointer, bound)
275
362
  bytes = read(pointer, kind.zero? ? LEAF_SIZE : BRANCH_SIZE, bound: bound)
@@ -277,10 +364,11 @@ module XitDB
277
364
  # @type var counts: Array[Integer]
278
365
  counts = kind.zero? ? [] : Format.integers(Format.slice(bytes, LEAF_SIZE, 128), "Q>*").first(num)
279
366
  node = Node.new(kind, slots, counts)
280
- validate_node(node, expected)
367
+ validate_node!(node, expected)
281
368
  node
282
369
  end
283
370
 
371
+ # Validates the node kind and number of occupied slots.
284
372
  def read_node_header(pointer, bound)
285
373
  kind, num = Format.pair(read(pointer, 2, bound: bound), "CC")
286
374
  unless [0, 1].include?(kind) && num <= CAPACITY && (kind.zero? || num.positive?)
@@ -290,6 +378,7 @@ module XitDB
290
378
  [kind, num]
291
379
  end
292
380
 
381
+ # Decodes and validates a sequence of packed slots.
293
382
  def node_slots(bytes, start, count)
294
383
  Array.new(count) do |index|
295
384
  slot = Slot.parse(Format.slice(bytes, start + (index * Slot::SIZE), Slot::SIZE))
@@ -299,25 +388,29 @@ module XitDB
299
388
  end
300
389
  end
301
390
 
302
- def validate_node(node, expected)
391
+ # Checks slot roles and the expected element count below a node.
392
+ def validate_node!(node, expected)
303
393
  case node.kind
304
- when 1 then validate_branch(node)
305
- when 0 then validate_leaf(node)
394
+ when 1 then validate_branch!(node)
395
+ when 0 then validate_leaf!(node)
306
396
  end
307
397
 
308
398
  raise CorruptionError, "invalid B-tree subtree count" unless node.size == expected
309
399
  end
310
400
 
311
- def validate_branch(node)
401
+ # Requires branch pointers and positive child counts.
402
+ def validate_branch!(node)
312
403
  return unless node.slots.any? { |slot| slot.tag != :index } || node.counts.any?(&:zero?)
313
404
 
314
405
  raise CorruptionError, "invalid B-tree branch"
315
406
  end
316
407
 
317
- def validate_leaf(node)
408
+ # Rejects branch pointers inside leaf data.
409
+ def validate_leaf!(node)
318
410
  raise CorruptionError, "index slot in B-tree leaf" if node.slots.any? { |slot| slot.tag == :index }
319
411
  end
320
412
 
413
+ # Follows subtree counts to an indexed leaf, recording ancestors and rejecting cycles.
321
414
  def descend(pointer, rank, expected, insertion: false, ancestors: [])
322
415
  return [[], Node.new(0, [], []), 0] unless pointer
323
416
 
@@ -342,16 +435,19 @@ module XitDB
342
435
  end
343
436
  end
344
437
 
438
+ # Finds the current leaf pointer from a recorded path or root.
345
439
  def pointer_for(path, root_pointer)
346
440
  path.empty? ? root_pointer : path.last[1].slots[path.last[2]].value
347
441
  end
348
442
 
349
- def validate_slot(slot)
443
+ # Rejects unknown tags and internal branch slots as user elements.
444
+ def validate_slot!(slot)
350
445
  raise TypeError, "expected Slot" unless slot.is_a?(Slot)
351
446
  raise CorruptionError, "unknown slot tag" unless Slot::TAGS.include?(slot.tag)
352
447
  raise InvalidValueError, "index slot is internal to B-tree branches" if slot.tag == :index
353
448
  end
354
449
 
450
+ # Runs a mutation with rollback of bytes and cursors on any exception.
355
451
  def mutate
356
452
  cursor.slot
357
453
  transaction = cursor.transaction
@@ -369,7 +465,7 @@ module XitDB
369
465
  end
370
466
  end
371
467
 
372
- # References retain each source cursor's read boundary while nodes are shared.
468
+ # Reads a shared subtree using its retained source boundary.
373
469
  def node_for(ref)
374
470
  pointer = ref[0]
375
471
  raise CorruptionError, "missing B-tree pointer" unless pointer
@@ -377,16 +473,19 @@ module XitDB
377
473
  read_node(pointer, ref[1], bound: ref[2] || cursor.bound)
378
474
  end
379
475
 
476
+ # Builds a child reference retaining the parent's read boundary.
380
477
  def child_ref(ref, node, index)
381
478
  [node.slots[index].value, node.counts[index], ref[2]]
382
479
  end
383
480
 
481
+ # Combines slots and counts from nodes at the same level.
384
482
  def merge_nodes(left, right)
385
483
  raise CorruptionError, "unequal B-tree node kinds" unless left.kind == right.kind
386
484
 
387
485
  Node.new(left.kind, left.slots + right.slots, left.counts + right.counts)
388
486
  end
389
487
 
488
+ # Writes one node or divides an oversized node into two references.
390
489
  def assemble(transaction, node)
391
490
  if node.slots.size <= CAPACITY
392
491
  [[write_node(transaction, node), node.size, nil]]
@@ -399,6 +498,7 @@ module XitDB
399
498
  end
400
499
  end
401
500
 
501
+ # Builds a branch from subtree pointers and counts.
402
502
  def branch(refs)
403
503
  slots = refs.map do |ref|
404
504
  pointer = ref[0]
@@ -409,6 +509,7 @@ module XitDB
409
509
  Node.new(1, slots, refs.map { |ref| ref[1] })
410
510
  end
411
511
 
512
+ # Counts branch levels while rejecting cyclic child pointers.
412
513
  def height(ref)
413
514
  levels = 0
414
515
  # @type var seen: Hash[Integer?, bool]
@@ -425,6 +526,7 @@ module XitDB
425
526
  end
426
527
  end
427
528
 
529
+ # Aligns tree heights and joins boundary paths while sharing other subtrees.
428
530
  def join_roots(transaction, left, right)
429
531
  return right if left[1].zero?
430
532
  return left if right[1].zero?
@@ -461,6 +563,7 @@ module XitDB
461
563
  result.size == 1 ? result.first : assemble(transaction, branch(result)).first
462
564
  end
463
565
 
566
+ # Builds a subtree for an adjacent range of existing children.
464
567
  def sibling_root(transaction, ref, node, range)
465
568
  indices = node.slots.each_index.to_a[range] || []
466
569
  refs = indices.map { |index| child_ref(ref, node, index) }
@@ -470,6 +573,7 @@ module XitDB
470
573
  assemble(transaction, branch(refs)).first
471
574
  end
472
575
 
576
+ # Divides a tree at a rank and rebuilds only the split path.
473
577
  def split_root(transaction, ref, rank)
474
578
  return [[nil, 0, nil], ref] if rank.zero?
475
579
  return [ref, [nil, 0, nil]] if rank == ref[1]
@@ -504,12 +608,14 @@ module XitDB
504
608
  [left, right]
505
609
  end
506
610
 
611
+ # Creates a new transaction-owned cursor for a shared subtree root.
507
612
  def detached(transaction, ref)
508
613
  pointer = ref[0] || write_node(transaction, Node.new(0, [], []))
509
614
  position = transaction.append([ref[1], pointer].pack("Q>Q>"))
510
615
  self.class.new(transaction.cursor(slot: Slot.new(tag: :linked_array_list, value: position)))
511
616
  end
512
617
 
618
+ # Encodes a node, reusing storage only at or above the writable boundary.
513
619
  def write_node(transaction, node, old_pointer = nil)
514
620
  bytes = [node.kind, node.slots.size].pack("CC")
515
621
  bytes << node.slots.map(&:to_bytes).join << ("\0".b * ((CAPACITY - node.slots.size) * Slot::SIZE))
@@ -522,6 +628,7 @@ module XitDB
522
628
  end
523
629
  end
524
630
 
631
+ # Writes one node or splits its overflow into two balanced nodes.
525
632
  def write_split(transaction, node, old_pointer)
526
633
  return [[write_node(transaction, node, old_pointer), node.size]] if node.slots.size <= CAPACITY
527
634
 
@@ -530,6 +637,7 @@ module XitDB
530
637
  [[write_node(transaction, left, old_pointer), left.size], [write_node(transaction, right), right.size]]
531
638
  end
532
639
 
640
+ # Updates the collection header and propagates its slot through ancestors.
533
641
  def publish(transaction, count, pointer, propagate: true)
534
642
  bytes = [count, pointer].pack("Q>Q>")
535
643
  old = cursor.slot
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ module XitDB
4
+ module Core
5
+ # An immutable copy of binary payload bytes and an optional application format tag.
6
+ class ByteValue
7
+ # @!attribute [r] bytes
8
+ # Frozen binary payload.
9
+ # @return [String]
10
+ # @!attribute [r] format_tag
11
+ # Optional frozen two-byte application format identifier.
12
+ # @return [String, nil]
13
+ attr_reader :bytes, :format_tag
14
+
15
+ # Copies bytes and an optional two-byte tag into a frozen value.
16
+ # @param bytes [String] binary input bytes
17
+ # @param format_tag [String, nil] optional two-byte application format identifier
18
+ # @return [ByteValue]
19
+ # @raise [TypeError] if bytes is not a String
20
+ # @raise [ArgumentError] if format_tag is neither nil nor a two-byte String
21
+ def initialize(bytes, format_tag: nil)
22
+ raise TypeError, "bytes must be a String" unless bytes.is_a?(String)
23
+ unless format_tag.nil? || (format_tag.is_a?(String) && format_tag.bytesize == 2)
24
+ raise ArgumentError, "format_tag must contain two bytes"
25
+ end
26
+
27
+ @bytes = bytes.b.freeze
28
+ @format_tag = format_tag&.b&.freeze
29
+ freeze
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ module XitDB
4
+ module Core
5
+ module Bytes
6
+ # An immutable reader for one byte payload within a fixed database boundary.
7
+ # @api private
8
+ class Source
9
+ # @!attribute [r] size
10
+ # Declared number of payload or digest bytes.
11
+ # @return [Integer]
12
+ # @!attribute [r] format_tag
13
+ # Optional frozen two-byte application format identifier.
14
+ # @return [String, nil]
15
+ attr_reader :size, :format_tag
16
+
17
+ # Builds a frozen reader limited to one payload and storage boundary.
18
+ # @param database [Database] database that owns the stored records
19
+ # @param location [String, Integer] inline payload bytes or absolute payload offset
20
+ # @param size [Integer] number of bytes
21
+ # @param format_tag [String, nil] optional two-byte application format identifier
22
+ # @param bound [Integer] exclusive upper byte boundary for reads
23
+ # @return [Source]
24
+ def initialize(database, location, size, format_tag, bound)
25
+ @database = database
26
+ @location = location
27
+ @size = size
28
+ @format_tag = format_tag
29
+ @bound = bound
30
+ freeze
31
+ end
32
+
33
+ # Reads an exact range inside this payload.
34
+ # @param position [Integer] zero-based byte offset inside the payload
35
+ # @param length [Integer] number of bytes
36
+ # @return [String]
37
+ # @raise [InvalidValueError] if the requested range exceeds the payload
38
+ # @raise [TypeError] if position or length is not an Integer
39
+ def read(position, length)
40
+ @database.check_open!
41
+ unless position.is_a?(Integer) && length.is_a?(Integer)
42
+ raise TypeError, "byte positions and lengths must be Integers"
43
+ end
44
+ if position.negative? || length.negative? || position + length > size
45
+ raise InvalidValueError, "truncated byte payload"
46
+ end
47
+
48
+ location = @location
49
+ if location.is_a?(String)
50
+ Format.slice(location, position, length)
51
+ else
52
+ @database.read_at(location + position, length, bound: @bound)
53
+ end
54
+ end
55
+ end
56
+ private_constant :Source
57
+ end
58
+ end
59
+ end