From 3a673a333c5a6cb8e4e8edb9cb56ce46755af04a Mon Sep 17 00:00:00 2001 From: kortschak Date: Sat, 28 Jun 2014 12:55:21 +0930 Subject: [PATCH] Destutter gremlin --- db/repl.go | 2 +- http/query.go | 4 ++-- query/gremlin/environ.go | 4 ++-- query/gremlin/finals.go | 34 +++++++++++++++++----------------- query/gremlin/functional_test.go | 6 +++--- query/gremlin/session.go | 28 ++++++++++++++-------------- query/gremlin/traversals.go | 8 ++++---- 7 files changed, 43 insertions(+), 43 deletions(-) diff --git a/db/repl.go b/db/repl.go index e07efc3..770fb33 100644 --- a/db/repl.go +++ b/db/repl.go @@ -70,7 +70,7 @@ func Repl(ts graph.TripleStore, queryLanguage string, cfg *config.Config) { case "gremlin": fallthrough default: - ses = gremlin.NewGremlinSession(ts, cfg.GremlinTimeout, true) + ses = gremlin.NewSession(ts, cfg.GremlinTimeout, true) } inputBf := bufio.NewReader(os.Stdin) line := "" diff --git a/http/query.go b/http/query.go index 8170e13..da11b7f 100644 --- a/http/query.go +++ b/http/query.go @@ -71,7 +71,7 @@ func (api *Api) ServeV1Query(w http.ResponseWriter, r *http.Request, params http var ses graph.HttpSession switch params.ByName("query_lang") { case "gremlin": - ses = gremlin.NewGremlinSession(api.ts, api.config.GremlinTimeout, false) + ses = gremlin.NewSession(api.ts, api.config.GremlinTimeout, false) case "mql": ses = mql.NewMqlSession(api.ts) default: @@ -120,7 +120,7 @@ func (api *Api) ServeV1Shape(w http.ResponseWriter, r *http.Request, params http var ses graph.HttpSession switch params.ByName("query_lang") { case "gremlin": - ses = gremlin.NewGremlinSession(api.ts, api.config.GremlinTimeout, false) + ses = gremlin.NewSession(api.ts, api.config.GremlinTimeout, false) case "mql": ses = mql.NewMqlSession(api.ts) default: diff --git a/query/gremlin/environ.go b/query/gremlin/environ.go index 4e7f332..503ad4f 100644 --- a/query/gremlin/environ.go +++ b/query/gremlin/environ.go @@ -21,7 +21,7 @@ import ( "github.com/robertkrimen/otto" ) -func BuildGremlinEnv(ses *GremlinSession) *otto.Otto { +func BuildEnviron(ses *Session) *otto.Otto { env := otto.New() setupGremlin(env, ses) return env @@ -55,7 +55,7 @@ func isVertexChain(obj *otto.Object) bool { return false } -func setupGremlin(env *otto.Otto, ses *GremlinSession) { +func setupGremlin(env *otto.Otto, ses *Session) { graph, _ := env.Object("graph = {}") graph.Set("Vertex", func(call otto.FunctionCall) otto.Value { call.Otto.Run("var out = {}") diff --git a/query/gremlin/finals.go b/query/gremlin/finals.go index f8c978d..57d3ba1 100644 --- a/query/gremlin/finals.go +++ b/query/gremlin/finals.go @@ -21,9 +21,9 @@ import ( "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("GetLimit", limitFunc(env, ses, obj)) 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)) } -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 { it := buildIteratorTree(obj, ses.ts) - it.AddTag(GremlinTopResultTag) + it.AddTag(TopResultTag) ses.limit = -1 ses.count = 0 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 { if len(call.ArgumentList) > 0 { limitVal, _ := call.Argument(0).ToInteger() it := buildIteratorTree(obj, ses.ts) - it.AddTag(GremlinTopResultTag) + it.AddTag(TopResultTag) ses.limit = int(limitVal) ses.count = 0 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 { it := buildIteratorTree(obj, ses.ts) - it.AddTag(GremlinTopResultTag) + it.AddTag(TopResultTag) limit := -1 if len(call.ArgumentList) > 0 { 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 { it := buildIteratorTree(obj, ses.ts) - it.AddTag(GremlinTopResultTag) + it.AddTag(TopResultTag) limit := 1 var val otto.Value 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 { it := buildIteratorTree(obj, ses.ts) - it.AddTag(GremlinTopResultTag) + it.AddTag(TopResultTag) limit := -1 if len(call.ArgumentList) == 0 { 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) for k, v := range m { outputMap[k] = ses.ts.GetNameFor(v) @@ -142,7 +142,7 @@ func tagsToValueMap(m map[string]graph.TSVal, ses *GremlinSession) map[string]st 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) count := 0 it, _ = it.Optimize() @@ -178,7 +178,7 @@ func runIteratorToArray(it graph.Iterator, ses *GremlinSession, limit int) []map 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) count := 0 it, _ = it.Optimize() @@ -200,7 +200,7 @@ func runIteratorToArrayNoTags(it graph.Iterator, ses *GremlinSession, limit int) 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 it, _ = it.Optimize() for { @@ -236,7 +236,7 @@ func runIteratorWithCallback(it graph.Iterator, ses *GremlinSession, callback ot it.Close() } -func runIteratorOnSession(it graph.Iterator, ses *GremlinSession) { +func runIteratorOnSession(it graph.Iterator, ses *Session) { if ses.lookingForQueryShape { graph.OutputQueryShapeForIterator(it, ses.ts, &(ses.queryShape)) return diff --git a/query/gremlin/functional_test.go b/query/gremlin/functional_test.go index f6c65fb..e1bf733 100644 --- a/query/gremlin/functional_test.go +++ b/query/gremlin/functional_test.go @@ -35,9 +35,9 @@ import ( // +---+ // -func buildTripleStore() *GremlinSession { +func buildTripleStore() *Session { ts := memstore.MakeTestingMemstore() - return NewGremlinSession(ts, -1, false) + return NewSession(ts, -1, false) } 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) { - ConveyQueryTag(doc, query, GremlinTopResultTag, expected) + ConveyQueryTag(doc, query, TopResultTag, expected) } func ConveyQueryTag(doc string, query string, tag string, expected []string) { diff --git a/query/gremlin/session.go b/query/gremlin/session.go index a0b0483..ab490b9 100644 --- a/query/gremlin/session.go +++ b/query/gremlin/session.go @@ -25,7 +25,7 @@ import ( "github.com/google/cayley/graph" ) -type GremlinSession struct { +type Session struct { ts graph.TripleStore currentChannel chan interface{} env *otto.Otto @@ -42,10 +42,10 @@ type GremlinSession struct { emptyEnv *otto.Otto } -func NewGremlinSession(inputTripleStore graph.TripleStore, timeoutSec int, persist bool) *GremlinSession { - var g GremlinSession +func NewSession(inputTripleStore graph.TripleStore, timeoutSec int, persist bool) *Session { + var g Session g.ts = inputTripleStore - g.env = BuildGremlinEnv(&g) + g.env = BuildEnviron(&g) g.limit = -1 g.count = 0 g.lookingForQueryShape = false @@ -68,11 +68,11 @@ type GremlinResult struct { actualResults *map[string]graph.TSVal } -func (g *GremlinSession) ToggleDebug() { +func (g *Session) ToggleDebug() { 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) g.queryShape = make(map[string]interface{}) g.lookingForQueryShape = true @@ -81,7 +81,7 @@ func (g *GremlinSession) GetQuery(input string, output_struct chan map[string]in 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) if err != nil { return graph.ParseFail, err @@ -90,7 +90,7 @@ func (g *GremlinSession) InputParses(input string) (graph.ParseResult, error) { 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 { return false } @@ -111,7 +111,7 @@ func (g *GremlinSession) SendResult(result *GremlinResult) bool { 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 defer func() { 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) } -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) g.err = nil g.currentChannel = out @@ -169,7 +169,7 @@ func (g *GremlinSession) ExecInput(input string, out chan interface{}, limit int return } -func (s *GremlinSession) ToText(result interface{}) string { +func (s *Session) ToText(result interface{}) string { data := result.(*GremlinResult) if data.metaresult { if data.err != "" { @@ -220,7 +220,7 @@ func (s *GremlinSession) ToText(result interface{}) string { } // Web stuff -func (ses *GremlinSession) BuildJson(result interface{}) { +func (ses *Session) BuildJson(result interface{}) { data := result.(*GremlinResult) if !data.metaresult { 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() if ses.err != nil { return nil, ses.err @@ -261,6 +261,6 @@ func (ses *GremlinSession) GetJson() (interface{}, error) { return ses.dataOutput, nil } -func (ses *GremlinSession) ClearJson() { +func (ses *Session) ClearJson() { ses.dataOutput = nil } diff --git a/query/gremlin/traversals.go b/query/gremlin/traversals.go index c0f4704..32d4490 100644 --- a/query/gremlin/traversals.go +++ b/query/gremlin/traversals.go @@ -21,7 +21,7 @@ import ( "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("Out", gremlinFunc("out", 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)) } -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 { call.Otto.Run("var 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 { call.Otto.Run("var 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 { call.Otto.Run("var out = {}") out, _ := call.Otto.Object("out")