Make Close() method on Iterators return an error

This commit is contained in:
Andrew Dunham 2015-04-14 20:17:31 -07:00
parent 5c9979ec8b
commit 1b6395ed0a
19 changed files with 89 additions and 33 deletions

View file

@ -174,10 +174,11 @@ func (it *AllIterator) Contains(v graph.Value) bool {
return true
}
func (it *AllIterator) Close() {
func (it *AllIterator) Close() error {
it.result = nil
it.buffer = nil
it.done = true
return nil
}
func (it *AllIterator) Size() (int64, bool) {

View file

@ -106,10 +106,11 @@ func (it *Iterator) Clone() graph.Iterator {
return out
}
func (it *Iterator) Close() {
func (it *Iterator) Close() error {
it.result = nil
it.buffer = nil
it.done = true
return nil
}
func (it *Iterator) isLiveValue(val []byte) bool {

View file

@ -130,12 +130,13 @@ func (it *Iterator) Reset() {
it.result = nil
}
func (it *Iterator) Close() {
func (it *Iterator) Close() error {
it.buffer = nil
it.offset = 0
it.done = true
it.last = ""
it.result = nil
return nil
}
func (it *Iterator) Tagger() *graph.Tagger {

View file

@ -136,7 +136,7 @@ type Iterator interface {
Describe() Description
// Close the iterator and do internal cleanup.
Close()
Close() error
// UID returns the unique identifier of the iterator.
UID() uint64

View file

@ -55,7 +55,9 @@ func (it *Int64) Reset() {
it.at = it.min
}
func (it *Int64) Close() {}
func (it *Int64) Close() error {
return nil
}
func (it *Int64) Clone() graph.Iterator {
out := NewInt64(it.min, it.max)

View file

@ -263,12 +263,22 @@ func (it *And) 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 (it *And) Close() {
//
// Note: as this potentially involves closing multiple subiterators, only
// the first error encountered while closing will be reported (if any).
func (it *And) Close() error {
it.cleanUp()
it.primaryIt.Close()
var ret error
ret = it.primaryIt.Close()
for _, sub := range it.internalIterators {
sub.Close()
if err := sub.Close(); err != nil && ret != nil {
ret = err
}
}
return ret
}
// Register this as an "and" iterator.

View file

@ -63,7 +63,9 @@ func (it *Fixed) Reset() {
it.lastIndex = 0
}
func (it *Fixed) Close() {}
func (it *Fixed) Close() error {
return nil
}
func (it *Fixed) Tagger() *graph.Tagger {
return &it.tags

View file

@ -247,11 +247,17 @@ func (it *HasA) Stats() graph.IteratorStats {
}
// Close the subiterator, the result iterator (if any) and the HasA.
func (it *HasA) Close() {
func (it *HasA) Close() error {
var ret error
if it.resultIt != nil {
it.resultIt.Close()
ret = it.resultIt.Close()
}
it.primaryIt.Close()
if err := it.primaryIt.Close(); err != nil && ret != nil {
ret = err
}
return ret
}
// Register this iterator as a HasA.

View file

@ -114,7 +114,9 @@ func (it *Null) Size() (int64, bool) {
func (it *Null) Reset() {}
func (it *Null) Close() {}
func (it *Null) Close() error {
return nil
}
// A null iterator costs nothing. Use it!
func (it *Null) Stats() graph.IteratorStats {

View file

@ -181,9 +181,17 @@ func (it *LinksTo) Result() graph.Value {
}
// Close our subiterators.
func (it *LinksTo) Close() {
it.nextIt.Close()
it.primaryIt.Close()
func (it *LinksTo) Close() error {
var ret error
if err := it.nextIt.Close(); err != nil && ret != nil {
ret = err
}
if err := it.primaryIt.Close(); err != nil && ret != nil {
ret = err
}
return ret
}
// We won't ever have a new result, but our subiterators might.

View file

@ -69,11 +69,11 @@ func (it *Materialize) Reset() {
it.index = -1
}
func (it *Materialize) Close() {
it.subIt.Close()
func (it *Materialize) Close() error {
it.containsMap = nil
it.values = nil
it.hasRun = false
return it.subIt.Close()
}
func (it *Materialize) Tagger() *graph.Tagger {

View file

@ -123,9 +123,19 @@ func (it *Not) NextPath() bool {
return false
}
func (it *Not) Close() {
it.primaryIt.Close()
it.allIt.Close()
// Close closes the primary and all iterators. If an error occurs, only the
// first one will be returned.
func (it *Not) Close() error {
var ret error
if err := it.primaryIt.Close(); err != nil && ret != nil {
ret = err
}
if err := it.allIt.Close(); err != nil && ret != nil {
ret = err
}
return ret
}
func (it *Not) Type() graph.Type { return graph.Not }

View file

@ -57,8 +57,8 @@ func (it *Optional) Reset() {
it.lastCheck = false
}
func (it *Optional) Close() {
it.subIt.Close()
func (it *Optional) Close() error {
return it.subIt.Close()
}
func (it *Optional) Tagger() *graph.Tagger {

View file

@ -241,12 +241,21 @@ func (it *Or) cleanUp() {}
// Close this graph.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 (it *Or) Close() {
// follow this contract, the Or follows the contract.
//
// Note: as this potentially involves closing multiple subiterators, only
// the first error encountered while closing will be reported (if any).
func (it *Or) Close() error {
it.cleanUp()
var ret error
for _, sub := range it.internalIterators {
sub.Close()
if err := sub.Close(); err != nil && ret != nil {
ret = err
}
}
return ret
}
func (it *Or) Optimize() (graph.Iterator, bool) {

View file

@ -92,8 +92,8 @@ func (it *Comparison) doComparison(val graph.Value) bool {
}
}
func (it *Comparison) Close() {
it.subIt.Close()
func (it *Comparison) Close() error {
return it.subIt.Close()
}
func RunIntOp(a int64, op Operator, b int64) bool {

View file

@ -145,11 +145,12 @@ func (it *AllIterator) Contains(v graph.Value) bool {
return true
}
func (it *AllIterator) Close() {
func (it *AllIterator) Close() error {
if it.open {
it.iter.Release()
it.open = false
}
return nil
}
func (it *AllIterator) Size() (int64, bool) {

View file

@ -109,11 +109,12 @@ func (it *Iterator) Clone() graph.Iterator {
return out
}
func (it *Iterator) Close() {
func (it *Iterator) Close() error {
if it.open {
it.iter.Release()
it.open = false
}
return nil
}
func (it *Iterator) isLiveValue(val []byte) bool {

View file

@ -105,7 +105,9 @@ func (it *Iterator) Clone() graph.Iterator {
return m
}
func (it *Iterator) Close() {}
func (it *Iterator) Close() error {
return nil
}
func (it *Iterator) checkValid(index int64) bool {
return it.qs.log[index].DeletedBy == 0

View file

@ -99,8 +99,8 @@ func (it *Iterator) Reset() {
}
func (it *Iterator) Close() {
it.iter.Close()
func (it *Iterator) Close() error {
return it.iter.Close()
}
func (it *Iterator) Tagger() *graph.Tagger {