Skip to content

Commit ea149dc

Browse files
authored
Make it possible to use wildcards to get field names (#87)
* Make it possible to use wildcards to get field names * Change sleep time to 3 seconds
1 parent 3064b5c commit ea149dc

File tree

7 files changed

+100
-8
lines changed

7 files changed

+100
-8
lines changed

metastore_integration_test/storage_dynamodb_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func TestDynamodbStorageStorageEvents(t *testing.T) {
231231
// dynamodbStorage.Put("/world.txt", []byte("world"))
232232

233233
// wait for events to be processed
234-
time.Sleep(1 * time.Second)
234+
time.Sleep(3 * time.Second)
235235
done2 <- true
236236

237237
actual := len(eventList)

metastore_integration_test/storage_etcd_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ func TestEtcdStorageEvents(t *testing.T) {
220220
etcdStorage.Put("/world.txt", []byte("world"))
221221

222222
// wait for events to be processed
223-
time.Sleep(1 * time.Second)
223+
time.Sleep(3 * time.Second)
224224

225225
done <- true
226226

metastore_test/storage_fs_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ func TestFileSystemStorageEvents(t *testing.T) {
244244
fsMetastore.Put("/hello2.txt", []byte("hello2"))
245245

246246
// wait for events to be processed
247-
time.Sleep(1 * time.Second)
247+
time.Sleep(3 * time.Second)
248248

249249
done <- true
250250

server/index_service.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/mosuka/phalanx/proto"
2626
phalanxaggregations "github.com/mosuka/phalanx/search/aggregations"
2727
phalanxqueries "github.com/mosuka/phalanx/search/queries"
28+
"github.com/mosuka/phalanx/util/wildcard"
2829
"github.com/thanhpk/randstr"
2930
"go.uber.org/zap"
3031
"golang.org/x/sync/errgroup"
@@ -1207,8 +1208,8 @@ func (s *IndexService) Search(req *proto.SearchRequest) (*proto.SearchResponse,
12071208
doc.Timestamp = timestamp.UTC().UnixNano()
12081209
default:
12091210
exists := false
1210-
for _, fieldName := range request.Fields {
1211-
if fieldName == field {
1211+
for _, reqField := range request.Fields {
1212+
if wildcard.Match(reqField, field) {
12121213
exists = true
12131214
break
12141215
}

server/marshaler.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,9 +218,6 @@ func (m *Marshaler) Unmarshal(data []byte, v interface{}) error {
218218
value.IndexName = indexName
219219
}
220220

221-
// if query, ok := m["query"].(string); ok {
222-
// value.Query = query
223-
// }
224221
if query, ok := m["query"].(map[string]interface{}); ok {
225222
queryType, ok := query["type"].(string)
226223
if !ok {

util/wildcard/wildcard.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package wildcard
2+
3+
import (
4+
"regexp"
5+
"strings"
6+
)
7+
8+
var wildcardRegexpReplacer = strings.NewReplacer(
9+
// characters in the wildcard that must
10+
// be escaped in the regexp
11+
"+", `\+`,
12+
"(", `\(`,
13+
")", `\)`,
14+
"^", `\^`,
15+
"$", `\$`,
16+
".", `\.`,
17+
"{", `\{`,
18+
"}", `\}`,
19+
"[", `\[`,
20+
"]", `\]`,
21+
`|`, `\|`,
22+
`\`, `\\`,
23+
// wildcard characters
24+
"*", ".*",
25+
"?", ".")
26+
27+
func Match(pattern, text string) bool {
28+
regexpString := wildcardRegexpReplacer.Replace(pattern)
29+
30+
regexpObj := regexp.MustCompile(regexpString)
31+
32+
return regexpObj.MatchString(text)
33+
}

util/wildcard/wildcard_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package wildcard
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestMatch(t *testing.T) {
8+
actual := Match("*", "hoge")
9+
expected := true
10+
if actual != expected {
11+
t.Fatalf("expected %v, but %v\n", expected, actual)
12+
}
13+
}
14+
15+
func TestMatchSuffix(t *testing.T) {
16+
actual := Match("*oge", "hoge")
17+
expected := true
18+
if actual != expected {
19+
t.Fatalf("expected %v, but %v\n", expected, actual)
20+
}
21+
}
22+
23+
func TestMatchSuffixUnmatch(t *testing.T) {
24+
actual := Match("*age", "hoge")
25+
expected := false
26+
if actual != expected {
27+
t.Fatalf("expected %v, but %v\n", expected, actual)
28+
}
29+
}
30+
31+
func TestMatchPrefix(t *testing.T) {
32+
actual := Match("ho*", "hoge")
33+
expected := true
34+
if actual != expected {
35+
t.Fatalf("expected %v, but %v\n", expected, actual)
36+
}
37+
}
38+
39+
func TestMatchPrefixUnmatch(t *testing.T) {
40+
actual := Match("hu*", "hoge")
41+
expected := false
42+
if actual != expected {
43+
t.Fatalf("expected %v, but %v\n", expected, actual)
44+
}
45+
}
46+
47+
func TestMatchExact(t *testing.T) {
48+
actual := Match("hoge", "hoge")
49+
expected := true
50+
if actual != expected {
51+
t.Fatalf("expected %v, but %v\n", expected, actual)
52+
}
53+
}
54+
55+
func TestMatchExactUnmatch(t *testing.T) {
56+
actual := Match("huge", "hoge")
57+
expected := false
58+
if actual != expected {
59+
t.Fatalf("expected %v, but %v\n", expected, actual)
60+
}
61+
}

0 commit comments

Comments
 (0)