From 929beb624c7686c8c8b8a94bc6e0f891222e7d7d Mon Sep 17 00:00:00 2001 From: kortschak Date: Sat, 28 Jun 2014 22:09:42 +0930 Subject: [PATCH] Canonicalise mql receiver names And again, remove a multitude of redundant infixes I missed previously. --- query/mql/build_iterator.go | 90 ++++++++++++++++++++++---------------------- query/mql/fill.go | 58 ++++++++++++++-------------- query/mql/functional_test.go | 4 +- query/mql/query.go | 56 +++++++++++++-------------- query/mql/session.go | 44 +++++++++++----------- 5 files changed, 126 insertions(+), 126 deletions(-) diff --git a/query/mql/build_iterator.go b/query/mql/build_iterator.go index 6273696..77569ad 100644 --- a/query/mql/build_iterator.go +++ b/query/mql/build_iterator.go @@ -24,31 +24,31 @@ import ( "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)) +func (q *Query) buildFixed(s string) graph.Iterator { + f := q.ses.ts.MakeFixed() + f.AddValue(q.ses.ts.GetIdFor(s)) return f } -func (m *MqlQuery) buildResultIterator(path MqlPath) graph.Iterator { - all := m.ses.ts.GetNodesAllIterator() +func (q *Query) buildResultIterator(path Path) graph.Iterator { + all := q.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{}) +func (q *Query) BuildIteratorTree(query interface{}) { + q.isRepeated = make(map[Path]bool) + q.queryStructure = make(map[Path]map[string]interface{}) + q.queryResult = make(map[ResultPath]map[string]interface{}) + q.queryResult[""] = make(map[string]interface{}) - m.it, m.err = m.buildIteratorTreeInternal(query, NewMqlPath()) - if m.err != nil { - m.isError = true + q.it, q.err = q.buildIteratorTreeInternal(query, NewPath()) + if q.err != nil { + 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 err error 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. // Things which are really bool-like are special cases and will be dealt with separately. if t { - it = m.buildFixed("true") + it = q.buildFixed("true") } - it = m.buildFixed("false") + it = q.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)) + it = q.buildFixed(fmt.Sprintf("%d", t)) } else { - it = m.buildFixed(fmt.Sprintf("%f", t)) + it = q.buildFixed(fmt.Sprintf("%f", t)) } case string: // for JSON strings - it = m.buildFixed(t) + it = q.buildFixed(t) case []interface{}: // for JSON arrays - m.isRepeated[path] = true + q.isRepeated[path] = true if len(t) == 0 { - it = m.buildResultIterator(path) + it = q.buildResultIterator(path) } else if len(t) == 1 { - it, err = m.buildIteratorTreeInternal(t[0], path) + it, err = q.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) + it, err = q.buildIteratorTreeMapInternal(t, path) case nil: - it = m.buildResultIterator(path) + it = q.buildResultIterator(path) default: log.Fatal("Unknown JSON type?", query) } @@ -98,9 +98,9 @@ func (m *MqlQuery) buildIteratorTreeInternal(query interface{}, path MqlPath) (g 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.AddSubIterator(m.ses.ts.GetNodesAllIterator()) + it.AddSubIterator(q.ses.ts.GetNodesAllIterator()) var err error err = nil outputStructure := make(map[string]interface{}) @@ -122,29 +122,29 @@ func (m *MqlQuery) buildIteratorTreeMapInternal(query map[string]interface{}, pa // Other special constructs here var subit graph.Iterator if key == "id" { - subit, err = m.buildIteratorTreeInternal(subquery, path.Follow(key)) + subit, err = q.buildIteratorTreeInternal(subquery, path.Follow(key)) if err != nil { return nil, err } it.AddSubIterator(subit) } else { - subit, err = m.buildIteratorTreeInternal(subquery, path.Follow(key)) + subit, err = q.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")) + predFixed := q.ses.ts.MakeFixed() + predFixed.AddValue(q.ses.ts.GetIdFor(pred)) + subAnd.AddSubIterator(graph.NewLinksToIterator(q.ses.ts, predFixed, "p")) if reverse { - lto := graph.NewLinksToIterator(m.ses.ts, subit, "s") + lto := graph.NewLinksToIterator(q.ses.ts, subit, "s") subAnd.AddSubIterator(lto) - hasa := graph.NewHasaIterator(m.ses.ts, subAnd, "o") + hasa := graph.NewHasaIterator(q.ses.ts, subAnd, "o") it.AddSubIterator(hasa) } else { - lto := graph.NewLinksToIterator(m.ses.ts, subit, "o") + lto := graph.NewLinksToIterator(q.ses.ts, subit, "o") subAnd.AddSubIterator(lto) - hasa := graph.NewHasaIterator(m.ses.ts, subAnd, "s") + hasa := graph.NewHasaIterator(q.ses.ts, subAnd, "s") it.AddSubIterator(hasa) } } @@ -152,30 +152,30 @@ func (m *MqlQuery) buildIteratorTreeMapInternal(query map[string]interface{}, pa if err != nil { return nil, err } - m.queryStructure[path] = outputStructure + q.queryStructure[path] = outputStructure return it, nil } -type MqlResultPathSlice []MqlResultPath +type ResultPathSlice []ResultPath -func (sl MqlResultPathSlice) Len() int { - return len(sl) +func (p ResultPathSlice) Len() int { + return len(p) } -func (sl MqlResultPathSlice) Less(i, j int) bool { - iLen := len(strings.Split(string(sl[i]), "\x30")) - jLen := len(strings.Split(string(sl[j]), "\x30")) +func (p ResultPathSlice) Less(i, j int) bool { + iLen := len(strings.Split(string(p[i]), "\x30")) + jLen := len(strings.Split(string(p[j]), "\x30")) if iLen < jLen { return true } if iLen == jLen { - if len(string(sl[i])) < len(string(sl[j])) { + if len(string(p[i])) < len(string(p[j])) { return true } } return false } -func (sl MqlResultPathSlice) Swap(i, j int) { - sl[i], sl[j] = sl[j], sl[i] +func (p ResultPathSlice) Swap(i, j int) { + p[i], p[j] = p[j], p[i] } diff --git a/query/mql/fill.go b/query/mql/fill.go index 26de32a..5ecc2b3 100644 --- a/query/mql/fill.go +++ b/query/mql/fill.go @@ -20,18 +20,18 @@ import ( "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. - results := make(map[MqlPath]string) + results := make(map[Path]string) 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 { resultPaths[k.ToResultPathFromMap(results)] = v } - var paths MqlResultPathSlice + var paths ResultPathSlice for path, _ := range resultPaths { paths = append(paths, path) @@ -44,32 +44,32 @@ func (m *MqlQuery) treeifyResult(tags map[string]graph.TSVal) map[MqlResultPath] currentPath := path.getPath() value := resultPaths[path] namePath := path.AppendValue(value) - if _, ok := m.queryResult[namePath]; !ok { + if _, ok := q.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 := q.queryResult[""][value]; !ok { + 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. - newStruct := m.copyPathStructure(currentPath) - if m.isRepeated[currentPath] && currentPath != "" { - switch t := m.queryResult[targetPath][key].(type) { + newStruct := q.copyPathStructure(currentPath) + if q.isRepeated[currentPath] && currentPath != "" { + switch t := q.queryResult[targetPath][key].(type) { case nil: x := make([]interface{}, 0) x = append(x, newStruct) - m.queryResult[targetPath][key] = x - m.queryResult[namePath] = newStruct + q.queryResult[targetPath][key] = x + q.queryResult[namePath] = newStruct case []interface{}: - m.queryResult[targetPath][key] = append(t, newStruct) - m.queryResult[namePath] = newStruct + q.queryResult[targetPath][key] = append(t, newStruct) + q.queryResult[namePath] = newStruct } } else { - m.queryResult[namePath] = newStruct - m.queryResult[targetPath][key] = newStruct + q.queryResult[namePath] = newStruct + q.queryResult[targetPath][key] = newStruct } } } @@ -80,26 +80,26 @@ func (m *MqlQuery) treeifyResult(tags map[string]graph.TSVal) map[MqlResultPath] currentPath := path.getPath() value := resultPaths[path] namePath := path.AppendValue(value) - if _, ok := m.queryStructure[currentPath]; ok { + if _, ok := q.queryStructure[currentPath]; ok { // We're dealing with ids. - if _, ok := m.queryResult[namePath]["id"]; ok { - m.queryResult[namePath]["id"] = value + if _, ok := q.queryResult[namePath]["id"]; ok { + q.queryResult[namePath]["id"] = value } } else { // Just a value. targetPath, key := path.splitLastPath() - if m.isRepeated[currentPath] { - switch t := m.queryResult[targetPath][key].(type) { + if q.isRepeated[currentPath] { + switch t := q.queryResult[targetPath][key].(type) { case nil: x := make([]interface{}, 0) x = append(x, value) - m.queryResult[targetPath][key] = x + q.queryResult[targetPath][key] = x case []interface{}: - m.queryResult[targetPath][key] = append(t, value) + q.queryResult[targetPath][key] = append(t, value) } } 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 } -func (m *MqlQuery) buildResults() { - for _, v := range m.resultOrder { - m.results = append(m.results, m.queryResult[""][v]) +func (q *Query) buildResults() { + for _, v := range q.resultOrder { + q.results = append(q.results, q.queryResult[""][v]) } } diff --git a/query/mql/functional_test.go b/query/mql/functional_test.go index e814574..145d59f 100644 --- a/query/mql/functional_test.go +++ b/query/mql/functional_test.go @@ -40,7 +40,7 @@ func buildTripleStore() *Session { 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 switch ex := expected.(type) { case bool: @@ -134,7 +134,7 @@ func runAndTestQuery(query string, expected string, t *testing.T) { actual_struct, _ := ses.GetJson() var expected_struct interface{} json.Unmarshal([]byte(expected), &expected_struct) - compareJsonInterfaces(actual_struct, expected_struct, NewMqlPath(), t) + compareJsonInterfaces(actual_struct, expected_struct, NewPath(), t) ses.ClearJson() } diff --git a/query/mql/query.go b/query/mql/query.go index c52bb28..7460e48 100644 --- a/query/mql/query.go +++ b/query/mql/query.go @@ -21,53 +21,53 @@ import ( "github.com/google/cayley/graph" ) -type MqlPath string -type MqlResultPath string +type Path string +type ResultPath string -type MqlQuery struct { +type Query struct { ses *Session it graph.Iterator - isRepeated map[MqlPath]bool - queryStructure map[MqlPath]map[string]interface{} - queryResult map[MqlResultPath]map[string]interface{} + isRepeated map[Path]bool + queryStructure map[Path]map[string]interface{} + queryResult map[ResultPath]map[string]interface{} results []interface{} resultOrder []string isError bool 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{}) - for k, v := range mqlQuery.queryStructure[path] { + for k, v := range q.queryStructure[path] { output[k] = v } return output } -func NewMqlPath() MqlPath { +func NewPath() Path { return "" } -func (p MqlPath) Follow(s string) MqlPath { - return MqlPath(fmt.Sprintf("%s\x1E%s", p, s)) +func (p Path) Follow(s string) Path { + 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) } -func NewMqlResultPath() MqlResultPath { +func NewResultPath() ResultPath { return "" } -func (p MqlResultPath) FollowPath(followPiece string, value string) MqlResultPath { +func (p ResultPath) FollowPath(followPiece string, value string) ResultPath { 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 { - out := NewMqlPath() +func (p ResultPath) getPath() Path { + out := NewPath() pathPieces := strings.Split(string(p), "\x1E") for len(pathPieces) > 1 { a := pathPieces[1] @@ -77,22 +77,22 @@ func (p MqlResultPath) getPath() MqlPath { return out } -func (p MqlResultPath) splitLastPath() (MqlResultPath, string) { +func (p ResultPath) splitLastPath() (ResultPath, string) { 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) == "" { - 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 { - output := NewMqlResultPath() +func (p Path) ToResultPathFromMap(resultMap map[Path]string) ResultPath { + output := NewResultPath() pathPieces := strings.Split(string(p), "\x1E")[1:] - pathSoFar := NewMqlPath() + pathSoFar := NewPath() for _, piece := range pathPieces { output = output.FollowPath(piece, resultMap[pathSoFar]) pathSoFar = pathSoFar.Follow(piece) @@ -100,8 +100,8 @@ func (p MqlPath) ToResultPathFromMap(resultMap map[MqlPath]string) MqlResultPath return output } -func NewMqlQuery(ses *Session) *MqlQuery { - var q MqlQuery +func NewQuery(ses *Session) *Query { + var q Query q.ses = ses q.results = make([]interface{}, 0) q.resultOrder = make([]string, 0) diff --git a/query/mql/session.go b/query/mql/session.go index 8565581..b249dd7 100644 --- a/query/mql/session.go +++ b/query/mql/session.go @@ -26,7 +26,7 @@ import ( type Session struct { ts graph.TripleStore - currentQuery *MqlQuery + currentQuery *Query debug bool } @@ -47,7 +47,7 @@ func (m *Session) GetQuery(input string, output_struct chan map[string]interface if err != nil { return } - m.currentQuery = NewMqlQuery(m) + m.currentQuery = NewQuery(m) m.currentQuery.BuildIteratorTree(mqlQuery) output := make(map[string]interface{}) 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 } -func (m *Session) InputParses(input string) (graph.ParseResult, error) { +func (s *Session) InputParses(input string) (graph.ParseResult, error) { var x interface{} err := json.Unmarshal([]byte(input), &x) if err != nil { @@ -70,19 +70,19 @@ func (m *Session) InputParses(input string) (graph.ParseResult, error) { 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) 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 { + s.currentQuery = NewQuery(s) + s.currentQuery.BuildIteratorTree(mqlQuery) + if s.currentQuery.isError { return } - it, _ := m.currentQuery.it.Optimize() + it, _ := s.currentQuery.it.Optimize() if glog.V(2) { 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)) out := fmt.Sprintln("****") tagKeys := make([]string, len(tags)) - m.currentQuery.treeifyResult(tags) - m.currentQuery.buildResults() - r, _ := json.MarshalIndent(m.currentQuery.results, "", " ") + s.currentQuery.treeifyResult(tags) + s.currentQuery.buildResults() + r, _ := json.MarshalIndent(s.currentQuery.results, "", " ") fmt.Println(string(r)) i := 0 for k, _ := range tags { @@ -120,25 +120,25 @@ func (m *Session) ToText(result interface{}) string { if k == "$_" { 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 } -func (m *Session) BuildJson(result interface{}) { - m.currentQuery.treeifyResult(*(result.(*map[string]graph.TSVal))) +func (s *Session) BuildJson(result interface{}) { + s.currentQuery.treeifyResult(*(result.(*map[string]graph.TSVal))) } -func (m *Session) GetJson() (interface{}, error) { - m.currentQuery.buildResults() - if m.currentQuery.isError { - return nil, m.currentQuery.err +func (s *Session) GetJson() (interface{}, error) { + s.currentQuery.buildResults() + if s.currentQuery.isError { + return nil, s.currentQuery.err } else { - return m.currentQuery.results, nil + return s.currentQuery.results, nil } } -func (m *Session) ClearJson() { - // Since we create a new MqlQuery underneath every query, clearing isn't necessary. +func (s *Session) ClearJson() { + // Since we create a new Query underneath every query, clearing isn't necessary. return }