|
15 | 15 | // specific language governing permissions and limitations |
16 | 16 | // under the License. |
17 | 17 |
|
| 18 | +#include <algorithm> |
18 | 19 | #include <cstdint> |
19 | 20 | #include <cstdio> |
20 | 21 | #include <memory> |
|
23 | 24 |
|
24 | 25 | #include <gtest/gtest.h> |
25 | 26 |
|
| 27 | +#include "arrow/testing/gtest_util.h" |
| 28 | + |
| 29 | +#include "parquet/deprecated_io.h" |
26 | 30 | #include "parquet/exception.h" |
27 | 31 | #include "parquet/platform.h" |
28 | 32 | #include "parquet/test-util.h" |
29 | 33 |
|
30 | 34 | using arrow::default_memory_pool; |
31 | 35 | using arrow::MemoryPool; |
32 | 36 |
|
33 | | -namespace parquet {} // namespace parquet |
| 37 | +namespace parquet { |
| 38 | + |
| 39 | +class MockRandomAccessSource : public RandomAccessSource { |
| 40 | + public: |
| 41 | + MockRandomAccessSource(const uint8_t* data, int64_t size) |
| 42 | + : data_(data), position_(0), size_(size) {} |
| 43 | + |
| 44 | + int64_t Size() const override { return size_; } |
| 45 | + |
| 46 | + int64_t Read(int64_t nbytes, uint8_t* out) override { |
| 47 | + ThrowIfClosed(); |
| 48 | + int64_t bytes_to_read = std::min(nbytes, size_ - position_); |
| 49 | + if (bytes_to_read == 0) { |
| 50 | + return 0; |
| 51 | + } |
| 52 | + memcpy(out, data_ + position_, bytes_to_read); |
| 53 | + position_ += bytes_to_read; |
| 54 | + return bytes_to_read; |
| 55 | + } |
| 56 | + |
| 57 | + std::shared_ptr<Buffer> Read(int64_t nbytes) override { |
| 58 | + ThrowIfClosed(); |
| 59 | + int64_t bytes_to_read = std::min(nbytes, size_ - position_); |
| 60 | + std::shared_ptr<ResizableBuffer> out = |
| 61 | + AllocateBuffer(::arrow::default_memory_pool(), bytes_to_read); |
| 62 | + Read(bytes_to_read, out->mutable_data()); |
| 63 | + return std::move(out); |
| 64 | + } |
| 65 | + |
| 66 | + std::shared_ptr<Buffer> ReadAt(int64_t position, int64_t nbytes) override { |
| 67 | + ThrowIfClosed(); |
| 68 | + position_ = position; |
| 69 | + return Read(nbytes); |
| 70 | + } |
| 71 | + |
| 72 | + int64_t ReadAt(int64_t position, int64_t nbytes, uint8_t* out) override { |
| 73 | + ThrowIfClosed(); |
| 74 | + position_ = position; |
| 75 | + return Read(nbytes, out); |
| 76 | + } |
| 77 | + |
| 78 | + void Close() override { closed_ = true; } |
| 79 | + |
| 80 | + int64_t Tell() override { |
| 81 | + ThrowIfClosed(); |
| 82 | + return position_; |
| 83 | + } |
| 84 | + |
| 85 | + bool closed() const { return closed_; } |
| 86 | + |
| 87 | + private: |
| 88 | + const uint8_t* data_; |
| 89 | + int64_t position_; |
| 90 | + int64_t size_; |
| 91 | + bool closed_ = false; |
| 92 | + |
| 93 | + void ThrowIfClosed() { |
| 94 | + if (closed_) { |
| 95 | + throw ParquetException("file is closed"); |
| 96 | + } |
| 97 | + } |
| 98 | +}; |
| 99 | + |
| 100 | +TEST(ParquetInputWrapper, BasicOperation) { |
| 101 | + std::string data = "some example data"; |
| 102 | + |
| 103 | + auto source = std::unique_ptr<RandomAccessSource>(new MockRandomAccessSource( |
| 104 | + reinterpret_cast<const uint8_t*>(data.data()), static_cast<int64_t>(data.size()))); |
| 105 | + ParquetInputWrapper wrapper(std::move(source)); |
| 106 | + |
| 107 | + ASSERT_FALSE(wrapper.closed()); |
| 108 | + |
| 109 | + int64_t position = -1; |
| 110 | + ASSERT_OK(wrapper.Tell(&position)); |
| 111 | + ASSERT_EQ(0, position); |
| 112 | + |
| 113 | + // Read into memory |
| 114 | + uint8_t buf[4] = {0}; |
| 115 | + int64_t bytes_read = -1; |
| 116 | + ASSERT_OK(wrapper.Read(4, &bytes_read, buf)); |
| 117 | + ASSERT_EQ(4, bytes_read); |
| 118 | + ASSERT_EQ(0, memcmp(buf, data.data(), 4)); |
| 119 | + |
| 120 | + ASSERT_OK(wrapper.Tell(&position)); |
| 121 | + ASSERT_EQ(4, position); |
| 122 | + |
| 123 | + // Seek |
| 124 | + ASSERT_RAISES(NotImplemented, wrapper.Seek(5)); |
| 125 | + |
| 126 | + // Read buffer |
| 127 | + std::shared_ptr<Buffer> buffer; |
| 128 | + ASSERT_OK(wrapper.Read(7, &buffer)); |
| 129 | + ASSERT_EQ(0, memcmp(buffer->data(), data.data() + 4, 7)); |
| 130 | + |
| 131 | + // ReadAt |
| 132 | + ASSERT_OK(wrapper.ReadAt(13, 4, &buffer)); |
| 133 | + ASSERT_EQ(4, buffer->size()); |
| 134 | + ASSERT_EQ(0, memcmp(buffer->data(), data.data() + 13, 4)); |
| 135 | + |
| 136 | + // GetSize |
| 137 | + int64_t size = -1; |
| 138 | + ASSERT_OK(wrapper.GetSize(&size)); |
| 139 | + ASSERT_EQ(static_cast<int64_t>(data.size()), size); |
| 140 | + |
| 141 | + // Close |
| 142 | + ASSERT_OK(wrapper.Close()); |
| 143 | + ASSERT_TRUE(wrapper.closed()); |
| 144 | +} |
| 145 | + |
| 146 | +class MockOutputStream : public OutputStream { |
| 147 | + public: |
| 148 | + MockOutputStream() {} |
| 149 | + |
| 150 | + void Write(const uint8_t* data, int64_t length) override { |
| 151 | + ThrowIfClosed(); |
| 152 | + size_ += length; |
| 153 | + } |
| 154 | + |
| 155 | + void Close() override { closed_ = true; } |
| 156 | + |
| 157 | + int64_t Tell() override { |
| 158 | + ThrowIfClosed(); |
| 159 | + return size_; |
| 160 | + } |
| 161 | + |
| 162 | + bool closed() const { return closed_; } |
| 163 | + |
| 164 | + private: |
| 165 | + int64_t size_ = 0; |
| 166 | + bool closed_ = false; |
| 167 | + |
| 168 | + void ThrowIfClosed() { |
| 169 | + if (closed_) { |
| 170 | + throw ParquetException("file is closed"); |
| 171 | + } |
| 172 | + } |
| 173 | +}; |
| 174 | + |
| 175 | +TEST(ParquetOutputWrapper, BasicOperation) { |
| 176 | + auto stream = std::unique_ptr<OutputStream>(new MockOutputStream); |
| 177 | + ParquetOutputWrapper wrapper(std::move(stream)); |
| 178 | + |
| 179 | + int64_t position = -1; |
| 180 | + ASSERT_OK(wrapper.Tell(&position)); |
| 181 | + ASSERT_EQ(0, position); |
| 182 | + |
| 183 | + std::string data = "food"; |
| 184 | + |
| 185 | + ASSERT_OK(wrapper.Write(reinterpret_cast<const uint8_t*>(data.data()), 4)); |
| 186 | + ASSERT_OK(wrapper.Tell(&position)); |
| 187 | + ASSERT_EQ(4, position); |
| 188 | + |
| 189 | + // Close |
| 190 | + ASSERT_OK(wrapper.Close()); |
| 191 | + ASSERT_TRUE(wrapper.closed()); |
| 192 | + |
| 193 | + // Test catch exceptions |
| 194 | + ASSERT_RAISES(IOError, wrapper.Tell(&position)); |
| 195 | + ASSERT_RAISES(IOError, wrapper.Write(reinterpret_cast<const uint8_t*>(data.data()), 4)); |
| 196 | +} |
| 197 | + |
| 198 | +TEST(ParquetOutputWrapper, DtorCloses) { |
| 199 | + MockOutputStream stream; |
| 200 | + { ParquetOutputWrapper wrapper(&stream); } |
| 201 | + ASSERT_TRUE(stream.closed()); |
| 202 | +} |
| 203 | + |
| 204 | +} // namespace parquet |
0 commit comments