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