1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
|
// Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/sync/engine/get_commit_ids_command.h"
#include <set>
#include <utility>
#include <vector>
#include "chrome/browser/sync/engine/syncer_util.h"
#include "chrome/browser/sync/syncable/syncable.h"
#include "chrome/browser/sync/util/sync_types.h"
using std::set;
using std::vector;
namespace browser_sync {
using sessions::SyncSession;
using sessions::StatusController;
GetCommitIdsCommand::GetCommitIdsCommand(int commit_batch_size)
: requested_commit_batch_size_(commit_batch_size) {}
GetCommitIdsCommand::~GetCommitIdsCommand() {}
void GetCommitIdsCommand::ExecuteImpl(SyncSession* session) {
// Gather the full set of unsynced items and store it in the session. They
// are not in the correct order for commit.
syncable::Directory::UnsyncedMetaHandles all_unsynced_handles;
SyncerUtil::GetUnsyncedEntries(session->write_transaction(),
&all_unsynced_handles);
StatusController* status = session->status_controller();
status->set_unsynced_handles(all_unsynced_handles);
BuildCommitIds(status->unsynced_handles(), session->write_transaction());
const vector<syncable::Id>& verified_commit_ids =
ordered_commit_set_.GetCommitIds();
for (size_t i = 0; i < verified_commit_ids.size(); i++)
LOG(INFO) << "Debug commit batch result:" << verified_commit_ids[i];
status->set_commit_ids(verified_commit_ids);
}
void GetCommitIdsCommand::AddUncommittedParentsAndTheirPredecessors(
syncable::BaseTransaction* trans,
syncable::Id parent_id) {
using namespace syncable;
OrderedCommitSet item_dependencies;
// Climb the tree adding entries leaf -> root.
while (!parent_id.ServerKnows()) {
Entry parent(trans, GET_BY_ID, parent_id);
CHECK(parent.good()) << "Bad user-only parent in item path.";
int64 handle = parent.Get(META_HANDLE);
if (ordered_commit_set_.HaveCommitItem(handle) ||
item_dependencies.HaveCommitItem(handle)) {
break;
}
if (!AddItemThenPredecessors(trans, &parent, IS_UNSYNCED,
&item_dependencies)) {
break; // Parent was already present in the set.
}
parent_id = parent.Get(PARENT_ID);
}
// Reverse what we added to get the correct order.
ordered_commit_set_.AppendReverse(item_dependencies);
}
bool GetCommitIdsCommand::AddItem(syncable::Entry* item,
OrderedCommitSet* result) {
int64 item_handle = item->Get(syncable::META_HANDLE);
if (result->HaveCommitItem(item_handle) ||
ordered_commit_set_.HaveCommitItem(item_handle)) {
return false;
}
result->AddCommitItem(item_handle, item->Get(syncable::ID));
return true;
}
bool GetCommitIdsCommand::AddItemThenPredecessors(
syncable::BaseTransaction* trans,
syncable::Entry* item,
syncable::IndexedBitField inclusion_filter,
OrderedCommitSet* result) {
if (!AddItem(item, result))
return false;
if (item->Get(syncable::IS_DEL))
return true; // Deleted items have no predecessors.
syncable::Id prev_id = item->Get(syncable::PREV_ID);
while (!prev_id.IsRoot()) {
syncable::Entry prev(trans, syncable::GET_BY_ID, prev_id);
CHECK(prev.good()) << "Bad id when walking predecessors.";
if (!prev.Get(inclusion_filter))
break;
if (!AddItem(&prev, result))
break;
prev_id = prev.Get(syncable::PREV_ID);
}
return true;
}
void GetCommitIdsCommand::AddPredecessorsThenItem(
syncable::BaseTransaction* trans,
syncable::Entry* item,
syncable::IndexedBitField inclusion_filter) {
OrderedCommitSet item_dependencies;
AddItemThenPredecessors(trans, item, inclusion_filter, &item_dependencies);
// Reverse what we added to get the correct order.
ordered_commit_set_.AppendReverse(item_dependencies);
}
bool GetCommitIdsCommand::IsCommitBatchFull() {
return ordered_commit_set_.Size() >= requested_commit_batch_size_;
}
void GetCommitIdsCommand::AddCreatesAndMoves(
const vector<int64>& unsynced_handles,
syncable::WriteTransaction* write_transaction) {
// Add moves and creates, and prepend their uncommitted parents.
for (CommitMetahandleIterator iterator(unsynced_handles, write_transaction,
&ordered_commit_set_);
!IsCommitBatchFull() && iterator.Valid();
iterator.Increment()) {
int64 metahandle = iterator.Current();
syncable::Entry entry(write_transaction,
syncable::GET_BY_HANDLE,
metahandle);
if (!entry.Get(syncable::IS_DEL)) {
AddUncommittedParentsAndTheirPredecessors(write_transaction,
entry.Get(syncable::PARENT_ID));
AddPredecessorsThenItem(write_transaction, &entry, syncable::IS_UNSYNCED);
}
}
// It's possible that we overcommitted while trying to expand dependent
// items. If so, truncate the set down to the allowed size.
ordered_commit_set_.Truncate(requested_commit_batch_size_);
}
void GetCommitIdsCommand::AddDeletes(const vector<int64>& unsynced_handles,
syncable::WriteTransaction* write_transaction) {
set<syncable::Id> legal_delete_parents;
for (CommitMetahandleIterator iterator(unsynced_handles, write_transaction,
&ordered_commit_set_);
!IsCommitBatchFull() && iterator.Valid();
iterator.Increment()) {
int64 metahandle = iterator.Current();
syncable::Entry entry(write_transaction, syncable::GET_BY_HANDLE,
metahandle);
if (entry.Get(syncable::IS_DEL)) {
syncable::Entry parent(write_transaction, syncable::GET_BY_ID,
entry.Get(syncable::PARENT_ID));
// If the parent is deleted and unsynced, then any children of that
// parent don't need to be added to the delete queue.
//
// Note: the parent could be synced if there was an update deleting a
// folder when we had a deleted all items in it.
// We may get more updates, or we may want to delete the entry.
if (parent.good() &&
parent.Get(syncable::IS_DEL) &&
parent.Get(syncable::IS_UNSYNCED)) {
// However, if an entry is moved, these rules can apply differently.
//
// If the entry was moved, then the destination parent was deleted,
// then we'll miss it in the roll up. We have to add it in manually.
// TODO(chron): Unit test for move / delete cases:
// Case 1: Locally moved, then parent deleted
// Case 2: Server moved, then locally issue recursive delete.
if (entry.Get(syncable::ID).ServerKnows() &&
entry.Get(syncable::PARENT_ID) !=
entry.Get(syncable::SERVER_PARENT_ID)) {
LOG(INFO) << "Inserting moved and deleted entry, will be missed by"
" delete roll." << entry.Get(syncable::ID);
ordered_commit_set_.AddCommitItem(metahandle,
entry.Get(syncable::ID));
}
// Skip this entry since it's a child of a parent that will be
// deleted. The server will unroll the delete and delete the
// child as well.
continue;
}
legal_delete_parents.insert(entry.Get(syncable::PARENT_ID));
}
}
// We could store all the potential entries with a particular parent during
// the above scan, but instead we rescan here. This is less efficient, but
// we're dropping memory alloc/dealloc in favor of linear scans of recently
// examined entries.
//
// Scan through the UnsyncedMetaHandles again. If we have a deleted
// entry, then check if the parent is in legal_delete_parents.
//
// Parent being in legal_delete_parents means for the child:
// a recursive delete is not currently happening (no recent deletes in same
// folder)
// parent did expect at least one old deleted child
// parent was not deleted
for (CommitMetahandleIterator iterator(unsynced_handles, write_transaction,
&ordered_commit_set_);
!IsCommitBatchFull() && iterator.Valid();
iterator.Increment()) {
int64 metahandle = iterator.Current();
syncable::MutableEntry entry(write_transaction, syncable::GET_BY_HANDLE,
metahandle);
if (entry.Get(syncable::IS_DEL)) {
syncable::Id parent_id = entry.Get(syncable::PARENT_ID);
if (legal_delete_parents.count(parent_id)) {
ordered_commit_set_.AddCommitItem(metahandle, entry.Get(syncable::ID));
}
}
}
}
void GetCommitIdsCommand::BuildCommitIds(const vector<int64>& unsynced_handles,
syncable::WriteTransaction* write_transaction) {
// Commits follow these rules:
// 1. Moves or creates are preceded by needed folder creates, from
// root to leaf. For folders whose contents are ordered, moves
// and creates appear in order.
// 2. Moves/Creates before deletes.
// 3. Deletes, collapsed.
// We commit deleted moves under deleted items as moves when collapsing
// delete trees.
// Add moves and creates, and prepend their uncommitted parents.
AddCreatesAndMoves(unsynced_handles, write_transaction);
// Add all deletes.
AddDeletes(unsynced_handles, write_transaction);
}
} // namespace browser_sync
|