From aac44226a013f24eb0f1cdb58a52ad2cbba4e70c Mon Sep 17 00:00:00 2001 From: Kai Liu Date: Wed, 16 Oct 2013 16:57:20 -0700 Subject: [PATCH] Add bloom filter to predefined table stats Summary: As title. Test Plan: Updated the unit tests to make sure new statistic is correctly written/read. Reviewers: dhruba, haobo CC: leveldb Differential Revision: https://reviews.facebook.net/D13497 --- include/rocksdb/table_stats.h | 4 ++++ table/table.cc | 30 +++++++++++++++++------------- table/table.h | 1 + table/table_builder.cc | 6 ++++++ table/table_test.cc | 14 ++++++++++++++ 5 files changed, 42 insertions(+), 13 deletions(-) diff --git a/include/rocksdb/table_stats.h b/include/rocksdb/table_stats.h index 6e2ad2b64..811be107e 100644 --- a/include/rocksdb/table_stats.h +++ b/include/rocksdb/table_stats.h @@ -35,6 +35,10 @@ struct TableStats { // the number of entries in this table uint64_t num_entries = 0; + // The name of the filter policy used in this table. + // If no filter policy is used, `filter_policy_name` will be an empty string. + std::string filter_policy_name; + // user collected stats UserCollectedStats user_collected_stats; }; diff --git a/table/table.cc b/table/table.cc index c4a5eed2f..6243ab144 100644 --- a/table/table.cc +++ b/table/table.cc @@ -269,7 +269,8 @@ Status Table::ReadStats(const Slice& handle_value, Rep* rep) { ); auto& table_stats = rep->table_stats; - std::unordered_map predefined_name_stat_map = { + // All pre-defined stats of type uint64_t + std::unordered_map predefined_uint64_stats = { { TableStatsNames::kDataSize, &table_stats.data_size }, { TableStatsNames::kIndexSize, &table_stats.index_size }, { TableStatsNames::kRawKeySize, &table_stats.raw_key_size }, @@ -294,14 +295,9 @@ Status Table::ReadStats(const Slice& handle_value, Rep* rep) { last_key = key; auto raw_val = iter->value(); - auto pos = predefined_name_stat_map.find(key); + auto pos = predefined_uint64_stats.find(key); - if (pos == predefined_name_stat_map.end()) { - // handle user-collected - table_stats.user_collected_stats.insert( - std::make_pair(iter->key().ToString(), raw_val.ToString()) - ); - } else { + if (pos != predefined_uint64_stats.end()) { // handle predefined rocksdb stats uint64_t val; if (!GetVarint64(&raw_val, &val)) { @@ -313,6 +309,13 @@ Status Table::ReadStats(const Slice& handle_value, Rep* rep) { continue; } *(pos->second) = val; + } else if (key == TableStatsNames::kFilterPolicy) { + table_stats.filter_policy_name = raw_val.ToString(); + } else { + // handle user-collected + table_stats.user_collected_stats.insert( + std::make_pair(iter->key().ToString(), raw_val.ToString()) + ); } } @@ -602,11 +605,12 @@ uint64_t Table::ApproximateOffsetOf(const Slice& key) const { const std::string Table::kFilterBlockPrefix = "filter."; const std::string Table::kStatsBlock = "rocksdb.stats"; -const std::string TableStatsNames::kDataSize = "rocksdb.data.size"; -const std::string TableStatsNames::kIndexSize = "rocksdb.index.size"; -const std::string TableStatsNames::kRawKeySize = "rocksdb.raw.key.size"; -const std::string TableStatsNames::kRawValueSize = "rocksdb.raw.value.size"; +const std::string TableStatsNames::kDataSize = "rocksdb.data.size"; +const std::string TableStatsNames::kIndexSize = "rocksdb.index.size"; +const std::string TableStatsNames::kRawKeySize = "rocksdb.raw.key.size"; +const std::string TableStatsNames::kRawValueSize = "rocksdb.raw.value.size"; const std::string TableStatsNames::kNumDataBlocks = "rocksdb.num.data.blocks"; -const std::string TableStatsNames::kNumEntries = "rocksdb.num.entries"; +const std::string TableStatsNames::kNumEntries = "rocksdb.num.entries"; +const std::string TableStatsNames::kFilterPolicy = "rocksdb.filter.policy"; } // namespace rocksdb diff --git a/table/table.h b/table/table.h index d96179e1f..13dc3f32b 100644 --- a/table/table.h +++ b/table/table.h @@ -120,6 +120,7 @@ struct TableStatsNames { static const std::string kRawValueSize; static const std::string kNumDataBlocks; static const std::string kNumEntries; + static const std::string kFilterPolicy; }; } // namespace rocksdb diff --git a/table/table_builder.cc b/table/table_builder.cc index 458fc4c2a..f4996f75d 100644 --- a/table/table_builder.cc +++ b/table/table_builder.cc @@ -368,6 +368,12 @@ Status TableBuilder::Finish() { ); AddStats(stats, TableStatsNames::kNumEntries, r->num_entries); AddStats(stats, TableStatsNames::kNumDataBlocks, r->num_data_blocks); + if (r->filter_block != nullptr) { + stats.insert(std::make_pair( + TableStatsNames::kFilterPolicy, + r->options.filter_policy->Name() + )); + } for (const auto& stat : stats) { stats_block.Add(stat.first, stat.second); diff --git a/table/table_test.cc b/table/table_test.cc index 1bd9c362b..066b7362e 100644 --- a/table/table_test.cc +++ b/table/table_test.cc @@ -886,6 +886,7 @@ TEST(TableTest, BasicTableStats) { ASSERT_EQ(raw_key_size, stats.raw_key_size); ASSERT_EQ(raw_value_size, stats.raw_value_size); ASSERT_EQ(1ul, stats.num_data_blocks); + ASSERT_EQ("", stats.filter_policy_name); // no filter policy is used // Verify data size. BlockBuilder block_builder(&options); @@ -899,6 +900,19 @@ TEST(TableTest, BasicTableStats) { ); } +TEST(TableTest, FilterPolicyNameStats) { + TableConstructor c(BytewiseComparator()); + c.Add("a1", "val1"); + std::vector keys; + KVMap kvmap; + Options options; + options.filter_policy = NewBloomFilterPolicy(10); + + c.Finish(options, &keys, &kvmap); + auto& stats = c.table()->GetTableStats(); + ASSERT_EQ("rocksdb.BuiltinBloomFilter", stats.filter_policy_name); +} + static std::string RandomString(Random* rnd, int len) { std::string r; test::RandomString(rnd, len, &r);