zrepl/pruning/keep_last_n.go
Christian Schwarz d684302864 pruning: fix tests + implement 'not_replicated' and 'keep_regex' keep rule
tests expected that a KeepRule returns a *keep* list whereas it
actually returns a *destroy* list.
2018-08-30 11:46:47 +02:00

33 lines
548 B
Go

package pruning
import (
"github.com/pkg/errors"
"sort"
)
type KeepLastN struct {
n int
}
func NewKeepLastN(n int) (*KeepLastN, error) {
if n <= 0 {
return nil, errors.Errorf("must specify positive number as 'keep last count', got %d", n)
}
return &KeepLastN{n}, nil
}
func (k KeepLastN) KeepRule(snaps []Snapshot) (destroyList []Snapshot) {
if k.n > len(snaps) {
return []Snapshot{}
}
res := shallowCopySnapList(snaps)
sort.Slice(res, func(i, j int) bool {
return res[i].Date().After(res[j].Date())
})
return res[k.n:]
}