Coverage Report

Created: 2023-06-07 06:28

/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
293k
static void InitTypeCrc(uint32_t* type_crc) {
17
1.76M
  for (int i = 0; i <= kMaxRecordType; i++) {
18
1.46M
    char t = static_cast<char>(i);
19
1.46M
    type_crc[i] = crc32c::Value(&t, 1);
20
1.46M
  }
21
293k
}
22
23
293k
Writer::Writer(WritableFile* dest) : dest_(dest), block_offset_(0) {
24
293k
  InitTypeCrc(type_crc_);
25
293k
}
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
293k
Writer::~Writer() = default;
33
34
2.23M
Status Writer::AddRecord(const Slice& slice) {
35
2.23M
  const char* ptr = slice.data();
36
2.23M
  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.23M
  Status s;
42
2.23M
  bool begin = true;
43
2.29M
  do {
44
2.29M
    const int leftover = kBlockSize - block_offset_;
45
2.29M
    assert(leftover >= 0);
46
2.29M
    if (leftover < kHeaderSize) {
47
      // Switch to a new block
48
57.5k
      if (leftover > 0) {
49
        // Fill the trailer (literal below relies on kHeaderSize being 7)
50
376
        static_assert(kHeaderSize == 7, "");
51
376
        dest_->Append(Slice("\x00\x00\x00\x00\x00\x00", leftover));
52
376
      }
53
57.5k
      block_offset_ = 0;
54
57.5k
    }
55
56
    // Invariant: we never leave < kHeaderSize bytes in a block.
57
2.29M
    assert(kBlockSize - block_offset_ - kHeaderSize >= 0);
58
59
2.29M
    const size_t avail = kBlockSize - block_offset_ - kHeaderSize;
60
2.29M
    const size_t fragment_length = (left < avail) ? left : avail;
61
62
2.29M
    RecordType type;
63
2.29M
    const bool end = (left == fragment_length);
64
2.29M
    if (begin && end) {
65
2.23M
      type = kFullType;
66
2.23M
    } else if (begin) {
67
6.77k
      type = kFirstType;
68
57.1k
    } else if (end) {
69
6.77k
      type = kLastType;
70
50.3k
    } else {
71
50.3k
      type = kMiddleType;
72
50.3k
    }
73
74
2.29M
    s = EmitPhysicalRecord(type, ptr, fragment_length);
75
2.29M
    ptr += fragment_length;
76
2.29M
    left -= fragment_length;
77
2.29M
    begin = false;
78
2.29M
  } while (s.ok() && left > 0);
79
2.23M
  return s;
80
2.23M
}
81
82
Status Writer::EmitPhysicalRecord(RecordType t, const char* ptr,
83
2.29M
                                  size_t length) {
84
2.29M
  assert(length <= 0xffff);  // Must fit in two bytes
85
2.29M
  assert(block_offset_ + kHeaderSize + length <= kBlockSize);
86
87
  // Format the header
88
2.29M
  char buf[kHeaderSize];
89
2.29M
  buf[4] = static_cast<char>(length & 0xff);
90
2.29M
  buf[5] = static_cast<char>(length >> 8);
91
2.29M
  buf[6] = static_cast<char>(t);
92
93
  // Compute the crc of the record type and the payload.
94
2.29M
  uint32_t crc = crc32c::Extend(type_crc_[t], ptr, length);
95
2.29M
  crc = crc32c::Mask(crc);  // Adjust for storage
96
2.29M
  EncodeFixed32(buf, crc);
97
98
  // Write the header and the payload
99
2.29M
  Status s = dest_->Append(Slice(buf, kHeaderSize));
100
2.29M
  if (s.ok()) {
101
2.29M
    s = dest_->Append(Slice(ptr, length));
102
2.29M
    if (s.ok()) {
103
2.29M
      s = dest_->Flush();
104
2.29M
    }
105
2.29M
  }
106
2.29M
  block_offset_ += kHeaderSize + length;
107
2.29M
  return s;
108
2.29M
}
109
110
}  // namespace log
111
}  // namespace leveldb