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,12 +2,24 @@
2
2
 
3
3
  module XitDB
4
4
  module Core
5
- # A byte-ordered map backed by a counted B-tree.
5
+ # A map that orders binary string keys by their bytes.
6
+ #
7
+ # A counted tree supports key lookup, insertion rank, and indexed access.
8
+ #
9
+ # Iteration yields binary key strings and stored value slots, including empty value slots for sets.
6
10
  class SortedMap
7
11
  include Enumerable
8
12
 
13
+ # @!attribute [r] cursor
14
+ # View locating this collection.
15
+ # @return [Cursor]
9
16
  attr_reader :cursor
10
17
 
18
+ # Opens a map or set whose keys follow binary byte order.
19
+ # @param cursor [Cursor] view locating this collection or value
20
+ # @param set [Boolean] whether a new collection stores keys without values
21
+ # @return [SortedMap]
22
+ # @raise [InvalidValueError] if the cursor holds an incompatible collection tag
11
23
  def initialize(cursor, set: false)
12
24
  @cursor = cursor
13
25
  tag = if cursor.slot.tag == :none
@@ -21,53 +33,100 @@ module XitDB
21
33
  @tree = Tree.new(cursor, tag: tag)
22
34
  end
23
35
 
36
+ # Returns the total number of keys.
37
+ # @return [Integer]
24
38
  def size
25
39
  @tree.size
26
40
  end
27
41
 
42
+ # Reads the value for an exact binary key.
43
+ # @param key_bytes [String] binary key compared in byte order
44
+ # @return [Slot, nil]
28
45
  def get(key_bytes)
29
46
  entry = @tree.find(key_bytes)
30
47
  @tree.record(entry).last if entry
31
48
  end
32
49
 
50
+ # Tests whether an exact binary key exists.
51
+ # @param key_bytes [String] binary key compared in byte order
52
+ # @return [Boolean]
33
53
  def include?(key_bytes)
34
54
  !@tree.find(key_bytes).nil?
35
55
  end
36
56
 
57
+ # Stores or replaces a value for a binary key.
58
+ # @param key_bytes [String] binary key compared in byte order
59
+ # @param value [Slot] value slot to store
60
+ # @return [self]
61
+ # @raise [ReadOnlyError] if the collection has no writable transaction
37
62
  def put(key_bytes, value:)
38
63
  replace_child(key_bytes, value)
39
64
  end
40
65
 
66
+ # Adds a binary key without replacing an existing entry.
67
+ # @param key_bytes [String] binary key compared in byte order
68
+ # @return [self]
69
+ # @raise [ReadOnlyError] if the collection has no writable transaction
41
70
  def add(key_bytes)
42
71
  @tree.store(key_bytes, key_only: true)
43
72
  self
44
73
  end
45
74
 
75
+ # Removes an exact key and invalidates its value views.
76
+ # @param key_bytes [String] binary key compared in byte order
77
+ # @return [Boolean]
78
+ # @raise [ReadOnlyError] if the collection has no writable transaction
46
79
  def remove(key_bytes)
47
80
  @tree.remove(key_bytes)
48
81
  end
49
82
 
83
+ # Counts keys strictly smaller than this key, including when the key is absent.
84
+ # @param key_bytes [String] binary key compared in byte order
85
+ # @return [Integer]
50
86
  def rank(key_bytes)
51
87
  @tree.locate(key_bytes).last
52
88
  end
53
89
 
90
+ # Reads a pair by sorted index, counting negative indexes from the end.
91
+ # @param index [Integer] zero-based element index; supported negative indexes count from the end
92
+ # @return [Array(String, Slot), nil]
54
93
  def at(index)
55
94
  entry = @tree.at(index)
56
95
  decode(entry) if entry
57
96
  end
58
97
 
98
+ # Visits key and value pairs in ascending binary key order.
99
+ # @return [self, Enumerator<Array>]
100
+ # @yield [entry] receives the binary key and value slot as a pair
101
+ # @yieldparam entry [Array(String, Slot)] binary key and stored value slot
102
+ # @yieldreturn [Object] ignored
103
+ # @note Without a block, returns an Enumerator.
59
104
  def each(&block)
60
105
  return enum_for(:each) unless block
61
106
 
62
107
  each_from_index(0, &block)
63
108
  end
64
109
 
110
+ # Visits pairs from the first key greater than or equal to the supplied key.
111
+ # @param key_bytes [String] binary key compared in byte order
112
+ # @return [self, Enumerator<Array>]
113
+ # @yield [entry] receives the binary key and value slot as a pair
114
+ # @yieldparam entry [Array(String, Slot)] binary key and stored value slot
115
+ # @yieldreturn [Object] ignored
116
+ # @note Without a block, returns an Enumerator.
65
117
  def each_from(key_bytes, &block)
