Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(383)

Side by Side Diff: content/browser/leveldb_wrapper_impl.cc

Issue 2583253002: Start adding tests for LevelDBWrapper, and fix a bunch of bugs. (Closed)
Patch Set: nicer Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after
48 } 48 }
49 49
50 LevelDBWrapperImpl::LevelDBWrapperImpl( 50 LevelDBWrapperImpl::LevelDBWrapperImpl(
51 leveldb::mojom::LevelDBDatabase* database, 51 leveldb::mojom::LevelDBDatabase* database,
52 const std::string& prefix, 52 const std::string& prefix,
53 size_t max_size, 53 size_t max_size,
54 base::TimeDelta default_commit_delay, 54 base::TimeDelta default_commit_delay,
55 int max_bytes_per_hour, 55 int max_bytes_per_hour,
56 int max_commits_per_hour, 56 int max_commits_per_hour,
57 const base::Closure& no_bindings_callback) 57 const base::Closure& no_bindings_callback)
58 : prefix_(prefix), 58 : prefix_(leveldb::StdStringToUint8Vector(prefix)),
59 no_bindings_callback_(no_bindings_callback), 59 no_bindings_callback_(no_bindings_callback),
60 database_(database), 60 database_(database),
61 bytes_used_(0), 61 bytes_used_(0),
62 max_size_(max_size), 62 max_size_(max_size),
63 start_time_(base::TimeTicks::Now()), 63 start_time_(base::TimeTicks::Now()),
64 default_commit_delay_(default_commit_delay), 64 default_commit_delay_(default_commit_delay),
65 data_rate_limiter_(max_bytes_per_hour, base::TimeDelta::FromHours(1)), 65 data_rate_limiter_(max_bytes_per_hour, base::TimeDelta::FromHours(1)),
66 commit_rate_limiter_(max_commits_per_hour, base::TimeDelta::FromHours(1)), 66 commit_rate_limiter_(max_commits_per_hour, base::TimeDelta::FromHours(1)),
67 weak_ptr_factory_(this) { 67 weak_ptr_factory_(this) {
68 bindings_.set_connection_error_handler(base::Bind( 68 bindings_.set_connection_error_handler(base::Bind(
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
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(leveldb::StdStringToUint8Vector(prefix_), 252 database_->GetPrefixed(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 std::vector<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] = it->value; 263 DCHECK_GE(it->key.size(), prefix_.size());
264 (*map_)[std::vector<uint8_t>(it->key.begin() + prefix_.size(),
265 it->key.end())] = it->value;
266 }
264 267
265 // We proceed without using a backing store, nothing will be persisted but the 268 // We proceed without using a backing store, nothing will be persisted but the
266 // class is functional for the lifetime of the object. 269 // class is functional for the lifetime of the object.
267 // TODO(michaeln): Uma here or in the DB file? 270 // TODO(michaeln): Uma here or in the DB file?
268 if (status != leveldb::mojom::DatabaseError::OK) 271 if (status != leveldb::mojom::DatabaseError::OK)
269 database_ = nullptr; 272 database_ = nullptr;
270 273
271 std::vector<base::Closure> tasks; 274 std::vector<base::Closure> tasks;
272 on_load_complete_tasks_.swap(tasks); 275 on_load_complete_tasks_.swap(tasks);
273 for (auto& task : tasks) 276 for (auto& task : tasks)
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
309 base::TimeDelta delay = std::max( 312 base::TimeDelta delay = std::max(
310 default_commit_delay_, 313 default_commit_delay_,
311 std::max(commit_rate_limiter_.ComputeDelayNeeded(elapsed_time), 314 std::max(commit_rate_limiter_.ComputeDelayNeeded(elapsed_time),
312 data_rate_limiter_.ComputeDelayNeeded(elapsed_time))); 315 data_rate_limiter_.ComputeDelayNeeded(elapsed_time)));
313 // TODO(michaeln): UMA_HISTOGRAM_LONG_TIMES("LevelDBWrapper.CommitDelay", d); 316 // TODO(michaeln): UMA_HISTOGRAM_LONG_TIMES("LevelDBWrapper.CommitDelay", d);
314 return delay; 317 return delay;
315 } 318 }
316 319
317 void LevelDBWrapperImpl::CommitChanges() { 320 void LevelDBWrapperImpl::CommitChanges() {
318 DCHECK(database_); 321 DCHECK(database_);
319 if (commit_batch_) 322 if (!commit_batch_)
320 return; 323 return;
321 324
322 commit_rate_limiter_.add_samples(1); 325 commit_rate_limiter_.add_samples(1);
323 data_rate_limiter_.add_samples(commit_batch_->GetDataSize()); 326 data_rate_limiter_.add_samples(commit_batch_->GetDataSize());
324 327
325 // Commit all our changes in a single batch. 328 // Commit all our changes in a single batch.
326 std::vector<leveldb::mojom::BatchedOperationPtr> operations; 329 std::vector<leveldb::mojom::BatchedOperationPtr> operations;
327 if (commit_batch_->clear_all_first) { 330 if (commit_batch_->clear_all_first) {
328 leveldb::mojom::BatchedOperationPtr item = 331 leveldb::mojom::BatchedOperationPtr item =
329 leveldb::mojom::BatchedOperation::New(); 332 leveldb::mojom::BatchedOperation::New();
330 item->type = leveldb::mojom::BatchOperationType::DELETE_PREFIXED_KEY; 333 item->type = leveldb::mojom::BatchOperationType::DELETE_PREFIXED_KEY;
331 item->key = leveldb::StdStringToUint8Vector(prefix_); 334 item->key = prefix_;
332 operations.push_back(std::move(item)); 335 operations.push_back(std::move(item));
333 } 336 }
334 for (auto& it : commit_batch_->changed_values) { 337 for (auto& it : commit_batch_->changed_values) {
335 leveldb::mojom::BatchedOperationPtr item = 338 leveldb::mojom::BatchedOperationPtr item =
336 leveldb::mojom::BatchedOperation::New(); 339 leveldb::mojom::BatchedOperation::New();
337 item->key = std::move(it.first); 340 item->key.reserve(prefix_.size() + it.first.size());
341 item->key.insert(item->key.end(), prefix_.begin(), prefix_.end());
342 item->key.insert(item->key.end(), it.first.begin(), it.first.end());
338 if (!it.second) { 343 if (!it.second) {
339 item->type = leveldb::mojom::BatchOperationType::DELETE_KEY; 344 item->type = leveldb::mojom::BatchOperationType::DELETE_KEY;
340 } else { 345 } else {
341 item->type = leveldb::mojom::BatchOperationType::PUT_KEY; 346 item->type = leveldb::mojom::BatchOperationType::PUT_KEY;
342 item->value = std::move(*(it.second)); 347 item->value = std::move(*(it.second));
343 } 348 }
344 operations.push_back(std::move(item)); 349 operations.push_back(std::move(item));
345 } 350 }
346 commit_batch_.reset(); 351 commit_batch_.reset();
347 352
348 ++commit_batches_in_flight_; 353 ++commit_batches_in_flight_;
349 354
350 // TODO(michaeln): Currently there is no guarantee LevelDBDatabaseImp::Write 355 // TODO(michaeln): Currently there is no guarantee LevelDBDatabaseImp::Write
351 // will run during a clean shutdown. We need that to avoid dataloss. 356 // will run during a clean shutdown. We need that to avoid dataloss.
352 database_->Write(std::move(operations), 357 database_->Write(std::move(operations),
353 base::Bind(&LevelDBWrapperImpl::OnCommitComplete, 358 base::Bind(&LevelDBWrapperImpl::OnCommitComplete,
354 weak_ptr_factory_.GetWeakPtr())); 359 weak_ptr_factory_.GetWeakPtr()));
355 } 360 }
356 361
357 void LevelDBWrapperImpl::OnCommitComplete(leveldb::mojom::DatabaseError error) { 362 void LevelDBWrapperImpl::OnCommitComplete(leveldb::mojom::DatabaseError error) {
358 // TODO(michaeln): What if it fails, uma here or in the DB class? 363 // TODO(michaeln): What if it fails, uma here or in the DB class?
359 --commit_batches_in_flight_; 364 --commit_batches_in_flight_;
360 StartCommitTimer(); 365 StartCommitTimer();
361 } 366 }
362 367
363 } // namespace content 368 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698