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:
kortschak 2014-06-30 22:22:50 +09:30
parent 88be6bee37
commit 1768e593a8
62 changed files with 3240 additions and 3130 deletions

View file

@ -16,22 +16,23 @@ package memstore
import (
"github.com/google/cayley/graph"
"github.com/google/cayley/graph/iterator"
)
type AllIterator struct {
graph.Int64AllIterator
iterator.Int64
ts *TripleStore
}
func NewMemstoreAllIterator(ts *TripleStore) *AllIterator {
var out AllIterator
out.Int64AllIterator = *graph.NewInt64AllIterator(1, ts.idCounter-1)
out.Int64 = *iterator.NewInt64(1, ts.idCounter-1)
out.ts = ts
return &out
}
func (it *AllIterator) Next() (graph.TSVal, bool) {
next, out := it.Int64AllIterator.Next()
next, out := it.Int64.Next()
if !out {
return next, out
}

View file

@ -22,10 +22,11 @@ import (
"github.com/petar/GoLLRB/llrb"
"github.com/google/cayley/graph"
"github.com/google/cayley/graph/iterator"
)
type Iterator struct {
graph.BaseIterator
iterator.Base
tree *llrb.LLRB
data string
isRunning bool
@ -53,7 +54,7 @@ func IterateOne(tree *llrb.LLRB, last Int64) Int64 {
func NewLlrbIterator(tree *llrb.LLRB, data string) *Iterator {
var it Iterator
graph.BaseIteratorInit(&it.BaseIterator)
iterator.BaseInit(&it.Base)
it.tree = tree
it.iterLast = Int64(-1)
it.data = data

View file

@ -30,16 +30,16 @@ import "github.com/google/cayley/graph"
func MakeTestingMemstore() *TripleStore {
ts := NewTripleStore()
ts.AddTriple(graph.MakeTriple("A", "follows", "B", ""))
ts.AddTriple(graph.MakeTriple("C", "follows", "B", ""))
ts.AddTriple(graph.MakeTriple("C", "follows", "D", ""))
ts.AddTriple(graph.MakeTriple("D", "follows", "B", ""))
ts.AddTriple(graph.MakeTriple("B", "follows", "F", ""))
ts.AddTriple(graph.MakeTriple("F", "follows", "G", ""))
ts.AddTriple(graph.MakeTriple("D", "follows", "G", ""))
ts.AddTriple(graph.MakeTriple("E", "follows", "F", ""))
ts.AddTriple(graph.MakeTriple("B", "status", "cool", "status_graph"))
ts.AddTriple(graph.MakeTriple("D", "status", "cool", "status_graph"))
ts.AddTriple(graph.MakeTriple("G", "status", "cool", "status_graph"))
ts.AddTriple(&graph.Triple{"A", "follows", "B", ""})
ts.AddTriple(&graph.Triple{"C", "follows", "B", ""})
ts.AddTriple(&graph.Triple{"C", "follows", "D", ""})
ts.AddTriple(&graph.Triple{"D", "follows", "B", ""})
ts.AddTriple(&graph.Triple{"B", "follows", "F", ""})
ts.AddTriple(&graph.Triple{"F", "follows", "G", ""})
ts.AddTriple(&graph.Triple{"D", "follows", "G", ""})
ts.AddTriple(&graph.Triple{"E", "follows", "F", ""})
ts.AddTriple(&graph.Triple{"B", "status", "cool", "status_graph"})
ts.AddTriple(&graph.Triple{"D", "status", "cool", "status_graph"})
ts.AddTriple(&graph.Triple{"G", "status", "cool", "status_graph"})
return ts
}

View file

@ -19,6 +19,7 @@ import (
"github.com/barakmich/glog"
"github.com/google/cayley/graph"
"github.com/google/cayley/graph/iterator"
"github.com/petar/GoLLRB/llrb"
)
@ -226,7 +227,7 @@ func (ts *TripleStore) GetTripleIterator(d graph.Direction, value graph.TSVal) g
if ok {
return NewLlrbIterator(index, data)
}
return &graph.NullIterator{}
return &iterator.Null{}
}
func (ts *TripleStore) Size() int64 {
@ -238,7 +239,7 @@ func (ts *TripleStore) DebugPrint() {
if i == 0 {
continue
}
glog.V(2).Infoln("%d: %s", i, t.ToString())
glog.V(2).Infoln("%d: %s", i, t)
}
}
@ -251,11 +252,11 @@ func (ts *TripleStore) GetNameFor(id graph.TSVal) string {
}
func (ts *TripleStore) GetTriplesAllIterator() graph.Iterator {
return graph.NewInt64AllIterator(0, ts.Size())
return iterator.NewInt64(0, ts.Size())
}
func (ts *TripleStore) MakeFixed() *graph.FixedIterator {
return graph.NewFixedIteratorWithCompare(graph.BasicEquality)
func (ts *TripleStore) FixedIterator() graph.FixedIterator {
return iterator.NewFixedIteratorWithCompare(iterator.BasicEquality)
}
func (ts *TripleStore) GetTripleDirection(val graph.TSVal, d graph.Direction) graph.TSVal {

View file

@ -16,18 +16,19 @@ package memstore
import (
"github.com/google/cayley/graph"
"github.com/google/cayley/graph/iterator"
)
func (ts *TripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
switch it.Type() {
case "linksto":
return ts.optimizeLinksTo(it.(*graph.LinksToIterator))
return ts.optimizeLinksTo(it.(*iterator.LinksTo))
}
return it, false
}
func (ts *TripleStore) optimizeLinksTo(it *graph.LinksToIterator) (graph.Iterator, bool) {
func (ts *TripleStore) optimizeLinksTo(it *iterator.LinksTo) (graph.Iterator, bool) {
subs := it.GetSubIterators()
if len(subs) != 1 {
return it, false

View file

@ -21,6 +21,7 @@ import (
. "github.com/smartystreets/goconvey/convey"
"github.com/google/cayley/graph"
"github.com/google/cayley/graph/iterator"
)
func TestMemstore(t *testing.T) {
@ -38,19 +39,19 @@ func TestMemstore(t *testing.T) {
func TestIteratorsAndNextResultOrderA(t *testing.T) {
ts := MakeTestingMemstore()
fixed := ts.MakeFixed()
fixed := ts.FixedIterator()
fixed.AddValue(ts.GetIdFor("C"))
all := ts.GetNodesAllIterator()
lto := graph.NewLinksToIterator(ts, all, graph.Object)
innerAnd := graph.NewAndIterator()
lto := iterator.NewLinksTo(ts, all, graph.Object)
innerAnd := iterator.NewAnd()
fixed2 := ts.MakeFixed()
fixed2 := ts.FixedIterator()
fixed2.AddValue(ts.GetIdFor("follows"))
lto2 := graph.NewLinksToIterator(ts, fixed2, graph.Predicate)
lto2 := iterator.NewLinksTo(ts, fixed2, graph.Predicate)
innerAnd.AddSubIterator(lto2)
innerAnd.AddSubIterator(lto)
hasa := graph.NewHasaIterator(ts, innerAnd, graph.Subject)
outerAnd := graph.NewAndIterator()
hasa := iterator.NewHasA(ts, innerAnd, graph.Subject)
outerAnd := iterator.NewAnd()
outerAnd.AddSubIterator(fixed)
outerAnd.AddSubIterator(hasa)
val, ok := outerAnd.Next()
@ -96,9 +97,9 @@ func CompareStringSlices(t *testing.T, expected []string, actual []string) {
func TestLinksToOptimization(t *testing.T) {
ts := MakeTestingMemstore()
fixed := ts.MakeFixed()
fixed := ts.FixedIterator()
fixed.AddValue(ts.GetIdFor("cool"))
lto := graph.NewLinksToIterator(ts, fixed, graph.Object)
lto := iterator.NewLinksTo(ts, fixed, graph.Object)
lto.AddTag("foo")
newIt, changed := lto.Optimize()
if !changed {
@ -119,17 +120,17 @@ func TestLinksToOptimization(t *testing.T) {
func TestRemoveTriple(t *testing.T) {
ts := MakeTestingMemstore()
ts.RemoveTriple(graph.MakeTriple("E", "follows", "F", ""))
fixed := ts.MakeFixed()
ts.RemoveTriple(&graph.Triple{"E", "follows", "F", ""})
fixed := ts.FixedIterator()
fixed.AddValue(ts.GetIdFor("E"))
lto := graph.NewLinksToIterator(ts, fixed, graph.Subject)
fixed2 := ts.MakeFixed()
lto := iterator.NewLinksTo(ts, fixed, graph.Subject)
fixed2 := ts.FixedIterator()
fixed2.AddValue(ts.GetIdFor("follows"))
lto2 := graph.NewLinksToIterator(ts, fixed2, graph.Predicate)
innerAnd := graph.NewAndIterator()
lto2 := iterator.NewLinksTo(ts, fixed2, graph.Predicate)
innerAnd := iterator.NewAnd()
innerAnd.AddSubIterator(lto2)
innerAnd.AddSubIterator(lto)
hasa := graph.NewHasaIterator(ts, innerAnd, graph.Object)
hasa := iterator.NewHasA(ts, innerAnd, graph.Object)
newIt, _ := hasa.Optimize()
_, ok := newIt.Next()
if ok {