1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved. 2 // This source code is licensed under both the GPLv2 (found in the 3 // COPYING file in the root directory) and Apache 2.0 License 4 // (found in the LICENSE.Apache file in the root 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 #pragma once 10 11 #include <cstddef> 12 13 #include "rocksdb/rocksdb_namespace.h" 14 15 namespace ROCKSDB_NAMESPACE { 16 17 class Slice; 18 class Status; 19 class ColumnFamilyHandle; 20 class WriteBatch; 21 struct SliceParts; 22 23 // Abstract base class that defines the basic interface for a write batch. 24 // See WriteBatch for a basic implementation and WrithBatchWithIndex for an 25 // indexed implementation. 26 class WriteBatchBase { 27 public: ~WriteBatchBase()28 virtual ~WriteBatchBase() {} 29 30 // Store the mapping "key->value" in the database. 31 virtual Status Put(ColumnFamilyHandle* column_family, const Slice& key, 32 const Slice& value) = 0; 33 virtual Status Put(const Slice& key, const Slice& value) = 0; 34 35 // Variant of Put() that gathers output like writev(2). The key and value 36 // that will be written to the database are concatenations of arrays of 37 // slices. 38 virtual Status Put(ColumnFamilyHandle* column_family, const SliceParts& key, 39 const SliceParts& value); 40 virtual Status Put(const SliceParts& key, const SliceParts& value); 41 42 // Merge "value" with the existing value of "key" in the database. 43 // "key->merge(existing, value)" 44 virtual Status Merge(ColumnFamilyHandle* column_family, const Slice& key, 45 const Slice& value) = 0; 46 virtual Status Merge(const Slice& key, const Slice& value) = 0; 47 48 // variant that takes SliceParts 49 virtual Status Merge(ColumnFamilyHandle* column_family, const SliceParts& key, 50 const SliceParts& value); 51 virtual Status Merge(const SliceParts& key, const SliceParts& value); 52 53 // If the database contains a mapping for "key", erase it. Else do nothing. 54 virtual Status Delete(ColumnFamilyHandle* column_family, 55 const Slice& key) = 0; 56 virtual Status Delete(const Slice& key) = 0; 57 58 // variant that takes SliceParts 59 virtual Status Delete(ColumnFamilyHandle* column_family, 60 const SliceParts& key); 61 virtual Status Delete(const SliceParts& key); 62 63 // If the database contains a mapping for "key", erase it. Expects that the 64 // key was not overwritten. Else do nothing. 65 virtual Status SingleDelete(ColumnFamilyHandle* column_family, 66 const Slice& key) = 0; 67 virtual Status SingleDelete(const Slice& key) = 0; 68 69 // variant that takes SliceParts 70 virtual Status SingleDelete(ColumnFamilyHandle* column_family, 71 const SliceParts& key); 72 virtual Status SingleDelete(const SliceParts& key); 73 74 // If the database contains mappings in the range ["begin_key", "end_key"), 75 // erase them. Else do nothing. 76 virtual Status DeleteRange(ColumnFamilyHandle* column_family, 77 const Slice& begin_key, const Slice& end_key) = 0; 78 virtual Status DeleteRange(const Slice& begin_key, const Slice& end_key) = 0; 79 80 // variant that takes SliceParts 81 virtual Status DeleteRange(ColumnFamilyHandle* column_family, 82 const SliceParts& begin_key, 83 const SliceParts& end_key); 84 virtual Status DeleteRange(const SliceParts& begin_key, 85 const SliceParts& end_key); 86 87 // Append a blob of arbitrary size to the records in this batch. The blob will 88 // be stored in the transaction log but not in any other file. In particular, 89 // it will not be persisted to the SST files. When iterating over this 90 // WriteBatch, WriteBatch::Handler::LogData will be called with the contents 91 // of the blob as it is encountered. Blobs, puts, deletes, and merges will be 92 // encountered in the same order in which they were inserted. The blob will 93 // NOT consume sequence number(s) and will NOT increase the count of the batch 94 // 95 // Example application: add timestamps to the transaction log for use in 96 // replication. 97 virtual Status PutLogData(const Slice& blob) = 0; 98 99 // Clear all updates buffered in this batch. 100 virtual void Clear() = 0; 101 102 // Covert this batch into a WriteBatch. This is an abstracted way of 103 // converting any WriteBatchBase(eg WriteBatchWithIndex) into a basic 104 // WriteBatch. 105 virtual WriteBatch* GetWriteBatch() = 0; 106 107 // Records the state of the batch for future calls to RollbackToSavePoint(). 108 // May be called multiple times to set multiple save points. 109 virtual void SetSavePoint() = 0; 110 111 // Remove all entries in this batch (Put, Merge, Delete, PutLogData) since the 112 // most recent call to SetSavePoint() and removes the most recent save point. 113 // If there is no previous call to SetSavePoint(), behaves the same as 114 // Clear(). 115 virtual Status RollbackToSavePoint() = 0; 116 117 // Pop the most recent save point. 118 // If there is no previous call to SetSavePoint(), Status::NotFound() 119 // will be returned. 120 // Otherwise returns Status::OK(). 121 virtual Status PopSavePoint() = 0; 122 123 // Sets the maximum size of the write batch in bytes. 0 means no limit. 124 virtual void SetMaxBytes(size_t max_bytes) = 0; 125 }; 126 127 } // namespace ROCKSDB_NAMESPACE 128