-
Notifications
You must be signed in to change notification settings - Fork 178
/
snapshot_tree.go
87 lines (73 loc) · 2.07 KB
/
snapshot_tree.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package primary
import (
"fmt"
"github.com/onflow/flow-go/fvm/storage/logical"
"github.com/onflow/flow-go/fvm/storage/snapshot"
)
type timestampedSnapshotTree struct {
currentSnapshotTime logical.Time
baseSnapshotTime logical.Time
snapshot.SnapshotTree
fullLog snapshot.UpdateLog
}
func newTimestampedSnapshotTree(
storageSnapshot snapshot.StorageSnapshot,
snapshotTime logical.Time,
) timestampedSnapshotTree {
return timestampedSnapshotTree{
currentSnapshotTime: snapshotTime,
baseSnapshotTime: snapshotTime,
SnapshotTree: snapshot.NewSnapshotTree(storageSnapshot),
fullLog: nil,
}
}
func (tree timestampedSnapshotTree) Append(
executionSnapshot *snapshot.ExecutionSnapshot,
) timestampedSnapshotTree {
return timestampedSnapshotTree{
currentSnapshotTime: tree.currentSnapshotTime + 1,
baseSnapshotTime: tree.baseSnapshotTime,
SnapshotTree: tree.SnapshotTree.Append(executionSnapshot),
fullLog: append(tree.fullLog, executionSnapshot.WriteSet),
}
}
func (tree timestampedSnapshotTree) SnapshotTime() logical.Time {
return tree.currentSnapshotTime
}
func (tree timestampedSnapshotTree) UpdatesSince(
snapshotTime logical.Time,
) (
snapshot.UpdateLog,
error,
) {
if snapshotTime < tree.baseSnapshotTime {
// This should never happen.
return nil, fmt.Errorf(
"missing update log range [%v, %v)",
snapshotTime,
tree.baseSnapshotTime)
}
if snapshotTime > tree.currentSnapshotTime {
// This should never happen.
return nil, fmt.Errorf(
"missing update log range (%v, %v]",
tree.currentSnapshotTime,
snapshotTime)
}
return tree.fullLog[int(snapshotTime-tree.baseSnapshotTime):], nil
}
type rebaseableTimestampedSnapshotTree struct {
timestampedSnapshotTree
}
func newRebaseableTimestampedSnapshotTree(
snapshotTree timestampedSnapshotTree,
) *rebaseableTimestampedSnapshotTree {
return &rebaseableTimestampedSnapshotTree{
timestampedSnapshotTree: snapshotTree,
}
}
func (tree *rebaseableTimestampedSnapshotTree) Rebase(
base timestampedSnapshotTree,
) {
tree.timestampedSnapshotTree = base
}