start pruning reimplementation in cmd/pruning subpackage

This commit is contained in:
Christian Schwarz
2018-08-27 15:09:24 +02:00
parent b4ea5f56b2
commit ecd9db4ac6
6 changed files with 231 additions and 0 deletions
+39
View File
@@ -0,0 +1,39 @@
package pruning
import (
"time"
)
type KeepRule interface {
KeepRule(snaps []Snapshot) []Snapshot
}
type Snapshot interface {
Name() string
Replicated() bool
Date() time.Time
}
func PruneSnapshots(snaps []Snapshot, keepRules []KeepRule) []Snapshot {
if len(keepRules) == 0 {
return snaps
}
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
}