Canonicalise iterator receiver names

This apparently meaningless churn improves godoc readability.
This commit is contained in:
kortschak 2014-06-28 21:36:50 +09:30
parent dc65ebce9e
commit 60d5c60817
10 changed files with 432 additions and 435 deletions

View file

@ -45,70 +45,69 @@ func NewInt64AllIterator(min, max int64) *Int64AllIterator {
}
// Start back at the beginning
func (a *Int64AllIterator) Reset() {
a.at = a.min
func (it *Int64AllIterator) Reset() {
it.at = it.min
}
func (a *Int64AllIterator) Close() {
}
func (it *Int64AllIterator) Close() {}
func (a *Int64AllIterator) Clone() Iterator {
out := NewInt64AllIterator(a.min, a.max)
out.CopyTagsFrom(a)
func (it *Int64AllIterator) Clone() Iterator {
out := NewInt64AllIterator(it.min, it.max)
out.CopyTagsFrom(it)
return out
}
// Prints the All iterator as just an "all".
func (a *Int64AllIterator) DebugString(indent int) string {
return fmt.Sprintf("%s(%s)", strings.Repeat(" ", indent), a.Type())
func (it *Int64AllIterator) DebugString(indent int) string {
return fmt.Sprintf("%s(%s)", strings.Repeat(" ", indent), it.Type())
}
// Next() on an Int64 all iterator is a simple incrementing counter.
// Return the next integer, and mark it as the result.
func (a *Int64AllIterator) Next() (TSVal, bool) {
NextLogIn(a)
if a.at == -1 {
return NextLogOut(a, nil, false)
func (it *Int64AllIterator) Next() (TSVal, bool) {
NextLogIn(it)
if it.at == -1 {
return NextLogOut(it, nil, false)
}
val := a.at
a.at = a.at + 1
if a.at > a.max {
a.at = -1
val := it.at
it.at = it.at + 1
if it.at > it.max {
it.at = -1
}
a.Last = val
return NextLogOut(a, val, true)
it.Last = val
return NextLogOut(it, val, true)
}
// The number of elements in an Int64AllIterator is the size of the range.
// The size is exact.
func (a *Int64AllIterator) Size() (int64, bool) {
Size := ((a.max - a.min) + 1)
func (it *Int64AllIterator) Size() (int64, bool) {
Size := ((it.max - it.min) + 1)
return Size, true
}
// Check() for an Int64AllIterator is merely seeing if the passed value is
// withing the range, assuming the value is an int64.
func (a *Int64AllIterator) Check(tsv TSVal) bool {
CheckLogIn(a, tsv)
func (it *Int64AllIterator) Check(tsv TSVal) bool {
CheckLogIn(it, tsv)
v := tsv.(int64)
if a.min <= v && v <= a.max {
a.Last = v
return CheckLogOut(a, v, true)
if it.min <= v && v <= it.max {
it.Last = v
return CheckLogOut(it, v, true)
}
return CheckLogOut(a, v, false)
return CheckLogOut(it, v, false)
}
// The type of this iterator is an "all". This is important, as it puts it in
// the class of "all iterators.
func (a *Int64AllIterator) Type() string { return "all" }
func (it *Int64AllIterator) Type() string { return "all" }
// There's nothing to optimize about this little iterator.
func (a *Int64AllIterator) Optimize() (Iterator, bool) { return a, false }
func (it *Int64AllIterator) Optimize() (Iterator, bool) { return it, false }
// Stats for an Int64AllIterator are simple. Super cheap to do any operation,
// and as big as the range.
func (a *Int64AllIterator) GetStats() *IteratorStats {
s, _ := a.Size()
func (it *Int64AllIterator) GetStats() *IteratorStats {
s, _ := it.Size()
return &IteratorStats{
CheckCost: 1,
NextCost: 1,

View file

@ -41,80 +41,80 @@ func NewAndIterator() *AndIterator {
}
// Reset all internal iterators
func (and *AndIterator) Reset() {
and.primaryIt.Reset()
for _, it := range and.internalIterators {
it.Reset()
func (it *AndIterator) Reset() {
it.primaryIt.Reset()
for _, sub := range it.internalIterators {
sub.Reset()
}
and.checkList = nil
it.checkList = nil
}
func (and *AndIterator) Clone() Iterator {
newAnd := NewAndIterator()
newAnd.AddSubIterator(and.primaryIt.Clone())
newAnd.CopyTagsFrom(and)
for _, it := range and.internalIterators {
newAnd.AddSubIterator(it.Clone())
func (it *AndIterator) Clone() Iterator {
and := NewAndIterator()
and.AddSubIterator(it.primaryIt.Clone())
and.CopyTagsFrom(it)
for _, sub := range it.internalIterators {
and.AddSubIterator(sub.Clone())
}
if and.checkList != nil {
newAnd.optimizeCheck()
if it.checkList != nil {
and.optimizeCheck()
}
return newAnd
return and
}
// Returns a list.List of the subiterators, in order (primary iterator first).
func (and *AndIterator) GetSubIterators() *list.List {
func (it *AndIterator) GetSubIterators() *list.List {
l := list.New()
l.PushBack(and.primaryIt)
for _, it := range and.internalIterators {
l.PushBack(it)
l.PushBack(it.primaryIt)
for _, sub := range it.internalIterators {
l.PushBack(sub)
}
return l
}
// Overrides BaseIterator TagResults, as it needs to add it's own results and
// recurse down it's subiterators.
func (and *AndIterator) TagResults(out *map[string]TSVal) {
and.BaseIterator.TagResults(out)
if and.primaryIt != nil {
and.primaryIt.TagResults(out)
func (it *AndIterator) TagResults(out *map[string]TSVal) {
it.BaseIterator.TagResults(out)
if it.primaryIt != nil {
it.primaryIt.TagResults(out)
}
for _, it := range and.internalIterators {
it.TagResults(out)
for _, sub := range it.internalIterators {
sub.TagResults(out)
}
}
// DEPRECATED Returns the ResultTree for this iterator, recurses to it's subiterators.
func (and *AndIterator) GetResultTree() *ResultTree {
tree := NewResultTree(and.LastResult())
tree.AddSubtree(and.primaryIt.GetResultTree())
for _, it := range and.internalIterators {
tree.AddSubtree(it.GetResultTree())
func (it *AndIterator) GetResultTree() *ResultTree {
tree := NewResultTree(it.LastResult())
tree.AddSubtree(it.primaryIt.GetResultTree())
for _, sub := range it.internalIterators {
tree.AddSubtree(sub.GetResultTree())
}
return tree
}
// Prints information about this iterator.
func (and *AndIterator) DebugString(indent int) string {
func (it *AndIterator) DebugString(indent int) string {
var total string
for i, it := range and.internalIterators {
for i, sub := range it.internalIterators {
total += strings.Repeat(" ", indent+2)
total += fmt.Sprintf("%d:\n%s\n", i, it.DebugString(indent+4))
total += fmt.Sprintf("%d:\n%s\n", i, sub.DebugString(indent+4))
}
var tags string
for _, k := range and.Tags() {
for _, k := range it.Tags() {
tags += fmt.Sprintf("%s;", k)
}
spaces := strings.Repeat(" ", indent+2)
return fmt.Sprintf("%s(%s %d\n%stags:%s\n%sprimary_it:\n%s\n%sother_its:\n%s)",
strings.Repeat(" ", indent),
and.Type(),
and.GetUid(),
it.Type(),
it.GetUid(),
spaces,
tags,
spaces,
and.primaryIt.DebugString(indent+4),
it.primaryIt.DebugString(indent+4),
spaces,
total)
}
@ -125,43 +125,42 @@ func (and *AndIterator) DebugString(indent int) string {
// important. Calling Optimize() is the way to change the order based on
// subiterator statistics. Without Optimize(), the order added is the order
// used.
func (and *AndIterator) AddSubIterator(sub Iterator) {
if and.itCount > 0 {
and.internalIterators = append(and.internalIterators, sub)
and.itCount++
func (it *AndIterator) AddSubIterator(sub Iterator) {
if it.itCount > 0 {
it.internalIterators = append(it.internalIterators, sub)
it.itCount++
return
}
and.primaryIt = sub
and.itCount++
it.primaryIt = sub
it.itCount++
}
// Returns the Next value from the And iterator. Because the And is the
// intersection of its subiterators, it must choose one subiterator to produce a
// candidate, and check this value against the subiterators. A productive choice
// of primary iterator is therefore very important.
func (and *AndIterator) Next() (TSVal, bool) {
NextLogIn(and)
func (it *AndIterator) Next() (TSVal, bool) {
NextLogIn(it)
var curr TSVal
var exists bool
for {
curr, exists = and.primaryIt.Next()
curr, exists = it.primaryIt.Next()
if !exists {
return NextLogOut(and, nil, false)
return NextLogOut(it, nil, false)
}
if and.checkSubIts(curr) {
and.Last = curr
return NextLogOut(and, curr, true)
if it.checkSubIts(curr) {
it.Last = curr
return NextLogOut(it, curr, true)
}
}
panic("Somehow broke out of Next() loop in AndIterator")
}
// Checks a value against the non-primary iterators, in order.
func (and *AndIterator) checkSubIts(val TSVal) bool {
func (it *AndIterator) checkSubIts(val TSVal) bool {
var subIsGood = true
for _, it := range and.internalIterators {
subIsGood = it.Check(val)
for _, sub := range it.internalIterators {
subIsGood = sub.Check(val)
if !subIsGood {
break
}
@ -169,43 +168,43 @@ func (and *AndIterator) checkSubIts(val TSVal) bool {
return subIsGood
}
func (and *AndIterator) checkCheckList(val TSVal) bool {
func (it *AndIterator) checkCheckList(val TSVal) bool {
var isGood = true
for e := and.checkList.Front(); e != nil; e = e.Next() {
for e := it.checkList.Front(); e != nil; e = e.Next() {
isGood = e.Value.(Iterator).Check(val)
if !isGood {
break
}
}
return CheckLogOut(and, val, isGood)
return CheckLogOut(it, val, isGood)
}
// Check a value against the entire iterator, in order.
func (and *AndIterator) Check(val TSVal) bool {
CheckLogIn(and, val)
if and.checkList != nil {
return and.checkCheckList(val)
func (it *AndIterator) Check(val TSVal) bool {
CheckLogIn(it, val)
if it.checkList != nil {
return it.checkCheckList(val)
}
mainGood := and.primaryIt.Check(val)
mainGood := it.primaryIt.Check(val)
if !mainGood {
return CheckLogOut(and, val, false)
return CheckLogOut(it, val, false)
}
othersGood := and.checkSubIts(val)
othersGood := it.checkSubIts(val)
if !othersGood {
return CheckLogOut(and, val, false)
return CheckLogOut(it, val, false)
}
and.Last = val
return CheckLogOut(and, val, true)
it.Last = val
return CheckLogOut(it, val, true)
}
// Returns the approximate size of the And iterator. Because we're dealing
// with an intersection, we know that the largest we can be is the size of the
// smallest iterator. This is the heuristic we shall follow. Better heuristics
// welcome.
func (and *AndIterator) Size() (int64, bool) {
val, b := and.primaryIt.Size()
for _, it := range and.internalIterators {
newval, newb := it.Size()
func (it *AndIterator) Size() (int64, bool) {
val, b := it.primaryIt.Size()
for _, sub := range it.internalIterators {
newval, newb := sub.Size()
if val > newval {
val = newval
}
@ -217,12 +216,12 @@ func (and *AndIterator) Size() (int64, bool) {
// An And has no NextResult of its own -- that is, there are no other values
// which satisfy our previous result that are not the result itself. Our
// subiterators might, however, so just pass the call recursively.
func (and *AndIterator) NextResult() bool {
if and.primaryIt.NextResult() {
func (it *AndIterator) NextResult() bool {
if it.primaryIt.NextResult() {
return true
}
for _, it := range and.internalIterators {
if it.NextResult() {
for _, sub := range it.internalIterators {
if sub.NextResult() {
return true
}
}
@ -230,19 +229,18 @@ func (and *AndIterator) NextResult() bool {
}
// Perform and-specific cleanup, of which there currently is none.
func (and *AndIterator) cleanUp() {
}
func (it *AndIterator) cleanUp() {}
// Close this iterator, and, by extension, close the subiterators.
// Close should be idempotent, and it follows that if it's subiterators
// follow this contract, the And follows the contract.
func (and *AndIterator) Close() {
and.cleanUp()
and.primaryIt.Close()
for _, it := range and.internalIterators {
it.Close()
func (it *AndIterator) Close() {
it.cleanUp()
it.primaryIt.Close()
for _, sub := range it.internalIterators {
sub.Close()
}
}
// Register this as an "and" iterator.
func (and *AndIterator) Type() string { return "and" }
func (it *AndIterator) Type() string { return "and" }

View file

@ -37,10 +37,10 @@ import (
// Optimizes the AndIterator, by picking the most efficient way to Next() and
// Check() its subiterators. For SQL fans, this is equivalent to JOIN.
func (and *AndIterator) Optimize() (Iterator, bool) {
func (it *AndIterator) Optimize() (Iterator, bool) {
// First, let's get the list of iterators, in order (first one is Next()ed,
// the rest are Check()ed)
oldItList := and.GetSubIterators()
oldItList := it.GetSubIterators()
// 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
@ -54,10 +54,10 @@ func (and *AndIterator) Optimize() (Iterator, bool) {
// If we can find only one subiterator which is equivalent to this whole and,
// we can replace the And...
out := and.optimizeReplacement(itList)
out := it.optimizeReplacement(itList)
if out != nil {
// ...Move the tags to the replacement...
moveTagsTo(out, and)
moveTagsTo(out, it)
// ...Close everyone except `out`, our replacement...
closeIteratorList(itList, out)
// ...And return it.
@ -80,14 +80,14 @@ func (and *AndIterator) Optimize() (Iterator, bool) {
}
// Move the tags hanging on us (like any good replacement).
newAnd.CopyTagsFrom(and)
newAnd.CopyTagsFrom(it)
newAnd.optimizeCheck()
// And close ourselves but not our subiterators -- some may still be alive in
// the new And (they were unchanged upon calling Optimize() on them, at the
// start).
and.cleanUp()
it.cleanUp()
return newAnd, true
}
@ -104,7 +104,7 @@ func closeIteratorList(l *list.List, except Iterator) {
// Find if there is a single subiterator which is a valid replacement for this
// AndIterator.
func (and *AndIterator) optimizeReplacement(itList *list.List) Iterator {
func (_ *AndIterator) optimizeReplacement(itList *list.List) Iterator {
// If we were created with no SubIterators, we're as good as Null.
if itList.Len() == 0 {
return &NullIterator{}
@ -190,8 +190,8 @@ func optimizeOrder(l *list.List) *list.List {
// optimizeCheck(l) creates an alternate check list, containing the same contents
// but with a new ordering, however it wishes.
func (and *AndIterator) optimizeCheck() {
subIts := and.GetSubIterators()
func (it *AndIterator) optimizeCheck() {
subIts := it.GetSubIterators()
out := list.New()
// Find the iterator with the lowest Check() cost, push it to the front, repeat.
@ -211,15 +211,15 @@ func (and *AndIterator) optimizeCheck() {
subIts.Remove(best)
}
and.checkList = out
it.checkList = out
}
// If we're replacing ourselves by a single iterator, we need to grab the
// result tags from the iterators that, while still valid and would hold
// the same values as this and, are not going to stay.
// getSubTags() returns a map of the tags for all the subiterators.
func (and *AndIterator) getSubTags() map[string]bool {
subs := and.GetSubIterators()
func (it *AndIterator) getSubTags() map[string]bool {
subs := it.GetSubIterators()
tags := make(map[string]bool)
for e := subs.Front(); e != nil; e = e.Next() {
it := e.Value.(Iterator)
@ -227,23 +227,23 @@ func (and *AndIterator) getSubTags() map[string]bool {
tags[tag] = true
}
}
for _, tag := range and.Tags() {
for _, tag := range it.Tags() {
tags[tag] = true
}
return tags
}
// moveTagsTo() gets the tags for all of the And's subiterators and the
// And itself, and moves them to `out`.
func moveTagsTo(out Iterator, and *AndIterator) {
tagmap := and.getSubTags()
for _, tag := range out.Tags() {
// moveTagsTo() gets the tags for all of the src's subiterators and the
// src itself, and moves them to dst.
func moveTagsTo(dst Iterator, src *AndIterator) {
tagmap := src.getSubTags()
for _, tag := range dst.Tags() {
if tagmap[tag] {
delete(tagmap, tag)
}
}
for k, _ := range tagmap {
out.AddTag(k)
dst.AddTag(k)
}
}
@ -308,13 +308,13 @@ func hasOneUsefulIterator(l *list.List) Iterator {
// and.GetStats() 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 (and *AndIterator) GetStats() *IteratorStats {
primaryStats := and.primaryIt.GetStats()
func (it *AndIterator) GetStats() *IteratorStats {
primaryStats := it.primaryIt.GetStats()
CheckCost := primaryStats.CheckCost
NextCost := primaryStats.NextCost
Size := primaryStats.Size
for _, it := range and.internalIterators {
stats := it.GetStats()
for _, sub := range it.internalIterators {
stats := sub.GetStats()
NextCost += stats.CheckCost
CheckCost += stats.CheckCost
if Size > stats.Size {

View file

@ -60,98 +60,97 @@ func NewFixedIteratorWithCompare(compareFn Equality) *FixedIterator {
return &it
}
func (f *FixedIterator) Reset() {
f.lastIndex = 0
func (it *FixedIterator) Reset() {
it.lastIndex = 0
}
func (f *FixedIterator) Close() {
}
func (it *FixedIterator) Close() {}
func (f *FixedIterator) Clone() Iterator {
out := NewFixedIteratorWithCompare(f.cmp)
for _, val := range f.values {
func (it *FixedIterator) Clone() Iterator {
out := NewFixedIteratorWithCompare(it.cmp)
for _, val := range it.values {
out.AddValue(val)
}
out.CopyTagsFrom(f)
out.CopyTagsFrom(it)
return out
}
// Add a value to the iterator. The array now contains this value.
// TODO(barakmich): This ought to be a set someday, disallowing repeated values.
func (f *FixedIterator) AddValue(v TSVal) {
f.values = append(f.values, v)
func (it *FixedIterator) AddValue(v TSVal) {
it.values = append(it.values, v)
}
// Print some information about the iterator.
func (f *FixedIterator) DebugString(indent int) string {
func (it *FixedIterator) DebugString(indent int) string {
value := ""
if len(f.values) > 0 {
value = fmt.Sprint(f.values[0])
if len(it.values) > 0 {
value = fmt.Sprint(it.values[0])
}
return fmt.Sprintf("%s(%s tags: %s Size: %d id0: %d)",
strings.Repeat(" ", indent),
f.Type(),
f.FixedTags(),
len(f.values),
it.Type(),
it.FixedTags(),
len(it.values),
value,
)
}
// Register this iterator as a Fixed iterator.
func (f *FixedIterator) Type() string {
func (it *FixedIterator) Type() string {
return "fixed"
}
// Check if the passed value is equal to one of the values stored in the iterator.
func (f *FixedIterator) Check(v TSVal) bool {
func (it *FixedIterator) Check(v TSVal) bool {
// Could be optimized by keeping it sorted or using a better datastructure.
// However, for fixed iterators, which are by definition kind of tiny, this
// isn't a big issue.
CheckLogIn(f, v)
for _, x := range f.values {
if f.cmp(x, v) {
f.Last = x
return CheckLogOut(f, v, true)
CheckLogIn(it, v)
for _, x := range it.values {
if it.cmp(x, v) {
it.Last = x
return CheckLogOut(it, v, true)
}
}
return CheckLogOut(f, v, false)
return CheckLogOut(it, v, false)
}
// Return the next stored value from the iterator.
func (f *FixedIterator) Next() (TSVal, bool) {
NextLogIn(f)
if f.lastIndex == len(f.values) {
return NextLogOut(f, nil, false)
func (it *FixedIterator) Next() (TSVal, bool) {
NextLogIn(it)
if it.lastIndex == len(it.values) {
return NextLogOut(it, nil, false)
}
out := f.values[f.lastIndex]
f.Last = out
f.lastIndex++
return NextLogOut(f, out, true)
out := it.values[it.lastIndex]
it.Last = out
it.lastIndex++
return NextLogOut(it, out, true)
}
// Optimize() for a Fixed iterator is simple. Returns a Null iterator if it's empty
// (so that other iterators upstream can treat this as null) or there is no
// optimization.
func (f *FixedIterator) Optimize() (Iterator, bool) {
func (it *FixedIterator) Optimize() (Iterator, bool) {
if len(f.values) == 1 && f.values[0] == nil {
if len(it.values) == 1 && it.values[0] == nil {
return &NullIterator{}, true
}
return f, false
return it, false
}
// Size is the number of values stored.
func (f *FixedIterator) Size() (int64, bool) {
return int64(len(f.values)), true
func (it *FixedIterator) Size() (int64, bool) {
return int64(len(it.values)), true
}
// 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 (a *FixedIterator) GetStats() *IteratorStats {
func (it *FixedIterator) GetStats() *IteratorStats {
return &IteratorStats{
CheckCost: int64(len(a.values)),
NextCost: int64(len(a.values)),
Size: int64(len(a.values)),
CheckCost: int64(len(it.values)),
NextCost: int64(len(it.values)),
Size: int64(len(it.values)),
}
}

View file

@ -36,8 +36,9 @@ package graph
import (
"container/list"
"fmt"
"github.com/barakmich/glog"
"strings"
"github.com/barakmich/glog"
)
// A HasaIterator consists of a reference back to the TripleStore that it references,
@ -63,94 +64,93 @@ func NewHasaIterator(ts TripleStore, subIt Iterator, dir string) *HasaIterator {
}
// Return our sole subiterator, in a list.List.
func (h *HasaIterator) GetSubIterators() *list.List {
func (it *HasaIterator) GetSubIterators() *list.List {
l := list.New()
l.PushBack(h.primaryIt)
l.PushBack(it.primaryIt)
return l
}
func (h *HasaIterator) Reset() {
h.primaryIt.Reset()
if h.resultIt != nil {
h.resultIt.Close()
func (it *HasaIterator) Reset() {
it.primaryIt.Reset()
if it.resultIt != nil {
it.resultIt.Close()
}
}
func (h *HasaIterator) Clone() Iterator {
out := NewHasaIterator(h.ts, h.primaryIt.Clone(), h.direction)
out.CopyTagsFrom(h)
func (it *HasaIterator) Clone() Iterator {
out := NewHasaIterator(it.ts, it.primaryIt.Clone(), it.direction)
out.CopyTagsFrom(it)
return out
}
// Direction accessor.
func (h *HasaIterator) Direction() string { return h.direction }
func (it *HasaIterator) Direction() string { return it.direction }
// Pass the Optimize() call along to the subiterator. If it becomes Null,
// then the HasA becomes Null (there are no triples that have any directions).
func (h *HasaIterator) Optimize() (Iterator, bool) {
newPrimary, changed := h.primaryIt.Optimize()
func (it *HasaIterator) Optimize() (Iterator, bool) {
newPrimary, changed := it.primaryIt.Optimize()
if changed {
h.primaryIt = newPrimary
if h.primaryIt.Type() == "null" {
return h.primaryIt, true
it.primaryIt = newPrimary
if it.primaryIt.Type() == "null" {
return it.primaryIt, true
}
}
return h, false
return it, false
}
// Pass the TagResults down the chain.
func (h *HasaIterator) TagResults(out *map[string]TSVal) {
h.BaseIterator.TagResults(out)
h.primaryIt.TagResults(out)
func (it *HasaIterator) TagResults(out *map[string]TSVal) {
it.BaseIterator.TagResults(out)
it.primaryIt.TagResults(out)
}
// DEPRECATED Return results in a ResultTree.
func (h *HasaIterator) GetResultTree() *ResultTree {
tree := NewResultTree(h.LastResult())
tree.AddSubtree(h.primaryIt.GetResultTree())
func (it *HasaIterator) GetResultTree() *ResultTree {
tree := NewResultTree(it.LastResult())
tree.AddSubtree(it.primaryIt.GetResultTree())
return tree
}
// Print some information about this iterator.
func (h *HasaIterator) DebugString(indent int) string {
func (it *HasaIterator) DebugString(indent int) string {
var tags string
for _, k := range h.Tags() {
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), h.Type(), h.GetUid(), tags, h.direction, h.primaryIt.DebugString(indent+4))
return fmt.Sprintf("%s(%s %d tags:%s direction:%s\n%s)", strings.Repeat(" ", indent), it.Type(), it.GetUid(), tags, it.direction, it.primaryIt.DebugString(indent+4))
}
// Check a value against our internal iterator. In order to do this, we must first open a new
// iterator of "triples that have `val` in our direction", given to us by the triple store,
// and then Next() values out of that iterator and Check() them against our subiterator.
func (h *HasaIterator) Check(val TSVal) bool {
CheckLogIn(h, val)
func (it *HasaIterator) Check(val TSVal) bool {
CheckLogIn(it, val)
if glog.V(4) {
glog.V(4).Infoln("Id is", h.ts.GetNameFor(val))
glog.V(4).Infoln("Id is", it.ts.GetNameFor(val))
}
// TODO(barakmich): Optimize this
if h.resultIt != nil {
h.resultIt.Close()
if it.resultIt != nil {
it.resultIt.Close()
}
h.resultIt = h.ts.GetTripleIterator(h.direction, val)
return CheckLogOut(h, val, h.GetCheckResult())
it.resultIt = it.ts.GetTripleIterator(it.direction, val)
return CheckLogOut(it, val, it.GetCheckResult())
}
// GetCheckResult() is shared code between Check() and GetNextResult() -- calls next on the
// result iterator (a triple iterator based on the last checked value) and returns true if
// another match is made.
func (h *HasaIterator) GetCheckResult() bool {
func (it *HasaIterator) GetCheckResult() bool {
for {
linkVal, ok := h.resultIt.Next()
linkVal, ok := it.resultIt.Next()
if !ok {
break
}
if glog.V(4) {
glog.V(4).Infoln("Triple is", h.ts.GetTriple(linkVal).ToString())
glog.V(4).Infoln("Triple is", it.ts.GetTriple(linkVal).ToString())
}
if h.primaryIt.Check(linkVal) {
h.Last = h.ts.GetTripleDirection(linkVal, h.direction)
if it.primaryIt.Check(linkVal) {
it.Last = it.ts.GetTripleDirection(linkVal, it.direction)
return true
}
}
@ -158,37 +158,37 @@ func (h *HasaIterator) GetCheckResult() bool {
}
// Get the next result that matches this branch.
func (h *HasaIterator) NextResult() bool {
func (it *HasaIterator) NextResult() bool {
// Order here is important. If the subiterator has a NextResult, then we
// need do nothing -- there is a next result, and we shouldn't move forward.
// However, we then need to get the next result from our last Check().
//
// The upshot is, the end of NextResult() bubbles up from the bottom of the
// iterator tree up, and we need to respect that.
if h.primaryIt.NextResult() {
if it.primaryIt.NextResult() {
return true
}
return h.GetCheckResult()
return it.GetCheckResult()
}
// Get the next result from this iterator. This is simpler than Check. We have a
// subiterator we can get a value from, and we can take that resultant triple,
// pull our direction out of it, and return that.
func (h *HasaIterator) Next() (TSVal, bool) {
NextLogIn(h)
if h.resultIt != nil {
h.resultIt.Close()
func (it *HasaIterator) Next() (TSVal, bool) {
NextLogIn(it)
if it.resultIt != nil {
it.resultIt.Close()
}
h.resultIt = &NullIterator{}
it.resultIt = &NullIterator{}
tID, ok := h.primaryIt.Next()
tID, ok := it.primaryIt.Next()
if !ok {
return NextLogOut(h, 0, false)
return NextLogOut(it, 0, false)
}
name := h.ts.GetTriple(tID).Get(h.direction)
val := h.ts.GetIdFor(name)
h.Last = val
return NextLogOut(h, val, true)
name := it.ts.GetTriple(tID).Get(it.direction)
val := it.ts.GetIdFor(name)
it.Last = val
return NextLogOut(it, val, true)
}
// GetStats() returns the statistics on the HasA iterator. This is curious. Next
@ -197,8 +197,8 @@ func (h *HasaIterator) Next() (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 (h *HasaIterator) GetStats() *IteratorStats {
subitStats := h.primaryIt.GetStats()
func (it *HasaIterator) GetStats() *IteratorStats {
subitStats := it.primaryIt.GetStats()
// TODO(barakmich): These should really come from the triplestore itself
// and be optimized.
faninFactor := int64(1)
@ -213,12 +213,12 @@ func (h *HasaIterator) GetStats() *IteratorStats {
}
// Close the subiterator, the result iterator (if any) and the HasA.
func (h *HasaIterator) Close() {
if h.resultIt != nil {
h.resultIt.Close()
func (it *HasaIterator) Close() {
if it.resultIt != nil {
it.resultIt.Close()
}
h.primaryIt.Close()
it.primaryIt.Close()
}
// Register this iterator as a HasA.
func (h *HasaIterator) Type() string { return "hasa" }
func (it *HasaIterator) Type() string { return "hasa" }

View file

@ -20,8 +20,9 @@ package graph
import (
"container/list"
"fmt"
"github.com/barakmich/glog"
"strings"
"github.com/barakmich/glog"
)
var iterator_n int = 0
@ -120,50 +121,50 @@ type BaseIterator struct {
}
// Called by subclases.
func BaseIteratorInit(b *BaseIterator) {
func BaseIteratorInit(it *BaseIterator) {
// Your basic iterator is nextable
b.nextable = true
b.uid = iterator_n
it.nextable = true
it.uid = iterator_n
if glog.V(2) {
iterator_n++
}
}
func (b *BaseIterator) GetUid() int {
return b.uid
func (it *BaseIterator) GetUid() int {
return it.uid
}
// Adds a tag to the iterator. Most iterators don't need to override.
func (b *BaseIterator) AddTag(tag string) {
if b.tags == nil {
b.tags = make([]string, 0)
func (it *BaseIterator) AddTag(tag string) {
if it.tags == nil {
it.tags = make([]string, 0)
}
b.tags = append(b.tags, tag)
it.tags = append(it.tags, tag)
}
func (b *BaseIterator) AddFixedTag(tag string, value TSVal) {
if b.fixedTags == nil {
b.fixedTags = make(map[string]TSVal)
func (it *BaseIterator) AddFixedTag(tag string, value TSVal) {
if it.fixedTags == nil {
it.fixedTags = make(map[string]TSVal)
}
b.fixedTags[tag] = value
it.fixedTags[tag] = value
}
// Returns the tags.
func (b *BaseIterator) Tags() []string {
return b.tags
func (it *BaseIterator) Tags() []string {
return it.tags
}
func (b *BaseIterator) FixedTags() map[string]TSVal {
return b.fixedTags
func (it *BaseIterator) FixedTags() map[string]TSVal {
return it.fixedTags
}
func (b *BaseIterator) CopyTagsFrom(other_it Iterator) {
func (it *BaseIterator) CopyTagsFrom(other_it Iterator) {
for _, tag := range other_it.Tags() {
b.AddTag(tag)
it.AddTag(tag)
}
for k, v := range other_it.FixedTags() {
b.AddFixedTag(k, v)
it.AddFixedTag(k, v)
}
}
@ -185,55 +186,55 @@ func (n *BaseIterator) GetStats() *IteratorStats {
}
// DEPRECATED
func (b *BaseIterator) GetResultTree() *ResultTree {
tree := NewResultTree(b.LastResult())
func (it *BaseIterator) GetResultTree() *ResultTree {
tree := NewResultTree(it.LastResult())
return tree
}
// Nothing in a base iterator.
func (n *BaseIterator) Next() (TSVal, bool) {
func (it *BaseIterator) Next() (TSVal, bool) {
return nil, false
}
func (n *BaseIterator) NextResult() bool {
func (it *BaseIterator) NextResult() bool {
return false
}
// Returns the last result of an iterator.
func (n *BaseIterator) LastResult() TSVal {
return n.Last
func (it *BaseIterator) LastResult() TSVal {
return it.Last
}
// If you're empty and you know it, clap your hands.
func (n *BaseIterator) Size() (int64, bool) {
func (it *BaseIterator) Size() (int64, bool) {
return 0, true
}
// No subiterators. Only those with subiterators need to do anything here.
func (n *BaseIterator) GetSubIterators() *list.List {
func (it *BaseIterator) GetSubIterators() *list.List {
return nil
}
// Accessor
func (b *BaseIterator) Nextable() bool { return b.nextable }
func (it *BaseIterator) Nextable() bool { return it.nextable }
// Fill the map based on the tags assigned to this iterator. Default
// functionality works well for most iterators.
func (a *BaseIterator) TagResults(out_map *map[string]TSVal) {
for _, tag := range a.Tags() {
(*out_map)[tag] = a.LastResult()
func (it *BaseIterator) TagResults(out_map *map[string]TSVal) {
for _, tag := range it.Tags() {
(*out_map)[tag] = it.LastResult()
}
for tag, value := range a.FixedTags() {
for tag, value := range it.FixedTags() {
(*out_map)[tag] = value
}
}
// Nothing to clean up.
//func (a *BaseIterator) Close() {}
func (a *NullIterator) Close() {}
func (it *NullIterator) Close() {}
func (a *BaseIterator) Reset() {}
func (it *BaseIterator) Reset() {}
// Here we define the simplest base iterator -- the Null iterator. It contains nothing.
// It is the empty set. Often times, queries that contain one of these match nothing,
@ -244,26 +245,25 @@ type NullIterator struct {
// Fairly useless New function.
func NewNullIterator() *NullIterator {
var n NullIterator
return &n
return &NullIterator{}
}
func (n *NullIterator) Clone() Iterator { return NewNullIterator() }
func (it *NullIterator) Clone() Iterator { return NewNullIterator() }
// Name the null iterator.
func (n *NullIterator) Type() string { return "null" }
func (it *NullIterator) Type() string { return "null" }
// A good iterator will close itself when it returns true.
// Null has nothing it needs to do.
func (n *NullIterator) Optimize() (Iterator, bool) { return n, false }
func (it *NullIterator) Optimize() (Iterator, bool) { return it, false }
// Print the null iterator.
func (n *NullIterator) DebugString(indent int) string {
func (it *NullIterator) DebugString(indent int) string {
return strings.Repeat(" ", indent) + "(null)"
}
// A null iterator costs nothing. Use it!
func (n *NullIterator) GetStats() *IteratorStats {
func (it *NullIterator) GetStats() *IteratorStats {
return &IteratorStats{0, 0, 0}
}

View file

@ -28,8 +28,9 @@ package graph
import (
"fmt"
"github.com/barakmich/glog"
"strings"
"github.com/barakmich/glog"
)
// An optional iterator has the subconstraint iterator we wish to be optional
@ -49,24 +50,24 @@ func NewOptionalIterator(it Iterator) *OptionalIterator {
return &o
}
func (o *OptionalIterator) Reset() {
o.subIt.Reset()
o.lastCheck = false
func (it *OptionalIterator) Reset() {
it.subIt.Reset()
it.lastCheck = false
}
func (o *OptionalIterator) Close() {
o.subIt.Close()
func (it *OptionalIterator) Close() {
it.subIt.Close()
}
func (o *OptionalIterator) Clone() Iterator {
out := NewOptionalIterator(o.subIt.Clone())
out.CopyTagsFrom(o)
func (it *OptionalIterator) Clone() Iterator {
out := NewOptionalIterator(it.subIt.Clone())
out.CopyTagsFrom(it)
return out
}
// Nexting the iterator is unsupported -- error and return an empty set.
// (As above, a reasonable alternative would be to Next() an all iterator)
func (o *OptionalIterator) Next() (TSVal, bool) {
func (it *OptionalIterator) Next() (TSVal, bool) {
glog.Errorln("Nexting an un-nextable iterator")
return nil, false
}
@ -74,9 +75,9 @@ func (o *OptionalIterator) Next() (TSVal, bool) {
// An optional iterator only has a next result if, (a) last time we checked
// we had any results whatsoever, and (b) there was another subresult in our
// optional subbranch.
func (o *OptionalIterator) NextResult() bool {
if o.lastCheck {
return o.subIt.NextResult()
func (it *OptionalIterator) NextResult() bool {
if it.lastCheck {
return it.subIt.NextResult()
}
return false
}
@ -84,48 +85,48 @@ func (o *OptionalIterator) NextResult() bool {
// Check() is the real hack of this iterator. It always returns true, regardless
// of whether the subiterator matched. But we keep track of whether the subiterator
// matched for results purposes.
func (o *OptionalIterator) Check(val TSVal) bool {
checked := o.subIt.Check(val)
o.lastCheck = checked
o.Last = val
func (it *OptionalIterator) Check(val TSVal) bool {
checked := it.subIt.Check(val)
it.lastCheck = checked
it.Last = val
return true
}
// If we failed the check, then the subiterator should not contribute to the result
// set. Otherwise, go ahead and tag it.
func (o *OptionalIterator) TagResults(out *map[string]TSVal) {
if o.lastCheck == false {
func (it *OptionalIterator) TagResults(out *map[string]TSVal) {
if it.lastCheck == false {
return
}
o.subIt.TagResults(out)
it.subIt.TagResults(out)
}
// Registers the optional iterator.
func (o *OptionalIterator) Type() string { return "optional" }
func (it *OptionalIterator) Type() string { return "optional" }
// Prints the optional and it's subiterator.
func (o *OptionalIterator) DebugString(indent int) string {
func (it *OptionalIterator) DebugString(indent int) string {
return fmt.Sprintf("%s(%s tags:%s\n%s)",
strings.Repeat(" ", indent),
o.Type(),
o.Tags(),
o.subIt.DebugString(indent+4))
it.Type(),
it.Tags(),
it.subIt.DebugString(indent+4))
}
// There's nothing to optimize for an optional. Optimize the subiterator and
// potentially replace it.
func (o *OptionalIterator) Optimize() (Iterator, bool) {
newSub, changed := o.subIt.Optimize()
func (it *OptionalIterator) Optimize() (Iterator, bool) {
newSub, changed := it.subIt.Optimize()
if changed {
o.subIt.Close()
o.subIt = newSub
it.subIt.Close()
it.subIt = newSub
}
return o, false
return it, false
}
// We're only as expensive as our subiterator. Except, we can't be nexted.
func (o *OptionalIterator) GetStats() *IteratorStats {
subStats := o.subIt.GetStats()
func (it *OptionalIterator) GetStats() *IteratorStats {
subStats := it.subIt.GetStats()
return &IteratorStats{
CheckCost: subStats.CheckCost,
NextCost: int64(1 << 62),

View file

@ -54,68 +54,68 @@ func NewShortCircuitOrIterator() *OrIterator {
}
// Reset all internal iterators
func (or *OrIterator) Reset() {
for _, it := range or.internalIterators {
it.Reset()
func (it *OrIterator) Reset() {
for _, sub := range it.internalIterators {
sub.Reset()
}
or.currentIterator = -1
it.currentIterator = -1
}
func (or *OrIterator) Clone() Iterator {
var newOr *OrIterator
if or.isShortCircuiting {
newOr = NewShortCircuitOrIterator()
func (it *OrIterator) Clone() Iterator {
var or *OrIterator
if it.isShortCircuiting {
or = NewShortCircuitOrIterator()
} else {
newOr = NewOrIterator()
or = NewOrIterator()
}
for _, it := range or.internalIterators {
newOr.AddSubIterator(it.Clone())
for _, sub := range it.internalIterators {
or.AddSubIterator(sub.Clone())
}
or.CopyTagsFrom(or)
return newOr
it.CopyTagsFrom(it)
return or
}
// Returns a list.List of the subiterators, in order.
func (or *OrIterator) GetSubIterators() *list.List {
func (it *OrIterator) GetSubIterators() *list.List {
l := list.New()
for _, it := range or.internalIterators {
l.PushBack(it)
for _, sub := range it.internalIterators {
l.PushBack(sub)
}
return l
}
// Overrides BaseIterator TagResults, as it needs to add it's own results and
// recurse down it's subiterators.
func (or *OrIterator) TagResults(out *map[string]TSVal) {
or.BaseIterator.TagResults(out)
or.internalIterators[or.currentIterator].TagResults(out)
func (it *OrIterator) TagResults(out *map[string]TSVal) {
it.BaseIterator.TagResults(out)
it.internalIterators[it.currentIterator].TagResults(out)
}
// DEPRECATED Returns the ResultTree for this iterator, recurses to it's subiterators.
func (or *OrIterator) GetResultTree() *ResultTree {
tree := NewResultTree(or.LastResult())
for _, it := range or.internalIterators {
tree.AddSubtree(it.GetResultTree())
func (it *OrIterator) GetResultTree() *ResultTree {
tree := NewResultTree(it.LastResult())
for _, sub := range it.internalIterators {
tree.AddSubtree(sub.GetResultTree())
}
return tree
}
// Prints information about this iterator.
func (or *OrIterator) DebugString(indent int) string {
func (it *OrIterator) DebugString(indent int) string {
var total string
for i, it := range or.internalIterators {
for i, sub := range it.internalIterators {
total += strings.Repeat(" ", indent+2)
total += fmt.Sprintf("%d:\n%s\n", i, it.DebugString(indent+4))
total += fmt.Sprintf("%d:\n%s\n", i, sub.DebugString(indent+4))
}
var tags string
for _, k := range or.Tags() {
for _, k := range it.Tags() {
tags += fmt.Sprintf("%s;", k)
}
spaces := strings.Repeat(" ", indent+2)
return fmt.Sprintf("%s(%s\n%stags:%s\n%sits:\n%s)",
strings.Repeat(" ", indent),
or.Type(),
it.Type(),
spaces,
tags,
spaces,
@ -123,49 +123,49 @@ func (or *OrIterator) DebugString(indent int) string {
}
// Add a subiterator to this Or iterator. Order matters.
func (or *OrIterator) AddSubIterator(sub Iterator) {
or.internalIterators = append(or.internalIterators, sub)
or.itCount++
func (it *OrIterator) AddSubIterator(sub Iterator) {
it.internalIterators = append(it.internalIterators, sub)
it.itCount++
}
// Returns the Next value from the Or iterator. Because the Or is the
// union of its subiterators, it must produce from all subiterators -- unless
// it's shortcircuiting, in which case, it's the first one that returns anything.
func (or *OrIterator) Next() (TSVal, bool) {
NextLogIn(or)
func (it *OrIterator) Next() (TSVal, bool) {
NextLogIn(it)
var curr TSVal
var exists bool
firstTime := false
for {
if or.currentIterator == -1 {
or.currentIterator = 0
if it.currentIterator == -1 {
it.currentIterator = 0
firstTime = true
}
curIt := or.internalIterators[or.currentIterator]
curIt := it.internalIterators[it.currentIterator]
curr, exists = curIt.Next()
if !exists {
if or.isShortCircuiting && !firstTime {
return NextLogOut(or, nil, false)
if it.isShortCircuiting && !firstTime {
return NextLogOut(it, nil, false)
}
or.currentIterator++
if or.currentIterator == or.itCount {
return NextLogOut(or, nil, false)
it.currentIterator++
if it.currentIterator == it.itCount {
return NextLogOut(it, nil, false)
}
} else {
or.Last = curr
return NextLogOut(or, curr, true)
it.Last = curr
return NextLogOut(it, curr, true)
}
}
panic("Somehow broke out of Next() loop in OrIterator")
}
// Checks a value against the iterators, in order.
func (or *OrIterator) checkSubIts(val TSVal) bool {
func (it *OrIterator) checkSubIts(val TSVal) bool {
var subIsGood = false
for i, it := range or.internalIterators {
subIsGood = it.Check(val)
for i, sub := range it.internalIterators {
subIsGood = sub.Check(val)
if subIsGood {
or.currentIterator = i
it.currentIterator = i
break
}
}
@ -173,27 +173,27 @@ func (or *OrIterator) checkSubIts(val TSVal) bool {
}
// Check a value against the entire iterator, in order.
func (or *OrIterator) Check(val TSVal) bool {
CheckLogIn(or, val)
anyGood := or.checkSubIts(val)
func (it *OrIterator) Check(val TSVal) bool {
CheckLogIn(it, val)
anyGood := it.checkSubIts(val)
if !anyGood {
return CheckLogOut(or, val, false)
return CheckLogOut(it, val, false)
}
or.Last = val
return CheckLogOut(or, val, true)
it.Last = val
return CheckLogOut(it, val, true)
}
// Returns the approximate size of the Or iterator. Because we're dealing
// with a union, we know that the largest we can be is the sum of all the iterators,
// or in the case of short-circuiting, the longest.
func (or *OrIterator) Size() (int64, bool) {
func (it *OrIterator) Size() (int64, bool) {
var val int64
var b bool
if or.isShortCircuiting {
if it.isShortCircuiting {
val = 0
b = true
for _, it := range or.internalIterators {
newval, newb := it.Size()
for _, sub := range it.internalIterators {
newval, newb := sub.Size()
if val < newval {
val = newval
}
@ -202,8 +202,8 @@ func (or *OrIterator) Size() (int64, bool) {
} else {
val = 0
b = true
for _, it := range or.internalIterators {
newval, newb := it.Size()
for _, sub := range it.internalIterators {
newval, newb := sub.Size()
val += newval
b = newb && b
}
@ -215,34 +215,34 @@ func (or *OrIterator) Size() (int64, bool) {
// which satisfy our previous result that are not the result itself. Our
// subiterators might, however, so just pass the call recursively. In the case of
// shortcircuiting, only allow new results from the currently checked iterator
func (or *OrIterator) NextResult() bool {
if or.currentIterator != -1 {
return or.internalIterators[or.currentIterator].NextResult()
func (it *OrIterator) NextResult() bool {
if it.currentIterator != -1 {
return it.internalIterators[it.currentIterator].NextResult()
}
return false
}
// Perform or-specific cleanup, of which there currently is none.
func (or *OrIterator) cleanUp() {}
func (it *OrIterator) cleanUp() {}
// Close this iterator, and, by extension, close the subiterators.
// Close should be idempotent, and it follows that if it's subiterators
// follow this contract, the And follows the contract.
func (or *OrIterator) Close() {
or.cleanUp()
for _, it := range or.internalIterators {
it.Close()
func (it *OrIterator) Close() {
it.cleanUp()
for _, sub := range it.internalIterators {
sub.Close()
}
}
func (or *OrIterator) Optimize() (Iterator, bool) {
oldItList := or.GetSubIterators()
func (it *OrIterator) Optimize() (Iterator, bool) {
oldItList := it.GetSubIterators()
itList := optimizeSubIterators(oldItList)
// Close the replaced iterators (they ought to close themselves, but Close()
// is idempotent, so this just protects against any machinations).
closeIteratorList(oldItList, nil)
newOr := NewOrIterator()
newOr.isShortCircuiting = or.isShortCircuiting
newOr.isShortCircuiting = it.isShortCircuiting
// Add the subiterators in order.
for e := itList.Front(); e != nil; e = e.Next() {
@ -250,24 +250,24 @@ func (or *OrIterator) Optimize() (Iterator, bool) {
}
// Move the tags hanging on us (like any good replacement).
newOr.CopyTagsFrom(or)
newOr.CopyTagsFrom(it)
// And close ourselves but not our subiterators -- some may still be alive in
// the new And (they were unchanged upon calling Optimize() on them, at the
// start).
or.cleanUp()
it.cleanUp()
return newOr, true
}
func (or *OrIterator) GetStats() *IteratorStats {
func (it *OrIterator) GetStats() *IteratorStats {
CheckCost := int64(0)
NextCost := int64(0)
Size := int64(0)
for _, it := range or.internalIterators {
stats := it.GetStats()
for _, sub := range it.internalIterators {
stats := sub.GetStats()
NextCost += stats.NextCost
CheckCost += stats.CheckCost
if or.isShortCircuiting {
if it.isShortCircuiting {
if Size < stats.Size {
Size = stats.Size
}
@ -284,4 +284,4 @@ func (or *OrIterator) GetStats() *IteratorStats {
}
// Register this as an "or" iterator.
func (or *OrIterator) Type() string { return "or" }
func (it *OrIterator) Type() string { return "or" }

View file

@ -25,16 +25,16 @@ type ResultTree struct {
}
func NewResultTree(result TSVal) *ResultTree {
var tree ResultTree
tree.subtrees = list.New()
tree.result = result
return &tree
var t ResultTree
t.subtrees = list.New()
t.result = result
return &t
}
func (tree *ResultTree) ToString() string {
base := fmt.Sprintf("(%d", tree.result)
if tree.subtrees.Len() != 0 {
for e := tree.subtrees.Front(); e != nil; e = e.Next() {
func (t *ResultTree) ToString() string {
base := fmt.Sprintf("(%d", t.result)
if t.subtrees.Len() != 0 {
for e := t.subtrees.Front(); e != nil; e = e.Next() {
base += fmt.Sprintf(" %s", (e.Value.(*ResultTree)).ToString())
}
}
@ -42,8 +42,8 @@ func (tree *ResultTree) ToString() string {
return base
}
func (tree *ResultTree) AddSubtree(sub *ResultTree) {
tree.subtrees.PushBack(sub)
func (t *ResultTree) AddSubtree(sub *ResultTree) {
t.subtrees.PushBack(sub)
}
func StringResultTreeEvaluator(it Iterator) string {

View file

@ -68,30 +68,30 @@ func NewValueComparisonIterator(
// Here's the non-boilerplate part of the ValueComparison iterator. Given a value
// and our operator, determine whether or not we meet the requirement.
func (vc *ValueComparisonIterator) doComparison(val TSVal) bool {
func (it *ValueComparisonIterator) doComparison(val TSVal) bool {
//TODO(barakmich): Implement string comparison.
nodeStr := vc.ts.GetNameFor(val)
switch cVal := vc.comparisonValue.(type) {
nodeStr := it.ts.GetNameFor(val)
switch cVal := it.comparisonValue.(type) {
case int:
cInt := int64(cVal)
intVal, err := strconv.ParseInt(nodeStr, 10, 64)
if err != nil {
return false
}
return RunIntOp(intVal, vc.op, cInt)
return RunIntOp(intVal, it.op, cInt)
case int64:
intVal, err := strconv.ParseInt(nodeStr, 10, 64)
if err != nil {
return false
}
return RunIntOp(intVal, vc.op, cVal)
return RunIntOp(intVal, it.op, cVal)
default:
return true
}
}
func (vc *ValueComparisonIterator) Close() {
vc.subIt.Close()
func (it *ValueComparisonIterator) Close() {
it.subIt.Close()
}
func RunIntOp(a int64, op ComparisonOperator, b int64) bool {
@ -110,84 +110,84 @@ func RunIntOp(a int64, op ComparisonOperator, b int64) bool {
}
}
func (vc *ValueComparisonIterator) Reset() {
vc.subIt.Reset()
func (it *ValueComparisonIterator) Reset() {
it.subIt.Reset()
}
func (vc *ValueComparisonIterator) Clone() Iterator {
out := NewValueComparisonIterator(vc.subIt.Clone(), vc.op, vc.comparisonValue, vc.ts)
out.CopyTagsFrom(vc)
func (it *ValueComparisonIterator) Clone() Iterator {
out := NewValueComparisonIterator(it.subIt.Clone(), it.op, it.comparisonValue, it.ts)
out.CopyTagsFrom(it)
return out
}
func (vc *ValueComparisonIterator) Next() (TSVal, bool) {
func (it *ValueComparisonIterator) Next() (TSVal, bool) {
var val TSVal
var ok bool
for {
val, ok = vc.subIt.Next()
val, ok = it.subIt.Next()
if !ok {
return nil, false
}
if vc.doComparison(val) {
if it.doComparison(val) {
break
}
}
vc.Last = val
it.Last = val
return val, ok
}
func (vc *ValueComparisonIterator) NextResult() bool {
func (it *ValueComparisonIterator) NextResult() bool {
for {
hasNext := vc.subIt.NextResult()
hasNext := it.subIt.NextResult()
if !hasNext {
return false
}
if vc.doComparison(vc.subIt.LastResult()) {
if it.doComparison(it.subIt.LastResult()) {
return true
}
}
vc.Last = vc.subIt.LastResult()
it.Last = it.subIt.LastResult()
return true
}
func (vc *ValueComparisonIterator) Check(val TSVal) bool {
if !vc.doComparison(val) {
func (it *ValueComparisonIterator) Check(val TSVal) bool {
if !it.doComparison(val) {
return false
}
return vc.subIt.Check(val)
return it.subIt.Check(val)
}
// If we failed the check, then the subiterator should not contribute to the result
// set. Otherwise, go ahead and tag it.
func (vc *ValueComparisonIterator) TagResults(out *map[string]TSVal) {
vc.BaseIterator.TagResults(out)
vc.subIt.TagResults(out)
func (it *ValueComparisonIterator) TagResults(out *map[string]TSVal) {
it.BaseIterator.TagResults(out)
it.subIt.TagResults(out)
}
// Registers the value-comparison iterator.
func (vc *ValueComparisonIterator) Type() string { return "value-comparison" }
func (it *ValueComparisonIterator) Type() string { return "value-comparison" }
// Prints the value-comparison and its subiterator.
func (vc *ValueComparisonIterator) DebugString(indent int) string {
func (it *ValueComparisonIterator) DebugString(indent int) string {
return fmt.Sprintf("%s(%s\n%s)",
strings.Repeat(" ", indent),
vc.Type(), vc.subIt.DebugString(indent+4))
it.Type(), it.subIt.DebugString(indent+4))
}
// There's nothing to optimize, locally, for a value-comparison iterator.
// Replace the underlying iterator if need be.
// potentially replace it.
func (vc *ValueComparisonIterator) Optimize() (Iterator, bool) {
newSub, changed := vc.subIt.Optimize()
func (it *ValueComparisonIterator) Optimize() (Iterator, bool) {
newSub, changed := it.subIt.Optimize()
if changed {
vc.subIt.Close()
vc.subIt = newSub
it.subIt.Close()
it.subIt = newSub
}
return vc, false
return it, false
}
// We're only as expensive as our subiterator.
// Again, optimized value comparison iterators should do better.
func (vc *ValueComparisonIterator) GetStats() *IteratorStats {
return vc.subIt.GetStats()
func (it *ValueComparisonIterator) GetStats() *IteratorStats {
return it.subIt.GetStats()
}