Coverage Report

Created: 2025-07-18 07:11

/src/leveldb/db/log_writer.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style license that can be
3
// found in the LICENSE file. See the AUTHORS file for names of contributors.
4
5
#include "db/log_writer.h"
6
7
#include <cstdint>
8
9
#include "leveldb/env.h"
10
#include "util/coding.h"
11
#include "util/crc32c.h"
12
13
namespace leveldb {
14
namespace log {
15
16
271k
static void InitTypeCrc(uint32_t* type_crc) {
17
1.62M
  for (int i = 0; i <= kMaxRecordType; i++) {
18
1.35M
    char t = static_cast<char>(i);
19
1.35M
    type_crc[i] = crc32c::Value(&t, 1);
20
1.35M
  }
21
271k
}
22
23
271k
Writer::Writer(WritableFile* dest) : dest_(dest), block_offset_(0) {
24
271k
  InitTypeCrc(type_crc_);
25
271k
}
26
27
Writer::Writer(WritableFile* dest, uint64_t dest_length)
28
0
    : dest_(dest), block_offset_(dest_length % kBlockSize) {
29
0
  InitTypeCrc(type_crc_);
30
0
}
31
32
271k
Writer::~Writer() = default;
33
34
2.37M
Status Writer::AddRecord(const Slice& slice) {
35
2.37M
  const char* ptr = slice.data();
36
2.37M
  size_t left = slice.size();
37
38
  // Fragment the record if necessary and emit it.  Note that if slice
39
  // is empty, we still want to iterate once to emit a single
40
  // zero-length record
41
2.37M
  Status s;
42
2.37M
  bool begin = true;
43
2.45M
  do {
44
2.45M
    const int leftover = kBlockSize - block_offset_;
45
2.45M
    assert(leftover >= 0);
46
2.45M
    if (leftover < kHeaderSize) {
47
      // Switch to a new block
48
79.2k
      if (leftover > 0) {
49
        // Fill the trailer (literal below relies on kHeaderSize being 7)
50
408
        static_assert(kHeaderSize == 7, "");
51
408
        dest_->Append(Slice("\x00\x00\x00\x00\x00\x00", leftover));
52
408
      }
53
79.2k
      block_offset_ = 0;
54
79.2k
    }
55
56
    // Invariant: we never leave < kHeaderSize bytes in a block.
57
2.45M
    assert(kBlockSize - block_offset_ - kHeaderSize >= 0);
58
59
2.45M
    const size_t avail = kBlockSize - block_offset_ - kHeaderSize;
60
2.45M
    const size_t fragment_length = (left < avail) ? left : avail;
61
62
2.45M
    RecordType type;
63
2.45M
    const bool end = (left == fragment_length);
64
2.45M
    if (begin && end) {
65
2.37M
      type = kFullType;
66
2.37M
    } else if (begin) {
67
7.02k
      type = kFirstType;
68
78.7k
    } else if (end) {
69
7.02k
      type = kLastType;
70
71.7k
    } else {
71
71.7k
      type = kMiddleType;
72
71.7k
    }
73
74
2.45M
    s = EmitPhysicalRecord(type, ptr, fragment_length);
75
2.45M
    ptr += fragment_length;
76
2.45M
    left -= fragment_length;
77
2.45M
    begin = false;
78
2.45M
  } while (s.ok() && left > 0);
79
2.37M
  return s;
80
2.37M
}
81
82
Status Writer::EmitPhysicalRecord(RecordType t, const char* ptr,
83
2.45M
                                  size_t length) {
84
2.45M
  assert(length <= 0xffff);  // Must fit in two bytes
85
2.45M
  assert(block_offset_ + kHeaderSize + length <= kBlockSize);
86
87
  // Format the header
88
2.45M
  char buf[kHeaderSize];
89
2.45M
  buf[4] = static_cast<char>(length & 0xff);
90
2.45M
  buf[5] = static_cast<char>(length >> 8);
91
2.45M
  buf[6] = static_cast<char>(t);
92
93
  // Compute the crc of the record type and the payload.
94
2.45M
  uint32_t crc = crc32c::Extend(type_crc_[t], ptr, length);
95
2.45M
  crc = crc32c::Mask(crc);  // Adjust for storage
96
2.45M
  EncodeFixed32(buf, crc);
97
98
  // Write the header and the payload
99
2.45M
  Status s = dest_->Append(Slice(buf, kHeaderSize));
100
2.45M
  if (s.ok()) {
101
2.45M
    s = dest_->Append(Slice(ptr, length));
102
2.45M
    if (s.ok()) {
103
2.45M
      s = dest_->Flush();
104
2.45M
    }
105
2.45M
  }
106
2.45M
  block_offset_ += kHeaderSize + length;
107
2.45M
  return s;
108
2.45M
}
109
110
}  // namespace log
111
}  // namespace leveldb