| 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_macros.h" | 9 #include "base/metrics/histogram_macros.h" |
| 10 #include "base/time/time.h" | 10 #include "base/time/time.h" |
| 11 #include "content/browser/indexed_db/indexed_db_tracing.h" | 11 #include "content/browser/indexed_db/indexed_db_tracing.h" |
| 12 #include "content/browser/indexed_db/leveldb/leveldb_database.h" | 12 #include "content/browser/indexed_db/leveldb/leveldb_database.h" |
| 13 #include "content/browser/indexed_db/leveldb/leveldb_write_batch.h" | 13 #include "content/browser/indexed_db/leveldb/leveldb_write_batch.h" |
| 14 #include "third_party/leveldatabase/src/include/leveldb/db.h" | 14 #include "third_party/leveldatabase/src/include/leveldb/db.h" |
| 15 | 15 |
| 16 using base::StringPiece; | 16 using base::StringPiece; |
| 17 | 17 |
| 18 namespace { | 18 namespace { |
| 19 | 19 |
| 20 // Tests if the given key is before the end of a range, which | 20 // Tests if the given key is before the end of a range, which |
| 21 // may have an open (exclusive) or closed (inclusive) bound. | 21 // may have an open (exclusive) or closed (inclusive) bound. |
| 22 bool IsKeyBeforeEndOfRange(const content::LevelDBComparator* comparator, | 22 bool IsKeyBeforeEndOfRange(const content::LevelDBComparator* comparator, |
| 23 const StringPiece& key, | 23 const StringPiece& key, |
| 24 const StringPiece& end, | 24 const StringPiece& end, |
| 25 bool open) { | 25 bool open) { |
| 26 return (open ? comparator->Compare(key, end) < 0 | 26 return (open ? comparator->Compare(key, end) < 0 |
| 27 : comparator->Compare(key, end) <= 0); | 27 : comparator->Compare(key, end) <= 0); |
| 28 } | 28 } |
| 29 | |
| 30 } // namespace | 29 } // namespace |
| 31 | 30 |
| 32 namespace content { | 31 namespace content { |
| 33 | 32 |
| 34 LevelDBTransaction::LevelDBTransaction(LevelDBDatabase* db) | 33 LevelDBTransaction::LevelDBTransaction(LevelDBDatabase* db) |
| 35 : db_(db), | 34 : db_(db), |
| 36 snapshot_(db), | 35 snapshot_(db), |
| 37 comparator_(db->Comparator()), | 36 comparator_(db->Comparator()), |
| 38 data_comparator_(comparator_), | 37 data_comparator_(comparator_), |
| 39 data_(data_comparator_) {} | 38 data_(data_comparator_) {} |
| 40 | 39 |
| 41 LevelDBTransaction::Record::Record() {} | 40 LevelDBTransaction::Record::Record() {} |
| 42 LevelDBTransaction::Record::~Record() {} | 41 LevelDBTransaction::Record::~Record() {} |
| 43 | 42 |
| 44 LevelDBTransaction::~LevelDBTransaction() {} | 43 LevelDBTransaction::~LevelDBTransaction() {} |
| 45 | 44 |
| 46 void LevelDBTransaction::Set(const StringPiece& key, | 45 void LevelDBTransaction::Set(const StringPiece& key, |
| 47 std::string* value, | 46 std::string* value, |
| 48 bool deleted) { | 47 bool deleted) { |
| 49 DCHECK(!finished_); | 48 DCHECK(!finished_); |
| 50 DataType::iterator it = data_.find(key); | 49 DataType::iterator it = data_.find(key); |
| 51 | 50 |
| 52 if (it == data_.end()) { | 51 if (it == data_.end()) { |
| 53 std::unique_ptr<Record> record = base::MakeUnique<Record>(); | 52 std::unique_ptr<Record> record = base::MakeUnique<Record>(); |
| 53 size_ += SizeOfRecordInMap(key.size()) + value->size(); |
| 54 record->key.assign(key.begin(), key.end() - key.begin()); | 54 record->key.assign(key.begin(), key.end() - key.begin()); |
| 55 record->value.swap(*value); | 55 record->value.swap(*value); |
| 56 record->deleted = deleted; | 56 record->deleted = deleted; |
| 57 data_[record->key] = std::move(record); | 57 data_[record->key] = std::move(record); |
| 58 NotifyIterators(); | 58 NotifyIterators(); |
| 59 return; | 59 return; |
| 60 } | 60 } |
| 61 size_ += value->size(); |
| 61 it->second->value.swap(*value); | 62 it->second->value.swap(*value); |
| 63 size_ -= value->size(); |
| 62 it->second->deleted = deleted; | 64 it->second->deleted = deleted; |
| 63 } | 65 } |
| 64 | 66 |
| 65 void LevelDBTransaction::Put(const StringPiece& key, std::string* value) { | 67 void LevelDBTransaction::Put(const StringPiece& key, std::string* value) { |
| 66 Set(key, value, false); | 68 Set(key, value, false); |
| 67 } | 69 } |
| 68 | 70 |
| 69 void LevelDBTransaction::Remove(const StringPiece& key) { | 71 void LevelDBTransaction::Remove(const StringPiece& key) { |
| 70 std::string empty; | 72 std::string empty; |
| 71 Set(key, &empty, true); | 73 Set(key, &empty, true); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 | 158 |
| 157 std::unique_ptr<LevelDBIterator> LevelDBTransaction::CreateIterator() { | 159 std::unique_ptr<LevelDBIterator> LevelDBTransaction::CreateIterator() { |
| 158 return TransactionIterator::Create(this); | 160 return TransactionIterator::Create(this); |
| 159 } | 161 } |
| 160 | 162 |
| 161 std::unique_ptr<LevelDBTransaction::DataIterator> | 163 std::unique_ptr<LevelDBTransaction::DataIterator> |
| 162 LevelDBTransaction::DataIterator::Create(LevelDBTransaction* transaction) { | 164 LevelDBTransaction::DataIterator::Create(LevelDBTransaction* transaction) { |
| 163 return base::WrapUnique(new DataIterator(transaction)); | 165 return base::WrapUnique(new DataIterator(transaction)); |
| 164 } | 166 } |
| 165 | 167 |
| 168 constexpr uint64_t LevelDBTransaction::SizeOfRecordInMap(size_t key_size) { |
| 169 return sizeof(Record) + key_size * 2; |
| 170 } |
| 171 |
| 166 bool LevelDBTransaction::DataIterator::IsValid() const { | 172 bool LevelDBTransaction::DataIterator::IsValid() const { |
| 167 return iterator_ != data_->end(); | 173 return iterator_ != data_->end(); |
| 168 } | 174 } |
| 169 | 175 |
| 170 leveldb::Status LevelDBTransaction::DataIterator::SeekToLast() { | 176 leveldb::Status LevelDBTransaction::DataIterator::SeekToLast() { |
| 171 iterator_ = data_->end(); | 177 iterator_ = data_->end(); |
| 172 if (iterator_ != data_->begin()) | 178 if (iterator_ != data_->begin()) |
| 173 --iterator_; | 179 --iterator_; |
| 174 return leveldb::Status::OK(); | 180 return leveldb::Status::OK(); |
| 175 } | 181 } |
| (...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 return db_iterator_->IsDetached(); | 373 return db_iterator_->IsDetached(); |
| 368 } | 374 } |
| 369 | 375 |
| 370 void LevelDBTransaction::TransactionIterator::DataChanged() { | 376 void LevelDBTransaction::TransactionIterator::DataChanged() { |
| 371 data_changed_ = true; | 377 data_changed_ = true; |
| 372 } | 378 } |
| 373 | 379 |
| 374 void LevelDBTransaction::TransactionIterator::Delete() { | 380 void LevelDBTransaction::TransactionIterator::Delete() { |
| 375 DCHECK(IsValid()); | 381 DCHECK(IsValid()); |
| 376 if (current_ == data_iterator_.get()) { | 382 if (current_ == data_iterator_.get()) { |
| 383 transaction_->size_ -= data_iterator_->Value().size(); |
| 377 data_iterator_->Delete(); | 384 data_iterator_->Delete(); |
| 378 } else { | 385 } else { |
| 379 std::unique_ptr<Record> record = base::MakeUnique<Record>(); | 386 std::unique_ptr<Record> record = base::MakeUnique<Record>(); |
| 380 record->key = Key().as_string(); | 387 record->key = Key().as_string(); |
| 381 record->deleted = true; | 388 record->deleted = true; |
| 389 transaction_->size_ += |
| 390 LevelDBTransaction::SizeOfRecordInMap(record->key.size()); |
| 382 transaction_->data_[record->key] = std::move(record); | 391 transaction_->data_[record->key] = std::move(record); |
| 383 } | 392 } |
| 384 } | 393 } |
| 385 | 394 |
| 386 void LevelDBTransaction::TransactionIterator::RefreshDataIterator() const { | 395 void LevelDBTransaction::TransactionIterator::RefreshDataIterator() const { |
| 387 DCHECK(data_changed_); | 396 DCHECK(data_changed_); |
| 388 | 397 |
| 389 data_changed_ = false; | 398 data_changed_ = false; |
| 390 | 399 |
| 391 if (data_iterator_->IsValid() && data_iterator_.get() == current_) { | 400 if (data_iterator_->IsValid() && data_iterator_.get() == current_) { |
| (...skipping 147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 539 | 548 |
| 540 leveldb::Status s = db_->Write(*write_batch_); | 549 leveldb::Status s = db_->Write(*write_batch_); |
| 541 if (s.ok()) { | 550 if (s.ok()) { |
| 542 finished_ = true; | 551 finished_ = true; |
| 543 write_batch_->Clear(); | 552 write_batch_->Clear(); |
| 544 } | 553 } |
| 545 return s; | 554 return s; |
| 546 } | 555 } |
| 547 | 556 |
| 548 } // namespace content | 557 } // namespace content |
| OLD | NEW |