Canonicalise mql receiver names
And again, remove a multitude of redundant infixes I missed previously.
This commit is contained in:
parent
5a1a7259c8
commit
929beb624c
5 changed files with 126 additions and 126 deletions
|
|
@ -24,31 +24,31 @@ import (
|
||||||
"github.com/google/cayley/graph"
|
"github.com/google/cayley/graph"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (m *MqlQuery) buildFixed(s string) graph.Iterator {
|
func (q *Query) buildFixed(s string) graph.Iterator {
|
||||||
f := m.ses.ts.MakeFixed()
|
f := q.ses.ts.MakeFixed()
|
||||||
f.AddValue(m.ses.ts.GetIdFor(s))
|
f.AddValue(q.ses.ts.GetIdFor(s))
|
||||||
return f
|
return f
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MqlQuery) buildResultIterator(path MqlPath) graph.Iterator {
|
func (q *Query) buildResultIterator(path Path) graph.Iterator {
|
||||||
all := m.ses.ts.GetNodesAllIterator()
|
all := q.ses.ts.GetNodesAllIterator()
|
||||||
all.AddTag(string(path))
|
all.AddTag(string(path))
|
||||||
return graph.NewOptionalIterator(all)
|
return graph.NewOptionalIterator(all)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MqlQuery) BuildIteratorTree(query interface{}) {
|
func (q *Query) BuildIteratorTree(query interface{}) {
|
||||||
m.isRepeated = make(map[MqlPath]bool)
|
q.isRepeated = make(map[Path]bool)
|
||||||
m.queryStructure = make(map[MqlPath]map[string]interface{})
|
q.queryStructure = make(map[Path]map[string]interface{})
|
||||||
m.queryResult = make(map[MqlResultPath]map[string]interface{})
|
q.queryResult = make(map[ResultPath]map[string]interface{})
|
||||||
m.queryResult[""] = make(map[string]interface{})
|
q.queryResult[""] = make(map[string]interface{})
|
||||||
|
|
||||||
m.it, m.err = m.buildIteratorTreeInternal(query, NewMqlPath())
|
q.it, q.err = q.buildIteratorTreeInternal(query, NewPath())
|
||||||
if m.err != nil {
|
if q.err != nil {
|
||||||
m.isError = true
|
q.isError = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MqlQuery) buildIteratorTreeInternal(query interface{}, path MqlPath) (graph.Iterator, error) {
|
func (q *Query) buildIteratorTreeInternal(query interface{}, path Path) (graph.Iterator, error) {
|
||||||
var it graph.Iterator
|
var it graph.Iterator
|
||||||
var err error
|
var err error
|
||||||
err = nil
|
err = nil
|
||||||
|
|
@ -58,36 +58,36 @@ func (m *MqlQuery) buildIteratorTreeInternal(query interface{}, path MqlPath) (g
|
||||||
// Treat the bool as a string and call it a day.
|
// Treat the bool as a string and call it a day.
|
||||||
// Things which are really bool-like are special cases and will be dealt with separately.
|
// Things which are really bool-like are special cases and will be dealt with separately.
|
||||||
if t {
|
if t {
|
||||||
it = m.buildFixed("true")
|
it = q.buildFixed("true")
|
||||||
}
|
}
|
||||||
it = m.buildFixed("false")
|
it = q.buildFixed("false")
|
||||||
case float64:
|
case float64:
|
||||||
// for JSON numbers
|
// for JSON numbers
|
||||||
// Damn you, Javascript, and your lack of integer values.
|
// Damn you, Javascript, and your lack of integer values.
|
||||||
if math.Floor(t) == t {
|
if math.Floor(t) == t {
|
||||||
// Treat it like an integer.
|
// Treat it like an integer.
|
||||||
it = m.buildFixed(fmt.Sprintf("%d", t))
|
it = q.buildFixed(fmt.Sprintf("%d", t))
|
||||||
} else {
|
} else {
|
||||||
it = m.buildFixed(fmt.Sprintf("%f", t))
|
it = q.buildFixed(fmt.Sprintf("%f", t))
|
||||||
}
|
}
|
||||||
case string:
|
case string:
|
||||||
// for JSON strings
|
// for JSON strings
|
||||||
it = m.buildFixed(t)
|
it = q.buildFixed(t)
|
||||||
case []interface{}:
|
case []interface{}:
|
||||||
// for JSON arrays
|
// for JSON arrays
|
||||||
m.isRepeated[path] = true
|
q.isRepeated[path] = true
|
||||||
if len(t) == 0 {
|
if len(t) == 0 {
|
||||||
it = m.buildResultIterator(path)
|
it = q.buildResultIterator(path)
|
||||||
} else if len(t) == 1 {
|
} else if len(t) == 1 {
|
||||||
it, err = m.buildIteratorTreeInternal(t[0], path)
|
it, err = q.buildIteratorTreeInternal(t[0], path)
|
||||||
} else {
|
} else {
|
||||||
err = errors.New(fmt.Sprintf("Multiple fields at location root%s", path.DisplayString()))
|
err = errors.New(fmt.Sprintf("Multiple fields at location root%s", path.DisplayString()))
|
||||||
}
|
}
|
||||||
case map[string]interface{}:
|
case map[string]interface{}:
|
||||||
// for JSON objects
|
// for JSON objects
|
||||||
it, err = m.buildIteratorTreeMapInternal(t, path)
|
it, err = q.buildIteratorTreeMapInternal(t, path)
|
||||||
case nil:
|
case nil:
|
||||||
it = m.buildResultIterator(path)
|
it = q.buildResultIterator(path)
|
||||||
default:
|
default:
|
||||||
log.Fatal("Unknown JSON type?", query)
|
log.Fatal("Unknown JSON type?", query)
|
||||||
}
|
}
|
||||||
|
|
@ -98,9 +98,9 @@ func (m *MqlQuery) buildIteratorTreeInternal(query interface{}, path MqlPath) (g
|
||||||
return it, nil
|
return it, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MqlQuery) buildIteratorTreeMapInternal(query map[string]interface{}, path MqlPath) (graph.Iterator, error) {
|
func (q *Query) buildIteratorTreeMapInternal(query map[string]interface{}, path Path) (graph.Iterator, error) {
|
||||||
it := graph.NewAndIterator()
|
it := graph.NewAndIterator()
|
||||||
it.AddSubIterator(m.ses.ts.GetNodesAllIterator())
|
it.AddSubIterator(q.ses.ts.GetNodesAllIterator())
|
||||||
var err error
|
var err error
|
||||||
err = nil
|
err = nil
|
||||||
outputStructure := make(map[string]interface{})
|
outputStructure := make(map[string]interface{})
|
||||||
|
|
@ -122,29 +122,29 @@ func (m *MqlQuery) buildIteratorTreeMapInternal(query map[string]interface{}, pa
|
||||||
// Other special constructs here
|
// Other special constructs here
|
||||||
var subit graph.Iterator
|
var subit graph.Iterator
|
||||||
if key == "id" {
|
if key == "id" {
|
||||||
subit, err = m.buildIteratorTreeInternal(subquery, path.Follow(key))
|
subit, err = q.buildIteratorTreeInternal(subquery, path.Follow(key))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
it.AddSubIterator(subit)
|
it.AddSubIterator(subit)
|
||||||
} else {
|
} else {
|
||||||
subit, err = m.buildIteratorTreeInternal(subquery, path.Follow(key))
|
subit, err = q.buildIteratorTreeInternal(subquery, path.Follow(key))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
subAnd := graph.NewAndIterator()
|
subAnd := graph.NewAndIterator()
|
||||||
predFixed := m.ses.ts.MakeFixed()
|
predFixed := q.ses.ts.MakeFixed()
|
||||||
predFixed.AddValue(m.ses.ts.GetIdFor(pred))
|
predFixed.AddValue(q.ses.ts.GetIdFor(pred))
|
||||||
subAnd.AddSubIterator(graph.NewLinksToIterator(m.ses.ts, predFixed, "p"))
|
subAnd.AddSubIterator(graph.NewLinksToIterator(q.ses.ts, predFixed, "p"))
|
||||||
if reverse {
|
if reverse {
|
||||||
lto := graph.NewLinksToIterator(m.ses.ts, subit, "s")
|
lto := graph.NewLinksToIterator(q.ses.ts, subit, "s")
|
||||||
subAnd.AddSubIterator(lto)
|
subAnd.AddSubIterator(lto)
|
||||||
hasa := graph.NewHasaIterator(m.ses.ts, subAnd, "o")
|
hasa := graph.NewHasaIterator(q.ses.ts, subAnd, "o")
|
||||||
it.AddSubIterator(hasa)
|
it.AddSubIterator(hasa)
|
||||||
} else {
|
} else {
|
||||||
lto := graph.NewLinksToIterator(m.ses.ts, subit, "o")
|
lto := graph.NewLinksToIterator(q.ses.ts, subit, "o")
|
||||||
subAnd.AddSubIterator(lto)
|
subAnd.AddSubIterator(lto)
|
||||||
hasa := graph.NewHasaIterator(m.ses.ts, subAnd, "s")
|
hasa := graph.NewHasaIterator(q.ses.ts, subAnd, "s")
|
||||||
it.AddSubIterator(hasa)
|
it.AddSubIterator(hasa)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -152,30 +152,30 @@ func (m *MqlQuery) buildIteratorTreeMapInternal(query map[string]interface{}, pa
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
m.queryStructure[path] = outputStructure
|
q.queryStructure[path] = outputStructure
|
||||||
return it, nil
|
return it, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
type MqlResultPathSlice []MqlResultPath
|
type ResultPathSlice []ResultPath
|
||||||
|
|
||||||
func (sl MqlResultPathSlice) Len() int {
|
func (p ResultPathSlice) Len() int {
|
||||||
return len(sl)
|
return len(p)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sl MqlResultPathSlice) Less(i, j int) bool {
|
func (p ResultPathSlice) Less(i, j int) bool {
|
||||||
iLen := len(strings.Split(string(sl[i]), "\x30"))
|
iLen := len(strings.Split(string(p[i]), "\x30"))
|
||||||
jLen := len(strings.Split(string(sl[j]), "\x30"))
|
jLen := len(strings.Split(string(p[j]), "\x30"))
|
||||||
if iLen < jLen {
|
if iLen < jLen {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
if iLen == jLen {
|
if iLen == jLen {
|
||||||
if len(string(sl[i])) < len(string(sl[j])) {
|
if len(string(p[i])) < len(string(p[j])) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sl MqlResultPathSlice) Swap(i, j int) {
|
func (p ResultPathSlice) Swap(i, j int) {
|
||||||
sl[i], sl[j] = sl[j], sl[i]
|
p[i], p[j] = p[j], p[i]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -20,18 +20,18 @@ import (
|
||||||
"github.com/google/cayley/graph"
|
"github.com/google/cayley/graph"
|
||||||
)
|
)
|
||||||
|
|
||||||
func (m *MqlQuery) treeifyResult(tags map[string]graph.TSVal) map[MqlResultPath]string {
|
func (q *Query) treeifyResult(tags map[string]graph.TSVal) map[ResultPath]string {
|
||||||
// Transform the map into something a little more interesting.
|
// Transform the map into something a little more interesting.
|
||||||
results := make(map[MqlPath]string)
|
results := make(map[Path]string)
|
||||||
for k, v := range tags {
|
for k, v := range tags {
|
||||||
results[MqlPath(k)] = m.ses.ts.GetNameFor(v)
|
results[Path(k)] = q.ses.ts.GetNameFor(v)
|
||||||
}
|
}
|
||||||
resultPaths := make(map[MqlResultPath]string)
|
resultPaths := make(map[ResultPath]string)
|
||||||
for k, v := range results {
|
for k, v := range results {
|
||||||
resultPaths[k.ToResultPathFromMap(results)] = v
|
resultPaths[k.ToResultPathFromMap(results)] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
var paths MqlResultPathSlice
|
var paths ResultPathSlice
|
||||||
|
|
||||||
for path, _ := range resultPaths {
|
for path, _ := range resultPaths {
|
||||||
paths = append(paths, path)
|
paths = append(paths, path)
|
||||||
|
|
@ -44,32 +44,32 @@ func (m *MqlQuery) treeifyResult(tags map[string]graph.TSVal) map[MqlResultPath]
|
||||||
currentPath := path.getPath()
|
currentPath := path.getPath()
|
||||||
value := resultPaths[path]
|
value := resultPaths[path]
|
||||||
namePath := path.AppendValue(value)
|
namePath := path.AppendValue(value)
|
||||||
if _, ok := m.queryResult[namePath]; !ok {
|
if _, ok := q.queryResult[namePath]; !ok {
|
||||||
targetPath, key := path.splitLastPath()
|
targetPath, key := path.splitLastPath()
|
||||||
if path == "" {
|
if path == "" {
|
||||||
targetPath, key = "", value
|
targetPath, key = "", value
|
||||||
if _, ok := m.queryResult[""][value]; !ok {
|
if _, ok := q.queryResult[""][value]; !ok {
|
||||||
m.resultOrder = append(m.resultOrder, value)
|
q.resultOrder = append(q.resultOrder, value)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if _, ok := m.queryStructure[currentPath]; ok {
|
if _, ok := q.queryStructure[currentPath]; ok {
|
||||||
// If there's substructure, then copy that in.
|
// If there's substructure, then copy that in.
|
||||||
newStruct := m.copyPathStructure(currentPath)
|
newStruct := q.copyPathStructure(currentPath)
|
||||||
if m.isRepeated[currentPath] && currentPath != "" {
|
if q.isRepeated[currentPath] && currentPath != "" {
|
||||||
switch t := m.queryResult[targetPath][key].(type) {
|
switch t := q.queryResult[targetPath][key].(type) {
|
||||||
case nil:
|
case nil:
|
||||||
x := make([]interface{}, 0)
|
x := make([]interface{}, 0)
|
||||||
x = append(x, newStruct)
|
x = append(x, newStruct)
|
||||||
m.queryResult[targetPath][key] = x
|
q.queryResult[targetPath][key] = x
|
||||||
m.queryResult[namePath] = newStruct
|
q.queryResult[namePath] = newStruct
|
||||||
case []interface{}:
|
case []interface{}:
|
||||||
m.queryResult[targetPath][key] = append(t, newStruct)
|
q.queryResult[targetPath][key] = append(t, newStruct)
|
||||||
m.queryResult[namePath] = newStruct
|
q.queryResult[namePath] = newStruct
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
m.queryResult[namePath] = newStruct
|
q.queryResult[namePath] = newStruct
|
||||||
m.queryResult[targetPath][key] = newStruct
|
q.queryResult[targetPath][key] = newStruct
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -80,26 +80,26 @@ func (m *MqlQuery) treeifyResult(tags map[string]graph.TSVal) map[MqlResultPath]
|
||||||
currentPath := path.getPath()
|
currentPath := path.getPath()
|
||||||
value := resultPaths[path]
|
value := resultPaths[path]
|
||||||
namePath := path.AppendValue(value)
|
namePath := path.AppendValue(value)
|
||||||
if _, ok := m.queryStructure[currentPath]; ok {
|
if _, ok := q.queryStructure[currentPath]; ok {
|
||||||
// We're dealing with ids.
|
// We're dealing with ids.
|
||||||
if _, ok := m.queryResult[namePath]["id"]; ok {
|
if _, ok := q.queryResult[namePath]["id"]; ok {
|
||||||
m.queryResult[namePath]["id"] = value
|
q.queryResult[namePath]["id"] = value
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// Just a value.
|
// Just a value.
|
||||||
targetPath, key := path.splitLastPath()
|
targetPath, key := path.splitLastPath()
|
||||||
if m.isRepeated[currentPath] {
|
if q.isRepeated[currentPath] {
|
||||||
switch t := m.queryResult[targetPath][key].(type) {
|
switch t := q.queryResult[targetPath][key].(type) {
|
||||||
case nil:
|
case nil:
|
||||||
x := make([]interface{}, 0)
|
x := make([]interface{}, 0)
|
||||||
x = append(x, value)
|
x = append(x, value)
|
||||||
m.queryResult[targetPath][key] = x
|
q.queryResult[targetPath][key] = x
|
||||||
case []interface{}:
|
case []interface{}:
|
||||||
m.queryResult[targetPath][key] = append(t, value)
|
q.queryResult[targetPath][key] = append(t, value)
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
m.queryResult[targetPath][key] = value
|
q.queryResult[targetPath][key] = value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -107,8 +107,8 @@ func (m *MqlQuery) treeifyResult(tags map[string]graph.TSVal) map[MqlResultPath]
|
||||||
return resultPaths
|
return resultPaths
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MqlQuery) buildResults() {
|
func (q *Query) buildResults() {
|
||||||
for _, v := range m.resultOrder {
|
for _, v := range q.resultOrder {
|
||||||
m.results = append(m.results, m.queryResult[""][v])
|
q.results = append(q.results, q.queryResult[""][v])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -40,7 +40,7 @@ func buildTripleStore() *Session {
|
||||||
return NewSession(ts)
|
return NewSession(ts)
|
||||||
}
|
}
|
||||||
|
|
||||||
func compareJsonInterfaces(actual interface{}, expected interface{}, path MqlPath, t *testing.T) {
|
func compareJsonInterfaces(actual interface{}, expected interface{}, path Path, t *testing.T) {
|
||||||
isError := false
|
isError := false
|
||||||
switch ex := expected.(type) {
|
switch ex := expected.(type) {
|
||||||
case bool:
|
case bool:
|
||||||
|
|
@ -134,7 +134,7 @@ func runAndTestQuery(query string, expected string, t *testing.T) {
|
||||||
actual_struct, _ := ses.GetJson()
|
actual_struct, _ := ses.GetJson()
|
||||||
var expected_struct interface{}
|
var expected_struct interface{}
|
||||||
json.Unmarshal([]byte(expected), &expected_struct)
|
json.Unmarshal([]byte(expected), &expected_struct)
|
||||||
compareJsonInterfaces(actual_struct, expected_struct, NewMqlPath(), t)
|
compareJsonInterfaces(actual_struct, expected_struct, NewPath(), t)
|
||||||
ses.ClearJson()
|
ses.ClearJson()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -21,53 +21,53 @@ import (
|
||||||
"github.com/google/cayley/graph"
|
"github.com/google/cayley/graph"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MqlPath string
|
type Path string
|
||||||
type MqlResultPath string
|
type ResultPath string
|
||||||
|
|
||||||
type MqlQuery struct {
|
type Query struct {
|
||||||
ses *Session
|
ses *Session
|
||||||
it graph.Iterator
|
it graph.Iterator
|
||||||
isRepeated map[MqlPath]bool
|
isRepeated map[Path]bool
|
||||||
queryStructure map[MqlPath]map[string]interface{}
|
queryStructure map[Path]map[string]interface{}
|
||||||
queryResult map[MqlResultPath]map[string]interface{}
|
queryResult map[ResultPath]map[string]interface{}
|
||||||
results []interface{}
|
results []interface{}
|
||||||
resultOrder []string
|
resultOrder []string
|
||||||
isError bool
|
isError bool
|
||||||
err error
|
err error
|
||||||
}
|
}
|
||||||
|
|
||||||
func (mqlQuery *MqlQuery) copyPathStructure(path MqlPath) map[string]interface{} {
|
func (q *Query) copyPathStructure(path Path) map[string]interface{} {
|
||||||
output := make(map[string]interface{})
|
output := make(map[string]interface{})
|
||||||
for k, v := range mqlQuery.queryStructure[path] {
|
for k, v := range q.queryStructure[path] {
|
||||||
output[k] = v
|
output[k] = v
|
||||||
}
|
}
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMqlPath() MqlPath {
|
func NewPath() Path {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
func (p MqlPath) Follow(s string) MqlPath {
|
func (p Path) Follow(s string) Path {
|
||||||
return MqlPath(fmt.Sprintf("%s\x1E%s", p, s))
|
return Path(fmt.Sprintf("%s\x1E%s", p, s))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p MqlPath) DisplayString() string {
|
func (p Path) DisplayString() string {
|
||||||
return strings.Replace(string(p), "\x1E", ".", -1)
|
return strings.Replace(string(p), "\x1E", ".", -1)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMqlResultPath() MqlResultPath {
|
func NewResultPath() ResultPath {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p MqlResultPath) FollowPath(followPiece string, value string) MqlResultPath {
|
func (p ResultPath) FollowPath(followPiece string, value string) ResultPath {
|
||||||
if string(p) == "" {
|
if string(p) == "" {
|
||||||
return MqlResultPath(fmt.Sprintf("%s\x1E%s", value, followPiece))
|
return ResultPath(fmt.Sprintf("%s\x1E%s", value, followPiece))
|
||||||
}
|
}
|
||||||
return MqlResultPath(fmt.Sprintf("%s\x1E%s\x1E%s", p, value, followPiece))
|
return ResultPath(fmt.Sprintf("%s\x1E%s\x1E%s", p, value, followPiece))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p MqlResultPath) getPath() MqlPath {
|
func (p ResultPath) getPath() Path {
|
||||||
out := NewMqlPath()
|
out := NewPath()
|
||||||
pathPieces := strings.Split(string(p), "\x1E")
|
pathPieces := strings.Split(string(p), "\x1E")
|
||||||
for len(pathPieces) > 1 {
|
for len(pathPieces) > 1 {
|
||||||
a := pathPieces[1]
|
a := pathPieces[1]
|
||||||
|
|
@ -77,22 +77,22 @@ func (p MqlResultPath) getPath() MqlPath {
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p MqlResultPath) splitLastPath() (MqlResultPath, string) {
|
func (p ResultPath) splitLastPath() (ResultPath, string) {
|
||||||
pathPieces := strings.Split(string(p), "\x1E")
|
pathPieces := strings.Split(string(p), "\x1E")
|
||||||
return MqlResultPath(strings.Join(pathPieces[:len(pathPieces)-1], "\x1E")), pathPieces[len(pathPieces)-1]
|
return ResultPath(strings.Join(pathPieces[:len(pathPieces)-1], "\x1E")), pathPieces[len(pathPieces)-1]
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p MqlResultPath) AppendValue(value string) MqlResultPath {
|
func (p ResultPath) AppendValue(value string) ResultPath {
|
||||||
if string(p) == "" {
|
if string(p) == "" {
|
||||||
return MqlResultPath(value)
|
return ResultPath(value)
|
||||||
}
|
}
|
||||||
return MqlResultPath(fmt.Sprintf("%s\x1E%s", p, value))
|
return ResultPath(fmt.Sprintf("%s\x1E%s", p, value))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p MqlPath) ToResultPathFromMap(resultMap map[MqlPath]string) MqlResultPath {
|
func (p Path) ToResultPathFromMap(resultMap map[Path]string) ResultPath {
|
||||||
output := NewMqlResultPath()
|
output := NewResultPath()
|
||||||
pathPieces := strings.Split(string(p), "\x1E")[1:]
|
pathPieces := strings.Split(string(p), "\x1E")[1:]
|
||||||
pathSoFar := NewMqlPath()
|
pathSoFar := NewPath()
|
||||||
for _, piece := range pathPieces {
|
for _, piece := range pathPieces {
|
||||||
output = output.FollowPath(piece, resultMap[pathSoFar])
|
output = output.FollowPath(piece, resultMap[pathSoFar])
|
||||||
pathSoFar = pathSoFar.Follow(piece)
|
pathSoFar = pathSoFar.Follow(piece)
|
||||||
|
|
@ -100,8 +100,8 @@ func (p MqlPath) ToResultPathFromMap(resultMap map[MqlPath]string) MqlResultPath
|
||||||
return output
|
return output
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMqlQuery(ses *Session) *MqlQuery {
|
func NewQuery(ses *Session) *Query {
|
||||||
var q MqlQuery
|
var q Query
|
||||||
q.ses = ses
|
q.ses = ses
|
||||||
q.results = make([]interface{}, 0)
|
q.results = make([]interface{}, 0)
|
||||||
q.resultOrder = make([]string, 0)
|
q.resultOrder = make([]string, 0)
|
||||||
|
|
|
||||||
|
|
@ -26,7 +26,7 @@ import (
|
||||||
|
|
||||||
type Session struct {
|
type Session struct {
|
||||||
ts graph.TripleStore
|
ts graph.TripleStore
|
||||||
currentQuery *MqlQuery
|
currentQuery *Query
|
||||||
debug bool
|
debug bool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -47,7 +47,7 @@ func (m *Session) GetQuery(input string, output_struct chan map[string]interface
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
m.currentQuery = NewMqlQuery(m)
|
m.currentQuery = NewQuery(m)
|
||||||
m.currentQuery.BuildIteratorTree(mqlQuery)
|
m.currentQuery.BuildIteratorTree(mqlQuery)
|
||||||
output := make(map[string]interface{})
|
output := make(map[string]interface{})
|
||||||
graph.OutputQueryShapeForIterator(m.currentQuery.it, m.ts, &output)
|
graph.OutputQueryShapeForIterator(m.currentQuery.it, m.ts, &output)
|
||||||
|
|
@ -61,7 +61,7 @@ func (m *Session) GetQuery(input string, output_struct chan map[string]interface
|
||||||
output_struct <- output
|
output_struct <- output
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Session) InputParses(input string) (graph.ParseResult, error) {
|
func (s *Session) InputParses(input string) (graph.ParseResult, error) {
|
||||||
var x interface{}
|
var x interface{}
|
||||||
err := json.Unmarshal([]byte(input), &x)
|
err := json.Unmarshal([]byte(input), &x)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
@ -70,19 +70,19 @@ func (m *Session) InputParses(input string) (graph.ParseResult, error) {
|
||||||
return graph.Parsed, nil
|
return graph.Parsed, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Session) ExecInput(input string, c chan interface{}, limit int) {
|
func (s *Session) ExecInput(input string, c chan interface{}, limit int) {
|
||||||
defer close(c)
|
defer close(c)
|
||||||
var mqlQuery interface{}
|
var mqlQuery interface{}
|
||||||
err := json.Unmarshal([]byte(input), &mqlQuery)
|
err := json.Unmarshal([]byte(input), &mqlQuery)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
m.currentQuery = NewMqlQuery(m)
|
s.currentQuery = NewQuery(s)
|
||||||
m.currentQuery.BuildIteratorTree(mqlQuery)
|
s.currentQuery.BuildIteratorTree(mqlQuery)
|
||||||
if m.currentQuery.isError {
|
if s.currentQuery.isError {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
it, _ := m.currentQuery.it.Optimize()
|
it, _ := s.currentQuery.it.Optimize()
|
||||||
if glog.V(2) {
|
if glog.V(2) {
|
||||||
glog.V(2).Infoln(it.DebugString(0))
|
glog.V(2).Infoln(it.DebugString(0))
|
||||||
}
|
}
|
||||||
|
|
@ -102,13 +102,13 @@ func (m *Session) ExecInput(input string, c chan interface{}, limit int) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Session) ToText(result interface{}) string {
|
func (s *Session) ToText(result interface{}) string {
|
||||||
tags := *(result.(*map[string]graph.TSVal))
|
tags := *(result.(*map[string]graph.TSVal))
|
||||||
out := fmt.Sprintln("****")
|
out := fmt.Sprintln("****")
|
||||||
tagKeys := make([]string, len(tags))
|
tagKeys := make([]string, len(tags))
|
||||||
m.currentQuery.treeifyResult(tags)
|
s.currentQuery.treeifyResult(tags)
|
||||||
m.currentQuery.buildResults()
|
s.currentQuery.buildResults()
|
||||||
r, _ := json.MarshalIndent(m.currentQuery.results, "", " ")
|
r, _ := json.MarshalIndent(s.currentQuery.results, "", " ")
|
||||||
fmt.Println(string(r))
|
fmt.Println(string(r))
|
||||||
i := 0
|
i := 0
|
||||||
for k, _ := range tags {
|
for k, _ := range tags {
|
||||||
|
|
@ -120,25 +120,25 @@ func (m *Session) ToText(result interface{}) string {
|
||||||
if k == "$_" {
|
if k == "$_" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
out += fmt.Sprintf("%s : %s\n", k, m.ts.GetNameFor(tags[k]))
|
out += fmt.Sprintf("%s : %s\n", k, s.ts.GetNameFor(tags[k]))
|
||||||
}
|
}
|
||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Session) BuildJson(result interface{}) {
|
func (s *Session) BuildJson(result interface{}) {
|
||||||
m.currentQuery.treeifyResult(*(result.(*map[string]graph.TSVal)))
|
s.currentQuery.treeifyResult(*(result.(*map[string]graph.TSVal)))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Session) GetJson() (interface{}, error) {
|
func (s *Session) GetJson() (interface{}, error) {
|
||||||
m.currentQuery.buildResults()
|
s.currentQuery.buildResults()
|
||||||
if m.currentQuery.isError {
|
if s.currentQuery.isError {
|
||||||
return nil, m.currentQuery.err
|
return nil, s.currentQuery.err
|
||||||
} else {
|
} else {
|
||||||
return m.currentQuery.results, nil
|
return s.currentQuery.results, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *Session) ClearJson() {
|
func (s *Session) ClearJson() {
|
||||||
// Since we create a new MqlQuery underneath every query, clearing isn't necessary.
|
// Since we create a new Query underneath every query, clearing isn't necessary.
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue