The file_input_stream interface was messy: it was not fiber safe (e.g., code
doing seek() in the middle of an ongoing read_exactly()), and went against
the PIMPL philosophy.
So this patch removes the file_input_stream class, and replaces it with a
completely different design:
We now have in fstream.hh a global function:
input_stream<char>
make_file_input_stream(
lw_shared_ptr<file> file, uint64_t offset = 0,
uint64_t buffer_size = 8192);
In other words, instead of "seeking" in an input stream, we just open a new
input stream object at a particular offset of the given file. Multiple input
streams might be concurrently active on the same file.
Note how make_file_input_stream now returns a regular "input_stream", not a
subtype, and it can be used just like any normal input_stream to read the stream
starting at the given position.
This patch makes "input_stream" a "final" type: we no longer subclass it in our
code, and we shouldn't in the future because it goes against the PIMPL design
(the subclass should be of the inner workings, like the data_source_impl, not
of input_stream).
Signed-off-by: Nadav Har'El <nyh@cloudius-systems.com>
67 lines
2.1 KiB
C++
67 lines
2.1 KiB
C++
/*
|
|
* This file is open source software, licensed to you under the terms
|
|
* of the Apache License, Version 2.0 (the "License"). See the NOTICE file
|
|
* distributed with this work for additional information regarding copyright
|
|
* ownership. You may not use this file except in compliance with the License.
|
|
*
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing,
|
|
* software distributed under the License is distributed on an
|
|
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
|
* KIND, either express or implied. See the License for the
|
|
* specific language governing permissions and limitations
|
|
* under the License.
|
|
*/
|
|
/*
|
|
* Copyright (C) 2015 Cloudius Systems, Ltd.
|
|
*/
|
|
|
|
// Demonstration of file_input_stream. Don't expect stellar performance
|
|
// since no read-ahead or caching is done yet.
|
|
|
|
#include "core/fstream.hh"
|
|
#include "core/app-template.hh"
|
|
#include "core/shared_ptr.hh"
|
|
#include "core/reactor.hh"
|
|
#include <algorithm>
|
|
|
|
struct reader {
|
|
reader(file f) : is(make_file_input_stream(
|
|
make_lw_shared<file>(std::move(f)), 0, 4096)) {}
|
|
input_stream<char> is;
|
|
size_t count = 0;
|
|
|
|
// for input_stream::consume():
|
|
template <typename Done>
|
|
void operator()(temporary_buffer<char> data, Done&& done) {
|
|
if (data.empty()) {
|
|
done(std::move(data));
|
|
} else {
|
|
count += std::count(data.begin(), data.end(), '\n');
|
|
// FIXME: last line without \n?
|
|
}
|
|
}
|
|
};
|
|
|
|
int main(int ac, char** av) {
|
|
app_template app;
|
|
namespace bpo = boost::program_options;
|
|
app.add_positional_options({
|
|
{ "file", bpo::value<std::string>(), "File to process", 1 },
|
|
});
|
|
app.run(ac, av, [&app] {
|
|
auto fname = app.configuration()["file"].as<std::string>();
|
|
engine().open_file_dma(fname, open_flags::ro | open_flags::create).then([] (file f) {
|
|
auto r = make_shared<reader>(std::move(f));
|
|
r->is.consume(*r).then([r] {
|
|
print("%d lines\n", r->count);
|
|
engine().exit(0);
|
|
});
|
|
});
|
|
});
|
|
}
|
|
|