Remove PlatformFile from profile_browsertest
[chromium-blink-merge.git] / content / browser / indexed_db / leveldb / leveldb_transaction.h
blob8827db84a4e3c4bd7fe4c3cfbef364274272bcb0
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.
5 #ifndef CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_
6 #define CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_
8 #include <map>
9 #include <set>
10 #include <string>
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"
19 namespace content {
21 class LevelDBWriteBatch;
23 class CONTENT_EXPORT LevelDBTransaction
24 : public base::RefCounted<LevelDBTransaction> {
25 public:
26 explicit LevelDBTransaction(LevelDBDatabase* db);
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();
36 scoped_ptr<LevelDBIterator> CreateIterator();
38 private:
39 virtual ~LevelDBTransaction();
40 friend class base::RefCounted<LevelDBTransaction>;
42 struct Record {
43 Record();
44 ~Record();
45 std::string key;
46 std::string value;
47 bool deleted;
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;
59 private:
60 const LevelDBComparator* comparator_;
63 typedef std::map<base::StringPiece, Record*, Comparator> DataType;
65 class DataIterator : public LevelDBIterator {
66 public:
67 static scoped_ptr<DataIterator> Create(LevelDBTransaction* transaction);
68 virtual ~DataIterator();
70 virtual bool IsValid() const OVERRIDE;
71 virtual void SeekToLast() OVERRIDE;
72 virtual void Seek(const base::StringPiece& slice) OVERRIDE;
73 virtual void Next() OVERRIDE;
74 virtual void Prev() OVERRIDE;
75 virtual base::StringPiece Key() const OVERRIDE;
76 virtual base::StringPiece Value() const OVERRIDE;
77 bool IsDeleted() const;
79 private:
80 explicit DataIterator(LevelDBTransaction* transaction);
81 DataType* data_;
82 DataType::iterator iterator_;
85 class TransactionIterator : public LevelDBIterator {
86 public:
87 virtual ~TransactionIterator();
88 static scoped_ptr<TransactionIterator> Create(
89 scoped_refptr<LevelDBTransaction> transaction);
91 virtual bool IsValid() const OVERRIDE;
92 virtual void SeekToLast() OVERRIDE;
93 virtual void Seek(const base::StringPiece& target) OVERRIDE;
94 virtual void Next() OVERRIDE;
95 virtual void Prev() OVERRIDE;
96 virtual base::StringPiece Key() const OVERRIDE;
97 virtual base::StringPiece Value() const OVERRIDE;
98 void DataChanged();
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;
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_;
115 enum Direction {
116 FORWARD,
117 REVERSE
119 Direction direction_;
120 mutable bool data_changed_;
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();
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_;
138 // Reads go straight to the database, ignoring any writes cached in
139 // write_batch_, and writes are write-through, without consolidation.
140 class LevelDBDirectTransaction {
141 public:
142 static scoped_ptr<LevelDBDirectTransaction> Create(LevelDBDatabase* db);
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();
152 private:
153 explicit LevelDBDirectTransaction(LevelDBDatabase* db);
155 LevelDBDatabase* db_;
156 scoped_ptr<LevelDBWriteBatch> write_batch_;
157 bool finished_;
160 } // namespace content
162 #endif // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_