zrepl/zfs/mapping.go
Christian Schwarz 3ba3648f0f zfs: use channel as iterator for ZFSList results
The old approach with ZFSList would keep the two-dimensional array of
lines and their fields in memory (for a short time), which could easily
consume 100s of MiB with > 10000 snapshots / bookmarks (see #34)

fixes #61
2018-02-18 13:28:46 +01:00

48 lines
863 B
Go

package zfs
import (
"context"
"fmt"
)
type DatasetFilter interface {
Filter(p *DatasetPath) (pass bool, err error)
}
func ZFSListMapping(filter DatasetFilter) (datasets []*DatasetPath, err error) {
if filter == nil {
panic("filter must not be nil")
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
rchan := make(chan ZFSListResult)
go ZFSListChan(ctx, rchan, []string{"name"}, "-r", "-t", "filesystem,volume")
datasets = make([]*DatasetPath, 0)
for r := range rchan {
if r.err != nil {
err = r.err
return
}
var path *DatasetPath
if path, err = NewDatasetPath(r.fields[0]); err != nil {
return
}
pass, filterErr := filter.Filter(path)
if filterErr != nil {
return nil, fmt.Errorf("error calling filter: %s", filterErr)
}
if pass {
datasets = append(datasets, path)
}
}
return
}