| OLD | NEW |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/leveldb_wrapper_impl.h" | 5 #include "content/browser/leveldb_wrapper_impl.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/threading/thread_task_runner_handle.h" | 8 #include "base/threading/thread_task_runner_handle.h" |
| 9 #include "components/leveldb/public/cpp/util.h" |
| 9 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 10 #include "mojo/common/common_type_converters.h" | |
| 11 | 11 |
| 12 namespace content { | 12 namespace content { |
| 13 | 13 |
| 14 bool LevelDBWrapperImpl::s_aggressive_flushing_enabled_ = false; | 14 bool LevelDBWrapperImpl::s_aggressive_flushing_enabled_ = false; |
| 15 | 15 |
| 16 LevelDBWrapperImpl::RateLimiter::RateLimiter(size_t desired_rate, | 16 LevelDBWrapperImpl::RateLimiter::RateLimiter(size_t desired_rate, |
| 17 base::TimeDelta time_quantum) | 17 base::TimeDelta time_quantum) |
| 18 : rate_(desired_rate), samples_(0), time_quantum_(time_quantum) { | 18 : rate_(desired_rate), samples_(0), time_quantum_(time_quantum) { |
| 19 DCHECK_GT(desired_rate, 0ul); | 19 DCHECK_GT(desired_rate, 0ul); |
| 20 } | 20 } |
| (...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 242 no_bindings_callback_.Run(); | 242 no_bindings_callback_.Run(); |
| 243 } | 243 } |
| 244 | 244 |
| 245 void LevelDBWrapperImpl::LoadMap(const base::Closure& completion_callback) { | 245 void LevelDBWrapperImpl::LoadMap(const base::Closure& completion_callback) { |
| 246 DCHECK(!map_); | 246 DCHECK(!map_); |
| 247 on_load_complete_tasks_.push_back(completion_callback); | 247 on_load_complete_tasks_.push_back(completion_callback); |
| 248 if (on_load_complete_tasks_.size() > 1) | 248 if (on_load_complete_tasks_.size() > 1) |
| 249 return; | 249 return; |
| 250 | 250 |
| 251 // TODO(michaeln): Import from sqlite localstorage db. | 251 // TODO(michaeln): Import from sqlite localstorage db. |
| 252 database_->GetPrefixed(mojo::Array<uint8_t>::From(prefix_), | 252 database_->GetPrefixed(leveldb::StdStringToUint8Vector(prefix_), |
| 253 base::Bind(&LevelDBWrapperImpl::OnLoadComplete, | 253 base::Bind(&LevelDBWrapperImpl::OnLoadComplete, |
| 254 weak_ptr_factory_.GetWeakPtr())); | 254 weak_ptr_factory_.GetWeakPtr())); |
| 255 } | 255 } |
| 256 | 256 |
| 257 void LevelDBWrapperImpl::OnLoadComplete( | 257 void LevelDBWrapperImpl::OnLoadComplete( |
| 258 leveldb::mojom::DatabaseError status, | 258 leveldb::mojom::DatabaseError status, |
| 259 mojo::Array<leveldb::mojom::KeyValuePtr> data) { | 259 std::vector<leveldb::mojom::KeyValuePtr> data) { |
| 260 DCHECK(!map_); | 260 DCHECK(!map_); |
| 261 map_.reset(new ValueMap); | 261 map_.reset(new ValueMap); |
| 262 for (auto& it : data) | 262 for (auto& it : data) |
| 263 (*map_)[it->key.PassStorage()] = it->value.PassStorage(); | 263 (*map_)[it->key] = it->value; |
| 264 | 264 |
| 265 // We proceed without using a backing store, nothing will be persisted but the | 265 // We proceed without using a backing store, nothing will be persisted but the |
| 266 // class is functional for the lifetime of the object. | 266 // class is functional for the lifetime of the object. |
| 267 // TODO(michaeln): Uma here or in the DB file? | 267 // TODO(michaeln): Uma here or in the DB file? |
| 268 if (status != leveldb::mojom::DatabaseError::OK) | 268 if (status != leveldb::mojom::DatabaseError::OK) |
| 269 database_ = nullptr; | 269 database_ = nullptr; |
| 270 | 270 |
| 271 std::vector<base::Closure> tasks; | 271 std::vector<base::Closure> tasks; |
| 272 on_load_complete_tasks_.swap(tasks); | 272 on_load_complete_tasks_.swap(tasks); |
| 273 for (auto& task : tasks) | 273 for (auto& task : tasks) |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 316 | 316 |
| 317 void LevelDBWrapperImpl::CommitChanges() { | 317 void LevelDBWrapperImpl::CommitChanges() { |
| 318 DCHECK(database_); | 318 DCHECK(database_); |
| 319 if (commit_batch_) | 319 if (commit_batch_) |
| 320 return; | 320 return; |
| 321 | 321 |
| 322 commit_rate_limiter_.add_samples(1); | 322 commit_rate_limiter_.add_samples(1); |
| 323 data_rate_limiter_.add_samples(commit_batch_->GetDataSize()); | 323 data_rate_limiter_.add_samples(commit_batch_->GetDataSize()); |
| 324 | 324 |
| 325 // Commit all our changes in a single batch. | 325 // Commit all our changes in a single batch. |
| 326 mojo::Array<leveldb::mojom::BatchedOperationPtr> operations; | 326 std::vector<leveldb::mojom::BatchedOperationPtr> operations; |
| 327 if (commit_batch_->clear_all_first) { | 327 if (commit_batch_->clear_all_first) { |
| 328 leveldb::mojom::BatchedOperationPtr item = | 328 leveldb::mojom::BatchedOperationPtr item = |
| 329 leveldb::mojom::BatchedOperation::New(); | 329 leveldb::mojom::BatchedOperation::New(); |
| 330 item->type = leveldb::mojom::BatchOperationType::DELETE_PREFIXED_KEY; | 330 item->type = leveldb::mojom::BatchOperationType::DELETE_PREFIXED_KEY; |
| 331 item->key = mojo::Array<uint8_t>::From(std::string(prefix_)); | 331 item->key = leveldb::StdStringToUint8Vector(prefix_); |
| 332 operations.push_back(std::move(item)); | 332 operations.push_back(std::move(item)); |
| 333 } | 333 } |
| 334 for (auto& it : commit_batch_->changed_values) { | 334 for (auto& it : commit_batch_->changed_values) { |
| 335 leveldb::mojom::BatchedOperationPtr item = | 335 leveldb::mojom::BatchedOperationPtr item = |
| 336 leveldb::mojom::BatchedOperation::New(); | 336 leveldb::mojom::BatchedOperation::New(); |
| 337 item->key = std::vector<uint8_t>(it.first); | 337 item->key = std::move(it.first); |
| 338 if (!it.second) { | 338 if (!it.second) { |
| 339 item->type = leveldb::mojom::BatchOperationType::DELETE_KEY; | 339 item->type = leveldb::mojom::BatchOperationType::DELETE_KEY; |
| 340 } else { | 340 } else { |
| 341 item->type = leveldb::mojom::BatchOperationType::PUT_KEY; | 341 item->type = leveldb::mojom::BatchOperationType::PUT_KEY; |
| 342 item->value = std::move(*(it.second)); | 342 item->value = std::move(*(it.second)); |
| 343 } | 343 } |
| 344 operations.push_back(std::move(item)); | 344 operations.push_back(std::move(item)); |
| 345 } | 345 } |
| 346 commit_batch_.reset(); | 346 commit_batch_.reset(); |
| 347 | 347 |
| 348 ++commit_batches_in_flight_; | 348 ++commit_batches_in_flight_; |
| 349 | 349 |
| 350 // TODO(michaeln): Currently there is no guarantee LevelDBDatabaseImp::Write | 350 // TODO(michaeln): Currently there is no guarantee LevelDBDatabaseImp::Write |
| 351 // will run during a clean shutdown. We need that to avoid dataloss. | 351 // will run during a clean shutdown. We need that to avoid dataloss. |
| 352 database_->Write(std::move(operations), | 352 database_->Write(std::move(operations), |
| 353 base::Bind(&LevelDBWrapperImpl::OnCommitComplete, | 353 base::Bind(&LevelDBWrapperImpl::OnCommitComplete, |
| 354 weak_ptr_factory_.GetWeakPtr())); | 354 weak_ptr_factory_.GetWeakPtr())); |
| 355 } | 355 } |
| 356 | 356 |
| 357 void LevelDBWrapperImpl::OnCommitComplete(leveldb::mojom::DatabaseError error) { | 357 void LevelDBWrapperImpl::OnCommitComplete(leveldb::mojom::DatabaseError error) { |
| 358 // TODO(michaeln): What if it fails, uma here or in the DB class? | 358 // TODO(michaeln): What if it fails, uma here or in the DB class? |
| 359 --commit_batches_in_flight_; | 359 --commit_batches_in_flight_; |
| 360 StartCommitTimer(); | 360 StartCommitTimer(); |
| 361 } | 361 } |
| 362 | 362 |
| 363 } // namespace content | 363 } // namespace content |
| OLD | NEW |