OLD | NEW |
---|---|
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 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 "sync/engine/get_commit_ids_command.h" | 5 #include "sync/engine/get_commit_ids.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <utility> | |
9 #include <vector> | 8 #include <vector> |
10 | 9 |
10 #include "base/basictypes.h" | |
11 #include "sync/engine/syncer_util.h" | 11 #include "sync/engine/syncer_util.h" |
12 #include "sync/sessions/nudge_tracker.h" | 12 #include "sync/syncable/directory.h" |
13 #include "sync/syncable/entry.h" | 13 #include "sync/syncable/entry.h" |
14 #include "sync/syncable/nigori_handler.h" | 14 #include "sync/syncable/nigori_handler.h" |
15 #include "sync/syncable/nigori_util.h" | 15 #include "sync/syncable/nigori_util.h" |
16 #include "sync/syncable/syncable_base_transaction.h" | 16 #include "sync/syncable/syncable_base_transaction.h" |
17 #include "sync/syncable/syncable_util.h" | 17 #include "sync/syncable/syncable_util.h" |
18 #include "sync/util/cryptographer.h" | 18 #include "sync/util/cryptographer.h" |
19 | 19 |
20 using std::set; | 20 using std::set; |
21 using std::vector; | 21 using std::vector; |
22 | 22 |
23 namespace syncer { | 23 namespace syncer { |
24 | 24 |
25 using sessions::OrderedCommitSet; | 25 void GetCommitIdsForType( |
26 using sessions::SyncSession; | 26 syncable::BaseTransaction* trans, |
27 using sessions::StatusController; | 27 ModelType type, |
28 const size_t max_entries, | |
29 std::vector<int64>* out) { | |
30 syncable::Directory* dir = trans->directory(); | |
28 | 31 |
29 GetCommitIdsCommand::GetCommitIdsCommand( | |
30 syncable::BaseTransaction* trans, | |
31 ModelTypeSet requested_types, | |
32 const size_t commit_batch_size, | |
33 sessions::OrderedCommitSet* commit_set) | |
34 : trans_(trans), | |
35 requested_types_(requested_types), | |
36 requested_commit_batch_size_(commit_batch_size), | |
37 commit_set_(commit_set) { | |
38 } | |
39 | |
40 GetCommitIdsCommand::~GetCommitIdsCommand() {} | |
41 | |
42 SyncerError GetCommitIdsCommand::ExecuteImpl(SyncSession* session) { | |
43 // Gather the full set of unsynced items and store it in the session. They | 32 // Gather the full set of unsynced items and store it in the session. They |
44 // are not in the correct order for commit. | 33 // are not in the correct order for commit. |
45 std::set<int64> ready_unsynced_set; | 34 std::set<int64> ready_unsynced_set; |
46 syncable::Directory::Metahandles all_unsynced_handles; | 35 syncable::Directory::Metahandles all_unsynced_handles; |
47 GetUnsyncedEntries(trans_, | 36 GetUnsyncedEntries(trans, &all_unsynced_handles); |
48 &all_unsynced_handles); | |
49 | 37 |
50 ModelTypeSet encrypted_types; | 38 ModelTypeSet encrypted_types; |
51 bool passphrase_missing = false; | 39 bool passphrase_missing = false; |
52 Cryptographer* cryptographer = | 40 Cryptographer* cryptographer = dir->GetCryptographer(trans); |
53 session->context()-> | |
54 directory()->GetCryptographer(trans_); | |
55 if (cryptographer) { | 41 if (cryptographer) { |
56 encrypted_types = session->context()->directory()->GetNigoriHandler()-> | 42 encrypted_types = dir->GetNigoriHandler()->GetEncryptedTypes(trans); |
57 GetEncryptedTypes(trans_); | |
58 passphrase_missing = cryptographer->has_pending_keys(); | 43 passphrase_missing = cryptographer->has_pending_keys(); |
59 }; | 44 }; |
60 | 45 |
61 // We filter out all unready entries from the set of unsynced handles. This | 46 // We filter out all unready entries from the set of unsynced handles. This |
62 // new set of ready and unsynced items is then what we use to determine what | 47 // new set of ready and unsynced items is then what we use to determine what |
63 // is a candidate for commit. The caller of this SyncerCommand is responsible | 48 // is a candidate for commit. The caller of this SyncerCommand is responsible |
64 // for ensuring that no throttled types are included among the | 49 // for ensuring that no throttled types are included among the |
65 // requested_types. | 50 // requested_types. |
66 FilterUnreadyEntries(trans_, | 51 FilterUnreadyEntries(trans, |
67 requested_types_, | 52 ModelTypeSet(type), |
68 encrypted_types, | 53 encrypted_types, |
69 passphrase_missing, | 54 passphrase_missing, |
70 all_unsynced_handles, | 55 all_unsynced_handles, |
71 &ready_unsynced_set); | 56 &ready_unsynced_set); |
72 | 57 |
73 BuildCommitIds(trans_, | 58 OrderCommitIds(trans, max_entries, ready_unsynced_set, out); |
74 session->context()->routing_info(), | |
75 ready_unsynced_set); | |
76 | 59 |
77 return SYNCER_OK; | 60 for (size_t i = 0; i < out->size(); i++) |
61 DVLOG(1) << "Debug commit batch result:" << (*out)[i]; | |
78 } | 62 } |
79 | 63 |
80 namespace { | |
81 | |
82 bool IsEntryInConflict(const syncable::Entry& entry) { | 64 bool IsEntryInConflict(const syncable::Entry& entry) { |
83 if (entry.Get(syncable::IS_UNSYNCED) && | 65 if (entry.Get(syncable::IS_UNSYNCED) && |
84 entry.Get(syncable::SERVER_VERSION) > 0 && | 66 entry.Get(syncable::SERVER_VERSION) > 0 && |
85 (entry.Get(syncable::SERVER_VERSION) > | 67 (entry.Get(syncable::SERVER_VERSION) > |
86 entry.Get(syncable::BASE_VERSION))) { | 68 entry.Get(syncable::BASE_VERSION))) { |
87 // The local and server versions don't match. The item must be in | 69 // The local and server versions don't match. The item must be in |
88 // conflict, so there's no point in attempting to commit. | 70 // conflict, so there's no point in attempting to commit. |
89 DCHECK(entry.Get(syncable::IS_UNAPPLIED_UPDATE)); | 71 DCHECK(entry.Get(syncable::IS_UNAPPLIED_UPDATE)); |
90 DVLOG(1) << "Excluding entry from commit due to version mismatch " | 72 DVLOG(1) << "Excluding entry from commit due to version mismatch " |
91 << entry; | 73 << entry; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
147 | 129 |
148 if (entry.IsRoot()) { | 130 if (entry.IsRoot()) { |
149 NOTREACHED() << "Permanent item became unsynced " << entry; | 131 NOTREACHED() << "Permanent item became unsynced " << entry; |
150 return false; | 132 return false; |
151 } | 133 } |
152 | 134 |
153 DVLOG(2) << "Entry is ready for commit: " << entry; | 135 DVLOG(2) << "Entry is ready for commit: " << entry; |
154 return true; | 136 return true; |
155 } | 137 } |
156 | 138 |
157 } // namespace | 139 void FilterUnreadyEntries( |
158 | |
159 void GetCommitIdsCommand::FilterUnreadyEntries( | |
160 syncable::BaseTransaction* trans, | 140 syncable::BaseTransaction* trans, |
161 ModelTypeSet requested_types, | 141 ModelTypeSet requested_types, |
162 ModelTypeSet encrypted_types, | 142 ModelTypeSet encrypted_types, |
163 bool passphrase_missing, | 143 bool passphrase_missing, |
164 const syncable::Directory::Metahandles& unsynced_handles, | 144 const syncable::Directory::Metahandles& unsynced_handles, |
165 std::set<int64>* ready_unsynced_set) { | 145 std::set<int64>* ready_unsynced_set) { |
166 for (syncable::Directory::Metahandles::const_iterator iter = | 146 for (syncable::Directory::Metahandles::const_iterator iter = |
167 unsynced_handles.begin(); iter != unsynced_handles.end(); ++iter) { | 147 unsynced_handles.begin(); iter != unsynced_handles.end(); ++iter) { |
168 syncable::Entry entry(trans, syncable::GET_BY_HANDLE, *iter); | 148 syncable::Entry entry(trans, syncable::GET_BY_HANDLE, *iter); |
169 if (IsEntryReadyForCommit(requested_types, | 149 if (IsEntryReadyForCommit(requested_types, |
170 encrypted_types, | 150 encrypted_types, |
171 passphrase_missing, | 151 passphrase_missing, |
172 entry)) { | 152 entry)) { |
173 ready_unsynced_set->insert(*iter); | 153 ready_unsynced_set->insert(*iter); |
174 } | 154 } |
175 } | 155 } |
176 } | 156 } |
177 | 157 |
178 bool GetCommitIdsCommand::AddUncommittedParentsAndTheirPredecessors( | 158 namespace { |
159 | |
160 // This class helps to implement OrderCommitIds(). Its members track the | |
161 // progress of a traversal while its methods extend it. It can return early if | |
162 // the traversal reaches the desired size before the full traversal is complete. | |
163 class Traversal { | |
164 public: | |
165 Traversal( | |
179 syncable::BaseTransaction* trans, | 166 syncable::BaseTransaction* trans, |
180 const ModelSafeRoutingInfo& routes, | 167 int64 max_entries, |
168 std::vector<int64>* out); | |
169 ~Traversal(); | |
170 | |
171 // The following functions do not modify the traversal directly. They return | |
172 // their results in the |result| vector instead. | |
173 | |
174 bool AddUncommittedParentsAndTheirPredecessors( | |
Nicolas Zea
2013/09/05 23:55:37
make these methods private?
rlarocque
2013/09/06 18:58:26
Done.
| |
175 const std::set<int64>& ready_unsynced_set, | |
176 const syncable::Entry& item, | |
177 std::vector<int64>* result) const; | |
178 | |
179 void TryAddItem(const std::set<int64>& ready_unsynced_set, | |
180 const syncable::Entry& item, | |
181 std::vector<int64>* result) const; | |
182 | |
183 void AddItemThenPredecessors( | |
184 const std::set<int64>& ready_unsynced_set, | |
185 const syncable::Entry& item, | |
186 std::vector<int64>* result) const; | |
187 | |
188 void AddPredecessorsThenItem( | |
189 const std::set<int64>& ready_unsynced_set, | |
190 const syncable::Entry& item, | |
191 std::vector<int64>* result) const; | |
192 | |
193 // First step of traversal building. Adds non-deleted items in order. | |
194 void AddCreatesAndMoves(const std::set<int64>& ready_unsynced_set); | |
195 | |
196 // Second step of traverals building. Appends deleted items. | |
197 void AddDeletes(const std::set<int64>& ready_unsynced_set); | |
198 | |
199 private: | |
200 // Returns true if we've collected enough items. | |
201 bool IsFull() const; | |
202 | |
203 // Returns true if the specified handle is already in the traversal. | |
204 bool HaveItem(int64 handle) const; | |
205 | |
206 // Adds the specified handles to the traversal. | |
207 void AppendManyToTraversal(std::vector<int64>); | |
Nicolas Zea
2013/09/05 23:55:37
variable name
rlarocque
2013/09/06 18:58:26
Done.
| |
208 | |
209 // Adds the specifed handle to the traversal. | |
210 void AppendToTraversal(int64 handle); | |
211 | |
212 std::vector<int64>* out_; | |
Nicolas Zea
2013/09/05 23:55:37
perhaps use Metahandles, which I believe is just t
rlarocque
2013/09/06 18:58:26
Done.
| |
213 std::set<int64> added_handles_; | |
rlarocque
2013/09/05 18:33:48
This member is not really necessary, but I felt ba
| |
214 const size_t max_entries_; | |
215 syncable::BaseTransaction* trans_; | |
Nicolas Zea
2013/09/05 23:55:37
DISALLOW_COPY_AND_ASSIGN?
rlarocque
2013/09/06 18:58:26
Done.
| |
216 }; | |
217 | |
218 Traversal::Traversal( | |
219 syncable::BaseTransaction* trans, | |
220 int64 max_entries, | |
221 std::vector<int64>* out) | |
222 : out_(out), | |
223 max_entries_(max_entries), | |
224 trans_(trans) { } | |
225 | |
226 Traversal::~Traversal() {} | |
227 | |
228 bool Traversal::AddUncommittedParentsAndTheirPredecessors( | |
181 const std::set<int64>& ready_unsynced_set, | 229 const std::set<int64>& ready_unsynced_set, |
182 const syncable::Entry& item, | 230 const syncable::Entry& item, |
183 sessions::OrderedCommitSet* result) const { | 231 std::vector<int64>* result) const { |
184 OrderedCommitSet item_dependencies(routes); | 232 std::vector<int64> dependencies; |
185 syncable::Id parent_id = item.Get(syncable::PARENT_ID); | 233 syncable::Id parent_id = item.Get(syncable::PARENT_ID); |
186 | 234 |
187 // Climb the tree adding entries leaf -> root. | 235 // Climb the tree adding entries leaf -> root. |
188 while (!parent_id.ServerKnows()) { | 236 while (!parent_id.ServerKnows()) { |
189 syncable::Entry parent(trans, syncable::GET_BY_ID, parent_id); | 237 syncable::Entry parent(trans_, syncable::GET_BY_ID, parent_id); |
190 CHECK(parent.good()) << "Bad user-only parent in item path."; | 238 CHECK(parent.good()) << "Bad user-only parent in item path."; |
191 int64 handle = parent.Get(syncable::META_HANDLE); | 239 int64 handle = parent.Get(syncable::META_HANDLE); |
192 if (commit_set_->HaveCommitItem(handle)) { | 240 if (HaveItem(handle)) { |
193 // We've already added this parent (and therefore all of its parents). | 241 // We've already added this parent (and therefore all of its parents). |
194 // We can return early. | 242 // We can return early. |
195 break; | 243 break; |
196 } | 244 } |
197 if (IsEntryInConflict(parent)) { | 245 if (IsEntryInConflict(parent)) { |
198 // We ignore all entries that are children of a conflicing item. Return | 246 // We ignore all entries that are children of a conflicing item. Return |
199 // false immediately to forget the traversal we've built up so far. | 247 // false immediately to forget the traversal we've built up so far. |
200 DVLOG(1) << "Parent was in conflict, omitting " << item; | 248 DVLOG(1) << "Parent was in conflict, omitting " << item; |
201 return false; | 249 return false; |
202 } | 250 } |
203 AddItemThenPredecessors(trans, | 251 AddItemThenPredecessors(ready_unsynced_set, |
204 ready_unsynced_set, | |
205 parent, | 252 parent, |
206 &item_dependencies); | 253 &dependencies); |
207 parent_id = parent.Get(syncable::PARENT_ID); | 254 parent_id = parent.Get(syncable::PARENT_ID); |
208 } | 255 } |
209 | 256 |
210 // Reverse what we added to get the correct order. | 257 // Reverse what we added to get the correct order. |
211 result->AppendReverse(item_dependencies); | 258 result->insert(result->end(), dependencies.begin(), dependencies.end()); |
Nicolas Zea
2013/09/05 23:55:37
this is no longer reversing right?
rlarocque
2013/09/06 18:58:26
Good catch! Somehow the unit tests missed this.
| |
212 return true; | 259 return true; |
213 } | 260 } |
214 | 261 |
215 // Adds the given item to the list if it is unsynced and ready for commit. | 262 // Adds the given item to the list if it is unsynced and ready for commit. |
216 void GetCommitIdsCommand::TryAddItem(const std::set<int64>& ready_unsynced_set, | 263 void Traversal::TryAddItem(const std::set<int64>& ready_unsynced_set, |
217 const syncable::Entry& item, | 264 const syncable::Entry& item, |
218 OrderedCommitSet* result) const { | 265 std::vector<int64>* result) const { |
219 DCHECK(item.Get(syncable::IS_UNSYNCED)); | 266 DCHECK(item.Get(syncable::IS_UNSYNCED)); |
220 int64 item_handle = item.Get(syncable::META_HANDLE); | 267 int64 item_handle = item.Get(syncable::META_HANDLE); |
221 if (ready_unsynced_set.count(item_handle) != 0) { | 268 if (ready_unsynced_set.count(item_handle) != 0) { |
222 result->AddCommitItem(item_handle, item.GetModelType()); | 269 result->push_back(item_handle); |
223 } | 270 } |
224 } | 271 } |
225 | 272 |
226 // Adds the given item, and all its unsynced predecessors. The traversal will | 273 // Adds the given item, and all its unsynced predecessors. The traversal will |
227 // be cut short if any item along the traversal is not IS_UNSYNCED, or if we | 274 // be cut short if any item along the traversal is not IS_UNSYNCED, or if we |
228 // detect that this area of the tree has already been traversed. Items that are | 275 // detect that this area of the tree has already been traversed. Items that are |
229 // not 'ready' for commit (see IsEntryReadyForCommit()) will not be added to the | 276 // not 'ready' for commit (see IsEntryReadyForCommit()) will not be added to the |
230 // list, though they will not stop the traversal. | 277 // list, though they will not stop the traversal. |
231 void GetCommitIdsCommand::AddItemThenPredecessors( | 278 void Traversal::AddItemThenPredecessors( |
232 syncable::BaseTransaction* trans, | |
233 const std::set<int64>& ready_unsynced_set, | 279 const std::set<int64>& ready_unsynced_set, |
234 const syncable::Entry& item, | 280 const syncable::Entry& item, |
235 OrderedCommitSet* result) const { | 281 std::vector<int64>* result) const { |
236 int64 item_handle = item.Get(syncable::META_HANDLE); | 282 int64 item_handle = item.Get(syncable::META_HANDLE); |
237 if (commit_set_->HaveCommitItem(item_handle)) { | 283 if (HaveItem(item_handle)) { |
238 // We've already added this item to the commit set, and so must have | 284 // We've already added this item to the commit set, and so must have |
239 // already added the predecessors as well. | 285 // already added the predecessors as well. |
240 return; | 286 return; |
241 } | 287 } |
242 TryAddItem(ready_unsynced_set, item, result); | 288 TryAddItem(ready_unsynced_set, item, result); |
243 if (item.Get(syncable::IS_DEL)) | 289 if (item.Get(syncable::IS_DEL)) |
244 return; // Deleted items have no predecessors. | 290 return; // Deleted items have no predecessors. |
245 | 291 |
246 syncable::Id prev_id = item.GetPredecessorId(); | 292 syncable::Id prev_id = item.GetPredecessorId(); |
247 while (!prev_id.IsRoot()) { | 293 while (!prev_id.IsRoot()) { |
248 syncable::Entry prev(trans, syncable::GET_BY_ID, prev_id); | 294 syncable::Entry prev(trans_, syncable::GET_BY_ID, prev_id); |
249 CHECK(prev.good()) << "Bad id when walking predecessors."; | 295 CHECK(prev.good()) << "Bad id when walking predecessors."; |
250 if (!prev.Get(syncable::IS_UNSYNCED)) { | 296 if (!prev.Get(syncable::IS_UNSYNCED)) { |
251 // We're interested in "runs" of unsynced items. This item breaks | 297 // We're interested in "runs" of unsynced items. This item breaks |
252 // the streak, so we stop traversing. | 298 // the streak, so we stop traversing. |
253 return; | 299 return; |
254 } | 300 } |
255 int64 handle = prev.Get(syncable::META_HANDLE); | 301 int64 handle = prev.Get(syncable::META_HANDLE); |
256 if (commit_set_->HaveCommitItem(handle)) { | 302 if (HaveItem(handle)) { |
257 // We've already added this item to the commit set, and so must have | 303 // We've already added this item to the commit set, and so must have |
258 // already added the predecessors as well. | 304 // already added the predecessors as well. |
259 return; | 305 return; |
260 } | 306 } |
261 TryAddItem(ready_unsynced_set, prev, result); | 307 TryAddItem(ready_unsynced_set, prev, result); |
262 prev_id = prev.GetPredecessorId(); | 308 prev_id = prev.GetPredecessorId(); |
263 } | 309 } |
264 } | 310 } |
265 | 311 |
266 // Same as AddItemThenPredecessor, but the traversal order will be reversed. | 312 // Same as AddItemThenPredecessor, but the traversal order will be reversed. |
267 void GetCommitIdsCommand::AddPredecessorsThenItem( | 313 void Traversal::AddPredecessorsThenItem( |
268 syncable::BaseTransaction* trans, | |
269 const ModelSafeRoutingInfo& routes, | |
270 const std::set<int64>& ready_unsynced_set, | 314 const std::set<int64>& ready_unsynced_set, |
271 const syncable::Entry& item, | 315 const syncable::Entry& item, |
272 OrderedCommitSet* result) const { | 316 std::vector<int64>* result) const { |
273 OrderedCommitSet item_dependencies(routes); | 317 std::vector<int64> dependencies; |
274 AddItemThenPredecessors(trans, ready_unsynced_set, item, &item_dependencies); | 318 AddItemThenPredecessors(ready_unsynced_set, item, &dependencies); |
275 | 319 |
276 // Reverse what we added to get the correct order. | 320 // Reverse what we added to get the correct order. |
277 result->AppendReverse(item_dependencies); | 321 result->insert(result->end(), dependencies.rbegin(), dependencies.rend()); |
278 } | 322 } |
279 | 323 |
280 bool GetCommitIdsCommand::IsCommitBatchFull() const { | 324 bool Traversal::IsFull() const { |
281 return commit_set_->Size() >= requested_commit_batch_size_; | 325 return out_->size() >= max_entries_; |
282 } | 326 } |
283 | 327 |
284 void GetCommitIdsCommand::AddCreatesAndMoves( | 328 bool Traversal::HaveItem(int64 handle) const { |
285 syncable::BaseTransaction* trans, | 329 return added_handles_.find(handle) != added_handles_.end(); |
286 const ModelSafeRoutingInfo& routes, | 330 } |
331 | |
332 void Traversal::AppendManyToTraversal(std::vector<int64> handles) { | |
333 out_->insert(out_->end(), handles.begin(), handles.end()); | |
334 added_handles_.insert(handles.begin(), handles.end()); | |
335 } | |
336 | |
337 void Traversal::AppendToTraversal(int64 metahandle) { | |
338 out_->push_back(metahandle); | |
339 added_handles_.insert(metahandle); | |
340 } | |
341 | |
342 void Traversal::AddCreatesAndMoves( | |
287 const std::set<int64>& ready_unsynced_set) { | 343 const std::set<int64>& ready_unsynced_set) { |
288 // Add moves and creates, and prepend their uncommitted parents. | 344 // Add moves and creates, and prepend their uncommitted parents. |
289 for (std::set<int64>::const_iterator iter = ready_unsynced_set.begin(); | 345 for (std::set<int64>::const_iterator iter = ready_unsynced_set.begin(); |
290 !IsCommitBatchFull() && iter != ready_unsynced_set.end(); ++iter) { | 346 !IsFull() && iter != ready_unsynced_set.end(); ++iter) { |
291 int64 metahandle = *iter; | 347 int64 metahandle = *iter; |
292 if (commit_set_->HaveCommitItem(metahandle)) | 348 if (HaveItem(metahandle)) |
293 continue; | 349 continue; |
294 | 350 |
295 syncable::Entry entry(trans, | 351 syncable::Entry entry(trans_, |
296 syncable::GET_BY_HANDLE, | 352 syncable::GET_BY_HANDLE, |
297 metahandle); | 353 metahandle); |
298 if (!entry.Get(syncable::IS_DEL)) { | 354 if (!entry.Get(syncable::IS_DEL)) { |
299 // We only commit an item + its dependencies if it and all its | 355 // We only commit an item + its dependencies if it and all its |
300 // dependencies are not in conflict. | 356 // dependencies are not in conflict. |
301 OrderedCommitSet item_dependencies(routes); | 357 std::vector<int64> item_dependencies; |
302 if (AddUncommittedParentsAndTheirPredecessors( | 358 if (AddUncommittedParentsAndTheirPredecessors( |
303 trans, | |
304 routes, | |
305 ready_unsynced_set, | 359 ready_unsynced_set, |
306 entry, | 360 entry, |
307 &item_dependencies)) { | 361 &item_dependencies)) { |
308 AddPredecessorsThenItem(trans, | 362 AddPredecessorsThenItem(ready_unsynced_set, |
309 routes, | |
310 ready_unsynced_set, | |
311 entry, | 363 entry, |
312 &item_dependencies); | 364 &item_dependencies); |
313 commit_set_->Append(item_dependencies); | 365 AppendManyToTraversal(item_dependencies); |
314 } | 366 } |
315 } | 367 } |
316 } | 368 } |
317 | 369 |
318 // It's possible that we overcommitted while trying to expand dependent | 370 // It's possible that we overcommitted while trying to expand dependent |
319 // items. If so, truncate the set down to the allowed size. | 371 // items. If so, truncate the set down to the allowed size. |
320 commit_set_->Truncate(requested_commit_batch_size_); | 372 if (out_->size() > max_entries_) { |
373 out_->resize(max_entries_); | |
374 } | |
321 } | 375 } |
322 | 376 |
323 void GetCommitIdsCommand::AddDeletes( | 377 void Traversal::AddDeletes( |
324 syncable::BaseTransaction* trans, | |
325 const std::set<int64>& ready_unsynced_set) { | 378 const std::set<int64>& ready_unsynced_set) { |
326 set<syncable::Id> legal_delete_parents; | 379 set<syncable::Id> legal_delete_parents; |
327 | 380 |
328 for (std::set<int64>::const_iterator iter = ready_unsynced_set.begin(); | 381 for (std::set<int64>::const_iterator iter = ready_unsynced_set.begin(); |
329 !IsCommitBatchFull() && iter != ready_unsynced_set.end(); ++iter) { | 382 !IsFull() && iter != ready_unsynced_set.end(); ++iter) { |
330 int64 metahandle = *iter; | 383 int64 metahandle = *iter; |
331 if (commit_set_->HaveCommitItem(metahandle)) | 384 if (HaveItem(metahandle)) |
332 continue; | 385 continue; |
333 | 386 |
334 syncable::Entry entry(trans, syncable::GET_BY_HANDLE, | 387 syncable::Entry entry(trans_, syncable::GET_BY_HANDLE, |
335 metahandle); | 388 metahandle); |
336 | 389 |
337 if (entry.Get(syncable::IS_DEL)) { | 390 if (entry.Get(syncable::IS_DEL)) { |
338 syncable::Entry parent(trans, syncable::GET_BY_ID, | 391 syncable::Entry parent(trans_, syncable::GET_BY_ID, |
339 entry.Get(syncable::PARENT_ID)); | 392 entry.Get(syncable::PARENT_ID)); |
340 // If the parent is deleted and unsynced, then any children of that | 393 // If the parent is deleted and unsynced, then any children of that |
341 // parent don't need to be added to the delete queue. | 394 // parent don't need to be added to the delete queue. |
342 // | 395 // |
343 // Note: the parent could be synced if there was an update deleting a | 396 // Note: the parent could be synced if there was an update deleting a |
344 // folder when we had a deleted all items in it. | 397 // folder when we had a deleted all items in it. |
345 // We may get more updates, or we may want to delete the entry. | 398 // We may get more updates, or we may want to delete the entry. |
346 if (parent.good() && | 399 if (parent.good() && |
347 parent.Get(syncable::IS_DEL) && | 400 parent.Get(syncable::IS_DEL) && |
348 parent.Get(syncable::IS_UNSYNCED)) { | 401 parent.Get(syncable::IS_UNSYNCED)) { |
349 // However, if an entry is moved, these rules can apply differently. | 402 // However, if an entry is moved, these rules can apply differently. |
350 // | 403 // |
351 // If the entry was moved, then the destination parent was deleted, | 404 // If the entry was moved, then the destination parent was deleted, |
352 // then we'll miss it in the roll up. We have to add it in manually. | 405 // then we'll miss it in the roll up. We have to add it in manually. |
353 // TODO(chron): Unit test for move / delete cases: | 406 // TODO(chron): Unit test for move / delete cases: |
354 // Case 1: Locally moved, then parent deleted | 407 // Case 1: Locally moved, then parent deleted |
355 // Case 2: Server moved, then locally issue recursive delete. | 408 // Case 2: Server moved, then locally issue recursive delete. |
356 if (entry.Get(syncable::ID).ServerKnows() && | 409 if (entry.Get(syncable::ID).ServerKnows() && |
357 entry.Get(syncable::PARENT_ID) != | 410 entry.Get(syncable::PARENT_ID) != |
358 entry.Get(syncable::SERVER_PARENT_ID)) { | 411 entry.Get(syncable::SERVER_PARENT_ID)) { |
359 DVLOG(1) << "Inserting moved and deleted entry, will be missed by " | 412 DVLOG(1) << "Inserting moved and deleted entry, will be missed by " |
360 << "delete roll." << entry.Get(syncable::ID); | 413 << "delete roll." << entry.Get(syncable::ID); |
361 | 414 |
362 commit_set_->AddCommitItem(metahandle, entry.GetModelType()); | 415 AppendToTraversal(metahandle); |
363 } | 416 } |
364 | 417 |
365 // Skip this entry since it's a child of a parent that will be | 418 // Skip this entry since it's a child of a parent that will be |
366 // deleted. The server will unroll the delete and delete the | 419 // deleted. The server will unroll the delete and delete the |
367 // child as well. | 420 // child as well. |
368 continue; | 421 continue; |
369 } | 422 } |
370 | 423 |
371 legal_delete_parents.insert(entry.Get(syncable::PARENT_ID)); | 424 legal_delete_parents.insert(entry.Get(syncable::PARENT_ID)); |
372 } | 425 } |
373 } | 426 } |
374 | 427 |
375 // We could store all the potential entries with a particular parent during | 428 // We could store all the potential entries with a particular parent during |
376 // the above scan, but instead we rescan here. This is less efficient, but | 429 // the above scan, but instead we rescan here. This is less efficient, but |
377 // we're dropping memory alloc/dealloc in favor of linear scans of recently | 430 // we're dropping memory alloc/dealloc in favor of linear scans of recently |
378 // examined entries. | 431 // examined entries. |
379 // | 432 // |
380 // Scan through the UnsyncedMetaHandles again. If we have a deleted | 433 // Scan through the UnsyncedMetaHandles again. If we have a deleted |
381 // entry, then check if the parent is in legal_delete_parents. | 434 // entry, then check if the parent is in legal_delete_parents. |
382 // | 435 // |
383 // Parent being in legal_delete_parents means for the child: | 436 // Parent being in legal_delete_parents means for the child: |
384 // a recursive delete is not currently happening (no recent deletes in same | 437 // a recursive delete is not currently happening (no recent deletes in same |
385 // folder) | 438 // folder) |
386 // parent did expect at least one old deleted child | 439 // parent did expect at least one old deleted child |
387 // parent was not deleted | 440 // parent was not deleted |
388 for (std::set<int64>::const_iterator iter = ready_unsynced_set.begin(); | 441 for (std::set<int64>::const_iterator iter = ready_unsynced_set.begin(); |
389 !IsCommitBatchFull() && iter != ready_unsynced_set.end(); ++iter) { | 442 !IsFull() && iter != ready_unsynced_set.end(); ++iter) { |
390 int64 metahandle = *iter; | 443 int64 metahandle = *iter; |
391 if (commit_set_->HaveCommitItem(metahandle)) | 444 if (HaveItem(metahandle)) |
392 continue; | 445 continue; |
393 syncable::Entry entry(trans, syncable::GET_BY_HANDLE, | 446 syncable::Entry entry(trans_, syncable::GET_BY_HANDLE, |
394 metahandle); | 447 metahandle); |
395 if (entry.Get(syncable::IS_DEL)) { | 448 if (entry.Get(syncable::IS_DEL)) { |
396 syncable::Id parent_id = entry.Get(syncable::PARENT_ID); | 449 syncable::Id parent_id = entry.Get(syncable::PARENT_ID); |
397 if (legal_delete_parents.count(parent_id)) { | 450 if (legal_delete_parents.count(parent_id)) { |
398 commit_set_->AddCommitItem(metahandle, entry.GetModelType()); | 451 AppendToTraversal(metahandle); |
399 } | 452 } |
400 } | 453 } |
401 } | 454 } |
402 } | 455 } |
403 | 456 |
404 void GetCommitIdsCommand::BuildCommitIds( | 457 } // namespace |
458 | |
459 void OrderCommitIds( | |
405 syncable::BaseTransaction* trans, | 460 syncable::BaseTransaction* trans, |
406 const ModelSafeRoutingInfo& routes, | 461 size_t max_entries, |
407 const std::set<int64>& ready_unsynced_set) { | 462 const std::set<int64>& ready_unsynced_set, |
463 std::vector<int64>* out) { | |
408 // Commits follow these rules: | 464 // Commits follow these rules: |
409 // 1. Moves or creates are preceded by needed folder creates, from | 465 // 1. Moves or creates are preceded by needed folder creates, from |
410 // root to leaf. For folders whose contents are ordered, moves | 466 // root to leaf. For folders whose contents are ordered, moves |
411 // and creates appear in order. | 467 // and creates appear in order. |
412 // 2. Moves/Creates before deletes. | 468 // 2. Moves/Creates before deletes. |
413 // 3. Deletes, collapsed. | 469 // 3. Deletes, collapsed. |
414 // We commit deleted moves under deleted items as moves when collapsing | 470 // We commit deleted moves under deleted items as moves when collapsing |
415 // delete trees. | 471 // delete trees. |
416 | 472 |
473 Traversal traversal(trans, max_entries, out); | |
474 | |
417 // Add moves and creates, and prepend their uncommitted parents. | 475 // Add moves and creates, and prepend their uncommitted parents. |
418 AddCreatesAndMoves(trans, routes, ready_unsynced_set); | 476 traversal.AddCreatesAndMoves(ready_unsynced_set); |
419 | 477 |
420 // Add all deletes. | 478 // Add all deletes. |
421 AddDeletes(trans, ready_unsynced_set); | 479 traversal.AddDeletes(ready_unsynced_set); |
480 } | |
481 | |
482 void GetCommitIds( | |
483 syncable::BaseTransaction* trans, | |
484 ModelTypeSet requested_types, | |
485 const size_t commit_batch_size, | |
486 sessions::OrderedCommitSet* ordered_commit_set) { | |
487 for (ModelTypeSet::Iterator it = requested_types.First(); | |
488 it.Good(); it.Inc()) { | |
489 size_t space_remaining = commit_batch_size - ordered_commit_set->Size(); | |
Nicolas Zea
2013/09/05 23:55:37
size_t is unsigned right? The <= 0 condition doesn
rlarocque
2013/09/06 18:58:26
Good point.
This should still work as written, si
| |
490 if (space_remaining <= 0) | |
491 break; | |
492 std::vector<int64> out; | |
493 GetCommitIdsForType( | |
494 trans, | |
495 it.Get(), | |
496 space_remaining, | |
497 &out); | |
498 ordered_commit_set->AddCommitItems(out, it.Get()); | |
499 } | |
422 } | 500 } |
423 | 501 |
424 } // namespace syncer | 502 } // namespace syncer |
OLD | NEW |