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
@@ -4,9 +4,23 @@ require_relative "input_guard"
4
4
 
5
5
  module XitDB
6
6
  # Encodes and materializes nested values without recursive tree traversal.
7
+ #
8
+ # Each traversal records active ancestors to detect cycles. Sibling values
9
+ # may share an object; only a reference to an active ancestor is a cycle.
10
+ # @api private
7
11
  module ValueWalker
8
12
  # Frames retain only the current ancestors and their incremental iterators.
13
+ # Encode a nested Ruby value using one traversal frame per active ancestor.
14
+ #
15
+ # A frame stores a container, its path, an iterator, and the pending selector.
16
+ # This keeps deep input from consuming Ruby's method call stack.
17
+ # @param value [Object] supported input
18
+ # @param context [Context] writable destination
19
+ # @param path [Array<Object>] destination location
20
+ # @return [Core::Slot]
21
+ # @raise [CyclicValueError] if an input contains itself through its descendants
9
22
  def self.encode(value, context:, path:)
23
+ state = context.profile.packing_state(context: context, reading: false)
10
24
  # @type var active: ::Hash[Integer, bool]
11
25
  active = {}
12
26
  # @type var stack: ::Array[encoding_frame]
@@ -17,7 +31,7 @@ module XitDB
17
31
  result = nil
18
32
  while true # rubocop:disable Style/InfiniteLoop -- Codec StopIteration must propagate.
19
33
  if current
20
- result = begin_encoding(current[0], current[1], context, active, stack)
34
+ result = begin_encoding(current[0], current[1], context, active, stack, state)
21
35
  current = nil
22
36
  end
23
37
  if result
@@ -41,9 +55,14 @@ module XitDB
41
55
  result || raise(CorruptionError, "encoding produced no value")
42
56
  end
43
57
 
58
+ # Build independent Ruby containers from a database view without recursive traversal.
59
+ # @param view [Views::Base]
60
+ # @return [Hash, Array, Set]
61
+ # @raise [CyclicValueError] if stored references form an ancestor cycle
44
62
  def self.materialize(view)
45
63
  view.check!
46
64
  context = view.context
65
+ state = context.profile.packing_state(context: context, reading: true)
47
66
  # @type var active: ::Hash[[Symbol, Integer], bool]
48
67
  active = {}
49
68
  # @type var stack: ::Array[materialization_frame]
@@ -56,7 +75,9 @@ module XitDB
56
75
  materialized = nil
57
76
  while true # rubocop:disable Style/InfiniteLoop -- Codec StopIteration must propagate.
58
77
  if current
59
- begin_materialization(current[0], current[1], selected, context, active, stack)
78
+ result = begin_materialization(current[0], current[1], selected, context, active, stack, state)
79
+ return result if result && stack.empty?
80
+
60
81
  current = nil
61
82
  selected = nil
62
83
  end
@@ -72,47 +93,70 @@ module XitDB
72
93
  materialized || raise(CorruptionError, "materialization produced no value")
73
94
  end
74
95
 
75
- def self.begin_encoding(input, location, context, active, stack)
76
- InputGuard.check!(input, path: location)
96
+ # Validate input and encode a scalar or push a container traversal frame.
97
+ # @return [Core::Slot, nil]
98
+ def self.begin_encoding(input, location, context, active, stack, state)
77
99
  case input
78
- when Views::Base then encode_view(input, context, location)
100
+ when Views::Base
101
+ InputGuard.check!(input, path: location)
102
+ encode_view(input, context, location)
79
103
  else
80
104
  profile = context.profile
105
+ if defined?(Ruby::Pages) && profile.is_a?(Profiles::Ruby)
106
+ stored = Ruby::Pages.encode(input, context: context)
107
+ return stored if stored
108
+ end
81
109
  kind = profile.input_kind(input, path: location)
82
- return encode_scalar(input, context, location) unless kind
110
+ return encode_scalar(input, context, location, state) unless kind
83
111
 
84
112
  identity = BasicObject.instance_method(:__id__).bind_call(input)
85
- raise CyclicValueError.new("cyclic value at #{location.inspect}", path: location) if active[identity]
113
+ if active[identity]
114
+ error = CyclicValueError.new("cyclic value", path: location)
115
+ raise error, "cyclic value at #{error.path.inspect}"
116
+ end
86
117
 
87
118
  active[identity] = true
88
- adapter = profile.empty_container(kind, context: context, path: location)
89
- stack << [input, location, adapter, profile.input_entries(input, path: location), nil]
119
+ adapter = profile.empty_container(kind, context: context, path: location, state: state)
120
+ stack << [input, location, adapter, profile.input_entries(input, path: location, state: state), nil]
90
121
  nil
