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/core/format.rb
CHANGED
|
@@ -2,22 +2,44 @@
|
|
|
2
2
|
|
|
3
3
|
module XitDB
|
|
4
4
|
module Core
|
|
5
|
-
#
|
|
5
|
+
# Binary decoding checks shared by the storage implementation.
|
|
6
|
+
#
|
|
7
|
+
# A byte offset counts from the start of the file. All stored integers use explicit binary widths.
|
|
8
|
+
# @api private
|
|
6
9
|
module Format
|
|
10
|
+
# Largest integer that fits in an unsigned 64-bit field.
|
|
7
11
|
UINT64_MAX = (1 << 64) - 1
|
|
8
12
|
|
|
9
|
-
|
|
13
|
+
# Checks that an integer is inside the allowed range.
|
|
14
|
+
# @param value [Object] value to validate as an Integer
|
|
15
|
+
# @param range [Range<Integer>] allowed integer values
|
|
16
|
+
# @return [Integer]
|
|
17
|
+
# @raise [TypeError] if value is not an Integer
|
|
18
|
+
# @raise [RangeError] if value is outside range
|
|
19
|
+
def self.integer!(value, range)
|
|
10
20
|
raise TypeError, "expected Integer, got #{value.class}" unless value.is_a?(Integer)
|
|
11
21
|
raise RangeError, "integer outside #{range}" unless range.cover?(value)
|
|
12
22
|
|
|
13
23
|
value
|
|
14
24
|
end
|
|
15
25
|
|
|
16
|
-
|
|
26
|
+
# Checks that a byte string has exactly the required width.
|
|
27
|
+
# @param bytes [String] binary input bytes
|
|
28
|
+
# @param width [Integer] required byte count
|
|
29
|
+
# @return [void]
|
|
30
|
+
# @raise [TypeError] if bytes is not a String
|
|
31
|
+
# @raise [CorruptionError] if the width differs
|
|
32
|
+
def self.ensure_width!(bytes, width)
|
|
17
33
|
raise TypeError, "expected String, got #{bytes.class}" unless bytes.is_a?(String)
|
|
18
34
|
raise CorruptionError, "expected #{width} bytes, got #{bytes.bytesize}" unless bytes.bytesize == width
|
|
19
35
|
end
|
|
20
36
|
|
|
37
|
+
# Reads a byte range and rejects a missing slice.
|
|
38
|
+
# @param bytes [String] binary input to slice
|
|
39
|
+
# @param offset [Integer, Range] byte index or range within bytes
|
|
40
|
+
# @param length [Integer, nil] byte count; nil selects one byte for an integer offset
|
|
41
|
+
# @return [String]
|
|
42
|
+
# @raise [CorruptionError] if the slice starts outside the payload
|
|
21
43
|
def self.slice(bytes, offset, length = nil)
|
|
22
44
|
result = if offset.is_a?(Range)
|
|
23
45
|
bytes.byteslice(offset)
|
|
@@ -29,6 +51,9 @@ module XitDB
|
|
|
29
51
|
result
|
|
30
52
|
end
|
|
31
53
|
|
|
54
|
+
# Decodes an unsigned 64-bit integer in big-endian order.
|
|
55
|
+
# @param bytes [String] binary input bytes
|
|
56
|
+
# @return [Integer]
|
|
32
57
|
def self.unsigned(bytes)
|
|
33
58
|
value = bytes.unpack1("Q>")
|
|
34
59
|
raise CorruptionError, "invalid unsigned integer" unless value.is_a?(Integer)
|
|
@@ -36,6 +61,10 @@ module XitDB
|
|
|
36
61
|
value
|
|
37
62
|
end
|
|
38
63
|
|
|
64
|
+
# Decodes packed integers and rejects incomplete values.
|
|
65
|
+
# @param bytes [String] binary input bytes
|
|
66
|
+
# @param format [String] Ruby unpack format for the packed integers
|
|
67
|
+
# @return [Array<Integer>]
|
|
39
68
|
def self.integers(bytes, format)
|
|
40
69
|
bytes.unpack(format).map do |value|
|
|
41
70
|
raise CorruptionError, "invalid integer payload" unless value.is_a?(Integer)
|
|
@@ -44,105 +73,17 @@ module XitDB
|
|
|
44
73
|
end
|
|
45
74
|
end
|
|
46
75
|
|
|
76
|
+
# Decodes the first two integers in a packed record.
|
|
77
|
+
# @param bytes [String] binary input bytes
|
|
78
|
+
# @param format [String] Ruby unpack format for the packed integers
|
|
79
|
+
# @return [Array(Integer, Integer)]
|
|
47
80
|
def self.pair(bytes, format)
|
|
48
81
|
values = integers(bytes, format)
|
|
49
82
|
[values.fetch(0), values.fetch(1)]
|
|
50
83
|
end
|
|
51
84
|
end
|
|
52
|
-
|
|
53
|
-
# The immutable database format header.
|
|
54
|
-
class Header
|
|
55
|
-
SIZE = 20
|
|
56
|
-
VERSION = 1
|
|
57
|
-
|
|
58
|
-
attr_reader :file_id, :hash_id, :hash_size
|
|
59
|
-
|
|
60
|
-
def initialize(file_id:, hash_id:, hash_size:, padding: 0)
|
|
61
|
-
@file_id = Format.integer(file_id, 0..Format::UINT64_MAX)
|
|
62
|
-
@hash_size = Format.integer(hash_size, 0..65_535)
|
|
63
|
-
raise TypeError, "hash_id must be a String" unless hash_id.is_a?(String)
|
|
64
|
-
raise ArgumentError, "hash_id must contain four bytes" unless hash_id.bytesize == 4
|
|
65
|
-
|
|
66
|
-
@hash_id = hash_id.b.freeze
|
|
67
|
-
@padding = Format.integer(padding, 0..255)
|
|
68
|
-
freeze
|
|
69
|
-
end
|
|
70
|
-
|
|
71
|
-
def self.parse(bytes)
|
|
72
|
-
Format.width(bytes, SIZE)
|
|
73
|
-
magic = Format.slice(bytes, 0, 3)
|
|
74
|
-
padding = bytes.getbyte(3) || raise(CorruptionError, "missing header padding")
|
|
75
|
-
version, hash_size = Format.pair(Format.slice(bytes, 4, 4), "nn")
|
|
76
|
-
hash_id = Format.slice(bytes, 8, 4)
|
|
77
|
-
file_id = Format.unsigned(Format.slice(bytes, 12, 8))
|
|
78
|
-
raise CorruptionError, "invalid xitdb magic number" unless magic == "xit"
|
|
79
|
-
raise FormatVersionError, "unsupported xitdb format version #{version}" unless version == VERSION
|
|
80
|
-
|
|
81
|
-
new(file_id: file_id, hash_id: hash_id, hash_size: hash_size, padding: padding)
|
|
82
|
-
end
|
|
83
|
-
|
|
84
|
-
def to_bytes
|
|
85
|
-
["xit", @padding, VERSION, hash_size, hash_id, file_id].pack("a3Cnn a4Q>")
|
|
86
|
-
end
|
|
87
|
-
end
|
|
88
|
-
|
|
89
|
-
# An immutable tagged value or record reference.
|
|
90
|
-
class Slot
|
|
91
|
-
SIZE = 9
|
|
92
|
-
TAGS = %i[none index array_list linked_array_list hash_map kv_pair bytes short_bytes uint int float
|
|
93
|
-
hash_set counted_hash_map counted_hash_set sorted_map sorted_set sorted_kv_pair].freeze
|
|
94
|
-
|
|
95
|
-
attr_reader :tag, :value, :full
|
|
96
|
-
|
|
97
|
-
def initialize(tag: :none, value: 0, full: false)
|
|
98
|
-
code = tag.is_a?(Symbol) ? TAGS.index(tag) : tag
|
|
99
|
-
raise ArgumentError, "invalid slot tag: #{tag.inspect}" unless code.is_a?(Integer) && (0..127).cover?(code)
|
|
100
|
-
raise TypeError, "full must be true or false" unless full.equal?(true) || full.equal?(false)
|
|
101
|
-
|
|
102
|
-
@tag = TAGS[code] || code
|
|
103
|
-
@value = Format.integer(value, 0..Format::UINT64_MAX)
|
|
104
|
-
@full = full
|
|
105
|
-
freeze
|
|
106
|
-
end
|
|
107
|
-
|
|
108
|
-
def self.parse(bytes)
|
|
109
|
-
Format.width(bytes, SIZE)
|
|
110
|
-
flags = bytes.getbyte(0) || raise(CorruptionError, "missing slot flags")
|
|
111
|
-
value = Format.unsigned(Format.slice(bytes, 1, 8))
|
|
112
|
-
new(tag: flags & 0x7f, value: value, full: flags.anybits?(0x80))
|
|
113
|
-
end
|
|
114
|
-
|
|
115
|
-
def self.int(value)
|
|
116
|
-
Format.integer(value, -(1 << 63)..((1 << 63) - 1))
|
|
117
|
-
new(tag: :int, value: value & Format::UINT64_MAX)
|
|
118
|
-
end
|
|
119
|
-
|
|
120
|
-
def self.uint(value)
|
|
121
|
-
new(tag: :uint, value: value)
|
|
122
|
-
end
|
|
123
|
-
|
|
124
|
-
def self.float(value)
|
|
125
|
-
raise TypeError, "expected Float, got #{value.class}" unless value.is_a?(Float)
|
|
126
|
-
|
|
127
|
-
new(tag: :float, value: Format.unsigned([value].pack("G")))
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
def self.null
|
|
131
|
-
new(full: true)
|
|
132
|
-
end
|
|
133
|
-
|
|
134
|
-
def empty?
|
|
135
|
-
tag == :none && !full
|
|
136
|
-
end
|
|
137
|
-
|
|
138
|
-
def ==(other)
|
|
139
|
-
other.is_a?(Slot) && tag == other.tag && full == other.full && value == other.value
|
|
140
|
-
end
|
|
141
|
-
|
|
142
|
-
def to_bytes
|
|
143
|
-
code = tag.is_a?(Symbol) ? TAGS.index(tag) : tag
|
|
144
|
-
[code | (full ? 0x80 : 0), value].pack("CQ>")
|
|
145
|
-
end
|
|
146
|
-
end
|
|
147
85
|
end
|
|
148
86
|
end
|
|
87
|
+
|
|
88
|
+
require_relative "header"
|
|
89
|
+
require_relative "slot"
|
data/lib/xitdb/core/hash_map.rb
CHANGED
|
@@ -2,14 +2,31 @@
|
|
|
2
2
|
|
|
3
3
|
module XitDB
|
|
4
4
|
module Core
|
|
5
|
-
# A
|
|
5
|
+
# A map indexed by complete binary digests supplied by its caller.
|
|
6
|
+
#
|
|
7
|
+
# The original key and value are stored as slots. The digest, not the key slot, determines entry identity.
|
|
8
|
+
#
|
|
9
|
+
# Branches select four digest bits at a time. Counted variants store size.
|
|
10
|
+
# Other variants count entries by traversal.
|
|
6
11
|
class HashMap
|
|
12
|
+
# Bytes in one digest branch containing sixteen slots.
|
|
7
13
|
BLOCK_SIZE = 16 * Slot::SIZE
|
|
14
|
+
# A zero-filled digest branch with no entries.
|
|
8
15
|
EMPTY_BLOCK = ("\0".b * BLOCK_SIZE).freeze
|
|
16
|
+
# Storage tags supported by the hash map and set reader.
|
|
9
17
|
TAGS = %i[hash_map hash_set counted_hash_map counted_hash_set].freeze
|
|
10
18
|
|
|
19
|
+
# @!attribute [r] cursor
|
|
20
|
+
# View locating this collection.
|
|
21
|
+
# @return [Cursor]
|
|
11
22
|
attr_reader :cursor
|
|
12
23
|
|
|
24
|
+
# Opens a digest-indexed map or set, optionally with a stored entry count.
|
|
25
|
+
# @param cursor [Cursor] view locating this collection or value
|
|
26
|
+
# @param counted [Boolean] whether to store the entry count with the root
|
|
27
|
+
# @param set [Boolean] whether a new collection stores keys without values
|
|
28
|
+
# @return [HashMap]
|
|
29
|
+
# @raise [InvalidValueError] if the cursor holds an incompatible collection tag
|
|
13
30
|
def initialize(cursor, counted: false, set: false)
|
|
14
31
|
@cursor = cursor
|
|
15
32
|
@tag = collection_tag(counted, set)
|
|
@@ -24,22 +41,36 @@ module XitDB
|
|
|
24
41
|
mutate { |transaction| publish(writable_root(transaction)) }
|
|
25
42
|
end
|
|
26
43
|
|
|
44
|
+
# Opens the map or set variant identified by a cursor's slot tag.
|
|
45
|
+
# @param cursor [Cursor] view locating this collection or value
|
|
46
|
+
# @return [HashMap]
|
|
27
47
|
def self.from_cursor(cursor)
|
|
28
48
|
new(cursor, counted: %i[counted_hash_map counted_hash_set].include?(cursor.slot.tag))
|
|
29
49
|
end
|
|
30
50
|
|
|
51
|
+
# Reads the value stored for an exact digest.
|
|
52
|
+
# @param hash_bytes [String] precomputed binary digest with the database header hash width
|
|
53
|
+
# @return [Slot, nil]
|
|
31
54
|
def get(hash_bytes)
|
|
32
55
|
entry(hash_bytes)&.last
|
|
33
56
|
end
|
|
34
57
|
|
|
58
|
+
# Reads the original key slot stored for an exact digest.
|
|
59
|
+
# @param hash_bytes [String] precomputed binary digest with the database header hash width
|
|
60
|
+
# @return [Slot, nil]
|
|
35
61
|
def key(hash_bytes)
|
|
36
62
|
entry(hash_bytes)&.first
|
|
37
63
|
end
|
|
38
64
|
|
|
65
|
+
# Tests whether an entry with this digest exists.
|
|
66
|
+
# @param hash_bytes [String] precomputed binary digest with the database header hash width
|
|
67
|
+
# @return [Boolean]
|
|
39
68
|
def include?(hash_bytes)
|
|
40
69
|
!entry(hash_bytes).nil?
|
|
41
70
|
end
|
|
42
71
|
|
|
72
|
+
# Returns the stored count for counted collections, or visits entries to count them.
|
|
73
|
+
# @return [Integer]
|
|
43
74
|
def size
|
|
44
75
|
start = root_pointer
|
|
45
76
|
return 0 unless start
|
|
@@ -47,23 +78,38 @@ module XitDB
|
|
|
47
78
|
counted? ? Format.unsigned(read(start, 8)) : each_entry.count
|
|
48
79
|
end
|
|
49
80
|
|
|
81
|
+
# Stores a value by digest, preserving an already stored nonempty key.
|
|
82
|
+
# @param hash_bytes [String] precomputed binary digest with the database header hash width
|
|
83
|
+
# @param key [Slot] original key slot retained with the digest
|
|
84
|
+
# @param value [Slot] value slot to store
|
|
85
|
+
# @return [self]
|
|
86
|
+
# @raise [ReadOnlyError] if the collection has no writable transaction
|
|
50
87
|
def put(hash_bytes, key:, value:)
|
|
51
|
-
hash_bytes = validate_hash(hash_bytes)
|
|
52
|
-
validate_slot(key)
|
|
53
|
-
validate_slot(value)
|
|
88
|
+
hash_bytes = validate_hash!(hash_bytes)
|
|
89
|
+
validate_slot!(key)
|
|
90
|
+
validate_slot!(value)
|
|
54
91
|
write_entry(hash_bytes, key: key, value: value)
|
|
55
92
|
self
|
|
56
93
|
end
|
|
57
94
|
|
|
95
|
+
# Adds a key by digest without replacing an existing value.
|
|
96
|
+
# @param hash_bytes [String] precomputed binary digest with the database header hash width
|
|
97
|
+
# @param key [Slot] original key slot retained with the digest
|
|
98
|
+
# @return [self]
|
|
99
|
+
# @raise [ReadOnlyError] if the collection has no writable transaction
|
|
58
100
|
def add(hash_bytes, key:)
|
|
59
|
-
hash_bytes = validate_hash(hash_bytes)
|
|
60
|
-
validate_slot(key)
|
|
101
|
+
hash_bytes = validate_hash!(hash_bytes)
|
|
102
|
+
validate_slot!(key)
|
|
61
103
|
write_entry(hash_bytes, key: key, value: nil)
|
|
62
104
|
self
|
|
63
105
|
end
|
|
64
106
|
|
|
107
|
+
# Finds a value view, optionally creating empty key and value slots.
|
|
108
|
+
# @param hash_bytes [String] precomputed binary digest with the database header hash width
|
|
109
|
+
# @param create [Boolean] whether to create an empty element when the requested position is absent
|
|
110
|
+
# @return [Cursor, nil]
|
|
65
111
|
def cursor_at(hash_bytes, create: false)
|
|
66
|
-
hash_bytes = validate_hash(hash_bytes)
|
|
112
|
+
hash_bytes = validate_hash!(hash_bytes)
|
|
67
113
|
value = get(hash_bytes)
|
|
68
114
|
if value.nil? && create
|
|
69
115
|
put(hash_bytes, key: Slot.new, value: Slot.new)
|
|
@@ -74,17 +120,30 @@ module XitDB
|
|
|
74
120
|
cursor.hash_child(hash_bytes.freeze, value)
|
|
75
121
|
end
|
|
76
122
|
|
|
123
|
+
# Replaces the value of an existing digest entry.
|
|
124
|
+
# @param hash_bytes [String] precomputed binary digest with the database header hash width
|
|
125
|
+
# @param slot [Slot] stored value or record descriptor
|
|
126
|
+
# @param owner [Cursor, nil] child view to preserve during replacement
|
|
127
|
+
# @param collection [Boolean] whether this is an internal copied collection update
|
|
128
|
+
# @param propagate [Boolean] whether to update parent collection paths immediately
|
|
129
|
+
# @return [self]
|
|
130
|
+
# @raise [ReadOnlyError] if the collection has no writable transaction
|
|
131
|
+
# @api private
|
|
77
132
|
def replace_child(hash_bytes, slot, owner: nil, collection: false, propagate: true)
|
|
78
|
-
hash_bytes = validate_hash(hash_bytes)
|
|
79
|
-
validate_slot(slot)
|
|
133
|
+
hash_bytes = validate_hash!(hash_bytes)
|
|
134
|
+
validate_slot!(slot)
|
|
80
135
|
raise ExpiredViewError, "hash entry was removed" unless include?(hash_bytes)
|
|
81
136
|
|
|
82
137
|
write_entry(hash_bytes, key: nil, value: slot, owner: owner, collection: collection, propagate: propagate)
|
|
83
138
|
self
|
|
84
139
|
end
|
|
85
140
|
|
|
141
|
+
# Removes a digest entry and invalidates its value views.
|
|
142
|
+
# @param hash_bytes [String] precomputed binary digest with the database header hash width
|
|
143
|
+
# @return [Boolean]
|
|
144
|
+
# @raise [ReadOnlyError] if the collection has no writable transaction
|
|
86
145
|
def remove(hash_bytes) # rubocop:disable Naming/PredicateMethod -- This public method removes an entry.
|
|
87
|
-
hash_bytes = validate_hash(hash_bytes)
|
|
146
|
+
hash_bytes = validate_hash!(hash_bytes)
|
|
88
147
|
writable!
|
|
89
148
|
return false unless include?(hash_bytes)
|
|
90
149
|
|
|
@@ -112,6 +171,14 @@ module XitDB
|
|
|
112
171
|
true
|
|
113
172
|
end
|
|
114
173
|
|
|
174
|
+
# Visits each stored digest, original key slot, and value slot.
|
|
175
|
+
# @return [self, Enumerator<Array>]
|
|
176
|
+
# @yield [hash_bytes, key, value] receives one stored entry
|
|
177
|
+
# @yieldparam hash_bytes [String] stored binary digest
|
|
178
|
+
# @yieldparam key [Slot] original key
|
|
179
|
+
# @yieldparam value [Slot] stored value
|
|
180
|
+
# @yieldreturn [Object] ignored
|
|
181
|
+
# @note Without a block, returns an Enumerator.
|
|
115
182
|
def each_entry
|
|
116
183
|
return enum_for(:each_entry) unless block_given?
|
|
117
184
|
|
|
@@ -145,6 +212,7 @@ module XitDB
|
|
|
145
212
|
|
|
146
213
|
private
|
|
147
214
|
|
|
215
|
+
# Selects the storage tag for counted and key-only variants.
|
|
148
216
|
def collection_tag(counted, set)
|
|
149
217
|
if counted
|
|
150
218
|
set ? :counted_hash_set : :counted_hash_map
|
|
@@ -153,10 +221,12 @@ module XitDB
|
|
|
153
221
|
end
|
|
154
222
|
end
|
|
155
223
|
|
|
224
|
+
# Checks whether a branch can collapse into its only remaining leaf.
|
|
156
225
|
def collapsible?(pointer, start, entries)
|
|
157
226
|
pointer != start + count_bytes && entries.size <= 1 && entries.none? { |slot| slot.tag == :index }
|
|
158
227
|
end
|
|
159
228
|
|
|
229
|
+
# Removes an entry and copies or collapses ancestor blocks.
|
|
160
230
|
def collapse_path(transaction, start, path)
|
|
161
231
|
replacement = Slot.new
|
|
162
232
|
path.reverse_each do |old_pointer, block, index|
|
|
@@ -172,35 +242,42 @@ module XitDB
|
|
|
172
242
|
end
|
|
173
243
|
end
|
|
174
244
|
|
|
245
|
+
# Tests whether a tag includes a stored entry count.
|
|
175
246
|
def counted_tag?(tag)
|
|
176
247
|
%i[counted_hash_map counted_hash_set].include?(tag)
|
|
177
248
|
end
|
|
178
249
|
|
|
250
|
+
# Tests whether this collection stores its entry count.
|
|
179
251
|
def counted?
|
|
180
252
|
counted_tag?(@tag)
|
|
181
253
|
end
|
|
182
254
|
|
|
255
|
+
# Returns the width of the optional count prefix.
|
|
183
256
|
def count_bytes
|
|
184
257
|
counted? ? 8 : 0
|
|
185
258
|
end
|
|
186
259
|
|
|
260
|
+
# Reads the digest width declared by the database header.
|
|
187
261
|
def hash_size
|
|
188
262
|
cursor.database.header.hash_size
|
|
189
263
|
end
|
|
190
264
|
|
|
191
|
-
|
|
265
|
+
# Checks that a binary digest has the database's declared nonzero width.
|
|
266
|
+
def validate_hash!(hash)
|
|
192
267
|
raise TypeError, "hash must be a String" unless hash.is_a?(String)
|
|
193
268
|
raise ArgumentError, "hash has incorrect width" unless hash.bytesize == hash_size && hash_size.positive?
|
|
194
269
|
|
|
195
270
|
hash.b
|
|
196
271
|
end
|
|
197
272
|
|
|
198
|
-
|
|
273
|
+
# Rejects unknown slots and internal branch pointers as keys or values.
|
|
274
|
+
def validate_slot!(slot)
|
|
199
275
|
raise TypeError, "expected Slot" unless slot.is_a?(Slot)
|
|
200
276
|
raise CorruptionError, "unknown hash record slot tag" unless Slot::TAGS.include?(slot.tag)
|
|
201
277
|
raise InvalidValueError, "index slot is internal to hash branches" if slot.tag == :index
|
|
202
278
|
end
|
|
203
279
|
|
|
280
|
+
# Selects four digest bits, starting at the low bits of the final byte.
|
|
204
281
|
def nibble(hash, depth)
|
|
205
282
|
raise CorruptionError, "hash trie exceeds hash width" if depth >= hash_size * 2
|
|
206
283
|
|
|
@@ -210,6 +287,7 @@ module XitDB
|
|
|
210
287
|
(byte >> (depth % 2 * 4)) & 15
|
|
211
288
|
end
|
|
212
289
|
|
|
290
|
+
# Validates the collection tag and reads its root block.
|
|
213
291
|
def root_pointer
|
|
214
292
|
slot = cursor.slot
|
|
215
293
|
return nil if slot.tag == :none
|
|
@@ -219,10 +297,12 @@ module XitDB
|
|
|
219
297
|
slot.value
|
|
220
298
|
end
|
|
221
299
|
|
|
300
|
+
# Reads bytes within the cursor's storage boundary.
|
|
222
301
|
def read(offset, length)
|
|
223
302
|
cursor.database.read_at(offset, length, bound: cursor.bound)
|
|
224
303
|
end
|
|
225
304
|
|
|
305
|
+
# Decodes a trie slot and rejects unknown tags.
|
|
226
306
|
def parse_slot(bytes)
|
|
227
307
|
slot = Slot.parse(bytes)
|
|
228
308
|
raise CorruptionError, "unknown hash slot tag" unless Slot::TAGS.include?(slot.tag)
|
|
@@ -230,6 +310,7 @@ module XitDB
|
|
|
230
310
|
slot
|
|
231
311
|
end
|
|
232
312
|
|
|
313
|
+
# Reads a digest and its original key and value slots.
|
|
233
314
|
def read_record(slot)
|
|
234
315
|
bytes = read(slot.value, hash_size + (Slot::SIZE * 2))
|
|
235
316
|
key = parse_slot(Format.slice(bytes, hash_size, Slot::SIZE))
|
|
@@ -239,8 +320,9 @@ module XitDB
|
|
|
239
320
|
[Format.slice(bytes, 0, hash_size).freeze, key, value]
|
|
240
321
|
end
|
|
241
322
|
|
|
323
|
+
# Walks digest branches and checks the full stored digest at a leaf.
|
|
242
324
|
def entry(hash)
|
|
243
|
-
hash = validate_hash(hash)
|
|
325
|
+
hash = validate_hash!(hash)
|
|
244
326
|
start = root_pointer
|
|
245
327
|
return nil unless start
|
|
246
328
|
|
|
@@ -262,11 +344,13 @@ module XitDB
|
|
|
262
344
|
end
|
|
263
345
|
end
|
|
264
346
|
|
|
347
|
+
# Checks cursor validity and requires a writable transaction.
|
|
265
348
|
def writable!
|
|
266
349
|
cursor.slot
|
|
267
350
|
raise ReadOnlyError, "hash collection is read-only" unless cursor.transaction
|
|
268
351
|
end
|
|
269
352
|
|
|
353
|
+
# Restores a checkpoint when any part of a trie update fails.
|
|
270
354
|
def mutate
|
|
271
355
|
writable!
|
|
272
356
|
transaction = cursor.transaction
|
|
@@ -283,6 +367,7 @@ module XitDB
|
|
|
283
367
|
end
|
|
284
368
|
end
|
|
285
369
|
|
|
370
|
+
# Allocates an empty root or copies one below the writable boundary.
|
|
286
371
|
def writable_root(transaction)
|
|
287
372
|
start = root_pointer
|
|
288
373
|
return transaction.append(("\0".b * count_bytes) + EMPTY_BLOCK) unless start
|
|
@@ -291,18 +376,21 @@ module XitDB
|
|
|
291
376
|
start < transaction.mutable_start ? transaction.append(bytes) : start
|
|
292
377
|
end
|
|
293
378
|
|
|
379
|
+
# Updates the collection cursor to the new root pointer.
|
|
294
380
|
def publish(start, propagate: true)
|
|
295
381
|
cursor.update_collection(Slot.new(tag: @tag, value: start), propagate: propagate)
|
|
296
382
|
end
|
|
297
383
|
|
|
384
|
+
# Changes the stored count only for counted collections.
|
|
298
385
|
def adjust_count(transaction, start, delta)
|
|
299
386
|
return unless counted?
|
|
300
387
|
|
|
301
388
|
count = Format.unsigned(read(start, 8)) + delta
|
|
302
|
-
Format.integer(count, 0..Format::UINT64_MAX)
|
|
389
|
+
Format.integer!(count, 0..Format::UINT64_MAX)
|
|
303
390
|
transaction.write_at(start, [count].pack("Q>"))
|
|
304
391
|
end
|
|
305
392
|
|
|
393
|
+
# Copies the digest path and writes or replaces its leaf record.
|
|
306
394
|
def write_entry(hash, key:, value:, owner: nil, collection: false, propagate: true)
|
|
307
395
|
mutate do |transaction|
|
|
308
396
|
start = writable_root(transaction)
|
|
@@ -329,6 +417,7 @@ module XitDB
|
|
|
329
417
|
end
|
|
330
418
|
end
|
|
331
419
|
|
|
420
|
+
# Stores a matching digest or adds a branch when digests share a prefix.
|
|
332
421
|
def write_leaf(transaction, position, slot, hash, depth, start, key, value)
|
|
333
422
|
stored_hash, stored_key, stored_value = slot.empty? ? [nil, Slot.new, Slot.new] : read_record(slot)
|
|
334
423
|
if stored_hash.nil? || stored_hash == hash
|
|
@@ -347,22 +436,35 @@ module XitDB
|
|
|
347
436
|
end
|
|
348
437
|
end
|
|
349
438
|
|
|
350
|
-
# A
|
|
439
|
+
# A digest-indexed collection that stores original key slots without requiring values.
|
|
351
440
|
class HashSet < HashMap
|
|
441
|
+
# Opens a key-only hash collection, optionally with a stored count.
|
|
442
|
+
# @param cursor [Cursor] view locating this collection or value
|
|
443
|
+
# @param counted [Boolean] whether to store the entry count with the root
|
|
444
|
+
# @return [HashSet]
|
|
445
|
+
# @raise [InvalidValueError] if the cursor holds an incompatible collection tag
|
|
352
446
|
def initialize(cursor, counted: false)
|
|
353
447
|
super(cursor, counted: counted, set: true)
|
|
354
448
|
end
|
|
355
449
|
end
|
|
356
450
|
|
|
357
|
-
# A hash
|
|
451
|
+
# A hash map with a stored entry count for constant-time size reads.
|
|
358
452
|
class CountedHashMap < HashMap
|
|
453
|
+
# Opens a hash map whose size is stored in its root.
|
|
454
|
+
# @param cursor [Cursor] view locating this collection or value
|
|
455
|
+
# @return [CountedHashMap]
|
|
456
|
+
# @raise [InvalidValueError] if the cursor holds an incompatible collection tag
|
|
359
457
|
def initialize(cursor)
|
|
360
458
|
super(cursor, counted: true)
|
|
361
459
|
end
|
|
362
460
|
end
|
|
363
461
|
|
|
364
|
-
# A hash set with a stored entry count.
|
|
462
|
+
# A hash set with a stored entry count for constant-time size reads.
|
|
365
463
|
class CountedHashSet < HashSet
|
|
464
|
+
# Opens a hash set whose size is stored in its root.
|
|
465
|
+
# @param cursor [Cursor] view locating this collection or value
|
|
466
|
+
# @return [CountedHashSet]
|
|
467
|
+
# @raise [InvalidValueError] if the cursor holds an incompatible collection tag
|
|
366
468
|
def initialize(cursor)
|
|
367
469
|
super(cursor, counted: true)
|
|
368
470
|
end
|
data/lib/xitdb/core/hasher.rb
CHANGED
|
@@ -4,21 +4,44 @@ require "digest"
|
|
|
4
4
|
|
|
5
5
|
module XitDB
|
|
6
6
|
module Core
|
|
7
|
-
#
|
|
7
|
+
# A fixed-width binary digest algorithm and its stored identity.
|
|
8
|
+
#
|
|
9
|
+
# Hash collections receive digest bytes from their caller.
|
|
10
|
+
# This class checks algorithm output before those bytes reach storage.
|
|
8
11
|
class Hasher
|
|
12
|
+
# @!attribute [r] id
|
|
13
|
+
# Four-byte hash algorithm identifier.
|
|
14
|
+
# @return [String]
|
|
15
|
+
# @!attribute [r] size
|
|
16
|
+
# Declared number of payload or digest bytes.
|
|
17
|
+
# @return [Integer]
|
|
9
18
|
attr_reader :id, :size
|
|
10
19
|
|
|
20
|
+
# Defines a hash algorithm with a fixed identifier and output width.
|
|
21
|
+
# @param id [String] four-byte hash algorithm identifier
|
|
22
|
+
# @param size [Integer] number of bytes
|
|
23
|
+
# @return [Hasher]
|
|
24
|
+
# @yield [bytes] computes one binary digest
|
|
25
|
+
# @yieldparam bytes [String] frozen binary input
|
|
26
|
+
# @yieldreturn [String] exactly size digest bytes
|
|
27
|
+
# @raise [ArgumentError] if id is not four bytes or the block is absent
|
|
28
|
+
# @raise [RangeError] if size is outside 1..65_535
|
|
11
29
|
def initialize(id:, size:, &digest)
|
|
12
30
|
raise TypeError, "id must be a String" unless id.is_a?(String)
|
|
13
31
|
raise ArgumentError, "id must contain four bytes" unless id.bytesize == 4
|
|
14
32
|
raise ArgumentError, "digest block is required" unless digest
|
|
15
33
|
|
|
16
34
|
@id = id.b.freeze
|
|
17
|
-
@size = Format.integer(size, 1..65_535)
|
|
35
|
+
@size = Format.integer!(size, 1..65_535)
|
|
18
36
|
@digest = digest
|
|
19
37
|
freeze
|
|
20
38
|
end
|
|
21
39
|
|
|
40
|
+
# Calculates a digest and checks its declared width.
|
|
41
|
+
# @param bytes [String] binary input bytes
|
|
42
|
+
# @return [String]
|
|
43
|
+
# @raise [TypeError] if input or digest output is not a String
|
|
44
|
+
# @raise [ArgumentError] if the digest output has the wrong byte count
|
|
22
45
|
def digest(bytes)
|
|
23
46
|
raise TypeError, "bytes must be a String" unless bytes.is_a?(String)
|
|
24
47
|
|
|
@@ -29,18 +52,26 @@ module XitDB
|
|
|
29
52
|
result.b.freeze
|
|
30
53
|
end
|
|
31
54
|
|
|
55
|
+
# Builds the default 20-byte SHA-1 hasher.
|
|
56
|
+
# @return [Hasher]
|
|
32
57
|
def self.sha1
|
|
33
58
|
new(id: "sha1", size: 20) { |bytes| Digest::SHA1.digest(bytes) }
|
|
34
59
|
end
|
|
35
60
|
|
|
61
|
+
# Builds a 32-byte SHA-256 hasher.
|
|
62
|
+
# @return [Hasher]
|
|
36
63
|
def self.sha256
|
|
37
64
|
new(id: "sha2", size: 32) { |bytes| Digest::SHA256.digest(bytes) }
|
|
38
65
|
end
|
|
39
66
|
|
|
67
|
+
# Builds a 48-byte SHA-384 hasher.
|
|
68
|
+
# @return [Hasher]
|
|
40
69
|
def self.sha384
|
|
41
70
|
new(id: "sha2", size: 48) { |bytes| Digest::SHA384.digest(bytes) }
|
|
42
71
|
end
|
|
43
72
|
|
|
73
|
+
# Builds a 64-byte SHA-512 hasher.
|
|
74
|
+
# @return [Hasher]
|
|
44
75
|
def self.sha512
|
|
45
76
|
new(id: "sha2", size: 64) { |bytes| Digest::SHA512.digest(bytes) }
|
|
46
77
|
end
|