model_type_store.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Copyright 2015 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_SYNC_MODEL_MODEL_TYPE_STORE_H_
  5. #define COMPONENTS_SYNC_MODEL_MODEL_TYPE_STORE_H_
  6. #include <memory>
  7. #include "base/callback.h"
  8. #include "components/sync/base/model_type.h"
  9. #include "components/sync/model/model_error.h"
  10. #include "components/sync/model/model_type_store_base.h"
  11. #include "third_party/abseil-cpp/absl/types/optional.h"
  12. namespace syncer {
  13. class MetadataBatch;
  14. // ModelTypeStore is leveldb backed store for model type's data, metadata and
  15. // global metadata.
  16. //
  17. // Store keeps records for entries identified by ids. For each entry store keeps
  18. // data and metadata. Also store keeps one record for global metadata.
  19. //
  20. // To create store call one of Create*Store static factory functions. Model type
  21. // controls store's lifetime with returned unique_ptr. Call to Create*Store
  22. // function triggers asynchronous store backend initialization, callback will be
  23. // called with results when initialization is done.
  24. //
  25. // Read operations are asynchronous, initiated with one of Read* functions,
  26. // provided callback will be called with result code and output of read
  27. // operation.
  28. //
  29. // Write operations are done in context of write batch. To get one call
  30. // CreateWriteBatch(). After that pass write batch object to Write/Delete
  31. // functions. WriteBatch only accumulates pending changes, doesn't actually do
  32. // data modification. Calling CommitWriteBatch writes all accumulated changes to
  33. // disk atomically. Callback passed to CommitWriteBatch will be called with
  34. // result of write operation. If write batch object is destroyed without
  35. // comitting accumulated write operations will not be persisted.
  36. //
  37. // Destroying store object doesn't necessarily cancel asynchronous operations
  38. // issued previously. You should be prepared to handle callbacks from those
  39. // operations.
  40. class ModelTypeStore : public ModelTypeStoreBase {
  41. public:
  42. using InitCallback =
  43. base::OnceCallback<void(const absl::optional<ModelError>& error,
  44. std::unique_ptr<ModelTypeStore> store)>;
  45. using CallbackWithResult =
  46. base::OnceCallback<void(const absl::optional<ModelError>& error)>;
  47. using ReadDataCallback =
  48. base::OnceCallback<void(const absl::optional<ModelError>& error,
  49. std::unique_ptr<RecordList> data_records,
  50. std::unique_ptr<IdList> missing_id_list)>;
  51. using ReadAllDataCallback =
  52. base::OnceCallback<void(const absl::optional<ModelError>& error,
  53. std::unique_ptr<RecordList> data_records)>;
  54. using ReadMetadataCallback =
  55. base::OnceCallback<void(const absl::optional<ModelError>& error,
  56. std::unique_ptr<MetadataBatch> metadata_batch)>;
  57. // Callback that runs on the backend sequence, see ReadAllDataAndPreprocess().
  58. using PreprocessCallback = base::OnceCallback<absl::optional<ModelError>(
  59. std::unique_ptr<RecordList> data_records)>;
  60. // Read operations return records either for all entries or only for ones
  61. // identified in |id_list|. |error| is nullopt if all records were read
  62. // successfully, otherwise an empty or partial list of read records is
  63. // returned.
  64. // Callback for ReadData (ReadDataCallback) in addition receives list of ids
  65. // that were not found in store (missing_id_list).
  66. virtual void ReadData(const IdList& id_list, ReadDataCallback callback) = 0;
  67. virtual void ReadAllData(ReadAllDataCallback callback) = 0;
  68. // ReadMetadataCallback will be invoked with three parameters: result of
  69. // operation, list of metadata records and global metadata.
  70. virtual void ReadAllMetadata(ReadMetadataCallback callback) = 0;
  71. // Similar to ReadAllData() but allows some custom processing in the
  72. // background sequence (e.g. proto parsing). Note that |preprocess_callback|
  73. // will not run if reading itself triggers an error.
  74. // |completion_on_frontend_sequence_callback| is guaranteed to outlive
  75. // |preprocess_on_backend_sequence_callback|.
  76. virtual void ReadAllDataAndPreprocess(
  77. PreprocessCallback preprocess_on_backend_sequence_callback,
  78. CallbackWithResult completion_on_frontend_sequence_callback) = 0;
  79. // Creates write batch for write operations.
  80. virtual std::unique_ptr<WriteBatch> CreateWriteBatch() = 0;
  81. // Commits write operations accumulated in write batch. If write operation
  82. // fails result is UNSPECIFIED_ERROR and write operations will not be
  83. // reflected in the store.
  84. virtual void CommitWriteBatch(std::unique_ptr<WriteBatch> write_batch,
  85. CallbackWithResult callback) = 0;
  86. // Deletion of everything, usually exercised during DisableSync().
  87. virtual void DeleteAllDataAndMetadata(CallbackWithResult callback) = 0;
  88. };
  89. // Typedef for a store factory that has all params bound except InitCallback.
  90. using RepeatingModelTypeStoreFactory =
  91. base::RepeatingCallback<void(ModelType type, ModelTypeStore::InitCallback)>;
  92. // Same as above but as a OnceCallback.
  93. using OnceModelTypeStoreFactory =
  94. base::OnceCallback<void(ModelType type, ModelTypeStore::InitCallback)>;
  95. } // namespace syncer
  96. #endif // COMPONENTS_SYNC_MODEL_MODEL_TYPE_STORE_H_