91
122
  end
92
123
  end
93
124
 
125
+ # Reuse a compatible stored reference after freezing transaction structure.
126
+ # @return [Core::Slot]
127
+ # @raise [ForeignValueError] if reference sharing is unsafe
94
128
  def self.encode_view(input, context, location)
95
129
  input.check!
96
130
  unless input.context.reference_compatible?(context)
97
131
  raise ForeignValueError.new("view belongs to another store or file generation", path: location)
98
132
  end
99
133
 
100
- context.writable_transaction.freeze
134
+ context.writable_transaction!.freeze
101
135
  assigned = input.slot_for_assignment(context: context, path: location)
136
+ if defined?(Ruby::Pages) && context.profile.is_a?(Profiles::Ruby)
137
+ assigned = Ruby::Pages.persist(assigned, context)
138
+ end
102
139
  assigned.empty? ? encode({}, context: context, path: location) : assigned
103
140
  end
104
141
 
105
- def self.encode_scalar(input, context, location)
106
- context.profile.encode_scalar(input, core: context.database.core)
142
+ # Encode a scalar and attach its logical path to value errors.
143
+ # @return [Core::Slot]
144
+ def self.encode_scalar(input, context, location, state)
145
+ context.profile.encode_scalar(input, core: context.database.core, state: state, validated: true)
107
146
  rescue ValueError => e
108
- raise e.class.new("#{e.message} at #{location.inspect}", path: location), cause: e
147
+ error = e.class.new(e.message, path: location)
148
+ raise error, "#{error.message} at #{error.path.inspect}", cause: e
109
149
  end
110
150
 
151
+ # Attach an encoded child at the parent frame's pending selector.
152
+ # @return [void]
111
153
  def self.assign_encoding_parent(stack, stored)
112
154
  frame = stack.last || raise(CorruptionError, "missing encoding frame")
113
155
  frame[2].assign(frame[4], stored)
114
156
  end
115
157
 
158
+ # Pop a completed container and attach its stored reference to its parent.
159
+ # @return [Core::Slot]
116
160
  def self.finish_encoding(frame, active, stack)
117
161
  stored = frame[2].slot
118
162
  active.delete(BasicObject.instance_method(:__id__).bind_call(frame[0]))
@@ -121,14 +165,18 @@ module XitDB
121
165
  stored
122
166
  end
123
167
 
168
+ # Read the next input pair, rescuing only iterator exhaustion.
169
+ # @return [Array, nil]
124
170
  def self.next_input(frame)
125
171
  frame[3].next
126
172
  rescue StopIteration
127
173
  nil
128
174
  end
129
175
 
130
- def self.begin_materialization(slot, path, selected, context, active, stack)
131
- adapter = selected || context.profile.container(slot, context: context, path: path)
176
+ # Decode a scalar into its parent or push a fresh Ruby container frame.
177
+ # @return [Hash, Array, nil]
178
+ def self.begin_materialization(slot, path, selected, context, active, stack, state)
179
+ adapter = selected || context.profile.container(slot, context: context, path: path, state: state)
132
180
  unless adapter
133
181
  result = context.profile.decode(slot, context: context, path: path, immutable: false)
134
182
  frame = stack.last || raise(CorruptionError, "missing materialization frame")
@@ -136,6 +184,11 @@ module XitDB
136
184
  return
137
185
  end
138
186
 
187
+ if (result = materialize_flat(adapter))
188
+ insert(stack.last || raise(CorruptionError, "missing materialization frame"), result) unless stack.empty?
189
+ return result
190
+ end
191
+
139
192
  # @type var identity: [Symbol, Integer]
140
193
  identity = [slot.tag, slot.value]
141
194
  raise CyclicValueError.new("cyclic stored value", path: path) if active[identity]
@@ -148,14 +201,27 @@ module XitDB
148
201
  else []
149
202
  end
150
203
  stack << [identity, path, adapter.each_entry, output, nil]
204
+ nil
205
+ end
206
+
207
+ # Reads bounded page layouts directly; mixed layouts use traversal frames.
208
+ def self.materialize_flat(adapter)
209
+ return unless defined?(Ruby::PageArray)
210
+ return unless adapter.is_a?(Ruby::PageArray) || adapter.is_a?(Ruby::PageRow)
211
+
212
+ adapter.materialize_flat
151
213
  end
152
214
 
215
+ # Read the next stored selector/slot pair, or nil when exhausted.
216
+ # @return [Array, nil]
153
217
  def self.next_stored_entry(frame)
