| 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 <stdint.h> |
| 8 |
|
| #include "leveldb/env.h" |
| 9 |
|
| #include "util/coding.h" |
| 10 |
|
| #include "util/crc32c.h" |
| 11 |
|
| |
| 12 |
|
| namespace leveldb { |
| 13 |
|
| namespace log { |
| 14 |
|
| |
| 15 |
8 |
| Writer::Writer(WritableFile* dest) |
| 16 |
8 |
| : dest_(dest), |
| 17 |
8 |
| block_offset_(0) { |
| 18 |
48 |
▶ | for (int i = 0; i <= kMaxRecordType; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i = 0; i <= kMaxRecordType; i++) { |
40 |
8 |
48 |
|
| 19 |
40 |
| char t = static_cast<char>(i); |
| 20 |
40 |
▶ | type_crc_[i] = crc32c::Value(&t, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| type_crc_[i] = crc32c::Value(&t, 1); |
40 |
0 |
40 |
|
| 21 |
|
| } |
| 22 |
8 |
| } |
| 23 |
|
| |
| 24 |
8 |
| Writer::~Writer() { |
| 25 |
8 |
| } |
| 26 |
|
| |
| 27 |
26 |
| Status Writer::AddRecord(const Slice& slice) { |
| 28 |
26 |
| const char* ptr = slice.data(); |
| 29 |
26 |
| size_t left = slice.size(); |
| 30 |
|
| |
| 31 |
|
| // Fragment the record if necessary and emit it. Note that if slice |
| 32 |
|
| // is empty, we still want to iterate once to emit a single |
| 33 |
|
| // zero-length record |
| 34 |
26 |
| Status s; |
| 35 |
26 |
| bool begin = true; |
| 36 |
|
| do { |
| 37 |
26 |
| const int leftover = kBlockSize - block_offset_; |
| 38 |
26 |
▶ | assert(leftover >= 0); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(leftover >= 0); |
0 |
26 |
26 |
|
| 39 |
26 |
▶ | if (leftover < kHeaderSize) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (leftover < kHeaderSize) { |
0 |
26 |
26 |
|
| 40 |
|
| // Switch to a new block |
| 41 |
0 |
▶ | if (leftover > 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (leftover > 0) { |
0 |
0 |
0 |
|
| 42 |
|
| // Fill the trailer (literal below relies on kHeaderSize being 7) |
| 43 |
|
| assert(kHeaderSize == 7); |
| 44 |
0 |
▶ | dest_->Append(Slice("\x00\x00\x00\x00\x00\x00", leftover)); |
Branch: 1 branch
| Branch | True | False | Total |
| dest_->Append(Slice("\x00\x00\x00\x00\x00\x00", leftover)); |
0 |
0 |
0 |
|
| 45 |
|
| } |
| 46 |
0 |
| block_offset_ = 0; |
| 47 |
|
| } |
| 48 |
|
| |
| 49 |
|
| // Invariant: we never leave < kHeaderSize bytes in a block. |
| 50 |
26 |
▶ | assert(kBlockSize - block_offset_ - kHeaderSize >= 0); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(kBlockSize - block_offset_ - kHeaderSize >= 0); |
0 |
26 |
26 |
|
| 51 |
|
| |
| 52 |
26 |
| const size_t avail = kBlockSize - block_offset_ - kHeaderSize; |
| 53 |
26 |
▶ | const size_t fragment_length = (left < avail) ? left : avail; |
Branch: 1 branch
| Branch | True | False | Total |
| const size_t fragment_length = (left < avail) ? left : avail; |
26 |
0 |
26 |
|
| 54 |
|
| |
| 55 |
|
| RecordType type; |
| 56 |
26 |
| const bool end = (left == fragment_length); |
| 57 |
26 |
▶ | if (begin && end) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (begin && end) { |
26 |
0 |
26 |
| if (begin && end) { |
26 |
0 |
26 |
|
| 58 |
26 |
| type = kFullType; |
| 59 |
0 |
▶ | } else if (begin) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (begin) { |
0 |
0 |
0 |
|
| 60 |
0 |
| type = kFirstType; |
| 61 |
0 |
▶ | } else if (end) { |
Branch: 1 branch
| Branch | True | False | Total |
| } else if (end) { |
0 |
0 |
0 |
|
| 62 |
0 |
| type = kLastType; |
| 63 |
|
| } else { |
| 64 |
0 |
| type = kMiddleType; |
| 65 |
|
| } |
| 66 |
|
| |
| 67 |
26 |
▶ | s = EmitPhysicalRecord(type, ptr, fragment_length); |
Branch: 2 branches
| Branch | True | False | Total |
| s = EmitPhysicalRecord(type, ptr, fragment_length); |
26 |
0 |
26 |
| s = EmitPhysicalRecord(type, ptr, fragment_length); |
26 |
0 |
26 |
|
| 68 |
26 |
| ptr += fragment_length; |
| 69 |
26 |
| left -= fragment_length; |
| 70 |
26 |
| begin = false; |
| 71 |
26 |
▶ | } while (s.ok() && left > 0); |
Branch: 3 branches
| Branch | True | False | Total |
| } while (s.ok() && left > 0); |
26 |
0 |
26 |
| } while (s.ok() && left > 0); |
0 |
26 |
26 |
| } while (s.ok() && left > 0); |
0 |
26 |
26 |
|
| 72 |
52 |
| return s; |
| 73 |
|
| } |
| 74 |
|
| |
| 75 |
26 |
| Status Writer::EmitPhysicalRecord(RecordType t, const char* ptr, size_t n) { |
| 76 |
26 |
▶ | assert(n <= 0xffff); // Must fit in two bytes |
Branch: 1 branch
| Branch | True | False | Total |
| assert(n <= 0xffff); // Must fit in two bytes |
0 |
26 |
26 |
|
| 77 |
26 |
▶ | assert(block_offset_ + kHeaderSize + n <= kBlockSize); |
Branch: 1 branch
| Branch | True | False | Total |
| assert(block_offset_ + kHeaderSize + n <= kBlockSize); |
0 |
26 |
26 |
|
| 78 |
|
| |
| 79 |
|
| // Format the header |
| 80 |
|
| char buf[kHeaderSize]; |
| 81 |
26 |
| buf[4] = static_cast<char>(n & 0xff); |
| 82 |
26 |
| buf[5] = static_cast<char>(n >> 8); |
| 83 |
26 |
| buf[6] = static_cast<char>(t); |
| 84 |
|
| |
| 85 |
|
| // Compute the crc of the record type and the payload. |
| 86 |
26 |
▶ | uint32_t crc = crc32c::Extend(type_crc_[t], ptr, n); |
Branch: 1 branch
| Branch | True | False | Total |
| uint32_t crc = crc32c::Extend(type_crc_[t], ptr, n); |
26 |
0 |
26 |
|
| 87 |
26 |
| crc = crc32c::Mask(crc); // Adjust for storage |
| 88 |
26 |
▶ | EncodeFixed32(buf, crc); |
Branch: 1 branch
| Branch | True | False | Total |
| EncodeFixed32(buf, crc); |
26 |
0 |
26 |
|
| 89 |
|
| |
| 90 |
|
| // Write the header and the payload |
| 91 |
26 |
▶ | Status s = dest_->Append(Slice(buf, kHeaderSize)); |
Branch: 1 branch
| Branch | True | False | Total |
| Status s = dest_->Append(Slice(buf, kHeaderSize)); |
26 |
0 |
26 |
|
| 92 |
26 |
▶ | if (s.ok()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s.ok()) { |
26 |
0 |
26 |
|
| 93 |
26 |
▶ | s = dest_->Append(Slice(ptr, n)); |
Branch: 2 branches
| Branch | True | False | Total |
| s = dest_->Append(Slice(ptr, n)); |
26 |
0 |
26 |
| s = dest_->Append(Slice(ptr, n)); |
26 |
0 |
26 |
|
| 94 |
26 |
▶ | if (s.ok()) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (s.ok()) { |
26 |
0 |
26 |
|
| 95 |
26 |
▶ | s = dest_->Flush(); |
Branch: 2 branches
| Branch | True | False | Total |
| s = dest_->Flush(); |
26 |
0 |
26 |
| s = dest_->Flush(); |
26 |
0 |
26 |
|
| 96 |
|
| } |
| 97 |
|
| } |
| 98 |
26 |
| block_offset_ += kHeaderSize + n; |
| 99 |
52 |
| return s; |
| 100 |
|
| } |
| 101 |
|
| |
| 102 |
|
| } |
| 103 |
|
| } |