Move query language packages into query
This commit is contained in:
parent
a5f8391739
commit
c66d6d3994
26 changed files with 2193 additions and 2193 deletions
315
query/gremlin/gremlin_build_iterator.go
Normal file
315
query/gremlin/gremlin_build_iterator.go
Normal file
|
|
@ -0,0 +1,315 @@
|
|||
// 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 gremlin
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
|
||||
"github.com/barakmich/glog"
|
||||
"github.com/robertkrimen/otto"
|
||||
|
||||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
func getStrings(obj *otto.Object, field string) []string {
|
||||
strings := make([]string, 0)
|
||||
val, _ := obj.Get(field)
|
||||
if !val.IsUndefined() {
|
||||
export, _ := val.Export()
|
||||
array := export.([]interface{})
|
||||
for _, arg := range array {
|
||||
strings = append(strings, arg.(string))
|
||||
}
|
||||
}
|
||||
return strings
|
||||
}
|
||||
|
||||
func getStringArgs(obj *otto.Object) []string { return getStrings(obj, "string_args") }
|
||||
|
||||
func buildIteratorTree(obj *otto.Object, ts graph.TripleStore) graph.Iterator {
|
||||
if !isVertexChain(obj) {
|
||||
return graph.NewNullIterator()
|
||||
}
|
||||
return buildIteratorTreeHelper(obj, ts, graph.NewNullIterator())
|
||||
}
|
||||
|
||||
func makeListOfStringsFromArrayValue(obj *otto.Object) []string {
|
||||
var output []string
|
||||
lengthValue, _ := obj.Get("length")
|
||||
length, _ := lengthValue.ToInteger()
|
||||
ulength := uint32(length)
|
||||
for index := uint32(0); index < ulength; index += 1 {
|
||||
name := strconv.FormatInt(int64(index), 10)
|
||||
value, err := obj.Get(name)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if !value.IsString() {
|
||||
continue
|
||||
}
|
||||
s, _ := value.ToString()
|
||||
output = append(output, s)
|
||||
}
|
||||
return output
|
||||
}
|
||||
|
||||
func buildIteratorFromValue(val otto.Value, ts graph.TripleStore) graph.Iterator {
|
||||
if val.IsNull() || val.IsUndefined() {
|
||||
return ts.GetNodesAllIterator()
|
||||
}
|
||||
if val.IsPrimitive() {
|
||||
thing, _ := val.Export()
|
||||
switch v := thing.(type) {
|
||||
case string:
|
||||
it := ts.MakeFixed()
|
||||
it.AddValue(ts.GetIdFor(v))
|
||||
return it
|
||||
default:
|
||||
glog.Errorln("Trying to build unknown primitive value.")
|
||||
}
|
||||
}
|
||||
switch val.Class() {
|
||||
case "Object":
|
||||
return buildIteratorTree(val.Object(), ts)
|
||||
case "Array":
|
||||
// Had better be an array of strings
|
||||
strings := makeListOfStringsFromArrayValue(val.Object())
|
||||
it := ts.MakeFixed()
|
||||
for _, x := range strings {
|
||||
it.AddValue(ts.GetIdFor(x))
|
||||
}
|
||||
return it
|
||||
case "Number":
|
||||
fallthrough
|
||||
case "Boolean":
|
||||
fallthrough
|
||||
case "Date":
|
||||
fallthrough
|
||||
case "String":
|
||||
it := ts.MakeFixed()
|
||||
str, _ := val.ToString()
|
||||
it.AddValue(ts.GetIdFor(str))
|
||||
return it
|
||||
default:
|
||||
glog.Errorln("Trying to handle unsupported Javascript value.")
|
||||
return graph.NewNullIterator()
|
||||
}
|
||||
}
|
||||
|
||||
func buildInOutIterator(obj *otto.Object, ts graph.TripleStore, base graph.Iterator, isReverse bool) graph.Iterator {
|
||||
argList, _ := obj.Get("_gremlin_values")
|
||||
if argList.Class() != "GoArray" {
|
||||
glog.Errorln("How is arglist not an array? Return nothing.", argList.Class())
|
||||
return graph.NewNullIterator()
|
||||
}
|
||||
argArray := argList.Object()
|
||||
lengthVal, _ := argArray.Get("length")
|
||||
length, _ := lengthVal.ToInteger()
|
||||
var predicateNodeIterator graph.Iterator
|
||||
if length == 0 {
|
||||
predicateNodeIterator = ts.GetNodesAllIterator()
|
||||
} else {
|
||||
zero, _ := argArray.Get("0")
|
||||
predicateNodeIterator = buildIteratorFromValue(zero, ts)
|
||||
}
|
||||
if length >= 2 {
|
||||
var tags []string
|
||||
one, _ := argArray.Get("1")
|
||||
if one.IsString() {
|
||||
s, _ := one.ToString()
|
||||
tags = append(tags, s)
|
||||
} else if one.Class() == "Array" {
|
||||
tags = makeListOfStringsFromArrayValue(one.Object())
|
||||
}
|
||||
for _, tag := range tags {
|
||||
predicateNodeIterator.AddTag(tag)
|
||||
}
|
||||
}
|
||||
|
||||
in, out := "s", "o"
|
||||
if isReverse {
|
||||
in, out = out, in
|
||||
}
|
||||
lto := graph.NewLinksToIterator(ts, base, in)
|
||||
and := graph.NewAndIterator()
|
||||
and.AddSubIterator(graph.NewLinksToIterator(ts, predicateNodeIterator, "p"))
|
||||
and.AddSubIterator(lto)
|
||||
return graph.NewHasaIterator(ts, and, out)
|
||||
}
|
||||
|
||||
func buildIteratorTreeHelper(obj *otto.Object, ts graph.TripleStore, base graph.Iterator) graph.Iterator {
|
||||
var it graph.Iterator
|
||||
it = base
|
||||
// TODO: Better error handling
|
||||
kindVal, _ := obj.Get("_gremlin_type")
|
||||
stringArgs := getStringArgs(obj)
|
||||
var subIt graph.Iterator
|
||||
prevVal, _ := obj.Get("_gremlin_prev")
|
||||
if !prevVal.IsObject() {
|
||||
subIt = base
|
||||
} else {
|
||||
subIt = buildIteratorTreeHelper(prevVal.Object(), ts, base)
|
||||
}
|
||||
|
||||
kind, _ := kindVal.ToString()
|
||||
switch kind {
|
||||
case "vertex":
|
||||
if len(stringArgs) == 0 {
|
||||
it = ts.GetNodesAllIterator()
|
||||
} else {
|
||||
fixed := ts.MakeFixed()
|
||||
for _, name := range stringArgs {
|
||||
fixed.AddValue(ts.GetIdFor(name))
|
||||
}
|
||||
it = fixed
|
||||
}
|
||||
case "tag":
|
||||
it = subIt
|
||||
for _, tag := range stringArgs {
|
||||
it.AddTag(tag)
|
||||
}
|
||||
case "save":
|
||||
all := ts.GetNodesAllIterator()
|
||||
if len(stringArgs) > 2 || len(stringArgs) == 0 {
|
||||
return graph.NewNullIterator()
|
||||
}
|
||||
if len(stringArgs) == 2 {
|
||||
all.AddTag(stringArgs[1])
|
||||
} else {
|
||||
all.AddTag(stringArgs[0])
|
||||
}
|
||||
predFixed := ts.MakeFixed()
|
||||
predFixed.AddValue(ts.GetIdFor(stringArgs[0]))
|
||||
subAnd := graph.NewAndIterator()
|
||||
subAnd.AddSubIterator(graph.NewLinksToIterator(ts, predFixed, "p"))
|
||||
subAnd.AddSubIterator(graph.NewLinksToIterator(ts, all, "o"))
|
||||
hasa := graph.NewHasaIterator(ts, subAnd, "s")
|
||||
and := graph.NewAndIterator()
|
||||
and.AddSubIterator(hasa)
|
||||
and.AddSubIterator(subIt)
|
||||
it = and
|
||||
case "saver":
|
||||
all := ts.GetNodesAllIterator()
|
||||
if len(stringArgs) > 2 || len(stringArgs) == 0 {
|
||||
return graph.NewNullIterator()
|
||||
}
|
||||
if len(stringArgs) == 2 {
|
||||
all.AddTag(stringArgs[1])
|
||||
} else {
|
||||
all.AddTag(stringArgs[0])
|
||||
}
|
||||
predFixed := ts.MakeFixed()
|
||||
predFixed.AddValue(ts.GetIdFor(stringArgs[0]))
|
||||
subAnd := graph.NewAndIterator()
|
||||
subAnd.AddSubIterator(graph.NewLinksToIterator(ts, predFixed, "p"))
|
||||
subAnd.AddSubIterator(graph.NewLinksToIterator(ts, all, "s"))
|
||||
hasa := graph.NewHasaIterator(ts, subAnd, "o")
|
||||
and := graph.NewAndIterator()
|
||||
and.AddSubIterator(hasa)
|
||||
and.AddSubIterator(subIt)
|
||||
it = and
|
||||
case "has":
|
||||
fixed := ts.MakeFixed()
|
||||
if len(stringArgs) < 2 {
|
||||
return graph.NewNullIterator()
|
||||
}
|
||||
for _, name := range stringArgs[1:] {
|
||||
fixed.AddValue(ts.GetIdFor(name))
|
||||
}
|
||||
predFixed := ts.MakeFixed()
|
||||
predFixed.AddValue(ts.GetIdFor(stringArgs[0]))
|
||||
subAnd := graph.NewAndIterator()
|
||||
subAnd.AddSubIterator(graph.NewLinksToIterator(ts, predFixed, "p"))
|
||||
subAnd.AddSubIterator(graph.NewLinksToIterator(ts, fixed, "o"))
|
||||
hasa := graph.NewHasaIterator(ts, subAnd, "s")
|
||||
and := graph.NewAndIterator()
|
||||
and.AddSubIterator(hasa)
|
||||
and.AddSubIterator(subIt)
|
||||
it = and
|
||||
case "morphism":
|
||||
it = base
|
||||
case "and":
|
||||
arg, _ := obj.Get("_gremlin_values")
|
||||
firstArg, _ := arg.Object().Get("0")
|
||||
if !isVertexChain(firstArg.Object()) {
|
||||
return graph.NewNullIterator()
|
||||
}
|
||||
argIt := buildIteratorTree(firstArg.Object(), ts)
|
||||
|
||||
and := graph.NewAndIterator()
|
||||
and.AddSubIterator(subIt)
|
||||
and.AddSubIterator(argIt)
|
||||
it = and
|
||||
case "back":
|
||||
arg, _ := obj.Get("_gremlin_back_chain")
|
||||
argIt := buildIteratorTree(arg.Object(), ts)
|
||||
and := graph.NewAndIterator()
|
||||
and.AddSubIterator(subIt)
|
||||
and.AddSubIterator(argIt)
|
||||
it = and
|
||||
case "is":
|
||||
fixed := ts.MakeFixed()
|
||||
for _, name := range stringArgs {
|
||||
fixed.AddValue(ts.GetIdFor(name))
|
||||
}
|
||||
and := graph.NewAndIterator()
|
||||
and.AddSubIterator(fixed)
|
||||
and.AddSubIterator(subIt)
|
||||
it = and
|
||||
case "or":
|
||||
arg, _ := obj.Get("_gremlin_values")
|
||||
firstArg, _ := arg.Object().Get("0")
|
||||
if !isVertexChain(firstArg.Object()) {
|
||||
return graph.NewNullIterator()
|
||||
}
|
||||
argIt := buildIteratorTree(firstArg.Object(), ts)
|
||||
|
||||
or := graph.NewOrIterator()
|
||||
or.AddSubIterator(subIt)
|
||||
or.AddSubIterator(argIt)
|
||||
it = or
|
||||
case "both":
|
||||
// Hardly the most efficient pattern, but the most general.
|
||||
// Worth looking into an Optimize() optimization here.
|
||||
clone := subIt.Clone()
|
||||
it1 := buildInOutIterator(obj, ts, subIt, false)
|
||||
it2 := buildInOutIterator(obj, ts, clone, true)
|
||||
|
||||
or := graph.NewOrIterator()
|
||||
or.AddSubIterator(it1)
|
||||
or.AddSubIterator(it2)
|
||||
it = or
|
||||
case "out":
|
||||
it = buildInOutIterator(obj, ts, subIt, false)
|
||||
case "follow":
|
||||
// Follow a morphism
|
||||
arg, _ := obj.Get("_gremlin_values")
|
||||
firstArg, _ := arg.Object().Get("0")
|
||||
if isVertexChain(firstArg.Object()) {
|
||||
return graph.NewNullIterator()
|
||||
}
|
||||
it = buildIteratorTreeHelper(firstArg.Object(), ts, subIt)
|
||||
case "followr":
|
||||
// Follow a morphism
|
||||
arg, _ := obj.Get("_gremlin_followr")
|
||||
if isVertexChain(arg.Object()) {
|
||||
return graph.NewNullIterator()
|
||||
}
|
||||
it = buildIteratorTreeHelper(arg.Object(), ts, subIt)
|
||||
case "in":
|
||||
it = buildInOutIterator(obj, ts, subIt, true)
|
||||
}
|
||||
return it
|
||||
}
|
||||
95
query/gremlin/gremlin_env.go
Normal file
95
query/gremlin/gremlin_env.go
Normal file
|
|
@ -0,0 +1,95 @@
|
|||
// 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 gremlin
|
||||
|
||||
// Builds a new Gremlin environment pointing at a session.
|
||||
|
||||
import (
|
||||
"github.com/barakmich/glog"
|
||||
"github.com/robertkrimen/otto"
|
||||
)
|
||||
|
||||
func BuildGremlinEnv(ses *GremlinSession) *otto.Otto {
|
||||
env := otto.New()
|
||||
setupGremlin(env, ses)
|
||||
return env
|
||||
}
|
||||
|
||||
func concatStringArgs(call otto.FunctionCall) *[]interface{} {
|
||||
outStrings := make([]interface{}, 0)
|
||||
for _, arg := range call.ArgumentList {
|
||||
if arg.IsString() {
|
||||
outStrings = append(outStrings, arg.String())
|
||||
}
|
||||
if arg.IsObject() && arg.Class() == "Array" {
|
||||
obj, _ := arg.Export()
|
||||
for _, x := range obj.([]interface{}) {
|
||||
outStrings = append(outStrings, x.(string))
|
||||
}
|
||||
}
|
||||
}
|
||||
return &outStrings
|
||||
}
|
||||
|
||||
func isVertexChain(obj *otto.Object) bool {
|
||||
val, _ := obj.Get("_gremlin_type")
|
||||
if x, _ := val.ToString(); x == "vertex" {
|
||||
return true
|
||||
}
|
||||
val, _ = obj.Get("_gremlin_prev")
|
||||
if val.IsObject() {
|
||||
return isVertexChain(val.Object())
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func setupGremlin(env *otto.Otto, ses *GremlinSession) {
|
||||
graph, _ := env.Object("graph = {}")
|
||||
graph.Set("Vertex", func(call otto.FunctionCall) otto.Value {
|
||||
call.Otto.Run("var out = {}")
|
||||
out, err := call.Otto.Object("out")
|
||||
if err != nil {
|
||||
glog.Error(err.Error())
|
||||
return otto.TrueValue()
|
||||
}
|
||||
out.Set("_gremlin_type", "vertex")
|
||||
outStrings := concatStringArgs(call)
|
||||
if len(*outStrings) > 0 {
|
||||
out.Set("string_args", *outStrings)
|
||||
}
|
||||
embedTraversals(env, ses, out)
|
||||
embedFinals(env, ses, out)
|
||||
return out.Value()
|
||||
})
|
||||
|
||||
graph.Set("Morphism", func(call otto.FunctionCall) otto.Value {
|
||||
call.Otto.Run("var out = {}")
|
||||
out, _ := call.Otto.Object("out")
|
||||
out.Set("_gremlin_type", "morphism")
|
||||
embedTraversals(env, ses, out)
|
||||
return out.Value()
|
||||
})
|
||||
graph.Set("Emit", func(call otto.FunctionCall) otto.Value {
|
||||
value := call.Argument(0)
|
||||
if value.IsDefined() {
|
||||
ses.SendResult(&GremlinResult{metaresult: false, err: "", val: &value, actualResults: nil})
|
||||
}
|
||||
return otto.NullValue()
|
||||
})
|
||||
env.Run("graph.V = graph.Vertex")
|
||||
env.Run("graph.M = graph.Morphism")
|
||||
env.Run("g = graph")
|
||||
|
||||
}
|
||||
274
query/gremlin/gremlin_finals.go
Normal file
274
query/gremlin/gremlin_finals.go
Normal file
|
|
@ -0,0 +1,274 @@
|
|||
// 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 gremlin
|
||||
|
||||
import (
|
||||
"github.com/barakmich/glog"
|
||||
"github.com/robertkrimen/otto"
|
||||
|
||||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
const GremlinTopResultTag = "id"
|
||||
|
||||
func embedFinals(env *otto.Otto, ses *GremlinSession, 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))
|
||||
obj.Set("ToValue", toValueFunc(env, ses, obj, false))
|
||||
obj.Set("TagArray", toArrayFunc(env, ses, obj, true))
|
||||
obj.Set("TagValue", toValueFunc(env, ses, obj, true))
|
||||
obj.Set("Map", 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 {
|
||||
return func(call otto.FunctionCall) otto.Value {
|
||||
it := buildIteratorTree(obj, ses.ts)
|
||||
it.AddTag(GremlinTopResultTag)
|
||||
ses.limit = -1
|
||||
ses.count = 0
|
||||
runIteratorOnSession(it, ses)
|
||||
return otto.NullValue()
|
||||
}
|
||||
}
|
||||
|
||||
func limitFunc(env *otto.Otto, ses *GremlinSession, 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)
|
||||
ses.limit = int(limitVal)
|
||||
ses.count = 0
|
||||
runIteratorOnSession(it, ses)
|
||||
}
|
||||
return otto.NullValue()
|
||||
}
|
||||
}
|
||||
|
||||
func toArrayFunc(env *otto.Otto, ses *GremlinSession, 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)
|
||||
limit := -1
|
||||
if len(call.ArgumentList) > 0 {
|
||||
limitParsed, _ := call.Argument(0).ToInteger()
|
||||
limit = int(limitParsed)
|
||||
}
|
||||
var val otto.Value
|
||||
var err error
|
||||
if !withTags {
|
||||
array := runIteratorToArrayNoTags(it, ses, limit)
|
||||
val, err = call.Otto.ToValue(array)
|
||||
} else {
|
||||
array := runIteratorToArray(it, ses, limit)
|
||||
val, err = call.Otto.ToValue(array)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return otto.NullValue()
|
||||
}
|
||||
return val
|
||||
}
|
||||
}
|
||||
|
||||
func toValueFunc(env *otto.Otto, ses *GremlinSession, 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)
|
||||
limit := 1
|
||||
var val otto.Value
|
||||
var err error
|
||||
if !withTags {
|
||||
array := runIteratorToArrayNoTags(it, ses, limit)
|
||||
if len(array) < 1 {
|
||||
return otto.NullValue()
|
||||
}
|
||||
val, err = call.Otto.ToValue(array[0])
|
||||
} else {
|
||||
array := runIteratorToArray(it, ses, limit)
|
||||
if len(array) < 1 {
|
||||
return otto.NullValue()
|
||||
}
|
||||
val, err = call.Otto.ToValue(array[0])
|
||||
}
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return otto.NullValue()
|
||||
} else {
|
||||
return val
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func mapFunc(env *otto.Otto, ses *GremlinSession, obj *otto.Object) func(otto.FunctionCall) otto.Value {
|
||||
return func(call otto.FunctionCall) otto.Value {
|
||||
it := buildIteratorTree(obj, ses.ts)
|
||||
it.AddTag(GremlinTopResultTag)
|
||||
limit := -1
|
||||
if len(call.ArgumentList) == 0 {
|
||||
return otto.NullValue()
|
||||
}
|
||||
callback := call.Argument(len(call.ArgumentList) - 1)
|
||||
if len(call.ArgumentList) > 1 {
|
||||
limitParsed, _ := call.Argument(0).ToInteger()
|
||||
limit = int(limitParsed)
|
||||
}
|
||||
runIteratorWithCallback(it, ses, callback, call, limit)
|
||||
return otto.NullValue()
|
||||
}
|
||||
}
|
||||
|
||||
func tagsToValueMap(m map[string]graph.TSVal, ses *GremlinSession) map[string]string {
|
||||
outputMap := make(map[string]string)
|
||||
for k, v := range m {
|
||||
outputMap[k] = ses.ts.GetNameFor(v)
|
||||
}
|
||||
return outputMap
|
||||
}
|
||||
|
||||
func runIteratorToArray(it graph.Iterator, ses *GremlinSession, limit int) []map[string]string {
|
||||
output := make([]map[string]string, 0)
|
||||
count := 0
|
||||
it, _ = it.Optimize()
|
||||
for {
|
||||
if ses.doHalt {
|
||||
return nil
|
||||
}
|
||||
_, ok := it.Next()
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
tags := make(map[string]graph.TSVal)
|
||||
it.TagResults(&tags)
|
||||
output = append(output, tagsToValueMap(tags, ses))
|
||||
count++
|
||||
if limit >= 0 && count >= limit {
|
||||
break
|
||||
}
|
||||
for it.NextResult() == true {
|
||||
if ses.doHalt {
|
||||
return nil
|
||||
}
|
||||
tags := make(map[string]graph.TSVal)
|
||||
it.TagResults(&tags)
|
||||
output = append(output, tagsToValueMap(tags, ses))
|
||||
count++
|
||||
if limit >= 0 && count >= limit {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
it.Close()
|
||||
return output
|
||||
}
|
||||
|
||||
func runIteratorToArrayNoTags(it graph.Iterator, ses *GremlinSession, limit int) []string {
|
||||
output := make([]string, 0)
|
||||
count := 0
|
||||
it, _ = it.Optimize()
|
||||
for {
|
||||
if ses.doHalt {
|
||||
return nil
|
||||
}
|
||||
val, ok := it.Next()
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
output = append(output, ses.ts.GetNameFor(val))
|
||||
count++
|
||||
if limit >= 0 && count >= limit {
|
||||
break
|
||||
}
|
||||
}
|
||||
it.Close()
|
||||
return output
|
||||
}
|
||||
|
||||
func runIteratorWithCallback(it graph.Iterator, ses *GremlinSession, callback otto.Value, this otto.FunctionCall, limit int) {
|
||||
count := 0
|
||||
it, _ = it.Optimize()
|
||||
for {
|
||||
if ses.doHalt {
|
||||
return
|
||||
}
|
||||
_, ok := it.Next()
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
tags := make(map[string]graph.TSVal)
|
||||
it.TagResults(&tags)
|
||||
val, _ := this.Otto.ToValue(tagsToValueMap(tags, ses))
|
||||
val, _ = callback.Call(this.This, val)
|
||||
count++
|
||||
if limit >= 0 && count >= limit {
|
||||
break
|
||||
}
|
||||
for it.NextResult() == true {
|
||||
if ses.doHalt {
|
||||
return
|
||||
}
|
||||
tags := make(map[string]graph.TSVal)
|
||||
it.TagResults(&tags)
|
||||
val, _ := this.Otto.ToValue(tagsToValueMap(tags, ses))
|
||||
val, _ = callback.Call(this.This, val)
|
||||
count++
|
||||
if limit >= 0 && count >= limit {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
it.Close()
|
||||
}
|
||||
|
||||
func runIteratorOnSession(it graph.Iterator, ses *GremlinSession) {
|
||||
if ses.lookingForQueryShape {
|
||||
graph.OutputQueryShapeForIterator(it, ses.ts, &(ses.queryShape))
|
||||
return
|
||||
}
|
||||
it, _ = it.Optimize()
|
||||
glog.V(2).Infoln(it.DebugString(0))
|
||||
for {
|
||||
// TODO(barakmich): Better halting.
|
||||
if ses.doHalt {
|
||||
return
|
||||
}
|
||||
_, ok := it.Next()
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
tags := make(map[string]graph.TSVal)
|
||||
it.TagResults(&tags)
|
||||
cont := ses.SendResult(&GremlinResult{metaresult: false, err: "", val: nil, actualResults: &tags})
|
||||
if !cont {
|
||||
break
|
||||
}
|
||||
for it.NextResult() == true {
|
||||
if ses.doHalt {
|
||||
return
|
||||
}
|
||||
tags := make(map[string]graph.TSVal)
|
||||
it.TagResults(&tags)
|
||||
cont := ses.SendResult(&GremlinResult{metaresult: false, err: "", val: nil, actualResults: &tags})
|
||||
if !cont {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
it.Close()
|
||||
}
|
||||
230
query/gremlin/gremlin_functional_test.go
Normal file
230
query/gremlin/gremlin_functional_test.go
Normal file
|
|
@ -0,0 +1,230 @@
|
|||
// 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 gremlin
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"testing"
|
||||
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
|
||||
"github.com/google/cayley/graph/memstore"
|
||||
)
|
||||
|
||||
// +---+ +---+
|
||||
// | A |------- ->| F |<--
|
||||
// +---+ \------>+---+-/ +---+ \--+---+
|
||||
// ------>|#B#| | | E |
|
||||
// +---+-------/ >+---+ | +---+
|
||||
// | C | / v
|
||||
// +---+ -/ +---+
|
||||
// ---- +---+/ |#G#|
|
||||
// \-->|#D#|------------->+---+
|
||||
// +---+
|
||||
//
|
||||
|
||||
func buildTripleStore() *GremlinSession {
|
||||
ts := memstore.MakeTestingMemstore()
|
||||
return NewGremlinSession(ts, -1, false)
|
||||
}
|
||||
|
||||
func shouldBeUnordered(actual interface{}, expected ...interface{}) string {
|
||||
if len(expected) != 1 {
|
||||
return "Only one list supported"
|
||||
}
|
||||
actualStr := actual.([]string)
|
||||
expectedStr := expected[0].([]string)
|
||||
sort.Strings(actualStr)
|
||||
sort.Strings(expectedStr)
|
||||
return ShouldResemble(actualStr, expectedStr)
|
||||
}
|
||||
|
||||
func runQueryGetTag(query string, tag string) ([]string, int) {
|
||||
js := buildTripleStore()
|
||||
output := make([]string, 0)
|
||||
c := make(chan interface{}, 5)
|
||||
js.ExecInput(query, c, -1)
|
||||
count := 0
|
||||
for result := range c {
|
||||
count++
|
||||
data := result.(*GremlinResult)
|
||||
if data.val == nil {
|
||||
val := (*data.actualResults)[tag]
|
||||
if val != nil {
|
||||
output = append(output, js.ts.GetNameFor(val))
|
||||
}
|
||||
}
|
||||
}
|
||||
return output, count
|
||||
}
|
||||
|
||||
func ConveyQuery(doc string, query string, expected []string) {
|
||||
ConveyQueryTag(doc, query, GremlinTopResultTag, expected)
|
||||
}
|
||||
|
||||
func ConveyQueryTag(doc string, query string, tag string, expected []string) {
|
||||
Convey(doc, func() {
|
||||
actual, _ := runQueryGetTag(query, tag)
|
||||
So(actual, shouldBeUnordered, expected)
|
||||
})
|
||||
}
|
||||
|
||||
func TestGremlin(t *testing.T) {
|
||||
Convey("With a default memtriplestore", t, func() {
|
||||
|
||||
ConveyQuery("Can get a single vertex",
|
||||
`g.V("A").All()`,
|
||||
[]string{"A"})
|
||||
|
||||
ConveyQuery("Can use .Out()",
|
||||
`g.V("A").Out("follows").All()`,
|
||||
[]string{"B"})
|
||||
|
||||
ConveyQuery("Can use .In()",
|
||||
`g.V("B").In("follows").All()`,
|
||||
[]string{"A", "C", "D"})
|
||||
|
||||
ConveyQuery("Can use .Both()",
|
||||
`g.V("F").Both("follows").All()`,
|
||||
[]string{"B", "G", "E"})
|
||||
|
||||
ConveyQuery("Can use .Tag()-.Is()-.Back()",
|
||||
`g.V("B").In("follows").Tag("foo").Out("status").Is("cool").Back("foo").All()`,
|
||||
[]string{"D"})
|
||||
|
||||
ConveyQuery("Can separate .Tag()-.Is()-.Back()",
|
||||
`
|
||||
x = g.V("C").Out("follows").Tag("foo").Out("status").Is("cool").Back("foo")
|
||||
x.In("follows").Is("D").Back("foo").All()
|
||||
`,
|
||||
[]string{"B"})
|
||||
|
||||
Convey("Can do multiple .Back()s", func() {
|
||||
query := `
|
||||
g.V("E").Out("follows").As("f").Out("follows").Out("status").Is("cool").Back("f").In("follows").In("follows").As("acd").Out("status").Is("cool").Back("f").All()
|
||||
`
|
||||
expected := []string{"D"}
|
||||
actual, _ := runQueryGetTag(query, "acd")
|
||||
So(actual, shouldBeUnordered, expected)
|
||||
})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestGremlinMorphism(t *testing.T) {
|
||||
Convey("With a default memtriplestore", t, func() {
|
||||
|
||||
ConveyQuery("Simple morphism works",
|
||||
`
|
||||
grandfollows = g.M().Out("follows").Out("follows")
|
||||
g.V("C").Follow(grandfollows).All()
|
||||
`,
|
||||
[]string{"G", "F", "B"})
|
||||
|
||||
ConveyQuery("Reverse morphism works",
|
||||
`
|
||||
grandfollows = g.M().Out("follows").Out("follows")
|
||||
g.V("F").FollowR(grandfollows).All()
|
||||
`, []string{"A", "C", "D"})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestGremlinIntersection(t *testing.T) {
|
||||
Convey("With a default memtriplestore", t, func() {
|
||||
ConveyQuery("Simple intersection",
|
||||
`
|
||||
function follows(x) { return g.V(x).Out("follows") }
|
||||
|
||||
follows("D").And(follows("C")).All()
|
||||
`, []string{"B"})
|
||||
|
||||
ConveyQuery("Simple Morphism Intersection",
|
||||
`
|
||||
grandfollows = g.M().Out("follows").Out("follows")
|
||||
function gfollows(x) { return g.V(x).Follow(grandfollows) }
|
||||
|
||||
gfollows("A").And(gfollows("C")).All()
|
||||
`, []string{"F"})
|
||||
|
||||
ConveyQuery("Double Morphism Intersection",
|
||||
`
|
||||
grandfollows = g.M().Out("follows").Out("follows")
|
||||
function gfollows(x) { return g.V(x).Follow(grandfollows) }
|
||||
|
||||
gfollows("E").And(gfollows("C")).And(gfollows("B")).All()
|
||||
`, []string{"G"})
|
||||
|
||||
ConveyQuery("Reverse Intersection",
|
||||
`
|
||||
grandfollows = g.M().Out("follows").Out("follows")
|
||||
|
||||
g.V("G").FollowR(grandfollows).Intersect(g.V("F").FollowR(grandfollows)).All()
|
||||
`, []string{"C"})
|
||||
|
||||
ConveyQuery("Standard sort of morphism intersection, continue follow",
|
||||
`
|
||||
gfollowers = g.M().In("follows").In("follows")
|
||||
function cool(x) { return g.V(x).As("a").Out("status").Is("cool").Back("a") }
|
||||
cool("G").Follow(gfollowers).Intersect(cool("B").Follow(gfollowers)).All()
|
||||
`, []string{"C"})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestGremlinHas(t *testing.T) {
|
||||
Convey("With a default memtriplestore", t, func() {
|
||||
ConveyQuery("Test a simple Has",
|
||||
`g.V().Has("status", "cool").All()`,
|
||||
[]string{"G", "D", "B"})
|
||||
|
||||
ConveyQuery("Test a double Has",
|
||||
`g.V().Has("status", "cool").Has("follows", "F").All()`,
|
||||
[]string{"B"})
|
||||
|
||||
})
|
||||
}
|
||||
|
||||
func TestGremlinTag(t *testing.T) {
|
||||
Convey("With a default memtriplestore", t, func() {
|
||||
ConveyQueryTag("Test a simple save",
|
||||
`g.V().Save("status", "somecool").All()`,
|
||||
"somecool",
|
||||
[]string{"cool", "cool", "cool"})
|
||||
|
||||
ConveyQueryTag("Test a simple saveR",
|
||||
`g.V("cool").SaveR("status", "who").All()`,
|
||||
"who",
|
||||
[]string{"G", "D", "B"})
|
||||
|
||||
ConveyQueryTag("Test an out save",
|
||||
`g.V("D").Out(null, "pred").All()`,
|
||||
"pred",
|
||||
[]string{"follows", "follows", "status"})
|
||||
|
||||
ConveyQueryTag("Test a tag list",
|
||||
`g.V("D").Out(null, ["pred", "foo", "bar"]).All()`,
|
||||
"foo",
|
||||
[]string{"follows", "follows", "status"})
|
||||
|
||||
ConveyQuery("Test a pred list",
|
||||
`g.V("D").Out(["follows", "status"]).All()`,
|
||||
[]string{"B", "G", "cool"})
|
||||
|
||||
ConveyQuery("Test a predicate path",
|
||||
`g.V("D").Out(g.V("follows"), "pred").All()`,
|
||||
[]string{"B", "G"})
|
||||
})
|
||||
}
|
||||
266
query/gremlin/gremlin_session.go
Normal file
266
query/gremlin/gremlin_session.go
Normal file
|
|
@ -0,0 +1,266 @@
|
|||
// 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 gremlin
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"sort"
|
||||
"time"
|
||||
|
||||
"github.com/robertkrimen/otto"
|
||||
|
||||
"github.com/google/cayley/graph"
|
||||
)
|
||||
|
||||
type GremlinSession struct {
|
||||
ts graph.TripleStore
|
||||
currentChannel chan interface{}
|
||||
env *otto.Otto
|
||||
debug bool
|
||||
limit int
|
||||
count int
|
||||
dataOutput []interface{}
|
||||
lookingForQueryShape bool
|
||||
queryShape map[string]interface{}
|
||||
err error
|
||||
script *otto.Script
|
||||
doHalt bool
|
||||
timeoutSec time.Duration
|
||||
emptyEnv *otto.Otto
|
||||
}
|
||||
|
||||
func NewGremlinSession(inputTripleStore graph.TripleStore, timeoutSec int, persist bool) *GremlinSession {
|
||||
var g GremlinSession
|
||||
g.ts = inputTripleStore
|
||||
g.env = BuildGremlinEnv(&g)
|
||||
g.limit = -1
|
||||
g.count = 0
|
||||
g.lookingForQueryShape = false
|
||||
if persist {
|
||||
g.emptyEnv = g.env
|
||||
}
|
||||
if timeoutSec < 0 {
|
||||
g.timeoutSec = time.Duration(-1)
|
||||
} else {
|
||||
g.timeoutSec = time.Duration(timeoutSec)
|
||||
}
|
||||
g.ClearJson()
|
||||
return &g
|
||||
}
|
||||
|
||||
type GremlinResult struct {
|
||||
metaresult bool
|
||||
err string
|
||||
val *otto.Value
|
||||
actualResults *map[string]graph.TSVal
|
||||
}
|
||||
|
||||
func (g *GremlinSession) ToggleDebug() {
|
||||
g.debug = !g.debug
|
||||
}
|
||||
|
||||
func (g *GremlinSession) GetQuery(input string, output_struct chan map[string]interface{}) {
|
||||
defer close(output_struct)
|
||||
g.queryShape = make(map[string]interface{})
|
||||
g.lookingForQueryShape = true
|
||||
g.env.Run(input)
|
||||
output_struct <- g.queryShape
|
||||
g.queryShape = nil
|
||||
}
|
||||
|
||||
func (g *GremlinSession) InputParses(input string) (graph.ParseResult, error) {
|
||||
script, err := g.env.Compile("", input)
|
||||
if err != nil {
|
||||
return graph.ParseFail, err
|
||||
}
|
||||
g.script = script
|
||||
return graph.Parsed, nil
|
||||
}
|
||||
|
||||
func (g *GremlinSession) SendResult(result *GremlinResult) bool {
|
||||
if g.limit >= 0 && g.limit == g.count {
|
||||
return false
|
||||
}
|
||||
if g.doHalt {
|
||||
return false
|
||||
}
|
||||
if g.currentChannel != nil {
|
||||
g.currentChannel <- result
|
||||
g.count++
|
||||
if g.limit >= 0 && g.limit == g.count {
|
||||
return false
|
||||
} else {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
var halt = errors.New("Query Timeout")
|
||||
|
||||
func (g *GremlinSession) runUnsafe(input interface{}) (otto.Value, error) {
|
||||
g.doHalt = false
|
||||
defer func() {
|
||||
if caught := recover(); caught != nil {
|
||||
if caught == halt {
|
||||
g.err = halt
|
||||
return
|
||||
}
|
||||
panic(caught) // Something else happened, repanic!
|
||||
}
|
||||
}()
|
||||
|
||||
g.env.Interrupt = make(chan func(), 1) // The buffer prevents blocking
|
||||
|
||||
if g.timeoutSec != -1 {
|
||||
go func() {
|
||||
time.Sleep(g.timeoutSec * time.Second) // Stop after two seconds
|
||||
g.doHalt = true
|
||||
if g.env != nil {
|
||||
g.env.Interrupt <- func() {
|
||||
panic(halt)
|
||||
}
|
||||
g.env = g.emptyEnv
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
return g.env.Run(input) // Here be dragons (risky code)
|
||||
}
|
||||
|
||||
func (g *GremlinSession) ExecInput(input string, out chan interface{}, limit int) {
|
||||
defer close(out)
|
||||
g.err = nil
|
||||
g.currentChannel = out
|
||||
var err error
|
||||
var value otto.Value
|
||||
if g.script == nil {
|
||||
value, err = g.runUnsafe(input)
|
||||
} else {
|
||||
value, err = g.runUnsafe(g.script)
|
||||
}
|
||||
if err != nil {
|
||||
out <- &GremlinResult{metaresult: true,
|
||||
err: err.Error(),
|
||||
val: &value,
|
||||
actualResults: nil}
|
||||
} else {
|
||||
out <- &GremlinResult{metaresult: true,
|
||||
err: "",
|
||||
val: &value,
|
||||
actualResults: nil}
|
||||
}
|
||||
g.currentChannel = nil
|
||||
g.script = nil
|
||||
g.env = g.emptyEnv
|
||||
return
|
||||
}
|
||||
|
||||
func (s *GremlinSession) ToText(result interface{}) string {
|
||||
data := result.(*GremlinResult)
|
||||
if data.metaresult {
|
||||
if data.err != "" {
|
||||
return fmt.Sprintln("Error: ", data.err)
|
||||
}
|
||||
if data.val != nil {
|
||||
s, _ := data.val.Export()
|
||||
if data.val.IsObject() {
|
||||
typeVal, _ := data.val.Object().Get("_gremlin_type")
|
||||
if !typeVal.IsUndefined() {
|
||||
s = "[internal Iterator]"
|
||||
}
|
||||
}
|
||||
return fmt.Sprintln("=>", s)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
var out string
|
||||
out = fmt.Sprintln("****")
|
||||
if data.val == nil {
|
||||
tags := data.actualResults
|
||||
tagKeys := make([]string, len(*tags))
|
||||
i := 0
|
||||
for k, _ := range *tags {
|
||||
tagKeys[i] = k
|
||||
i++
|
||||
}
|
||||
sort.Strings(tagKeys)
|
||||
for _, k := range tagKeys {
|
||||
if k == "$_" {
|
||||
continue
|
||||
}
|
||||
out += fmt.Sprintf("%s : %s\n", k, s.ts.GetNameFor((*tags)[k]))
|
||||
}
|
||||
} else {
|
||||
if data.val.IsObject() {
|
||||
export, _ := data.val.Export()
|
||||
mapExport := export.(map[string]string)
|
||||
for k, v := range mapExport {
|
||||
out += fmt.Sprintf("%s : %v\n", k, v)
|
||||
}
|
||||
} else {
|
||||
strVersion, _ := data.val.ToString()
|
||||
out += fmt.Sprintf("%s\n", strVersion)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// Web stuff
|
||||
func (ses *GremlinSession) BuildJson(result interface{}) {
|
||||
data := result.(*GremlinResult)
|
||||
if !data.metaresult {
|
||||
if data.val == nil {
|
||||
obj := make(map[string]string)
|
||||
tags := data.actualResults
|
||||
tagKeys := make([]string, len(*tags))
|
||||
i := 0
|
||||
for k, _ := range *tags {
|
||||
tagKeys[i] = k
|
||||
i++
|
||||
}
|
||||
sort.Strings(tagKeys)
|
||||
for _, k := range tagKeys {
|
||||
obj[k] = ses.ts.GetNameFor((*tags)[k])
|
||||
}
|
||||
ses.dataOutput = append(ses.dataOutput, obj)
|
||||
} else {
|
||||
if data.val.IsObject() {
|
||||
export, _ := data.val.Export()
|
||||
ses.dataOutput = append(ses.dataOutput, export)
|
||||
} else {
|
||||
strVersion, _ := data.val.ToString()
|
||||
ses.dataOutput = append(ses.dataOutput, strVersion)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (ses *GremlinSession) GetJson() (interface{}, error) {
|
||||
defer ses.ClearJson()
|
||||
if ses.err != nil {
|
||||
return nil, ses.err
|
||||
}
|
||||
if ses.doHalt {
|
||||
return nil, halt
|
||||
}
|
||||
return ses.dataOutput, nil
|
||||
}
|
||||
|
||||
func (ses *GremlinSession) ClearJson() {
|
||||
ses.dataOutput = nil
|
||||
}
|
||||
11
query/gremlin/gremlin_test.nt
Normal file
11
query/gremlin/gremlin_test.nt
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
A follows B .
|
||||
C follows B .
|
||||
C follows D .
|
||||
D follows B .
|
||||
B follows F .
|
||||
F follows G .
|
||||
D follows G .
|
||||
E follows F .
|
||||
B status cool .
|
||||
D status cool .
|
||||
G status cool .
|
||||
184
query/gremlin/gremlin_traversals.go
Normal file
184
query/gremlin/gremlin_traversals.go
Normal file
|
|
@ -0,0 +1,184 @@
|
|||
// 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 gremlin
|
||||
|
||||
// Adds special traversal functions to JS Gremlin objects. Most of these just build the chain of objects, and won't often need the session.
|
||||
|
||||
import (
|
||||
"github.com/barakmich/glog"
|
||||
"github.com/robertkrimen/otto"
|
||||
)
|
||||
|
||||
func embedTraversals(env *otto.Otto, ses *GremlinSession, 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))
|
||||
obj.Set("Both", gremlinFunc("both", obj, env, ses))
|
||||
obj.Set("Follow", gremlinFunc("follow", obj, env, ses))
|
||||
obj.Set("FollowR", gremlinFollowR("followr", obj, env, ses))
|
||||
obj.Set("And", gremlinFunc("and", obj, env, ses))
|
||||
obj.Set("Intersect", gremlinFunc("and", obj, env, ses))
|
||||
obj.Set("Union", gremlinFunc("or", obj, env, ses))
|
||||
obj.Set("Or", gremlinFunc("or", obj, env, ses))
|
||||
obj.Set("Back", gremlinBack("back", obj, env, ses))
|
||||
obj.Set("Tag", gremlinFunc("tag", obj, env, ses))
|
||||
obj.Set("As", gremlinFunc("tag", obj, env, ses))
|
||||
obj.Set("Has", gremlinFunc("has", obj, env, ses))
|
||||
obj.Set("Save", gremlinFunc("save", 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 {
|
||||
return func(call otto.FunctionCall) otto.Value {
|
||||
call.Otto.Run("var out = {}")
|
||||
out, _ := call.Otto.Object("out")
|
||||
out.Set("_gremlin_type", kind)
|
||||
out.Set("_gremlin_values", call.ArgumentList)
|
||||
out.Set("_gremlin_prev", prevObj)
|
||||
outStrings := concatStringArgs(call)
|
||||
if len(*outStrings) > 0 {
|
||||
out.Set("string_args", *outStrings)
|
||||
}
|
||||
embedTraversals(env, ses, out)
|
||||
if isVertexChain(call.This.Object()) {
|
||||
embedFinals(env, ses, out)
|
||||
}
|
||||
return out.Value()
|
||||
}
|
||||
}
|
||||
|
||||
func gremlinBack(kind string, prevObj *otto.Object, env *otto.Otto, ses *GremlinSession) func(otto.FunctionCall) otto.Value {
|
||||
return func(call otto.FunctionCall) otto.Value {
|
||||
call.Otto.Run("var out = {}")
|
||||
out, _ := call.Otto.Object("out")
|
||||
out.Set("_gremlin_type", kind)
|
||||
out.Set("_gremlin_values", call.ArgumentList)
|
||||
outStrings := concatStringArgs(call)
|
||||
if len(*outStrings) > 0 {
|
||||
out.Set("string_args", *outStrings)
|
||||
}
|
||||
var otherChain *otto.Object
|
||||
var thisObj *otto.Object
|
||||
if len(*outStrings) != 0 {
|
||||
otherChain, thisObj = reverseGremlinChainTo(call.Otto, prevObj, (*outStrings)[0].(string))
|
||||
} else {
|
||||
otherChain, thisObj = reverseGremlinChainTo(call.Otto, prevObj, "")
|
||||
}
|
||||
out.Set("_gremlin_prev", thisObj)
|
||||
out.Set("_gremlin_back_chain", otherChain)
|
||||
embedTraversals(env, ses, out)
|
||||
if isVertexChain(call.This.Object()) {
|
||||
embedFinals(env, ses, out)
|
||||
}
|
||||
return out.Value()
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func gremlinFollowR(kind string, prevObj *otto.Object, env *otto.Otto, ses *GremlinSession) func(otto.FunctionCall) otto.Value {
|
||||
return func(call otto.FunctionCall) otto.Value {
|
||||
call.Otto.Run("var out = {}")
|
||||
out, _ := call.Otto.Object("out")
|
||||
out.Set("_gremlin_type", kind)
|
||||
out.Set("_gremlin_values", call.ArgumentList)
|
||||
outStrings := concatStringArgs(call)
|
||||
if len(*outStrings) > 0 {
|
||||
out.Set("string_args", *outStrings)
|
||||
}
|
||||
if len(call.ArgumentList) == 0 {
|
||||
return prevObj.Value()
|
||||
}
|
||||
arg := call.Argument(0)
|
||||
if isVertexChain(arg.Object()) {
|
||||
return prevObj.Value()
|
||||
}
|
||||
newChain, _ := reverseGremlinChainTo(call.Otto, arg.Object(), "")
|
||||
out.Set("_gremlin_prev", prevObj)
|
||||
out.Set("_gremlin_followr", newChain)
|
||||
embedTraversals(env, ses, out)
|
||||
if isVertexChain(call.This.Object()) {
|
||||
embedFinals(env, ses, out)
|
||||
}
|
||||
return out.Value()
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func reverseGremlinChainTo(env *otto.Otto, prevObj *otto.Object, tag string) (*otto.Object, *otto.Object) {
|
||||
env.Run("var _base_object = {}")
|
||||
base, err := env.Object("_base_object")
|
||||
if err != nil {
|
||||
glog.Error(err)
|
||||
return otto.NullValue().Object(), otto.NullValue().Object()
|
||||
}
|
||||
if isVertexChain(prevObj) {
|
||||
base.Set("_gremlin_type", "vertex")
|
||||
} else {
|
||||
base.Set("_gremlin_type", "morphism")
|
||||
}
|
||||
return reverseGremlinChainHelper(env, prevObj, base, tag)
|
||||
}
|
||||
|
||||
func reverseGremlinChainHelper(env *otto.Otto, chain *otto.Object, newBase *otto.Object, tag string) (*otto.Object, *otto.Object) {
|
||||
kindVal, _ := chain.Get("_gremlin_type")
|
||||
kind, _ := kindVal.ToString()
|
||||
|
||||
if tag != "" {
|
||||
if kind == "tag" {
|
||||
tags := getStringArgs(chain)
|
||||
for _, t := range tags {
|
||||
if t == tag {
|
||||
return newBase, chain
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if kind == "morphism" || kind == "vertex" {
|
||||
return newBase, chain
|
||||
}
|
||||
var newKind string
|
||||
switch kind {
|
||||
case "in":
|
||||
newKind = "out"
|
||||
case "out":
|
||||
newKind = "in"
|
||||
default:
|
||||
newKind = kind
|
||||
}
|
||||
prev, _ := chain.Get("_gremlin_prev")
|
||||
env.Run("var out = {}")
|
||||
out, _ := env.Object("out")
|
||||
out.Set("_gremlin_type", newKind)
|
||||
values, _ := chain.Get("_gremlin_values")
|
||||
out.Set("_gremlin_values", values)
|
||||
back, _ := chain.Get("_gremlin_back_chain")
|
||||
out.Set("_gremlin_back_chain", back)
|
||||
out.Set("_gremlin_prev", newBase)
|
||||
strings, _ := chain.Get("string_args")
|
||||
out.Set("string_args", strings)
|
||||
return reverseGremlinChainHelper(env, prev.Object(), out, tag)
|
||||
}
|
||||
|
||||
func debugChain(obj *otto.Object) bool {
|
||||
val, _ := obj.Get("_gremlin_type")
|
||||
x, _ := val.ToString()
|
||||
glog.V(2).Infoln(x)
|
||||
val, _ = obj.Get("_gremlin_prev")
|
||||
if val.IsObject() {
|
||||
return debugChain(val.Object())
|
||||
}
|
||||
return false
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue