Move query language packages into query

This commit is contained in:
kortschak 2014-06-28 01:43:13 +09:30
parent a5f8391739
commit c66d6d3994
26 changed files with 2193 additions and 2193 deletions

View file

@ -0,0 +1,181 @@
// 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 (
"errors"
"fmt"
"log"
"math"
"strings"
"github.com/google/cayley/graph"
)
func (m *MqlQuery) buildFixed(s string) graph.Iterator {
f := m.ses.ts.MakeFixed()
f.AddValue(m.ses.ts.GetIdFor(s))
return f
}
func (m *MqlQuery) buildResultIterator(path MqlPath) graph.Iterator {
all := m.ses.ts.GetNodesAllIterator()
all.AddTag(string(path))
return graph.NewOptionalIterator(all)
}
func (m *MqlQuery) BuildIteratorTree(query interface{}) {
m.isRepeated = make(map[MqlPath]bool)
m.queryStructure = make(map[MqlPath]map[string]interface{})
m.queryResult = make(map[MqlResultPath]map[string]interface{})
m.queryResult[""] = make(map[string]interface{})
m.it, m.err = m.buildIteratorTreeInternal(query, NewMqlPath())
if m.err != nil {
m.isError = true
}
}
func (m *MqlQuery) buildIteratorTreeInternal(query interface{}, path MqlPath) (graph.Iterator, error) {
var it graph.Iterator
var err error
err = nil
switch t := query.(type) {
case bool:
// for JSON booleans
// 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.
if t {
it = m.buildFixed("true")
}
it = m.buildFixed("false")
case float64:
// for JSON numbers
// Damn you, Javascript, and your lack of integer values.
if math.Floor(t) == t {
// Treat it like an integer.
it = m.buildFixed(fmt.Sprintf("%d", t))
} else {
it = m.buildFixed(fmt.Sprintf("%f", t))
}
case string:
// for JSON strings
it = m.buildFixed(t)
case []interface{}:
// for JSON arrays
m.isRepeated[path] = true
if len(t) == 0 {
it = m.buildResultIterator(path)
} else if len(t) == 1 {
it, err = m.buildIteratorTreeInternal(t[0], path)
} else {
err = errors.New(fmt.Sprintf("Multiple fields at location root%s", path.DisplayString()))
}
case map[string]interface{}:
// for JSON objects
it, err = m.buildIteratorTreeMapInternal(t, path)
case nil:
it = m.buildResultIterator(path)
default:
log.Fatal("Unknown JSON type?", query)
}
if err != nil {
return nil, err
}
it.AddTag(string(path))
return it, nil
}
func (m *MqlQuery) buildIteratorTreeMapInternal(query map[string]interface{}, path MqlPath) (graph.Iterator, error) {
it := graph.NewAndIterator()
it.AddSubIterator(m.ses.ts.GetNodesAllIterator())
var err error
err = nil
outputStructure := make(map[string]interface{})
for key, subquery := range query {
outputStructure[key] = nil
reverse := false
pred := key
if strings.HasPrefix(pred, "@") {
i := strings.Index(pred, ":")
if i != -1 {
pred = pred[(i + 1):]
}
}
if strings.HasPrefix(pred, "!") {
reverse = true
pred = strings.TrimPrefix(pred, "!")
}
// Other special constructs here
var subit graph.Iterator
if key == "id" {
subit, err = m.buildIteratorTreeInternal(subquery, path.Follow(key))
if err != nil {
return nil, err
}
it.AddSubIterator(subit)
} else {
subit, err = m.buildIteratorTreeInternal(subquery, path.Follow(key))
if err != nil {
return nil, err
}
subAnd := graph.NewAndIterator()
predFixed := m.ses.ts.MakeFixed()
predFixed.AddValue(m.ses.ts.GetIdFor(pred))
subAnd.AddSubIterator(graph.NewLinksToIterator(m.ses.ts, predFixed, "p"))
if reverse {
lto := graph.NewLinksToIterator(m.ses.ts, subit, "s")
subAnd.AddSubIterator(lto)
hasa := graph.NewHasaIterator(m.ses.ts, subAnd, "o")
it.AddSubIterator(hasa)
} else {
lto := graph.NewLinksToIterator(m.ses.ts, subit, "o")
subAnd.AddSubIterator(lto)
hasa := graph.NewHasaIterator(m.ses.ts, subAnd, "s")
it.AddSubIterator(hasa)
}
}
}
if err != nil {
return nil, err
}
m.queryStructure[path] = outputStructure
return it, nil
}
type MqlResultPathSlice []MqlResultPath
func (sl MqlResultPathSlice) Len() int {
return len(sl)
}
func (sl MqlResultPathSlice) Less(i, j int) bool {
iLen := len(strings.Split(string(sl[i]), "\x30"))
jLen := len(strings.Split(string(sl[j]), "\x30"))
if iLen < jLen {
return true
}
if iLen == jLen {
if len(string(sl[i])) < len(string(sl[j])) {
return true
}
}
return false
}
func (sl MqlResultPathSlice) Swap(i, j int) {
sl[i], sl[j] = sl[j], sl[i]
}

114
query/mql/mql_fill.go Normal file
View file

@ -0,0 +1,114 @@
// 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 (
"sort"
"github.com/google/cayley/graph"
)
func (m *MqlQuery) treeifyResult(tags map[string]graph.TSVal) map[MqlResultPath]string {
// Transform the map into something a little more interesting.
results := make(map[MqlPath]string)
for k, v := range tags {
results[MqlPath(k)] = m.ses.ts.GetNameFor(v)
}
resultPaths := make(map[MqlResultPath]string)
for k, v := range results {
resultPaths[k.ToResultPathFromMap(results)] = v
}
var paths MqlResultPathSlice
for path, _ := range resultPaths {
paths = append(paths, path)
}
sort.Sort(paths)
// Build Structure
for _, path := range paths {
currentPath := path.getPath()
value := resultPaths[path]
namePath := path.AppendValue(value)
if _, ok := m.queryResult[namePath]; !ok {
targetPath, key := path.splitLastPath()
if path == "" {
targetPath, key = "", value
if _, ok := m.queryResult[""][value]; !ok {
m.resultOrder = append(m.resultOrder, value)
}
}
if _, ok := m.queryStructure[currentPath]; ok {
// If there's substructure, then copy that in.
newStruct := m.copyPathStructure(currentPath)
if m.isRepeated[currentPath] && currentPath != "" {
switch t := m.queryResult[targetPath][key].(type) {
case nil:
x := make([]interface{}, 0)
x = append(x, newStruct)
m.queryResult[targetPath][key] = x
m.queryResult[namePath] = newStruct
case []interface{}:
m.queryResult[targetPath][key] = append(t, newStruct)
m.queryResult[namePath] = newStruct
}
} else {
m.queryResult[namePath] = newStruct
m.queryResult[targetPath][key] = newStruct
}
}
}
}
// Fill values
for _, path := range paths {
currentPath := path.getPath()
value := resultPaths[path]
namePath := path.AppendValue(value)
if _, ok := m.queryStructure[currentPath]; ok {
// We're dealing with ids.
if _, ok := m.queryResult[namePath]["id"]; ok {
m.queryResult[namePath]["id"] = value
}
} else {
// Just a value.
targetPath, key := path.splitLastPath()
if m.isRepeated[currentPath] {
switch t := m.queryResult[targetPath][key].(type) {
case nil:
x := make([]interface{}, 0)
x = append(x, value)
m.queryResult[targetPath][key] = x
case []interface{}:
m.queryResult[targetPath][key] = append(t, value)
}
} else {
m.queryResult[targetPath][key] = value
}
}
}
return resultPaths
}
func (m *MqlQuery) buildResults() {
for _, v := range m.resultOrder {
m.results = append(m.results, m.queryResult[""][v])
}
}

