Merge pull request #28 from kortschak/destutter
Destutter labels and files
This commit is contained in:
commit
dc65ebce9e
74 changed files with 4945 additions and 4929 deletions
14
.travis.yml
14
.travis.yml
|
|
@ -4,3 +4,17 @@ install:
|
|||
- go get github.com/jacobsa/ogletest
|
||||
- go get github.com/jacobsa/oglematchers
|
||||
- go get github.com/smartystreets/goconvey
|
||||
- go get github.com/badgerodon/peg
|
||||
- go get github.com/barakmich/glog
|
||||
- go get github.com/julienschmidt/httprouter
|
||||
- go get github.com/petar/GoLLRB/llrb
|
||||
- go get github.com/robertkrimen/otto
|
||||
- go get github.com/russross/blackfriday
|
||||
- go get github.com/stretchrcom/testify/mock
|
||||
- go get github.com/syndtr/goleveldb/leveldb
|
||||
- go get github.com/syndtr/goleveldb/leveldb/cache
|
||||
- go get github.com/syndtr/goleveldb/leveldb/iterator
|
||||
- go get github.com/syndtr/goleveldb/leveldb/opt
|
||||
- go get github.com/syndtr/goleveldb/leveldb/util
|
||||
- go get labix.org/v2/mgo
|
||||
- go get labix.org/v2/mgo/bson
|
||||
|
|
|
|||
1
AUTHORS
1
AUTHORS
|
|
@ -9,3 +9,4 @@
|
|||
# Please keep the list sorted.
|
||||
|
||||
Google Inc.
|
||||
Robert Daniel Kortschak <dan.kortschak@adelaide.edu.au>
|
||||
|
|
|
|||
|
|
@ -12,3 +12,4 @@
|
|||
# Please keep the list sorted.
|
||||
|
||||
Barak Michener <barakmich@google.com> <barak@cayley.io> <me@barakmich.com>
|
||||
Robert Daniel Kortschak <dan.kortschak@adelaide.edu.au>
|
||||
|
|
|
|||
|
|
@ -5,6 +5,7 @@ Cayley is an open-source graph inspired by the graph database behind [Freebase](
|
|||
|
||||
Its goal is to be a part of the developer's toolbox where [Linked Data](http://linkeddata.org/) and graph-shaped data (semantic webs, social networks, etc) in general are concerned.
|
||||
|
||||
[](https://travis-ci.org/google/cayley)
|
||||
|
||||
## Features
|
||||
|
||||
|
|
|
|||
|
|
@ -29,7 +29,7 @@ import (
|
|||
func init() {
|
||||
glog.SetToStderr(true)
|
||||
cfg := config.ParseConfigFromFile("cayley_appengine.cfg")
|
||||
ts := memstore.NewMemTripleStore()
|
||||
ts := memstore.NewTripleStore()
|
||||
glog.Errorln(cfg)
|
||||
LoadTriplesFromFileInto(ts, cfg.DatabasePath, cfg.LoadSize)
|
||||
http.SetupRoutes(ts, cfg)
|
||||
|
|
|
|||
14
cayley.go
14
cayley.go
|
|
@ -69,18 +69,18 @@ func main() {
|
|||
}
|
||||
switch cmd {
|
||||
case "init":
|
||||
db.CayleyInit(cfg, *tripleFile)
|
||||
db.Init(cfg, *tripleFile)
|
||||
case "load":
|
||||
ts = db.OpenTSFromConfig(cfg)
|
||||
db.CayleyLoad(ts, cfg, *tripleFile, false)
|
||||
ts = db.Open(cfg)
|
||||
db.Load(ts, cfg, *tripleFile, false)
|
||||
ts.Close()
|
||||
case "repl":
|
||||
ts = db.OpenTSFromConfig(cfg)
|
||||
db.CayleyRepl(ts, *queryLanguage, cfg)
|
||||
ts = db.Open(cfg)
|
||||
db.Repl(ts, *queryLanguage, cfg)
|
||||
ts.Close()
|
||||
case "http":
|
||||
ts = db.OpenTSFromConfig(cfg)
|
||||
http.CayleyHTTP(ts, cfg)
|
||||
ts = db.Open(cfg)
|
||||
http.Serve(ts, cfg)
|
||||
ts.Close()
|
||||
default:
|
||||
fmt.Println("No command", cmd)
|
||||
|
|
|
|||
|
|
@ -22,7 +22,7 @@ import (
|
|||
"github.com/barakmich/glog"
|
||||
)
|
||||
|
||||
type CayleyConfig struct {
|
||||
type Config struct {
|
||||
DatabaseType string `json:"database"`
|
||||
DatabasePath string `json:"db_path"`
|
||||
DatabaseOptions map[string]interface{} `json:"db_options"`
|
||||
|
|
@ -41,8 +41,8 @@ var port = flag.String("port", "64210", "Port to listen on.")
|
|||
var readOnly = flag.Bool("read_only", false, "Disable writing via HTTP.")
|
||||
var gremlinTimeout = flag.Int("gremlin_timeout", 30, "Number of seconds until an individual query times out.")
|
||||
|
||||
func ParseConfigFromFile(filename string) *CayleyConfig {
|
||||
config := &CayleyConfig{}
|
||||
func ParseConfigFromFile(filename string) *Config {
|
||||
config := &Config{}
|
||||
if filename == "" {
|
||||
return config
|
||||
}
|
||||
|
|
@ -61,7 +61,7 @@ func ParseConfigFromFile(filename string) *CayleyConfig {
|
|||
return config
|
||||
}
|
||||
|
||||
func ParseConfigFromFlagsAndFile(fileFlag string) *CayleyConfig {
|
||||
func ParseConfigFromFlagsAndFile(fileFlag string) *Config {
|
||||
// Find the file...
|
||||
var trueFilename string
|
||||
if fileFlag != "" {
|
||||
|
|
|
|||
|
|
@ -20,7 +20,7 @@ import (
|
|||
"github.com/google/cayley/graph/mongo"
|
||||
)
|
||||
|
||||
func CayleyInit(cfg *config.CayleyConfig, triplePath string) bool {
|
||||
func Init(cfg *config.Config, triplePath string) bool {
|
||||
created := false
|
||||
dbpath := cfg.DatabasePath
|
||||
switch cfg.DatabaseType {
|
||||
|
|
@ -32,8 +32,8 @@ func CayleyInit(cfg *config.CayleyConfig, triplePath string) bool {
|
|||
return true
|
||||
}
|
||||
if created && triplePath != "" {
|
||||
ts := OpenTSFromConfig(cfg)
|
||||
CayleyLoad(ts, cfg, triplePath, true)
|
||||
ts := Open(cfg)
|
||||
Load(ts, cfg, triplePath, true)
|
||||
ts.Close()
|
||||
}
|
||||
return created
|
||||
|
|
|
|||
|
|
@ -25,11 +25,11 @@ import (
|
|||
"github.com/google/cayley/nquads"
|
||||
)
|
||||
|
||||
func CayleyLoad(ts graph.TripleStore, cfg *config.CayleyConfig, triplePath string, firstTime bool) {
|
||||
func Load(ts graph.TripleStore, cfg *config.Config, triplePath string, firstTime bool) {
|
||||
switch cfg.DatabaseType {
|
||||
case "mongo", "mongodb":
|
||||
if firstTime {
|
||||
loadMongo(ts.(*mongo.MongoTripleStore), triplePath)
|
||||
loadMongo(ts.(*mongo.TripleStore), triplePath)
|
||||
} else {
|
||||
LoadTriplesFromFileInto(ts, triplePath, cfg.LoadSize)
|
||||
}
|
||||
|
|
@ -43,7 +43,7 @@ func CayleyLoad(ts graph.TripleStore, cfg *config.CayleyConfig, triplePath strin
|
|||
|
||||
}
|
||||
|
||||
func loadMongo(ts *mongo.MongoTripleStore, path string) {
|
||||
func loadMongo(ts *mongo.TripleStore, path string) {
|
||||
tChan := make(chan *graph.Triple)
|
||||
go ReadTriplesFromFile(tChan, path)
|
||||
ts.BulkLoad(tChan)
|
||||
|
|
|
|||
10
db/open.go
10
db/open.go
|
|
@ -24,16 +24,16 @@ import (
|
|||
"github.com/google/cayley/graph/mongo"
|
||||
)
|
||||
|
||||
func OpenTSFromConfig(cfg *config.CayleyConfig) graph.TripleStore {
|
||||
func Open(cfg *config.Config) graph.TripleStore {
|
||||
glog.Infof("Opening database \"%s\" at %s", cfg.DatabaseType, cfg.DatabasePath)
|
||||
switch cfg.DatabaseType {
|
||||
case "mongo", "mongodb":
|
||||
return mongo.NewMongoTripleStore(cfg.DatabasePath, cfg.DatabaseOptions)
|
||||
return mongo.NewTripleStore(cfg.DatabasePath, cfg.DatabaseOptions)
|
||||
case "leveldb":
|
||||
return leveldb.NewDefaultLevelDBTripleStore(cfg.DatabasePath, cfg.DatabaseOptions)
|
||||
return leveldb.NewTripleStore(cfg.DatabasePath, cfg.DatabaseOptions)
|
||||
case "mem":
|
||||
ts := memstore.NewMemTripleStore()
|
||||
CayleyLoad(ts, cfg, cfg.DatabasePath, true)
|
||||
ts := memstore.NewTripleStore()
|
||||
Load(ts, cfg, cfg.DatabasePath, true)
|
||||
return ts
|
||||
}
|
||||
panic("Unsupported database backend " + cfg.DatabaseType)
|
||||
|
|
|
|||
16
db/repl.go
16
db/repl.go
|
|
@ -40,7 +40,7 @@ func un(s string, startTime time.Time) {
|
|||
fmt.Printf(s, float64(endTime.UnixNano()-startTime.UnixNano())/float64(1E6))
|
||||
}
|
||||
|
||||
func RunQuery(query string, ses graph.Session) {
|
||||
func Run(query string, ses graph.Session) {
|
||||
nResults := 0
|
||||
startTrace, startTime := trace("Elapsed time: %g ms\n\n")
|
||||
defer func() {
|
||||
|
|
@ -60,17 +60,17 @@ func RunQuery(query string, ses graph.Session) {
|
|||
}
|
||||
}
|
||||
|
||||
func CayleyRepl(ts graph.TripleStore, queryLanguage string, cfg *config.CayleyConfig) {
|
||||
func Repl(ts graph.TripleStore, queryLanguage string, cfg *config.Config) {
|
||||
var ses graph.Session
|
||||
switch queryLanguage {
|
||||
case "sexp":
|
||||
ses = sexp.NewSexpSession(ts)
|
||||
ses = sexp.NewSession(ts)
|
||||
case "mql":
|
||||
ses = mql.NewMqlSession(ts)
|
||||
ses = mql.NewSession(ts)
|
||||
case "gremlin":
|
||||
fallthrough
|
||||
default:
|
||||
ses = gremlin.NewGremlinSession(ts, cfg.GremlinTimeout, true)
|
||||
ses = gremlin.NewSession(ts, cfg.GremlinTimeout, true)
|
||||
}
|
||||
inputBf := bufio.NewReader(os.Stdin)
|
||||
line := ""
|
||||
|
|
@ -106,7 +106,7 @@ func CayleyRepl(ts graph.TripleStore, queryLanguage string, cfg *config.CayleyCo
|
|||
}
|
||||
if strings.HasPrefix(line, ":a") {
|
||||
var tripleStmt = line[3:]
|
||||
triple := nquads.ParseLineToTriple(tripleStmt)
|
||||
triple := nquads.Parse(tripleStmt)
|
||||
if triple == nil {
|
||||
fmt.Println("Not a valid triple.")
|
||||
line = ""
|
||||
|
|
@ -118,7 +118,7 @@ func CayleyRepl(ts graph.TripleStore, queryLanguage string, cfg *config.CayleyCo
|
|||
}
|
||||
if strings.HasPrefix(line, ":d") {
|
||||
var tripleStmt = line[3:]
|
||||
triple := nquads.ParseLineToTriple(tripleStmt)
|
||||
triple := nquads.Parse(tripleStmt)
|
||||
if triple == nil {
|
||||
fmt.Println("Not a valid triple.")
|
||||
line = ""
|
||||
|
|
@ -131,7 +131,7 @@ func CayleyRepl(ts graph.TripleStore, queryLanguage string, cfg *config.CayleyCo
|
|||
result, err := ses.InputParses(line)
|
||||
switch result {
|
||||
case graph.Parsed:
|
||||
RunQuery(line, ses)
|
||||
Run(line, ses)
|
||||
line = ""
|
||||
case graph.ParseFail:
|
||||
fmt.Println("Error: ", err)
|
||||
|
|
|
|||
|
|
@ -19,26 +19,26 @@ import (
|
|||
"fmt"
|
||||
"strings"
|
||||
|
||||
leveldb_it "github.com/syndtr/goleveldb/leveldb/iterator"
|
||||
leveldb_opt "github.com/syndtr/goleveldb/leveldb/opt"
|
||||
"github.com/syndtr/goleveldb/leveldb/iterator"
|
||||
"github.com/syndtr/goleveldb/leveldb/opt"
|
||||
|
||||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
type LevelDBAllIterator struct {
|
||||
type AllIterator struct {
|
||||
graph.BaseIterator
|
||||
prefix []byte
|
||||
dir string
|
||||
open bool
|
||||
it leveldb_it.Iterator
|
||||
ts *LevelDBTripleStore
|
||||
ro *leveldb_opt.ReadOptions
|
||||
it iterator.Iterator
|
||||
ts *TripleStore
|
||||
ro *opt.ReadOptions
|
||||
}
|
||||
|
||||
func NewLevelDBAllIterator(prefix, dir string, ts *LevelDBTripleStore) *LevelDBAllIterator {
|
||||
var it LevelDBAllIterator
|
||||
func NewLevelDBAllIterator(prefix, dir string, ts *TripleStore) *AllIterator {
|
||||
var it AllIterator
|
||||
graph.BaseIteratorInit(&it.BaseIterator)
|
||||
it.ro = &leveldb_opt.ReadOptions{}
|
||||
it.ro = &opt.ReadOptions{}
|
||||
it.ro.DontFillCache = true
|
||||
it.it = ts.db.NewIterator(nil, it.ro)
|
||||
it.prefix = []byte(prefix)
|
||||
|
|
@ -53,7 +53,7 @@ func NewLevelDBAllIterator(prefix, dir string, ts *LevelDBTripleStore) *LevelDBA
|
|||
return &it
|
||||
}
|
||||
|
||||
func (a *LevelDBAllIterator) Reset() {
|
||||
func (a *AllIterator) Reset() {
|
||||
if !a.open {
|
||||
a.it = a.ts.db.NewIterator(nil, a.ro)
|
||||
a.open = true
|
||||
|
|
@ -65,13 +65,13 @@ func (a *LevelDBAllIterator) Reset() {
|
|||
}
|
||||
}
|
||||
|
||||
func (a *LevelDBAllIterator) Clone() graph.Iterator {
|
||||
func (a *AllIterator) Clone() graph.Iterator {
|
||||
out := NewLevelDBAllIterator(string(a.prefix), a.dir, a.ts)
|
||||
out.CopyTagsFrom(a)
|
||||
return out
|
||||
}
|
||||
|
||||
func (a *LevelDBAllIterator) Next() (graph.TSVal, bool) {
|
||||
func (a *AllIterator) Next() (graph.TSVal, bool) {
|
||||
if !a.open {
|
||||
a.Last = nil
|
||||
return nil, false
|
||||
|
|
@ -91,19 +91,19 @@ func (a *LevelDBAllIterator) Next() (graph.TSVal, bool) {
|
|||
return out, true
|
||||
}
|
||||
|
||||
func (a *LevelDBAllIterator) Check(v graph.TSVal) bool {
|
||||
func (a *AllIterator) Check(v graph.TSVal) bool {
|
||||
a.Last = v
|
||||
return true
|
||||
}
|
||||
|
||||
func (lit *LevelDBAllIterator) Close() {
|
||||
func (lit *AllIterator) Close() {
|
||||
if lit.open {
|
||||
lit.it.Release()
|
||||
lit.open = false
|
||||
}
|
||||
}
|
||||
|
||||
func (a *LevelDBAllIterator) Size() (int64, bool) {
|
||||
func (a *AllIterator) Size() (int64, bool) {
|
||||
size, err := a.ts.GetApproximateSizeForPrefix(a.prefix)
|
||||
if err == nil {
|
||||
return size, false
|
||||
|
|
@ -112,19 +112,19 @@ func (a *LevelDBAllIterator) Size() (int64, bool) {
|
|||
return int64(^uint64(0) >> 1), false
|
||||
}
|
||||
|
||||
func (lit *LevelDBAllIterator) DebugString(indent int) string {
|
||||
func (lit *AllIterator) DebugString(indent int) string {
|
||||
size, _ := lit.Size()
|
||||
return fmt.Sprintf("%s(%s tags: %v leveldb size:%d %s %p)", strings.Repeat(" ", indent), lit.Type(), lit.Tags(), size, lit.dir, lit)
|
||||
}
|
||||
|
||||
func (lit *LevelDBAllIterator) Type() string { return "all" }
|
||||
func (lit *LevelDBAllIterator) Sorted() bool { return false }
|
||||
func (lit *AllIterator) Type() string { return "all" }
|
||||
func (lit *AllIterator) Sorted() bool { return false }
|
||||
|
||||
func (lit *LevelDBAllIterator) Optimize() (graph.Iterator, bool) {
|
||||
func (lit *AllIterator) Optimize() (graph.Iterator, bool) {
|
||||
return lit, false
|
||||
}
|
||||
|
||||
func (lit *LevelDBAllIterator) GetStats() *graph.IteratorStats {
|
||||
func (lit *AllIterator) GetStats() *graph.IteratorStats {
|
||||
s, _ := lit.Size()
|
||||
return &graph.IteratorStats{
|
||||
CheckCost: 1,
|
||||
|
|
@ -16,30 +16,29 @@ package leveldb
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
_ "encoding/binary"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
leveldb_it "github.com/syndtr/goleveldb/leveldb/iterator"
|
||||
leveldb_opt "github.com/syndtr/goleveldb/leveldb/opt"
|
||||
"github.com/syndtr/goleveldb/leveldb/iterator"
|
||||
"github.com/syndtr/goleveldb/leveldb/opt"
|
||||
|
||||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
type LevelDBIterator struct {
|
||||
type Iterator struct {
|
||||
graph.BaseIterator
|
||||
nextPrefix []byte
|
||||
checkId []byte
|
||||
dir string
|
||||
open bool
|
||||
it leveldb_it.Iterator
|
||||
ts *LevelDBTripleStore
|
||||
ro *leveldb_opt.ReadOptions
|
||||
it iterator.Iterator
|
||||
ts *TripleStore
|
||||
ro *opt.ReadOptions
|
||||
originalPrefix string
|
||||
}
|
||||
|
||||
func NewLevelDBIterator(prefix, dir string, value graph.TSVal, ts *LevelDBTripleStore) *LevelDBIterator {
|
||||
var it LevelDBIterator
|
||||
func NewIterator(prefix, dir string, value graph.TSVal, ts *TripleStore) *Iterator {
|
||||
var it Iterator
|
||||
graph.BaseIteratorInit(&it.BaseIterator)
|
||||
it.checkId = value.([]byte)
|
||||
it.dir = dir
|
||||
|
|
@ -47,7 +46,7 @@ func NewLevelDBIterator(prefix, dir string, value graph.TSVal, ts *LevelDBTriple
|
|||
it.nextPrefix = make([]byte, 0, 2+ts.hasher.Size())
|
||||
it.nextPrefix = append(it.nextPrefix, []byte(prefix)...)
|
||||
it.nextPrefix = append(it.nextPrefix, []byte(it.checkId[1:])...)
|
||||
it.ro = &leveldb_opt.ReadOptions{}
|
||||
it.ro = &opt.ReadOptions{}
|
||||
it.ro.DontFillCache = true
|
||||
it.it = ts.db.NewIterator(nil, it.ro)
|
||||
it.open = true
|
||||
|
|
@ -60,7 +59,7 @@ func NewLevelDBIterator(prefix, dir string, value graph.TSVal, ts *LevelDBTriple
|
|||
return &it
|
||||
}
|
||||
|
||||
func (lit *LevelDBIterator) Reset() {
|
||||
func (lit *Iterator) Reset() {
|
||||
if !lit.open {
|
||||
lit.it = lit.ts.db.NewIterator(nil, lit.ro)
|
||||
lit.open = true
|
||||
|
|
@ -72,20 +71,20 @@ func (lit *LevelDBIterator) Reset() {
|
|||
}
|
||||
}
|
||||
|
||||
func (lit *LevelDBIterator) Clone() graph.Iterator {
|
||||
out := NewLevelDBIterator(lit.originalPrefix, lit.dir, lit.checkId, lit.ts)
|
||||
func (lit *Iterator) Clone() graph.Iterator {
|
||||
out := NewIterator(lit.originalPrefix, lit.dir, lit.checkId, lit.ts)
|
||||
out.CopyTagsFrom(lit)
|
||||
return out
|
||||
}
|
||||
|
||||
func (lit *LevelDBIterator) Close() {
|
||||
func (lit *Iterator) Close() {
|
||||
if lit.open {
|
||||
lit.it.Release()
|
||||
lit.open = false
|
||||
}
|
||||
}
|
||||
|
||||
func (lit *LevelDBIterator) Next() (graph.TSVal, bool) {
|
||||
func (lit *Iterator) Next() (graph.TSVal, bool) {
|
||||
if lit.it == nil {
|
||||
lit.Last = nil
|
||||
return nil, false
|
||||
|
|
@ -114,7 +113,7 @@ func (lit *LevelDBIterator) Next() (graph.TSVal, bool) {
|
|||
return nil, false
|
||||
}
|
||||
|
||||
func GetPositionFromPrefix(prefix []byte, dir string, ts *LevelDBTripleStore) int {
|
||||
func GetPositionFromPrefix(prefix []byte, dir string, ts *TripleStore) int {
|
||||
if bytes.Equal(prefix, []byte("sp")) {
|
||||
switch dir {
|
||||
case "s":
|
||||
|
|
@ -166,7 +165,7 @@ func GetPositionFromPrefix(prefix []byte, dir string, ts *LevelDBTripleStore) in
|
|||
panic("Notreached")
|
||||
}
|
||||
|
||||
func (lit *LevelDBIterator) Check(v graph.TSVal) bool {
|
||||
func (lit *Iterator) Check(v graph.TSVal) bool {
|
||||
val := v.([]byte)
|
||||
if val[0] == 'z' {
|
||||
return false
|
||||
|
|
@ -186,23 +185,23 @@ func (lit *LevelDBIterator) Check(v graph.TSVal) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func (lit *LevelDBIterator) Size() (int64, bool) {
|
||||
func (lit *Iterator) Size() (int64, bool) {
|
||||
return lit.ts.GetSizeFor(lit.checkId), true
|
||||
}
|
||||
|
||||
func (lit *LevelDBIterator) DebugString(indent int) string {
|
||||
func (lit *Iterator) DebugString(indent int) string {
|
||||
size, _ := lit.Size()
|
||||
return fmt.Sprintf("%s(%s %d tags: %v dir: %s size:%d %s)", strings.Repeat(" ", indent), lit.Type(), lit.GetUid(), lit.Tags(), lit.dir, size, lit.ts.GetNameFor(lit.checkId))
|
||||
}
|
||||
|
||||
func (lit *LevelDBIterator) Type() string { return "leveldb" }
|
||||
func (lit *LevelDBIterator) Sorted() bool { return false }
|
||||
func (lit *Iterator) Type() string { return "leveldb" }
|
||||
func (lit *Iterator) Sorted() bool { return false }
|
||||
|
||||
func (lit *LevelDBIterator) Optimize() (graph.Iterator, bool) {
|
||||
func (lit *Iterator) Optimize() (graph.Iterator, bool) {
|
||||
return lit, false
|
||||
}
|
||||
|
||||
func (lit *LevelDBIterator) GetStats() *graph.IteratorStats {
|
||||
func (lit *Iterator) GetStats() *graph.IteratorStats {
|
||||
s, _ := lit.Size()
|
||||
return &graph.IteratorStats{
|
||||
CheckCost: 1,
|
||||
|
|
@ -79,7 +79,7 @@ func TestCreateDatabase(t *testing.T) {
|
|||
ok := CreateNewLevelDB(tmpDir)
|
||||
So(ok, ShouldBeTrue)
|
||||
Convey("And has good defaults for a new database", func() {
|
||||
ts := NewDefaultLevelDBTripleStore(tmpDir, nil)
|
||||
ts := NewTripleStore(tmpDir, nil)
|
||||
So(ts, ShouldNotBeNil)
|
||||
So(ts.Size(), ShouldEqual, 0)
|
||||
ts.Close()
|
||||
|
|
@ -89,7 +89,7 @@ func TestCreateDatabase(t *testing.T) {
|
|||
Convey("Fails if it cannot create the database", func() {
|
||||
ok := CreateNewLevelDB("/dev/null/some terrible path")
|
||||
So(ok, ShouldBeFalse)
|
||||
So(func() { NewDefaultLevelDBTripleStore("/dev/null/some terrible path", nil) }, ShouldPanic)
|
||||
So(func() { NewTripleStore("/dev/null/some terrible path", nil) }, ShouldPanic)
|
||||
})
|
||||
|
||||
Reset(func() {
|
||||
|
|
@ -101,14 +101,14 @@ func TestCreateDatabase(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestLoadDatabase(t *testing.T) {
|
||||
var ts *LevelDBTripleStore
|
||||
var ts *TripleStore
|
||||
|
||||
Convey("Given a created database path", t, func() {
|
||||
tmpDir, _ := ioutil.TempDir(os.TempDir(), "cayley_test")
|
||||
t.Log(tmpDir)
|
||||
ok := CreateNewLevelDB(tmpDir)
|
||||
So(ok, ShouldBeTrue)
|
||||
ts = NewDefaultLevelDBTripleStore(tmpDir, nil)
|
||||
ts = NewTripleStore(tmpDir, nil)
|
||||
|
||||
Convey("Can load a single triple", func() {
|
||||
ts.AddTriple(graph.MakeTriple("Something", "points_to", "Something Else", "context"))
|
||||
|
|
@ -139,7 +139,7 @@ func TestLoadDatabase(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestIterator(t *testing.T) {
|
||||
var ts *LevelDBTripleStore
|
||||
var ts *TripleStore
|
||||
|
||||
Convey("Given a prepared database", t, func() {
|
||||
tmpDir, _ := ioutil.TempDir(os.TempDir(), "cayley_test")
|
||||
|
|
@ -147,7 +147,7 @@ func TestIterator(t *testing.T) {
|
|||
defer os.RemoveAll(tmpDir)
|
||||
ok := CreateNewLevelDB(tmpDir)
|
||||
So(ok, ShouldBeTrue)
|
||||
ts = NewDefaultLevelDBTripleStore(tmpDir, nil)
|
||||
ts = NewTripleStore(tmpDir, nil)
|
||||
ts.AddTripleSet(makeTripleSet())
|
||||
var it graph.Iterator
|
||||
|
||||
|
|
@ -234,7 +234,7 @@ func TestIterator(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestSetIterator(t *testing.T) {
|
||||
var ts *LevelDBTripleStore
|
||||
var ts *TripleStore
|
||||
var tmpDir string
|
||||
|
||||
Convey("Given a prepared database", t, func() {
|
||||
|
|
@ -243,7 +243,7 @@ func TestSetIterator(t *testing.T) {
|
|||
defer os.RemoveAll(tmpDir)
|
||||
ok := CreateNewLevelDB(tmpDir)
|
||||
So(ok, ShouldBeTrue)
|
||||
ts = NewDefaultLevelDBTripleStore(tmpDir, nil)
|
||||
ts = NewTripleStore(tmpDir, nil)
|
||||
ts.AddTripleSet(makeTripleSet())
|
||||
var it graph.Iterator
|
||||
|
||||
|
|
@ -383,7 +383,7 @@ func TestSetIterator(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestOptimize(t *testing.T) {
|
||||
var ts *LevelDBTripleStore
|
||||
var ts *TripleStore
|
||||
var lto graph.Iterator
|
||||
var tmpDir string
|
||||
|
||||
|
|
@ -393,7 +393,7 @@ func TestOptimize(t *testing.T) {
|
|||
defer os.RemoveAll(tmpDir)
|
||||
ok := CreateNewLevelDB(tmpDir)
|
||||
So(ok, ShouldBeTrue)
|
||||
ts = NewDefaultLevelDBTripleStore(tmpDir, nil)
|
||||
ts = NewTripleStore(tmpDir, nil)
|
||||
ts.AddTripleSet(makeTripleSet())
|
||||
|
||||
Convey("With an linksto-fixed pair", func() {
|
||||
|
|
|
|||
|
|
@ -34,7 +34,7 @@ import (
|
|||
const DefaultCacheSize = 2
|
||||
const DefaultWriteBufferSize = 20
|
||||
|
||||
type LevelDBTripleStore struct {
|
||||
type TripleStore struct {
|
||||
dbOpts *leveldb_opt.Options
|
||||
db *leveldb.DB
|
||||
path string
|
||||
|
|
@ -53,7 +53,7 @@ func CreateNewLevelDB(path string) bool {
|
|||
return false
|
||||
}
|
||||
defer db.Close()
|
||||
ts := &LevelDBTripleStore{}
|
||||
ts := &TripleStore{}
|
||||
ts.db = db
|
||||
ts.writeopts = &leveldb_opt.WriteOptions{
|
||||
Sync: true,
|
||||
|
|
@ -62,8 +62,8 @@ func CreateNewLevelDB(path string) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
func NewDefaultLevelDBTripleStore(path string, options graph.OptionsDict) *LevelDBTripleStore {
|
||||
var ts LevelDBTripleStore
|
||||
func NewTripleStore(path string, options graph.OptionsDict) *TripleStore {
|
||||
var ts TripleStore
|
||||
ts.path = path
|
||||
cache_size := DefaultCacheSize
|
||||
if val, ok := options.GetIntKey("cache_size_mb"); ok {
|
||||
|
|
@ -94,7 +94,7 @@ func NewDefaultLevelDBTripleStore(path string, options graph.OptionsDict) *Level
|
|||
return &ts
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) GetStats() string {
|
||||
func (ts *TripleStore) GetStats() string {
|
||||
out := ""
|
||||
stats, err := ts.db.GetProperty("leveldb.stats")
|
||||
if err == nil {
|
||||
|
|
@ -104,11 +104,11 @@ func (ts *LevelDBTripleStore) GetStats() string {
|
|||
return out
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) Size() int64 {
|
||||
func (ts *TripleStore) Size() int64 {
|
||||
return ts.size
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) createKeyFor(dir1, dir2, dir3 string, triple *graph.Triple) []byte {
|
||||
func (ts *TripleStore) createKeyFor(dir1, dir2, dir3 string, triple *graph.Triple) []byte {
|
||||
key := make([]byte, 0, 2+(ts.hasher.Size()*3))
|
||||
key = append(key, []byte(dir1+dir2)...)
|
||||
key = append(key, ts.convertStringToByteHash(triple.Get(dir1))...)
|
||||
|
|
@ -117,7 +117,7 @@ func (ts *LevelDBTripleStore) createKeyFor(dir1, dir2, dir3 string, triple *grap
|
|||
return key
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) createProvKeyFor(dir1, dir2, dir3 string, triple *graph.Triple) []byte {
|
||||
func (ts *TripleStore) createProvKeyFor(dir1, dir2, dir3 string, triple *graph.Triple) []byte {
|
||||
key := make([]byte, 0, 2+(ts.hasher.Size()*4))
|
||||
key = append(key, []byte("c"+dir1)...)
|
||||
key = append(key, ts.convertStringToByteHash(triple.Get("c"))...)
|
||||
|
|
@ -127,14 +127,14 @@ func (ts *LevelDBTripleStore) createProvKeyFor(dir1, dir2, dir3 string, triple *
|
|||
return key
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) createValueKeyFor(s string) []byte {
|
||||
func (ts *TripleStore) createValueKeyFor(s string) []byte {
|
||||
key := make([]byte, 0, 1+ts.hasher.Size())
|
||||
key = append(key, []byte("z")...)
|
||||
key = append(key, ts.convertStringToByteHash(s)...)
|
||||
return key
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) AddTriple(t *graph.Triple) {
|
||||
func (ts *TripleStore) AddTriple(t *graph.Triple) {
|
||||
batch := &leveldb.Batch{}
|
||||
ts.buildWrite(batch, t)
|
||||
err := ts.db.Write(batch, ts.writeopts)
|
||||
|
|
@ -145,7 +145,7 @@ func (ts *LevelDBTripleStore) AddTriple(t *graph.Triple) {
|
|||
ts.size++
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) RemoveTriple(t *graph.Triple) {
|
||||
func (ts *TripleStore) RemoveTriple(t *graph.Triple) {
|
||||
_, err := ts.db.Get(ts.createKeyFor("s", "p", "o", t), ts.readopts)
|
||||
if err != nil && err != leveldb.ErrNotFound {
|
||||
glog.Errorf("Couldn't access DB to confirm deletion")
|
||||
|
|
@ -174,7 +174,7 @@ func (ts *LevelDBTripleStore) RemoveTriple(t *graph.Triple) {
|
|||
ts.size--
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) buildTripleWrite(batch *leveldb.Batch, t *graph.Triple) {
|
||||
func (ts *TripleStore) buildTripleWrite(batch *leveldb.Batch, t *graph.Triple) {
|
||||
bytes, err := json.Marshal(*t)
|
||||
if err != nil {
|
||||
glog.Errorf("Couldn't write to buffer for triple %s\n %s\n", t.ToString(), err)
|
||||
|
|
@ -188,7 +188,7 @@ func (ts *LevelDBTripleStore) buildTripleWrite(batch *leveldb.Batch, t *graph.Tr
|
|||
}
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) buildWrite(batch *leveldb.Batch, t *graph.Triple) {
|
||||
func (ts *TripleStore) buildWrite(batch *leveldb.Batch, t *graph.Triple) {
|
||||
ts.buildTripleWrite(batch, t)
|
||||
ts.UpdateValueKeyBy(t.Get("s"), 1, nil)
|
||||
ts.UpdateValueKeyBy(t.Get("p"), 1, nil)
|
||||
|
|
@ -203,7 +203,7 @@ type ValueData struct {
|
|||
Size int64
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) UpdateValueKeyBy(name string, amount int, batch *leveldb.Batch) {
|
||||
func (ts *TripleStore) UpdateValueKeyBy(name string, amount int, batch *leveldb.Batch) {
|
||||
value := &ValueData{name, int64(amount)}
|
||||
key := ts.createValueKeyFor(name)
|
||||
b, err := ts.db.Get(key, ts.readopts)
|
||||
|
|
@ -249,7 +249,7 @@ func (ts *LevelDBTripleStore) UpdateValueKeyBy(name string, amount int, batch *l
|
|||
}
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) AddTripleSet(t_s []*graph.Triple) {
|
||||
func (ts *TripleStore) AddTripleSet(t_s []*graph.Triple) {
|
||||
batch := &leveldb.Batch{}
|
||||
newTs := len(t_s)
|
||||
resizeMap := make(map[string]int)
|
||||
|
|
@ -273,7 +273,7 @@ func (ts *LevelDBTripleStore) AddTripleSet(t_s []*graph.Triple) {
|
|||
ts.size += int64(newTs)
|
||||
}
|
||||
|
||||
func (ldbts *LevelDBTripleStore) Close() {
|
||||
func (ldbts *TripleStore) Close() {
|
||||
buf := new(bytes.Buffer)
|
||||
err := binary.Write(buf, binary.LittleEndian, ldbts.size)
|
||||
if err == nil {
|
||||
|
|
@ -288,7 +288,7 @@ func (ldbts *LevelDBTripleStore) Close() {
|
|||
ldbts.open = false
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) GetTriple(k graph.TSVal) *graph.Triple {
|
||||
func (ts *TripleStore) GetTriple(k graph.TSVal) *graph.Triple {
|
||||
var triple graph.Triple
|
||||
b, err := ts.db.Get(k.([]byte), ts.readopts)
|
||||
if err != nil && err != leveldb.ErrNotFound {
|
||||
|
|
@ -307,7 +307,7 @@ func (ts *LevelDBTripleStore) GetTriple(k graph.TSVal) *graph.Triple {
|
|||
return &triple
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) convertStringToByteHash(s string) []byte {
|
||||
func (ts *TripleStore) convertStringToByteHash(s string) []byte {
|
||||
ts.hasher.Reset()
|
||||
key := make([]byte, 0, ts.hasher.Size())
|
||||
ts.hasher.Write([]byte(s))
|
||||
|
|
@ -315,11 +315,11 @@ func (ts *LevelDBTripleStore) convertStringToByteHash(s string) []byte {
|
|||
return key
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) GetIdFor(s string) graph.TSVal {
|
||||
func (ts *TripleStore) GetIdFor(s string) graph.TSVal {
|
||||
return ts.createValueKeyFor(s)
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) getValueData(value_key []byte) ValueData {
|
||||
func (ts *TripleStore) getValueData(value_key []byte) ValueData {
|
||||
var out ValueData
|
||||
if glog.V(3) {
|
||||
glog.V(3).Infof("%s %v\n", string(value_key[0]), value_key)
|
||||
|
|
@ -339,7 +339,7 @@ func (ts *LevelDBTripleStore) getValueData(value_key []byte) ValueData {
|
|||
return out
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) GetNameFor(k graph.TSVal) string {
|
||||
func (ts *TripleStore) GetNameFor(k graph.TSVal) string {
|
||||
if k == nil {
|
||||
glog.V(2).Infoln("k was nil")
|
||||
return ""
|
||||
|
|
@ -347,14 +347,14 @@ func (ts *LevelDBTripleStore) GetNameFor(k graph.TSVal) string {
|
|||
return ts.getValueData(k.([]byte)).Name
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) GetSizeFor(k graph.TSVal) int64 {
|
||||
func (ts *TripleStore) GetSizeFor(k graph.TSVal) int64 {
|
||||
if k == nil {
|
||||
return 0
|
||||
}
|
||||
return int64(ts.getValueData(k.([]byte)).Size)
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) getSize() {
|
||||
func (ts *TripleStore) getSize() {
|
||||
var size int64
|
||||
b, err := ts.db.Get([]byte("__size"), ts.readopts)
|
||||
if err != nil && err != leveldb.ErrNotFound {
|
||||
|
|
@ -373,7 +373,7 @@ func (ts *LevelDBTripleStore) getSize() {
|
|||
ts.size = size
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) GetApproximateSizeForPrefix(pre []byte) (int64, error) {
|
||||
func (ts *TripleStore) GetApproximateSizeForPrefix(pre []byte) (int64, error) {
|
||||
limit := make([]byte, len(pre))
|
||||
copy(limit, pre)
|
||||
end := len(limit) - 1
|
||||
|
|
@ -388,29 +388,29 @@ func (ts *LevelDBTripleStore) GetApproximateSizeForPrefix(pre []byte) (int64, er
|
|||
return 0, nil
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) GetTripleIterator(dir string, val graph.TSVal) graph.Iterator {
|
||||
func (ts *TripleStore) GetTripleIterator(dir string, val graph.TSVal) graph.Iterator {
|
||||
switch dir {
|
||||
case "s":
|
||||
return NewLevelDBIterator("sp", "s", val, ts)
|
||||
return NewIterator("sp", "s", val, ts)
|
||||
case "p":
|
||||
return NewLevelDBIterator("po", "p", val, ts)
|
||||
return NewIterator("po", "p", val, ts)
|
||||
case "o":
|
||||
return NewLevelDBIterator("os", "o", val, ts)
|
||||
return NewIterator("os", "o", val, ts)
|
||||
case "c":
|
||||
return NewLevelDBIterator("cp", "c", val, ts)
|
||||
return NewIterator("cp", "c", val, ts)
|
||||
}
|
||||
panic("Notreached " + dir)
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) GetNodesAllIterator() graph.Iterator {
|
||||
func (ts *TripleStore) GetNodesAllIterator() graph.Iterator {
|
||||
return NewLevelDBAllIterator("z", "v", ts)
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) GetTriplesAllIterator() graph.Iterator {
|
||||
func (ts *TripleStore) GetTriplesAllIterator() graph.Iterator {
|
||||
return NewLevelDBAllIterator("po", "p", ts)
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) GetTripleDirection(val graph.TSVal, direction string) graph.TSVal {
|
||||
func (ts *TripleStore) GetTripleDirection(val graph.TSVal, direction string) graph.TSVal {
|
||||
v := val.([]uint8)
|
||||
offset := GetPositionFromPrefix(v[0:2], direction, ts)
|
||||
if offset != -1 {
|
||||
|
|
@ -424,6 +424,6 @@ func compareBytes(a, b graph.TSVal) bool {
|
|||
return bytes.Equal(a.([]uint8), b.([]uint8))
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) MakeFixed() *graph.FixedIterator {
|
||||
func (ts *TripleStore) MakeFixed() *graph.FixedIterator {
|
||||
return graph.NewFixedIteratorWithCompare(compareBytes)
|
||||
}
|
||||
|
|
@ -18,7 +18,7 @@ import (
|
|||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
func (ts *LevelDBTripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
|
||||
func (ts *TripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
|
||||
switch it.Type() {
|
||||
case "linksto":
|
||||
return ts.optimizeLinksTo(it.(*graph.LinksToIterator))
|
||||
|
|
@ -27,7 +27,7 @@ func (ts *LevelDBTripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterato
|
|||
return it, false
|
||||
}
|
||||
|
||||
func (ts *LevelDBTripleStore) optimizeLinksTo(it *graph.LinksToIterator) (graph.Iterator, bool) {
|
||||
func (ts *TripleStore) optimizeLinksTo(it *graph.LinksToIterator) (graph.Iterator, bool) {
|
||||
l := it.GetSubIterators()
|
||||
if l.Len() != 1 {
|
||||
return it, false
|
||||
|
|
@ -18,19 +18,19 @@ import (
|
|||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
type MemstoreAllIterator struct {
|
||||
type AllIterator struct {
|
||||
graph.Int64AllIterator
|
||||
ts *MemTripleStore
|
||||
ts *TripleStore
|
||||
}
|
||||
|
||||
func NewMemstoreAllIterator(ts *MemTripleStore) *MemstoreAllIterator {
|
||||
var out MemstoreAllIterator
|
||||
func NewMemstoreAllIterator(ts *TripleStore) *AllIterator {
|
||||
var out AllIterator
|
||||
out.Int64AllIterator = *graph.NewInt64AllIterator(1, ts.idCounter-1)
|
||||
out.ts = ts
|
||||
return &out
|
||||
}
|
||||
|
||||
func (memall *MemstoreAllIterator) Next() (graph.TSVal, bool) {
|
||||
func (memall *AllIterator) Next() (graph.TSVal, bool) {
|
||||
next, out := memall.Int64AllIterator.Next()
|
||||
if !out {
|
||||
return next, out
|
||||
|
|
@ -24,7 +24,7 @@ import (
|
|||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
type LlrbIterator struct {
|
||||
type Iterator struct {
|
||||
graph.BaseIterator
|
||||
tree *llrb.LLRB
|
||||
data string
|
||||
|
|
@ -51,8 +51,8 @@ func IterateOne(tree *llrb.LLRB, last Int64) Int64 {
|
|||
return next
|
||||
}
|
||||
|
||||
func NewLlrbIterator(tree *llrb.LLRB, data string) *LlrbIterator {
|
||||
var it LlrbIterator
|
||||
func NewLlrbIterator(tree *llrb.LLRB, data string) *Iterator {
|
||||
var it Iterator
|
||||
graph.BaseIteratorInit(&it.BaseIterator)
|
||||
it.tree = tree
|
||||
it.iterLast = Int64(-1)
|
||||
|
|
@ -60,19 +60,19 @@ func NewLlrbIterator(tree *llrb.LLRB, data string) *LlrbIterator {
|
|||
return &it
|
||||
}
|
||||
|
||||
func (it *LlrbIterator) Reset() {
|
||||
func (it *Iterator) Reset() {
|
||||
it.iterLast = Int64(-1)
|
||||
}
|
||||
|
||||
func (it *LlrbIterator) Clone() graph.Iterator {
|
||||
func (it *Iterator) Clone() graph.Iterator {
|
||||
var new_it = NewLlrbIterator(it.tree, it.data)
|
||||
new_it.CopyTagsFrom(it)
|
||||
return new_it
|
||||
}
|
||||
|
||||
func (it *LlrbIterator) Close() {}
|
||||
func (it *Iterator) Close() {}
|
||||
|
||||
func (it *LlrbIterator) Next() (graph.TSVal, bool) {
|
||||
func (it *Iterator) Next() (graph.TSVal, bool) {
|
||||
graph.NextLogIn(it)
|
||||
if it.tree.Max() == nil || it.Last == int64(it.tree.Max().(Int64)) {
|
||||
return graph.NextLogOut(it, nil, false)
|
||||
|
|
@ -82,11 +82,11 @@ func (it *LlrbIterator) Next() (graph.TSVal, bool) {
|
|||
return graph.NextLogOut(it, it.Last, true)
|
||||
}
|
||||
|
||||
func (it *LlrbIterator) Size() (int64, bool) {
|
||||
func (it *Iterator) Size() (int64, bool) {
|
||||
return int64(it.tree.Len()), true
|
||||
}
|
||||
|
||||
func (it *LlrbIterator) Check(v graph.TSVal) bool {
|
||||
func (it *Iterator) Check(v graph.TSVal) bool {
|
||||
graph.CheckLogIn(it, v)
|
||||
if it.tree.Has(Int64(v.(int64))) {
|
||||
it.Last = v
|
||||
|
|
@ -95,22 +95,22 @@ func (it *LlrbIterator) Check(v graph.TSVal) bool {
|
|||
return graph.CheckLogOut(it, v, false)
|
||||
}
|
||||
|
||||
func (it *LlrbIterator) DebugString(indent int) string {
|
||||
func (it *Iterator) DebugString(indent int) string {
|
||||
size, _ := it.Size()
|
||||
return fmt.Sprintf("%s(%s tags:%s size:%d %s)", strings.Repeat(" ", indent), it.Type(), it.Tags(), size, it.data)
|
||||
}
|
||||
|
||||
func (it *LlrbIterator) Type() string {
|
||||
func (it *Iterator) Type() string {
|
||||
return "llrb"
|
||||
}
|
||||
func (it *LlrbIterator) Sorted() bool {
|
||||
func (it *Iterator) Sorted() bool {
|
||||
return true
|
||||
}
|
||||
func (it *LlrbIterator) Optimize() (graph.Iterator, bool) {
|
||||
func (it *Iterator) Optimize() (graph.Iterator, bool) {
|
||||
return it, false
|
||||
}
|
||||
|
||||
func (it *LlrbIterator) GetStats() *graph.IteratorStats {
|
||||
func (it *Iterator) GetStats() *graph.IteratorStats {
|
||||
return &graph.IteratorStats{
|
||||
CheckCost: int64(math.Log(float64(it.tree.Len()))) + 1,
|
||||
NextCost: 1,
|
||||
|
|
@ -28,8 +28,8 @@ import "github.com/google/cayley/graph"
|
|||
// +---+
|
||||
//
|
||||
|
||||
func MakeTestingMemstore() *MemTripleStore {
|
||||
ts := NewMemTripleStore()
|
||||
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", ""))
|
||||
|
|
|
|||
|
|
@ -66,7 +66,7 @@ func (tdi *TripleDirectionIndex) Get(dir string, id int64) (*llrb.LLRB, bool) {
|
|||
return tree, exists
|
||||
}
|
||||
|
||||
type MemTripleStore struct {
|
||||
type TripleStore struct {
|
||||
idCounter int64
|
||||
tripleIdCounter int64
|
||||
idMap map[string]int64
|
||||
|
|
@ -77,8 +77,8 @@ type MemTripleStore struct {
|
|||
// vip_index map[string]map[int64]map[string]map[int64]*llrb.Tree
|
||||
}
|
||||
|
||||
func NewMemTripleStore() *MemTripleStore {
|
||||
var ts MemTripleStore
|
||||
func NewTripleStore() *TripleStore {
|
||||
var ts TripleStore
|
||||
ts.idMap = make(map[string]int64)
|
||||
ts.revIdMap = make(map[int64]string)
|
||||
ts.triples = make([]graph.Triple, 1, 200)
|
||||
|
|
@ -92,13 +92,13 @@ func NewMemTripleStore() *MemTripleStore {
|
|||
return &ts
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) AddTripleSet(triples []*graph.Triple) {
|
||||
func (ts *TripleStore) AddTripleSet(triples []*graph.Triple) {
|
||||
for _, t := range triples {
|
||||
ts.AddTriple(t)
|
||||
}
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) tripleExists(t *graph.Triple) (bool, int64) {
|
||||
func (ts *TripleStore) tripleExists(t *graph.Triple) (bool, int64) {
|
||||
smallest := -1
|
||||
var smallest_tree *llrb.LLRB
|
||||
for _, dir := range graph.TripleDirections {
|
||||
|
|
@ -135,7 +135,7 @@ func (ts *MemTripleStore) tripleExists(t *graph.Triple) (bool, int64) {
|
|||
return false, 0
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) AddTriple(t *graph.Triple) {
|
||||
func (ts *TripleStore) AddTriple(t *graph.Triple) {
|
||||
if exists, _ := ts.tripleExists(t); exists {
|
||||
return
|
||||
}
|
||||
|
|
@ -169,7 +169,7 @@ func (ts *MemTripleStore) AddTriple(t *graph.Triple) {
|
|||
// TODO(barakmich): Add VIP indexing
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) RemoveTriple(t *graph.Triple) {
|
||||
func (ts *TripleStore) RemoveTriple(t *graph.Triple) {
|
||||
var tripleID int64
|
||||
var exists bool
|
||||
tripleID = 0
|
||||
|
|
@ -215,11 +215,11 @@ func (ts *MemTripleStore) RemoveTriple(t *graph.Triple) {
|
|||
}
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) GetTriple(index graph.TSVal) *graph.Triple {
|
||||
func (ts *TripleStore) GetTriple(index graph.TSVal) *graph.Triple {
|
||||
return &ts.triples[index.(int64)]
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) GetTripleIterator(direction string, value graph.TSVal) graph.Iterator {
|
||||
func (ts *TripleStore) GetTripleIterator(direction string, value graph.TSVal) graph.Iterator {
|
||||
index, ok := ts.index.Get(direction, value.(int64))
|
||||
data := fmt.Sprintf("dir:%s val:%d", direction, value.(int64))
|
||||
if ok {
|
||||
|
|
@ -228,11 +228,11 @@ func (ts *MemTripleStore) GetTripleIterator(direction string, value graph.TSVal)
|
|||
return &graph.NullIterator{}
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) Size() int64 {
|
||||
func (ts *TripleStore) Size() int64 {
|
||||
return ts.size - 1 // Don't count the sentinel
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) DebugPrint() {
|
||||
func (ts *TripleStore) DebugPrint() {
|
||||
for i, t := range ts.triples {
|
||||
if i == 0 {
|
||||
continue
|
||||
|
|
@ -241,28 +241,28 @@ func (ts *MemTripleStore) DebugPrint() {
|
|||
}
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) GetIdFor(name string) graph.TSVal {
|
||||
func (ts *TripleStore) GetIdFor(name string) graph.TSVal {
|
||||
return ts.idMap[name]
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) GetNameFor(id graph.TSVal) string {
|
||||
func (ts *TripleStore) GetNameFor(id graph.TSVal) string {
|
||||
return ts.revIdMap[id.(int64)]
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) GetTriplesAllIterator() graph.Iterator {
|
||||
func (ts *TripleStore) GetTriplesAllIterator() graph.Iterator {
|
||||
return graph.NewInt64AllIterator(0, ts.Size())
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) MakeFixed() *graph.FixedIterator {
|
||||
func (ts *TripleStore) MakeFixed() *graph.FixedIterator {
|
||||
return graph.NewFixedIteratorWithCompare(graph.BasicEquality)
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) GetTripleDirection(val graph.TSVal, direction string) graph.TSVal {
|
||||
func (ts *TripleStore) GetTripleDirection(val graph.TSVal, direction string) graph.TSVal {
|
||||
name := ts.GetTriple(val).Get(direction)
|
||||
return ts.GetIdFor(name)
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) GetNodesAllIterator() graph.Iterator {
|
||||
func (ts *TripleStore) GetNodesAllIterator() graph.Iterator {
|
||||
return NewMemstoreAllIterator(ts)
|
||||
}
|
||||
func (ts *MemTripleStore) Close() {}
|
||||
func (ts *TripleStore) Close() {}
|
||||
|
|
@ -18,7 +18,7 @@ import (
|
|||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
func (ts *MemTripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
|
||||
func (ts *TripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
|
||||
switch it.Type() {
|
||||
case "linksto":
|
||||
return ts.optimizeLinksTo(it.(*graph.LinksToIterator))
|
||||
|
|
@ -27,7 +27,7 @@ func (ts *MemTripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, b
|
|||
return it, false
|
||||
}
|
||||
|
||||
func (ts *MemTripleStore) optimizeLinksTo(it *graph.LinksToIterator) (graph.Iterator, bool) {
|
||||
func (ts *TripleStore) optimizeLinksTo(it *graph.LinksToIterator) (graph.Iterator, bool) {
|
||||
l := it.GetSubIterators()
|
||||
if l.Len() != 1 {
|
||||
return it, false
|
||||
|
|
@ -107,7 +107,7 @@ func TestLinksToOptimization(t *testing.T) {
|
|||
if newIt.Type() != "llrb" {
|
||||
t.Fatal("Didn't swap out to LLRB")
|
||||
}
|
||||
v := newIt.(*LlrbIterator)
|
||||
v := newIt.(*Iterator)
|
||||
v_clone := v.Clone()
|
||||
if v_clone.DebugString(0) != v.DebugString(0) {
|
||||
t.Fatal("Wrong iterator. Got ", v_clone.DebugString(0))
|
||||
|
|
@ -25,9 +25,9 @@ import (
|
|||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
type MongoIterator struct {
|
||||
type Iterator struct {
|
||||
graph.BaseIterator
|
||||
ts *MongoTripleStore
|
||||
ts *TripleStore
|
||||
dir string
|
||||
iter *mgo.Iter
|
||||
hash string
|
||||
|
|
@ -38,8 +38,8 @@ type MongoIterator struct {
|
|||
collection string
|
||||
}
|
||||
|
||||
func NewMongoIterator(ts *MongoTripleStore, collection string, dir string, val graph.TSVal) *MongoIterator {
|
||||
var m MongoIterator
|
||||
func NewMongoIterator(ts *TripleStore, collection string, dir string, val graph.TSVal) *Iterator {
|
||||
var m Iterator
|
||||
graph.BaseIteratorInit(&m.BaseIterator)
|
||||
|
||||
m.name = ts.GetNameFor(val)
|
||||
|
|
@ -70,8 +70,8 @@ func NewMongoIterator(ts *MongoTripleStore, collection string, dir string, val g
|
|||
return &m
|
||||
}
|
||||
|
||||
func NewMongoAllIterator(ts *MongoTripleStore, collection string) *MongoIterator {
|
||||
var m MongoIterator
|
||||
func NewMongoAllIterator(ts *TripleStore, collection string) *Iterator {
|
||||
var m Iterator
|
||||
m.ts = ts
|
||||
m.dir = "all"
|
||||
m.constraint = nil
|
||||
|
|
@ -88,17 +88,17 @@ func NewMongoAllIterator(ts *MongoTripleStore, collection string) *MongoIterator
|
|||
return &m
|
||||
}
|
||||
|
||||
func (m *MongoIterator) Reset() {
|
||||
func (m *Iterator) Reset() {
|
||||
m.iter.Close()
|
||||
m.iter = m.ts.db.C(m.collection).Find(m.constraint).Iter()
|
||||
|
||||
}
|
||||
|
||||
func (m *MongoIterator) Close() {
|
||||
func (m *Iterator) Close() {
|
||||
m.iter.Close()
|
||||
}
|
||||
|
||||
func (m *MongoIterator) Clone() graph.Iterator {
|
||||
func (m *Iterator) Clone() graph.Iterator {
|
||||
var newM graph.Iterator
|
||||
if m.isAll {
|
||||
newM = NewMongoAllIterator(m.ts, m.collection)
|
||||
|
|
@ -109,7 +109,7 @@ func (m *MongoIterator) Clone() graph.Iterator {
|
|||
return newM
|
||||
}
|
||||
|
||||
func (m *MongoIterator) Next() (graph.TSVal, bool) {
|
||||
func (m *Iterator) Next() (graph.TSVal, bool) {
|
||||
var result struct {
|
||||
Id string "_id"
|
||||
//Sub string "Sub"
|
||||
|
|
@ -120,7 +120,7 @@ func (m *MongoIterator) Next() (graph.TSVal, bool) {
|
|||
if !found {
|
||||
err := m.iter.Err()
|
||||
if err != nil {
|
||||
glog.Errorln("Error Nexting MongoIterator: ", err)
|
||||
glog.Errorln("Error Nexting Iterator: ", err)
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
|
@ -128,7 +128,7 @@ func (m *MongoIterator) Next() (graph.TSVal, bool) {
|
|||
return result.Id, true
|
||||
}
|
||||
|
||||
func (m *MongoIterator) Check(v graph.TSVal) bool {
|
||||
func (m *Iterator) Check(v graph.TSVal) bool {
|
||||
graph.CheckLogIn(m, v)
|
||||
if m.isAll {
|
||||
m.Last = v
|
||||
|
|
@ -153,25 +153,25 @@ func (m *MongoIterator) Check(v graph.TSVal) bool {
|
|||
return graph.CheckLogOut(m, v, false)
|
||||
}
|
||||
|
||||
func (m *MongoIterator) Size() (int64, bool) {
|
||||
func (m *Iterator) Size() (int64, bool) {
|
||||
return m.size, true
|
||||
}
|
||||
|
||||
func (m *MongoIterator) Type() string {
|
||||
func (m *Iterator) Type() string {
|
||||
if m.isAll {
|
||||
return "all"
|
||||
}
|
||||
return "mongo"
|
||||
}
|
||||
func (m *MongoIterator) Sorted() bool { return true }
|
||||
func (m *MongoIterator) Optimize() (graph.Iterator, bool) { return m, false }
|
||||
func (m *Iterator) Sorted() bool { return true }
|
||||
func (m *Iterator) Optimize() (graph.Iterator, bool) { return m, false }
|
||||
|
||||
func (m *MongoIterator) DebugString(indent int) string {
|
||||
func (m *Iterator) DebugString(indent int) string {
|
||||
size, _ := m.Size()
|
||||
return fmt.Sprintf("%s(%s size:%d %s %s)", strings.Repeat(" ", indent), m.Type(), size, m.hash, m.name)
|
||||
}
|
||||
|
||||
func (m *MongoIterator) GetStats() *graph.IteratorStats {
|
||||
func (m *Iterator) GetStats() *graph.IteratorStats {
|
||||
size, _ := m.Size()
|
||||
return &graph.IteratorStats{
|
||||
CheckCost: 1,
|
||||
|
|
@ -29,7 +29,7 @@ import (
|
|||
|
||||
const DefaultDBName = "cayley"
|
||||
|
||||
type MongoTripleStore struct {
|
||||
type TripleStore struct {
|
||||
session *mgo.Session
|
||||
db *mgo.Database
|
||||
hasher hash.Hash
|
||||
|
|
@ -65,8 +65,8 @@ func CreateNewMongoGraph(addr string, options graph.OptionsDict) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
func NewMongoTripleStore(addr string, options graph.OptionsDict) *MongoTripleStore {
|
||||
var ts MongoTripleStore
|
||||
func NewTripleStore(addr string, options graph.OptionsDict) *TripleStore {
|
||||
var ts TripleStore
|
||||
conn, err := mgo.Dial(addr)
|
||||
if err != nil {
|
||||
glog.Fatal("Error connecting: ", err)
|
||||
|
|
@ -83,7 +83,7 @@ func NewMongoTripleStore(addr string, options graph.OptionsDict) *MongoTripleSto
|
|||
return &ts
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) getIdForTriple(t *graph.Triple) string {
|
||||
func (ts *TripleStore) getIdForTriple(t *graph.Triple) string {
|
||||
id := ts.ConvertStringToByteHash(t.Sub)
|
||||
id += ts.ConvertStringToByteHash(t.Pred)
|
||||
id += ts.ConvertStringToByteHash(t.Obj)
|
||||
|
|
@ -91,7 +91,7 @@ func (ts *MongoTripleStore) getIdForTriple(t *graph.Triple) string {
|
|||
return id
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) ConvertStringToByteHash(s string) string {
|
||||
func (ts *TripleStore) ConvertStringToByteHash(s string) string {
|
||||
ts.hasher.Reset()
|
||||
key := make([]byte, 0, ts.hasher.Size())
|
||||
ts.hasher.Write([]byte(s))
|
||||
|
|
@ -105,7 +105,7 @@ type MongoNode struct {
|
|||
Size int "Size"
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) updateNodeBy(node_name string, inc int) {
|
||||
func (ts *TripleStore) updateNodeBy(node_name string, inc int) {
|
||||
var size MongoNode
|
||||
node := ts.GetIdFor(node_name)
|
||||
err := ts.db.C("nodes").FindId(node).One(&size)
|
||||
|
|
@ -142,7 +142,7 @@ func (ts *MongoTripleStore) updateNodeBy(node_name string, inc int) {
|
|||
}
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) writeTriple(t *graph.Triple) bool {
|
||||
func (ts *TripleStore) writeTriple(t *graph.Triple) bool {
|
||||
tripledoc := bson.M{"_id": ts.getIdForTriple(t), "Sub": t.Sub, "Pred": t.Pred, "Obj": t.Obj, "Provenance": t.Provenance}
|
||||
err := ts.db.C("triples").Insert(tripledoc)
|
||||
if err != nil {
|
||||
|
|
@ -156,7 +156,7 @@ func (ts *MongoTripleStore) writeTriple(t *graph.Triple) bool {
|
|||
return true
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) AddTriple(t *graph.Triple) {
|
||||
func (ts *TripleStore) AddTriple(t *graph.Triple) {
|
||||
_ = ts.writeTriple(t)
|
||||
ts.updateNodeBy(t.Sub, 1)
|
||||
ts.updateNodeBy(t.Pred, 1)
|
||||
|
|
@ -166,7 +166,7 @@ func (ts *MongoTripleStore) AddTriple(t *graph.Triple) {
|
|||
}
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) AddTripleSet(in []*graph.Triple) {
|
||||
func (ts *TripleStore) AddTripleSet(in []*graph.Triple) {
|
||||
ts.session.SetSafe(nil)
|
||||
idMap := make(map[string]int)
|
||||
for _, t := range in {
|
||||
|
|
@ -186,7 +186,7 @@ func (ts *MongoTripleStore) AddTripleSet(in []*graph.Triple) {
|
|||
ts.session.SetSafe(&mgo.Safe{})
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) RemoveTriple(t *graph.Triple) {
|
||||
func (ts *TripleStore) RemoveTriple(t *graph.Triple) {
|
||||
err := ts.db.C("triples").RemoveId(ts.getIdForTriple(t))
|
||||
if err == mgo.ErrNotFound {
|
||||
return
|
||||
|
|
@ -202,7 +202,7 @@ func (ts *MongoTripleStore) RemoveTriple(t *graph.Triple) {
|
|||
}
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) GetTriple(val graph.TSVal) *graph.Triple {
|
||||
func (ts *TripleStore) GetTriple(val graph.TSVal) *graph.Triple {
|
||||
var bsonDoc bson.M
|
||||
err := ts.db.C("triples").FindId(val.(string)).One(&bsonDoc)
|
||||
if err != nil {
|
||||
|
|
@ -215,23 +215,23 @@ func (ts *MongoTripleStore) GetTriple(val graph.TSVal) *graph.Triple {
|
|||
bsonDoc["Provenance"].(string))
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) GetTripleIterator(dir string, val graph.TSVal) graph.Iterator {
|
||||
func (ts *TripleStore) GetTripleIterator(dir string, val graph.TSVal) graph.Iterator {
|
||||
return NewMongoIterator(ts, "triples", dir, val)
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) GetNodesAllIterator() graph.Iterator {
|
||||
func (ts *TripleStore) GetNodesAllIterator() graph.Iterator {
|
||||
return NewMongoAllIterator(ts, "nodes")
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) GetTriplesAllIterator() graph.Iterator {
|
||||
func (ts *TripleStore) GetTriplesAllIterator() graph.Iterator {
|
||||
return NewMongoAllIterator(ts, "triples")
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) GetIdFor(s string) graph.TSVal {
|
||||
func (ts *TripleStore) GetIdFor(s string) graph.TSVal {
|
||||
return ts.ConvertStringToByteHash(s)
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) GetNameFor(v graph.TSVal) string {
|
||||
func (ts *TripleStore) GetNameFor(v graph.TSVal) string {
|
||||
val, ok := ts.idCache.Get(v.(string))
|
||||
if ok {
|
||||
return val
|
||||
|
|
@ -245,7 +245,7 @@ func (ts *MongoTripleStore) GetNameFor(v graph.TSVal) string {
|
|||
return node.Name
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) Size() int64 {
|
||||
func (ts *TripleStore) Size() int64 {
|
||||
count, err := ts.db.C("triples").Count()
|
||||
if err != nil {
|
||||
glog.Error("Error: ", err)
|
||||
|
|
@ -258,15 +258,15 @@ func compareStrings(a, b graph.TSVal) bool {
|
|||
return a.(string) == b.(string)
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) MakeFixed() *graph.FixedIterator {
|
||||
func (ts *TripleStore) MakeFixed() *graph.FixedIterator {
|
||||
return graph.NewFixedIteratorWithCompare(compareStrings)
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) Close() {
|
||||
func (ts *TripleStore) Close() {
|
||||
ts.db.Session.Close()
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) GetTripleDirection(in graph.TSVal, dir string) graph.TSVal {
|
||||
func (ts *TripleStore) GetTripleDirection(in graph.TSVal, dir string) graph.TSVal {
|
||||
// Maybe do the trick here
|
||||
var offset int
|
||||
switch dir {
|
||||
|
|
@ -283,7 +283,7 @@ func (ts *MongoTripleStore) GetTripleDirection(in graph.TSVal, dir string) graph
|
|||
return val
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) BulkLoad(t_chan chan *graph.Triple) {
|
||||
func (ts *TripleStore) BulkLoad(t_chan chan *graph.Triple) {
|
||||
ts.session.SetSafe(nil)
|
||||
for triple := range t_chan {
|
||||
ts.writeTriple(triple)
|
||||
|
|
@ -18,7 +18,7 @@ import (
|
|||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
func (ts *MongoTripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
|
||||
func (ts *TripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator, bool) {
|
||||
switch it.Type() {
|
||||
case "linksto":
|
||||
return ts.optimizeLinksTo(it.(*graph.LinksToIterator))
|
||||
|
|
@ -27,7 +27,7 @@ func (ts *MongoTripleStore) OptimizeIterator(it graph.Iterator) (graph.Iterator,
|
|||
return it, false
|
||||
}
|
||||
|
||||
func (ts *MongoTripleStore) optimizeLinksTo(it *graph.LinksToIterator) (graph.Iterator, bool) {
|
||||
func (ts *TripleStore) optimizeLinksTo(it *graph.LinksToIterator) (graph.Iterator, bool) {
|
||||
l := it.GetSubIterators()
|
||||
if l.Len() != 1 {
|
||||
return it, false
|
||||
|
|
@ -32,7 +32,7 @@ func TestBadParse(t *testing.T) {
|
|||
|
||||
func TestParseSexpWithMemstore(t *testing.T) {
|
||||
Convey("With a Memstore", t, func() {
|
||||
ts := memstore.NewMemTripleStore()
|
||||
ts := memstore.NewTripleStore()
|
||||
|
||||
Convey("It should parse an empty query", func() {
|
||||
it := BuildIteratorTreeForQuery(ts, "()")
|
||||
|
|
@ -64,7 +64,7 @@ func TestParseSexpWithMemstore(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestTreeConstraintParse(t *testing.T) {
|
||||
ts := memstore.NewMemTripleStore()
|
||||
ts := memstore.NewTripleStore()
|
||||
ts.AddTriple(graph.MakeTriple("i", "like", "food", ""))
|
||||
ts.AddTriple(graph.MakeTriple("food", "is", "good", ""))
|
||||
query := "(\"i\"\n" +
|
||||
|
|
@ -84,7 +84,7 @@ func TestTreeConstraintParse(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestTreeConstraintTagParse(t *testing.T) {
|
||||
ts := memstore.NewMemTripleStore()
|
||||
ts := memstore.NewTripleStore()
|
||||
ts.AddTriple(graph.MakeTriple("i", "like", "food", ""))
|
||||
ts.AddTriple(graph.MakeTriple("food", "is", "good", ""))
|
||||
query := "(\"i\"\n" +
|
||||
|
|
@ -104,7 +104,7 @@ func TestTreeConstraintTagParse(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestMultipleConstraintParse(t *testing.T) {
|
||||
ts := memstore.NewMemTripleStore()
|
||||
ts := memstore.NewTripleStore()
|
||||
ts.AddTriple(graph.MakeTriple("i", "like", "food", ""))
|
||||
ts.AddTriple(graph.MakeTriple("i", "like", "beer", ""))
|
||||
ts.AddTriple(graph.MakeTriple("you", "like", "beer", ""))
|
||||
|
|
|
|||
|
|
@ -24,22 +24,22 @@ import (
|
|||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
type SexpSession struct {
|
||||
type Session struct {
|
||||
ts graph.TripleStore
|
||||
debug bool
|
||||
}
|
||||
|
||||
func NewSexpSession(inputTripleStore graph.TripleStore) *SexpSession {
|
||||
var s SexpSession
|
||||
func NewSession(inputTripleStore graph.TripleStore) *Session {
|
||||
var s Session
|
||||
s.ts = inputTripleStore
|
||||
return &s
|
||||
}
|
||||
|
||||
func (s *SexpSession) ToggleDebug() {
|
||||
func (s *Session) ToggleDebug() {
|
||||
s.debug = !s.debug
|
||||
}
|
||||
|
||||
func (s *SexpSession) InputParses(input string) (graph.ParseResult, error) {
|
||||
func (s *Session) InputParses(input string) (graph.ParseResult, error) {
|
||||
var parenDepth int
|
||||
for i, x := range input {
|
||||
if x == '(' {
|
||||
|
|
@ -65,7 +65,7 @@ func (s *SexpSession) InputParses(input string) (graph.ParseResult, error) {
|
|||
return graph.ParseFail, errors.New("Invalid Syntax")
|
||||
}
|
||||
|
||||
func (s *SexpSession) ExecInput(input string, out chan interface{}, limit int) {
|
||||
func (s *Session) ExecInput(input string, out chan interface{}, limit int) {
|
||||
it := BuildIteratorTreeForQuery(s.ts, input)
|
||||
newIt, changed := it.Optimize()
|
||||
if changed {
|
||||
|
|
@ -101,7 +101,7 @@ func (s *SexpSession) ExecInput(input string, out chan interface{}, limit int) {
|
|||
close(out)
|
||||
}
|
||||
|
||||
func (s *SexpSession) ToText(result interface{}) string {
|
||||
func (s *Session) ToText(result interface{}) string {
|
||||
out := fmt.Sprintln("****")
|
||||
tags := result.(*map[string]graph.TSVal)
|
||||
tagKeys := make([]string, len(*tags))
|
||||
|
|
@ -106,7 +106,7 @@ func (h *TemplateRequestHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
|
|||
}
|
||||
|
||||
type Api struct {
|
||||
config *config.CayleyConfig
|
||||
config *config.Config
|
||||
ts graph.TripleStore
|
||||
}
|
||||
|
||||
|
|
@ -119,7 +119,7 @@ func (api *Api) ApiV1(r *httprouter.Router) {
|
|||
r.POST("/api/v1/delete", LogRequest(api.ServeV1Delete))
|
||||
}
|
||||
|
||||
func SetupRoutes(ts graph.TripleStore, cfg *config.CayleyConfig) {
|
||||
func SetupRoutes(ts graph.TripleStore, cfg *config.Config) {
|
||||
r := httprouter.New()
|
||||
assets := findAssetsPath()
|
||||
if glog.V(2) {
|
||||
|
|
@ -141,7 +141,7 @@ func SetupRoutes(ts graph.TripleStore, cfg *config.CayleyConfig) {
|
|||
http.Handle("/", r)
|
||||
}
|
||||
|
||||
func CayleyHTTP(ts graph.TripleStore, cfg *config.CayleyConfig) {
|
||||
func Serve(ts graph.TripleStore, cfg *config.Config) {
|
||||
SetupRoutes(ts, cfg)
|
||||
glog.Infof("Cayley now listening on %s:%s\n", cfg.ListenHost, cfg.ListenPort)
|
||||
fmt.Printf("Cayley now listening on %s:%s\n", cfg.ListenHost, cfg.ListenPort)
|
||||
|
|
@ -71,9 +71,9 @@ func (api *Api) ServeV1Query(w http.ResponseWriter, r *http.Request, params http
|
|||
var ses graph.HttpSession
|
||||
switch params.ByName("query_lang") {
|
||||
case "gremlin":
|
||||
ses = gremlin.NewGremlinSession(api.ts, api.config.GremlinTimeout, false)
|
||||
ses = gremlin.NewSession(api.ts, api.config.GremlinTimeout, false)
|
||||
case "mql":
|
||||
ses = mql.NewMqlSession(api.ts)
|
||||
ses = mql.NewSession(api.ts)
|
||||
default:
|
||||
return FormatJson400(w, "Need a query language.")
|
||||
}
|
||||
|
|
@ -120,9 +120,9 @@ func (api *Api) ServeV1Shape(w http.ResponseWriter, r *http.Request, params http
|
|||
var ses graph.HttpSession
|
||||
switch params.ByName("query_lang") {
|
||||
case "gremlin":
|
||||
ses = gremlin.NewGremlinSession(api.ts, api.config.GremlinTimeout, false)
|
||||
ses = gremlin.NewSession(api.ts, api.config.GremlinTimeout, false)
|
||||
case "mql":
|
||||
ses = mql.NewMqlSession(api.ts)
|
||||
ses = mql.NewSession(api.ts)
|
||||
default:
|
||||
return FormatJson400(w, "Need a query language.")
|
||||
}
|
||||
|
|
@ -27,7 +27,7 @@ import (
|
|||
func isWhitespace(s uint8) bool {
|
||||
return (s == '\t' || s == '\r' || s == ' ')
|
||||
}
|
||||
func ParseLineToTriple(str string) *graph.Triple {
|
||||
func Parse(str string) *graph.Triple {
|
||||
// Skip leading whitespace.
|
||||
str = skipWhitespace(str)
|
||||
// Check for a comment
|
||||
|
|
@ -184,7 +184,7 @@ func ReadNQuadsFromReader(c chan *graph.Triple, reader io.Reader) {
|
|||
if pre {
|
||||
continue
|
||||
}
|
||||
triple := ParseLineToTriple(line)
|
||||
triple := Parse(line)
|
||||
line = ""
|
||||
if triple != nil {
|
||||
nTriples++
|
||||
|
|
|
|||
|
|
@ -25,61 +25,61 @@ import (
|
|||
func TestParsingNTriples(t *testing.T) {
|
||||
Convey("When parsing", t, func() {
|
||||
Convey("It should not parse invalid triples", func() {
|
||||
x := ParseLineToTriple("invalid")
|
||||
x := Parse("invalid")
|
||||
So(x, ShouldBeNil)
|
||||
})
|
||||
Convey("It should not parse comments", func() {
|
||||
x := ParseLineToTriple("# nominally valid triple .")
|
||||
x := Parse("# nominally valid triple .")
|
||||
So(x, ShouldBeNil)
|
||||
})
|
||||
Convey("It should parse simple triples", func() {
|
||||
x := ParseLineToTriple("this is valid .")
|
||||
x := Parse("this is valid .")
|
||||
So(x, ShouldNotBeNil)
|
||||
So(x.Sub, ShouldEqual, "this")
|
||||
})
|
||||
Convey("It should parse quoted triples", func() {
|
||||
x := ParseLineToTriple("this is \"valid too\" .")
|
||||
x := Parse("this is \"valid too\" .")
|
||||
So(x, ShouldNotBeNil)
|
||||
So(x.Obj, ShouldEqual, "valid too")
|
||||
So(x.Provenance, ShouldEqual, "")
|
||||
})
|
||||
Convey("It should parse escaped quoted triples", func() {
|
||||
x := ParseLineToTriple("he said \"\\\"That's all folks\\\"\" .")
|
||||
x := Parse("he said \"\\\"That's all folks\\\"\" .")
|
||||
So(x, ShouldNotBeNil)
|
||||
So(x.Obj, ShouldEqual, "\"That's all folks\"")
|
||||
So(x.Provenance, ShouldEqual, "")
|
||||
})
|
||||
|
||||
Convey("It should parse an example real triple", func() {
|
||||
x := ParseLineToTriple("\":/guid/9202a8c04000641f80000000010c843c\" \"name\" \"George Morris\" .")
|
||||
x := Parse("\":/guid/9202a8c04000641f80000000010c843c\" \"name\" \"George Morris\" .")
|
||||
So(x, ShouldNotBeNil)
|
||||
So(x.Obj, ShouldEqual, "George Morris")
|
||||
So(x.Provenance, ShouldEqual, "")
|
||||
})
|
||||
|
||||
Convey("It should parse a pathologically spaced triple", func() {
|
||||
x := ParseLineToTriple("foo is \"\\tA big tough\\r\\nDeal\\\\\" .")
|
||||
x := Parse("foo is \"\\tA big tough\\r\\nDeal\\\\\" .")
|
||||
So(x, ShouldNotBeNil)
|
||||
So(x.Obj, ShouldEqual, "\tA big tough\r\nDeal\\")
|
||||
So(x.Provenance, ShouldEqual, "")
|
||||
})
|
||||
|
||||
Convey("It should parse a simple quad", func() {
|
||||
x := ParseLineToTriple("this is valid quad .")
|
||||
x := Parse("this is valid quad .")
|
||||
So(x, ShouldNotBeNil)
|
||||
So(x.Obj, ShouldEqual, "valid")
|
||||
So(x.Provenance, ShouldEqual, "quad")
|
||||
})
|
||||
|
||||
Convey("It should parse a quoted quad", func() {
|
||||
x := ParseLineToTriple("this is valid \"quad thing\" .")
|
||||
x := Parse("this is valid \"quad thing\" .")
|
||||
So(x, ShouldNotBeNil)
|
||||
So(x.Obj, ShouldEqual, "valid")
|
||||
So(x.Provenance, ShouldEqual, "quad thing")
|
||||
})
|
||||
|
||||
Convey("It should parse crazy escaped quads", func() {
|
||||
x := ParseLineToTriple("\"\\\"this\" \"\\\"is\" \"\\\"valid\" \"\\\"quad thing\".")
|
||||
x := Parse("\"\\\"this\" \"\\\"is\" \"\\\"valid\" \"\\\"quad thing\".")
|
||||
So(x, ShouldNotBeNil)
|
||||
So(x.Sub, ShouldEqual, "\"this")
|
||||
So(x.Pred, ShouldEqual, "\"is")
|
||||
|
|
@ -93,27 +93,27 @@ func TestParsingNTriplesOfficial(t *testing.T) {
|
|||
Convey("When using some public test cases...", t, func() {
|
||||
Convey("It should handle some simple cases with comments", func() {
|
||||
var x *graph.Triple
|
||||
x = ParseLineToTriple("<http://example/s> <http://example/p> <http://example/o> . # comment")
|
||||
x = Parse("<http://example/s> <http://example/p> <http://example/o> . # comment")
|
||||
So(x, ShouldNotBeNil)
|
||||
So(x.Sub, ShouldEqual, "http://example/s")
|
||||
So(x.Pred, ShouldEqual, "http://example/p")
|
||||
So(x.Obj, ShouldEqual, "http://example/o")
|
||||
So(x.Provenance, ShouldEqual, "")
|
||||
x = ParseLineToTriple("<http://example/s> <http://example/p> _:o . # comment")
|
||||
x = Parse("<http://example/s> <http://example/p> _:o . # comment")
|
||||
So(x, ShouldNotBeNil)
|
||||
So(x.Sub, ShouldEqual, "http://example/s")
|
||||
So(x.Pred, ShouldEqual, "http://example/p")
|
||||
So(x.Obj, ShouldEqual, "_:o")
|
||||
So(x.Provenance, ShouldEqual, "")
|
||||
x = ParseLineToTriple("<http://example/s> <http://example/p> \"o\" . # comment")
|
||||
x = Parse("<http://example/s> <http://example/p> \"o\" . # comment")
|
||||
So(x, ShouldNotBeNil)
|
||||
So(x.Obj, ShouldEqual, "o")
|
||||
So(x.Provenance, ShouldEqual, "")
|
||||
x = ParseLineToTriple("<http://example/s> <http://example/p> \"o\"^^<http://example/dt> . # comment")
|
||||
x = Parse("<http://example/s> <http://example/p> \"o\"^^<http://example/dt> . # comment")
|
||||
So(x, ShouldNotBeNil)
|
||||
So(x.Obj, ShouldEqual, "o")
|
||||
So(x.Provenance, ShouldEqual, "")
|
||||
x = ParseLineToTriple("<http://example/s> <http://example/p> \"o\"@en . # comment")
|
||||
x = Parse("<http://example/s> <http://example/p> \"o\"@en . # comment")
|
||||
So(x, ShouldNotBeNil)
|
||||
So(x.Obj, ShouldEqual, "o")
|
||||
So(x.Provenance, ShouldEqual, "")
|
||||
|
|
@ -123,7 +123,7 @@ func TestParsingNTriplesOfficial(t *testing.T) {
|
|||
|
||||
func BenchmarkParser(b *testing.B) {
|
||||
for n := 0; n < b.N; n++ {
|
||||
x := ParseLineToTriple("<http://example/s> <http://example/p> \"object of some real\\tlength\"@en . # comment")
|
||||
x := Parse("<http://example/s> <http://example/p> \"object of some real\\tlength\"@en . # comment")
|
||||
if x.Obj != "object of some real\tlength" {
|
||||
b.Fail()
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,7 +21,7 @@ import (
|
|||
"github.com/robertkrimen/otto"
|
||||
)
|
||||
|
||||
func BuildGremlinEnv(ses *GremlinSession) *otto.Otto {
|
||||
func BuildEnviron(ses *Session) *otto.Otto {
|
||||
env := otto.New()
|
||||
setupGremlin(env, ses)
|
||||
return env
|
||||
|
|
@ -55,7 +55,7 @@ func isVertexChain(obj *otto.Object) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
func setupGremlin(env *otto.Otto, ses *GremlinSession) {
|
||||
func setupGremlin(env *otto.Otto, ses *Session) {
|
||||
graph, _ := env.Object("graph = {}")
|
||||
graph.Set("Vertex", func(call otto.FunctionCall) otto.Value {
|
||||
call.Otto.Run("var out = {}")
|
||||
|
|
@ -21,9 +21,9 @@ import (
|
|||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
const GremlinTopResultTag = "id"
|
||||
const TopResultTag = "id"
|
||||
|
||||
func embedFinals(env *otto.Otto, ses *GremlinSession, obj *otto.Object) {
|
||||
func embedFinals(env *otto.Otto, ses *Session, obj *otto.Object) {
|
||||
obj.Set("All", allFunc(env, ses, obj))
|
||||
obj.Set("GetLimit", limitFunc(env, ses, obj))
|
||||
obj.Set("ToArray", toArrayFunc(env, ses, obj, false))
|
||||
|
|
@ -34,10 +34,10 @@ func embedFinals(env *otto.Otto, ses *GremlinSession, obj *otto.Object) {
|
|||
obj.Set("ForEach", mapFunc(env, ses, obj))
|
||||
}
|
||||
|
||||
func allFunc(env *otto.Otto, ses *GremlinSession, obj *otto.Object) func(otto.FunctionCall) otto.Value {
|
||||
func allFunc(env *otto.Otto, ses *Session, obj *otto.Object) func(otto.FunctionCall) otto.Value {
|
||||
return func(call otto.FunctionCall) otto.Value {
|
||||
it := buildIteratorTree(obj, ses.ts)
|
||||
it.AddTag(GremlinTopResultTag)
|
||||
it.AddTag(TopResultTag)
|
||||
ses.limit = -1
|
||||
ses.count = 0
|
||||
runIteratorOnSession(it, ses)
|
||||
|
|
@ -45,12 +45,12 @@ func allFunc(env *otto.Otto, ses *GremlinSession, obj *otto.Object) func(otto.Fu
|
|||
}
|
||||
}
|
||||
|
||||
func limitFunc(env *otto.Otto, ses *GremlinSession, obj *otto.Object) func(otto.FunctionCall) otto.Value {
|
||||
func limitFunc(env *otto.Otto, ses *Session, obj *otto.Object) func(otto.FunctionCall) otto.Value {
|
||||
return func(call otto.FunctionCall) otto.Value {
|
||||
if len(call.ArgumentList) > 0 {
|
||||
limitVal, _ := call.Argument(0).ToInteger()
|
||||
it := buildIteratorTree(obj, ses.ts)
|
||||
it.AddTag(GremlinTopResultTag)
|
||||
it.AddTag(TopResultTag)
|
||||
ses.limit = int(limitVal)
|
||||
ses.count = 0
|
||||
runIteratorOnSession(it, ses)
|
||||
|
|
@ -59,10 +59,10 @@ func limitFunc(env *otto.Otto, ses *GremlinSession, obj *otto.Object) func(otto.
|
|||
}
|
||||
}
|
||||
|
||||
func toArrayFunc(env *otto.Otto, ses *GremlinSession, obj *otto.Object, withTags bool) func(otto.FunctionCall) otto.Value {
|
||||
func toArrayFunc(env *otto.Otto, ses *Session, obj *otto.Object, withTags bool) func(otto.FunctionCall) otto.Value {
|
||||
return func(call otto.FunctionCall) otto.Value {
|
||||
it := buildIteratorTree(obj, ses.ts)
|
||||
it.AddTag(GremlinTopResultTag)
|
||||
it.AddTag(TopResultTag)
|
||||
limit := -1
|
||||
if len(call.ArgumentList) > 0 {
|
||||
limitParsed, _ := call.Argument(0).ToInteger()
|
||||
|
|
@ -86,10 +86,10 @@ func toArrayFunc(env *otto.Otto, ses *GremlinSession, obj *otto.Object, withTags
|
|||
}
|
||||
}
|
||||
|
||||
func toValueFunc(env *otto.Otto, ses *GremlinSession, obj *otto.Object, withTags bool) func(otto.FunctionCall) otto.Value {
|
||||
func toValueFunc(env *otto.Otto, ses *Session, obj *otto.Object, withTags bool) func(otto.FunctionCall) otto.Value {
|
||||
return func(call otto.FunctionCall) otto.Value {
|
||||
it := buildIteratorTree(obj, ses.ts)
|
||||
it.AddTag(GremlinTopResultTag)
|
||||
it.AddTag(TopResultTag)
|
||||
limit := 1
|
||||
var val otto.Value
|
||||
var err error
|
||||
|
|
@ -116,10 +116,10 @@ func toValueFunc(env *otto.Otto, ses *GremlinSession, obj *otto.Object, withTags
|
|||
}
|
||||
}
|
||||
|
||||
func mapFunc(env *otto.Otto, ses *GremlinSession, obj *otto.Object) func(otto.FunctionCall) otto.Value {
|
||||
func mapFunc(env *otto.Otto, ses *Session, obj *otto.Object) func(otto.FunctionCall) otto.Value {
|
||||
return func(call otto.FunctionCall) otto.Value {
|
||||
it := buildIteratorTree(obj, ses.ts)
|
||||
it.AddTag(GremlinTopResultTag)
|
||||
it.AddTag(TopResultTag)
|
||||
limit := -1
|
||||
if len(call.ArgumentList) == 0 {
|
||||
return otto.NullValue()
|
||||
|
|
@ -134,7 +134,7 @@ func mapFunc(env *otto.Otto, ses *GremlinSession, obj *otto.Object) func(otto.Fu
|
|||
}
|
||||
}
|
||||
|
||||
func tagsToValueMap(m map[string]graph.TSVal, ses *GremlinSession) map[string]string {
|
||||
func tagsToValueMap(m map[string]graph.TSVal, ses *Session) map[string]string {
|
||||
outputMap := make(map[string]string)
|
||||
for k, v := range m {
|
||||
outputMap[k] = ses.ts.GetNameFor(v)
|
||||
|
|
@ -142,7 +142,7 @@ func tagsToValueMap(m map[string]graph.TSVal, ses *GremlinSession) map[string]st
|
|||
return outputMap
|
||||
}
|
||||
|
||||
func runIteratorToArray(it graph.Iterator, ses *GremlinSession, limit int) []map[string]string {
|
||||
func runIteratorToArray(it graph.Iterator, ses *Session, limit int) []map[string]string {
|
||||
output := make([]map[string]string, 0)
|
||||
count := 0
|
||||
it, _ = it.Optimize()
|
||||
|
|
@ -178,7 +178,7 @@ func runIteratorToArray(it graph.Iterator, ses *GremlinSession, limit int) []map
|
|||
return output
|
||||
}
|
||||
|
||||
func runIteratorToArrayNoTags(it graph.Iterator, ses *GremlinSession, limit int) []string {
|
||||
func runIteratorToArrayNoTags(it graph.Iterator, ses *Session, limit int) []string {
|
||||
output := make([]string, 0)
|
||||
count := 0
|
||||
it, _ = it.Optimize()
|
||||
|
|
@ -200,7 +200,7 @@ func runIteratorToArrayNoTags(it graph.Iterator, ses *GremlinSession, limit int)
|
|||
return output
|
||||
}
|
||||
|
||||
func runIteratorWithCallback(it graph.Iterator, ses *GremlinSession, callback otto.Value, this otto.FunctionCall, limit int) {
|
||||
func runIteratorWithCallback(it graph.Iterator, ses *Session, callback otto.Value, this otto.FunctionCall, limit int) {
|
||||
count := 0
|
||||
it, _ = it.Optimize()
|
||||
for {
|
||||
|
|
@ -236,7 +236,7 @@ func runIteratorWithCallback(it graph.Iterator, ses *GremlinSession, callback ot
|
|||
it.Close()
|
||||
}
|
||||
|
||||
func runIteratorOnSession(it graph.Iterator, ses *GremlinSession) {
|
||||
func runIteratorOnSession(it graph.Iterator, ses *Session) {
|
||||
if ses.lookingForQueryShape {
|
||||
graph.OutputQueryShapeForIterator(it, ses.ts, &(ses.queryShape))
|
||||
return
|
||||
|
|
@ -35,9 +35,9 @@ import (
|
|||
// +---+
|
||||
//
|
||||
|
||||
func buildTripleStore() *GremlinSession {
|
||||
func buildTripleStore() *Session {
|
||||
ts := memstore.MakeTestingMemstore()
|
||||
return NewGremlinSession(ts, -1, false)
|
||||
return NewSession(ts, -1, false)
|
||||
}
|
||||
|
||||
func shouldBeUnordered(actual interface{}, expected ...interface{}) string {
|
||||
|
|
@ -71,7 +71,7 @@ func runQueryGetTag(query string, tag string) ([]string, int) {
|
|||
}
|
||||
|
||||
func ConveyQuery(doc string, query string, expected []string) {
|
||||
ConveyQueryTag(doc, query, GremlinTopResultTag, expected)
|
||||
ConveyQueryTag(doc, query, TopResultTag, expected)
|
||||
}
|
||||
|
||||
func ConveyQueryTag(doc string, query string, tag string, expected []string) {
|
||||
|
|
@ -25,7 +25,7 @@ import (
|
|||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
type GremlinSession struct {
|
||||
type Session struct {
|
||||
ts graph.TripleStore
|
||||
currentChannel chan interface{}
|
||||
env *otto.Otto
|
||||
|
|
@ -42,10 +42,10 @@ type GremlinSession struct {
|
|||
emptyEnv *otto.Otto
|
||||
}
|
||||
|
||||
func NewGremlinSession(inputTripleStore graph.TripleStore, timeoutSec int, persist bool) *GremlinSession {
|
||||
var g GremlinSession
|
||||
func NewSession(inputTripleStore graph.TripleStore, timeoutSec int, persist bool) *Session {
|
||||
var g Session
|
||||
g.ts = inputTripleStore
|
||||
g.env = BuildGremlinEnv(&g)
|
||||
g.env = BuildEnviron(&g)
|
||||
g.limit = -1
|
||||
g.count = 0
|
||||
g.lookingForQueryShape = false
|
||||
|
|
@ -68,11 +68,11 @@ type GremlinResult struct {
|
|||
actualResults *map[string]graph.TSVal
|
||||
}
|
||||
|
||||
func (g *GremlinSession) ToggleDebug() {
|
||||
func (g *Session) ToggleDebug() {
|
||||
g.debug = !g.debug
|
||||
}
|
||||
|
||||
func (g *GremlinSession) GetQuery(input string, output_struct chan map[string]interface{}) {
|
||||
func (g *Session) GetQuery(input string, output_struct chan map[string]interface{}) {
|
||||
defer close(output_struct)
|
||||
g.queryShape = make(map[string]interface{})
|
||||
g.lookingForQueryShape = true
|
||||
|
|
@ -81,7 +81,7 @@ func (g *GremlinSession) GetQuery(input string, output_struct chan map[string]in
|
|||
g.queryShape = nil
|
||||
}
|
||||
|
||||
func (g *GremlinSession) InputParses(input string) (graph.ParseResult, error) {
|
||||
func (g *Session) InputParses(input string) (graph.ParseResult, error) {
|
||||
script, err := g.env.Compile("", input)
|
||||
if err != nil {
|
||||
return graph.ParseFail, err
|
||||
|
|
@ -90,7 +90,7 @@ func (g *GremlinSession) InputParses(input string) (graph.ParseResult, error) {
|
|||
return graph.Parsed, nil
|
||||
}
|
||||
|
||||
func (g *GremlinSession) SendResult(result *GremlinResult) bool {
|
||||
func (g *Session) SendResult(result *GremlinResult) bool {
|
||||
if g.limit >= 0 && g.limit == g.count {
|
||||
return false
|
||||
}
|
||||
|
|
@ -111,7 +111,7 @@ func (g *GremlinSession) SendResult(result *GremlinResult) bool {
|
|||
|
||||
var halt = errors.New("Query Timeout")
|
||||
|
||||
func (g *GremlinSession) runUnsafe(input interface{}) (otto.Value, error) {
|
||||
func (g *Session) runUnsafe(input interface{}) (otto.Value, error) {
|
||||
g.doHalt = false
|
||||
defer func() {
|
||||
if caught := recover(); caught != nil {
|
||||
|
|
@ -141,7 +141,7 @@ func (g *GremlinSession) runUnsafe(input interface{}) (otto.Value, error) {
|
|||
return g.env.Run(input) // Here be dragons (risky code)
|
||||
}
|
||||
|
||||
func (g *GremlinSession) ExecInput(input string, out chan interface{}, limit int) {
|
||||
func (g *Session) ExecInput(input string, out chan interface{}, limit int) {
|
||||
defer close(out)
|
||||
g.err = nil
|
||||
g.currentChannel = out
|
||||
|
|
@ -169,7 +169,7 @@ func (g *GremlinSession) ExecInput(input string, out chan interface{}, limit int
|
|||
return
|
||||
}
|
||||
|
||||
func (s *GremlinSession) ToText(result interface{}) string {
|
||||
func (s *Session) ToText(result interface{}) string {
|
||||
data := result.(*GremlinResult)
|
||||
if data.metaresult {
|
||||
if data.err != "" {
|
||||
|
|
@ -220,7 +220,7 @@ func (s *GremlinSession) ToText(result interface{}) string {
|
|||
}
|
||||
|
||||
// Web stuff
|
||||
func (ses *GremlinSession) BuildJson(result interface{}) {
|
||||
func (ses *Session) BuildJson(result interface{}) {
|
||||
data := result.(*GremlinResult)
|
||||
if !data.metaresult {
|
||||
if data.val == nil {
|
||||
|
|
@ -250,7 +250,7 @@ func (ses *GremlinSession) BuildJson(result interface{}) {
|
|||
|
||||
}
|
||||
|
||||
func (ses *GremlinSession) GetJson() (interface{}, error) {
|
||||
func (ses *Session) GetJson() (interface{}, error) {
|
||||
defer ses.ClearJson()
|
||||
if ses.err != nil {
|
||||
return nil, ses.err
|
||||
|
|
@ -261,6 +261,6 @@ func (ses *GremlinSession) GetJson() (interface{}, error) {
|
|||
return ses.dataOutput, nil
|
||||
}
|
||||
|
||||
func (ses *GremlinSession) ClearJson() {
|
||||
func (ses *Session) ClearJson() {
|
||||
ses.dataOutput = nil
|
||||
}
|
||||
|
|
@ -21,7 +21,7 @@ import (
|
|||
"github.com/robertkrimen/otto"
|
||||
)
|
||||
|
||||
func embedTraversals(env *otto.Otto, ses *GremlinSession, obj *otto.Object) {
|
||||
func embedTraversals(env *otto.Otto, ses *Session, obj *otto.Object) {
|
||||
obj.Set("In", gremlinFunc("in", obj, env, ses))
|
||||
obj.Set("Out", gremlinFunc("out", obj, env, ses))
|
||||
obj.Set("Is", gremlinFunc("is", obj, env, ses))
|
||||
|
|
@ -40,7 +40,7 @@ func embedTraversals(env *otto.Otto, ses *GremlinSession, obj *otto.Object) {
|
|||
obj.Set("SaveR", gremlinFunc("saver", obj, env, ses))
|
||||
}
|
||||
|
||||
func gremlinFunc(kind string, prevObj *otto.Object, env *otto.Otto, ses *GremlinSession) func(otto.FunctionCall) otto.Value {
|
||||
func gremlinFunc(kind string, prevObj *otto.Object, env *otto.Otto, ses *Session) func(otto.FunctionCall) otto.Value {
|
||||
return func(call otto.FunctionCall) otto.Value {
|
||||
call.Otto.Run("var out = {}")
|
||||
out, _ := call.Otto.Object("out")
|
||||
|
|
@ -59,7 +59,7 @@ func gremlinFunc(kind string, prevObj *otto.Object, env *otto.Otto, ses *Gremlin
|
|||
}
|
||||
}
|
||||
|
||||
func gremlinBack(kind string, prevObj *otto.Object, env *otto.Otto, ses *GremlinSession) func(otto.FunctionCall) otto.Value {
|
||||
func gremlinBack(kind string, prevObj *otto.Object, env *otto.Otto, ses *Session) func(otto.FunctionCall) otto.Value {
|
||||
return func(call otto.FunctionCall) otto.Value {
|
||||
call.Otto.Run("var out = {}")
|
||||
out, _ := call.Otto.Object("out")
|
||||
|
|
@ -87,7 +87,7 @@ func gremlinBack(kind string, prevObj *otto.Object, env *otto.Otto, ses *Gremlin
|
|||
}
|
||||
}
|
||||
|
||||
func gremlinFollowR(kind string, prevObj *otto.Object, env *otto.Otto, ses *GremlinSession) func(otto.FunctionCall) otto.Value {
|
||||
func gremlinFollowR(kind string, prevObj *otto.Object, env *otto.Otto, ses *Session) func(otto.FunctionCall) otto.Value {
|
||||
return func(call otto.FunctionCall) otto.Value {
|
||||
call.Otto.Run("var out = {}")
|
||||
out, _ := call.Otto.Object("out")
|
||||
|
|
@ -35,9 +35,9 @@ import (
|
|||
// +---+
|
||||
//
|
||||
|
||||
func buildTripleStore() *MqlSession {
|
||||
func buildTripleStore() *Session {
|
||||
ts := memstore.MakeTestingMemstore()
|
||||
return NewMqlSession(ts)
|
||||
return NewSession(ts)
|
||||
}
|
||||
|
||||
func compareJsonInterfaces(actual interface{}, expected interface{}, path MqlPath, t *testing.T) {
|
||||
|
|
@ -25,7 +25,7 @@ type MqlPath string
|
|||
type MqlResultPath string
|
||||
|
||||
type MqlQuery struct {
|
||||
ses *MqlSession
|
||||
ses *Session
|
||||
it graph.Iterator
|
||||
isRepeated map[MqlPath]bool
|
||||
queryStructure map[MqlPath]map[string]interface{}
|
||||
|
|
@ -100,7 +100,7 @@ func (p MqlPath) ToResultPathFromMap(resultMap map[MqlPath]string) MqlResultPath
|
|||
return output
|
||||
}
|
||||
|
||||
func NewMqlQuery(ses *MqlSession) *MqlQuery {
|
||||
func NewMqlQuery(ses *Session) *MqlQuery {
|
||||
var q MqlQuery
|
||||
q.ses = ses
|
||||
q.results = make([]interface{}, 0)
|
||||
|
|
@ -24,23 +24,23 @@ import (
|
|||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
type MqlSession struct {
|
||||
type Session struct {
|
||||
ts graph.TripleStore
|
||||
currentQuery *MqlQuery
|
||||
debug bool
|
||||
}
|
||||
|
||||
func NewMqlSession(ts graph.TripleStore) *MqlSession {
|
||||
var m MqlSession
|
||||
func NewSession(ts graph.TripleStore) *Session {
|
||||
var m Session
|
||||
m.ts = ts
|
||||
return &m
|
||||
}
|
||||
|
||||
func (m *MqlSession) ToggleDebug() {
|
||||
func (m *Session) ToggleDebug() {
|
||||
m.debug = !m.debug
|
||||
}
|
||||
|
||||
func (m *MqlSession) GetQuery(input string, output_struct chan map[string]interface{}) {
|
||||
func (m *Session) GetQuery(input string, output_struct chan map[string]interface{}) {
|
||||
defer close(output_struct)
|
||||
var mqlQuery interface{}
|
||||
err := json.Unmarshal([]byte(input), &mqlQuery)
|
||||
|
|
@ -61,7 +61,7 @@ func (m *MqlSession) GetQuery(input string, output_struct chan map[string]interf
|
|||
output_struct <- output
|
||||
}
|
||||
|
||||
func (m *MqlSession) InputParses(input string) (graph.ParseResult, error) {
|
||||
func (m *Session) InputParses(input string) (graph.ParseResult, error) {
|
||||
var x interface{}
|
||||
err := json.Unmarshal([]byte(input), &x)
|
||||
if err != nil {
|
||||
|
|
@ -70,7 +70,7 @@ func (m *MqlSession) InputParses(input string) (graph.ParseResult, error) {
|
|||
return graph.Parsed, nil
|
||||
}
|
||||
|
||||
func (m *MqlSession) ExecInput(input string, c chan interface{}, limit int) {
|
||||
func (m *Session) ExecInput(input string, c chan interface{}, limit int) {
|
||||
defer close(c)
|
||||
var mqlQuery interface{}
|
||||
err := json.Unmarshal([]byte(input), &mqlQuery)
|
||||
|
|
@ -102,7 +102,7 @@ func (m *MqlSession) ExecInput(input string, c chan interface{}, limit int) {
|
|||
}
|
||||
}
|
||||
|
||||
func (m *MqlSession) ToText(result interface{}) string {
|
||||
func (m *Session) ToText(result interface{}) string {
|
||||
tags := *(result.(*map[string]graph.TSVal))
|
||||
out := fmt.Sprintln("****")
|
||||
tagKeys := make([]string, len(tags))
|
||||
|
|
@ -125,11 +125,11 @@ func (m *MqlSession) ToText(result interface{}) string {
|
|||
return out
|
||||
}
|
||||
|
||||
func (m *MqlSession) BuildJson(result interface{}) {
|
||||
func (m *Session) BuildJson(result interface{}) {
|
||||
m.currentQuery.treeifyResult(*(result.(*map[string]graph.TSVal)))
|
||||
}
|
||||
|
||||
func (m *MqlSession) GetJson() (interface{}, error) {
|
||||
func (m *Session) GetJson() (interface{}, error) {
|
||||
m.currentQuery.buildResults()
|
||||
if m.currentQuery.isError {
|
||||
return nil, m.currentQuery.err
|
||||
|
|
@ -138,7 +138,7 @@ func (m *MqlSession) GetJson() (interface{}, error) {
|
|||
}
|
||||
}
|
||||
|
||||
func (m *MqlSession) ClearJson() {
|
||||
func (m *Session) ClearJson() {
|
||||
// Since we create a new MqlQuery underneath every query, clearing isn't necessary.
|
||||
return
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue