예제 #1
0
파일: gremlin.go 프로젝트: nunb/degdb
func runQuery(queryLang, code string) (interface{}, error) {
	qs := &QuadStore{}
	var ses query.HTTP
	switch queryLang {
	case "gremlin":
		ses = gremlin.NewSession(qs, 100*time.Second, false)
	case "mql":
		ses = mql.NewSession(qs)
	default:
	}
	result, err := ses.Parse(code)
	switch result {
	case query.Parsed:
		output, err := Run(code, ses)
		if err != nil {
			return nil, err
		}
		return output, nil
	case query.ParseFail:
		ses = nil
		return nil, err
	default:
		ses = nil
		return nil, fmt.Errorf("Incomplete data?")
	}
}
예제 #2
0
func (api *API) ServeV1Shape(w http.ResponseWriter, r *http.Request, params httprouter.Params) int {
	var ses query.HTTP
	switch params.ByName("query_lang") {
	case "gremlin":
		ses = gremlin.NewSession(api.handle.QuadStore, api.config.Timeout, false)
	case "mql":
		ses = mql.NewSession(api.handle.QuadStore)
	default:
		return jsonResponse(w, 400, "Need a query language.")
	}
	bodyBytes, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return jsonResponse(w, 400, err)
	}
	code := string(bodyBytes)
	result, err := ses.InputParses(code)
	switch result {
	case query.Parsed:
		var output []byte
		var err error
		output, err = GetQueryShape(code, ses)
		if err != nil {
			return jsonResponse(w, 400, err)
		}
		fmt.Fprint(w, string(output))
		return 200
	case query.ParseFail:
		return jsonResponse(w, 400, err)
	default:
		return jsonResponse(w, 500, "Incomplete data?")
	}
}
예제 #3
0
파일: query.go 프로젝트: e4x/cayley
func GetQueryShape(q string, ses query.HTTP) ([]byte, error) {
	s, err := ses.ShapeOf(q)
	if err != nil {
		return nil, err
	}
	return json.Marshal(s)
}
예제 #4
0
func GetQueryShape(q string, ses query.HTTP) ([]byte, error) {
	c := make(chan map[string]interface{}, 5)
	go ses.GetQuery(q, c)
	var data map[string]interface{}
	for res := range c {
		data = res
	}
	return json.Marshal(data)
}
예제 #5
0
파일: query.go 프로젝트: e4x/cayley
// TODO(barakmich): Turn this into proper middleware.
func (api *API) ServeV1Query(w http.ResponseWriter, r *http.Request, params httprouter.Params) int {
	h, err := api.GetHandleForRequest(r)
	var ses query.HTTP
	switch params.ByName("query_lang") {
	case "gremlin":
		ses = gremlin.NewSession(h.QuadStore, api.config.Timeout, false)
	case "mql":
		ses = mql.NewSession(h.QuadStore)
	default:
		return jsonResponse(w, 400, "Need a query language.")
	}
	bodyBytes, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return jsonResponse(w, 400, err)
	}
	code := string(bodyBytes)
	result, err := ses.Parse(code)
	switch result {
	case query.Parsed:
		var output interface{}
		var bytes []byte
		var err error
		output, err = Run(code, ses)
		if err != nil {
			bytes, err = WrapErrResult(err)
			http.Error(w, string(bytes), 400)
			ses = nil
			return 400
		}
		bytes, err = WrapResult(output)
		if err != nil {
			ses = nil
			return jsonResponse(w, 400, err)
		}
		fmt.Fprint(w, string(bytes))
		ses = nil
		return 200
	case query.ParseFail:
		ses = nil
		return jsonResponse(w, 400, err)
	default:
		ses = nil
		return jsonResponse(w, 500, "Incomplete data?")
	}
}
예제 #6
0
func Run(q string, ses query.HTTP) (interface{}, error) {
	c := make(chan interface{}, 5)
	go ses.ExecInput(q, c, 100)
	for res := range c {
		ses.BuildJSON(res)
	}
	return ses.GetJSON()
}
예제 #7
0
파일: query.go 프로젝트: e4x/cayley
func Run(q string, ses query.HTTP) (interface{}, error) {
	c := make(chan interface{}, 5)
	go ses.Execute(q, c, 100)
	for res := range c {
		ses.Collate(res)
	}
	return ses.Results()
}
예제 #8
0
func runGremlinQuery(q string, session cayleyQuery.HTTP) (interface{}, error) {
	c := make(chan interface{}, 5)
	go session.Execute(q, c, 100)
	for result := range c {
		session.Collate(result)
	}

	return session.Results()
}