]>
git.proxmox.com Git - ceph.git/blob - 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.
9 // A WriteBatchWithIndex with a binary searchable index built for all the keys
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"
27 class ColumnFamilyHandle
;
43 // an entry for Put, Merge, Delete, or SingleDelete entry for write batches.
44 // Used in WBWIIterator.
51 // Iterator of one column family out of a WriteBatchWithIndex.
54 virtual ~WBWIIterator() {}
56 virtual bool Valid() const = 0;
58 virtual void SeekToFirst() = 0;
60 virtual void SeekToLast() = 0;
62 virtual void Seek(const Slice
& key
) = 0;
64 virtual void SeekForPrev(const Slice
& key
) = 0;
66 virtual void Next() = 0;
68 virtual void Prev() = 0;
70 // the return WriteEntry is only valid until the next mutation of
71 // WriteBatchWithIndex
72 virtual WriteEntry
Entry() const = 0;
74 virtual Status
status() const = 0;
77 // A WriteBatchWithIndex with a binary searchable index built for all the keys
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
{
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);
100 virtual ~WriteBatchWithIndex();
102 using WriteBatchBase::Put
;
103 Status
Put(ColumnFamilyHandle
* column_family
, const Slice
& key
,
104 const Slice
& value
) override
;
106 Status
Put(const Slice
& key
, const Slice
& value
) override
;
108 using WriteBatchBase::Merge
;
109 Status
Merge(ColumnFamilyHandle
* column_family
, const Slice
& key
,
110 const Slice
& value
) override
;
112 Status
Merge(const Slice
& key
, const Slice
& value
) override
;
114 using WriteBatchBase::Delete
;
115 Status
Delete(ColumnFamilyHandle
* column_family
, const Slice
& key
) override
;
116 Status
Delete(const Slice
& key
) override
;
118 using WriteBatchBase::SingleDelete
;
119 Status
SingleDelete(ColumnFamilyHandle
* column_family
,
120 const Slice
& key
) override
;
121 Status
SingleDelete(const Slice
& key
) override
;
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
;
128 using WriteBatchBase::PutLogData
;
129 Status
PutLogData(const Slice
& blob
) override
;
131 using WriteBatchBase::Clear
;
132 void Clear() override
;
134 using WriteBatchBase::GetWriteBatch
;
135 WriteBatch
* GetWriteBatch() override
;
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
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();
148 // Will create a new Iterator that will use WBWIIterator as a delta and
149 // base_iterator as base.
151 // This function is only supported if the WriteBatchWithIndex was
152 // constructed with overwrite_key=true.
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
);
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
,
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
);
177 // Similar to DB::Get() but will also read writes from this batch.
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
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
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
,
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
;
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
203 // Calling RollbackToSavePoint invalidates any open iterators on this batch.
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
;
210 void SetMaxBytes(size_t max_bytes
) override
;
214 std::unique_ptr
<Rep
> rep
;
217 } // namespace rocksdb
219 #endif // !ROCKSDB_LITE