exploreUnion.go 2.64 KB
Newer Older
1 2 3 4 5
package selector

import (
	"fmt"

tavit ohanian's avatar
tavit ohanian committed
6
	ld "gitlab.dms3.io/ld/go-ld-prime"
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
)

// ExploreUnion allows selection to continue with two or more distinct selectors
// while exploring the same tree of data.
//
// ExploreUnion can be used to apply a Matcher on one node (causing it to
// be considered part of a (possibly labelled) result set), while simultaneously
// continuing to explore deeper parts of the tree with another selector,
// for example.
type ExploreUnion struct {
	Members []Selector
}

// Interests for ExploreUnion is:
// - nil (aka all) if any member selector has nil interests
// - the union of values returned by all member selectors otherwise
tavit ohanian's avatar
tavit ohanian committed
23
func (s ExploreUnion) Interests() []ld.PathSegment {
24 25 26 27 28 29 30 31 32
	// Check for any high-cardinality selectors first; if so, shortcircuit.
	//  (n.b. we're assuming the 'Interests' method is cheap here.)
	for _, m := range s.Members {
		if m.Interests() == nil {
			return nil
		}
	}
	// Accumulate the whitelist of interesting path segments.
	// TODO: Dedup?
tavit ohanian's avatar
tavit ohanian committed
33
	v := []ld.PathSegment{}
34 35 36 37 38 39 40 41 42 43 44
	for _, m := range s.Members {
		v = append(v, m.Interests()...)
	}
	return v
}

// Explore for a Union selector calls explore for each member selector
// and returns:
// - a new union selector if more than one member returns a selector
// - if exactly one member returns a selector, that selector
// - nil if no members return a selector
tavit ohanian's avatar
tavit ohanian committed
45
func (s ExploreUnion) Explore(n ld.Node, p ld.PathSegment) Selector {
46 47 48 49 50
	// TODO: memory efficient?
	nonNilResults := make([]Selector, 0, len(s.Members))
	for _, member := range s.Members {
		resultSelector := member.Explore(n, p)
		if resultSelector != nil {
51
			nonNilResults = append(nonNilResults, resultSelector)
52 53 54 55 56 57 58 59 60 61 62 63 64
		}
	}
	if len(nonNilResults) == 0 {
		return nil
	}
	if len(nonNilResults) == 1 {
		return nonNilResults[0]
	}
	return ExploreUnion{nonNilResults}
}

// Decide returns true for a Union selector if any of the member selectors
// return true
tavit ohanian's avatar
tavit ohanian committed
65
func (s ExploreUnion) Decide(n ld.Node) bool {
66 67 68 69 70 71 72 73 74 75
	for _, m := range s.Members {
		if m.Decide(n) {
			return true
		}
	}
	return false
}

// ParseExploreUnion assembles a Selector
// from an ExploreUnion selector node
tavit ohanian's avatar
tavit ohanian committed
76 77
func (pc ParseContext) ParseExploreUnion(n ld.Node) (Selector, error) {
	if n.Kind() != ld.Kind_List {
78 79 80 81 82 83 84 85 86 87
		return nil, fmt.Errorf("selector spec parse rejected: explore union selector must be a list")
	}
	x := ExploreUnion{
		make([]Selector, 0, n.Length()),
	}
	for itr := n.ListIterator(); !itr.Done(); {
		_, v, err := itr.Next()
		if err != nil {
			return nil, fmt.Errorf("error during selector spec parse: %s", err)
		}
88
		member, err := pc.ParseSelector(v)
89 90 91 92 93 94 95
		if err != nil {
			return nil, err
		}
		x.Members = append(x.Members, member)
	}
	return x, nil
}