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,98 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module XitDB
|
|
4
|
+
module Core
|
|
5
|
+
# The immutable file profile stored in the first 20 bytes.
|
|
6
|
+
#
|
|
7
|
+
# The profile records the format version, file identity, and hash algorithm identity and width.
|
|
8
|
+
class Header
|
|
9
|
+
# Width of the encoded file header in bytes.
|
|
10
|
+
SIZE = 20
|
|
11
|
+
# Default version of the binary database format.
|
|
12
|
+
VERSION = 2
|
|
13
|
+
|
|
14
|
+
# @!attribute [r] file_id
|
|
15
|
+
# Unsigned 64-bit file identifier.
|
|
16
|
+
# @return [Integer]
|
|
17
|
+
# @!attribute [r] hash_id
|
|
18
|
+
# Four-byte hash algorithm identifier.
|
|
19
|
+
# @return [String]
|
|
20
|
+
# @!attribute [r] hash_size
|
|
21
|
+
# Digest width in bytes.
|
|
22
|
+
# @return [Integer]
|
|
23
|
+
# @!attribute [r] version
|
|
24
|
+
# On-disk format version.
|
|
25
|
+
# @return [Integer]
|
|
26
|
+
attr_reader :file_id, :hash_id, :hash_size, :version
|
|
27
|
+
|
|
28
|
+
# Rejects unsupported file format versions before creating a file.
|
|
29
|
+
# @param version [Integer] requested file format version
|
|
30
|
+
# @return [void]
|
|
31
|
+
# @raise [FormatVersionError] if the format version is unsupported
|
|
32
|
+
def self.validate_version!(version)
|
|
33
|
+
return if [1, VERSION].any? { |supported| supported.eql?(version) }
|
|
34
|
+
|
|
35
|
+
raise FormatVersionError, "unsupported xitdb format version #{version}"
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
# Builds a frozen header identifying the file and its hash format.
|
|
39
|
+
# @param file_id [Integer] unsigned 64-bit file identifier
|
|
40
|
+
# @param hash_id [String] four-byte hash algorithm identifier
|
|
41
|
+
# @param hash_size [Integer] number of bytes in each stored digest
|
|
42
|
+
# @param padding [Integer] header padding byte, from 0 through 255
|
|
43
|
+
# @param version [Integer] on-disk format version, 1 or 2
|
|
44
|
+
# @return [Header]
|
|
45
|
+
# @raise [ArgumentError] if hash_id is not four bytes
|
|
46
|
+
# @raise [TypeError] if an argument has the wrong type
|
|
47
|
+
# @raise [RangeError] if a numeric field exceeds its encoded range
|
|
48
|
+
# @raise [FormatVersionError] if the format version is unsupported
|
|
49
|
+
def initialize(file_id:, hash_id:, hash_size:, padding: 0, version: VERSION)
|
|
50
|
+
Header.validate_version!(version)
|
|
51
|
+
@version = version
|
|
52
|
+
@file_id = Format.integer!(file_id, 0..Format::UINT64_MAX)
|
|
53
|
+
@hash_size = Format.integer!(hash_size, 0..65_535)
|
|
54
|
+
raise TypeError, "hash_id must be a String" unless hash_id.is_a?(String)
|
|
55
|
+
raise ArgumentError, "hash_id must contain four bytes" unless hash_id.bytesize == 4
|
|
56
|
+
|
|
57
|
+
@hash_id = hash_id.b.freeze
|
|
58
|
+
@padding = Format.integer!(padding, 0..255)
|
|
59
|
+
freeze
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# Reads and validates the 20-byte database header.
|
|
63
|
+
# @param bytes [String] binary input bytes
|
|
64
|
+
# @return [Header]
|
|
65
|
+
# @raise [CorruptionError] if size or magic bytes are invalid
|
|
66
|
+
# @raise [FormatVersionError] if the format version is unsupported
|
|
67
|
+
def self.parse(bytes)
|
|
68
|
+
Format.ensure_width!(bytes, SIZE)
|
|
69
|
+
magic = Format.slice(bytes, 0, 3)
|
|
70
|
+
padding = bytes.getbyte(3) || raise(CorruptionError, "missing header padding")
|
|
71
|
+
version, hash_size = Format.pair(Format.slice(bytes, 4, 4), "nn")
|
|
72
|
+
hash_id = Format.slice(bytes, 8, 4)
|
|
73
|
+
file_id = Format.unsigned(Format.slice(bytes, 12, 8))
|
|
74
|
+
raise CorruptionError, "invalid xitdb magic number" unless magic == "xit"
|
|
75
|
+
|
|
76
|
+
new(file_id: file_id, hash_id: hash_id, hash_size: hash_size, padding: padding, version: version)
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Returns the position of the eight-byte committed file size.
|
|
80
|
+
# @return [Integer]
|
|
81
|
+
def committed_size_offset
|
|
82
|
+
version == 1 ? 29 : 24
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Returns the first byte after the root header.
|
|
86
|
+
# @return [Integer]
|
|
87
|
+
def data_offset
|
|
88
|
+
committed_size_offset + 8
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Encodes the header in the database's binary format.
|
|
92
|
+
# @return [String]
|
|
93
|
+
def to_bytes
|
|
94
|
+
["xit", @padding, version, hash_size, hash_id, file_id].pack("a3Cnn a4Q>")
|
|
95
|
+
end
|
|
96
|
+
end
|
|
97
|
+
end
|
|
98
|
+
end
|
|
@@ -2,57 +2,113 @@
|
|
|
2
2
|
|
|
3
3
|
module XitDB
|
|
4
4
|
module Core
|
|
5
|
-
# An indexed list backed by a
|
|
5
|
+
# An indexed slot list backed by a tree with stored subtree counts.
|
|
6
|
+
#
|
|
7
|
+
# Counts locate elements without scanning earlier slots.
|
|
8
|
+
# Split, slice, and concat create detached lists that share unchanged nodes.
|
|
9
|
+
#
|
|
10
|
+
# These structural operations need a writable transaction.
|
|
6
11
|
class LinkedArrayList
|
|
7
12
|
include Enumerable
|
|
8
13
|
|
|
14
|
+
# @!attribute [r] cursor
|
|
15
|
+
# View locating this collection.
|
|
16
|
+
# @return [Cursor]
|
|
9
17
|
attr_reader :cursor
|
|
10
18
|
|
|
19
|
+
# Opens a list whose counted tree supports indexed insertion and removal.
|
|
20
|
+
# @param cursor [Cursor] view locating this collection or value
|
|
21
|
+
# @return [LinkedArrayList]
|
|
11
22
|
def initialize(cursor)
|
|
12
23
|
@cursor = cursor
|
|
13
24
|
@tree = BTree.new(cursor)
|
|
14
25
|
end
|
|
15
26
|
|
|
27
|
+
# Returns the number of elements in the tree.
|
|
28
|
+
# @return [Integer]
|
|
16
29
|
def size
|
|
17
30
|
@tree.size
|
|
18
31
|
end
|
|
19
32
|
|
|
33
|
+
# Reads a slot by index, counting negative indexes from the end.
|
|
34
|
+
# @param index [Integer] zero-based element index; supported negative indexes count from the end
|
|
35
|
+
# @return [Slot, nil]
|
|
20
36
|
def get(index)
|
|
21
37
|
@tree.at(index)
|
|
22
38
|
end
|
|
23
39
|
|
|
40
|
+
# Adds a slot after the final element.
|
|
41
|
+
# @param slot [Slot] stored value or record descriptor
|
|
42
|
+
# @return [self]
|
|
43
|
+
# @raise [ReadOnlyError] if the collection has no writable transaction
|
|
24
44
|
def append(slot)
|
|
25
45
|
@tree.insert(size, slot)
|
|
26
46
|
self
|
|
27
47
|
end
|
|
28
48
|
|
|
49
|
+
# Replaces an existing element without changing list length.
|
|
50
|
+
# @param index [Integer] zero-based element index; supported negative indexes count from the end
|
|
51
|
+
# @param slot [Slot] stored value or record descriptor
|
|
52
|
+
# @return [self]
|
|
53
|
+
# @raise [ReadOnlyError] if the collection has no writable transaction
|
|
54
|
+
# @raise [IndexError] if the index or range is outside the allowed list positions
|
|
29
55
|
def put(index, slot)
|
|
30
56
|
replace_child(index, slot)
|
|
31
57
|
end
|
|
32
58
|
|
|
59
|
+
# Removes an element and shifts later indexes left.
|
|
60
|
+
# @param index [Integer] zero-based element index; supported negative indexes count from the end
|
|
61
|
+
# @return [Slot, nil]
|
|
33
62
|
def delete_at(index)
|
|
34
63
|
@tree.delete_at(index)
|
|
35
64
|
end
|
|
36
65
|
|
|
66
|
+
# Creates a detached list for a range, sharing unchanged tree nodes.
|
|
67
|
+
# @param start [Integer] first selected index; negative indexes count from the end
|
|
68
|
+
# @param length [Integer] number of elements in the selected range
|
|
69
|
+
# @return [LinkedArrayList]
|
|
70
|
+
# @raise [ReadOnlyError] if the collection has no writable transaction
|
|
71
|
+
# @raise [IndexError] if the index or range is outside the allowed list positions
|
|
37
72
|
def slice(start, length)
|
|
38
73
|
self.class.new(@tree.slice(start, length).cursor)
|
|
39
74
|
end
|
|
40
75
|
|
|
76
|
+
# Creates detached lists before and from the split index.
|
|
77
|
+
# @param index [Integer] zero-based element index; supported negative indexes count from the end
|
|
78
|
+
# @return [Array<LinkedArrayList>]
|
|
79
|
+
# @raise [ReadOnlyError] if the collection has no writable transaction
|
|
80
|
+
# @raise [IndexError] if the index or range is outside the allowed list positions
|
|
41
81
|
def split(index)
|
|
42
82
|
@tree.split(index).map { |tree| self.class.new(tree.cursor) }
|
|
43
83
|
end
|
|
44
84
|
|
|
85
|
+
# Creates a detached list containing this list followed by the other list.
|
|
86
|
+
# @param other [LinkedArrayList] list from the same database to append to this list
|
|
87
|
+
# @return [LinkedArrayList]
|
|
88
|
+
# @raise [ReadOnlyError] if the collection has no writable transaction
|
|
89
|
+
# @raise [ForeignValueError] if the other list belongs to another database
|
|
45
90
|
def concat(other)
|
|
46
91
|
raise TypeError, "expected LinkedArrayList" unless other.is_a?(LinkedArrayList)
|
|
47
92
|
|
|
48
93
|
self.class.new(@tree.concat(BTree.new(other.cursor)).cursor)
|
|
49
94
|
end
|
|
50
95
|
|
|
96
|
+
# Replaces an element and forwards ownership and propagation options.
|
|
97
|
+
# @param index [Integer] zero-based element index; supported negative indexes count from the end
|
|
98
|
+
# @param slot [Slot] stored value or record descriptor
|
|
99
|
+
# @return [self]
|
|
100
|
+
# @raise [ReadOnlyError] if the collection has no writable transaction
|
|
101
|
+
# @raise [IndexError] if the index or range is outside the allowed list positions
|
|
102
|
+
# @api private
|
|
51
103
|
def replace_child(index, slot, **)
|
|
52
104
|
@tree.replace(index, slot, **)
|
|
53
105
|
self
|
|
54
106
|
end
|
|
55
107
|
|
|
108
|
+
# Finds an element view, optionally appending an empty element at the end.
|
|
109
|
+
# @param index [Integer] zero-based element index; supported negative indexes count from the end
|
|
110
|
+
# @param create [Boolean] whether to create an empty element when the requested position is absent
|
|
111
|
+
# @return [Cursor, nil]
|
|
56
112
|
def cursor_at(index, create: false)
|
|
57
113
|
slot = get(index)
|
|
58
114
|
if !slot && create
|
|
@@ -67,6 +123,12 @@ module XitDB
|
|
|
67
123
|
cursor.array_child(index, slot)
|
|
68
124
|
end
|
|
69
125
|
|
|
126
|
+
# Visits all slots in index order.
|
|
127
|
+
# @return [self, Enumerator<Slot>]
|
|
128
|
+
# @yield [slot] receives each visible element
|
|
129
|
+
# @yieldparam slot [Slot] stored element
|
|
130
|
+
# @yieldreturn [Object] ignored
|
|
131
|
+
# @note Without a block, returns an Enumerator.
|
|
70
132
|
def each(&block)
|
|
71
133
|
return enum_for(:each) unless block
|
|
72
134
|
|
|
@@ -74,6 +136,13 @@ module XitDB
|
|
|
74
136
|
self
|
|
75
137
|
end
|
|
76
138
|
|
|
139
|
+
# Visits slots from an index, counting negative indexes from the end.
|
|
140
|
+
# @param index [Integer] zero-based element index; supported negative indexes count from the end
|
|
141
|
+
# @return [self, Enumerator<Slot>]
|
|
142
|
+
# @yield [slot] receives each visible element
|
|
143
|
+
# @yieldparam slot [Slot] stored element
|
|
144
|
+
# @yieldreturn [Object] ignored
|
|
145
|
+
# @note Without a block, returns an Enumerator.
|
|
77
146
|
def each_from_index(index, &block)
|
|
78
147
|
return enum_for(:each_from_index, index) unless block
|
|
79
148
|
|
|
@@ -2,261 +2,57 @@
|
|
|
2
2
|
|
|
3
3
|
require "monitor"
|
|
4
4
|
|
|
5
|
+
require_relative "store"
|
|
6
|
+
|
|
5
7
|
module XitDB
|
|
6
8
|
module Core
|
|
7
|
-
#
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
def close_unreturned
|
|
11
|
-
close
|
|
12
|
-
rescue Exception # rubocop:disable Lint/RescueException -- Cleanup must also preserve nonstandard exceptions.
|
|
13
|
-
begin
|
|
14
|
-
close(discard: true)
|
|
15
|
-
rescue Exception # rubocop:disable Lint/RescueException -- Cleanup must also preserve nonstandard exceptions.
|
|
16
|
-
nil
|
|
17
|
-
end
|
|
18
|
-
end
|
|
19
|
-
private :close_unreturned
|
|
20
|
-
|
|
21
|
-
def read_at(offset, length)
|
|
22
|
-
synchronize do
|
|
23
|
-
position(offset)
|
|
24
|
-
position(length)
|
|
25
|
-
next "".b if length.zero?
|
|
26
|
-
raise EOFError, "read exceeds store length" if offset + length > store_length
|
|
27
|
-
|
|
28
|
-
read_bytes(offset, length)
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
def write_at(offset, bytes)
|
|
33
|
-
synchronize do
|
|
34
|
-
writable!
|
|
35
|
-
position(offset)
|
|
36
|
-
string(bytes)
|
|
37
|
-
write_bytes(offset, bytes.b) unless bytes.empty?
|
|
38
|
-
bytes.bytesize
|
|
39
|
-
end
|
|
40
|
-
end
|
|
41
|
-
|
|
42
|
-
def append(bytes)
|
|
43
|
-
synchronize do
|
|
44
|
-
writable!
|
|
45
|
-
offset = store_length
|
|
46
|
-
write_at(offset, bytes)
|
|
47
|
-
offset
|
|
48
|
-
end
|
|
49
|
-
end
|
|
50
|
-
|
|
51
|
-
def length
|
|
52
|
-
synchronize { store_length }
|
|
53
|
-
end
|
|
54
|
-
|
|
55
|
-
def empty?
|
|
56
|
-
length.zero?
|
|
57
|
-
end
|
|
58
|
-
|
|
59
|
-
def truncate(length)
|
|
60
|
-
synchronize do
|
|
61
|
-
writable!
|
|
62
|
-
position(length)
|
|
63
|
-
truncate_bytes(length)
|
|
64
|
-
nil
|
|
65
|
-
end
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
def flush
|
|
69
|
-
synchronize { flush_bytes }
|
|
70
|
-
end
|
|
71
|
-
|
|
72
|
-
def sync
|
|
73
|
-
synchronize do
|
|
74
|
-
flush_bytes
|
|
75
|
-
sync_bytes
|
|
76
|
-
end
|
|
77
|
-
end
|
|
78
|
-
|
|
79
|
-
# Pin the handle across all storage calls in one logical operation.
|
|
80
|
-
def with_operation
|
|
81
|
-
thread = Thread.current
|
|
82
|
-
synchronize { @operation_users[thread] = @operation_users.fetch(thread, 0) + 1 }
|
|
83
|
-
begin
|
|
84
|
-
yield
|
|
85
|
-
ensure
|
|
86
|
-
@state.synchronize do
|
|
87
|
-
@operation_users[thread] -= 1
|
|
88
|
-
@operation_users.delete(thread) if @operation_users[thread].zero?
|
|
89
|
-
@idle.broadcast
|
|
90
|
-
end
|
|
91
|
-
end
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
def with_exclusive_lock(timeout: 5, on_release_error: nil)
|
|
95
|
-
raise TypeError, "timeout must be a real Numeric" unless timeout.is_a?(Numeric) && timeout.real?
|
|
96
|
-
raise ArgumentError, "timeout must be finite and nonnegative" unless timeout.finite? && timeout >= 0
|
|
97
|
-
|
|
98
|
-
deadline = monotonic_time + timeout
|
|
99
|
-
synchronize do
|
|
100
|
-
writable!
|
|
101
|
-
raise TransactionStateError, "writer lock is already held by this thread" if @lock_owner == Thread.current
|
|
102
|
-
|
|
103
|
-
@lock_users += 1
|
|
104
|
-
end
|
|
105
|
-
locked = false
|
|
106
|
-
completed = false
|
|
107
|
-
begin
|
|
108
|
-
lock_writer(deadline)
|
|
109
|
-
locked = true
|
|
110
|
-
@state.synchronize do
|
|
111
|
-
writable!
|
|
112
|
-
@lock_owner = Thread.current
|
|
113
|
-
end
|
|
114
|
-
result = yield
|
|
115
|
-
completed = true
|
|
116
|
-
result
|
|
117
|
-
ensure
|
|
118
|
-
begin
|
|
119
|
-
release_writer(locked, completed, on_release_error)
|
|
120
|
-
ensure
|
|
121
|
-
@state.synchronize do
|
|
122
|
-
@lock_users -= 1
|
|
123
|
-
@idle.broadcast
|
|
124
|
-
end
|
|
125
|
-
end
|
|
126
|
-
end
|
|
127
|
-
end
|
|
128
|
-
|
|
129
|
-
def close(discard: false)
|
|
130
|
-
@state.synchronize do
|
|
131
|
-
return if @closed
|
|
132
|
-
raise TransactionStateError, "cannot close inside the writer block" if @lock_owner == Thread.current
|
|
133
|
-
if @operation_users.key?(Thread.current)
|
|
134
|
-
raise TransactionStateError, "cannot close inside an active operation"
|
|
135
|
-
end
|
|
136
|
-
|
|
137
|
-
@closing = true
|
|
138
|
-
begin
|
|
139
|
-
@idle.wait until @lock_users.zero? && @operation_users.empty?
|
|
140
|
-
return if @closed
|
|
141
|
-
|
|
142
|
-
flush_bytes unless discard
|
|
143
|
-
close_bytes
|
|
144
|
-
@closed = true
|
|
145
|
-
ensure
|
|
146
|
-
@closing = false
|
|
147
|
-
end
|
|
148
|
-
end
|
|
149
|
-
end
|
|
150
|
-
|
|
151
|
-
def closed?
|
|
152
|
-
@state.synchronize { @closed }
|
|
153
|
-
end
|
|
154
|
-
|
|
155
|
-
private
|
|
156
|
-
|
|
157
|
-
def release_writer(locked, completed, on_release_error)
|
|
158
|
-
@state.synchronize do
|
|
159
|
-
@lock_owner = nil if locked
|
|
160
|
-
begin
|
|
161
|
-
unlock_writer if locked
|
|
162
|
-
rescue Exception => e # rubocop:disable Lint/RescueException -- Cleanup must also preserve nonstandard exceptions.
|
|
163
|
-
@release_failed = true
|
|
164
|
-
on_release_error&.call(e)
|
|
165
|
-
# Raising here would replace a pending exception or nonlocal exit.
|
|
166
|
-
raise if completed
|
|
167
|
-
end
|
|
168
|
-
end
|
|
169
|
-
end
|
|
170
|
-
|
|
171
|
-
def initialize_state(writer_mutex = Mutex.new)
|
|
172
|
-
@state = Monitor.new
|
|
173
|
-
@idle = @state.new_cond
|
|
174
|
-
@writer_mutex = writer_mutex
|
|
175
|
-
@lock_users = 0
|
|
176
|
-
@operation_users = {}
|
|
177
|
-
@lock_owner = nil
|
|
178
|
-
@closed = false
|
|
179
|
-
@closing = false
|
|
180
|
-
@release_failed = false
|
|
181
|
-
end
|
|
182
|
-
|
|
183
|
-
def synchronize
|
|
184
|
-
@state.synchronize do
|
|
185
|
-
if @closed || (@closing && @lock_owner != Thread.current && !@operation_users.key?(Thread.current))
|
|
186
|
-
raise ClosedError, "store is closed"
|
|
187
|
-
end
|
|
188
|
-
|
|
189
|
-
yield
|
|
190
|
-
end
|
|
191
|
-
end
|
|
192
|
-
|
|
193
|
-
def writable!
|
|
194
|
-
raise ReadOnlyError, "store is read-only" if readonly?
|
|
195
|
-
raise TransactionStateError, "writer lock release failed; reopen the store" if @release_failed
|
|
196
|
-
end
|
|
197
|
-
|
|
198
|
-
def position(value)
|
|
199
|
-
raise TypeError, "offset and length must be Integers" unless value.is_a?(Integer)
|
|
200
|
-
raise ArgumentError, "offset and length must be nonnegative" if value.negative?
|
|
201
|
-
end
|
|
202
|
-
|
|
203
|
-
def string(bytes)
|
|
204
|
-
raise TypeError, "bytes must be a String" unless bytes.is_a?(String)
|
|
205
|
-
end
|
|
206
|
-
|
|
207
|
-
def monotonic_time
|
|
208
|
-
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
209
|
-
end
|
|
210
|
-
|
|
211
|
-
def wait_for_writer(deadline)
|
|
212
|
-
remaining = deadline - monotonic_time
|
|
213
|
-
raise BusyError, "writer lock timed out" unless remaining.positive?
|
|
214
|
-
|
|
215
|
-
sleep([remaining, 0.005].min)
|
|
216
|
-
end
|
|
217
|
-
|
|
218
|
-
def lock_writer(deadline)
|
|
219
|
-
wait_for_writer(deadline) until @writer_mutex.try_lock
|
|
220
|
-
end
|
|
221
|
-
|
|
222
|
-
def unlock_writer
|
|
223
|
-
@writer_mutex.unlock
|
|
224
|
-
end
|
|
225
|
-
end
|
|
226
|
-
private_constant :Store
|
|
227
|
-
|
|
228
|
-
# A synchronized store backed by a binary string.
|
|
9
|
+
# A synchronized byte store backed by a private binary string.
|
|
10
|
+
#
|
|
11
|
+
# Offsets use bytes, starting at zero. Closing discards all data; sync has no persistent storage to update.
|
|
229
12
|
class MemoryStore
|
|
230
13
|
include Store
|
|
231
14
|
|
|
15
|
+
# @!attribute [r] storage_identity
|
|
16
|
+
# Identity shared by handles referring to the same underlying storage.
|
|
17
|
+
# @return [Object, Array<Integer>]
|
|
232
18
|
attr_reader :storage_identity
|
|
233
19
|
|
|
20
|
+
# Copies initial bytes into a writable in-memory store.
|
|
21
|
+
# @param bytes [String] binary input bytes
|
|
22
|
+
# @return [MemoryStore]
|
|
234
23
|
def initialize(bytes = "".b)
|
|
235
|
-
|
|
24
|
+
ensure_string!(bytes)
|
|
236
25
|
@bytes = bytes.b
|
|
237
26
|
@storage_identity = Object.new.freeze
|
|
238
27
|
initialize_state
|
|
239
28
|
end
|
|
240
29
|
|
|
30
|
+
# Reports whether writes are disabled.
|
|
31
|
+
# @return [Boolean]
|
|
241
32
|
def readonly?
|
|
242
33
|
false
|
|
243
34
|
end
|
|
244
35
|
|
|
245
36
|
private
|
|
246
37
|
|
|
38
|
+
# Counts the bytes held by the in-memory store.
|
|
247
39
|
def store_length
|
|
248
40
|
@bytes.bytesize
|
|
249
41
|
end
|
|
250
42
|
|
|
43
|
+
# Reads a validated range from the binary string.
|
|
251
44
|
def read_bytes(offset, length)
|
|
252
|
-
|
|
45
|
+
# A shared tail slice would make the next write copy the complete store.
|
|
46
|
+
"".b.bytesplice(0, 0, @bytes, offset, length)
|
|
253
47
|
end
|
|
254
48
|
|
|
49
|
+
# Replaces a range, filling any gap before it with zero bytes.
|
|
255
50
|
def write_bytes(offset, bytes)
|
|
256
51
|
@bytes << ("\0".b * (offset - store_length)) if offset > store_length
|
|
257
52
|
@bytes[offset, bytes.bytesize] = bytes
|
|
258
53
|
end
|
|
259
54
|
|
|
55
|
+
# Shrinks or extends the binary string to the requested length.
|
|
260
56
|
def truncate_bytes(length)
|
|
261
57
|
if length < store_length
|
|
262
58
|
@bytes = Format.slice(@bytes, 0, length)
|
|
@@ -265,10 +61,13 @@ module XitDB
|
|
|
265
61
|
end
|
|
266
62
|
end
|
|
267
63
|
|
|
64
|
+
# Completes a flush without work because memory has no write buffer.
|
|
268
65
|
def flush_bytes; end
|
|
269
66
|
|
|
67
|
+
# Completes synchronization without work because this store has no disk file.
|
|
270
68
|
def sync_bytes; end
|
|
271
69
|
|
|
70
|
+
# Releases all bytes held by the in-memory store.
|
|
272
71
|
def close_bytes
|
|
273
72
|
@bytes.clear
|
|
274
73
|
end
|
|
@@ -4,17 +4,29 @@ require "tempfile"
|
|
|
4
4
|
|
|
5
5
|
module XitDB
|
|
6
6
|
module Core
|
|
7
|
-
#
|
|
7
|
+
# A source-to-destination offset table used while copying records.
|
|
8
|
+
#
|
|
9
|
+
# It starts in memory and moves to a temporary database after CACHE_LIMIT entries.
|
|
10
|
+
# @api private
|
|
8
11
|
class OffsetMap
|
|
12
|
+
# Maximum distinct offset mappings retained in memory before a temporary store is created.
|
|
9
13
|
CACHE_LIMIT = 16_384
|
|
10
14
|
|
|
15
|
+
# Largest number of offset entries held in the in-memory cache.
|
|
16
|
+
# @return [Integer]
|
|
11
17
|
attr_reader :peak_cache_size
|
|
12
18
|
|
|
19
|
+
# Starts an offset lookup table in memory.
|
|
20
|
+
# @return [OffsetMap]
|
|
13
21
|
def initialize
|
|
14
22
|
@cache = {}
|
|
15
23
|
@peak_cache_size = 0
|
|
16
24
|
end
|
|
17
25
|
|
|
26
|
+
# Finds the copied destination for a source offset.
|
|
27
|
+
# @param offset [Integer] absolute byte offset
|
|
28
|
+
# @return [Integer, nil]
|
|
29
|
+
# @raise [ClosedError] if the offset map has been closed
|
|
18
30
|
def fetch(offset)
|
|
19
31
|
raise ClosedError, "offset map is closed" if @closed
|
|
20
32
|
|
|
@@ -22,6 +34,11 @@ module XitDB
|
|
|
22
34
|
map ? map.get(key(offset))&.value : @cache[offset]
|
|
23
35
|
end
|
|
24
36
|
|
|
37
|
+
# Records a destination, spilling to a temporary database when the cache fills.
|
|
38
|
+
# @param offset [Integer] absolute byte offset
|
|
39
|
+
# @param target [Integer] absolute destination byte offset
|
|
40
|
+
# @return [Integer]
|
|
41
|
+
# @raise [ClosedError] if the offset map has been closed
|
|
25
42
|
def store(offset, target)
|
|
26
43
|
raise ClosedError, "offset map is closed" if @closed
|
|
27
44
|
|
|
@@ -36,6 +53,8 @@ module XitDB
|
|
|
36
53
|
target
|
|
37
54
|
end
|
|
38
55
|
|
|
56
|
+
# Releases the cache and closes and deletes its temporary backing file.
|
|
57
|
+
# @return [nil]
|
|
39
58
|
def close
|
|
40
59
|
return if @cleanup_complete
|
|
41
60
|
|
|
@@ -53,10 +72,12 @@ module XitDB
|
|
|
53
72
|
|
|
54
73
|
private
|
|
55
74
|
|
|
75
|
+
# Encodes a source offset as an unsigned 64-bit binary map key.
|
|
56
76
|
def key(offset)
|
|
57
77
|
[offset].pack("Q>")
|
|
58
78
|
end
|
|
59
79
|
|
|
80
|
+
# Moves cached offsets to a private file-backed map in one scratch transaction.
|
|
60
81
|
def spill
|
|
61
82
|
temporary = Tempfile.new("xitdb-offset-map-")
|
|
62
83
|
@temporary = temporary
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module XitDB
|
|
4
|
+
module Core
|
|
5
|
+
# A bounded group of slots with an opaque unsigned metadata word.
|
|
6
|
+
# Compaction follows every field while preserving shared references.
|
|
7
|
+
# @api private
|
|
8
|
+
class Record
|
|
9
|
+
# Maximum fields read or written in one record.
|
|
10
|
+
MAX_FIELDS = 32
|
|
11
|
+
# Bytes containing metadata and field count.
|
|
12
|
+
HEADER_SIZE = 8
|
|
13
|
+
|
|
14
|
+
# @return [Integer] application-defined unsigned metadata
|
|
15
|
+
attr_reader :metadata
|
|
16
|
+
# @return [Array<Slot>] frozen ordered fields
|
|
17
|
+
attr_reader :fields
|
|
18
|
+
|
|
19
|
+
# Validates a bounded record before any storage allocation.
|
|
20
|
+
# @param metadata [Integer] unsigned 32-bit application metadata
|
|
21
|
+
# @param fields [Array<Slot>] at most 32 known slots
|
|
22
|
+
def initialize(metadata:, fields:)
|
|
23
|
+
@metadata = Format.integer!(metadata, 0..((1 << 32) - 1))
|
|
24
|
+
raise ArgumentError, "record exceeds field limit" if fields.size > MAX_FIELDS
|
|
25
|
+
|
|
26
|
+
fields.each do |field|
|
|
27
|
+
raise TypeError, "record fields must be slots" unless field.is_a?(Slot)
|
|
28
|
+
raise CorruptionError, "unknown record field tag" unless Slot::TAGS.include?(field.tag)
|
|
29
|
+
end
|
|
30
|
+
@fields = fields.dup.freeze
|
|
31
|
+
freeze
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Appends a complete record without publishing its descriptor.
|
|
35
|
+
# The owning logical mutation supplies rollback and root publication.
|
|
36
|
+
# @param transaction [Transaction] active writer
|
|
37
|
+
# @param metadata [Integer] unsigned 32-bit application metadata
|
|
38
|
+
# @param fields [Array<Slot>] bounded ordered fields
|
|
39
|
+
# @return [Slot] external record descriptor
|
|
40
|
+
def self.append(transaction, metadata:, fields:)
|
|
41
|
+
transaction.check_active!
|
|
42
|
+
record = new(metadata: metadata, fields: fields)
|
|
43
|
+
bytes = [record.metadata, record.fields.size].pack("NN") + record.fields.map(&:to_bytes).join.b
|
|
44
|
+
Slot.new(tag: :record, value: transaction.append(bytes))
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# Reads a record within its captured boundary, checking its count first.
|
|
48
|
+
# @param database [Database] source storage
|
|
49
|
+
# @param slot [Slot] record descriptor
|
|
50
|
+
# @param bound [Integer] exclusive readable boundary
|
|
51
|
+
# @return [Record] validated immutable fields
|
|
52
|
+
def self.read(database, slot, bound:)
|
|
53
|
+
raise InvalidValueError, "slot does not contain a record" unless slot.tag == :record
|
|
54
|
+
|
|
55
|
+
metadata, count = Format.pair(database.read_at(slot.value, HEADER_SIZE, bound: bound), "NN")
|
|
56
|
+
raise CorruptionError, "record exceeds field limit" if count > MAX_FIELDS
|
|
57
|
+
|
|
58
|
+
bytes = database.read_at(slot.value + HEADER_SIZE, count * Slot::SIZE, bound: bound)
|
|
59
|
+
fields = Array.new(count) { |index| Slot.parse(Format.slice(bytes, index * Slot::SIZE, Slot::SIZE)) }
|
|
60
|
+
new(metadata: metadata, fields: fields)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|