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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +16 -0
- data/README.md +15 -3
- data/docs/benchmarks.md +4 -1
- data/docs/core.md +15 -2
- data/docs/learning.md +211 -0
- data/docs/native-profile-performance.md +254 -0
- data/docs/ruby-profile-v1.md +2 -0
- data/docs/ruby-profile-v2.md +279 -0
- data/docs/ruby-profile-v3.md +53 -0
- data/docs/ruby-profile-v4.md +67 -0
- data/docs/ruby.md +26 -3
- data/examples/indexed_users.rb +3 -0
- data/examples/portable.rb +3 -0
- data/examples/ruby.rb +2 -0
- data/lib/xitdb/collection_declaration.rb +39 -0
- data/lib/xitdb/context.rb +154 -8
- data/lib/xitdb/conversion/byte_source.rb +112 -0
- data/lib/xitdb/conversion.rb +35 -73
- data/lib/xitdb/core/array_builder.rb +74 -0
- data/lib/xitdb/core/array_list.rb +92 -8
- data/lib/xitdb/core/b_tree.rb +127 -19
- data/lib/xitdb/core/byte_value.rb +33 -0
- data/lib/xitdb/core/bytes/source.rb +59 -0
- data/lib/xitdb/core/bytes.rb +54 -58
- data/lib/xitdb/core/compaction.rb +49 -7
- data/lib/xitdb/core/crc64.rb +8 -1
- data/lib/xitdb/core/cursor.rb +108 -18
- data/lib/xitdb/core/database.rb +163 -29
- data/lib/xitdb/core/file_store.rb +43 -1
- data/lib/xitdb/core/format.rb +39 -98
- data/lib/xitdb/core/hash_map.rb +119 -17
- data/lib/xitdb/core/hasher.rb +33 -2
- data/lib/xitdb/core/header.rb +98 -0
- data/lib/xitdb/core/linked_array_list.rb +70 -1
- data/lib/xitdb/core/memory_store.rb +23 -224
- data/lib/xitdb/core/offset_map.rb +22 -1
- data/lib/xitdb/core/record.rb +64 -0
- data/lib/xitdb/core/replication.rb +108 -25
- data/lib/xitdb/core/slot.rb +123 -0
- data/lib/xitdb/core/sorted_map/tree.rb +317 -0
- data/lib/xitdb/core/sorted_map.rb +78 -256
- data/lib/xitdb/core/store.rb +286 -0
- data/lib/xitdb/core/transaction.rb +84 -6
- data/lib/xitdb/core.rb +12 -0
- data/lib/xitdb/database.rb +194 -15
- data/lib/xitdb/declarations.rb +33 -33
- data/lib/xitdb/errors.rb +15 -0
- data/lib/xitdb/history.rb +21 -0
- data/lib/xitdb/input_guard.rb +15 -1
- data/lib/xitdb/profiles/portable/adapter.rb +380 -0
- data/lib/xitdb/profiles/portable.rb +129 -243
- data/lib/xitdb/profiles/ruby.rb +206 -24
- data/lib/xitdb/revision.rb +17 -0
- data/lib/xitdb/ruby/codec.rb +61 -54
- data/lib/xitdb/ruby/codec_registry.rb +79 -0
- data/lib/xitdb/ruby/containers.rb +355 -82
- data/lib/xitdb/ruby/encoded_value.rb +38 -0
- data/lib/xitdb/ruby/indexed_page_decoder.rb +72 -0
- data/lib/xitdb/ruby/indexed_page_encoder.rb +58 -0
- data/lib/xitdb/ruby/input.rb +55 -0
- data/lib/xitdb/ruby/key_encoding.rb +43 -5
- data/lib/xitdb/ruby/ordered_keys.rb +46 -3
- data/lib/xitdb/ruby/packed_array.rb +179 -0
- data/lib/xitdb/ruby/packed_map.rb +272 -0
- data/lib/xitdb/ruby/packing_state.rb +84 -0
- data/lib/xitdb/ruby/page_array.rb +218 -0
- data/lib/xitdb/ruby/page_cell.rb +20 -0
- data/lib/xitdb/ruby/page_codec.rb +46 -0
- data/lib/xitdb/ruby/page_decoder.rb +172 -0
- data/lib/xitdb/ruby/page_draft.rb +24 -0
- data/lib/xitdb/ruby/page_encoder.rb +149 -0
- data/lib/xitdb/ruby/page_reference.rb +26 -0
- data/lib/xitdb/ruby/page_row.rb +108 -0
- data/lib/xitdb/ruby/pages.rb +94 -0
- data/lib/xitdb/ruby/scalars.rb +175 -33
- data/lib/xitdb/ruby/wire/reader.rb +138 -0
- data/lib/xitdb/ruby/wire.rb +36 -90
- data/lib/xitdb/ruby.rb +43 -3
- data/lib/xitdb/snapshot.rb +8 -0
- data/lib/xitdb/streams/handle.rb +46 -0
- data/lib/xitdb/streams/reader.rb +122 -0
- data/lib/xitdb/streams/writer.rb +43 -0
- data/lib/xitdb/streams.rb +8 -140
- data/lib/xitdb/uint.rb +17 -0
- data/lib/xitdb/value_walker.rb +86 -16
- data/lib/xitdb/version.rb +2 -1
- data/lib/xitdb/views/array.rb +94 -74
- data/lib/xitdb/views/base.rb +144 -12
- data/lib/xitdb/views/linked_array.rb +7 -1
- data/lib/xitdb/views/map.rb +71 -3
- data/lib/xitdb/views/set.rb +13 -1
- data/lib/xitdb/views/slice.rb +101 -0
- data/lib/xitdb/views/sorted.rb +25 -37
- data/lib/xitdb/views/sorted_map.rb +39 -0
- data/lib/xitdb/views/sorted_set.rb +10 -0
- data/lib/xitdb.rb +3 -0
- data/sig/core.rbs +65 -34
- data/sig/public.rbs +24 -19
- data/sig/ruby.rbs +260 -42
- metadata +42 -1
|
@@ -3,17 +3,36 @@
|
|
|
3
3
|
require_relative "../input_guard"
|
|
4
4
|
|
|
5
5
|
module XitDB
|
|
6
|
+
# Defines how Ruby objects map to stored scalar values, keys, and collection layouts.
|
|
7
|
+
# The portable profile follows native xitdb layouts; the Ruby profile also preserves Ruby types and insertion order.
|
|
6
8
|
module Profiles
|
|
7
|
-
#
|
|
9
|
+
# Stores a shared subset of Ruby values in native xitdb layouts.
|
|
10
|
+
# Scalars include nil, signed and unsigned 64-bit integers, Float values, and byte strings.
|
|
11
|
+
# Map keys and set members must be Strings and use raw byte identity.
|
|
12
|
+
# Untagged strings decode as UTF-8 when valid and as binary otherwise.
|
|
13
|
+
# Native maps use the configured hasher; sorted collections compare raw key bytes.
|
|
8
14
|
class Portable
|
|
15
|
+
# Portable encoding does not share Ruby dictionaries or scalar records.
|
|
16
|
+
def packing_state(**)
|
|
17
|
+
nil
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# Maps native storage tags to the logical kinds used by public collection views.
|
|
9
21
|
KINDS = {
|
|
10
22
|
hash_map: :map, counted_hash_map: :map, sorted_map: :sorted_map,
|
|
11
23
|
hash_set: :set, counted_hash_set: :set, sorted_set: :sorted_set,
|
|
12
24
|
array_list: :array, linked_array_list: :linked_array
|
|
13
25
|
}.freeze
|
|
14
26
|
|
|
27
|
+
# Hash algorithm for portable maps and sets.
|
|
28
|
+
#
|
|
29
|
+
# @return [Core::Hasher] hash algorithm for portable maps and sets
|
|
15
30
|
attr_reader :hasher
|
|
16
31
|
|
|
32
|
+
# Selects the hash algorithm used for portable hash maps and sets.
|
|
33
|
+
#
|
|
34
|
+
# @param hasher [Core::Hasher] algorithm matching the database header, SHA-1 by default
|
|
35
|
+
# @raise [TypeError] if the hasher is not a Core::Hasher
|
|
17
36
|
def initialize(hasher: Core::Hasher.sha1)
|
|
18
37
|
raise TypeError, "hasher must be a Core::Hasher" unless hasher.is_a?(Core::Hasher)
|
|
19
38
|
|
|
@@ -21,7 +40,15 @@ module XitDB
|
|
|
21
40
|
freeze
|
|
22
41
|
end
|
|
23
42
|
|
|
24
|
-
|
|
43
|
+
# Stores one portable scalar using native slots or bytes.
|
|
44
|
+
# Use Core::ByteValue to preserve an explicit byte format tag.
|
|
45
|
+
#
|
|
46
|
+
# @param value [nil, Integer, UInt, Float, String, Core::ByteValue] portable scalar
|
|
47
|
+
# @param core [Core::Database] destination storage
|
|
48
|
+
# @return [Core::Slot] stored scalar descriptor
|
|
49
|
+
# @raise [UnsupportedValueError] if the scalar class or singleton behavior is unsupported
|
|
50
|
+
# @raise [RangeError] if an integer exceeds the native 64-bit range
|
|
51
|
+
def encode_scalar(value, core:, **)
|
|
25
52
|
InputGuard.check!(value)
|
|
26
53
|
case value
|
|
27
54
|
when nil then Core::Slot.null
|
|
@@ -36,6 +63,15 @@ module XitDB
|
|
|
36
63
|
end
|
|
37
64
|
end
|
|
38
65
|
|
|
66
|
+
# Reads a native scalar and restores signed integers and Float bit patterns.
|
|
67
|
+
# Tagged bytes remain Core::ByteValue objects; untagged bytes become UTF-8 or binary Strings.
|
|
68
|
+
#
|
|
69
|
+
# @param slot [Core::Slot] scalar descriptor
|
|
70
|
+
# @param core [Core::Database] source storage
|
|
71
|
+
# @param bound [Integer, nil] exclusive read limit, or the core default
|
|
72
|
+
# @param immutable [Boolean] whether to freeze returned untagged Strings
|
|
73
|
+
# @return [nil, Integer, Float, String, Core::ByteValue] decoded portable value
|
|
74
|
+
# @raise [UnsupportedValueError] if the slot tag is not a portable scalar tag
|
|
39
75
|
def decode_scalar(slot, core:, bound: nil, immutable: true)
|
|
40
76
|
case slot.tag
|
|
41
77
|
when :none then nil
|
|
@@ -54,6 +90,12 @@ module XitDB
|
|
|
54
90
|
end
|
|
55
91
|
end
|
|
56
92
|
|
|
93
|
+
# Copies a String key as binary bytes and freezes the identity.
|
|
94
|
+
# String encoding labels do not affect portable key identity.
|
|
95
|
+
#
|
|
96
|
+
# @param value [String] portable map key or set member
|
|
97
|
+
# @return [String] frozen binary key bytes
|
|
98
|
+
# @raise [UnsupportedValueError] if the key is not a supported String
|
|
57
99
|
def key_bytes(value)
|
|
58
100
|
InputGuard.check!(value)
|
|
59
101
|
case value
|
|
@@ -62,10 +104,25 @@ module XitDB
|
|
|
62
104
|
end
|
|
63
105
|
end
|
|
64
106
|
|
|
107
|
+
# Walks a Ruby value tree and stores each scalar and collection through this profile.
|
|
108
|
+
#
|
|
109
|
+
# @param value [Object] source value or nested collection
|
|
110
|
+
# @param context [Context] destination transaction context
|
|
111
|
+
# @param path [Array<Object>] logical location used in errors
|
|
112
|
+
# @return [Core::Slot] stored root of the encoded value
|
|
113
|
+
# @raise [ValueError] if an input value, key, or collection cannot be represented
|
|
65
114
|
def encode(value, context:, path:)
|
|
66
115
|
ValueWalker.encode(value, context: context, path: path)
|
|
67
116
|
end
|
|
68
117
|
|
|
118
|
+
# Returns a lazy view for collections and a decoded Ruby object for scalars.
|
|
119
|
+
#
|
|
120
|
+
# @param slot [Core::Slot] stored value descriptor
|
|
121
|
+
# @param context [Context] source snapshot or transaction context
|
|
122
|
+
# @param path [Array<Object>] logical location of the value
|
|
123
|
+
# @param immutable [Boolean] whether decoded scalar objects are frozen
|
|
124
|
+
# @return [Object] collection view or scalar
|
|
125
|
+
# @raise [ValueError] if stored data cannot be decoded by this profile
|
|
69
126
|
def decode(slot, context:, path:, immutable: true)
|
|
70
127
|
if (kind = KINDS[slot.tag])
|
|
71
128
|
context.view(slot, path: path, kind: kind)
|
|
@@ -74,27 +131,55 @@ module XitDB
|
|
|
74
131
|
end
|
|
75
132
|
end
|
|
76
133
|
|
|
77
|
-
|
|
134
|
+
# Recognizes this profile's collection layout and binds it to the context.
|
|
135
|
+
# An empty root is treated as an empty map.
|
|
136
|
+
#
|
|
137
|
+
# @param slot [Core::Slot] candidate collection descriptor
|
|
138
|
+
# @param context [Context] owner of the read or write
|
|
139
|
+
# @param path [Array<Object>] logical collection location
|
|
140
|
+
# @return [Adapter, nil] bound adapter, or nil for a scalar
|
|
141
|
+
def container(slot, context:, path:, **)
|
|
78
142
|
return nil unless KINDS.key?(slot.tag) || (slot.empty? && path.empty?)
|
|
79
143
|
|
|
80
144
|
Adapter.new(self, context.cursor(slot), context: context, path: path)
|
|
81
145
|
end
|
|
82
146
|
|
|
83
|
-
|
|
147
|
+
# Creates an adapter for a new empty collection at a logical location.
|
|
148
|
+
#
|
|
149
|
+
# @param kind [Symbol, Array(Symbol, Symbol)] collection kind, optionally paired with a sorted key kind
|
|
150
|
+
# @param context [Context] destination transaction context
|
|
151
|
+
# @param path [Array<Object>] logical collection location
|
|
152
|
+
# @return [Adapter] new empty collection adapter
|
|
153
|
+
def empty_container(kind, context:, path:, **)
|
|
84
154
|
kind = kind.first if kind.is_a?(Array)
|
|
85
155
|
Adapter.new(self, context.cursor(Core::Slot.new), context: context, path: path, tag: kind)
|
|
86
156
|
end
|
|
87
157
|
|
|
158
|
+
# Reports the logical kind to preserve during cross-profile copying.
|
|
159
|
+
#
|
|
160
|
+
# @param adapter [Object] source adapter implementing kind
|
|
161
|
+
# @return [Symbol] logical collection kind
|
|
88
162
|
def conversion_kind(adapter)
|
|
89
163
|
adapter.kind
|
|
90
164
|
end
|
|
91
165
|
|
|
166
|
+
# Creates a destination collection while copying between profiles.
|
|
167
|
+
#
|
|
168
|
+
# @param kind [Symbol, Array(Symbol, Symbol)] logical collection kind and optional sorted key kind
|
|
169
|
+
# @param context [Context] destination transaction context
|
|
170
|
+
# @param path [Array<Object>] destination collection location
|
|
171
|
+
# @return [Adapter] destination adapter
|
|
92
172
|
def conversion_container(kind, context:, path:)
|
|
93
173
|
kind = kind.first if kind.is_a?(Array)
|
|
94
174
|
tag = { map: :hash_map, set: :hash_set, array: :array_list, linked_array: :linked_array_list }.fetch(kind, kind)
|
|
95
175
|
empty_container(tag, context: context, path: path)
|
|
96
176
|
end
|
|
97
177
|
|
|
178
|
+
# Exposes byte-backed scalars for conversion without materializing the entire value.
|
|
179
|
+
#
|
|
180
|
+
# @param slot [Core::Slot] candidate scalar descriptor
|
|
181
|
+
# @param context [Context] source snapshot or transaction context
|
|
182
|
+
# @return [Array, nil] pair of byte source and encoding hint, or nil when inapplicable
|
|
98
183
|
def conversion_bytes(slot, context:)
|
|
99
184
|
return unless %i[bytes short_bytes].include?(slot.tag)
|
|
100
185
|
|
|
@@ -102,18 +187,33 @@ module XitDB
|
|
|
102
187
|
[source, source.format_tag ? nil : :portable]
|
|
103
188
|
end
|
|
104
189
|
|
|
190
|
+
# Copies a streamed scalar into this profile using its encoding hint.
|
|
191
|
+
#
|
|
192
|
+
# @param source [Conversion::ByteSource] bounded source bytes
|
|
193
|
+
# @param encoding [Symbol, Encoding, nil] conversion hint identifying portable, binary, encoded, or tagged bytes
|
|
194
|
+
# @param context [Context] destination transaction context
|
|
195
|
+
# @param path [Array<Object>] destination location used in errors
|
|
196
|
+
# @return [Core::Slot] descriptor for the copied bytes
|
|
197
|
+
# @raise [ValueError] if the source cannot be represented by this profile
|
|
105
198
|
def copy_conversion_bytes(source, encoding, context:, path:)
|
|
106
199
|
source.write_to(context, format_tag: encoding ? nil : source.format_tag)
|
|
107
200
|
rescue ValueError => e
|
|
108
201
|
raise e.class.new(e.message, path: path), cause: e
|
|
109
202
|
end
|
|
110
203
|
|
|
204
|
+
# Validates a candidate collection and identifies its storage kind.
|
|
205
|
+
# Scalar values return nil and are handled by the scalar encoder.
|
|
206
|
+
#
|
|
207
|
+
# @param value [Object] candidate input
|
|
208
|
+
# @param path [Array<Object>] logical input location used in errors
|
|
209
|
+
# @return [Symbol, Array(Symbol, Symbol), nil] recognized kind and optional key kind
|
|
210
|
+
# @raise [UnsupportedValueError] if collection behavior cannot be preserved
|
|
111
211
|
def input_kind(value, path:)
|
|
112
212
|
InputGuard.check!(value, path: path)
|
|
113
213
|
klass = Object.instance_method(:class).bind_call(value)
|
|
114
214
|
case value
|
|
115
215
|
when Hash, Array, Set
|
|
116
|
-
validate_container(value, klass, path)
|
|
216
|
+
validate_container!(value, klass, path)
|
|
117
217
|
{ Hash => :hash_map, Array => :array_list, Set => :hash_set }.fetch(klass)
|
|
118
218
|
when CollectionDeclaration
|
|
119
219
|
if value.key_kind
|
|
@@ -124,12 +224,23 @@ module XitDB
|
|
|
124
224
|
end
|
|
125
225
|
end
|
|
126
226
|
|
|
227
|
+
# Detects Hash settings that this profile cannot preserve.
|
|
228
|
+
#
|
|
229
|
+
# @param value [Hash] input hash
|
|
230
|
+
# @return [Boolean] whether a default value, default procedure, or identity comparison is configured
|
|
127
231
|
def hash_defaults?(value)
|
|
128
232
|
default = !BasicObject.instance_method(:equal?).bind_call(value.default, nil)
|
|
129
233
|
!value.default_proc.nil? || default || value.compare_by_identity?
|
|
130
234
|
end
|
|
131
235
|
|
|
132
|
-
|
|
236
|
+
# Rejects collection subclasses and comparison or default behavior that cannot be stored.
|
|
237
|
+
#
|
|
238
|
+
# @param value [Hash, Array, Set] input collection
|
|
239
|
+
# @param klass [Class] exact class obtained without application reflection
|
|
240
|
+
# @param path [Array<Object>] logical input location used in errors
|
|
241
|
+
# @return [void]
|
|
242
|
+
# @raise [UnsupportedValueError] if the collection is a subclass or has unsupported behavior
|
|
243
|
+
def validate_container!(value, klass, path)
|
|
133
244
|
unless [Hash, Array, Set].include?(klass)
|
|
134
245
|
raise UnsupportedValueError.new("container subclasses and singleton behavior are unsupported", path: path)
|
|
135
246
|
end
|
|
@@ -141,7 +252,16 @@ module XitDB
|
|
|
141
252
|
raise UnsupportedValueError.new("Set identity comparison is unsupported", path: path)
|
|
142
253
|
end
|
|
143
254
|
|
|
144
|
-
|
|
255
|
+
# Lazily validates keys and produces selectors with their child values.
|
|
256
|
+
# Distinct input keys with the same portable bytes raise KeyCollisionError during enumeration.
|
|
257
|
+
# Arrays use integer positions; sets use their members as selectors.
|
|
258
|
+
#
|
|
259
|
+
# @param value [Hash, Array, Set, CollectionDeclaration] input collection
|
|
260
|
+
# @param path [Array<Object>] logical parent location used in key errors
|
|
261
|
+
# @return [Enumerator<Array(Object, Object)>] selector and child-value pairs
|
|
262
|
+
# @raise [ValueError] during enumeration if a key cannot be represented
|
|
263
|
+
# @raise [KeyCollisionError] during enumeration if distinct input keys have identical portable bytes
|
|
264
|
+
def input_entries(value, path:, **)
|
|
145
265
|
Enumerator.new do |output|
|
|
146
266
|
# @type var seen: Hash[String, bool]
|
|
147
267
|
seen = {}
|
|
@@ -166,242 +286,8 @@ module XitDB
|
|
|
166
286
|
end
|
|
167
287
|
end
|
|
168
288
|
end
|
|
169
|
-
|
|
170
|
-
# All knowledge of portable storage layouts stays inside this adapter.
|
|
171
|
-
class Adapter
|
|
172
|
-
attr_reader :kind, :cursor
|
|
173
|
-
|
|
174
|
-
def initialize(profile, cursor, context:, path:, tag: nil)
|
|
175
|
-
@profile = profile
|
|
176
|
-
@cursor = cursor
|
|
177
|
-
@context = context
|
|
178
|
-
@path = path
|
|
179
|
-
tag ||= cursor.slot.tag == :none ? :hash_map : cursor.slot.tag
|
|
180
|
-
@kind = KINDS.fetch(tag)
|
|
181
|
-
@collection = case tag
|
|
182
|
-
when :hash_map, :hash_set, :counted_hash_map, :counted_hash_set
|
|
183
|
-
Core::HashMap.new(cursor, counted: %i[counted_hash_map counted_hash_set].include?(tag),
|
|
184
|
-
set: %i[hash_set counted_hash_set].include?(tag))
|
|
185
|
-
when :array_list then Core::ArrayList.new(cursor)
|
|
186
|
-
when :linked_array_list then Core::LinkedArrayList.new(cursor)
|
|
187
|
-
when :sorted_map, :sorted_set then Core::SortedMap.new(cursor, set: tag == :sorted_set)
|
|
188
|
-
end
|
|
189
|
-
end
|
|
190
|
-
|
|
191
|
-
def slot
|
|
192
|
-
cursor.slot
|
|
193
|
-
end
|
|
194
|
-
|
|
195
|
-
def size
|
|
196
|
-
@collection.size
|
|
197
|
-
end
|
|
198
|
-
|
|
199
|
-
def empty?
|
|
200
|
-
size.zero?
|
|
201
|
-
end
|
|
202
|
-
|
|
203
|
-
def lookup(selector)
|
|
204
|
-
collection = @collection
|
|
205
|
-
if collection.is_a?(Core::ArrayList) || collection.is_a?(Core::LinkedArrayList)
|
|
206
|
-
collection.get(index_selector(selector))
|
|
207
|
-
elsif collection.is_a?(Core::SortedMap)
|
|
208
|
-
collection.get(key_bytes(selector))
|
|
209
|
-
else
|
|
210
|
-
collection.get(@profile.hasher.digest(key_bytes(selector)))
|
|
211
|
-
end
|
|
212
|
-
end
|
|
213
|
-
|
|
214
|
-
def assign(selector, stored)
|
|
215
|
-
case kind
|
|
216
|
-
when :array, :linked_array
|
|
217
|
-
if selector == size
|
|
218
|
-
sequence_collection.append(stored)
|
|
219
|
-
else
|
|
220
|
-
sequence_collection.put(index_selector(selector),
|
|
221
|
-
stored)
|
|
222
|
-
end
|
|
223
|
-
when :sorted_map
|
|
224
|
-
sorted_collection.put(key_bytes(selector), value: stored)
|
|
225
|
-
when :sorted_set
|
|
226
|
-
sorted_collection.add(key_bytes(selector))
|
|
227
|
-
when :map, :set
|
|
228
|
-
bytes = key_bytes(selector)
|
|
229
|
-
digest = @profile.hasher.digest(bytes)
|
|
230
|
-
key = @context.database.core.bytes(bytes)
|
|
231
|
-
kind == :map ? hash_collection.put(digest, key: key, value: stored) : hash_collection.add(digest, key: key)
|
|
232
|
-
end
|
|
233
|
-
self
|
|
234
|
-
end
|
|
235
|
-
|
|
236
|
-
def delete(selector)
|
|
237
|
-
case kind
|
|
238
|
-
when :array
|
|
239
|
-
index = selector
|
|
240
|
-
count = size
|
|
241
|
-
raise IndexError, "array index outside list" unless index.is_a?(Integer) && index >= 0 && index < count
|
|
242
|
-
|
|
243
|
-
(index...(count - 1)).each do |position|
|
|
244
|
-
array_collection.put(position, required_slot(array_collection.get(position + 1)))
|
|
245
|
-
end
|
|
246
|
-
array_collection.truncate(count - 1)
|
|
247
|
-
when :linked_array then linked_collection.delete_at(index_selector(selector))
|
|
248
|
-
when :map, :set then hash_collection.remove(@profile.hasher.digest(key_bytes(selector)))
|
|
249
|
-
else sorted_collection.remove(key_bytes(selector))
|
|
250
|
-
end
|
|
251
|
-
end
|
|
252
|
-
|
|
253
|
-
# Radix insertion moves the suffix and performs one radix write per slot.
|
|
254
|
-
# Linked insertion follows one count-guided tree path.
|
|
255
|
-
def insert(index, stored)
|
|
256
|
-
if kind == :linked_array
|
|
257
|
-
Core::BTree.new(cursor).insert(index, stored)
|
|
258
|
-
else
|
|
259
|
-
count = size
|
|
260
|
-
array_collection.append(stored)
|
|
261
|
-
(count - 1).downto(index) do |position|
|
|
262
|
-
array_collection.put(position + 1, required_slot(array_collection.get(position)))
|
|
263
|
-
end
|
|
264
|
-
array_collection.put(index, stored)
|
|
265
|
-
end
|
|
266
|
-
self
|
|
267
|
-
end
|
|
268
|
-
|
|
269
|
-
def truncate(count)
|
|
270
|
-
if kind == :linked_array
|
|
271
|
-
cursor.replace(linked_collection.slice(0, count).cursor.slot)
|
|
272
|
-
else
|
|
273
|
-
array_collection.truncate(count)
|
|
274
|
-
end
|
|
275
|
-
self
|
|
276
|
-
end
|
|
277
|
-
|
|
278
|
-
def slice(start, count)
|
|
279
|
-
return linked_collection.slice(start, count).cursor.slot if kind == :linked_array
|
|
280
|
-
|
|
281
|
-
output = @profile.empty_container(:array_list, context: @context, path: @path)
|
|
282
|
-
count.times { |index| output.assign(index, required_slot(lookup(start + index))) }
|
|
283
|
-
output.slot
|
|
284
|
-
end
|
|
285
|
-
|
|
286
|
-
def concat(other)
|
|
287
|
-
if kind == :linked_array && other.kind == :linked_array
|
|
288
|
-
cursor.replace(linked_collection.concat(Core::LinkedArrayList.new(other.cursor)).cursor.slot)
|
|
289
|
-
else
|
|
290
|
-
other.each_entry { |_index, stored| assign(size, stored) }
|
|
291
|
-
end
|
|
292
|
-
self
|
|
293
|
-
end
|
|
294
|
-
|
|
295
|
-
def rank(key)
|
|
296
|
-
sorted_collection.rank(@profile.key_bytes(key))
|
|
297
|
-
end
|
|
298
|
-
|
|
299
|
-
def each_from(key)
|
|
300
|
-
return enum_for(:each_from, key) unless block_given?
|
|
301
|
-
|
|
302
|
-
sorted_collection.each_from(@profile.key_bytes(key)) { |bytes, stored| yield decode_key(bytes), stored }
|
|
303
|
-
self
|
|
304
|
-
end
|
|
305
|
-
|
|
306
|
-
def each_from_index(index)
|
|
307
|
-
return enum_for(:each_from_index, index) unless block_given?
|
|
308
|
-
|
|
309
|
-
index += size if index.negative?
|
|
310
|
-
return self unless index >= 0 && index < size
|
|
311
|
-
|
|
312
|
-
case kind
|
|
313
|
-
when :array
|
|
314
|
-
(index...size).each { |position| yield position, required_slot(lookup(position)) }
|
|
315
|
-
when :linked_array
|
|
316
|
-
linked_collection.each_from_index(index).with_index(index) { |stored, position| yield position, stored }
|
|
317
|
-
else
|
|
318
|
-
sorted_collection.each_from_index(index) { |bytes, stored| yield decode_key(bytes), stored }
|
|
319
|
-
end
|
|
320
|
-
self
|
|
321
|
-
end
|
|
322
|
-
|
|
323
|
-
def each_entry
|
|
324
|
-
return enum_for(:each_entry) unless block_given?
|
|
325
|
-
|
|
326
|
-
case kind
|
|
327
|
-
when :array, :linked_array
|
|
328
|
-
sequence_collection.each.with_index { |stored, index| yield index, stored }
|
|
329
|
-
when :sorted_map, :sorted_set
|
|
330
|
-
sorted_collection.each { |bytes, stored| yield decode_key(bytes), stored }
|
|
331
|
-
else
|
|
332
|
-
hash_collection.each_entry do |_digest, key, stored|
|
|
333
|
-
value = @profile.decode_scalar(key, core: @context.database.core, bound: @context.read_bound)
|
|
334
|
-
raise CorruptionError, "portable map key is not an untagged string" unless value.is_a?(String)
|
|
335
|
-
|
|
336
|
-
yield value, stored
|
|
337
|
-
end
|
|
338
|
-
end
|
|
339
|
-
self
|
|
340
|
-
end
|
|
341
|
-
|
|
342
|
-
private
|
|
343
|
-
|
|
344
|
-
def index_selector(selector)
|
|
345
|
-
raise TypeError, "index must be an Integer" unless selector.is_a?(Integer)
|
|
346
|
-
|
|
347
|
-
selector
|
|
348
|
-
end
|
|
349
|
-
|
|
350
|
-
def required_slot(slot)
|
|
351
|
-
raise CorruptionError, "missing collection slot" unless slot
|
|
352
|
-
|
|
353
|
-
slot
|
|
354
|
-
end
|
|
355
|
-
|
|
356
|
-
def array_collection
|
|
357
|
-
collection = @collection
|
|
358
|
-
raise CorruptionError, "unexpected collection layout" unless collection.is_a?(Core::ArrayList)
|
|
359
|
-
|
|
360
|
-
collection
|
|
361
|
-
end
|
|
362
|
-
|
|
363
|
-
def linked_collection
|
|
364
|
-
collection = @collection
|
|
365
|
-
raise CorruptionError, "unexpected collection layout" unless collection.is_a?(Core::LinkedArrayList)
|
|
366
|
-
|
|
367
|
-
collection
|
|
368
|
-
end
|
|
369
|
-
|
|
370
|
-
def sorted_collection
|
|
371
|
-
collection = @collection
|
|
372
|
-
raise CorruptionError, "unexpected collection layout" unless collection.is_a?(Core::SortedMap)
|
|
373
|
-
|
|
374
|
-
collection
|
|
375
|
-
end
|
|
376
|
-
|
|
377
|
-
def hash_collection
|
|
378
|
-
collection = @collection
|
|
379
|
-
raise CorruptionError, "unexpected collection layout" unless collection.is_a?(Core::HashMap)
|
|
380
|
-
|
|
381
|
-
collection
|
|
382
|
-
end
|
|
383
|
-
|
|
384
|
-
def sequence_collection
|
|
385
|
-
collection = @collection
|
|
386
|
-
unless collection.is_a?(Core::ArrayList) || collection.is_a?(Core::LinkedArrayList)
|
|
387
|
-
raise CorruptionError, "unexpected sequence layout"
|
|
388
|
-
end
|
|
389
|
-
|
|
390
|
-
collection
|
|
391
|
-
end
|
|
392
|
-
|
|
393
|
-
def key_bytes(selector)
|
|
394
|
-
@profile.key_bytes(selector)
|
|
395
|
-
rescue ValueError => e
|
|
396
|
-
raise e.class.new(e.message, path: @path + [selector]), cause: e
|
|
397
|
-
end
|
|
398
|
-
|
|
399
|
-
def decode_key(bytes)
|
|
400
|
-
value = bytes.dup.force_encoding(Encoding::UTF_8)
|
|
401
|
-
value.force_encoding(Encoding::BINARY) unless value.valid_encoding?
|
|
402
|
-
value.freeze
|
|
403
|
-
end
|
|
404
|
-
end
|
|
405
289
|
end
|
|
406
290
|
end
|
|
407
291
|
end
|
|
292
|
+
|
|
293
|
+
require_relative "portable/adapter"
|