OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_ | 5 #ifndef CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_ |
6 #define CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_ | 6 #define CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/memory/ref_counted.h" | 12 #include "base/memory/ref_counted.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/strings/string_piece.h" |
14 #include "content/browser/indexed_db/leveldb/avltree.h" | 15 #include "content/browser/indexed_db/leveldb/avltree.h" |
15 #include "content/browser/indexed_db/leveldb/leveldb_comparator.h" | 16 #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_database.h" |
17 #include "content/browser/indexed_db/leveldb/leveldb_iterator.h" | 18 #include "content/browser/indexed_db/leveldb/leveldb_iterator.h" |
18 #include "content/browser/indexed_db/leveldb/leveldb_slice.h" | |
19 | 19 |
20 namespace content { | 20 namespace content { |
21 | 21 |
22 class LevelDBWriteBatch; | 22 class LevelDBWriteBatch; |
23 | 23 |
24 class CONTENT_EXPORT LevelDBTransaction | 24 class CONTENT_EXPORT LevelDBTransaction |
25 : public base::RefCounted<LevelDBTransaction> { | 25 : public base::RefCounted<LevelDBTransaction> { |
26 public: | 26 public: |
27 static scoped_refptr<LevelDBTransaction> Create(LevelDBDatabase* db); | 27 static scoped_refptr<LevelDBTransaction> Create(LevelDBDatabase* db); |
28 | 28 |
29 void Put(const LevelDBSlice& key, std::vector<char>* value); | 29 void Put(const base::StringPiece& key, std::string* value); |
30 void Remove(const LevelDBSlice& key); | 30 void Remove(const base::StringPiece& key); |
31 bool Get(const LevelDBSlice& key, std::string* value, bool* found); | 31 bool Get(const base::StringPiece& key, std::string* value, bool* found); |
32 bool Commit(); | 32 bool Commit(); |
33 void Rollback(); | 33 void Rollback(); |
34 | 34 |
35 scoped_ptr<LevelDBIterator> CreateIterator(); | 35 scoped_ptr<LevelDBIterator> CreateIterator(); |
36 | 36 |
37 private: | 37 private: |
38 explicit LevelDBTransaction(LevelDBDatabase* db); | 38 explicit LevelDBTransaction(LevelDBDatabase* db); |
39 virtual ~LevelDBTransaction(); | 39 virtual ~LevelDBTransaction(); |
40 friend class base::RefCounted<LevelDBTransaction>; | 40 friend class base::RefCounted<LevelDBTransaction>; |
41 | 41 |
42 struct AVLTreeNode { | 42 struct AVLTreeNode { |
43 AVLTreeNode(); | 43 AVLTreeNode(); |
44 ~AVLTreeNode(); | 44 ~AVLTreeNode(); |
45 std::vector<char> key; | 45 std::string key; |
46 std::vector<char> value; | 46 std::string value; |
47 bool deleted; | 47 bool deleted; |
48 | 48 |
49 AVLTreeNode* less; | 49 AVLTreeNode* less; |
50 AVLTreeNode* greater; | 50 AVLTreeNode* greater; |
51 int balance_factor; | 51 int balance_factor; |
52 DISALLOW_COPY_AND_ASSIGN(AVLTreeNode); | 52 DISALLOW_COPY_AND_ASSIGN(AVLTreeNode); |
53 }; | 53 }; |
54 | 54 |
55 struct AVLTreeAbstractor { | 55 struct AVLTreeAbstractor { |
56 typedef AVLTreeNode* handle; | 56 typedef AVLTreeNode* handle; |
57 typedef size_t size; | 57 typedef size_t size; |
58 typedef LevelDBSlice key; | 58 typedef base::StringPiece key; |
59 | 59 |
60 handle GetLess(handle h) { return h->less; } | 60 handle GetLess(handle h) { return h->less; } |
61 void SetLess(handle h, handle less) { h->less = less; } | 61 void SetLess(handle h, handle less) { h->less = less; } |
62 handle GetGreater(handle h) { return h->greater; } | 62 handle GetGreater(handle h) { return h->greater; } |
63 void SetGreater(handle h, handle greater) { h->greater = greater; } | 63 void SetGreater(handle h, handle greater) { h->greater = greater; } |
64 | 64 |
65 int GetBalanceFactor(handle h) { return h->balance_factor; } | 65 int GetBalanceFactor(handle h) { return h->balance_factor; } |
66 void SetBalanceFactor(handle h, int bf) { h->balance_factor = bf; } | 66 void SetBalanceFactor(handle h, int bf) { h->balance_factor = bf; } |
67 | 67 |
68 int CompareKeyKey(const key& ka, const key& kb) { | 68 int CompareKeyKey(const key& ka, const key& kb) { |
69 return comparator_->Compare(ka, kb); | 69 return comparator_->Compare(ka, kb); |
70 } | 70 } |
71 int CompareKeyNode(const key& k, handle h) { | 71 int CompareKeyNode(const key& k, handle h) { |
72 return CompareKeyKey(k, key(h->key)); | 72 return CompareKeyKey(k, h->key); |
73 } | 73 } |
74 int CompareNodeNode(handle ha, handle hb) { | 74 int CompareNodeNode(handle ha, handle hb) { |
75 return CompareKeyKey(key(ha->key), key(hb->key)); | 75 return CompareKeyKey(ha->key, hb->key); |
76 } | 76 } |
77 | 77 |
78 static handle Null() { return 0; } | 78 static handle Null() { return 0; } |
79 | 79 |
80 const LevelDBComparator* comparator_; | 80 const LevelDBComparator* comparator_; |
81 }; | 81 }; |
82 | 82 |
83 typedef AVLTree<AVLTreeAbstractor> TreeType; | 83 typedef AVLTree<AVLTreeAbstractor> TreeType; |
84 | 84 |
85 class TreeIterator : public LevelDBIterator { | 85 class TreeIterator : public LevelDBIterator { |
86 public: | 86 public: |
87 static scoped_ptr<TreeIterator> Create(LevelDBTransaction* transaction); | 87 static scoped_ptr<TreeIterator> Create(LevelDBTransaction* transaction); |
88 virtual ~TreeIterator(); | 88 virtual ~TreeIterator(); |
89 | 89 |
90 virtual bool IsValid() const OVERRIDE; | 90 virtual bool IsValid() const OVERRIDE; |
91 virtual void SeekToLast() OVERRIDE; | 91 virtual void SeekToLast() OVERRIDE; |
92 virtual void Seek(const LevelDBSlice& slice) OVERRIDE; | 92 virtual void Seek(const base::StringPiece& slice) OVERRIDE; |
93 virtual void Next() OVERRIDE; | 93 virtual void Next() OVERRIDE; |
94 virtual void Prev() OVERRIDE; | 94 virtual void Prev() OVERRIDE; |
95 virtual LevelDBSlice Key() const OVERRIDE; | 95 virtual base::StringPiece Key() const OVERRIDE; |
96 virtual LevelDBSlice Value() const OVERRIDE; | 96 virtual base::StringPiece Value() const OVERRIDE; |
97 bool IsDeleted() const; | 97 bool IsDeleted() const; |
98 void Reset(); | 98 void Reset(); |
99 | 99 |
100 private: | 100 private: |
101 explicit TreeIterator(LevelDBTransaction* transaction); | 101 explicit TreeIterator(LevelDBTransaction* transaction); |
102 mutable TreeType::Iterator iterator_; // Dereferencing this is non-const. | 102 mutable TreeType::Iterator iterator_; // Dereferencing this is non-const. |
103 TreeType* tree_; | 103 TreeType* tree_; |
104 LevelDBTransaction* transaction_; | 104 LevelDBTransaction* transaction_; |
105 std::vector<char> key_; | 105 std::string key_; |
106 }; | 106 }; |
107 | 107 |
108 class TransactionIterator : public LevelDBIterator { | 108 class TransactionIterator : public LevelDBIterator { |
109 public: | 109 public: |
110 virtual ~TransactionIterator(); | 110 virtual ~TransactionIterator(); |
111 static scoped_ptr<TransactionIterator> Create( | 111 static scoped_ptr<TransactionIterator> Create( |
112 scoped_refptr<LevelDBTransaction> transaction); | 112 scoped_refptr<LevelDBTransaction> transaction); |
113 | 113 |
114 virtual bool IsValid() const OVERRIDE; | 114 virtual bool IsValid() const OVERRIDE; |
115 virtual void SeekToLast() OVERRIDE; | 115 virtual void SeekToLast() OVERRIDE; |
116 virtual void Seek(const LevelDBSlice& target) OVERRIDE; | 116 virtual void Seek(const base::StringPiece& target) OVERRIDE; |
117 virtual void Next() OVERRIDE; | 117 virtual void Next() OVERRIDE; |
118 virtual void Prev() OVERRIDE; | 118 virtual void Prev() OVERRIDE; |
119 virtual LevelDBSlice Key() const OVERRIDE; | 119 virtual base::StringPiece Key() const OVERRIDE; |
120 virtual LevelDBSlice Value() const OVERRIDE; | 120 virtual base::StringPiece Value() const OVERRIDE; |
121 void TreeChanged(); | 121 void TreeChanged(); |
122 | 122 |
123 private: | 123 private: |
124 explicit TransactionIterator(scoped_refptr<LevelDBTransaction> transaction); | 124 explicit TransactionIterator(scoped_refptr<LevelDBTransaction> transaction); |
125 void HandleConflictsAndDeletes(); | 125 void HandleConflictsAndDeletes(); |
126 void SetCurrentIteratorToSmallestKey(); | 126 void SetCurrentIteratorToSmallestKey(); |
127 void SetCurrentIteratorToLargestKey(); | 127 void SetCurrentIteratorToLargestKey(); |
128 void RefreshTreeIterator() const; | 128 void RefreshTreeIterator() const; |
129 bool TreeIteratorIsLower() const; | 129 bool TreeIteratorIsLower() const; |
130 bool TreeIteratorIsHigher() const; | 130 bool TreeIteratorIsHigher() const; |
131 | 131 |
132 scoped_refptr<LevelDBTransaction> transaction_; | 132 scoped_refptr<LevelDBTransaction> transaction_; |
133 const LevelDBComparator* comparator_; | 133 const LevelDBComparator* comparator_; |
134 mutable scoped_ptr<TreeIterator> tree_iterator_; | 134 mutable scoped_ptr<TreeIterator> tree_iterator_; |
135 scoped_ptr<LevelDBIterator> db_iterator_; | 135 scoped_ptr<LevelDBIterator> db_iterator_; |
136 LevelDBIterator* current_; | 136 LevelDBIterator* current_; |
137 | 137 |
138 enum Direction { | 138 enum Direction { |
139 FORWARD, | 139 FORWARD, |
140 REVERSE | 140 REVERSE |
141 }; | 141 }; |
142 Direction direction_; | 142 Direction direction_; |
143 mutable bool tree_changed_; | 143 mutable bool tree_changed_; |
144 }; | 144 }; |
145 | 145 |
146 void Set(const LevelDBSlice& key, | 146 void Set(const base::StringPiece& key, std::string* value, bool deleted); |
147 std::vector<char>* value, | |
148 bool deleted); | |
149 void ClearTree(); | 147 void ClearTree(); |
150 void RegisterIterator(TransactionIterator* iterator); | 148 void RegisterIterator(TransactionIterator* iterator); |
151 void UnregisterIterator(TransactionIterator* iterator); | 149 void UnregisterIterator(TransactionIterator* iterator); |
152 void NotifyIteratorsOfTreeChange(); | 150 void NotifyIteratorsOfTreeChange(); |
153 | 151 |
154 LevelDBDatabase* db_; | 152 LevelDBDatabase* db_; |
155 const LevelDBSnapshot snapshot_; | 153 const LevelDBSnapshot snapshot_; |
156 const LevelDBComparator* comparator_; | 154 const LevelDBComparator* comparator_; |
157 TreeType tree_; | 155 TreeType tree_; |
158 bool finished_; | 156 bool finished_; |
159 std::set<TransactionIterator*> iterators_; | 157 std::set<TransactionIterator*> iterators_; |
160 }; | 158 }; |
161 | 159 |
162 class LevelDBWriteOnlyTransaction { | 160 class LevelDBWriteOnlyTransaction { |
163 public: | 161 public: |
164 static scoped_ptr<LevelDBWriteOnlyTransaction> Create(LevelDBDatabase* db); | 162 static scoped_ptr<LevelDBWriteOnlyTransaction> Create(LevelDBDatabase* db); |
165 | 163 |
166 ~LevelDBWriteOnlyTransaction(); | 164 ~LevelDBWriteOnlyTransaction(); |
167 void Remove(const LevelDBSlice& key); | 165 void Remove(const base::StringPiece& key); |
168 bool Commit(); | 166 bool Commit(); |
169 | 167 |
170 private: | 168 private: |
171 explicit LevelDBWriteOnlyTransaction(LevelDBDatabase* db); | 169 explicit LevelDBWriteOnlyTransaction(LevelDBDatabase* db); |
172 | 170 |
173 LevelDBDatabase* db_; | 171 LevelDBDatabase* db_; |
174 scoped_ptr<LevelDBWriteBatch> write_batch_; | 172 scoped_ptr<LevelDBWriteBatch> write_batch_; |
175 bool finished_; | 173 bool finished_; |
176 }; | 174 }; |
177 | 175 |
178 } // namespace content | 176 } // namespace content |
179 | 177 |
180 #endif // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_ | 178 #endif // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_ |
OLD | NEW |