From a6cf432313242df9cf41d44828e5fc3e307088c1 Mon Sep 17 00:00:00 2001 From: kortschak Date: Thu, 31 Jul 2014 08:52:24 +0930 Subject: [PATCH] Move query interface definitions into query --- db/repl.go | 11 ++++++----- graph/session.go | 45 --------------------------------------------- graph/sexp/session.go | 11 ++++++----- http/query.go | 18 +++++++++--------- query/gremlin/session.go | 7 ++++--- query/mql/session.go | 7 ++++--- query/session.go | 45 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 74 insertions(+), 70 deletions(-) delete mode 100644 graph/session.go create mode 100644 query/session.go diff --git a/db/repl.go b/db/repl.go index 3dcd083..731b0fa 100644 --- a/db/repl.go +++ b/db/repl.go @@ -27,6 +27,7 @@ import ( "github.com/google/cayley/graph" "github.com/google/cayley/graph/sexp" "github.com/google/cayley/quad/cquads" + "github.com/google/cayley/query" "github.com/google/cayley/query/gremlin" "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)) } -func Run(query string, ses graph.Session) { +func Run(query string, ses query.Session) { nResults := 0 startTrace, startTime := trace("Elapsed time: %g ms\n\n") defer func() { @@ -62,7 +63,7 @@ func Run(query string, ses graph.Session) { } func Repl(ts graph.TripleStore, queryLanguage string, cfg *config.Config) error { - var ses graph.Session + var ses query.Session switch queryLanguage { case "sexp": 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)) switch result { - case graph.Parsed: + case query.Parsed: Run(string(line), ses) line = line[:0] - case graph.ParseFail: + case query.ParseFail: fmt.Println("Error: ", err) line = line[:0] - case graph.ParseMore: + case query.ParseMore: } } } diff --git a/graph/session.go b/graph/session.go deleted file mode 100644 index c525459..0000000 --- a/graph/session.go +++ /dev/null @@ -1,45 +0,0 @@ -// Copyright 2014 The Cayley Authors. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package graph - -// Defines the graph session interface general to all query languages. - -type ParseResult int - -const ( - Parsed ParseResult = iota - ParseMore - ParseFail -) - -type Session interface { - // Return whether the string is a valid expression. - InputParses(string) (ParseResult, error) - ExecInput(string, chan interface{}, int) - ToText(interface{}) string - ToggleDebug() -} - -type HttpSession interface { - // Return whether the string is a valid expression. - InputParses(string) (ParseResult, error) - // Runs the query and returns individual results on the channel. - ExecInput(string, chan interface{}, int) - GetQuery(string, chan map[string]interface{}) - BuildJson(interface{}) - GetJson() ([]interface{}, error) - ClearJson() - ToggleDebug() -} diff --git a/graph/sexp/session.go b/graph/sexp/session.go index 0fb4810..c1a227b 100644 --- a/graph/sexp/session.go +++ b/graph/sexp/session.go @@ -22,6 +22,7 @@ import ( "sort" "github.com/google/cayley/graph" + "github.com/google/cayley/query" ) type Session struct { @@ -39,7 +40,7 @@ func (s *Session) ToggleDebug() { 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 for i, x := range input { if x == '(' { @@ -52,17 +53,17 @@ func (s *Session) InputParses(input string) (graph.ParseResult, error) { if (i - 10) > min { 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 { - return graph.ParseMore, nil + return query.ParseMore, nil } 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) { diff --git a/http/query.go b/http/query.go index a8b8c2f..e8b5d72 100644 --- a/http/query.go +++ b/http/query.go @@ -22,7 +22,7 @@ import ( "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/mql" ) @@ -47,7 +47,7 @@ func WrapResult(result interface{}) ([]byte, error) { 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) go ses.ExecInput(query, c, 100) for res := range c { @@ -56,7 +56,7 @@ func RunJsonQuery(query string, ses graph.HttpSession) (interface{}, error) { 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) go ses.GetQuery(query, c) 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. 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") { case "gremlin": 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) result, err := ses.InputParses(code) switch result { - case graph.Parsed: + case query.Parsed: var output interface{} var bytes []byte var err error @@ -103,7 +103,7 @@ func (api *Api) ServeV1Query(w http.ResponseWriter, r *http.Request, params http fmt.Fprint(w, string(bytes)) ses = nil return 200 - case graph.ParseFail: + case query.ParseFail: ses = nil return FormatJson400(w, err) 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 { - var ses graph.HttpSession + var ses query.HttpSession switch params.ByName("query_lang") { case "gremlin": 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) result, err := ses.InputParses(code) switch result { - case graph.Parsed: + case query.Parsed: var output []byte var err error 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)) return 200 - case graph.ParseFail: + case query.ParseFail: return FormatJson400(w, err) default: return FormatJsonError(w, 500, "Incomplete data?") diff --git a/query/gremlin/session.go b/query/gremlin/session.go index 9e28f97..00532b8 100644 --- a/query/gremlin/session.go +++ b/query/gremlin/session.go @@ -23,6 +23,7 @@ import ( "github.com/robertkrimen/otto" "github.com/google/cayley/graph" + "github.com/google/cayley/query" ) type Session struct { @@ -81,13 +82,13 @@ func (s *Session) GetQuery(input string, output_struct chan map[string]interface 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) if err != nil { - return graph.ParseFail, err + return query.ParseFail, err } s.script = script - return graph.Parsed, nil + return query.Parsed, nil } func (s *Session) SendResult(result *GremlinResult) bool { diff --git a/query/mql/session.go b/query/mql/session.go index bc983fd..c272005 100644 --- a/query/mql/session.go +++ b/query/mql/session.go @@ -23,6 +23,7 @@ import ( "github.com/google/cayley/graph" "github.com/google/cayley/graph/iterator" + "github.com/google/cayley/query" ) type Session struct { @@ -62,13 +63,13 @@ func (m *Session) GetQuery(input string, output_struct chan map[string]interface output_struct <- output } -func (s *Session) InputParses(input string) (graph.ParseResult, error) { +func (s *Session) InputParses(input string) (query.ParseResult, error) { var x interface{} err := json.Unmarshal([]byte(input), &x) 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) { diff --git a/query/session.go b/query/session.go new file mode 100644 index 0000000..1531fd2 --- /dev/null +++ b/query/session.go @@ -0,0 +1,45 @@ +// Copyright 2014 The Cayley Authors. All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package query + +// Defines the graph session interface general to all query languages. + +type ParseResult int + +const ( + Parsed ParseResult = iota + ParseMore + ParseFail +) + +type Session interface { + // Return whether the string is a valid expression. + InputParses(string) (ParseResult, error) + ExecInput(string, chan interface{}, int) + ToText(interface{}) string + ToggleDebug() +} + +type HttpSession interface { + // Return whether the string is a valid expression. + InputParses(string) (ParseResult, error) + // Runs the query and returns individual results on the channel. + ExecInput(string, chan interface{}, int) + GetQuery(string, chan map[string]interface{}) + BuildJson(interface{}) + GetJson() ([]interface{}, error) + ClearJson() + ToggleDebug() +}