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