leveldb_transaction.h revision 0529e5d033099cbfc42635f6f6183833b09dff6e
1// Copyright (c) 2013 The Chromium 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.
4
5#ifndef CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_
6#define CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_
7
8#include <map>
9#include <set>
10#include <string>
11
12#include "base/memory/ref_counted.h"
13#include "base/memory/scoped_ptr.h"
14#include "base/strings/string_piece.h"
15#include "content/browser/indexed_db/leveldb/leveldb_comparator.h"
16#include "content/browser/indexed_db/leveldb/leveldb_database.h"
17#include "content/browser/indexed_db/leveldb/leveldb_iterator.h"
18
19namespace content {
20
21class LevelDBWriteBatch;
22
23class CONTENT_EXPORT LevelDBTransaction
24    : public base::RefCounted<LevelDBTransaction> {
25 public:
26  explicit LevelDBTransaction(LevelDBDatabase* db);
27
28  void Put(const base::StringPiece& key, std::string* value);
29  void Remove(const base::StringPiece& key);
30  leveldb::Status Get(const base::StringPiece& key,
31                      std::string* value,
32                      bool* found);
33  leveldb::Status Commit();
34  void Rollback();
35
36  scoped_ptr<LevelDBIterator> CreateIterator();
37
38 private:
39  virtual ~LevelDBTransaction();
40  friend class base::RefCounted<LevelDBTransaction>;
41
42  struct Record {
43    Record();
44    ~Record();
45    std::string key;
46    std::string value;
47    bool deleted;
48  };
49
50  class Comparator {
51   public:
52    explicit Comparator(const LevelDBComparator* comparator)
53        : comparator_(comparator) {}
54    bool operator()(const base::StringPiece& a,
55                    const base::StringPiece& b) const {
56      return comparator_->Compare(a, b) < 0;
57    }
58
59   private:
60    const LevelDBComparator* comparator_;
61  };
62
63  typedef std::map<base::StringPiece, Record*, Comparator> DataType;
64
65  class DataIterator : public LevelDBIterator {
66   public:
67    static scoped_ptr<DataIterator> Create(LevelDBTransaction* transaction);
68    virtual ~DataIterator();
69
70    virtual bool IsValid() const OVERRIDE;
71    virtual leveldb::Status SeekToLast() OVERRIDE;
72    virtual leveldb::Status Seek(const base::StringPiece& slice) OVERRIDE;
73    virtual leveldb::Status Next() OVERRIDE;
74    virtual leveldb::Status Prev() OVERRIDE;
75    virtual base::StringPiece Key() const OVERRIDE;
76    virtual base::StringPiece Value() const OVERRIDE;
77    bool IsDeleted() const;
78
79   private:
80    explicit DataIterator(LevelDBTransaction* transaction);
81    DataType* data_;
82    DataType::iterator iterator_;
83  };
84
85  class TransactionIterator : public LevelDBIterator {
86   public:
87    virtual ~TransactionIterator();
88    static scoped_ptr<TransactionIterator> Create(
89        scoped_refptr<LevelDBTransaction> transaction);
90
91    virtual bool IsValid() const OVERRIDE;
92    virtual leveldb::Status SeekToLast() OVERRIDE;
93    virtual leveldb::Status Seek(const base::StringPiece& target) OVERRIDE;
94    virtual leveldb::Status Next() OVERRIDE;
95    virtual leveldb::Status Prev() OVERRIDE;
96    virtual base::StringPiece Key() const OVERRIDE;
97    virtual base::StringPiece Value() const OVERRIDE;
98    void DataChanged();
99
100   private:
101    explicit TransactionIterator(scoped_refptr<LevelDBTransaction> transaction);
102    void HandleConflictsAndDeletes();
103    void SetCurrentIteratorToSmallestKey();
104    void SetCurrentIteratorToLargestKey();
105    void RefreshDataIterator() const;
106    bool DataIteratorIsLower() const;
107    bool DataIteratorIsHigher() const;
108
109    scoped_refptr<LevelDBTransaction> transaction_;
110    const LevelDBComparator* comparator_;
111    mutable scoped_ptr<DataIterator> data_iterator_;
112    scoped_ptr<LevelDBIterator> db_iterator_;
113    LevelDBIterator* current_;
114
115    enum Direction {
116      FORWARD,
117      REVERSE
118    };
119    Direction direction_;
120    mutable bool data_changed_;
121  };
122
123  void Set(const base::StringPiece& key, std::string* value, bool deleted);
124  void Clear();
125  void RegisterIterator(TransactionIterator* iterator);
126  void UnregisterIterator(TransactionIterator* iterator);
127  void NotifyIterators();
128
129  LevelDBDatabase* db_;
130  const LevelDBSnapshot snapshot_;
131  const LevelDBComparator* comparator_;
132  Comparator data_comparator_;
133  DataType data_;
134  bool finished_;
135  std::set<TransactionIterator*> iterators_;
136};
137
138// Reads go straight to the database, ignoring any writes cached in
139// write_batch_, and writes are write-through, without consolidation.
140class LevelDBDirectTransaction {
141 public:
142  static scoped_ptr<LevelDBDirectTransaction> Create(LevelDBDatabase* db);
143
144  ~LevelDBDirectTransaction();
145  void Put(const base::StringPiece& key, const std::string* value);
146  leveldb::Status Get(const base::StringPiece& key,
147                      std::string* value,
148                      bool* found);
149  void Remove(const base::StringPiece& key);
150  leveldb::Status Commit();
151
152 private:
153  explicit LevelDBDirectTransaction(LevelDBDatabase* db);
154
155  LevelDBDatabase* db_;
156  scoped_ptr<LevelDBWriteBatch> write_batch_;
157  bool finished_;
158};
159
160}  // namespace content
161
162#endif  // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_
163