| OLD | NEW |
| 1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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 |
| 11 #include "chrome/browser/sync/engine/syncer_session.h" | |
| 12 #include "chrome/browser/sync/engine/syncer_util.h" | 11 #include "chrome/browser/sync/engine/syncer_util.h" |
| 13 #include "chrome/browser/sync/syncable/syncable.h" | 12 #include "chrome/browser/sync/syncable/syncable.h" |
| 14 #include "chrome/browser/sync/util/sync_types.h" | 13 #include "chrome/browser/sync/util/sync_types.h" |
| 15 | 14 |
| 16 using std::set; | 15 using std::set; |
| 17 using std::vector; | 16 using std::vector; |
| 18 | 17 |
| 19 namespace browser_sync { | 18 namespace browser_sync { |
| 20 | 19 |
| 20 using sessions::SyncSession; |
| 21 using sessions::StatusController; |
| 22 |
| 21 GetCommitIdsCommand::GetCommitIdsCommand(int commit_batch_size) | 23 GetCommitIdsCommand::GetCommitIdsCommand(int commit_batch_size) |
| 22 : requested_commit_batch_size_(commit_batch_size) {} | 24 : requested_commit_batch_size_(commit_batch_size) {} |
| 23 | 25 |
| 24 GetCommitIdsCommand::~GetCommitIdsCommand() {} | 26 GetCommitIdsCommand::~GetCommitIdsCommand() {} |
| 25 | 27 |
| 26 void GetCommitIdsCommand::ExecuteImpl(SyncerSession* session) { | 28 void GetCommitIdsCommand::ExecuteImpl(SyncSession* session) { |
| 27 // Gather the full set of unsynced items and store it in the session. They | 29 // Gather the full set of unsynced items and store it in the session. They |
| 28 // are not in the correct order for commit. | 30 // are not in the correct order for commit. |
| 29 syncable::Directory::UnsyncedMetaHandles all_unsynced_handles; | 31 syncable::Directory::UnsyncedMetaHandles all_unsynced_handles; |
| 30 SyncerUtil::GetUnsyncedEntries(session->write_transaction(), | 32 SyncerUtil::GetUnsyncedEntries(session->write_transaction(), |
| 31 &all_unsynced_handles); | 33 &all_unsynced_handles); |
| 32 session->set_unsynced_handles(all_unsynced_handles); | 34 StatusController* status = session->status_controller(); |
| 35 status->set_unsynced_handles(all_unsynced_handles); |
| 33 | 36 |
| 34 BuildCommitIds(session); | 37 BuildCommitIds(status->unsynced_handles(), session->write_transaction()); |
| 35 | 38 |
| 36 const vector<syncable::Id>& verified_commit_ids = | 39 const vector<syncable::Id>& verified_commit_ids = |
| 37 ordered_commit_set_.GetCommitIds(); | 40 ordered_commit_set_.GetCommitIds(); |
| 38 | 41 |
| 39 for (size_t i = 0; i < verified_commit_ids.size(); i++) | 42 for (size_t i = 0; i < verified_commit_ids.size(); i++) |
| 40 LOG(INFO) << "Debug commit batch result:" << verified_commit_ids[i]; | 43 LOG(INFO) << "Debug commit batch result:" << verified_commit_ids[i]; |
| 41 | 44 |
| 42 session->set_commit_ids(verified_commit_ids); | 45 status->set_commit_ids(verified_commit_ids); |
| 43 } | 46 } |
| 44 | 47 |
| 45 void GetCommitIdsCommand::AddUncommittedParentsAndTheirPredecessors( | 48 void GetCommitIdsCommand::AddUncommittedParentsAndTheirPredecessors( |
| 46 syncable::BaseTransaction* trans, | 49 syncable::BaseTransaction* trans, |
| 47 syncable::Id parent_id) { | 50 syncable::Id parent_id) { |
| 48 using namespace syncable; | 51 using namespace syncable; |
| 49 OrderedCommitSet item_dependencies; | 52 OrderedCommitSet item_dependencies; |
| 50 | 53 |
| 51 // Climb the tree adding entries leaf -> root. | 54 // Climb the tree adding entries leaf -> root. |
| 52 while (!parent_id.ServerKnows()) { | 55 while (!parent_id.ServerKnows()) { |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 AddItemThenPredecessors(trans, item, inclusion_filter, &item_dependencies); | 113 AddItemThenPredecessors(trans, item, inclusion_filter, &item_dependencies); |
| 111 | 114 |
| 112 // Reverse what we added to get the correct order. | 115 // Reverse what we added to get the correct order. |
| 113 ordered_commit_set_.AppendReverse(item_dependencies); | 116 ordered_commit_set_.AppendReverse(item_dependencies); |
| 114 } | 117 } |
| 115 | 118 |
| 116 bool GetCommitIdsCommand::IsCommitBatchFull() { | 119 bool GetCommitIdsCommand::IsCommitBatchFull() { |
| 117 return ordered_commit_set_.Size() >= requested_commit_batch_size_; | 120 return ordered_commit_set_.Size() >= requested_commit_batch_size_; |
| 118 } | 121 } |
| 119 | 122 |
| 120 void GetCommitIdsCommand::AddCreatesAndMoves(SyncerSession* session) { | 123 void GetCommitIdsCommand::AddCreatesAndMoves( |
| 124 const vector<int64>& unsynced_handles, |
| 125 syncable::WriteTransaction* write_transaction) { |
| 121 // Add moves and creates, and prepend their uncommitted parents. | 126 // Add moves and creates, and prepend their uncommitted parents. |
| 122 for (CommitMetahandleIterator iterator(session, &ordered_commit_set_); | 127 for (CommitMetahandleIterator iterator(unsynced_handles, write_transaction, |
| 123 !IsCommitBatchFull() && iterator.Valid(); | 128 &ordered_commit_set_); |
| 124 iterator.Increment()) { | 129 !IsCommitBatchFull() && iterator.Valid(); |
| 130 iterator.Increment()) { |
| 125 int64 metahandle = iterator.Current(); | 131 int64 metahandle = iterator.Current(); |
| 126 | 132 |
| 127 syncable::Entry entry(session->write_transaction(), | 133 syncable::Entry entry(write_transaction, |
| 128 syncable::GET_BY_HANDLE, | 134 syncable::GET_BY_HANDLE, |
| 129 metahandle); | 135 metahandle); |
| 130 if (!entry.Get(syncable::IS_DEL)) { | 136 if (!entry.Get(syncable::IS_DEL)) { |
| 131 AddUncommittedParentsAndTheirPredecessors( | 137 AddUncommittedParentsAndTheirPredecessors(write_transaction, |
| 132 session->write_transaction(), entry.Get(syncable::PARENT_ID)); | 138 entry.Get(syncable::PARENT_ID)); |
| 133 AddPredecessorsThenItem(session->write_transaction(), &entry, | 139 AddPredecessorsThenItem(write_transaction, &entry, syncable::IS_UNSYNCED); |
| 134 syncable::IS_UNSYNCED); | |
| 135 } | 140 } |
| 136 } | 141 } |
| 137 | 142 |
| 138 // It's possible that we overcommitted while trying to expand dependent | 143 // It's possible that we overcommitted while trying to expand dependent |
| 139 // items. If so, truncate the set down to the allowed size. | 144 // items. If so, truncate the set down to the allowed size. |
| 140 ordered_commit_set_.Truncate(requested_commit_batch_size_); | 145 ordered_commit_set_.Truncate(requested_commit_batch_size_); |
| 141 } | 146 } |
| 142 | 147 |
| 143 void GetCommitIdsCommand::AddDeletes(SyncerSession* session) { | 148 void GetCommitIdsCommand::AddDeletes(const vector<int64>& unsynced_handles, |
| 149 syncable::WriteTransaction* write_transaction) { |
| 144 set<syncable::Id> legal_delete_parents; | 150 set<syncable::Id> legal_delete_parents; |
| 145 | 151 |
| 146 for (CommitMetahandleIterator iterator(session, &ordered_commit_set_); | 152 for (CommitMetahandleIterator iterator(unsynced_handles, write_transaction, |
| 147 !IsCommitBatchFull() && iterator.Valid(); | 153 &ordered_commit_set_); |
| 148 iterator.Increment()) { | 154 !IsCommitBatchFull() && iterator.Valid(); |
| 155 iterator.Increment()) { |
| 149 int64 metahandle = iterator.Current(); | 156 int64 metahandle = iterator.Current(); |
| 150 | 157 |
| 151 syncable::Entry entry(session->write_transaction(), | 158 syncable::Entry entry(write_transaction, syncable::GET_BY_HANDLE, |
| 152 syncable::GET_BY_HANDLE, | |
| 153 metahandle); | 159 metahandle); |
| 154 | 160 |
| 155 if (entry.Get(syncable::IS_DEL)) { | 161 if (entry.Get(syncable::IS_DEL)) { |
| 156 syncable::Entry parent(session->write_transaction(), | 162 syncable::Entry parent(write_transaction, syncable::GET_BY_ID, |
| 157 syncable::GET_BY_ID, | |
| 158 entry.Get(syncable::PARENT_ID)); | 163 entry.Get(syncable::PARENT_ID)); |
| 159 // If the parent is deleted and unsynced, then any children of that | 164 // If the parent is deleted and unsynced, then any children of that |
| 160 // parent don't need to be added to the delete queue. | 165 // parent don't need to be added to the delete queue. |
| 161 // | 166 // |
| 162 // Note: the parent could be synced if there was an update deleting a | 167 // Note: the parent could be synced if there was an update deleting a |
| 163 // folder when we had a deleted all items in it. | 168 // folder when we had a deleted all items in it. |
| 164 // We may get more updates, or we may want to delete the entry. | 169 // We may get more updates, or we may want to delete the entry. |
| 165 if (parent.good() && | 170 if (parent.good() && |
| 166 parent.Get(syncable::IS_DEL) && | 171 parent.Get(syncable::IS_DEL) && |
| 167 parent.Get(syncable::IS_UNSYNCED)) { | 172 parent.Get(syncable::IS_UNSYNCED)) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 // | 204 // |
| 200 // Scan through the UnsyncedMetaHandles again. If we have a deleted | 205 // Scan through the UnsyncedMetaHandles again. If we have a deleted |
| 201 // entry, then check if the parent is in legal_delete_parents. | 206 // entry, then check if the parent is in legal_delete_parents. |
| 202 // | 207 // |
| 203 // Parent being in legal_delete_parents means for the child: | 208 // Parent being in legal_delete_parents means for the child: |
| 204 // a recursive delete is not currently happening (no recent deletes in same | 209 // a recursive delete is not currently happening (no recent deletes in same |
| 205 // folder) | 210 // folder) |
| 206 // parent did expect at least one old deleted child | 211 // parent did expect at least one old deleted child |
| 207 // parent was not deleted | 212 // parent was not deleted |
| 208 | 213 |
| 209 for (CommitMetahandleIterator iterator(session, &ordered_commit_set_); | 214 for (CommitMetahandleIterator iterator(unsynced_handles, write_transaction, |
| 215 &ordered_commit_set_); |
| 210 !IsCommitBatchFull() && iterator.Valid(); | 216 !IsCommitBatchFull() && iterator.Valid(); |
| 211 iterator.Increment()) { | 217 iterator.Increment()) { |
| 212 int64 metahandle = iterator.Current(); | 218 int64 metahandle = iterator.Current(); |
| 213 syncable::MutableEntry entry(session->write_transaction(), | 219 syncable::MutableEntry entry(write_transaction, syncable::GET_BY_HANDLE, |
| 214 syncable::GET_BY_HANDLE, | |
| 215 metahandle); | 220 metahandle); |
| 216 if (entry.Get(syncable::IS_DEL)) { | 221 if (entry.Get(syncable::IS_DEL)) { |
| 217 syncable::Id parent_id = entry.Get(syncable::PARENT_ID); | 222 syncable::Id parent_id = entry.Get(syncable::PARENT_ID); |
| 218 if (legal_delete_parents.count(parent_id)) { | 223 if (legal_delete_parents.count(parent_id)) { |
| 219 ordered_commit_set_.AddCommitItem(metahandle, entry.Get(syncable::ID)); | 224 ordered_commit_set_.AddCommitItem(metahandle, entry.Get(syncable::ID)); |
| 220 } | 225 } |
| 221 } | 226 } |
| 222 } | 227 } |
| 223 } | 228 } |
| 224 | 229 |
| 225 void GetCommitIdsCommand::BuildCommitIds(SyncerSession* session) { | 230 void GetCommitIdsCommand::BuildCommitIds(const vector<int64>& unsynced_handles, |
| 231 syncable::WriteTransaction* write_transaction) { |
| 226 // Commits follow these rules: | 232 // Commits follow these rules: |
| 227 // 1. Moves or creates are preceded by needed folder creates, from | 233 // 1. Moves or creates are preceded by needed folder creates, from |
| 228 // root to leaf. For folders whose contents are ordered, moves | 234 // root to leaf. For folders whose contents are ordered, moves |
| 229 // and creates appear in order. | 235 // and creates appear in order. |
| 230 // 2. Moves/Creates before deletes. | 236 // 2. Moves/Creates before deletes. |
| 231 // 3. Deletes, collapsed. | 237 // 3. Deletes, collapsed. |
| 232 // We commit deleted moves under deleted items as moves when collapsing | 238 // We commit deleted moves under deleted items as moves when collapsing |
| 233 // delete trees. | 239 // delete trees. |
| 234 | 240 |
| 235 // Add moves and creates, and prepend their uncommitted parents. | 241 // Add moves and creates, and prepend their uncommitted parents. |
| 236 AddCreatesAndMoves(session); | 242 AddCreatesAndMoves(unsynced_handles, write_transaction); |
| 237 | 243 |
| 238 // Add all deletes. | 244 // Add all deletes. |
| 239 AddDeletes(session); | 245 AddDeletes(unsynced_handles, write_transaction); |
| 240 } | 246 } |
| 241 | 247 |
| 242 } // namespace browser_sync | 248 } // namespace browser_sync |
| OLD | NEW |