cayley/query/mql/session.go
2014-08-27 21:27:01 +09:30

142 lines
3.4 KiB
Go

// 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 mql
import (
"encoding/json"
"fmt"
"sort"
"github.com/barakmich/glog"
"github.com/google/cayley/graph"
"github.com/google/cayley/graph/iterator"
"github.com/google/cayley/query"
)
type Session struct {
qs graph.QuadStore
currentQuery *Query
debug bool
}
func NewSession(qs graph.QuadStore) *Session {
var m Session
m.qs = qs
return &m
}
func (m *Session) ToggleDebug() {
m.debug = !m.debug
}
func (m *Session) GetQuery(input string, output_struct chan map[string]interface{}) {
defer close(output_struct)
var mqlQuery interface{}
err := json.Unmarshal([]byte(input), &mqlQuery)
if err != nil {
return
}
m.currentQuery = NewQuery(m)
m.currentQuery.BuildIteratorTree(mqlQuery)
output := make(map[string]interface{})
iterator.OutputQueryShapeForIterator(m.currentQuery.it, m.qs, output)
nodes := output["nodes"].([]iterator.Node)
new_nodes := make([]iterator.Node, 0)
for _, n := range nodes {
n.Tags = nil
new_nodes = append(new_nodes, n)
}
output["nodes"] = new_nodes
output_struct <- output
}
func (s *Session) InputParses(input string) (query.ParseResult, error) {
var x interface{}
err := json.Unmarshal([]byte(input), &x)
if err != nil {
return query.ParseFail, err
}
return query.Parsed, nil
}
func (s *Session) ExecInput(input string, c chan interface{}, _ int) {
defer close(c)
var mqlQuery interface{}
err := json.Unmarshal([]byte(input), &mqlQuery)
if err != nil {
return
}
s.currentQuery = NewQuery(s)
s.currentQuery.BuildIteratorTree(mqlQuery)
if s.currentQuery.isError() {
return
}
it, _ := s.currentQuery.it.Optimize()
if glog.V(2) {
glog.V(2).Infoln(it.DebugString(0))
}
for graph.Next(it) {
tags := make(map[string]graph.Value)
it.TagResults(tags)
c <- tags
for it.NextPath() == true {
tags := make(map[string]graph.Value)
it.TagResults(tags)
c <- tags
}
}
}
func (s *Session) ToText(result interface{}) string {
tags := result.(map[string]graph.Value)
out := fmt.Sprintln("****")
tagKeys := make([]string, len(tags))
s.currentQuery.treeifyResult(tags)
s.currentQuery.buildResults()
r, _ := json.MarshalIndent(s.currentQuery.results, "", " ")
fmt.Println(string(r))
i := 0
for k, _ := range tags {
tagKeys[i] = string(k)
i++
}
sort.Strings(tagKeys)
for _, k := range tagKeys {
if k == "$_" {
continue
}
out += fmt.Sprintf("%s : %s\n", k, s.qs.NameOf(tags[k]))
}
return out
}
func (s *Session) BuildJson(result interface{}) {
s.currentQuery.treeifyResult(result.(map[string]graph.Value))
}
func (s *Session) GetJson() ([]interface{}, error) {
s.currentQuery.buildResults()
if s.currentQuery.isError() {
return nil, s.currentQuery.err
} else {
return s.currentQuery.results, nil
}
}
func (s *Session) ClearJson() {
// Since we create a new Query underneath every query, clearing isn't necessary.
return
}