github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/util/prefix_helper_test.go (about) 1 // Copyright 2015 PingCAP, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package util 15 16 import ( 17 "fmt" 18 "testing" 19 20 . "github.com/insionng/yougam/libraries/pingcap/check" 21 "github.com/insionng/yougam/libraries/pingcap/tidb/kv" 22 "github.com/insionng/yougam/libraries/pingcap/tidb/store/localstore" 23 "github.com/insionng/yougam/libraries/pingcap/tidb/store/localstore/goleveldb" 24 "github.com/insionng/yougam/libraries/pingcap/tidb/util/testleak" 25 ) 26 27 const ( 28 startIndex = 0 29 testCount = 12 30 testPow = 10 31 ) 32 33 func TestT(t *testing.T) { 34 TestingT(t) 35 } 36 37 var _ = Suite(&testPrefixSuite{}) 38 39 type testPrefixSuite struct { 40 s kv.Storage 41 } 42 43 func (s *testPrefixSuite) SetUpSuite(c *C) { 44 path := "memory:" 45 d := localstore.Driver{ 46 Driver: goleveldb.MemoryDriver{}, 47 } 48 store, err := d.Open(path) 49 c.Assert(err, IsNil) 50 s.s = store 51 52 // must in cache 53 cacheS, _ := d.Open(path) 54 c.Assert(cacheS, Equals, store) 55 } 56 57 func (s *testPrefixSuite) TearDownSuite(c *C) { 58 err := s.s.Close() 59 c.Assert(err, IsNil) 60 } 61 62 func encodeInt(n int) []byte { 63 return []byte(fmt.Sprintf("%d", n)) 64 } 65 66 type MockContext struct { 67 prefix int 68 values map[fmt.Stringer]interface{} 69 kv.Storage 70 txn kv.Transaction 71 } 72 73 func (c *MockContext) SetValue(key fmt.Stringer, value interface{}) { 74 c.values[key] = value 75 } 76 77 func (c *MockContext) Value(key fmt.Stringer) interface{} { 78 value := c.values[key] 79 return value 80 } 81 82 func (c *MockContext) ClearValue(key fmt.Stringer) { 83 delete(c.values, key) 84 } 85 86 func (c *MockContext) GetTxn(forceNew bool) (kv.Transaction, error) { 87 var err error 88 c.txn, err = c.Begin() 89 if err != nil { 90 return nil, err 91 } 92 93 return c.txn, nil 94 } 95 96 func (c *MockContext) fillTxn() error { 97 if c.txn == nil { 98 return nil 99 } 100 101 var err error 102 for i := startIndex; i < testCount; i++ { 103 val := encodeInt(i + (c.prefix * testPow)) 104 err = c.txn.Set(val, val) 105 if err != nil { 106 return err 107 } 108 } 109 110 return nil 111 } 112 113 func (c *MockContext) FinishTxn(rollback bool) error { 114 if c.txn == nil { 115 return nil 116 } 117 118 return c.txn.Commit() 119 } 120 121 func (s *testPrefixSuite) TestPrefix(c *C) { 122 defer testleak.AfterTest(c)() 123 ctx := &MockContext{10000000, make(map[fmt.Stringer]interface{}), s.s, nil} 124 ctx.fillTxn() 125 txn, err := ctx.GetTxn(false) 126 c.Assert(err, IsNil) 127 err = DelKeyWithPrefix(txn, encodeInt(ctx.prefix)) 128 c.Assert(err, IsNil) 129 err = ctx.FinishTxn(false) 130 c.Assert(err, IsNil) 131 132 txn, err = s.s.Begin() 133 c.Assert(err, IsNil) 134 k := []byte("key100jfowi878230") 135 err = txn.Set(k, []byte("val32dfaskli384757^*&%^")) 136 c.Assert(err, IsNil) 137 err = ScanMetaWithPrefix(txn, k, func(kv.Key, []byte) bool { 138 return true 139 }) 140 c.Assert(err, IsNil) 141 err = txn.Commit() 142 c.Assert(err, IsNil) 143 } 144 145 func (s *testPrefixSuite) TestPrefixFilter(c *C) { 146 defer testleak.AfterTest(c)() 147 rowKey := []byte("test@#$%l(le[0]..prefix) 2uio") 148 rowKey[8] = 0x00 149 rowKey[9] = 0x00 150 f := RowKeyPrefixFilter(rowKey) 151 b := f(append(rowKey, []byte("akjdf3*(34")...)) 152 c.Assert(b, IsFalse) 153 buf := f([]byte("sjfkdlsaf")) 154 c.Assert(buf, IsTrue) 155 }