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

@ -18,6 +18,7 @@ import (
"github.com/badgerodon/peg"
"github.com/google/cayley/graph"
"github.com/google/cayley/graph/iterator"
)
func BuildIteratorTreeForQuery(ts graph.TripleStore, query string) graph.Iterator {
@ -195,7 +196,7 @@ func buildIteratorTree(tree *peg.ExpressionTree, ts graph.TripleStore) graph.Ite
if tree.Children[0].Children[0].Name == "ColonIdentifier" {
n = nodeID[1:]
}
fixed := ts.MakeFixed()
fixed := ts.FixedIterator()
fixed.AddValue(ts.GetIdFor(n))
out = fixed
}
@ -207,11 +208,11 @@ func buildIteratorTree(tree *peg.ExpressionTree, ts graph.TripleStore) graph.Ite
i++
}
it := buildIteratorTree(tree.Children[i], ts)
lto := graph.NewLinksToIterator(ts, it, graph.Predicate)
lto := iterator.NewLinksTo(ts, it, graph.Predicate)
return lto
case "RootConstraint":
constraintCount := 0
and := graph.NewAndIterator()
and := iterator.NewAnd()
for _, c := range tree.Children {
switch c.Name {
case "NodeIdentifier":
@ -227,10 +228,10 @@ func buildIteratorTree(tree *peg.ExpressionTree, ts graph.TripleStore) graph.Ite
}
return and
case "Constraint":
var hasa *graph.HasaIterator
var hasa *iterator.HasA
topLevelDir := graph.Subject
subItDir := graph.Object
subAnd := graph.NewAndIterator()
subAnd := iterator.NewAnd()
isOptional := false
for _, c := range tree.Children {
switch c.Name {
@ -251,21 +252,21 @@ func buildIteratorTree(tree *peg.ExpressionTree, ts graph.TripleStore) graph.Ite
fallthrough
case "RootConstraint":
it := buildIteratorTree(c, ts)
l := graph.NewLinksToIterator(ts, it, subItDir)
l := iterator.NewLinksTo(ts, it, subItDir)
subAnd.AddSubIterator(l)
continue
default:
continue
}
}
hasa = graph.NewHasaIterator(ts, subAnd, topLevelDir)
hasa = iterator.NewHasA(ts, subAnd, topLevelDir)
if isOptional {
optional := graph.NewOptionalIterator(hasa)
optional := iterator.NewOptional(hasa)
return optional
}
return hasa
default:
return &graph.NullIterator{}
return &iterator.Null{}
}
panic("Not reached")
}

View file

@ -40,7 +40,7 @@ func TestParseSexpWithMemstore(t *testing.T) {
})
Convey("It should get a single triple linkage", func() {
ts.AddTriple(graph.MakeTriple("i", "can", "win", ""))
ts.AddTriple(&graph.Triple{"i", "can", "win", ""})
query := "($a (:can \"win\"))"
So(len(query), ShouldEqual, 17)
it := BuildIteratorTreeForQuery(ts, query)
@ -51,7 +51,7 @@ func TestParseSexpWithMemstore(t *testing.T) {
})
Convey("It can get an internal linkage", func() {
ts.AddTriple(graph.MakeTriple("i", "can", "win", ""))
ts.AddTriple(&graph.Triple{"i", "can", "win", ""})
query := "(\"i\" (:can $a))"
it := BuildIteratorTreeForQuery(ts, query)
So(it.Type(), ShouldEqual, "and")
@ -65,8 +65,8 @@ func TestParseSexpWithMemstore(t *testing.T) {
func TestTreeConstraintParse(t *testing.T) {
ts := memstore.NewTripleStore()
ts.AddTriple(graph.MakeTriple("i", "like", "food", ""))
ts.AddTriple(graph.MakeTriple("food", "is", "good", ""))
ts.AddTriple(&graph.Triple{"i", "like", "food", ""})
ts.AddTriple(&graph.Triple{"food", "is", "good", ""})
query := "(\"i\"\n" +
"(:like\n" +
"($a (:is :good))))"
@ -85,8 +85,8 @@ func TestTreeConstraintParse(t *testing.T) {
func TestTreeConstraintTagParse(t *testing.T) {
ts := memstore.NewTripleStore()
ts.AddTriple(graph.MakeTriple("i", "like", "food", ""))
ts.AddTriple(graph.MakeTriple("food", "is", "good", ""))
ts.AddTriple(&graph.Triple{"i", "like", "food", ""})
ts.AddTriple(&graph.Triple{"food", "is", "good", ""})
query := "(\"i\"\n" +
"(:like\n" +
"($a (:is :good))))"
@ -105,9 +105,9 @@ func TestTreeConstraintTagParse(t *testing.T) {
func TestMultipleConstraintParse(t *testing.T) {
ts := memstore.NewTripleStore()
ts.AddTriple(graph.MakeTriple("i", "like", "food", ""))
ts.AddTriple(graph.MakeTriple("i", "like", "beer", ""))
ts.AddTriple(graph.MakeTriple("you", "like", "beer", ""))
ts.AddTriple(&graph.Triple{"i", "like", "food", ""})
ts.AddTriple(&graph.Triple{"i", "like", "beer", ""})
ts.AddTriple(&graph.Triple{"you", "like", "beer", ""})
query := "($a \n" +
"(:like :beer)\n" +
"(:like \"food\"))"