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,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module XitDB
4
+ module Core
5
+ # Builds an unpublished indexed list with at most sixteen slots per level.
6
+ # The owner supplies transaction rollback and publishes the completed root.
7
+ # @api private
8
+ class ArrayBuilder
9
+ def initialize(transaction)
10
+ @transaction = transaction
11
+ @levels = [[]]
12
+ @count = 0
13
+ @slot = nil
14
+ end
15
+
16
+ # Adds one validated element before finalization.
17
+ def append(slot)
18
+ @transaction.check_active!
19
+ raise TransactionStateError, "array builder is finalized" if @slot
20
+ raise TypeError, "expected Slot" unless slot.is_a?(Slot)
21
+ raise CorruptionError, "unknown slot tag" unless Slot::TAGS.include?(slot.tag)
22
+ raise InvalidValueError, "index slot is internal to array branches" if slot.tag == :index
23
+ raise RangeError, "array size exceeds unsigned 64-bit range" if @count == Format::UINT64_MAX
24
+
25
+ push(0, slot)
26
+ @count += 1
27
+ self
28
+ end
29
+
30
+ # Flushes partial blocks once and returns the stable completed descriptor.
31
+ def slot
32
+ @transaction.check_active!
33
+ stored = @slot
34
+ return stored if stored
35
+
36
+ level = 0
37
+ loop do
38
+ fields = @levels.fetch(level)
39
+ if @levels.drop(level + 1).all?(&:empty?)
40
+ pointer = level.positive? && fields.size == 1 ? fields.fetch(0).value : block(fields)
41
+ completed = Slot.new(tag: :array_list,
42
+ value: @transaction.append([@count, pointer].pack("Q>Q>")))
43
+ @slot = completed
44
+ return completed
45
+ end
46
+ unless fields.empty?
47
+ @levels[level] = []
48
+ push(level + 1, Slot.new(tag: :index, value: block(fields)))
49
+ end
50
+ level += 1
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def push(level, slot)
57
+ loop do
58
+ @levels[level] ||= []
59
+ fields = @levels.fetch(level)
60
+ fields << slot
61
+ return unless fields.size == 16
62
+
63
+ slot = Slot.new(tag: :index, value: block(fields))
64
+ @levels[level] = []
65
+ level += 1
66
+ end
67
+ end
68
+
69
+ def block(fields)
70
+ @transaction.append(fields.map(&:to_bytes).join.b.ljust(ArrayList::BLOCK_SIZE, "\0"))
71
+ end
72
+ end
73
+ end
74
+ end
@@ -1,35 +1,62 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module XitDB
4
+ # Advanced binary storage API for slots, cursors, collections, and transactions.
5
+ #
6
+ # A slot describes a stored value. A cursor locates a value inside a database.
7
+ # This layer handles encoded bytes and slots; XitDB::Database accepts application values.
4
8
  module Core
5
- # A persistent vector with sixteen-way branches.
9
+ # An indexed slot list stored in a tree with sixteen positions per block.
10
+ #
11
+ # Each branch uses four index bits. Missing branches read as empty slots inside the visible list length.
12
+ #
13
+ # Writes copy protected blocks so earlier views keep their values.
6
14
  class ArrayList
7
15
  include Enumerable
8
16
 
17
+ # Bytes storing the visible element count and root block offset.
9
18
  HEADER_SIZE = 16
19
+ # Bytes in one block of sixteen slot descriptors.
10
20
  BLOCK_SIZE = 16 * Slot::SIZE
21
+ # A zero-filled block representing sixteen unused positions.
11
22
  EMPTY_BLOCK = ("\0".b * BLOCK_SIZE).freeze
12
23
 
24
+ # @!attribute [r] cursor
25
+ # View locating this collection.
26
+ # @return [Cursor]
13
27
  attr_reader :cursor
14
28
 
15
- def initialize(cursor)
29
+ # Opens an indexed list, initializing an empty writable cursor when needed.
30
+ # @param cursor [Cursor] view locating this collection or value
31
+ # @param fields [Array<Slot>] up to sixteen initial slots for a new writable list
32
+ # @return [ArrayList]
33
+ # @raise [InvalidValueError] if the cursor holds an incompatible collection tag
34
+ def initialize(cursor, fields: [])
16
35
  @cursor = cursor
17
36
  unless %i[none array_list].include?(cursor.slot.tag)
18
37
  raise InvalidValueError, "slot does not contain an array list"
19
38
  end
20
39
 
40
+ validate_initial_fields!(fields) unless fields.empty?
21
41
  return unless cursor.slot.tag == :none && cursor.transaction
22
42
 
23
43
  mutate do |transaction|
