Ensure that a Tx cursor can seek to the appropriate keys when there are a large number of keys. This test also checks that seek will always move forward to the next key. Related: https://github.com/boltdb/bolt/pull/187
(t *testing.T)
| 299 | // |
| 300 | // Related: https://github.com/boltdb/bolt/pull/187 |
| 301 | func TestCursor_Seek_Large(t *testing.T) { |
| 302 | db := btesting.MustCreateDB(t) |
| 303 | |
| 304 | var count = 10000 |
| 305 | |
| 306 | // Insert every other key between 0 and $count. |
| 307 | if err := db.Update(func(tx *bolt.Tx) error { |
| 308 | b, err := tx.CreateBucket([]byte("widgets")) |
| 309 | if err != nil { |
| 310 | t.Fatal(err) |
| 311 | } |
| 312 | |
| 313 | for i := 0; i < count; i += 100 { |
| 314 | for j := i; j < i+100; j += 2 { |
| 315 | k := make([]byte, 8) |
| 316 | binary.BigEndian.PutUint64(k, uint64(j)) |
| 317 | if err := b.Put(k, make([]byte, 100)); err != nil { |
| 318 | t.Fatal(err) |
| 319 | } |
| 320 | } |
| 321 | } |
| 322 | return nil |
| 323 | }); err != nil { |
| 324 | t.Fatal(err) |
| 325 | } |
| 326 | |
| 327 | if err := db.View(func(tx *bolt.Tx) error { |
| 328 | c := tx.Bucket([]byte("widgets")).Cursor() |
| 329 | for i := 0; i < count; i++ { |
| 330 | seek := make([]byte, 8) |
| 331 | binary.BigEndian.PutUint64(seek, uint64(i)) |
| 332 | |
| 333 | k, _ := c.Seek(seek) |
| 334 | |
| 335 | // The last seek is beyond the end of the range so |
| 336 | // it should return nil. |
| 337 | if i == count-1 { |
| 338 | if k != nil { |
| 339 | t.Fatal("expected nil key") |
| 340 | } |
| 341 | continue |
| 342 | } |
| 343 | |
| 344 | // Otherwise we should seek to the exact key or the next key. |
| 345 | num := binary.BigEndian.Uint64(k) |
| 346 | if i%2 == 0 { |
| 347 | if num != uint64(i) { |
| 348 | t.Fatalf("unexpected num: %d", num) |
| 349 | } |
| 350 | } else { |
| 351 | if num != uint64(i+1) { |
| 352 | t.Fatalf("unexpected num: %d", num) |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | return nil |
| 358 | }); err != nil { |
nothing calls this directly
no test coverage detected