YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/rocksdb/db/write_batch_internal.h
Line
Count
Source (jump to first uncovered line)
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
24
#ifndef YB_ROCKSDB_DB_WRITE_BATCH_INTERNAL_H
25
#define YB_ROCKSDB_DB_WRITE_BATCH_INTERNAL_H
26
27
#pragma once
28
29
#include <vector>
30
31
#include "yb/rocksdb/db/write_thread.h"
32
#include "yb/rocksdb/types.h"
33
#include "yb/rocksdb/util/autovector.h"
34
#include "yb/rocksdb/write_batch.h"
35
36
#include "yb/util/enums.h"
37
38
namespace rocksdb {
39
40
class MemTable;
41
class FlushScheduler;
42
class ColumnFamilyData;
43
44
class ColumnFamilyMemTables {
45
 public:
46
378k
  virtual ~ColumnFamilyMemTables() {}
47
  virtual bool Seek(uint32_t column_family_id) = 0;
48
  // returns true if the update to memtable should be ignored
49
  // (useful when recovering from log whose updates have already
50
  // been processed)
51
  virtual uint64_t GetLogNumber() const = 0;
52
  virtual MemTable* GetMemTable() const = 0;
53
  virtual ColumnFamilyHandle* GetColumnFamilyHandle() = 0;
54
0
  virtual ColumnFamilyData* current() { return nullptr; }
55
};
56
57
class ColumnFamilyMemTablesDefault : public ColumnFamilyMemTables {
58
 public:
59
  explicit ColumnFamilyMemTablesDefault(MemTable* mem)
60
34
      : ok_(false), mem_(mem) {}
61
62
1.05k
  bool Seek(uint32_t column_family_id) override {
63
1.05k
    ok_ = (column_family_id == 0);
64
1.05k
    return ok_;
65
1.05k
  }
66
67
0
  uint64_t GetLogNumber() const override { return 0; }
68
69
1.05k
  MemTable* GetMemTable() const override {
70
1.05k
    assert(ok_);
71
1.05k
    return mem_;
72
1.05k
  }
73
74
0
  ColumnFamilyHandle* GetColumnFamilyHandle() override { return nullptr; }
75
76
 private:
77
  bool ok_;
78
  MemTable* mem_;
79
};
80
81
YB_DEFINE_ENUM(InsertFlag,
82
               (kFilterDeletes)            // ignore deletes of non-existing keys
83
               (kConcurrentMemtableWrites) // allow concurrent writes
84
               );
85
typedef yb::EnumBitSet<InsertFlag> InsertFlags;
86
87
// WriteBatchInternal provides static methods for manipulating a
88
// WriteBatch that we don't want in the public WriteBatch interface.
89
class WriteBatchInternal {
90
 public:
91
  // WriteBatch methods with column_family_id instead of ColumnFamilyHandle*
92
  static void Put(WriteBatch* batch, uint32_t column_family_id,
93
                  const Slice& key, const Slice& value);
94
95
  static void Put(WriteBatch* batch, uint32_t column_family_id,
96
                  const SliceParts& key, const SliceParts& value);
97
98
  static void Delete(WriteBatch* batch, uint32_t column_family_id,
99
                     const SliceParts& key);
100
101
  static void Delete(WriteBatch* batch, uint32_t column_family_id,
102
                     const Slice& key);
103
104
  static void SingleDelete(WriteBatch* batch, uint32_t column_family_id,
105
                           const SliceParts& key);
106
107
  static void SingleDelete(WriteBatch* batch, uint32_t column_family_id,
108
                           const Slice& key);
109
110
  static void Merge(WriteBatch* batch, uint32_t column_family_id,
111
                    const Slice& key, const Slice& value);
112
113
  static void Merge(WriteBatch* batch, uint32_t column_family_id,
114
                    const SliceParts& key, const SliceParts& value);
115
116
  // Return the number of entries in the batch.
117
  static uint32_t Count(const WriteBatch* batch);
118
119
  // Set the count for the number of entries in the batch.
120
  static void SetCount(WriteBatch* batch, uint32_t n);
121
122
  // Return the seqeunce number for the start of this batch.
123
  static SequenceNumber Sequence(const WriteBatch* batch);
124
125
  // Store the specified number as the sequence number for the start of
126
  // this batch.
127
  static void SetSequence(WriteBatch* batch, SequenceNumber seq);
128
129
  // Returns the offset of the first entry in the batch.
130
  // This offset is only valid if the batch is not empty.
131
  static size_t GetFirstOffset(WriteBatch* batch);
132
133
18.6M
  static Slice Contents(const WriteBatch* batch) {
134
18.6M
    return Slice(batch->rep_);
135
18.6M
  }
136
137
47.2M
  static size_t ByteSize(const WriteBatch* batch) {
138
47.2M
    return batch->rep_.size();
139
47.2M
  }
140
141
  static void SetContents(WriteBatch* batch, const Slice& contents);
142
143
  // Inserts batches[i] into memtable, for i in 0..num_batches-1 inclusive.
144
  //
145
  // If dont_filter_deletes is false AND options.filter_deletes is true
146
  // AND db->KeyMayExist is false, then a Delete won't modify the memtable.
147
  //
148
  // If ignore_missing_column_families == true. WriteBatch
149
  // referencing non-existing column family will be ignored.
150
  // If ignore_missing_column_families == false, processing of the
151
  // batches will be stopped if a reference is found to a non-existing
152
  // column family and InvalidArgument() will be returned.  The writes
153
  // in batches may be only partially applied at that point.
154
  //
155
  // If log_number is non-zero, the memtable will be updated only if
156
  // memtables->GetLogNumber() >= log_number.
157
  //
158
  // If flush_scheduler is non-null, it will be invoked if the memtable
159
  // should be flushed.
160
  //
161
  // Under concurrent use, the caller is responsible for making sure that
162
  // the memtables object itself is thread-local.
163
  static Status InsertInto(const autovector<WriteThread::Writer*>& batches,
164
                           SequenceNumber sequence,
165
                           ColumnFamilyMemTables* memtables,
166
                           FlushScheduler* flush_scheduler,
167
                           bool ignore_missing_column_families = false,
168
                           uint64_t log_number = 0, DB* db = nullptr,
169
                           InsertFlags insert_flags = InsertFlags());
170
171
  // Convenience form of InsertInto when you have only one batch
172
  static Status InsertInto(const WriteBatch* batch,
173
                           ColumnFamilyMemTables* memtables,
174
                           FlushScheduler* flush_scheduler,
175
                           bool ignore_missing_column_families = false,
176
                           uint64_t log_number = 0, DB* db = nullptr,
177
                           InsertFlags insert_flags = InsertFlags());
178
179
  static void Append(WriteBatch* dst, const WriteBatch* src);
180
181
  // Returns the byte size of appending a WriteBatch with ByteSize
182
  // leftByteSize and a WriteBatch with ByteSize rightByteSize
183
  static size_t AppendedByteSize(size_t leftByteSize, size_t rightByteSize);
184
};
185
186
}  // namespace rocksdb
187
188
#endif // YB_ROCKSDB_DB_WRITE_BATCH_INTERNAL_H