小组成员: 曹可心-10223903406 朴祉燕-10224602413
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.6 KiB

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