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 LevelDBWriteBatch; | 24 class LevelDBWriteBatch; |
| 24 | 25 |
|
dmurph
2017/03/01 20:06:35
Because this took me a while to understand:
Can y
jsbell
2017/03/01 22:45:25
Done - feedback welcome!
| |
| 25 class CONTENT_EXPORT LevelDBTransaction | 26 class CONTENT_EXPORT LevelDBTransaction |
| 26 : public base::RefCounted<LevelDBTransaction> { | 27 : public base::RefCounted<LevelDBTransaction> { |
| 27 public: | 28 public: |
| 28 void Put(const base::StringPiece& key, std::string* value); | 29 void Put(const base::StringPiece& key, std::string* value); |
| 29 | 30 |
| 30 // Returns true if this operation performs a change, where the value wasn't | 31 void Remove(const base::StringPiece& key); |
| 31 // already deleted. | 32 |
| 32 bool Remove(const base::StringPiece& key); | 33 leveldb::Status RemoveRange(const base::StringPiece& begin, |
| 34 const base::StringPiece& end, | |
| 35 bool upper_open); | |
| 36 | |
| 33 virtual leveldb::Status Get(const base::StringPiece& key, | 37 virtual leveldb::Status Get(const base::StringPiece& key, |
| 34 std::string* value, | 38 std::string* value, |
| 35 bool* found); | 39 bool* found); |
| 36 virtual leveldb::Status Commit(); | 40 virtual leveldb::Status Commit(); |
| 37 void Rollback(); | 41 void Rollback(); |
| 38 | 42 |
| 39 std::unique_ptr<LevelDBIterator> CreateIterator(); | 43 std::unique_ptr<LevelDBIterator> CreateIterator(); |
| 40 | 44 |
| 41 protected: | 45 protected: |
| 42 virtual ~LevelDBTransaction(); | 46 virtual ~LevelDBTransaction(); |
| 43 explicit LevelDBTransaction(LevelDBDatabase* db); | 47 explicit LevelDBTransaction(LevelDBDatabase* db); |
| 44 friend class IndexedDBClassFactory; | 48 friend class IndexedDBClassFactory; |
| 45 | 49 |
| 46 private: | 50 private: |
| 47 friend class base::RefCounted<LevelDBTransaction>; | 51 friend class base::RefCounted<LevelDBTransaction>; |
| 48 FRIEND_TEST_ALL_PREFIXES(LevelDBDatabaseTest, Transaction); | 52 friend class content::LevelDBTransactionRangeTest; |
| 49 FRIEND_TEST_ALL_PREFIXES(LevelDBDatabaseTest, TransactionCommitTest); | 53 FRIEND_TEST_ALL_PREFIXES(LevelDBTransactionTest, GetAndPut); |
| 50 FRIEND_TEST_ALL_PREFIXES(LevelDBDatabaseTest, TransactionIterator); | 54 FRIEND_TEST_ALL_PREFIXES(LevelDBTransactionTest, Commit); |
| 55 FRIEND_TEST_ALL_PREFIXES(LevelDBTransactionTest, Iterator); | |
| 51 | 56 |
| 52 struct Record { | 57 struct Record { |
| 53 Record(); | 58 Record(); |
| 54 ~Record(); | 59 ~Record(); |
| 55 std::string key; | 60 std::string key; |
| 56 std::string value; | 61 std::string value; |
| 57 bool deleted; | 62 bool deleted = false; |
| 58 }; | 63 }; |
| 59 | 64 |
| 60 class Comparator { | 65 class Comparator { |
| 61 public: | 66 public: |
| 62 explicit Comparator(const LevelDBComparator* comparator) | 67 explicit Comparator(const LevelDBComparator* comparator) |
| 63 : comparator_(comparator) {} | 68 : comparator_(comparator) {} |
| 64 bool operator()(const base::StringPiece& a, | 69 bool operator()(const base::StringPiece& a, |
| 65 const base::StringPiece& b) const { | 70 const base::StringPiece& b) const { |
| 66 return comparator_->Compare(a, b) < 0; | 71 return comparator_->Compare(a, b) < 0; |
| 67 } | 72 } |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 81 | 86 |
| 82 bool IsValid() const override; | 87 bool IsValid() const override; |
| 83 leveldb::Status SeekToLast() override; | 88 leveldb::Status SeekToLast() override; |
| 84 leveldb::Status Seek(const base::StringPiece& slice) override; | 89 leveldb::Status Seek(const base::StringPiece& slice) override; |
| 85 leveldb::Status Next() override; | 90 leveldb::Status Next() override; |
| 86 leveldb::Status Prev() override; | 91 leveldb::Status Prev() override; |
| 87 base::StringPiece Key() const override; | 92 base::StringPiece Key() const override; |
| 88 base::StringPiece Value() const override; | 93 base::StringPiece Value() const override; |
| 89 bool IsDeleted() const; | 94 bool IsDeleted() const; |
| 90 | 95 |
| 96 // Mark the current record as deleted. | |
| 97 void Delete(); | |
| 98 | |
| 91 private: | 99 private: |
| 92 explicit DataIterator(LevelDBTransaction* transaction); | 100 explicit DataIterator(LevelDBTransaction* transaction); |
| 93 DataType* data_; | 101 DataType* data_; |
| 94 DataType::iterator iterator_; | 102 DataType::iterator iterator_; |
| 95 | 103 |
| 96 DISALLOW_COPY_AND_ASSIGN(DataIterator); | 104 DISALLOW_COPY_AND_ASSIGN(DataIterator); |
| 97 }; | 105 }; |
| 98 | 106 |
| 99 class TransactionIterator : public LevelDBIterator { | 107 class TransactionIterator : public LevelDBIterator { |
| 100 public: | 108 public: |
| 101 ~TransactionIterator() override; | 109 ~TransactionIterator() override; |
| 102 static std::unique_ptr<TransactionIterator> Create( | 110 static std::unique_ptr<TransactionIterator> Create( |
| 103 scoped_refptr<LevelDBTransaction> transaction); | 111 scoped_refptr<LevelDBTransaction> transaction); |
| 104 | 112 |
| 105 bool IsValid() const override; | 113 bool IsValid() const override; |
| 106 leveldb::Status SeekToLast() override; | 114 leveldb::Status SeekToLast() override; |
| 107 leveldb::Status Seek(const base::StringPiece& target) override; | 115 leveldb::Status Seek(const base::StringPiece& target) override; |
| 108 leveldb::Status Next() override; | 116 leveldb::Status Next() override; |
| 109 leveldb::Status Prev() override; | 117 leveldb::Status Prev() override; |
| 110 base::StringPiece Key() const override; | 118 base::StringPiece Key() const override; |
| 111 base::StringPiece Value() const override; | 119 base::StringPiece Value() const override; |
| 112 void DataChanged(); | 120 void DataChanged(); |
| 113 | 121 |
| 122 // If the current iterator is the data iterator, mark the record as | |
| 123 // deleted and return true. Otherwise return false - the caller may want | |
| 124 // to insert a deletion record. | |
| 125 bool DeleteData(); | |
|
dmurph
2017/03/01 20:06:35
Better name? "MaybeDeleteOnDataIterator"?
jsbell
2017/03/01 22:45:25
I realized I could pull the non-DataIterator case
| |
| 126 | |
| 114 private: | 127 private: |
| 115 enum Direction { FORWARD, REVERSE }; | 128 enum Direction { FORWARD, REVERSE }; |
| 116 | 129 |
| 117 explicit TransactionIterator(scoped_refptr<LevelDBTransaction> transaction); | 130 explicit TransactionIterator(scoped_refptr<LevelDBTransaction> transaction); |
| 118 void HandleConflictsAndDeletes(); | 131 void HandleConflictsAndDeletes(); |
| 119 void SetCurrentIteratorToSmallestKey(); | 132 void SetCurrentIteratorToSmallestKey(); |
| 120 void SetCurrentIteratorToLargestKey(); | 133 void SetCurrentIteratorToLargestKey(); |
| 121 void RefreshDataIterator() const; | 134 void RefreshDataIterator() const; |
| 122 bool DataIteratorIsLower() const; | 135 bool DataIteratorIsLower() const; |
| 123 bool DataIteratorIsHigher() const; | 136 bool DataIteratorIsHigher() const; |
| 124 | 137 |
| 125 scoped_refptr<LevelDBTransaction> transaction_; | 138 scoped_refptr<LevelDBTransaction> transaction_; |
| 126 const LevelDBComparator* comparator_; | 139 const LevelDBComparator* comparator_; |
| 127 mutable std::unique_ptr<DataIterator> data_iterator_; | 140 mutable std::unique_ptr<DataIterator> data_iterator_; |
| 128 std::unique_ptr<LevelDBIterator> db_iterator_; | 141 std::unique_ptr<LevelDBIterator> db_iterator_; |
| 129 LevelDBIterator* current_; | 142 LevelDBIterator* current_ = nullptr; |
| 130 | 143 |
| 131 Direction direction_; | 144 Direction direction_ = FORWARD; |
| 132 mutable bool data_changed_; | 145 mutable bool data_changed_ = false; |
| 133 | 146 |
| 134 DISALLOW_COPY_AND_ASSIGN(TransactionIterator); | 147 DISALLOW_COPY_AND_ASSIGN(TransactionIterator); |
| 135 }; | 148 }; |
| 136 // Returns true if the key was originally marked deleted, false otherwise. | 149 |
| 137 bool Set(const base::StringPiece& key, std::string* value, bool deleted); | 150 void Set(const base::StringPiece& key, std::string* value, bool deleted); |
| 138 void RegisterIterator(TransactionIterator* iterator); | 151 void RegisterIterator(TransactionIterator* iterator); |
| 139 void UnregisterIterator(TransactionIterator* iterator); | 152 void UnregisterIterator(TransactionIterator* iterator); |
| 140 void NotifyIterators(); | 153 void NotifyIterators(); |
| 141 | 154 |
| 142 LevelDBDatabase* db_; | 155 LevelDBDatabase* db_; |
| 143 const LevelDBSnapshot snapshot_; | 156 const LevelDBSnapshot snapshot_; |
| 144 const LevelDBComparator* comparator_; | 157 const LevelDBComparator* comparator_; |
| 145 Comparator data_comparator_; | 158 Comparator data_comparator_; |
| 146 DataType data_; | 159 DataType data_; |
| 147 bool finished_; | 160 bool finished_ = false; |
| 148 std::set<TransactionIterator*> iterators_; | 161 std::set<TransactionIterator*> iterators_; |
| 149 | 162 |
| 150 DISALLOW_COPY_AND_ASSIGN(LevelDBTransaction); | 163 DISALLOW_COPY_AND_ASSIGN(LevelDBTransaction); |
| 151 }; | 164 }; |
| 152 | 165 |
| 153 // Reads go straight to the database, ignoring any writes cached in | 166 // Reads go straight to the database, ignoring any writes cached in |
| 154 // write_batch_, and writes are write-through, without consolidation. | 167 // write_batch_, and writes are write-through, without consolidation. |
| 155 class LevelDBDirectTransaction { | 168 class LevelDBDirectTransaction { |
| 156 public: | 169 public: |
| 157 static std::unique_ptr<LevelDBDirectTransaction> Create(LevelDBDatabase* db); | 170 static std::unique_ptr<LevelDBDirectTransaction> Create(LevelDBDatabase* db); |
| 158 | 171 |
| 159 ~LevelDBDirectTransaction(); | 172 ~LevelDBDirectTransaction(); |
| 160 void Put(const base::StringPiece& key, const std::string* value); | 173 void Put(const base::StringPiece& key, const std::string* value); |
| 161 leveldb::Status Get(const base::StringPiece& key, | 174 leveldb::Status Get(const base::StringPiece& key, |
| 162 std::string* value, | 175 std::string* value, |
| 163 bool* found); | 176 bool* found); |
| 164 void Remove(const base::StringPiece& key); | 177 void Remove(const base::StringPiece& key); |
| 165 leveldb::Status Commit(); | 178 leveldb::Status Commit(); |
| 166 | 179 |
| 167 private: | 180 private: |
| 168 explicit LevelDBDirectTransaction(LevelDBDatabase* db); | 181 explicit LevelDBDirectTransaction(LevelDBDatabase* db); |
| 169 | 182 |
| 170 LevelDBDatabase* db_; | 183 LevelDBDatabase* db_; |
| 171 std::unique_ptr<LevelDBWriteBatch> write_batch_; | 184 std::unique_ptr<LevelDBWriteBatch> write_batch_; |
| 172 bool finished_; | 185 bool finished_ = false; |
| 173 | 186 |
| 174 DISALLOW_COPY_AND_ASSIGN(LevelDBDirectTransaction); | 187 DISALLOW_COPY_AND_ASSIGN(LevelDBDirectTransaction); |
| 175 }; | 188 }; |
| 176 | 189 |
| 177 } // namespace content | 190 } // namespace content |
| 178 | 191 |
| 179 #endif // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_ | 192 #endif // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_ |
| OLD | NEW |