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
|
@@ -2,12 +2,29 @@
|
|
|
2
2
|
|
|
3
3
|
module XitDB
|
|
4
4
|
module Core
|
|
5
|
+
# A receiver's committed boundary and root checksum used to select a replication start.
|
|
6
|
+
# @!attribute [r] committed_size
|
|
7
|
+
# @return [Integer] committed byte boundary
|
|
8
|
+
# @!attribute [r] checksum
|
|
9
|
+
# @return [Integer] unsigned 64-bit checksum of the committed root trailer
|
|
10
|
+
# A receiver's committed boundary and root checksum used to select a replication start.
|
|
11
|
+
# @!attribute [r] committed_size
|
|
12
|
+
# @return [Integer] committed byte boundary
|
|
13
|
+
# @!attribute [r] checksum
|
|
14
|
+
# @return [Integer] unsigned 64-bit checksum of the committed root trailer
|
|
5
15
|
ReplicaState = Data.define(:committed_size, :checksum)
|
|
6
16
|
|
|
7
|
-
#
|
|
17
|
+
# Byte-level replication operations included in Core::Database.
|
|
18
|
+
#
|
|
19
|
+
# A replica state identifies a committed boundary and its root checksum.
|
|
20
|
+
#
|
|
21
|
+
# A matching boundary permits an incremental append; otherwise the receiver needs a complete copy.
|
|
8
22
|
module Replication
|
|
23
|
+
# Maximum bytes requested or emitted per replication chunk.
|
|
9
24
|
CHUNK_SIZE = 64 * 1024
|
|
10
25
|
|
|
26
|
+
# Captures the committed byte count and root checksum for replication negotiation.
|
|
27
|
+
# @return [ReplicaState]
|
|
11
28
|
def replica_state
|
|
12
29
|
with_operation do
|
|
13
30
|
size, = committed_state
|
|
@@ -16,14 +33,19 @@ module XitDB
|
|
|
16
33
|
end
|
|
17
34
|
end
|
|
18
35
|
|
|
36
|
+
# Returns the matching committed boundary, or zero when a full copy is required.
|
|
37
|
+
# @param state [ReplicaState] receiver committed boundary and checksum
|
|
38
|
+
# @return [Integer]
|
|
39
|
+
# @note Matching uses the root trailer checksum at the supplied boundary.
|
|
40
|
+
# @raise [TypeError] if state is not a ReplicaState
|
|
19
41
|
def replication_start(state)
|
|
20
42
|
raise TypeError, "expected ReplicaState" unless state.is_a?(ReplicaState)
|
|
21
43
|
|
|
22
|
-
Replication.
|
|
23
|
-
Format.integer(state.checksum, 0..Format::UINT64_MAX)
|
|
44
|
+
Replication.ensure_position!(state.committed_size)
|
|
45
|
+
Format.integer!(state.checksum, 0..Format::UINT64_MAX)
|
|
24
46
|
with_operation do
|
|
25
47
|
size, = committed_state
|
|
26
|
-
next 0 if state.committed_size > size
|
|
48
|
+
next 0 if state.committed_size > size
|
|
27
49
|
|
|
28
50
|
begin
|
|
29
51
|
_, checksum = Replication.trailer(@store, header, state.committed_size, size)
|
|
@@ -34,20 +56,33 @@ module XitDB
|
|
|
34
56
|
end
|
|
35
57
|
end
|
|
36
58
|
|
|
59
|
+
# Writes a committed byte range to an IO-like destination.
|
|
60
|
+
# @param start [Integer] absolute starting byte offset of the transfer
|
|
61
|
+
# @param finish [Integer] exclusive end offset of the committed range
|
|
62
|
+
# @param io [#write] binary destination implementing write(bytes)
|
|
63
|
+
# @return [nil]
|
|
64
|
+
# @note A zero start writes a complete header with root metadata for finish.
|
|
65
|
+
# @raise [ArgumentError] if the byte range is invalid or exceeds committed data
|
|
66
|
+
# @raise [IOError] if the destination reports an invalid write count
|
|
37
67
|
def read_committed(start, finish, io)
|
|
38
|
-
Replication.
|
|
39
|
-
Replication.
|
|
68
|
+
Replication.ensure_position!(start)
|
|
69
|
+
Replication.ensure_position!(finish)
|
|
40
70
|
with_operation do
|
|
41
71
|
size, = committed_state
|
|
42
|
-
if start > finish || finish > size || (start.positive? && start <
|
|
72
|
+
if start > finish || finish > size || (start.positive? && start < header.data_offset)
|
|
43
73
|
raise ArgumentError, "invalid committed byte range"
|
|
44
74
|
end
|
|
45
75
|
|
|
46
76
|
if start.zero?
|
|
47
77
|
slot, = Replication.trailer(@store, header, finish, size)
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
78
|
+
if header.version == 1
|
|
79
|
+
Replication.write_all(io, @store.read_at(0, Header::SIZE))
|
|
80
|
+
Replication.write_all(io, slot.to_bytes)
|
|
81
|
+
else
|
|
82
|
+
Replication.write_all(io, @store.read_at(0, header.committed_size_offset))
|
|
83
|
+
end
|
|
84
|
+
Replication.write_all(io, [finish].pack("Q>"))
|
|
85
|
+
start = header.data_offset
|
|
51
86
|
end
|
|
52
87
|
while start < finish
|
|
53
88
|
chunk = @store.read_at(start, [finish - start, CHUNK_SIZE].min)
|
|
@@ -58,9 +93,18 @@ module XitDB
|
|
|
58
93
|
nil
|
|
59
94
|
end
|
|
60
95
|
|
|
96
|
+
# Appends a replication range and publishes its validated final root.
|
|
97
|
+
# @param start [Integer] absolute starting byte offset of the transfer
|
|
98
|
+
# @param io [#read] binary source implementing read(length)
|
|
99
|
+
# @param length [Integer] number of bytes
|
|
100
|
+
# @return [nil]
|
|
101
|
+
# @raise [ArgumentError] if start is zero or differs from the current committed size
|
|
102
|
+
# @raise [EOFError] if the stream ends before length bytes
|
|
103
|
+
# @raise [CommitError] if storage fails; its outcome describes possible publication
|
|
104
|
+
# @raise [TransactionStateError] if this handle requires reopening
|
|
61
105
|
def apply_replicated(start, io, length)
|
|
62
|
-
Replication.
|
|
63
|
-
Replication.
|
|
106
|
+
Replication.ensure_position!(start)
|
|
107
|
+
Replication.ensure_position!(length)
|
|
64
108
|
raise ArgumentError, "full replacement requires Database.copy_replica" if start.zero?
|
|
65
109
|
raise TransactionStateError, "database requires reopening" if @needs_reopen
|
|
66
110
|
|
|
@@ -81,7 +125,7 @@ module XitDB
|
|
|
81
125
|
Replication.storage_write { @store.sync }
|
|
82
126
|
outcome = :unknown
|
|
83
127
|
Replication.storage_write(outcome: outcome) do
|
|
84
|
-
|
|
128
|
+
write_root_header(slot, finish)
|
|
85
129
|
@store.sync
|
|
86
130
|
end
|
|
87
131
|
outcome = :committed
|
|
@@ -101,6 +145,7 @@ module XitDB
|
|
|
101
145
|
raise
|
|
102
146
|
end
|
|
103
147
|
|
|
148
|
+
# Removes stale uncommitted bytes and appends declared replication chunks.
|
|
104
149
|
def append_replica_chunks(io, length, size)
|
|
105
150
|
Replication.storage_write { @store.truncate(size) } if @store.length > size
|
|
106
151
|
Replication.read_chunks(io, length) do |chunk|
|
|
@@ -108,6 +153,7 @@ module XitDB
|
|
|
108
153
|
end
|
|
109
154
|
end
|
|
110
155
|
|
|
156
|
+
# Rolls back an unpublished append or marks uncertain publication for reopening.
|
|
111
157
|
def recover_replica_application(size, completed, outcome)
|
|
112
158
|
@needs_reopen = true if outcome == :unknown
|
|
113
159
|
return if completed || outcome != :not_committed
|
|
@@ -120,8 +166,17 @@ module XitDB
|
|
|
120
166
|
end
|
|
121
167
|
private :append_replica_chunks, :recover_replica_application
|
|
122
168
|
|
|
169
|
+
# Stages and validates a complete replica in an empty destination file.
|
|
170
|
+
# @param to [String] destination file path, which must be empty or absent
|
|
171
|
+
# @param source_io [#read] binary reader implementing read(length)
|
|
172
|
+
# @param length [Integer] number of bytes
|
|
173
|
+
# @return [Database]
|
|
174
|
+
# @raise [ArgumentError] if the target is nonempty or the replica is shorter than a database
|
|
175
|
+
# @raise [CorruptionError] if root metadata or the checksum is invalid
|
|
176
|
+
# @raise [EOFError] if the stream ends before length bytes
|
|
177
|
+
# @raise [CommitError] if durable staging or cleanup fails
|
|
123
178
|
def self.copy_replica(to:, source_io:, length:)
|
|
124
|
-
|
|
179
|
+
ensure_position!(length)
|
|
125
180
|
raise ArgumentError, "full replica is shorter than a database" if length < Database::MINIMUM_SIZE
|
|
126
181
|
|
|
127
182
|
store = FileStore.open(to)
|
|
@@ -137,7 +192,7 @@ module XitDB
|
|
|
137
192
|
|
|
138
193
|
begin
|
|
139
194
|
read_chunks(source_io, length) { |chunk| storage_write { store.append(chunk) } }
|
|
140
|
-
validate_replica(store, length)
|
|
195
|
+
validate_replica!(store, length)
|
|
141
196
|
|
|
142
197
|
outcome = :unknown
|
|
143
198
|
storage_write(outcome: outcome) { store.sync }
|
|
@@ -159,6 +214,7 @@ module XitDB
|
|
|
159
214
|
end
|
|
160
215
|
end
|
|
161
216
|
|
|
217
|
+
# Removes staged bytes only when durable publication has not started.
|
|
162
218
|
def self.cleanup_staged_replica(store, completed, outcome)
|
|
163
219
|
return if completed || outcome != :not_committed
|
|
164
220
|
|
|
@@ -170,27 +226,35 @@ module XitDB
|
|
|
170
226
|
end
|
|
171
227
|
private_class_method :cleanup_staged_replica
|
|
172
228
|
|
|
173
|
-
|
|
229
|
+
# Checks the declared size and trailer. Upstream v1 writers may leave the cached slot stale.
|
|
230
|
+
def self.validate_replica!(store, length)
|
|
174
231
|
header = Header.parse(store.read_at(0, Header::SIZE))
|
|
175
|
-
|
|
176
|
-
unless Format.unsigned(Format.slice(root, 9, 8)) == length
|
|
232
|
+
unless Format.unsigned(store.read_at(header.committed_size_offset, 8)) == length
|
|
177
233
|
raise CorruptionError, "full replica length does not match its root header"
|
|
178
234
|
end
|
|
179
235
|
|
|
180
|
-
|
|
181
|
-
return if root.byteslice(0, 9) == slot.to_bytes
|
|
182
|
-
|
|
183
|
-
raise CorruptionError, "full replica root does not match its trailer"
|
|
236
|
+
trailer(store, header, length, length)
|
|
184
237
|
end
|
|
185
|
-
private_class_method :validate_replica
|
|
238
|
+
private_class_method :validate_replica!
|
|
186
239
|
|
|
187
|
-
|
|
240
|
+
# Rejects offsets and lengths that are not nonnegative integers.
|
|
241
|
+
# @param value [Integer] integer value
|
|
242
|
+
# @return [void]
|
|
243
|
+
def self.ensure_position!(value)
|
|
188
244
|
raise TypeError, "offset and length must be Integers" unless value.is_a?(Integer)
|
|
189
245
|
raise ArgumentError, "offset and length must be nonnegative" if value.negative?
|
|
190
246
|
end
|
|
191
247
|
|
|
248
|
+
# Validates a committed trailer's location, slot tag, and checksum.
|
|
249
|
+
# @param store [MemoryStore, FileStore] underlying byte storage
|
|
250
|
+
# @param header [Header] file header containing the checksum identity
|
|
251
|
+
# @param finish [Integer] exclusive end offset of the committed range
|
|
252
|
+
# @param bound [Integer] exclusive upper byte boundary for reads
|
|
253
|
+
# @return [Array(Slot, Integer)]
|
|
254
|
+
# @raise [CorruptionError] if the boundary, slot tag, or checksum is invalid
|
|
192
255
|
def self.trailer(store, header, finish, bound)
|
|
193
|
-
|
|
256
|
+
minimum_size = header.data_offset + Database::TRAILER_SIZE
|
|
257
|
+
raise CorruptionError, "invalid root trailer position" unless finish.between?(minimum_size, bound)
|
|
194
258
|
|
|
195
259
|
position = finish - Database::TRAILER_SIZE
|
|
196
260
|
bytes = store.read_at(position, Database::TRAILER_SIZE)
|
|
@@ -206,6 +270,15 @@ module XitDB
|
|
|
206
270
|
raise CorruptionError, e.message
|
|
207
271
|
end
|
|
208
272
|
|
|
273
|
+
# Reads exactly the declared number of bytes, yielding bounded chunks.
|
|
274
|
+
# @param io [#read] binary source implementing read(length)
|
|
275
|
+
# @param length [Integer] number of bytes
|
|
276
|
+
# @return [void]
|
|
277
|
+
# @yield [bytes] consumes each chunk before reading the next
|
|
278
|
+
# @yieldparam bytes [String] at most CHUNK_SIZE bytes
|
|
279
|
+
# @yieldreturn [Object] ignored
|
|
280
|
+
# @raise [EOFError] if the stream ends early
|
|
281
|
+
# @raise [IOError] if the reader returns an invalid chunk
|
|
209
282
|
def self.read_chunks(io, length)
|
|
210
283
|
while length.positive?
|
|
211
284
|
count = [length, CHUNK_SIZE].min
|
|
@@ -220,6 +293,10 @@ module XitDB
|
|
|
220
293
|
end
|
|
221
294
|
end
|
|
222
295
|
|
|
296
|
+
# Repeats writes until the destination accepts every byte.
|
|
297
|
+
# @param io [#write] binary destination implementing write(bytes)
|
|
298
|
+
# @param bytes [String] binary input bytes
|
|
299
|
+
# @return [void]
|
|
223
300
|
def self.write_all(io, bytes)
|
|
224
301
|
offset = 0
|
|
225
302
|
while offset < bytes.bytesize
|
|
@@ -232,6 +309,12 @@ module XitDB
|
|
|
232
309
|
end
|
|
233
310
|
end
|
|
234
311
|
|
|
312
|
+
# Adds the known commit outcome to a storage failure.
|
|
313
|
+
# @param outcome [Symbol] :not_committed, :committed, or :unknown durability outcome
|
|
314
|
+
# @return [Object]
|
|
315
|
+
# @yield performs the storage operation
|
|
316
|
+
# @yieldreturn [Object] result returned to the caller
|
|
317
|
+
# @raise [CommitError] wraps any storage exception with the supplied outcome
|
|
235
318
|
def self.storage_write(outcome: :not_committed)
|
|
236
319
|
yield
|
|
237
320
|
rescue Exception => e # rubocop:disable Lint/RescueException -- Every storage failure must retain its commit outcome.
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module XitDB
|
|
4
|
+
module Core
|
|
5
|
+
# An immutable nine-byte descriptor for one database value.
|
|
6
|
+
#
|
|
7
|
+
# The tag says how to read value: it holds scalar bits or the offset of another record.
|
|
8
|
+
#
|
|
9
|
+
# The full flag has a tag-specific meaning, such as explicit null or a byte format tag.
|
|
10
|
+
#
|
|
11
|
+
# Use the scalar factory methods to encode numbers; value itself always holds unsigned 64-bit bits.
|
|
12
|
+
#
|
|
13
|
+
# @example Build slots for scalar values
|
|
14
|
+
# Slot.int(-3).tag # => :int
|
|
15
|
+
# Slot.uint(3).value # => 3
|
|
16
|
+
# Slot.null.empty? # => false
|
|
17
|
+
# Slot.new.empty? # => true
|
|
18
|
+
class Slot
|
|
19
|
+
# Width of a slot: one tag byte and eight value bytes.
|
|
20
|
+
SIZE = 9
|
|
21
|
+
# Known symbolic tags, ordered by their seven-bit numeric storage code.
|
|
22
|
+
TAGS = %i[none index array_list linked_array_list hash_map kv_pair bytes short_bytes uint int float
|
|
23
|
+
hash_set counted_hash_map counted_hash_set sorted_map sorted_set sorted_kv_pair record].freeze
|
|
24
|
+
|
|
25
|
+
# @!attribute [r] tag
|
|
26
|
+
# Type tag that determines how to interpret value.
|
|
27
|
+
# @return [Symbol, Integer]
|
|
28
|
+
# @!attribute [r] value
|
|
29
|
+
# Unsigned 64-bit scalar bits or record offset.
|
|
30
|
+
# @return [Integer]
|
|
31
|
+
attr_reader :tag, :value
|
|
32
|
+
|
|
33
|
+
# Builds a frozen nine-byte value descriptor.
|
|
34
|
+
# @param tag [Symbol, Integer] storage type tag
|
|
35
|
+
# @param value [Integer] integer value
|
|
36
|
+
# @param full [Boolean] tag-dependent flag stored in the high tag bit
|
|
37
|
+
# @return [Slot]
|
|
38
|
+
# @raise [ArgumentError] if the tag cannot be encoded in seven bits
|
|
39
|
+
# @raise [TypeError] if value or full has the wrong type
|
|
40
|
+
# @raise [RangeError] if value exceeds unsigned 64-bit storage
|
|
41
|
+
def initialize(tag: :none, value: 0, full: false)
|
|
42
|
+
code = tag.is_a?(Symbol) ? TAGS.index(tag) : tag
|
|
43
|
+
raise ArgumentError, "invalid slot tag: #{tag.inspect}" unless code.is_a?(Integer) && (0..127).cover?(code)
|
|
44
|
+
raise TypeError, "full must be true or false" unless full.equal?(true) || full.equal?(false)
|
|
45
|
+
|
|
46
|
+
@tag = TAGS[code] || code
|
|
47
|
+
@value = Format.integer!(value, 0..Format::UINT64_MAX)
|
|
48
|
+
@full = full
|
|
49
|
+
freeze
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Decodes a descriptor, including its tag and full flag.
|
|
53
|
+
# @param bytes [String] binary input bytes
|
|
54
|
+
# @return [Slot]
|
|
55
|
+
# @raise [CorruptionError] if bytes does not contain exactly nine bytes
|
|
56
|
+
def self.parse(bytes)
|
|
57
|
+
Format.ensure_width!(bytes, SIZE)
|
|
58
|
+
flags = bytes.getbyte(0) || raise(CorruptionError, "missing slot flags")
|
|
59
|
+
value = Format.unsigned(Format.slice(bytes, 1, 8))
|
|
60
|
+
new(tag: flags & 0x7f, value: value, full: flags.anybits?(0x80))
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# Stores a signed 64-bit integer using its unsigned bit pattern.
|
|
64
|
+
# @param value [Integer] integer value
|
|
65
|
+
# @return [Slot]
|
|
66
|
+
# @raise [RangeError] if value exceeds the signed 64-bit range
|
|
67
|
+
def self.int(value)
|
|
68
|
+
Format.integer!(value, -(1 << 63)..((1 << 63) - 1))
|
|
69
|
+
new(tag: :int, value: value & Format::UINT64_MAX)
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Stores an unsigned 64-bit integer directly in a slot.
|
|
73
|
+
# @param value [Integer] integer value
|
|
74
|
+
# @return [Slot]
|
|
75
|
+
# @raise [RangeError] if value exceeds the unsigned 64-bit range
|
|
76
|
+
def self.uint(value)
|
|
77
|
+
new(tag: :uint, value: value)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# Stores the bit pattern of a Ruby Float in a slot.
|
|
81
|
+
# @param value [Float] floating-point value to encode
|
|
82
|
+
# @return [Slot]
|
|
83
|
+
# @raise [TypeError] if value is not a Float
|
|
84
|
+
def self.float(value)
|
|
85
|
+
raise TypeError, "expected Float, got #{value.class}" unless value.is_a?(Float)
|
|
86
|
+
|
|
87
|
+
new(tag: :float, value: Format.unsigned([value].pack("G")))
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Builds an explicit null slot, distinct from an empty slot.
|
|
91
|
+
# @return [Slot]
|
|
92
|
+
def self.null
|
|
93
|
+
new(full: true)
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Returns the tag-specific flag stored in the high tag bit.
|
|
97
|
+
# @return [Boolean]
|
|
98
|
+
def full?
|
|
99
|
+
@full
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
# Tests whether this slot represents an unused position.
|
|
103
|
+
# @return [Boolean]
|
|
104
|
+
def empty?
|
|
105
|
+
tag == :none && !full?
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Compares the tag, value, and full flag with another slot.
|
|
109
|
+
# @param other [Object] value to compare
|
|
110
|
+
# @return [Boolean]
|
|
111
|
+
def ==(other)
|
|
112
|
+
other.is_a?(Slot) && tag == other.tag && full? == other.full? && value == other.value
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Encodes the tag, full flag, and value as nine bytes.
|
|
116
|
+
# @return [String]
|
|
117
|
+
def to_bytes
|
|
118
|
+
code = tag.is_a?(Symbol) ? TAGS.index(tag) : tag
|
|
119
|
+
[code | (full? ? 0x80 : 0), value].pack("CQ>")
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|