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

Function TestIteratePrefix

iterator_test.go:127–237  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

125}
126
127func TestIteratePrefix(t *testing.T) {
128 testIteratorPrefix := func(t *testing.T, db *DB) {
129 bkey := func(i int) []byte {
130 return []byte(fmt.Sprintf("%04d", i))
131 }
132 val := []byte("OK")
133 n := 10000
134
135 batch := db.NewWriteBatch()
136 for i := 0; i < n; i++ {
137 if (i % 1000) == 0 {
138 t.Logf("Put i=%d\n", i)
139 }
140 require.NoError(t, batch.Set(bkey(i), val))
141 }
142 require.NoError(t, batch.Flush())
143
144 countKeys := func(prefix string) int {
145 t.Logf("Testing with prefix: %s", prefix)
146 var count int
147 opt := DefaultIteratorOptions
148 opt.Prefix = []byte(prefix)
149 err := db.View(func(txn *Txn) error {
150 itr := txn.NewIterator(opt)
151 defer itr.Close()
152 for itr.Rewind(); itr.Valid(); itr.Next() {
153 item := itr.Item()
154 err := item.Value(func(v []byte) error {
155 require.Equal(t, val, v)
156 return nil
157 })
158 require.NoError(t, err)
159 require.True(t, bytes.HasPrefix(item.Key(), opt.Prefix))
160 count++
161 }
162 return nil
163 })
164 require.NoError(t, err)
165 return count
166 }
167
168 countOneKey := func(key []byte) int {
169 var count int
170 err := db.View(func(txn *Txn) error {
171 itr := txn.NewKeyIterator(key, DefaultIteratorOptions)
172 defer itr.Close()
173 for itr.Rewind(); itr.Valid(); itr.Next() {
174 item := itr.Item()
175 err := item.Value(func(v []byte) error {
176 require.Equal(t, val, v)
177 return nil
178 })
179 require.NoError(t, err)
180 require.Equal(t, key, item.Key())
181 count++
182 }
183 return nil
184 })

Callers

nothing calls this directly

Calls 15

SetMethod · 0.95
FlushMethod · 0.95
runBadgerTestFunction · 0.85
getTestOptionsFunction · 0.85
NewWriteBatchMethod · 0.80
ViewMethod · 0.80
ItemMethod · 0.80
NewKeyIteratorMethod · 0.80
WithKeepBlocksInCacheMethod · 0.80
CloseMethod · 0.65
RewindMethod · 0.65

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…