@@ -324,21 +324,22 @@ class DBTest {
324
324
kHashCuckoo = 7 ,
325
325
kMergePut = 8 ,
326
326
kFilter = 9 ,
327
- kUncompressed = 10 ,
328
- kNumLevel_3 = 11 ,
329
- kDBLogDir = 12 ,
330
- kWalDir = 13 ,
331
- kManifestFileSize = 14 ,
332
- kCompactOnFlush = 15 ,
333
- kPerfOptions = 16 ,
334
- kDeletesFilterFirst = 17 ,
335
- kHashSkipList = 18 ,
336
- kUniversalCompaction = 19 ,
337
- kCompressedBlockCache = 20 ,
338
- kInfiniteMaxOpenFiles = 21 ,
339
- kxxHashChecksum = 22 ,
340
- kFIFOCompaction = 23 ,
341
- kEnd = 24
327
+ kFullFilter = 10 ,
328
+ kUncompressed = 11 ,
329
+ kNumLevel_3 = 12 ,
330
+ kDBLogDir = 13 ,
331
+ kWalDir = 14 ,
332
+ kManifestFileSize = 15 ,
333
+ kCompactOnFlush = 16 ,
334
+ kPerfOptions = 17 ,
335
+ kDeletesFilterFirst = 18 ,
336
+ kHashSkipList = 19 ,
337
+ kUniversalCompaction = 20 ,
338
+ kCompressedBlockCache = 21 ,
339
+ kInfiniteMaxOpenFiles = 22 ,
340
+ kxxHashChecksum = 23 ,
341
+ kFIFOCompaction = 24 ,
342
+ kEnd = 25
342
343
};
343
344
int option_config_;
344
345
@@ -448,6 +449,30 @@ class DBTest {
448
449
}
449
450
}
450
451
452
+ // Switch between different filter policy
453
+ // Jump from kDefault to kFilter to kFullFilter
454
+ bool ChangeFilterOptions (Options* prev_options = nullptr ) {
455
+ if (option_config_ == kDefault ) {
456
+ option_config_ = kFilter ;
457
+ if (prev_options == nullptr ) {
458
+ prev_options = &last_options_;
459
+ }
460
+ Destroy (prev_options);
461
+ TryReopen ();
462
+ return true ;
463
+ } else if (option_config_ == kFilter ) {
464
+ option_config_ = kFullFilter ;
465
+ if (prev_options == nullptr ) {
466
+ prev_options = &last_options_;
467
+ }
468
+ Destroy (prev_options);
469
+ TryReopen ();
470
+ return true ;
471
+ } else {
472
+ return false ;
473
+ }
474
+ }
475
+
451
476
// Return the current option configuration.
452
477
Options CurrentOptions (
453
478
const anon::OptionsOverride& options_override = anon::OptionsOverride()) {
@@ -486,7 +511,10 @@ class DBTest {
486
511
options.merge_operator = MergeOperators::CreatePutOperator ();
487
512
break ;
488
513
case kFilter :
489
- table_options.filter_policy .reset (NewBloomFilterPolicy (10 ));
514
+ table_options.filter_policy .reset (NewBloomFilterPolicy (10 , true ));
515
+ break ;
516
+ case kFullFilter :
517
+ table_options.filter_policy .reset (NewBloomFilterPolicy (10 , false ));
490
518
break ;
491
519
case kUncompressed :
492
520
options.compression = kNoCompression ;
@@ -5744,6 +5772,92 @@ TEST(DBTest, BloomFilter) {
5744
5772
} while (ChangeCompactOptions ());
5745
5773
}
5746
5774
5775
+ TEST (DBTest, BloomFilterRate) {
5776
+ while (ChangeFilterOptions ()) {
5777
+ Options options = CurrentOptions ();
5778
+ options.statistics = rocksdb::CreateDBStatistics ();
5779
+ CreateAndReopenWithCF ({" pikachu" }, &options);
5780
+
5781
+ const int maxKey = 10000 ;
5782
+ for (int i = 0 ; i < maxKey; i++) {
5783
+ ASSERT_OK (Put (1 , Key (i), Key (i)));
5784
+ }
5785
+ // Add a large key to make the file contain wide range
5786
+ ASSERT_OK (Put (1 , Key (maxKey + 55555 ), Key (maxKey + 55555 )));
5787
+ Flush (1 );
5788
+
5789
+ // Check if they can be found
5790
+ for (int i = 0 ; i < maxKey; i++) {
5791
+ ASSERT_EQ (Key (i), Get (1 , Key (i)));
5792
+ }
5793
+ ASSERT_EQ (TestGetTickerCount (options, BLOOM_FILTER_USEFUL), 0 );
5794
+
5795
+ // Check if filter is useful
5796
+ for (int i = 0 ; i < maxKey; i++) {
5797
+ ASSERT_EQ (" NOT_FOUND" , Get (1 , Key (i+33333 )));
5798
+ }
5799
+ ASSERT_GE (TestGetTickerCount (options, BLOOM_FILTER_USEFUL), maxKey*0.98 );
5800
+ }
5801
+ }
5802
+
5803
+ TEST (DBTest, BloomFilterCompatibility) {
5804
+ Options options;
5805
+ options.statistics = rocksdb::CreateDBStatistics ();
5806
+ BlockBasedTableOptions table_options;
5807
+ table_options.filter_policy .reset (NewBloomFilterPolicy (10 , true ));
5808
+ options.table_factory .reset (NewBlockBasedTableFactory (table_options));
5809
+
5810
+ // Create with block based filter
5811
+ CreateAndReopenWithCF ({" pikachu" }, &options);
5812
+
5813
+ const int maxKey = 10000 ;
5814
+ for (int i = 0 ; i < maxKey; i++) {
5815
+ ASSERT_OK (Put (1 , Key (i), Key (i)));
5816
+ }
5817
+ ASSERT_OK (Put (1 , Key (maxKey + 55555 ), Key (maxKey + 55555 )));
5818
+ Flush (1 );
5819
+
5820
+ // Check db with full filter
5821
+ table_options.filter_policy .reset (NewBloomFilterPolicy (10 , false ));
5822
+ options.table_factory .reset (NewBlockBasedTableFactory (table_options));
5823
+ ReopenWithColumnFamilies ({" default" , " pikachu" }, &options);
5824
+
5825
+ // Check if they can be found
5826
+ for (int i = 0 ; i < maxKey; i++) {
5827
+ ASSERT_EQ (Key (i), Get (1 , Key (i)));
5828
+ }
5829
+ ASSERT_EQ (TestGetTickerCount (options, BLOOM_FILTER_USEFUL), 0 );
5830
+ }
5831
+
5832
+ TEST (DBTest, BloomFilterReverseCompatibility) {
5833
+ Options options;
5834
+ options.statistics = rocksdb::CreateDBStatistics ();
5835
+ BlockBasedTableOptions table_options;
5836
+ table_options.filter_policy .reset (NewBloomFilterPolicy (10 , false ));
5837
+ options.table_factory .reset (NewBlockBasedTableFactory (table_options));
5838
+
5839
+ // Create with full filter
5840
+ CreateAndReopenWithCF ({" pikachu" }, &options);
5841
+
5842
+ const int maxKey = 10000 ;
5843
+ for (int i = 0 ; i < maxKey; i++) {
5844
+ ASSERT_OK (Put (1 , Key (i), Key (i)));
5845
+ }
5846
+ ASSERT_OK (Put (1 , Key (maxKey + 55555 ), Key (maxKey + 55555 )));
5847
+ Flush (1 );
5848
+
5849
+ // Check db with block_based filter
5850
+ table_options.filter_policy .reset (NewBloomFilterPolicy (10 , true ));
5851
+ options.table_factory .reset (NewBlockBasedTableFactory (table_options));
5852
+ ReopenWithColumnFamilies ({" default" , " pikachu" }, &options);
5853
+
5854
+ // Check if they can be found
5855
+ for (int i = 0 ; i < maxKey; i++) {
5856
+ ASSERT_EQ (Key (i), Get (1 , Key (i)));
5857
+ }
5858
+ ASSERT_EQ (TestGetTickerCount (options, BLOOM_FILTER_USEFUL), 0 );
5859
+ }
5860
+
5747
5861
TEST (DBTest, SnapshotFiles) {
5748
5862
do {
5749
5863
Options options = CurrentOptions ();
@@ -7194,47 +7308,49 @@ void PrefixScanInit(DBTest *dbtest) {
7194
7308
} // namespace
7195
7309
7196
7310
TEST (DBTest, PrefixScan) {
7197
- int count;
7198
- Slice prefix;
7199
- Slice key;
7200
- char buf[100 ];
7201
- Iterator* iter;
7202
- snprintf (buf, sizeof (buf), " 03______:" );
7203
- prefix = Slice (buf, 8 );
7204
- key = Slice (buf, 9 );
7205
- // db configs
7206
- env_->count_random_reads_ = true ;
7207
- Options options = CurrentOptions ();
7208
- options.env = env_;
7209
- options.prefix_extractor .reset (NewFixedPrefixTransform (8 ));
7210
- options.disable_auto_compactions = true ;
7211
- options.max_background_compactions = 2 ;
7212
- options.create_if_missing = true ;
7213
- options.memtable_factory .reset (NewHashSkipListRepFactory (16 ));
7311
+ while (ChangeFilterOptions ()) {
7312
+ int count;
7313
+ Slice prefix;
7314
+ Slice key;
7315
+ char buf[100 ];
7316
+ Iterator* iter;
7317
+ snprintf (buf, sizeof (buf), " 03______:" );
7318
+ prefix = Slice (buf, 8 );
7319
+ key = Slice (buf, 9 );
7320
+ // db configs
7321
+ env_->count_random_reads_ = true ;
7322
+ Options options = CurrentOptions ();
7323
+ options.env = env_;
7324
+ options.prefix_extractor .reset (NewFixedPrefixTransform (8 ));
7325
+ options.disable_auto_compactions = true ;
7326
+ options.max_background_compactions = 2 ;
7327
+ options.create_if_missing = true ;
7328
+ options.memtable_factory .reset (NewHashSkipListRepFactory (16 ));
7214
7329
7215
- BlockBasedTableOptions table_options;
7216
- table_options.no_block_cache = true ;
7217
- table_options.filter_policy .reset (NewBloomFilterPolicy (10 ));
7218
- table_options.whole_key_filtering = false ;
7219
- options.table_factory .reset (NewBlockBasedTableFactory (table_options));
7330
+ BlockBasedTableOptions table_options;
7331
+ table_options.no_block_cache = true ;
7332
+ table_options.filter_policy .reset (NewBloomFilterPolicy (10 ));
7333
+ table_options.whole_key_filtering = false ;
7334
+ options.table_factory .reset (NewBlockBasedTableFactory (table_options));
7220
7335
7221
- // 11 RAND I/Os
7222
- DestroyAndReopen (&options);
7223
- PrefixScanInit (this );
7224
- count = 0 ;
7225
- env_->random_read_counter_ .Reset ();
7226
- iter = db_->NewIterator (ReadOptions ());
7227
- for (iter->Seek (prefix); iter->Valid (); iter->Next ()) {
7228
- if (! iter->key ().starts_with (prefix)) {
7229
- break ;
7336
+ // 11 RAND I/Os
7337
+ DestroyAndReopen (&options);
7338
+ PrefixScanInit (this );
7339
+ count = 0 ;
7340
+ env_->random_read_counter_ .Reset ();
7341
+ iter = db_->NewIterator (ReadOptions ());
7342
+ for (iter->Seek (prefix); iter->Valid (); iter->Next ()) {
7343
+ if (! iter->key ().starts_with (prefix)) {
7344
+ break ;
7345
+ }
7346
+ count++;
7230
7347
}
7231
- count++;
7232
- }
7233
- ASSERT_OK (iter->status ());
7234
- delete iter;
7235
- ASSERT_EQ (count, 2 );
7236
- ASSERT_EQ (env_->random_read_counter_ .Read (), 2 );
7237
- Close ();
7348
+ ASSERT_OK (iter->status ());
7349
+ delete iter;
7350
+ ASSERT_EQ (count, 2 );
7351
+ ASSERT_EQ (env_->random_read_counter_ .Read (), 2 );
7352
+ Close ();
7353
+ } // end of while
7238
7354
}
7239
7355
7240
7356
TEST (DBTest, TailingIteratorSingle) {
0 commit comments