66
118
  return enum_for(:each_from, key_bytes) unless block
67
119
 
68
120
  each_from_index(rank(key_bytes), &block)
69
121
  end
70
122
 
123
+ # Visits sorted pairs from an index, counting negative indexes from the end.
124
+ # @param index [Integer] zero-based element index; supported negative indexes count from the end
125
+ # @return [self, Enumerator<Array>]
126
+ # @yield [entry] receives the binary key and value slot as a pair
127
+ # @yieldparam entry [Array(String, Slot)] binary key and stored value slot
128
+ # @yieldreturn [Object] ignored
129
+ # @note Without a block, returns an Enumerator.
71
130
  def each_from_index(index)
72
131
  return enum_for(:each_from_index, index) unless block_given?
73
132
 
@@ -75,6 +134,10 @@ module XitDB
75
134
  self
76
135
  end
77
136
 
137
+ # Finds a value view, optionally creating a key with an empty value.
138
+ # @param key_bytes [String] binary key compared in byte order
139
+ # @param create [Boolean] whether to create an empty element when the requested position is absent
140
+ # @return [Cursor, nil]
78
141
  def cursor_at(key_bytes, create: false)
79
142
  slot = get(key_bytes)
80
143
  if !slot && create
@@ -84,6 +147,12 @@ module XitDB
84
147
  cursor.hash_child(key_bytes.b.freeze, slot) if slot
85
148
  end
86
149
 
150
+ # Stores a child value and forwards ownership and propagation options.
151
+ # @param key_bytes [String] binary key compared in byte order
152
+ # @param slot [Slot] stored value or record descriptor
153
+ # @return [self]
154
+ # @raise [ReadOnlyError] if the collection has no writable transaction
155
+ # @api private
87
156
  def replace_child(key_bytes, slot, **)
88
157
  @tree.store(key_bytes, value: slot, **)
89
158
  self
@@ -91,271 +160,24 @@ module XitDB
91
160
 
92
161
  private
93
162
 
163
+ # Reads a sorted entry into its binary key and value slot.
94
164
  def decode(entry)
95
165
  key, value = @tree.record(entry)
96
166
  [cursor.database.read_bytes(key, bound: cursor.bound).bytes, value]
97
167
  end
