YugabyteDB (2.13.1.0-b60, 21121d69985fbf76aa6958d8f04a9bfa936293b5)

Coverage Report

Created: 2022-03-22 16:43

/Users/deen/code/yugabyte-db/src/yb/rocksdb/utilities/write_batch_with_index.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
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
6
// Use of this source code is governed by a BSD-style license that can be
7
// found in the LICENSE file. See the AUTHORS file for names of contributors.
8
//
9
// The following only applies to changes made to this file as part of YugaByte development.
10
//
11
// Portions Copyright (c) YugaByte, Inc.
12
//
13
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
14
// in compliance with the License.  You may obtain a copy of the License at
15
//
16
// http://www.apache.org/licenses/LICENSE-2.0
17
//
18
// Unless required by applicable law or agreed to in writing, software distributed under the License
19
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
20
// or implied.  See the License for the specific language governing permissions and limitations
21
// under the License.
22
//
23
// A WriteBatchWithIndex with a binary searchable index built for all the keys
24
// inserted.
25
#ifndef ROCKSDB_INCLUDE_ROCKSDB_UTILITIES_WRITE_BATCH_WITH_INDEX_H
26
#define ROCKSDB_INCLUDE_ROCKSDB_UTILITIES_WRITE_BATCH_WITH_INDEX_H
27
28
#pragma once
29
30
#ifndef ROCKSDB_LITE
31
32
#include <string>
33
34
#include "yb/rocksdb/comparator.h"
35
#include "yb/rocksdb/iterator.h"
36
#include "yb/util/slice.h"
37
#include "yb/rocksdb/status.h"
38
#include "yb/rocksdb/write_batch.h"
39
#include "yb/rocksdb/write_batch_base.h"
40
41
namespace rocksdb {
42
43
class ColumnFamilyHandle;
44
class Comparator;
45
class DB;
46
struct ReadOptions;
47
struct DBOptions;
48
49
enum WriteType {
50
  kPutRecord,
51
  kMergeRecord,
52
  kDeleteRecord,
53
  kSingleDeleteRecord,
54
  kLogDataRecord
55
};
56
57
// an entry for Put, Merge, Delete, or SingleDelete entry for write batches.
58
// Used in WBWIIterator.
59
struct WriteEntry {
60
  WriteType type;
61
  Slice key;
62
  Slice value;
63
};
64
65
// Iterator of one column family out of a WriteBatchWithIndex.
66
class WBWIIterator {
67
 public:
68
501k
  virtual ~WBWIIterator() {}
69
70
  virtual bool Valid() const = 0;
71
72
  virtual void SeekToFirst() = 0;
73
74
  virtual void SeekToLast() = 0;
75
76
  virtual void Seek(const Slice& key) = 0;
77
78
  virtual void Next() = 0;
79
80
  virtual void Prev() = 0;
81
82
  // the return WriteEntry is only valid until the next mutation of
83
  // WriteBatchWithIndex
84
  virtual WriteEntry Entry() const = 0;
85
86
  virtual Status status() const = 0;
87
};
88
89
// A WriteBatchWithIndex with a binary searchable index built for all the keys
90
// inserted.
91
// In Put(), Merge() Delete(), or SingleDelete(), the same function of the
92
// wrapped will be called. At the same time, indexes will be built.
93
// By calling GetWriteBatch(), a user will get the WriteBatch for the data
94
// they inserted, which can be used for DB::Write().
95
// A user can call NewIterator() to create an iterator.
96
class WriteBatchWithIndex : public WriteBatchBase {
97
 public:
98
  // backup_index_comparator: the backup comparator used to compare keys
99
  // within the same column family, if column family is not given in the
100
  // interface, or we can't find a column family from the column family handle
101
  // passed in, backup_index_comparator will be used for the column family.
102
  // reserved_bytes: reserved bytes in underlying WriteBatch
103
  // overwrite_key: if true, overwrite the key in the index when inserting
104
  //                the same key as previously, so iterator will never
105
  //                show two entries with the same key.
106
  explicit WriteBatchWithIndex(
107
      const Comparator* backup_index_comparator = BytewiseComparator(),
108
      size_t reserved_bytes = 0, bool overwrite_key = false);
109
  virtual ~WriteBatchWithIndex();
110
111
  using WriteBatchBase::Put;
112
  void Put(ColumnFamilyHandle* column_family, const Slice& key,
113
           const Slice& value) override;
114
115
  void Put(const Slice& key, const Slice& value) override;
116
117
  using WriteBatchBase::Merge;
118
  void Merge(ColumnFamilyHandle* column_family, const Slice& key,
119
             const Slice& value) override;
120
121
  void Merge(const Slice& key, const Slice& value) override;
122
123
  using WriteBatchBase::Delete;
124
  void Delete(ColumnFamilyHandle* column_family, const Slice& key) override;
125
  void Delete(const Slice& key) override;
126
127
  using WriteBatchBase::SingleDelete;
128
  void SingleDelete(ColumnFamilyHandle* column_family,
129
                    const Slice& key) override;
130
  void SingleDelete(const Slice& key) override;
131
132
  using WriteBatchBase::PutLogData;
133
  void PutLogData(const Slice& blob) override;
134
135
  using WriteBatchBase::Clear;
136
  void Clear() override;
137
138
  using WriteBatchBase::GetWriteBatch;
139
  WriteBatch* GetWriteBatch() override;
140
141
  // Create an iterator of a column family. User can call iterator.Seek() to
142
  // search to the next entry of or after a key. Keys will be iterated in the
143
  // order given by index_comparator. For multiple updates on the same key,
144
  // each update will be returned as a separate entry, in the order of update
145
  // time.
146
  //
147
  // The returned iterator should be deleted by the caller.
148
  WBWIIterator* NewIterator(ColumnFamilyHandle* column_family);
149
  // Create an iterator of the default column family.
150
  WBWIIterator* NewIterator();
151
152
  // Will create a new Iterator that will use WBWIIterator as a delta and
153
  // base_iterator as base.
154
  //
155
  // This function is only supported if the WriteBatchWithIndex was
156
  // constructed with overwrite_key=true.
157
  //
158
  // The returned iterator should be deleted by the caller.
159
  // The base_iterator is now 'owned' by the returned iterator. Deleting the
160
  // returned iterator will also delete the base_iterator.
161
  Iterator* NewIteratorWithBase(ColumnFamilyHandle* column_family,
162
                                Iterator* base_iterator);
163
  // default column family
164
  Iterator* NewIteratorWithBase(Iterator* base_iterator);
165
166
  // Similar to DB::Get() but will only read the key from this batch.
167
  // If the batch does not have enough data to resolve Merge operations,
168
  // MergeInProgress status may be returned.
169
  Status GetFromBatch(ColumnFamilyHandle* column_family,
170
                      const DBOptions& options, const Slice& key,
171
                      std::string* value);
172
173
  // Similar to previous function but does not require a column_family.
174
  // Note:  An InvalidArgument status will be returned if there are any Merge
175
  // operators for this key.  Use previous method instead.
176
  Status GetFromBatch(const DBOptions& options, const Slice& key,
177
                      std::string* value) {
178
    return GetFromBatch(nullptr, options, key, value);
179
  }
180
181
  // Similar to DB::Get() but will also read writes from this batch.
182
  //
183
  // This function will query both this batch and the DB and then merge
184
  // the results using the DB's merge operator (if the batch contains any
185
  // merge requests).
186
  //
187
  // Setting read_options.snapshot will affect what is read from the DB
188
  // but will NOT change which keys are read from the batch (the keys in
189
  // this batch do not yet belong to any snapshot and will be fetched
190
  // regardless).
191
  Status GetFromBatchAndDB(DB* db, const ReadOptions& read_options,
192
                           const Slice& key, std::string* value);
193
  Status GetFromBatchAndDB(DB* db, const ReadOptions& read_options,
194
                           ColumnFamilyHandle* column_family, const Slice& key,
195
                           std::string* value);
196
197
  // Records the state of the batch for future calls to RollbackToSavePoint().
198
  // May be called multiple times to set multiple save points.
199
  void SetSavePoint() override;
200
201
  // Remove all entries in this batch (Put, Merge, Delete, SingleDelete,
202
  // PutLogData) since the most recent call to SetSavePoint() and removes the
203
  // most recent save point.
204
  // If there is no previous call to SetSavePoint(), behaves the same as
205
  // Clear().
206
  //
207
  // Calling RollbackToSavePoint invalidates any open iterators on this batch.
208
  //
209
  // Returns Status::OK() on success,
210
  //         STATUS(NotFound, "") if no previous call to SetSavePoint(),
211
  //         or other Status on corruption.
212
  Status RollbackToSavePoint() override;
213
214
 private:
215
  struct Rep;
216
  Rep* rep;
217
};
218
219
}  // namespace rocksdb
220
221
#endif  // !ROCKSDB_LITE
222
223
#endif // ROCKSDB_INCLUDE_ROCKSDB_UTILITIES_WRITE_BATCH_WITH_INDEX_H