154
218
  frame[2].next
155
219
  rescue StopIteration
156
220
  nil
157
221
  end
158
222
 
223
+ # Insert a set member directly or select the next value for decoding.
224
+ # @return [Array(Core::Slot, Array), nil]
159
225
  def self.materialized_child(frame, entry)
160
226
  selector, child = entry
161
227
  frame[4] = selector.is_a?(String) ? selector.dup : selector
@@ -167,6 +233,8 @@ module XitDB
167
233
  end
168
234
  end
169
235
 
236
+ # Pop a completed Ruby container and insert it into its parent.
237
+ # @return [Hash, Array, Set]
170
238
  def self.finish_materialization(frame, active, stack)
171
239
  active.delete(frame[0])
172
240
  stack.pop
@@ -178,6 +246,8 @@ module XitDB
178
246
  output
179
247
  end
180
248
 
249
+ # Put a decoded value into the pending Hash key or Array position.
250
+ # @return [void]
181
251
  def self.insert(frame, value)
182
252
  output = frame[3]
183
253
  case output
@@ -187,6 +257,6 @@ module XitDB
187
257
  end
188
258
  private_class_method :insert, :begin_encoding, :encode_view, :encode_scalar, :assign_encoding_parent,
189
259
  :finish_encoding, :next_input, :begin_materialization, :next_stored_entry,
190
- :materialized_child, :finish_materialization
260
+ :materialized_child, :finish_materialization, :materialize_flat
191
261
  end
192
262
  end
data/lib/xitdb/version.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module XitDB
4
- VERSION = "0.1.0"
4
+ # Version of the Ruby gem, independent of the on-disk format version.
5
+ VERSION = "0.2.0"
5
6
  end
@@ -3,18 +3,31 @@
3
3
  module XitDB
4
4
  module Views
5
5
  # Provides indexed reads and writes for persistent arrays.
6
+ #
7
+ # Positions start at zero. Negative positions count from the end.
8
+ # Reading a range captures a {Slice}; it does not create a writable subarray.
6
9
  class Array < Base
10
+ # Sentinel for an omitted count or default, distinct from an explicit nil.
7
11
  UNSET = MissingArgument.new.freeze
8
12
  private_constant :UNSET
9
13
 
10
14
  alias length size
11
15
 
16
+ # Read an element or capture a read-only range without loading the whole array.
17
+ # @param index [Integer, Range] negative positions count from the end
18
+ # @param count [Integer] optional slice length
19
+ # @return [Object, Slice, nil] element, captured range, or nil outside the array
20
+ # @see Slice
12
21
  def [](index, count = UNSET)
13
22
  context.read { read_index(index, count) }
14
23
  end
15
24
 
25
+ # Choose element, range, or start/count selection.
26
+ # @param index [Integer, Range]
27
+ # @param count [Integer, MissingArgument]
28
+ # @return [Object, Slice, nil]
16
29
  def read_index(index, count)
17
- return read_slice(integer_argument(index), integer_argument(count)) unless count.equal?(UNSET)
30
+ return read_slice(integer_argument!(index), integer_argument!(count)) unless count.equal?(UNSET)
18
31
  return read_range(index) if index.is_a?(Range)
19
32
 
20
33
  index = normalize_index(index)
@@ -25,9 +38,12 @@ module XitDB
25
38
  end
26
39
  private :read_index
27
40
 
41
+ # Translate inclusive, exclusive, and open-ended ranges into a bounded slice.
42
+ # @param index [Range]
43
+ # @return [Slice, nil]
28
44
  def read_range(index)
29
- start = index.begin.nil? ? 0 : integer_argument(index.begin)
30
- finish = index.end.nil? ? size : integer_argument(index.end)
45
+ start = index.begin.nil? ? 0 : integer_argument!(index.begin)
46
+ finish = index.end.nil? ? size : integer_argument!(index.end)
31
47
  start += size if start.negative?
32
48
  return nil if start.negative? || start > size
33
49
 
@@ -37,6 +53,13 @@ module XitDB
37
53
  end
38
54
  private :read_range
39
55
 
56
+ # Read an element with a default or fallback block for an absent position.
57
+ # @param index [Integer] negative positions count from the end
58
+ # @param default [Object] fallback; omitted means raise
59
+ # @yield [index] compute a fallback, taking precedence over the default
60
+ # @yieldparam index [Integer]
61
+ # @return [Object]
62
+ # @raise [IndexError] if absent without a default or block
40
63
  def fetch(index, default = UNSET)
41
64
  found, value = context.read do
42
65
  position = normalize_index(index)
