Fix Err fallout for graph/iterator.Materialize iterator
This commit is contained in:
parent
0148f6ef12
commit
912b126e92
2 changed files with 89 additions and 2 deletions
|
|
@ -48,6 +48,7 @@ type Materialize struct {
|
||||||
subIt graph.Iterator
|
subIt graph.Iterator
|
||||||
hasRun bool
|
hasRun bool
|
||||||
aborted bool
|
aborted bool
|
||||||
|
err error
|
||||||
runstats graph.IteratorStats
|
runstats graph.IteratorStats
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -108,6 +109,7 @@ func (it *Materialize) Clone() graph.Iterator {
|
||||||
if it.hasRun {
|
if it.hasRun {
|
||||||
out.hasRun = true
|
out.hasRun = true
|
||||||
out.aborted = it.aborted
|
out.aborted = it.aborted
|
||||||
|
out.err = it.err
|
||||||
out.values = it.values
|
out.values = it.values
|
||||||
out.containsMap = it.containsMap
|
out.containsMap = it.containsMap
|
||||||
out.actualSize = it.actualSize
|
out.actualSize = it.actualSize
|
||||||
|
|
@ -199,8 +201,15 @@ func (it *Materialize) Next() bool {
|
||||||
if !it.hasRun {
|
if !it.hasRun {
|
||||||
it.materializeSet()
|
it.materializeSet()
|
||||||
}
|
}
|
||||||
|
if it.err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
if it.aborted {
|
if it.aborted {
|
||||||
return graph.Next(it.subIt)
|
n := graph.Next(it.subIt)
|
||||||
|
if err := graph.Err(it.subIt); err != nil {
|
||||||
|
it.err = err
|
||||||
|
}
|
||||||
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
it.index++
|
it.index++
|
||||||
|
|
@ -211,6 +220,10 @@ func (it *Materialize) Next() bool {
|
||||||
return graph.NextLogOut(it, it.Result(), true)
|
return graph.NextLogOut(it, it.Result(), true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (it *Materialize) Err() error {
|
||||||
|
return it.err
|
||||||
|
}
|
||||||
|
|
||||||
func (it *Materialize) Contains(v graph.Value) bool {
|
func (it *Materialize) Contains(v graph.Value) bool {
|
||||||
graph.ContainsLogIn(it, v)
|
graph.ContainsLogIn(it, v)
|
||||||
it.runstats.Contains += 1
|
it.runstats.Contains += 1
|
||||||
|
|
@ -283,7 +296,9 @@ func (it *Materialize) materializeSet() {
|
||||||
it.actualSize += 1
|
it.actualSize += 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if it.aborted {
|
if err := graph.Err(it.subIt); err != nil {
|
||||||
|
it.err = err
|
||||||
|
} else if it.aborted {
|
||||||
if glog.V(2) {
|
if glog.V(2) {
|
||||||
glog.V(2).Infoln("Aborting subiterator")
|
glog.V(2).Infoln("Aborting subiterator")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
72
graph/iterator/materialize_iterator_test.go
Normal file
72
graph/iterator/materialize_iterator_test.go
Normal file
|
|
@ -0,0 +1,72 @@
|
||||||
|
// Copyright 2014 The Cayley Authors. All rights reserved.
|
||||||
|
//
|
||||||
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
// you may not use this file except in compliance with the License.
|
||||||
|
// You may obtain a copy of the License at
|
||||||
|
//
|
||||||
|
// http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
//
|
||||||
|
// Unless required by applicable law or agreed to in writing, software
|
||||||
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
// See the License for the specific language governing permissions and
|
||||||
|
// limitations under the License.
|
||||||
|
|
||||||
|
package iterator
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
//"github.com/google/cayley/graph"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMaterializeIteratorError(t *testing.T) {
|
||||||
|
retErr := errors.New("unique")
|
||||||
|
errIt := newTestIterator(false, retErr)
|
||||||
|
|
||||||
|
// This tests that we properly return 0 results and the error when the
|
||||||
|
// underlying iterator returns an error.
|
||||||
|
mIt := NewMaterialize(errIt)
|
||||||
|
|
||||||
|
if mIt.Next() != false {
|
||||||
|
t.Errorf("Materialize iterator did not pass through underlying 'false'")
|
||||||
|
}
|
||||||
|
if mIt.Err() != retErr {
|
||||||
|
t.Errorf("Materialize iterator did not pass through underlying Err")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMaterializeIteratorErrorAbort(t *testing.T) {
|
||||||
|
retErr := errors.New("unique")
|
||||||
|
errIt := newTestIterator(false, retErr)
|
||||||
|
|
||||||
|
// This tests that we properly return 0 results and the error when the
|
||||||
|
// underlying iterator is larger than our 'abort at' value, and then
|
||||||
|
// returns an error.
|
||||||
|
or := NewOr()
|
||||||
|
or.AddSubIterator(NewInt64(1, int64(abortMaterializeAt+1)))
|
||||||
|
or.AddSubIterator(errIt)
|
||||||
|
|
||||||
|
mIt := NewMaterialize(or)
|
||||||
|
|
||||||
|
// Should get all the underlying values...
|
||||||
|
for i := 0; i < abortMaterializeAt+1; i++ {
|
||||||
|
if !mIt.Next() {
|
||||||
|
t.Errorf("Materialize iterator returned spurious 'false' on iteration %d", i)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if mIt.Err() != nil {
|
||||||
|
t.Errorf("Materialize iterator returned non-nil Err on iteration %d", i)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ... and then the error value
|
||||||
|
if mIt.Next() != false {
|
||||||
|
t.Errorf("Materialize iterator did not pass through underlying 'false'")
|
||||||
|
}
|
||||||
|
if mIt.Err() != retErr {
|
||||||
|
t.Errorf("Materialize iterator did not pass through underlying Err")
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue