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