Coverage Report

Created: 2026-09-28 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/rocksdb/file/line_file_reader.cc
Line
Count
Source
1
//  Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
2
//  This source code is licensed under both the GPLv2 (found in the
3
//  COPYING file in the root directory) and Apache 2.0 License
4
//  (found in the LICENSE.Apache file in the root directory).
5
6
#include "file/line_file_reader.h"
7
8
#include <cstring>
9
10
#include "monitoring/iostats_context_imp.h"
11
12
namespace ROCKSDB_NAMESPACE {
13
14
IOStatus LineFileReader::Create(const std::shared_ptr<FileSystem>& fs,
15
                                const std::string& fname,
16
                                const FileOptions& file_opts,
17
                                std::unique_ptr<LineFileReader>* reader,
18
                                IODebugContext* dbg,
19
0
                                RateLimiter* rate_limiter) {
20
0
  std::unique_ptr<FSSequentialFile> file;
21
0
  IOStatus io_s = fs->NewSequentialFile(fname, file_opts, &file, dbg);
22
0
  if (io_s.ok()) {
23
0
    reader->reset(new LineFileReader(
24
0
        std::move(file), fname, nullptr,
25
0
        std::vector<std::shared_ptr<EventListener>>{}, rate_limiter));
26
0
  }
27
0
  return io_s;
28
0
}
29
30
bool LineFileReader::ReadLine(std::string* out,
31
28.5M
                              Env::IOPriority rate_limiter_priority) {
32
28.5M
  assert(out);
33
28.5M
  if (!io_status_.ok()) {
34
    // Status should be checked (or permit unchecked) any time we return false.
35
0
    io_status_.MustCheck();
36
0
    return false;
37
0
  }
38
28.5M
  out->clear();
39
28.6M
  for (;;) {
40
    // Look for line delimiter
41
28.6M
    const char* found = static_cast<const char*>(
42
28.6M
        std::memchr(buf_begin_, '\n', buf_end_ - buf_begin_));
43
28.6M
    if (found) {
44
28.4M
      size_t len = found - buf_begin_;
45
28.4M
      out->append(buf_begin_, len);
46
28.4M
      buf_begin_ += len + /*delim*/ 1;
47
28.4M
      ++line_number_;
48
28.4M
      return true;
49
28.4M
    }
50
276k
    if (at_eof_) {
51
92.2k
      io_status_.MustCheck();
52
92.2k
      return false;
53
92.2k
    }
54
    // else flush and reload buffer
55
184k
    out->append(buf_begin_, buf_end_ - buf_begin_);
56
184k
    Slice result;
57
184k
    io_status_ =
58
184k
        sfr_.Read(buf_.size(), &result, buf_.data(), rate_limiter_priority);
59
184k
    IOSTATS_ADD(bytes_read, result.size());
60
184k
    if (!io_status_.ok()) {
61
0
      io_status_.MustCheck();
62
0
      return false;
63
0
    }
64
184k
    if (result.size() != buf_.size()) {
65
      // The obscure way of indicating EOF
66
92.2k
      at_eof_ = true;
67
92.2k
    }
68
184k
    buf_begin_ = result.data();
69
184k
    buf_end_ = result.data() + result.size();
70
184k
  }
71
28.5M
}
72
73
}  // namespace ROCKSDB_NAMESPACE