github.com/zuoyebang/bitalosdb@v1.1.1-0.20240516111551-79a8c4d8ce20/internal/arenaskl/skl_test.go (about) 1 // Copyright 2021 The Bitalosdb author(hustxrb@163.com) and other contributors. 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 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package arenaskl 16 17 import ( 18 "bytes" 19 "encoding/binary" 20 "fmt" 21 "strconv" 22 "sync" 23 "sync/atomic" 24 "testing" 25 "time" 26 27 "github.com/zuoyebang/bitalosdb/internal/base" 28 29 "github.com/stretchr/testify/require" 30 "golang.org/x/exp/rand" 31 ) 32 33 const arenaSize = 1 << 20 34 35 type iterAdapter struct { 36 *Iterator 37 key *base.InternalKey 38 val []byte 39 } 40 41 func newIterAdapter(iter *Iterator) *iterAdapter { 42 return &iterAdapter{ 43 Iterator: iter, 44 } 45 } 46 47 func (i *iterAdapter) update(key *base.InternalKey, val []byte) bool { 48 i.key = key 49 i.val = val 50 return i.key != nil 51 } 52 53 func (i *iterAdapter) String() string { 54 return "iter-adapter" 55 } 56 57 func (i *iterAdapter) SeekGE(key []byte) bool { 58 return i.update(i.Iterator.SeekGE(key)) 59 } 60 61 func (i *iterAdapter) SeekPrefixGE(prefix, key []byte, trySeekUsingNext bool) bool { 62 return i.update(i.Iterator.SeekPrefixGE(prefix, key, trySeekUsingNext)) 63 } 64 65 func (i *iterAdapter) SeekLT(key []byte) bool { 66 return i.update(i.Iterator.SeekLT(key)) 67 } 68 69 func (i *iterAdapter) First() bool { 70 return i.update(i.Iterator.First()) 71 } 72 73 func (i *iterAdapter) Last() bool { 74 return i.update(i.Iterator.Last()) 75 } 76 77 func (i *iterAdapter) Next() bool { 78 return i.update(i.Iterator.Next()) 79 } 80 81 func (i *iterAdapter) Prev() bool { 82 return i.update(i.Iterator.Prev()) 83 } 84 85 func (i *iterAdapter) Key() base.InternalKey { 86 return *i.key 87 } 88 89 func (i *iterAdapter) Value() []byte { 90 return i.val 91 } 92 93 func (i *iterAdapter) Valid() bool { 94 return i.key != nil 95 } 96 97 func makeIntKey(i int) base.InternalKey { 98 return base.InternalKey{UserKey: []byte(fmt.Sprintf("%05d", i))} 99 } 100 101 func makeKey(s string) []byte { 102 return []byte(s) 103 } 104 105 func makeIkey(s string) base.InternalKey { 106 return base.InternalKey{UserKey: []byte(s)} 107 } 108 109 func makeValue(i int) []byte { 110 return []byte(fmt.Sprintf("v%05d", i)) 111 } 112 113 func makeInserterAdd(s *Skiplist) func(key base.InternalKey, value []byte) error { 114 ins := &Inserter{} 115 return func(key base.InternalKey, value []byte) error { 116 return ins.Add(s, key, value) 117 } 118 } 119 120 // length iterates over skiplist to give exact size. 121 func length(s *Skiplist) int { 122 count := 0 123 124 it := newIterAdapter(s.NewIter(nil, nil)) 125 for valid := it.First(); valid; valid = it.Next() { 126 count++ 127 } 128 129 return count 130 } 131 132 // length iterates over skiplist in reverse order to give exact size. 133 func lengthRev(s *Skiplist) int { 134 count := 0 135 136 it := newIterAdapter(s.NewIter(nil, nil)) 137 for valid := it.Last(); valid; valid = it.Prev() { 138 count++ 139 } 140 141 return count 142 } 143 144 func TestEmpty(t *testing.T) { 145 key := makeKey("aaa") 146 l := NewSkiplist(newArena(arenaSize), bytes.Compare) 147 it := newIterAdapter(l.NewIter(nil, nil)) 148 149 require.False(t, it.Valid()) 150 151 it.First() 152 require.False(t, it.Valid()) 153 154 it.Last() 155 require.False(t, it.Valid()) 156 157 require.False(t, it.SeekGE(key)) 158 require.False(t, it.Valid()) 159 } 160 161 func TestFull(t *testing.T) { 162 l := NewSkiplist(newArena(1000), bytes.Compare) 163 164 foundArenaFull := false 165 for i := 0; i < 100; i++ { 166 err := l.Add(makeIntKey(i), makeValue(i)) 167 if err == ErrArenaFull { 168 foundArenaFull = true 169 break 170 } 171 } 172 173 require.True(t, foundArenaFull) 174 175 err := l.Add(makeIkey("someval"), nil) 176 require.Equal(t, ErrArenaFull, err) 177 } 178 179 func TestSeqnum(t *testing.T) { 180 l := NewSkiplist(newArena(arenaSize), bytes.Compare) 181 it := newIterAdapter(l.NewIter(nil, nil)) 182 183 add := makeInserterAdd(l) 184 185 add(base.MakeInternalKey([]byte("key1"), 1, base.InternalKeyKindSet), makeValue(1)) 186 add(base.MakeInternalKey([]byte("key12"), 3, base.InternalKeyKindSet), makeValue(123)) 187 add(base.MakeInternalKey([]byte("key1"), 2, base.InternalKeyKindSet), makeValue(2)) 188 add(base.MakeInternalKey([]byte("key12"), 2, base.InternalKeyKindSet), makeValue(122)) 189 add(base.MakeInternalKey([]byte("key1"), 3, base.InternalKeyKindSet), makeValue(3)) 190 add(base.MakeInternalKey([]byte("key12"), 1, base.InternalKeyKindSet), makeValue(121)) 191 192 for it.First(); it.Valid(); it.Next() { 193 fmt.Println(it.Key().String(), string(it.Value())) 194 } 195 } 196 197 func TestBasic(t *testing.T) { 198 for _, inserter := range []bool{false, true} { 199 t.Run(fmt.Sprintf("inserter=%t", inserter), func(t *testing.T) { 200 l := NewSkiplist(newArena(arenaSize), bytes.Compare) 201 it := newIterAdapter(l.NewIter(nil, nil)) 202 203 add := l.Add 204 if inserter { 205 add = makeInserterAdd(l) 206 } 207 208 add(makeIkey("key1"), makeValue(1)) 209 add(makeIkey("key3"), makeValue(3)) 210 add(makeIkey("key2"), makeValue(2)) 211 212 require.True(t, it.SeekGE(makeKey("key"))) 213 require.True(t, it.Valid()) 214 require.NotEqual(t, "key", it.Key().UserKey) 215 216 require.True(t, it.SeekGE(makeKey("key1"))) 217 require.EqualValues(t, "key1", it.Key().UserKey) 218 require.EqualValues(t, makeValue(1), it.Value()) 219 220 require.True(t, it.SeekGE(makeKey("key2"))) 221 require.EqualValues(t, "key2", it.Key().UserKey) 222 require.EqualValues(t, makeValue(2), it.Value()) 223 224 require.True(t, it.SeekGE(makeKey("key3"))) 225 require.EqualValues(t, "key3", it.Key().UserKey) 226 require.EqualValues(t, makeValue(3), it.Value()) 227 228 key := makeIkey("a") 229 key.SetSeqNum(1) 230 add(key, nil) 231 key.SetSeqNum(2) 232 add(key, nil) 233 234 require.True(t, it.SeekGE(makeKey("a"))) 235 require.True(t, it.Valid()) 236 require.EqualValues(t, "a", it.Key().UserKey) 237 require.EqualValues(t, 2, it.Key().SeqNum()) 238 239 require.True(t, it.Next()) 240 require.True(t, it.Valid()) 241 require.EqualValues(t, "a", it.Key().UserKey) 242 require.EqualValues(t, 1, it.Key().SeqNum()) 243 244 key = makeIkey("b") 245 key.SetSeqNum(2) 246 add(key, nil) 247 key.SetSeqNum(1) 248 add(key, nil) 249 250 require.True(t, it.SeekGE(makeKey("b"))) 251 require.True(t, it.Valid()) 252 require.EqualValues(t, "b", it.Key().UserKey) 253 require.EqualValues(t, 2, it.Key().SeqNum()) 254 255 require.True(t, it.Next()) 256 require.True(t, it.Valid()) 257 require.EqualValues(t, "b", it.Key().UserKey) 258 require.EqualValues(t, 1, it.Key().SeqNum()) 259 }) 260 } 261 } 262 263 func TestConcurrentBasic(t *testing.T) { 264 const n = 1000 265 266 for _, inserter := range []bool{false, true} { 267 t.Run(fmt.Sprintf("inserter=%t", inserter), func(t *testing.T) { 268 l := NewSkiplist(newArena(arenaSize), bytes.Compare) 269 l.testing = true 270 271 var wg sync.WaitGroup 272 for i := 0; i < n; i++ { 273 wg.Add(1) 274 go func(i int) { 275 defer wg.Done() 276 277 if inserter { 278 var ins Inserter 279 ins.Add(l, makeIntKey(i), makeValue(i)) 280 } else { 281 l.Add(makeIntKey(i), makeValue(i)) 282 } 283 }(i) 284 } 285 wg.Wait() 286 287 for i := 0; i < n; i++ { 288 wg.Add(1) 289 go func(i int) { 290 defer wg.Done() 291 292 it := newIterAdapter(l.NewIter(nil, nil)) 293 require.True(t, it.SeekGE(makeKey(fmt.Sprintf("%05d", i)))) 294 require.EqualValues(t, fmt.Sprintf("%05d", i), it.Key().UserKey) 295 }(i) 296 } 297 wg.Wait() 298 require.Equal(t, n, length(l)) 299 require.Equal(t, n, lengthRev(l)) 300 }) 301 } 302 } 303 304 func TestConcurrentOneKey(t *testing.T) { 305 const n = 100 306 key := makeKey("thekey") 307 ikey := makeIkey("thekey") 308 309 for _, inserter := range []bool{false, true} { 310 t.Run(fmt.Sprintf("inserter=%t", inserter), func(t *testing.T) { 311 l := NewSkiplist(newArena(arenaSize), bytes.Compare) 312 l.testing = true 313 314 var wg sync.WaitGroup 315 writeDone := make(chan struct{}, 1) 316 for i := 0; i < n; i++ { 317 wg.Add(1) 318 go func(i int) { 319 defer func() { 320 wg.Done() 321 select { 322 case writeDone <- struct{}{}: 323 default: 324 } 325 }() 326 327 if inserter { 328 var ins Inserter 329 ins.Add(l, ikey, makeValue(i)) 330 } else { 331 l.Add(ikey, makeValue(i)) 332 } 333 }(i) 334 } 335 <-writeDone 336 var sawValue int32 337 for i := 0; i < n; i++ { 338 wg.Add(1) 339 go func() { 340 defer wg.Done() 341 342 it := newIterAdapter(l.NewIter(nil, nil)) 343 it.SeekGE(key) 344 require.True(t, it.Valid()) 345 require.True(t, bytes.Equal(key, it.Key().UserKey)) 346 347 atomic.AddInt32(&sawValue, 1) 348 v, err := strconv.Atoi(string(it.Value()[1:])) 349 require.NoError(t, err) 350 require.True(t, 0 <= v && v < n) 351 }() 352 } 353 wg.Wait() 354 require.Equal(t, int32(n), sawValue) 355 require.Equal(t, 1, length(l)) 356 require.Equal(t, 1, lengthRev(l)) 357 }) 358 } 359 } 360 361 func TestSkiplistAdd(t *testing.T) { 362 for _, inserter := range []bool{false, true} { 363 t.Run(fmt.Sprintf("inserter=%t", inserter), func(t *testing.T) { 364 l := NewSkiplist(newArena(arenaSize), bytes.Compare) 365 it := newIterAdapter(l.NewIter(nil, nil)) 366 367 add := l.Add 368 if inserter { 369 add = makeInserterAdd(l) 370 } 371 372 err := add(base.InternalKey{}, nil) 373 require.Nil(t, err) 374 require.True(t, it.SeekGE([]byte{})) 375 require.EqualValues(t, []byte{}, it.Key().UserKey) 376 require.EqualValues(t, []byte{}, it.Value()) 377 378 l = NewSkiplist(newArena(arenaSize), bytes.Compare) 379 it = newIterAdapter(l.NewIter(nil, nil)) 380 381 add = l.Add 382 if inserter { 383 add = makeInserterAdd(l) 384 } 385 386 err = add(makeIkey(""), []byte{}) 387 require.Nil(t, err) 388 require.True(t, it.SeekGE([]byte{})) 389 require.EqualValues(t, []byte{}, it.Key().UserKey) 390 require.EqualValues(t, []byte{}, it.Value()) 391 392 err = add(makeIntKey(2), makeValue(2)) 393 require.Nil(t, err) 394 require.True(t, it.SeekGE(makeKey("00002"))) 395 require.EqualValues(t, "00002", it.Key().UserKey) 396 require.EqualValues(t, makeValue(2), it.Value()) 397 398 err = add(makeIntKey(1), makeValue(1)) 399 require.Nil(t, err) 400 require.True(t, it.SeekGE(makeKey("00001"))) 401 require.EqualValues(t, "00001", it.Key().UserKey) 402 require.EqualValues(t, makeValue(1), it.Value()) 403 404 err = add(makeIntKey(4), makeValue(4)) 405 require.Nil(t, err) 406 require.True(t, it.SeekGE(makeKey("00004"))) 407 require.EqualValues(t, "00004", it.Key().UserKey) 408 require.EqualValues(t, makeValue(4), it.Value()) 409 410 err = add(makeIntKey(3), makeValue(3)) 411 require.Nil(t, err) 412 require.True(t, it.SeekGE(makeKey("00003"))) 413 require.EqualValues(t, "00003", it.Key().UserKey) 414 require.EqualValues(t, makeValue(3), it.Value()) 415 416 err = add(makeIntKey(2), nil) 417 require.Equal(t, ErrRecordExists, err) 418 require.EqualValues(t, "00003", it.Key().UserKey) 419 require.EqualValues(t, makeValue(3), it.Value()) 420 421 require.Equal(t, 5, length(l)) 422 require.Equal(t, 5, lengthRev(l)) 423 }) 424 } 425 } 426 427 func TestConcurrentAdd(t *testing.T) { 428 for _, inserter := range []bool{false, true} { 429 t.Run(fmt.Sprintf("inserter=%t", inserter), func(t *testing.T) { 430 const n = 100 431 432 l := NewSkiplist(newArena(arenaSize), bytes.Compare) 433 l.testing = true 434 435 start := make([]sync.WaitGroup, n) 436 end := make([]sync.WaitGroup, n) 437 438 for i := 0; i < n; i++ { 439 start[i].Add(1) 440 end[i].Add(2) 441 } 442 443 for f := 0; f < 2; f++ { 444 go func(f int) { 445 it := newIterAdapter(l.NewIter(nil, nil)) 446 add := l.Add 447 if inserter { 448 add = makeInserterAdd(l) 449 } 450 451 for i := 0; i < n; i++ { 452 start[i].Wait() 453 454 key := makeIntKey(i) 455 if add(key, nil) == nil { 456 require.True(t, it.SeekGE(key.UserKey)) 457 require.EqualValues(t, key, it.Key()) 458 } 459 460 end[i].Done() 461 } 462 }(f) 463 } 464 465 for i := 0; i < n; i++ { 466 start[i].Done() 467 end[i].Wait() 468 } 469 470 require.Equal(t, n, length(l)) 471 require.Equal(t, n, lengthRev(l)) 472 }) 473 } 474 } 475 476 func TestIteratorNext(t *testing.T) { 477 const n = 100 478 l := NewSkiplist(newArena(arenaSize), bytes.Compare) 479 it := newIterAdapter(l.NewIter(nil, nil)) 480 481 require.False(t, it.Valid()) 482 483 it.First() 484 require.False(t, it.Valid()) 485 486 for i := n - 1; i >= 0; i-- { 487 l.Add(makeIntKey(i), makeValue(i)) 488 } 489 490 it.First() 491 for i := 0; i < n; i++ { 492 require.True(t, it.Valid()) 493 require.EqualValues(t, makeIntKey(i), it.Key()) 494 require.EqualValues(t, makeValue(i), it.Value()) 495 it.Next() 496 } 497 require.False(t, it.Valid()) 498 } 499 500 func TestIteratorPrev(t *testing.T) { 501 const n = 100 502 l := NewSkiplist(newArena(arenaSize), bytes.Compare) 503 it := newIterAdapter(l.NewIter(nil, nil)) 504 505 require.False(t, it.Valid()) 506 507 it.Last() 508 require.False(t, it.Valid()) 509 510 var ins Inserter 511 for i := 0; i < n; i++ { 512 ins.Add(l, makeIntKey(i), makeValue(i)) 513 } 514 515 it.Last() 516 for i := n - 1; i >= 0; i-- { 517 require.True(t, it.Valid()) 518 require.EqualValues(t, makeIntKey(i), it.Key()) 519 require.EqualValues(t, makeValue(i), it.Value()) 520 it.Prev() 521 } 522 require.False(t, it.Valid()) 523 } 524 525 func TestIteratorSeekGEAndSeekPrefixGE(t *testing.T) { 526 const n = 100 527 l := NewSkiplist(newArena(arenaSize), bytes.Compare) 528 it := newIterAdapter(l.NewIter(nil, nil)) 529 530 require.False(t, it.Valid()) 531 it.First() 532 require.False(t, it.Valid()) 533 534 var ins Inserter 535 for i := n - 1; i >= 0; i-- { 536 v := i*10 + 1000 537 ins.Add(l, makeIntKey(v), makeValue(v)) 538 } 539 540 require.True(t, it.SeekGE(makeKey(""))) 541 require.True(t, it.Valid()) 542 require.EqualValues(t, "01000", it.Key().UserKey) 543 require.EqualValues(t, "v01000", it.Value()) 544 545 require.True(t, it.SeekGE(makeKey("01000"))) 546 require.True(t, it.Valid()) 547 require.EqualValues(t, "01000", it.Key().UserKey) 548 require.EqualValues(t, "v01000", it.Value()) 549 550 require.True(t, it.SeekGE(makeKey("01005"))) 551 require.True(t, it.Valid()) 552 require.EqualValues(t, "01010", it.Key().UserKey) 553 require.EqualValues(t, "v01010", it.Value()) 554 555 require.True(t, it.SeekGE(makeKey("01010"))) 556 require.True(t, it.Valid()) 557 require.EqualValues(t, "01010", it.Key().UserKey) 558 require.EqualValues(t, "v01010", it.Value()) 559 560 require.False(t, it.SeekGE(makeKey("99999"))) 561 require.False(t, it.Valid()) 562 563 { 564 require.True(t, it.SeekPrefixGE(makeKey("01000"), makeKey("01000"), false)) 565 require.True(t, it.Valid()) 566 require.EqualValues(t, "01000", it.Key().UserKey) 567 require.EqualValues(t, "v01000", it.Value()) 568 569 require.True(t, it.SeekPrefixGE(makeKey("01000"), makeKey("01000"), true)) 570 require.True(t, it.Valid()) 571 require.EqualValues(t, "01000", it.Key().UserKey) 572 require.EqualValues(t, "v01000", it.Value()) 573 574 require.True(t, it.SeekPrefixGE(makeKey("01020"), makeKey("01020"), true)) 575 require.True(t, it.Valid()) 576 require.EqualValues(t, "01020", it.Key().UserKey) 577 require.EqualValues(t, "v01020", it.Value()) 578 579 require.True(t, it.SeekPrefixGE(makeKey("01200"), makeKey("01200"), true)) 580 require.True(t, it.Valid()) 581 require.EqualValues(t, "01200", it.Key().UserKey) 582 require.EqualValues(t, "v01200", it.Value()) 583 584 require.True(t, it.SeekPrefixGE(makeKey("01100"), makeKey("01100"), true)) 585 require.True(t, it.Valid()) 586 require.EqualValues(t, "01200", it.Key().UserKey) 587 require.EqualValues(t, "v01200", it.Value()) 588 589 require.True(t, it.SeekPrefixGE(makeKey("01100"), makeKey("01100"), false)) 590 require.True(t, it.Valid()) 591 require.EqualValues(t, "01100", it.Key().UserKey) 592 require.EqualValues(t, "v01100", it.Value()) 593 } 594 595 ins.Add(l, base.InternalKey{}, nil) 596 require.True(t, it.SeekGE([]byte{})) 597 require.True(t, it.Valid()) 598 require.EqualValues(t, "", it.Key().UserKey) 599 600 require.True(t, it.SeekGE(makeKey(""))) 601 require.True(t, it.Valid()) 602 require.EqualValues(t, "", it.Key().UserKey) 603 } 604 605 func TestIteratorSeekLT(t *testing.T) { 606 const n = 100 607 l := NewSkiplist(newArena(arenaSize), bytes.Compare) 608 it := newIterAdapter(l.NewIter(nil, nil)) 609 610 require.False(t, it.Valid()) 611 it.First() 612 require.False(t, it.Valid()) 613 614 var ins Inserter 615 for i := n - 1; i >= 0; i-- { 616 v := i*10 + 1000 617 ins.Add(l, makeIntKey(v), makeValue(v)) 618 } 619 620 require.False(t, it.SeekLT(makeKey(""))) 621 require.False(t, it.Valid()) 622 623 require.False(t, it.SeekLT(makeKey("01000"))) 624 require.False(t, it.Valid()) 625 626 require.True(t, it.SeekLT(makeKey("01001"))) 627 require.True(t, it.Valid()) 628 require.EqualValues(t, "01000", it.Key().UserKey) 629 require.EqualValues(t, "v01000", it.Value()) 630 631 require.True(t, it.SeekLT(makeKey("01005"))) 632 require.True(t, it.Valid()) 633 require.EqualValues(t, "01000", it.Key().UserKey) 634 require.EqualValues(t, "v01000", it.Value()) 635 636 require.True(t, it.SeekLT(makeKey("01991"))) 637 require.True(t, it.Valid()) 638 require.EqualValues(t, "01990", it.Key().UserKey) 639 require.EqualValues(t, "v01990", it.Value()) 640 641 require.True(t, it.SeekLT(makeKey("99999"))) 642 require.True(t, it.Valid()) 643 require.EqualValues(t, "01990", it.Key().UserKey) 644 require.EqualValues(t, "v01990", it.Value()) 645 646 ins.Add(l, base.InternalKey{}, nil) 647 require.False(t, it.SeekLT([]byte{})) 648 require.False(t, it.Valid()) 649 650 require.True(t, it.SeekLT(makeKey("\x01"))) 651 require.True(t, it.Valid()) 652 require.EqualValues(t, "", it.Key().UserKey) 653 } 654 655 func TestIteratorBounds(t *testing.T) { 656 l := NewSkiplist(newArena(arenaSize), bytes.Compare) 657 for i := 1; i < 10; i++ { 658 require.NoError(t, l.Add(makeIntKey(i), makeValue(i))) 659 } 660 661 key := func(i int) []byte { 662 return makeIntKey(i).UserKey 663 } 664 665 it := newIterAdapter(l.NewIter(key(3), key(7))) 666 667 for i := 3; i <= 6; i++ { 668 k := key(i) 669 require.True(t, it.SeekGE(k)) 670 require.EqualValues(t, string(k), string(it.Key().UserKey)) 671 } 672 673 for i := 1; i < 3; i++ { 674 k := key(i) 675 require.True(t, it.SeekGE(k)) 676 require.EqualValues(t, string(k), string(it.Key().UserKey)) 677 } 678 679 for i := 7; i < 10; i++ { 680 require.False(t, it.SeekGE(key(i))) 681 } 682 683 require.True(t, it.SeekGE(key(6))) 684 require.EqualValues(t, "00006", it.Key().UserKey) 685 require.EqualValues(t, "v00006", it.Value()) 686 687 require.False(t, it.Next()) 688 689 for i := 4; i <= 7; i++ { 690 require.True(t, it.SeekLT(key(i))) 691 require.EqualValues(t, string(key(i-1)), string(it.Key().UserKey)) 692 } 693 694 for i := 8; i < 9; i++ { 695 require.True(t, it.SeekLT(key(8))) 696 require.EqualValues(t, string(key(i-1)), string(it.Key().UserKey)) 697 } 698 699 for i := 1; i < 4; i++ { 700 require.False(t, it.SeekLT(key(i))) 701 } 702 703 require.True(t, it.SeekLT(key(4))) 704 require.EqualValues(t, "00003", it.Key().UserKey) 705 require.EqualValues(t, "v00003", it.Value()) 706 707 require.False(t, it.Prev()) 708 } 709 710 func TestBytesIterated(t *testing.T) { 711 l := NewSkiplist(newArena(arenaSize), bytes.Compare) 712 emptySize := l.arena.Size() 713 for i := 0; i < 200; i++ { 714 bytesIterated := l.bytesIterated(t) 715 expected := uint64(l.arena.Size() - emptySize) 716 if bytesIterated != expected { 717 t.Fatalf("bytesIterated: got %d, want %d", bytesIterated, expected) 718 } 719 l.Add(base.InternalKey{UserKey: []byte{byte(i)}}, nil) 720 } 721 } 722 723 func (s *Skiplist) bytesIterated(t *testing.T) (bytesIterated uint64) { 724 x := s.NewFlushIter(&bytesIterated) 725 var prevIterated uint64 726 for key, _ := x.First(); key != nil; key, _ = x.Next() { 727 if bytesIterated < prevIterated { 728 t.Fatalf("bytesIterated moved backward: %d < %d", bytesIterated, prevIterated) 729 } 730 prevIterated = bytesIterated 731 } 732 if x.Close() != nil { 733 return 0 734 } 735 return bytesIterated 736 } 737 738 func randomKey(rng *rand.Rand, b []byte) base.InternalKey { 739 key := rng.Uint32() 740 key2 := rng.Uint32() 741 binary.LittleEndian.PutUint32(b, key) 742 binary.LittleEndian.PutUint32(b[4:], key2) 743 return base.InternalKey{UserKey: b} 744 } 745 746 func BenchmarkReadWrite(b *testing.B) { 747 for i := 0; i <= 10; i++ { 748 readFrac := float32(i) / 10.0 749 b.Run(fmt.Sprintf("frac_%d", i*10), func(b *testing.B) { 750 l := NewSkiplist(newArena(uint32((b.N+2)*maxNodeSize)), bytes.Compare) 751 b.ResetTimer() 752 var count int 753 b.RunParallel(func(pb *testing.PB) { 754 it := l.NewIter(nil, nil) 755 rng := rand.New(rand.NewSource(uint64(time.Now().UnixNano()))) 756 buf := make([]byte, 8) 757 758 for pb.Next() { 759 if rng.Float32() < readFrac { 760 key, _ := it.SeekGE(randomKey(rng, buf).UserKey) 761 if key != nil { 762 _ = key 763 count++ 764 } 765 } else { 766 _ = l.Add(randomKey(rng, buf), nil) 767 } 768 } 769 }) 770 }) 771 } 772 } 773 774 func BenchmarkOrderedWrite(b *testing.B) { 775 l := NewSkiplist(newArena(8<<20), bytes.Compare) 776 var ins Inserter 777 buf := make([]byte, 8) 778 779 b.ResetTimer() 780 for i := 0; i < b.N; i++ { 781 binary.BigEndian.PutUint64(buf, uint64(i)) 782 if err := ins.Add(l, base.InternalKey{UserKey: buf}, nil); err == ErrArenaFull { 783 b.StopTimer() 784 l = NewSkiplist(newArena(uint32((b.N+2)*maxNodeSize)), bytes.Compare) 785 ins = Inserter{} 786 b.StartTimer() 787 } 788 } 789 } 790 791 func BenchmarkIterNext(b *testing.B) { 792 l := NewSkiplist(newArena(64<<10), bytes.Compare) 793 rng := rand.New(rand.NewSource(uint64(time.Now().UnixNano()))) 794 buf := make([]byte, 8) 795 for { 796 if err := l.Add(randomKey(rng, buf), nil); err == ErrArenaFull { 797 break 798 } 799 } 800 801 it := l.NewIter(nil, nil) 802 b.ResetTimer() 803 for i := 0; i < b.N; i++ { 804 key, _ := it.Next() 805 if key == nil { 806 key, _ = it.First() 807 } 808 _ = key 809 } 810 } 811 812 func BenchmarkIterPrev(b *testing.B) { 813 l := NewSkiplist(newArena(64<<10), bytes.Compare) 814 rng := rand.New(rand.NewSource(uint64(time.Now().UnixNano()))) 815 buf := make([]byte, 8) 816 for { 817 if err := l.Add(randomKey(rng, buf), nil); err == ErrArenaFull { 818 break 819 } 820 } 821 822 it := l.NewIter(nil, nil) 823 _, _ = it.Last() 824 b.ResetTimer() 825 for i := 0; i < b.N; i++ { 826 key, _ := it.Prev() 827 if key == nil { 828 key, _ = it.Last() 829 } 830 _ = key 831 } 832 } 833 834 func BenchmarkSeekPrefixGE(b *testing.B) { 835 l := NewSkiplist(newArena(64<<10), bytes.Compare) 836 var count int 837 for count = 0; ; count++ { 838 if err := l.Add(makeIntKey(count), makeValue(count)); err == ErrArenaFull { 839 break 840 } 841 } 842 for _, skip := range []int{1, 2, 4, 8, 16} { 843 for _, useNext := range []bool{false, true} { 844 b.Run(fmt.Sprintf("skip=%d/use-next=%t", skip, useNext), func(b *testing.B) { 845 it := l.NewIter(nil, nil) 846 j := 0 847 var k []byte 848 makeKey := func() { 849 k = []byte(fmt.Sprintf("%05d", j)) 850 } 851 makeKey() 852 it.SeekPrefixGE(k, k, false) 853 b.ResetTimer() 854 for i := 0; i < b.N; i++ { 855 j += skip 856 trySeekUsingNext := useNext 857 if j >= count { 858 j = 0 859 trySeekUsingNext = false 860 } 861 makeKey() 862 it.SeekPrefixGE(k, k, trySeekUsingNext) 863 } 864 }) 865 } 866 } 867 } 868 869 func TestInvalidInternalKeyDecoding(t *testing.T) { 870 a := newArena(arenaSize) 871 872 nd, err := newRawNode(a, 1, 1, 1) 873 require.Nil(t, err) 874 875 l := NewSkiplist(a, bytes.Compare) 876 it := Iterator{ 877 list: l, 878 nd: nd, 879 } 880 it.decodeKey() 881 require.Nil(t, it.key.UserKey) 882 require.Equal(t, uint64(base.InternalKeyKindInvalid), it.key.Trailer) 883 }