1 // Copyright (c) 2011 The LevelDB Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. See the AUTHORS file for names of contributors. 4 // 5 // WriteBatch holds a collection of updates to apply atomically to a DB. 6 // 7 // The updates are applied in the order in which they are added 8 // to the WriteBatch. For example, the value of "key" will be "v3" 9 // after the following batch is written: 10 // 11 // batch.Put("key", "v1"); 12 // batch.Delete("key"); 13 // batch.Put("key", "v2"); 14 // batch.Put("key", "v3"); 15 // 16 // Multiple threads can invoke const methods on a WriteBatch without 17 // external synchronization, but if any of the threads may call a 18 // non-const method, all threads accessing the same WriteBatch must use 19 // external synchronization. 20 21 #ifndef STORAGE_LEVELDB_INCLUDE_WRITE_BATCH_H_ 22 #define STORAGE_LEVELDB_INCLUDE_WRITE_BATCH_H_ 23 24 #include <string> 25 26 #include "leveldb/export.h" 27 #include "leveldb/status.h" 28 29 namespace leveldb { 30 31 class Slice; 32 33 class LEVELDB_EXPORT WriteBatch { 34 public: 35 class LEVELDB_EXPORT Handler { 36 public: 37 virtual ~Handler(); 38 virtual void Put(const Slice& key, const Slice& value) = 0; 39 virtual void Delete(const Slice& key) = 0; 40 }; 41 42 WriteBatch(); 43 44 // Intentionally copyable. 45 WriteBatch(const WriteBatch&) = default; 46 WriteBatch& operator=(const WriteBatch&) = default; 47 48 ~WriteBatch(); 49 50 // Store the mapping "key->value" in the database. 51 void Put(const Slice& key, const Slice& value); 52 53 // If the database contains a mapping for "key", erase it. Else do nothing. 54 void Delete(const Slice& key); 55 56 // Clear all updates buffered in this batch. 57 void Clear(); 58 59 // The size of the database changes caused by this batch. 60 // 61 // This number is tied to implementation details, and may change across 62 // releases. It is intended for LevelDB usage metrics. 63 size_t ApproximateSize() const; 64 65 // Copies the operations in "source" to this batch. 66 // 67 // This runs in O(source size) time. However, the constant factor is better 68 // than calling Iterate() over the source batch with a Handler that replicates 69 // the operations into this batch. 70 void Append(const WriteBatch& source); 71 72 // Support for iterating over the contents of a batch. 73 Status Iterate(Handler* handler) const; 74 75 private: 76 friend class WriteBatchInternal; 77 78 std::string rep_; // See comment in write_batch.cc for the format of rep_ 79 }; 80 81 } // namespace leveldb 82 83 #endif // STORAGE_LEVELDB_INCLUDE_WRITE_BATCH_H_ 84