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,380 @@
1
+ # frozen_string_literal: true
2
+
3
+ module XitDB
4
+ module Profiles
5
+ class Portable
6
+ # Adapts native portable layouts to the shared collection operations.
7
+ # Hash maps and sets visit entries in hash traversal order; sorted collections use byte order.
8
+ # Sequence adapters use integer positions and return encoded child slots for lazy decoding.
9
+ class Adapter
10
+ attr_reader :kind, :cursor
11
+
12
+ # @!attribute [r] kind
13
+ # Logical collection kind used by public views.
14
+ #
15
+ # @return [Symbol] logical collection kind used by public views
16
+ # @!attribute [r] cursor
17
+ # Cursor holding the native collection root.
18
+ #
19
+ # @return [Core::Cursor] cursor holding the native collection root
20
+
21
+ # Binds a native collection to a context cursor and logical path.
22
+ #
23
+ # @param profile [Portable] portable scalar and key rules
24
+ # @param cursor [Core::Cursor] collection root cursor
25
+ # @param context [Context] owner of the operation
26
+ # @param path [Array<Object>] logical collection location
27
+ # @param tag [Symbol, nil] native collection tag, or infer it from the cursor
28
+ def initialize(profile, cursor, context:, path:, tag: nil)
29
+ @profile = profile
30
+ @cursor = cursor
31
+ @context = context
32
+ @path = path
33
+ tag ||= cursor.slot.tag == :none ? :hash_map : cursor.slot.tag
34
+ @kind = KINDS.fetch(tag)
35
+ @collection = case tag
36
+ when :hash_map, :hash_set, :counted_hash_map, :counted_hash_set
37
+ Core::HashMap.new(cursor, counted: %i[counted_hash_map counted_hash_set].include?(tag),
38
+ set: %i[hash_set counted_hash_set].include?(tag))
39
+ when :array_list then Core::ArrayList.new(cursor)
40
+ when :linked_array_list then Core::LinkedArrayList.new(cursor)
41
+ when :sorted_map, :sorted_set then Core::SortedMap.new(cursor, set: tag == :sorted_set)
42
+ end
43
+ end
44
+
45
+ # Returns the collection descriptor held by the adapter cursor.
46
+ #
47
+ # @return [Core::Slot] current stored collection root
48
+ def slot
49
+ cursor.slot
50
+ end
51
+
52
+ # Counts the entries visible through this collection adapter.
53
+ #
54
+ # @return [Integer] number of keys, members, or sequence elements
55
+ def size
56
+ @collection.size
57
+ end
58
+
59
+ # Checks whether the collection has any entries.
60
+ #
61
+ # @return [Boolean] true when the entry count is zero
62
+ def empty?
63
+ size.zero?
64
+ end
65
+
66
+ # Looks up a stored child without decoding its scalar or collection value.
67
+ #
68
+ # @param selector [Object] key, member, or zero-based sequence index
69
+ # @return [Core::Slot, nil] stored child, or nil when absent
70
+ # @raise [TypeError] if a sequence selector is not an Integer
71
+ # @raise [ValueError] if the key or stored layout is invalid
72
+ def lookup(selector)
73
+ collection = @collection
74
+ if collection.is_a?(Core::ArrayList) || collection.is_a?(Core::LinkedArrayList)
75
+ collection.get(index_selector!(selector))
76
+ elsif collection.is_a?(Core::SortedMap)
77
+ collection.get(key_bytes(selector))
78
+ else
79
+ collection.get(@profile.hasher.digest(key_bytes(selector)))
80
+ end
81
+ end
82
+
83
+ # Stores an encoded child at a key or sequence position.
84
+ # A sequence position equal to size appends the child.
85
+ #
86
+ # @param selector [Object] key, member, or nonnegative sequence index
87
+ # @param stored [Core::Slot] already encoded child value
88
+ # @return [self] this adapter
89
+ # @raise [ValueError] if the key or stored layout is invalid
90
+ def assign(selector, stored)
91
+ case kind
92
+ when :array, :linked_array
93
+ if selector == size
94
+ sequence_collection!.append(stored)
95
+ else
96
+ sequence_collection!.put(index_selector!(selector),
97
+ stored)
98
+ end
99
+ when :sorted_map
100
+ sorted_collection!.put(key_bytes(selector), value: stored)
101
+ when :sorted_set
102
+ sorted_collection!.add(key_bytes(selector))
103
+ when :map, :set
104
+ bytes = key_bytes(selector)
105
+ digest = @profile.hasher.digest(bytes)
106
+ key = @context.database.core.bytes(bytes)
107
+ if kind == :map
108
+ hash_collection!.put(digest, key: key, value: stored)
109
+ else
110
+ hash_collection!.add(digest, key: key)
111
+ end
112
+ end
113
+ self
114
+ end
115
+
116
+ # Removes a key, set member, or sequence element.
117
+ # Deleting a sequence element shifts the following positions down by one.
118
+ #
119
+ # @param selector [Object] key, member, or nonnegative sequence index
120
+ # @return [Object] underlying removal result, which varies by layout
121
+ # @raise [IndexError] if an array index is outside the collection
122
+ def delete(selector)
123
+ case kind
124
+ when :array
125
+ index = selector
126
+ count = size
127
+ raise IndexError, "array index outside list" unless index.is_a?(Integer) && index >= 0 && index < count
128
+
129
+ (index...(count - 1)).each do |position|
130
+ array_collection!.put(position, required_slot!(array_collection!.get(position + 1)))
131
+ end
132
+ array_collection!.truncate(count - 1)
133
+ when :linked_array then linked_collection!.delete_at(index_selector!(selector))
134
+ when :map, :set then hash_collection!.remove(@profile.hasher.digest(key_bytes(selector)))
135
+ else sorted_collection!.remove(key_bytes(selector))
136
+ end
137
+ end
138
+
139
+ # Radix insertion moves the suffix and performs one radix write per slot.
140
+ # Linked insertion follows one count-guided tree path.
141
+ # Inserts a stored sequence element and shifts the following positions.
142
+ # Array insertion copies the suffix; linked arrays follow a counted tree path.
143
+ #
144
+ # @param index [Integer] nonnegative insertion position validated by the caller
145
+ # @param stored [Core::Slot] encoded element
146
+ # @return [self] this adapter
147
+ def insert(index, stored)
148
+ if kind == :linked_array
149
+ Core::BTree.new(cursor).insert(index, stored)
150
+ else
151
+ count = size
152
+ array_collection!.append(stored)
153
+ (count - 1).downto(index) do |position|
154
+ array_collection!.put(position + 1, required_slot!(array_collection!.get(position)))
155
+ end
156
+ array_collection!.put(index, stored)
157
+ end
158
+ self
159
+ end
160
+
161
+ # Keeps the first count elements of a sequence.
162
+ #
163
+ # @param count [Integer] retained length validated by the caller
164
+ # @return [self] this adapter
165
+ def truncate(count)
166
+ if kind == :linked_array
167
+ cursor.replace(linked_collection!.slice(0, count).cursor.slot)
168
+ else
169
+ array_collection!.truncate(count)
170
+ end
171
+ self
172
+ end
173
+
174
+ # Builds a stored sequence containing a contiguous range of elements.
175
+ #
176
+ # @param start [Integer] nonnegative start position validated by the caller
177
+ # @param count [Integer] element count validated by the caller
178
+ # @return [Core::Slot] descriptor of the new sequence
179
+ def slice(start, count)
180
+ return linked_collection!.slice(start, count).cursor.slot if kind == :linked_array
181
+
182
+ output = @profile.empty_container(:array_list, context: @context, path: @path)
183
+ count.times { |index| output.assign(index, required_slot!(lookup(start + index))) }
184
+ output.slot
185
+ end
186
+
187
+ # Appends the stored elements of another sequence.
188
+ # Two linked arrays can join through their tree structure.
189
+ #
190
+ # @param other [Object] sequence adapter implementing kind, cursor, and each_entry
191
+ # @return [self] this adapter
192
+ def concat(other)
193
+ if kind == :linked_array && other.kind == :linked_array
194
+ cursor.replace(linked_collection!.concat(Core::LinkedArrayList.new(other.cursor)).cursor.slot)
195
+ else
196
+ other.each_entry { |_index, stored| assign(size, stored) }
197
+ end
198
+ self
199
+ end
200
+
201
+ # Finds the number of sorted keys strictly less than the requested key.
202
+ #
203
+ # @param key [Object] key in the sorted collection's accepted key type
204
+ # @return [Integer] zero-based position, including the insertion position for an absent key
205
+ def rank(key)
206
+ sorted_collection!.rank(@profile.key_bytes(key))
207
+ end
208
+
209
+ # Visits sorted entries starting at the first key greater than or equal to the requested key.
210
+ #
211
+ # @param key [Object] lower-bound sorted key
212
+ # @return [self, Enumerator] this adapter with a block, otherwise an enumerator
213
+ # @yield [key, stored] visits an entry in ascending key order
214
+ # @yieldparam key [Object] decoded key
215
+ # @yieldparam stored [Core::Slot] encoded child value
216
+ # @yieldreturn [void] ignored
217
+ def each_from(key)
218
+ return enum_for(:each_from, key) unless block_given?
219
+
220
+ sorted_collection!.each_from(@profile.key_bytes(key)) { |bytes, stored| yield decode_key(bytes), stored }
221
+ self
222
+ end
223
+
224
+ # Visits sequence or sorted entries starting at an ordinal position.
225
+ # Negative positions count from the end; positions outside the collection visit no entries.
226
+ #
227
+ # @param index [Integer] starting ordinal position
228
+ # @return [self, Enumerator] this adapter with a block, otherwise an enumerator
229
+ # @yield [selector, stored] visits each remaining entry
230
+ # @yieldparam selector [Object] key or sequence position
231
+ # @yieldparam stored [Core::Slot] encoded child value
232
+ # @yieldreturn [void] ignored
233
+ def each_from_index(index)
234
+ return enum_for(:each_from_index, index) unless block_given?
235
+
236
+ index += size if index.negative?
237
+ return self unless index >= 0 && index < size
238
+
239
+ case kind
240
+ when :array
241
+ (index...size).each { |position| yield position, required_slot!(lookup(position)) }
242
+ when :linked_array
243
+ linked_collection!.each_from_index(index).with_index(index) { |stored, position| yield position, stored }
244
+ else
245
+ sorted_collection!.each_from_index(index) { |bytes, stored| yield decode_key(bytes), stored }
246
+ end
247
+ self
248
+ end
249
+
250
+ # Visits every entry in the order defined by the collection layout.
251
+ #
252
+ # @return [self, Enumerator] this adapter with a block, otherwise an enumerator
253
+ # @yield [selector, stored] visits one entry
254
+ # @yieldparam selector [Object] decoded key or integer sequence position
255
+ # @yieldparam stored [Core::Slot] encoded child value
256
+ # @yieldreturn [void] ignored
257
+ def each_entry
258
+ return enum_for(:each_entry) unless block_given?
259
+
260
+ case kind
261
+ when :array, :linked_array
262
+ sequence_collection!.each.with_index { |stored, index| yield index, stored }
263
+ when :sorted_map, :sorted_set
264
+ sorted_collection!.each { |bytes, stored| yield decode_key(bytes), stored }
265
+ else
266
+ hash_collection!.each_entry do |_digest, key, stored|
267
+ value = @profile.decode_scalar(key, core: @context.database.core, bound: @context.read_bound)
268
+ raise CorruptionError, "portable map key is not an untagged string" unless value.is_a?(String)
269
+
270
+ yield value, stored
271
+ end
272
+ end
273
+ self
274
+ end
275
+
276
+ private
277
+
278
+ # Requires an Integer selector before passing an index to core storage.
279
+ #
280
+ # @param selector [Object] candidate sequence position
281
+ # @return [Integer] supplied index
282
+ # @raise [TypeError] if the selector is not an Integer
283
+ def index_selector!(selector)
284
+ raise TypeError, "index must be an Integer" unless selector.is_a?(Integer)
285
+
286
+ selector
287
+ end
288
+
289
+ # Requires a stored child where the collection structure promises one.
290
+ #
291
+ # @param slot [Core::Slot, nil] result of an internal child lookup
292
+ # @return [Core::Slot] present child
293
+ # @raise [CorruptionError] if the promised child is absent
294
+ def required_slot!(slot)
295
+ raise CorruptionError, "missing collection slot" unless slot
296
+
297
+ slot
298
+ end
299
+
300
+ # Checks the active layout before using its collection-specific operations.
301
+ #
302
+ # @return [Core::ArrayList] typed underlying collection
303
+ # @raise [CorruptionError] if the active layout has the wrong collection type
304
+ def array_collection!
305
+ collection = @collection
306
+ raise CorruptionError, "unexpected collection layout" unless collection.is_a?(Core::ArrayList)
307
+
308
+ collection
309
+ end
310
+
311
+ # Checks the active layout before using its collection-specific operations.
312
+ #
313
+ # @return [Core::LinkedArrayList] typed underlying collection
314
+ # @raise [CorruptionError] if the active layout has the wrong collection type
315
+ def linked_collection!
316
+ collection = @collection
317
+ raise CorruptionError, "unexpected collection layout" unless collection.is_a?(Core::LinkedArrayList)
318
+
319
+ collection
320
+ end
321
+
322
+ # Checks the active layout before using its collection-specific operations.
323
+ #
324
+ # @return [Core::SortedMap] typed underlying collection
325
+ # @raise [CorruptionError] if the active layout has the wrong collection type
326
+ def sorted_collection!
327
+ collection = @collection
328
+ raise CorruptionError, "unexpected collection layout" unless collection.is_a?(Core::SortedMap)
329
+
330
+ collection
331
+ end
332
+
333
+ # Checks the active layout before using hash-specific operations.
334
+ #
335
+ # @return [Core::HashMap] native hash map or set
336
+ # @raise [CorruptionError] if the active layout is not a hash collection
337
+ def hash_collection!
338
+ collection = @collection
339
+ raise CorruptionError, "unexpected collection layout" unless collection.is_a?(Core::HashMap)
340
+
341
+ collection
342
+ end
343
+
344
+ # Checks the active layout before using its collection-specific operations.
345
+ #
346
+ # @return [Core::ArrayList, Core::LinkedArrayList] typed underlying collection
347
+ # @raise [CorruptionError] if the active layout has the wrong collection type
348
+ def sequence_collection!
349
+ collection = @collection
350
+ unless collection.is_a?(Core::ArrayList) || collection.is_a?(Core::LinkedArrayList)
351
+ raise CorruptionError, "unexpected sequence layout"
352
+ end
353
+
354
+ collection
355
+ end
356
+
357
+ # Encodes a selector and attaches its logical path to value errors.
358
+ #
359
+ # @param selector [Object] key or member to encode
360
+ # @return [String] canonical binary key bytes
361
+ # @raise [ValueError] if the key is unsupported for this collection
362
+ def key_bytes(selector)
363
+ @profile.key_bytes(selector)
364
+ rescue ValueError => e
365
+ raise e.class.new(e.message, path: @path + [selector]), cause: e
366
+ end
367
+
368
+ # Decodes raw sorted-key bytes as UTF-8 when valid, or binary otherwise.
369
+ #
370
+ # @param bytes [String] stored raw key bytes
371
+ # @return [String] frozen decoded key
372
+ def decode_key(bytes)
373
+ value = bytes.dup.force_encoding(Encoding::UTF_8)
374
+ value.force_encoding(Encoding::BINARY) unless value.valid_encoding?
375
+ value.freeze
376
+ end
377
+ end
378
+ end
379
+ end
380
+ end