From 413195edd2990e0a9b4c3500360722cff76e1a0a Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Mon, 9 Oct 2017 23:29:34 +0200 Subject: Support queries that overlap buffers --- src/util/query.cc | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/util/query.cc b/src/util/query.cc index f71b257..8b2b55c 100644 --- a/src/util/query.cc +++ b/src/util/query.cc @@ -15,12 +15,12 @@ std::vector get_lines_starting_with( return std::vector{}; } - posix_fadvise(fileno(file), 0, 0, 1); // FDADVICE_SEQUENTIAL std::vector result; char buffer[16*1024 + 1]; char* start_of_match = nullptr; + std::string overlap; while ( std::size_t n = std::fread(buffer, sizeof(char), @@ -30,14 +30,24 @@ std::vector get_lines_starting_with( (p = static_cast(std::memchr(p, '\n', (buffer + n) - p))); ++p ) { if ( start_of_match != nullptr ) { - result.emplace_back(start_of_match, p - start_of_match); - start_of_match = nullptr; + if ( overlap.empty() ) { + result.emplace_back(start_of_match, p - start_of_match); + start_of_match = nullptr; + } else { + result.emplace_back(overlap.append(buffer, p - buffer)); + start_of_match = nullptr; + overlap.clear(); + } } if ( std::strncmp(substring.c_str(), p+1, substring.size()) == 0 ) { start_of_match = p+1; } } + + if ( start_of_match != nullptr ) { + overlap = std::string(start_of_match, (buffer + n) - start_of_match); + } } std::fclose(file); -- cgit v1.2.3