View file

@ -0,0 +1,264 @@
// 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"
"testing"
. "github.com/smartystreets/goconvey/convey"
"github.com/google/cayley/graph/memstore"
)
// +---+ +---+
// | A |------- ->| F |<--
// +---+ \------>+---+-/ +---+ \--+---+
// ------>|#B#| | | E |
// +---+-------/ >+---+ | +---+
// | C | / v
// +---+ -/ +---+
// ---- +---+/ |#G#|
// \-->|#D#|------------->+---+
// +---+
//
func buildTripleStore() *MqlSession {
ts := memstore.MakeTestingMemstore()
return NewMqlSession(ts)
}
func compareJsonInterfaces(actual interface{}, expected interface{}, path MqlPath, t *testing.T) {
isError := false
switch ex := expected.(type) {
case bool:
switch ac := actual.(type) {
case bool:
if ac != ex {
isError = true
}
default:
t.Log("Mismatched type")
isError = true
}
case float64:
switch ac := actual.(type) {
case float64:
if ac != ex {
isError = true
}
default:
t.Log("Mismatched type")
isError = true
}
case string:
switch ac := actual.(type) {
case string:
if ac != ex {
isError = true
}
default:
isError = true
}
case []interface{}:
switch ac := actual.(type) {
case []interface{}:
if len(ac) != len(ex) {
t.Log("Different lengths")
isError = true
} else {
for i, elem := range ex {
compareJsonInterfaces(ac[i], elem, path.Follow(string(i)), t)
}
}
default:
t.Log("Mismatched type")
isError = true
}
case map[string]interface{}:
switch ac := actual.(type) {
case map[string]interface{}:
for k, v := range ex {
actual_value, ok := ac[k]
if !ok {
t.Log("Key", k, "not in actual output.")
isError = true
} else {
compareJsonInterfaces(actual_value, v, path.Follow(string(k)), t)
}
}
default:
t.Log("Mismatched type")
isError = true
}
case nil:
switch ac := actual.(type) {
case nil:
if ac != ex {
isError = true
}
default:
t.Log("Mismatched type")
isError = true
}
default:
t.Error("Unknown JSON type?", expected)
}
if isError {
actual_bytes, _ := json.MarshalIndent(actual, "", " ")
expected_bytes, _ := json.MarshalIndent(expected, "", " ")
t.Error(path.DisplayString(), ":\n", string(actual_bytes), "\nexpected", string(expected_bytes))
}
}
func runAndTestQuery(query string, expected string, t *testing.T) {
ses := buildTripleStore()
c := make(chan interface{}, 5)
go ses.ExecInput(query, c, -1)
for result := range c {
ses.BuildJson(result)
}
actual_struct, _ := ses.GetJson()
var expected_struct interface{}
json.Unmarshal([]byte(expected), &expected_struct)
compareJsonInterfaces(actual_struct, expected_struct, NewMqlPath(), t)
ses.ClearJson()
}
func TestGetAllIds(t *testing.T) {
Convey("Should get all IDs in the database", t, func() {
query := `
[{"id": null}]
`
expected := `
[
{"id": "A"},
{"id": "follows"},
{"id": "B"},
{"id": "C"},
{"id": "D"},
{"id": "F"},
{"id": "G"},
{"id": "E"},
{"id": "status"},
{"id": "cool"},
{"id": "status_graph"}
]
`
runAndTestQuery(query, expected, t)
})
}
func TestGetCool(t *testing.T) {
query := `
[{"id": null, "status": "cool"}]
`
expected := `
[
{"id": "B", "status": "cool"},
{"id": "D", "status": "cool"},
{"id": "G", "status": "cool"}
]
`
runAndTestQuery(query, expected, t)
}
func TestGetFollowsList(t *testing.T) {
query := `
[{"id": "C", "follows": []}]
`
expected := `
[{
"id": "C",
"follows": [
"B", "D"
]
}]
`
runAndTestQuery(query, expected, t)
}
func TestGetFollowsStruct(t *testing.T) {
query := `
[{"id": null, "follows": {"id": null, "status": "cool"}}]
`
expected := `
[
{"id": "A", "follows": {"id": "B", "status": "cool"}},
{"id": "C", "follows": {"id": "D", "status": "cool"}},
{"id": "D", "follows": {"id": "G", "status": "cool"}},
{"id": "F", "follows": {"id": "G", "status": "cool"}}
]
`
runAndTestQuery(query, expected, t)
}
func TestGetFollowsReverseStructList(t *testing.T) {
query := `
[{"id": null, "!follows": [{"id": null, "status" : "cool"}]}]
`
expected := `
[
{"id": "F", "!follows": [{"id": "B", "status": "cool"}]},
{"id": "B", "!follows": [{"id": "D", "status": "cool"}]},
{"id": "G", "!follows": [{"id": "D", "status": "cool"}]}
]
`
runAndTestQuery(query, expected, t)
}
func TestGetRevFollowsList(t *testing.T) {
query := `
[{"id": "F", "!follows": []}]
`
expected := `
[{
"id": "F",
"!follows": [
"B", "E"
]
}]
`
runAndTestQuery(query, expected, t)
}
func TestCoFollows(t *testing.T) {
query := `
[{"id": null, "@A:follows": "B", "@B:follows": "D"}]
`
expected := `
[{
"id": "C",
"@A:follows": "B",
"@B:follows": "D"
}]
`
runAndTestQuery(query, expected, t)
}
func TestRevCoFollows(t *testing.T) {
query := `
[{"id": null, "!follows": {"id": "C"}, "@a:!follows": "D"}]
`
expected := `
[{
"id": "B",
"!follows": {"id": "C"},
"@a:!follows": "D"
}]
`
runAndTestQuery(query, expected, t)
}

