Clean up style
This commit is contained in:
parent
8ccf842518
commit
15a45ef0d4
3 changed files with 59 additions and 69 deletions
|
|
@ -84,7 +84,7 @@ func setupGremlin(env *otto.Otto, ses *Session) {
|
||||||
graph.Set("Emit", func(call otto.FunctionCall) otto.Value {
|
graph.Set("Emit", func(call otto.FunctionCall) otto.Value {
|
||||||
value := call.Argument(0)
|
value := call.Argument(0)
|
||||||
if value.IsDefined() {
|
if value.IsDefined() {
|
||||||
ses.SendResult(&GremlinResult{metaresult: false, err: nil, val: &value, actualResults: nil})
|
ses.SendResult(&GremlinResult{val: &value})
|
||||||
}
|
}
|
||||||
return otto.NullValue()
|
return otto.NullValue()
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -248,8 +248,8 @@ func runIteratorWithCallback(it graph.Iterator, ses *Session, callback otto.Valu
|
||||||
}
|
}
|
||||||
|
|
||||||
func runIteratorOnSession(it graph.Iterator, ses *Session) {
|
func runIteratorOnSession(it graph.Iterator, ses *Session) {
|
||||||
if ses.lookingForQueryShape {
|
if ses.wantShape {
|
||||||
iterator.OutputQueryShapeForIterator(it, ses.ts, ses.queryShape)
|
iterator.OutputQueryShapeForIterator(it, ses.ts, ses.shape)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
it, _ = it.Optimize()
|
it, _ = it.Optimize()
|
||||||
|
|
@ -267,8 +267,7 @@ func runIteratorOnSession(it graph.Iterator, ses *Session) {
|
||||||
}
|
}
|
||||||
tags := make(map[string]graph.Value)
|
tags := make(map[string]graph.Value)
|
||||||
it.TagResults(tags)
|
it.TagResults(tags)
|
||||||
cont := ses.SendResult(&GremlinResult{metaresult: false, err: nil, val: nil, actualResults: &tags})
|
if !ses.SendResult(&GremlinResult{actualResults: &tags}) {
|
||||||
if !cont {
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
for it.NextResult() == true {
|
for it.NextResult() == true {
|
||||||
|
|
@ -279,8 +278,7 @@ func runIteratorOnSession(it graph.Iterator, ses *Session) {
|
||||||
}
|
}
|
||||||
tags := make(map[string]graph.Value)
|
tags := make(map[string]graph.Value)
|
||||||
it.TagResults(tags)
|
it.TagResults(tags)
|
||||||
cont := ses.SendResult(&GremlinResult{metaresult: false, err: nil, val: nil, actualResults: &tags})
|
if !ses.SendResult(&GremlinResult{actualResults: &tags}) {
|
||||||
if !cont {
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -27,40 +27,36 @@ import (
|
||||||
"github.com/google/cayley/query"
|
"github.com/google/cayley/query"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var ErrKillTimeout = errors.New("query timed out")
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
ts graph.TripleStore
|
ts graph.TripleStore
|
||||||
currentChannel chan interface{}
|
results chan interface{}
|
||||||
env *otto.Otto
|
env *otto.Otto
|
||||||
envLock sync.Mutex
|
envLock sync.Mutex
|
||||||
debug bool
|
debug bool
|
||||||
limit int
|
limit int
|
||||||
count int
|
count int
|
||||||
dataOutput []interface{}
|
dataOutput []interface{}
|
||||||
lookingForQueryShape bool
|
wantShape bool
|
||||||
queryShape map[string]interface{}
|
shape map[string]interface{}
|
||||||
err error
|
err error
|
||||||
script *otto.Script
|
script *otto.Script
|
||||||
kill chan struct{}
|
kill chan struct{}
|
||||||
timeoutSec time.Duration
|
timeoutSec time.Duration
|
||||||
emptyEnv *otto.Otto
|
emptyEnv *otto.Otto
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSession(inputTripleStore graph.TripleStore, timeoutSec int, persist bool) *Session {
|
func NewSession(inputTripleStore graph.TripleStore, timeoutSec int, persist bool) *Session {
|
||||||
var g Session
|
g := Session{
|
||||||
g.ts = inputTripleStore
|
ts: inputTripleStore,
|
||||||
|
limit: -1,
|
||||||
|
timeoutSec: time.Duration(timeoutSec),
|
||||||
|
}
|
||||||
g.env = BuildEnviron(&g)
|
g.env = BuildEnviron(&g)
|
||||||
g.limit = -1
|
|
||||||
g.count = 0
|
|
||||||
g.lookingForQueryShape = false
|
|
||||||
if persist {
|
if persist {
|
||||||
g.emptyEnv = g.env
|
g.emptyEnv = g.env
|
||||||
}
|
}
|
||||||
if timeoutSec < 0 {
|
|
||||||
g.timeoutSec = time.Duration(-1)
|
|
||||||
} else {
|
|
||||||
g.timeoutSec = time.Duration(timeoutSec)
|
|
||||||
}
|
|
||||||
g.ClearJson()
|
|
||||||
return &g
|
return &g
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -75,13 +71,13 @@ func (s *Session) ToggleDebug() {
|
||||||
s.debug = !s.debug
|
s.debug = !s.debug
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) GetQuery(input string, output_struct chan map[string]interface{}) {
|
func (s *Session) GetQuery(input string, out chan map[string]interface{}) {
|
||||||
defer close(output_struct)
|
defer close(out)
|
||||||
s.queryShape = make(map[string]interface{})
|
s.shape = make(map[string]interface{})
|
||||||
s.lookingForQueryShape = true
|
s.wantShape = true
|
||||||
s.env.Run(input)
|
s.env.Run(input)
|
||||||
output_struct <- s.queryShape
|
out <- s.shape
|
||||||
s.queryShape = nil
|
s.shape = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) InputParses(input string) (query.ParseResult, error) {
|
func (s *Session) InputParses(input string) (query.ParseResult, error) {
|
||||||
|
|
@ -102,8 +98,8 @@ func (s *Session) SendResult(result *GremlinResult) bool {
|
||||||
return false
|
return false
|
||||||
default:
|
default:
|
||||||
}
|
}
|
||||||
if s.currentChannel != nil {
|
if s.results != nil {
|
||||||
s.currentChannel <- result
|
s.results <- result
|
||||||
s.count++
|
s.count++
|
||||||
if s.limit >= 0 && s.limit == s.count {
|
if s.limit >= 0 && s.limit == s.count {
|
||||||
return false
|
return false
|
||||||
|
|
@ -114,8 +110,6 @@ func (s *Session) SendResult(result *GremlinResult) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
var ErrKillTimeout = errors.New("query timed out")
|
|
||||||
|
|
||||||
func (s *Session) runUnsafe(input interface{}) (otto.Value, error) {
|
func (s *Session) runUnsafe(input interface{}) (otto.Value, error) {
|
||||||
s.kill = make(chan struct{})
|
s.kill = make(chan struct{})
|
||||||
defer func() {
|
defer func() {
|
||||||
|
|
@ -128,11 +122,12 @@ func (s *Session) runUnsafe(input interface{}) (otto.Value, error) {
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
s.env.Interrupt = make(chan func(), 1) // The buffer prevents blocking
|
// Use buffered chan to prevent blocking.
|
||||||
|
s.env.Interrupt = make(chan func(), 1)
|
||||||
|
|
||||||
if s.timeoutSec != -1 {
|
if s.timeoutSec >= 0 {
|
||||||
go func() {
|
go func() {
|
||||||
time.Sleep(s.timeoutSec * time.Second) // Stop after two seconds
|
time.Sleep(s.timeoutSec * time.Second)
|
||||||
close(s.kill)
|
close(s.kill)
|
||||||
s.envLock.Lock()
|
s.envLock.Lock()
|
||||||
defer s.envLock.Unlock()
|
defer s.envLock.Unlock()
|
||||||
|
|
@ -147,13 +142,13 @@ func (s *Session) runUnsafe(input interface{}) (otto.Value, error) {
|
||||||
|
|
||||||
s.envLock.Lock()
|
s.envLock.Lock()
|
||||||
defer s.envLock.Unlock()
|
defer s.envLock.Unlock()
|
||||||
return s.env.Run(input) // Here be dragons (risky code)
|
return s.env.Run(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) ExecInput(input string, out chan interface{}, limit int) {
|
func (s *Session) ExecInput(input string, out chan interface{}, limit int) {
|
||||||
defer close(out)
|
defer close(out)
|
||||||
s.err = nil
|
s.err = nil
|
||||||
s.currentChannel = out
|
s.results = out
|
||||||
var err error
|
var err error
|
||||||
var value otto.Value
|
var value otto.Value
|
||||||
if s.script == nil {
|
if s.script == nil {
|
||||||
|
|
@ -162,24 +157,22 @@ func (s *Session) ExecInput(input string, out chan interface{}, limit int) {
|
||||||
value, err = s.runUnsafe(s.script)
|
value, err = s.runUnsafe(s.script)
|
||||||
}
|
}
|
||||||
out <- &GremlinResult{
|
out <- &GremlinResult{
|
||||||
metaresult: true,
|
metaresult: true,
|
||||||
err: err,
|
err: err,
|
||||||
val: &value,
|
val: &value,
|
||||||
actualResults: nil,
|
|
||||||
}
|
}
|
||||||
s.currentChannel = nil
|
s.results = nil
|
||||||
s.script = nil
|
s.script = nil
|
||||||
s.envLock.Lock()
|
s.envLock.Lock()
|
||||||
s.env = s.emptyEnv
|
s.env = s.emptyEnv
|
||||||
s.envLock.Unlock()
|
s.envLock.Unlock()
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Session) 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 != nil {
|
if data.err != nil {
|
||||||
return fmt.Sprintln("Error: ", data.err)
|
return fmt.Sprintf("Error: %v\n", data.err)
|
||||||
}
|
}
|
||||||
if data.val != nil {
|
if data.val != nil {
|
||||||
s, _ := data.val.Export()
|
s, _ := data.val.Export()
|
||||||
|
|
@ -226,7 +219,7 @@ func (s *Session) ToText(result interface{}) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Web stuff
|
// Web stuff
|
||||||
func (ses *Session) BuildJson(result interface{}) {
|
func (s *Session) BuildJson(result interface{}) {
|
||||||
data := result.(*GremlinResult)
|
data := result.(*GremlinResult)
|
||||||
if !data.metaresult {
|
if !data.metaresult {
|
||||||
if data.val == nil {
|
if data.val == nil {
|
||||||
|
|
@ -240,35 +233,34 @@ func (ses *Session) BuildJson(result interface{}) {
|
||||||
}
|
}
|
||||||
sort.Strings(tagKeys)
|
sort.Strings(tagKeys)
|
||||||
for _, k := range tagKeys {
|
for _, k := range tagKeys {
|
||||||
obj[k] = ses.ts.NameOf((*tags)[k])
|
obj[k] = s.ts.NameOf((*tags)[k])
|
||||||
}
|
}
|
||||||
ses.dataOutput = append(ses.dataOutput, obj)
|
s.dataOutput = append(s.dataOutput, obj)
|
||||||
} else {
|
} else {
|
||||||
if data.val.IsObject() {
|
if data.val.IsObject() {
|
||||||
export, _ := data.val.Export()
|
export, _ := data.val.Export()
|
||||||
ses.dataOutput = append(ses.dataOutput, export)
|
s.dataOutput = append(s.dataOutput, export)
|
||||||
} else {
|
} else {
|
||||||
strVersion, _ := data.val.ToString()
|
strVersion, _ := data.val.ToString()
|
||||||
ses.dataOutput = append(ses.dataOutput, strVersion)
|
s.dataOutput = append(s.dataOutput, strVersion)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ses *Session) GetJson() ([]interface{}, error) {
|
func (s *Session) GetJson() ([]interface{}, error) {
|
||||||
defer ses.ClearJson()
|
defer s.ClearJson()
|
||||||
if ses.err != nil {
|
if s.err != nil {
|
||||||
return nil, ses.err
|
return nil, s.err
|
||||||
}
|
}
|
||||||
select {
|
select {
|
||||||
case <-ses.kill:
|
case <-s.kill:
|
||||||
return nil, ErrKillTimeout
|
return nil, ErrKillTimeout
|
||||||
default:
|
default:
|
||||||
return ses.dataOutput, nil
|
return s.dataOutput, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ses *Session) ClearJson() {
|
func (s *Session) ClearJson() {
|
||||||
ses.dataOutput = nil
|
s.dataOutput = nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue