forked from zrepl/zrepl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pruning.go
71 lines (59 loc) · 1.5 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
package pruning
import (
"fmt"
"time"
"github.com/pkg/errors"
"github.com/zrepl/zrepl/config"
)
type KeepRule interface {
KeepRule(snaps []Snapshot) (destroyList []Snapshot)
}
type Snapshot interface {
Name() string
Replicated() bool
Date() time.Time
}
// The returned snapshot list is guaranteed to only contains elements of input parameter snaps
func PruneSnapshots(snaps []Snapshot, keepRules []KeepRule) []Snapshot {
if len(keepRules) == 0 {
return []Snapshot{}
}
remCount := make(map[Snapshot]int, len(snaps))
for _, r := range keepRules {
ruleRems := r.KeepRule(snaps)
for _, ruleRem := range ruleRems {
remCount[ruleRem]++
}
}
remove := make([]Snapshot, 0, len(snaps))
for snap, rc := range remCount {
if rc == len(keepRules) {
remove = append(remove, snap)
}
}
return remove
}
func RulesFromConfig(in []config.PruningEnum) (rules []KeepRule, err error) {
rules = make([]KeepRule, len(in))
for i := range in {
rules[i], err = RuleFromConfig(in[i])
if err != nil {
return nil, errors.Wrapf(err, "cannot build rule #%d", i)
}
}
return rules, nil
}
func RuleFromConfig(in config.PruningEnum) (KeepRule, error) {
switch v := in.Ret.(type) {
case *config.PruneKeepNotReplicated:
return NewKeepNotReplicated(), nil
case *config.PruneKeepLastN:
return NewKeepLastN(v.Count)
case *config.PruneKeepRegex:
return NewKeepRegex(v.Regex, v.Negate)
case *config.PruneGrid:
return NewKeepGrid(v)
default:
return nil, fmt.Errorf("unknown keep rule type %T", v)
}
}