Reduce graph.Iterator method names
This commit is contained in:
parent
e4fb5d2bb8
commit
8576f66d20
21 changed files with 115 additions and 98 deletions
|
|
@ -108,9 +108,9 @@ func (it *Int64) Optimize() (graph.Iterator, bool) { return it, false }
|
|||
|
||||
// Stats for an Int64 are simple. Super cheap to do any operation,
|
||||
// and as big as the range.
|
||||
func (it *Int64) GetStats() *graph.IteratorStats {
|
||||
func (it *Int64) Stats() graph.IteratorStats {
|
||||
s, _ := it.Size()
|
||||
return &graph.IteratorStats{
|
||||
return graph.IteratorStats{
|
||||
CheckCost: 1,
|
||||
NextCost: 1,
|
||||
Size: s,
|
||||
|
|
|
|||
|
|
@ -64,7 +64,7 @@ func (it *And) Clone() graph.Iterator {
|
|||
}
|
||||
|
||||
// Returns a slice of the subiterators, in order (primary iterator first).
|
||||
func (it *And) GetSubIterators() []graph.Iterator {
|
||||
func (it *And) SubIterators() []graph.Iterator {
|
||||
iters := make([]graph.Iterator, len(it.internalIterators)+1)
|
||||
iters[0] = it.primaryIt
|
||||
copy(iters[1:], it.internalIterators)
|
||||
|
|
@ -84,11 +84,11 @@ func (it *And) TagResults(out *map[string]graph.TSVal) {
|
|||
}
|
||||
|
||||
// DEPRECATED Returns the ResultTree for this iterator, recurses to it's subiterators.
|
||||
func (it *And) GetResultTree() *graph.ResultTree {
|
||||
func (it *And) ResultTree() *graph.ResultTree {
|
||||
tree := graph.NewResultTree(it.LastResult())
|
||||
tree.AddSubtree(it.primaryIt.GetResultTree())
|
||||
tree.AddSubtree(it.primaryIt.ResultTree())
|
||||
for _, sub := range it.internalIterators {
|
||||
tree.AddSubtree(sub.GetResultTree())
|
||||
tree.AddSubtree(sub.ResultTree())
|
||||
}
|
||||
return tree
|
||||
}
|
||||
|
|
@ -109,13 +109,14 @@ func (it *And) DebugString(indent int) string {
|
|||
return fmt.Sprintf("%s(%s %d\n%stags:%s\n%sprimary_it:\n%s\n%sother_its:\n%s)",
|
||||
strings.Repeat(" ", indent),
|
||||
it.Type(),
|
||||
it.GetUid(),
|
||||
it.UID(),
|
||||
spaces,
|
||||
tags,
|
||||
spaces,
|
||||
it.primaryIt.DebugString(indent+4),
|
||||
spaces,
|
||||
total)
|
||||
total,
|
||||
)
|
||||
}
|
||||
|
||||
// Add a subiterator to this And iterator.
|
||||
|
|
|
|||
|
|
@ -42,7 +42,7 @@ import (
|
|||
func (it *And) Optimize() (graph.Iterator, bool) {
|
||||
// First, let's get the slice of iterators, in order (first one is Next()ed,
|
||||
// the rest are Check()ed)
|
||||
old := it.GetSubIterators()
|
||||
old := it.SubIterators()
|
||||
|
||||
// And call Optimize() on our subtree, replacing each one in the order we
|
||||
// found them. it_list is the newly optimized versions of these, and changed
|
||||
|
|
@ -145,20 +145,20 @@ func optimizeOrder(its []graph.Iterator) []graph.Iterator {
|
|||
// all of it's contents, and to Check() each of those against everyone
|
||||
// else.
|
||||
for _, it := range its {
|
||||
if !it.Nextable() {
|
||||
if !it.CanNext() {
|
||||
bad = append(bad, it)
|
||||
continue
|
||||
}
|
||||
rootStats := it.GetStats()
|
||||
rootStats := it.Stats()
|
||||
cost := rootStats.NextCost
|
||||
for _, f := range its {
|
||||
if !f.Nextable() {
|
||||
if !f.CanNext() {
|
||||
continue
|
||||
}
|
||||
if f == it {
|
||||
continue
|
||||
}
|
||||
stats := f.GetStats()
|
||||
stats := f.Stats()
|
||||
cost += stats.CheckCost
|
||||
}
|
||||
cost *= rootStats.Size
|
||||
|
|
@ -177,7 +177,7 @@ func optimizeOrder(its []graph.Iterator) []graph.Iterator {
|
|||
|
||||
// ... push everyone else after...
|
||||
for _, it := range its {
|
||||
if !it.Nextable() {
|
||||
if !it.CanNext() {
|
||||
continue
|
||||
}
|
||||
if it != best {
|
||||
|
|
@ -192,7 +192,7 @@ func optimizeOrder(its []graph.Iterator) []graph.Iterator {
|
|||
type byCost []graph.Iterator
|
||||
|
||||
func (c byCost) Len() int { return len(c) }
|
||||
func (c byCost) Less(i, j int) bool { return c[i].GetStats().CheckCost < c[j].GetStats().CheckCost }
|
||||
func (c byCost) Less(i, j int) bool { return c[i].Stats().CheckCost < c[j].Stats().CheckCost }
|
||||
func (c byCost) Swap(i, j int) { c[i], c[j] = c[j], c[i] }
|
||||
|
||||
// optimizeCheck(l) creates an alternate check list, containing the same contents
|
||||
|
|
@ -202,7 +202,7 @@ func (it *And) optimizeCheck() {
|
|||
// TODO(kortschak) Reuse it.checkList if possible.
|
||||
// This involves providing GetSubIterators with a slice to fill.
|
||||
// Generally this is a worthwhile thing to do in other places as well.
|
||||
it.checkList = it.GetSubIterators()
|
||||
it.checkList = it.SubIterators()
|
||||
sort.Sort(byCost(it.checkList))
|
||||
}
|
||||
|
||||
|
|
@ -212,7 +212,7 @@ func (it *And) optimizeCheck() {
|
|||
// getSubTags() returns a map of the tags for all the subiterators.
|
||||
func (it *And) getSubTags() map[string]struct{} {
|
||||
tags := make(map[string]struct{})
|
||||
for _, sub := range it.GetSubIterators() {
|
||||
for _, sub := range it.SubIterators() {
|
||||
for _, tag := range sub.Tags() {
|
||||
tags[tag] = struct{}{}
|
||||
}
|
||||
|
|
@ -292,23 +292,23 @@ func hasOneUsefulIterator(its []graph.Iterator) graph.Iterator {
|
|||
return nil
|
||||
}
|
||||
|
||||
// and.GetStats() lives here in and-iterator-optimize.go because it may
|
||||
// and.Stats() lives here in and-iterator-optimize.go because it may
|
||||
// in the future return different statistics based on how it is optimized.
|
||||
// For now, however, it's pretty static.
|
||||
func (it *And) GetStats() *graph.IteratorStats {
|
||||
primaryStats := it.primaryIt.GetStats()
|
||||
func (it *And) Stats() graph.IteratorStats {
|
||||
primaryStats := it.primaryIt.Stats()
|
||||
CheckCost := primaryStats.CheckCost
|
||||
NextCost := primaryStats.NextCost
|
||||
Size := primaryStats.Size
|
||||
for _, sub := range it.internalIterators {
|
||||
stats := sub.GetStats()
|
||||
stats := sub.Stats()
|
||||
NextCost += stats.CheckCost
|
||||
CheckCost += stats.CheckCost
|
||||
if Size > stats.Size {
|
||||
Size = stats.Size
|
||||
}
|
||||
}
|
||||
return &graph.IteratorStats{
|
||||
return graph.IteratorStats{
|
||||
CheckCost: CheckCost,
|
||||
NextCost: NextCost,
|
||||
Size: Size,
|
||||
|
|
|
|||
|
|
@ -79,7 +79,7 @@ func TestReorderWithTag(t *testing.T) {
|
|||
}
|
||||
expectedTags := []string{"good", "slow"}
|
||||
tagsOut := make([]string, 0)
|
||||
for _, sub := range newIt.GetSubIterators() {
|
||||
for _, sub := range newIt.SubIterators() {
|
||||
for _, x := range sub.Tags() {
|
||||
tagsOut = append(tagsOut, x)
|
||||
}
|
||||
|
|
@ -98,12 +98,12 @@ func TestAndStatistics(t *testing.T) {
|
|||
// Make all2 the default iterator
|
||||
a.AddSubIterator(all2)
|
||||
a.AddSubIterator(all)
|
||||
stats1 := a.GetStats()
|
||||
stats1 := a.Stats()
|
||||
newIt, changed := a.Optimize()
|
||||
if !changed {
|
||||
t.Error("Didn't optimize")
|
||||
}
|
||||
stats2 := newIt.GetStats()
|
||||
stats2 := newIt.Stats()
|
||||
if stats2.NextCost > stats1.NextCost {
|
||||
t.Error("And didn't optimize. Next cost old ", stats1.NextCost, "and new ", stats2.NextCost)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -148,8 +148,8 @@ func (it *Fixed) Size() (int64, bool) {
|
|||
|
||||
// As we right now have to scan the entire list, Next and Check are linear with the
|
||||
// size. However, a better data structure could remove these limits.
|
||||
func (it *Fixed) GetStats() *graph.IteratorStats {
|
||||
return &graph.IteratorStats{
|
||||
func (it *Fixed) Stats() graph.IteratorStats {
|
||||
return graph.IteratorStats{
|
||||
CheckCost: int64(len(it.values)),
|
||||
NextCost: int64(len(it.values)),
|
||||
Size: int64(len(it.values)),
|
||||
|
|
|
|||
|
|
@ -65,7 +65,7 @@ func NewHasA(ts graph.TripleStore, subIt graph.Iterator, d graph.Direction) *Has
|
|||
}
|
||||
|
||||
// Return our sole subiterator.
|
||||
func (it *HasA) GetSubIterators() []graph.Iterator {
|
||||
func (it *HasA) SubIterators() []graph.Iterator {
|
||||
return []graph.Iterator{it.primaryIt}
|
||||
}
|
||||
|
||||
|
|
@ -105,9 +105,9 @@ func (it *HasA) TagResults(out *map[string]graph.TSVal) {
|
|||
}
|
||||
|
||||
// DEPRECATED Return results in a ResultTree.
|
||||
func (it *HasA) GetResultTree() *graph.ResultTree {
|
||||
func (it *HasA) ResultTree() *graph.ResultTree {
|
||||
tree := graph.NewResultTree(it.LastResult())
|
||||
tree.AddSubtree(it.primaryIt.GetResultTree())
|
||||
tree.AddSubtree(it.primaryIt.ResultTree())
|
||||
return tree
|
||||
}
|
||||
|
||||
|
|
@ -117,7 +117,7 @@ func (it *HasA) DebugString(indent int) string {
|
|||
for _, k := range it.Tags() {
|
||||
tags += fmt.Sprintf("%s;", k)
|
||||
}
|
||||
return fmt.Sprintf("%s(%s %d tags:%s direction:%s\n%s)", strings.Repeat(" ", indent), it.Type(), it.GetUid(), tags, it.dir, it.primaryIt.DebugString(indent+4))
|
||||
return fmt.Sprintf("%s(%s %d tags:%s direction:%s\n%s)", strings.Repeat(" ", indent), it.Type(), it.UID(), tags, it.dir, it.primaryIt.DebugString(indent+4))
|
||||
}
|
||||
|
||||
// Check a value against our internal iterator. In order to do this, we must first open a new
|
||||
|
|
@ -196,15 +196,15 @@ func (it *HasA) Next() (graph.TSVal, bool) {
|
|||
// one sticks -- potentially expensive, depending on fanout. Size, however, is
|
||||
// potentially smaller. we know at worst it's the size of the subiterator, but
|
||||
// if there are many repeated values, it could be much smaller in totality.
|
||||
func (it *HasA) GetStats() *graph.IteratorStats {
|
||||
subitStats := it.primaryIt.GetStats()
|
||||
func (it *HasA) Stats() graph.IteratorStats {
|
||||
subitStats := it.primaryIt.Stats()
|
||||
// TODO(barakmich): These should really come from the triplestore itself
|
||||
// and be optimized.
|
||||
faninFactor := int64(1)
|
||||
fanoutFactor := int64(30)
|
||||
nextConstant := int64(2)
|
||||
tripleConstant := int64(1)
|
||||
return &graph.IteratorStats{
|
||||
return graph.IteratorStats{
|
||||
NextCost: tripleConstant + subitStats.NextCost,
|
||||
CheckCost: (fanoutFactor * nextConstant) * subitStats.CheckCost,
|
||||
Size: faninFactor * subitStats.Size,
|
||||
|
|
|
|||
|
|
@ -20,13 +20,18 @@ package iterator
|
|||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/barakmich/glog"
|
||||
|
||||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
var iterator_n int = 0
|
||||
var nextIteratorID uintptr
|
||||
|
||||
func nextID() uintptr {
|
||||
return atomic.AddUintptr(&nextIteratorID, 1) - 1
|
||||
}
|
||||
|
||||
// The Base iterator is the iterator other iterators inherit from to get some
|
||||
// default functionality.
|
||||
|
|
@ -34,21 +39,20 @@ type Base struct {
|
|||
Last graph.TSVal
|
||||
tags []string
|
||||
fixedTags map[string]graph.TSVal
|
||||
nextable bool
|
||||
uid int
|
||||
canNext bool
|
||||
uid uintptr
|
||||
}
|
||||
|
||||
// Called by subclases.
|
||||
func BaseInit(it *Base) {
|
||||
// Your basic iterator is nextable
|
||||
it.nextable = true
|
||||
it.uid = iterator_n
|
||||
it.canNext = true
|
||||
if glog.V(2) {
|
||||
iterator_n++
|
||||
it.uid = nextID()
|
||||
}
|
||||
}
|
||||
|
||||
func (it *Base) GetUid() int {
|
||||
func (it *Base) UID() uintptr {
|
||||
return it.uid
|
||||
}
|
||||
|
||||
|
|
@ -99,12 +103,12 @@ func (it *Base) Check(v graph.TSVal) bool {
|
|||
|
||||
// Base iterators should never appear in a tree if they are, select against
|
||||
// them.
|
||||
func (it *Base) GetStats() *graph.IteratorStats {
|
||||
return &graph.IteratorStats{100000, 100000, 100000}
|
||||
func (it *Base) Stats() graph.IteratorStats {
|
||||
return graph.IteratorStats{100000, 100000, 100000}
|
||||
}
|
||||
|
||||
// DEPRECATED
|
||||
func (it *Base) GetResultTree() *graph.ResultTree {
|
||||
func (it *Base) ResultTree() *graph.ResultTree {
|
||||
tree := graph.NewResultTree(it.LastResult())
|
||||
return tree
|
||||
}
|
||||
|
|
@ -129,12 +133,12 @@ func (it *Base) Size() (int64, bool) {
|
|||
}
|
||||
|
||||
// No subiterators. Only those with subiterators need to do anything here.
|
||||
func (it *Base) GetSubIterators() []graph.Iterator {
|
||||
func (it *Base) SubIterators() []graph.Iterator {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Accessor
|
||||
func (it *Base) Nextable() bool { return it.nextable }
|
||||
func (it *Base) CanNext() bool { return it.canNext }
|
||||
|
||||
// Fill the map based on the tags assigned to this iterator. Default
|
||||
// functionality works well for most iterators.
|
||||
|
|
@ -182,6 +186,6 @@ func (it *Null) DebugString(indent int) string {
|
|||
}
|
||||
|
||||
// A null iterator costs nothing. Use it!
|
||||
func (it *Null) GetStats() *graph.IteratorStats {
|
||||
return &graph.IteratorStats{}
|
||||
func (it *Null) Stats() graph.IteratorStats {
|
||||
return graph.IteratorStats{}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -83,9 +83,9 @@ func (it *LinksTo) TagResults(out *map[string]graph.TSVal) {
|
|||
}
|
||||
|
||||
// DEPRECATED
|
||||
func (it *LinksTo) GetResultTree() *graph.ResultTree {
|
||||
func (it *LinksTo) ResultTree() *graph.ResultTree {
|
||||
tree := graph.NewResultTree(it.LastResult())
|
||||
tree.AddSubtree(it.primaryIt.GetResultTree())
|
||||
tree.AddSubtree(it.primaryIt.ResultTree())
|
||||
return tree
|
||||
}
|
||||
|
||||
|
|
@ -93,7 +93,7 @@ func (it *LinksTo) GetResultTree() *graph.ResultTree {
|
|||
func (it *LinksTo) DebugString(indent int) string {
|
||||
return fmt.Sprintf("%s(%s %d direction:%s\n%s)",
|
||||
strings.Repeat(" ", indent),
|
||||
it.Type(), it.GetUid(), it.dir, it.primaryIt.DebugString(indent+4))
|
||||
it.Type(), it.UID(), it.dir, it.primaryIt.DebugString(indent+4))
|
||||
}
|
||||
|
||||
// If it checks in the right direction for the subiterator, it is a valid link
|
||||
|
|
@ -109,7 +109,7 @@ func (it *LinksTo) Check(val graph.TSVal) bool {
|
|||
}
|
||||
|
||||
// Return a list containing only our subiterator.
|
||||
func (it *LinksTo) GetSubIterators() []graph.Iterator {
|
||||
func (it *LinksTo) SubIterators() []graph.Iterator {
|
||||
return []graph.Iterator{it.primaryIt}
|
||||
}
|
||||
|
||||
|
|
@ -169,13 +169,13 @@ func (it *LinksTo) NextResult() bool {
|
|||
func (it *LinksTo) Type() string { return "linksto" }
|
||||
|
||||
// Return a guess as to how big or costly it is to next the iterator.
|
||||
func (it *LinksTo) GetStats() *graph.IteratorStats {
|
||||
subitStats := it.primaryIt.GetStats()
|
||||
func (it *LinksTo) Stats() graph.IteratorStats {
|
||||
subitStats := it.primaryIt.Stats()
|
||||
// TODO(barakmich): These should really come from the triplestore itself
|
||||
fanoutFactor := int64(20)
|
||||
checkConstant := int64(1)
|
||||
nextConstant := int64(2)
|
||||
return &graph.IteratorStats{
|
||||
return graph.IteratorStats{
|
||||
NextCost: nextConstant + subitStats.NextCost,
|
||||
CheckCost: checkConstant + subitStats.CheckCost,
|
||||
Size: fanoutFactor * subitStats.Size,
|
||||
|
|
|
|||
|
|
@ -47,7 +47,7 @@ type Optional struct {
|
|||
func NewOptional(it graph.Iterator) *Optional {
|
||||
var o Optional
|
||||
BaseInit(&o.Base)
|
||||
o.nextable = false
|
||||
o.canNext = false
|
||||
o.subIt = it
|
||||
return &o
|
||||
}
|
||||
|
|
@ -127,9 +127,9 @@ func (it *Optional) Optimize() (graph.Iterator, bool) {
|
|||
}
|
||||
|
||||
// We're only as expensive as our subiterator. Except, we can't be nexted.
|
||||
func (it *Optional) GetStats() *graph.IteratorStats {
|
||||
subStats := it.subIt.GetStats()
|
||||
return &graph.IteratorStats{
|
||||
func (it *Optional) Stats() graph.IteratorStats {
|
||||
subStats := it.subIt.Stats()
|
||||
return graph.IteratorStats{
|
||||
CheckCost: subStats.CheckCost,
|
||||
NextCost: int64(1 << 62),
|
||||
Size: subStats.Size,
|
||||
|
|
|
|||
|
|
@ -77,7 +77,7 @@ func (it *Or) Clone() graph.Iterator {
|
|||
}
|
||||
|
||||
// Returns a list.List of the subiterators, in order. The returned slice must not be modified.
|
||||
func (it *Or) GetSubIterators() []graph.Iterator {
|
||||
func (it *Or) SubIterators() []graph.Iterator {
|
||||
return it.internalIterators
|
||||
}
|
||||
|
||||
|
|
@ -89,10 +89,10 @@ func (it *Or) TagResults(out *map[string]graph.TSVal) {
|
|||
}
|
||||
|
||||
// DEPRECATED Returns the ResultTree for this graph.iterator, recurses to it's subiterators.
|
||||
func (it *Or) GetResultTree() *graph.ResultTree {
|
||||
func (it *Or) ResultTree() *graph.ResultTree {
|
||||
tree := graph.NewResultTree(it.LastResult())
|
||||
for _, sub := range it.internalIterators {
|
||||
tree.AddSubtree(sub.GetResultTree())
|
||||
tree.AddSubtree(sub.ResultTree())
|
||||
}
|
||||
return tree
|
||||
}
|
||||
|
|
@ -233,7 +233,7 @@ func (it *Or) Close() {
|
|||
}
|
||||
|
||||
func (it *Or) Optimize() (graph.Iterator, bool) {
|
||||
old := it.GetSubIterators()
|
||||
old := it.SubIterators()
|
||||
optIts := optimizeSubIterators(old)
|
||||
// Close the replaced iterators (they ought to close themselves, but Close()
|
||||
// is idempotent, so this just protects against any machinations).
|
||||
|
|
@ -256,12 +256,12 @@ func (it *Or) Optimize() (graph.Iterator, bool) {
|
|||
return newOr, true
|
||||
}
|
||||
|
||||
func (it *Or) GetStats() *graph.IteratorStats {
|
||||
func (it *Or) Stats() graph.IteratorStats {
|
||||
CheckCost := int64(0)
|
||||
NextCost := int64(0)
|
||||
Size := int64(0)
|
||||
for _, sub := range it.internalIterators {
|
||||
stats := sub.GetStats()
|
||||
stats := sub.Stats()
|
||||
NextCost += stats.NextCost
|
||||
CheckCost += stats.CheckCost
|
||||
if it.isShortCircuiting {
|
||||
|
|
@ -272,7 +272,7 @@ func (it *Or) GetStats() *graph.IteratorStats {
|
|||
Size += stats.Size
|
||||
}
|
||||
}
|
||||
return &graph.IteratorStats{
|
||||
return graph.IteratorStats{
|
||||
CheckCost: CheckCost,
|
||||
NextCost: NextCost,
|
||||
Size: Size,
|
||||
|
|
|
|||
|
|
@ -116,7 +116,7 @@ func (qs *queryShape) MakeNode(it graph.Iterator) *Node {
|
|||
|
||||
switch it.Type() {
|
||||
case "and":
|
||||
for _, sub := range it.GetSubIterators() {
|
||||
for _, sub := range it.SubIterators() {
|
||||
qs.nodeId++
|
||||
newNode := qs.MakeNode(sub)
|
||||
if sub.Type() != "or" {
|
||||
|
|
@ -143,7 +143,7 @@ func (qs *queryShape) MakeNode(it graph.Iterator) *Node {
|
|||
qs.AddNode(newNode)
|
||||
qs.RemoveHasa()
|
||||
case "or":
|
||||
for _, sub := range it.GetSubIterators() {
|
||||
for _, sub := range it.SubIterators() {
|
||||
qs.nodeId++
|
||||
newNode := qs.MakeNode(sub)
|
||||
if sub.Type() == "or" {
|
||||
|
|
|
|||
|
|
@ -185,6 +185,6 @@ func (it *Comparison) Optimize() (graph.Iterator, bool) {
|
|||
|
||||
// We're only as expensive as our subiterator.
|
||||
// Again, optimized value comparison iterators should do better.
|
||||
func (it *Comparison) GetStats() *graph.IteratorStats {
|
||||
return it.subIt.GetStats()
|
||||
func (it *Comparison) Stats() graph.IteratorStats {
|
||||
return it.subIt.Stats()
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue