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 content { | 18 namespace content { |
19 | 19 |
20 LevelDBTransaction::LevelDBTransaction(LevelDBDatabase* db) | 20 LevelDBTransaction::LevelDBTransaction(LevelDBDatabase* db) |
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() {} |
29 LevelDBTransaction::Record::~Record() {} | 29 LevelDBTransaction::Record::~Record() {} |
30 | 30 |
31 LevelDBTransaction::~LevelDBTransaction() {} | 31 LevelDBTransaction::~LevelDBTransaction() {} |
32 | 32 |
33 bool LevelDBTransaction::Set(const StringPiece& key, | 33 bool LevelDBTransaction::Set(const StringPiece& key, |
34 std::string* value, | 34 std::string* value, |
35 bool deleted) { | 35 bool deleted) { |
36 DCHECK(!finished_); | 36 DCHECK(!finished_); |
37 DataType::iterator it = data_.find(key); | 37 DataType::iterator it = data_.find(key); |
38 | 38 |
(...skipping 14 matching lines...) Expand all Loading... |
53 | 53 |
54 void LevelDBTransaction::Put(const StringPiece& key, std::string* value) { | 54 void LevelDBTransaction::Put(const StringPiece& key, std::string* value) { |
55 Set(key, value, false); | 55 Set(key, value, false); |
56 } | 56 } |
57 | 57 |
58 bool LevelDBTransaction::Remove(const StringPiece& key) { | 58 bool LevelDBTransaction::Remove(const StringPiece& key) { |
59 std::string empty; | 59 std::string empty; |
60 return !Set(key, &empty, true); | 60 return !Set(key, &empty, true); |
61 } | 61 } |
62 | 62 |
| 63 leveldb::Status LevelDBTransaction::RemoveRange(const StringPiece& begin, |
| 64 const StringPiece& end, |
| 65 bool upper_open) { |
| 66 IDB_TRACE("LevelDBTransaction::RemoveRange"); |
| 67 data_.erase(data_.lower_bound(begin), data_.lower_bound(end)); |
| 68 if (!upper_open) |
| 69 data_.erase(end); |
| 70 std::unique_ptr<LevelDBIterator> it = CreateIterator(); |
| 71 leveldb::Status s; |
| 72 for (s = it->Seek(begin); |
| 73 s.ok() && it->IsValid() && |
| 74 (upper_open ? comparator_->Compare(it->Key(), end) < 0 |
| 75 : comparator_->Compare(it->Key(), end) <= 0); |
| 76 s = it->Next()) { |
| 77 std::unique_ptr<Record> record = base::MakeUnique<Record>(); |
| 78 record->key = it->Key().as_string(); |
| 79 record->deleted = true; |
| 80 data_[record->key] = std::move(record); |
| 81 } |
| 82 NotifyIterators(); |
| 83 return s; |
| 84 } |
| 85 |
63 leveldb::Status LevelDBTransaction::Get(const StringPiece& key, | 86 leveldb::Status LevelDBTransaction::Get(const StringPiece& key, |
64 std::string* value, | 87 std::string* value, |
65 bool* found) { | 88 bool* found) { |
66 *found = false; | 89 *found = false; |
67 DCHECK(!finished_); | 90 DCHECK(!finished_); |
68 std::string string_key(key.begin(), key.end() - key.begin()); | 91 std::string string_key(key.begin(), key.end() - key.begin()); |
69 DataType::const_iterator it = data_.find(string_key); | 92 DataType::const_iterator it = data_.find(string_key); |
70 | 93 |
71 if (it != data_.end()) { | 94 if (it != data_.end()) { |
72 if (it->second->deleted) | 95 if (it->second->deleted) |
(...skipping 414 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
487 | 510 |
488 leveldb::Status s = db_->Write(*write_batch_); | 511 leveldb::Status s = db_->Write(*write_batch_); |
489 if (s.ok()) { | 512 if (s.ok()) { |
490 finished_ = true; | 513 finished_ = true; |
491 write_batch_->Clear(); | 514 write_batch_->Clear(); |
492 } | 515 } |
493 return s; | 516 return s; |
494 } | 517 } |
495 | 518 |
496 } // namespace content | 519 } // namespace content |
OLD | NEW |