| 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 |
| 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 } |
| 68 | 73 |
| 69 private: | 74 private: |
| 70 const LevelDBComparator* comparator_; | 75 const LevelDBComparator* comparator_; |
| 71 }; | 76 }; |
| 72 | 77 |
| 73 typedef std::map<base::StringPiece, std::unique_ptr<Record>, Comparator> | 78 typedef std::map<base::StringPiece, std::unique_ptr<Record>, Comparator> |
| 74 DataType; | 79 DataType; |
| 75 | 80 |
| 81 // A DataIterator walks the uncommitted data in a transaction. It wraps a |
| 82 // std::map::iterator and provides the LevelDBIterator API. It is only used |
| 83 // internally as part of the implementation of TransactionIterator. |
| 76 class DataIterator : public LevelDBIterator { | 84 class DataIterator : public LevelDBIterator { |
| 77 public: | 85 public: |
| 78 static std::unique_ptr<DataIterator> Create( | 86 static std::unique_ptr<DataIterator> Create( |
| 79 LevelDBTransaction* transaction); | 87 LevelDBTransaction* transaction); |
| 80 ~DataIterator() override; | 88 ~DataIterator() override; |
| 81 | |
| 82 bool IsValid() const override; | 89 bool IsValid() const override; |
| 83 leveldb::Status SeekToLast() override; | 90 leveldb::Status SeekToLast() override; |
| 84 leveldb::Status Seek(const base::StringPiece& slice) override; | 91 leveldb::Status Seek(const base::StringPiece& slice) override; |
| 85 leveldb::Status Next() override; | 92 leveldb::Status Next() override; |
| 86 leveldb::Status Prev() override; | 93 leveldb::Status Prev() override; |
| 87 base::StringPiece Key() const override; | 94 base::StringPiece Key() const override; |
| 88 base::StringPiece Value() const override; | 95 base::StringPiece Value() const override; |
| 89 bool IsDeleted() const; | 96 bool IsDeleted() const; |
| 90 | 97 |
| 98 // Mark the current record as deleted. |
| 99 void Delete(); |
| 100 |
| 91 private: | 101 private: |
| 92 explicit DataIterator(LevelDBTransaction* transaction); | 102 explicit DataIterator(LevelDBTransaction* transaction); |
| 93 DataType* data_; | 103 DataType* data_; |
| 94 DataType::iterator iterator_; | 104 DataType::iterator iterator_; |
| 95 | 105 |
| 96 DISALLOW_COPY_AND_ASSIGN(DataIterator); | 106 DISALLOW_COPY_AND_ASSIGN(DataIterator); |
| 97 }; | 107 }; |
| 98 | 108 |
| 109 // A TransactionIterator wraps a pair of a DataIterator (which walks the |
| 110 // uncommitted data) and LevelDBIterator wrapping a leveldb::Iterator (which |
| 111 // walks the data previously committed to the backing store). This provides |
| 112 // a unified view on committed and uncommitted data. |
| 99 class TransactionIterator : public LevelDBIterator { | 113 class TransactionIterator : public LevelDBIterator { |
| 100 public: | 114 public: |
| 101 ~TransactionIterator() override; | 115 ~TransactionIterator() override; |
| 102 static std::unique_ptr<TransactionIterator> Create( | 116 static std::unique_ptr<TransactionIterator> Create( |
| 103 scoped_refptr<LevelDBTransaction> transaction); | 117 scoped_refptr<LevelDBTransaction> transaction); |
| 104 | 118 |
| 105 bool IsValid() const override; | 119 bool IsValid() const override; |
| 106 leveldb::Status SeekToLast() override; | 120 leveldb::Status SeekToLast() override; |
| 107 leveldb::Status Seek(const base::StringPiece& target) override; | 121 leveldb::Status Seek(const base::StringPiece& target) override; |
| 108 leveldb::Status Next() override; | 122 leveldb::Status Next() override; |
| 109 leveldb::Status Prev() override; | 123 leveldb::Status Prev() override; |
| 110 base::StringPiece Key() const override; | 124 base::StringPiece Key() const override; |
| 111 base::StringPiece Value() const override; | 125 base::StringPiece Value() const override; |
| 112 void DataChanged(); | 126 void DataChanged(); |
| 113 | 127 |
| 128 // Mark the current record as deleted. If an existing record |
| 129 // is present in the uncommitted data this will convert it to |
| 130 // a deletion record, otherwise it will insert a new one. |
| 131 void Delete(); |
| 132 |
| 114 private: | 133 private: |
| 115 enum Direction { FORWARD, REVERSE }; | 134 enum Direction { FORWARD, REVERSE }; |
| 116 | 135 |
| 117 explicit TransactionIterator(scoped_refptr<LevelDBTransaction> transaction); | 136 explicit TransactionIterator(scoped_refptr<LevelDBTransaction> transaction); |
| 118 void HandleConflictsAndDeletes(); | 137 void HandleConflictsAndDeletes(); |
| 119 void SetCurrentIteratorToSmallestKey(); | 138 void SetCurrentIteratorToSmallestKey(); |
| 120 void SetCurrentIteratorToLargestKey(); | 139 void SetCurrentIteratorToLargestKey(); |
| 121 void RefreshDataIterator() const; | 140 void RefreshDataIterator() const; |
| 122 bool DataIteratorIsLower() const; | 141 bool DataIteratorIsLower() const; |
| 123 bool DataIteratorIsHigher() const; | 142 bool DataIteratorIsHigher() const; |
| 124 | 143 |
| 125 scoped_refptr<LevelDBTransaction> transaction_; | 144 scoped_refptr<LevelDBTransaction> transaction_; |
| 126 const LevelDBComparator* comparator_; | 145 const LevelDBComparator* comparator_; |
| 127 mutable std::unique_ptr<DataIterator> data_iterator_; | 146 mutable std::unique_ptr<DataIterator> data_iterator_; |
| 128 std::unique_ptr<LevelDBIterator> db_iterator_; | 147 std::unique_ptr<LevelDBIterator> db_iterator_; |
| 129 LevelDBIterator* current_; | 148 LevelDBIterator* current_ = nullptr; |
| 130 | 149 |
| 131 Direction direction_; | 150 Direction direction_ = FORWARD; |
| 132 mutable bool data_changed_; | 151 mutable bool data_changed_ = false; |
| 133 | 152 |
| 134 DISALLOW_COPY_AND_ASSIGN(TransactionIterator); | 153 DISALLOW_COPY_AND_ASSIGN(TransactionIterator); |
| 135 }; | 154 }; |
| 136 // Returns true if the key was originally marked deleted, false otherwise. | 155 |
| 137 bool Set(const base::StringPiece& key, std::string* value, bool deleted); | 156 void Set(const base::StringPiece& key, std::string* value, bool deleted); |
| 138 void RegisterIterator(TransactionIterator* iterator); | 157 void RegisterIterator(TransactionIterator* iterator); |
| 139 void UnregisterIterator(TransactionIterator* iterator); | 158 void UnregisterIterator(TransactionIterator* iterator); |
| 140 void NotifyIterators(); | 159 void NotifyIterators(); |
| 141 | 160 |
| 142 LevelDBDatabase* db_; | 161 LevelDBDatabase* db_; |
| 143 const LevelDBSnapshot snapshot_; | 162 const LevelDBSnapshot snapshot_; |
| 144 const LevelDBComparator* comparator_; | 163 const LevelDBComparator* comparator_; |
| 145 Comparator data_comparator_; | 164 Comparator data_comparator_; |
| 146 DataType data_; | 165 DataType data_; |
| 147 bool finished_; | 166 bool finished_ = false; |
| 148 std::set<TransactionIterator*> iterators_; | 167 std::set<TransactionIterator*> iterators_; |
| 149 | 168 |
| 150 DISALLOW_COPY_AND_ASSIGN(LevelDBTransaction); | 169 DISALLOW_COPY_AND_ASSIGN(LevelDBTransaction); |
| 151 }; | 170 }; |
| 152 | 171 |
| 153 // Reads go straight to the database, ignoring any writes cached in | 172 // Reads go straight to the database, ignoring any writes cached in |
| 154 // write_batch_, and writes are write-through, without consolidation. | 173 // write_batch_, and writes are write-through, without consolidation. |
| 155 class LevelDBDirectTransaction { | 174 class LevelDBDirectTransaction { |
| 156 public: | 175 public: |
| 157 static std::unique_ptr<LevelDBDirectTransaction> Create(LevelDBDatabase* db); | 176 static std::unique_ptr<LevelDBDirectTransaction> Create(LevelDBDatabase* db); |
| 158 | 177 |
| 159 ~LevelDBDirectTransaction(); | 178 ~LevelDBDirectTransaction(); |
| 160 void Put(const base::StringPiece& key, const std::string* value); | 179 void Put(const base::StringPiece& key, const std::string* value); |
| 161 leveldb::Status Get(const base::StringPiece& key, | 180 leveldb::Status Get(const base::StringPiece& key, |
| 162 std::string* value, | 181 std::string* value, |
| 163 bool* found); | 182 bool* found); |
| 164 void Remove(const base::StringPiece& key); | 183 void Remove(const base::StringPiece& key); |
| 165 leveldb::Status Commit(); | 184 leveldb::Status Commit(); |
| 166 | 185 |
| 167 private: | 186 private: |
| 168 explicit LevelDBDirectTransaction(LevelDBDatabase* db); | 187 explicit LevelDBDirectTransaction(LevelDBDatabase* db); |
| 169 | 188 |
| 170 LevelDBDatabase* db_; | 189 LevelDBDatabase* db_; |
| 171 std::unique_ptr<LevelDBWriteBatch> write_batch_; | 190 std::unique_ptr<LevelDBWriteBatch> write_batch_; |
| 172 bool finished_; | 191 bool finished_ = false; |
| 173 | 192 |
| 174 DISALLOW_COPY_AND_ASSIGN(LevelDBDirectTransaction); | 193 DISALLOW_COPY_AND_ASSIGN(LevelDBDirectTransaction); |
| 175 }; | 194 }; |
| 176 | 195 |
| 177 } // namespace content | 196 } // namespace content |
| 178 | 197 |
| 179 #endif // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_ | 198 #endif // CONTENT_BROWSER_INDEXED_DB_LEVELDB_LEVELDB_TRANSACTION_H_ |
| OLD | NEW |