111
query/mql/mql_query.go Normal file
View file

@ -0,0 +1,111 @@
// 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 (
"fmt"
"strings"
"github.com/google/cayley/graph"
)
type MqlPath string
type MqlResultPath string
type MqlQuery struct {
ses *MqlSession
it graph.Iterator
isRepeated map[MqlPath]bool
queryStructure map[MqlPath]map[string]interface{}
queryResult map[MqlResultPath]map[string]interface{}
results []interface{}
resultOrder []string
isError bool
err error
}
func (mqlQuery *MqlQuery) copyPathStructure(path MqlPath) map[string]interface{} {
output := make(map[string]interface{})
for k, v := range mqlQuery.queryStructure[path] {
output[k] = v
}
return output
}
func NewMqlPath() MqlPath {
return ""
}
func (p MqlPath) Follow(s string) MqlPath {
return MqlPath(fmt.Sprintf("%s\x1E%s", p, s))
}
func (p MqlPath) DisplayString() string {
return strings.Replace(string(p), "\x1E", ".", -1)
}
func NewMqlResultPath() MqlResultPath {
return ""
}
func (p MqlResultPath) FollowPath(followPiece string, value string) MqlResultPath {
if string(p) == "" {
return MqlResultPath(fmt.Sprintf("%s\x1E%s", value, followPiece))
}
return MqlResultPath(fmt.Sprintf("%s\x1E%s\x1E%s", p, value, followPiece))
}
func (p MqlResultPath) getPath() MqlPath {
out := NewMqlPath()
pathPieces := strings.Split(string(p), "\x1E")
for len(pathPieces) > 1 {
a := pathPieces[1]
pathPieces = pathPieces[2:]
out = out.Follow(a)
}
return out
}
func (p MqlResultPath) splitLastPath() (MqlResultPath, string) {
pathPieces := strings.Split(string(p), "\x1E")
return MqlResultPath(strings.Join(pathPieces[:len(pathPieces)-1], "\x1E")), pathPieces[len(pathPieces)-1]
}
func (p MqlResultPath) AppendValue(value string) MqlResultPath {
if string(p) == "" {
return MqlResultPath(value)
}
return MqlResultPath(fmt.Sprintf("%s\x1E%s", p, value))
}
func (p MqlPath) ToResultPathFromMap(resultMap map[MqlPath]string) MqlResultPath {
output := NewMqlResultPath()
pathPieces := strings.Split(string(p), "\x1E")[1:]
pathSoFar := NewMqlPath()
for _, piece := range pathPieces {
output = output.FollowPath(piece, resultMap[pathSoFar])
pathSoFar = pathSoFar.Follow(piece)
}
return output
}
func NewMqlQuery(ses *MqlSession) *MqlQuery {
var q MqlQuery
q.ses = ses
q.results = make([]interface{}, 0)
q.resultOrder = make([]string, 0)
q.err = nil
q.isError = false
return &q
}

