Skip to content

Commit

Permalink
fix overshoot and optimize better
Browse files Browse the repository at this point in the history
  • Loading branch information
barakmich committed Aug 6, 2014
1 parent 09244dd commit 24f57df
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion cayley_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@ func TestQueries(t *testing.T) {

// TODO(kortschak) Be more rigorous in this result validation.
if len(got) != len(test.expect) {
t.Errorf("Unexpected number of results, got:%d expect:%d.", len(got), len(test.expect))
t.Errorf("Unexpected number of results, got:%d expect:%d on %s.", len(got), len(test.expect), test.message)
}
}
}
Expand Down
7 changes: 5 additions & 2 deletions graph/iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,14 @@ func Next(it Iterator) (Value, bool) {
}

// Height is a convienence function to measure the height of an iterator tree.
func Height(it Iterator) int {
func Height(it Iterator, until Type) int {
if it.Type() == until {
return 1
}
subs := it.SubIterators()
maxDepth := 0
for _, sub := range subs {
h := Height(sub)
h := Height(sub, until)
if h > maxDepth {
maxDepth = h
}
Expand Down
2 changes: 1 addition & 1 deletion graph/iterator/and_iterator_optimize.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ func materializeIts(its []graph.Iterator) []graph.Iterator {
for _, it := range its {
stats := it.Stats()
if stats.Size*stats.NextCost < stats.ContainsCost {
if graph.Height(it) > 10 {
if graph.Height(it, graph.Materialize) > 10 {
out = append(out, NewMaterialize(it))
continue
}
Expand Down
20 changes: 14 additions & 6 deletions graph/iterator/materialize_iterator.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,16 @@ func (it *Materialize) TagResults(dst map[string]graph.Value) {
if !it.hasRun {
return
}
if it.aborted {
it.subIt.TagResults(dst)
return
}
if it.lastIndex > len(it.values) {
return
}
for _, tag := range it.tags.Tags() {
dst[tag] = it.Result()
}

for tag, value := range it.values[it.lastIndex].tags {
dst[tag] = value
}
Expand Down Expand Up @@ -154,6 +160,7 @@ func (it *Materialize) Stats() graph.IteratorStats {
}

func (it *Materialize) Next() (graph.Value, bool) {
graph.NextLogIn(it)
if !it.hasRun {
it.materializeSet()
}
Expand All @@ -164,14 +171,15 @@ func (it *Materialize) Next() (graph.Value, bool) {
lastVal := it.Result()
for it.lastIndex < len(it.values) {
it.lastIndex++
if it.Result() != lastVal {
return it.Result(), true
if it.Result() != lastVal && it.Result() != nil {
return graph.NextLogOut(it, it.Result(), true)
}
}
return nil, false
return graph.NextLogOut(it, nil, false)
}

func (it *Materialize) Contains(v graph.Value) bool {
graph.ContainsLogIn(it, v)
if !it.hasRun {
it.materializeSet()
}
Expand All @@ -180,9 +188,9 @@ func (it *Materialize) Contains(v graph.Value) bool {
}
if i, ok := it.containsMap[v]; ok {
it.lastIndex = i
return true
return graph.ContainsLogOut(it, v, true)
}
return false
return graph.ContainsLogOut(it, v, false)
}

func (it *Materialize) NextResult() bool {
Expand Down

0 comments on commit 24f57df

Please sign in to comment.