Move query interface definitions into query

This commit is contained in:
kortschak 2014-07-31 08:52:24 +09:30
parent 38f7b79761
commit a6cf432313
7 changed files with 74 additions and 70 deletions

View file

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

View file

@ -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) {

View file

@ -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?")

View file

@ -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 {

View file

@ -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) {

View file

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package graph
package query
// Defines the graph session interface general to all query languages.