]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. |
2 | // This source code is licensed under the BSD-style license found in the | |
3 | // LICENSE file in the root directory of this source tree. An additional grant | |
4 | // of patent rights can be found in the PATENTS file in the same directory. | |
5 | // | |
6 | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. | |
7 | // Use of this source code is governed by a BSD-style license that can be | |
8 | // found in the LICENSE file. See the AUTHORS file for names of contributors. | |
9 | ||
10 | #include "db/log_writer.h" | |
11 | ||
12 | #include <stdint.h> | |
13 | #include "rocksdb/env.h" | |
14 | #include "util/coding.h" | |
15 | #include "util/crc32c.h" | |
16 | #include "util/file_reader_writer.h" | |
17 | ||
18 | namespace rocksdb { | |
19 | namespace log { | |
20 | ||
21 | Writer::Writer(unique_ptr<WritableFileWriter>&& dest, | |
22 | uint64_t log_number, bool recycle_log_files) | |
23 | : dest_(std::move(dest)), | |
24 | block_offset_(0), | |
25 | log_number_(log_number), | |
26 | recycle_log_files_(recycle_log_files) { | |
27 | for (int i = 0; i <= kMaxRecordType; i++) { | |
28 | char t = static_cast<char>(i); | |
29 | type_crc_[i] = crc32c::Value(&t, 1); | |
30 | } | |
31 | } | |
32 | ||
33 | Writer::~Writer() { | |
34 | } | |
35 | ||
36 | Status Writer::AddRecord(const Slice& slice) { | |
37 | const char* ptr = slice.data(); | |
38 | size_t left = slice.size(); | |
39 | ||
40 | // Header size varies depending on whether we are recycling or not. | |
41 | const int header_size = | |
42 | recycle_log_files_ ? kRecyclableHeaderSize : kHeaderSize; | |
43 | ||
44 | // Fragment the record if necessary and emit it. Note that if slice | |
45 | // is empty, we still want to iterate once to emit a single | |
46 | // zero-length record | |
47 | Status s; | |
48 | bool begin = true; | |
49 | do { | |
50 | const int64_t leftover = kBlockSize - block_offset_; | |
51 | assert(leftover >= 0); | |
52 | if (leftover < header_size) { | |
53 | // Switch to a new block | |
54 | if (leftover > 0) { | |
55 | // Fill the trailer (literal below relies on kHeaderSize and | |
56 | // kRecyclableHeaderSize being <= 11) | |
57 | assert(header_size <= 11); | |
58 | dest_->Append( | |
59 | Slice("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", leftover)); | |
60 | } | |
61 | block_offset_ = 0; | |
62 | } | |
63 | ||
64 | // Invariant: we never leave < header_size bytes in a block. | |
65 | assert(static_cast<int64_t>(kBlockSize - block_offset_) >= header_size); | |
66 | ||
67 | const size_t avail = kBlockSize - block_offset_ - header_size; | |
68 | const size_t fragment_length = (left < avail) ? left : avail; | |
69 | ||
70 | RecordType type; | |
71 | const bool end = (left == fragment_length); | |
72 | if (begin && end) { | |
73 | type = recycle_log_files_ ? kRecyclableFullType : kFullType; | |
74 | } else if (begin) { | |
75 | type = recycle_log_files_ ? kRecyclableFirstType : kFirstType; | |
76 | } else if (end) { | |
77 | type = recycle_log_files_ ? kRecyclableLastType : kLastType; | |
78 | } else { | |
79 | type = recycle_log_files_ ? kRecyclableMiddleType : kMiddleType; | |
80 | } | |
81 | ||
82 | s = EmitPhysicalRecord(type, ptr, fragment_length); | |
83 | ptr += fragment_length; | |
84 | left -= fragment_length; | |
85 | begin = false; | |
86 | } while (s.ok() && left > 0); | |
87 | return s; | |
88 | } | |
89 | ||
90 | Status Writer::EmitPhysicalRecord(RecordType t, const char* ptr, size_t n) { | |
91 | assert(n <= 0xffff); // Must fit in two bytes | |
92 | ||
93 | size_t header_size; | |
94 | char buf[kRecyclableHeaderSize]; | |
95 | ||
96 | // Format the header | |
97 | buf[4] = static_cast<char>(n & 0xff); | |
98 | buf[5] = static_cast<char>(n >> 8); | |
99 | buf[6] = static_cast<char>(t); | |
100 | ||
101 | uint32_t crc = type_crc_[t]; | |
102 | if (t < kRecyclableFullType) { | |
103 | // Legacy record format | |
104 | assert(block_offset_ + kHeaderSize + n <= kBlockSize); | |
105 | header_size = kHeaderSize; | |
106 | } else { | |
107 | // Recyclable record format | |
108 | assert(block_offset_ + kRecyclableHeaderSize + n <= kBlockSize); | |
109 | header_size = kRecyclableHeaderSize; | |
110 | ||
111 | // Only encode low 32-bits of the 64-bit log number. This means | |
112 | // we will fail to detect an old record if we recycled a log from | |
113 | // ~4 billion logs ago, but that is effectively impossible, and | |
114 | // even if it were we'dbe far more likely to see a false positive | |
115 | // on the 32-bit CRC. | |
116 | EncodeFixed32(buf + 7, static_cast<uint32_t>(log_number_)); | |
117 | crc = crc32c::Extend(crc, buf + 7, 4); | |
118 | } | |
119 | ||
120 | // Compute the crc of the record type and the payload. | |
121 | crc = crc32c::Extend(crc, ptr, n); | |
122 | crc = crc32c::Mask(crc); // Adjust for storage | |
123 | EncodeFixed32(buf, crc); | |
124 | ||
125 | // Write the header and the payload | |
126 | Status s = dest_->Append(Slice(buf, header_size)); | |
127 | if (s.ok()) { | |
128 | s = dest_->Append(Slice(ptr, n)); | |
129 | if (s.ok()) { | |
130 | s = dest_->Flush(); | |
131 | } | |
132 | } | |
133 | block_offset_ += header_size + n; | |
134 | return s; | |
135 | } | |
136 | ||
137 | } // namespace log | |
138 | } // namespace rocksdb |