write_batch.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #ifndef STORAGE_LEVELDB_INCLUDE_WRITE_BATCH_H_
  21. #define STORAGE_LEVELDB_INCLUDE_WRITE_BATCH_H_
  22. #include <string>
  23. #include "leveldb/export.h"
  24. #include "leveldb/status.h"
  25. namespace leveldb {
  26. class Slice;
  27. class LEVELDB_EXPORT WriteBatch {
  28. public:
  29. WriteBatch();
  30. // Intentionally copyable.
  31. WriteBatch(const WriteBatch&) = default;
  32. WriteBatch& operator =(const WriteBatch&) = default;
  33. ~WriteBatch();
  34. // Store the mapping "key->value" in the database.
  35. void Put(const Slice& key, const Slice& value);
  36. // If the database contains a mapping for "key", erase it. Else do nothing.
  37. void Delete(const Slice& key);
  38. // Clear all updates buffered in this batch.
  39. void Clear();
  40. // The size of the database changes caused by this batch.
  41. //
  42. // This number is tied to implementation details, and may change across
  43. // releases. It is intended for LevelDB usage metrics.
  44. size_t ApproximateSize() const;
  45. // Copies the operations in "source" to this batch.
  46. //
  47. // This runs in O(source size) time. However, the constant factor is better
  48. // than calling Iterate() over the source batch with a Handler that replicates
  49. // the operations into this batch.
  50. void Append(const WriteBatch& source);
  51. // Support for iterating over the contents of a batch.
  52. class Handler {
  53. public:
  54. virtual ~Handler();
  55. virtual void Put(const Slice& key, const Slice& value) = 0;
  56. virtual void Delete(const Slice& key) = 0;
  57. };
  58. Status Iterate(Handler* handler) const;
  59. private:
  60. friend class WriteBatchInternal;
  61. std::string rep_; // See comment in write_batch.cc for the format of rep_
  62. };
  63. } // namespace leveldb
  64. #endif // STORAGE_LEVELDB_INCLUDE_WRITE_BATCH_H_