generalize Linkage, add relevant comments

This commit is contained in:
Barak Michener 2015-04-19 20:07:14 -04:00
parent 6201e709ef
commit 355c8ee6bc
5 changed files with 33 additions and 18 deletions

View file

@ -30,7 +30,14 @@ type Tagger struct {
fixedTags map[string]Value fixedTags map[string]Value
} }
type LinkageSet struct { // TODO(barakmich): Linkage is general enough that there are places we take
//the combined arguments `quad.Direction, graph.Value` that it may be worth
//converting these into Linkages. If nothing else, future indexed iterators may
//benefit from the shared representation
// Linkage is a union type representing a set of values established for a given
// quad direction.
type Linkage struct {
Dir quad.Direction Dir quad.Direction
Values []Value Values []Value
} }

View file

@ -34,7 +34,8 @@ type And struct {
qs graph.QuadStore qs graph.QuadStore
} }
// Creates a new And iterator. // NewAnd creates an And iterator. `qs` is only required when needing a handle
// for QuadStore-specific optimizations, otherwise nil is acceptable.
func NewAnd(qs graph.QuadStore) *And { func NewAnd(qs graph.QuadStore) *And {
return &And{ return &And{
uid: NextUID(), uid: NextUID(),

View file

@ -99,10 +99,12 @@ func (it *And) Optimize() (graph.Iterator, bool) {
// Ask the graph.QuadStore if we can be replaced. Often times, this is a great // Ask the graph.QuadStore if we can be replaced. Often times, this is a great
// optimization opportunity (there's a fixed iterator underneath us, for // optimization opportunity (there's a fixed iterator underneath us, for
// example). // example).
newReplacement, hasOne := it.qs.OptimizeIterator(newAnd) if it.qs != nil {
if hasOne { newReplacement, hasOne := it.qs.OptimizeIterator(newAnd)
newAnd.Close() if hasOne {
return newReplacement, true newAnd.Close()
return newReplacement, true
}
} }
return newAnd, true return newAnd, true

View file

@ -23,6 +23,19 @@ import (
"github.com/google/cayley/quad" "github.com/google/cayley/quad"
) )
var _ graph.Nexter = &LinksTo{}
var linksToType graph.Type
func init() {
linksToType = graph.RegisterIterator("mongo-linksto")
}
// LinksTo is a MongoDB-dependent version of a LinksTo iterator. Like the normal
// LinksTo, it represents a set of links to a set of nodes, represented by its
// subiterator. However, this iterator may often be faster than the generic
// LinksTo, as it can use the secondary indices in Mongo as features within the
// Mongo query, reducing the size of the result set and speeding up iteration.
type LinksTo struct { type LinksTo struct {
uid uint64 uid uint64
collection string collection string
@ -33,13 +46,13 @@ type LinksTo struct {
nextIt *mgo.Iter nextIt *mgo.Iter
result graph.Value result graph.Value
runstats graph.IteratorStats runstats graph.IteratorStats
lset []graph.Linkage
err error err error
lset []graph.LinkageSet
} }
// NewLinksTo constructs a new indexed LinksTo iterator for Mongo around a direction // NewLinksTo constructs a new indexed LinksTo iterator for Mongo around a direction
// and a subiterator of nodes. // and a subiterator of nodes.
func NewLinksTo(qs *QuadStore, it graph.Iterator, collection string, d quad.Direction, lset []graph.LinkageSet) *LinksTo { func NewLinksTo(qs *QuadStore, it graph.Iterator, collection string, d quad.Direction, lset []graph.Linkage) *LinksTo {
return &LinksTo{ return &LinksTo{
uid: iterator.NextUID(), uid: iterator.NextUID(),
qs: qs, qs: qs,
@ -181,18 +194,10 @@ func (it *LinksTo) NextPath() bool {
return ok return ok
} }
var mongoIndexedLinksToType graph.Type
func init() {
mongoIndexedLinksToType = graph.RegisterIterator("mongo-indexed-linksto")
}
func (it *LinksTo) Type() graph.Type { func (it *LinksTo) Type() graph.Type {
return mongoIndexedLinksToType return linksToType
} }
var _ graph.Nexter = &LinksTo{}
func (it *LinksTo) Clone() graph.Iterator { func (it *LinksTo) Clone() graph.Iterator {
m := NewLinksTo(it.qs, it.primaryIt.Clone(), it.collection, it.dir, it.lset) m := NewLinksTo(it.qs, it.primaryIt.Clone(), it.collection, it.dir, it.lset)
m.tags.CopyFrom(it) m.tags.CopyFrom(it)

View file

@ -65,7 +65,7 @@ func (qs *QuadStore) optimizeAndIterator(it *iterator.And) (graph.Iterator, bool
} }
mongostats := firstmongo.Stats() mongostats := firstmongo.Stats()
lset := []graph.LinkageSet{ lset := []graph.Linkage{
{ {
Dir: firstmongo.dir, Dir: firstmongo.dir,
Values: []graph.Value{qs.ValueOf(firstmongo.name)}, Values: []graph.Value{qs.ValueOf(firstmongo.name)},