Move iterators into separate package
Also reduce API exposure and use standard library more - and fix bugs I previously introduces in mongo.
This commit is contained in:
parent
88be6bee37
commit
1768e593a8
62 changed files with 3240 additions and 3130 deletions
118
graph/iterator/all_iterator.go
Normal file
118
graph/iterator/all_iterator.go
Normal file
|
|
@ -0,0 +1,118 @@
|
|||
// 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
|
||||
|
||||
// Defines one of the base iterators, the All iterator. Which, logically
|
||||
// enough, represents all nodes or all links in the graph.
|
||||
//
|
||||
// This particular file is actually vestigal. It's up to the TripleStore to give
|
||||
// us an All iterator that represents all things in the graph. So this is
|
||||
// really the All iterator for the MemTripleStore. That said, it *is* one of
|
||||
// the base iterators, and it helps just to see it here.
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
// An All iterator across a range of int64 values, from `max` to `min`.
|
||||
type Int64 struct {
|
||||
Base
|
||||
max, min int64
|
||||
at int64
|
||||
}
|
||||
|
||||
// Creates a new Int64 with the given range.
|
||||
func NewInt64(min, max int64) *Int64 {
|
||||
var all Int64
|
||||
BaseInit(&all.Base)
|
||||
all.max = max
|
||||
all.min = min
|
||||
all.at = min
|
||||
return &all
|
||||
}
|
||||
|
||||
// Start back at the beginning
|
||||
func (it *Int64) Reset() {
|
||||
it.at = it.min
|
||||
}
|
||||
|
||||
func (it *Int64) Close() {}
|
||||
|
||||
func (it *Int64) Clone() graph.Iterator {
|
||||
out := NewInt64(it.min, it.max)
|
||||
out.CopyTagsFrom(it)
|
||||
return out
|
||||
}
|
||||
|
||||
// Prints the All iterator as just an "all".
|
||||
func (it *Int64) DebugString(indent int) string {
|
||||
return fmt.Sprintf("%s(%s tags: %v)", strings.Repeat(" ", indent), it.Type(), it.Tags())
|
||||
}
|
||||
|
||||
// Next() on an Int64 all iterator is a simple incrementing counter.
|
||||
// Return the next integer, and mark it as the result.
|
||||
func (it *Int64) Next() (graph.TSVal, bool) {
|
||||
NextLogIn(it)
|
||||
if it.at == -1 {
|
||||
return NextLogOut(it, nil, false)
|
||||
}
|
||||
val := it.at
|
||||
it.at = it.at + 1
|
||||
if it.at > it.max {
|
||||
it.at = -1
|
||||
}
|
||||
it.Last = val
|
||||
return NextLogOut(it, val, true)
|
||||
}
|
||||
|
||||
// The number of elements in an Int64 is the size of the range.
|
||||
// The size is exact.
|
||||
func (it *Int64) Size() (int64, bool) {
|
||||
Size := ((it.max - it.min) + 1)
|
||||
return Size, true
|
||||
}
|
||||
|
||||
// Check() for an Int64 is merely seeing if the passed value is
|
||||
// withing the range, assuming the value is an int64.
|
||||
func (it *Int64) Check(tsv graph.TSVal) bool {
|
||||
CheckLogIn(it, tsv)
|
||||
v := tsv.(int64)
|
||||
if it.min <= v && v <= it.max {
|
||||
it.Last = v
|
||||
return CheckLogOut(it, v, true)
|
||||
}
|
||||
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 (it *Int64) Type() string { return "all" }
|
||||
|
||||
// There's nothing to optimize about this little iterator.
|
||||
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 {
|
||||
s, _ := it.Size()
|
||||
return &graph.IteratorStats{
|
||||
CheckCost: 1,
|
||||
NextCost: 1,
|
||||
Size: s,
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue