|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include <gtest/gtest.h> |
| 17 | + |
| 18 | +#include "dwio/nimble/common/Buffer.h" |
| 19 | +#include "dwio/nimble/common/tests/TestUtils.h" |
| 20 | +#include "dwio/nimble/index/tests/TabletIndexTestUtils.h" |
| 21 | +#include "dwio/nimble/tablet/TabletReader.h" |
| 22 | +#include "dwio/nimble/tablet/TabletWriter.h" |
| 23 | +#include "dwio/nimble/tablet/tests/TabletTestUtils.h" |
| 24 | +#include "velox/common/file/File.h" |
| 25 | +#include "velox/common/memory/Memory.h" |
| 26 | + |
| 27 | +using namespace facebook; |
| 28 | + |
| 29 | +namespace { |
| 30 | + |
| 31 | +class TabletReaderTest : public ::testing::Test { |
| 32 | + protected: |
| 33 | + using ChunkSpec = nimble::index::test::ChunkSpec; |
| 34 | + using KeyChunkSpec = nimble::index::test::KeyChunkSpec; |
| 35 | + using StreamSpec = nimble::index::test::StreamSpec; |
| 36 | + |
| 37 | + static void SetUpTestCase() { |
| 38 | + // Initialize a fresh global MemoryManager instance for tests to ensure |
| 39 | + // deterministic behavior and isolation across unit tests. |
| 40 | + velox::memory::MemoryManager::testingSetInstance({}); |
| 41 | + } |
| 42 | + |
| 43 | + void SetUp() override { |
| 44 | + // Per-test setup hook. No-op for now, reserved for future initialization. |
| 45 | + } |
| 46 | + |
| 47 | + // Test case describing the expected outcome of a key lookup against the |
| 48 | + // tablet index. If expectedStripeIndex is std::nullopt, no matching stripe |
| 49 | + // should be found for the given key. |
| 50 | + struct LookupTestCase { |
| 51 | + std::string key; // lookup key (serialized) |
| 52 | + std::optional<uint32_t> |
| 53 | + expectedStripeIndex; // expected stripe index or nullopt if no match |
| 54 | + }; |
| 55 | + |
| 56 | + static nimble::Stream createStream( |
| 57 | + nimble::Buffer& buffer, |
| 58 | + const StreamSpec& spec) { |
| 59 | + return nimble::index::test::createStream(buffer, spec); |
| 60 | + } |
| 61 | + |
| 62 | + // Create multiple data Streams from a list of specifications. Reserves |
| 63 | + // capacity to avoid reallocations and uses createStream(...) for each entry. |
| 64 | + static std::vector<nimble::Stream> createStreams( |
| 65 | + nimble::Buffer& buffer, |
| 66 | + const std::vector<StreamSpec>& specs) { |
| 67 | + std::vector<nimble::Stream> streams; |
| 68 | + streams.reserve(specs.size()); |
| 69 | + for (const auto& spec : specs) { |
| 70 | + streams.push_back(createStream(buffer, spec)); |
| 71 | + } |
| 72 | + return streams; |
| 73 | + } |
| 74 | + |
| 75 | + // Memory pools used by writer/reader and test buffers. rootPool_ owns pool_. |
| 76 | + std::shared_ptr<velox::memory::MemoryPool> rootPool_{ |
| 77 | + velox::memory::memoryManager()->addRootPool("TabletTest")}; |
| 78 | + std::shared_ptr<velox::memory::MemoryPool> pool_{ |
| 79 | + rootPool_->addLeafChild("TabletTest")}; |
| 80 | +}; |
| 81 | + |
| 82 | +TEST_F(TabletReaderTest, SimpleStreamReader) { |
| 83 | + std::string file; |
| 84 | + velox::InMemoryWriteFile writeFile(&file); |
| 85 | + |
| 86 | + nimble::TabletIndexConfig indexConfig{ |
| 87 | + .columns = {"col1", "col2"}, |
| 88 | + .sortOrders = {velox::core::kAscNullsFirst, velox::core::kAscNullsFirst}, |
| 89 | + .enforceKeyOrder = true, |
| 90 | + }; |
| 91 | + |
| 92 | + auto tabletWriter = nimble::TabletWriter::create(&writeFile, *pool_, {}); |
| 93 | + |
| 94 | + nimble::Buffer buffer{*pool_}; |
| 95 | + |
| 96 | + { |
| 97 | + auto streams = createStreams( |
| 98 | + buffer, |
| 99 | + { |
| 100 | + {.offset = 0, |
| 101 | + .chunks = |
| 102 | + { |
| 103 | + {.rowCount = 50, .size = 10}, |
| 104 | + {.rowCount = 50, .size = 12}, |
| 105 | + }}, |
| 106 | + }); |
| 107 | + |
| 108 | + tabletWriter->writeStripe(100, std::move(streams)); |
| 109 | + } |
| 110 | + |
| 111 | + tabletWriter->close(); |
| 112 | + writeFile.close(); |
| 113 | + |
| 114 | + nimble::testing::InMemoryTrackableReadFile readFile(file, false); |
| 115 | + auto tablet = nimble::TabletReader::create(&readFile, *pool_); |
| 116 | + |
| 117 | + nimble::test::TabletReaderTestHelper tabletHelper(tablet.get()); |
| 118 | + EXPECT_EQ(tabletHelper.numStripeGroups(), 1) |
| 119 | + << "Expected a single stripe group"; |
| 120 | + |
| 121 | + EXPECT_EQ(tablet->stripeCount(), 1) << "One stripe should have been written"; |
| 122 | + EXPECT_EQ(tablet->tabletRowCount(), 100) |
| 123 | + << "Total row count across all stripes"; |
| 124 | + EXPECT_EQ(tablet->stripeRowCount(0), 100) |
| 125 | + << "Stripe 0 should contain 100 rows"; |
| 126 | +} |
| 127 | + |
| 128 | +} // namespace |
0 commit comments