Use error returns and interface type for parsing
Fixes issue #72 This change simplifies interactions with parsing N-Quads and makes reading datasets more robust. Changes made while here also improve performance: benchmark old ns/op new ns/op delta BenchmarkParser 1058 667 -36.96% We still use string concatenation which I'm not wildly happy about, but I think this can be left for a later change. Initial changes towards idiomatic error handling have been made. More significant changes are needed, but these have subtle design implication and need to be thought about more. 30kmoviesdata.nt.gz has been altered to properly escape double quotes. This was done mechanically and with manual curation to pick up straglers.
This commit is contained in:
parent
abdd649c82
commit
0e0e382d2b
11 changed files with 260 additions and 226 deletions
17
db/init.go
17
db/init.go
|
|
@ -19,16 +19,21 @@ import (
|
|||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
func Init(cfg *config.Config, triplePath string) bool {
|
||||
func Init(cfg *config.Config, triplePath string) error {
|
||||
err := graph.InitTripleStore(cfg.DatabaseType, cfg.DatabasePath, cfg.DatabaseOptions)
|
||||
if err != nil {
|
||||
return false
|
||||
return err
|
||||
}
|
||||
|
||||
if triplePath != "" {
|
||||
ts := Open(cfg)
|
||||
Load(ts, cfg, triplePath)
|
||||
ts, err := Open(cfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = Load(ts, cfg, triplePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
ts.Close()
|
||||
}
|
||||
return true
|
||||
return err
|
||||
}
|
||||
|
|
|
|||
64
db/load.go
64
db/load.go
|
|
@ -15,58 +15,54 @@
|
|||
package db
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"github.com/barakmich/glog"
|
||||
|
||||
"github.com/google/cayley/config"
|
||||
"github.com/google/cayley/graph"
|
||||
"github.com/google/cayley/nquads"
|
||||
)
|
||||
|
||||
func Load(ts graph.TripleStore, cfg *config.Config, triplePath string) {
|
||||
tChan := make(chan *graph.Triple)
|
||||
go ReadTriplesFromFile(tChan, triplePath)
|
||||
func Load(ts graph.TripleStore, cfg *config.Config, path string) error {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return fmt.Errorf("could not open file %q: %v", path, err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
dec := nquads.NewDecoder(f)
|
||||
|
||||
bulker, canBulk := ts.(graph.BulkLoader)
|
||||
if canBulk {
|
||||
err := bulker.BulkLoad(tChan)
|
||||
err = bulker.BulkLoad(dec)
|
||||
if err == nil {
|
||||
return
|
||||
return nil
|
||||
}
|
||||
if err != graph.ErrCannotBulkLoad {
|
||||
glog.Errorln("Error attempting to bulk load: ", err)
|
||||
if err == graph.ErrCannotBulkLoad {
|
||||
err = nil
|
||||
}
|
||||
}
|
||||
|
||||
LoadTriplesInto(tChan, ts, cfg.LoadSize)
|
||||
}
|
||||
|
||||
func ReadTriplesFromFile(c chan *graph.Triple, tripleFile string) {
|
||||
f, err := os.Open(tripleFile)
|
||||
if err != nil {
|
||||
glog.Fatalln("Couldn't open file", tripleFile)
|
||||
return err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := f.Close(); err != nil {
|
||||
glog.Fatalln(err)
|
||||
block := make([]*graph.Triple, 0, cfg.LoadSize)
|
||||
for {
|
||||
t, err := dec.Unmarshal()
|
||||
if err != nil {
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
return err
|
||||
}
|
||||
}()
|
||||
|
||||
nquads.ReadNQuadsFromReader(c, f)
|
||||
}
|
||||
|
||||
func LoadTriplesInto(tChan chan *graph.Triple, ts graph.TripleStore, loadSize int) {
|
||||
tripleblock := make([]*graph.Triple, loadSize)
|
||||
i := 0
|
||||
for t := range tChan {
|
||||
tripleblock[i] = t
|
||||
i++
|
||||
if i == loadSize {
|
||||
ts.AddTripleSet(tripleblock)
|
||||
i = 0
|
||||
block = append(block, t)
|
||||
if len(block) == cap(block) {
|
||||
ts.AddTripleSet(block)
|
||||
block = block[:0]
|
||||
}
|
||||
}
|
||||
ts.AddTripleSet(tripleblock[0:i])
|
||||
ts.AddTripleSet(block)
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
|||
11
db/open.go
11
db/open.go
|
|
@ -21,17 +21,20 @@ import (
|
|||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
func Open(cfg *config.Config) graph.TripleStore {
|
||||
func Open(cfg *config.Config) (graph.TripleStore, error) {
|
||||
glog.Infof("Opening database \"%s\" at %s", cfg.DatabaseType, cfg.DatabasePath)
|
||||
ts, err := graph.NewTripleStore(cfg.DatabaseType, cfg.DatabasePath, cfg.DatabaseOptions)
|
||||
if err != nil {
|
||||
glog.Fatalln(err.Error())
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Memstore is not persistent, so it MUST be loaded.
|
||||
if cfg.DatabaseType == "memstore" {
|
||||
Load(ts, cfg, cfg.DatabasePath)
|
||||
err = Load(ts, cfg, cfg.DatabasePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return ts
|
||||
return ts, nil
|
||||
}
|
||||
|
|
|
|||
66
db/repl.go
66
db/repl.go
|
|
@ -16,10 +16,11 @@ package db
|
|||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/cayley/config"
|
||||
|
|
@ -60,7 +61,7 @@ func Run(query string, ses graph.Session) {
|
|||
}
|
||||
}
|
||||
|
||||
func Repl(ts graph.TripleStore, queryLanguage string, cfg *config.Config) {
|
||||
func Repl(ts graph.TripleStore, queryLanguage string, cfg *config.Config) error {
|
||||
var ses graph.Session
|
||||
switch queryLanguage {
|
||||
case "sexp":
|
||||
|
|
@ -72,72 +73,75 @@ func Repl(ts graph.TripleStore, queryLanguage string, cfg *config.Config) {
|
|||
default:
|
||||
ses = gremlin.NewSession(ts, cfg.GremlinTimeout, true)
|
||||
}
|
||||
inputBf := bufio.NewReader(os.Stdin)
|
||||
line := ""
|
||||
buf := bufio.NewReader(os.Stdin)
|
||||
var line []byte
|
||||
for {
|
||||
if line == "" {
|
||||
if len(line) == 0 {
|
||||
fmt.Print("cayley> ")
|
||||
} else {
|
||||
fmt.Print("... ")
|
||||
}
|
||||
l, pre, err := inputBf.ReadLine()
|
||||
l, prefix, err := buf.ReadLine()
|
||||
if err == io.EOF {
|
||||
if line != "" {
|
||||
line = ""
|
||||
if len(line) != 0 {
|
||||
line = line[:0]
|
||||
} else {
|
||||
break
|
||||
return nil
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
line = ""
|
||||
line = line[:0]
|
||||
}
|
||||
if pre {
|
||||
panic("Line too long")
|
||||
if prefix {
|
||||
return errors.New("line too long")
|
||||
}
|
||||
line += string(l)
|
||||
if line == "" {
|
||||
line = append(line, l...)
|
||||
if len(line) == 0 {
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(line, ":debug") {
|
||||
if bytes.HasPrefix(line, []byte(":debug")) {
|
||||
ses.ToggleDebug()
|
||||
fmt.Println("Debug Toggled")
|
||||
line = ""
|
||||
line = line[:0]
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(line, ":a") {
|
||||
if bytes.HasPrefix(line, []byte(":a")) {
|
||||
var tripleStmt = line[3:]
|
||||
triple := nquads.Parse(tripleStmt)
|
||||
triple, err := nquads.Parse(string(tripleStmt))
|
||||
if triple == nil {
|
||||
fmt.Println("Not a valid triple.")
|
||||
line = ""
|
||||
if err != nil {
|
||||
fmt.Printf("not a valid triple: %v\n", err)
|
||||
}
|
||||
line = line[:0]
|
||||
continue
|
||||
}
|
||||
ts.AddTriple(triple)
|
||||
line = ""
|
||||
line = line[:0]
|
||||
continue
|
||||
}
|
||||
if strings.HasPrefix(line, ":d") {
|
||||
if bytes.HasPrefix(line, []byte(":d")) {
|
||||
var tripleStmt = line[3:]
|
||||
triple := nquads.Parse(tripleStmt)
|
||||
triple, err := nquads.Parse(string(tripleStmt))
|
||||
if triple == nil {
|
||||
fmt.Println("Not a valid triple.")
|
||||
line = ""
|
||||
if err != nil {
|
||||
fmt.Printf("not a valid triple: %v\n", err)
|
||||
}
|
||||
line = line[:0]
|
||||
continue
|
||||
}
|
||||
ts.RemoveTriple(triple)
|
||||
line = ""
|
||||
line = line[:0]
|
||||
continue
|
||||
}
|
||||
result, err := ses.InputParses(line)
|
||||
result, err := ses.InputParses(string(line))
|
||||
switch result {
|
||||
case graph.Parsed:
|
||||
Run(line, ses)
|
||||
line = ""
|
||||
Run(string(line), ses)
|
||||
line = line[:0]
|
||||
case graph.ParseFail:
|
||||
fmt.Println("Error: ", err)
|
||||
line = ""
|
||||
line = line[:0]
|
||||
case graph.ParseMore:
|
||||
default:
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue