shared_proto_database_client.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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_SHARED_PROTO_DATABASE_CLIENT_H_
  5. #define COMPONENTS_LEVELDB_PROTO_INTERNAL_SHARED_PROTO_DATABASE_CLIENT_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/bind.h"
  9. #include "base/component_export.h"
  10. #include "base/sequence_checker.h"
  11. #include "base/types/strong_alias.h"
  12. #include "components/leveldb_proto/internal/leveldb_database.h"
  13. #include "components/leveldb_proto/internal/proto/shared_db_metadata.pb.h"
  14. #include "components/leveldb_proto/internal/unique_proto_database.h"
  15. #include "components/leveldb_proto/public/shared_proto_database_client_list.h"
  16. #include "third_party/abseil-cpp/absl/types/optional.h"
  17. namespace leveldb_proto {
  18. // Key prefix not visible to the client.
  19. using KeyPrefix = base::StrongAlias<class KeyPrefixTag, std::string>;
  20. // Logical key visible to the client.
  21. using LogicalKey = base::StrongAlias<class LogicalKeyTag, std::string>;
  22. // Physical key used in the underlying db.
  23. using PhysicalKey = base::StrongAlias<class PhysicalKeyTag, std::string>;
  24. class SharedProtoDatabase;
  25. // TODO: Move all these as static or member functions in the class.
  26. using ClientCorruptCallback = base::OnceCallback<void(bool)>;
  27. using SharedClientInitCallback =
  28. base::OnceCallback<void(Enums::InitStatus,
  29. SharedDBMetadataProto::MigrationStatus)>;
  30. // An implementation of ProtoDatabase<T> that uses a shared LevelDB and task
  31. // runner.
  32. // Should be created, destroyed, and used on the same sequenced task runner.
  33. class COMPONENT_EXPORT(LEVELDB_PROTO) SharedProtoDatabaseClient
  34. : public UniqueProtoDatabase {
  35. public:
  36. static KeyPrefix PrefixForDatabase(ProtoDbType db_type);
  37. static bool HasPrefix(const PhysicalKey& key, const KeyPrefix& prefix);
  38. static absl::optional<LogicalKey> StripPrefix(const PhysicalKey& key,
  39. const KeyPrefix& prefix);
  40. static std::unique_ptr<KeyVector> PrefixStrings(
  41. std::unique_ptr<KeyVector> strings,
  42. const KeyPrefix& prefix);
  43. static bool KeyFilterStripPrefix(const KeyFilter& key_filter,
  44. const KeyPrefix& prefix,
  45. const PhysicalKey& key);
  46. static bool KeyStringFilterStripPrefix(const KeyFilter& key_filter,
  47. const KeyPrefix& prefix,
  48. const std::string& key);
  49. static Enums::KeyIteratorAction KeyIteratorControllerStripPrefix(
  50. const KeyIteratorController& controller,
  51. const KeyPrefix& prefix,
  52. const PhysicalKey& key);
  53. static Enums::KeyIteratorAction KeyStringIteratorControllerStripPrefix(
  54. const KeyIteratorController& controller,
  55. const KeyPrefix& prefix,
  56. const std::string& key);
  57. static void GetSharedDatabaseInitStatusAsync(
  58. const std::string& client_db_id,
  59. const scoped_refptr<SharedProtoDatabase>& db,
  60. Callbacks::InitStatusCallback callback);
  61. static void UpdateClientMetadataAsync(
  62. const scoped_refptr<SharedProtoDatabase>& db,
  63. const std::string& client_db_id,
  64. SharedDBMetadataProto::MigrationStatus migration_status,
  65. ClientCorruptCallback callback);
  66. // Destroys all the data from obsolete clients, for the given |db_wrapper|
  67. // instance. |callback| is called once all the obsolete clients data are
  68. // removed, with failure status if one or more of the update fails.
  69. static void DestroyObsoleteSharedProtoDatabaseClients(
  70. std::unique_ptr<ProtoLevelDBWrapper> db_wrapper,
  71. Callbacks::UpdateCallback callback);
  72. // Sets list of client names that are obsolete and will be cleared by next
  73. // call to DestroyObsoleteSharedProtoDatabaseClients(). |list| is list of dbs
  74. // with a |LAST| to mark the end of list.
  75. static void SetObsoleteClientListForTesting(const ProtoDbType* list);
  76. SharedProtoDatabaseClient(const SharedProtoDatabaseClient&) = delete;
  77. SharedProtoDatabaseClient& operator=(const SharedProtoDatabaseClient&) =
  78. delete;
  79. ~SharedProtoDatabaseClient() override;
  80. void Init(const std::string& client_uma_name,
  81. Callbacks::InitStatusCallback callback) override;
  82. void InitWithDatabase(LevelDB* database,
  83. const base::FilePath& database_dir,
  84. const leveldb_env::Options& options,
  85. bool destroy_on_corruption,
  86. Callbacks::InitStatusCallback callback) override;
  87. // Overrides for prepending namespace and type prefix to all operations on the
  88. // shared database.
  89. void UpdateEntries(std::unique_ptr<KeyValueVector> entries_to_save,
  90. std::unique_ptr<KeyVector> keys_to_remove,
  91. Callbacks::UpdateCallback callback) override;
  92. void UpdateEntriesWithRemoveFilter(
  93. std::unique_ptr<KeyValueVector> entries_to_save,
  94. const KeyFilter& delete_key_filter,
  95. Callbacks::UpdateCallback callback) override;
  96. void UpdateEntriesWithRemoveFilter(
  97. std::unique_ptr<KeyValueVector> entries_to_save,
  98. const KeyFilter& delete_key_filter,
  99. const std::string& target_prefix,
  100. Callbacks::UpdateCallback callback) override;
  101. void LoadEntries(Callbacks::LoadCallback callback) override;
  102. void LoadEntriesWithFilter(const KeyFilter& filter,
  103. Callbacks::LoadCallback callback) override;
  104. void LoadEntriesWithFilter(const KeyFilter& key_filter,
  105. const leveldb::ReadOptions& options,
  106. const std::string& target_prefix,
  107. Callbacks::LoadCallback callback) override;
  108. void LoadKeys(Callbacks::LoadKeysCallback callback) override;
  109. void LoadKeys(const std::string& target_prefix,
  110. Callbacks::LoadKeysCallback callback) override;
  111. void LoadKeysAndEntries(
  112. Callbacks::LoadKeysAndEntriesCallback callback) override;
  113. void LoadKeysAndEntriesWithFilter(
  114. const KeyFilter& filter,
  115. Callbacks::LoadKeysAndEntriesCallback callback) override;
  116. void LoadKeysAndEntriesWithFilter(
  117. const KeyFilter& filter,
  118. const leveldb::ReadOptions& options,
  119. const std::string& target_prefix,
  120. Callbacks::LoadKeysAndEntriesCallback callback) override;
  121. void LoadKeysAndEntriesInRange(
  122. const std::string& start,
  123. const std::string& end,
  124. Callbacks::LoadKeysAndEntriesCallback callback) override;
  125. void LoadKeysAndEntriesWhile(
  126. const std::string& start,
  127. const leveldb_proto::KeyIteratorController& controller,
  128. typename Callbacks::LoadKeysAndEntriesCallback callback) override;
  129. void GetEntry(const std::string& key,
  130. Callbacks::GetCallback callback) override;
  131. void Destroy(Callbacks::DestroyCallback callback) override;
  132. Callbacks::InitCallback GetInitCallback() const;
  133. const std::string& client_db_id() const { return prefix_.value(); }
  134. void set_migration_status(
  135. SharedDBMetadataProto::MigrationStatus migration_status) {
  136. migration_status_ = migration_status;
  137. }
  138. virtual void UpdateClientInitMetadata(SharedDBMetadataProto::MigrationStatus);
  139. SharedDBMetadataProto::MigrationStatus migration_status() const {
  140. return migration_status_;
  141. }
  142. private:
  143. friend class SharedProtoDatabase;
  144. friend class SharedProtoDatabaseTest;
  145. friend class SharedProtoDatabaseClientTest;
  146. friend class TestSharedProtoDatabaseClient;
  147. // Hide this so clients can only be created by the SharedProtoDatabase.
  148. SharedProtoDatabaseClient(
  149. std::unique_ptr<ProtoLevelDBWrapper> db_wrapper,
  150. ProtoDbType db_type,
  151. const scoped_refptr<SharedProtoDatabase>& parent_db);
  152. static void StripPrefixLoadKeysCallback(
  153. Callbacks::LoadKeysCallback callback,
  154. const KeyPrefix& prefix,
  155. bool success,
  156. std::unique_ptr<leveldb_proto::KeyVector> keys);
  157. static void StripPrefixLoadKeysAndEntriesCallback(
  158. Callbacks::LoadKeysAndEntriesCallback callback,
  159. const KeyPrefix& prefix,
  160. bool success,
  161. std::unique_ptr<KeyValueMap> keys_entries);
  162. static std::unique_ptr<KeyValueVector> PrefixKeyEntryVector(
  163. std::unique_ptr<KeyValueVector> kev,
  164. const KeyPrefix& prefix);
  165. SEQUENCE_CHECKER(sequence_checker_);
  166. // |is_corrupt_| should be set by the SharedProtoDatabase that creates this
  167. // when a client is created that doesn't know about a previous shared
  168. // database corruption.
  169. bool is_corrupt_ = false;
  170. SharedDBMetadataProto::MigrationStatus migration_status_ =
  171. SharedDBMetadataProto::MIGRATION_NOT_ATTEMPTED;
  172. const KeyPrefix prefix_;
  173. scoped_refptr<SharedProtoDatabase> parent_db_;
  174. base::WeakPtrFactory<SharedProtoDatabaseClient> weak_ptr_factory_{this};
  175. };
  176. } // namespace leveldb_proto
  177. #endif // COMPONENTS_LEVELDB_PROTO_INTERNAL_SHARED_PROTO_DATABASE_CLIENT_H_