github.com/turingchain2020/turingchain@v1.1.21/common/db/db_test.go (about) 1 // Copyright Turing Corp. 2018 All Rights Reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package db 6 7 import ( 8 "encoding/hex" 9 "math/rand" 10 "testing" 11 "time" 12 13 "fmt" 14 15 "github.com/turingchain2020/turingchain/types" 16 "github.com/stretchr/testify/assert" 17 "github.com/stretchr/testify/require" 18 ) 19 20 var random = rand.New(rand.NewSource(time.Now().UnixNano())) 21 22 const ( 23 strChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" // 62 characters 24 ) 25 26 func RandInt() int { 27 return random.Int() 28 } 29 30 func RandStr(length int) string { 31 chars := []byte{} 32 MAIN_LOOP: 33 for { 34 val := random.Int63() 35 for i := 0; i < 10; i++ { 36 v := int(val & 0x3f) // rightmost 6 bits 37 if v >= 62 { // only 62 characters in strChars 38 val >>= 6 39 continue 40 } else { 41 chars = append(chars, strChars[v]) 42 if len(chars) == length { 43 break MAIN_LOOP 44 } 45 val >>= 6 46 } 47 } 48 } 49 50 return string(chars) 51 } 52 53 func testDBIteratorAllKey(t *testing.T, db DB) { 54 var datas = [][]byte{ 55 []byte("aa0"), []byte("aa1"), []byte("bb0"), []byte("bb1"), []byte("cc0"), []byte("cc1"), 56 } 57 for _, v := range datas { 58 db.Set(v, v) 59 } 60 //一次遍历 61 it := db.Iterator(nil, types.EmptyValue, false) 62 i := 0 63 for it.Rewind(); it.Valid(); it.Next() { 64 assert.Equal(t, it.Key(), datas[i]) 65 db.Delete(it.Key()) 66 i++ 67 if i == 2 { 68 break 69 } 70 } 71 it.Close() 72 //从第3个开始遍历 73 it = db.Iterator([]byte("aa1"), types.EmptyValue, false) 74 i = 2 75 for it.Rewind(); it.Valid(); it.Next() { 76 assert.Equal(t, it.Key(), datas[i]) 77 db.Delete(it.Key()) 78 i++ 79 if i == 4 { 80 break 81 } 82 } 83 it.Close() 84 //从第5个开始遍历 85 it = db.Iterator([]byte("bb1"), types.EmptyValue, false) 86 i = 4 87 for it.Rewind(); it.Valid(); it.Next() { 88 assert.Equal(t, it.Key(), datas[i]) 89 db.Delete(it.Key()) 90 i++ 91 if i == 6 { 92 break 93 } 94 } 95 it.Close() 96 } 97 98 func testDBIteratorReserverExample(t *testing.T, db DB) { 99 var datas = [][]byte{ 100 []byte("aa0"), []byte("aa1"), []byte("bb0"), []byte("bb1"), []byte("cc0"), []byte("cc1"), 101 } 102 for _, v := range datas { 103 db.Set(v, v) 104 } 105 // 从尾部到头一次遍历 106 it := db.Iterator(nil, types.EmptyValue, true) 107 i := 5 108 for it.Rewind(); it.Valid(); it.Next() { 109 assert.Equal(t, it.Key(), datas[i]) 110 //fmt.Println(i, string(it.Key())) 111 i-- 112 } 113 it.Close() 114 assert.Equal(t, i, -1) 115 116 // 从bb0开始从后到前遍历,end需要填入bb0的下一个,才可以遍历到bb0 117 it = db.Iterator(nil, []byte("bb1"), true) 118 i = 2 119 for it.Rewind(); it.Valid(); it.Next() { 120 assert.Equal(t, it.Key(), datas[i]) 121 //fmt.Println(i, string(it.Key())) 122 i-- 123 } 124 it.Close() 125 assert.Equal(t, i, -1) 126 127 // 反向前缀查找 128 it = db.Iterator([]byte("bb"), nil, true) 129 i = 3 130 for it.Rewind(); it.Valid(); it.Next() { 131 assert.Equal(t, it.Key(), datas[i]) 132 // fmt.Println(string(it.Key())) 133 i-- 134 } 135 it.Close() 136 assert.Equal(t, i, 1) 137 } 138 139 // 迭代测试 140 func testDBIterator(t *testing.T, db DB) { 141 t.Log("test Set") 142 db.Set([]byte("aaaaaa/1"), []byte("aaaaaa/1")) 143 db.Set([]byte("my_key/1"), []byte("my_key/1")) 144 db.Set([]byte("my_key/2"), []byte("my_key/2")) 145 db.Set([]byte("my_key/3"), []byte("my_key/3")) 146 db.Set([]byte("my_key/4"), []byte("my_key/4")) 147 db.Set([]byte("my"), []byte("my")) 148 db.Set([]byte("my_"), []byte("my_")) 149 db.Set([]byte("zzzzzz/1"), []byte("zzzzzz/1")) 150 b, err := hex.DecodeString("ff") 151 require.NoError(t, err) 152 db.Set(b, []byte("0xff")) 153 154 t.Log("test Get") 155 v, _ := db.Get([]byte("aaaaaa/1")) 156 require.Equal(t, string(v), "aaaaaa/1") 157 //test list: 158 it0 := NewListHelper(db) 159 list0 := it0.List(nil, nil, 100, 1) 160 for _, v = range list0 { 161 t.Log("list0", string(v)) 162 } 163 t.Log("test PrefixScan") 164 it := NewListHelper(db) 165 list := it.PrefixScan(nil) 166 for _, v = range list { 167 t.Log("list:", string(v)) 168 } 169 assert.Equal(t, list0, list) 170 require.Equal(t, list, [][]byte{[]byte("aaaaaa/1"), []byte("my"), []byte("my_"), []byte("my_key/1"), []byte("my_key/2"), []byte("my_key/3"), []byte("my_key/4"), []byte("zzzzzz/1"), []byte("0xff")}) 171 t.Log("test IteratorScanFromFirst") 172 list = it.IteratorScanFromFirst([]byte("my"), 2, ListASC) 173 /*for _, v = range list { 174 t.Log(string(v)) 175 }*/ 176 require.Equal(t, list, [][]byte{[]byte("my"), []byte("my_")}) 177 178 t.Log("test IteratorScanFromLast") 179 list = it.IteratorScanFromLast([]byte("my"), 100, ListDESC) 180 /*for _, v = range list { 181 t.Log(string(v)) 182 }*/ 183 require.Equal(t, list, [][]byte{[]byte("my_key/4"), []byte("my_key/3"), []byte("my_key/2"), []byte("my_key/1"), []byte("my_"), []byte("my")}) 184 185 t.Log("test IteratorScan 1") 186 list = it.IteratorScan([]byte("my"), []byte("my_key/3"), 100, ListASC) 187 /*for _, v = range list { 188 t.Log(string(v)) 189 }*/ 190 require.Equal(t, list, [][]byte{[]byte("my_key/4")}) 191 192 t.Log("test IteratorScan 0") 193 list = it.IteratorScan([]byte("my"), []byte("my_key/3"), 100, ListDESC) 194 /*for _, v = range list { 195 t.Log(string(v)) 196 }*/ 197 require.Equal(t, list, [][]byte{[]byte("my_key/2"), []byte("my_key/1"), []byte("my_"), []byte("my")}) 198 } 199 200 func testDBBoundary(t *testing.T, db DB) { 201 a, _ := hex.DecodeString("0f") 202 c, _ := hex.DecodeString("0fff") 203 b, _ := hex.DecodeString("ff") 204 d, _ := hex.DecodeString("ffff") 205 db.Set(a, []byte("0x0f")) 206 db.Set(c, []byte("0x0fff")) 207 db.Set(b, []byte("0xff")) 208 db.Set(d, []byte("0xffff")) 209 210 var v []byte 211 _ = v 212 it := NewListHelper(db) 213 214 // f为prefix 215 t.Log("PrefixScan") 216 list := it.PrefixScan(a) 217 require.Equal(t, list, [][]byte{[]byte("0x0f"), []byte("0x0fff")}) 218 219 t.Log("IteratorScanFromFirst") 220 list = it.IteratorScanFromFirst(a, 2, ListASC) 221 require.Equal(t, list, [][]byte{[]byte("0x0f"), []byte("0x0fff")}) 222 223 t.Log("IteratorScanFromLast") 224 list = it.IteratorScanFromLast(a, 100, ListDESC) 225 require.Equal(t, list, [][]byte{[]byte("0x0fff"), []byte("0x0f")}) 226 227 t.Log("IteratorScan 1") 228 list = it.IteratorScan(a, a, 100, ListASC) 229 require.Equal(t, list, [][]byte{[]byte("0x0fff")}) 230 231 t.Log("IteratorScan 0") 232 list = it.IteratorScan(a, a, 100, ListDESC) 233 require.Equal(t, list, [][]byte(nil)) 234 235 // ff为prefix 236 t.Log("PrefixScan") 237 list = it.PrefixScan(b) 238 require.Equal(t, list, [][]byte{[]byte("0xff"), []byte("0xffff")}) 239 240 t.Log("IteratorScanFromFirst") 241 list = it.IteratorScanFromFirst(b, 2, ListASC) 242 require.Equal(t, list, [][]byte{[]byte("0xff"), []byte("0xffff")}) 243 244 t.Log("IteratorScanFromLast") 245 list = it.IteratorScanFromLast(b, 100, ListDESC) 246 require.Equal(t, list, [][]byte{[]byte("0xffff"), []byte("0xff")}) 247 248 t.Log("IteratorScan 1") 249 list = it.IteratorScan(b, b, 100, ListASC) 250 require.Equal(t, list, [][]byte{[]byte("0xffff")}) 251 252 t.Log("IteratorScan 0") 253 list = it.IteratorScan(b, d, 100, ListDESC) 254 require.Equal(t, list, [][]byte{[]byte("0xff")}) 255 } 256 257 func testDBIteratorDel(t *testing.T, db DB) { 258 for i := 0; i < 1000; i++ { 259 k := []byte(fmt.Sprintf("my_key/%010d", i)) 260 v := []byte(fmt.Sprintf("my_value/%010d", i)) 261 db.Set(k, v) 262 } 263 264 prefix := []byte("my") 265 it := db.Iterator(prefix, nil, true) 266 defer it.Close() 267 for it.Rewind(); it.Valid(); it.Next() { 268 t.Log(string(it.Key()), "*********", string(it.Value())) 269 batch := db.NewBatch(true) 270 batch.Delete(it.Key()) 271 batch.Write() 272 } 273 } 274 275 func testBatch(t *testing.T, db DB) { 276 batch := db.NewBatch(false) 277 batch.Set([]byte("hello"), []byte("world")) 278 err := batch.Write() 279 assert.Nil(t, err) 280 281 batch = db.NewBatch(false) 282 v, err := db.Get([]byte("hello")) 283 assert.Nil(t, err) 284 assert.Equal(t, v, []byte("world")) 285 286 //set and del 287 batch.Set([]byte("hello1"), []byte("world")) 288 batch.Set([]byte("hello2"), []byte("world")) 289 batch.Set([]byte("hello3"), []byte("world")) 290 batch.Set([]byte("hello4"), []byte("world")) 291 batch.Set([]byte("hello5"), []byte("world")) 292 batch.Delete([]byte("hello1")) 293 err = batch.Write() 294 assert.Nil(t, err) 295 v, err = db.Get([]byte("hello1")) 296 assert.Equal(t, err, types.ErrNotFound) 297 assert.Nil(t, v) 298 } 299 300 func testTransaction(t *testing.T, db DB) { 301 tx, err := db.BeginTx() 302 assert.Nil(t, err) 303 tx.Set([]byte("hello1"), []byte("world1")) 304 value, err := tx.Get([]byte("hello1")) 305 assert.Nil(t, err) 306 assert.Equal(t, "world1", string(value)) 307 tx.Rollback() 308 value, err = db.Get([]byte("hello1")) 309 assert.Equal(t, types.ErrNotFound, err) 310 assert.Equal(t, []byte(nil), value) 311 312 tx, err = db.BeginTx() 313 assert.Nil(t, err) 314 tx.Set([]byte("hello2"), []byte("world2")) 315 value, err = tx.Get([]byte("hello2")) 316 assert.Nil(t, err) 317 assert.Equal(t, "world2", string(value)) 318 err = tx.Commit() 319 assert.Nil(t, err) 320 value, err = db.Get([]byte("hello2")) 321 assert.Nil(t, err) 322 assert.Equal(t, "world2", string(value)) 323 } 324 325 // 返回值测试 326 func testDBIteratorResult(t *testing.T, db DB) { 327 t.Log("test Set") 328 db.Set([]byte("aaaaaa/1"), []byte("aaaaaa/1")) 329 db.Set([]byte("my_key/1"), []byte("my_value/1")) 330 db.Set([]byte("my_key/2"), []byte("my_value/2")) 331 db.Set([]byte("my_key/3"), []byte("my_value/3")) 332 db.Set([]byte("my_key/4"), []byte("my_value/4")) 333 db.Set([]byte("my"), []byte("my")) 334 db.Set([]byte("my_"), []byte("my_")) 335 db.Set([]byte("zzzzzz/1"), []byte("zzzzzz/1")) 336 b, err := hex.DecodeString("ff") 337 require.NoError(t, err) 338 db.Set(b, []byte("0xff")) 339 340 t.Log("test Get") 341 v, _ := db.Get([]byte("aaaaaa/1")) 342 require.Equal(t, string(v), "aaaaaa/1") 343 //test list: 344 it0 := NewListHelper(db) 345 list0 := it0.List([]byte("my_key"), nil, 100, ListASC|ListKeyOnly) 346 require.Equal(t, list0, [][]byte{[]byte("my_key/1"), []byte("my_key/2"), []byte("my_key/3"), []byte("my_key/4")}) 347 348 it1 := NewListHelper(db) 349 list1 := it1.List([]byte("my_key"), nil, 100, ListASC) 350 require.Equal(t, list1, [][]byte{[]byte("my_value/1"), []byte("my_value/2"), []byte("my_value/3"), []byte("my_value/4")}) 351 352 it2 := NewListHelper(db) 353 list2 := it2.List([]byte("my_key"), nil, 100, ListASC|ListWithKey) 354 require.Equal(t, 4, len(list2)) 355 for i, v := range list2 { 356 var kv types.KeyValue 357 err = types.Decode(v, &kv) 358 require.Equal(t, nil, err) 359 require.Equal(t, fmt.Sprintf("my_key/%d", i+1), string(kv.Key)) 360 require.Equal(t, fmt.Sprintf("my_value/%d", i+1), string(kv.Value)) 361 } 362 }