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
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module XitDB
|
|
4
|
+
module Ruby
|
|
5
|
+
# Stores bounded maps and sets directly in insertion order.
|
|
6
|
+
# Larger collections and oversized keys use the indexed adapter.
|
|
7
|
+
# @api private
|
|
8
|
+
class PackedMap < Containers
|
|
9
|
+
MAP = 1
|
|
10
|
+
SET = 6
|
|
11
|
+
SCHEMA_MAP = 4
|
|
12
|
+
DICTIONARY = 5
|
|
13
|
+
MAX_MEMBERS = 16
|
|
14
|
+
MAX_KEY_BYTES = 128
|
|
15
|
+
MAX_KEY_PAYLOAD = 256
|
|
16
|
+
|
|
17
|
+
# Opens a compact record or creates an unpublished empty collection.
|
|
18
|
+
# Packed records initialize common adapter fields without reading an envelope.
|
|
19
|
+
# rubocop:disable-next Lint/MissingSuper
|
|
20
|
+
def initialize(profile, cursor, context:, path:, kind: nil, defer: false, record: nil, state: nil)
|
|
21
|
+
@profile = profile
|
|
22
|
+
@cursor = cursor
|
|
23
|
+
@context = context
|
|
24
|
+
@path = path
|
|
25
|
+
@kind = kind == :set ? :set : :map
|
|
26
|
+
@deferred = defer
|
|
27
|
+
@state = state
|
|
28
|
+
@schema_slot = nil
|
|
29
|
+
@schema_identity = nil
|
|
30
|
+
@fallback = nil
|
|
31
|
+
@keys = []
|
|
32
|
+
@identities = []
|
|
33
|
+
@key_slots = []
|
|
34
|
+
@values = []
|
|
35
|
+
return if kind || cursor.slot.empty?
|
|
36
|
+
|
|
37
|
+
invalid! if cursor.slot.full?
|
|
38
|
+
read_record(record || Core::Record.read(context.database.core, cursor.slot, bound: context.read_bound))
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Publishes a completed bulk value when its descriptor is requested.
|
|
42
|
+
def slot
|
|
43
|
+
publish if @deferred
|
|
44
|
+
cursor.slot
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def size
|
|
48
|
+
fallback = @fallback
|
|
49
|
+
fallback ? fallback.size : @keys.size
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def lookup(selector)
|
|
53
|
+
fallback = @fallback
|
|
54
|
+
return fallback.lookup(selector) if fallback
|
|
55
|
+
|
|
56
|
+
index = @identities.index(key_bytes(selector))
|
|
57
|
+
return nil unless index
|
|
58
|
+
|
|
59
|
+
kind == :set ? Core::Slot.null : @values.fetch(index)
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def assign(selector, stored)
|
|
63
|
+
stored = Pages.persist(stored, @context)
|
|
64
|
+
if (fallback = @fallback)
|
|
65
|
+
fallback.assign(selector, stored)
|
|
66
|
+
else
|
|
67
|
+
identity = key_bytes(selector)
|
|
68
|
+
index = @identities.index(identity)
|
|
69
|
+
if index
|
|
70
|
+
@values[index] = stored unless kind == :set
|
|
71
|
+
elsif size == MAX_MEMBERS || identity.bytesize > MAX_KEY_BYTES
|
|
72
|
+
promote(selector, stored)
|
|
73
|
+
else
|
|
74
|
+
append_member(selector, identity, stored)
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
publish unless @deferred
|
|
78
|
+
self
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def delete(selector)
|
|
82
|
+
if (fallback = @fallback)
|
|
83
|
+
fallback.delete(selector)
|
|
84
|
+
else
|
|
85
|
+
index = @identities.index(key_bytes(selector))
|
|
86
|
+
return self unless index
|
|
87
|
+
|
|
88
|
+
@keys.delete_at(index)
|
|
89
|
+
@identities.delete_at(index)
|
|
90
|
+
@key_slots.delete_at(index)
|
|
91
|
+
@values.delete_at(index)
|
|
92
|
+
end
|
|
93
|
+
publish unless @deferred
|
|
94
|
+
self
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def clear_needed?
|
|
98
|
+
fallback = @fallback
|
|
99
|
+
fallback ? fallback.clear_needed? : size.positive?
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def clear
|
|
103
|
+
if (fallback = @fallback)
|
|
104
|
+
fallback.clear
|
|
105
|
+
else
|
|
106
|
+
@keys.clear
|
|
107
|
+
@identities.clear
|
|
108
|
+
@key_slots.clear
|
|
109
|
+
@values.clear
|
|
110
|
+
end
|
|
111
|
+
publish unless @deferred
|
|
112
|
+
self
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
def each_entry(&block)
|
|
116
|
+
return enum_for(:each_entry) unless block
|
|
117
|
+
|
|
118
|
+
if (fallback = @fallback)
|
|
119
|
+
fallback.each_entry(&block)
|
|
120
|
+
else
|
|
121
|
+
@keys.each_with_index { |key, index| yield(key, @values.fetch(index)) }
|
|
122
|
+
end
|
|
123
|
+
self
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
private
|
|
127
|
+
|
|
128
|
+
def append_member(selector, identity, stored)
|
|
129
|
+
@key_slots << @profile.encode_scalar(selector, core: @context.database.core, state: @state, validated: true)
|
|
130
|
+
@keys << case selector
|
|
131
|
+
when String then Input.string(selector).freeze
|
|
132
|
+
else selector
|
|
133
|
+
end
|
|
134
|
+
@identities << identity
|
|
135
|
+
@values << (kind == :set ? Core::Slot.null : stored)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
def each_normalized_index(index)
|
|
139
|
+
if (fallback = @fallback)
|
|
140
|
+
fallback.each_entry.with_index { |entry, position| yield entry[0], entry[1] if position >= index }
|
|
141
|
+
else
|
|
142
|
+
(index...size).each { |position| yield @keys.fetch(position), @values.fetch(position) }
|
|
143
|
+
end
|
|
144
|
+
self
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def read_record(record)
|
|
148
|
+
return read_schema(record.fields) if record.metadata == SCHEMA_MAP
|
|
149
|
+
|
|
150
|
+
@kind = case record.metadata
|
|
151
|
+
when MAP then :map
|
|
152
|
+
when SET then :set
|
|
153
|
+
else invalid!
|
|
154
|
+
end
|
|
155
|
+
stride = kind == :set ? 1 : 2
|
|
156
|
+
read_members(record.fields, stride)
|
|
157
|
+
rescue ValueError
|
|
158
|
+
invalid!
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def read_members(fields, stride)
|
|
162
|
+
invalid! unless (fields.size % stride).zero? && fields.size / stride <= MAX_MEMBERS
|
|
163
|
+
(fields.size / stride).times do |index|
|
|
164
|
+
original = fields.fetch(index * stride)
|
|
165
|
+
key = bounded_key(original)
|
|
166
|
+
identity = @profile.stored_key_bytes(key)
|
|
167
|
+
invalid! if identity.bytesize > MAX_KEY_BYTES || @identities.include?(identity)
|
|
168
|
+
|
|
169
|
+
@key_slots << original
|
|
170
|
+
@keys << key
|
|
171
|
+
@identities << identity
|
|
172
|
+
@values << (kind == :set ? Core::Slot.null : fields.fetch((index * stride) + 1))
|
|
173
|
+
end
|
|
174
|
+
end
|
|
175
|
+
|
|
176
|
+
def read_schema(fields)
|
|
177
|
+
invalid! unless (3..17).cover?(fields.size)
|
|
178
|
+
schema_slot = fields.fetch(0)
|
|
179
|
+
invalid! unless schema_slot.tag == :record && !schema_slot.full?
|
|
180
|
+
|
|
181
|
+
schema = @state&.decoded_schemas&.[](schema_slot.value)
|
|
182
|
+
schema ||= decode_schema(schema_slot)
|
|
183
|
+
invalid! unless schema[0].size == fields.size - 1
|
|
184
|
+
|
|
185
|
+
@key_slots = schema[0].dup
|
|
186
|
+
@keys = schema[1].dup
|
|
187
|
+
@identities = schema[2].dup
|
|
188
|
+
@values = fields.drop(1)
|
|
189
|
+
@schema_slot = schema_slot
|
|
190
|
+
@schema_identity = @key_slots.map(&:to_bytes).join.b
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
def decode_schema(slot)
|
|
194
|
+
record = Core::Record.read(@context.database.core, slot, bound: @context.read_bound)
|
|
195
|
+
invalid! unless record.metadata == DICTIONARY && (2..MAX_MEMBERS).cover?(record.fields.size)
|
|
196
|
+
keys = record.fields.map { |original| bounded_key(original) }
|
|
197
|
+
identities = keys.map { |key| @profile.stored_key_bytes(key) }
|
|
198
|
+
invalid! if identities.any? { |identity| identity.bytesize > MAX_KEY_BYTES }
|
|
199
|
+
invalid! unless identities.uniq.size == identities.size
|
|
200
|
+
|
|
201
|
+
# @type var schema: decoded_schema
|
|
202
|
+
schema = [record.fields, keys.freeze, identities.freeze]
|
|
203
|
+
schema.freeze
|
|
204
|
+
@state&.store_decoded_schema(slot.value, schema)
|
|
205
|
+
schema
|
|
206
|
+
end
|
|
207
|
+
|
|
208
|
+
def key_bytes(selector)
|
|
209
|
+
@profile.key_bytes(selector, state: @state)
|
|
210
|
+
rescue ValueError => e
|
|
211
|
+
raise e.class.new(e.message, path: @path + [selector]), cause: e
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
def bounded_key(slot)
|
|
215
|
+
if %i[bytes short_bytes].include?(slot.tag)
|
|
216
|
+
source = Core::Bytes.open(@context.database.core, slot, bound: @context.read_bound)
|
|
217
|
+
invalid! if source.size > MAX_KEY_PAYLOAD
|
|
218
|
+
end
|
|
219
|
+
decode_key(slot)
|
|
220
|
+
end
|
|
221
|
+
|
|
222
|
+
def promote(selector, stored)
|
|
223
|
+
fallback = Containers.new(@profile, @context.cursor(Core::Slot.new), context: @context,
|
|
224
|
+
path: @path, kind: kind, defer: true)
|
|
225
|
+
@keys.each_with_index { |key, index| fallback.assign(key, @values.fetch(index)) }
|
|
226
|
+
fallback.assign(selector, stored)
|
|
227
|
+
@fallback = fallback
|
|
228
|
+
end
|
|
229
|
+
|
|
230
|
+
def publish
|
|
231
|
+
stored = if (fallback = @fallback)
|
|
232
|
+
fallback.slot
|
|
233
|
+
else
|
|
234
|
+
metadata, fields = storage_record
|
|
235
|
+
Core::Record.append(@context.writable_transaction!, metadata: metadata, fields: fields)
|
|
236
|
+
end
|
|
237
|
+
cursor.replace(stored)
|
|
238
|
+
@deferred = false
|
|
239
|
+
self
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
def storage_record
|
|
243
|
+
if kind == :map && size >= 2
|
|
244
|
+
identity = @key_slots.map(&:to_bytes).join.b
|
|
245
|
+
schema = @schema_slot if @schema_identity == identity
|
|
246
|
+
schema ||= @state&.schema(identity) do
|
|
247
|
+
Core::Record.append(@context.writable_transaction!, metadata: DICTIONARY, fields: @key_slots)
|
|
248
|
+
end
|
|
249
|
+
if schema
|
|
250
|
+
@schema_slot = schema
|
|
251
|
+
@schema_identity = identity
|
|
252
|
+
return [SCHEMA_MAP, [schema, *@values]]
|
|
253
|
+
end
|
|
254
|
+
end
|
|
255
|
+
[kind == :set ? SET : MAP, record_fields]
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
def record_fields
|
|
259
|
+
return @key_slots if kind == :set
|
|
260
|
+
|
|
261
|
+
# @type var fields: Array[Core::Slot]
|
|
262
|
+
fields = []
|
|
263
|
+
@key_slots.each_with_index { |key, index| fields << key << @values.fetch(index) }
|
|
264
|
+
fields
|
|
265
|
+
end
|
|
266
|
+
|
|
267
|
+
def invalid!
|
|
268
|
+
raise InvalidValueError, "invalid packed Ruby map or set"
|
|
269
|
+
end
|
|
270
|
+
end
|
|
271
|
+
end
|
|
272
|
+
end
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module XitDB
|
|
4
|
+
module Ruby
|
|
5
|
+
# Bounded sharing tables owned by one encode or read-only materialization.
|
|
6
|
+
# Stored offsets never survive transaction rollback or another operation.
|
|
7
|
+
# @api private
|
|
8
|
+
class PackingState
|
|
9
|
+
MAX_SCALARS = 256
|
|
10
|
+
MAX_SCHEMAS = 64
|
|
11
|
+
MAX_BYTES = 64
|
|
12
|
+
|
|
13
|
+
attr_reader :scalars, :schemas, :decoded_schemas
|
|
14
|
+
|
|
15
|
+
def initialize
|
|
16
|
+
@scalars = {}
|
|
17
|
+
@schemas = {}
|
|
18
|
+
@decoded_schemas = {}
|
|
19
|
+
# @type var frozen_keys: Hash[String, String]
|
|
20
|
+
frozen_keys = {}
|
|
21
|
+
@frozen_keys = frozen_keys.compare_by_identity
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Reuses validation only for the same immutable exact String object.
|
|
25
|
+
def key_bytes(value, &)
|
|
26
|
+
case value
|
|
27
|
+
when String then string_key_bytes(value, &)
|
|
28
|
+
else yield
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# Includes the exact type, encoding, and bytes in short scalar identity.
|
|
33
|
+
# Input validation belongs to the caller, including cache hits.
|
|
34
|
+
def scalar_identity(value)
|
|
35
|
+
klass = Object.instance_method(:class).bind_call(value)
|
|
36
|
+
return unless String.equal?(klass) || Symbol.equal?(klass)
|
|
37
|
+
|
|
38
|
+
text = case value
|
|
39
|
+
when String then value
|
|
40
|
+
when Symbol then value.to_s
|
|
41
|
+
else return
|
|
42
|
+
end
|
|
43
|
+
return if text.bytesize > MAX_BYTES
|
|
44
|
+
|
|
45
|
+
# @type var identity: scalar_identity
|
|
46
|
+
identity = [klass, text.encoding, text.b.freeze]
|
|
47
|
+
identity.freeze
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def store_scalar(identity, slot)
|
|
51
|
+
@scalars[identity] = slot if @scalars.size < MAX_SCALARS
|
|
52
|
+
slot
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def schema(identity)
|
|
56
|
+
stored = @schemas[identity]
|
|
57
|
+
return stored if stored
|
|
58
|
+
return if @schemas.size == MAX_SCHEMAS
|
|
59
|
+
|
|
60
|
+
@schemas[identity.freeze] = yield
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def store_decoded_schema(offset, schema)
|
|
64
|
+
@decoded_schemas[offset] = schema if @decoded_schemas.size < MAX_SCHEMAS
|
|
65
|
+
schema
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
private
|
|
69
|
+
|
|
70
|
+
def string_key_bytes(value)
|
|
71
|
+
# @type var length: Integer
|
|
72
|
+
length = String.instance_method(:bytesize).bind_call(value)
|
|
73
|
+
eligible = String.equal?(Object.instance_method(:class).bind_call(value)) &&
|
|
74
|
+
Object.instance_method(:frozen?).bind_call(value) &&
|
|
75
|
+
length <= MAX_BYTES
|
|
76
|
+
return @frozen_keys.fetch(value) if eligible && @frozen_keys.key?(value)
|
|
77
|
+
|
|
78
|
+
identity = yield
|
|
79
|
+
@frozen_keys[value] = identity if eligible && @frozen_keys.size < MAX_SCALARS
|
|
80
|
+
identity
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module XitDB
|
|
4
|
+
module Ruby
|
|
5
|
+
# Keeps fixed-size groups of flat rows in independently replaceable pages.
|
|
6
|
+
# @api private
|
|
7
|
+
class PageArray < Containers
|
|
8
|
+
# rubocop:disable-next Lint/MissingSuper
|
|
9
|
+
def initialize(profile, cursor, context:, path:, record:)
|
|
10
|
+
@profile = profile
|
|
11
|
+
@cursor = cursor
|
|
12
|
+
@context = context
|
|
13
|
+
@path = path
|
|
14
|
+
@kind = :array
|
|
15
|
+
@fallback = nil
|
|
16
|
+
fields = record.fields
|
|
17
|
+
invalid! unless !cursor.slot.full? && fields.size == 2 && fields.none?(&:full?) &&
|
|
18
|
+
fields.fetch(0).tag == :uint && fields.fetch(1).tag == :array_list
|
|
19
|
+
|
|
20
|
+
@count = fields.fetch(0).value
|
|
21
|
+
@pages = Core::ArrayList.new(context.cursor(fields.fetch(1)))
|
|
22
|
+
invalid! unless @count.positive? && @pages.size == (@count + Pages::ROW_LIMIT - 1) / Pages::ROW_LIMIT
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def size
|
|
26
|
+
fallback = @fallback
|
|
27
|
+
fallback ? fallback.size : @count
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def lookup(selector)
|
|
31
|
+
fallback = @fallback
|
|
32
|
+
return fallback.lookup(selector) if fallback
|
|
33
|
+
|
|
34
|
+
index = index_selector!(selector)
|
|
35
|
+
index += size if index.negative?
|
|
36
|
+
return unless index >= 0 && index < size
|
|
37
|
+
|
|
38
|
+
page_index, row_index = index.divmod(Pages::ROW_LIMIT)
|
|
39
|
+
page = page_slot!(page_index)
|
|
40
|
+
if page.tag == :record
|
|
41
|
+
generic_page(page, page_index).lookup(row_index)
|
|
42
|
+
else
|
|
43
|
+
PageReference.new(page, row_index, page_size(page_index))
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def each_entry
|
|
48
|
+
return enum_for(:each_entry) unless block_given?
|
|
49
|
+
|
|
50
|
+
size.times { |index| yield index, required_slot!(lookup(index)) }
|
|
51
|
+
self
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
def materialize_flat
|
|
55
|
+
return if @fallback
|
|
56
|
+
|
|
57
|
+
# @type var output: Array[PageCodec::row]
|
|
58
|
+
output = []
|
|
59
|
+
@pages.each.with_index do |page, index|
|
|
60
|
+
return nil if page.tag == :record
|
|
61
|
+
|
|
62
|
+
output.concat(PageCodec.decode_page(Pages.bytes(page, @context), page_size(index)))
|
|
63
|
+
end
|
|
64
|
+
output
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def assign(selector, stored)
|
|
68
|
+
index = index_selector!(selector)
|
|
69
|
+
if @fallback || index == size
|
|
70
|
+
promote.assign(index, stored)
|
|
71
|
+
else
|
|
72
|
+
index += size if index.negative?
|
|
73
|
+
raise IndexError, "array index outside list" unless index >= 0 && index < size
|
|
74
|
+
|
|
75
|
+
replace_row(index, stored)
|
|
76
|
+
end
|
|
77
|
+
publish
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def insert(index, stored)
|
|
81
|
+
promote.insert(index, stored)
|
|
82
|
+
publish
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
def delete(selector)
|
|
86
|
+
promote.delete(selector)
|
|
87
|
+
publish
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
def truncate(count)
|
|
91
|
+
promote.truncate(count)
|
|
92
|
+
publish
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def clear
|
|
96
|
+
promote.clear
|
|
97
|
+
publish
|
|
98
|
+
end
|
|
99
|
+
|
|
100
|
+
private
|
|
101
|
+
|
|
102
|
+
def page_size(index)
|
|
103
|
+
[Pages::ROW_LIMIT, @count - (index * Pages::ROW_LIMIT)].min
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def page_slot!(index)
|
|
107
|
+
page = required_slot!(@pages.get(index))
|
|
108
|
+
invalid! if page.full? || !%i[bytes short_bytes record].include?(page.tag)
|
|
109
|
+
page
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def generic_page(page, index)
|
|
113
|
+
record = Core::Record.read(@context.database.core, page, bound: @context.read_bound)
|
|
114
|
+
invalid! unless [PackedArray::INLINE, PackedArray::TREE].include?(record.metadata)
|
|
115
|
+
|
|
116
|
+
adapter = PackedArray.new(@profile, @context.cursor(page), context: @context, path: @path, record: record)
|
|
117
|
+
invalid! unless adapter.size == page_size(index)
|
|
118
|
+
|
|
119
|
+
adapter
|
|
120
|
+
end
|
|
121
|
+
|
|
122
|
+
def replace_row(index, stored)
|
|
123
|
+
page_index, row_index = index.divmod(Pages::ROW_LIMIT)
|
|
124
|
+
page = page_slot!(page_index)
|
|
125
|
+
if page.tag == :record
|
|
126
|
+
adapter = generic_page(page, page_index)
|
|
127
|
+
adapter.assign(row_index, stored)
|
|
128
|
+
@pages.put(page_index, adapter.slot)
|
|
129
|
+
return
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
rows = PageCodec.decode_page(Pages.bytes(page, @context), page_size(page_index))
|
|
133
|
+
replacement = flat_row(stored)
|
|
134
|
+
if replacement
|
|
135
|
+
rows[row_index] = replacement
|
|
136
|
+
bytes = PageCodec.encode_stored_page(rows)
|
|
137
|
+
if bytes
|
|
138
|
+
@pages.put(page_index, @context.database.core.bytes(bytes))
|
|
139
|
+
return
|
|
140
|
+
end
|
|
141
|
+
end
|
|
142
|
+
adapter = promote_page(page, page_index)
|
|
143
|
+
adapter.assign(row_index, stored)
|
|
144
|
+
@pages.put(page_index, adapter.slot)
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def flat_row(stored)
|
|
148
|
+
return stored.decoded if stored.is_a?(PageDraft)
|
|
149
|
+
|
|
150
|
+
adapter = @profile.container(stored, context: @context, path: @path)
|
|
151
|
+
return unless adapter && adapter.kind == :map && adapter.size <= 16
|
|
152
|
+
|
|
153
|
+
# @type var output: PageCodec::row
|
|
154
|
+
output = {}
|
|
155
|
+
adapter.each_entry do |key, cell|
|
|
156
|
+
return nil unless key.is_a?(String)
|
|
157
|
+
return nil unless page_cell?(cell)
|
|
158
|
+
|
|
159
|
+
value = @profile.decode(cell, context: @context, path: @path, immutable: false)
|
|
160
|
+
case value
|
|
161
|
+
when NilClass, TrueClass, FalseClass, Integer, Float, String then output[key] = value
|
|
162
|
+
else return nil
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
output
|
|
166
|
+
end
|
|
167
|
+
|
|
168
|
+
# Recognizes built-in cells without invoking application decoders.
|
|
169
|
+
def page_cell?(slot)
|
|
170
|
+
return true if slot.is_a?(PageCell) || %i[none int uint float].include?(slot.tag)
|
|
171
|
+
return false unless slot.full? && %i[bytes short_bytes].include?(slot.tag)
|
|
172
|
+
|
|
173
|
+
source = Core::Bytes.open(@context.database.core, slot, bound: @context.read_bound)
|
|
174
|
+
return false unless source.format_tag == "rb" && source.size.between?(2, Pages::BYTE_LIMIT)
|
|
175
|
+
|
|
176
|
+
prefix = source.read(0, 2)
|
|
177
|
+
prefix == "\x01\x01".b || prefix == "\x01\x03".b
|
|
178
|
+
end
|
|
179
|
+
|
|
180
|
+
def promote_page(page, index)
|
|
181
|
+
output = PackedArray.new(@profile, @context.cursor(Core::Slot.new), context: @context, path: @path,
|
|
182
|
+
kind: :array, defer: true)
|
|
183
|
+
count = page_size(index)
|
|
184
|
+
count.times { |row_index| output.assign(row_index, PageReference.new(page, row_index, count)) }
|
|
185
|
+
output.slot
|
|
186
|
+
output
|
|
187
|
+
end
|
|
188
|
+
|
|
189
|
+
def promote
|
|
190
|
+
fallback = @fallback
|
|
191
|
+
return fallback if fallback
|
|
192
|
+
|
|
193
|
+
output = PackedArray.new(@profile, @context.cursor(Core::Slot.new), context: @context, path: @path,
|
|
194
|
+
kind: :array, defer: true)
|
|
195
|
+
each_entry { |index, stored| output.assign(index, stored) }
|
|
196
|
+
output.slot
|
|
197
|
+
@fallback = output
|
|
198
|
+
end
|
|
199
|
+
|
|
200
|
+
def publish
|
|
201
|
+
fallback = @fallback
|
|
202
|
+
stored = if fallback
|
|
203
|
+
fallback.slot
|
|
204
|
+
else
|
|
205
|
+
Core::Record.append(@context.writable_transaction!, metadata: Pages::ARRAY,
|
|
206
|
+
fields: [Core::Slot.uint(@count),
|
|
207
|
+
@pages.cursor.slot])
|
|
208
|
+
end
|
|
209
|
+
cursor.replace(stored)
|
|
210
|
+
self
|
|
211
|
+
end
|
|
212
|
+
|
|
213
|
+
def invalid!
|
|
214
|
+
raise InvalidValueError, "invalid Ruby page array"
|
|
215
|
+
end
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
end
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module XitDB
|
|
4
|
+
module Ruby
|
|
5
|
+
# Holds one decoded scalar for an adapter operation.
|
|
6
|
+
# @api private
|
|
7
|
+
class PageCell < Core::Slot
|
|
8
|
+
attr_reader :decoded
|
|
9
|
+
|
|
10
|
+
def initialize(value)
|
|
11
|
+
@decoded = value
|
|
12
|
+
super(tag: 127)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def to_bytes
|
|
16
|
+
raise InvalidValueError, "persist page cell before serialization"
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "input"
|
|
4
|
+
require_relative "page_encoder"
|
|
5
|
+
require_relative "page_decoder"
|
|
6
|
+
require_relative "indexed_page_encoder"
|
|
7
|
+
require_relative "indexed_page_decoder"
|
|
8
|
+
|
|
9
|
+
module XitDB
|
|
10
|
+
module Ruby
|
|
11
|
+
# Encodes bounded flat rows with Ruby's byte and numeric operations.
|
|
12
|
+
# @api private
|
|
13
|
+
module PageCodec
|
|
14
|
+
PAGE_LIMIT = 65_536
|
|
15
|
+
ROW_LIMIT = 1024
|
|
16
|
+
FIELD_LIMIT = 16
|
|
17
|
+
|
|
18
|
+
def self.encode_page(rows)
|
|
19
|
+
IndexedPageEncoder.new.encode(rows) || PageEncoder.new.encode(rows)
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# Rows must contain fresh built-in values decoded from stored slots.
|
|
23
|
+
def self.encode_stored_page(rows)
|
|
24
|
+
IndexedPageEncoder.new(stored: true).encode(rows) || PageEncoder.new(stored: true).encode(rows)
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def self.decode_page(bytes, expected = nil)
|
|
28
|
+
decoder(bytes, expected).page
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def self.decode_row(bytes, index, expected = nil)
|
|
32
|
+
raise TypeError, "row index must be an Integer" unless Object.instance_method(:is_a?).bind_call(index, Integer)
|
|
33
|
+
raise IndexError, "row outside Ruby page" unless index.between?(0, (1 << 62) - 1)
|
|
34
|
+
|
|
35
|
+
decoder(bytes, expected).row(index)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def self.decoder(bytes, expected)
|
|
39
|
+
# @type var prefix: String
|
|
40
|
+
prefix = String.instance_method(:byteslice).bind_call(bytes, 0, 4)
|
|
41
|
+
prefix == "XR01" ? IndexedPageDecoder.new(bytes, expected) : PageDecoder.new(bytes, expected)
|
|
42
|
+
end
|
|
43
|
+
private_class_method :decoder
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|