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#include "leveldb/status.h"
26
27namespace leveldb {
28
29class Slice;
30
31class WriteBatch {
32 public:
33  WriteBatch();
34  ~WriteBatch();
35
36  // Store the mapping "key->value" in the database.
37  void Put(const Slice& key, const Slice& value);
38
39  // If the database contains a mapping for "key", erase it.  Else do nothing.
40  void Delete(const Slice& key);
41
42  // Clear all updates buffered in this batch.
43  void Clear();
44
45  // Support for iterating over the contents of a batch.
46  class Handler {
47   public:
48    virtual ~Handler();
49    virtual void Put(const Slice& key, const Slice& value) = 0;
50    virtual void Delete(const Slice& key) = 0;
51  };
52  Status Iterate(Handler* handler) const;
53
54 private:
55  friend class WriteBatchInternal;
56
57  std::string rep_;  // See comment in write_batch.cc for the format of rep_
58
59  // Intentionally copyable
60};
61
62}  // namespace leveldb
63
64#endif  // STORAGE_LEVELDB_INCLUDE_WRITE_BATCH_H_
65