144
query/mql/mql_session.go Normal file
View file

@ -0,0 +1,144 @@
// 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"
)
type MqlSession struct {
ts graph.TripleStore
currentQuery *MqlQuery
debug bool
}
func NewMqlSession(ts graph.TripleStore) *MqlSession {
var m MqlSession
m.ts = ts
return &m
}
func (m *MqlSession) ToggleDebug() {
m.debug = !m.debug
}
func (m *MqlSession) 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 = NewMqlQuery(m)
m.currentQuery.BuildIteratorTree(mqlQuery)
output := make(map[string]interface{})
graph.OutputQueryShapeForIterator(m.currentQuery.it, m.ts, &output)
nodes := output["nodes"].([]graph.Node)
new_nodes := make([]graph.Node, 0)
for _, n := range nodes {
n.Tags = nil
new_nodes = append(new_nodes, n)
}
output["nodes"] = new_nodes
output_struct <- output
}
func (m *MqlSession) InputParses(input string) (graph.ParseResult, error) {
var x interface{}
err := json.Unmarshal([]byte(input), &x)
if err != nil {
return graph.ParseFail, err
}
return graph.Parsed, nil
}
func (m *MqlSession) ExecInput(input string, c chan interface{}, limit int) {
defer close(c)
var mqlQuery interface{}
err := json.Unmarshal([]byte(input), &mqlQuery)
if err != nil {
return
}
m.currentQuery = NewMqlQuery(m)
m.currentQuery.BuildIteratorTree(mqlQuery)
if m.currentQuery.isError {
return
}
it, _ := m.currentQuery.it.Optimize()
if glog.V(2) {
glog.V(2).Infoln(it.DebugString(0))
}
for {
_, ok := it.Next()
if !ok {
break
}
tags := make(map[string]graph.TSVal)
it.TagResults(&tags)
c <- &tags
for it.NextResult() == true {
tags := make(map[string]graph.TSVal)
it.TagResults(&tags)
c <- &tags
}
}
}
func (m *MqlSession) ToText(result interface{}) string {
tags := *(result.(*map[string]graph.TSVal))
out := fmt.Sprintln("****")
tagKeys := make([]string, len(tags))
m.currentQuery.treeifyResult(tags)
m.currentQuery.buildResults()
r, _ := json.MarshalIndent(m.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, m.ts.GetNameFor(tags[k]))
}
return out
}
func (m *MqlSession) BuildJson(result interface{}) {
m.currentQuery.treeifyResult(*(result.(*map[string]graph.TSVal)))
}
func (m *MqlSession) GetJson() (interface{}, error) {
m.currentQuery.buildResults()
if m.currentQuery.isError {
return nil, m.currentQuery.err
} else {
return m.currentQuery.results, nil
}
}
func (m *MqlSession) ClearJson() {
// Since we create a new MqlQuery underneath every query, clearing isn't necessary.
return
}