24
- _count, _pointer, start = writable_header(transaction)
44
+ start = transaction.length
45
+ block = fields.empty? ? EMPTY_BLOCK : fields.map(&:to_bytes).join.b.ljust(BLOCK_SIZE, "\0")
46
+ transaction.append([fields.size, start + HEADER_SIZE].pack("Q>Q>") + block)
25
47
  cursor.update_collection(Slot.new(tag: :array_list, value: start))
26
48
  end
27
49
  end
28
50
 
51
+ # Returns the number of visible elements.
52
+ # @return [Integer]
29
53
  def size
30
54
  header.first
31
55
  end
32
56
 
57
+ # Reads an element, with negative indexes counted from the end.
58
+ # @param index [Integer] zero-based element index; supported negative indexes count from the end
59
+ # @return [Slot, nil]
33
60
  def get(index)
34
61
  count, pointer = header
35
62
  index = resolve_index(index, count)
@@ -39,6 +66,10 @@ module XitDB
39
66
  read_element(pointer, index, depth(count))
40
67
  end
41
68
 
69
+ # Returns an element view, optionally appending an empty slot at the list end.
70
+ # @param index [Integer] zero-based element index; supported negative indexes count from the end
71
+ # @param create [Boolean] whether to create an empty element when the requested position is absent
72
+ # @return [Cursor, nil]
42
73
  def cursor_at(index, create: false)
43
74
  count = size
44
75
  resolved = resolve_index(index, count)
@@ -56,8 +87,12 @@ module XitDB
56
87
  cursor.array_child(resolved, slot)
57
88
  end
58
89
 
90
+ # Adds a slot at the end and grows branch depth when needed.
91
+ # @param slot [Slot] stored value or record descriptor
92
+ # @return [self]
93
+ # @raise [ReadOnlyError] if the collection has no writable transaction
59
94
  def append(slot)
60
- validate_slot(slot)
95
+ validate_slot!(slot)
61
96
  mutate do |transaction|
62
97
  count, pointer, start = writable_header(transaction)
63
98
  raise RangeError, "array size exceeds unsigned 64-bit range" if count == Format::UINT64_MAX
@@ -73,14 +108,29 @@ module XitDB
73
108
  self
74
109
  end
75
110
 
111
+ # Replaces an element at an existing index.
112
+ # @param index [Integer] zero-based element index; supported negative indexes count from the end
113
+ # @param slot [Slot] stored value or record descriptor
114
+ # @return [self]
115
+ # @raise [ReadOnlyError] if the collection has no writable transaction
116
+ # @raise [IndexError] if the index or range is outside the allowed list positions
76
117
  def put(index, slot)
77
118
  replace_child(index, slot)
78
119
  self
79
120
  end
80
121
 
81
- # A child write updates its current path, including ancestors copied by freeze.
122
+ # Replaces a child slot while preserving the owning cursor when supplied.
123
+ # @param index [Integer] zero-based element index; supported negative indexes count from the end
124
+ # @param slot [Slot] stored value or record descriptor
125
+ # @param owner [Cursor, nil] child view to preserve during replacement
126
+ # @param collection [Boolean] whether this is an internal copied collection update
127
+ # @param propagate [Boolean] whether to update parent collection paths immediately
128
+ # @return [self]
129
+ # @raise [ReadOnlyError] if the collection has no writable transaction
130
+ # @raise [IndexError] if the index or range is outside the allowed list positions
131
+ # @api private
82
132
  def replace_child(index, slot, owner: nil, collection: false, propagate: true)
83
- validate_slot(slot)
133
+ validate_slot!(slot)
84
134
  index = resolve_index(index, size)
85
135
  raise IndexError, "array index outside list" unless index
86
136
 
@@ -94,8 +144,13 @@ module XitDB
94
144
  self
95
145
  end
96
146
 
147
+ # Keeps the first count elements and invalidates removed element views.
148
+ # @param count [Integer] number of elements to retain
149
+ # @return [self]
150
+ # @raise [ReadOnlyError] if the collection has no writable transaction
151
+ # @raise [IndexError] if the index or range is outside the allowed list positions
97
152
  def truncate(count)
98
- Format.integer(count, 0..Format::UINT64_MAX)
153
+ Format.integer!(count, 0..Format::UINT64_MAX)
99
154
  raise IndexError, "array size outside list" if count > size
100
155
 
101
156
  mutate do |transaction|
@@ -120,6 +175,12 @@ module XitDB
120
175
  self
121
176
  end
122
177
 
