attempt to build more interesting SQL queries
Subcommits: fix old iterator, and flesh out new builder iterator fix contains for builder iterator Working replacement iterator
This commit is contained in:
parent
da391c3db7
commit
185e236f15
5 changed files with 712 additions and 4 deletions
582
graph/sql/builder_iterator.go
Normal file
582
graph/sql/builder_iterator.go
Normal file
File diff suppressed because it is too large
Load diff
110
graph/sql/builder_iterator_test.go
Normal file
110
graph/sql/builder_iterator_test.go
Normal file
|
|
@ -0,0 +1,110 @@
|
||||||
|
// 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 (
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/google/cayley/graph"
|
||||||
|
"github.com/google/cayley/graph/iterator"
|
||||||
|
"github.com/google/cayley/quad"
|
||||||
|
)
|
||||||
|
|
||||||
|
var dbpath = flag.String("dbpath", "", "Path to running DB")
|
||||||
|
|
||||||
|
func TestSimpleSQL(t *testing.T) {
|
||||||
|
it := NewStatementIterator(nil, quad.Object, "cool")
|
||||||
|
s, v := it.buildQuery(false, nil)
|
||||||
|
fmt.Println(s, v)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functional tests
|
||||||
|
|
||||||
|
func TestQuadIteration(t *testing.T) {
|
||||||
|
if *dbpath == "" {
|
||||||
|
t.SkipNow()
|
||||||
|
}
|
||||||
|
db, err := newQuadStore(*dbpath, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
it := NewStatementIterator(db.(*QuadStore), quad.Object, "Humphrey Bogart")
|
||||||
|
for graph.Next(it) {
|
||||||
|
fmt.Println(it.Result())
|
||||||
|
}
|
||||||
|
it = NewStatementIterator(db.(*QuadStore), quad.Subject, "/en/casablanca_1942")
|
||||||
|
s, v := it.buildQuery(false, 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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestNodeIteration(t *testing.T) {
|
||||||
|
if *dbpath == "" {
|
||||||
|
t.SkipNow()
|
||||||
|
}
|
||||||
|
db, err := newQuadStore(*dbpath, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
it := &StatementIterator{
|
||||||
|
uid: iterator.NextUID(),
|
||||||
|
qs: db.(*QuadStore),
|
||||||
|
stType: node,
|
||||||
|
dir: quad.Object,
|
||||||
|
tags: []tag{
|
||||||
|
tag{
|
||||||
|
pair: tableDir{
|
||||||
|
table: "t_4",
|
||||||
|
dir: quad.Subject,
|
||||||
|
},
|
||||||
|
t: "x",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
where: baseClause{
|
||||||
|
pair: tableDir{
|
||||||
|
table: "t_4",
|
||||||
|
dir: quad.Subject,
|
||||||
|
},
|
||||||
|
strTarget: []string{"/en/casablanca_1942"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
s, v := it.buildQuery(false, nil)
|
||||||
|
it.Tagger().Add("id")
|
||||||
|
fmt.Println(s, v)
|
||||||
|
for graph.Next(it) {
|
||||||
|
fmt.Println(it.Result())
|
||||||
|
out := make(map[string]graph.Value)
|
||||||
|
it.TagResults(out)
|
||||||
|
for k, v := range out {
|
||||||
|
fmt.Printf("%s: %v\n", k, v.(string))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
contains := it.Contains("Casablanca")
|
||||||
|
s, v = it.buildQuery(true, "Casablanca")
|
||||||
|
fmt.Println(s, v)
|
||||||
|
it.Tagger().Add("id")
|
||||||
|
if !contains {
|
||||||
|
t.Error("Didn't contain Casablanca")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -199,10 +199,12 @@ func (it *Iterator) Next() bool {
|
||||||
func (it *Iterator) Contains(v graph.Value) bool {
|
func (it *Iterator) Contains(v graph.Value) bool {
|
||||||
graph.ContainsLogIn(it, v)
|
graph.ContainsLogIn(it, v)
|
||||||
if it.isAll {
|
if it.isAll {
|
||||||
|
it.result = v
|
||||||
return graph.ContainsLogOut(it, v, true)
|
return graph.ContainsLogOut(it, v, true)
|
||||||
}
|
}
|
||||||
q := v.(quad.Quad)
|
q := v.(quad.Quad)
|
||||||
if q.Get(it.dir) == it.val.(string) {
|
if q.Get(it.dir) == it.val.(string) {
|
||||||
|
it.result = v
|
||||||
return graph.ContainsLogOut(it, v, true)
|
return graph.ContainsLogOut(it, v, true)
|
||||||
}
|
}
|
||||||
return graph.ContainsLogOut(it, v, false)
|
return graph.ContainsLogOut(it, v, false)
|
||||||
|
|
@ -217,6 +219,9 @@ func (it *Iterator) Size() (int64, bool) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (it *Iterator) Result() graph.Value {
|
func (it *Iterator) Result() graph.Value {
|
||||||
|
if it.result == nil {
|
||||||
|
glog.Fatalln("result was nil", it)
|
||||||
|
}
|
||||||
return it.result
|
return it.result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -239,7 +244,7 @@ func (it *Iterator) Type() graph.Type {
|
||||||
return sqlType
|
return sqlType
|
||||||
}
|
}
|
||||||
|
|
||||||
func (it *Iterator) Sorted() bool { return true }
|
func (it *Iterator) Sorted() bool { return false }
|
||||||
func (it *Iterator) Optimize() (graph.Iterator, bool) { return it, false }
|
func (it *Iterator) Optimize() (graph.Iterator, bool) { return it, false }
|
||||||
|
|
||||||
func (it *Iterator) Describe() graph.Description {
|
func (it *Iterator) Describe() graph.Description {
|
||||||
|
|
|
||||||
|
|
@ -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 NewIterator(qs, d, val)
|
return NewStatementIterator(qs, d, val.(string))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (qs *QuadStore) NodesAllIterator() graph.Iterator {
|
func (qs *QuadStore) NodesAllIterator() graph.Iterator {
|
||||||
|
|
|
||||||
|
|
@ -37,12 +37,14 @@ import (
|
||||||
_ "github.com/google/cayley/graph/leveldb"
|
_ "github.com/google/cayley/graph/leveldb"
|
||||||
_ "github.com/google/cayley/graph/memstore"
|
_ "github.com/google/cayley/graph/memstore"
|
||||||
_ "github.com/google/cayley/graph/mongo"
|
_ "github.com/google/cayley/graph/mongo"
|
||||||
|
_ "github.com/google/cayley/graph/sql"
|
||||||
|
|
||||||
// Load writer registry
|
// Load writer registry
|
||||||
_ "github.com/google/cayley/writer"
|
_ "github.com/google/cayley/writer"
|
||||||
)
|
)
|
||||||
|
|
||||||
var backend = flag.String("backend", "memstore", "Which backend to test. Loads test data to /tmp if not present.")
|
var backend = flag.String("backend", "memstore", "Which backend to test. Loads test data to /tmp if not present.")
|
||||||
|
var backendPath = flag.String("backend_path", "", "Path to the chosen backend. Will have sane testing defaults if not specified")
|
||||||
|
|
||||||
var benchmarkQueries = []struct {
|
var benchmarkQueries = []struct {
|
||||||
message string
|
message string
|
||||||
|
|
@ -422,6 +424,7 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
func prepare(t testing.TB) {
|
func prepare(t testing.TB) {
|
||||||
|
var remote bool
|
||||||
cfg.DatabaseType = *backend
|
cfg.DatabaseType = *backend
|
||||||
switch *backend {
|
switch *backend {
|
||||||
case "memstore":
|
case "memstore":
|
||||||
|
|
@ -436,14 +439,21 @@ func prepare(t testing.TB) {
|
||||||
cfg.DatabaseOptions = map[string]interface{}{
|
cfg.DatabaseOptions = map[string]interface{}{
|
||||||
"database_name": "cayley_test", // provide a default test database
|
"database_name": "cayley_test", // provide a default test database
|
||||||
}
|
}
|
||||||
|
remote = true
|
||||||
|
case "sql":
|
||||||
|
cfg.DatabasePath = "postgres://localhost/cayley_test"
|
||||||
|
remote = true
|
||||||
default:
|
default:
|
||||||
t.Fatalf("Untestable backend store %s", *backend)
|
t.Fatalf("Untestable backend store %s", *backend)
|
||||||
}
|
}
|
||||||
|
if *backendPath != "" {
|
||||||
|
cfg.DatabasePath = *backendPath
|
||||||
|
}
|
||||||
|
|
||||||
var err error
|
var err error
|
||||||
create.Do(func() {
|
create.Do(func() {
|
||||||
needsLoad := true
|
needsLoad := true
|
||||||
if graph.IsPersistent(cfg.DatabaseType) {
|
if graph.IsPersistent(cfg.DatabaseType) && !remote {
|
||||||
if _, err := os.Stat(cfg.DatabasePath); os.IsNotExist(err) {
|
if _, err := os.Stat(cfg.DatabasePath); os.IsNotExist(err) {
|
||||||
err = db.Init(cfg)
|
err = db.Init(cfg)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -459,7 +469,7 @@ func prepare(t testing.TB) {
|
||||||
t.Fatalf("Failed to open %q: %v", cfg.DatabasePath, err)
|
t.Fatalf("Failed to open %q: %v", cfg.DatabasePath, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if needsLoad {
|
if needsLoad && !remote {
|
||||||
err = internal.Load(handle.QuadWriter, cfg, "../data/30kmoviedata.nq.gz", "cquad")
|
err = internal.Load(handle.QuadWriter, cfg, "../data/30kmoviedata.nq.gz", "cquad")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to load %q: %v", cfg.DatabasePath, err)
|
t.Fatalf("Failed to load %q: %v", cfg.DatabasePath, err)
|
||||||
|
|
@ -524,6 +534,7 @@ func checkQueries(t *testing.T) {
|
||||||
if testing.Short() && test.long {
|
if testing.Short() && test.long {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
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)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue