-
Notifications
You must be signed in to change notification settings - Fork 179
/
pruning.go
87 lines (71 loc) · 2.55 KB
/
pruning.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 types
import "fmt"
// Pruning option string constants
const (
PruningOptionDefault = "default"
PruningOptionEverything = "everything"
PruningOptionNothing = "nothing"
PruningOptionCustom = "custom"
)
var (
// PruneDefault defines a pruning strategy where the last 100 heights are kept
// in addition to every 10000th and where to-be pruned heights are pruned at
// every 10th height.
PruneDefault = NewPruningOptions(100, 10000, 10, 200)
// PruneEverything defines a pruning strategy where all committed heights are
// deleted, storing only the current height and where to-be pruned heights are
// pruned at every 10th height.
PruneEverything = NewPruningOptions(5, 0, 10, 5)
// PruneNothing defines a pruning strategy where all heights are kept on disk.
PruneNothing = NewPruningOptions(0, 1, 0, 1<<64-1)
)
// PruningOptions defines the pruning strategy used when determining which
// heights are removed from disk when committing state.
type PruningOptions struct {
// KeepRecent defines how many recent heights to keep on disk.
KeepRecent uint64
// KeepEvery defines how many offset heights are kept on disk past KeepRecent.
KeepEvery uint64
// Interval defines when the pruned heights are removed from disk.
Interval uint64
// MaxRetainNum defines how many historic states to keep on disk.
MaxRetainNum uint64
}
func NewPruningOptions(keepRecent, keepEvery, interval, maxRetainNum uint64) PruningOptions {
return PruningOptions{
KeepRecent: keepRecent,
KeepEvery: keepEvery,
Interval: interval,
MaxRetainNum: maxRetainNum,
}
}
func (po PruningOptions) Validate() error {
if po.KeepEvery == 0 && po.Interval == 0 {
return fmt.Errorf("invalid 'Interval' when pruning everything: %d", po.Interval)
}
if po.KeepEvery == 1 && po.Interval != 0 { // prune nothing
return fmt.Errorf("invalid 'Interval' when pruning nothing: %d", po.Interval)
}
if po.KeepEvery > 1 && po.Interval == 0 {
return fmt.Errorf("invalid 'Interval' when pruning: %d", po.Interval)
}
if po.KeepRecent > po.MaxRetainNum {
return fmt.Errorf("invalid 'KeepRecent' when pruning MaxRetainNum: %d", po.MaxRetainNum)
}
if po.KeepEvery > 0 && po.MaxRetainNum == 0 {
return fmt.Errorf("invalid 'KeepEvery' when pruning MaxRetainNum: %d", po.MaxRetainNum)
}
return nil
}
func NewPruningOptionsFromString(strategy string) PruningOptions {
switch strategy {
case PruningOptionEverything:
return PruneEverything
case PruningOptionNothing:
return PruneNothing
case PruningOptionDefault:
return PruneDefault
default:
return PruneDefault
}
}