Commit bb5c1040 authored by Quentin Smith's avatar Quentin Smith

analysis/app: parse prefix separately

Change-Id: I185b853a2580ee16741cbf1581ba465400a5b28b
Reviewed-on: https://go-review.googlesource.com/35947Reviewed-by: default avatarRuss Cox <rsc@golang.org>
parent 68248f17
...@@ -167,7 +167,7 @@ type compareData struct { ...@@ -167,7 +167,7 @@ type compareData struct {
func (a *App) compareQuery(q string) *compareData { func (a *App) compareQuery(q string) *compareData {
// Parse query // Parse query
queries := parseQueryString(q) prefix, queries := parseQueryString(q)
// Send requests // Send requests
// TODO(quentin): Issue requests in parallel? // TODO(quentin): Issue requests in parallel?
...@@ -175,6 +175,9 @@ func (a *App) compareQuery(q string) *compareData { ...@@ -175,6 +175,9 @@ func (a *App) compareQuery(q string) *compareData {
var found int var found int
for _, qPart := range queries { for _, qPart := range queries {
group := &resultGroup{} group := &resultGroup{}
if prefix != "" {
qPart = prefix + " " + qPart
}
res := a.StorageClient.Query(qPart) res := a.StorageClient.Query(qPart)
for res.Next() { for res.Next() {
group.add(res.Result()) group.add(res.Result())
......
...@@ -8,11 +8,11 @@ import "strings" ...@@ -8,11 +8,11 @@ import "strings"
// parseQueryString splits a user-entered query into one or more storage server queries. // parseQueryString splits a user-entered query into one or more storage server queries.
// The supported query formats are: // The supported query formats are:
// prefix | one vs two - parsed as {"prefix one", "prefix two"} // prefix | one vs two - parsed as "prefix", {"one", "two"}
// prefix one vs two - parsed as {"prefix one", "two"} // prefix one vs two - parsed as "", {"prefix one", "two"}
// anything else - parsed as {"anything else"} // anything else - parsed as "", {"anything else"}
// The vs and | separators must not be quoted. // The vs and | separators must not be quoted.
func parseQueryString(q string) []string { func parseQueryString(q string) (string, []string) {
var queries []string var queries []string
var parts []string var parts []string
var prefix string var prefix string
...@@ -33,10 +33,10 @@ func parseQueryString(q string) []string { ...@@ -33,10 +33,10 @@ func parseQueryString(q string) []string {
case c == ' ', c == '\t': case c == ' ', c == '\t':
switch part := q[:r]; { switch part := q[:r]; {
case part == "|" && prefix == "": case part == "|" && prefix == "":
prefix = strings.Join(parts, " ") + " " prefix = strings.Join(parts, " ")
parts = nil parts = nil
case part == "vs": case part == "vs":
queries = append(queries, prefix+strings.Join(parts, " ")) queries = append(queries, strings.Join(parts, " "))
parts = nil parts = nil
default: default:
parts = append(parts, part) parts = append(parts, part)
...@@ -54,7 +54,7 @@ func parseQueryString(q string) []string { ...@@ -54,7 +54,7 @@ func parseQueryString(q string) []string {
parts = append(parts, q) parts = append(parts, q)
} }
if len(parts) > 0 { if len(parts) > 0 {
queries = append(queries, prefix+strings.Join(parts, " ")) queries = append(queries, strings.Join(parts, " "))
} }
return queries return prefix, queries
} }
...@@ -12,19 +12,23 @@ import ( ...@@ -12,19 +12,23 @@ import (
func TestParseQueryString(t *testing.T) { func TestParseQueryString(t *testing.T) {
tests := []struct { tests := []struct {
q string q string
want []string wantPrefix string
wantParts []string
}{ }{
{"prefix | one vs two", []string{"prefix one", "prefix two"}}, {"prefix | one vs two", "prefix", []string{"one", "two"}},
{"prefix one vs two", []string{"prefix one", "two"}}, {"prefix one vs two", "", []string{"prefix one", "two"}},
{"anything else", []string{"anything else"}}, {"anything else", "", []string{"anything else"}},
{`one vs "two vs three"`, []string{"one", `"two vs three"`}}, {`one vs "two vs three"`, "", []string{"one", `"two vs three"`}},
{"mixed\ttabs \"and\tspaces\"", []string{"mixed tabs \"and\tspaces\""}}, {"mixed\ttabs \"and\tspaces\"", "", []string{"mixed tabs \"and\tspaces\""}},
} }
for _, test := range tests { for _, test := range tests {
t.Run(test.q, func(t *testing.T) { t.Run(test.q, func(t *testing.T) {
have := parseQueryString(test.q) havePrefix, haveParts := parseQueryString(test.q)
if !reflect.DeepEqual(have, test.want) { if havePrefix != test.wantPrefix {
t.Fatalf("parseQueryString = %#v, want %#v", have, test.want) t.Errorf("parseQueryString returned prefix %q, want %q", havePrefix, test.wantPrefix)
}
if !reflect.DeepEqual(haveParts, test.wantParts) {
t.Errorf("parseQueryString returned parts %#v, want %#v", haveParts, test.wantParts)
} }
}) })
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment