OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "sync/syncable/syncable_delete_journal.h" | |
6 | |
7 #include <stddef.h> | |
8 #include <stdint.h> | |
9 | |
10 #include "base/stl_util.h" | |
11 #include "sync/internal_api/public/base/model_type.h" | |
12 | |
13 namespace syncer { | |
14 namespace syncable { | |
15 | |
16 DeleteJournal::DeleteJournal(JournalIndex* initial_journal) { | |
17 CHECK(initial_journal); | |
18 delete_journals_.swap(*initial_journal); | |
19 } | |
20 | |
21 DeleteJournal::~DeleteJournal() { | |
22 STLDeleteElements(&delete_journals_); | |
23 } | |
24 | |
25 size_t DeleteJournal::GetDeleteJournalSize(BaseTransaction* trans) const { | |
26 DCHECK(trans); | |
27 return delete_journals_.size(); | |
28 } | |
29 | |
30 void DeleteJournal::UpdateDeleteJournalForServerDelete( | |
31 BaseTransaction* trans, bool was_deleted, const EntryKernel& entry) { | |
32 DCHECK(trans); | |
33 | |
34 // Should be sufficient to check server type only but check for local | |
35 // type too because of incomplete test setup. | |
36 if (!(IsDeleteJournalEnabled(entry.GetServerModelType()) || | |
37 IsDeleteJournalEnabled( | |
38 GetModelTypeFromSpecifics(entry.ref(SPECIFICS))))) { | |
39 return; | |
40 } | |
41 | |
42 JournalIndex::iterator it = delete_journals_.find(&entry); | |
43 | |
44 if (entry.ref(SERVER_IS_DEL)) { | |
45 if (it == delete_journals_.end()) { | |
46 // New delete. | |
47 EntryKernel* t = new EntryKernel(entry); | |
48 delete_journals_.insert(t); | |
49 delete_journals_to_purge_.erase(t->ref(META_HANDLE)); | |
50 } | |
51 } else { | |
52 // Undelete. This could happen in two cases: | |
53 // * An entry was deleted then undeleted, i.e. server delete was | |
54 // overwritten because of entry has unsynced data locally. | |
55 // * A data type was broken, i.e. encountered unrecoverable error, in last | |
56 // sync session and all its entries were duplicated in delete journals. | |
57 // On restart, entries are recreated from downloads and recreation calls | |
58 // UpdateDeleteJournals() to remove live entries from delete journals, | |
59 // thus only deleted entries remain in journals. | |
60 if (it != delete_journals_.end()) { | |
61 delete_journals_to_purge_.insert((*it)->ref(META_HANDLE)); | |
62 delete *it; | |
63 delete_journals_.erase(it); | |
64 } else if (was_deleted) { | |
65 delete_journals_to_purge_.insert(entry.ref(META_HANDLE)); | |
66 } | |
67 } | |
68 } | |
69 | |
70 void DeleteJournal::GetDeleteJournals(BaseTransaction* trans, | |
71 ModelType type, | |
72 EntryKernelSet* deleted_entries) { | |
73 DCHECK(trans); | |
74 for (JournalIndex::const_iterator it = delete_journals_.begin(); | |
75 it != delete_journals_.end(); ++it) { | |
76 if ((*it)->GetServerModelType() == type || | |
77 GetModelTypeFromSpecifics((*it)->ref(SPECIFICS)) == type) { | |
78 deleted_entries->insert(*it); | |
79 } | |
80 } | |
81 passive_delete_journal_types_.Put(type); | |
82 } | |
83 | |
84 void DeleteJournal::PurgeDeleteJournals(BaseTransaction* trans, | |
85 const MetahandleSet& to_purge) { | |
86 DCHECK(trans); | |
87 JournalIndex::iterator it = delete_journals_.begin(); | |
88 while (it != delete_journals_.end()) { | |
89 int64_t handle = (*it)->ref(META_HANDLE); | |
90 if (to_purge.count(handle)) { | |
91 delete *it; | |
92 delete_journals_.erase(it++); | |
93 } else { | |
94 ++it; | |
95 } | |
96 } | |
97 delete_journals_to_purge_.insert(to_purge.begin(), to_purge.end()); | |
98 } | |
99 | |
100 void DeleteJournal::TakeSnapshotAndClear(BaseTransaction* trans, | |
101 EntryKernelSet* journal_entries, | |
102 MetahandleSet* journals_to_purge) { | |
103 DCHECK(trans); | |
104 // Move passive delete journals to snapshot. Will copy back if snapshot fails | |
105 // to save. | |
106 JournalIndex::iterator it = delete_journals_.begin(); | |
107 while (it != delete_journals_.end()) { | |
108 if (passive_delete_journal_types_.Has((*it)->GetServerModelType()) || | |
109 passive_delete_journal_types_.Has(GetModelTypeFromSpecifics( | |
110 (*it)->ref(SPECIFICS)))) { | |
111 journal_entries->insert(*it); | |
112 delete_journals_.erase(it++); | |
113 } else { | |
114 ++it; | |
115 } | |
116 } | |
117 *journals_to_purge = delete_journals_to_purge_; | |
118 delete_journals_to_purge_.clear(); | |
119 } | |
120 | |
121 void DeleteJournal::AddJournalBatch(BaseTransaction* trans, | |
122 const EntryKernelSet& entries) { | |
123 DCHECK(trans); | |
124 EntryKernel needle; | |
125 for (EntryKernelSet::const_iterator i = entries.begin(); | |
126 i != entries.end(); ++i) { | |
127 needle.put(ID, (*i)->ref(ID)); | |
128 if (delete_journals_.find(&needle) == delete_journals_.end()) { | |
129 delete_journals_.insert(new EntryKernel(**i)); | |
130 } | |
131 delete_journals_to_purge_.erase((*i)->ref(META_HANDLE)); | |
132 } | |
133 } | |
134 | |
135 /* static */ | |
136 bool DeleteJournal::IsDeleteJournalEnabled(ModelType type) { | |
137 switch (type) { | |
138 case BOOKMARKS: | |
139 return true; | |
140 default: | |
141 return false; | |
142 } | |
143 } | |
144 | |
145 } // namespace syncable | |
146 } // namespace syncer | |
OLD | NEW |