Files
scylladb/core/fstream.cc
Glauber Costa 861d2625b2 file_stream: proper seek support.
Our file_stream interface supports seek, but when we try to seek to arbitrary
locations that are smaller than an aio-boundary (say, for instance, f->seek(4)),
we will end up not being able to perform the read.

We need to guarantee the reads are aligned, and will then present to the caller
the buffer properly offset.

Signed-off-by: Glauber Costa <glommer@cloudius-systems.com>
2015-02-18 22:56:07 +02:00

36 lines
1.2 KiB
C++

/*
* Copyright (C) 2015 Cloudius Systems, Ltd.
*/
#include "fstream.hh"
#include <new>
#include <malloc.h>
#include <algorithm>
future<temporary_buffer<char>>
file_data_source_impl::get() {
// must align allocation for dma
auto p = ::memalign(std::min<size_t>(_buffer_size, 4096), _buffer_size);
if (!p) {
throw std::bad_alloc();
}
auto q = static_cast<char*>(p);
temporary_buffer<char> buf(q, _buffer_size, make_free_deleter(p));
auto old_pos = _pos;
// dma_read needs to be aligned. It doesn't have to be page-aligned,
// though, and we could get away with something much smaller. However, if
// we start reading in things outside page boundaries, we will end up with
// various pages around, some of them with overlapping ranges. Those would
// be very challenging to cache.
old_pos &= ~4095;
auto front = _pos - old_pos;
_pos += _buffer_size - front;
return _file.dma_read(old_pos, q, _buffer_size).then(
[buf = std::move(buf), front] (size_t size) mutable {
buf.trim(size);
buf.trim_front(front);
return make_ready_future<temporary_buffer<char>>(std::move(buf));
});
}