Skip to content

Commit 927b241

Browse files
Chongfeng Hufacebook-github-bot
Chongfeng Hu
authored andcommitted
Add new Tablet API to expose optionalSections metadata (facebookincubator#125)
Summary: This new API will unlock use cases such as enumerating all optional sections in the Nimble file. It will also empower `nimble_dump` to be able to provide relevant information about the optional sections. Differential Revision: D67949242
1 parent e098251 commit 927b241

File tree

3 files changed

+62
-13
lines changed

3 files changed

+62
-13
lines changed

dwio/nimble/tablet/TabletReader.cpp

+10-9
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "dwio/nimble/common/Buffer.h"
1919
#include "dwio/nimble/common/EncodingPrimitives.h"
2020
#include "dwio/nimble/common/Exceptions.h"
21+
#include "dwio/nimble/common/Types.h"
2122
#include "dwio/nimble/tablet/Compression.h"
2223
#include "dwio/nimble/tablet/Constants.h"
2324
#include "dwio/nimble/tablet/FooterGenerated.h"
@@ -383,11 +384,11 @@ TabletReader::TabletReader(
383384
for (auto i = 0; i < optionalSections->names()->size(); ++i) {
384385
optionalSections_.insert(std::make_pair(
385386
optionalSections->names()->GetAsString(i)->str(),
386-
std::make_tuple(
387+
MetadataSection{
387388
optionalSections->offsets()->Get(i),
388389
optionalSections->sizes()->Get(i),
389390
static_cast<CompressionType>(
390-
optionalSections->compression_types()->Get(i)))));
391+
optionalSections->compression_types()->Get(i))}));
391392
}
392393
}
393394

@@ -399,9 +400,9 @@ TabletReader::TabletReader(
399400
continue;
400401
}
401402

402-
const auto sectionOffset = std::get<0>(it->second);
403-
const auto sectionSize = std::get<1>(it->second);
404-
const auto sectionCompressionType = std::get<2>(it->second);
403+
const auto sectionOffset = it->second.offset();
404+
const auto sectionSize = it->second.size();
405+
const auto sectionCompressionType = it->second.compressionType();
405406

406407
if (sectionOffset < offset) {
407408
// Section was not read yet. Need to read from file.
@@ -427,7 +428,7 @@ TabletReader::TabletReader(
427428
auto iobuf = std::move(result[i]);
428429
const std::string preload{mustRead[i].label};
429430
auto metadata = std::make_unique<MetadataBuffer>(
430-
memoryPool_, iobuf, std::get<2>(optionalSections_[preload]));
431+
memoryPool_, iobuf, optionalSections_.at(preload).compressionType());
431432
optionalSectionsCache_.wlock()->insert({preload, std::move(metadata)});
432433
}
433434
}
@@ -670,9 +671,9 @@ std::optional<Section> TabletReader::loadOptionalSection(
670671
return std::nullopt;
671672
}
672673

673-
const auto offset = std::get<0>(it->second);
674-
const auto size = std::get<1>(it->second);
675-
const auto compressionType = std::get<2>(it->second);
674+
const auto offset = it->second.offset();
675+
const auto size = it->second.size();
676+
const auto compressionType = it->second.compressionType();
676677

677678
velox::common::Region region{offset, size, name};
678679
folly::IOBuf iobuf;

dwio/nimble/tablet/TabletReader.h

+33-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <span>
1818

1919
#include "dwio/nimble/common/Checksum.h"
20+
#include "dwio/nimble/common/Types.h"
2021
#include "dwio/nimble/common/Vector.h"
2122
#include "folly/Range.h"
2223
#include "folly/Synchronized.h"
@@ -85,6 +86,32 @@ class Section {
8586
MetadataBuffer buffer_;
8687
};
8788

89+
class MetadataSection {
90+
public:
91+
MetadataSection(
92+
uint64_t offset,
93+
uint32_t size,
94+
CompressionType compressionType)
95+
: offset_{offset}, size_{size}, compressionType_{compressionType} {}
96+
97+
uint64_t offset() const {
98+
return offset_;
99+
}
100+
101+
uint32_t size() const {
102+
return size_;
103+
}
104+
105+
CompressionType compressionType() const {
106+
return compressionType_;
107+
}
108+
109+
private:
110+
uint64_t offset_;
111+
uint32_t size_;
112+
CompressionType compressionType_;
113+
};
114+
88115
class Postscript {
89116
public:
90117
uint32_t footerSize() const {
@@ -251,6 +278,11 @@ class TabletReader {
251278
const StripeIdentifier& stripe,
252279
std::span<const uint32_t> streamIdentifiers) const;
253280

281+
const std::unordered_map<std::string, MetadataSection>& optionalSections()
282+
const {
283+
return optionalSections_;
284+
}
285+
254286
std::optional<Section> loadOptionalSection(
255287
const std::string& name,
256288
bool keepCache = false) const;
@@ -349,10 +381,7 @@ class TabletReader {
349381
uint32_t stripeCount_{0};
350382
const uint32_t* stripeRowCounts_{nullptr};
351383
const uint64_t* stripeOffsets_{nullptr};
352-
std::unordered_map<
353-
std::string,
354-
std::tuple<uint64_t, uint32_t, CompressionType>>
355-
optionalSections_;
384+
std::unordered_map<std::string, MetadataSection> optionalSections_;
356385
mutable folly::Synchronized<
357386
std::unordered_map<std::string, std::unique_ptr<MetadataBuffer>>>
358387
optionalSectionsCache_;

dwio/nimble/tablet/tests/TabletTests.cpp

+19
Original file line numberDiff line numberDiff line change
@@ -561,6 +561,23 @@ TEST(TabletTests, OptionalSections) {
561561
file, useChaniedBuffers);
562562
nimble::TabletReader tablet{*pool, &readFile};
563563

564+
ASSERT_EQ(tablet.optionalSections().size(), 3);
565+
ASSERT_TRUE(tablet.optionalSections().contains("section1"));
566+
ASSERT_EQ(
567+
tablet.optionalSections().at("section1").compressionType(),
568+
nimble::CompressionType::Uncompressed);
569+
ASSERT_EQ(tablet.optionalSections().at("section1").size(), random.size());
570+
ASSERT_TRUE(tablet.optionalSections().contains("section2"));
571+
ASSERT_EQ(
572+
tablet.optionalSections().at("section2").compressionType(),
573+
nimble::CompressionType::Uncompressed);
574+
ASSERT_EQ(tablet.optionalSections().at("section2").size(), zeroes.size());
575+
ASSERT_TRUE(tablet.optionalSections().contains("section3"));
576+
ASSERT_EQ(
577+
tablet.optionalSections().at("section3").compressionType(),
578+
nimble::CompressionType::Uncompressed);
579+
ASSERT_EQ(tablet.optionalSections().at("section3").size(), 0);
580+
564581
auto check1 = [&]() {
565582
auto section = tablet.loadOptionalSection("section1");
566583
ASSERT_TRUE(section.has_value());
@@ -607,6 +624,8 @@ TEST(TabletTests, OptionalSectionsEmpty) {
607624
file, useChaniedBuffers);
608625
nimble::TabletReader tablet{*pool, &readFile};
609626

627+
ASSERT_TRUE(tablet.optionalSections().empty());
628+
610629
auto section = tablet.loadOptionalSection("section1");
611630
ASSERT_FALSE(section.has_value());
612631
}

0 commit comments

Comments
 (0)