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
data/lib/xitdb/profiles/ruby.rb
CHANGED
|
@@ -4,26 +4,82 @@ require_relative "../ruby/codec"
|
|
|
4
4
|
|
|
5
5
|
module XitDB
|
|
6
6
|
module Profiles
|
|
7
|
-
#
|
|
7
|
+
# Connects Ruby values to scalar encoders and collection adapters.
|
|
8
|
+
# Maps and sets preserve insertion order; typed sorted collections preserve their declared key order.
|
|
9
|
+
# SHA-256 hashes canonical key bytes under the "rb04" header identifier.
|
|
10
|
+
# Application codecs belong to this profile instance and are not written as executable code.
|
|
8
11
|
class Ruby
|
|
9
12
|
attr_reader :hasher, :codecs
|
|
10
13
|
|
|
14
|
+
# @!attribute [r] hasher
|
|
15
|
+
# SHA-256 hasher with the Ruby profile identifier.
|
|
16
|
+
#
|
|
17
|
+
# @return [Core::Hasher] SHA-256 hasher with the Ruby profile identifier
|
|
18
|
+
# @!attribute [r] codecs
|
|
19
|
+
# Frozen validated application registrations.
|
|
20
|
+
#
|
|
21
|
+
# @return [Array<XitDB::Ruby::Codec>] frozen validated application registrations
|
|
22
|
+
|
|
23
|
+
# Validates application codecs and selects the Ruby profile SHA-256 hasher.
|
|
24
|
+
#
|
|
25
|
+
# @param codecs [Array<XitDB::Ruby::Codec>] serializers for exact application classes
|
|
26
|
+
# @raise [CodecConfigurationError] if registrations are invalid or conflict
|
|
11
27
|
def initialize(codecs: [])
|
|
12
28
|
@registry = XitDB::Ruby::CodecRegistry.new(codecs)
|
|
13
29
|
@codecs = @registry.codecs
|
|
14
|
-
@hasher = Core::Hasher.new(id: "
|
|
30
|
+
@hasher = Core::Hasher.new(id: "rb04", size: 32) { |bytes| Digest::SHA256.digest(bytes) }
|
|
15
31
|
freeze
|
|
16
32
|
end
|
|
17
33
|
|
|
34
|
+
# Walks a Ruby value tree and stores each scalar and collection through this profile.
|
|
35
|
+
#
|
|
36
|
+
# @param value [Object] source value or nested collection
|
|
37
|
+
# @param context [Context] destination transaction context
|
|
38
|
+
# @param path [Array<Object>] logical location used in errors
|
|
39
|
+
# @return [Core::Slot] stored root of the encoded value
|
|
40
|
+
# @raise [ValueError] if an input value, key, or collection cannot be represented
|
|
18
41
|
def encode(value, context:, path:)
|
|
19
42
|
ValueWalker.encode(value, context: context, path: path)
|
|
20
43
|
end
|
|
21
44
|
|
|
22
|
-
|
|
23
|
-
|
|
45
|
+
# Delegates built-in and application scalar storage to the Ruby encoder.
|
|
46
|
+
#
|
|
47
|
+
# @param value [Object] supported scalar or registered application value
|
|
48
|
+
# @param core [Core::Database] destination storage
|
|
49
|
+
# @return [Core::Slot] stored scalar descriptor
|
|
50
|
+
# @raise [ValueError] if the scalar cannot be represented
|
|
51
|
+
def encode_scalar(value, core:, state: nil, validated: false)
|
|
52
|
+
XitDB::Ruby::Input.check!(value) unless validated
|
|
53
|
+
value = XitDB::Ruby::Input.string(value) if Object.instance_method(:instance_of?).bind_call(value, String)
|
|
54
|
+
identity = state&.scalar_identity(value)
|
|
55
|
+
cached = state.scalars[identity] if state && identity
|
|
56
|
+
return cached if cached
|
|
57
|
+
|
|
58
|
+
stored = XitDB::Ruby::Scalars.encode_validated(value, core: core, registry: @registry)
|
|
59
|
+
state.store_scalar(identity, stored) if state && identity
|
|
60
|
+
stored
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Owns bounded tables for exactly one encoding or immutable traversal.
|
|
64
|
+
def packing_state(context:, reading:)
|
|
65
|
+
XitDB::Ruby::PackingState.new unless reading && context.transaction
|
|
24
66
|
end
|
|
25
67
|
|
|
68
|
+
# Returns a lazy view for collections and a decoded Ruby object for scalars.
|
|
69
|
+
#
|
|
70
|
+
# @param slot [Core::Slot] stored value descriptor
|
|
71
|
+
# @param context [Context] source snapshot or transaction context
|
|
72
|
+
# @param path [Array<Object>] logical location of the value
|
|
73
|
+
# @param immutable [Boolean] whether decoded scalar objects are frozen
|
|
74
|
+
# @return [Object] collection view or scalar
|
|
75
|
+
# @raise [ValueError] if stored data cannot be decoded by this profile
|
|
26
76
|
def decode(slot, context:, path:, immutable: true)
|
|
77
|
+
if slot.is_a?(XitDB::Ruby::PageCell)
|
|
78
|
+
value = slot.decoded
|
|
79
|
+
return value unless value.is_a?(String)
|
|
80
|
+
|
|
81
|
+
return immutable ? value.dup.freeze : value.dup
|
|
82
|
+
end
|
|
27
83
|
if (adapter = container(slot, context: context, path: path))
|
|
28
84
|
context.view(slot, path: path, kind: adapter.kind)
|
|
29
85
|
else
|
|
@@ -32,22 +88,88 @@ module XitDB
|
|
|
32
88
|
end
|
|
33
89
|
end
|
|
34
90
|
|
|
35
|
-
|
|
91
|
+
# Builds canonical bytes for Ruby map and set key identity.
|
|
92
|
+
#
|
|
93
|
+
# @param value [nil, Boolean, Integer, Float, String, Symbol] supported exact key class
|
|
94
|
+
# @return [String] frozen binary identity bytes
|
|
95
|
+
# @raise [UnsupportedValueError] if the key type or value is unsupported
|
|
96
|
+
def key_bytes(value, state: nil)
|
|
97
|
+
return state.key_bytes(value) { XitDB::Ruby::KeyEncoding.dump(value) } if state
|
|
98
|
+
|
|
36
99
|
XitDB::Ruby::KeyEncoding.dump(value)
|
|
37
100
|
end
|
|
38
101
|
|
|
39
|
-
|
|
40
|
-
|
|
102
|
+
# Stored keys come from built-in decoders, without application callbacks.
|
|
103
|
+
def stored_key_bytes(value)
|
|
104
|
+
XitDB::Ruby::KeyEncoding.dump_stored(value)
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
# Recognizes this profile's collection layout and binds it to the context.
|
|
108
|
+
# An empty root is treated as an empty map.
|
|
109
|
+
#
|
|
110
|
+
# @param slot [Core::Slot] candidate collection descriptor
|
|
111
|
+
# @param context [Context] owner of the read or write
|
|
112
|
+
# @param path [Array<Object>] logical collection location
|
|
113
|
+
# @return [XitDB::Ruby::Containers, nil] bound adapter, or nil for a scalar
|
|
114
|
+
def container(slot, context:, path:, state: nil)
|
|
115
|
+
return nil if slot.is_a?(XitDB::Ruby::PageCell)
|
|
116
|
+
if slot.is_a?(XitDB::Ruby::PageReference)
|
|
117
|
+
return XitDB::Ruby::PageRow.new(self, context.cursor(slot), context: context, path: path, reference: slot)
|
|
118
|
+
end
|
|
119
|
+
return record_container(slot, context: context, path: path, state: state) if slot.tag == :record
|
|
120
|
+
if slot.empty? && path.empty?
|
|
121
|
+
return XitDB::Ruby::PackedMap.new(self, context.cursor(slot), context: context, path: path)
|
|
122
|
+
end
|
|
123
|
+
return nil unless slot.tag == :array_list
|
|
41
124
|
|
|
42
125
|
XitDB::Ruby::Containers.new(self, context.cursor(slot), context: context, path: path)
|
|
43
126
|
end
|
|
44
127
|
|
|
45
|
-
|
|
128
|
+
# Dispatches bounded native records after validating their common header.
|
|
129
|
+
def record_container(slot, context:, path:, state:)
|
|
130
|
+
raise InvalidValueError, "invalid Ruby record flag" if slot.full?
|
|
131
|
+
|
|
132
|
+
record = Core::Record.read(context.database.core, slot, bound: context.read_bound)
|
|
133
|
+
case record.metadata
|
|
134
|
+
when XitDB::Ruby::Pages::ARRAY
|
|
135
|
+
XitDB::Ruby::PageArray.new(self, context.cursor(slot), context: context, path: path, record: record)
|
|
136
|
+
when XitDB::Ruby::Pages::ROW
|
|
137
|
+
reference = XitDB::Ruby::Pages.reference(record)
|
|
138
|
+
XitDB::Ruby::PageRow.new(self, context.cursor(slot), context: context, path: path, reference: reference)
|
|
139
|
+
when 2, 3
|
|
140
|
+
XitDB::Ruby::PackedArray.new(self, context.cursor(slot), context: context, path: path, record: record)
|
|
141
|
+
else
|
|
142
|
+
XitDB::Ruby::PackedMap.new(self, context.cursor(slot), context: context, path: path, record: record,
|
|
143
|
+
state: state)
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
private :record_container
|
|
147
|
+
|
|
148
|
+
# Creates an adapter for a new empty collection at a logical location.
|
|
149
|
+
#
|
|
150
|
+
# @param kind [Symbol, Array(Symbol, Symbol)] collection kind, optionally paired with a sorted key kind
|
|
151
|
+
# @param context [Context] destination transaction context
|
|
152
|
+
# @param path [Array<Object>] logical collection location
|
|
153
|
+
# @return [XitDB::Ruby::Containers] new empty collection adapter
|
|
154
|
+
def empty_container(kind, context:, path:, state: nil)
|
|
46
155
|
kind, key_kind = kind if kind.is_a?(::Array)
|
|
156
|
+
if kind == :array
|
|
157
|
+
return XitDB::Ruby::PackedArray.new(self, context.cursor(Core::Slot.new), context: context, path: path,
|
|
158
|
+
kind: kind, defer: true)
|
|
159
|
+
end
|
|
160
|
+
if %i[map set].include?(kind)
|
|
161
|
+
return XitDB::Ruby::PackedMap.new(self, context.cursor(Core::Slot.new), context: context, path: path,
|
|
162
|
+
kind: kind, defer: true, state: state)
|
|
163
|
+
end
|
|
47
164
|
XitDB::Ruby::Containers.new(self, context.cursor(Core::Slot.new), context: context, path: path,
|
|
48
|
-
kind: kind, key_kind: key_kind)
|
|
165
|
+
kind: kind, key_kind: key_kind, defer: true)
|
|
49
166
|
end
|
|
50
167
|
|
|
168
|
+
# Retains the declared key kind when converting a typed sorted collection.
|
|
169
|
+
#
|
|
170
|
+
# @param adapter [Object] source collection adapter
|
|
171
|
+
# @return [Symbol, Array(Symbol, Symbol)] logical kind, paired with a key kind for sorted collections
|
|
172
|
+
# @raise [InvalidValueError] if the sorted envelope lacks its key-kind field
|
|
51
173
|
def conversion_kind(adapter)
|
|
52
174
|
return adapter.kind unless %i[sorted_map sorted_set].include?(adapter.kind)
|
|
53
175
|
|
|
@@ -58,12 +180,24 @@ module XitDB
|
|
|
58
180
|
[adapter.kind, XitDB::Ruby::Containers::KEY_KINDS.key(key)]
|
|
59
181
|
end
|
|
60
182
|
|
|
183
|
+
# Creates a destination collection while copying between profiles.
|
|
184
|
+
#
|
|
185
|
+
# @param kind [Symbol, Array(Symbol, Symbol)] logical collection kind and optional sorted key kind
|
|
186
|
+
# @param context [Context] destination transaction context
|
|
187
|
+
# @param path [Array<Object>] destination collection location
|
|
188
|
+
# @return [XitDB::Ruby::Containers] destination adapter
|
|
61
189
|
def conversion_container(kind, context:, path:)
|
|
62
190
|
# @type var kind: Symbol | [Symbol, Symbol?]
|
|
63
191
|
kind = [kind, :string] if kind.is_a?(Symbol) && %i[sorted_map sorted_set].include?(kind)
|
|
64
192
|
empty_container(kind, context: context, path: path)
|
|
65
193
|
end
|
|
66
194
|
|
|
195
|
+
# Exposes byte-backed scalars for conversion without materializing the entire value.
|
|
196
|
+
# Ruby String payloads expose their original bytes and Encoding; other Ruby scalar payloads return nil.
|
|
197
|
+
#
|
|
198
|
+
# @param slot [Core::Slot] candidate scalar descriptor
|
|
199
|
+
# @param context [Context] source snapshot or transaction context
|
|
200
|
+
# @return [Array, nil] pair of byte source and encoding hint, or nil when inapplicable
|
|
67
201
|
def conversion_bytes(slot, context:)
|
|
68
202
|
return unless %i[bytes short_bytes].include?(slot.tag)
|
|
69
203
|
|
|
@@ -89,6 +223,14 @@ module XitDB
|
|
|
89
223
|
[source.slice(position, size), encoding]
|
|
90
224
|
end
|
|
91
225
|
|
|
226
|
+
# Copies a streamed scalar into this profile using its encoding hint.
|
|
227
|
+
#
|
|
228
|
+
# @param source [Conversion::ByteSource] bounded source bytes
|
|
229
|
+
# @param encoding [Symbol, Encoding, nil] conversion hint identifying portable, binary, encoded, or tagged bytes
|
|
230
|
+
# @param context [Context] destination transaction context
|
|
231
|
+
# @param path [Array<Object>] destination location used in errors
|
|
232
|
+
# @return [Core::Slot] descriptor for the copied bytes
|
|
233
|
+
# @raise [ValueError] if the source cannot be represented by this profile
|
|
92
234
|
def copy_conversion_bytes(source, encoding, context:, path:)
|
|
93
235
|
if source.format_tag == "rb"
|
|
94
236
|
raise UnsupportedValueError.new("portable rb bytes require an explicit transform", path: path)
|
|
@@ -100,37 +242,66 @@ module XitDB
|
|
|
100
242
|
source.write_to(context, prefix: prefix, format_tag: "rb")
|
|
101
243
|
end
|
|
102
244
|
|
|
245
|
+
# Validates a candidate collection and identifies its storage kind.
|
|
246
|
+
# Scalar values return nil and are handled by the scalar encoder.
|
|
247
|
+
#
|
|
248
|
+
# @param value [Object] candidate input
|
|
249
|
+
# @param path [Array<Object>] logical input location used in errors
|
|
250
|
+
# @return [Symbol, Array(Symbol, Symbol), nil] recognized kind and optional key kind
|
|
251
|
+
# @raise [UnsupportedValueError] if collection behavior cannot be preserved
|
|
103
252
|
def input_kind(value, path:)
|
|
104
|
-
|
|
253
|
+
XitDB::Ruby::Input.check!(value, path: path)
|
|
105
254
|
return nil if @registry.for_value(value)
|
|
106
255
|
|
|
107
256
|
klass = Object.instance_method(:class).bind_call(value)
|
|
108
257
|
case value
|
|
109
258
|
when Hash, ::Array, ::Set
|
|
110
|
-
validate_container(value, klass, path)
|
|
259
|
+
validate_container!(value, klass, path)
|
|
111
260
|
{ Hash => :map, ::Array => :array, ::Set => :set }.fetch(klass)
|
|
112
261
|
when CollectionDeclaration
|
|
113
262
|
declaration_kind(value, klass, path)
|
|
114
263
|
end
|
|
115
264
|
end
|
|
116
265
|
|
|
266
|
+
# Detects Hash settings that this profile cannot preserve.
|
|
267
|
+
#
|
|
268
|
+
# @param value [Hash] input hash
|
|
269
|
+
# @return [Boolean] whether a default value, default procedure, or identity comparison is configured
|
|
117
270
|
def hash_defaults?(value)
|
|
118
|
-
|
|
119
|
-
!value.default_proc.nil? || default || value.compare_by_identity?
|
|
271
|
+
XitDB::Ruby::Input.hash_defaults?(value)
|
|
120
272
|
end
|
|
121
273
|
|
|
122
|
-
|
|
274
|
+
# Rejects collection subclasses and comparison or default behavior that cannot be stored.
|
|
275
|
+
#
|
|
276
|
+
# @param value [Hash, Array, Set] input collection
|
|
277
|
+
# @param klass [Class] exact class obtained without application reflection
|
|
278
|
+
# @param path [Array<Object>] logical input location used in errors
|
|
279
|
+
# @return [void]
|
|
280
|
+
# @raise [UnsupportedValueError] if the collection is a subclass or has unsupported behavior
|
|
281
|
+
def validate_container!(value, klass, path)
|
|
123
282
|
unless [Hash, ::Array, ::Set].include?(klass)
|
|
124
283
|
raise UnsupportedValueError.new("container subclasses are unsupported", path: path)
|
|
125
284
|
end
|
|
126
|
-
if value.is_a?(Hash) && hash_defaults?(value)
|
|
127
|
-
raise UnsupportedValueError.new("Hash defaults and identity comparison are unsupported", path: path)
|
|
128
|
-
end
|
|
129
|
-
return unless value.is_a?(::Set) && value.compare_by_identity?
|
|
130
285
|
|
|
131
|
-
|
|
286
|
+
case value
|
|
287
|
+
when Hash
|
|
288
|
+
if hash_defaults?(value)
|
|
289
|
+
raise UnsupportedValueError.new("Hash defaults and identity comparison are unsupported", path: path)
|
|
290
|
+
end
|
|
291
|
+
when ::Set
|
|
292
|
+
if value.compare_by_identity?
|
|
293
|
+
raise UnsupportedValueError.new("Set identity comparison is unsupported", path: path)
|
|
294
|
+
end
|
|
295
|
+
end
|
|
132
296
|
end
|
|
133
297
|
|
|
298
|
+
# Maps an explicit collection declaration to its Ruby-profile kind.
|
|
299
|
+
#
|
|
300
|
+
# @param value [CollectionDeclaration] requested collection layout
|
|
301
|
+
# @param klass [Class] exact declaration class
|
|
302
|
+
# @param path [Array<Object>] logical location used in errors
|
|
303
|
+
# @return [Symbol, Array(Symbol, Symbol)] kind, with the required key kind for sorted collections
|
|
304
|
+
# @raise [UnsupportedValueError] if the declaration is a subclass, unsupported, or lacks a sorted key kind
|
|
134
305
|
def declaration_kind(value, klass, path)
|
|
135
306
|
unless CollectionDeclaration.equal?(klass)
|
|
136
307
|
raise UnsupportedValueError.new("collection declaration subclasses are unsupported", path: path)
|
|
@@ -150,21 +321,32 @@ module XitDB
|
|
|
150
321
|
end
|
|
151
322
|
end
|
|
152
323
|
|
|
153
|
-
|
|
324
|
+
# Lazily validates keys and produces selectors with their child values.
|
|
325
|
+
# Arrays use integer positions; sets use their members as selectors.
|
|
326
|
+
#
|
|
327
|
+
# @param value [Hash, Array, Set, CollectionDeclaration] input collection
|
|
328
|
+
# @param path [Array<Object>] logical parent location used in key errors
|
|
329
|
+
# @return [Enumerator<Array(Object, Object)>] selector and child-value pairs
|
|
330
|
+
# @raise [ValueError] during enumeration if a key cannot be represented
|
|
331
|
+
def input_entries(value, path:, state: nil)
|
|
154
332
|
Enumerator.new do |output|
|
|
155
333
|
case value
|
|
156
334
|
when Hash, ::Set
|
|
157
|
-
entries =
|
|
158
|
-
|
|
335
|
+
entries = case value
|
|
336
|
+
when Hash then XitDB::Ruby::Input.entries(value)
|
|
337
|
+
else value.each.lazy.map { |member| [member, nil] }
|
|
338
|
+
end
|
|
339
|
+
entries.each do |entry|
|
|
340
|
+
key, child = entry
|
|
159
341
|
begin
|
|
160
|
-
key_bytes(key)
|
|
342
|
+
key_bytes(key, state: state)
|
|
161
343
|
rescue ValueError => e
|
|
162
344
|
raise e.class.new(e.message, path: path + [key]), cause: e
|
|
163
345
|
end
|
|
164
346
|
output << [key, child]
|
|
165
347
|
end
|
|
166
348
|
when ::Array
|
|
167
|
-
value.
|
|
349
|
+
XitDB::Ruby::Input.entries(value).each { |entry| output << entry }
|
|
168
350
|
end
|
|
169
351
|
end
|
|
170
352
|
end
|
data/lib/xitdb/revision.rb
CHANGED
|
@@ -2,7 +2,17 @@
|
|
|
2
2
|
|
|
3
3
|
module XitDB
|
|
4
4
|
# Identifies a history entry and verifies its file generation and trailer.
|
|
5
|
+
#
|
|
6
|
+
# Obtain this opaque token from {Snapshot#revision}. Pass it to
|
|
7
|
+
# {Database#snapshot} to read that state again through a compatible handle.
|
|
8
|
+
# A token is not a portable index for another database or file generation.
|
|
5
9
|
class Revision
|
|
10
|
+
# Record a history position and the proof of its file generation.
|
|
11
|
+
# @api private
|
|
12
|
+
# @param file_id [Integer] file's random identity
|
|
13
|
+
# @param trailer_position [Integer] byte offset of the commit record
|
|
14
|
+
# @param checksum [Integer] integrity proof for that record
|
|
15
|
+
# @param index [Integer] logical history position
|
|
6
16
|
def initialize(file_id:, trailer_position:, checksum:, index:)
|
|
7
17
|
@file_id = file_id
|
|
8
18
|
@trailer_position = trailer_position
|
|
@@ -11,6 +21,11 @@ module XitDB
|
|
|
11
21
|
freeze
|
|
12
22
|
end
|
|
13
23
|
|
|
24
|
+
# Validate the saved proof and read the corresponding root.
|
|
25
|
+
# @api private
|
|
26
|
+
# @param database [Database]
|
|
27
|
+
# @return [Snapshot]
|
|
28
|
+
# @raise [ForeignRevisionError] if the file identity or saved commit proof differs
|
|
14
29
|
def resolve(database)
|
|
15
30
|
core = database.core
|
|
16
31
|
current = core.root_cursor
|
|
@@ -35,6 +50,8 @@ module XitDB
|
|
|
35
50
|
raise ForeignRevisionError, "revision history is no longer valid"
|
|
36
51
|
end
|
|
37
52
|
|
|
53
|
+
# Describe the token without exposing its internal proof fields.
|
|
54
|
+
# @return [String]
|
|
38
55
|
def inspect
|
|
39
56
|
"#<XitDB::Revision>"
|
|
40
57
|
end
|
data/lib/xitdb/ruby/codec.rb
CHANGED
|
@@ -5,10 +5,43 @@ require_relative "scalars"
|
|
|
5
5
|
|
|
6
6
|
module XitDB
|
|
7
7
|
module Ruby
|
|
8
|
-
#
|
|
8
|
+
# Defines serialization for one exact application class in the Ruby profile.
|
|
9
|
+
# The dump callback converts a value to bytes; the load callback reconstructs it from frozen binary bytes.
|
|
10
|
+
# The identifier is stored with the payload. Keep it stable and supply the codec when reopening a database.
|
|
11
|
+
# Without its codec, a stored value is returned as an {EncodedValue} that retains the original payload.
|
|
12
|
+
# Codec instances are frozen; callback objects can still hold application state.
|
|
13
|
+
# Load callbacks must return a fresh object and control any mutable objects referenced inside it.
|
|
14
|
+
#
|
|
15
|
+
# @example Register a simple application value
|
|
16
|
+
# Money = Struct.new(:cents)
|
|
17
|
+
# codec = XitDB::Ruby::Codec.new(
|
|
18
|
+
# id: "example/money/v1", type: Money,
|
|
19
|
+
# dump: ->(money) { money.cents.to_s },
|
|
20
|
+
# load: ->(bytes) { Money.new(Integer(bytes)) }
|
|
21
|
+
# )
|
|
22
|
+
# XitDB::Ruby.memory(codecs: [codec]) do |db|
|
|
23
|
+
# db.transaction { |root| root[:price] = Money.new(125) }
|
|
24
|
+
# db.snapshot[:price].cents # => 125
|
|
25
|
+
# end
|
|
9
26
|
class Codec
|
|
10
27
|
attr_reader :id, :type
|
|
11
28
|
|
|
29
|
+
# @!attribute [r] id
|
|
30
|
+
# Frozen UTF-8 identifier stored in application payloads.
|
|
31
|
+
#
|
|
32
|
+
# @return [String] frozen UTF-8 identifier stored in application payloads
|
|
33
|
+
# @!attribute [r] type
|
|
34
|
+
# Exact application class matched by this codec.
|
|
35
|
+
#
|
|
36
|
+
# @return [Class] exact application class matched by this codec
|
|
37
|
+
|
|
38
|
+
# Captures public callback methods and freezes the codec configuration.
|
|
39
|
+
#
|
|
40
|
+
# @param id [String] nonempty identifier converted to UTF-8
|
|
41
|
+
# @param type [Class] exact application class handled by this codec
|
|
42
|
+
# @param dump [#call] callback accepting a value and returning an exact String
|
|
43
|
+
# @param load [#call] callback accepting frozen binary bytes and returning an application value
|
|
44
|
+
# @raise [CodecConfigurationError] if the identifier, type, or callbacks are invalid
|
|
12
45
|
def initialize(id:, type:, dump:, load:)
|
|
13
46
|
unless Object.instance_method(:instance_of?).bind_call(id, String) &&
|
|
14
47
|
Object.instance_method(:is_a?).bind_call(type, Class)
|
|
@@ -29,6 +62,12 @@ module XitDB
|
|
|
29
62
|
raise CodecConfigurationError, e.message
|
|
30
63
|
end
|
|
31
64
|
|
|
65
|
+
# Calls the serializer and copies its result into a binary string.
|
|
66
|
+
#
|
|
67
|
+
# @param value [Object] application value to serialize
|
|
68
|
+
# @return [String] mutable binary payload copy
|
|
69
|
+
# @raise [CodecError] if the callback fails or returns anything except an exact String
|
|
70
|
+
# @raise [XitDB::Error] if the callback raises a database error
|
|
32
71
|
def dump(value)
|
|
33
72
|
bytes = invoke(@dump, value, :dump)
|
|
34
73
|
case bytes
|
|
@@ -43,12 +82,23 @@ module XitDB
|
|
|
43
82
|
String.new(bytes).b
|
|
44
83
|
end
|
|
45
84
|
|
|
85
|
+
# Calls the deserializer with an isolated, frozen binary copy of the payload.
|
|
86
|
+
#
|
|
87
|
+
# @param bytes [String] serialized application bytes
|
|
88
|
+
# @return [Object] callback result, without a class check
|
|
89
|
+
# @raise [CodecError] if the callback raises a non-database StandardError
|
|
90
|
+
# @raise [XitDB::Error] if the callback raises a database error
|
|
46
91
|
def load(bytes)
|
|
47
92
|
invoke(@load, String.new(bytes).b.freeze, :load)
|
|
48
93
|
end
|
|
49
94
|
|
|
50
95
|
private
|
|
51
96
|
|
|
97
|
+
# Captures the public call method so later method replacement does not change this callback.
|
|
98
|
+
#
|
|
99
|
+
# @param callback [#call] callable object
|
|
100
|
+
# @return [Method] captured public call method
|
|
101
|
+
# @raise [CodecConfigurationError] if a public call method is unavailable
|
|
52
102
|
def capture_callback(callback)
|
|
53
103
|
unless Object.instance_method(:is_a?).bind_call(callback, Object) &&
|
|
54
104
|
Object.instance_method(:public_methods).bind_call(callback).include?(:call)
|
|
@@ -58,6 +108,14 @@ module XitDB
|
|
|
58
108
|
Object.instance_method(:public_method).bind_call(callback, :call)
|
|
59
109
|
end
|
|
60
110
|
|
|
111
|
+
# Invokes a captured callback and adds codec context to application failures.
|
|
112
|
+
#
|
|
113
|
+
# @param callback [Method] captured serializer or deserializer
|
|
114
|
+
# @param value [Object] callback argument
|
|
115
|
+
# @param phase [Symbol] :dump or :load for error messages
|
|
116
|
+
# @return [Object] callback result
|
|
117
|
+
# @raise [CodecError] if a non-database StandardError escapes the callback
|
|
118
|
+
# @raise [XitDB::Error] preserves database errors from the callback
|
|
61
119
|
def invoke(callback, value, phase)
|
|
62
120
|
callback.call(value)
|
|
63
121
|
rescue XitDB::Error
|
|
@@ -66,58 +124,7 @@ module XitDB
|
|
|
66
124
|
raise CodecError, "codec #{id} #{phase} failed: #{e.message}"
|
|
67
125
|
end
|
|
68
126
|
end
|
|
69
|
-
|
|
70
|
-
# Resolves codecs by their exact Ruby class and wire identifier.
|
|
71
|
-
class CodecRegistry
|
|
72
|
-
attr_reader :codecs
|
|
73
|
-
|
|
74
|
-
def initialize(codecs)
|
|
75
|
-
unless Object.instance_method(:instance_of?).bind_call(codecs, ::Array)
|
|
76
|
-
raise CodecConfigurationError, "codecs must be an Array"
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
@codecs = ::Array.instance_method(:map).bind_call(codecs) { |codec| codec }.freeze
|
|
80
|
-
# @type var by_type: Hash[Class, Codec]
|
|
81
|
-
by_type = {}
|
|
82
|
-
@by_type = by_type.compare_by_identity
|
|
83
|
-
# @type ivar @by_id: Hash[String, Codec]
|
|
84
|
-
@by_id = {}
|
|
85
|
-
@codecs.each do |codec|
|
|
86
|
-
validate_registration(codec)
|
|
87
|
-
@by_type[codec.type] = codec
|
|
88
|
-
@by_id[codec.id] = codec
|
|
89
|
-
end
|
|
90
|
-
@by_type.freeze
|
|
91
|
-
@by_id.freeze
|
|
92
|
-
freeze
|
|
93
|
-
rescue UnsupportedValueError => e
|
|
94
|
-
raise CodecConfigurationError, e.message
|
|
95
|
-
end
|
|
96
|
-
|
|
97
|
-
def validate_registration(codec)
|
|
98
|
-
unless Object.instance_method(:instance_of?).bind_call(codec, Codec) &&
|
|
99
|
-
Object.instance_method(:frozen?).bind_call(codec)
|
|
100
|
-
raise CodecConfigurationError, "registrations must be immutable Codec values"
|
|
101
|
-
end
|
|
102
|
-
|
|
103
|
-
InputGuard.check!(codec)
|
|
104
|
-
unless Module.instance_method(:<=).bind_call(codec.type, Object)
|
|
105
|
-
raise CodecConfigurationError, "codec types must inherit Object"
|
|
106
|
-
end
|
|
107
|
-
if Scalars.builtin_type?(codec.type) || [Hash, ::Array, ::Set, CollectionDeclaration].include?(codec.type) ||
|
|
108
|
-
Module.instance_method(:<=).bind_call(codec.type, Views::Base) ||
|
|
109
|
-
@by_type.key?(codec.type) || @by_id.key?(codec.id)
|
|
110
|
-
raise CodecConfigurationError, "duplicate codec or builtin override: #{codec.id}"
|
|
111
|
-
end
|
|
112
|
-
end
|
|
113
|
-
|
|
114
|
-
def for_value(value)
|
|
115
|
-
@by_type[Object.instance_method(:class).bind_call(value)]
|
|
116
|
-
end
|
|
117
|
-
|
|
118
|
-
def for_id(id)
|
|
119
|
-
@by_id[id]
|
|
120
|
-
end
|
|
121
|
-
end
|
|
122
127
|
end
|
|
123
128
|
end
|
|
129
|
+
|
|
130
|
+
require_relative "codec_registry"
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module XitDB
|
|
4
|
+
module Ruby
|
|
5
|
+
# Indexes immutable codec registrations by exact class and stored identifier.
|
|
6
|
+
# Subclasses do not inherit a codec registration. Built-in values and collection classes cannot be overridden.
|
|
7
|
+
class CodecRegistry
|
|
8
|
+
# Frozen copy of the validated registrations.
|
|
9
|
+
#
|
|
10
|
+
# @return [Array<Codec>] frozen copy of the validated registrations
|
|
11
|
+
attr_reader :codecs
|
|
12
|
+
|
|
13
|
+
# Copies registrations and builds frozen lookup tables.
|
|
14
|
+
#
|
|
15
|
+
# @param codecs [Array<Codec>] immutable codec values with distinct identifiers and exact types
|
|
16
|
+
# @raise [CodecConfigurationError] if registrations are invalid, duplicated, or override built-in types
|
|
17
|
+
def initialize(codecs)
|
|
18
|
+
unless Object.instance_method(:instance_of?).bind_call(codecs, ::Array)
|
|
19
|
+
raise CodecConfigurationError, "codecs must be an Array"
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
@codecs = ::Array.instance_method(:map).bind_call(codecs) { |codec| codec }.freeze
|
|
23
|
+
# @type var by_type: Hash[Class, Codec]
|
|
24
|
+
by_type = {}
|
|
25
|
+
@by_type = by_type.compare_by_identity
|
|
26
|
+
# @type ivar @by_id: Hash[String, Codec]
|
|
27
|
+
@by_id = {}
|
|
28
|
+
@codecs.each do |codec|
|
|
29
|
+
validate_registration!(codec)
|
|
30
|
+
@by_type[codec.type] = codec
|
|
31
|
+
@by_id[codec.id] = codec
|
|
32
|
+
end
|
|
33
|
+
@by_type.freeze
|
|
34
|
+
@by_id.freeze
|
|
35
|
+
freeze
|
|
36
|
+
rescue UnsupportedValueError => e
|
|
37
|
+
raise CodecConfigurationError, e.message
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# Checks one registration against built-in types and entries already indexed.
|
|
41
|
+
#
|
|
42
|
+
# @param codec [Codec] frozen registration to validate
|
|
43
|
+
# @return [void]
|
|
44
|
+
# @raise [CodecConfigurationError] if the codec is invalid, duplicated, or overrides a reserved type
|
|
45
|
+
def validate_registration!(codec)
|
|
46
|
+
unless Object.instance_method(:instance_of?).bind_call(codec, Codec) &&
|
|
47
|
+
Object.instance_method(:frozen?).bind_call(codec)
|
|
48
|
+
raise CodecConfigurationError, "registrations must be immutable Codec values"
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
InputGuard.check!(codec)
|
|
52
|
+
unless Module.instance_method(:<=).bind_call(codec.type, Object)
|
|
53
|
+
raise CodecConfigurationError, "codec types must inherit Object"
|
|
54
|
+
end
|
|
55
|
+
if Scalars.builtin_type?(codec.type) || [Hash, ::Array, ::Set, CollectionDeclaration].include?(codec.type) ||
|
|
56
|
+
Module.instance_method(:<=).bind_call(codec.type, Views::Base) ||
|
|
57
|
+
@by_type.key?(codec.type) || @by_id.key?(codec.id)
|
|
58
|
+
raise CodecConfigurationError, "duplicate codec or builtin override: #{codec.id}"
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Finds the codec registered for the exact class of a value.
|
|
63
|
+
#
|
|
64
|
+
# @param value [Object] candidate application value
|
|
65
|
+
# @return [Codec, nil] matching registration, or nil when unregistered
|
|
66
|
+
def for_value(value)
|
|
67
|
+
@by_type[Object.instance_method(:class).bind_call(value)]
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
# Finds the codec named by a stored payload.
|
|
71
|
+
#
|
|
72
|
+
# @param id [String] stored codec identifier
|
|
73
|
+
# @return [Codec, nil] matching registration, or nil when unavailable
|
|
74
|
+
def for_id(id)
|
|
75
|
+
@by_id[id]
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|