table/table_test.cc
// Due to the difficulities of the intersaction between statistics, this test
// only tests the case when "index block is put to block cache"
Due to the difficulities of the intersaction between statistics, this test
only tests the case when "index block is put to block cache"
Due to the difficulities of the intersaction between statistics this test only tests the case when " index block is put to block cache "
TEST_F(BlockBasedTableTest, FilterBlockInBlockCache) {
  Options options;
  options.create_if_missing = true;
  options.statistics = CreateDBStatistics();
  BlockBasedTableOptions table_options;
  table_options.block_cache = NewLRUCache(1024);
  table_options.cache_index_and_filter_blocks = true;
  options.table_factory.reset(new BlockBasedTableFactory(table_options));
  std::vector<std::string> keys;
  KVMap kvmap;
  TableConstructor c(BytewiseComparator());
  c.Add("key", "value");
  const ImmutableCFOptions ioptions(options);
  c.Finish(options, ioptions, table_options,
           GetPlainInternalComparator(options.comparator), &keys, &kvmap);
  auto* reader = dynamic_cast<BlockBasedTable*>(c.GetTableReader());
  ASSERT_TRUE(!reader->TEST_filter_block_preloaded());
  ASSERT_TRUE(!reader->TEST_index_reader_preloaded());
  unique_ptr<Iterator> iter;
  {
    BlockCachePropertiesSnapshot props(options.statistics.get());
    props.AssertEqual(1,  
                      0, 0, 0);
  }
  {
    iter.reset(c.NewIterator());
    BlockCachePropertiesSnapshot props(options.statistics.get());
    props.AssertEqual(1, 0 + 1,  
                      0, 0);
  }
  {
    iter->SeekToFirst();
    BlockCachePropertiesSnapshot props(options.statistics.get());
    props.AssertEqual(1, 1, 0 + 1,  
                      0);
  }
  {
    iter.reset(c.NewIterator());
    iter->SeekToFirst();
    BlockCachePropertiesSnapshot props(options.statistics.get());
    props.AssertEqual(1, 1 + 1, 
                      1, 0 + 1 );
  }
  iter.reset();
  table_options.block_cache = NewLRUCache(1);
  options.statistics = CreateDBStatistics();
  options.table_factory.reset(new BlockBasedTableFactory(table_options));
  const ImmutableCFOptions ioptions2(options);
  c.Reopen(ioptions2);
  {
    BlockCachePropertiesSnapshot props(options.statistics.get());
    props.AssertEqual(1,  
                      0, 0, 0);
  }
  {
    iter.reset(c.NewIterator());
    BlockCachePropertiesSnapshot props(options.statistics.get());
    props.AssertEqual(1 + 1,  
                      0, 0,   
                      0);
  }
  {
    iter->SeekToFirst();
    BlockCachePropertiesSnapshot props(options.statistics.get());
    props.AssertEqual(2, 0, 0 + 1,  
                      0);
  }
  iter.reset();
  table_options.block_cache = NewLRUCache(4096);
  table_options.cache_index_and_filter_blocks = false;
  options.table_factory.reset(NewBlockBasedTableFactory(table_options));
  TableConstructor c3(BytewiseComparator());
  std::string user_key = "k01";
  InternalKey internal_key(user_key, 0, kTypeValue);
  c3.Add(internal_key.Encode().ToString(), "hello");
  ImmutableCFOptions ioptions3(options);
  c3.Finish(options, ioptions3, table_options,
           GetPlainInternalComparator(options.comparator), &keys, &kvmap);
  table_options.filter_policy.reset(NewBloomFilterPolicy(1));
  options.table_factory.reset(new BlockBasedTableFactory(table_options));
  options.statistics = CreateDBStatistics();
  ImmutableCFOptions ioptions4(options);
  ASSERT_OK(c3.Reopen(ioptions4));
  reader = dynamic_cast<BlockBasedTable*>(c3.GetTableReader());
  ASSERT_TRUE(!reader->TEST_filter_block_preloaded());
  std::string value;
  GetContext get_context(options.comparator, nullptr, nullptr, nullptr,
                         GetContext::kNotFound, user_key, &value, nullptr,
                         nullptr, nullptr);
  ASSERT_OK(reader->Get(ReadOptions(), user_key, &get_context));
  ASSERT_EQ(value, "hello");
  BlockCachePropertiesSnapshot props(options.statistics.get());
  props.AssertFilterBlockStat(0, 0);
}
TEST_F ( BlockBasedTableTest , FilterBlockInBlockCache ) { Options options ; options . create_if_missing = true ; options . statistics = CreateDBStatistics ( ) ; BlockBasedTableOptions table_options ; table_options . block_cache = NewLRUCache ( 1024 ) ; table_options . cache_index_and_filter_blocks = true ; options . table_factory . reset ( new BlockBasedTableFactory ( table_options ) ) ; std :: vector < std :: string > keys ; KVMap kvmap ; TableConstructor c ( BytewiseComparator ( ) ) ; c . Add ( " " , " " ) ; const ImmutableCFOptions ioptions ( options ) ; c . Finish ( options , ioptions , table_options , GetPlainInternalComparator ( options . comparator ) , & keys , & kvmap ) ; auto * reader = dynamic_cast < BlockBasedTable * > ( c . GetTableReader ( ) ) ; ASSERT_TRUE ( ! reader -> TEST_filter_block_preloaded ( ) ) ; ASSERT_TRUE ( ! reader -> TEST_index_reader_preloaded ( ) ) ; unique_ptr < Iterator > iter ; { BlockCachePropertiesSnapshot props ( options . statistics . get ( ) ) ; props . AssertEqual ( 1 , 0 , 0 , 0 ) ; } { iter . reset ( c . NewIterator ( ) ) ; BlockCachePropertiesSnapshot props ( options . statistics . get ( ) ) ; props . AssertEqual ( 1 , 0 + 1 , 0 , 0 ) ; } { iter -> SeekToFirst ( ) ; BlockCachePropertiesSnapshot props ( options . statistics . get ( ) ) ; props . AssertEqual ( 1 , 1 , 0 + 1 , 0 ) ; } { iter . reset ( c . NewIterator ( ) ) ; iter -> SeekToFirst ( ) ; BlockCachePropertiesSnapshot props ( options . statistics . get ( ) ) ; props . AssertEqual ( 1 , 1 + 1 , 1 , 0 + 1 ) ; } iter . reset ( ) ; table_options . block_cache = NewLRUCache ( 1 ) ; options . statistics = CreateDBStatistics ( ) ; options . table_factory . reset ( new BlockBasedTableFactory ( table_options ) ) ; const ImmutableCFOptions ioptions2 ( options ) ; c . Reopen ( ioptions2 ) ; { BlockCachePropertiesSnapshot props ( options . statistics . get ( ) ) ; props . AssertEqual ( 1 , 0 , 0 , 0 ) ; } { iter . reset ( c . NewIterator ( ) ) ; BlockCachePropertiesSnapshot props ( options . statistics . get ( ) ) ; props . AssertEqual ( 1 + 1 , 0 , 0 , 0 ) ; } { iter -> SeekToFirst ( ) ; BlockCachePropertiesSnapshot props ( options . statistics . get ( ) ) ; props . AssertEqual ( 2 , 0 , 0 + 1 , 0 ) ; } iter . reset ( ) ; table_options . block_cache = NewLRUCache ( 4096 ) ; table_options . cache_index_and_filter_blocks = false ; options . table_factory . reset ( NewBlockBasedTableFactory ( table_options ) ) ; TableConstructor c3 ( BytewiseComparator ( ) ) ; std :: string user_key = " " ; InternalKey internal_key ( user_key , 0 , kTypeValue ) ; c3 . Add ( internal_key . Encode ( ) . ToString ( ) , " " ) ; ImmutableCFOptions ioptions3 ( options ) ; c3 . Finish ( options , ioptions3 , table_options , GetPlainInternalComparator ( options . comparator ) , & keys , & kvmap ) ; table_options . filter_policy . reset ( NewBloomFilterPolicy ( 1 ) ) ; options . table_factory . reset ( new BlockBasedTableFactory ( table_options ) ) ; options . statistics = CreateDBStatistics ( ) ; ImmutableCFOptions ioptions4 ( options ) ; ASSERT_OK ( c3 . Reopen ( ioptions4 ) ) ; reader = dynamic_cast < BlockBasedTable * > ( c3 . GetTableReader ( ) ) ; ASSERT_TRUE ( ! reader -> TEST_filter_block_preloaded ( ) ) ; std :: string value ; GetContext get_context ( options . comparator , nullptr , nullptr , nullptr , GetContext :: kNotFound , user_key , & value , nullptr , nullptr , nullptr ) ; ASSERT_OK ( reader -> Get ( ReadOptions ( ) , user_key , & get_context ) ) ; ASSERT_EQ ( value , " " ) ; BlockCachePropertiesSnapshot props ( options . statistics . get ( ) ) ; props . AssertFilterBlockStat ( 0 , 0 ) ; }
Due to the difficulities of the intersaction between statistics, this test
only tests the case when "index block is put to block cache"
Due to the difficulities of the intersaction between statistics this test only tests the case when " index block is put to block cache "
// -- Table construction // Enable the cache for index/filter blocks // preloading filter/index blocks is prohibited. // -- PART 1: Open with regular block cache. // Since block_cache is disabled, no cache activities will be involved. // At first, no block will be accessed. // index will be added to block cache. // index block miss // Only index block will be accessed // NOTE: to help better highlight the "detla" of each ticker, I use // <last_value> + <added_value> to indicate the increment of changed // value; other numbers remain the same. // index block hit // Only data block will be accessed // data block miss // Data block will be in cache /* index block hit */ /* data block hit */ // release the iterator so that the block cache can reset correctly. // -- PART 2: Open with very small block cache // In this test, no block will ever get hit since the block cache is // too small to fit even one entry. // index block miss // Both index and data block get accessed. // It first cache index block then data block. But since the cache size // is only 1, index block will be purged after data block is inserted. // index block miss // data block miss // SeekToFirst() accesses data block. With similar reason, we expect data // block's cache miss. // data block miss // -- PART 3: Open table with bloom filter enabled but not in SST file // Generate table without filter policy // Open table with filter policy