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:
kortschak 2014-07-22 19:55:18 +09:30
parent abdd649c82
commit 0e0e382d2b
11 changed files with 260 additions and 226 deletions

View file

@ -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:
}
}
}