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_
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"
21 class LevelDBWriteBatch
;
23 class CONTENT_EXPORT LevelDBTransaction
24 : public base::RefCounted
<LevelDBTransaction
> {
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
,
33 leveldb::Status
Commit();
36 scoped_ptr
<LevelDBIterator
> CreateIterator();
39 virtual ~LevelDBTransaction();
40 friend class base::RefCounted
<LevelDBTransaction
>;
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;
60 const LevelDBComparator
* comparator_
;
63 typedef std::map
<base::StringPiece
, Record
*, Comparator
> DataType
;
65 class DataIterator
: public LevelDBIterator
{
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;
80 explicit DataIterator(LevelDBTransaction
* transaction
);
82 DataType::iterator iterator_
;
85 class TransactionIterator
: public LevelDBIterator
{
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
;
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_
;
119 Direction direction_
;
120 mutable bool data_changed_
;
123 void Set(const base::StringPiece
& key
, std::string
* value
, bool deleted
);
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_
;
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
{
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
,
149 void Remove(const base::StringPiece
& key
);
150 leveldb::Status
Commit();
153 explicit LevelDBDirectTransaction(LevelDBDatabase
* db
);
155 LevelDBDatabase
* db_
;
156 scoped_ptr
<LevelDBWriteBatch
> write_batch_
;
160 } // namespace content
162 #endif // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_