| 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 "components/leveldb/public/cpp/util.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 LevelDBWrapperImpl::CommitBatch::CommitBatch() : clear_all_first(false) {} | 34 LevelDBWrapperImpl::CommitBatch::CommitBatch() : clear_all_first(false) {} |
| 35 LevelDBWrapperImpl::CommitBatch::~CommitBatch() {} | 35 LevelDBWrapperImpl::CommitBatch::~CommitBatch() {} |
| 36 | 36 |
| 37 LevelDBWrapperImpl::LevelDBWrapperImpl( | 37 LevelDBWrapperImpl::LevelDBWrapperImpl( |
| 38 leveldb::mojom::LevelDBDatabase* database, | 38 leveldb::mojom::LevelDBDatabase* database, |
| 39 const std::string& prefix, | 39 const std::string& prefix, |
| 40 size_t max_size, | 40 size_t max_size, |
| 41 base::TimeDelta default_commit_delay, | 41 base::TimeDelta default_commit_delay, |
| 42 int max_bytes_per_hour, | 42 int max_bytes_per_hour, |
| 43 int max_commits_per_hour, | 43 int max_commits_per_hour, |
| 44 const base::Closure& no_bindings_callback) | 44 const base::Closure& no_bindings_callback, |
| 45 const PrepareToCommitCallback& prepare_to_commit_callback) |
| 45 : prefix_(leveldb::StdStringToUint8Vector(prefix)), | 46 : prefix_(leveldb::StdStringToUint8Vector(prefix)), |
| 46 no_bindings_callback_(no_bindings_callback), | 47 no_bindings_callback_(no_bindings_callback), |
| 48 prepare_to_commit_callback_(prepare_to_commit_callback), |
| 47 database_(database), | 49 database_(database), |
| 48 bytes_used_(0), | 50 bytes_used_(0), |
| 49 max_size_(max_size), | 51 max_size_(max_size), |
| 50 start_time_(base::TimeTicks::Now()), | 52 start_time_(base::TimeTicks::Now()), |
| 51 default_commit_delay_(default_commit_delay), | 53 default_commit_delay_(default_commit_delay), |
| 52 data_rate_limiter_(max_bytes_per_hour, base::TimeDelta::FromHours(1)), | 54 data_rate_limiter_(max_bytes_per_hour, base::TimeDelta::FromHours(1)), |
| 53 commit_rate_limiter_(max_commits_per_hour, base::TimeDelta::FromHours(1)), | 55 commit_rate_limiter_(max_commits_per_hour, base::TimeDelta::FromHours(1)), |
| 54 weak_ptr_factory_(this) { | 56 weak_ptr_factory_(this) { |
| 55 bindings_.set_connection_error_handler(base::Bind( | 57 bindings_.set_connection_error_handler(base::Bind( |
| 56 &LevelDBWrapperImpl::OnConnectionError, base::Unretained(this))); | 58 &LevelDBWrapperImpl::OnConnectionError, base::Unretained(this))); |
| (...skipping 270 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 | 329 |
| 328 void LevelDBWrapperImpl::CommitChanges() { | 330 void LevelDBWrapperImpl::CommitChanges() { |
| 329 DCHECK(database_); | 331 DCHECK(database_); |
| 330 DCHECK(map_); | 332 DCHECK(map_); |
| 331 if (!commit_batch_) | 333 if (!commit_batch_) |
| 332 return; | 334 return; |
| 333 | 335 |
| 334 commit_rate_limiter_.add_samples(1); | 336 commit_rate_limiter_.add_samples(1); |
| 335 | 337 |
| 336 // Commit all our changes in a single batch. | 338 // Commit all our changes in a single batch. |
| 337 std::vector<leveldb::mojom::BatchedOperationPtr> operations; | 339 std::vector<leveldb::mojom::BatchedOperationPtr> operations = |
| 340 prepare_to_commit_callback_.Run(); |
| 338 if (commit_batch_->clear_all_first) { | 341 if (commit_batch_->clear_all_first) { |
| 339 leveldb::mojom::BatchedOperationPtr item = | 342 leveldb::mojom::BatchedOperationPtr item = |
| 340 leveldb::mojom::BatchedOperation::New(); | 343 leveldb::mojom::BatchedOperation::New(); |
| 341 item->type = leveldb::mojom::BatchOperationType::DELETE_PREFIXED_KEY; | 344 item->type = leveldb::mojom::BatchOperationType::DELETE_PREFIXED_KEY; |
| 342 item->key = prefix_; | 345 item->key = prefix_; |
| 343 operations.push_back(std::move(item)); | 346 operations.push_back(std::move(item)); |
| 344 } | 347 } |
| 345 size_t data_size = 0; | 348 size_t data_size = 0; |
| 346 for (const auto& key: commit_batch_->changed_keys) { | 349 for (const auto& key: commit_batch_->changed_keys) { |
| 347 data_size += key.size(); | 350 data_size += key.size(); |
| (...skipping 25 matching lines...) Expand all Loading... |
| 373 weak_ptr_factory_.GetWeakPtr())); | 376 weak_ptr_factory_.GetWeakPtr())); |
| 374 } | 377 } |
| 375 | 378 |
| 376 void LevelDBWrapperImpl::OnCommitComplete(leveldb::mojom::DatabaseError error) { | 379 void LevelDBWrapperImpl::OnCommitComplete(leveldb::mojom::DatabaseError error) { |
| 377 // TODO(michaeln): What if it fails, uma here or in the DB class? | 380 // TODO(michaeln): What if it fails, uma here or in the DB class? |
| 378 --commit_batches_in_flight_; | 381 --commit_batches_in_flight_; |
| 379 StartCommitTimer(); | 382 StartCommitTimer(); |
| 380 } | 383 } |
| 381 | 384 |
| 382 } // namespace content | 385 } // namespace content |
| OLD | NEW |