journal_machines 0.7.1

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.
@@ -0,0 +1,49 @@
1
+ //! The two handle classes: values that compare and hash by the identity
2
+ //! they carry, so two answers naming the same pin or attempt are `==`.
3
+ use journal_machines::{Pin, PinKind, Replacement};
4
+ use magnus::{Ruby, Symbol};
5
+ use matryona_machines::ruby::symbol;
6
+
7
+ #[derive(PartialEq, Eq, Hash)]
8
+ #[magnus::wrap(class = "JournalMachines::Pin", free_immediately, size)]
9
+ pub struct PinHandle {
10
+ pub pin: Pin,
11
+ }
12
+
13
+ impl PinHandle {
14
+ pub fn kind(ruby: &Ruby, this: &Self) -> Symbol {
15
+ let name = match this.pin.kind() {
16
+ PinKind::Reader => "reader",
17
+ PinKind::Checkpoint => "checkpoint",
18
+ };
19
+ symbol(ruby, name)
20
+ }
21
+
22
+ pub const fn generation(&self) -> u64 {
23
+ self.pin.generation()
24
+ }
25
+
26
+ pub const fn scope(&self) -> u16 {
27
+ self.pin.scope()
28
+ }
29
+ }
30
+
31
+ #[derive(PartialEq, Eq, Hash)]
32
+ #[magnus::wrap(class = "JournalMachines::Replacement", free_immediately, size)]
33
+ pub struct ReplacementHandle {
34
+ pub replacement: Replacement,
35
+ }
36
+
37
+ impl ReplacementHandle {
38
+ pub const fn segment(&self) -> u64 {
39
+ self.replacement.segment().raw()
40
+ }
41
+
42
+ pub const fn attempt(&self) -> u64 {
43
+ self.replacement.attempt()
44
+ }
45
+
46
+ pub const fn scope(&self) -> u16 {
47
+ self.replacement.scope()
48
+ }
49
+ }
@@ -0,0 +1,86 @@
1
+ //! Private native entry point.
2
+ use crate::{Journal, PINS, PinHandle, ReplacementHandle, SEGMENTS, values};
3
+ use magnus::{
4
+ Error, RClass, Ruby, function, method,
5
+ prelude::*,
6
+ typed_data::{Hash, IsEql},
7
+ };
8
+ use matryona_machines::ruby::Class;
9
+
10
+ /// `==`, `eql?` and `hash` by the identity a handle carries.
11
+ macro_rules! value_object {
12
+ ($class:expr, $handle:ty) => {{
13
+ let class: RClass = $class;
14
+ class.define_method("==", method!(<$handle as IsEql>::is_eql, 1))?;
15
+ class.define_method("eql?", method!(<$handle as IsEql>::is_eql, 1))?;
16
+ class.define_method("hash", method!(<$handle as Hash>::hash, 0))?;
17
+ }};
18
+ }
19
+
20
+ #[magnus::init]
21
+ fn init(ruby: &Ruby) -> Result<(), Error> {
22
+ let module = ruby.define_module("JournalMachines")?;
23
+ module.const_set("SEGMENT_CAPACITY", SEGMENTS)?;
24
+ module.const_set("PIN_CAPACITY", PINS)?;
25
+ let pin = module.define_class("Pin", ruby.class_object())?;
26
+ pin.undef_default_alloc_func();
27
+ pin.define_method("kind", method!(PinHandle::kind, 0))?;
28
+ pin.define_method("generation", method!(PinHandle::generation, 0))?;
29
+ pin.define_method("scope", method!(PinHandle::scope, 0))?;
30
+ value_object!(pin, PinHandle);
31
+ let replacement = module.define_class("Replacement", ruby.class_object())?;
32
+ replacement.undef_default_alloc_func();
33
+ replacement.define_method("segment", method!(ReplacementHandle::segment, 0))?;
34
+ replacement.define_method("attempt", method!(ReplacementHandle::attempt, 0))?;
35
+ replacement.define_method("scope", method!(ReplacementHandle::scope, 0))?;
36
+ value_object!(replacement, ReplacementHandle);
37
+ crate::classes::resolve(ruby, module, pin, replacement)?;
38
+ let class = module.define_class("Journal", ruby.class_object())?;
39
+ class.undef_default_alloc_func();
40
+ class.define_singleton_method("__open", function!(Journal::open, 4))?;
41
+ class.define_method("scope", method!(Journal::scope, 0))?;
42
+ class.define_method("issued", method!(Journal::issued, 0))?;
43
+ class.define_singleton_method("__check_policy", function!(Journal::check_policy, 4))?;
44
+ class.define_singleton_method("__check_due", function!(values::check_due, 1))?;
45
+ class.define_singleton_method("__check_rolled", function!(values::check_rolled, 3))?;
46
+ class.define_singleton_method("__check_reclaimed", function!(values::check_reclaimed, 4))?;
47
+ class.define_singleton_method("__check_segment", function!(values::check_segment, 5))?;
48
+ class.define_method("__policy", method!(Journal::policy, 0))?;
49
+ class.define_method("size", method!(Journal::size, 0))?;
50
+ class.define_method("empty?", method!(Journal::is_empty, 0))?;
51
+ class.define_method("roll_decision", method!(Journal::roll_decision, 3))?;
52
+ class.define_method("roll", method!(Journal::roll, 3))?;
53
+ class.define_method("observe_live", method!(Journal::observe_live, 2))?;
54
+ class.define_method("active", method!(Journal::active, 0))?;
55
+ class.define_method("segment", method!(Journal::segment, 1))?;
56
+ class.define_method("covering", method!(Journal::covering, 1))?;
57
+ class.define_method("segments", method!(Journal::segments, 2))?;
58
+ class.define_method(
59
+ "compaction_candidates",
60
+ method!(Journal::compaction_candidates, 2),
61
+ )?;
62
+ class.define_method("reclaimable", method!(Journal::reclaimable, 2))?;
63
+ class.define_method("pin_reader", method!(Journal::pin_reader, 1))?;
64
+ class.define_method("pin_checkpoint", method!(Journal::pin_checkpoint, 2))?;
65
+ class.define_method("advance", method!(Journal::advance, 2))?;
66
+ class.define_method("checkpoint", method!(Journal::checkpoint, 3))?;
67
+ class.define_method(
68
+ "pin_checkpoint_from",
69
+ method!(Journal::pin_checkpoint_from, 1),
70
+ )?;
71
+ class.define_method("checkpoint_from", method!(Journal::checkpoint_from, 2))?;
72
+ class.define_method("release", method!(Journal::release, 1))?;
73
+ class.define_method("pin_offset", method!(Journal::pin_offset, 1))?;
74
+ class.define_method("low_pin", method!(Journal::low_pin, 0))?;
75
+ class.define_method("pins", method!(Journal::pins, 0))?;
76
+ class.define_method("propose", method!(Journal::propose, 1))?;
77
+ class.define_method("confirm_rewritten", method!(Journal::confirm_rewritten, 3))?;
78
+ class.define_method(
79
+ "confirm_superseded",
80
+ method!(Journal::confirm_superseded, 1),
81
+ )?;
82
+ class.define_method("reclaim", method!(Journal::reclaim, 1))?;
83
+ class.define_method("abort", method!(Journal::abort, 1))?;
84
+ // `__open` answers a Journal: its class is fixed now, like the handles'.
85
+ Class::pin_wrapped::<Journal>(ruby, class).map(drop)
86
+ }
@@ -0,0 +1,432 @@
1
+ //! Thin binding to the bounded segment metadata journal.
2
+ mod answers;
3
+ mod classes;
4
+ mod convert;
5
+ mod handles;
6
+ mod init;
7
+ mod values;
8
+
9
+ use answers::reply;
10
+ use classes::classes;
11
+ use convert::{answer, policy, policy_from};
12
+ use handles::{PinHandle, ReplacementHandle};
13
+
14
+ use std::{cell::RefCell, num::NonZeroU64};
15
+
16
+ use journal_machines::{
17
+ Advanced, Checkpointed, Confirmed, Durable, Pin, Refusal, Replacement, Roll, Segment, SegmentId,
18
+ };
19
+ use magnus::{Error, Integer, IntoValue, RArray, Ruby, Symbol, Value, prelude::*, typed_data::Obj};
20
+ use matryona_machines::ruby::{
21
+ Refused, SymbolNames, Symbols, class_name, duration, exact_array_n, exact_typed, integer, keep,
22
+ nanoseconds, sibling_why,
23
+ };
24
+
25
+ /// An offset tracker's assignment generation and committed base, in that
26
+ /// order: a sibling gem's value (`OffsetMachines::Tracker`), read by its
27
+ /// public `generation` and `base` readers. Both are asked for before either
28
+ /// runs, so a tracker missing one runs none.
29
+ fn tracker(ruby: &Ruby, value: Value) -> Result<[Value; 2], Error> {
30
+ sibling_why(ruby, value, ["generation", "base"], |why| match why {
31
+ Refused::MissingReader(reader) => format!(
32
+ "expected an offset tracker responding to #generation and #base, {} lacks #{reader}",
33
+ class_name(value)
34
+ ),
35
+ _ => format!(
36
+ "expected an offset tracker responding to #generation and #base, not {}",
37
+ class_name(value)
38
+ ),
39
+ })
40
+ }
41
+
42
+ /// The pin exactly a `Pin` handle names; `TypeError` for anything else, a
43
+ /// subclass instance included.
44
+ fn pin(ruby: &Ruby, value: Value) -> Result<Pin, Error> {
45
+ let class = classes(ruby)?.pin.get(ruby);
46
+ let message = "expected a JournalMachines::Pin";
47
+ exact_typed::<PinHandle>(ruby, value, class, message).map(|handle| handle.pin)
48
+ }
49
+
50
+ /// The attempt exactly a `Replacement` handle names; `TypeError` for
51
+ /// anything else, a subclass instance included.
52
+ fn replacement(ruby: &Ruby, value: Value) -> Result<Replacement, Error> {
53
+ let class = classes(ruby)?.replacement.get(ruby);
54
+ let message = "expected a JournalMachines::Replacement";
55
+ exact_typed::<ReplacementHandle>(ruby, value, class, message).map(|handle| handle.replacement)
56
+ }
57
+
58
+ /// A segment id given as an Integer named `name`.
59
+ fn id(value: Value, name: &str) -> Result<SegmentId, Error> {
60
+ integer(value, name).map(SegmentId::from_raw)
61
+ }
62
+
63
+ const SEGMENTS: usize = 64;
64
+
65
+ /// Which of the core's three segment iterators a scan reads.
66
+ #[derive(Clone, Copy)]
67
+ enum Scan {
68
+ All,
69
+ Candidates,
70
+ Reclaimable,
71
+ }
72
+ const PINS: usize = 32;
73
+
74
+ #[magnus::wrap(class = "JournalMachines::Journal", free_immediately, size)]
75
+ struct Journal {
76
+ core: RefCell<journal_machines::Journal<SEGMENTS, PINS>>,
77
+ }
78
+
79
+ impl Journal {
80
+ /// `Journal::resumed`; `Journal::with_scope` is the same with nothing
81
+ /// issued. Raises `RangeError` where the core answers `None`.
82
+ fn open(
83
+ ruby: &Ruby,
84
+ base_offset: Value,
85
+ now: Value,
86
+ pair: Value,
87
+ policy: Value,
88
+ ) -> Result<Self, Error> {
89
+ let (base, now) = (integer(base_offset, "base_offset")?, duration(now, "now")?);
90
+ let [scope, issued] =
91
+ exact_array_n::<2>(ruby, pair, "scope and issued must be an Array of two")?;
92
+ let (scope, issued) = (integer(scope, "scope")?, integer(issued, "issued")?);
93
+ let policy = policy_from(ruby, policy)?;
94
+ journal_machines::Journal::resumed(policy, base, now, scope, issued)
95
+ .map(|core| Self {
96
+ core: RefCell::new(core),
97
+ })
98
+ .ok_or_else(|| {
99
+ Error::new(
100
+ ruby.exception_range_error(),
101
+ "issued must leave an identity for the active segment: 0..2**48-2",
102
+ )
103
+ })
104
+ }
105
+
106
+ fn scope(&self) -> u16 {
107
+ self.core.borrow().scope()
108
+ }
109
+
110
+ fn issued(&self) -> u64 {
111
+ self.core.borrow().issued()
112
+ }
113
+
114
+ /// Raises where `Policy::new` answers `None`; `Policy.new` calls it.
115
+ fn check_policy(
116
+ ruby: &Ruby,
117
+ max_bytes: Value,
118
+ max_records: Value,
119
+ max_age: Value,
120
+ min_dead_per_mille: Value,
121
+ ) -> Result<(), Error> {
122
+ policy(ruby, max_bytes, max_records, max_age, min_dead_per_mille).map(drop)
123
+ }
124
+
125
+ /// The four `Policy` fields, in order.
126
+ fn policy(ruby: &Ruby, this: &Self) -> (Option<u64>, Option<u64>, Option<Integer>, u16) {
127
+ let policy = this.core.borrow().policy();
128
+ (
129
+ policy.max_bytes().map(NonZeroU64::get),
130
+ policy.max_records().map(NonZeroU64::get),
131
+ policy.max_age().map(|age| nanoseconds(ruby, age)),
132
+ policy.min_dead_per_mille(),
133
+ )
134
+ }
135
+
136
+ fn size(&self) -> usize {
137
+ self.core.borrow().len()
138
+ }
139
+
140
+ fn is_empty(&self) -> bool {
141
+ self.core.borrow().is_empty()
142
+ }
143
+
144
+ /// `:keep`, a `Due` naming the threshold reached, or a refusal.
145
+ fn roll_decision(
146
+ ruby: &Ruby,
147
+ this: &Self,
148
+ bytes: Value,
149
+ records: Value,
150
+ now: Value,
151
+ ) -> Result<Value, Error> {
152
+ let (bytes, records) = (integer(bytes, "bytes")?, integer(records, "records")?);
153
+ let now = duration(now, "now")?;
154
+ let due = classes(ruby)?.due.get(ruby);
155
+ let decision = this.core.borrow().roll_decision(bytes, records, now);
156
+ reply(ruby, decision, |roll| match roll {
157
+ Roll::Keep => Ok(answers::Rolls::symbol_of(ruby, Roll::Keep).as_value()),
158
+ Roll::Due(reason) => due.funcall("new", (answers::Reasons::symbol(ruby, reason),)),
159
+ })
160
+ }
161
+
162
+ fn roll(
163
+ ruby: &Ruby,
164
+ this: Obj<Self>,
165
+ bytes: Value,
166
+ records: Value,
167
+ now: Value,
168
+ ) -> Result<Value, Error> {
169
+ this.check_frozen()?;
170
+ let (bytes, records) = (integer(bytes, "bytes")?, integer(records, "records")?);
171
+ let now = duration(now, "now")?;
172
+ let class = classes(ruby)?.rolled.get(ruby);
173
+ keep(
174
+ ruby,
175
+ this,
176
+ &this.core,
177
+ |core| core.roll(bytes, records, now),
178
+ |rolled| reply(ruby, rolled, |rolled| answers::rolled(class, rolled)),
179
+ )
180
+ }
181
+
182
+ fn observe_live(ruby: &Ruby, this: Obj<Self>, id: Value, live: Value) -> Result<Symbol, Error> {
183
+ this.check_frozen()?;
184
+ let (id, live) = (self::id(id, "id")?, integer(live, "live_records")?);
185
+ let outcome = this.core.borrow_mut().observe_live(id, live);
186
+ Ok(answer(ruby, outcome, |()| "recorded"))
187
+ }
188
+
189
+ /// One read of the journal answered as a `Segment` or `nil`.
190
+ fn view(
191
+ ruby: &Ruby,
192
+ this: &Self,
193
+ read: impl FnOnce(&journal_machines::Journal<SEGMENTS, PINS>) -> Option<Segment>,
194
+ ) -> Result<Option<Value>, Error> {
195
+ let class = classes(ruby)?.segment.get(ruby);
196
+ let segment = read(&this.core.borrow());
197
+ segment
198
+ .map(|segment| answers::segment(ruby, class, segment))
199
+ .transpose()
200
+ }
201
+
202
+ fn active(ruby: &Ruby, this: &Self) -> Result<Option<Value>, Error> {
203
+ Self::view(ruby, this, journal_machines::Journal::active)
204
+ }
205
+
206
+ fn segment(ruby: &Ruby, this: &Self, id: Value) -> Result<Option<Value>, Error> {
207
+ let id = self::id(id, "id")?;
208
+ Self::view(ruby, this, |core| core.segment(id))
209
+ }
210
+
211
+ fn covering(ruby: &Ruby, this: &Self, offset: Value) -> Result<Option<Value>, Error> {
212
+ let offset = integer(offset, "offset")?;
213
+ Self::view(ruby, this, |core| core.covering(offset))
214
+ }
215
+
216
+ /// At most `limit` rows (clamped to the capacity) of one scan after
217
+ /// segment `after`. The views are read into a Rust buffer first, so no
218
+ /// Ruby value is built while the journal is borrowed.
219
+ fn scan(
220
+ ruby: &Ruby,
221
+ this: &Self,
222
+ after: Option<Value>,
223
+ limit: Value,
224
+ scan: Scan,
225
+ ) -> Result<RArray, Error> {
226
+ let after = after.map(|after| id(after, "after")).transpose()?;
227
+ let limit: u64 = integer(limit, "limit")?;
228
+ let take = usize::try_from(limit).map_or(SEGMENTS, |limit| limit.min(SEGMENTS));
229
+ let class = classes(ruby)?.segment.get(ruby);
230
+ let views: Vec<Segment> = {
231
+ let core = this.core.borrow();
232
+ match scan {
233
+ Scan::All => core.segments(after).take(take).collect(),
234
+ Scan::Candidates => core.compaction_candidates(after).take(take).collect(),
235
+ Scan::Reclaimable => core.reclaimable(after).take(take).collect(),
236
+ }
237
+ };
238
+ let rows = ruby.ary_new_capa(views.len());
239
+ for segment in views {
240
+ rows.push(answers::segment(ruby, class, segment)?)?;
241
+ }
242
+ Ok(rows)
243
+ }
244
+
245
+ fn segments(
246
+ ruby: &Ruby,
247
+ this: &Self,
248
+ after: Option<Value>,
249
+ limit: Value,
250
+ ) -> Result<RArray, Error> {
251
+ Self::scan(ruby, this, after, limit, Scan::All)
252
+ }
253
+
254
+ fn compaction_candidates(
255
+ ruby: &Ruby,
256
+ this: &Self,
257
+ after: Option<Value>,
258
+ limit: Value,
259
+ ) -> Result<RArray, Error> {
260
+ Self::scan(ruby, this, after, limit, Scan::Candidates)
261
+ }
262
+
263
+ fn reclaimable(
264
+ ruby: &Ruby,
265
+ this: &Self,
266
+ after: Option<Value>,
267
+ limit: Value,
268
+ ) -> Result<RArray, Error> {
269
+ Self::scan(ruby, this, after, limit, Scan::Reclaimable)
270
+ }
271
+
272
+ fn pinned(ruby: &Ruby, pin: Result<Pin, Refusal>) -> Result<Value, Error> {
273
+ reply(ruby, pin, |pin| Ok(PinHandle { pin }.into_value_with(ruby)))
274
+ }
275
+
276
+ fn pin_reader(ruby: &Ruby, this: Obj<Self>, offset: Value) -> Result<Value, Error> {
277
+ this.check_frozen()?;
278
+ let offset = integer(offset, "offset")?;
279
+ let pin = this.core.borrow_mut().pin_reader(offset);
280
+ Self::pinned(ruby, pin)
281
+ }
282
+
283
+ fn pin_checkpoint(
284
+ ruby: &Ruby,
285
+ this: Obj<Self>,
286
+ assignment: Value,
287
+ offset: Value,
288
+ ) -> Result<Value, Error> {
289
+ this.check_frozen()?;
290
+ let assignment = integer(assignment, "assignment")?;
291
+ let offset = integer(offset, "offset")?;
292
+ let pin = this.core.borrow_mut().pin_checkpoint(assignment, offset);
293
+ Self::pinned(ruby, pin)
294
+ }
295
+
296
+ /// `pin_checkpoint` at a tracker's committed base, under its generation,
297
+ /// both read from the one tracker before anything else is converted.
298
+ fn pin_checkpoint_from(ruby: &Ruby, this: Obj<Self>, source: Value) -> Result<Value, Error> {
299
+ this.check_frozen()?;
300
+ let [assignment, offset] = tracker(ruby, source)?;
301
+ Self::pin_checkpoint(ruby, this, assignment, offset)
302
+ }
303
+
304
+ /// `checkpoint` to a tracker's committed base, under its generation.
305
+ fn checkpoint_from(
306
+ ruby: &Ruby,
307
+ this: Obj<Self>,
308
+ handle: Value,
309
+ source: Value,
310
+ ) -> Result<Symbol, Error> {
311
+ this.check_frozen()?;
312
+ let [assignment, offset] = tracker(ruby, source)?;
313
+ Self::checkpoint(ruby, this, handle, assignment, offset)
314
+ }
315
+
316
+ fn advance(
317
+ ruby: &Ruby,
318
+ this: Obj<Self>,
319
+ handle: Value,
320
+ offset: Value,
321
+ ) -> Result<Symbol, Error> {
322
+ this.check_frozen()?;
323
+ let (pin, offset) = (pin(ruby, handle)?, integer(offset, "offset")?);
324
+ let outcome = this.core.borrow_mut().advance(pin, offset);
325
+ Ok(answer(ruby, outcome, |advanced| match advanced {
326
+ Advanced::Unchanged => "unchanged",
327
+ Advanced::Moved => "moved",
328
+ }))
329
+ }
330
+
331
+ fn checkpoint(
332
+ ruby: &Ruby,
333
+ this: Obj<Self>,
334
+ handle: Value,
335
+ assignment: Value,
336
+ offset: Value,
337
+ ) -> Result<Symbol, Error> {
338
+ this.check_frozen()?;
339
+ let (pin, assignment) = (pin(ruby, handle)?, integer(assignment, "assignment")?);
340
+ let offset = integer(offset, "offset")?;
341
+ let outcome = this.core.borrow_mut().checkpoint(pin, assignment, offset);
342
+ Ok(answer(ruby, outcome, |checkpointed| match checkpointed {
343
+ Checkpointed::Reassigned => "reassigned",
344
+ Checkpointed::Unchanged => "unchanged",
345
+ Checkpointed::Advanced => "advanced",
346
+ }))
347
+ }
348
+
349
+ fn release(ruby: &Ruby, this: Obj<Self>, handle: Value) -> Result<Symbol, Error> {
350
+ this.check_frozen()?;
351
+ let pin = pin(ruby, handle)?;
352
+ let outcome = this.core.borrow_mut().release(pin);
353
+ Ok(answer(ruby, outcome, |()| "released"))
354
+ }
355
+
356
+ fn pin_offset(ruby: &Ruby, this: &Self, handle: Value) -> Result<Option<u64>, Error> {
357
+ let pin = pin(ruby, handle)?;
358
+ Ok(this.core.borrow().pin_offset(pin))
359
+ }
360
+
361
+ fn low_pin(&self) -> Option<u64> {
362
+ self.core.borrow().low_pin()
363
+ }
364
+
365
+ fn pins(&self) -> usize {
366
+ self.core.borrow().pins()
367
+ }
368
+
369
+ fn propose(ruby: &Ruby, this: Obj<Self>, id: Value) -> Result<Value, Error> {
370
+ this.check_frozen()?;
371
+ let id = self::id(id, "id")?;
372
+ let proposed = this.core.borrow_mut().propose(id);
373
+ reply(ruby, proposed, |replacement| {
374
+ Ok(ReplacementHandle { replacement }.into_value_with(ruby))
375
+ })
376
+ }
377
+
378
+ /// Called once the receiver is known mutable and the arguments read.
379
+ fn confirm(ruby: &Ruby, this: Obj<Self>, handle: Replacement, durable: Durable) -> Symbol {
380
+ let outcome = this.core.borrow_mut().confirm_durable(handle, durable);
381
+ answer(ruby, outcome, |confirmed| match confirmed {
382
+ Confirmed::AlreadyDurable => "already_durable",
383
+ Confirmed::Recorded => "recorded",
384
+ })
385
+ }
386
+
387
+ fn confirm_rewritten(
388
+ ruby: &Ruby,
389
+ this: Obj<Self>,
390
+ handle: Value,
391
+ bytes: Value,
392
+ live_records: Value,
393
+ ) -> Result<Symbol, Error> {
394
+ this.check_frozen()?;
395
+ let handle = replacement(ruby, handle)?;
396
+ let durable = Durable::Rewritten {
397
+ bytes: integer(bytes, "bytes")?,
398
+ live_records: integer(live_records, "live_records")?,
399
+ };
400
+ Ok(Self::confirm(ruby, this, handle, durable))
401
+ }
402
+
403
+ fn confirm_superseded(ruby: &Ruby, this: Obj<Self>, handle: Value) -> Result<Symbol, Error> {
404
+ this.check_frozen()?;
405
+ let handle = replacement(ruby, handle)?;
406
+ Ok(Self::confirm(ruby, this, handle, Durable::Superseded))
407
+ }
408
+
409
+ fn reclaim(ruby: &Ruby, this: Obj<Self>, handle: Value) -> Result<Value, Error> {
410
+ this.check_frozen()?;
411
+ let handle = replacement(ruby, handle)?;
412
+ let class = classes(ruby)?.reclaimed.get(ruby);
413
+ keep(
414
+ ruby,
415
+ this,
416
+ &this.core,
417
+ |core| core.reclaim(handle),
418
+ |outcome| {
419
+ reply(ruby, outcome, |reclaimed| {
420
+ answers::reclaimed(class, reclaimed)
421
+ })
422
+ },
423
+ )
424
+ }
425
+
426
+ fn abort(ruby: &Ruby, this: Obj<Self>, handle: Value) -> Result<Symbol, Error> {
427
+ this.check_frozen()?;
428
+ let handle = replacement(ruby, handle)?;
429
+ let outcome = this.core.borrow_mut().abort(handle);
430
+ Ok(answer(ruby, outcome, |()| "aborted"))
431
+ }
432
+ }
@@ -0,0 +1,97 @@
1
+ //! Private hooks the Ruby `Data` values validate with: each builds the core
2
+ //! value and raises where the core's constructor answers `None`.
3
+ use journal_machines::{Reclaimed, Rolled, Segment, SegmentId};
4
+ use magnus::{Error, Ruby, Value};
5
+ use matryona_machines::ruby::{
6
+ Symbols, duration, exact_array_n, integer, optional, optional_typed,
7
+ };
8
+
9
+ use crate::{
10
+ ReplacementHandle,
11
+ answers::{Reasons, Retentions, States},
12
+ classes::classes,
13
+ };
14
+
15
+ fn range(ruby: &Ruby, message: &'static str) -> Error {
16
+ Error::new(ruby.exception_range_error(), message)
17
+ }
18
+
19
+ fn id(value: Value, name: &str) -> Result<SegmentId, Error> {
20
+ integer(value, name).map(SegmentId::from_raw)
21
+ }
22
+
23
+ /// `Due.new`: the core's `Roll::Due(reason)` for one of its three reasons.
24
+ pub fn check_due(ruby: &Ruby, value: Value) -> Result<(), Error> {
25
+ Reasons::parse(ruby, value).map(drop)
26
+ }
27
+
28
+ /// `Rolled.new`: the core's `Rolled::new`.
29
+ pub fn check_rolled(
30
+ ruby: &Ruby,
31
+ sealed: Value,
32
+ opened: Value,
33
+ next_offset: Value,
34
+ ) -> Result<(), Error> {
35
+ let (sealed, opened) = (id(sealed, "sealed")?, id(opened, "opened")?);
36
+ Rolled::new(sealed, opened, integer(next_offset, "next_offset")?)
37
+ .map(drop)
38
+ .ok_or_else(|| range(ruby, "no journal records this roll"))
39
+ }
40
+
41
+ /// `Reclaimed.new`: the core's `Reclaimed::new`.
42
+ pub fn check_reclaimed(
43
+ ruby: &Ruby,
44
+ segment: Value,
45
+ revision: Value,
46
+ bytes: Value,
47
+ replaced_by: Value,
48
+ ) -> Result<(), Error> {
49
+ let (segment, revision) = (id(segment, "segment")?, integer(revision, "revision")?);
50
+ let bytes = integer(bytes, "bytes")?;
51
+ let replaced_by = optional(replaced_by, "replaced_by")?;
52
+ Reclaimed::new(segment, revision, bytes, replaced_by)
53
+ .map(drop)
54
+ .ok_or_else(|| range(ruby, "no journal answers this reclaim"))
55
+ }
56
+
57
+ /// `Segment.new`: the core's `Segment::checked`. `head` is id, revision,
58
+ /// base offset and records; `counts` stored and live records, bytes and the
59
+ /// opening instant in nanoseconds.
60
+ pub fn check_segment(
61
+ ruby: &Ruby,
62
+ head: Value,
63
+ counts: Value,
64
+ state_name: Value,
65
+ retention: Value,
66
+ replacement: Value,
67
+ ) -> Result<(), Error> {
68
+ let [id_value, revision, base_offset, records] =
69
+ exact_array_n::<4>(ruby, head, "head must be an Array of four")?;
70
+ let [stored_records, live_records, bytes, opened_at] =
71
+ exact_array_n::<4>(ruby, counts, "counts must be an Array of four")?;
72
+ let (id, revision) = (id(id_value, "id")?, integer(revision, "revision")?);
73
+ let base_offset = integer(base_offset, "base_offset")?;
74
+ let records = integer(records, "records")?;
75
+ let stored_records = integer(stored_records, "stored_records")?;
76
+ let live_records = integer(live_records, "live_records")?;
77
+ let (bytes, opened_at) = (integer(bytes, "bytes")?, duration(opened_at, "opened_at")?);
78
+ // After the Integers, as when magnus converted the arguments in order.
79
+ let class = classes(ruby)?.replacement.get(ruby);
80
+ let message = "replacement must be a JournalMachines::Replacement or nil";
81
+ let replacement = optional_typed::<ReplacementHandle>(ruby, replacement, class, message)?;
82
+ Segment::new(
83
+ id,
84
+ revision,
85
+ base_offset,
86
+ records,
87
+ stored_records,
88
+ live_records,
89
+ bytes,
90
+ opened_at,
91
+ States::parse(ruby, state_name)?,
92
+ Retentions::parse(ruby, retention)?,
93
+ replacement.map(|handle| handle.replacement),
94
+ )
95
+ .map(drop)
96
+ .ok_or_else(|| range(ruby, "no journal answers this segment"))
97
+ }
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+ module JournalMachines
3
+ VERSION = '0.7.1'
4
+ end