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