Class: Evinrude::Log
Defined Under Namespace
Classes: SnapshottedEntryError, TruncationUnderflowError
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(logger:, snapshot_last_term: 0, snapshot_last_index: 0) ⇒ Log
Returns a new instance of Log.
12
13
14
15
16
|
# File 'lib/evinrude/log.rb', line 12
def initialize(logger:, snapshot_last_term: 0, snapshot_last_index: 0)
@logger, @snapshot_last_term, @snapshot_last_index = logger, snapshot_last_term, snapshot_last_index
@entries = []
end
|
Instance Attribute Details
#snapshot_last_index ⇒ Object
Returns the value of attribute snapshot_last_index.
10
11
12
|
# File 'lib/evinrude/log.rb', line 10
def snapshot_last_index
@snapshot_last_index
end
|
#snapshot_last_term ⇒ Object
Returns the value of attribute snapshot_last_term.
10
11
12
|
# File 'lib/evinrude/log.rb', line 10
def snapshot_last_term
@snapshot_last_term
end
|
Instance Method Details
#[](n) ⇒ Object
82
83
84
85
86
87
88
89
90
|
# File 'lib/evinrude/log.rb', line 82
def [](n)
if n == 0
zeroth_log_entry
elsif n <= @snapshot_last_index
raise SnapshottedEntryError
else
@entries[n - @snapshot_last_index - 1]
end
end
|
#append(entry) ⇒ Object
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/evinrude/log.rb', line 18
def append(entry)
logger.debug(logloc) { "Appending new entry #{entry.inspect} as ##{@snapshot_last_index + @entries.length + 1}" }
@entries << entry
if @entries.length > 1000
old_len = @entries.length
snapshotted_entries = @entries[0..499]
@entries = @entries[500..]
@snapshot_last_index += 500
@snapshot_last_term = snapshotted_entries.last.term
end
end
|
#entries_from(n) ⇒ Object
66
67
68
|
# File 'lib/evinrude/log.rb', line 66
def entries_from(n)
@entries[(n-@snapshot_last_index-1)..] || []
end
|
#entry_term(n) ⇒ Object
58
59
60
61
62
63
64
|
# File 'lib/evinrude/log.rb', line 58
def entry_term(n)
if n == @snapshot_last_index
@snapshot_last_term
else
self[n]&.term
end
end
|
#has_entry?(n) ⇒ Boolean
38
39
40
|
# File 'lib/evinrude/log.rb', line 38
def has_entry?(n)
n == 0 || n <= @snapshot_last_index + @entries.length
end
|
#last_entry_term ⇒ Object
50
51
52
53
54
55
56
|
# File 'lib/evinrude/log.rb', line 50
def last_entry_term
if @entries.empty?
@snapshot_last_term
else
@entries.last.term
end
end
|
#last_index ⇒ Object
46
47
48
|
# File 'lib/evinrude/log.rb', line 46
def last_index
@entries.length + @snapshot_last_index
end
|
#new_snapshot(last_term, last_index) ⇒ Object
31
32
33
34
35
36
|
# File 'lib/evinrude/log.rb', line 31
def new_snapshot(last_term, last_index)
@snapshot_last_term = last_term
@snapshot_last_index = last_index
@entries = []
end
|
#snapshotted_entry?(n) ⇒ Boolean
42
43
44
|
# File 'lib/evinrude/log.rb', line 42
def snapshotted_entry?(n)
n > 0 && n <= @snapshot_last_index
end
|
#truncate_to(n) ⇒ Object
Make the last entry kept in the log the nth.
71
72
73
74
75
76
77
78
79
80
|
# File 'lib/evinrude/log.rb', line 71
def truncate_to(n)
if n > @snapshot_last_index
@entries = @entries[0..n-@snapshot_last_index-1]
elsif n == @snapshot_last_index
@entries = []
else
raise TruncationUnderflowError,
"Cannot truncate to log entry ##{n}; into the snapshot"
end
end
|