2#include "orchestrator.hpp"
4#if !defined(__EMSCRIPTEN__)
8 MmapParser::MmapParser(
10 const CSVFormat& format,
11 const ColNamesPtr& col_names
12 ) : CSVParserDriverBase(format, col_names) {
13 this->_filename = std::string(filename);
14 auto head_and_size = get_csv_head_mmap(filename, error_);
16 this->head_ = std::move(head_and_size.first);
17 this->source_size_ = head_and_size.second;
18 this->resolve_format_from_head(format);
21 this->parse_orchestrator_ = make_csv_parse_orchestrator(
23 this->whitespace_flags(),
31 MmapParser::~MmapParser() =
default;
33 bool MmapParser::finalize_loaded_chunk(
35 std::shared_ptr<void> owner,
39 const size_t base_offset = this->mmap_pos - length;
40 const bool source_exhausted = this->mmap_pos == this->source_size_;
41 CSVParseWindowResult result = this->parse_orchestrator_->parse_window(
47 this->output(), error_
49 if (error_)
return false;
51 if (source_exhausted) {
55 this->mmap_pos -= (length - result.complete_prefix_length);
56 return result.completed_row();
59 size_t MmapParser::read_window_size(
size_t chunk_size)
const noexcept {
60 return this->parse_orchestrator_
61 ? this->parse_orchestrator_->read_window_size(chunk_size)
65 void MmapParser::next(
size_t bytes = CSV_CHUNK_SIZE_DEFAULT) {
79 auto head_owner = std::make_shared<std::string>(std::move(head_));
81 this->mmap_pos += length;
93 if (error_ || this->eof_ || completed_row) {
99 const size_t offset = this->mmap_pos;
100 const size_t remaining = (offset < this->source_size_)
101 ? (this->source_size_ - offset)
103 const size_t length = std::min(
remaining, this->read_window_size(bytes));
112 std::error_code error;
113 auto mmap = mio::make_mmap_source(this->_filename, offset, length, error);
115 fail(error_, CSVErrorCode::MappingFailed,
116 make_mmap_failure_message(this->_filename, offset, length));
117 error_.system = error;
120 auto mmap_owner = std::make_shared<mio::basic_mmap_source<char>>(std::move(
mmap));
121 this->mmap_pos += length;
127 this->finalize_loaded_chunk(chunk,
mmap_owner, length, bytes);
Borrow a row and error state for synchronous, single-pass field access.
The all encompassing namespace.
std::string_view string_view
The string_view class used by this library.