proto_leveldb_wrapper.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef COMPONENTS_LEVELDB_PROTO_INTERNAL_PROTO_LEVELDB_WRAPPER_H_
  5. #define COMPONENTS_LEVELDB_PROTO_INTERNAL_PROTO_LEVELDB_WRAPPER_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <utility>
  10. #include <vector>
  11. #include "base/bind.h"
  12. #include "base/callback.h"
  13. #include "base/component_export.h"
  14. #include "base/memory/ptr_util.h"
  15. #include "base/memory/raw_ptr.h"
  16. #include "base/strings/string_util.h"
  17. #include "base/task/sequenced_task_runner.h"
  18. #include "base/task/task_runner_util.h"
  19. #include "base/threading/sequenced_task_runner_handle.h"
  20. #include "base/threading/thread_checker.h"
  21. #include "components/leveldb_proto/internal/proto_leveldb_wrapper_metrics.h"
  22. #include "components/leveldb_proto/public/proto_database.h"
  23. #include "third_party/leveldatabase/env_chromium.h"
  24. namespace base {
  25. class FilePath;
  26. }
  27. namespace leveldb_proto {
  28. class LevelDB;
  29. using KeyValueVector = std::vector<std::pair<std::string, std::string>>;
  30. using KeyValueMap = std::map<std::string, std::string>;
  31. using KeyVector = std::vector<std::string>;
  32. using ValueVector = std::vector<std::string>;
  33. // When the ProtoDatabase instance is deleted, in-progress asynchronous
  34. // operations will be completed and the corresponding callbacks will be called.
  35. // Construction/calls/destruction should all happen on the same thread.
  36. class COMPONENT_EXPORT(LEVELDB_PROTO) ProtoLevelDBWrapper {
  37. public:
  38. // Used to destroy database when initialization fails.
  39. static void Destroy(
  40. const base::FilePath& db_dir,
  41. const std::string& client_id,
  42. const scoped_refptr<base::SequencedTaskRunner>& task_runner,
  43. Callbacks::DestroyCallback callback);
  44. // All blocking calls/disk access will happen on the provided |task_runner|.
  45. explicit ProtoLevelDBWrapper(
  46. const scoped_refptr<base::SequencedTaskRunner>& task_runner);
  47. ProtoLevelDBWrapper(
  48. const scoped_refptr<base::SequencedTaskRunner>& task_runner,
  49. LevelDB* db);
  50. ProtoLevelDBWrapper(const ProtoLevelDBWrapper&) = delete;
  51. ProtoLevelDBWrapper& operator=(const ProtoLevelDBWrapper&) = delete;
  52. virtual ~ProtoLevelDBWrapper();
  53. void UpdateEntries(std::unique_ptr<KeyValueVector> entries_to_save,
  54. std::unique_ptr<KeyVector> keys_to_remove,
  55. Callbacks::UpdateCallback callback);
  56. void UpdateEntriesWithRemoveFilter(
  57. std::unique_ptr<KeyValueVector> entries_to_save,
  58. const KeyFilter& delete_key_filter,
  59. Callbacks::UpdateCallback callback);
  60. void UpdateEntriesWithRemoveFilter(
  61. std::unique_ptr<KeyValueVector> entries_to_save,
  62. const KeyFilter& delete_key_filter,
  63. const std::string& target_prefix,
  64. Callbacks::UpdateCallback callback);
  65. void LoadEntries(Callbacks::LoadCallback callback);
  66. void LoadEntriesWithFilter(const KeyFilter& key_filter,
  67. Callbacks::LoadCallback callback);
  68. void LoadEntriesWithFilter(const KeyFilter& key_filter,
  69. const leveldb::ReadOptions& options,
  70. const std::string& target_prefix,
  71. Callbacks::LoadCallback callback);
  72. void LoadKeysAndEntries(Callbacks::LoadKeysAndEntriesCallback callback);
  73. void LoadKeysAndEntriesWithFilter(
  74. const KeyFilter& filter,
  75. Callbacks::LoadKeysAndEntriesCallback callback);
  76. void LoadKeysAndEntriesWithFilter(
  77. const KeyFilter& filter,
  78. const leveldb::ReadOptions& options,
  79. const std::string& target_prefix,
  80. Callbacks::LoadKeysAndEntriesCallback callback);
  81. void LoadKeysAndEntriesInRange(
  82. const std::string& start,
  83. const std::string& end,
  84. Callbacks::LoadKeysAndEntriesCallback callback);
  85. void LoadKeysAndEntriesWhile(const KeyIteratorController& controller,
  86. const leveldb::ReadOptions& options,
  87. const std::string& start_key,
  88. Callbacks::LoadKeysAndEntriesCallback callback);
  89. void LoadKeysAndEntriesWhile(const KeyFilter& while_callback,
  90. const KeyFilter& filter,
  91. const leveldb::ReadOptions& options,
  92. const std::string& start_key,
  93. Callbacks::LoadKeysAndEntriesCallback callback);
  94. void LoadKeysAndEntriesWhile(const std::string& start_key,
  95. const KeyIteratorController& controller,
  96. Callbacks::LoadKeysAndEntriesCallback callback);
  97. void LoadKeys(Callbacks::LoadKeysCallback callback);
  98. void LoadKeys(const std::string& target_prefix,
  99. Callbacks::LoadKeysCallback callback);
  100. void GetEntry(const std::string& key, Callbacks::GetCallback callback);
  101. void RemoveKeys(const KeyFilter& filter,
  102. const std::string& target_prefix,
  103. Callbacks::UpdateCallback callback);
  104. void Destroy(Callbacks::DestroyCallback callback);
  105. void RunInitCallback(Callbacks::InitCallback callback,
  106. const leveldb::Status* status);
  107. // Allow callers to provide their own Database implementation.
  108. void InitWithDatabase(LevelDB* database,
  109. const base::FilePath& database_dir,
  110. const leveldb_env::Options& options,
  111. bool destroy_on_corruption,
  112. Callbacks::InitStatusCallback callback);
  113. void SetMetricsId(const std::string& id);
  114. bool GetApproximateMemoryUse(uint64_t* approx_mem_use);
  115. const scoped_refptr<base::SequencedTaskRunner>& task_runner();
  116. private:
  117. SEQUENCE_CHECKER(sequence_checker_);
  118. // Used to run blocking tasks in-order, must be the TaskRunner that |db_|
  119. // relies on.
  120. scoped_refptr<base::SequencedTaskRunner> task_runner_;
  121. raw_ptr<LevelDB> db_ = nullptr;
  122. // The identifier used when recording metrics to determine the source of the
  123. // LevelDB calls, likely the database client name.
  124. std::string metrics_id_ = "Default";
  125. base::WeakPtrFactory<ProtoLevelDBWrapper> weak_ptr_factory_{this};
  126. };
  127. } // namespace leveldb_proto
  128. #endif // COMPONENTS_LEVELDB_PROTO_INTERNAL_PROTO_LEVELDB_WRAPPER_H_