| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "sync/engine/update_applicator.h" | |
| 6 | |
| 7 #include <stdint.h> | |
| 8 | |
| 9 #include <vector> | |
| 10 | |
| 11 #include "base/logging.h" | |
| 12 #include "sync/engine/syncer_util.h" | |
| 13 #include "sync/syncable/entry.h" | |
| 14 #include "sync/syncable/mutable_entry.h" | |
| 15 #include "sync/syncable/syncable_id.h" | |
| 16 #include "sync/syncable/syncable_write_transaction.h" | |
| 17 | |
| 18 using std::vector; | |
| 19 | |
| 20 namespace syncer { | |
| 21 | |
| 22 using syncable::ID; | |
| 23 | |
| 24 UpdateApplicator::UpdateApplicator(Cryptographer* cryptographer) | |
| 25 : cryptographer_(cryptographer), | |
| 26 updates_applied_(0), | |
| 27 encryption_conflicts_(0), | |
| 28 hierarchy_conflicts_(0) { | |
| 29 } | |
| 30 | |
| 31 UpdateApplicator::~UpdateApplicator() { | |
| 32 } | |
| 33 | |
| 34 // Attempt to apply all updates, using multiple passes if necessary. | |
| 35 // | |
| 36 // Some updates must be applied in order. For example, children must be created | |
| 37 // after their parent folder is created. This function runs an O(n^2) algorithm | |
| 38 // that will keep trying until there is nothing left to apply, or it stops | |
| 39 // making progress, which would indicate that the hierarchy is invalid. | |
| 40 // | |
| 41 // The update applicator also has to deal with simple conflicts, which occur | |
| 42 // when an item is modified on both the server and the local model. We remember | |
| 43 // their IDs so they can be passed to the conflict resolver after all the other | |
| 44 // applications are complete. | |
| 45 // | |
| 46 // Finally, there are encryption conflicts, which can occur when we don't have | |
| 47 // access to all the Nigori keys. There's nothing we can do about them here. | |
| 48 void UpdateApplicator::AttemptApplications( | |
| 49 syncable::WriteTransaction* trans, | |
| 50 const std::vector<int64_t>& handles) { | |
| 51 std::vector<int64_t> to_apply = handles; | |
| 52 | |
| 53 DVLOG(1) << "UpdateApplicator running over " << to_apply.size() << " items."; | |
| 54 while (!to_apply.empty()) { | |
| 55 std::vector<int64_t> to_reapply; | |
| 56 | |
| 57 for (std::vector<int64_t>::iterator i = to_apply.begin(); | |
| 58 i != to_apply.end(); ++i) { | |
| 59 syncable::MutableEntry entry(trans, syncable::GET_BY_HANDLE, *i); | |
| 60 UpdateAttemptResponse result = AttemptToUpdateEntry( | |
| 61 trans, &entry, cryptographer_); | |
| 62 | |
| 63 switch (result) { | |
| 64 case SUCCESS: | |
| 65 updates_applied_++; | |
| 66 break; | |
| 67 case CONFLICT_SIMPLE: | |
| 68 simple_conflict_ids_.insert(entry.GetId()); | |
| 69 break; | |
| 70 case CONFLICT_ENCRYPTION: | |
| 71 encryption_conflicts_++; | |
| 72 break; | |
| 73 case CONFLICT_HIERARCHY: | |
| 74 // The decision to classify these as hierarchy conflcits is tentative. | |
| 75 // If we make any progress this round, we'll clear the hierarchy | |
| 76 // conflict count and attempt to reapply these updates. | |
| 77 to_reapply.push_back(*i); | |
| 78 break; | |
| 79 default: | |
| 80 NOTREACHED(); | |
| 81 break; | |
| 82 } | |
| 83 } | |
| 84 | |
| 85 if (to_reapply.size() == to_apply.size()) { | |
| 86 // We made no progress. Must be stubborn hierarchy conflicts. | |
| 87 hierarchy_conflicts_ = to_apply.size(); | |
| 88 break; | |
| 89 } | |
| 90 | |
| 91 // We made some progress, so prepare for what might be another iteration. | |
| 92 // If everything went well, to_reapply will be empty and we'll break out on | |
| 93 // the while condition. | |
| 94 to_apply.swap(to_reapply); | |
| 95 to_reapply.clear(); | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 } // namespace syncer | |
| OLD | NEW |