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 bool Get(const base::StringPiece
& key
, std::string
* value
, bool* found
);
34 scoped_ptr
<LevelDBIterator
> CreateIterator();
37 virtual ~LevelDBTransaction();
38 friend class base::RefCounted
<LevelDBTransaction
>;
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;
58 const LevelDBComparator
* comparator_
;
61 typedef std::map
<base::StringPiece
, Record
*, Comparator
> DataType
;
63 class DataIterator
: public LevelDBIterator
{
65 static scoped_ptr
<DataIterator
> Create(LevelDBTransaction
* transaction
);
66 virtual ~DataIterator();
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;
78 explicit DataIterator(LevelDBTransaction
* transaction
);
80 DataType::iterator iterator_
;
83 class TransactionIterator
: public LevelDBIterator
{
85 virtual ~TransactionIterator();
86 static scoped_ptr
<TransactionIterator
> Create(
87 scoped_refptr
<LevelDBTransaction
> transaction
);
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
;
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;
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_
;
117 Direction direction_
;
118 mutable bool data_changed_
;
121 void Set(const base::StringPiece
& key
, std::string
* value
, bool deleted
);
123 void RegisterIterator(TransactionIterator
* iterator
);
124 void UnregisterIterator(TransactionIterator
* iterator
);
125 void NotifyIterators();
127 LevelDBDatabase
* db_
;
128 const LevelDBSnapshot snapshot_
;
129 const LevelDBComparator
* comparator_
;
130 Comparator data_comparator_
;
133 std::set
<TransactionIterator
*> iterators_
;
136 class LevelDBWriteOnlyTransaction
{
138 static scoped_ptr
<LevelDBWriteOnlyTransaction
> Create(LevelDBDatabase
* db
);
140 ~LevelDBWriteOnlyTransaction();
141 void Remove(const base::StringPiece
& key
);
145 explicit LevelDBWriteOnlyTransaction(LevelDBDatabase
* db
);
147 LevelDBDatabase
* db_
;
148 scoped_ptr
<LevelDBWriteBatch
> write_batch_
;
152 } // namespace content
154 #endif // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_