Move query interface definitions into query
This commit is contained in:
parent
38f7b79761
commit
a6cf432313
7 changed files with 74 additions and 70 deletions
11
db/repl.go
11
db/repl.go
|
|
@ -27,6 +27,7 @@ import (
|
||||||
"github.com/google/cayley/graph"
|
"github.com/google/cayley/graph"
|
||||||
"github.com/google/cayley/graph/sexp"
|
"github.com/google/cayley/graph/sexp"
|
||||||
"github.com/google/cayley/quad/cquads"
|
"github.com/google/cayley/quad/cquads"
|
||||||
|
"github.com/google/cayley/query"
|
||||||
"github.com/google/cayley/query/gremlin"
|
"github.com/google/cayley/query/gremlin"
|
||||||
"github.com/google/cayley/query/mql"
|
"github.com/google/cayley/query/mql"
|
||||||
)
|
)
|
||||||
|
|
@ -41,7 +42,7 @@ func un(s string, startTime time.Time) {
|
||||||
fmt.Printf(s, float64(endTime.UnixNano()-startTime.UnixNano())/float64(1E6))
|
fmt.Printf(s, float64(endTime.UnixNano()-startTime.UnixNano())/float64(1E6))
|
||||||
}
|
}
|
||||||
|
|
||||||
func Run(query string, ses graph.Session) {
|
func Run(query string, ses query.Session) {
|
||||||
nResults := 0
|
nResults := 0
|
||||||
startTrace, startTime := trace("Elapsed time: %g ms\n\n")
|
startTrace, startTime := trace("Elapsed time: %g ms\n\n")
|
||||||
defer func() {
|
defer func() {
|
||||||
|
|
@ -62,7 +63,7 @@ func Run(query string, ses graph.Session) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func Repl(ts graph.TripleStore, queryLanguage string, cfg *config.Config) error {
|
func Repl(ts graph.TripleStore, queryLanguage string, cfg *config.Config) error {
|
||||||
var ses graph.Session
|
var ses query.Session
|
||||||
switch queryLanguage {
|
switch queryLanguage {
|
||||||
case "sexp":
|
case "sexp":
|
||||||
ses = sexp.NewSession(ts)
|
ses = sexp.NewSession(ts)
|
||||||
|
|
@ -140,13 +141,13 @@ func Repl(ts graph.TripleStore, queryLanguage string, cfg *config.Config) error
|
||||||
}
|
}
|
||||||
result, err := ses.InputParses(string(line))
|
result, err := ses.InputParses(string(line))
|
||||||
switch result {
|
switch result {
|
||||||
case graph.Parsed:
|
case query.Parsed:
|
||||||
Run(string(line), ses)
|
Run(string(line), ses)
|
||||||
line = line[:0]
|
line = line[:0]
|
||||||
case graph.ParseFail:
|
case query.ParseFail:
|
||||||
fmt.Println("Error: ", err)
|
fmt.Println("Error: ", err)
|
||||||
line = line[:0]
|
line = line[:0]
|
||||||
case graph.ParseMore:
|
case query.ParseMore:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,7 @@ import (
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
"github.com/google/cayley/graph"
|
"github.com/google/cayley/graph"
|
||||||
|
"github.com/google/cayley/query"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
|
|
@ -39,7 +40,7 @@ func (s *Session) ToggleDebug() {
|
||||||
s.debug = !s.debug
|
s.debug = !s.debug
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) InputParses(input string) (graph.ParseResult, error) {
|
func (s *Session) InputParses(input string) (query.ParseResult, error) {
|
||||||
var parenDepth int
|
var parenDepth int
|
||||||
for i, x := range input {
|
for i, x := range input {
|
||||||
if x == '(' {
|
if x == '(' {
|
||||||
|
|
@ -52,17 +53,17 @@ func (s *Session) InputParses(input string) (graph.ParseResult, error) {
|
||||||
if (i - 10) > min {
|
if (i - 10) > min {
|
||||||
min = i - 10
|
min = i - 10
|
||||||
}
|
}
|
||||||
return graph.ParseFail, errors.New(fmt.Sprintf("Too many close parens at char %d: %s", i, input[min:i]))
|
return query.ParseFail, errors.New(fmt.Sprintf("Too many close parens at char %d: %s", i, input[min:i]))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if parenDepth > 0 {
|
if parenDepth > 0 {
|
||||||
return graph.ParseMore, nil
|
return query.ParseMore, nil
|
||||||
}
|
}
|
||||||
if len(ParseString(input)) > 0 {
|
if len(ParseString(input)) > 0 {
|
||||||
return graph.Parsed, nil
|
return query.Parsed, nil
|
||||||
}
|
}
|
||||||
return graph.ParseFail, errors.New("Invalid Syntax")
|
return query.ParseFail, errors.New("Invalid Syntax")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) ExecInput(input string, out chan interface{}, limit int) {
|
func (s *Session) ExecInput(input string, out chan interface{}, limit int) {
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ import (
|
||||||
|
|
||||||
"github.com/julienschmidt/httprouter"
|
"github.com/julienschmidt/httprouter"
|
||||||
|
|
||||||
"github.com/google/cayley/graph"
|
"github.com/google/cayley/query"
|
||||||
"github.com/google/cayley/query/gremlin"
|
"github.com/google/cayley/query/gremlin"
|
||||||
"github.com/google/cayley/query/mql"
|
"github.com/google/cayley/query/mql"
|
||||||
)
|
)
|
||||||
|
|
@ -47,7 +47,7 @@ func WrapResult(result interface{}) ([]byte, error) {
|
||||||
return json.MarshalIndent(wrap, "", " ")
|
return json.MarshalIndent(wrap, "", " ")
|
||||||
}
|
}
|
||||||
|
|
||||||
func RunJsonQuery(query string, ses graph.HttpSession) (interface{}, error) {
|
func RunJsonQuery(query string, ses query.HttpSession) (interface{}, error) {
|
||||||
c := make(chan interface{}, 5)
|
c := make(chan interface{}, 5)
|
||||||
go ses.ExecInput(query, c, 100)
|
go ses.ExecInput(query, c, 100)
|
||||||
for res := range c {
|
for res := range c {
|
||||||
|
|
@ -56,7 +56,7 @@ func RunJsonQuery(query string, ses graph.HttpSession) (interface{}, error) {
|
||||||
return ses.GetJson()
|
return ses.GetJson()
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetQueryShape(query string, ses graph.HttpSession) ([]byte, error) {
|
func GetQueryShape(query string, ses query.HttpSession) ([]byte, error) {
|
||||||
c := make(chan map[string]interface{}, 5)
|
c := make(chan map[string]interface{}, 5)
|
||||||
go ses.GetQuery(query, c)
|
go ses.GetQuery(query, c)
|
||||||
var data map[string]interface{}
|
var data map[string]interface{}
|
||||||
|
|
@ -68,7 +68,7 @@ func GetQueryShape(query string, ses graph.HttpSession) ([]byte, error) {
|
||||||
|
|
||||||
// TODO(barakmich): Turn this into proper middleware.
|
// TODO(barakmich): Turn this into proper middleware.
|
||||||
func (api *Api) ServeV1Query(w http.ResponseWriter, r *http.Request, params httprouter.Params) int {
|
func (api *Api) ServeV1Query(w http.ResponseWriter, r *http.Request, params httprouter.Params) int {
|
||||||
var ses graph.HttpSession
|
var ses query.HttpSession
|
||||||
switch params.ByName("query_lang") {
|
switch params.ByName("query_lang") {
|
||||||
case "gremlin":
|
case "gremlin":
|
||||||
ses = gremlin.NewSession(api.ts, api.config.GremlinTimeout, false)
|
ses = gremlin.NewSession(api.ts, api.config.GremlinTimeout, false)
|
||||||
|
|
@ -84,7 +84,7 @@ func (api *Api) ServeV1Query(w http.ResponseWriter, r *http.Request, params http
|
||||||
code := string(bodyBytes)
|
code := string(bodyBytes)
|
||||||
result, err := ses.InputParses(code)
|
result, err := ses.InputParses(code)
|
||||||
switch result {
|
switch result {
|
||||||
case graph.Parsed:
|
case query.Parsed:
|
||||||
var output interface{}
|
var output interface{}
|
||||||
var bytes []byte
|
var bytes []byte
|
||||||
var err error
|
var err error
|
||||||
|
|
@ -103,7 +103,7 @@ func (api *Api) ServeV1Query(w http.ResponseWriter, r *http.Request, params http
|
||||||
fmt.Fprint(w, string(bytes))
|
fmt.Fprint(w, string(bytes))
|
||||||
ses = nil
|
ses = nil
|
||||||
return 200
|
return 200
|
||||||
case graph.ParseFail:
|
case query.ParseFail:
|
||||||
ses = nil
|
ses = nil
|
||||||
return FormatJson400(w, err)
|
return FormatJson400(w, err)
|
||||||
default:
|
default:
|
||||||
|
|
@ -116,7 +116,7 @@ func (api *Api) ServeV1Query(w http.ResponseWriter, r *http.Request, params http
|
||||||
}
|
}
|
||||||
|
|
||||||
func (api *Api) ServeV1Shape(w http.ResponseWriter, r *http.Request, params httprouter.Params) int {
|
func (api *Api) ServeV1Shape(w http.ResponseWriter, r *http.Request, params httprouter.Params) int {
|
||||||
var ses graph.HttpSession
|
var ses query.HttpSession
|
||||||
switch params.ByName("query_lang") {
|
switch params.ByName("query_lang") {
|
||||||
case "gremlin":
|
case "gremlin":
|
||||||
ses = gremlin.NewSession(api.ts, api.config.GremlinTimeout, false)
|
ses = gremlin.NewSession(api.ts, api.config.GremlinTimeout, false)
|
||||||
|
|
@ -132,7 +132,7 @@ func (api *Api) ServeV1Shape(w http.ResponseWriter, r *http.Request, params http
|
||||||
code := string(bodyBytes)
|
code := string(bodyBytes)
|
||||||
result, err := ses.InputParses(code)
|
result, err := ses.InputParses(code)
|
||||||
switch result {
|
switch result {
|
||||||
case graph.Parsed:
|
case query.Parsed:
|
||||||
var output []byte
|
var output []byte
|
||||||
var err error
|
var err error
|
||||||
output, err = GetQueryShape(code, ses)
|
output, err = GetQueryShape(code, ses)
|
||||||
|
|
@ -141,7 +141,7 @@ func (api *Api) ServeV1Shape(w http.ResponseWriter, r *http.Request, params http
|
||||||
}
|
}
|
||||||
fmt.Fprint(w, string(output))
|
fmt.Fprint(w, string(output))
|
||||||
return 200
|
return 200
|
||||||
case graph.ParseFail:
|
case query.ParseFail:
|
||||||
return FormatJson400(w, err)
|
return FormatJson400(w, err)
|
||||||
default:
|
default:
|
||||||
return FormatJsonError(w, 500, "Incomplete data?")
|
return FormatJsonError(w, 500, "Incomplete data?")
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ import (
|
||||||
"github.com/robertkrimen/otto"
|
"github.com/robertkrimen/otto"
|
||||||
|
|
||||||
"github.com/google/cayley/graph"
|
"github.com/google/cayley/graph"
|
||||||
|
"github.com/google/cayley/query"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
|
|
@ -81,13 +82,13 @@ func (s *Session) GetQuery(input string, output_struct chan map[string]interface
|
||||||
s.queryShape = nil
|
s.queryShape = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) InputParses(input string) (graph.ParseResult, error) {
|
func (s *Session) InputParses(input string) (query.ParseResult, error) {
|
||||||
script, err := s.env.Compile("", input)
|
script, err := s.env.Compile("", input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return graph.ParseFail, err
|
return query.ParseFail, err
|
||||||
}
|
}
|
||||||
s.script = script
|
s.script = script
|
||||||
return graph.Parsed, nil
|
return query.Parsed, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) SendResult(result *GremlinResult) bool {
|
func (s *Session) SendResult(result *GremlinResult) bool {
|
||||||
|
|
|
||||||
|
|
@ -23,6 +23,7 @@ import (
|
||||||
|
|
||||||
"github.com/google/cayley/graph"
|
"github.com/google/cayley/graph"
|
||||||
"github.com/google/cayley/graph/iterator"
|
"github.com/google/cayley/graph/iterator"
|
||||||
|
"github.com/google/cayley/query"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
|
|
@ -62,13 +63,13 @@ func (m *Session) GetQuery(input string, output_struct chan map[string]interface
|
||||||
output_struct <- output
|
output_struct <- output
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) InputParses(input string) (graph.ParseResult, error) {
|
func (s *Session) InputParses(input string) (query.ParseResult, error) {
|
||||||
var x interface{}
|
var x interface{}
|
||||||
err := json.Unmarshal([]byte(input), &x)
|
err := json.Unmarshal([]byte(input), &x)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return graph.ParseFail, err
|
return query.ParseFail, err
|
||||||
}
|
}
|
||||||
return graph.Parsed, nil
|
return query.Parsed, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) ExecInput(input string, c chan interface{}, limit int) {
|
func (s *Session) ExecInput(input string, c chan interface{}, limit int) {
|
||||||
|
|
|
||||||
|
|
@ -12,7 +12,7 @@
|
||||||
// See the License for the specific language governing permissions and
|
// See the License for the specific language governing permissions and
|
||||||
// limitations under the License.
|
// limitations under the License.
|
||||||
|
|
||||||
package graph
|
package query
|
||||||
|
|
||||||
// Defines the graph session interface general to all query languages.
|
// Defines the graph session interface general to all query languages.
|
||||||
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue