| 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 #include "content/browser/indexed_db/leveldb/leveldb_transaction.h" | 5 #include "content/browser/indexed_db/leveldb/leveldb_transaction.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "base/memory/ptr_util.h" | 8 #include "base/memory/ptr_util.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 : db_(db), | 21 : db_(db), |
| 22 snapshot_(db), | 22 snapshot_(db), |
| 23 comparator_(db->Comparator()), | 23 comparator_(db->Comparator()), |
| 24 data_comparator_(comparator_), | 24 data_comparator_(comparator_), |
| 25 data_(data_comparator_), | 25 data_(data_comparator_), |
| 26 finished_(false) {} | 26 finished_(false) {} |
| 27 | 27 |
| 28 LevelDBTransaction::Record::Record() : deleted(false) {} | 28 LevelDBTransaction::Record::Record() : deleted(false) {} |
| 29 LevelDBTransaction::Record::~Record() {} | 29 LevelDBTransaction::Record::~Record() {} |
| 30 | 30 |
| 31 void LevelDBTransaction::Clear() { | 31 LevelDBTransaction::~LevelDBTransaction() {} |
| 32 for (const auto& it : data_) | |
| 33 delete it.second; | |
| 34 data_.clear(); | |
| 35 } | |
| 36 | |
| 37 LevelDBTransaction::~LevelDBTransaction() { Clear(); } | |
| 38 | 32 |
| 39 bool LevelDBTransaction::Set(const StringPiece& key, | 33 bool LevelDBTransaction::Set(const StringPiece& key, |
| 40 std::string* value, | 34 std::string* value, |
| 41 bool deleted) { | 35 bool deleted) { |
| 42 DCHECK(!finished_); | 36 DCHECK(!finished_); |
| 43 DataType::iterator it = data_.find(key); | 37 DataType::iterator it = data_.find(key); |
| 44 | 38 |
| 45 if (it == data_.end()) { | 39 if (it == data_.end()) { |
| 46 Record* record = new Record(); | 40 std::unique_ptr<Record> record = base::MakeUnique<Record>(); |
| 47 record->key.assign(key.begin(), key.end() - key.begin()); | 41 record->key.assign(key.begin(), key.end() - key.begin()); |
| 48 record->value.swap(*value); | 42 record->value.swap(*value); |
| 49 record->deleted = deleted; | 43 record->deleted = deleted; |
| 50 data_[record->key] = record; | 44 data_[record->key] = std::move(record); |
| 51 NotifyIterators(); | 45 NotifyIterators(); |
| 52 return false; | 46 return false; |
| 53 } | 47 } |
| 54 bool replaced_deleted_value = it->second->deleted; | 48 bool replaced_deleted_value = it->second->deleted; |
| 55 it->second->value.swap(*value); | 49 it->second->value.swap(*value); |
| 56 it->second->deleted = deleted; | 50 it->second->deleted = deleted; |
| 57 return replaced_deleted_value; | 51 return replaced_deleted_value; |
| 58 } | 52 } |
| 59 | 53 |
| 60 void LevelDBTransaction::Put(const StringPiece& key, std::string* value) { | 54 void LevelDBTransaction::Put(const StringPiece& key, std::string* value) { |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 | 94 |
| 101 base::TimeTicks begin_time = base::TimeTicks::Now(); | 95 base::TimeTicks begin_time = base::TimeTicks::Now(); |
| 102 std::unique_ptr<LevelDBWriteBatch> write_batch = LevelDBWriteBatch::Create(); | 96 std::unique_ptr<LevelDBWriteBatch> write_batch = LevelDBWriteBatch::Create(); |
| 103 | 97 |
| 104 auto it = data_.begin(); | 98 auto it = data_.begin(); |
| 105 while (it != data_.end()) { | 99 while (it != data_.end()) { |
| 106 if (!it->second->deleted) | 100 if (!it->second->deleted) |
| 107 write_batch->Put(it->first, it->second->value); | 101 write_batch->Put(it->first, it->second->value); |
| 108 else | 102 else |
| 109 write_batch->Remove(it->first); | 103 write_batch->Remove(it->first); |
| 110 | |
| 111 delete it->second; | |
| 112 data_.erase(it++); | 104 data_.erase(it++); |
| 113 } | 105 } |
| 114 | 106 |
| 115 DCHECK(data_.empty()); | 107 DCHECK(data_.empty()); |
| 116 | 108 |
| 117 leveldb::Status s = db_->Write(*write_batch); | 109 leveldb::Status s = db_->Write(*write_batch); |
| 118 if (s.ok()) { | 110 if (s.ok()) { |
| 119 finished_ = true; | 111 finished_ = true; |
| 120 UMA_HISTOGRAM_TIMES("WebCore.IndexedDB.LevelDB.Transaction.CommitTime", | 112 UMA_HISTOGRAM_TIMES("WebCore.IndexedDB.LevelDB.Transaction.CommitTime", |
| 121 base::TimeTicks::Now() - begin_time); | 113 base::TimeTicks::Now() - begin_time); |
| 122 } | 114 } |
| 123 return s; | 115 return s; |
| 124 } | 116 } |
| 125 | 117 |
| 126 void LevelDBTransaction::Rollback() { | 118 void LevelDBTransaction::Rollback() { |
| 127 DCHECK(!finished_); | 119 DCHECK(!finished_); |
| 128 finished_ = true; | 120 finished_ = true; |
| 129 Clear(); | 121 data_.clear(); |
| 130 } | 122 } |
| 131 | 123 |
| 132 std::unique_ptr<LevelDBIterator> LevelDBTransaction::CreateIterator() { | 124 std::unique_ptr<LevelDBIterator> LevelDBTransaction::CreateIterator() { |
| 133 return TransactionIterator::Create(this); | 125 return TransactionIterator::Create(this); |
| 134 } | 126 } |
| 135 | 127 |
| 136 std::unique_ptr<LevelDBTransaction::DataIterator> | 128 std::unique_ptr<LevelDBTransaction::DataIterator> |
| 137 LevelDBTransaction::DataIterator::Create(LevelDBTransaction* transaction) { | 129 LevelDBTransaction::DataIterator::Create(LevelDBTransaction* transaction) { |
| 138 return base::WrapUnique(new DataIterator(transaction)); | 130 return base::WrapUnique(new DataIterator(transaction)); |
| 139 } | 131 } |
| (...skipping 355 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 495 | 487 |
| 496 leveldb::Status s = db_->Write(*write_batch_); | 488 leveldb::Status s = db_->Write(*write_batch_); |
| 497 if (s.ok()) { | 489 if (s.ok()) { |
| 498 finished_ = true; | 490 finished_ = true; |
| 499 write_batch_->Clear(); | 491 write_batch_->Clear(); |
| 500 } | 492 } |
| 501 return s; | 493 return s; |
| 502 } | 494 } |
| 503 | 495 |
| 504 } // namespace content | 496 } // namespace content |
| OLD | NEW |