/Users/deen/code/yugabyte-db/src/yb/rocksdb/db/log_writer.h
Line | Count | Source |
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 | | // The following only applies to changes made to this file as part of YugaByte development. |
7 | | // |
8 | | // Portions Copyright (c) YugaByte, Inc. |
9 | | // |
10 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
11 | | // in compliance with the License. You may obtain a copy of the License at |
12 | | // |
13 | | // http://www.apache.org/licenses/LICENSE-2.0 |
14 | | // |
15 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
16 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
17 | | // or implied. See the License for the specific language governing permissions and limitations |
18 | | // under the License. |
19 | | // |
20 | | // Copyright (c) 2011 The LevelDB Authors. All rights reserved. |
21 | | // Use of this source code is governed by a BSD-style license that can be |
22 | | // found in the LICENSE file. See the AUTHORS file for names of contributors. |
23 | | #pragma once |
24 | | |
25 | | #include <stdint.h> |
26 | | |
27 | | #include <memory> |
28 | | |
29 | | #include "yb/rocksdb/db/log_format.h" |
30 | | #include "yb/util/slice.h" |
31 | | #include "yb/rocksdb/status.h" |
32 | | |
33 | | namespace rocksdb { |
34 | | |
35 | | class WritableFileWriter; |
36 | | |
37 | | using std::unique_ptr; |
38 | | |
39 | | namespace log { |
40 | | |
41 | | /** |
42 | | * Writer is a general purpose log stream writer. It provides an append-only |
43 | | * abstraction for writing data. The details of the how the data is written is |
44 | | * handled by the WriteableFile sub-class implementation. |
45 | | * |
46 | | * File format: |
47 | | * |
48 | | * File is broken down into variable sized records. The format of each record |
49 | | * is described below. |
50 | | * +-----+-------------+--+----+----------+------+-- ... ----+ |
51 | | * File | r0 | r1 |P | r2 | r3 | r4 | | |
52 | | * +-----+-------------+--+----+----------+------+-- ... ----+ |
53 | | * <--- kBlockSize ------>|<-- kBlockSize ------>| |
54 | | * rn = variable size records |
55 | | * P = Padding |
56 | | * |
57 | | * Data is written out in kBlockSize chunks. If next record does not fit |
58 | | * into the space left, the leftover space will be padded with \0. |
59 | | * |
60 | | * Legacy record format: |
61 | | * |
62 | | * +---------+-----------+-----------+--- ... ---+ |
63 | | * |CRC (4B) | Size (2B) | Type (1B) | Payload | |
64 | | * +---------+-----------+-----------+--- ... ---+ |
65 | | * |
66 | | * CRC = 32bit hash computed over the payload using CRC |
67 | | * Size = Length of the payload data |
68 | | * Type = Type of record |
69 | | * (kZeroType, kFullType, kFirstType, kLastType, kMiddleType ) |
70 | | * The type is used to group a bunch of records together to represent |
71 | | * blocks that are larger than kBlockSize |
72 | | * Payload = Byte stream as long as specified by the payload size |
73 | | * |
74 | | * Recyclable record format: |
75 | | * |
76 | | * +---------+-----------+-----------+----------------+--- ... ---+ |
77 | | * |CRC (4B) | Size (2B) | Type (1B) | Log number (4B)| Payload | |
78 | | * +---------+-----------+-----------+----------------+--- ... ---+ |
79 | | * |
80 | | * Same as above, with the addition of |
81 | | * Log number = 32bit log file number, so that we can distinguish between |
82 | | * records written by the most recent log writer vs a previous one. |
83 | | */ |
84 | | class Writer { |
85 | | public: |
86 | | // Create a writer that will append data to "*dest". |
87 | | // "*dest" must be initially empty. |
88 | | // "*dest" must remain live while this Writer is in use. |
89 | | explicit Writer(unique_ptr<WritableFileWriter>&& dest, |
90 | | uint64_t log_number, bool recycle_log_files); |
91 | | ~Writer(); |
92 | | |
93 | | Status AddRecord(const Slice& slice); |
94 | | |
95 | 708k | WritableFileWriter* file() { return dest_.get(); } |
96 | 12 | const WritableFileWriter* file() const { return dest_.get(); } |
97 | | |
98 | | private: |
99 | | unique_ptr<WritableFileWriter> dest_; |
100 | | size_t block_offset_; // Current offset in block |
101 | | uint64_t log_number_; |
102 | | bool recycle_log_files_; |
103 | | |
104 | | // crc32c values for all supported record types. These are |
105 | | // pre-computed to reduce the overhead of computing the crc of the |
106 | | // record type stored in the header. |
107 | | uint32_t type_crc_[kMaxRecordType + 1]; |
108 | | |
109 | | Status EmitPhysicalRecord(RecordType type, const char* ptr, size_t length); |
110 | | |
111 | | // No copying allowed |
112 | | Writer(const Writer&); |
113 | | void operator=(const Writer&); |
114 | | }; |
115 | | |
116 | | } // namespace log |
117 | | } // namespace rocksdb |