Commit 328d3bee authored by Chris Mumford's avatar Chris Mumford Committed by Commit Bot

leveldb: Deleted leveldb_proto::Options.

leveldb_proto::Options is a small subset (mostly) of leveldb::Options.
It had two leveldb::Options members (shared_cache and write_buffer_size),
and a (possibly empty) database_name. This change deletes
leveldb_proto::Options in lieu of using leveldb::Options. This simplifies
the code as well as adds support for the remaining options specified
in leveldb::Options.

Bug: 762587
Change-Id: Ia7872ecb09b2b181354c820ac3296ea6304a80b0
Reviewed-on: https://chromium-review.googlesource.com/655697Reviewed-by: default avatarMathieu Perreault <mathp@chromium.org>
Reviewed-by: default avatarMin Qin <qinmin@chromium.org>
Reviewed-by: default avatarBernhard Bauer <bauerb@chromium.org>
Reviewed-by: default avatarJohn Mellor <johnme@chromium.org>
Commit-Queue: Chris Mumford <cmumford@chromium.org>
Cr-Commit-Position: refs/heads/master@{#501330}
parent fc369f2a
...@@ -57,11 +57,9 @@ BudgetDatabase::BudgetDatabase(Profile* profile, ...@@ -57,11 +57,9 @@ BudgetDatabase::BudgetDatabase(Profile* profile,
base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}))), base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}))),
clock_(base::WrapUnique(new base::DefaultClock)), clock_(base::WrapUnique(new base::DefaultClock)),
weak_ptr_factory_(this) { weak_ptr_factory_(this) {
db_->InitWithOptions( db_->InitWithOptions(kDatabaseUMAName, database_dir, leveldb_env::Options(),
kDatabaseUMAName, base::BindOnce(&BudgetDatabase::OnDatabaseInit,
leveldb_proto::Options(database_dir, SharedReadCache::Default), weak_ptr_factory_.GetWeakPtr()));
base::BindOnce(&BudgetDatabase::OnDatabaseInit,
weak_ptr_factory_.GetWeakPtr()));
} }
BudgetDatabase::~BudgetDatabase() {} BudgetDatabase::~BudgetDatabase() {}
......
...@@ -42,8 +42,7 @@ bool DownloadStore::IsInitialized() { ...@@ -42,8 +42,7 @@ bool DownloadStore::IsInitialized() {
void DownloadStore::Initialize(InitCallback callback) { void DownloadStore::Initialize(InitCallback callback) {
DCHECK(!IsInitialized()); DCHECK(!IsInitialized());
db_->InitWithOptions( db_->InitWithOptions(
kDatabaseClientName, kDatabaseClientName, database_dir_, leveldb_env::Options(),
leveldb_proto::Options(database_dir_, SharedReadCache::Default),
base::BindOnce(&DownloadStore::OnDatabaseInited, base::BindOnce(&DownloadStore::OnDatabaseInited,
weak_factory_.GetWeakPtr(), std::move(callback))); weak_factory_.GetWeakPtr(), std::move(callback)));
} }
...@@ -85,8 +84,7 @@ void DownloadStore::OnDatabaseDestroyed(StoreCallback callback, bool success) { ...@@ -85,8 +84,7 @@ void DownloadStore::OnDatabaseDestroyed(StoreCallback callback, bool success) {
} }
db_->InitWithOptions( db_->InitWithOptions(
kDatabaseClientName, kDatabaseClientName, database_dir_, leveldb_env::Options(),
leveldb_proto::Options(database_dir_, SharedReadCache::Default),
base::BindOnce(&DownloadStore::OnDatabaseInitedAfterDestroy, base::BindOnce(&DownloadStore::OnDatabaseInitedAfterDestroy,
weak_factory_.GetWeakPtr(), std::move(callback))); weak_factory_.GetWeakPtr(), std::move(callback)));
} }
......
...@@ -6,7 +6,6 @@ static_library("leveldb_proto") { ...@@ -6,7 +6,6 @@ static_library("leveldb_proto") {
sources = [ sources = [
"leveldb_database.cc", "leveldb_database.cc",
"leveldb_database.h", "leveldb_database.h",
"options.h",
"proto_database.h", "proto_database.h",
"proto_database_impl.h", "proto_database_impl.h",
] ]
......
...@@ -67,27 +67,18 @@ bool LevelDB::InitWithOptions(const base::FilePath& database_dir, ...@@ -67,27 +67,18 @@ bool LevelDB::InitWithOptions(const base::FilePath& database_dir,
return false; return false;
} }
bool LevelDB::Init(const leveldb_proto::Options& options) { bool LevelDB::Init(const base::FilePath& database_dir,
leveldb_env::Options leveldb_options; const leveldb_env::Options& options) {
leveldb_options.create_if_missing = true; leveldb_env::Options open_options = options;
leveldb_options.max_open_files = 0; // Use minimum. open_options.create_if_missing = true;
open_options.max_open_files = 0; // Use minimum.
if (options.write_buffer_size != 0)
leveldb_options.write_buffer_size = options.write_buffer_size; if (database_dir.empty()) {
switch (options.shared_cache) {
case leveldb_env::SharedReadCache::Web:
leveldb_options.block_cache = leveldb_env::SharedWebBlockCache();
break;
case leveldb_env::SharedReadCache::Default:
// fallthrough
break;
}
if (options.database_dir.empty()) {
env_.reset(leveldb::NewMemEnv(leveldb::Env::Default())); env_.reset(leveldb::NewMemEnv(leveldb::Env::Default()));
leveldb_options.env = env_.get(); open_options.env = env_.get();
} }
return InitWithOptions(options.database_dir, leveldb_options); return InitWithOptions(database_dir, open_options);
} }
bool LevelDB::Save(const base::StringPairs& entries_to_save, bool LevelDB::Save(const base::StringPairs& entries_to_save,
......
...@@ -13,7 +13,6 @@ ...@@ -13,7 +13,6 @@
#include "base/macros.h" #include "base/macros.h"
#include "base/strings/string_split.h" #include "base/strings/string_split.h"
#include "base/threading/thread_collision_warner.h" #include "base/threading/thread_collision_warner.h"
#include "components/leveldb_proto/options.h"
#include "third_party/leveldatabase/env_chromium.h" #include "third_party/leveldatabase/env_chromium.h"
namespace base { namespace base {
...@@ -42,9 +41,10 @@ class LevelDB { ...@@ -42,9 +41,10 @@ class LevelDB {
explicit LevelDB(const char* client_name); explicit LevelDB(const char* client_name);
virtual ~LevelDB(); virtual ~LevelDB();
// Initializes a leveldb with the given options. If |options.database_dir| is // Initializes a leveldb with the given options. If |database_dir| is
// empty, this opens an in-memory db. // empty, this opens an in-memory db.
virtual bool Init(const leveldb_proto::Options& options); virtual bool Init(const base::FilePath& database_dir,
const leveldb_env::Options& options);
virtual bool Save(const base::StringPairs& pairs_to_save, virtual bool Save(const base::StringPairs& pairs_to_save,
const std::vector<std::string>& keys_to_remove); const std::vector<std::string>& keys_to_remove);
......
// Copyright 2016 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.
#ifndef COMPONENTS_LEVELDB_PROTO_OPTIONS_H_
#define COMPONENTS_LEVELDB_PROTO_OPTIONS_H_
#include "base/files/file_path.h"
#include "third_party/leveldatabase/env_chromium.h"
namespace leveldb_proto {
struct Options {
Options(const base::FilePath& database_dir,
leveldb_env::SharedReadCache shared_cache,
size_t write_buffer_size = 0)
: database_dir(database_dir),
shared_cache(shared_cache),
write_buffer_size(write_buffer_size) {}
base::FilePath database_dir;
leveldb_env::SharedReadCache shared_cache;
size_t write_buffer_size;
};
} // namespace leveldb_proto
#endif // COMPONENTS_LEVELDB_PROTO_OPTIONS_H_
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
#include <vector> #include <vector>
#include "base/callback.h" #include "base/callback.h"
#include "components/leveldb_proto/options.h" #include "third_party/leveldatabase/env_chromium.h"
namespace base { namespace base {
class FilePath; class FilePath;
...@@ -45,15 +45,14 @@ class ProtoDatabase { ...@@ -45,15 +45,14 @@ class ProtoDatabase {
void Init(const char* client_name, void Init(const char* client_name,
const base::FilePath& database_dir, const base::FilePath& database_dir,
InitCallback callback) { InitCallback callback) {
InitWithOptions( InitWithOptions(client_name, database_dir, leveldb_env::Options(),
client_name, std::move(callback));
Options(database_dir, leveldb_env::SharedReadCache::Default),
std::move(callback));
} }
// Similar to Init, but takes additional options. // Similar to Init, but takes additional options.
virtual void InitWithOptions(const char* client_name, virtual void InitWithOptions(const char* client_name,
const Options& options, const base::FilePath& database_dir,
const leveldb_env::Options& options,
InitCallback callback) = 0; InitCallback callback) = 0;
// Asynchronously saves |entries_to_save| and deletes entries from // Asynchronously saves |entries_to_save| and deletes entries from
......
...@@ -45,7 +45,8 @@ class ProtoDatabaseImpl : public ProtoDatabase<T> { ...@@ -45,7 +45,8 @@ class ProtoDatabaseImpl : public ProtoDatabase<T> {
// part of the constructor // part of the constructor
void InitWithOptions( void InitWithOptions(
const char* client_name, const char* client_name,
const Options& options, const base::FilePath& database_dir,
const leveldb_env::Options& options,
typename ProtoDatabase<T>::InitCallback callback) override; typename ProtoDatabase<T>::InitCallback callback) override;
void UpdateEntries( void UpdateEntries(
std::unique_ptr<typename ProtoDatabase<T>::KeyEntryVector> std::unique_ptr<typename ProtoDatabase<T>::KeyEntryVector>
...@@ -60,7 +61,8 @@ class ProtoDatabaseImpl : public ProtoDatabase<T> { ...@@ -60,7 +61,8 @@ class ProtoDatabaseImpl : public ProtoDatabase<T> {
// Allow callers to provide their own Database implementation. // Allow callers to provide their own Database implementation.
void InitWithDatabase(std::unique_ptr<LevelDB> database, void InitWithDatabase(std::unique_ptr<LevelDB> database,
const Options& options, const base::FilePath& database_dir,
const leveldb_env::Options& options,
typename ProtoDatabase<T>::InitCallback callback); typename ProtoDatabase<T>::InitCallback callback);
private: private:
...@@ -118,12 +120,13 @@ void RunDestroyCallback(typename ProtoDatabase<T>::DestroyCallback callback, ...@@ -118,12 +120,13 @@ void RunDestroyCallback(typename ProtoDatabase<T>::DestroyCallback callback,
} }
inline void InitFromTaskRunner(LevelDB* database, inline void InitFromTaskRunner(LevelDB* database,
const Options& options, const base::FilePath& database_dir,
const leveldb_env::Options& options,
bool* success) { bool* success) {
DCHECK(success); DCHECK(success);
// TODO(cjhopman): Histogram for database size. // TODO(cjhopman): Histogram for database size.
*success = database->Init(options); *success = database->Init(database_dir, options);
} }
inline void DestroyFromTaskRunner(const base::FilePath& database_dir, inline void DestroyFromTaskRunner(const base::FilePath& database_dir,
...@@ -220,12 +223,13 @@ ProtoDatabaseImpl<T>::~ProtoDatabaseImpl() { ...@@ -220,12 +223,13 @@ ProtoDatabaseImpl<T>::~ProtoDatabaseImpl() {
template <typename T> template <typename T>
void ProtoDatabaseImpl<T>::InitWithOptions( void ProtoDatabaseImpl<T>::InitWithOptions(
const char* client_name, const char* client_name,
const Options& options, const base::FilePath& database_dir,
const leveldb_env::Options& options,
typename ProtoDatabase<T>::InitCallback callback) { typename ProtoDatabase<T>::InitCallback callback) {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
database_dir_ = options.database_dir; database_dir_ = database_dir;
InitWithDatabase(base::WrapUnique(new LevelDB(client_name)), options, InitWithDatabase(base::WrapUnique(new LevelDB(client_name)), database_dir,
std::move(callback)); options, std::move(callback));
} }
template <typename T> template <typename T>
...@@ -253,7 +257,8 @@ void ProtoDatabaseImpl<T>::Destroy( ...@@ -253,7 +257,8 @@ void ProtoDatabaseImpl<T>::Destroy(
template <typename T> template <typename T>
void ProtoDatabaseImpl<T>::InitWithDatabase( void ProtoDatabaseImpl<T>::InitWithDatabase(
std::unique_ptr<LevelDB> database, std::unique_ptr<LevelDB> database,
const Options& options, const base::FilePath& database_dir,
const leveldb_env::Options& options,
typename ProtoDatabase<T>::InitCallback callback) { typename ProtoDatabase<T>::InitCallback callback) {
DCHECK(thread_checker_.CalledOnValidThread()); DCHECK(thread_checker_.CalledOnValidThread());
DCHECK(!db_); DCHECK(!db_);
...@@ -262,8 +267,8 @@ void ProtoDatabaseImpl<T>::InitWithDatabase( ...@@ -262,8 +267,8 @@ void ProtoDatabaseImpl<T>::InitWithDatabase(
bool* success = new bool(false); bool* success = new bool(false);
task_runner_->PostTaskAndReply( task_runner_->PostTaskAndReply(
FROM_HERE, FROM_HERE,
base::Bind(InitFromTaskRunner, base::Unretained(db_.get()), options, base::Bind(InitFromTaskRunner, base::Unretained(db_.get()), database_dir,
success), options, success),
base::BindOnce(RunInitCallback<T>, std::move(callback), base::BindOnce(RunInitCallback<T>, std::move(callback),
base::Owned(success))); base::Owned(success)));
} }
......
...@@ -32,7 +32,8 @@ class FakeDB : public ProtoDatabase<T> { ...@@ -32,7 +32,8 @@ class FakeDB : public ProtoDatabase<T> {
// ProtoDatabase implementation. // ProtoDatabase implementation.
void InitWithOptions( void InitWithOptions(
const char* client_name, const char* client_name,
const Options& options, const base::FilePath& database_dir,
const leveldb_env::Options& options,
typename ProtoDatabase<T>::InitCallback callback) override; typename ProtoDatabase<T>::InitCallback callback) override;
void UpdateEntries( void UpdateEntries(
std::unique_ptr<typename ProtoDatabase<T>::KeyEntryVector> std::unique_ptr<typename ProtoDatabase<T>::KeyEntryVector>
...@@ -96,9 +97,10 @@ FakeDB<T>::~FakeDB() {} ...@@ -96,9 +97,10 @@ FakeDB<T>::~FakeDB() {}
template <typename T> template <typename T>
void FakeDB<T>::InitWithOptions( void FakeDB<T>::InitWithOptions(
const char* client_name, const char* client_name,
const Options& options, const base::FilePath& database_dir,
const leveldb_env::Options& options,
typename ProtoDatabase<T>::InitCallback callback) { typename ProtoDatabase<T>::InitCallback callback) {
dir_ = options.database_dir; dir_ = database_dir;
init_callback_ = std::move(callback); init_callback_ = std::move(callback);
} }
......
...@@ -45,18 +45,16 @@ RemoteSuggestionsDatabase::RemoteSuggestionsDatabase( ...@@ -45,18 +45,16 @@ RemoteSuggestionsDatabase::RemoteSuggestionsDatabase(
base::MakeUnique<ProtoDatabaseImpl<SnippetImageProto>>(file_task_runner); base::MakeUnique<ProtoDatabaseImpl<SnippetImageProto>>(file_task_runner);
base::FilePath snippet_dir = database_dir.AppendASCII(kSnippetDatabaseFolder); base::FilePath snippet_dir = database_dir.AppendASCII(kSnippetDatabaseFolder);
leveldb_env::Options options;
options.write_buffer_size = kDatabaseWriteBufferSizeBytes;
database_->InitWithOptions( database_->InitWithOptions(
kDatabaseUMAClientName, kDatabaseUMAClientName, snippet_dir, options,
leveldb_proto::Options(snippet_dir, SharedReadCache::Default,
kDatabaseWriteBufferSizeBytes),
base::Bind(&RemoteSuggestionsDatabase::OnDatabaseInited, base::Bind(&RemoteSuggestionsDatabase::OnDatabaseInited,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
base::FilePath image_dir = database_dir.AppendASCII(kImageDatabaseFolder); base::FilePath image_dir = database_dir.AppendASCII(kImageDatabaseFolder);
image_database_->InitWithOptions( image_database_->InitWithOptions(
kImageDatabaseUMAClientName, kImageDatabaseUMAClientName, image_dir, options,
leveldb_proto::Options(image_dir, SharedReadCache::Default,
kDatabaseWriteBufferSizeBytes),
base::Bind(&RemoteSuggestionsDatabase::OnImageDatabaseInited, base::Bind(&RemoteSuggestionsDatabase::OnImageDatabaseInited,
weak_ptr_factory_.GetWeakPtr())); weak_ptr_factory_.GetWeakPtr()));
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment