refactor: parametrize PrefixFilter VersionType check

refs #34
This commit is contained in:
Christian Schwarz 2017-11-12 23:02:23 +01:00
parent cef63ac176
commit 51af880701
5 changed files with 21 additions and 17 deletions

View File

@ -47,7 +47,7 @@ func (a *IntervalAutosnap) Run(ctx context.Context, didSnaps chan struct{}) {
l := a.log.WithField(logFSField, d.ToString())
fsvs, err := zfs.ZFSListFilesystemVersions(d, &PrefixSnapshotFilter{a.Prefix})
fsvs, err := zfs.ZFSListFilesystemVersions(d, NewTypedPrefixFilter(a.Prefix, zfs.Snapshot))
if err != nil {
l.WithError(err).Error("cannot list filesystem versions")
continue

View File

@ -6,8 +6,18 @@ import (
"strings"
)
type PrefixSnapshotFilter struct {
Prefix string
type PrefixFilter struct {
prefix string
fstype zfs.VersionType
fstypeSet bool // optionals anyone?
}
func NewPrefixFilter(prefix string) *PrefixFilter {
return &PrefixFilter{prefix: prefix}
}
func NewTypedPrefixFilter(prefix string, versionType zfs.VersionType) *PrefixFilter {
return &PrefixFilter{prefix, versionType, true}
}
func parseSnapshotPrefix(i string) (p string, err error) {
@ -19,15 +29,8 @@ func parseSnapshotPrefix(i string) (p string, err error) {
return
}
func parsePrefixSnapshotFilter(i string) (f *PrefixSnapshotFilter, err error) {
if !(len(i) > 0) {
err = errors.Errorf("snapshot prefix must be longer than 0 characters")
return
}
f = &PrefixSnapshotFilter{i}
return
}
func (f *PrefixSnapshotFilter) Filter(fsv zfs.FilesystemVersion) (accept bool, err error) {
return fsv.Type == zfs.Snapshot && strings.HasPrefix(fsv.Name, f.Prefix), nil
func (f *PrefixFilter) Filter(fsv zfs.FilesystemVersion) (accept bool, err error) {
fstypeMatches := (!f.fstypeSet || fsv.Type == f.fstype)
prefixMatches := strings.HasPrefix(fsv.Name, f.prefix)
return fstypeMatches && prefixMatches, nil
}

View File

@ -90,7 +90,7 @@ func (j *LocalJob) JobStart(ctx context.Context) {
// All local datasets will be passed to its Map() function,
// but only those for which a mapping exists will actually be pulled.
// We can pay this small performance penalty for now.
handler := NewHandler(log.WithField(logTaskField, "handler"), localPullACL{}, &PrefixSnapshotFilter{j.SnapshotPrefix})
handler := NewHandler(log.WithField(logTaskField, "handler"), localPullACL{}, NewPrefixFilter(j.SnapshotPrefix))
registerEndpoints(local, handler)

View File

@ -164,7 +164,7 @@ outer:
}
// construct connection handler
handler := NewHandler(log, j.Filesystems, &PrefixSnapshotFilter{j.SnapshotPrefix})
handler := NewHandler(log, j.Filesystems, NewPrefixFilter(j.SnapshotPrefix))
// handle connection
rpcServer := rpc.NewServer(rwc)

View File

@ -46,7 +46,8 @@ func (p *Pruner) Run(ctx context.Context) (r []PruneResult, err error) {
log := log.WithField(logFSField, fs.ToString())
fsversions, err := zfs.ZFSListFilesystemVersions(fs, &PrefixSnapshotFilter{p.SnapshotPrefix})
snapshotFilter := NewTypedPrefixFilter(p.SnapshotPrefix, zfs.Snapshot)
fsversions, err := zfs.ZFSListFilesystemVersions(fs, snapshotFilter)
if err != nil {
log.WithError(err).Error("error listing filesytem versions")
continue