OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/syncer_util.h" | 5 #include "sync/engine/syncer_util.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 // static | 74 // static |
75 int SyncerUtil::GetUnsyncedEntries(syncable::BaseTransaction* trans, | 75 int SyncerUtil::GetUnsyncedEntries(syncable::BaseTransaction* trans, |
76 std::vector<int64> *handles) { | 76 std::vector<int64> *handles) { |
77 trans->directory()->GetUnsyncedMetaHandles(trans, handles); | 77 trans->directory()->GetUnsyncedMetaHandles(trans, handles); |
78 DVLOG_IF(1, !handles->empty()) << "Have " << handles->size() | 78 DVLOG_IF(1, !handles->empty()) << "Have " << handles->size() |
79 << " unsynced items."; | 79 << " unsynced items."; |
80 return handles->size(); | 80 return handles->size(); |
81 } | 81 } |
82 | 82 |
83 // static | 83 // static |
84 void SyncerUtil::ChangeEntryIDAndUpdateChildren( | |
85 syncable::WriteTransaction* trans, | |
86 syncable::MutableEntry* entry, | |
87 const syncable::Id& new_id, | |
88 syncable::Directory::ChildHandles* children) { | |
89 syncable::Id old_id = entry->Get(ID); | |
90 if (!entry->Put(ID, new_id)) { | |
91 Entry old_entry(trans, GET_BY_ID, new_id); | |
92 CHECK(old_entry.good()); | |
93 LOG(FATAL) << "Attempt to change ID to " << new_id | |
94 << " conflicts with existing entry.\n\n" | |
95 << *entry << "\n\n" << old_entry; | |
96 } | |
97 if (entry->Get(IS_DIR)) { | |
98 // Get all child entries of the old id. | |
99 trans->directory()->GetChildHandlesById(trans, old_id, children); | |
100 Directory::ChildHandles::iterator i = children->begin(); | |
101 while (i != children->end()) { | |
102 MutableEntry child_entry(trans, GET_BY_HANDLE, *i++); | |
103 CHECK(child_entry.good()); | |
104 // Use the unchecked setter here to avoid touching the child's NEXT_ID | |
105 // and PREV_ID fields (which Put(PARENT_ID) would normally do to | |
106 // maintain linked-list invariants). In this case, NEXT_ID and PREV_ID | |
107 // among the children will be valid after the loop, since we update all | |
108 // the children at once. | |
109 child_entry.PutParentIdPropertyOnly(new_id); | |
110 } | |
111 } | |
112 // Update Id references on the previous and next nodes in the sibling | |
113 // order. Do this by reinserting into the linked list; the first | |
114 // step in PutPredecessor is to Unlink from the existing order, which | |
115 // will overwrite the stale Id value from the adjacent nodes. | |
116 if (entry->Get(PREV_ID) == entry->Get(NEXT_ID) && | |
117 entry->Get(PREV_ID) == old_id) { | |
118 // We just need a shallow update to |entry|'s fields since it is already | |
119 // self looped. | |
120 entry->Put(NEXT_ID, new_id); | |
121 entry->Put(PREV_ID, new_id); | |
122 } else { | |
123 entry->PutPredecessor(entry->Get(PREV_ID)); | |
124 } | |
125 } | |
126 | |
127 // static | |
128 void SyncerUtil::ChangeEntryIDAndUpdateChildren( | |
129 syncable::WriteTransaction* trans, | |
130 syncable::MutableEntry* entry, | |
131 const syncable::Id& new_id) { | |
132 syncable::Directory::ChildHandles children; | |
133 ChangeEntryIDAndUpdateChildren(trans, entry, new_id, &children); | |
134 } | |
135 | |
136 // static | |
137 syncable::Id SyncerUtil::FindLocalIdToUpdate( | 84 syncable::Id SyncerUtil::FindLocalIdToUpdate( |
138 syncable::BaseTransaction* trans, | 85 syncable::BaseTransaction* trans, |
139 const SyncEntity& update) { | 86 const SyncEntity& update) { |
140 // Expected entry points of this function: | 87 // Expected entry points of this function: |
141 // SyncEntity has NOT been applied to SERVER fields. | 88 // SyncEntity has NOT been applied to SERVER fields. |
142 // SyncEntity has NOT been applied to LOCAL fields. | 89 // SyncEntity has NOT been applied to LOCAL fields. |
143 // DB has not yet been modified, no entries created for this update. | 90 // DB has not yet been modified, no entries created for this update. |
144 | 91 |
145 const std::string& client_id = trans->directory()->cache_guid(); | 92 const std::string& client_id = trans->directory()->cache_guid(); |
146 | 93 |
(...skipping 622 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
769 if (update.version() < target->Get(SERVER_VERSION)) { | 716 if (update.version() < target->Get(SERVER_VERSION)) { |
770 LOG(WARNING) << "Update older than current server version for " | 717 LOG(WARNING) << "Update older than current server version for " |
771 << *target << " Update:" | 718 << *target << " Update:" |
772 << SyncerProtoUtil::SyncEntityDebugString(update); | 719 << SyncerProtoUtil::SyncEntityDebugString(update); |
773 return VERIFY_SUCCESS; // Expected in new sync protocol. | 720 return VERIFY_SUCCESS; // Expected in new sync protocol. |
774 } | 721 } |
775 return VERIFY_UNDECIDED; | 722 return VERIFY_UNDECIDED; |
776 } | 723 } |
777 | 724 |
778 } // namespace browser_sync | 725 } // namespace browser_sync |
OLD | NEW |