MCPcopy
hub / github.com/dgraph-io/badger / TestIterate2Basic

Function TestIterate2Basic

db_test.go:660–733  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

658}
659
660func TestIterate2Basic(t *testing.T) {
661 test := func(t *testing.T, db *DB) {
662 bkey := func(i int) []byte {
663 return []byte(fmt.Sprintf("%09d", i))
664 }
665 bval := func(i int) []byte {
666 return []byte(fmt.Sprintf("%025d", i))
667 }
668
669 // n := 500000
670 n := 10000
671 for i := 0; i < n; i++ {
672 if (i % 1000) == 0 {
673 t.Logf("Put i=%d\n", i)
674 }
675 txnSet(t, db, bkey(i), bval(i), byte(i%127))
676 }
677
678 opt := IteratorOptions{}
679 opt.PrefetchValues = true
680 opt.PrefetchSize = 10
681
682 txn := db.NewTransaction(false)
683 it := txn.NewIterator(opt)
684 {
685 var count int
686 rewind := true
687 t.Log("Starting first basic iteration")
688 for it.Rewind(); it.Valid(); it.Next() {
689 item := it.Item()
690 key := item.Key()
691 if rewind && count == 5000 {
692 // Rewind would skip /head/ key, and it.Next() would skip 0.
693 count = 1
694 it.Rewind()
695 t.Log("Rewinding from 5000 to zero.")
696 rewind = false
697 continue
698 }
699 require.EqualValues(t, bkey(count), string(key))
700 val := getItemValue(t, item)
701 require.EqualValues(t, bval(count), string(val))
702 require.Equal(t, byte(count%127), item.UserMeta())
703 count++
704 }
705 require.EqualValues(t, n, count)
706 }
707
708 {
709 t.Log("Starting second basic iteration")
710 idx := 5030
711 for it.Seek(bkey(idx)); it.Valid(); it.Next() {
712 item := it.Item()
713 require.EqualValues(t, bkey(idx), string(item.Key()))
714 require.EqualValues(t, bval(idx), string(getItemValue(t, item)))
715 idx++
716 }
717 }

Callers

nothing calls this directly

Calls 15

RewindMethod · 0.95
ValidMethod · 0.95
NextMethod · 0.95
ItemMethod · 0.95
SeekMethod · 0.95
CloseMethod · 0.95
txnSetFunction · 0.85
getItemValueFunction · 0.85
runBadgerTestFunction · 0.85
DefaultOptionsFunction · 0.85
OpenFunction · 0.85
NewTransactionMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…