178
+ # Visits visible slots in index order, including empty positions.
179
+ # @return [self, Enumerator<Slot>]
180
+ # @yield [slot] receives each visible element
181
+ # @yieldparam slot [Slot] stored element
182
+ # @yieldreturn [Object] ignored
183
+ # @note Without a block, returns an Enumerator.
123
184
  def each
124
185
  return enum_for(:each) unless block_given?
125
186
 
@@ -151,6 +212,17 @@ module XitDB
151
212
 
152
213
  private
153
214
 
215
+ # Checks the bounded initial record before allocating any storage.
216
+ def validate_initial_fields!(fields)
217
+ unless cursor.slot.empty? && cursor.transaction
218
+ raise ArgumentError, "initial fields require an empty writable cursor"
219
+ end
220
+ raise ArgumentError, "initial fields exceed one array block" if fields.size > 16
221
+
222
+ fields.each { |field| validate_slot!(field) }
223
+ end
224
+
225
+ # Builds reverse traversal frames for the visible branches of one block.
154
226
  def branches(block, shift, base, count)
155
227
  # @type var result: Array[[Integer?, Integer, Integer]]
156
228
  result = []
@@ -166,6 +238,7 @@ module XitDB
166
238
  result
167
239
  end
168
240
 
241
+ # Reads the element count and root block pointer.
169
242
  def header
170
243
  slot = cursor.slot
171
244
  return [0, nil] if slot.tag == :none
@@ -176,10 +249,12 @@ module XitDB
176
249
  [count, pointer]
177
250
  end
178
251
 
252
+ # Calculates branch depth for an element count.
179
253
  def depth(count)
180
254
  [((count - 1).bit_length - 1) / 4, 0].max
181
255
  end
182
256
 
257
+ # Normalizes a negative index and returns nil outside the visible list.
183
258
  def resolve_index(index, count)
184
259
  raise TypeError, "array index must be an Integer" unless index.is_a?(Integer)
185
260
 
@@ -187,10 +262,12 @@ module XitDB
187
262
  index if index >= 0 && index < count
188
263
  end
189
264
 
265
+ # Reads bytes inside the collection cursor's boundary.
190
266
  def read(offset, length)
191
267
  cursor.database.read_at(offset, length, bound: cursor.bound)
192
268
  end
193
269
 
270
+ # Decodes a slot and rejects unrecognized array tags.
194
271
  def parse_slot(bytes)
195
272
  slot = Slot.parse(bytes)
196
273
  raise CorruptionError, "unknown array slot tag" unless Slot::TAGS.include?(slot.tag)
@@ -198,10 +275,12 @@ module XitDB
198
275
  slot
199
276
  end
200
277
 
278
+ # Reads and validates one slot at an absolute offset.
201
279
  def read_slot(offset)
202
280
  parse_slot(read(offset, Slot::SIZE))
203
281
  end
204
282
 
283
+ # Follows four index bits per level to find a leaf or empty branch.
205
284
  def read_element(pointer, index, shift)
206
285
  loop do
207
286
  read(pointer, BLOCK_SIZE)
@@ -219,12 +298,14 @@ module XitDB
219
298
  end
220
299
  end
221
300
 
222
- def validate_slot(slot)
301
+ # Rejects unknown slots and branch pointers supplied as list elements.
302
+ def validate_slot!(slot)
223
303
  raise TypeError, "expected Slot" unless slot.is_a?(Slot)
224
304
  raise CorruptionError, "unknown slot tag" unless Slot::TAGS.include?(slot.tag)
225
305
  raise InvalidValueError, "index slot is internal to array branches" if slot.tag == :index
226
306
  end
227
307
 
308
+ # Runs a write with a checkpoint and restores it on any exception.
228
309
  def mutate
229
310
  cursor.slot
230
311
  transaction = cursor.transaction
@@ -241,6 +322,7 @@ module XitDB
241
322
  end
242
323
  end
243
324
 
325
+ # Allocates a header and obtains a writable root block.
244
326
  def writable_header(transaction)
245
327
  count, pointer = header
246
328
  if pointer.nil?
@@ -254,11 +336,13 @@ module XitDB
254
336
  [count, pointer, start]
255
337
  end
256
338
 
339
+ # Copies a block only when it lies below the transaction's writable boundary.
257
340
  def copy_block(transaction, pointer)
258
341
  block = transaction.read_at(pointer, BLOCK_SIZE)
259
342
  pointer < transaction.mutable_start ? transaction.append(block) : pointer
260
343
  end
261
344
 
345
+ # Copies or allocates the branch path and returns the writable leaf offset.
262
346
  def writable_element(transaction, pointer, index, shift)
263
347
  loop do
264
348
  position = pointer + (((index >> (shift * 4)) & 15) * Slot::SIZE)