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,172 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module XitDB
|
|
4
|
+
module Ruby
|
|
5
|
+
# Validates every cell while allocating only requested row values.
|
|
6
|
+
# @api private
|
|
7
|
+
class PageDecoder
|
|
8
|
+
def initialize(bytes, expected)
|
|
9
|
+
raise TypeError, "page must be a String" unless Object.instance_method(:is_a?).bind_call(bytes, String)
|
|
10
|
+
|
|
11
|
+
# @type var source: String
|
|
12
|
+
source = String.instance_method(:byteslice).bind_call(bytes, 0, PageCodec::PAGE_LIMIT + 1)
|
|
13
|
+
@bytes = source.b
|
|
14
|
+
raise InvalidValueError, "Ruby page byte limit exceeded" if @bytes.bytesize > PageCodec::PAGE_LIMIT
|
|
15
|
+
|
|
16
|
+
@position = 0
|
|
17
|
+
raise InvalidValueError, "invalid Ruby page magic" unless take(4) == "XP01"
|
|
18
|
+
|
|
19
|
+
@count = unsigned
|
|
20
|
+
width = unsigned
|
|
21
|
+
validate_count!(@count, width, expected)
|
|
22
|
+
@keys = schema(width)
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def page
|
|
26
|
+
rows = ::Array.new(@count) { read_row }
|
|
27
|
+
finish!
|
|
28
|
+
rows
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def row(index)
|
|
32
|
+
raise IndexError, "row outside Ruby page" if index >= @count
|
|
33
|
+
|
|
34
|
+
result = nil
|
|
35
|
+
@count.times do |position|
|
|
36
|
+
if position == index
|
|
37
|
+
result = read_row
|
|
38
|
+
else
|
|
39
|
+
@keys.size.times { skip_cell }
|
|
40
|
+
end
|
|
41
|
+
end
|
|
42
|
+
finish!
|
|
43
|
+
result || raise(IndexError, "row outside Ruby page")
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
private
|
|
47
|
+
|
|
48
|
+
def validate_count!(count, width, expected)
|
|
49
|
+
unless count.between?(1, PageCodec::ROW_LIMIT) && width <= PageCodec::FIELD_LIMIT
|
|
50
|
+
raise InvalidValueError, "invalid Ruby page count"
|
|
51
|
+
end
|
|
52
|
+
return if BasicObject.instance_method(:equal?).bind_call(expected, nil)
|
|
53
|
+
|
|
54
|
+
unless Object.instance_method(:is_a?).bind_call(expected, Integer)
|
|
55
|
+
raise TypeError, "expected row count must be an Integer"
|
|
56
|
+
end
|
|
57
|
+
raise InvalidValueError, "Ruby page row count mismatch" unless expected == count
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def schema(width)
|
|
61
|
+
# @type var seen: Hash[String, bool]
|
|
62
|
+
seen = {}
|
|
63
|
+
::Array.new(width) do
|
|
64
|
+
key = cell
|
|
65
|
+
raise InvalidValueError, "Ruby page key must be a String" unless key.is_a?(String)
|
|
66
|
+
raise InvalidValueError, "duplicate Ruby page key" if seen.key?(key)
|
|
67
|
+
|
|
68
|
+
key = -key
|
|
69
|
+
seen[key] = true
|
|
70
|
+
key
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def read_row
|
|
75
|
+
@keys.to_h { |key| [key, cell] }
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def cell
|
|
79
|
+
tag = byte
|
|
80
|
+
case tag
|
|
81
|
+
when 0 then nil
|
|
82
|
+
when 1 then false
|
|
83
|
+
when 2 then true
|
|
84
|
+
when 3
|
|
85
|
+
value = unsigned
|
|
86
|
+
(value >> 1) ^ -(value & 1)
|
|
87
|
+
when 4 then take(8).unpack1("G")
|
|
88
|
+
else
|
|
89
|
+
encoding = string_encoding(tag)
|
|
90
|
+
take(unsigned).force_encoding(encoding)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def skip_cell
|
|
95
|
+
tag = byte
|
|
96
|
+
case tag
|
|
97
|
+
when 0, 1, 2 then nil
|
|
98
|
+
when 3 then unsigned
|
|
99
|
+
when 4 then advance!(8)
|
|
100
|
+
else
|
|
101
|
+
string_encoding(tag)
|
|
102
|
+
advance!(unsigned)
|
|
103
|
+
end
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def string_encoding(tag)
|
|
107
|
+
case tag
|
|
108
|
+
when 5 then Encoding::UTF_8
|
|
109
|
+
when 6 then Encoding::US_ASCII
|
|
110
|
+
when 7 then Encoding::BINARY
|
|
111
|
+
when 8 then named_encoding
|
|
112
|
+
else raise InvalidValueError, "unknown Ruby page tag"
|
|
113
|
+
end
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
def named_encoding
|
|
117
|
+
length = unsigned
|
|
118
|
+
raise InvalidValueError, "invalid Ruby page encoding name" unless length.between?(1, 127)
|
|
119
|
+
|
|
120
|
+
name = take(length)
|
|
121
|
+
encoding = Encoding.find(name)
|
|
122
|
+
unless encoding && encoding.name == name && ![Encoding::UTF_8, Encoding::US_ASCII, Encoding::BINARY].include?(encoding)
|
|
123
|
+
raise InvalidValueError, "noncanonical Ruby page encoding"
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
encoding
|
|
127
|
+
rescue ArgumentError
|
|
128
|
+
raise InvalidValueError, "invalid Ruby page encoding"
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def unsigned
|
|
132
|
+
value = 0
|
|
133
|
+
(0..63).step(7) do |shift|
|
|
134
|
+
part = byte
|
|
135
|
+
raise InvalidValueError, "Ruby page integer overflow" if shift == 63 && part > 1
|
|
136
|
+
|
|
137
|
+
value |= (part & 127) << shift
|
|
138
|
+
next unless part < 128
|
|
139
|
+
|
|
140
|
+
raise InvalidValueError, "noncanonical Ruby page integer" if shift.positive? && part.zero?
|
|
141
|
+
|
|
142
|
+
return value
|
|
143
|
+
end
|
|
144
|
+
raise InvalidValueError, "Ruby page integer overflow"
|
|
145
|
+
end
|
|
146
|
+
|
|
147
|
+
def byte
|
|
148
|
+
value = @bytes.getbyte(@position)
|
|
149
|
+
raise InvalidValueError, "truncated Ruby page" unless value
|
|
150
|
+
|
|
151
|
+
@position += 1
|
|
152
|
+
value
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def take(length)
|
|
156
|
+
position = @position
|
|
157
|
+
advance!(length)
|
|
158
|
+
@bytes.byteslice(position, length) || raise(InvalidValueError, "truncated Ruby page")
|
|
159
|
+
end
|
|
160
|
+
|
|
161
|
+
def advance!(length)
|
|
162
|
+
raise InvalidValueError, "truncated Ruby page" if length > @bytes.bytesize - @position
|
|
163
|
+
|
|
164
|
+
@position += length
|
|
165
|
+
end
|
|
166
|
+
|
|
167
|
+
def finish!
|
|
168
|
+
raise InvalidValueError, "trailing Ruby page bytes" unless @position == @bytes.bytesize
|
|
169
|
+
end
|
|
170
|
+
end
|
|
171
|
+
end
|
|
172
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module XitDB
|
|
4
|
+
module Ruby
|
|
5
|
+
# Carries an edited row only until its parent publishes the operation.
|
|
6
|
+
# @api private
|
|
7
|
+
class PageDraft < Core::Slot
|
|
8
|
+
attr_reader :decoded
|
|
9
|
+
|
|
10
|
+
def initialize(decoded)
|
|
11
|
+
@decoded = decoded
|
|
12
|
+
super(tag: :record)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def ==(other)
|
|
16
|
+
equal?(other)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def to_bytes
|
|
20
|
+
raise InvalidValueError, "persist page draft before serialization"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module XitDB
|
|
4
|
+
module Ruby
|
|
5
|
+
# Builds one bounded page without retaining caller-owned values.
|
|
6
|
+
# @api private
|
|
7
|
+
class PageEncoder
|
|
8
|
+
def initialize(stored: false)
|
|
9
|
+
@stored = stored
|
|
10
|
+
@bytes = +"XP01".b
|
|
11
|
+
@keys = []
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def encode(rows)
|
|
15
|
+
return unless exact?(rows, ::Array)
|
|
16
|
+
|
|
17
|
+
# @type var rows: ::Array[top]
|
|
18
|
+
# @type var count: Integer
|
|
19
|
+
count = ::Array.instance_method(:size).bind_call(rows)
|
|
20
|
+
return unless count.between?(1, PageCodec::ROW_LIMIT)
|
|
21
|
+
|
|
22
|
+
rows = ::Array.instance_method(:map).bind_call(rows) { |row| row }
|
|
23
|
+
|
|
24
|
+
count = rows.size
|
|
25
|
+
return unless write_schema(rows.fetch(0), count)
|
|
26
|
+
|
|
27
|
+
count.times do |index|
|
|
28
|
+
row = rows[index]
|
|
29
|
+
return nil unless valid_row?(row)
|
|
30
|
+
|
|
31
|
+
# @type var row: Hash[String, top]
|
|
32
|
+
return nil unless encode_row(row)
|
|
33
|
+
end
|
|
34
|
+
@bytes if rows.size == count
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
def write_schema(first, count)
|
|
40
|
+
return false unless exact?(first, Hash)
|
|
41
|
+
|
|
42
|
+
# @type var first: Hash[String, top]
|
|
43
|
+
# @type var width: Integer
|
|
44
|
+
width = Hash.instance_method(:size).bind_call(first)
|
|
45
|
+
return false if width > PageCodec::FIELD_LIMIT
|
|
46
|
+
|
|
47
|
+
@keys = Hash.instance_method(:keys).bind_call(first)
|
|
48
|
+
@bytes << unsigned(count) << unsigned(@keys.size)
|
|
49
|
+
@keys.all? { |key| exact?(key, String) && append(cell(key)) }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
# Bound reflection prevents object hooks from running before validation.
|
|
53
|
+
def exact?(value, klass)
|
|
54
|
+
return false unless Object.instance_method(:instance_of?).bind_call(value, klass)
|
|
55
|
+
|
|
56
|
+
true
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def valid_row?(row)
|
|
60
|
+
return false unless exact?(row, Hash)
|
|
61
|
+
|
|
62
|
+
# @type var row: Hash[String, top]
|
|
63
|
+
# @type var width: Integer
|
|
64
|
+
width = Hash.instance_method(:size).bind_call(row)
|
|
65
|
+
return false unless width == @keys.size
|
|
66
|
+
return true if @stored
|
|
67
|
+
|
|
68
|
+
!Input.hash_defaults?(row)
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
# rubocop:disable-next Naming/PredicateMethod -- Appends a row on success.
|
|
72
|
+
def encode_row(row)
|
|
73
|
+
index = 0
|
|
74
|
+
Hash.instance_method(:each_pair).bind_call(row) do |key, value|
|
|
75
|
+
expected = @keys[index]
|
|
76
|
+
return false unless same_key?(key, expected) && append(cell(value))
|
|
77
|
+
|
|
78
|
+
index += 1
|
|
79
|
+
end
|
|
80
|
+
index == @keys.size
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def same_key?(key, expected)
|
|
84
|
+
return true if BasicObject.instance_method(:equal?).bind_call(key, expected)
|
|
85
|
+
return false unless expected && exact?(key, String)
|
|
86
|
+
|
|
87
|
+
# @type var key: String
|
|
88
|
+
# @type var encoding: Encoding
|
|
89
|
+
encoding = String.instance_method(:encoding).bind_call(key)
|
|
90
|
+
encoding == String.instance_method(:encoding).bind_call(expected) &&
|
|
91
|
+
String.instance_method(:==).bind_call(key, expected)
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
# rubocop:disable-next Naming/PredicateMethod -- Appends bytes on success.
|
|
95
|
+
def append(bytes)
|
|
96
|
+
return false unless bytes && bytes.bytesize <= PageCodec::PAGE_LIMIT - @bytes.bytesize
|
|
97
|
+
|
|
98
|
+
@bytes << bytes
|
|
99
|
+
true
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def cell(value)
|
|
103
|
+
case value
|
|
104
|
+
when nil then "\x00".b
|
|
105
|
+
when false then "\x01".b
|
|
106
|
+
when true then "\x02".b
|
|
107
|
+
when Integer then integer(value)
|
|
108
|
+
when Float then "\x04".b + [value].pack("G")
|
|
109
|
+
else text(value)
|
|
110
|
+
end
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def integer(value)
|
|
114
|
+
return unless value.between?(-(1 << 62), (1 << 62) - 1)
|
|
115
|
+
|
|
116
|
+
"\x03".b + unsigned(value.negative? ? (-value * 2) - 1 : value * 2)
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
def text(value)
|
|
120
|
+
return unless exact?(value, String)
|
|
121
|
+
|
|
122
|
+
# @type var value: String
|
|
123
|
+
# @type var length: Integer
|
|
124
|
+
length = String.instance_method(:bytesize).bind_call(value)
|
|
125
|
+
return if length > PageCodec::PAGE_LIMIT
|
|
126
|
+
|
|
127
|
+
value = Input.string(value)
|
|
128
|
+
|
|
129
|
+
encoding = value.encoding
|
|
130
|
+
prefix = case encoding
|
|
131
|
+
when Encoding::UTF_8 then "\x05".b
|
|
132
|
+
when Encoding::US_ASCII then "\x06".b
|
|
133
|
+
when Encoding::BINARY then "\x07".b
|
|
134
|
+
else "\x08".b + unsigned(encoding.name.bytesize) + encoding.name.b
|
|
135
|
+
end
|
|
136
|
+
prefix + unsigned(value.bytesize) + value.b
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def unsigned(value)
|
|
140
|
+
bytes = +"".b
|
|
141
|
+
while value > 127
|
|
142
|
+
bytes << ((value & 127) | 128)
|
|
143
|
+
value >>= 7
|
|
144
|
+
end
|
|
145
|
+
bytes << value
|
|
146
|
+
end
|
|
147
|
+
end
|
|
148
|
+
end
|
|
149
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module XitDB
|
|
4
|
+
module Ruby
|
|
5
|
+
# Carries a row location without retaining decoded values.
|
|
6
|
+
# @api private
|
|
7
|
+
class PageReference < Core::Slot
|
|
8
|
+
attr_reader :page, :index, :count
|
|
9
|
+
|
|
10
|
+
def initialize(page, index, count)
|
|
11
|
+
@page = page
|
|
12
|
+
@index = index
|
|
13
|
+
@count = count
|
|
14
|
+
super(tag: :record, value: page.value)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
def ==(other)
|
|
18
|
+
other.is_a?(PageReference) && page == other.page && index == other.index && count == other.count
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def to_bytes
|
|
22
|
+
raise InvalidValueError, "persist page reference before serialization"
|
|
23
|
+
end
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
end
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module XitDB
|
|
4
|
+
module Ruby
|
|
5
|
+
# Decodes a selected row lazily within one adapter operation.
|
|
6
|
+
# @api private
|
|
7
|
+
class PageRow < Containers
|
|
8
|
+
# rubocop:disable-next Lint/MissingSuper
|
|
9
|
+
def initialize(profile, cursor, context:, path:, reference:)
|
|
10
|
+
@profile = profile
|
|
11
|
+
@cursor = cursor
|
|
12
|
+
@context = context
|
|
13
|
+
@path = path
|
|
14
|
+
@kind = :map
|
|
15
|
+
@reference = reference
|
|
16
|
+
@row = nil
|
|
17
|
+
@fallback = nil
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def size
|
|
21
|
+
fallback = @fallback
|
|
22
|
+
fallback ? fallback.size : row.size
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def lookup(selector)
|
|
26
|
+
fallback = @fallback
|
|
27
|
+
return fallback.lookup(selector) if fallback
|
|
28
|
+
|
|
29
|
+
identity = key_bytes(selector)
|
|
30
|
+
key = row.each_key.find { |candidate| @profile.stored_key_bytes(candidate) == identity }
|
|
31
|
+
key ? PageCell.new(row.fetch(key)) : nil
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def each_entry(&)
|
|
35
|
+
return enum_for(:each_entry) unless block_given?
|
|
36
|
+
|
|
37
|
+
fallback = @fallback
|
|
38
|
+
if fallback
|
|
39
|
+
fallback.each_entry(&)
|
|
40
|
+
else
|
|
41
|
+
row.each_pair { |key, value| yield key, PageCell.new(value) }
|
|
42
|
+
end
|
|
43
|
+
self
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def materialize_flat
|
|
47
|
+
return if @fallback
|
|
48
|
+
|
|
49
|
+
row.transform_values { |value| value.is_a?(String) ? value.dup : value }
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def assign(selector, stored)
|
|
53
|
+
return self if replace_numeric(selector, stored)
|
|
54
|
+
|
|
55
|
+
promote.assign(selector, stored)
|
|
56
|
+
publish
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def delete(selector)
|
|
60
|
+
promote.delete(selector)
|
|
61
|
+
publish
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def clear
|
|
65
|
+
promote.clear
|
|
66
|
+
publish
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
private
|
|
70
|
+
|
|
71
|
+
# rubocop:disable-next Naming/PredicateMethod -- Changes the row and cursor on success.
|
|
72
|
+
def replace_numeric(selector, stored)
|
|
73
|
+
return false if @fallback || @path.empty? || stored.full? || !%i[none int uint float].include?(stored.tag)
|
|
74
|
+
|
|
75
|
+
identity = key_bytes(selector)
|
|
76
|
+
key = row.each_key.find { |candidate| @profile.stored_key_bytes(candidate) == identity }
|
|
77
|
+
return false unless key
|
|
78
|
+
|
|
79
|
+
value = @profile.decode(stored, context: @context, path: @path, immutable: false)
|
|
80
|
+
case value
|
|
81
|
+
when NilClass, Integer, Float then row[key] = value
|
|
82
|
+
else return false
|
|
83
|
+
end
|
|
84
|
+
cursor.replace(PageDraft.new(row))
|
|
85
|
+
true
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
def row
|
|
89
|
+
@row ||= PageCodec.decode_row(Pages.bytes(@reference.page, @context), @reference.index, @reference.count)
|
|
90
|
+
end
|
|
91
|
+
|
|
92
|
+
def promote
|
|
93
|
+
fallback = @fallback
|
|
94
|
+
return fallback if fallback
|
|
95
|
+
|
|
96
|
+
output = PackedMap.new(@profile, @context.cursor(Core::Slot.new), context: @context, path: @path,
|
|
97
|
+
kind: :map, defer: true)
|
|
98
|
+
row.each_pair { |key, value| output.assign(key, PageCell.new(value)) }
|
|
99
|
+
@fallback = output
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def publish
|
|
103
|
+
cursor.replace(promote.slot)
|
|
104
|
+
self
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
end
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module XitDB
|
|
4
|
+
module Ruby
|
|
5
|
+
# Stores bounded flat-row pages and persists transient row descriptors.
|
|
6
|
+
# @api private
|
|
7
|
+
module Pages
|
|
8
|
+
MIN_ROWS = 32
|
|
9
|
+
ROW_LIMIT = 1024
|
|
10
|
+
BYTE_LIMIT = 65_536
|
|
11
|
+
ARRAY = 7
|
|
12
|
+
ROW = 8
|
|
13
|
+
|
|
14
|
+
def self.encode(input, context:)
|
|
15
|
+
klass = Object.instance_method(:class).bind_call(input)
|
|
16
|
+
return unless BasicObject.instance_method(:equal?).bind_call(klass, ::Array)
|
|
17
|
+
|
|
18
|
+
# @type var input: ::Array[top]
|
|
19
|
+
# @type var count: Integer
|
|
20
|
+
count = ::Array.instance_method(:size).bind_call(input)
|
|
21
|
+
return if count < MIN_ROWS
|
|
22
|
+
|
|
23
|
+
transaction = context.writable_transaction!
|
|
24
|
+
checkpoint = transaction.checkpoint
|
|
25
|
+
completed = false
|
|
26
|
+
begin
|
|
27
|
+
builder = Core::ArrayBuilder.new(transaction)
|
|
28
|
+
(0...count).step(ROW_LIMIT) do |index|
|
|
29
|
+
length = [ROW_LIMIT, count - index].min
|
|
30
|
+
# @type var rows: ::Array[top]?
|
|
31
|
+
rows = ::Array.instance_method(:slice).bind_call(input, index, length)
|
|
32
|
+
return nil unless rows && rows.size == length
|
|
33
|
+
|
|
34
|
+
bytes = PageCodec.encode_page(rows)
|
|
35
|
+
return nil unless bytes
|
|
36
|
+
|
|
37
|
+
builder.append(context.database.core.bytes(bytes))
|
|
38
|
+
end
|
|
39
|
+
return nil unless count == ::Array.instance_method(:size).bind_call(input)
|
|
40
|
+
|
|
41
|
+
stored = Core::Record.append(transaction, metadata: ARRAY,
|
|
42
|
+
fields: [Core::Slot.uint(count), builder.slot])
|
|
43
|
+
completed = true
|
|
44
|
+
stored
|
|
45
|
+
ensure
|
|
46
|
+
completed ? transaction.release_checkpoint(checkpoint) : transaction.restore(checkpoint)
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def self.persist(slot, context)
|
|
51
|
+
case slot
|
|
52
|
+
when PageReference
|
|
53
|
+
Core::Record.append(context.writable_transaction!, metadata: ROW,
|
|
54
|
+
fields: [slot.page, Core::Slot.uint(slot.index),
|
|
55
|
+
Core::Slot.uint(slot.count)])
|
|
56
|
+
when PageCell
|
|
57
|
+
context.profile.encode_scalar(slot.decoded, core: context.database.core)
|
|
58
|
+
when PageDraft
|
|
59
|
+
context.profile.encode(slot.decoded, context: context, path: [])
|
|
60
|
+
else slot
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def self.bytes(slot, context)
|
|
65
|
+
unless %i[bytes short_bytes].include?(slot.tag) && !slot.full?
|
|
66
|
+
raise InvalidValueError, "invalid Ruby page byte descriptor"
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
source = Core::Bytes.open(context.database.core, slot, bound: context.read_bound)
|
|
70
|
+
raise InvalidValueError, "Ruby page exceeds byte limit" if source.size > BYTE_LIMIT
|
|
71
|
+
|
|
72
|
+
source.read(0, source.size)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def self.reference(record)
|
|
76
|
+
fields = record.fields
|
|
77
|
+
unless record.metadata == ROW && fields.size == 3 && fields.none?(&:full?) &&
|
|
78
|
+
%i[bytes short_bytes].include?(fields.fetch(0).tag) &&
|
|
79
|
+
fields.fetch(1).tag == :uint && fields.fetch(2).tag == :uint
|
|
80
|
+
raise InvalidValueError, "invalid Ruby page row descriptor"
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
page = fields.fetch(0)
|
|
84
|
+
index = fields.fetch(1)
|
|
85
|
+
count = fields.fetch(2)
|
|
86
|
+
unless count.value.between?(1, ROW_LIMIT) && index.value < count.value
|
|
87
|
+
raise InvalidValueError, "invalid Ruby page row bounds"
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
PageReference.new(page, index.value, count.value)
|
|
91
|
+
end
|
|
92
|
+
end
|
|
93
|
+
end
|
|
94
|
+
end
|