github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/pingcap/tidb/kv/union_store_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 kv 15 16 import ( 17 "github.com/insionng/yougam/libraries/juju/errors" 18 . "github.com/insionng/yougam/libraries/pingcap/check" 19 "github.com/insionng/yougam/libraries/pingcap/tidb/terror" 20 "github.com/insionng/yougam/libraries/pingcap/tidb/util/testleak" 21 ) 22 23 var _ = Suite(&testUnionStoreSuite{}) 24 25 type testUnionStoreSuite struct { 26 store MemBuffer 27 us UnionStore 28 } 29 30 func (s *testUnionStoreSuite) SetUpTest(c *C) { 31 s.store = NewMemDbBuffer() 32 s.us = NewUnionStore(&mockSnapshot{s.store}) 33 } 34 35 func (s *testUnionStoreSuite) TearDownTest(c *C) { 36 s.us.Release() 37 } 38 39 func (s *testUnionStoreSuite) TestGetSet(c *C) { 40 defer testleak.AfterTest(c)() 41 s.store.Set([]byte("1"), []byte("1")) 42 v, err := s.us.Get([]byte("1")) 43 c.Assert(err, IsNil) 44 c.Assert(v, BytesEquals, []byte("1")) 45 s.us.Set([]byte("1"), []byte("2")) 46 v, err = s.us.Get([]byte("1")) 47 c.Assert(err, IsNil) 48 c.Assert(v, BytesEquals, []byte("2")) 49 } 50 51 func (s *testUnionStoreSuite) TestDelete(c *C) { 52 defer testleak.AfterTest(c)() 53 s.store.Set([]byte("1"), []byte("1")) 54 err := s.us.Delete([]byte("1")) 55 c.Assert(err, IsNil) 56 _, err = s.us.Get([]byte("1")) 57 c.Assert(IsErrNotFound(err), IsTrue) 58 59 s.us.Set([]byte("1"), []byte("2")) 60 v, err := s.us.Get([]byte("1")) 61 c.Assert(err, IsNil) 62 c.Assert(v, BytesEquals, []byte("2")) 63 } 64 65 func (s *testUnionStoreSuite) TestSeek(c *C) { 66 defer testleak.AfterTest(c)() 67 s.store.Set([]byte("1"), []byte("1")) 68 s.store.Set([]byte("2"), []byte("2")) 69 s.store.Set([]byte("3"), []byte("3")) 70 71 iter, err := s.us.Seek(nil) 72 c.Assert(err, IsNil) 73 checkIterator(c, iter, [][]byte{[]byte("1"), []byte("2"), []byte("3")}, [][]byte{[]byte("1"), []byte("2"), []byte("3")}) 74 75 iter, err = s.us.Seek([]byte("2")) 76 c.Assert(err, IsNil) 77 checkIterator(c, iter, [][]byte{[]byte("2"), []byte("3")}, [][]byte{[]byte("2"), []byte("3")}) 78 79 s.us.Set([]byte("4"), []byte("4")) 80 iter, err = s.us.Seek([]byte("2")) 81 c.Assert(err, IsNil) 82 checkIterator(c, iter, [][]byte{[]byte("2"), []byte("3"), []byte("4")}, [][]byte{[]byte("2"), []byte("3"), []byte("4")}) 83 84 s.us.Delete([]byte("3")) 85 iter, err = s.us.Seek([]byte("2")) 86 c.Assert(err, IsNil) 87 checkIterator(c, iter, [][]byte{[]byte("2"), []byte("4")}, [][]byte{[]byte("2"), []byte("4")}) 88 } 89 90 func (s *testUnionStoreSuite) TestSeekReverse(c *C) { 91 defer testleak.AfterTest(c)() 92 s.store.Set([]byte("1"), []byte("1")) 93 s.store.Set([]byte("2"), []byte("2")) 94 s.store.Set([]byte("3"), []byte("3")) 95 96 iter, err := s.us.SeekReverse(nil) 97 c.Assert(err, IsNil) 98 checkIterator(c, iter, [][]byte{[]byte("3"), []byte("2"), []byte("1")}, [][]byte{[]byte("3"), []byte("2"), []byte("1")}) 99 100 iter, err = s.us.SeekReverse([]byte("3")) 101 c.Assert(err, IsNil) 102 checkIterator(c, iter, [][]byte{[]byte("2"), []byte("1")}, [][]byte{[]byte("2"), []byte("1")}) 103 104 s.us.Set([]byte("0"), []byte("0")) 105 iter, err = s.us.SeekReverse([]byte("3")) 106 c.Assert(err, IsNil) 107 checkIterator(c, iter, [][]byte{[]byte("2"), []byte("1"), []byte("0")}, [][]byte{[]byte("2"), []byte("1"), []byte("0")}) 108 109 s.us.Delete([]byte("1")) 110 iter, err = s.us.SeekReverse([]byte("3")) 111 c.Assert(err, IsNil) 112 checkIterator(c, iter, [][]byte{[]byte("2"), []byte("0")}, [][]byte{[]byte("2"), []byte("0")}) 113 } 114 115 func (s *testUnionStoreSuite) TestLazyConditionCheck(c *C) { 116 defer testleak.AfterTest(c)() 117 s.store.Set([]byte("1"), []byte("1")) 118 s.store.Set([]byte("2"), []byte("2")) 119 120 v, err := s.us.Get([]byte("1")) 121 c.Assert(err, IsNil) 122 c.Assert(v, BytesEquals, []byte("1")) 123 124 s.us.SetOption(PresumeKeyNotExists, nil) 125 s.us.SetOption(PresumeKeyNotExistsError, ErrNotExist) 126 _, err = s.us.Get([]byte("2")) 127 c.Assert(terror.ErrorEqual(err, ErrNotExist), IsTrue) 128 129 err = s.us.CheckLazyConditionPairs() 130 c.Assert(err, NotNil) 131 } 132 133 func checkIterator(c *C, iter Iterator, keys [][]byte, values [][]byte) { 134 defer iter.Close() 135 c.Assert(len(keys), Equals, len(values)) 136 for i, k := range keys { 137 v := values[i] 138 c.Assert(iter.Valid(), IsTrue) 139 c.Assert([]byte(iter.Key()), BytesEquals, k) 140 c.Assert(iter.Value(), BytesEquals, v) 141 c.Assert(iter.Next(), IsNil) 142 } 143 c.Assert(iter.Valid(), IsFalse) 144 } 145 146 type mockSnapshot struct { 147 store MemBuffer 148 } 149 150 func (s *mockSnapshot) Get(k Key) ([]byte, error) { 151 return s.store.Get(k) 152 } 153 154 func (s *mockSnapshot) BatchGet(keys []Key) (map[string][]byte, error) { 155 m := make(map[string][]byte) 156 for _, k := range keys { 157 v, err := s.store.Get(k) 158 if IsErrNotFound(err) { 159 continue 160 } 161 if err != nil { 162 return nil, errors.Trace(err) 163 } 164 m[string(k)] = v 165 } 166 return m, nil 167 } 168 169 func (s *mockSnapshot) Seek(k Key) (Iterator, error) { 170 return s.store.Seek(k) 171 } 172 173 func (s *mockSnapshot) SeekReverse(k Key) (Iterator, error) { 174 return s.store.SeekReverse(k) 175 } 176 177 func (s *mockSnapshot) Release() { 178 s.store.Release() 179 }