func (db *Store) initReadOptions() { db.ro = rocksdb.NewDefaultReadOptions() }
func TestPrefixSeekWithDelimiterKey(t *testing.T) { setup() defer teardown() Convey("Giving meta store", t, func() { Convey("First iteration: populate data", func() { // @rdallman suggest this hack on gorocksdb issue #24 // maxKey := []byte{ // 0xFF, 0xFF, 0xFF, 0xFF, // 0xFF, 0xFF, 0xFF, 0xFF, // 0xFF, 0xFF, 0xFF, 0xFF, // 0xFF, 0xFF, 0xFF, 0xFF, // } // mdb.Put(maxKey, []byte("")) for i := 0; i < 1000; i++ { key := NewFlakeKey(TableJobFeed, mdb.NextId()) mdb.Put(key.Bytes(), []byte("value1")) } for i := 0; i < 1000; i++ { key := NewFlakeKey(TableJobRunning, mdb.NextId()) mdb.Put(key.Bytes(), []byte("value2")) } for i := 0; i < 1000; i++ { key := NewFlakeKey(TableMax, mdb.NextId()) mdb.Put(key.Bytes(), []byte("value3")) } ro := rocksdb.NewDefaultReadOptions() // ro.prefix_seek = true is on by default key := NewFlakeKey(TableJobFeed, mdb.NextId()) it := mdb.rdb.NewIterator(ro) defer it.Close() it.Seek(key.Prefix().Bytes()) numFound := 0 for ; it.Valid(); it.Next() { kk := it.Key() kk.Free() numFound++ } So(it.Err(), ShouldBeNil) // mdb switched to Block-based format //So(numFound, ShouldEqual, 1000) So(numFound, ShouldEqual, 3000) }) Convey("Second iteration: reopen db", func() { // reopen rdb.Close() mdb.Close() setup() defer teardown() // iter to key>=prefix ro := rocksdb.NewDefaultReadOptions() // ro.prefix_seek = true is on by default key := NewFlakeKey(TableJobFeed, mdb.NextId()) it := mdb.rdb.NewIterator(ro) defer it.Close() numFound := 0 it.Seek(key.Prefix().Bytes()) for ; it.Valid(); it.Next() { kk := it.Key() kk.Free() numFound++ } So(it.Err(), ShouldBeNil) So(numFound, ShouldEqual, 3000) // so we need to use ValidForPrefix key = NewFlakeKey(TableJobFeed, mdb.NextId()) it = mdb.Iterator() defer it.Close() numFound = 0 it.Seek(key.Prefix().Bytes()) for ; it.ValidForPrefix(key.Prefix().Bytes()); it.Next() { kk := it.Key() kk.Free() numFound++ } So(it.Err(), ShouldBeNil) So(numFound, ShouldEqual, 1000) // iter to key>=prefix key = NewFlakeKey(TableJobRunning, mdb.NextId()) it = mdb.Iterator() defer it.Close() numFound = 0 it.Seek(key.Prefix().Bytes()) for ; it.Valid(); it.Next() { kk := it.Key() kk.Free() numFound++ } So(it.Err(), ShouldBeNil) So(numFound, ShouldEqual, 2000) // iter.ValidForPrefix key = NewFlakeKey(TableJobRunning, mdb.NextId()) it = mdb.Iterator() defer it.Close() numFound = 0 it.Seek(key.Prefix().Bytes()) for ; it.ValidForPrefix(key.Prefix().Bytes()); it.Next() { kk := it.Key() kk.Free() numFound++ } So(it.Err(), ShouldBeNil) So(numFound, ShouldEqual, 1000) }) }) }