Skip to content

Commit

Permalink
fixed keys bug
Browse files Browse the repository at this point in the history
  • Loading branch information
zhs committed Nov 8, 2018
1 parent e96afe6 commit e5bfc68
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 51 deletions.
141 changes: 94 additions & 47 deletions command/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,65 +2,112 @@ package command

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestGlobMatch(t *testing.T) {
if !globMatch([]byte("hello"), []byte("h?llo"), false) {
t.Fatal()
}
if !globMatch([]byte("hello"), []byte("h??lo"), false) {
t.Fatal()
}
if globMatch([]byte("healo"), []byte("h?llo"), false) {
t.Fatal()
func matchPrefixCase() map[string]string {
cs := map[string]string{
"abc?[a-z]": "abc",
"?abc": "",
"\\*[^abc]?": "*",
}
return cs
}

if !globMatch([]byte("hello"), []byte("h*o"), false) {
t.Fatal()
}
if !globMatch([]byte("ho"), []byte("h*o"), false) {
t.Fatal()
}
if !globMatch([]byte("lo"), []byte("*lo"), false) {
t.Fatal()
}
if !globMatch([]byte("hellabcdlo"), []byte("h*lo"), false) {
t.Fatal()
}
if globMatch([]byte("hellabcdlao"), []byte("h*lo"), false) {
t.Fatal()
}
if !globMatch([]byte("hello"), []byte("**"), false) {
t.Fatal()
}
if !globMatch([]byte("hello"), []byte("*?"), false) {
t.Fatal()
}
type patternMap map[string]bool

if !globMatch([]byte("hello"), []byte("h[edf]llo"), false) {
t.Fatal()
}
if !globMatch([]byte("hdllo"), []byte("h[edf]llo"), false) {
t.Fatal()
}
if !globMatch([]byte("hfllo"), []byte("h[edf]llo"), false) {
t.Fatal()
}
if globMatch([]byte("hallo"), []byte("h[edf]llo"), false) {
t.Fatal()
func matchCase(nocase bool) map[string]*patternMap {
var cs map[string]*patternMap
if !nocase {
cs = map[string]*patternMap{
"*": &patternMap{
"": true,
"abcd": true,
"*[*]": true,
},
"******a": &patternMap{
"a": true,
"***a": true,
"bcdea": true,
"abcd": false,
},
"\\*?aaa": &patternMap{
"*caaa": true,
"abc": false,
},
"[a-z][^0-9][z-a]?[a-z": &patternMap{
"abz.a": true,
"a1z.*": false,
"abz.e": true,
},
"[a-z]*cat*[h][^b]*eyes*": &patternMap{
"my cat has very bright eyes": true,
"my dog has very bright eyes": false,
},
"h?llo": &patternMap{
"hello": true,
"healo": false,
},
"h??lo": &patternMap{
"hello": true,
},
"h*o": &patternMap{
"hello": true,
"ho": true,
},
}

} else {
cs = map[string]*patternMap{
"[A-Z][0-9]*": &patternMap{
"B1": true,
"B2000": true,
"b2000": false,
},
"*A": &patternMap{
"abcdA": true,
"abcda": false,
"Ae": false,
},
"?A*C": &patternMap{
"1AbcdC": true,
"cA12344C": true,
"1abcdc": false,
},
}
}

if !globMatch([]byte("hallo"), []byte("h[^edf]llo"), false) {
t.Fatal()
return cs
}

func TestGlobMatchPrefix(t *testing.T) {
list := matchPrefixCase()
for match, exptected := range list {
val := globMatchPrefix([]byte(match))
assert.Equal(t, exptected, string(val))
}
if globMatch([]byte("hello"), []byte("h[^edf]llo"), false) {
t.Fatal()
}

func TestPatternMatch(t *testing.T) {
cs := matchCase(false)
for pattern, vals := range cs {
for val, expected := range map[string]bool(*vals) {
actual := globMatch([]byte(pattern), []byte(val), false)
assert.Equal(t, expected, actual, "err log:", pattern, val)
}
}

if globMatch([]byte("hello"), []byte("h"), false) {
t.Fatal()
// check upper string
cs = matchCase(true)
for pattern, vals := range cs {
for val, expected := range map[string]bool(*vals) {
actual := globMatch([]byte(pattern), []byte(val), true)
assert.Equal(t, expected, actual, "err log:", pattern, val)
}
}
}

func BenchmarkGlobMatch(b *testing.B) {
for i := 0; i < b.N; i++ {
globMatch([]byte("hellabcdlo"), []byte("h*lo"), false)
Expand Down
5 changes: 1 addition & 4 deletions command/keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func ExpireAt(ctx *Context, txn *db.Transaction) (OnCommit, error) {

at := int64(time.Second * time.Duration(timestamp))
if at <= 0 {
at = db.Now()
at = db.Now() - 1
}

if err := kv.ExpireAt(key, at); err != nil {
Expand Down Expand Up @@ -149,9 +149,6 @@ func TTL(ctx *Context, txn *db.Transaction) (OnCommit, error) {
}
return nil, err
}
if db.IsExpired(obj, now) {
return Integer(ctx.Out, -2), nil
}
if obj.ExpireAt == 0 {
return Integer(ctx.Out, -1), nil
}
Expand Down
6 changes: 6 additions & 0 deletions command/test_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package command
import (
"bytes"
"io"
"strings"

"gitlab.meitu.com/platform/thanos/conf"
"gitlab.meitu.com/platform/thanos/context"
Expand Down Expand Up @@ -40,3 +41,8 @@ func ContextTest(name string, args ...string) *Context {
func ctxString(buf io.Writer) string {
return buf.(*bytes.Buffer).String()
}

func ctxLines(buf io.Writer) []string {
str := ctxString(buf)
return strings.Split(str, "\r\n")
}

0 comments on commit e5bfc68

Please sign in to comment.