@@ -54,32 +77,44 @@ module XitDB
54
77
  raise IndexError, "array index outside list"
55
78
  end
56
79
 
80
+ # Read the first element or collect up to count values into a Ruby Array.
81
+ # @param count [Integer] optional nonnegative number of values
82
+ # @return [Object, ::Array, nil] one value, an array, or nil for an empty array
57
83
  def first(count = UNSET)
58
84
  context.read do
59
85
  check!
60
86
  return self[0] if count.equal?(UNSET)
61
87
 
62
- requested = nonnegative_count(count)
88
+ requested = nonnegative_count!(count)
63
89
  each.take(requested)
64
90
  end
65
91
  end
66
92
 
93
+ # Read the last element or collect up to count values in their original order.
94
+ # @param count [Integer] optional nonnegative number of values
95
+ # @return [Object, ::Array, nil]
67
96
  def last(count = UNSET)
68
97
  context.read do
69
98
  return self[-1] if count.equal?(UNSET)
70
99
 
71
- requested = [nonnegative_count(count), size].min
100
+ requested = [nonnegative_count!(count), size].min
72
101
  ((size - requested)...size).map { |index| self[index] }
73
102
  end
74
103
  end
75
104
 
105
+ # Assign one position, filling any gap with nil values.
106
+ # @param index [Integer] negative positions refer to existing positions from the end
107
+ # @param value [Object] value supported by the profile
108
+ # @return [Object] assigned value
109
+ # @raise [IndexError] if the position remains negative or exceeds storage limits
110
+ # @raise [ReadOnlyError] unless the view is writable
76
111
  def []=(index, value)
77
112
  check!
78
113
  context.writable!
79
114
  index = normalize_index(index)
80
115
  raise IndexError, "array index outside list" if index.negative?
81
116
 
82
- validate_growth(index + 1)
117
+ validate_growth!(index + 1)
83
118
 
84
119
  previous_size = size
85
120
  edit(positions: [previous_size, index].min..index) do |collection|
@@ -92,27 +127,40 @@ module XitDB
92
127
  value
93
128
  end
94
129
 
130
+ # Append one value and return this view.
131
+ # @param value [Object]
132
+ # @return [self]
95
133
  def <<(value)
96
134
  push(value)
97
135
  end
98
136
 
137
+ # Append values together as one logical mutation.
138
+ # @param values [::Array<Object>]
139
+ # @return [self]
99
140
  def push(*values)
100
141
  insert(size, *values)
101
142
  end
102
143
 
103
144
  # Radix arrays shift the suffix for insertion and deletion. Linked arrays
104
145
  # use their positional tree operations; neither operation decodes values.
146
+ # Insert values before a position and expire views at shifted positions.
147
+ #
148
+ # An index of -1 appends. Positions beyond the end create nil-filled gaps.
149
+ # @param index [Integer]
150
+ # @param values [::Array<Object>]
151
+ # @return [self]
152
+ # @raise [IndexError] if the normalized position is negative
105
153
  def insert(index, *values)
106
154
  check!
107
155
  context.writable!
108
- index = integer_argument(index)
156
+ index = integer_argument!(index)
109
157
  return self if values.empty?
110
158
 
111
159
  previous_size = size
112
160
  index += previous_size + 1 if index.negative?
113
161
  raise IndexError, "array index outside list" if index.negative?
114
162
 
115
- validate_growth([previous_size, index].max + values.size)
163
+ validate_growth!([previous_size, index].max + values.size)
116
164
  edit(positions: [previous_size, index].min..) do |collection|
117
165
  encoded = values.each_with_index.map do |value, offset|
118
166
  context.profile.encode(value, context: context, path: path + [index + offset])
@@ -124,6 +172,9 @@ module XitDB
124
172
  self
125
173
  end
126
174
 
175
+ # Remove one position and shift later entries left.
176
+ # @param index [Integer] negative positions count from the end
177
+ # @return [Object, nil] removed value or nil outside the array
127
178
  def delete_at(index)
128
179
  check!
129
180
  context.writable!
@@ -144,12 +195,15 @@ module XitDB
144
195
  value
145
196
  end
146
197
 
198
+ # Remove the last value or up to count values from the end.
199
+ # @param count [Integer] optional nonnegative number to remove
200
+ # @return [Object, ::Array, nil] removed value, ordered array of values, or nil
147
201
  def pop(count = UNSET)
148
202
  check!
149
203
  context.writable!
150
204
  return delete_at(-1) if count.equal?(UNSET)
151
205
 
152
- count = [nonnegative_count(count), size].min
206
+ count = [nonnegative_count!(count), size].min
153
207
  return [] if count.zero?
