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 <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/strings/string_piece.h" | 16 #include "base/strings/string_piece.h" |
| 17 #include "content/browser/indexed_db/leveldb/leveldb_comparator.h" | 17 #include "content/browser/indexed_db/leveldb/leveldb_comparator.h" |
| 18 #include "content/browser/indexed_db/leveldb/leveldb_database.h" | 18 #include "content/browser/indexed_db/leveldb/leveldb_database.h" |
| 19 #include "content/browser/indexed_db/leveldb/leveldb_iterator.h" | 19 #include "content/browser/indexed_db/leveldb/leveldb_iterator.h" |
| 20 | 20 |
| 21 namespace content { | 21 namespace content { |
| 22 | 22 |
| 23 class LevelDBTransactionRangeTest; | 23 class LevelDBTransactionRangeTest; |
| 24 class LevelDBTransactionTest; | |
| 24 class LevelDBWriteBatch; | 25 class LevelDBWriteBatch; |
| 25 | 26 |
| 26 class CONTENT_EXPORT LevelDBTransaction | 27 class CONTENT_EXPORT LevelDBTransaction |
| 27 : public base::RefCounted<LevelDBTransaction> { | 28 : public base::RefCounted<LevelDBTransaction> { |
| 28 public: | 29 public: |
| 29 void Put(const base::StringPiece& key, std::string* value); | 30 void Put(const base::StringPiece& key, std::string* value); |
| 30 | 31 |
| 31 void Remove(const base::StringPiece& key); | 32 void Remove(const base::StringPiece& key); |
| 32 | 33 |
| 33 leveldb::Status RemoveRange(const base::StringPiece& begin, | 34 leveldb::Status RemoveRange(const base::StringPiece& begin, |
| 34 const base::StringPiece& end, | 35 const base::StringPiece& end, |
| 35 bool upper_open); | 36 bool upper_open); |
| 36 | 37 |
| 37 virtual leveldb::Status Get(const base::StringPiece& key, | 38 virtual leveldb::Status Get(const base::StringPiece& key, |
| 38 std::string* value, | 39 std::string* value, |
| 39 bool* found); | 40 bool* found); |
| 40 virtual leveldb::Status Commit(); | 41 virtual leveldb::Status Commit(); |
| 41 void Rollback(); | 42 void Rollback(); |
| 42 | 43 |
| 43 std::unique_ptr<LevelDBIterator> CreateIterator(); | 44 std::unique_ptr<LevelDBIterator> CreateIterator(); |
| 44 | 45 |
| 45 protected: | 46 protected: |
| 46 virtual ~LevelDBTransaction(); | 47 virtual ~LevelDBTransaction(); |
| 47 explicit LevelDBTransaction(LevelDBDatabase* db); | 48 explicit LevelDBTransaction(LevelDBDatabase* db); |
| 48 friend class IndexedDBClassFactory; | 49 friend class IndexedDBClassFactory; |
| 49 | 50 |
| 50 private: | 51 private: |
| 51 friend class base::RefCounted<LevelDBTransaction>; | 52 friend class base::RefCounted<LevelDBTransaction>; |
| 52 friend class content::LevelDBTransactionRangeTest; | 53 friend class content::LevelDBTransactionRangeTest; |
| 54 friend class content::LevelDBTransactionTest; | |
| 53 FRIEND_TEST_ALL_PREFIXES(LevelDBTransactionTest, GetAndPut); | 55 FRIEND_TEST_ALL_PREFIXES(LevelDBTransactionTest, GetAndPut); |
| 54 FRIEND_TEST_ALL_PREFIXES(LevelDBTransactionTest, Commit); | 56 FRIEND_TEST_ALL_PREFIXES(LevelDBTransactionTest, Commit); |
| 55 FRIEND_TEST_ALL_PREFIXES(LevelDBTransactionTest, Iterator); | 57 FRIEND_TEST_ALL_PREFIXES(LevelDBTransactionTest, Iterator); |
| 56 | 58 |
| 57 struct Record { | 59 struct Record { |
| 58 Record(); | 60 Record(); |
| 59 ~Record(); | 61 ~Record(); |
| 60 std::string key; | 62 std::string key; |
| 61 std::string value; | 63 std::string value; |
| 62 bool deleted = false; | 64 bool deleted = false; |
| (...skipping 23 matching lines...) Expand all Loading... | |
| 86 static std::unique_ptr<DataIterator> Create( | 88 static std::unique_ptr<DataIterator> Create( |
| 87 LevelDBTransaction* transaction); | 89 LevelDBTransaction* transaction); |
| 88 ~DataIterator() override; | 90 ~DataIterator() override; |
| 89 bool IsValid() const override; | 91 bool IsValid() const override; |
| 90 leveldb::Status SeekToLast() override; | 92 leveldb::Status SeekToLast() override; |
| 91 leveldb::Status Seek(const base::StringPiece& slice) override; | 93 leveldb::Status Seek(const base::StringPiece& slice) override; |
| 92 leveldb::Status Next() override; | 94 leveldb::Status Next() override; |
| 93 leveldb::Status Prev() override; | 95 leveldb::Status Prev() override; |
| 94 base::StringPiece Key() const override; | 96 base::StringPiece Key() const override; |
| 95 base::StringPiece Value() const override; | 97 base::StringPiece Value() const override; |
| 98 | |
| 96 bool IsDeleted() const; | 99 bool IsDeleted() const; |
| 97 | 100 |
| 98 // Mark the current record as deleted. | 101 // Mark the current record as deleted. |
| 99 void Delete(); | 102 void Delete(); |
| 100 | 103 |
| 101 private: | 104 private: |
| 102 explicit DataIterator(LevelDBTransaction* transaction); | 105 explicit DataIterator(LevelDBTransaction* transaction); |
| 103 DataType* data_; | 106 DataType* data_; |
| 104 DataType::iterator iterator_; | 107 DataType::iterator iterator_; |
| 105 | 108 |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 116 static std::unique_ptr<TransactionIterator> Create( | 119 static std::unique_ptr<TransactionIterator> Create( |
| 117 scoped_refptr<LevelDBTransaction> transaction); | 120 scoped_refptr<LevelDBTransaction> transaction); |
| 118 | 121 |
| 119 bool IsValid() const override; | 122 bool IsValid() const override; |
| 120 leveldb::Status SeekToLast() override; | 123 leveldb::Status SeekToLast() override; |
| 121 leveldb::Status Seek(const base::StringPiece& target) override; | 124 leveldb::Status Seek(const base::StringPiece& target) override; |
| 122 leveldb::Status Next() override; | 125 leveldb::Status Next() override; |
| 123 leveldb::Status Prev() override; | 126 leveldb::Status Prev() override; |
| 124 base::StringPiece Key() const override; | 127 base::StringPiece Key() const override; |
| 125 base::StringPiece Value() const override; | 128 base::StringPiece Value() const override; |
| 129 void PurgeMemory() override; | |
| 130 bool IsMemoryPurged() const override; | |
|
jsbell
2017/03/22 23:55:32
I think I'd keep this called 'IsEvicted' to be con
dmurph
2017/03/23 20:56:48
Done.
| |
| 131 | |
| 126 void DataChanged(); | 132 void DataChanged(); |
| 127 | 133 |
| 128 // Mark the current record as deleted. If an existing record | 134 // Mark the current record as deleted. If an existing record |
| 129 // is present in the uncommitted data this will convert it to | 135 // is present in the uncommitted data this will convert it to |
| 130 // a deletion record, otherwise it will insert a new one. | 136 // a deletion record, otherwise it will insert a new one. |
| 131 void Delete(); | 137 void Delete(); |
| 132 | 138 |
| 133 private: | 139 private: |
| 134 enum Direction { FORWARD, REVERSE }; | 140 enum Direction { FORWARD, REVERSE }; |
| 135 | 141 |
| 142 enum class DBIteratorState { | |
| 143 ACTIVE, | |
| 144 EVICTED_AND_VALID, | |
| 145 EVICTED_AND_INVALID | |
| 146 }; | |
| 147 | |
| 136 explicit TransactionIterator(scoped_refptr<LevelDBTransaction> transaction); | 148 explicit TransactionIterator(scoped_refptr<LevelDBTransaction> transaction); |
| 137 void HandleConflictsAndDeletes(); | 149 void HandleConflictsAndDeletes(); |
| 138 void SetCurrentIteratorToSmallestKey(); | 150 void SetCurrentIteratorToSmallestKey(); |
| 139 void SetCurrentIteratorToLargestKey(); | 151 void SetCurrentIteratorToLargestKey(); |
| 140 void RefreshDataIterator() const; | 152 void RefreshDataIterator() const; |
| 141 bool DataIteratorIsLower() const; | 153 bool DataIteratorIsLower() const; |
| 142 bool DataIteratorIsHigher() const; | 154 bool DataIteratorIsHigher() const; |
| 143 | 155 |
| 156 void LoadDBIteratorIfEvicted(); | |
|
jsbell
2017/03/22 23:55:32
Maybe 'RestoreDBIteratorIfEvicted' ?
dmurph
2017/03/23 20:56:48
Done.
| |
| 157 | |
| 144 scoped_refptr<LevelDBTransaction> transaction_; | 158 scoped_refptr<LevelDBTransaction> transaction_; |
| 145 const LevelDBComparator* comparator_; | 159 const LevelDBComparator* comparator_; |
| 146 mutable std::unique_ptr<DataIterator> data_iterator_; | 160 mutable std::unique_ptr<DataIterator> data_iterator_; |
| 147 std::unique_ptr<LevelDBIterator> db_iterator_; | 161 std::unique_ptr<LevelDBIterator> db_iterator_; |
| 162 DBIteratorState db_iterator_state_ = DBIteratorState::ACTIVE; | |
| 148 LevelDBIterator* current_ = nullptr; | 163 LevelDBIterator* current_ = nullptr; |
| 149 | 164 |
| 165 std::string db_key_before_eviction_; | |
| 166 | |
| 150 Direction direction_ = FORWARD; | 167 Direction direction_ = FORWARD; |
| 151 mutable bool data_changed_ = false; | 168 mutable bool data_changed_ = false; |
| 152 | 169 |
| 153 DISALLOW_COPY_AND_ASSIGN(TransactionIterator); | 170 DISALLOW_COPY_AND_ASSIGN(TransactionIterator); |
| 154 }; | 171 }; |
| 155 | 172 |
| 156 void Set(const base::StringPiece& key, std::string* value, bool deleted); | 173 void Set(const base::StringPiece& key, std::string* value, bool deleted); |
| 157 void RegisterIterator(TransactionIterator* iterator); | 174 void RegisterIterator(TransactionIterator* iterator); |
| 158 void UnregisterIterator(TransactionIterator* iterator); | 175 void UnregisterIterator(TransactionIterator* iterator); |
| 159 void NotifyIterators(); | 176 void NotifyIterators(); |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 189 LevelDBDatabase* db_; | 206 LevelDBDatabase* db_; |
| 190 std::unique_ptr<LevelDBWriteBatch> write_batch_; | 207 std::unique_ptr<LevelDBWriteBatch> write_batch_; |
| 191 bool finished_ = false; | 208 bool finished_ = false; |
| 192 | 209 |
| 193 DISALLOW_COPY_AND_ASSIGN(LevelDBDirectTransaction); | 210 DISALLOW_COPY_AND_ASSIGN(LevelDBDirectTransaction); |
| 194 }; | 211 }; |
| 195 | 212 |
| 196 } // namespace content | 213 } // namespace content |
| 197 | 214 |
| 198 #endif // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_ | 215 #endif // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_ |
| OLD | NEW |