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,286 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module XitDB
|
|
4
|
+
module Core
|
|
5
|
+
# Shared locking, byte access, and close coordination for storage handles.
|
|
6
|
+
#
|
|
7
|
+
# A logical operation keeps the handle open across several reads or writes.
|
|
8
|
+
#
|
|
9
|
+
# A writer lock permits one writer at a time. A flush drains buffers; sync also requests durable storage.
|
|
10
|
+
# @api private
|
|
11
|
+
module Store
|
|
12
|
+
# Closes an owned store after failure, preserving the original exception.
|
|
13
|
+
def close_unreturned
|
|
14
|
+
close
|
|
15
|
+
rescue Exception # rubocop:disable Lint/RescueException -- Cleanup must also preserve nonstandard exceptions.
|
|
16
|
+
begin
|
|
17
|
+
close(discard: true)
|
|
18
|
+
rescue Exception # rubocop:disable Lint/RescueException -- Cleanup must also preserve nonstandard exceptions.
|
|
19
|
+
nil
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
private :close_unreturned
|
|
23
|
+
|
|
24
|
+
# Reads exactly the requested bytes at an absolute offset.
|
|
25
|
+
# @param offset [Integer] absolute byte offset
|
|
26
|
+
# @param length [Integer] number of bytes
|
|
27
|
+
# @return [String]
|
|
28
|
+
# @raise [EOFError] if the requested range exceeds store length
|
|
29
|
+
# @raise [ClosedError] if the store is closed
|
|
30
|
+
def read_at(offset, length)
|
|
31
|
+
synchronize do
|
|
32
|
+
ensure_position!(offset)
|
|
33
|
+
ensure_position!(length)
|
|
34
|
+
next "".b if length.zero?
|
|
35
|
+
raise EOFError, "read exceeds store length" if offset + length > store_length
|
|
36
|
+
|
|
37
|
+
read_bytes(offset, length)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# Writes bytes at an absolute offset and returns their count.
|
|
42
|
+
# @param offset [Integer] absolute byte offset
|
|
43
|
+
# @param bytes [String] binary input bytes
|
|
44
|
+
# @return [Integer]
|
|
45
|
+
# @raise [ReadOnlyError] if the store is read-only
|
|
46
|
+
# @raise [ClosedError] if the store is closed
|
|
47
|
+
def write_at(offset, bytes)
|
|
48
|
+
synchronize do
|
|
49
|
+
writable!
|
|
50
|
+
ensure_position!(offset)
|
|
51
|
+
ensure_string!(bytes)
|
|
52
|
+
write_bytes(offset, bytes.b) unless bytes.empty?
|
|
53
|
+
bytes.bytesize
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# Adds bytes at the logical end and returns their starting offset.
|
|
58
|
+
# @param bytes [String] binary input bytes
|
|
59
|
+
# @return [Integer]
|
|
60
|
+
def append(bytes)
|
|
61
|
+
synchronize do
|
|
62
|
+
writable!
|
|
63
|
+
offset = store_length
|
|
64
|
+
write_at(offset, bytes)
|
|
65
|
+
offset
|
|
66
|
+
end
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# Counts logical bytes, including buffered writes.
|
|
70
|
+
# @return [Integer]
|
|
71
|
+
def length
|
|
72
|
+
synchronize { store_length }
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
# Tests whether the store contains zero bytes.
|
|
76
|
+
# @return [Boolean]
|
|
77
|
+
def empty?
|
|
78
|
+
length.zero?
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
# Sets the logical length, removing bytes or adding zero bytes.
|
|
82
|
+
# @param length [Integer] number of bytes
|
|
83
|
+
# @return [nil]
|
|
84
|
+
def truncate(length)
|
|
85
|
+
synchronize do
|
|
86
|
+
writable!
|
|
87
|
+
ensure_position!(length)
|
|
88
|
+
truncate_bytes(length)
|
|
89
|
+
nil
|
|
90
|
+
end
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Sends buffered writes to the underlying storage.
|
|
94
|
+
# @return [void]
|
|
95
|
+
def flush
|
|
96
|
+
synchronize { flush_bytes }
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
# Flushes writes and requests durable storage where supported.
|
|
100
|
+
# @return [void]
|
|
101
|
+
def sync
|
|
102
|
+
synchronize do
|
|
103
|
+
flush_bytes
|
|
104
|
+
sync_bytes
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Keeps the store open throughout one multi-call operation.
|
|
109
|
+
# @return [Object]
|
|
110
|
+
# @yield runs while close waits for this operation
|
|
111
|
+
# @yieldreturn [Object] result returned to the caller
|
|
112
|
+
# @api private
|
|
113
|
+
def with_operation
|
|
114
|
+
thread = Thread.current
|
|
115
|
+
synchronize { @operation_users[thread] = @operation_users.fetch(thread, 0) + 1 }
|
|
116
|
+
begin
|
|
117
|
+
yield
|
|
118
|
+
ensure
|
|
119
|
+
@state.synchronize do
|
|
120
|
+
@operation_users[thread] -= 1
|
|
121
|
+
@operation_users.delete(thread) if @operation_users[thread].zero?
|
|
122
|
+
@idle.broadcast
|
|
123
|
+
end
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
# Runs one writer while other writers wait until the timeout.
|
|
128
|
+
# @param timeout [Numeric] finite nonnegative number of seconds to wait for a writer
|
|
129
|
+
# @param on_release_error [Proc, nil] optional callback receiving a writer lock release exception
|
|
130
|
+
# @return [Object]
|
|
131
|
+
# @yield runs while this thread owns the writer lock
|
|
132
|
+
# @yieldreturn [Object] result returned to the caller
|
|
133
|
+
# @raise [BusyError] if another writer holds the lock past the timeout
|
|
134
|
+
# @raise [TransactionStateError] if this thread already holds the writer lock
|
|
135
|
+
# @raise [ReadOnlyError] if the store is read-only
|
|
136
|
+
def with_exclusive_lock(timeout: 5, on_release_error: nil)
|
|
137
|
+
raise TypeError, "timeout must be a real Numeric" unless timeout.is_a?(Numeric) && timeout.real?
|
|
138
|
+
raise ArgumentError, "timeout must be finite and nonnegative" unless timeout.finite? && timeout >= 0
|
|
139
|
+
|
|
140
|
+
deadline = monotonic_time + timeout
|
|
141
|
+
synchronize do
|
|
142
|
+
writable!
|
|
143
|
+
raise TransactionStateError, "writer lock is already held by this thread" if @lock_owner == Thread.current
|
|
144
|
+
|
|
145
|
+
@lock_users += 1
|
|
146
|
+
end
|
|
147
|
+
locked = false
|
|
148
|
+
completed = false
|
|
149
|
+
begin
|
|
150
|
+
lock_writer(deadline)
|
|
151
|
+
locked = true
|
|
152
|
+
@state.synchronize do
|
|
153
|
+
writable!
|
|
154
|
+
@lock_owner = Thread.current
|
|
155
|
+
end
|
|
156
|
+
result = yield
|
|
157
|
+
completed = true
|
|
158
|
+
result
|
|
159
|
+
ensure
|
|
160
|
+
begin
|
|
161
|
+
release_writer(locked, completed, on_release_error)
|
|
162
|
+
ensure
|
|
163
|
+
@state.synchronize do
|
|
164
|
+
@lock_users -= 1
|
|
165
|
+
@idle.broadcast
|
|
166
|
+
end
|
|
167
|
+
end
|
|
168
|
+
end
|
|
169
|
+
end
|
|
170
|
+
|
|
171
|
+
# Waits for operations and writers, then closes the store.
|
|
172
|
+
# @param discard [Boolean] whether to close without flushing buffered writes
|
|
173
|
+
# @return [void]
|
|
174
|
+
# @raise [TransactionStateError] if called inside this thread's active operation or writer block
|
|
175
|
+
def close(discard: false)
|
|
176
|
+
@state.synchronize do
|
|
177
|
+
return if @closed
|
|
178
|
+
raise TransactionStateError, "cannot close inside the writer block" if @lock_owner == Thread.current
|
|
179
|
+
if @operation_users.key?(Thread.current)
|
|
180
|
+
raise TransactionStateError, "cannot close inside an active operation"
|
|
181
|
+
end
|
|
182
|
+
|
|
183
|
+
@closing = true
|
|
184
|
+
begin
|
|
185
|
+
@idle.wait until @lock_users.zero? && @operation_users.empty?
|
|
186
|
+
return if @closed
|
|
187
|
+
|
|
188
|
+
flush_bytes unless discard
|
|
189
|
+
close_bytes
|
|
190
|
+
@closed = true
|
|
191
|
+
ensure
|
|
192
|
+
@closing = false
|
|
193
|
+
end
|
|
194
|
+
end
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
# Tests whether the store has completed closing.
|
|
198
|
+
# @return [Boolean]
|
|
199
|
+
def closed?
|
|
200
|
+
@state.synchronize { @closed }
|
|
201
|
+
end
|
|
202
|
+
|
|
203
|
+
private
|
|
204
|
+
|
|
205
|
+
# Releases writer ownership and preserves a pending caller failure.
|
|
206
|
+
def release_writer(locked, completed, on_release_error)
|
|
207
|
+
@state.synchronize do
|
|
208
|
+
@lock_owner = nil if locked
|
|
209
|
+
begin
|
|
210
|
+
unlock_writer if locked
|
|
211
|
+
rescue Exception => e # rubocop:disable Lint/RescueException -- Cleanup must also preserve nonstandard exceptions.
|
|
212
|
+
@release_failed = true
|
|
213
|
+
on_release_error&.call(e)
|
|
214
|
+
# Raising here would replace a pending exception or nonlocal exit.
|
|
215
|
+
raise if completed
|
|
216
|
+
end
|
|
217
|
+
end
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
# Creates the monitor, operation counters, and shared writer mutex.
|
|
221
|
+
def initialize_state(writer_mutex = Mutex.new)
|
|
222
|
+
@state = Monitor.new
|
|
223
|
+
@idle = @state.new_cond
|
|
224
|
+
@writer_mutex = writer_mutex
|
|
225
|
+
@lock_users = 0
|
|
226
|
+
@operation_users = {}
|
|
227
|
+
@lock_owner = nil
|
|
228
|
+
@closed = false
|
|
229
|
+
@closing = false
|
|
230
|
+
@release_failed = false
|
|
231
|
+
end
|
|
232
|
+
|
|
233
|
+
# Checks the store lifetime while holding its state monitor.
|
|
234
|
+
def synchronize
|
|
235
|
+
@state.synchronize do
|
|
236
|
+
if @closed || (@closing && @lock_owner != Thread.current && !@operation_users.key?(Thread.current))
|
|
237
|
+
raise ClosedError, "store is closed"
|
|
238
|
+
end
|
|
239
|
+
|
|
240
|
+
yield
|
|
241
|
+
end
|
|
242
|
+
end
|
|
243
|
+
|
|
244
|
+
# Rejects read-only writes and writes after a failed lock release.
|
|
245
|
+
def writable!
|
|
246
|
+
raise ReadOnlyError, "store is read-only" if readonly?
|
|
247
|
+
raise TransactionStateError, "writer lock release failed; reopen the store" if @release_failed
|
|
248
|
+
end
|
|
249
|
+
|
|
250
|
+
# Rejects offsets and lengths that are not nonnegative integers.
|
|
251
|
+
def ensure_position!(value)
|
|
252
|
+
raise TypeError, "offset and length must be Integers" unless value.is_a?(Integer)
|
|
253
|
+
raise ArgumentError, "offset and length must be nonnegative" if value.negative?
|
|
254
|
+
end
|
|
255
|
+
|
|
256
|
+
# Checks that a write payload is a String.
|
|
257
|
+
def ensure_string!(bytes)
|
|
258
|
+
raise TypeError, "bytes must be a String" unless bytes.is_a?(String)
|
|
259
|
+
end
|
|
260
|
+
|
|
261
|
+
# Reads the clock used for writer deadlines.
|
|
262
|
+
def monotonic_time
|
|
263
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
264
|
+
end
|
|
265
|
+
|
|
266
|
+
# Waits briefly for a writer and rejects an expired deadline.
|
|
267
|
+
def wait_for_writer(deadline)
|
|
268
|
+
remaining = deadline - monotonic_time
|
|
269
|
+
raise BusyError, "writer lock timed out" unless remaining.positive?
|
|
270
|
+
|
|
271
|
+
sleep([remaining, 0.005].min)
|
|
272
|
+
end
|
|
273
|
+
|
|
274
|
+
# Acquires the process-local writer mutex before its deadline.
|
|
275
|
+
def lock_writer(deadline)
|
|
276
|
+
wait_for_writer(deadline) until @writer_mutex.try_lock
|
|
277
|
+
end
|
|
278
|
+
|
|
279
|
+
# Releases the process-local writer mutex.
|
|
280
|
+
def unlock_writer
|
|
281
|
+
@writer_mutex.unlock
|
|
282
|
+
end
|
|
283
|
+
end
|
|
284
|
+
private_constant :Store
|
|
285
|
+
end
|
|
286
|
+
end
|
|
@@ -2,13 +2,28 @@
|
|
|
2
2
|
|
|
3
3
|
module XitDB
|
|
4
4
|
module Core
|
|
5
|
-
#
|
|
5
|
+
# Writable state owned by one database transaction and one thread.
|
|
6
|
+
#
|
|
7
|
+
# A checkpoint saves cursors and file length. Restoring it removes later writes and expires later cursors.
|
|
8
|
+
#
|
|
9
|
+
# Copy-on-write preserves earlier views by copying blocks below mutable_start before changing them.
|
|
6
10
|
class Transaction
|
|
7
11
|
Checkpoint = Struct.new(:owner, :slots, :length, :cancelled, keyword_init: true) # rubocop:disable Lint/StructNewOverride -- Length is the saved store boundary.
|
|
8
12
|
private_constant :Checkpoint
|
|
9
13
|
|
|
14
|
+
# @!attribute [r] root
|
|
15
|
+
# Writable root cursor for the active transaction.
|
|
16
|
+
# @return [Cursor]
|
|
17
|
+
# @!attribute [r] mutable_start
|
|
18
|
+
# First byte offset that can be changed without copying protected records.
|
|
19
|
+
# @return [Integer]
|
|
10
20
|
attr_reader :root, :mutable_start
|
|
11
21
|
|
|
22
|
+
# Starts thread-owned writable state from the current root slot.
|
|
23
|
+
# @param database [Database] database that owns the stored records
|
|
24
|
+
# @param store [MemoryStore, FileStore] underlying byte storage
|
|
25
|
+
# @param slot [Slot] stored value or record descriptor
|
|
26
|
+
# @return [Transaction]
|
|
12
27
|
def initialize(database, store, slot)
|
|
13
28
|
@database = database
|
|
14
29
|
@store = store
|
|
@@ -21,17 +36,31 @@ module XitDB
|
|
|
21
36
|
@root = cursor(slot: slot)
|
|
22
37
|
end
|
|
23
38
|
|
|
39
|
+
# Checks that the database is open and this transaction belongs to the calling thread.
|
|
40
|
+
# @return [void]
|
|
41
|
+
# @raise [ClosedError] if the database is closed
|
|
42
|
+
# @raise [ExpiredViewError] if the transaction has ended
|
|
43
|
+
# @raise [TransactionStateError] if called from another thread
|
|
24
44
|
def check_active!
|
|
25
45
|
@database.check_open!
|
|
26
46
|
raise ExpiredViewError, "transaction has ended" unless @active
|
|
27
47
|
raise TransactionStateError, "transaction belongs to another thread" unless @owner == Thread.current
|
|
28
48
|
end
|
|
29
49
|
|
|
50
|
+
# Rejects a nested logical mutation inside this transaction.
|
|
51
|
+
# @return [void]
|
|
52
|
+
# @raise [TransactionStateError] if a logical mutation is already running
|
|
53
|
+
# @api private
|
|
30
54
|
def check_logical_mutation!
|
|
31
55
|
check_active!
|
|
32
56
|
raise TransactionStateError, "a logical mutation is already active" if @logical_mutation
|
|
33
57
|
end
|
|
34
58
|
|
|
59
|
+
# Runs one logical mutation and clears its guard even when the block fails.
|
|
60
|
+
# @return [Object]
|
|
61
|
+
# @yield runs with the logical mutation guard held
|
|
62
|
+
# @yieldreturn [Object] result returned to the caller
|
|
63
|
+
# @api private
|
|
35
64
|
def with_logical_mutation
|
|
36
65
|
check_logical_mutation!
|
|
37
66
|
@logical_mutation = true
|
|
@@ -42,40 +71,67 @@ module XitDB
|
|
|
42
71
|
end
|
|
43
72
|
end
|
|
44
73
|
|
|
74
|
+
# Creates and registers a writable cursor for a slot.
|
|
75
|
+
# @param slot [Slot] stored value or record descriptor
|
|
76
|
+
# @return [Cursor]
|
|
45
77
|
def cursor(slot:)
|
|
46
78
|
check_active!
|
|
47
|
-
validate_slot(slot)
|
|
79
|
+
validate_slot!(slot)
|
|
48
80
|
cursor = Cursor.new(database: @database, slot: slot, bound: length, transaction: self)
|
|
49
81
|
@cursors[cursor] = true
|
|
50
82
|
cursor
|
|
51
83
|
end
|
|
52
84
|
|
|
85
|
+
# Checks that a cursor still belongs to this transaction state.
|
|
86
|
+
# @param cursor [Cursor] view locating this collection or value
|
|
87
|
+
# @return [void]
|
|
88
|
+
# @raise [ExpiredViewError] if rollback discarded the cursor
|
|
89
|
+
# @api private
|
|
53
90
|
def check_cursor!(cursor)
|
|
54
91
|
check_active!
|
|
55
92
|
raise ExpiredViewError, "cursor was discarded by restore" unless @cursors.key?(cursor)
|
|
56
93
|
end
|
|
57
94
|
|
|
95
|
+
# Checks cursor ownership and validates its replacement slot.
|
|
96
|
+
# @param cursor [Cursor] view locating this collection or value
|
|
97
|
+
# @param value [Slot] replacement slot to validate
|
|
98
|
+
# @return [void]
|
|
99
|
+
# @api private
|
|
58
100
|
def check_replacement!(cursor, value)
|
|
59
101
|
check_cursor!(cursor)
|
|
60
|
-
validate_slot(value)
|
|
102
|
+
validate_slot!(value)
|
|
61
103
|
end
|
|
62
104
|
|
|
105
|
+
# Returns the storage length visible to this transaction.
|
|
106
|
+
# @return [Integer]
|
|
63
107
|
def length
|
|
64
108
|
check_active!
|
|
65
109
|
@store.length
|
|
66
110
|
end
|
|
67
111
|
|
|
112
|
+
# Appends bytes and returns the absolute offset of the added data.
|
|
113
|
+
# @param bytes [String] binary input bytes
|
|
114
|
+
# @return [Integer]
|
|
68
115
|
def append(bytes)
|
|
69
116
|
check_active!
|
|
70
117
|
@store.append(bytes)
|
|
71
118
|
end
|
|
72
119
|
|
|
120
|
+
# Reads bytes within the transaction's current storage boundary.
|
|
121
|
+
# @param offset [Integer] absolute byte offset
|
|
122
|
+
# @param size [Integer] number of bytes
|
|
123
|
+
# @return [String]
|
|
73
124
|
def read_at(offset, size)
|
|
74
125
|
check_active!
|
|
75
126
|
@database.read_at(offset, size, bound: length)
|
|
76
127
|
end
|
|
77
128
|
|
|
78
|
-
#
|
|
129
|
+
# Changes allocated bytes at or above the copy-on-write boundary.
|
|
130
|
+
# @param offset [Integer] absolute byte offset
|
|
131
|
+
# @param bytes [String] binary input bytes
|
|
132
|
+
# @return [Integer]
|
|
133
|
+
# @raise [TransactionStateError] if offset is below mutable_start
|
|
134
|
+
# @raise [RangeError] if the write extends beyond allocated data
|
|
79
135
|
def write_at(offset, bytes)
|
|
80
136
|
check_active!
|
|
81
137
|
raise TransactionStateError, "write requires copy-on-write" if offset < @mutable_start
|
|
@@ -84,11 +140,16 @@ module XitDB
|
|
|
84
140
|
@store.write_at(offset, bytes)
|
|
85
141
|
end
|
|
86
142
|
|
|
143
|
+
# Moves the copy-on-write boundary to the current storage end.
|
|
144
|
+
# @return [Integer]
|
|
145
|
+
# @note This protects stored blocks. It does not call Object#freeze on the transaction.
|
|
87
146
|
def freeze
|
|
88
147
|
check_active!
|
|
89
148
|
@mutable_start = length
|
|
90
149
|
end
|
|
91
150
|
|
|
151
|
+
# Saves cursor states and storage length for a later rollback.
|
|
152
|
+
# @return [Checkpoint]
|
|
92
153
|
def checkpoint
|
|
93
154
|
check_active!
|
|
94
155
|
# @type var slots: Hash[Cursor, cursor_state]
|
|
@@ -99,6 +160,11 @@ module XitDB
|
|
|
99
160
|
checkpoint
|
|
100
161
|
end
|
|
101
162
|
|
|
163
|
+
# Rolls back bytes and cursors, consuming the supplied checkpoint and newer checkpoints.
|
|
164
|
+
# @param checkpoint [Checkpoint] live rollback state created by this transaction
|
|
165
|
+
# @return [nil]
|
|
166
|
+
# @raise [TransactionStateError] if the checkpoint is foreign, consumed, or no longer valid
|
|
167
|
+
# @note Cursors created after this checkpoint become expired views.
|
|
102
168
|
def restore(checkpoint)
|
|
103
169
|
check_active!
|
|
104
170
|
index = @checkpoints.index { |item| item.equal?(checkpoint) }
|
|
@@ -118,7 +184,11 @@ module XitDB
|
|
|
118
184
|
nil
|
|
119
185
|
end
|
|
120
186
|
|
|
121
|
-
#
|
|
187
|
+
# Discards rollback state after a successful internal operation.
|
|
188
|
+
# @param checkpoint [Checkpoint] live rollback state created by this transaction
|
|
189
|
+
# @return [nil]
|
|
190
|
+
# @raise [TransactionStateError] if the checkpoint belongs to another transaction
|
|
191
|
+
# @api private
|
|
122
192
|
def release_checkpoint(checkpoint)
|
|
123
193
|
check_active!
|
|
124
194
|
unless checkpoint.is_a?(Checkpoint) && checkpoint.owner.equal?(self)
|
|
@@ -130,16 +200,23 @@ module XitDB
|
|
|
130
200
|
nil
|
|
131
201
|
end
|
|
132
202
|
|
|
203
|
+
# Marks this transaction so its root will not be committed.
|
|
204
|
+
# @return [Boolean]
|
|
133
205
|
def cancel_publication
|
|
134
206
|
check_active!
|
|
135
207
|
@cancelled = true
|
|
136
208
|
end
|
|
137
209
|
|
|
210
|
+
# Tests whether root publication has been cancelled.
|
|
211
|
+
# @return [Boolean]
|
|
138
212
|
def cancelled?
|
|
139
213
|
check_active!
|
|
140
214
|
@cancelled
|
|
141
215
|
end
|
|
142
216
|
|
|
217
|
+
# Ends the transaction and discards registered cursors and checkpoints.
|
|
218
|
+
# @return [void]
|
|
219
|
+
# @api private
|
|
143
220
|
def expire!
|
|
144
221
|
@active = false
|
|
145
222
|
@checkpoints.each { |checkpoint| checkpoint.slots.clear }
|
|
@@ -150,7 +227,8 @@ module XitDB
|
|
|
150
227
|
|
|
151
228
|
private
|
|
152
229
|
|
|
153
|
-
|
|
230
|
+
# Rejects values that are not slots with a recognized tag.
|
|
231
|
+
def validate_slot!(slot)
|
|
154
232
|
raise TypeError, "expected Slot" unless slot.is_a?(Slot)
|
|
155
233
|
raise CorruptionError, "unknown slot tag" unless Slot::TAGS.include?(slot.tag)
|
|
156
234
|
end
|
data/lib/xitdb/core.rb
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
# Loads the advanced binary storage API in XitDB::Core.
|
|
4
|
+
#
|
|
5
|
+
# A Slot describes one value. A Cursor locates it. A Database owns the byte
|
|
6
|
+
# store and publishes root changes through a transaction.
|
|
7
|
+
# Collections in this API accept slots and encoded keys, not ordinary Ruby values.
|
|
8
|
+
# Most applications should start with XitDB::Database.
|
|
9
|
+
#
|
|
10
|
+
# @see XitDB::Core::Database
|
|
11
|
+
# @see XitDB::Core::Slot
|
|
12
|
+
# @see XitDB::Core::Cursor
|
|
13
|
+
|
|
3
14
|
require_relative "errors"
|
|
4
15
|
require_relative "core/format"
|
|
5
16
|
require_relative "core/hasher"
|
|
@@ -11,6 +22,7 @@ require_relative "core/cursor"
|
|
|
11
22
|
require_relative "core/transaction"
|
|
12
23
|
require_relative "core/database"
|
|
13
24
|
require_relative "core/array_list"
|
|
25
|
+
require_relative "core/array_builder"
|
|
14
26
|
require_relative "core/b_tree"
|
|
15
27
|
require_relative "core/linked_array_list"
|
|
16
28
|
require_relative "core/hash_map"
|