Skip to content

Commit

Permalink
Introduce metadata database to SharedProtoDatabase.
Browse files Browse the repository at this point in the history
The metadata database is primarily for storing corruption information
at the moment. Adds the creation/loading of entries from the metadata
DB on shared database init, and causes the database load to fail if
metadata can't be loaded, since that would be potentially dangerous.

Adds an async check for corruption information during
SharedProtoDatabaseClient init, and sets the corruption flag accordingly
so IsCorrupt() is correct.

Bug: 870813
Change-Id: Id24cd2c5c2aeed21d0e8bb108cc4126c3acb3049
Reviewed-on: https://chromium-review.googlesource.com/c/1356224
Commit-Queue: Troy Hildebrandt <thildebr@chromium.org>
Reviewed-by: Tommy Nyquist <nyquist@chromium.org>
Cr-Commit-Position: refs/heads/master@{#619703}
  • Loading branch information
Troy Hildebrandt authored and Commit Bot committed Jan 3, 2019
1 parent 36ab751 commit 7baaeb3
Show file tree
Hide file tree
Showing 11 changed files with 506 additions and 40 deletions.
12 changes: 12 additions & 0 deletions components/leveldb_proto/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

import("//third_party/protobuf/proto_library.gni")

proto_library("proto") {
sources = [
"proto/shared_db_metadata.proto",
]
}

static_library("leveldb_proto") {
sources = [
"leveldb_database.cc",
Expand All @@ -12,6 +20,7 @@ static_library("leveldb_proto") {
"proto_database_impl.h",
"proto_database_provider.cc",
"proto_database_provider.h",
"proto_database_wrapper.cc",
"proto_database_wrapper.h",
"proto_leveldb_wrapper.cc",
"proto_leveldb_wrapper.h",
Expand All @@ -27,6 +36,9 @@ static_library("leveldb_proto") {
"shared_proto_database_provider.h",
"unique_proto_database.h",
]
deps = [
":proto",
]

public_deps = [
"//base",
Expand Down
25 changes: 25 additions & 0 deletions components/leveldb_proto/proto/shared_db_metadata.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

syntax = "proto2";

option optimize_for = LITE_RUNTIME;

package leveldb_proto;

// A proto to for storing metadata related to a SharedProtoDatabase client.
//
// Next tag: 3
message SharedDBMetadataProto {
// For the global database, this is value increases every time a corruption
// is discovered, as a way for clients to determine whether there's been a
// corruption since the last time they checked.
// For clients, this number is set to the global value when they check their
// corruption status, to indicate that we don't need to tell them there was
// a corruption next time they check.
optional uint64 corruptions = 1;

// Timestamp for when the database was modified.
optional uint64 last_modified = 2;
}
19 changes: 19 additions & 0 deletions components/leveldb_proto/proto_database_wrapper.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright 2018 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/leveldb_proto/proto_database_wrapper.h"

#include "components/leveldb_proto/proto_database_provider.h"

namespace leveldb_proto {

void GetSharedDBInstance(
ProtoDatabaseProvider* db_provider,
base::OnceCallback<void(scoped_refptr<SharedProtoDatabase>)> callback) {
DCHECK(base::SequencedTaskRunnerHandle::IsSet());
db_provider->GetSharedDBInstance(std::move(callback),
base::SequencedTaskRunnerHandle::Get());
}

} // namespace leveldb_proto
9 changes: 9 additions & 0 deletions components/leveldb_proto/proto_database_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@

namespace leveldb_proto {

class ProtoDatabaseProvider;

// Avoids circular dependencies between ProtoDatabaseWrapper and
// ProtoDatabaseProvider, since we'd need to include the provider header here
// to use |db_provider_|'s GetSharedDBInstance.
void GetSharedDBInstance(
ProtoDatabaseProvider* db_provider,
base::OnceCallback<void(scoped_refptr<SharedProtoDatabase>)> callback);

// The ProtoDatabaseWrapper<T> owns a ProtoDatabase<T> instance, and allows the
// underlying ProtoDatabase<T> implementation to change without users of the
// wrapper needing to know.
Expand Down
9 changes: 7 additions & 2 deletions components/leveldb_proto/proto_leveldb_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,24 @@ void LoadKeysFromTaskRunner(

ProtoLevelDBWrapper::ProtoLevelDBWrapper(
const scoped_refptr<base::SequencedTaskRunner>& task_runner)
: task_runner_(task_runner) {
: task_runner_(task_runner), weak_ptr_factory_(this) {
DETACH_FROM_SEQUENCE(sequence_checker_);
}

ProtoLevelDBWrapper::ProtoLevelDBWrapper(
const scoped_refptr<base::SequencedTaskRunner>& task_runner,
LevelDB* db)
: task_runner_(task_runner), db_(db) {
: task_runner_(task_runner), db_(db), weak_ptr_factory_(this) {
DETACH_FROM_SEQUENCE(sequence_checker_);
}

ProtoLevelDBWrapper::~ProtoLevelDBWrapper() = default;

void ProtoLevelDBWrapper::RunInitCallback(Callbacks::InitCallback callback,
const leveldb::Status* status) {
std::move(callback).Run(status->ok());
}

void ProtoLevelDBWrapper::InitWithDatabase(
LevelDB* database,
const base::FilePath& database_dir,
Expand Down
5 changes: 5 additions & 0 deletions components/leveldb_proto/proto_leveldb_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ class ProtoLevelDBWrapper {

void Destroy(Callbacks::DestroyCallback callback);

void RunInitCallback(Callbacks::InitCallback callback,
const leveldb::Status* status);

// Allow callers to provide their own Database implementation.
void InitWithDatabase(LevelDB* database,
const base::FilePath& database_dir,
Expand All @@ -133,6 +136,8 @@ class ProtoLevelDBWrapper {
// LevelDB calls, likely the database client name.
std::string metrics_id_ = "Default";

base::WeakPtrFactory<ProtoLevelDBWrapper> weak_ptr_factory_;

DISALLOW_COPY_AND_ASSIGN(ProtoLevelDBWrapper);
};

Expand Down
Loading

0 comments on commit 7baaeb3

Please sign in to comment.