1179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
2179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// Use of this source code is governed by a BSD-style license that can be
3179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// found in the LICENSE file. See the AUTHORS file for names of contributors.
4179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org//
5179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// WriteBatch holds a collection of updates to apply atomically to a DB.
6179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org//
7179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// The updates are applied in the order in which they are added
8179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// to the WriteBatch.  For example, the value of "key" will be "v3"
9179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org// after the following batch is written:
10179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org//
11179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org//    batch.Put("key", "v1");
12179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org//    batch.Delete("key");
13179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org//    batch.Put("key", "v2");
14179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org//    batch.Put("key", "v3");
15a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org//
16a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org// Multiple threads can invoke const methods on a WriteBatch without
17a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org// external synchronization, but if any of the threads may call a
18a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org// non-const method, all threads accessing the same WriteBatch must use
19a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org// external synchronization.
20179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
21179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#ifndef STORAGE_LEVELDB_INCLUDE_WRITE_BATCH_H_
22179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#define STORAGE_LEVELDB_INCLUDE_WRITE_BATCH_H_
23179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
24179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#include <string>
25a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org#include "leveldb/status.h"
26179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
27179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgnamespace leveldb {
28179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
29179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgclass Slice;
30179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
31179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.orgclass WriteBatch {
32179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org public:
33179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  WriteBatch();
34179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  ~WriteBatch();
35179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
36179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Store the mapping "key->value" in the database.
37179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  void Put(const Slice& key, const Slice& value);
38179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
39179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // If the database contains a mapping for "key", erase it.  Else do nothing.
40179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  void Delete(const Slice& key);
41179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
42179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Clear all updates buffered in this batch.
43179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  void Clear();
44179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
45a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org  // Support for iterating over the contents of a batch.
46a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org  class Handler {
47a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org   public:
48a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org    virtual ~Handler();
49a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org    virtual void Put(const Slice& key, const Slice& value) = 0;
50a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org    virtual void Delete(const Slice& key) = 0;
51a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org  };
52a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org  Status Iterate(Handler* handler) const;
53a5b4129c0a8c01158cde2244a5811f15b9d45ec0dgrogan@chromium.org
54179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org private:
55179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  friend class WriteBatchInternal;
56179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
57179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  std::string rep_;  // See comment in write_batch.cc for the format of rep_
58179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
59179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org  // Intentionally copyable
60179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org};
61179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
6245b9940be332834440bd5299419f396e38085ebehans@chromium.org}  // namespace leveldb
63179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org
64179be588c25dccaa963df9c9c104fc6229435483jorlow@chromium.org#endif  // STORAGE_LEVELDB_INCLUDE_WRITE_BATCH_H_
65