154
208
 
155
209
  previous_size = size
@@ -167,6 +221,10 @@ module XitDB
167
221
  values
168
222
  end
169
223
 
224
+ # Append the contents of arrays or compatible array views in one mutation.
225
+ # @param others [::Array<::Array, Views::Array>] input collections
226
+ # @return [self]
227
+ # @raise [TypeError] if an input cannot be converted to an array
170
228
  def concat(*others)
171
229
  check!
172
230
  context.writable!
@@ -183,7 +241,7 @@ module XitDB
183
241
  inputs.each do |input|
184
242
  next if input.empty?
185
243
 
186
- validate_growth(collection.size + input.size)
244
+ validate_growth!(collection.size + input.size)
187
245
  added = true
188
246
  collection.concat(input)
189
247
  end
@@ -192,6 +250,8 @@ module XitDB
192
250
  self
193
251
  end
194
252
 
253
+ # Remove all values and expire child views.
254
+ # @return [self]
195
255
  def clear
196
256
  check!
197
257
  context.writable!
@@ -205,22 +265,35 @@ module XitDB
205
265
 
206
266
  protected
207
267
 
208
- def validate_growth(count)
268
+ # Reject a size that cannot fit the supported native array index range.
269
+ # @param count [Integer]
270
+ # @return [void]
271
+ def validate_growth!(count)
209
272
  raise IndexError, "array size too big" if count > LONG_MAX / 0.size
210
273
  end
211
274
 
275
+ # Convert an index and translate negative positions relative to size.
276
+ # @param index [Object]
277
+ # @return [Integer]
212
278
  def normalize_index(index)
213
- index = integer_argument(index)
279
+ index = integer_argument!(index)
214
280
  index.negative? ? index + size : index
215
281
  end
216
282
 
217
- def nonnegative_count(count)
218
- count = integer_argument(count)
283
+ # Convert a requested length and reject negative counts.
284
+ # @param count [Object]
285
+ # @return [Integer]
286
+ def nonnegative_count!(count)
287
+ count = integer_argument!(count)
219
288
  raise ArgumentError, "negative array size" if count.negative?
220
289
 
221
290
  count
222
291
  end
223
292
 
293
+ # Capture a bounded selection with a read-only context.
294
+ # @param start [Integer]
295
+ # @param count [Integer]
296
+ # @return [Slice, nil]
224
297
  def read_slice(start, count)
225
298
  start += size if start.negative?
226
299
  return nil if start.negative? || start > size || count.negative?
@@ -230,74 +303,21 @@ module XitDB
230
303
  offset: slice_offset + start, count: count)
231
304
  end
232
305
 
306
+ # Return the start position within the original backing array.
307
+ # @return [Integer]
233
308
  def slice_offset
234
309
  0
235
310
  end
236
311
 
312
+ # Expire moved child locations and invalidate active membership iterators.
313
+ # @param start [Integer] first affected position
314
+ # @return [void]
237
315
  def invalidate_positions(start)
238
316
  context.invalidate_positions(path, start)
239
317
  context.change_membership(path)
240
318
  end
241
319
  end
242
-
243
- # A captured, read-only range of an array.
244
- class Slice < Array
245
- def initialize(offset:, count:, **)
246
- super(**)
247
- @offset = offset
248
- @count = count
249
- end
250
-
251
- def adapter
252
- Selection.new(super, @offset, @count)
253
- end
254
-
255
- def slot_for_assignment(context:, path:)
256
- context.collection(slot, path: path).slice(@offset, @count)
257
- end
258
-
259
- protected
260
-
261
- def slice_offset
262
- @offset
263
- end
264
-
265
- def entry_path(selector)
266
- path + [@offset + selector]
267
- end
268
-
269
- # Limits the adapter's visible entries to the captured range.
270
- class Selection
271
- attr_reader :size
272
-
273
- def initialize(source, offset, count)
274
- @source = source
275
- @offset = offset
276
- @size = count
277
- end
278
-
279
- def kind
280
- @source.kind
281
- end
282
-
283
- def lookup(index)
284
- @source.lookup(@offset + index)
285
- end
286
-
287
- def each_entry
288
- return enum_for(:each_entry) unless block_given?
289
- return self if size.zero?
290
-
291
- index = 0
292
- @source.each_from_index(@offset) do |_selector, stored|
293
- yield index, stored
294
- index += 1
295
- break if index == size
296
- end
297
- self
298
- end
299
- end
300
- private_constant :Selection
301
- end
302
320
  end
303
321
  end
322
+
323
+ require_relative "slice"