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 |
| 72 static key make_key(const std::string& vector) { |
| 73 if (vector.empty()) |
| 74 return key(); |
| 75 return key(&*vector.begin(), vector.size()); |
| 76 } |
71 int CompareKeyNode(const key& k, handle h) { | 77 int CompareKeyNode(const key& k, handle h) { |
72 return CompareKeyKey(k, key(h->key)); | 78 return CompareKeyKey(k, make_key(h->key)); |
73 } | 79 } |
74 int CompareNodeNode(handle ha, handle hb) { | 80 int CompareNodeNode(handle ha, handle hb) { |
75 return CompareKeyKey(key(ha->key), key(hb->key)); | 81 return CompareKeyKey(make_key(ha->key), make_key(hb->key)); |
76 } | 82 } |
77 | 83 |
78 static handle Null() { return 0; } | 84 static handle Null() { return 0; } |
79 | 85 |
80 const LevelDBComparator* comparator_; | 86 const LevelDBComparator* comparator_; |
81 }; | 87 }; |
82 | 88 |
83 typedef AVLTree<AVLTreeAbstractor> TreeType; | 89 typedef AVLTree<AVLTreeAbstractor> TreeType; |
84 | 90 |
85 class TreeIterator : public LevelDBIterator { | 91 class TreeIterator : public LevelDBIterator { |
86 public: | 92 public: |
87 static scoped_ptr<TreeIterator> Create(LevelDBTransaction* transaction); | 93 static scoped_ptr<TreeIterator> Create(LevelDBTransaction* transaction); |
88 virtual ~TreeIterator(); | 94 virtual ~TreeIterator(); |
89 | 95 |
90 virtual bool IsValid() const OVERRIDE; | 96 virtual bool IsValid() const OVERRIDE; |
91 virtual void SeekToLast() OVERRIDE; | 97 virtual void SeekToLast() OVERRIDE; |
92 virtual void Seek(const LevelDBSlice& slice) OVERRIDE; | 98 virtual void Seek(const base::StringPiece& slice) OVERRIDE; |
93 virtual void Next() OVERRIDE; | 99 virtual void Next() OVERRIDE; |
94 virtual void Prev() OVERRIDE; | 100 virtual void Prev() OVERRIDE; |
95 virtual LevelDBSlice Key() const OVERRIDE; | 101 virtual base::StringPiece Key() const OVERRIDE; |
96 virtual LevelDBSlice Value() const OVERRIDE; | 102 virtual base::StringPiece Value() const OVERRIDE; |
97 bool IsDeleted() const; | 103 bool IsDeleted() const; |
98 void Reset(); | 104 void Reset(); |
99 | 105 |
100 private: | 106 private: |
101 explicit TreeIterator(LevelDBTransaction* transaction); | 107 explicit TreeIterator(LevelDBTransaction* transaction); |
102 mutable TreeType::Iterator iterator_; // Dereferencing this is non-const. | 108 mutable TreeType::Iterator iterator_; // Dereferencing this is non-const. |
103 TreeType* tree_; | 109 TreeType* tree_; |
104 LevelDBTransaction* transaction_; | 110 LevelDBTransaction* transaction_; |
105 std::vector<char> key_; | 111 std::string key_; |
106 }; | 112 }; |
107 | 113 |
108 class TransactionIterator : public LevelDBIterator { | 114 class TransactionIterator : public LevelDBIterator { |
109 public: | 115 public: |
110 virtual ~TransactionIterator(); | 116 virtual ~TransactionIterator(); |
111 static scoped_ptr<TransactionIterator> Create( | 117 static scoped_ptr<TransactionIterator> Create( |
112 scoped_refptr<LevelDBTransaction> transaction); | 118 scoped_refptr<LevelDBTransaction> transaction); |
113 | 119 |
114 virtual bool IsValid() const OVERRIDE; | 120 virtual bool IsValid() const OVERRIDE; |
115 virtual void SeekToLast() OVERRIDE; | 121 virtual void SeekToLast() OVERRIDE; |
116 virtual void Seek(const LevelDBSlice& target) OVERRIDE; | 122 virtual void Seek(const base::StringPiece& target) OVERRIDE; |
117 virtual void Next() OVERRIDE; | 123 virtual void Next() OVERRIDE; |
118 virtual void Prev() OVERRIDE; | 124 virtual void Prev() OVERRIDE; |
119 virtual LevelDBSlice Key() const OVERRIDE; | 125 virtual base::StringPiece Key() const OVERRIDE; |
120 virtual LevelDBSlice Value() const OVERRIDE; | 126 virtual base::StringPiece Value() const OVERRIDE; |
121 void TreeChanged(); | 127 void TreeChanged(); |
122 | 128 |
123 private: | 129 private: |
124 explicit TransactionIterator(scoped_refptr<LevelDBTransaction> transaction); | 130 explicit TransactionIterator(scoped_refptr<LevelDBTransaction> transaction); |
125 void HandleConflictsAndDeletes(); | 131 void HandleConflictsAndDeletes(); |
126 void SetCurrentIteratorToSmallestKey(); | 132 void SetCurrentIteratorToSmallestKey(); |
127 void SetCurrentIteratorToLargestKey(); | 133 void SetCurrentIteratorToLargestKey(); |
128 void RefreshTreeIterator() const; | 134 void RefreshTreeIterator() const; |
129 bool TreeIteratorIsLower() const; | 135 bool TreeIteratorIsLower() const; |
130 bool TreeIteratorIsHigher() const; | 136 bool TreeIteratorIsHigher() const; |
131 | 137 |
132 scoped_refptr<LevelDBTransaction> transaction_; | 138 scoped_refptr<LevelDBTransaction> transaction_; |
133 const LevelDBComparator* comparator_; | 139 const LevelDBComparator* comparator_; |
134 mutable scoped_ptr<TreeIterator> tree_iterator_; | 140 mutable scoped_ptr<TreeIterator> tree_iterator_; |
135 scoped_ptr<LevelDBIterator> db_iterator_; | 141 scoped_ptr<LevelDBIterator> db_iterator_; |
136 LevelDBIterator* current_; | 142 LevelDBIterator* current_; |
137 | 143 |
138 enum Direction { | 144 enum Direction { |
139 FORWARD, | 145 FORWARD, |
140 REVERSE | 146 REVERSE |
141 }; | 147 }; |
142 Direction direction_; | 148 Direction direction_; |
143 mutable bool tree_changed_; | 149 mutable bool tree_changed_; |
144 }; | 150 }; |
145 | 151 |
146 void Set(const LevelDBSlice& key, | 152 void Set(const base::StringPiece& key, std::string* value, bool deleted); |
147 std::vector<char>* value, | |
148 bool deleted); | |
149 void ClearTree(); | 153 void ClearTree(); |
150 void RegisterIterator(TransactionIterator* iterator); | 154 void RegisterIterator(TransactionIterator* iterator); |
151 void UnregisterIterator(TransactionIterator* iterator); | 155 void UnregisterIterator(TransactionIterator* iterator); |
152 void NotifyIteratorsOfTreeChange(); | 156 void NotifyIteratorsOfTreeChange(); |
153 | 157 |
154 LevelDBDatabase* db_; | 158 LevelDBDatabase* db_; |
155 const LevelDBSnapshot snapshot_; | 159 const LevelDBSnapshot snapshot_; |
156 const LevelDBComparator* comparator_; | 160 const LevelDBComparator* comparator_; |
157 TreeType tree_; | 161 TreeType tree_; |
158 bool finished_; | 162 bool finished_; |
159 std::set<TransactionIterator*> iterators_; | 163 std::set<TransactionIterator*> iterators_; |
160 }; | 164 }; |
161 | 165 |
162 class LevelDBWriteOnlyTransaction { | 166 class LevelDBWriteOnlyTransaction { |
163 public: | 167 public: |
164 static scoped_ptr<LevelDBWriteOnlyTransaction> Create(LevelDBDatabase* db); | 168 static scoped_ptr<LevelDBWriteOnlyTransaction> Create(LevelDBDatabase* db); |
165 | 169 |
166 ~LevelDBWriteOnlyTransaction(); | 170 ~LevelDBWriteOnlyTransaction(); |
167 void Remove(const LevelDBSlice& key); | 171 void Remove(const base::StringPiece& key); |
168 bool Commit(); | 172 bool Commit(); |
169 | 173 |
170 private: | 174 private: |
171 explicit LevelDBWriteOnlyTransaction(LevelDBDatabase* db); | 175 explicit LevelDBWriteOnlyTransaction(LevelDBDatabase* db); |
172 | 176 |
173 LevelDBDatabase* db_; | 177 LevelDBDatabase* db_; |
174 scoped_ptr<LevelDBWriteBatch> write_batch_; | 178 scoped_ptr<LevelDBWriteBatch> write_batch_; |
175 bool finished_; | 179 bool finished_; |
176 }; | 180 }; |
177 | 181 |
178 } // namespace content | 182 } // namespace content |
179 | 183 |
180 #endif // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_ | 184 #endif // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_ |
OLD | NEW |