OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #include "chrome/browser/sync/engine/get_commit_ids_command.h" | 5 #include "chrome/browser/sync/engine/get_commit_ids_command.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <utility> | 8 #include <utility> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
(...skipping 26 matching lines...) Expand all Loading... |
37 &all_unsynced_handles); | 37 &all_unsynced_handles); |
38 | 38 |
39 Cryptographer* cryptographer = | 39 Cryptographer* cryptographer = |
40 session->context()->directory_manager()->GetCryptographer( | 40 session->context()->directory_manager()->GetCryptographer( |
41 session->write_transaction()); | 41 session->write_transaction()); |
42 if (cryptographer) { | 42 if (cryptographer) { |
43 encrypted_types_ = cryptographer->GetEncryptedTypes(); | 43 encrypted_types_ = cryptographer->GetEncryptedTypes(); |
44 passphrase_missing_ = cryptographer->has_pending_keys(); | 44 passphrase_missing_ = cryptographer->has_pending_keys(); |
45 }; | 45 }; |
46 | 46 |
47 const syncable::ModelEnumSet throttled_types = | 47 const syncable::ModelTypeSet throttled_types = |
48 session->context()->GetThrottledTypes(); | 48 session->context()->GetThrottledTypes(); |
49 // We filter out all unready entries from the set of unsynced handles to | 49 // We filter out all unready entries from the set of unsynced handles to |
50 // ensure we don't trigger useless sync cycles attempting to retry due to | 50 // ensure we don't trigger useless sync cycles attempting to retry due to |
51 // there being work to do. (see ScheduleNextSync in sync_scheduler) | 51 // there being work to do. (see ScheduleNextSync in sync_scheduler) |
52 FilterUnreadyEntries(session->write_transaction(), | 52 FilterUnreadyEntries(session->write_transaction(), |
53 throttled_types, | 53 throttled_types, |
54 &all_unsynced_handles); | 54 &all_unsynced_handles); |
55 | 55 |
56 StatusController* status = session->mutable_status_controller(); | 56 StatusController* status = session->mutable_status_controller(); |
57 status->set_unsynced_handles(all_unsynced_handles); | 57 status->set_unsynced_handles(all_unsynced_handles); |
58 BuildCommitIds(status->unsynced_handles(), session->write_transaction(), | 58 BuildCommitIds(status->unsynced_handles(), session->write_transaction(), |
59 session->routing_info(), throttled_types); | 59 session->routing_info(), throttled_types); |
60 | 60 |
61 const vector<syncable::Id>& verified_commit_ids = | 61 const vector<syncable::Id>& verified_commit_ids = |
62 ordered_commit_set_->GetAllCommitIds(); | 62 ordered_commit_set_->GetAllCommitIds(); |
63 | 63 |
64 for (size_t i = 0; i < verified_commit_ids.size(); i++) | 64 for (size_t i = 0; i < verified_commit_ids.size(); i++) |
65 DVLOG(1) << "Debug commit batch result:" << verified_commit_ids[i]; | 65 DVLOG(1) << "Debug commit batch result:" << verified_commit_ids[i]; |
66 | 66 |
67 status->set_commit_set(*ordered_commit_set_.get()); | 67 status->set_commit_set(*ordered_commit_set_.get()); |
68 } | 68 } |
69 | 69 |
70 namespace { | 70 namespace { |
71 | 71 |
72 // An entry ready for commit is defined as: | 72 // An entry ready for commit is defined as: |
73 // 1. Not in conflict (SERVER_VERSION == BASE_VERSION || SERVER_VERSION == 0) | 73 // 1. Not in conflict (SERVER_VERSION == BASE_VERSION || SERVER_VERSION == 0) |
74 // and not requiring encryption (any entry containing an encrypted datatype | 74 // and not requiring encryption (any entry containing an encrypted datatype |
75 // while the cryptographer requires a passphrase is not ready for commit.) | 75 // while the cryptographer requires a passphrase is not ready for commit.) |
76 // 2. Its type is not currently throttled. | 76 // 2. Its type is not currently throttled. |
77 bool IsEntryReadyForCommit(syncable::ModelEnumSet encrypted_types, | 77 bool IsEntryReadyForCommit(syncable::ModelTypeSet encrypted_types, |
78 bool passphrase_missing, | 78 bool passphrase_missing, |
79 const syncable::Entry& entry, | 79 const syncable::Entry& entry, |
80 syncable::ModelEnumSet throttled_types) { | 80 syncable::ModelTypeSet throttled_types) { |
81 if (!entry.Get(syncable::IS_UNSYNCED)) | 81 if (!entry.Get(syncable::IS_UNSYNCED)) |
82 return false; | 82 return false; |
83 | 83 |
84 if (entry.Get(syncable::SERVER_VERSION) > 0 && | 84 if (entry.Get(syncable::SERVER_VERSION) > 0 && |
85 (entry.Get(syncable::SERVER_VERSION) > | 85 (entry.Get(syncable::SERVER_VERSION) > |
86 entry.Get(syncable::BASE_VERSION))) { | 86 entry.Get(syncable::BASE_VERSION))) { |
87 // The local and server versions don't match. The item must be in | 87 // The local and server versions don't match. The item must be in |
88 // conflict, so there's no point in attempting to commit. | 88 // conflict, so there's no point in attempting to commit. |
89 DCHECK(entry.Get(syncable::IS_UNAPPLIED_UPDATE)); // In conflict. | 89 DCHECK(entry.Get(syncable::IS_UNAPPLIED_UPDATE)); // In conflict. |
90 // TODO(zea): switch this to DVLOG once it's clear bug 100660 is fixed. | 90 // TODO(zea): switch this to DVLOG once it's clear bug 100660 is fixed. |
(...skipping 23 matching lines...) Expand all Loading... |
114 if (throttled_types.Has(type)) | 114 if (throttled_types.Has(type)) |
115 return false; | 115 return false; |
116 | 116 |
117 return true; | 117 return true; |
118 } | 118 } |
119 | 119 |
120 } // namespace | 120 } // namespace |
121 | 121 |
122 void GetCommitIdsCommand::FilterUnreadyEntries( | 122 void GetCommitIdsCommand::FilterUnreadyEntries( |
123 syncable::BaseTransaction* trans, | 123 syncable::BaseTransaction* trans, |
124 syncable::ModelEnumSet throttled_types, | 124 syncable::ModelTypeSet throttled_types, |
125 syncable::Directory::UnsyncedMetaHandles* unsynced_handles) { | 125 syncable::Directory::UnsyncedMetaHandles* unsynced_handles) { |
126 syncable::Directory::UnsyncedMetaHandles::iterator iter; | 126 syncable::Directory::UnsyncedMetaHandles::iterator iter; |
127 syncable::Directory::UnsyncedMetaHandles new_unsynced_handles; | 127 syncable::Directory::UnsyncedMetaHandles new_unsynced_handles; |
128 new_unsynced_handles.reserve(unsynced_handles->size()); | 128 new_unsynced_handles.reserve(unsynced_handles->size()); |
129 for (iter = unsynced_handles->begin(); | 129 for (iter = unsynced_handles->begin(); |
130 iter != unsynced_handles->end(); | 130 iter != unsynced_handles->end(); |
131 ++iter) { | 131 ++iter) { |
132 syncable::Entry entry(trans, syncable::GET_BY_HANDLE, *iter); | 132 syncable::Entry entry(trans, syncable::GET_BY_HANDLE, *iter); |
133 if (IsEntryReadyForCommit(encrypted_types_, | 133 if (IsEntryReadyForCommit(encrypted_types_, |
134 passphrase_missing_, | 134 passphrase_missing_, |
135 entry, | 135 entry, |
136 throttled_types)) | 136 throttled_types)) |
137 new_unsynced_handles.push_back(*iter); | 137 new_unsynced_handles.push_back(*iter); |
138 } | 138 } |
139 if (new_unsynced_handles.size() != unsynced_handles->size()) | 139 if (new_unsynced_handles.size() != unsynced_handles->size()) |
140 unsynced_handles->swap(new_unsynced_handles); | 140 unsynced_handles->swap(new_unsynced_handles); |
141 } | 141 } |
142 | 142 |
143 void GetCommitIdsCommand::AddUncommittedParentsAndTheirPredecessors( | 143 void GetCommitIdsCommand::AddUncommittedParentsAndTheirPredecessors( |
144 syncable::BaseTransaction* trans, | 144 syncable::BaseTransaction* trans, |
145 syncable::Id parent_id, | 145 syncable::Id parent_id, |
146 const ModelSafeRoutingInfo& routes, | 146 const ModelSafeRoutingInfo& routes, |
147 syncable::ModelEnumSet throttled_types) { | 147 syncable::ModelTypeSet throttled_types) { |
148 OrderedCommitSet item_dependencies(routes); | 148 OrderedCommitSet item_dependencies(routes); |
149 | 149 |
150 // Climb the tree adding entries leaf -> root. | 150 // Climb the tree adding entries leaf -> root. |
151 while (!parent_id.ServerKnows()) { | 151 while (!parent_id.ServerKnows()) { |
152 syncable::Entry parent(trans, syncable::GET_BY_ID, parent_id); | 152 syncable::Entry parent(trans, syncable::GET_BY_ID, parent_id); |
153 CHECK(parent.good()) << "Bad user-only parent in item path."; | 153 CHECK(parent.good()) << "Bad user-only parent in item path."; |
154 int64 handle = parent.Get(syncable::META_HANDLE); | 154 int64 handle = parent.Get(syncable::META_HANDLE); |
155 if (ordered_commit_set_->HaveCommitItem(handle) || | 155 if (ordered_commit_set_->HaveCommitItem(handle) || |
156 item_dependencies.HaveCommitItem(handle)) { | 156 item_dependencies.HaveCommitItem(handle)) { |
157 break; | 157 break; |
158 } | 158 } |
159 if (!AddItemThenPredecessors(trans, throttled_types, &parent, | 159 if (!AddItemThenPredecessors(trans, throttled_types, &parent, |
160 syncable::IS_UNSYNCED, | 160 syncable::IS_UNSYNCED, |
161 &item_dependencies)) { | 161 &item_dependencies)) { |
162 break; // Parent was already present in the set. | 162 break; // Parent was already present in the set. |
163 } | 163 } |
164 parent_id = parent.Get(syncable::PARENT_ID); | 164 parent_id = parent.Get(syncable::PARENT_ID); |
165 } | 165 } |
166 | 166 |
167 // Reverse what we added to get the correct order. | 167 // Reverse what we added to get the correct order. |
168 ordered_commit_set_->AppendReverse(item_dependencies); | 168 ordered_commit_set_->AppendReverse(item_dependencies); |
169 } | 169 } |
170 | 170 |
171 bool GetCommitIdsCommand::AddItem(syncable::Entry* item, | 171 bool GetCommitIdsCommand::AddItem(syncable::Entry* item, |
172 syncable::ModelEnumSet throttled_types, | 172 syncable::ModelTypeSet throttled_types, |
173 OrderedCommitSet* result) { | 173 OrderedCommitSet* result) { |
174 if (!IsEntryReadyForCommit(encrypted_types_, passphrase_missing_, *item, | 174 if (!IsEntryReadyForCommit(encrypted_types_, passphrase_missing_, *item, |
175 throttled_types)) | 175 throttled_types)) |
176 return false; | 176 return false; |
177 int64 item_handle = item->Get(syncable::META_HANDLE); | 177 int64 item_handle = item->Get(syncable::META_HANDLE); |
178 if (result->HaveCommitItem(item_handle) || | 178 if (result->HaveCommitItem(item_handle) || |
179 ordered_commit_set_->HaveCommitItem(item_handle)) { | 179 ordered_commit_set_->HaveCommitItem(item_handle)) { |
180 return false; | 180 return false; |
181 } | 181 } |
182 result->AddCommitItem(item_handle, item->Get(syncable::ID), | 182 result->AddCommitItem(item_handle, item->Get(syncable::ID), |
183 item->GetModelType()); | 183 item->GetModelType()); |
184 return true; | 184 return true; |
185 } | 185 } |
186 | 186 |
187 bool GetCommitIdsCommand::AddItemThenPredecessors( | 187 bool GetCommitIdsCommand::AddItemThenPredecessors( |
188 syncable::BaseTransaction* trans, | 188 syncable::BaseTransaction* trans, |
189 syncable::ModelEnumSet throttled_types, | 189 syncable::ModelTypeSet throttled_types, |
190 syncable::Entry* item, | 190 syncable::Entry* item, |
191 syncable::IndexedBitField inclusion_filter, | 191 syncable::IndexedBitField inclusion_filter, |
192 OrderedCommitSet* result) { | 192 OrderedCommitSet* result) { |
193 if (!AddItem(item, throttled_types, result)) | 193 if (!AddItem(item, throttled_types, result)) |
194 return false; | 194 return false; |
195 if (item->Get(syncable::IS_DEL)) | 195 if (item->Get(syncable::IS_DEL)) |
196 return true; // Deleted items have no predecessors. | 196 return true; // Deleted items have no predecessors. |
197 | 197 |
198 syncable::Id prev_id = item->Get(syncable::PREV_ID); | 198 syncable::Id prev_id = item->Get(syncable::PREV_ID); |
199 while (!prev_id.IsRoot()) { | 199 while (!prev_id.IsRoot()) { |
200 syncable::Entry prev(trans, syncable::GET_BY_ID, prev_id); | 200 syncable::Entry prev(trans, syncable::GET_BY_ID, prev_id); |
201 CHECK(prev.good()) << "Bad id when walking predecessors."; | 201 CHECK(prev.good()) << "Bad id when walking predecessors."; |
202 if (!prev.Get(inclusion_filter)) | 202 if (!prev.Get(inclusion_filter)) |
203 break; | 203 break; |
204 if (!AddItem(&prev, throttled_types, result)) | 204 if (!AddItem(&prev, throttled_types, result)) |
205 break; | 205 break; |
206 prev_id = prev.Get(syncable::PREV_ID); | 206 prev_id = prev.Get(syncable::PREV_ID); |
207 } | 207 } |
208 return true; | 208 return true; |
209 } | 209 } |
210 | 210 |
211 void GetCommitIdsCommand::AddPredecessorsThenItem( | 211 void GetCommitIdsCommand::AddPredecessorsThenItem( |
212 syncable::BaseTransaction* trans, | 212 syncable::BaseTransaction* trans, |
213 syncable::ModelEnumSet throttled_types, | 213 syncable::ModelTypeSet throttled_types, |
214 syncable::Entry* item, | 214 syncable::Entry* item, |
215 syncable::IndexedBitField inclusion_filter, | 215 syncable::IndexedBitField inclusion_filter, |
216 const ModelSafeRoutingInfo& routes) { | 216 const ModelSafeRoutingInfo& routes) { |
217 OrderedCommitSet item_dependencies(routes); | 217 OrderedCommitSet item_dependencies(routes); |
218 AddItemThenPredecessors(trans, throttled_types, item, inclusion_filter, | 218 AddItemThenPredecessors(trans, throttled_types, item, inclusion_filter, |
219 &item_dependencies); | 219 &item_dependencies); |
220 | 220 |
221 // Reverse what we added to get the correct order. | 221 // Reverse what we added to get the correct order. |
222 ordered_commit_set_->AppendReverse(item_dependencies); | 222 ordered_commit_set_->AppendReverse(item_dependencies); |
223 } | 223 } |
224 | 224 |
225 bool GetCommitIdsCommand::IsCommitBatchFull() { | 225 bool GetCommitIdsCommand::IsCommitBatchFull() { |
226 return ordered_commit_set_->Size() >= requested_commit_batch_size_; | 226 return ordered_commit_set_->Size() >= requested_commit_batch_size_; |
227 } | 227 } |
228 | 228 |
229 void GetCommitIdsCommand::AddCreatesAndMoves( | 229 void GetCommitIdsCommand::AddCreatesAndMoves( |
230 const vector<int64>& unsynced_handles, | 230 const vector<int64>& unsynced_handles, |
231 syncable::WriteTransaction* write_transaction, | 231 syncable::WriteTransaction* write_transaction, |
232 const ModelSafeRoutingInfo& routes, | 232 const ModelSafeRoutingInfo& routes, |
233 syncable::ModelEnumSet throttled_types) { | 233 syncable::ModelTypeSet throttled_types) { |
234 // Add moves and creates, and prepend their uncommitted parents. | 234 // Add moves and creates, and prepend their uncommitted parents. |
235 for (CommitMetahandleIterator iterator(unsynced_handles, write_transaction, | 235 for (CommitMetahandleIterator iterator(unsynced_handles, write_transaction, |
236 ordered_commit_set_.get()); | 236 ordered_commit_set_.get()); |
237 !IsCommitBatchFull() && iterator.Valid(); | 237 !IsCommitBatchFull() && iterator.Valid(); |
238 iterator.Increment()) { | 238 iterator.Increment()) { |
239 int64 metahandle = iterator.Current(); | 239 int64 metahandle = iterator.Current(); |
240 | 240 |
241 syncable::Entry entry(write_transaction, | 241 syncable::Entry entry(write_transaction, |
242 syncable::GET_BY_HANDLE, | 242 syncable::GET_BY_HANDLE, |
243 metahandle); | 243 metahandle); |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
334 ordered_commit_set_->AddCommitItem(metahandle, entry.Get(syncable::ID), | 334 ordered_commit_set_->AddCommitItem(metahandle, entry.Get(syncable::ID), |
335 entry.GetModelType()); | 335 entry.GetModelType()); |
336 } | 336 } |
337 } | 337 } |
338 } | 338 } |
339 } | 339 } |
340 | 340 |
341 void GetCommitIdsCommand::BuildCommitIds(const vector<int64>& unsynced_handles, | 341 void GetCommitIdsCommand::BuildCommitIds(const vector<int64>& unsynced_handles, |
342 syncable::WriteTransaction* write_transaction, | 342 syncable::WriteTransaction* write_transaction, |
343 const ModelSafeRoutingInfo& routes, | 343 const ModelSafeRoutingInfo& routes, |
344 syncable::ModelEnumSet throttled_types) { | 344 syncable::ModelTypeSet throttled_types) { |
345 ordered_commit_set_.reset(new OrderedCommitSet(routes)); | 345 ordered_commit_set_.reset(new OrderedCommitSet(routes)); |
346 // Commits follow these rules: | 346 // Commits follow these rules: |
347 // 1. Moves or creates are preceded by needed folder creates, from | 347 // 1. Moves or creates are preceded by needed folder creates, from |
348 // root to leaf. For folders whose contents are ordered, moves | 348 // root to leaf. For folders whose contents are ordered, moves |
349 // and creates appear in order. | 349 // and creates appear in order. |
350 // 2. Moves/Creates before deletes. | 350 // 2. Moves/Creates before deletes. |
351 // 3. Deletes, collapsed. | 351 // 3. Deletes, collapsed. |
352 // We commit deleted moves under deleted items as moves when collapsing | 352 // We commit deleted moves under deleted items as moves when collapsing |
353 // delete trees. | 353 // delete trees. |
354 | 354 |
355 // Add moves and creates, and prepend their uncommitted parents. | 355 // Add moves and creates, and prepend their uncommitted parents. |
356 AddCreatesAndMoves(unsynced_handles, write_transaction, routes, | 356 AddCreatesAndMoves(unsynced_handles, write_transaction, routes, |
357 throttled_types); | 357 throttled_types); |
358 | 358 |
359 // Add all deletes. | 359 // Add all deletes. |
360 AddDeletes(unsynced_handles, write_transaction); | 360 AddDeletes(unsynced_handles, write_transaction); |
361 } | 361 } |
362 | 362 |
363 } // namespace browser_sync | 363 } // namespace browser_sync |
OLD | NEW |