sstables: random_access_reader: move functions out of line

These are not good candidates for inlining.

Signed-off-by: Benny Halevy <bhalevy@scylladb.com>
This commit is contained in:
Benny Halevy
2020-06-29 12:58:08 +03:00
parent 8e3ecc30a9
commit 0bb1c0f37d
3 changed files with 85 additions and 45 deletions

View File

@@ -549,6 +549,7 @@ scylla_core = (['database.cc',
'sstables/prepended_input_stream.cc',
'sstables/m_format_read_helpers.cc',
'sstables/sstable_directory.cc',
'sstables/random_access_reader.cc',
'transport/cql_protocol_extension.cc',
'transport/event.cc',
'transport/event_notifier.cc',

View File

@@ -0,0 +1,77 @@
/*
* Copyright (C) 2020 ScyllaDB
*/
/*
* This file is part of Scylla.
*
* Scylla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scylla is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Scylla. If not, see <http://www.gnu.org/licenses/>.
*/
#include <exception>
#include "sstables/random_access_reader.hh"
#include "utils/disk-error-handler.hh"
#include "log.hh"
namespace sstables {
extern logging::logger sstlog;
future <temporary_buffer<char>> random_access_reader::read_exactly(size_t n) {
return _in->read_exactly(n);
}
void random_access_reader::seek(uint64_t pos) {
if (_in) {
// Future is waited on indirectly in `close()` (via `_close_gate`).
// FIXME: error handling
(void)seastar::with_gate(_close_gate, [in = std::move(_in)]() mutable {
auto fut = in->close();
return fut.then([in = std::move(in)] {});
});
}
_in = std::make_unique < input_stream < char >> (open_at(pos));
}
future<> random_access_reader::close() {
return _close_gate.close().then([this] {
return _in->close();
});
}
file_random_access_reader::file_random_access_reader(file f, uint64_t file_size, size_t buffer_size, unsigned read_ahead)
: _file(std::move(f)), _file_size(file_size), _buffer_size(buffer_size), _read_ahead(read_ahead) {
seek(0);
}
input_stream<char> file_random_access_reader::open_at(uint64_t pos) {
auto len = _file_size - pos;
file_input_stream_options options;
options.buffer_size = _buffer_size;
options.read_ahead = _read_ahead;
return make_file_input_stream(_file, pos, len, std::move(options));
}
future<> file_random_access_reader::close() {
return random_access_reader::close().finally([this] {
return _file.close().handle_exception([save = _file](auto ep) {
sstlog.warn("sstable close failed: {}", ep);
general_disk_error();
});
});
}
}

View File

@@ -26,15 +26,10 @@
#include <seastar/core/gate.hh>
#include <seastar/core/iostream.hh>
#include <seastar/core/temporary_buffer.hh>
#include <seastar/core/temporary_buffer.hh>
#include "utils/disk-error-handler.hh"
#include "log.hh"
#include "seastarx.hh"
namespace sstables {
extern logging::logger sstlog;
class random_access_reader {
std::unique_ptr <input_stream<char>> _in;
seastar::gate _close_gate;
@@ -42,29 +37,13 @@ protected:
virtual input_stream<char> open_at(uint64_t pos) = 0;
public:
future <temporary_buffer<char>> read_exactly(size_t n) {
return _in->read_exactly(n);
}
future <temporary_buffer<char>> read_exactly(size_t n);
void seek(uint64_t pos) {
if (_in) {
// Future is waited on indirectly in `close()` (via `_close_gate`).
// FIXME: error handling
(void)seastar::with_gate(_close_gate, [in = std::move(_in)]() mutable {
auto fut = in->close();
return fut.then([in = std::move(in)] {});
});
}
_in = std::make_unique < input_stream < char >> (open_at(pos));
}
void seek(uint64_t pos);
bool eof() const { return _in->eof(); }
virtual future<> close() {
return _close_gate.close().then([this] {
return _in->close();
});
}
virtual future<> close();
virtual ~random_access_reader() {}
};
@@ -75,28 +54,11 @@ class file_random_access_reader : public random_access_reader {
size_t _buffer_size;
unsigned _read_ahead;
public:
virtual input_stream<char> open_at(uint64_t pos) override {
auto len = _file_size - pos;
file_input_stream_options options;
options.buffer_size = _buffer_size;
options.read_ahead = _read_ahead;
virtual input_stream<char> open_at(uint64_t pos) override;
return make_file_input_stream(_file, pos, len, std::move(options));
}
explicit file_random_access_reader(file f, uint64_t file_size, size_t buffer_size = 8192, unsigned read_ahead = 4);
explicit file_random_access_reader(file f, uint64_t file_size, size_t buffer_size = 8192, unsigned read_ahead = 4)
: _file(std::move(f)), _file_size(file_size), _buffer_size(buffer_size), _read_ahead(read_ahead) {
seek(0);
}
virtual future<> close() override {
return random_access_reader::close().finally([this] {
return _file.close().handle_exception([save = _file](auto ep) {
sstlog.warn("sstable close failed: {}", ep);
general_disk_error();
});
});
}
virtual future<> close() override;
};
}