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/ruby/scalars.rb
CHANGED
|
@@ -3,55 +3,75 @@
|
|
|
3
3
|
require "date"
|
|
4
4
|
require "bigdecimal"
|
|
5
5
|
require_relative "../core"
|
|
6
|
-
require_relative "
|
|
6
|
+
require_relative "input"
|
|
7
7
|
require_relative "wire"
|
|
8
8
|
require_relative "key_encoding"
|
|
9
9
|
|
|
10
|
+
require_relative "encoded_value"
|
|
11
|
+
|
|
10
12
|
module XitDB
|
|
11
13
|
module Ruby
|
|
12
|
-
#
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
value = Core::ByteValue.new(bytes, format_tag: format_tag)
|
|
18
|
-
@codec_id = codec_id.dup.freeze
|
|
19
|
-
@format_tag = value.format_tag
|
|
20
|
-
@bytes = value.bytes
|
|
21
|
-
freeze
|
|
22
|
-
end
|
|
23
|
-
end
|
|
24
|
-
|
|
25
|
-
# Serializes builtin Ruby scalar values and registered codec payloads.
|
|
14
|
+
# Converts individual Ruby values to slots and reconstructs them from storage.
|
|
15
|
+
# A scalar is one value, such as a String or Time, rather than a map, set, or array.
|
|
16
|
+
# Nil, machine-sized integers, and Float values use native slots.
|
|
17
|
+
# Other Ruby values use versioned bytes tagged "rb", which preserve types and string encodings.
|
|
18
|
+
# Unregistered custom classes are rejected; missing codecs return {EncodedValue} objects.
|
|
26
19
|
module Scalars
|
|
20
|
+
# Exact scalar classes handled without an application codec.
|
|
27
21
|
TYPES = [NilClass, FalseClass, TrueClass, Integer, Float, String, Symbol, Time, Date, DateTime,
|
|
28
22
|
BigDecimal, Rational, EncodedValue, Core::ByteValue].freeze
|
|
29
23
|
private_constant :TYPES
|
|
30
24
|
|
|
25
|
+
# Stores one scalar, using a registered codec before checking built-in types.
|
|
26
|
+
#
|
|
27
|
+
# @param value [Object] supported built-in scalar or registered application value
|
|
28
|
+
# @param core [Core::Database] destination storage
|
|
29
|
+
# @param registry [CodecRegistry, nil] custom serializers available for this write
|
|
30
|
+
# @return [Core::Slot] stored value descriptor
|
|
31
|
+
# @raise [UnsupportedValueError] if the exact class or an extended scalar's behavior is unsupported
|
|
32
|
+
# @raise [CodecError] if application serialization fails
|
|
31
33
|
def self.encode(value, core:, registry: nil)
|
|
32
|
-
|
|
34
|
+
Input.check!(value)
|
|
35
|
+
value = Input.string(value) if Object.instance_method(:instance_of?).bind_call(value, String)
|
|
36
|
+
encode_validated(value, core: core, registry: registry)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Stores an input already checked by the owning profile traversal.
|
|
40
|
+
# @api private
|
|
41
|
+
def self.encode_validated(value, core:, registry: nil)
|
|
33
42
|
if (codec = registry&.for_value(value))
|
|
34
|
-
payload =
|
|
35
|
-
|
|
43
|
+
payload = codec.dump(value)
|
|
44
|
+
prefix = "\x01\x0a".b + Wire.t(codec.id) + Wire.u(payload.bytesize)
|
|
45
|
+
return core.bytes(payload, prefix: prefix, format_tag: "rb")
|
|
36
46
|
end
|
|
37
47
|
unless builtin_type?(Object.instance_method(:class).bind_call(value))
|
|
38
48
|
raise UnsupportedValueError, "unsupported Ruby scalar class"
|
|
39
49
|
end
|
|
40
50
|
|
|
41
|
-
value = checked_builtin(value)
|
|
51
|
+
value = checked_builtin!(value)
|
|
42
52
|
builtin_slot(value, core)
|
|
43
53
|
end
|
|
44
54
|
|
|
45
|
-
|
|
55
|
+
# Narrows a validated value to the supported built-in scalar union.
|
|
56
|
+
#
|
|
57
|
+
# @param value [Object] candidate built-in scalar
|
|
58
|
+
# @return [Object] the supported value
|
|
59
|
+
# @raise [UnsupportedValueError] if the value cannot be treated as a built-in scalar
|
|
60
|
+
def self.checked_builtin!(value)
|
|
46
61
|
case value
|
|
47
62
|
when Object
|
|
48
|
-
checked_object_builtin(value)
|
|
63
|
+
checked_object_builtin!(value)
|
|
49
64
|
else
|
|
50
65
|
raise UnsupportedValueError, "unsupported Ruby scalar class"
|
|
51
66
|
end
|
|
52
67
|
end
|
|
53
68
|
|
|
54
|
-
|
|
69
|
+
# Recognizes extended built-ins before checking basic key-compatible scalars.
|
|
70
|
+
#
|
|
71
|
+
# @param value [Object] candidate built-in scalar
|
|
72
|
+
# @return [Object] the supported value
|
|
73
|
+
# @raise [UnsupportedValueError] if no supported scalar type matches
|
|
74
|
+
def self.checked_object_builtin!(value)
|
|
55
75
|
return value if value.is_a?(DateTime)
|
|
56
76
|
return value if value.is_a?(Date)
|
|
57
77
|
return value if value.is_a?(BigDecimal)
|
|
@@ -60,10 +80,16 @@ module XitDB
|
|
|
60
80
|
when Time, Rational, EncodedValue, Core::ByteValue
|
|
61
81
|
value
|
|
62
82
|
else
|
|
63
|
-
KeyEncoding.checked_key(value)
|
|
83
|
+
KeyEncoding.checked_key!(value)
|
|
64
84
|
end
|
|
65
85
|
end
|
|
66
86
|
|
|
87
|
+
# Uses a native slot when possible and tagged bytes for other built-ins.
|
|
88
|
+
# Integers outside signed or unsigned 64-bit ranges use an arbitrary-precision payload.
|
|
89
|
+
#
|
|
90
|
+
# @param value [Object] validated built-in scalar
|
|
91
|
+
# @param core [Core::Database] destination storage
|
|
92
|
+
# @return [Core::Slot] native scalar or stored byte descriptor
|
|
67
93
|
def self.builtin_slot(value, core)
|
|
68
94
|
case value
|
|
69
95
|
when nil then Core::Slot.null
|
|
@@ -73,17 +99,26 @@ module XitDB
|
|
|
73
99
|
|
|
74
100
|
core.bytes("\x01\x04".b + Wire.z(value), format_tag: "rb")
|
|
75
101
|
when Float then Core::Slot.float(value)
|
|
102
|
+
when String then text_slot(value, 1, core)
|
|
103
|
+
when Symbol then text_slot(value.to_s, 2, core)
|
|
76
104
|
when EncodedValue, Core::ByteValue then core.bytes(value.bytes, format_tag: value.format_tag)
|
|
77
105
|
else core.bytes("\x01".b + builtin_payload(value), format_tag: "rb")
|
|
78
106
|
end
|
|
79
107
|
end
|
|
80
108
|
|
|
109
|
+
# Appends text fields in bounded chunks without joining the original bytes.
|
|
110
|
+
def self.text_slot(value, type, core)
|
|
111
|
+
prefix = [1, type].pack("CC") + Wire.t(value.encoding.name) + Wire.u(value.bytesize)
|
|
112
|
+
core.bytes(value, prefix: prefix, format_tag: "rb")
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Encodes a built-in type marker and its fields, without the version byte.
|
|
116
|
+
#
|
|
117
|
+
# @param value [Object] scalar requiring a tagged payload
|
|
118
|
+
# @return [String] binary type marker and fields
|
|
119
|
+
# @raise [UnsupportedValueError] if the value has no supported payload type
|
|
81
120
|
def self.builtin_payload(value)
|
|
82
121
|
case value
|
|
83
|
-
when String then "\x01".b + Wire.t(value.encoding.name) + Wire.b(value)
|
|
84
|
-
when Symbol
|
|
85
|
-
name = value.to_s
|
|
86
|
-
"\x02".b + Wire.t(name.encoding.name) + Wire.b(name)
|
|
87
122
|
when false then "\x03\x00".b
|
|
88
123
|
when true then "\x03\x01".b
|
|
89
124
|
when Time then time_bytes(value)
|
|
@@ -94,6 +129,11 @@ module XitDB
|
|
|
94
129
|
end
|
|
95
130
|
end
|
|
96
131
|
|
|
132
|
+
# Adds the decimal type marker to a BigDecimal field.
|
|
133
|
+
#
|
|
134
|
+
# @param value [BigDecimal] decimal value
|
|
135
|
+
# @return [String] decimal type marker and payload
|
|
136
|
+
# @raise [UnsupportedValueError] if the value is not a BigDecimal
|
|
97
137
|
def self.decimal_payload(value)
|
|
98
138
|
unless value.is_a?(BigDecimal)
|
|
99
139
|
raise UnsupportedValueError, "unsupported Ruby scalar: #{Object.instance_method(:class).bind_call(value)}"
|
|
@@ -102,6 +142,11 @@ module XitDB
|
|
|
102
142
|
"\x08".b + decimal_bytes(value)
|
|
103
143
|
end
|
|
104
144
|
|
|
145
|
+
# Stores an exact Time instant, its offset, and whether it uses UTC.
|
|
146
|
+
#
|
|
147
|
+
# @param value [Time] time to preserve
|
|
148
|
+
# @return [String] time type marker and fields
|
|
149
|
+
# @raise [UnsupportedValueError] if the UTC offset contains fractional seconds
|
|
105
150
|
def self.time_bytes(value)
|
|
106
151
|
offset = value.utc_offset
|
|
107
152
|
raise UnsupportedValueError, "Ruby profile Time offsets require whole seconds" unless offset == offset.to_i
|
|
@@ -109,10 +154,26 @@ module XitDB
|
|
|
109
154
|
"\x05".b + Wire.q(value.to_r) + Wire.z(offset.to_i) + (value.utc? ? "\x01".b : "\x00".b)
|
|
110
155
|
end
|
|
111
156
|
|
|
157
|
+
# Checks exact class membership in the built-in scalar list.
|
|
158
|
+
#
|
|
159
|
+
# @param type [Class] class to test
|
|
160
|
+
# @return [Boolean] whether the class is supported without a codec
|
|
112
161
|
def self.builtin_type?(type)
|
|
113
162
|
TYPES.include?(type)
|
|
114
163
|
end
|
|
115
164
|
|
|
165
|
+
# Reads one scalar and optionally freezes the returned object.
|
|
166
|
+
# Untagged bytes become binary Strings; foreign format tags remain Core::ByteValue objects.
|
|
167
|
+
#
|
|
168
|
+
# @param slot [Core::Slot] stored scalar descriptor
|
|
169
|
+
# @param core [Core::Database] source storage
|
|
170
|
+
# @param bound [Integer, nil] exclusive read limit, or the core default
|
|
171
|
+
# @param registry [CodecRegistry, nil] available application deserializers
|
|
172
|
+
# @param immutable [Boolean] whether to freeze the decoded object, including a codec result
|
|
173
|
+
# @return [Object] built-in scalar, decoded application object, or EncodedValue
|
|
174
|
+
# @raise [InvalidValueError] if the slot or payload is malformed
|
|
175
|
+
# @raise [UnsupportedEncodingError] if its version, type, or encoding is unsupported
|
|
176
|
+
# @raise [CodecError] if application deserialization fails
|
|
116
177
|
def self.decode(slot, core:, bound: nil, registry: nil, immutable: true)
|
|
117
178
|
value = case slot.tag
|
|
118
179
|
when :none then nil
|
|
@@ -132,6 +193,13 @@ module XitDB
|
|
|
132
193
|
immutable ? Object.instance_method(:freeze).bind_call(value) : value
|
|
133
194
|
end
|
|
134
195
|
|
|
196
|
+
# Checks the Ruby payload version, decodes its type, and rejects extra bytes.
|
|
197
|
+
#
|
|
198
|
+
# @param bytes [String] complete Ruby scalar payload
|
|
199
|
+
# @param registry [CodecRegistry, nil] available application deserializers
|
|
200
|
+
# @return [Object] decoded scalar
|
|
201
|
+
# @raise [UnsupportedEncodingError] if the version or type is unsupported
|
|
202
|
+
# @raise [InvalidValueError] if fields are malformed, truncated, or followed by extra bytes
|
|
135
203
|
def self.decode_payload(bytes, registry: nil)
|
|
136
204
|
reader = Wire::Reader.new(bytes)
|
|
137
205
|
version = reader.byte
|
|
@@ -144,12 +212,19 @@ module XitDB
|
|
|
144
212
|
raise InvalidValueError, e.message
|
|
145
213
|
end
|
|
146
214
|
|
|
215
|
+
# Consumes the type marker and dispatches to the corresponding scalar reader.
|
|
216
|
+
#
|
|
217
|
+
# @param reader [Wire::Reader] reader positioned after the version byte
|
|
218
|
+
# @param bytes [String] complete payload retained for missing codecs
|
|
219
|
+
# @param registry [CodecRegistry, nil] application deserializers
|
|
220
|
+
# @return [Object] decoded scalar
|
|
221
|
+
# @raise [UnsupportedEncodingError] if the type marker is unsupported
|
|
147
222
|
def self.decode_type(reader, bytes, registry)
|
|
148
223
|
type = reader.byte
|
|
149
224
|
case type
|
|
150
225
|
when 1 then read_string(reader)
|
|
151
226
|
when 2 then read_symbol(reader)
|
|
152
|
-
when 3 then read_boolean
|
|
227
|
+
when 3 then read_boolean(reader)
|
|
153
228
|
when 4 then reader.z
|
|
154
229
|
when 5 then read_time(reader)
|
|
155
230
|
when 6 then Date.jd(reader.z, read_start(reader))
|
|
@@ -159,6 +234,14 @@ module XitDB
|
|
|
159
234
|
end
|
|
160
235
|
end
|
|
161
236
|
|
|
237
|
+
# Decodes Rational and codec fields after the primary built-in type dispatch.
|
|
238
|
+
#
|
|
239
|
+
# @param type [Integer] scalar type marker
|
|
240
|
+
# @param reader [Wire::Reader] reader positioned after the type byte
|
|
241
|
+
# @param bytes [String] complete payload retained for missing codecs
|
|
242
|
+
# @param registry [CodecRegistry, nil] application deserializers
|
|
243
|
+
# @return [Object] Rational, application value, or EncodedValue
|
|
244
|
+
# @raise [UnsupportedEncodingError] if the type marker is unsupported
|
|
162
245
|
def self.decode_extended_type(type, reader, bytes, registry)
|
|
163
246
|
case type
|
|
164
247
|
when 9 then reader.q
|
|
@@ -167,6 +250,11 @@ module XitDB
|
|
|
167
250
|
end
|
|
168
251
|
end
|
|
169
252
|
|
|
253
|
+
# Reads an encoded name and interns it as a Ruby Symbol.
|
|
254
|
+
#
|
|
255
|
+
# @param reader [Wire::Reader] reader at the encoding-name field
|
|
256
|
+
# @return [Symbol] decoded name
|
|
257
|
+
# @raise [InvalidValueError] if the name has invalid bytes for its encoding
|
|
170
258
|
def self.read_symbol(reader)
|
|
171
259
|
name = read_string(reader)
|
|
172
260
|
raise InvalidValueError, "invalid Symbol bytes" unless name.valid_encoding?
|
|
@@ -176,13 +264,27 @@ module XitDB
|
|
|
176
264
|
raise InvalidValueError, e.message
|
|
177
265
|
end
|
|
178
266
|
|
|
179
|
-
|
|
267
|
+
# Reads the zero or one Boolean flag.
|
|
268
|
+
#
|
|
269
|
+
# @param reader [Wire::Reader] reader at the flag
|
|
270
|
+
# @return [Boolean] false for zero, true for one
|
|
271
|
+
# @raise [InvalidValueError] if the flag exceeds one
|
|
272
|
+
def self.read_boolean(reader)
|
|
180
273
|
flag = reader.byte
|
|
181
274
|
raise InvalidValueError, "invalid Boolean flag" unless flag <= 1
|
|
182
275
|
|
|
183
276
|
flag == 1
|
|
184
277
|
end
|
|
185
278
|
|
|
279
|
+
# Loads an application payload or retains the complete bytes when its codec is absent.
|
|
280
|
+
# Checks the end of the payload before executing application code.
|
|
281
|
+
#
|
|
282
|
+
# @param reader [Wire::Reader] reader at the codec identifier
|
|
283
|
+
# @param bytes [String] complete original scalar payload
|
|
284
|
+
# @param registry [CodecRegistry, nil] available application deserializers
|
|
285
|
+
# @return [Object, EncodedValue] loaded application value or preserved encoded value
|
|
286
|
+
# @raise [InvalidValueError] if the fields are malformed or extra bytes remain
|
|
287
|
+
# @raise [CodecError] if the load callback fails
|
|
186
288
|
def self.read_codec(reader, bytes, registry)
|
|
187
289
|
codec_id = reader.t
|
|
188
290
|
payload = reader.b
|
|
@@ -191,6 +293,11 @@ module XitDB
|
|
|
191
293
|
codec ? codec.load(payload) : EncodedValue.new(codec_id: codec_id, bytes: bytes)
|
|
192
294
|
end
|
|
193
295
|
|
|
296
|
+
# Encodes finite decimal fields or a marker for infinity or NaN.
|
|
297
|
+
#
|
|
298
|
+
# @param value [BigDecimal] decimal to encode
|
|
299
|
+
# @return [String] binary decimal fields
|
|
300
|
+
# @raise [InvalidValueError] if the decimal reports a base other than ten
|
|
194
301
|
def self.decimal_bytes(value)
|
|
195
302
|
return "\x03".b if value.nan?
|
|
196
303
|
return value.positive? ? "\x01".b : "\x02".b if value.infinite?
|
|
@@ -206,6 +313,11 @@ module XitDB
|
|
|
206
313
|
"\x00".b + (sign.negative? ? "\x01".b : "\x00".b) + Wire.t(digits) + Wire.z(exponent)
|
|
207
314
|
end
|
|
208
315
|
|
|
316
|
+
# Reconstructs an exact Time instant and restores its fixed offset or UTC state.
|
|
317
|
+
#
|
|
318
|
+
# @param reader [Wire::Reader] reader at the instant field
|
|
319
|
+
# @return [Time] reconstructed time
|
|
320
|
+
# @raise [InvalidValueError] if the offset or UTC flag is inconsistent
|
|
209
321
|
def self.read_time(reader)
|
|
210
322
|
instant = reader.q
|
|
211
323
|
offset = reader.z
|
|
@@ -218,6 +330,11 @@ module XitDB
|
|
|
218
330
|
utc == 1 ? time.utc : time.getlocal(offset)
|
|
219
331
|
end
|
|
220
332
|
|
|
333
|
+
# Reads and validates a Date calendar-reform boundary.
|
|
334
|
+
#
|
|
335
|
+
# @param reader [Wire::Reader] reader at the calendar-start field
|
|
336
|
+
# @return [Float] infinite boundary or supported Julian reform day
|
|
337
|
+
# @raise [InvalidValueError] if a finite boundary is outside the supported range
|
|
221
338
|
def self.read_start(reader)
|
|
222
339
|
start = reader.f64
|
|
223
340
|
raise InvalidValueError, "invalid calendar start" unless start.infinite? || (2_298_874..2_426_355).cover?(start)
|
|
@@ -225,6 +342,11 @@ module XitDB
|
|
|
225
342
|
start
|
|
226
343
|
end
|
|
227
344
|
|
|
345
|
+
# Reconstructs DateTime from its exact astronomical day, offset, and calendar start.
|
|
346
|
+
#
|
|
347
|
+
# @param reader [Wire::Reader] reader at the instant field
|
|
348
|
+
# @return [DateTime] reconstructed date and time
|
|
349
|
+
# @raise [InvalidValueError] if the offset exceeds one day or contains fractional seconds
|
|
228
350
|
def self.read_datetime(reader)
|
|
229
351
|
instant = reader.q
|
|
230
352
|
offset = reader.q
|
|
@@ -237,6 +359,11 @@ module XitDB
|
|
|
237
359
|
date + fraction
|
|
238
360
|
end
|
|
239
361
|
|
|
362
|
+
# Selects a finite decimal reader or constructs the encoded special value.
|
|
363
|
+
#
|
|
364
|
+
# @param reader [Wire::Reader] reader at the decimal-kind byte
|
|
365
|
+
# @return [BigDecimal] finite number, infinity, or NaN
|
|
366
|
+
# @raise [InvalidValueError] if the decimal-kind byte is unknown
|
|
240
367
|
def self.read_decimal(reader)
|
|
241
368
|
case reader.byte
|
|
242
369
|
when 0 then read_finite_decimal(reader)
|
|
@@ -247,6 +374,11 @@ module XitDB
|
|
|
247
374
|
end
|
|
248
375
|
end
|
|
249
376
|
|
|
377
|
+
# Checks canonical digits and verifies that Ruby can represent the stored decimal exponent.
|
|
378
|
+
#
|
|
379
|
+
# @param reader [Wire::Reader] reader at the sign field
|
|
380
|
+
# @return [BigDecimal] reconstructed finite decimal
|
|
381
|
+
# @raise [InvalidValueError] if digits, sign, exponent, or the reconstructed fields are invalid
|
|
250
382
|
def self.read_finite_decimal(reader)
|
|
251
383
|
sign = reader.byte
|
|
252
384
|
digits = reader.t
|
|
@@ -266,6 +398,11 @@ module XitDB
|
|
|
266
398
|
value
|
|
267
399
|
end
|
|
268
400
|
|
|
401
|
+
# Constructs a decimal while temporarily disabling BigDecimal exception flags.
|
|
402
|
+
# The previous exception mode is restored after construction.
|
|
403
|
+
#
|
|
404
|
+
# @param text [String] BigDecimal numeric representation
|
|
405
|
+
# @return [BigDecimal] constructed decimal
|
|
269
406
|
def self.build_decimal(text)
|
|
270
407
|
BigDecimal.save_exception_mode do
|
|
271
408
|
BigDecimal.mode(BigDecimal::EXCEPTION_ALL, false)
|
|
@@ -273,6 +410,11 @@ module XitDB
|
|
|
273
410
|
end
|
|
274
411
|
end
|
|
275
412
|
|
|
413
|
+
# Restores the named encoding without transcoding or validating the original string bytes.
|
|
414
|
+
#
|
|
415
|
+
# @param reader [Wire::Reader] reader at the UTF-8 encoding name
|
|
416
|
+
# @return [String] bytes labelled with the stored Ruby encoding
|
|
417
|
+
# @raise [UnsupportedEncodingError] if Ruby does not recognize the encoding name
|
|
276
418
|
def self.read_string(reader)
|
|
277
419
|
name = reader.t
|
|
278
420
|
bytes = reader.b
|
|
@@ -287,10 +429,10 @@ module XitDB
|
|
|
287
429
|
end
|
|
288
430
|
private_class_method :decode_payload, :read_string, :decimal_bytes, :read_time, :read_start,
|
|
289
431
|
:read_datetime, :read_decimal, :builtin_slot, :builtin_payload, :time_bytes,
|
|
290
|
-
:checked_builtin
|
|
291
|
-
:checked_object_builtin
|
|
292
|
-
:read_boolean
|
|
293
|
-
:read_codec, :read_finite_decimal, :build_decimal
|
|
432
|
+
:checked_builtin!, :decimal_payload,
|
|
433
|
+
:checked_object_builtin!, :decode_type, :decode_extended_type, :read_symbol,
|
|
434
|
+
:read_boolean,
|
|
435
|
+
:read_codec, :read_finite_decimal, :build_decimal, :text_slot
|
|
294
436
|
end
|
|
295
437
|
end
|
|
296
438
|
end
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module XitDB
|
|
4
|
+
module Ruby
|
|
5
|
+
module Wire
|
|
6
|
+
# Reads fields sequentially from one binary payload.
|
|
7
|
+
# Each read advances the position. Call {#finish!} after the last field to reject extra bytes.
|
|
8
|
+
# Integer and rational readers reject alternate, nonminimal representations of the same value.
|
|
9
|
+
class Reader
|
|
10
|
+
# Starts reading at the first byte of a payload.
|
|
11
|
+
#
|
|
12
|
+
# @param bytes [String] complete binary payload
|
|
13
|
+
def initialize(bytes)
|
|
14
|
+
@bytes = bytes.b
|
|
15
|
+
@position = 0
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# Reads one unsigned byte.
|
|
19
|
+
#
|
|
20
|
+
# @return [Integer] byte between 0 and 255
|
|
21
|
+
# @raise [InvalidValueError] if the payload ends before the byte
|
|
22
|
+
def byte
|
|
23
|
+
value = take(1).getbyte(0)
|
|
24
|
+
raise InvalidValueError, "truncated payload field" unless value
|
|
25
|
+
|
|
26
|
+
value
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Reads a minimal variable-length unsigned integer.
|
|
30
|
+
#
|
|
31
|
+
# @return [Integer] nonnegative number
|
|
32
|
+
# @raise [InvalidValueError] if the field is truncated or has redundant leading groups
|
|
33
|
+
def u
|
|
34
|
+
value = 0
|
|
35
|
+
shift = 0
|
|
36
|
+
loop do
|
|
37
|
+
part = byte
|
|
38
|
+
value |= (part & 0x7f) << shift
|
|
39
|
+
if part < 0x80
|
|
40
|
+
raise InvalidValueError, "nonminimal unsigned field" if shift.positive? && part.zero?
|
|
41
|
+
|
|
42
|
+
return value
|
|
43
|
+
end
|
|
44
|
+
shift += 7
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# Reads a byte count followed by exactly that many bytes.
|
|
49
|
+
#
|
|
50
|
+
# @return [String] binary data without its length prefix
|
|
51
|
+
# @raise [InvalidValueError] if the length or data is truncated
|
|
52
|
+
def b
|
|
53
|
+
take(u)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Reads a length-prefixed UTF-8 text field.
|
|
57
|
+
#
|
|
58
|
+
# @return [String] valid UTF-8 text
|
|
59
|
+
# @raise [InvalidValueError] if the field is truncated or contains invalid UTF-8
|
|
60
|
+
def t
|
|
61
|
+
text = b.force_encoding(Encoding::UTF_8)
|
|
62
|
+
raise InvalidValueError, "invalid UTF-8 text field" unless text.valid_encoding?
|
|
63
|
+
|
|
64
|
+
text
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
# Reads a signed integer and checks its sign and magnitude.
|
|
68
|
+
#
|
|
69
|
+
# @return [Integer] signed number
|
|
70
|
+
# @raise [InvalidValueError] if the sign is invalid, bytes are truncated, or the magnitude is nonminimal
|
|
71
|
+
def z
|
|
72
|
+
sign = byte
|
|
73
|
+
raise InvalidValueError, "invalid integer sign" unless sign <= 1
|
|
74
|
+
|
|
75
|
+
magnitude = b
|
|
76
|
+
if magnitude.getbyte(0)&.zero? || (sign == 1 && magnitude.empty?)
|
|
77
|
+
raise InvalidValueError, "nonminimal integer magnitude"
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
value = magnitude.empty? ? 0 : magnitude.unpack1("H*").to_s.to_i(16)
|
|
81
|
+
sign.zero? ? value : -value
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Reads a rational with a positive, reduced denominator.
|
|
85
|
+
#
|
|
86
|
+
# @return [Rational] exact fraction
|
|
87
|
+
# @raise [InvalidValueError] if fields are truncated or the fraction is not reduced with a positive denominator
|
|
88
|
+
def q
|
|
89
|
+
numerator = z
|
|
90
|
+
denominator = u
|
|
91
|
+
unless denominator.positive? && numerator.gcd(denominator) == 1
|
|
92
|
+
raise InvalidValueError, "rational must have a positive reduced denominator"
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
Rational(numerator, denominator)
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# Reads eight IEEE 754 bytes in network byte order.
|
|
99
|
+
#
|
|
100
|
+
# @return [Float] decoded floating-point value
|
|
101
|
+
# @raise [InvalidValueError] if eight bytes are unavailable
|
|
102
|
+
def f64
|
|
103
|
+
value = take(8).unpack1("G")
|
|
104
|
+
raise InvalidValueError, "invalid float payload" unless value.is_a?(Float)
|
|
105
|
+
|
|
106
|
+
value
|
|
107
|
+
end
|
|
108
|
+
|
|
109
|
+
# Checks that the complete payload has been consumed.
|
|
110
|
+
#
|
|
111
|
+
# @return [self] this reader
|
|
112
|
+
# @raise [InvalidValueError] if unread bytes remain
|
|
113
|
+
def finish!
|
|
114
|
+
raise InvalidValueError, "trailing payload bytes" unless @position == @bytes.bytesize
|
|
115
|
+
|
|
116
|
+
self
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
private
|
|
120
|
+
|
|
121
|
+
# Consumes a fixed number of bytes and advances the read position.
|
|
122
|
+
#
|
|
123
|
+
# @param size [Integer] nonnegative byte count
|
|
124
|
+
# @return [String] requested binary bytes
|
|
125
|
+
# @raise [InvalidValueError] if the requested bytes extend past the payload
|
|
126
|
+
def take(size)
|
|
127
|
+
raise InvalidValueError, "truncated payload field" if size > @bytes.bytesize - @position
|
|
128
|
+
|
|
129
|
+
result = @bytes.byteslice(@position, size)
|
|
130
|
+
raise InvalidValueError, "truncated payload field" unless result
|
|
131
|
+
|
|
132
|
+
@position += size
|
|
133
|
+
result
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
end
|
|
137
|
+
end
|
|
138
|
+
end
|
data/lib/xitdb/ruby/wire.rb
CHANGED
|
@@ -4,8 +4,17 @@ require_relative "../errors"
|
|
|
4
4
|
|
|
5
5
|
module XitDB
|
|
6
6
|
module Ruby
|
|
7
|
-
# Encodes
|
|
7
|
+
# Encodes the fields inside Ruby-profile scalar payloads.
|
|
8
|
+
# Each method returns binary bytes, which callers concatenate into a payload.
|
|
9
|
+
# Unsigned integers use seven data bits per byte, with the high bit marking another byte.
|
|
10
|
+
# Byte strings carry a byte count; signed integers carry a sign and a big-endian magnitude.
|
|
11
|
+
# These fields describe storage and do not define sorted-key order.
|
|
8
12
|
module Wire
|
|
13
|
+
# Encodes a nonnegative integer as a minimal variable-length unsigned field.
|
|
14
|
+
#
|
|
15
|
+
# @param value [Integer] nonnegative number of any size
|
|
16
|
+
# @return [String] binary encoded field
|
|
17
|
+
# @raise [InvalidValueError] if the value is not a nonnegative Integer
|
|
9
18
|
def self.u(value)
|
|
10
19
|
unless value.is_a?(Integer) && value >= 0
|
|
11
20
|
raise InvalidValueError, "unsigned field requires a nonnegative Integer"
|
|
@@ -21,10 +30,19 @@ module XitDB
|
|
|
21
30
|
bytes
|
|
22
31
|
end
|
|
23
32
|
|
|
33
|
+
# Prefixes raw bytes with their unsigned byte count.
|
|
34
|
+
#
|
|
35
|
+
# @param bytes [String] bytes preserved without transcoding
|
|
36
|
+
# @return [String] binary length and data
|
|
24
37
|
def self.b(bytes)
|
|
25
38
|
u(bytes.bytesize) + bytes.b
|
|
26
39
|
end
|
|
27
40
|
|
|
41
|
+
# Converts text to UTF-8 and prefixes it with its byte count.
|
|
42
|
+
#
|
|
43
|
+
# @param text [String] text to transcode
|
|
44
|
+
# @return [String] length-prefixed UTF-8 bytes
|
|
45
|
+
# @raise [InvalidValueError] if conversion fails or UTF-8 bytes are invalid
|
|
28
46
|
def self.t(text)
|
|
29
47
|
utf8 = text.encode(Encoding::UTF_8)
|
|
30
48
|
raise InvalidValueError, "text field requires valid UTF-8" unless utf8.valid_encoding?
|
|
@@ -34,6 +52,12 @@ module XitDB
|
|
|
34
52
|
raise InvalidValueError, e.message
|
|
35
53
|
end
|
|
36
54
|
|
|
55
|
+
# Encodes an integer using a sign byte and a length-prefixed magnitude.
|
|
56
|
+
# Zero has a positive sign and an empty magnitude.
|
|
57
|
+
#
|
|
58
|
+
# @param value [Integer] signed number of any size
|
|
59
|
+
# @return [String] binary signed field
|
|
60
|
+
# @raise [InvalidValueError] if the value is not an Integer
|
|
37
61
|
def self.z(value)
|
|
38
62
|
raise InvalidValueError, "signed field requires an Integer" unless value.is_a?(Integer)
|
|
39
63
|
|
|
@@ -43,104 +67,26 @@ module XitDB
|
|
|
43
67
|
(value.negative? ? "\x01".b : "\x00".b) + b(magnitude)
|
|
44
68
|
end
|
|
45
69
|
|
|
70
|
+
# Encodes a Rational as its signed numerator and unsigned denominator.
|
|
71
|
+
#
|
|
72
|
+
# @param value [Rational] reduced fraction
|
|
73
|
+
# @return [String] binary rational field
|
|
74
|
+
# @raise [InvalidValueError] if the value is not a Rational
|
|
46
75
|
def self.q(value)
|
|
47
76
|
raise InvalidValueError, "rational field requires a Rational" unless value.is_a?(Rational)
|
|
48
77
|
|
|
49
78
|
z(value.numerator) + u(value.denominator)
|
|
50
79
|
end
|
|
51
80
|
|
|
81
|
+
# Encodes a float as eight IEEE 754 bytes in network byte order.
|
|
82
|
+
#
|
|
83
|
+
# @param value [Float] floating-point value
|
|
84
|
+
# @return [String] eight binary bytes
|
|
52
85
|
def self.f64(value)
|
|
53
86
|
[value].pack("G")
|
|
54
87
|
end
|
|
55
|
-
|
|
56
|
-
# Reads canonical fields and rejects truncated or trailing bytes.
|
|
57
|
-
class Reader
|
|
58
|
-
def initialize(bytes)
|
|
59
|
-
@bytes = bytes.b
|
|
60
|
-
@position = 0
|
|
61
|
-
end
|
|
62
|
-
|
|
63
|
-
def byte
|
|
64
|
-
value = take(1).getbyte(0)
|
|
65
|
-
raise InvalidValueError, "truncated payload field" unless value
|
|
66
|
-
|
|
67
|
-
value
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
def u
|
|
71
|
-
value = 0
|
|
72
|
-
shift = 0
|
|
73
|
-
loop do
|
|
74
|
-
part = byte
|
|
75
|
-
value |= (part & 0x7f) << shift
|
|
76
|
-
if part < 0x80
|
|
77
|
-
raise InvalidValueError, "nonminimal unsigned field" if shift.positive? && part.zero?
|
|
78
|
-
|
|
79
|
-
return value
|
|
80
|
-
end
|
|
81
|
-
shift += 7
|
|
82
|
-
end
|
|
83
|
-
end
|
|
84
|
-
|
|
85
|
-
def b
|
|
86
|
-
take(u)
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
def t
|
|
90
|
-
text = b.force_encoding(Encoding::UTF_8)
|
|
91
|
-
raise InvalidValueError, "invalid UTF-8 text field" unless text.valid_encoding?
|
|
92
|
-
|
|
93
|
-
text
|
|
94
|
-
end
|
|
95
|
-
|
|
96
|
-
def z
|
|
97
|
-
sign = byte
|
|
98
|
-
raise InvalidValueError, "invalid integer sign" unless sign <= 1
|
|
99
|
-
|
|
100
|
-
magnitude = b
|
|
101
|
-
if magnitude.getbyte(0)&.zero? || (sign == 1 && magnitude.empty?)
|
|
102
|
-
raise InvalidValueError, "nonminimal integer magnitude"
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
value = magnitude.empty? ? 0 : magnitude.unpack1("H*").to_s.to_i(16)
|
|
106
|
-
sign.zero? ? value : -value
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
def q
|
|
110
|
-
numerator = z
|
|
111
|
-
denominator = u
|
|
112
|
-
unless denominator.positive? && numerator.gcd(denominator) == 1
|
|
113
|
-
raise InvalidValueError, "rational must have a positive reduced denominator"
|
|
114
|
-
end
|
|
115
|
-
|
|
116
|
-
Rational(numerator, denominator)
|
|
117
|
-
end
|
|
118
|
-
|
|
119
|
-
def f64
|
|
120
|
-
value = take(8).unpack1("G")
|
|
121
|
-
raise InvalidValueError, "invalid float payload" unless value.is_a?(Float)
|
|
122
|
-
|
|
123
|
-
value
|
|
124
|
-
end
|
|
125
|
-
|
|
126
|
-
def finish!
|
|
127
|
-
raise InvalidValueError, "trailing payload bytes" unless @position == @bytes.bytesize
|
|
128
|
-
|
|
129
|
-
self
|
|
130
|
-
end
|
|
131
|
-
|
|
132
|
-
private
|
|
133
|
-
|
|
134
|
-
def take(size)
|
|
135
|
-
raise InvalidValueError, "truncated payload field" if size > @bytes.bytesize - @position
|
|
136
|
-
|
|
137
|
-
result = @bytes.byteslice(@position, size)
|
|
138
|
-
raise InvalidValueError, "truncated payload field" unless result
|
|
139
|
-
|
|
140
|
-
@position += size
|
|
141
|
-
result
|
|
142
|
-
end
|
|
143
|
-
end
|
|
144
88
|
end
|
|
145
89
|
end
|
|
146
90
|
end
|
|
91
|
+
|
|
92
|
+
require_relative "wire/reader"
|