OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome/browser/sync_file_system/drive_backend/leveldb_wrapper.h" | 5 #include "chrome/browser/sync_file_system/drive_backend/leveldb_wrapper.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
71 DCHECK(Valid()); | 71 DCHECK(Valid()); |
72 | 72 |
73 const std::string key_str = key().ToString(); | 73 const std::string key_str = key().ToString(); |
74 Transaction deletion(DELETE_OPERATION, std::string()); | 74 Transaction deletion(DELETE_OPERATION, std::string()); |
75 map_iterator_ = db_->pending_.insert(map_iterator_, | 75 map_iterator_ = db_->pending_.insert(map_iterator_, |
76 std::make_pair(key_str, deletion)); | 76 std::make_pair(key_str, deletion)); |
77 // In case that |db_->pending_| already had an entry for the key, we have to | 77 // In case that |db_->pending_| already had an entry for the key, we have to |
78 // update the value. | 78 // update the value. |
79 map_iterator_->second = deletion; | 79 map_iterator_->second = deletion; |
80 | 80 |
| 81 ++(db_->num_deletes_); |
| 82 |
81 AdvanceIterators(); | 83 AdvanceIterators(); |
82 } | 84 } |
83 | 85 |
84 leveldb::Slice LevelDBWrapper::Iterator::key() { | 86 leveldb::Slice LevelDBWrapper::Iterator::key() { |
85 DCHECK(Valid()); | 87 DCHECK(Valid()); |
86 | 88 |
87 if (!db_iterator_->Valid()) | 89 if (!db_iterator_->Valid()) |
88 return map_iterator_->first; | 90 return map_iterator_->first; |
89 if (map_iterator_ == db_->pending_.end()) | 91 if (map_iterator_ == db_->pending_.end()) |
90 return db_iterator_->key(); | 92 return db_iterator_->key(); |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
135 | 137 |
136 while (map_iterator_ != db_->pending_.end() && | 138 while (map_iterator_ != db_->pending_.end() && |
137 map_iterator_->second.first == DELETE_OPERATION) | 139 map_iterator_->second.first == DELETE_OPERATION) |
138 ++map_iterator_; | 140 ++map_iterator_; |
139 } | 141 } |
140 | 142 |
141 // --------------------------------------------------------------------------- | 143 // --------------------------------------------------------------------------- |
142 // LevelDBWrapper class | 144 // LevelDBWrapper class |
143 // --------------------------------------------------------------------------- | 145 // --------------------------------------------------------------------------- |
144 LevelDBWrapper::LevelDBWrapper(scoped_ptr<leveldb::DB> db) | 146 LevelDBWrapper::LevelDBWrapper(scoped_ptr<leveldb::DB> db) |
145 : db_(db.Pass()) { | 147 : db_(db.Pass()), num_puts_(0), num_deletes_(0) { |
146 DCHECK(db_); | 148 DCHECK(db_); |
147 } | 149 } |
148 | 150 |
149 LevelDBWrapper::~LevelDBWrapper() {} | 151 LevelDBWrapper::~LevelDBWrapper() {} |
150 | 152 |
151 void LevelDBWrapper::Put(const std::string& key, | 153 void LevelDBWrapper::Put(const std::string& key, const std::string& value) { |
152 const std::string& value) { | |
153 pending_[key] = Transaction(PUT_OPERATION, value); | 154 pending_[key] = Transaction(PUT_OPERATION, value); |
| 155 ++num_puts_; |
154 } | 156 } |
155 | 157 |
156 void LevelDBWrapper::Delete(const std::string& key) { | 158 void LevelDBWrapper::Delete(const std::string& key) { |
157 pending_[key] = Transaction(DELETE_OPERATION, std::string()); | 159 pending_[key] = Transaction(DELETE_OPERATION, std::string()); |
| 160 ++num_deletes_; |
158 } | 161 } |
159 | 162 |
160 leveldb::Status LevelDBWrapper::Get(const std::string& key, | 163 leveldb::Status LevelDBWrapper::Get(const std::string& key, |
161 std::string* value) { | 164 std::string* value) { |
162 PendingOperationMap::iterator itr = pending_.find(key); | 165 PendingOperationMap::iterator itr = pending_.find(key); |
163 if (itr == pending_.end()) | 166 if (itr == pending_.end()) |
164 return db_->Get(leveldb::ReadOptions(), key, value); | 167 return db_->Get(leveldb::ReadOptions(), key, value); |
165 | 168 |
166 const Transaction& transaction = itr->second; | 169 const Transaction& transaction = itr->second; |
167 switch (transaction.first) { | 170 switch (transaction.first) { |
(...skipping 30 matching lines...) Expand all Loading... |
198 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch); | 201 leveldb::Status status = db_->Write(leveldb::WriteOptions(), &batch); |
199 // TODO(peria): Decide what to do depending on |status|. | 202 // TODO(peria): Decide what to do depending on |status|. |
200 if (status.ok()) | 203 if (status.ok()) |
201 Clear(); | 204 Clear(); |
202 | 205 |
203 return status; | 206 return status; |
204 } | 207 } |
205 | 208 |
206 void LevelDBWrapper::Clear() { | 209 void LevelDBWrapper::Clear() { |
207 pending_.clear(); | 210 pending_.clear(); |
| 211 num_puts_ = 0; |
| 212 num_deletes_ = 0; |
208 } | 213 } |
209 | 214 |
210 leveldb::DB* LevelDBWrapper::GetLevelDB() { | 215 leveldb::DB* LevelDBWrapper::GetLevelDB() { |
211 return db_.get(); | 216 return db_.get(); |
212 } | 217 } |
213 | 218 |
214 } // namespace drive_backend | 219 } // namespace drive_backend |
215 } // namespace sync_file_system | 220 } // namespace sync_file_system |
OLD | NEW |