98
-
99
- # Maintains sorted records and branch separators.
100
- class Tree < BTree
101
- BRANCH_SIZE = 418
102
- # A mutable sorted node and its separators.
103
- class Node
104
- attr_accessor :kind, :slots, :counts, :separators
105
-
106
- def initialize(kind, slots, counts, separators)
107
- @kind = kind
108
- @slots = slots
109
- @counts = counts
110
- @separators = separators
111
- end
112
-
113
- def size
114
- kind.zero? ? slots.size : counts.sum
115
- end
116
- end
117
-
118
- def record(entry)
119
- return [entry, Slot.new] if %i[bytes short_bytes].include?(entry.tag)
120
- raise CorruptionError, "invalid sorted entry" unless %i[kv_pair sorted_kv_pair].include?(entry.tag)
121
-
122
- offset = entry.value + (entry.tag == :kv_pair ? cursor.database.header.hash_size : 0)
123
- bytes = read(offset, 18)
124
- key = Slot.parse(Format.slice(bytes, 0, 9))
125
- value = Slot.parse(Format.slice(bytes, 9, 9))
126
- unless %i[bytes short_bytes].include?(key.tag) && Slot::TAGS.include?(value.tag) && value.tag != :index
127
- raise CorruptionError, "invalid sorted record slots"
128
- end
129
-
130
- [key, value]
131
- end
132
-
133
- def find(key)
134
- _path, node, index, found, = locate(key)
135
- node.slots[index] if found
136
- end
137
-
138
- def locate(key)
139
- raise TypeError, "sorted key must be a String" unless key.is_a?(String)
140
-
141
- key = key.b
142
- count, pointer = header
143
- # @type var path: sorted_path
144
- path = []
145
- # @type var seen: Hash[Integer?, bool]
146
- seen = {}
147
- rank = 0
148
- loop do
149
- raise CorruptionError, "cyclic sorted branch" if seen[pointer]
150
-
151
- seen[pointer] = true
152
- node = pointer ? read_node(pointer, count) : Node.new(0, [], [], [])
153
- if node.kind.zero?
154
- index = lower_bound(node.slots.size) { |i| key_bytes(record(node.slots[i]).first) >= key }
155
- found = index < node.slots.size && key_bytes(record(node.slots[index]).first) == key
156
- return [path, node, index, found, rank + index]
157
- end
158
- index = lower_bound(node.slots.size - 1) { |i| key_bytes(node.separators[i + 1]) > key }
159
- raise CorruptionError, "missing sorted branch pointer" unless pointer
160
-
161
- path << [pointer, node, index]
162
- rank += node.counts.first(index).sum
163
- pointer = node.slots[index].value
164
- count = node.counts[index]
165
- end
166
- end
167
-
168
- def store(key, value: nil, key_only: false, owner: nil, collection: false, propagate: true)
169
- unless key_only
170
- raise TypeError, "expected Slot" unless value
171
-
172
- validate_slot(value)
173
- end
174
- mutate do |transaction|
175
- count, pointer = header
176
- path, node, index, found, = locate(key)
177
- next if found && key_only
178
- raise RangeError, "sorted size exceeds unsigned 64-bit range" if !found && count == Format::UINT64_MAX
179
-
180
- store_leaf(transaction, key, node, index, found, key_only, value)
181
- root = write_path(transaction, path, node, pointer_for(path, pointer))
182
- publish(transaction, count + (found ? 0 : 1), root, propagate: propagate)
183
- cursor.invalidate_children(index: key.b, except: owner) unless collection || key_only
184
- end
185
- self
186
- end
187
-
188
- def remove(key)
189
- mutate do |transaction|
190
- count, pointer = header
191
- path, node, index, found, = locate(key)
192
- next false unless found
193
-
194
- node.slots.delete_at(index)
195
- old_pointer = pointer_for(path, pointer)
196
- path.reverse_each do |parent_pointer, parent, child_index|
197
- if node.slots.empty?
198
- parent.slots.delete_at(child_index)
199
- parent.counts.delete_at(child_index)
200
- parent.separators.delete_at(child_index)
201
- parent.separators[0] = Slot.new unless parent.separators.empty?
202
- else
203
- parent.slots[child_index] = Slot.new(tag: :index, value: write_node(transaction, node, old_pointer))
204
- parent.counts[child_index] = node.size
205
- end
206
- node = parent
207
- old_pointer = parent_pointer
208
- end
209
- root_pointer = collapse_sorted_root(transaction, node, old_pointer, count - 1)
210
- publish(transaction, count - 1, root_pointer)
211
- cursor.invalidate_children(index: key.b)
212
- true
213
- end
214
- end
215
-
216
- private
217
-
218
- def store_leaf(transaction, key, node, index, found, key_only, value)
219
- key_slot = found ? record(node.slots[index]).first : cursor.database.bytes(key)
220
- entry = if key_only
221
- key_slot
222
- else
223
- raise TypeError, "expected Slot" unless value
224
-
225
- Slot.new(tag: :sorted_kv_pair, value: transaction.append(key_slot.to_bytes + value.to_bytes))
226
- end
227
- found ? node.slots[index] = entry : node.slots.insert(index, entry)
228
- end
229
-
230
- def write_path(transaction, path, node, pointer)
231
- result = split_node(transaction, node, pointer)
232
- path.reverse_each do |parent_pointer, parent, child_index|
233
- parent.slots[child_index, 1] = result.map { |ptr, _, _| Slot.new(tag: :index, value: ptr) }
234
- parent.counts[child_index, 1] = result.map { |_, size, _| size }
235
- parent.separators.insert(child_index + 1, split_separator(result)) if result.size == 2
236
- result = split_node(transaction, parent, parent_pointer)
237
- end
238
- return result.first.first if result.size == 1
239
-
240
- write_node(transaction, Node.new(1, result.map { |ptr, _, _| Slot.new(tag: :index, value: ptr) },
241
- result.map { |_, size, _| size }, [Slot.new, split_separator(result)]))
242
- end
243
-
244
- def collapse_sorted_root(transaction, node, old_pointer, count)
245
- unless node.kind == 1 && node.slots.size == 1
246
- node = Node.new(0, [], [], []) if node.slots.empty?
247
- return write_node(transaction, node, old_pointer)
248
- end
249
-
250
- pointer = node.slots.first.value
251
- # @type var seen: Hash[Integer, bool]
252
- seen = {}
253
- loop do
254
- raise CorruptionError, "cyclic sorted branch" if seen[pointer]
255
-
256
- seen[pointer] = true
257
- child = read_node(pointer, count)
258
- return pointer unless child.kind == 1 && child.slots.size == 1
259
-
260
- pointer = child.slots.first.value
261
- end
262
- end
263
-
264
- def split_separator(result)
265
- result.last.last || raise(CorruptionError, "missing sorted split separator")
266
- end
267
-
268
- def lower_bound(size)
269
- left = 0
270
- right = size
271
- while left < right
272
- middle = (left + right) / 2
273
- yield(middle) ? right = middle : left = middle + 1
274
- end
275
- left
276
- end
277
-
278
- def key_bytes(slot)
279
- raise CorruptionError, "invalid sorted key" unless %i[bytes short_bytes].include?(slot.tag)
280
-
281
- cursor.database.read_bytes(slot, bound: cursor.bound).bytes
282
- end
283
-
284
- def read_node(pointer, expected, bound: cursor.bound)
285
- kind, num = Format.pair(read(pointer, 2, bound: bound), "CC")
286
- validate_header(kind, num)
287
-
288
- bytes = read(pointer, kind.zero? ? LEAF_SIZE : BRANCH_SIZE, bound: bound)
289
- slots = Array.new(num) { |i| Slot.parse(Format.slice(bytes, 2 + (i * 9), 9)) }
290
- # @type var separators: Array[Slot]
291
- separators = kind.zero? ? [] : Array.new(num) { |i| Slot.parse(Format.slice(bytes, LEAF_SIZE + (i * 9), 9)) }
292
- # @type var counts: Array[Integer]
293
- counts = kind.zero? ? [] : Format.integers(Format.slice(bytes, 290, 128), "Q>*").first(num)
294
- node = Node.new(kind, slots, counts, separators)
295
- raise CorruptionError, "invalid sorted node" unless valid_node?(node) && node.size == expected
296
-
297
- node
298
- end
299
-
300
- def validate_header(kind, count)
301
- return if [0, 1].include?(kind) && count <= CAPACITY && (kind.zero? || count.positive?)
302
-
303
- raise CorruptionError, "invalid sorted node header"
304
- end
305
-
306
- def valid_node?(node)
307
- if node.kind.zero?
308
- tags = %i[bytes short_bytes kv_pair sorted_kv_pair]
309
- node.slots.all? { |slot| tags.include?(slot.tag) }
310
- else
311
- tags = %i[bytes short_bytes]
312
- node.slots.all? { |slot| slot.tag == :index } && node.separators.drop(1).all? do |slot|
313
- tags.include?(slot.tag)
314
- end
315
- end
316
- end
317
-
318
- def write_node(transaction, node, old_pointer = nil)
319
- bytes = [node.kind, node.slots.size].pack("CC")
320
- bytes << padded_slots(node.slots)
321
- unless node.kind.zero?
322
- raise CorruptionError, "invalid sorted branch" unless node.is_a?(Node)
323
-
324
- bytes << padded_slots(node.separators)
325
- bytes << (node.counts + ([0] * (CAPACITY - node.counts.size))).pack("Q>*")
326
- end
327
- if old_pointer && old_pointer >= transaction.mutable_start
328
- transaction.write_at(old_pointer, bytes)
329
- old_pointer
330
- else
331
- transaction.append(bytes)
332
- end
333
- end
334
-
335
- def padded_slots(slots)
336
- slots.map(&:to_bytes).join + ("\0".b * ((CAPACITY - slots.size) * 9))
337
- end
338
-
339
- def split_node(transaction, node, old_pointer)
340
- return [[write_node(transaction, node, old_pointer), node.size, nil]] if node.slots.size <= CAPACITY
341
-
342
- separator = node.kind.zero? ? record(node.slots[SPLIT_COUNT]).first : node.separators[SPLIT_COUNT]
343
- left = Node.new(node.kind, node.slots.first(SPLIT_COUNT), node.counts.first(SPLIT_COUNT),
344
- node.separators.first(SPLIT_COUNT))
345
- right = Node.new(node.kind, node.slots.drop(SPLIT_COUNT), node.counts.drop(SPLIT_COUNT),
346
- node.separators.drop(SPLIT_COUNT))
347
- [[write_node(transaction, left, old_pointer), left.size, nil],
348
- [write_node(transaction, right), right.size, separator]]
349
- end
350
- end
351
- private_constant :Tree
352
168
  end
353
169
 
354
- # A byte-ordered collection of keys.
170
+ # A byte-ordered set of binary string keys.
355
171
  class SortedSet < SortedMap
172
+ # Opens a sorted key-only collection.
173
+ # @param cursor [Cursor] view locating this collection or value
174
+ # @return [SortedSet]
175
+ # @raise [InvalidValueError] if the cursor holds an incompatible collection tag
356
176
  def initialize(cursor)
357
177
  super(cursor, set: true)
358
178
  end
359
179
  end
360
180
  end
361
181
  end
182
+
183
+ require_relative "sorted_map/tree"