func TestDb_BloomFilter(t *testing.T) { h := newDbHarnessWopt(t, &opt.Options{ BlockCache: opt.NoCache, Filter: filter.NewBloomFilter(10), }) defer h.close() key := func(i int) string { return fmt.Sprintf("key%06d", i) } n := 10000 // Populate multiple layers for i := 0; i < n; i++ { h.put(key(i), key(i)) } h.compactMem() h.compactRange("a", "z") for i := 0; i < n; i += 100 { h.put(key(i), key(i)) } h.compactMem() // Prevent auto compactions triggered by seeks h.stor.DelaySync(storage.TypeTable) // Lookup present keys. Should rarely read from small sstable. h.stor.SetReadCounter(storage.TypeTable) for i := 0; i < n; i++ { h.getVal(key(i), key(i)) } cnt := int(h.stor.ReadCounter()) t.Logf("lookup of %d present keys yield %d sstable I/O reads", n, cnt) if min, max := n, n+2*n/100; cnt < min || cnt > max { t.Errorf("num of sstable I/O reads of present keys not in range of %d - %d, got %d", min, max, cnt) } // Lookup missing keys. Should rarely read from either sstable. h.stor.ResetReadCounter() for i := 0; i < n; i++ { h.get(key(i)+".missing", false) } cnt = int(h.stor.ReadCounter()) t.Logf("lookup of %d missing keys yield %d sstable I/O reads", n, cnt) if max := 3 * n / 100; cnt > max { t.Errorf("num of sstable I/O reads of missing keys was more than %d, got %d", max, cnt) } h.stor.ReleaseSync(storage.TypeTable) }
Key() []byte Value() []byte } func testKeyVal(t *testing.T, kv keyValue, want string) { res := string(kv.Key()) + "->" + string(kv.Value()) if res != want { t.Errorf("invalid key/value, want=%q, got=%q", want, res) } } func numKey(num int) string { return fmt.Sprintf("key%06d", num) } var _bloom_filter = filter.NewBloomFilter(10) func truno(t *testing.T, o *opt.Options, f func(h *dbHarness)) { for i := 0; i < 4; i++ { func() { switch i { case 0: case 1: if o == nil { o = &opt.Options{Filter: _bloom_filter} } else { old := o o = &opt.Options{} *o = *old o.Filter = _bloom_filter }