processor_entity_tracker.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2020 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_PROCESSOR_ENTITY_TRACKER_H_
  5. #define COMPONENTS_SYNC_MODEL_PROCESSOR_ENTITY_TRACKER_H_
  6. #include <map>
  7. #include <memory>
  8. #include <string>
  9. #include <unordered_set>
  10. #include <vector>
  11. #include "components/sync/base/client_tag_hash.h"
  12. #include "components/sync/engine/commit_and_get_updates_types.h"
  13. #include "components/sync/protocol/model_type_state.pb.h"
  14. namespace sync_pb {
  15. class EntityMetadata;
  16. class EntitySpecifics;
  17. } // namespace sync_pb
  18. namespace syncer {
  19. class ProcessorEntity;
  20. // This component tracks entities for ClientTagBasedModelTypeProcessor.
  21. class ProcessorEntityTracker {
  22. public:
  23. // Creates tracker and fills entities data from batch metadata map. This
  24. // constructor must be used only if initial_sync_done returns true in
  25. // |model_type_state|.
  26. ProcessorEntityTracker(
  27. const sync_pb::ModelTypeState& model_type_state,
  28. std::map<std::string, std::unique_ptr<sync_pb::EntityMetadata>>
  29. metadata_map);
  30. ~ProcessorEntityTracker();
  31. // Returns true if all processor entities have non-empty storage keys.
  32. // This may happen during initial merge and for some data types during any
  33. // remote creation.
  34. bool AllStorageKeysPopulated() const;
  35. // Clears any in-memory sync state associated with outstanding commits
  36. // for each entity.
  37. void ClearTransientSyncState();
  38. // Returns number of entities with non-deleted metadata.
  39. size_t CountNonTombstoneEntries() const;
  40. // Starts tracking new locally-created entity (must not be deleted outside
  41. // current object). The entity will be created unsynced with pending commit
  42. // data.
  43. ProcessorEntity* AddUnsyncedLocal(const std::string& storage_key,
  44. std::unique_ptr<EntityData> data,
  45. sync_pb::EntitySpecifics trimmed_specifics);
  46. // Starts tracking new remotely-created entity (must not be deleted outside
  47. // current object).
  48. ProcessorEntity* AddRemote(const std::string& storage_key,
  49. const UpdateResponseData& update_data,
  50. sync_pb::EntitySpecifics trimmed_specifics);
  51. // Removes item from |entities_| and |storage_key_to_tag_hash|. If entity does
  52. // not exist, does nothing.
  53. void RemoveEntityForClientTagHash(const ClientTagHash& client_tag_hash);
  54. void RemoveEntityForStorageKey(const std::string& storage_key);
  55. // Removes |storage_key| from |storage_key_to_tag_hash_| and clears it for
  56. // the corresponding entity. Does not remove the entity from |entities_|.
  57. void ClearStorageKey(const std::string& storage_key);
  58. // Returns the estimate of dynamically allocated memory in bytes.
  59. size_t EstimateMemoryUsage() const;
  60. // Gets the entity for the given tag hash, or null if there isn't one.
  61. ProcessorEntity* GetEntityForTagHash(const ClientTagHash& tag_hash);
  62. const ProcessorEntity* GetEntityForTagHash(
  63. const ClientTagHash& tag_hash) const;
  64. // Gets the entity for the given storage key, or null if there isn't one.
  65. ProcessorEntity* GetEntityForStorageKey(const std::string& storage_key);
  66. const ProcessorEntity* GetEntityForStorageKey(
  67. const std::string& storage_key) const;
  68. // Returns all entities including tombstones.
  69. std::vector<const ProcessorEntity*> GetAllEntitiesIncludingTombstones() const;
  70. // Returns all entities with local changes.
  71. // TODO(rushans): make it const, at this moment returned entities must be
  72. // initialized to commit.
  73. std::vector<ProcessorEntity*> GetEntitiesWithLocalChanges(size_t max_entries);
  74. // Returns true if there are any local entities to be committed.
  75. bool HasLocalChanges() const;
  76. const sync_pb::ModelTypeState& model_type_state() const {
  77. return model_type_state_;
  78. }
  79. void set_model_type_state(const sync_pb::ModelTypeState& model_type_state) {
  80. model_type_state_ = model_type_state;
  81. }
  82. // Returns number of entities, including tombstones.
  83. size_t size() const;
  84. // Increments sequence number for all entities except those in
  85. // |already_updated_storage_keys|. Returns affected list of entities.
  86. std::vector<const ProcessorEntity*> IncrementSequenceNumberForAllExcept(
  87. const std::unordered_set<std::string>& already_updated_storage_keys);
  88. // Assigns a new storage key to the entity for the given |client_tag_hash|.
  89. // Clears previous storage key if entity already has one (the metadata of the
  90. // entity must be deleted).
  91. void UpdateOrOverrideStorageKey(const ClientTagHash& client_tag_hash,
  92. const std::string& storage_key);
  93. private:
  94. // Creates a new processor entity (must not be deleted outside current
  95. // object).
  96. ProcessorEntity* AddInternal(const std::string& storage_key,
  97. const EntityData& data,
  98. int64_t server_version);
  99. // A map of client tag hash to sync entities known to this tracker. This
  100. // should contain entries and metadata, although the entities may not always
  101. // contain model type data/specifics.
  102. std::map<ClientTagHash, std::unique_ptr<ProcessorEntity>> entities_;
  103. // The model type metadata (progress marker, initial sync done, etc).
  104. sync_pb::ModelTypeState model_type_state_;
  105. // The bridge wants to communicate entirely via storage keys that it is free
  106. // to define and can understand more easily. All of the sync machinery wants
  107. // to use client tag hash. This mapping allows us to convert from storage key
  108. // to client tag hash. The other direction can use |entities_|.
  109. // Entity is temporarily not included in this map for the duration of
  110. // MergeSyncData/ApplySyncChanges call when the bridge doesn't support
  111. // GetStorageKey(). In this case the bridge is responsible for updating
  112. // storage key with UpdateStorageKey() call from within
  113. // MergeSyncData/ApplySyncChanges.
  114. std::map<std::string, ClientTagHash> storage_key_to_tag_hash_;
  115. };
  116. } // namespace syncer
  117. #endif // COMPONENTS_SYNC_MODEL_PROCESSOR_ENTITY_TRACKER_H_