OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_SYNC_SYNCABLE_SYNCABLE_DELETE_JOURNAL_H_ | 5 #ifndef COMPONENTS_SYNC_SYNCABLE_SYNCABLE_DELETE_JOURNAL_H_ |
6 #define COMPONENTS_SYNC_SYNCABLE_SYNCABLE_DELETE_JOURNAL_H_ | 6 #define COMPONENTS_SYNC_SYNCABLE_SYNCABLE_DELETE_JOURNAL_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <set> | 10 #include <map> |
11 | 11 |
12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
13 #include "base/macros.h" | 13 #include "base/macros.h" |
14 #include "base/synchronization/lock.h" | 14 #include "base/synchronization/lock.h" |
| 15 #include "components/sync/syncable/entry_kernel.h" |
15 #include "components/sync/syncable/metahandle_set.h" | 16 #include "components/sync/syncable/metahandle_set.h" |
16 #include "components/sync/syncable/syncable-inl.h" | |
17 | 17 |
18 namespace syncer { | 18 namespace syncer { |
19 namespace syncable { | 19 namespace syncable { |
20 | 20 |
21 class BaseTransaction; | 21 class BaseTransaction; |
22 struct EntryKernel; | |
23 | 22 |
24 typedef std::set<const EntryKernel*, LessField<IdField, ID>> JournalIndex; | 23 class LessByID { |
| 24 public: |
| 25 inline bool operator()(const EntryKernel* a, const EntryKernel* b) const { |
| 26 return a->ref(ID) < b->ref(ID); |
| 27 } |
| 28 }; |
| 29 // This should be |std::set<std::unique_ptr<EntryKernel>, LessByID>|. However, |
| 30 // DeleteJournal::TakeSnapshotAndClear needs to remove the unique_ptr from the |
| 31 // set, and std::set::extract is only available in C++17. TODO(avi): Switch to |
| 32 // using a std::set when Chromium allows C++17 use. |
| 33 typedef std::map<const EntryKernel*, std::unique_ptr<EntryKernel>, LessByID> |
| 34 JournalIndex; |
25 | 35 |
26 // DeleteJournal manages deleted entries that are not in sync directory until | 36 // DeleteJournal manages deleted entries that are not in sync directory until |
27 // it's safe to drop them after the deletion is confirmed with native models. | 37 // it's safe to drop them after the deletion is confirmed with native models. |
28 // DeleteJournal is thread-safe and can be accessed on any thread. Has to hold | 38 // DeleteJournal is thread-safe and can be accessed on any thread. Has to hold |
29 // a valid transaction object when calling methods of DeleteJournal, thus each | 39 // a valid transaction object when calling methods of DeleteJournal, thus each |
30 // method requires a non-null |trans| parameter. | 40 // method requires a non-null |trans| parameter. |
31 class DeleteJournal { | 41 class DeleteJournal { |
32 public: | 42 public: |
33 // Initialize |delete_journals_| using |intitial_journal|, whose content is | 43 // Initialize |delete_journals_| using |initial_journal|. |
34 // destroyed during initialization. | 44 explicit DeleteJournal(std::unique_ptr<JournalIndex> initial_journal); |
35 explicit DeleteJournal(JournalIndex* initial_journal); | |
36 ~DeleteJournal(); | 45 ~DeleteJournal(); |
37 | 46 |
38 // For testing only. | 47 // For testing only. |
39 size_t GetDeleteJournalSize(BaseTransaction* trans) const; | 48 size_t GetDeleteJournalSize(BaseTransaction* trans) const; |
40 | 49 |
41 // Add/remove |entry| to/from |delete_journals_| according to its | 50 // Add/remove |entry| to/from |delete_journals_| according to its |
42 // SERVER_IS_DEL field and |was_deleted|. Called on sync thread. | 51 // SERVER_IS_DEL field and |was_deleted|. Called on sync thread. |
43 void UpdateDeleteJournalForServerDelete(BaseTransaction* trans, | 52 void UpdateDeleteJournalForServerDelete(BaseTransaction* trans, |
44 bool was_deleted, | 53 bool was_deleted, |
45 const EntryKernel& entry); | 54 const EntryKernel& entry); |
(...skipping 14 matching lines...) Expand all Loading... |
60 // in |to_purge|. This should be called after model association and | 69 // in |to_purge|. This should be called after model association and |
61 // |to_purge| should contain handles of the entries whose deletions are | 70 // |to_purge| should contain handles of the entries whose deletions are |
62 // confirmed in native model. Can be called on any thread. | 71 // confirmed in native model. Can be called on any thread. |
63 void PurgeDeleteJournals(BaseTransaction* trans, | 72 void PurgeDeleteJournals(BaseTransaction* trans, |
64 const MetahandleSet& to_purge); | 73 const MetahandleSet& to_purge); |
65 | 74 |
66 // Move entries in |delete_journals_| whose types are in | 75 // Move entries in |delete_journals_| whose types are in |
67 // |passive_delete_journal_types_| to |journal_entries|. Move handles in | 76 // |passive_delete_journal_types_| to |journal_entries|. Move handles in |
68 // |delete_journals_to_purge_| to |journals_to_purge|. Called on sync thread. | 77 // |delete_journals_to_purge_| to |journals_to_purge|. Called on sync thread. |
69 void TakeSnapshotAndClear(BaseTransaction* trans, | 78 void TakeSnapshotAndClear(BaseTransaction* trans, |
70 EntryKernelSet* journal_entries, | 79 OwnedEntryKernelSet* journal_entries, |
71 MetahandleSet* journals_to_purge); | 80 MetahandleSet* journals_to_purge); |
72 | 81 |
73 // Add |entries| to |delete_journals_| regardless of their SERVER_IS_DEL | 82 // Add |entries| to |delete_journals_| regardless of their SERVER_IS_DEL |
74 // value. This is used to: | 83 // value. This is used to: |
75 // * restore delete journals from snapshot if snapshot failed to save. | 84 // * restore delete journals from snapshot if snapshot failed to save. |
76 // * batch add entries of a data type with unrecoverable error to delete | 85 // * batch add entries of a data type with unrecoverable error to delete |
77 // journal before purging them. | 86 // journal before purging them. |
78 // Called on sync thread. | 87 // Called on sync thread. |
79 void AddJournalBatch(BaseTransaction* trans, const EntryKernelSet& entries); | 88 void AddJournalBatch(BaseTransaction* trans, |
| 89 const OwnedEntryKernelSet& entries); |
80 | 90 |
81 // Return true if delete journals of |type| are maintained. | 91 // Return true if delete journals of |type| are maintained. |
82 static bool IsDeleteJournalEnabled(ModelType type); | 92 static bool IsDeleteJournalEnabled(ModelType type); |
83 | 93 |
84 private: | 94 private: |
85 FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest, ManageDeleteJournals); | 95 FRIEND_TEST_ALL_PREFIXES(SyncableDirectoryTest, ManageDeleteJournals); |
86 | 96 |
87 // Contains deleted entries that may not be persisted in native models. And | 97 // Contains deleted entries that may not be persisted in native models. And |
88 // in case of unrecoverable error, all purged entries are moved here for | 98 // in case of unrecoverable error, all purged entries are moved here for |
89 // bookkeeping to prevent back-from-dead entries that are deleted elsewhere | 99 // bookkeeping to prevent back-from-dead entries that are deleted elsewhere |
90 // when sync's down. | 100 // when sync's down. |
91 JournalIndex delete_journals_; | 101 JournalIndex delete_journals_; |
92 | 102 |
93 // Contains meta handles of deleted entries that have been persisted or | 103 // Contains meta handles of deleted entries that have been persisted or |
94 // undeleted, thus can be removed from database. | 104 // undeleted, thus can be removed from database. |
95 MetahandleSet delete_journals_to_purge_; | 105 MetahandleSet delete_journals_to_purge_; |
96 | 106 |
97 // Delete journals of these types can be cleared from memory after being | 107 // Delete journals of these types can be cleared from memory after being |
98 // saved to database. | 108 // saved to database. |
99 ModelTypeSet passive_delete_journal_types_; | 109 ModelTypeSet passive_delete_journal_types_; |
100 | 110 |
101 DISALLOW_COPY_AND_ASSIGN(DeleteJournal); | 111 DISALLOW_COPY_AND_ASSIGN(DeleteJournal); |
102 }; | 112 }; |
103 | 113 |
104 } // namespace syncable | 114 } // namespace syncable |
105 } // namespace syncer | 115 } // namespace syncer |
106 | 116 |
107 #endif // COMPONENTS_SYNC_SYNCABLE_SYNCABLE_DELETE_JOURNAL_H_ | 117 #endif // COMPONENTS_SYNC_SYNCABLE_SYNCABLE_DELETE_JOURNAL_H_ |
OLD | NEW |