2017-05-07 14:07:50 +02:00
|
|
|
package zfs
|
|
|
|
|
|
|
|
type DatasetPathForest struct {
|
|
|
|
roots []*datasetPathTree
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDatasetPathForest() *DatasetPathForest {
|
|
|
|
return &DatasetPathForest{
|
|
|
|
make([]*datasetPathTree, 0),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-06 13:04:29 +02:00
|
|
|
func (f *DatasetPathForest) Add(p *DatasetPath) {
|
2017-08-05 21:15:37 +02:00
|
|
|
if len(p.comps) <= 0 {
|
2017-05-07 14:07:50 +02:00
|
|
|
panic("dataset path too short. must have length > 0")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find its root
|
|
|
|
var root *datasetPathTree
|
|
|
|
for _, r := range f.roots {
|
2017-08-05 21:15:37 +02:00
|
|
|
if r.Add(p.comps) {
|
2017-05-07 14:07:50 +02:00
|
|
|
root = r
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if root == nil {
|
2017-08-05 21:15:37 +02:00
|
|
|
root = newDatasetPathTree(p.comps)
|
2017-05-07 14:07:50 +02:00
|
|
|
f.roots = append(f.roots, root)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type DatasetPathVisit struct {
|
2017-08-06 13:04:29 +02:00
|
|
|
Path *DatasetPath
|
2017-05-07 14:07:50 +02:00
|
|
|
// If true, the dataset referenced by Path was not in the list of datasets to traverse
|
|
|
|
FilledIn bool
|
2020-07-12 16:32:28 +02:00
|
|
|
Parent *DatasetPathVisit
|
2017-05-07 14:07:50 +02:00
|
|
|
}
|
|
|
|
|
2020-07-12 16:32:28 +02:00
|
|
|
type DatasetPathsVisitor func(v *DatasetPathVisit) (visitChildTree bool)
|
2017-05-07 14:07:50 +02:00
|
|
|
|
|
|
|
// Traverse a list of DatasetPaths top down, i.e. given a set of datasets with same
|
|
|
|
// path prefix, those with shorter prefix are traversed first.
|
2020-02-23 23:24:12 +01:00
|
|
|
// If there are gaps, i.e. the intermediary component a/b between a and a/b/c,
|
2017-05-07 14:07:50 +02:00
|
|
|
// those gaps are still visited but the FilledIn property of the visit is set to true.
|
|
|
|
func (f *DatasetPathForest) WalkTopDown(visitor DatasetPathsVisitor) {
|
|
|
|
|
|
|
|
for _, r := range f.roots {
|
2020-07-12 16:32:28 +02:00
|
|
|
r.WalkTopDown(&DatasetPathVisit{
|
|
|
|
Path: &DatasetPath{nil},
|
|
|
|
FilledIn: true,
|
|
|
|
Parent: nil,
|
|
|
|
}, visitor)
|
2017-05-07 14:07:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/* PRIVATE IMPLEMENTATION */
|
|
|
|
|
|
|
|
type datasetPathTree struct {
|
|
|
|
Component string
|
|
|
|
FilledIn bool
|
|
|
|
Children []*datasetPathTree
|
|
|
|
}
|
|
|
|
|
2017-08-05 21:15:37 +02:00
|
|
|
func (t *datasetPathTree) Add(p []string) bool {
|
2017-05-07 14:07:50 +02:00
|
|
|
|
|
|
|
if len(p) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if p[0] == t.Component {
|
|
|
|
|
|
|
|
remainder := p[1:]
|
|
|
|
if len(remainder) == 0 {
|
|
|
|
t.FilledIn = false
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range t.Children {
|
|
|
|
if c.Add(remainder) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Children = append(t.Children, newDatasetPathTree(remainder))
|
|
|
|
return true
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-07-12 16:32:28 +02:00
|
|
|
func (t *datasetPathTree) WalkTopDown(parent *DatasetPathVisit, visitor DatasetPathsVisitor) {
|
2017-05-07 14:07:50 +02:00
|
|
|
|
2020-07-12 16:32:28 +02:00
|
|
|
thisVisitPath := parent.Path.Copy()
|
|
|
|
thisVisitPath.Extend(&DatasetPath{[]string{t.Component}})
|
2017-05-07 14:07:50 +02:00
|
|
|
|
2020-07-12 16:32:28 +02:00
|
|
|
thisVisit := &DatasetPathVisit{
|
|
|
|
thisVisitPath,
|
2017-08-05 21:15:37 +02:00
|
|
|
t.FilledIn,
|
2020-07-12 16:32:28 +02:00
|
|
|
parent,
|
2017-08-05 21:15:37 +02:00
|
|
|
}
|
|
|
|
visitChildTree := visitor(thisVisit)
|
2017-05-07 14:07:50 +02:00
|
|
|
|
|
|
|
if visitChildTree {
|
|
|
|
for _, c := range t.Children {
|
2020-07-12 16:32:28 +02:00
|
|
|
c.WalkTopDown(thisVisit, visitor)
|
2017-05-07 14:07:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-08-05 21:15:37 +02:00
|
|
|
func newDatasetPathTree(initialComps []string) (t *datasetPathTree) {
|
2017-05-07 14:07:50 +02:00
|
|
|
t = &datasetPathTree{}
|
2019-03-22 20:45:27 +01:00
|
|
|
cur := t
|
2017-08-05 21:15:37 +02:00
|
|
|
for i, comp := range initialComps {
|
2017-05-07 14:07:50 +02:00
|
|
|
cur.Component = comp
|
|
|
|
cur.FilledIn = true
|
|
|
|
cur.Children = make([]*datasetPathTree, 0, 1)
|
2017-08-05 21:15:37 +02:00
|
|
|
if i == len(initialComps)-1 {
|
2017-05-07 14:07:50 +02:00
|
|
|
cur.FilledIn = false // last component is not filled in
|
|
|
|
break
|
|
|
|
}
|
|
|
|
child := &datasetPathTree{}
|
|
|
|
cur.Children = append(cur.Children, child)
|
|
|
|
cur = child
|
|
|
|
}
|
|
|
|
return t
|
|
|
|
}
|