From dcb6a3be4e07c46a5219651b8774149e2f4d1508 Mon Sep 17 00:00:00 2001 From: Jay Zhuang Date: Fri, 15 Jul 2022 14:42:00 -0700 Subject: [PATCH] Add helper function to get debug type name (#10243) Summary: Pull Request resolved: https://github.com/facebook/rocksdb/pull/10243 Reviewed By: riversand963 Differential Revision: D37370236 Pulled By: jay-zhuang fbshipit-source-id: 6e7a6fadf45fdfb5afe97b3f6fe4acf1260d4a86 --- db/db_basic_test.cc | 35 ++++++++++++++++++++------- db/dbformat.h | 4 +++- include/rocksdb/utilities/debug.h | 3 +-- utilities/debug.cc | 40 +++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 12 deletions(-) diff --git a/db/db_basic_test.cc b/db/db_basic_test.cc index 2fda22cc3..6b5f61c96 100644 --- a/db/db_basic_test.cc +++ b/db/db_basic_test.cc @@ -2618,13 +2618,20 @@ TEST_F(DBBasicTest, GetAllKeyVersions) { ASSERT_OK(Delete(std::to_string(i))); } std::vector key_versions; - ASSERT_OK(ROCKSDB_NAMESPACE::GetAllKeyVersions( - db_, Slice(), Slice(), std::numeric_limits::max(), - &key_versions)); + ASSERT_OK(GetAllKeyVersions(db_, Slice(), Slice(), + std::numeric_limits::max(), + &key_versions)); ASSERT_EQ(kNumInserts + kNumDeletes + kNumUpdates, key_versions.size()); - ASSERT_OK(ROCKSDB_NAMESPACE::GetAllKeyVersions( - db_, handles_[0], Slice(), Slice(), std::numeric_limits::max(), - &key_versions)); + for (size_t i = 0; i < kNumInserts + kNumDeletes + kNumUpdates; i++) { + if (i % 3 == 0) { + ASSERT_EQ(key_versions[i].GetTypeName(), "TypeDeletion"); + } else { + ASSERT_EQ(key_versions[i].GetTypeName(), "TypeValue"); + } + } + ASSERT_OK(GetAllKeyVersions(db_, handles_[0], Slice(), Slice(), + std::numeric_limits::max(), + &key_versions)); ASSERT_EQ(kNumInserts + kNumDeletes + kNumUpdates, key_versions.size()); // Check non-default column family @@ -2637,11 +2644,21 @@ TEST_F(DBBasicTest, GetAllKeyVersions) { for (size_t i = 0; i + 1 != kNumDeletes; ++i) { ASSERT_OK(Delete(1, std::to_string(i))); } - ASSERT_OK(ROCKSDB_NAMESPACE::GetAllKeyVersions( - db_, handles_[1], Slice(), Slice(), std::numeric_limits::max(), - &key_versions)); + ASSERT_OK(GetAllKeyVersions(db_, handles_[1], Slice(), Slice(), + std::numeric_limits::max(), + &key_versions)); ASSERT_EQ(kNumInserts + kNumDeletes + kNumUpdates - 3, key_versions.size()); } + +TEST_F(DBBasicTest, ValueTypeString) { + KeyVersion key_version; + // when adding new type, please also update `value_type_string_map` + for (unsigned char i = ValueType::kTypeDeletion; i < ValueType::kTypeMaxValid; + i++) { + key_version.type = i; + ASSERT_TRUE(key_version.GetTypeName() != "Invalid"); + } +} #endif // !ROCKSDB_LITE TEST_F(DBBasicTest, MultiGetIOBufferOverrun) { diff --git a/db/dbformat.h b/db/dbformat.h index 83bc37d78..51986d6af 100644 --- a/db/dbformat.h +++ b/db/dbformat.h @@ -68,7 +68,9 @@ enum ValueType : unsigned char { kTypeCommitXIDAndTimestamp = 0x15, // WAL only kTypeWideColumnEntity = 0x16, kTypeColumnFamilyWideColumnEntity = 0x17, // WAL only - kMaxValue = 0x7F // Not used for storing records. + kTypeMaxValid, // Should be after the last valid type, only used for + // validation + kMaxValue = 0x7F // Not used for storing records. }; // Defined in dbformat.cc diff --git a/include/rocksdb/utilities/debug.h b/include/rocksdb/utilities/debug.h index a2b6adcb0..0e0526557 100644 --- a/include/rocksdb/utilities/debug.h +++ b/include/rocksdb/utilities/debug.h @@ -25,9 +25,8 @@ struct KeyVersion { std::string user_key; std::string value; SequenceNumber sequence; - // TODO(ajkr): we should provide a helper function that converts the int to a - // string describing the type for easier debugging. int type; + std::string GetTypeName() const; }; // Returns listing of all versions of keys in the provided user key range. diff --git a/utilities/debug.cc b/utilities/debug.cc index 247d717ba..a33c95a6c 100644 --- a/utilities/debug.cc +++ b/utilities/debug.cc @@ -8,9 +8,49 @@ #include "rocksdb/utilities/debug.h" #include "db/db_impl/db_impl.h" +#include "rocksdb/utilities/options_type.h" namespace ROCKSDB_NAMESPACE { +static std::unordered_map value_type_string_map = { + {"TypeDeletion", ValueType::kTypeDeletion}, + {"TypeValue", ValueType::kTypeValue}, + {"TypeMerge", ValueType::kTypeMerge}, + {"TypeLogData", ValueType::kTypeLogData}, + {"TypeColumnFamilyDeletion", ValueType::kTypeColumnFamilyDeletion}, + {"TypeColumnFamilyValue", ValueType::kTypeColumnFamilyValue}, + {"TypeColumnFamilyMerge", ValueType::kTypeColumnFamilyMerge}, + {"TypeSingleDeletion", ValueType::kTypeSingleDeletion}, + {"TypeColumnFamilySingleDeletion", + ValueType::kTypeColumnFamilySingleDeletion}, + {"TypeBeginPrepareXID", ValueType::kTypeBeginPrepareXID}, + {"TypeEndPrepareXID", ValueType::kTypeEndPrepareXID}, + {"TypeCommitXID", ValueType::kTypeCommitXID}, + {"TypeRollbackXID", ValueType::kTypeRollbackXID}, + {"TypeNoop", ValueType::kTypeNoop}, + {"TypeColumnFamilyRangeDeletion", + ValueType::kTypeColumnFamilyRangeDeletion}, + {"TypeRangeDeletion", ValueType::kTypeRangeDeletion}, + {"TypeColumnFamilyBlobIndex", ValueType::kTypeColumnFamilyBlobIndex}, + {"TypeBlobIndex", ValueType::kTypeBlobIndex}, + {"TypeBeginPersistedPrepareXID", ValueType::kTypeBeginPersistedPrepareXID}, + {"TypeBeginUnprepareXID", ValueType::kTypeBeginUnprepareXID}, + {"TypeDeletionWithTimestamp", ValueType::kTypeDeletionWithTimestamp}, + {"TypeCommitXIDAndTimestamp", ValueType::kTypeCommitXIDAndTimestamp}, + {"TypeWideColumnEntity", ValueType::kTypeWideColumnEntity}, + {"TypeColumnFamilyWideColumnEntity", + ValueType::kTypeColumnFamilyWideColumnEntity}}; + +std::string KeyVersion::GetTypeName() const { + std::string type_name; + if (SerializeEnum(value_type_string_map, + static_cast(type), &type_name)) { + return type_name; + } else { + return "Invalid"; + } +} + Status GetAllKeyVersions(DB* db, Slice begin_key, Slice end_key, size_t max_num_ikeys, std::vector* key_versions) {