From f5e649453c353c4321556d4df66f7e9327d5a157 Mon Sep 17 00:00:00 2001 From: Levi Tamasi Date: Mon, 8 Jun 2020 15:59:25 -0700 Subject: [PATCH] Add convenience method GetFileMetaDataByNumber (#6940) Summary: The patch adds a convenience method `GetFileMetaDataByNumber` that builds on the `FileLocation` functionality introduced recently (see https://github.com/facebook/rocksdb/pull/6862). This method makes it possible to retrieve the `FileMetaData` directly as opposed to having to go through `LevelFiles` and friends. Pull Request resolved: https://github.com/facebook/rocksdb/pull/6940 Test Plan: `make check` Reviewed By: cheng-chang Differential Revision: D21905946 Pulled By: ltamasi fbshipit-source-id: af99e19de21242b2b4a87594a535c6028d16ee72 --- db/version_set.h | 11 +++++++++++ db/version_set_test.cc | 7 ++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/db/version_set.h b/db/version_set.h index 4a5683dd1..5b8a77523 100644 --- a/db/version_set.h +++ b/db/version_set.h @@ -324,6 +324,17 @@ class VersionStorageInfo { return it->second; } + // REQUIRES: This version has been saved (see VersionSet::SaveTo) + FileMetaData* GetFileMetaDataByNumber(uint64_t file_number) const { + auto location = GetFileLocation(file_number); + + if (!location.IsValid()) { + return nullptr; + } + + return files_[location.GetLevel()][location.GetPosition()]; + } + // REQUIRES: This version has been saved (see VersionSet::SaveTo) using BlobFiles = std::map>; const BlobFiles& GetBlobFiles() const { return blob_files_; } diff --git a/db/version_set_test.cc b/db/version_set_test.cc index d3f3361a9..f5ae29cee 100644 --- a/db/version_set_test.cc +++ b/db/version_set_test.cc @@ -421,7 +421,7 @@ TEST_F(VersionStorageInfoTest, GetOverlappingInputs) { 1, {"i", 0, kTypeValue}, {"j", 0, kTypeValue})); } -TEST_F(VersionStorageInfoTest, FileLocation) { +TEST_F(VersionStorageInfoTest, FileLocationAndMetaDataByNumber) { Add(0, 11U, "1", "2", 5000U); Add(0, 12U, "1", "2", 5000U); @@ -429,13 +429,18 @@ TEST_F(VersionStorageInfoTest, FileLocation) { ASSERT_EQ(vstorage_.GetFileLocation(11U), VersionStorageInfo::FileLocation(0, 0)); + ASSERT_NE(vstorage_.GetFileMetaDataByNumber(11U), nullptr); + ASSERT_EQ(vstorage_.GetFileLocation(12U), VersionStorageInfo::FileLocation(0, 1)); + ASSERT_NE(vstorage_.GetFileMetaDataByNumber(12U), nullptr); ASSERT_EQ(vstorage_.GetFileLocation(7U), VersionStorageInfo::FileLocation(2, 0)); + ASSERT_NE(vstorage_.GetFileMetaDataByNumber(7U), nullptr); ASSERT_FALSE(vstorage_.GetFileLocation(999U).IsValid()); + ASSERT_EQ(vstorage_.GetFileMetaDataByNumber(999U), nullptr); } class VersionStorageInfoTimestampTest : public VersionStorageInfoTestBase {