Destutter gremlin

This commit is contained in:
kortschak 2014-06-28 12:55:21 +09:30
parent 388618bfa7
commit 3a673a333c
7 changed files with 43 additions and 43 deletions

View file

@ -70,7 +70,7 @@ func Repl(ts graph.TripleStore, queryLanguage string, cfg *config.Config) {
case "gremlin": case "gremlin":
fallthrough fallthrough
default: default:
ses = gremlin.NewGremlinSession(ts, cfg.GremlinTimeout, true) ses = gremlin.NewSession(ts, cfg.GremlinTimeout, true)
} }
inputBf := bufio.NewReader(os.Stdin) inputBf := bufio.NewReader(os.Stdin)
line := "" line := ""

View file

@ -71,7 +71,7 @@ func (api *Api) ServeV1Query(w http.ResponseWriter, r *http.Request, params http
var ses graph.HttpSession var ses graph.HttpSession
switch params.ByName("query_lang") { switch params.ByName("query_lang") {
case "gremlin": case "gremlin":
ses = gremlin.NewGremlinSession(api.ts, api.config.GremlinTimeout, false) ses = gremlin.NewSession(api.ts, api.config.GremlinTimeout, false)
case "mql": case "mql":
ses = mql.NewMqlSession(api.ts) ses = mql.NewMqlSession(api.ts)
default: default:
@ -120,7 +120,7 @@ func (api *Api) ServeV1Shape(w http.ResponseWriter, r *http.Request, params http
var ses graph.HttpSession var ses graph.HttpSession
switch params.ByName("query_lang") { switch params.ByName("query_lang") {
case "gremlin": case "gremlin":
ses = gremlin.NewGremlinSession(api.ts, api.config.GremlinTimeout, false) ses = gremlin.NewSession(api.ts, api.config.GremlinTimeout, false)
case "mql": case "mql":
ses = mql.NewMqlSession(api.ts) ses = mql.NewMqlSession(api.ts)
default: default:

View file

@ -21,7 +21,7 @@ import (
"github.com/robertkrimen/otto" "github.com/robertkrimen/otto"
) )
func BuildGremlinEnv(ses *GremlinSession) *otto.Otto { func BuildEnviron(ses *Session) *otto.Otto {
env := otto.New() env := otto.New()
setupGremlin(env, ses) setupGremlin(env, ses)
return env return env
@ -55,7 +55,7 @@ func isVertexChain(obj *otto.Object) bool {
return false return false
} }
func setupGremlin(env *otto.Otto, ses *GremlinSession) { func setupGremlin(env *otto.Otto, ses *Session) {
graph, _ := env.Object("graph = {}") graph, _ := env.Object("graph = {}")
graph.Set("Vertex", func(call otto.FunctionCall) otto.Value { graph.Set("Vertex", func(call otto.FunctionCall) otto.Value {
call.Otto.Run("var out = {}") call.Otto.Run("var out = {}")

View file

@ -21,9 +21,9 @@ import (
"github.com/google/cayley/graph" "github.com/google/cayley/graph"
) )
const GremlinTopResultTag = "id" const TopResultTag = "id"
func embedFinals(env *otto.Otto, ses *GremlinSession, obj *otto.Object) { func embedFinals(env *otto.Otto, ses *Session, obj *otto.Object) {
obj.Set("All", allFunc(env, ses, obj)) obj.Set("All", allFunc(env, ses, obj))
obj.Set("GetLimit", limitFunc(env, ses, obj)) obj.Set("GetLimit", limitFunc(env, ses, obj))
obj.Set("ToArray", toArrayFunc(env, ses, obj, false)) obj.Set("ToArray", toArrayFunc(env, ses, obj, false))
@ -34,10 +34,10 @@ func embedFinals(env *otto.Otto, ses *GremlinSession, obj *otto.Object) {
obj.Set("ForEach", mapFunc(env, ses, obj)) obj.Set("ForEach", mapFunc(env, ses, obj))
} }
func allFunc(env *otto.Otto, ses *GremlinSession, obj *otto.Object) func(otto.FunctionCall) otto.Value { func allFunc(env *otto.Otto, ses *Session, obj *otto.Object) func(otto.FunctionCall) otto.Value {
return func(call otto.FunctionCall) otto.Value { return func(call otto.FunctionCall) otto.Value {
it := buildIteratorTree(obj, ses.ts) it := buildIteratorTree(obj, ses.ts)
it.AddTag(GremlinTopResultTag) it.AddTag(TopResultTag)
ses.limit = -1 ses.limit = -1
ses.count = 0 ses.count = 0
runIteratorOnSession(it, ses) runIteratorOnSession(it, ses)
@ -45,12 +45,12 @@ func allFunc(env *otto.Otto, ses *GremlinSession, obj *otto.Object) func(otto.Fu
} }
} }
func limitFunc(env *otto.Otto, ses *GremlinSession, obj *otto.Object) func(otto.FunctionCall) otto.Value { func limitFunc(env *otto.Otto, ses *Session, obj *otto.Object) func(otto.FunctionCall) otto.Value {
return func(call otto.FunctionCall) otto.Value { return func(call otto.FunctionCall) otto.Value {
if len(call.ArgumentList) > 0 { if len(call.ArgumentList) > 0 {
limitVal, _ := call.Argument(0).ToInteger() limitVal, _ := call.Argument(0).ToInteger()
it := buildIteratorTree(obj, ses.ts) it := buildIteratorTree(obj, ses.ts)
it.AddTag(GremlinTopResultTag) it.AddTag(TopResultTag)
ses.limit = int(limitVal) ses.limit = int(limitVal)
ses.count = 0 ses.count = 0
runIteratorOnSession(it, ses) runIteratorOnSession(it, ses)
@ -59,10 +59,10 @@ func limitFunc(env *otto.Otto, ses *GremlinSession, obj *otto.Object) func(otto.
} }
} }
func toArrayFunc(env *otto.Otto, ses *GremlinSession, obj *otto.Object, withTags bool) func(otto.FunctionCall) otto.Value { func toArrayFunc(env *otto.Otto, ses *Session, obj *otto.Object, withTags bool) func(otto.FunctionCall) otto.Value {
return func(call otto.FunctionCall) otto.Value { return func(call otto.FunctionCall) otto.Value {
it := buildIteratorTree(obj, ses.ts) it := buildIteratorTree(obj, ses.ts)
it.AddTag(GremlinTopResultTag) it.AddTag(TopResultTag)
limit := -1 limit := -1
if len(call.ArgumentList) > 0 { if len(call.ArgumentList) > 0 {
limitParsed, _ := call.Argument(0).ToInteger() limitParsed, _ := call.Argument(0).ToInteger()
@ -86,10 +86,10 @@ func toArrayFunc(env *otto.Otto, ses *GremlinSession, obj *otto.Object, withTags
} }
} }
func toValueFunc(env *otto.Otto, ses *GremlinSession, obj *otto.Object, withTags bool) func(otto.FunctionCall) otto.Value { func toValueFunc(env *otto.Otto, ses *Session, obj *otto.Object, withTags bool) func(otto.FunctionCall) otto.Value {
return func(call otto.FunctionCall) otto.Value { return func(call otto.FunctionCall) otto.Value {
it := buildIteratorTree(obj, ses.ts) it := buildIteratorTree(obj, ses.ts)
it.AddTag(GremlinTopResultTag) it.AddTag(TopResultTag)
limit := 1 limit := 1
var val otto.Value var val otto.Value
var err error var err error
@ -116,10 +116,10 @@ func toValueFunc(env *otto.Otto, ses *GremlinSession, obj *otto.Object, withTags
} }
} }
func mapFunc(env *otto.Otto, ses *GremlinSession, obj *otto.Object) func(otto.FunctionCall) otto.Value { func mapFunc(env *otto.Otto, ses *Session, obj *otto.Object) func(otto.FunctionCall) otto.Value {
return func(call otto.FunctionCall) otto.Value { return func(call otto.FunctionCall) otto.Value {
it := buildIteratorTree(obj, ses.ts) it := buildIteratorTree(obj, ses.ts)
it.AddTag(GremlinTopResultTag) it.AddTag(TopResultTag)
limit := -1 limit := -1
if len(call.ArgumentList) == 0 { if len(call.ArgumentList) == 0 {
return otto.NullValue() return otto.NullValue()
@ -134,7 +134,7 @@ func mapFunc(env *otto.Otto, ses *GremlinSession, obj *otto.Object) func(otto.Fu
} }
} }
func tagsToValueMap(m map[string]graph.TSVal, ses *GremlinSession) map[string]string { func tagsToValueMap(m map[string]graph.TSVal, ses *Session) map[string]string {
outputMap := make(map[string]string) outputMap := make(map[string]string)
for k, v := range m { for k, v := range m {
outputMap[k] = ses.ts.GetNameFor(v) outputMap[k] = ses.ts.GetNameFor(v)
@ -142,7 +142,7 @@ func tagsToValueMap(m map[string]graph.TSVal, ses *GremlinSession) map[string]st
return outputMap return outputMap
} }
func runIteratorToArray(it graph.Iterator, ses *GremlinSession, limit int) []map[string]string { func runIteratorToArray(it graph.Iterator, ses *Session, limit int) []map[string]string {
output := make([]map[string]string, 0) output := make([]map[string]string, 0)
count := 0 count := 0
it, _ = it.Optimize() it, _ = it.Optimize()
@ -178,7 +178,7 @@ func runIteratorToArray(it graph.Iterator, ses *GremlinSession, limit int) []map
return output return output
} }
func runIteratorToArrayNoTags(it graph.Iterator, ses *GremlinSession, limit int) []string { func runIteratorToArrayNoTags(it graph.Iterator, ses *Session, limit int) []string {
output := make([]string, 0) output := make([]string, 0)
count := 0 count := 0
it, _ = it.Optimize() it, _ = it.Optimize()
@ -200,7 +200,7 @@ func runIteratorToArrayNoTags(it graph.Iterator, ses *GremlinSession, limit int)
return output return output
} }
func runIteratorWithCallback(it graph.Iterator, ses *GremlinSession, callback otto.Value, this otto.FunctionCall, limit int) { func runIteratorWithCallback(it graph.Iterator, ses *Session, callback otto.Value, this otto.FunctionCall, limit int) {
count := 0 count := 0
it, _ = it.Optimize() it, _ = it.Optimize()
for { for {
@ -236,7 +236,7 @@ func runIteratorWithCallback(it graph.Iterator, ses *GremlinSession, callback ot
it.Close() it.Close()
} }
func runIteratorOnSession(it graph.Iterator, ses *GremlinSession) { func runIteratorOnSession(it graph.Iterator, ses *Session) {
if ses.lookingForQueryShape { if ses.lookingForQueryShape {
graph.OutputQueryShapeForIterator(it, ses.ts, &(ses.queryShape)) graph.OutputQueryShapeForIterator(it, ses.ts, &(ses.queryShape))
return return

View file

@ -35,9 +35,9 @@ import (
// +---+ // +---+
// //
func buildTripleStore() *GremlinSession { func buildTripleStore() *Session {
ts := memstore.MakeTestingMemstore() ts := memstore.MakeTestingMemstore()
return NewGremlinSession(ts, -1, false) return NewSession(ts, -1, false)
} }
func shouldBeUnordered(actual interface{}, expected ...interface{}) string { func shouldBeUnordered(actual interface{}, expected ...interface{}) string {
@ -71,7 +71,7 @@ func runQueryGetTag(query string, tag string) ([]string, int) {
} }
func ConveyQuery(doc string, query string, expected []string) { func ConveyQuery(doc string, query string, expected []string) {
ConveyQueryTag(doc, query, GremlinTopResultTag, expected) ConveyQueryTag(doc, query, TopResultTag, expected)
} }
func ConveyQueryTag(doc string, query string, tag string, expected []string) { func ConveyQueryTag(doc string, query string, tag string, expected []string) {

View file

@ -25,7 +25,7 @@ import (
"github.com/google/cayley/graph" "github.com/google/cayley/graph"
) )
type GremlinSession struct { type Session struct {
ts graph.TripleStore ts graph.TripleStore
currentChannel chan interface{} currentChannel chan interface{}
env *otto.Otto env *otto.Otto
@ -42,10 +42,10 @@ type GremlinSession struct {
emptyEnv *otto.Otto emptyEnv *otto.Otto
} }
func NewGremlinSession(inputTripleStore graph.TripleStore, timeoutSec int, persist bool) *GremlinSession { func NewSession(inputTripleStore graph.TripleStore, timeoutSec int, persist bool) *Session {
var g GremlinSession var g Session
g.ts = inputTripleStore g.ts = inputTripleStore
g.env = BuildGremlinEnv(&g) g.env = BuildEnviron(&g)
g.limit = -1 g.limit = -1
g.count = 0 g.count = 0
g.lookingForQueryShape = false g.lookingForQueryShape = false
@ -68,11 +68,11 @@ type GremlinResult struct {
actualResults *map[string]graph.TSVal actualResults *map[string]graph.TSVal
} }
func (g *GremlinSession) ToggleDebug() { func (g *Session) ToggleDebug() {
g.debug = !g.debug g.debug = !g.debug
} }
func (g *GremlinSession) GetQuery(input string, output_struct chan map[string]interface{}) { func (g *Session) GetQuery(input string, output_struct chan map[string]interface{}) {
defer close(output_struct) defer close(output_struct)
g.queryShape = make(map[string]interface{}) g.queryShape = make(map[string]interface{})
g.lookingForQueryShape = true g.lookingForQueryShape = true
@ -81,7 +81,7 @@ func (g *GremlinSession) GetQuery(input string, output_struct chan map[string]in
g.queryShape = nil g.queryShape = nil
} }
func (g *GremlinSession) InputParses(input string) (graph.ParseResult, error) { func (g *Session) InputParses(input string) (graph.ParseResult, error) {
script, err := g.env.Compile("", input) script, err := g.env.Compile("", input)
if err != nil { if err != nil {
return graph.ParseFail, err return graph.ParseFail, err
@ -90,7 +90,7 @@ func (g *GremlinSession) InputParses(input string) (graph.ParseResult, error) {
return graph.Parsed, nil return graph.Parsed, nil
} }
func (g *GremlinSession) SendResult(result *GremlinResult) bool { func (g *Session) SendResult(result *GremlinResult) bool {
if g.limit >= 0 && g.limit == g.count { if g.limit >= 0 && g.limit == g.count {
return false return false
} }
@ -111,7 +111,7 @@ func (g *GremlinSession) SendResult(result *GremlinResult) bool {
var halt = errors.New("Query Timeout") var halt = errors.New("Query Timeout")
func (g *GremlinSession) runUnsafe(input interface{}) (otto.Value, error) { func (g *Session) runUnsafe(input interface{}) (otto.Value, error) {
g.doHalt = false g.doHalt = false
defer func() { defer func() {
if caught := recover(); caught != nil { if caught := recover(); caught != nil {
@ -141,7 +141,7 @@ func (g *GremlinSession) runUnsafe(input interface{}) (otto.Value, error) {
return g.env.Run(input) // Here be dragons (risky code) return g.env.Run(input) // Here be dragons (risky code)
} }
func (g *GremlinSession) ExecInput(input string, out chan interface{}, limit int) { func (g *Session) ExecInput(input string, out chan interface{}, limit int) {
defer close(out) defer close(out)
g.err = nil g.err = nil
g.currentChannel = out g.currentChannel = out
@ -169,7 +169,7 @@ func (g *GremlinSession) ExecInput(input string, out chan interface{}, limit int
return return
} }
func (s *GremlinSession) ToText(result interface{}) string { func (s *Session) ToText(result interface{}) string {
data := result.(*GremlinResult) data := result.(*GremlinResult)
if data.metaresult { if data.metaresult {
if data.err != "" { if data.err != "" {
@ -220,7 +220,7 @@ func (s *GremlinSession) ToText(result interface{}) string {
} }
// Web stuff // Web stuff
func (ses *GremlinSession) BuildJson(result interface{}) { func (ses *Session) BuildJson(result interface{}) {
data := result.(*GremlinResult) data := result.(*GremlinResult)
if !data.metaresult { if !data.metaresult {
if data.val == nil { if data.val == nil {
@ -250,7 +250,7 @@ func (ses *GremlinSession) BuildJson(result interface{}) {
} }
func (ses *GremlinSession) GetJson() (interface{}, error) { func (ses *Session) GetJson() (interface{}, error) {
defer ses.ClearJson() defer ses.ClearJson()
if ses.err != nil { if ses.err != nil {
return nil, ses.err return nil, ses.err
@ -261,6 +261,6 @@ func (ses *GremlinSession) GetJson() (interface{}, error) {
return ses.dataOutput, nil return ses.dataOutput, nil
} }
func (ses *GremlinSession) ClearJson() { func (ses *Session) ClearJson() {
ses.dataOutput = nil ses.dataOutput = nil
} }

View file

@ -21,7 +21,7 @@ import (
"github.com/robertkrimen/otto" "github.com/robertkrimen/otto"
) )
func embedTraversals(env *otto.Otto, ses *GremlinSession, obj *otto.Object) { func embedTraversals(env *otto.Otto, ses *Session, obj *otto.Object) {
obj.Set("In", gremlinFunc("in", obj, env, ses)) obj.Set("In", gremlinFunc("in", obj, env, ses))
obj.Set("Out", gremlinFunc("out", obj, env, ses)) obj.Set("Out", gremlinFunc("out", obj, env, ses))
obj.Set("Is", gremlinFunc("is", obj, env, ses)) obj.Set("Is", gremlinFunc("is", obj, env, ses))
@ -40,7 +40,7 @@ func embedTraversals(env *otto.Otto, ses *GremlinSession, obj *otto.Object) {
obj.Set("SaveR", gremlinFunc("saver", obj, env, ses)) obj.Set("SaveR", gremlinFunc("saver", obj, env, ses))
} }
func gremlinFunc(kind string, prevObj *otto.Object, env *otto.Otto, ses *GremlinSession) func(otto.FunctionCall) otto.Value { func gremlinFunc(kind string, prevObj *otto.Object, env *otto.Otto, ses *Session) func(otto.FunctionCall) otto.Value {
return func(call otto.FunctionCall) otto.Value { return func(call otto.FunctionCall) otto.Value {
call.Otto.Run("var out = {}") call.Otto.Run("var out = {}")
out, _ := call.Otto.Object("out") out, _ := call.Otto.Object("out")
@ -59,7 +59,7 @@ func gremlinFunc(kind string, prevObj *otto.Object, env *otto.Otto, ses *Gremlin
} }
} }
func gremlinBack(kind string, prevObj *otto.Object, env *otto.Otto, ses *GremlinSession) func(otto.FunctionCall) otto.Value { func gremlinBack(kind string, prevObj *otto.Object, env *otto.Otto, ses *Session) func(otto.FunctionCall) otto.Value {
return func(call otto.FunctionCall) otto.Value { return func(call otto.FunctionCall) otto.Value {
call.Otto.Run("var out = {}") call.Otto.Run("var out = {}")
out, _ := call.Otto.Object("out") out, _ := call.Otto.Object("out")
@ -87,7 +87,7 @@ func gremlinBack(kind string, prevObj *otto.Object, env *otto.Otto, ses *Gremlin
} }
} }
func gremlinFollowR(kind string, prevObj *otto.Object, env *otto.Otto, ses *GremlinSession) func(otto.FunctionCall) otto.Value { func gremlinFollowR(kind string, prevObj *otto.Object, env *otto.Otto, ses *Session) func(otto.FunctionCall) otto.Value {
return func(call otto.FunctionCall) otto.Value { return func(call otto.FunctionCall) otto.Value {
call.Otto.Run("var out = {}") call.Otto.Run("var out = {}")
out, _ := call.Otto.Object("out") out, _ := call.Otto.Object("out")