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
@@ -0,0 +1,317 @@
1
+ # frozen_string_literal: true
2
+
3
+ module XitDB
4
+ module Core
5
+ class SortedMap
6
+ # Sorted record storage and separator-key maintenance for the counted tree.
7
+ # @api private
8
+ class Tree < BTree
9
+ # Bytes in a sorted branch containing slots, separator keys, and subtree counts.
10
+ BRANCH_SIZE = 418
11
+ # A mutable in-memory node used while rebuilding a stored tree path.
12
+ # @api private
13
+ class Node
14
+ # @!attribute [rw] kind
15
+ # Zero for a leaf, or one for a branch.
16
+ # @return [Integer]
17
+ # @!attribute [rw] slots
18
+ # Leaf entries or branch pointer slots.
19
+ # @return [Array<Slot>]
20
+ # @!attribute [rw] counts
21
+ # Number of elements under each branch pointer.
22
+ # @return [Array<Integer>]
23
+ # @!attribute [rw] separators
24
+ # Key slots that separate adjacent child ranges.
25
+ # @return [Array<Slot>]
26
+ attr_accessor :kind, :slots, :counts, :separators
27
+
28
+ # Builds a sorted leaf or branch with separator keys.
29
+ # @param kind [Integer] zero for a leaf, or one for a branch
30
+ # @param slots [Array<Slot>] leaf values or branch pointer slots
31
+ # @param counts [Array<Integer>] element count beneath each branch pointer
32
+ # @param separators [Array<Slot>] keys separating child ranges
33
+ # @return [Node]
34
+ def initialize(kind, slots, counts, separators)
35
+ @kind = kind
36
+ @slots = slots
37
+ @counts = counts
38
+ @separators = separators
39
+ end
40
+
41
+ # Counts leaf entries or sums child subtree counts.
42
+ # @return [Integer]
43
+ def size
44
+ kind.zero? ? slots.size : counts.sum
45
+ end
46
+ end
47
+
48
+ # Decodes a key-only slot or a record containing key and value slots.
49
+ # @param entry [Slot] stored sorted entry descriptor
50
+ # @return [Array(Slot, Slot)]
51
+ def record(entry)
52
+ return [entry, Slot.new] if %i[bytes short_bytes].include?(entry.tag)
53
+ raise CorruptionError, "invalid sorted entry" unless %i[kv_pair sorted_kv_pair].include?(entry.tag)
54
+
55
+ offset = entry.value + (entry.tag == :kv_pair ? cursor.database.header.hash_size : 0)
56
+ bytes = read(offset, 18)
57
+ key = Slot.parse(Format.slice(bytes, 0, 9))
58
+ value = Slot.parse(Format.slice(bytes, 9, 9))
59
+ unless %i[bytes short_bytes].include?(key.tag) && Slot::TAGS.include?(value.tag) && value.tag != :index
60
+ raise CorruptionError, "invalid sorted record slots"
61
+ end
62
+
63
+ [key, value]
64
+ end
65
+
66
+ # Finds the stored entry for an exact binary key.
67
+ # @param key [String] binary key compared in byte order
68
+ # @return [Slot, nil]
69
+ def find(key)
70
+ _path, node, index, found, = locate(key)
71
+ node.slots[index] if found
72
+ end
73
+
74
+ # Finds a key's leaf position, presence, rank, and ancestor path.
75
+ # @param key [String] binary key compared in byte order
76
+ # @return [Array]
77
+ def locate(key)
78
+ raise TypeError, "sorted key must be a String" unless key.is_a?(String)
79
+
80
+ key = key.b
81
+ count, pointer = header
82
+ # @type var path: sorted_path
83
+ path = []
84
+ # @type var seen: Hash[Integer?, bool]
85
+ seen = {}
86
+ rank = 0
87
+ loop do
88
+ raise CorruptionError, "cyclic sorted branch" if seen[pointer]
89
+
90
+ seen[pointer] = true
91
+ node = pointer ? read_node(pointer, count) : Node.new(0, [], [], [])
92
+ if node.kind.zero?
93
+ index = lower_bound(node.slots.size) { |i| key_bytes(record(node.slots[i]).first) >= key }
94
+ found = index < node.slots.size && key_bytes(record(node.slots[index]).first) == key
95
+ return [path, node, index, found, rank + index]
96
+ end
97
+ index = lower_bound(node.slots.size - 1) { |i| key_bytes(node.separators[i + 1]) > key }
98
+ raise CorruptionError, "missing sorted branch pointer" unless pointer
99
+
100
+ path << [pointer, node, index]
101
+ rank += node.counts.first(index).sum
102
+ pointer = node.slots[index].value
103
+ count = node.counts[index]
104
+ end
105
+ end
106
+
107
+ # Inserts a key-only entry or replaces its value, copying the search path.
108
+ # @param key [String] binary key compared in byte order
109
+ # @param value [Slot, nil] value slot; nil is permitted when key_only is true
110
+ # @param key_only [Boolean] whether to add a key without storing a value
111
+ # @param owner [Cursor, nil] child view to preserve during replacement
112
+ # @param collection [Boolean] whether this is an internal copied collection update
113
+ # @param propagate [Boolean] whether to update parent collection paths immediately
114
+ # @return [self]
115
+ def store(key, value: nil, key_only: false, owner: nil, collection: false, propagate: true)
116
+ unless key_only
117
+ raise TypeError, "expected Slot" unless value
118
+
119
+ validate_slot!(value)
120
+ end
121
+ mutate do |transaction|
122
+ count, pointer = header
123
+ path, node, index, found, = locate(key)
124
+ next if found && key_only
125
+ raise RangeError, "sorted size exceeds unsigned 64-bit range" if !found && count == Format::UINT64_MAX
126
+
127
+ store_leaf(transaction, key, node, index, found, key_only, value)
128
+ root = write_path(transaction, path, node, pointer_for(path, pointer))
129
+ publish(transaction, count + (found ? 0 : 1), root, propagate: propagate)
130
+ cursor.invalidate_children(index: key.b, except: owner) unless collection || key_only
131
+ end
132
+ self
133
+ end
134
+
135
+ # Removes a key and collapses redundant root levels.
136
+ # @param key [String] binary key compared in byte order
137
+ # @return [Boolean]
138
+ # @raise [ReadOnlyError] if the collection has no writable transaction
139
+ def remove(key)
140
+ mutate do |transaction|
141
+ count, pointer = header
142
+ path, node, index, found, = locate(key)
143
+ next false unless found
144
+
145
+ node.slots.delete_at(index)
146
+ old_pointer = pointer_for(path, pointer)
147
+ path.reverse_each do |parent_pointer, parent, child_index|
148
+ if node.slots.empty?
149
+ parent.slots.delete_at(child_index)
150
+ parent.counts.delete_at(child_index)
151
+ parent.separators.delete_at(child_index)
152
+ parent.separators[0] = Slot.new unless parent.separators.empty?
153
+ else
154
+ parent.slots[child_index] = Slot.new(tag: :index, value: write_node(transaction, node, old_pointer))
155
+ parent.counts[child_index] = node.size
156
+ end
157
+ node = parent
158
+ old_pointer = parent_pointer
159
+ end
160
+ root_pointer = collapse_sorted_root(transaction, node, old_pointer, count - 1)
161
+ publish(transaction, count - 1, root_pointer)
162
+ cursor.invalidate_children(index: key.b)
163
+ true
164
+ end
165
+ end
166
+
167
+ private
168
+
169
+ # Writes a key-only slot or a separate key/value record into a leaf.
170
+ def store_leaf(transaction, key, node, index, found, key_only, value)
171
+ key_slot = found ? record(node.slots[index]).first : cursor.database.bytes(key)
172
+ entry = if key_only
173
+ key_slot
174
+ else
175
+ raise TypeError, "expected Slot" unless value
176
+
177
+ Slot.new(tag: :sorted_kv_pair, value: transaction.append(key_slot.to_bytes + value.to_bytes))
178
+ end
179
+ found ? node.slots[index] = entry : node.slots.insert(index, entry)
180
+ end
181
+
182
+ # Rebuilds ancestors and separators after a leaf insertion or replacement.
183
+ def write_path(transaction, path, node, pointer)
184
+ result = split_node(transaction, node, pointer)
185
+ path.reverse_each do |parent_pointer, parent, child_index|
186
+ parent.slots[child_index, 1] = result.map { |ptr, _, _| Slot.new(tag: :index, value: ptr) }
187
+ parent.counts[child_index, 1] = result.map { |_, size, _| size }
188
+ parent.separators.insert(child_index + 1, split_separator(result)) if result.size == 2
189
+ result = split_node(transaction, parent, parent_pointer)
190
+ end
191
+ return result.first.first if result.size == 1
192
+
193
+ write_node(transaction, Node.new(1, result.map { |ptr, _, _| Slot.new(tag: :index, value: ptr) },
194
+ result.map { |_, size, _| size }, [Slot.new, split_separator(result)]))
195
+ end
196
+
197
+ # Removes redundant single-child roots while rejecting cycles.
198
+ def collapse_sorted_root(transaction, node, old_pointer, count)
199
+ unless node.kind == 1 && node.slots.size == 1
200
+ node = Node.new(0, [], [], []) if node.slots.empty?
201
+ return write_node(transaction, node, old_pointer)
202
+ end
203
+
204
+ pointer = node.slots.first.value
205
+ # @type var seen: Hash[Integer, bool]
206
+ seen = {}
207
+ loop do
208
+ raise CorruptionError, "cyclic sorted branch" if seen[pointer]
209
+
210
+ seen[pointer] = true
211
+ child = read_node(pointer, count)
212
+ return pointer unless child.kind == 1 && child.slots.size == 1
213
+
214
+ pointer = child.slots.first.value
215
+ end
216
+ end
217
+
218
+ # Retrieves the key separating two nodes after a split.
219
+ def split_separator(result)
220
+ result.last.last || raise(CorruptionError, "missing sorted split separator")
221
+ end
222
+
223
+ # Finds the first index whose yielded comparison is true using binary search.
224
+ def lower_bound(size)
225
+ left = 0
226
+ right = size
227
+ while left < right
228
+ middle = (left + right) / 2
229
+ yield(middle) ? right = middle : left = middle + 1
230
+ end
231
+ left
232
+ end
233
+
234
+ # Reads and validates bytes from a sorted key slot.
235
+ def key_bytes(slot)
236
+ raise CorruptionError, "invalid sorted key" unless %i[bytes short_bytes].include?(slot.tag)
237
+
238
+ cursor.database.read_bytes(slot, bound: cursor.bound).bytes
239
+ end
240
+
241
+ # Decodes slots, separators, and counts, then checks expected subtree size.
242
+ def read_node(pointer, expected, bound: cursor.bound)
243
+ kind, num = Format.pair(read(pointer, 2, bound: bound), "CC")
244
+ validate_header!(kind, num)
245
+
246
+ bytes = read(pointer, kind.zero? ? LEAF_SIZE : BRANCH_SIZE, bound: bound)
247
+ slots = Array.new(num) { |i| Slot.parse(Format.slice(bytes, 2 + (i * 9), 9)) }
248
+ # @type var separators: Array[Slot]
249
+ separators = kind.zero? ? [] : Array.new(num) { |i| Slot.parse(Format.slice(bytes, LEAF_SIZE + (i * 9), 9)) }
250
+ # @type var counts: Array[Integer]
251
+ counts = kind.zero? ? [] : Format.integers(Format.slice(bytes, 290, 128), "Q>*").first(num)
252
+ node = Node.new(kind, slots, counts, separators)
253
+ raise CorruptionError, "invalid sorted node" unless valid_node?(node) && node.size == expected
254
+
255
+ node
256
+ end
257
+
258
+ # Checks the kind and occupied slot count of a sorted node.
259
+ def validate_header!(kind, count)
260
+ return if [0, 1].include?(kind) && count <= CAPACITY && (kind.zero? || count.positive?)
261
+
262
+ raise CorruptionError, "invalid sorted node header"
263
+ end
264
+
265
+ # Checks that leaf entries and branch separators use permitted tags.
266
+ def valid_node?(node)
267
+ if node.kind.zero?
268
+ tags = %i[bytes short_bytes kv_pair sorted_kv_pair]
269
+ node.slots.all? { |slot| tags.include?(slot.tag) }
270
+ else
271
+ tags = %i[bytes short_bytes]
272
+ node.slots.all? { |slot| slot.tag == :index } && node.separators.drop(1).all? do |slot|
273
+ tags.include?(slot.tag)
274
+ end
275
+ end
276
+ end
277
+
278
+ # Encodes a sorted node, copying storage below the writable boundary.
279
+ def write_node(transaction, node, old_pointer = nil)
280
+ bytes = [node.kind, node.slots.size].pack("CC")
281
+ bytes << padded_slots(node.slots)
282
+ unless node.kind.zero?
283
+ raise CorruptionError, "invalid sorted branch" unless node.is_a?(Node)
284
+
285
+ bytes << padded_slots(node.separators)
286
+ bytes << (node.counts + ([0] * (CAPACITY - node.counts.size))).pack("Q>*")
287
+ end
288
+ if old_pointer && old_pointer >= transaction.mutable_start
289
+ transaction.write_at(old_pointer, bytes)
290
+ old_pointer
291
+ else
292
+ transaction.append(bytes)
293
+ end
294
+ end
295
+
296
+ # Encodes occupied slots and fills unused positions with zero bytes.
297
+ def padded_slots(slots)
298
+ slots.map(&:to_bytes).join + ("\0".b * ((CAPACITY - slots.size) * 9))
299
+ end
300
+
301
+ # Splits an overflowing sorted node and retains the new boundary key.
302
+ def split_node(transaction, node, old_pointer)
303
+ return [[write_node(transaction, node, old_pointer), node.size, nil]] if node.slots.size <= CAPACITY
304
+
305
+ separator = node.kind.zero? ? record(node.slots[SPLIT_COUNT]).first : node.separators[SPLIT_COUNT]
306
+ left = Node.new(node.kind, node.slots.first(SPLIT_COUNT), node.counts.first(SPLIT_COUNT),
307
+ node.separators.first(SPLIT_COUNT))
308
+ right = Node.new(node.kind, node.slots.drop(SPLIT_COUNT), node.counts.drop(SPLIT_COUNT),
309
+ node.separators.drop(SPLIT_COUNT))
310
+ [[write_node(transaction, left, old_pointer), left.size, nil],
311
+ [write_node(transaction, right), right.size, separator]]
312
+ end
313
+ end
314
+ private_constant :Tree
315
+ end
316
+ end
317
+ end