Chromium Code Reviews| 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 TreeNode { |
| 42 AVLTreeNode(); | 41 TreeNode(); |
| 43 ~AVLTreeNode(); | 42 ~TreeNode(); |
| 44 std::string key; | |
| 45 std::string value; | 43 std::string value; |
| 46 bool deleted; | 44 bool deleted; |
| 47 | |
| 48 AVLTreeNode* less; | |
| 49 AVLTreeNode* greater; | |
| 50 int balance_factor; | |
| 51 DISALLOW_COPY_AND_ASSIGN(AVLTreeNode); | |
| 52 }; | 45 }; |
| 53 | 46 |
| 54 struct AVLTreeAbstractor { | 47 class TreeComparator { |
|
ericu
2013/09/10 01:00:41
How about removing the word "Tree" everywhere?
jsbell
2013/09/10 17:00:06
Yeah, I suppose once the names are changed at all
| |
| 55 typedef AVLTreeNode* handle; | 48 public: |
| 56 typedef size_t size; | 49 explicit TreeComparator(const LevelDBComparator* comparator) |
| 57 typedef base::StringPiece key; | 50 : comparator_(comparator) {} |
| 58 | 51 bool operator()(const std::string& a, const std::string& b) const { |
| 59 handle GetLess(handle h) { return h->less; } | 52 return comparator_->Compare(a, b) < 0; |
| 60 void SetLess(handle h, handle less) { h->less = less; } | |
| 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 } | 53 } |
| 76 | 54 |
| 77 static handle Null() { return 0; } | 55 private: |
| 78 | |
| 79 const LevelDBComparator* comparator_; | 56 const LevelDBComparator* comparator_; |
| 80 }; | 57 }; |
| 81 | 58 |
| 82 typedef AVLTree<AVLTreeAbstractor> TreeType; | 59 typedef std::map<std::string, TreeNode*, TreeComparator> TreeType; |
| 83 | 60 |
| 84 class TreeIterator : public LevelDBIterator { | 61 class TreeIterator : public LevelDBIterator { |
| 85 public: | 62 public: |
| 86 static scoped_ptr<TreeIterator> Create(LevelDBTransaction* transaction); | 63 static scoped_ptr<TreeIterator> Create(LevelDBTransaction* transaction); |
| 87 virtual ~TreeIterator(); | 64 virtual ~TreeIterator(); |
| 88 | 65 |
| 89 virtual bool IsValid() const OVERRIDE; | 66 virtual bool IsValid() const OVERRIDE; |
| 90 virtual void SeekToLast() OVERRIDE; | 67 virtual void SeekToLast() OVERRIDE; |
| 91 virtual void Seek(const base::StringPiece& slice) OVERRIDE; | 68 virtual void Seek(const base::StringPiece& slice) OVERRIDE; |
| 92 virtual void Next() OVERRIDE; | 69 virtual void Next() OVERRIDE; |
| 93 virtual void Prev() OVERRIDE; | 70 virtual void Prev() OVERRIDE; |
| 94 virtual base::StringPiece Key() const OVERRIDE; | 71 virtual base::StringPiece Key() const OVERRIDE; |
| 95 virtual base::StringPiece Value() const OVERRIDE; | 72 virtual base::StringPiece Value() const OVERRIDE; |
| 96 bool IsDeleted() const; | 73 bool IsDeleted() const; |
| 97 void Reset(); | 74 void Reset(); |
| 98 | 75 |
| 99 private: | 76 private: |
| 100 explicit TreeIterator(LevelDBTransaction* transaction); | 77 explicit TreeIterator(LevelDBTransaction* transaction); |
| 101 mutable TreeType::Iterator iterator_; // Dereferencing this is non-const. | |
| 102 TreeType* tree_; | 78 TreeType* tree_; |
| 79 TreeType::iterator iterator_; | |
| 103 LevelDBTransaction* transaction_; | 80 LevelDBTransaction* transaction_; |
| 104 std::string key_; | 81 std::string key_; |
| 105 }; | 82 }; |
| 106 | 83 |
| 107 class TransactionIterator : public LevelDBIterator { | 84 class TransactionIterator : public LevelDBIterator { |
| 108 public: | 85 public: |
| 109 virtual ~TransactionIterator(); | 86 virtual ~TransactionIterator(); |
| 110 static scoped_ptr<TransactionIterator> Create( | 87 static scoped_ptr<TransactionIterator> Create( |
| 111 scoped_refptr<LevelDBTransaction> transaction); | 88 scoped_refptr<LevelDBTransaction> transaction); |
| 112 | 89 |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 144 | 121 |
| 145 void Set(const base::StringPiece& key, std::string* value, bool deleted); | 122 void Set(const base::StringPiece& key, std::string* value, bool deleted); |
| 146 void ClearTree(); | 123 void ClearTree(); |
| 147 void RegisterIterator(TransactionIterator* iterator); | 124 void RegisterIterator(TransactionIterator* iterator); |
| 148 void UnregisterIterator(TransactionIterator* iterator); | 125 void UnregisterIterator(TransactionIterator* iterator); |
| 149 void NotifyIteratorsOfTreeChange(); | 126 void NotifyIteratorsOfTreeChange(); |
| 150 | 127 |
| 151 LevelDBDatabase* db_; | 128 LevelDBDatabase* db_; |
| 152 const LevelDBSnapshot snapshot_; | 129 const LevelDBSnapshot snapshot_; |
| 153 const LevelDBComparator* comparator_; | 130 const LevelDBComparator* comparator_; |
| 131 TreeComparator tree_comparator_; | |
| 154 TreeType tree_; | 132 TreeType tree_; |
| 155 bool finished_; | 133 bool finished_; |
| 156 std::set<TransactionIterator*> iterators_; | 134 std::set<TransactionIterator*> iterators_; |
| 157 }; | 135 }; |
| 158 | 136 |
| 159 class LevelDBWriteOnlyTransaction { | 137 class LevelDBWriteOnlyTransaction { |
| 160 public: | 138 public: |
| 161 static scoped_ptr<LevelDBWriteOnlyTransaction> Create(LevelDBDatabase* db); | 139 static scoped_ptr<LevelDBWriteOnlyTransaction> Create(LevelDBDatabase* db); |
| 162 | 140 |
| 163 ~LevelDBWriteOnlyTransaction(); | 141 ~LevelDBWriteOnlyTransaction(); |
| 164 void Remove(const base::StringPiece& key); | 142 void Remove(const base::StringPiece& key); |
| 165 bool Commit(); | 143 bool Commit(); |
| 166 | 144 |
| 167 private: | 145 private: |
| 168 explicit LevelDBWriteOnlyTransaction(LevelDBDatabase* db); | 146 explicit LevelDBWriteOnlyTransaction(LevelDBDatabase* db); |
| 169 | 147 |
| 170 LevelDBDatabase* db_; | 148 LevelDBDatabase* db_; |
| 171 scoped_ptr<LevelDBWriteBatch> write_batch_; | 149 scoped_ptr<LevelDBWriteBatch> write_batch_; |
| 172 bool finished_; | 150 bool finished_; |
| 173 }; | 151 }; |
| 174 | 152 |
| 175 } // namespace content | 153 } // namespace content |
| 176 | 154 |
| 177 #endif // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_ | 155 #endif // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_ |
| OLD | NEW |