1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef COMPONENTS_LEVELDB_PROTO_INTERNAL_PROTO_DATABASE_IMPL_H_
#define COMPONENTS_LEVELDB_PROTO_INTERNAL_PROTO_DATABASE_IMPL_H_
#include <map>
#include <memory>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include "base/files/file_path.h"
#include "base/functional/bind.h"
#include "base/logging.h"
#include "base/task/sequenced_task_runner.h"
#include "components/leveldb_proto/internal/leveldb_database.h"
#include "components/leveldb_proto/internal/proto_database_selector.h"
#include "components/leveldb_proto/internal/shared_proto_database_provider.h"
#include "components/leveldb_proto/public/proto_database.h"
#include "components/leveldb_proto/public/shared_proto_database_client_list.h"
namespace google {
namespace protobuf {
class MessageLite;
} // namespace protobuf
} // namespace google
namespace leveldb_proto {
// Update transactions happen on background task runner and callback runs on the
// client task runner.
void COMPONENT_EXPORT(LEVELDB_PROTO) RunUpdateCallback(
scoped_refptr<base::SequencedTaskRunner> callback_task_runner,
Callbacks::UpdateCallback callback,
bool success);
// Load transactions happen on background task runner. The loaded keys need to
// be given to clients on client task runner.
void COMPONENT_EXPORT(LEVELDB_PROTO) RunLoadKeysCallback(
scoped_refptr<base::SequencedTaskRunner> callback_task_runner,
Callbacks::LoadKeysCallback callback,
bool success,
std::unique_ptr<KeyVector> keys);
// Helper to run destroy callback on the client task runner.
void COMPONENT_EXPORT(LEVELDB_PROTO) RunDestroyCallback(
scoped_refptr<base::SequencedTaskRunner> callback_task_runner,
Callbacks::DestroyCallback callback,
bool success);
// The ProtoDatabaseImpl<T> implements a ProtoDatabase<T> instance, and allows
// the underlying ProtoDatabase<T> implementation to change without users of the
// wrapper needing to know.
// This allows clients to request a DB instance without knowing whether or not
// it's a UniqueProtoDatabase or a SharedProtoDatabaseClient.
template <typename P, typename T = P>
class ProtoDatabaseImpl : public ProtoDatabase<P, T> {
public:
// Force usage of unique db.
ProtoDatabaseImpl(
ProtoDbType db_type,
const base::FilePath& db_dir,
const scoped_refptr<base::SequencedTaskRunner>& task_runner);
// Internal implementation is free to choose between unique and shared
// database to use here (transparently).
ProtoDatabaseImpl(ProtoDbType db_type,
const base::FilePath& db_dir,
const scoped_refptr<base::SequencedTaskRunner>& task_runner,
std::unique_ptr<SharedProtoDatabaseProvider> db_provider);
virtual ~ProtoDatabaseImpl() = default;
void Init(Callbacks::InitStatusCallback callback) override;
void Init(const leveldb_env::Options& unique_db_options,
Callbacks::InitStatusCallback callback) override;
// Internal only api.
void InitWithDatabase(LevelDB* database,
const base::FilePath& database_dir,
const leveldb_env::Options& options,
Callbacks::InitStatusCallback callback);
void UpdateEntries(std::unique_ptr<typename ProtoDatabase<T>::KeyEntryVector>
entries_to_save,
std::unique_ptr<KeyVector> keys_to_remove,
Callbacks::UpdateCallback callback) override;
void UpdateEntriesWithRemoveFilter(
std::unique_ptr<typename ProtoDatabase<T>::KeyEntryVector>
entries_to_save,
const KeyFilter& delete_key_filter,
Callbacks::UpdateCallback callback) override;
void LoadEntries(
typename Callbacks::Internal<T>::LoadCallback callback) override;
void LoadEntriesWithFilter(
const KeyFilter& filter,
typename Callbacks::Internal<T>::LoadCallback callback) override;
void LoadEntriesWithFilter(
const KeyFilter& key_filter,
const leveldb::ReadOptions& options,
const std::string& target_prefix,
typename Callbacks::Internal<T>::LoadCallback callback) override;
void LoadKeysAndEntries(
typename Callbacks::Internal<T>::LoadKeysAndEntriesCallback callback)
override;
void LoadKeysAndEntriesWithFilter(
const KeyFilter& filter,
typename Callbacks::Internal<T>::LoadKeysAndEntriesCallback callback)
override;
void LoadKeysAndEntriesWithFilter(
const KeyFilter& filter,
const leveldb::ReadOptions& options,
const std::string& target_prefix,
typename Callbacks::Internal<T>::LoadKeysAndEntriesCallback callback)
override;
void LoadKeysAndEntriesInRange(
const std::string& start,
const std::string& end,
typename Callbacks::Internal<T>::LoadKeysAndEntriesCallback callback)
override;
void LoadKeysAndEntriesWhile(
const std::string& start,
const KeyIteratorController& controller,
typename Callbacks::Internal<T>::LoadKeysAndEntriesCallback callback)
override;
void LoadKeys(Callbacks::LoadKeysCallback callback) override;
void GetEntry(const std::string& key,
typename Callbacks::Internal<T>::GetCallback callback) override;
void Destroy(Callbacks::DestroyCallback callback) override;
void RemoveKeysForTesting(const KeyFilter& key_filter,
const std::string& target_prefix,
Callbacks::UpdateCallback callback);
// Not thread safe.
ProtoDatabaseSelector* db_wrapper_for_testing() { return db_wrapper_.get(); }
private:
template <typename T_>
friend class ProtoDatabaseImplTest;
void InitInternal(const std::string& client_name,
const leveldb_env::Options& options,
bool use_shared_db,
Callbacks::InitStatusCallback callback);
void PostTransaction(base::OnceClosure task);
ProtoDbType db_type_;
scoped_refptr<ProtoDatabaseSelector> db_wrapper_;
const bool force_unique_db_;
const scoped_refptr<base::SequencedTaskRunner> task_runner_;
base::FilePath db_dir_;
};
namespace {
template <typename P, typename T>
std::string SerializeAsString(T* entry) {
if constexpr (std::is_base_of_v<google::protobuf::MessageLite, T>) {
return entry->SerializeAsString();
} else {
P proto;
DataToProto(entry, &proto);
return proto.SerializeAsString();
}
}
template <typename P>
bool ParseToProto(const std::string& serialized_entry, P* proto) {
if (!proto->ParseFromString(serialized_entry)) {
DLOG(WARNING) << "Unable to parse leveldb_proto entry";
*proto = P();
return false;
}
return true;
}
template <typename P, typename T>
bool ParseToClientType(const std::string& serialized_entry, T* output) {
if constexpr (std::is_base_of_v<google::protobuf::MessageLite, T>) {
return ParseToProto<T>(serialized_entry, output);
} else {
P proto;
if (!ParseToProto<P>(serialized_entry, &proto)) {
return false;
}
ProtoToData(&proto, output);
return true;
}
}
// Update transactions need to serialize the entries to be updated on background
// task runner. The database can be accessed on same task runner. The caller
// must wrap the callback using RunUpdateCallback() to ensure the callback runs
// in client task runner.
template <typename P, typename T>
void UpdateEntriesFromTaskRunner(
std::unique_ptr<typename Util::Internal<T>::KeyEntryVector> entries_to_save,
std::unique_ptr<KeyVector> keys_to_remove,
scoped_refptr<ProtoDatabaseSelector> db,
Callbacks::UpdateCallback callback) {
// Serialize the values from Proto to string before passing on to database.
auto pairs_to_save = std::make_unique<KeyValueVector>();
for (auto& pair : *entries_to_save) {
auto serialized = SerializeAsString<P, T>(&pair.second);
pairs_to_save->push_back(std::make_pair(pair.first, serialized));
}
db->UpdateEntries(std::move(pairs_to_save), std::move(keys_to_remove),
std::move(callback));
}
// Update transactions need to serialize the entries to be updated on background
// task runner. The database can be accessed on same task runner. The caller
// must wrap the callback using RunUpdateCallback() to ensure the callback runs
// in client task runner.
template <typename P, typename T>
void UpdateEntriesWithRemoveFilterFromTaskRunner(
std::unique_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save,
const KeyFilter& delete_key_filter,
scoped_refptr<ProtoDatabaseSelector> db,
Callbacks::UpdateCallback callback) {
// Serialize the values from Proto to string before passing on to database.
auto pairs_to_save = std::make_unique<KeyValueVector>();
for (auto& pair : *entries_to_save) {
auto serialized = SerializeAsString<P, T>(&pair.second);
pairs_to_save->push_back(std::make_pair(pair.first, serialized));
}
db->UpdateEntriesWithRemoveFilter(std::move(pairs_to_save), delete_key_filter,
std::move(callback));
}
// Load transactions happen on background task runner. The loaded entries need
// to be parsed into proto in background thread. This wraps the load callback
// and parses the entries and posts result onto client task runner.
template <typename P, typename T>
void ParseLoadedEntries(
scoped_refptr<base::SequencedTaskRunner> callback_task_runner,
typename Callbacks::Internal<T>::LoadCallback callback,
bool success,
std::unique_ptr<ValueVector> loaded_entries) {
auto entries = std::make_unique<std::vector<T>>();
if (!success || !loaded_entries) {
entries.reset();
} else {
for (const auto& serialized_entry : *loaded_entries) {
entries->emplace_back(T());
ParseToClientType<P, T>(serialized_entry, &entries->back());
}
}
callback_task_runner->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), success, std::move(entries)));
}
// Load transactions happen on background task runner. The loaded entries need
// to be parsed into proto in background thread. This wraps the load callback
// and parses the entries and posts result onto client task runner.
template <typename P, typename T>
void ParseLoadedKeysAndEntries(
scoped_refptr<base::SequencedTaskRunner> callback_task_runner,
typename Callbacks::Internal<T>::LoadKeysAndEntriesCallback callback,
bool success,
std::unique_ptr<KeyValueMap> loaded_entries) {
auto keys_entries = std::make_unique<std::map<std::string, T>>();
if (!success || !loaded_entries) {
keys_entries.reset();
} else {
for (const auto& pair : *loaded_entries) {
auto it = keys_entries->emplace(pair.first, T());
ParseToClientType<P, T>(pair.second, &(it.first->second));
}
}
callback_task_runner->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), success, std::move(keys_entries)));
}
// Load transactions happen on background task runner. The loaded entries need
// to be parsed into proto in background thread. This wraps the load callback
// and parses the entries and posts result onto client task runner.
template <typename P, typename T>
void ParseLoadedEntry(
scoped_refptr<base::SequencedTaskRunner> callback_task_runner,
typename Callbacks::Internal<T>::GetCallback callback,
bool success,
std::unique_ptr<std::string> serialized_entry) {
auto entry = std::make_unique<T>();
if (!success || !serialized_entry) {
entry.reset();
} else if (!ParseToClientType<P, T>(*serialized_entry, entry.get())) {
success = false;
}
callback_task_runner->PostTask(
FROM_HERE,
base::BindOnce(std::move(callback), success, std::move(entry)));
}
} // namespace
template <typename P, typename T>
ProtoDatabaseImpl<P, T>::ProtoDatabaseImpl(
ProtoDbType db_type,
const base::FilePath& db_dir,
const scoped_refptr<base::SequencedTaskRunner>& task_runner)
: db_type_(db_type),
db_wrapper_(new ProtoDatabaseSelector(db_type_, task_runner, nullptr)),
force_unique_db_(true),
task_runner_(task_runner),
db_dir_(db_dir) {}
template <typename P, typename T>
ProtoDatabaseImpl<P, T>::ProtoDatabaseImpl(
ProtoDbType db_type,
const base::FilePath& db_dir,
const scoped_refptr<base::SequencedTaskRunner>& task_runner,
std::unique_ptr<SharedProtoDatabaseProvider> db_provider)
: db_type_(db_type),
db_wrapper_(new ProtoDatabaseSelector(db_type_,
task_runner,
std::move(db_provider))),
force_unique_db_(false),
task_runner_(task_runner),
db_dir_(db_dir) {}
template <typename P, typename T>
void ProtoDatabaseImpl<P, T>::Init(
typename Callbacks::InitStatusCallback callback) {
bool use_shared_db =
!force_unique_db_ &&
SharedProtoDatabaseClientList::ShouldUseSharedDB(db_type_);
const std::string& client_uma_name =
SharedProtoDatabaseClientList::ProtoDbTypeToString(db_type_);
InitInternal(client_uma_name, CreateSimpleOptions(), use_shared_db,
std::move(callback));
}
template <typename P, typename T>
void ProtoDatabaseImpl<P, T>::Init(
const leveldb_env::Options& unique_db_options,
typename Callbacks::InitStatusCallback callback) {
bool use_shared_db =
!force_unique_db_ &&
SharedProtoDatabaseClientList::ShouldUseSharedDB(db_type_);
const std::string& client_uma_name =
SharedProtoDatabaseClientList::ProtoDbTypeToString(db_type_);
InitInternal(client_uma_name, unique_db_options, use_shared_db,
std::move(callback));
}
template <typename P, typename T>
void ProtoDatabaseImpl<P, T>::InitInternal(
const std::string& client_name,
const leveldb_env::Options& unique_db_options,
bool use_shared_db,
Callbacks::InitStatusCallback callback) {
task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&ProtoDatabaseSelector::InitUniqueOrShared, db_wrapper_,
client_name, db_dir_, unique_db_options, use_shared_db,
base::SequencedTaskRunner::GetCurrentDefault(),
std::move(callback)));
}
template <typename P, typename T>
void ProtoDatabaseImpl<P, T>::InitWithDatabase(
LevelDB* database,
const base::FilePath& database_dir,
const leveldb_env::Options& options,
Callbacks::InitStatusCallback callback) {
DCHECK(force_unique_db_);
task_runner_->PostTask(
FROM_HERE,
base::BindOnce(&ProtoDatabaseSelector::InitWithDatabase, db_wrapper_,
base::Unretained(database), database_dir, options,
base::SequencedTaskRunner::GetCurrentDefault(),
std::move(callback)));
}
template <typename P, typename T>
void ProtoDatabaseImpl<P, T>::UpdateEntries(
std::unique_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save,
std::unique_ptr<KeyVector> keys_to_remove,
Callbacks::UpdateCallback callback) {
base::OnceClosure update_task = base::BindOnce(
&UpdateEntriesFromTaskRunner<P, T>, std::move(entries_to_save),
std::move(keys_to_remove), db_wrapper_,
base::BindOnce(&RunUpdateCallback,
base::SequencedTaskRunner::GetCurrentDefault(),
std::move(callback)));
PostTransaction(std::move(update_task));
}
template <typename P, typename T>
void ProtoDatabaseImpl<P, T>::UpdateEntriesWithRemoveFilter(
std::unique_ptr<typename ProtoDatabase<T>::KeyEntryVector> entries_to_save,
const KeyFilter& delete_key_filter,
Callbacks::UpdateCallback callback) {
base::OnceClosure update_task = base::BindOnce(
&UpdateEntriesWithRemoveFilterFromTaskRunner<P, T>,
std::move(entries_to_save), delete_key_filter, db_wrapper_,
base::BindOnce(&RunUpdateCallback,
base::SequencedTaskRunner::GetCurrentDefault(),
std::move(callback)));
PostTransaction(std::move(update_task));
}
template <typename P, typename T>
void ProtoDatabaseImpl<P, T>::LoadEntries(
typename Callbacks::Internal<T>::LoadCallback callback) {
LoadEntriesWithFilter(KeyFilter(), std::move(callback));
}
template <typename P, typename T>
void ProtoDatabaseImpl<P, T>::LoadEntriesWithFilter(
const KeyFilter& filter,
typename Callbacks::Internal<T>::LoadCallback callback) {
LoadEntriesWithFilter(filter, leveldb::ReadOptions(), std::string(),
std::move(callback));
}
template <typename P, typename T>
void ProtoDatabaseImpl<P, T>::LoadEntriesWithFilter(
const KeyFilter& key_filter,
const leveldb::ReadOptions& options,
const std::string& target_prefix,
typename Callbacks::Internal<T>::LoadCallback callback) {
base::OnceClosure load_task = base::BindOnce(
&ProtoDatabaseSelector::LoadEntriesWithFilter, db_wrapper_, key_filter,
options, target_prefix,
base::BindOnce(&ParseLoadedEntries<P, T>,
base::SequencedTaskRunner::GetCurrentDefault(),
std::move(callback)));
PostTransaction(std::move(load_task));
}
template <typename P, typename T>
void ProtoDatabaseImpl<P, T>::LoadKeysAndEntries(
typename Callbacks::Internal<T>::LoadKeysAndEntriesCallback callback) {
LoadKeysAndEntriesWithFilter(KeyFilter(), std::move(callback));
}
template <typename P, typename T>
void ProtoDatabaseImpl<P, T>::LoadKeysAndEntriesWithFilter(
const KeyFilter& filter,
typename Callbacks::Internal<T>::LoadKeysAndEntriesCallback callback) {
LoadKeysAndEntriesWithFilter(filter, leveldb::ReadOptions(), std::string(),
std::move(callback));
}
template <typename P, typename T>
void ProtoDatabaseImpl<P, T>::LoadKeysAndEntriesWithFilter(
const KeyFilter& filter,
const leveldb::ReadOptions& options,
const std::string& target_prefix,
typename Callbacks::Internal<T>::LoadKeysAndEntriesCallback callback) {
base::OnceClosure load_task = base::BindOnce(
&ProtoDatabaseSelector::LoadKeysAndEntriesWithFilter, db_wrapper_, filter,
options, target_prefix,
base::BindOnce(&ParseLoadedKeysAndEntries<P, T>,
base::SequencedTaskRunner::GetCurrentDefault(),
std::move(callback)));
PostTransaction(std::move(load_task));
}
template <typename P, typename T>
void ProtoDatabaseImpl<P, T>::LoadKeysAndEntriesInRange(
const std::string& start,
const std::string& end,
typename Callbacks::Internal<T>::LoadKeysAndEntriesCallback callback) {
base::OnceClosure load_task = base::BindOnce(
&ProtoDatabaseSelector::LoadKeysAndEntriesInRange, db_wrapper_, start,
end,
base::BindOnce(&ParseLoadedKeysAndEntries<P, T>,
base::SequencedTaskRunner::GetCurrentDefault(),
std::move(callback)));
PostTransaction(std::move(load_task));
}
template <typename P, typename T>
void ProtoDatabaseImpl<P, T>::LoadKeysAndEntriesWhile(
const std::string& start,
const KeyIteratorController& controller,
typename Callbacks::Internal<T>::LoadKeysAndEntriesCallback callback) {
base::OnceClosure load_task = base::BindOnce(
&ProtoDatabaseSelector::LoadKeysAndEntriesWhile, db_wrapper_, start,
controller,
base::BindOnce(&ParseLoadedKeysAndEntries<P, T>,
base::SequencedTaskRunner::GetCurrentDefault(),
std::move(callback)));
PostTransaction(std::move(load_task));
}
template <typename P, typename T>
void ProtoDatabaseImpl<P, T>::LoadKeys(Callbacks::LoadKeysCallback callback) {
base::OnceClosure load_task = base::BindOnce(
&ProtoDatabaseSelector::LoadKeys, db_wrapper_,
base::BindOnce(&RunLoadKeysCallback,
base::SequencedTaskRunner::GetCurrentDefault(),
std::move(callback)));
PostTransaction(std::move(load_task));
}
template <typename P, typename T>
void ProtoDatabaseImpl<P, T>::GetEntry(
const std::string& key,
typename Callbacks::Internal<T>::GetCallback callback) {
base::OnceClosure get_task = base::BindOnce(
&ProtoDatabaseSelector::GetEntry, db_wrapper_, key,
base::BindOnce(&ParseLoadedEntry<P, T>,
base::SequencedTaskRunner::GetCurrentDefault(),
std::move(callback)));
PostTransaction(std::move(get_task));
}
template <typename P, typename T>
void ProtoDatabaseImpl<P, T>::Destroy(Callbacks::DestroyCallback callback) {
base::OnceClosure destroy_task = base::BindOnce(
&ProtoDatabaseSelector::Destroy, db_wrapper_,
base::BindOnce(&RunDestroyCallback,
base::SequencedTaskRunner::GetCurrentDefault(),
std::move(callback)));
PostTransaction(std::move(destroy_task));
}
template <typename P, typename T>
void ProtoDatabaseImpl<P, T>::RemoveKeysForTesting(
const KeyFilter& key_filter,
const std::string& target_prefix,
Callbacks::UpdateCallback callback) {
base::OnceClosure update_task = base::BindOnce(
&ProtoDatabaseSelector::RemoveKeysForTesting, db_wrapper_, key_filter,
target_prefix,
base::BindOnce(&RunUpdateCallback,
base::SequencedTaskRunner::GetCurrentDefault(),
std::move(callback)));
PostTransaction(std::move(update_task));
}
template <typename P, typename T>
void ProtoDatabaseImpl<P, T>::PostTransaction(base::OnceClosure task) {
task_runner_->PostTask(FROM_HERE,
base::BindOnce(&ProtoDatabaseSelector::AddTransaction,
db_wrapper_, std::move(task)));
}
} // namespace leveldb_proto
#endif // COMPONENTS_LEVELDB_PROTO_INTERNAL_PROTO_DATABASE_IMPL_H_
|