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,38 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module XitDB
|
|
4
|
+
module Ruby
|
|
5
|
+
# Preserves a complete scalar payload when its application codec is unavailable.
|
|
6
|
+
# The bytes include the Ruby payload version, type, codec identifier, and serialized data.
|
|
7
|
+
# Writing this object stores those bytes unchanged, which permits later decoding with the matching codec.
|
|
8
|
+
class EncodedValue
|
|
9
|
+
attr_reader :codec_id, :format_tag, :bytes
|
|
10
|
+
|
|
11
|
+
# @!attribute [r] codec_id
|
|
12
|
+
# Frozen identifier for the missing application codec.
|
|
13
|
+
#
|
|
14
|
+
# @return [String] frozen identifier for the missing application codec
|
|
15
|
+
# @!attribute [r] format_tag
|
|
16
|
+
# Frozen storage format label.
|
|
17
|
+
#
|
|
18
|
+
# @return [String, nil] frozen storage format label
|
|
19
|
+
# @!attribute [r] bytes
|
|
20
|
+
# Frozen complete payload, including version and codec fields.
|
|
21
|
+
#
|
|
22
|
+
# @return [String] frozen complete payload, including version and codec fields
|
|
23
|
+
|
|
24
|
+
# Copies and freezes the stored bytes and codec identifier.
|
|
25
|
+
#
|
|
26
|
+
# @param codec_id [String] identifier of the missing application codec
|
|
27
|
+
# @param bytes [String] complete encoded scalar payload
|
|
28
|
+
# @param format_tag [String] storage format label, normally "rb"
|
|
29
|
+
def initialize(codec_id:, bytes:, format_tag: "rb")
|
|
30
|
+
value = Core::ByteValue.new(bytes, format_tag: format_tag)
|
|
31
|
+
@codec_id = codec_id.dup.freeze
|
|
32
|
+
@format_tag = value.format_tag
|
|
33
|
+
@bytes = value.bytes
|
|
34
|
+
freeze
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
|
|
5
|
+
module XitDB
|
|
6
|
+
module Ruby
|
|
7
|
+
# Checks page integrity and decodes independently framed compact rows.
|
|
8
|
+
# @api private
|
|
9
|
+
class IndexedPageDecoder < PageDecoder
|
|
10
|
+
def initialize(bytes, expected)
|
|
11
|
+
raise TypeError, "page must be a String" unless Object.instance_method(:is_a?).bind_call(bytes, String)
|
|
12
|
+
|
|
13
|
+
# @type var source: String
|
|
14
|
+
source = String.instance_method(:byteslice).bind_call(bytes, 0, PageCodec::PAGE_LIMIT + 1)
|
|
15
|
+
source = source.b
|
|
16
|
+
raise InvalidValueError, "compact page byte limit" unless source.bytesize.between?(38, PageCodec::PAGE_LIMIT)
|
|
17
|
+
|
|
18
|
+
body = source.byteslice(0, source.bytesize - 32) || raise(InvalidValueError, "compact page body")
|
|
19
|
+
raise InvalidValueError, "compact page checksum" unless Digest::SHA256.digest(body) == source.byteslice(-32, 32)
|
|
20
|
+
raise InvalidValueError, "compact page magic" unless body.byteslice(0, 4) == "XR01"
|
|
21
|
+
|
|
22
|
+
body[0, 4] = "XP01"
|
|
23
|
+
super(body, expected)
|
|
24
|
+
@directory = @position
|
|
25
|
+
@payload = @directory + ((@count + 1) * 2)
|
|
26
|
+
@length = @bytes.bytesize - @payload
|
|
27
|
+
return if @length >= 0 && offset(0).zero? && offset(@count) == @length
|
|
28
|
+
|
|
29
|
+
raise InvalidValueError, "compact page directory"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def row(index)
|
|
33
|
+
raise IndexError, "row outside page" unless index.between?(0, @count - 1)
|
|
34
|
+
|
|
35
|
+
# @type var positions: Array[Integer]
|
|
36
|
+
positions = (@bytes.byteslice(@directory + (index * 2),
|
|
37
|
+
4) || raise(InvalidValueError, "compact row offsets")).unpack("S<2")
|
|
38
|
+
left = positions.fetch(0)
|
|
39
|
+
right = positions.fetch(1)
|
|
40
|
+
raise InvalidValueError, "compact row offsets" unless right.between?(left, @length)
|
|
41
|
+
|
|
42
|
+
@position = @payload + left
|
|
43
|
+
result = read_row
|
|
44
|
+
raise InvalidValueError, "compact row framing" unless @position == @payload + right
|
|
45
|
+
|
|
46
|
+
result
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def page
|
|
50
|
+
# @type var positions: Array[Integer]
|
|
51
|
+
positions = (@bytes.byteslice(@directory,
|
|
52
|
+
(@count + 1) * 2) || raise(InvalidValueError,
|
|
53
|
+
"compact page directory")).unpack("S<*")
|
|
54
|
+
@position = @payload
|
|
55
|
+
rows = ::Array.new(@count) do |index|
|
|
56
|
+
raise InvalidValueError, "compact row framing" unless @position == @payload + positions.fetch(index)
|
|
57
|
+
|
|
58
|
+
read_row
|
|
59
|
+
end
|
|
60
|
+
finish!
|
|
61
|
+
rows
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
private
|
|
65
|
+
|
|
66
|
+
def offset(index)
|
|
67
|
+
Integer((@bytes.byteslice(@directory + (index * 2),
|
|
68
|
+
2) || raise(InvalidValueError, "compact page directory")).unpack1("S<"))
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "digest"
|
|
4
|
+
|
|
5
|
+
module XitDB
|
|
6
|
+
module Ruby
|
|
7
|
+
# Adds bounded row offsets and whole-page integrity to compact cells.
|
|
8
|
+
# @api private
|
|
9
|
+
class IndexedPageEncoder < PageEncoder
|
|
10
|
+
def initialize(stored: false)
|
|
11
|
+
super
|
|
12
|
+
@bytes[0, 4] = "XR01"
|
|
13
|
+
@offsets = [0]
|
|
14
|
+
@directory = 0
|
|
15
|
+
@payload = 0
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def encode(rows)
|
|
19
|
+
bytes = super
|
|
20
|
+
return unless bytes
|
|
21
|
+
|
|
22
|
+
bytes[@directory, @offsets.size * 2] = @offsets.pack("S<*")
|
|
23
|
+
bytes + Digest::SHA256.digest(bytes)
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
private
|
|
27
|
+
|
|
28
|
+
# rubocop:disable-next Naming/PredicateMethod -- Writes the schema before reserving row offsets.
|
|
29
|
+
def write_schema(first, count)
|
|
30
|
+
return false unless super
|
|
31
|
+
|
|
32
|
+
size = (count + 1) * 2
|
|
33
|
+
return false if size > PageCodec::PAGE_LIMIT - 32 - @bytes.bytesize
|
|
34
|
+
|
|
35
|
+
@directory = @bytes.bytesize
|
|
36
|
+
@bytes << ("\x00".b * size)
|
|
37
|
+
@payload = @bytes.bytesize
|
|
38
|
+
true
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# rubocop:disable-next Naming/PredicateMethod -- Appends row offsets on success.
|
|
42
|
+
def encode_row(row)
|
|
43
|
+
return false unless super
|
|
44
|
+
|
|
45
|
+
@offsets << (@bytes.bytesize - @payload)
|
|
46
|
+
true
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
# rubocop:disable-next Naming/PredicateMethod -- Appends bytes on success.
|
|
50
|
+
def append(bytes)
|
|
51
|
+
return false unless bytes && bytes.bytesize <= PageCodec::PAGE_LIMIT - 32 - @bytes.bytesize
|
|
52
|
+
|
|
53
|
+
@bytes << bytes
|
|
54
|
+
true
|
|
55
|
+
end
|
|
56
|
+
end
|
|
57
|
+
end
|
|
58
|
+
end
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require_relative "../input_guard"
|
|
4
|
+
|
|
5
|
+
module XitDB
|
|
6
|
+
module Ruby
|
|
7
|
+
# Reads built-in contents without invoking per-object method overrides.
|
|
8
|
+
# @api private
|
|
9
|
+
module Input
|
|
10
|
+
CONTENT_TYPES = [String, ::Array, Hash].freeze
|
|
11
|
+
private_constant :CONTENT_TYPES
|
|
12
|
+
|
|
13
|
+
def self.check!(value, path: [])
|
|
14
|
+
klass = Object.instance_method(:class).bind_call(value)
|
|
15
|
+
return if CONTENT_TYPES.any? { |type| type.equal?(klass) }
|
|
16
|
+
|
|
17
|
+
InputGuard.check!(value, path: path)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def self.string(value)
|
|
21
|
+
unless Object.instance_method(:instance_of?).bind_call(value, String)
|
|
22
|
+
raise UnsupportedValueError, "String subclasses are unsupported"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
length = String.instance_method(:bytesize).bind_call(value)
|
|
26
|
+
String.instance_method(:byteslice).bind_call(value, 0, length)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def self.hash_defaults?(value)
|
|
30
|
+
default = Hash.instance_method(:default).bind_call(value)
|
|
31
|
+
procedure = Hash.instance_method(:default_proc).bind_call(value)
|
|
32
|
+
!BasicObject.instance_method(:equal?).bind_call(default, nil) ||
|
|
33
|
+
!BasicObject.instance_method(:equal?).bind_call(procedure, nil) ||
|
|
34
|
+
Hash.instance_method(:compare_by_identity?).bind_call(value)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def self.entries(value)
|
|
38
|
+
Enumerator.new do |output|
|
|
39
|
+
case value
|
|
40
|
+
when ::Array
|
|
41
|
+
index = 0
|
|
42
|
+
::Array.instance_method(:each).bind_call(value) do |child|
|
|
43
|
+
output << [index, child]
|
|
44
|
+
index += 1
|
|
45
|
+
end
|
|
46
|
+
when Hash
|
|
47
|
+
Hash.instance_method(:each_pair).bind_call(value) do |key, child|
|
|
48
|
+
output << [key, child]
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
end
|
|
54
|
+
end
|
|
55
|
+
end
|
|
@@ -1,17 +1,35 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "wire"
|
|
4
|
-
require_relative "
|
|
4
|
+
require_relative "input"
|
|
5
5
|
|
|
6
6
|
module XitDB
|
|
7
7
|
module Ruby
|
|
8
|
-
#
|
|
8
|
+
# Defines key identity for Ruby-profile maps and sets through canonical bytes.
|
|
9
|
+
# A type prefix keeps values such as Integer 1 and Float 1.0 distinct.
|
|
10
|
+
# Positive and negative floating-point zero share an identity; NaN is rejected.
|
|
11
|
+
# Empty strings and ASCII-only strings with ASCII-compatible encodings omit their encoding label.
|
|
12
|
+
# Other strings include their encoding name. These bytes support hashing, not numeric sorting.
|
|
9
13
|
module KeyEncoding
|
|
14
|
+
# Exact scalar classes allowed as ordinary Ruby map keys and set members.
|
|
10
15
|
TYPES = [NilClass, FalseClass, TrueClass, Integer, Float, String, Symbol].freeze
|
|
11
16
|
private_constant :TYPES
|
|
12
17
|
|
|
18
|
+
# Validates a built-in key and returns its canonical identity bytes.
|
|
19
|
+
#
|
|
20
|
+
# @param value [nil, Boolean, Integer, Float, String, Symbol] key with an exact supported class
|
|
21
|
+
# @return [String] frozen binary key identity
|
|
22
|
+
# @raise [UnsupportedValueError] if the key has an unsupported class or a NaN value
|
|
13
23
|
def self.dump(value)
|
|
14
|
-
|
|
24
|
+
Input.check!(value)
|
|
25
|
+
value = Input.string(value) if Object.instance_method(:is_a?).bind_call(value, String)
|
|
26
|
+
dump_stored(value)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Canonicalizes a key produced by the built-in scalar decoder.
|
|
30
|
+
# Exact type and NaN validation still apply.
|
|
31
|
+
# @api private
|
|
32
|
+
def self.dump_stored(value)
|
|
15
33
|
unless TYPES.include?(Object.instance_method(:class).bind_call(value))
|
|
16
34
|
raise UnsupportedValueError, "unsupported Ruby key class"
|
|
17
35
|
end
|
|
@@ -19,6 +37,11 @@ module XitDB
|
|
|
19
37
|
scalar_bytes(value).freeze
|
|
20
38
|
end
|
|
21
39
|
|
|
40
|
+
# Adds the scalar type prefix and encodes the key body.
|
|
41
|
+
#
|
|
42
|
+
# @param value [nil, Boolean, Integer, Float, String, Symbol] validated key
|
|
43
|
+
# @return [String] binary key identity
|
|
44
|
+
# @raise [UnsupportedValueError] if the value cannot be encoded as a key
|
|
22
45
|
def self.scalar_bytes(value)
|
|
23
46
|
case value
|
|
24
47
|
when nil then "\x00".b
|
|
@@ -29,23 +52,38 @@ module XitDB
|
|
|
29
52
|
when String then "\x05".b + string_body(value)
|
|
30
53
|
when Symbol then "\x06".b + string_body(value.to_s)
|
|
31
54
|
else
|
|
32
|
-
raise UnsupportedValueError, "unsupported Ruby key
|
|
55
|
+
raise UnsupportedValueError, "unsupported Ruby key class"
|
|
33
56
|
end
|
|
34
57
|
end
|
|
35
58
|
|
|
59
|
+
# Normalizes both signs of zero before encoding a Float key.
|
|
60
|
+
#
|
|
61
|
+
# @param value [Float] key value
|
|
62
|
+
# @return [String] float type prefix and eight data bytes
|
|
63
|
+
# @raise [UnsupportedValueError] if the value is NaN
|
|
36
64
|
def self.float_bytes(value)
|
|
37
65
|
raise UnsupportedValueError, "NaN keys are unsupported" if value.nan?
|
|
38
66
|
|
|
39
67
|
"\x04".b + Wire.f64(value.zero? ? 0.0 : value)
|
|
40
68
|
end
|
|
41
69
|
|
|
42
|
-
|
|
70
|
+
# Narrows a value to the built-in scalar types that can serve as keys.
|
|
71
|
+
# Exact-class and singleton checks belong to the caller.
|
|
72
|
+
#
|
|
73
|
+
# @param value [Object] candidate scalar
|
|
74
|
+
# @return [nil, Boolean, Integer, Float, String, Symbol] the supplied value
|
|
75
|
+
# @raise [UnsupportedValueError] if the value is outside these scalar types
|
|
76
|
+
def self.checked_key!(value)
|
|
43
77
|
case value
|
|
44
78
|
when NilClass, FalseClass, TrueClass, String, Integer, Float, Symbol then value
|
|
45
79
|
else raise UnsupportedValueError, "unsupported Ruby key class"
|
|
46
80
|
end
|
|
47
81
|
end
|
|
48
82
|
|
|
83
|
+
# Encodes the significant encoding label and original string bytes.
|
|
84
|
+
#
|
|
85
|
+
# @param value [String] string key or Symbol name
|
|
86
|
+
# @return [String] length-prefixed encoding label followed by length-prefixed bytes
|
|
49
87
|
def self.string_body(value)
|
|
50
88
|
label = if value.empty? || (value.encoding.ascii_compatible? && value.ascii_only?)
|
|
51
89
|
""
|
|
@@ -1,15 +1,27 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
require_relative "
|
|
3
|
+
require_relative "input"
|
|
4
4
|
|
|
5
5
|
module XitDB
|
|
6
6
|
module Ruby
|
|
7
|
-
#
|
|
7
|
+
# Converts typed sorted keys to bytes that compare in the required order.
|
|
8
|
+
# A sorted collection has one declared kind: string, integer, float, or time.
|
|
9
|
+
# Strings compare by UTF-8 bytes; numbers compare by value; times compare by exact instant.
|
|
10
|
+
# Float zero signs share one position, and NaN has no supported position.
|
|
8
11
|
module OrderedKeys
|
|
12
|
+
# Exact Ruby class accepted for each declared sorted-key kind.
|
|
9
13
|
TYPES = { string: String, integer: Integer, float: Float, time: Time }.freeze
|
|
10
14
|
|
|
15
|
+
# Encodes a key for a sorted collection with one declared type.
|
|
16
|
+
#
|
|
17
|
+
# @param value [String, Integer, Float, Time] exact instance of the declared key class
|
|
18
|
+
# @param kind [Symbol] :string, :integer, :float, or :time
|
|
19
|
+
# @return [String] frozen binary bytes with the declared ordering
|
|
20
|
+
# @raise [UnsupportedValueError] if the key has the wrong class, an altered Time, or a NaN value
|
|
21
|
+
# @raise [UnsupportedEncodingError] if a string cannot become valid UTF-8
|
|
11
22
|
def self.dump(value, kind:)
|
|
12
|
-
|
|
23
|
+
Input.check!(value)
|
|
24
|
+
value = Input.string(value) if Object.instance_method(:is_a?).bind_call(value, String)
|
|
13
25
|
unless TYPES[kind].equal?(Object.instance_method(:class).bind_call(value))
|
|
14
26
|
raise UnsupportedValueError, "sorted key must match its declared key kind"
|
|
15
27
|
end
|
|
@@ -27,6 +39,11 @@ module XitDB
|
|
|
27
39
|
bytes.freeze
|
|
28
40
|
end
|
|
29
41
|
|
|
42
|
+
# Converts a string key to the UTF-8 bytes used for ordering.
|
|
43
|
+
#
|
|
44
|
+
# @param value [String] key text
|
|
45
|
+
# @return [String] binary UTF-8 bytes
|
|
46
|
+
# @raise [UnsupportedEncodingError] if transcoding fails or produces invalid UTF-8
|
|
30
47
|
def self.string_bytes(value)
|
|
31
48
|
text = value.encode(Encoding::UTF_8)
|
|
32
49
|
raise UnsupportedEncodingError, "sorted string key must be valid UTF-8" unless text.valid_encoding?
|
|
@@ -36,6 +53,13 @@ module XitDB
|
|
|
36
53
|
raise UnsupportedEncodingError, "sorted string key cannot convert to UTF-8", cause: e
|
|
37
54
|
end
|
|
38
55
|
|
|
56
|
+
# Transforms Float bits so byte order follows numeric order.
|
|
57
|
+
# Both signs of zero use the positive-zero representation.
|
|
58
|
+
#
|
|
59
|
+
# @param value [Float] sortable value
|
|
60
|
+
# @return [String] eight binary bytes
|
|
61
|
+
# @raise [UnsupportedValueError] if the value is NaN
|
|
62
|
+
# @raise [InvalidValueError] if float bits cannot be read as an Integer
|
|
39
63
|
def self.float_bytes(value)
|
|
40
64
|
raise UnsupportedValueError, "NaN keys are unsupported" if value.nan?
|
|
41
65
|
|
|
@@ -45,6 +69,11 @@ module XitDB
|
|
|
45
69
|
[bits[63] == 1 ? bits ^ Core::Format::UINT64_MAX : bits ^ (1 << 63)].pack("Q>")
|
|
46
70
|
end
|
|
47
71
|
|
|
72
|
+
# Prefixes a nonnegative magnitude with its eight-byte big-endian byte count.
|
|
73
|
+
#
|
|
74
|
+
# @param number [Integer] nonnegative magnitude
|
|
75
|
+
# @return [String] length and magnitude bytes
|
|
76
|
+
# @raise [RangeError] if the magnitude length exceeds an unsigned 64-bit count
|
|
48
77
|
def self.unsigned(number)
|
|
49
78
|
hex = number.to_s(16)
|
|
50
79
|
hex = "0#{hex}" if hex.length.odd?
|
|
@@ -54,16 +83,30 @@ module XitDB
|
|
|
54
83
|
[magnitude.bytesize].pack("Q>") + magnitude
|
|
55
84
|
end
|
|
56
85
|
|
|
86
|
+
# Inverts each bit to reverse the byte comparison direction.
|
|
87
|
+
#
|
|
88
|
+
# @param bytes [String] binary field
|
|
89
|
+
# @return [String] complemented bytes
|
|
57
90
|
def self.complement(bytes)
|
|
58
91
|
bytes.bytes.map { |byte| byte ^ 255 }.pack("C*")
|
|
59
92
|
end
|
|
60
93
|
|
|
94
|
+
# Adds a sign prefix and reverses the magnitude order for negative values.
|
|
95
|
+
#
|
|
96
|
+
# @param number [Integer, Rational] value whose sign controls the encoding
|
|
97
|
+
# @param body [String] encoding of its positive magnitude
|
|
98
|
+
# @return [String] negative, zero, or positive ordered field
|
|
61
99
|
def self.signed(number, body)
|
|
62
100
|
return "\x01".b if number.zero?
|
|
63
101
|
|
|
64
102
|
number.negative? ? "\x00".b + complement(body) : "\x02".b + body
|
|
65
103
|
end
|
|
66
104
|
|
|
105
|
+
# Encodes a positive rational through successive division quotients.
|
|
106
|
+
# Alternating complemented fields preserve the order of exact Time instants.
|
|
107
|
+
#
|
|
108
|
+
# @param number [Rational] positive magnitude
|
|
109
|
+
# @return [String] ordered rational bytes
|
|
67
110
|
def self.rational(number)
|
|
68
111
|
numerator = number.numerator
|
|
69
112
|
denominator = number.denominator
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module XitDB
|
|
4
|
+
module Ruby
|
|
5
|
+
# Keeps short arrays in one record and larger arrays in a bounded slot tree.
|
|
6
|
+
# @api private
|
|
7
|
+
class PackedArray < Containers
|
|
8
|
+
INLINE = 2
|
|
9
|
+
TREE = 3
|
|
10
|
+
MAX_FIELDS = 16
|
|
11
|
+
|
|
12
|
+
# Packed arrays initialize their fields without an indexed envelope.
|
|
13
|
+
# rubocop:disable-next Lint/MissingSuper
|
|
14
|
+
def initialize(profile, cursor, context:, path:, kind: nil, defer: false, record: nil)
|
|
15
|
+
@profile = profile
|
|
16
|
+
@cursor = cursor
|
|
17
|
+
@context = context
|
|
18
|
+
@path = path
|
|
19
|
+
@kind = :array
|
|
20
|
+
@deferred = defer
|
|
21
|
+
@fields = []
|
|
22
|
+
@builder = nil
|
|
23
|
+
@count = 0
|
|
24
|
+
return if kind
|
|
25
|
+
|
|
26
|
+
invalid! if cursor.slot.full?
|
|
27
|
+
read_record(record || Core::Record.read(context.database.core, cursor.slot, bound: context.read_bound))
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def slot
|
|
31
|
+
publish if @deferred
|
|
32
|
+
cursor.slot
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def size
|
|
36
|
+
return @count if @builder
|
|
37
|
+
|
|
38
|
+
collection = @collection
|
|
39
|
+
collection ? collection.size : @fields.size
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def lookup(selector)
|
|
43
|
+
index = index_selector!(selector)
|
|
44
|
+
finish_builder
|
|
45
|
+
collection = @collection
|
|
46
|
+
collection ? array_collection!.get(index) : @fields[index]
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
def assign(selector, stored)
|
|
50
|
+
stored = Pages.persist(stored, @context)
|
|
51
|
+
index = index_selector!(selector)
|
|
52
|
+
if (builder = @builder)
|
|
53
|
+
raise IndexError, "unpublished array construction must append" unless index == size
|
|
54
|
+
|
|
55
|
+
builder.append(stored)
|
|
56
|
+
@count += 1
|
|
57
|
+
elsif @collection
|
|
58
|
+
return super
|
|
59
|
+
else
|
|
60
|
+
assign_inline(index, stored)
|
|
61
|
+
end
|
|
62
|
+
publish unless @deferred
|
|
63
|
+
self
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def insert(index, stored)
|
|
67
|
+
stored = Pages.persist(stored, @context)
|
|
68
|
+
finish_builder
|
|
69
|
+
return super if @collection
|
|
70
|
+
|
|
71
|
+
raise IndexError, "array index outside list" unless index.between?(0, size)
|
|
72
|
+
|
|
73
|
+
@fields.insert(index, stored)
|
|
74
|
+
start_builder if @fields.size > MAX_FIELDS
|
|
75
|
+
publish
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def delete(selector)
|
|
79
|
+
finish_builder
|
|
80
|
+
return super if @collection
|
|
81
|
+
|
|
82
|
+
index = index_selector!(selector)
|
|
83
|
+
raise IndexError, "array index outside list" unless index >= 0 && index < size
|
|
84
|
+
|
|
85
|
+
@fields.delete_at(index)
|
|
86
|
+
publish
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def truncate(count)
|
|
90
|
+
finish_builder
|
|
91
|
+
Core::Format.integer!(count, 0..size)
|
|
92
|
+
return super if @collection && count > MAX_FIELDS
|
|
93
|
+
|
|
94
|
+
@fields = Array.new(count) { |index| required_slot!(lookup(index)) }
|
|
95
|
+
@collection = nil
|
|
96
|
+
publish
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def clear
|
|
100
|
+
@fields.clear
|
|
101
|
+
@collection = nil
|
|
102
|
+
@builder = nil
|
|
103
|
+
@count = 0
|
|
104
|
+
publish
|
|
105
|
+
end
|
|
106
|
+
|
|
107
|
+
def each_entry
|
|
108
|
+
return enum_for(:each_entry) unless block_given?
|
|
109
|
+
|
|
110
|
+
finish_builder
|
|
111
|
+
collection = @collection
|
|
112
|
+
if collection
|
|
113
|
+
array_collection!.each.with_index { |stored, index| yield index, stored }
|
|
114
|
+
else
|
|
115
|
+
@fields.each_with_index { |stored, index| yield index, stored }
|
|
116
|
+
end
|
|
117
|
+
self
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
private
|
|
121
|
+
|
|
122
|
+
def assign_inline(index, stored)
|
|
123
|
+
if index == size
|
|
124
|
+
@fields << stored
|
|
125
|
+
start_builder if @fields.size > MAX_FIELDS
|
|
126
|
+
else
|
|
127
|
+
index += size if index.negative?
|
|
128
|
+
raise IndexError, "array index outside list" unless index >= 0 && index < size
|
|
129
|
+
|
|
130
|
+
@fields[index] = stored
|
|
131
|
+
end
|
|
132
|
+
end
|
|
133
|
+
|
|
134
|
+
def read_record(record)
|
|
135
|
+
fields = record.fields
|
|
136
|
+
case record.metadata
|
|
137
|
+
when INLINE
|
|
138
|
+
invalid! if fields.size > MAX_FIELDS
|
|
139
|
+
@fields = fields.dup
|
|
140
|
+
when TREE
|
|
141
|
+
invalid! unless fields.size == 1 && fields.fetch(0).tag == :array_list
|
|
142
|
+
@collection = Core::ArrayList.new(@context.cursor(fields.fetch(0)))
|
|
143
|
+
else invalid!
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def start_builder
|
|
148
|
+
builder = Core::ArrayBuilder.new(@context.writable_transaction!)
|
|
149
|
+
@fields.each { |stored| builder.append(stored) }
|
|
150
|
+
@builder = builder
|
|
151
|
+
@count = @fields.size
|
|
152
|
+
@fields.clear
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def finish_builder
|
|
156
|
+
builder = @builder
|
|
157
|
+
return unless builder
|
|
158
|
+
|
|
159
|
+
@collection = Core::ArrayList.new(@context.cursor(builder.slot))
|
|
160
|
+
@builder = nil
|
|
161
|
+
end
|
|
162
|
+
|
|
163
|
+
def publish
|
|
164
|
+
finish_builder
|
|
165
|
+
collection = @collection
|
|
166
|
+
fields = collection ? [collection.cursor.slot] : @fields
|
|
167
|
+
stored = Core::Record.append(@context.writable_transaction!, metadata: collection ? TREE : INLINE,
|
|
168
|
+
fields: fields)
|
|
169
|
+
cursor.replace(stored)
|
|
170
|
+
@deferred = false
|
|
171
|
+
self
|
|
172
|
+
end
|
|
173
|
+
|
|
174
|
+
def invalid!
|
|
175
|
+
raise InvalidValueError, "invalid packed Ruby array"
|
|
176
|
+
end
|
|
177
|
+
end
|
|
178
|
+
end
|
|
179
|
+
end
|