leveldb_transaction.h revision d0247b1b59f9c528cb6df88b4f2b9afaf80d181e
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  bool Get(const base::StringPiece& key, std::string* value, bool* found);
31  bool Commit();
32  void Rollback();
33
34  scoped_ptr<LevelDBIterator> CreateIterator();
35
36 private:
37  virtual ~LevelDBTransaction();
38  friend class base::RefCounted<LevelDBTransaction>;
39
40  struct Record {
41    Record();
42    ~Record();
43    std::string key;
44    std::string value;
45    bool deleted;
46  };
47
48  class Comparator {
49   public:
50    explicit Comparator(const LevelDBComparator* comparator)
51        : comparator_(comparator) {}
52    bool operator()(const base::StringPiece& a,
53                    const base::StringPiece& b) const {
54      return comparator_->Compare(a, b) < 0;
55    }
56
57   private:
58    const LevelDBComparator* comparator_;
59  };
60
61  typedef std::map<base::StringPiece, Record*, Comparator> DataType;
62
63  class DataIterator : public LevelDBIterator {
64   public:
65    static scoped_ptr<DataIterator> Create(LevelDBTransaction* transaction);
66    virtual ~DataIterator();
67
68    virtual bool IsValid() const OVERRIDE;
69    virtual void SeekToLast() OVERRIDE;
70    virtual void Seek(const base::StringPiece& slice) OVERRIDE;
71    virtual void Next() OVERRIDE;
72    virtual void Prev() OVERRIDE;
73    virtual base::StringPiece Key() const OVERRIDE;
74    virtual base::StringPiece Value() const OVERRIDE;
75    bool IsDeleted() const;
76
77   private:
78    explicit DataIterator(LevelDBTransaction* transaction);
79    DataType* data_;
80    DataType::iterator iterator_;
81  };
82
83  class TransactionIterator : public LevelDBIterator {
84   public:
85    virtual ~TransactionIterator();
86    static scoped_ptr<TransactionIterator> Create(
87        scoped_refptr<LevelDBTransaction> transaction);
88
89    virtual bool IsValid() const OVERRIDE;
90    virtual void SeekToLast() OVERRIDE;
91    virtual void Seek(const base::StringPiece& target) OVERRIDE;
92    virtual void Next() OVERRIDE;
93    virtual void Prev() OVERRIDE;
94    virtual base::StringPiece Key() const OVERRIDE;
95    virtual base::StringPiece Value() const OVERRIDE;
96    void DataChanged();
97
98   private:
99    explicit TransactionIterator(scoped_refptr<LevelDBTransaction> transaction);
100    void HandleConflictsAndDeletes();
101    void SetCurrentIteratorToSmallestKey();
102    void SetCurrentIteratorToLargestKey();
103    void RefreshDataIterator() const;
104    bool DataIteratorIsLower() const;
105    bool DataIteratorIsHigher() const;
106
107    scoped_refptr<LevelDBTransaction> transaction_;
108    const LevelDBComparator* comparator_;
109    mutable scoped_ptr<DataIterator> data_iterator_;
110    scoped_ptr<LevelDBIterator> db_iterator_;
111    LevelDBIterator* current_;
112
113    enum Direction {
114      FORWARD,
115      REVERSE
116    };
117    Direction direction_;
118    mutable bool data_changed_;
119  };
120
121  void Set(const base::StringPiece& key, std::string* value, bool deleted);
122  void Clear();
123  void RegisterIterator(TransactionIterator* iterator);
124  void UnregisterIterator(TransactionIterator* iterator);
125  void NotifyIterators();
126
127  LevelDBDatabase* db_;
128  const LevelDBSnapshot snapshot_;
129  const LevelDBComparator* comparator_;
130  Comparator data_comparator_;
131  DataType data_;
132  bool finished_;
133  std::set<TransactionIterator*> iterators_;
134};
135
136class LevelDBWriteOnlyTransaction {
137 public:
138  static scoped_ptr<LevelDBWriteOnlyTransaction> Create(LevelDBDatabase* db);
139
140  ~LevelDBWriteOnlyTransaction();
141  void Remove(const base::StringPiece& key);
142  bool Commit();
143
144 private:
145  explicit LevelDBWriteOnlyTransaction(LevelDBDatabase* db);
146
147  LevelDBDatabase* db_;
148  scoped_ptr<LevelDBWriteBatch> write_batch_;
149  bool finished_;
150};
151
152}  // namespace content
153
154#endif  // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_
155