diff --git a/file/random_access_file_reader.cc b/file/random_access_file_reader.cc index b18cf4c06..03a574254 100644 --- a/file/random_access_file_reader.cc +++ b/file/random_access_file_reader.cc @@ -84,7 +84,7 @@ Status RandomAccessFileReader::Read(uint64_t offset, size_t n, Slice* result, if (aligned_buf == nullptr) { buf.Read(scratch, offset_advance, res_len); } else { - scratch = buf.BufferStart(); + scratch = buf.BufferStart() + offset_advance; aligned_buf->reset(buf.Release()); } } diff --git a/file/random_access_file_reader_test.cc b/file/random_access_file_reader_test.cc index b74c21dba..04b135693 100644 --- a/file/random_access_file_reader_test.cc +++ b/file/random_access_file_reader_test.cc @@ -96,6 +96,34 @@ class RandomAccessFileReaderTest : public testing::Test { } }; +TEST_F(RandomAccessFileReaderTest, ReadDirectIO) { + if (!IsDirectIOSupported()) { + printf("Direct IO is not supported, skip this test\n"); + return; + } + + std::string fname = "read-direct-io"; + Random rand(0); + std::string content; + test::RandomString(&rand, static_cast(alignment()), &content); + Write(fname, content); + + FileOptions opts; + opts.use_direct_reads = true; + std::unique_ptr r; + Read(fname, opts, &r); + ASSERT_TRUE(r->use_direct_io()); + + size_t offset = alignment() / 2; + size_t len = alignment() / 3; + Slice result; + AlignedBuf buf; + for (bool for_compaction : {true, false}) { + ASSERT_OK(r->Read(offset, len, &result, nullptr, &buf, for_compaction)); + ASSERT_EQ(result.ToString(), content.substr(offset, len)); + } +} + TEST_F(RandomAccessFileReaderTest, MultiReadDirectIO) { if (!IsDirectIOSupported()) { printf("Direct IO is not supported, skip this test\n");