Add new builder iterators, v2
Subcommits: link iterator next/contains implement sql_node_iterator next/buildsql fix optimizers
This commit is contained in:
parent
621acae945
commit
7153a766c1
7 changed files with 1173 additions and 169 deletions
|
|
@ -23,141 +23,86 @@ import (
|
||||||
"github.com/google/cayley/quad"
|
"github.com/google/cayley/quad"
|
||||||
)
|
)
|
||||||
|
|
||||||
func intersect(a *StatementIterator, b *StatementIterator) (*StatementIterator, error) {
|
func intersect(a graph.Iterator, b graph.Iterator) (graph.Iterator, error) {
|
||||||
if a.stType != b.stType {
|
if anew, ok := a.(*SQLNodeIterator); ok {
|
||||||
return nil, errors.New("Cannot combine SQL iterators of two different types")
|
if bnew, ok := b.(*SQLNodeIterator); ok {
|
||||||
}
|
return intersectNode(anew, bnew)
|
||||||
min := a.size
|
|
||||||
if b.size < a.size {
|
|
||||||
min = b.size
|
|
||||||
}
|
|
||||||
var where clause
|
|
||||||
if a.where == nil {
|
|
||||||
if b.where == nil {
|
|
||||||
where = nil
|
|
||||||
}
|
}
|
||||||
where = b.where
|
} else if anew, ok := a.(*SQLLinkIterator); ok {
|
||||||
|
if bnew, ok := b.(*SQLLinkIterator); ok {
|
||||||
|
return intersectLink(anew, bnew)
|
||||||
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
if b.where == nil {
|
return nil, errors.New("Unknown iterator types")
|
||||||
where = a.where
|
|
||||||
}
|
|
||||||
where = joinClause{a.where, b.where, andClause}
|
|
||||||
}
|
}
|
||||||
out := &StatementIterator{
|
return nil, errors.New("Cannot combine SQL iterators of two different types")
|
||||||
uid: iterator.NextUID(),
|
|
||||||
qs: a.qs,
|
|
||||||
buildWhere: append(a.buildWhere, b.buildWhere...),
|
|
||||||
tags: append(a.tags, b.tags...),
|
|
||||||
where: where,
|
|
||||||
stType: a.stType,
|
|
||||||
size: min,
|
|
||||||
dir: a.dir,
|
|
||||||
}
|
|
||||||
out.tagger.CopyFrom(a)
|
|
||||||
out.tagger.CopyFrom(b)
|
|
||||||
if out.stType == node {
|
|
||||||
out.buildWhere = append(out.buildWhere, baseClause{
|
|
||||||
pair: tableDir{"", a.dir},
|
|
||||||
target: tableDir{b.tableName(), b.dir},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
return out, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func hasa(a *StatementIterator, d quad.Direction) (*StatementIterator, error) {
|
func intersectNode(a *SQLNodeIterator, b *SQLNodeIterator) (graph.Iterator, error) {
|
||||||
if a.stType != link {
|
m := &SQLNodeIterator{
|
||||||
|
uid: iterator.NextUID(),
|
||||||
|
qs: a.qs,
|
||||||
|
tableName: newTableName(),
|
||||||
|
linkIts: append(a.linkIts, b.linkIts...),
|
||||||
|
tagdirs: append(a.tagdirs, b.tagdirs...),
|
||||||
|
}
|
||||||
|
m.Tagger().CopyFrom(a)
|
||||||
|
m.Tagger().CopyFrom(b)
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func intersectLink(a *SQLLinkIterator, b *SQLLinkIterator) (graph.Iterator, error) {
|
||||||
|
m := &SQLLinkIterator{
|
||||||
|
uid: iterator.NextUID(),
|
||||||
|
qs: a.qs,
|
||||||
|
tableName: newTableName(),
|
||||||
|
nodeIts: append(a.nodeIts, b.nodeIts...),
|
||||||
|
constraints: append(a.constraints, b.constraints...),
|
||||||
|
}
|
||||||
|
m.Tagger().CopyFrom(a)
|
||||||
|
m.Tagger().CopyFrom(b)
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func hasa(aIn graph.Iterator, d quad.Direction) (graph.Iterator, error) {
|
||||||
|
a, ok := aIn.(*SQLLinkIterator)
|
||||||
|
if !ok {
|
||||||
return nil, errors.New("Can't take the HASA of a link SQL iterator")
|
return nil, errors.New("Can't take the HASA of a link SQL iterator")
|
||||||
}
|
}
|
||||||
|
|
||||||
out := &StatementIterator{
|
out := &SQLNodeIterator{
|
||||||
uid: iterator.NextUID(),
|
uid: iterator.NextUID(),
|
||||||
qs: a.qs,
|
qs: a.qs,
|
||||||
stType: node,
|
tableName: newTableName(),
|
||||||
dir: d,
|
linkIts: []sqlItDir{
|
||||||
}
|
sqlItDir{
|
||||||
where := a.where
|
it: a,
|
||||||
for _, w := range a.buildWhere {
|
|
||||||
w.pair.table = out.tableName()
|
|
||||||
wherenew := joinClause{where, w, andClause}
|
|
||||||
where = wherenew
|
|
||||||
}
|
|
||||||
out.where = where
|
|
||||||
//out := &StatementIterator{
|
|
||||||
//uid: iterator.NextUID(),
|
|
||||||
//qs: a.qs,
|
|
||||||
//stType: node,
|
|
||||||
//dir: d,
|
|
||||||
//buildWhere: a.buildWhere,
|
|
||||||
//where: a.where,
|
|
||||||
//size: -1,
|
|
||||||
//}
|
|
||||||
for k, v := range a.tagger.Fixed() {
|
|
||||||
out.tagger.AddFixed(k, v)
|
|
||||||
}
|
|
||||||
var tags []tag
|
|
||||||
for _, t := range a.tagger.Tags() {
|
|
||||||
tags = append(tags, tag{
|
|
||||||
pair: tableDir{
|
|
||||||
table: out.tableName(),
|
|
||||||
dir: quad.Any,
|
|
||||||
},
|
|
||||||
t: t,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
out.tags = append(tags, a.tags...)
|
|
||||||
return out, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func linksto(a *StatementIterator, d quad.Direction) (*StatementIterator, error) {
|
|
||||||
if a.stType != node {
|
|
||||||
return nil, errors.New("Can't take the LINKSTO of a node SQL iterator")
|
|
||||||
}
|
|
||||||
out := &StatementIterator{
|
|
||||||
uid: iterator.NextUID(),
|
|
||||||
qs: a.qs,
|
|
||||||
stType: link,
|
|
||||||
dir: d,
|
|
||||||
size: -1,
|
|
||||||
}
|
|
||||||
where := a.where
|
|
||||||
for _, w := range a.buildWhere {
|
|
||||||
w.pair.table = a.tableName()
|
|
||||||
wherenew := joinClause{where, w, andClause}
|
|
||||||
where = wherenew
|
|
||||||
}
|
|
||||||
|
|
||||||
out.where = where
|
|
||||||
out.buildWhere = []baseClause{
|
|
||||||
baseClause{
|
|
||||||
pair: tableDir{
|
|
||||||
dir: d,
|
dir: d,
|
||||||
},
|
},
|
||||||
target: tableDir{
|
|
||||||
table: a.tableName(),
|
|
||||||
dir: a.dir,
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
var tags []tag
|
return out, nil
|
||||||
for _, t := range a.tagger.Tags() {
|
}
|
||||||
tags = append(tags, tag{
|
|
||||||
pair: tableDir{
|
func linksto(aIn graph.Iterator, d quad.Direction) (graph.Iterator, error) {
|
||||||
table: a.tableName(),
|
a, ok := aIn.(*SQLNodeIterator)
|
||||||
dir: a.dir,
|
if !ok {
|
||||||
|
return nil, errors.New("Can't take the LINKSTO of a node SQL iterator")
|
||||||
|
}
|
||||||
|
|
||||||
|
out := &SQLLinkIterator{
|
||||||
|
uid: iterator.NextUID(),
|
||||||
|
qs: a.qs,
|
||||||
|
tableName: newTableName(),
|
||||||
|
nodeIts: []sqlItDir{
|
||||||
|
sqlItDir{
|
||||||
|
it: a,
|
||||||
|
dir: d,
|
||||||
},
|
},
|
||||||
t: t,
|
},
|
||||||
})
|
|
||||||
}
|
}
|
||||||
for k, v := range a.tagger.Fixed() {
|
|
||||||
out.tagger.AddFixed(k, v)
|
|
||||||
}
|
|
||||||
for _, t := range a.tags {
|
|
||||||
if t.pair.table == "" {
|
|
||||||
t.pair.table = a.tableName()
|
|
||||||
}
|
|
||||||
tags = append(tags, t)
|
|
||||||
}
|
|
||||||
out.tags = tags
|
|
||||||
return out, nil
|
return out, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -196,33 +141,34 @@ func (qs *QuadStore) optimizeLinksTo(it *iterator.LinksTo) (graph.Iterator, bool
|
||||||
it.Close()
|
it.Close()
|
||||||
return newIt, true
|
return newIt, true
|
||||||
}
|
}
|
||||||
case sqlBuilderType:
|
case sqlNodeType:
|
||||||
newit, err := linksto(primary.(*StatementIterator), it.Direction())
|
//p := primary.(*SQLNodeIterator)
|
||||||
|
newit, err := linksto(primary, it.Direction())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorln(err)
|
glog.Errorln(err)
|
||||||
return it, false
|
return it, false
|
||||||
}
|
}
|
||||||
newit.Tagger().CopyFrom(it)
|
newit.Tagger().CopyFrom(it)
|
||||||
return newit, true
|
return newit, true
|
||||||
case graph.All:
|
//case graph.All:
|
||||||
newit := &StatementIterator{
|
//newit := &StatementIterator{
|
||||||
uid: iterator.NextUID(),
|
//uid: iterator.NextUID(),
|
||||||
qs: qs,
|
//qs: qs,
|
||||||
stType: link,
|
//stType: link,
|
||||||
size: qs.Size(),
|
//size: qs.Size(),
|
||||||
}
|
//}
|
||||||
for _, t := range primary.Tagger().Tags() {
|
//for _, t := range primary.Tagger().Tags() {
|
||||||
newit.tags = append(newit.tags, tag{
|
//newit.tags = append(newit.tags, tag{
|
||||||
pair: tableDir{"", it.Direction()},
|
//pair: tableDir{"", it.Direction()},
|
||||||
t: t,
|
//t: t,
|
||||||
})
|
//})
|
||||||
}
|
//}
|
||||||
for k, v := range primary.Tagger().Fixed() {
|
//for k, v := range primary.Tagger().Fixed() {
|
||||||
newit.tagger.AddFixed(k, v)
|
//newit.tagger.AddFixed(k, v)
|
||||||
}
|
//}
|
||||||
newit.tagger.CopyFrom(it)
|
//newit.tagger.CopyFrom(it)
|
||||||
|
|
||||||
return newit, true
|
//return newit, true
|
||||||
}
|
}
|
||||||
return it, false
|
return it, false
|
||||||
}
|
}
|
||||||
|
|
@ -230,18 +176,18 @@ func (qs *QuadStore) optimizeLinksTo(it *iterator.LinksTo) (graph.Iterator, bool
|
||||||
func (qs *QuadStore) optimizeAnd(it *iterator.And) (graph.Iterator, bool) {
|
func (qs *QuadStore) optimizeAnd(it *iterator.And) (graph.Iterator, bool) {
|
||||||
subs := it.SubIterators()
|
subs := it.SubIterators()
|
||||||
var unusedIts []graph.Iterator
|
var unusedIts []graph.Iterator
|
||||||
var newit *StatementIterator
|
var newit graph.Iterator
|
||||||
newit = nil
|
newit = nil
|
||||||
changed := false
|
changed := false
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
for _, it := range subs {
|
for _, it := range subs {
|
||||||
if it.Type() == sqlBuilderType {
|
if it.Type() == sqlLinkType || it.Type() == sqlNodeType {
|
||||||
if newit == nil {
|
if newit == nil {
|
||||||
newit = it.(*StatementIterator)
|
newit = it
|
||||||
} else {
|
} else {
|
||||||
changed = true
|
changed = true
|
||||||
newit, err = intersect(newit, it.(*StatementIterator))
|
newit, err = intersect(newit, it)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Error(err)
|
glog.Error(err)
|
||||||
return it, false
|
return it, false
|
||||||
|
|
@ -256,7 +202,7 @@ func (qs *QuadStore) optimizeAnd(it *iterator.And) (graph.Iterator, bool) {
|
||||||
return it, false
|
return it, false
|
||||||
}
|
}
|
||||||
if len(unusedIts) == 0 {
|
if len(unusedIts) == 0 {
|
||||||
newit.tagger.CopyFrom(it)
|
newit.Tagger().CopyFrom(it)
|
||||||
return newit, true
|
return newit, true
|
||||||
}
|
}
|
||||||
newAnd := iterator.NewAnd(qs)
|
newAnd := iterator.NewAnd(qs)
|
||||||
|
|
@ -274,8 +220,8 @@ func (qs *QuadStore) optimizeHasA(it *iterator.HasA) (graph.Iterator, bool) {
|
||||||
return it, false
|
return it, false
|
||||||
}
|
}
|
||||||
primary := subs[0]
|
primary := subs[0]
|
||||||
if primary.Type() == sqlBuilderType {
|
if primary.Type() == sqlLinkType {
|
||||||
newit, err := hasa(primary.(*StatementIterator), it.Direction())
|
newit, err := hasa(primary, it.Direction())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
glog.Errorln(err)
|
glog.Errorln(err)
|
||||||
return it, false
|
return it, false
|
||||||
|
|
|
||||||
|
|
@ -23,41 +23,43 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestBuildIntersect(t *testing.T) {
|
func TestBuildIntersect(t *testing.T) {
|
||||||
a := NewStatementIterator(nil, quad.Subject, "Foo")
|
a := NewSQLLinkIterator(nil, quad.Subject, "Foo")
|
||||||
b := NewStatementIterator(nil, quad.Predicate, "is_equivalent_to")
|
b := NewSQLLinkIterator(nil, quad.Predicate, "is_equivalent_to")
|
||||||
it, err := intersect(a, b)
|
it, err := intersect(a, b)
|
||||||
|
i := it.(*SQLLinkIterator)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
s, v := it.buildQuery(false, nil)
|
s, v := i.buildSQL(true, nil)
|
||||||
fmt.Println(s, v)
|
fmt.Println(s, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildHasa(t *testing.T) {
|
func TestBuildHasa(t *testing.T) {
|
||||||
a := NewStatementIterator(nil, quad.Subject, "Foo")
|
a := NewSQLLinkIterator(nil, quad.Subject, "Foo")
|
||||||
a.tagger.Add("foo")
|
a.tagger.Add("foo")
|
||||||
b := NewStatementIterator(nil, quad.Predicate, "is_equivalent_to")
|
b := NewSQLLinkIterator(nil, quad.Predicate, "is_equivalent_to")
|
||||||
it1, err := intersect(a, b)
|
it1, err := intersect(a, b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
it2, err := hasa(it1, quad.Object)
|
it2, err := hasa(it1, quad.Object)
|
||||||
|
i2 := it2.(*SQLNodeIterator)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
s, v := it2.buildQuery(false, nil)
|
s, v := i2.buildSQL(true, nil)
|
||||||
fmt.Println(s, v)
|
fmt.Println(s, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestBuildLinksTo(t *testing.T) {
|
func TestBuildLinksTo(t *testing.T) {
|
||||||
a := NewStatementIterator(nil, quad.Subject, "Foo")
|
a := NewSQLLinkIterator(nil, quad.Subject, "Foo")
|
||||||
b := NewStatementIterator(nil, quad.Predicate, "is_equivalent_to")
|
b := NewSQLLinkIterator(nil, quad.Predicate, "is_equivalent_to")
|
||||||
it1, err := intersect(a, b)
|
it1, err := intersect(a, b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
it2, err := hasa(it1, quad.Object)
|
it2, err := hasa(it1, quad.Object)
|
||||||
it2.tagger.Add("foo")
|
it2.Tagger().Add("foo")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
|
|
@ -65,7 +67,8 @@ func TestBuildLinksTo(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
s, v := it3.buildQuery(false, nil)
|
i3 := it3.(*SQLLinkIterator)
|
||||||
|
s, v := i3.buildSQL(true, nil)
|
||||||
fmt.Println(s, v)
|
fmt.Println(s, v)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -77,8 +80,8 @@ func TestInterestingQuery(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
a := NewStatementIterator(db.(*QuadStore), quad.Object, "Humphrey Bogart")
|
a := NewSQLLinkIterator(db.(*QuadStore), quad.Object, "Humphrey Bogart")
|
||||||
b := NewStatementIterator(db.(*QuadStore), quad.Predicate, "name")
|
b := NewSQLLinkIterator(db.(*QuadStore), quad.Predicate, "name")
|
||||||
it1, err := intersect(a, b)
|
it1, err := intersect(a, b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
|
|
@ -92,7 +95,7 @@ func TestInterestingQuery(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
b = NewStatementIterator(db.(*QuadStore), quad.Predicate, "/film/performance/actor")
|
b = NewSQLLinkIterator(db.(*QuadStore), quad.Predicate, "/film/performance/actor")
|
||||||
it4, err := intersect(it3, b)
|
it4, err := intersect(it3, b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
|
|
@ -105,7 +108,7 @@ func TestInterestingQuery(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
b = NewStatementIterator(db.(*QuadStore), quad.Predicate, "/film/film/starring")
|
b = NewSQLLinkIterator(db.(*QuadStore), quad.Predicate, "/film/film/starring")
|
||||||
it7, err := intersect(it6, b)
|
it7, err := intersect(it6, b)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
|
|
@ -114,13 +117,14 @@ func TestInterestingQuery(t *testing.T) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Error(err)
|
t.Error(err)
|
||||||
}
|
}
|
||||||
s, v := it8.buildQuery(false, nil)
|
finalIt := it8.(*SQLNodeIterator)
|
||||||
it8.Tagger().Add("id")
|
s, v := finalIt.buildSQL(true, nil)
|
||||||
|
finalIt.Tagger().Add("id")
|
||||||
fmt.Println(s, v)
|
fmt.Println(s, v)
|
||||||
for graph.Next(it8) {
|
for graph.Next(finalIt) {
|
||||||
fmt.Println(it8.Result())
|
fmt.Println(finalIt.Result())
|
||||||
out := make(map[string]graph.Value)
|
out := make(map[string]graph.Value)
|
||||||
it8.TagResults(out)
|
finalIt.TagResults(out)
|
||||||
for k, v := range out {
|
for k, v := range out {
|
||||||
fmt.Printf("%s: %v\n", k, v.(string))
|
fmt.Printf("%s: %v\n", k, v.(string))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -200,7 +200,7 @@ func (qs *QuadStore) Quad(val graph.Value) quad.Quad {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qs *QuadStore) QuadIterator(d quad.Direction, val graph.Value) graph.Iterator {
|
func (qs *QuadStore) QuadIterator(d quad.Direction, val graph.Value) graph.Iterator {
|
||||||
return NewStatementIterator(qs, d, val.(string))
|
return NewSQLLinkIterator(qs, d, val.(string))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qs *QuadStore) NodesAllIterator() graph.Iterator {
|
func (qs *QuadStore) NodesAllIterator() graph.Iterator {
|
||||||
|
|
|
||||||
525
graph/sql/sql_link_iterator.go
Normal file
525
graph/sql/sql_link_iterator.go
Normal file
File diff suppressed because it is too large
Load diff
87
graph/sql/sql_link_iterator_test.go
Normal file
87
graph/sql/sql_link_iterator_test.go
Normal file
|
|
@ -0,0 +1,87 @@
|
||||||
|
// Copyright 2015 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 sql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/cayley/graph"
|
||||||
|
"github.com/google/cayley/graph/iterator"
|
||||||
|
"github.com/google/cayley/quad"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSQLLink(t *testing.T) {
|
||||||
|
it := NewSQLLinkIterator(nil, quad.Object, "cool")
|
||||||
|
s, v := it.buildSQL(true, nil)
|
||||||
|
fmt.Println(s, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSQLLinkIteration(t *testing.T) {
|
||||||
|
if *dbpath == "" {
|
||||||
|
t.SkipNow()
|
||||||
|
}
|
||||||
|
db, err := newQuadStore(*dbpath, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
it := NewSQLLinkIterator(db.(*QuadStore), quad.Object, "Humphrey Bogart")
|
||||||
|
for graph.Next(it) {
|
||||||
|
fmt.Println(it.Result())
|
||||||
|
}
|
||||||
|
it = NewSQLLinkIterator(db.(*QuadStore), quad.Subject, "/en/casablanca_1942")
|
||||||
|
s, v := it.buildSQL(true, nil)
|
||||||
|
fmt.Println(s, v)
|
||||||
|
c := 0
|
||||||
|
for graph.Next(it) {
|
||||||
|
fmt.Println(it.Result())
|
||||||
|
c += 1
|
||||||
|
}
|
||||||
|
if c != 18 {
|
||||||
|
t.Errorf("Not enough results, got %d expected 18", c)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSQLNodeIteration(t *testing.T) {
|
||||||
|
if *dbpath == "" {
|
||||||
|
t.SkipNow()
|
||||||
|
}
|
||||||
|
db, err := newQuadStore(*dbpath, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
link := NewSQLLinkIterator(db.(*QuadStore), quad.Object, "/en/humphrey_bogart")
|
||||||
|
it := &SQLNodeIterator{
|
||||||
|
uid: iterator.NextUID(),
|
||||||
|
qs: db.(*QuadStore),
|
||||||
|
tableName: newTableName(),
|
||||||
|
linkIts: []sqlItDir{
|
||||||
|
sqlItDir{it: link,
|
||||||
|
dir: quad.Subject,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
s, v := it.buildSQL(true, nil)
|
||||||
|
fmt.Println(s, v)
|
||||||
|
c := 0
|
||||||
|
for graph.Next(it) {
|
||||||
|
fmt.Println(it.Result())
|
||||||
|
c += 1
|
||||||
|
}
|
||||||
|
if c != 56 {
|
||||||
|
t.Errorf("Not enough results, got %d expected 56", c)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
435
graph/sql/sql_node_iterator.go
Normal file
435
graph/sql/sql_node_iterator.go
Normal file
|
|
@ -0,0 +1,435 @@
|
||||||
|
// Copyright 2015 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 sql
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/barakmich/glog"
|
||||||
|
"github.com/google/cayley/graph"
|
||||||
|
"github.com/google/cayley/graph/iterator"
|
||||||
|
"github.com/google/cayley/quad"
|
||||||
|
)
|
||||||
|
|
||||||
|
var sqlNodeType graph.Type
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
sqlNodeType = graph.RegisterIterator("sqlnode")
|
||||||
|
}
|
||||||
|
|
||||||
|
type SQLNodeIterator struct {
|
||||||
|
uid uint64
|
||||||
|
qs *QuadStore
|
||||||
|
tagger graph.Tagger
|
||||||
|
tableName string
|
||||||
|
err error
|
||||||
|
|
||||||
|
cursor *sql.Rows
|
||||||
|
linkIts []sqlItDir
|
||||||
|
size int64
|
||||||
|
tagdirs []tagDir
|
||||||
|
|
||||||
|
result map[string]string
|
||||||
|
resultIndex int
|
||||||
|
resultList [][]string
|
||||||
|
resultNext [][]string
|
||||||
|
cols []string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) sqlClone() sqlIterator {
|
||||||
|
return n.Clone().(*SQLNodeIterator)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) Clone() graph.Iterator {
|
||||||
|
m := &SQLNodeIterator{
|
||||||
|
uid: iterator.NextUID(),
|
||||||
|
qs: n.qs,
|
||||||
|
size: n.size,
|
||||||
|
tableName: n.tableName,
|
||||||
|
}
|
||||||
|
for _, i := range n.linkIts {
|
||||||
|
m.linkIts = append(m.linkIts, sqlItDir{
|
||||||
|
dir: i.dir,
|
||||||
|
it: i.it.sqlClone(),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
copy(n.tagdirs, m.tagdirs)
|
||||||
|
m.tagger.CopyFrom(n)
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) UID() uint64 {
|
||||||
|
return n.uid
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) Reset() {
|
||||||
|
n.err = nil
|
||||||
|
n.Close()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) Err() error {
|
||||||
|
return n.err
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) Close() error {
|
||||||
|
if n.cursor != nil {
|
||||||
|
err := n.cursor.Close()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
n.cursor = nil
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) Tagger() *graph.Tagger {
|
||||||
|
return &n.tagger
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) Result() graph.Value {
|
||||||
|
return n.result["__execd"]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) TagResults(dst map[string]graph.Value) {
|
||||||
|
for tag, value := range n.result {
|
||||||
|
if tag == "__execd" {
|
||||||
|
for _, tag := range n.tagger.Tags() {
|
||||||
|
dst[tag] = value
|
||||||
|
}
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
dst[tag] = value
|
||||||
|
}
|
||||||
|
|
||||||
|
for tag, value := range n.tagger.Fixed() {
|
||||||
|
dst[tag] = value
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) Type() graph.Type {
|
||||||
|
return sqlNodeType
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) SubIterators() []graph.Iterator {
|
||||||
|
// TODO(barakmich): SQL Subiterators shouldn't count? If it makes sense,
|
||||||
|
// there's no reason not to expose them though.
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) Sorted() bool { return false }
|
||||||
|
func (n *SQLNodeIterator) Optimize() (graph.Iterator, bool) { return n, false }
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) Size() (int64, bool) {
|
||||||
|
return n.qs.Size() / int64(len(n.linkIts)+1), true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) Describe() graph.Description {
|
||||||
|
size, _ := n.Size()
|
||||||
|
return graph.Description{
|
||||||
|
UID: n.UID(),
|
||||||
|
Name: fmt.Sprintf("SQL_NODE_QUERY: %#v", n),
|
||||||
|
Type: n.Type(),
|
||||||
|
Size: size,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) Stats() graph.IteratorStats {
|
||||||
|
size, _ := n.Size()
|
||||||
|
return graph.IteratorStats{
|
||||||
|
ContainsCost: 1,
|
||||||
|
NextCost: 5,
|
||||||
|
Size: size,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) NextPath() bool {
|
||||||
|
n.resultIndex += 1
|
||||||
|
if n.resultIndex >= len(n.resultList) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
n.buildResult(n.resultIndex)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) buildResult(i int) {
|
||||||
|
container := n.resultList[i]
|
||||||
|
n.result = make(map[string]string)
|
||||||
|
for i, c := range n.cols {
|
||||||
|
n.result[c] = container[i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) getTables() []string {
|
||||||
|
var out []string
|
||||||
|
for _, i := range n.linkIts {
|
||||||
|
out = append(out, i.it.getTables()...)
|
||||||
|
}
|
||||||
|
if len(out) == 0 {
|
||||||
|
out = append(out, n.tableName)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) tableID() tagDir {
|
||||||
|
if len(n.linkIts) == 0 {
|
||||||
|
return tagDir{
|
||||||
|
table: n.tableName,
|
||||||
|
dir: quad.Any,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return tagDir{
|
||||||
|
table: n.linkIts[0].it.tableID().table,
|
||||||
|
dir: n.linkIts[0].dir,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) getTags() []tagDir {
|
||||||
|
myTag := n.tableID()
|
||||||
|
var out []tagDir
|
||||||
|
for _, tag := range n.tagger.Tags() {
|
||||||
|
out = append(out, tagDir{
|
||||||
|
dir: myTag.dir,
|
||||||
|
table: myTag.table,
|
||||||
|
tag: tag,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
for _, tag := range n.tagdirs {
|
||||||
|
out = append(out, tagDir{
|
||||||
|
dir: tag.dir,
|
||||||
|
table: myTag.table,
|
||||||
|
tag: tag.tag,
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
for _, i := range n.linkIts {
|
||||||
|
out = append(out, i.it.getTags()...)
|
||||||
|
}
|
||||||
|
return out
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) height() int {
|
||||||
|
v := 0
|
||||||
|
for _, i := range n.linkIts {
|
||||||
|
if i.it.height() > v {
|
||||||
|
v = i.it.height()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return v + 1
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) buildWhere() (string, []string) {
|
||||||
|
var q []string
|
||||||
|
var vals []string
|
||||||
|
if len(n.linkIts) > 1 {
|
||||||
|
baseTable := n.linkIts[0].it.tableID().table
|
||||||
|
baseDir := n.linkIts[0].dir
|
||||||
|
for _, i := range n.linkIts[1:] {
|
||||||
|
table := i.it.tableID().table
|
||||||
|
dir := i.dir
|
||||||
|
q = append(q, fmt.Sprintf("%s.%s = %s.%s", baseTable, baseDir, table, dir))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, i := range n.linkIts {
|
||||||
|
s, v := i.it.buildWhere()
|
||||||
|
q = append(q, s)
|
||||||
|
vals = append(vals, v...)
|
||||||
|
}
|
||||||
|
query := strings.Join(q, " AND ")
|
||||||
|
return query, vals
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) buildSQL(next bool, val graph.Value) (string, []string) {
|
||||||
|
topData := n.tableID()
|
||||||
|
query := "SELECT "
|
||||||
|
var t []string
|
||||||
|
t = append(t, fmt.Sprintf("%s.%s as __execd", topData.table, topData.dir))
|
||||||
|
for _, v := range n.getTags() {
|
||||||
|
t = append(t, fmt.Sprintf("%s.%s as %s", v.table, v.dir, v.tag))
|
||||||
|
}
|
||||||
|
query += strings.Join(t, ", ")
|
||||||
|
query += " FROM "
|
||||||
|
t = []string{}
|
||||||
|
for _, k := range n.getTables() {
|
||||||
|
t = append(t, fmt.Sprintf("quads as %s", k))
|
||||||
|
}
|
||||||
|
query += strings.Join(t, ", ")
|
||||||
|
query += " WHERE "
|
||||||
|
constraint, values := n.buildWhere()
|
||||||
|
|
||||||
|
if !next {
|
||||||
|
v := val.(string)
|
||||||
|
if constraint != "" {
|
||||||
|
constraint += " AND "
|
||||||
|
}
|
||||||
|
constraint += fmt.Sprintf("%s.%s = ?", topData.table, topData.dir)
|
||||||
|
values = append(values, v)
|
||||||
|
}
|
||||||
|
query += constraint
|
||||||
|
query += ";"
|
||||||
|
|
||||||
|
glog.V(2).Infoln(query)
|
||||||
|
|
||||||
|
if glog.V(4) {
|
||||||
|
dstr := query
|
||||||
|
for i := 1; i <= len(values); i++ {
|
||||||
|
dstr = strings.Replace(dstr, "?", fmt.Sprintf("'%s'", values[i-1]), 1)
|
||||||
|
}
|
||||||
|
glog.V(4).Infoln(dstr)
|
||||||
|
}
|
||||||
|
return query, values
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) Next() bool {
|
||||||
|
var err error
|
||||||
|
graph.NextLogIn(n)
|
||||||
|
if n.cursor == nil {
|
||||||
|
err = n.makeCursor(true, nil)
|
||||||
|
n.cols, err = n.cursor.Columns()
|
||||||
|
if err != nil {
|
||||||
|
glog.Errorf("Couldn't get columns")
|
||||||
|
n.err = err
|
||||||
|
n.cursor.Close()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
// iterate the first one
|
||||||
|
if !n.cursor.Next() {
|
||||||
|
glog.V(4).Infoln("sql: No next")
|
||||||
|
err := n.cursor.Err()
|
||||||
|
if err != nil {
|
||||||
|
glog.Errorf("Cursor error in SQL: %v", err)
|
||||||
|
n.err = err
|
||||||
|
}
|
||||||
|
n.cursor.Close()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
s, err := scan(n.cursor, len(n.cols))
|
||||||
|
if err != nil {
|
||||||
|
n.err = err
|
||||||
|
n.cursor.Close()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
n.resultNext = append(n.resultNext, s)
|
||||||
|
}
|
||||||
|
if n.resultList != nil && n.resultNext == nil {
|
||||||
|
// We're on something and there's no next
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
n.resultList = n.resultNext
|
||||||
|
n.resultNext = nil
|
||||||
|
n.resultIndex = 0
|
||||||
|
for {
|
||||||
|
if !n.cursor.Next() {
|
||||||
|
glog.V(4).Infoln("sql: No next")
|
||||||
|
err := n.cursor.Err()
|
||||||
|
if err != nil {
|
||||||
|
glog.Errorf("Cursor error in SQL: %v", err)
|
||||||
|
n.err = err
|
||||||
|
}
|
||||||
|
n.cursor.Close()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
s, err := scan(n.cursor, len(n.cols))
|
||||||
|
if err != nil {
|
||||||
|
n.err = err
|
||||||
|
n.cursor.Close()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
if n.resultList[0][0] != s[0] {
|
||||||
|
n.resultNext = append(n.resultNext, s)
|
||||||
|
break
|
||||||
|
} else {
|
||||||
|
n.resultList = append(n.resultList, s)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
if len(n.resultList) == 0 {
|
||||||
|
return graph.NextLogOut(n, nil, false)
|
||||||
|
}
|
||||||
|
n.buildResult(0)
|
||||||
|
return graph.NextLogOut(n, n.Result(), true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) makeCursor(next bool, value graph.Value) error {
|
||||||
|
if n.cursor != nil {
|
||||||
|
n.cursor.Close()
|
||||||
|
}
|
||||||
|
var q string
|
||||||
|
var values []string
|
||||||
|
q, values = n.buildSQL(next, value)
|
||||||
|
q = convertToPostgres(q, values)
|
||||||
|
ivalues := make([]interface{}, 0, len(values))
|
||||||
|
for _, v := range values {
|
||||||
|
ivalues = append(ivalues, v)
|
||||||
|
}
|
||||||
|
cursor, err := n.qs.db.Query(q, ivalues...)
|
||||||
|
if err != nil {
|
||||||
|
glog.Errorf("Couldn't get cursor from SQL database: %v", err)
|
||||||
|
cursor = nil
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
n.cursor = cursor
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (n *SQLNodeIterator) Contains(v graph.Value) bool {
|
||||||
|
var err error
|
||||||
|
//if it.preFilter(v) {
|
||||||
|
//return false
|
||||||
|
//}
|
||||||
|
err = n.makeCursor(false, v)
|
||||||
|
if err != nil {
|
||||||
|
glog.Errorf("Couldn't make query: %v", err)
|
||||||
|
n.err = err
|
||||||
|
n.cursor.Close()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
n.cols, err = n.cursor.Columns()
|
||||||
|
if err != nil {
|
||||||
|
glog.Errorf("Couldn't get columns")
|
||||||
|
n.err = err
|
||||||
|
n.cursor.Close()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
n.resultList = nil
|
||||||
|
for {
|
||||||
|
if !n.cursor.Next() {
|
||||||
|
glog.V(4).Infoln("sql: No next")
|
||||||
|
err := n.cursor.Err()
|
||||||
|
if err != nil {
|
||||||
|
glog.Errorf("Cursor error in SQL: %v", err)
|
||||||
|
n.err = err
|
||||||
|
}
|
||||||
|
n.cursor.Close()
|
||||||
|
break
|
||||||
|
}
|
||||||
|
s, err := scan(n.cursor, len(n.cols))
|
||||||
|
if err != nil {
|
||||||
|
n.err = err
|
||||||
|
n.cursor.Close()
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
n.resultList = append(n.resultList, s)
|
||||||
|
}
|
||||||
|
n.cursor.Close()
|
||||||
|
n.cursor = nil
|
||||||
|
if len(n.resultList) != 0 {
|
||||||
|
n.resultIndex = 0
|
||||||
|
n.buildResult(0)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
@ -51,11 +51,14 @@ var benchmarkQueries = []struct {
|
||||||
long bool
|
long bool
|
||||||
query string
|
query string
|
||||||
tag string
|
tag string
|
||||||
expect []interface{}
|
// for testing
|
||||||
|
skip bool
|
||||||
|
expect []interface{}
|
||||||
}{
|
}{
|
||||||
// Easy one to get us started. How quick is the most straightforward retrieval?
|
// Easy one to get us started. How quick is the most straightforward retrieval?
|
||||||
{
|
{
|
||||||
message: "name predicate",
|
message: "name predicate",
|
||||||
|
skip: true,
|
||||||
query: `
|
query: `
|
||||||
g.V("Humphrey Bogart").In("name").All()
|
g.V("Humphrey Bogart").In("name").All()
|
||||||
`,
|
`,
|
||||||
|
|
@ -69,6 +72,7 @@ var benchmarkQueries = []struct {
|
||||||
// that's going to be measurably slower for every other backend.
|
// that's going to be measurably slower for every other backend.
|
||||||
{
|
{
|
||||||
message: "two large sets with no intersection",
|
message: "two large sets with no intersection",
|
||||||
|
skip: true,
|
||||||
query: `
|
query: `
|
||||||
function getId(x) { return g.V(x).In("name") }
|
function getId(x) { return g.V(x).In("name") }
|
||||||
var actor_to_film = g.M().In("/film/performance/actor").In("/film/film/starring")
|
var actor_to_film = g.M().In("/film/performance/actor").In("/film/film/starring")
|
||||||
|
|
@ -534,6 +538,9 @@ func checkQueries(t *testing.T) {
|
||||||
if testing.Short() && test.long {
|
if testing.Short() && test.long {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if test.skip {
|
||||||
|
continue
|
||||||
|
}
|
||||||
fmt.Printf("Now testing %s\n", test.message)
|
fmt.Printf("Now testing %s\n", test.message)
|
||||||
ses := gremlin.NewSession(handle.QuadStore, cfg.Timeout, true)
|
ses := gremlin.NewSession(handle.QuadStore, cfg.Timeout, true)
|
||||||
_, err := ses.Parse(test.query)
|
_, err := ses.Parse(test.query)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue