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/build_commit_command.h" | 5 #include "sync/engine/build_commit_command.h" |
6 | 6 |
7 #include <limits> | 7 #include <limits> |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
11 | 11 |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "sync/engine/syncer_proto_util.h" | 13 #include "sync/engine/syncer_proto_util.h" |
14 #include "sync/protocol/bookmark_specifics.pb.h" | 14 #include "sync/protocol/bookmark_specifics.pb.h" |
15 #include "sync/protocol/sync.pb.h" | |
15 #include "sync/sessions/ordered_commit_set.h" | 16 #include "sync/sessions/ordered_commit_set.h" |
16 #include "sync/sessions/sync_session.h" | 17 #include "sync/sessions/sync_session.h" |
17 #include "sync/syncable/directory.h" | 18 #include "sync/syncable/directory.h" |
18 #include "sync/syncable/mutable_entry.h" | 19 #include "sync/syncable/mutable_entry.h" |
19 #include "sync/syncable/syncable_changes_version.h" | 20 #include "sync/syncable/syncable_changes_version.h" |
21 #include "sync/syncable/syncable_proto_util.h" | |
20 #include "sync/syncable/write_transaction.h" | 22 #include "sync/syncable/write_transaction.h" |
21 #include "sync/util/time.h" | 23 #include "sync/util/time.h" |
22 | 24 |
23 using std::set; | 25 using std::set; |
24 using std::string; | 26 using std::string; |
25 using std::vector; | 27 using std::vector; |
26 | 28 |
27 namespace syncer { | 29 namespace syncer { |
28 | 30 |
29 using sessions::SyncSession; | 31 using sessions::SyncSession; |
(...skipping 17 matching lines...) Expand all Loading... | |
47 return std::numeric_limits<int64>::max(); | 49 return std::numeric_limits<int64>::max(); |
48 } | 50 } |
49 | 51 |
50 // static | 52 // static |
51 int64 BuildCommitCommand::GetGap() { | 53 int64 BuildCommitCommand::GetGap() { |
52 return 1LL << 20; | 54 return 1LL << 20; |
53 } | 55 } |
54 | 56 |
55 BuildCommitCommand::BuildCommitCommand( | 57 BuildCommitCommand::BuildCommitCommand( |
56 const sessions::OrderedCommitSet& batch_commit_set, | 58 const sessions::OrderedCommitSet& batch_commit_set, |
57 ClientToServerMessage* commit_message) | 59 sync_pb::ClientToServerMessage* commit_message) |
58 : batch_commit_set_(batch_commit_set), commit_message_(commit_message) { | 60 : batch_commit_set_(batch_commit_set), commit_message_(commit_message) { |
59 } | 61 } |
60 | 62 |
61 BuildCommitCommand::~BuildCommitCommand() {} | 63 BuildCommitCommand::~BuildCommitCommand() {} |
62 | 64 |
63 void BuildCommitCommand::AddExtensionsActivityToMessage( | 65 void BuildCommitCommand::AddExtensionsActivityToMessage( |
64 SyncSession* session, CommitMessage* message) { | 66 SyncSession* session, sync_pb::CommitMessage* message) { |
65 // We only send ExtensionsActivity to the server if bookmarks are being | 67 // We only send ExtensionsActivity to the server if bookmarks are being |
66 // committed. | 68 // committed. |
67 ExtensionsActivityMonitor* monitor = session->context()->extensions_monitor(); | 69 ExtensionsActivityMonitor* monitor = session->context()->extensions_monitor(); |
68 if (batch_commit_set_.HasBookmarkCommitId()) { | 70 if (batch_commit_set_.HasBookmarkCommitId()) { |
69 // This isn't perfect, since the set of extensions activity may not | 71 // This isn't perfect, since the set of extensions activity may not |
70 // correlate exactly with the items being committed. That's OK as | 72 // correlate exactly with the items being committed. That's OK as |
71 // long as we're looking for a rough estimate of extensions activity, | 73 // long as we're looking for a rough estimate of extensions activity, |
72 // not an precise mapping of which commits were triggered by which | 74 // not an precise mapping of which commits were triggered by which |
73 // extension. | 75 // extension. |
74 // | 76 // |
(...skipping 10 matching lines...) Expand all Loading... | |
85 sync_pb::ChromiumExtensionsActivity* activity_message = | 87 sync_pb::ChromiumExtensionsActivity* activity_message = |
86 message->add_extensions_activity(); | 88 message->add_extensions_activity(); |
87 activity_message->set_extension_id(it->second.extension_id); | 89 activity_message->set_extension_id(it->second.extension_id); |
88 activity_message->set_bookmark_writes_since_last_commit( | 90 activity_message->set_bookmark_writes_since_last_commit( |
89 it->second.bookmark_write_count); | 91 it->second.bookmark_write_count); |
90 } | 92 } |
91 } | 93 } |
92 } | 94 } |
93 | 95 |
94 namespace { | 96 namespace { |
95 void SetEntrySpecifics(MutableEntry* meta_entry, SyncEntity* sync_entry) { | 97 void SetEntrySpecifics(MutableEntry* meta_entry, |
98 sync_pb::SyncEntity* sync_entry) { | |
96 // Add the new style extension and the folder bit. | 99 // Add the new style extension and the folder bit. |
97 sync_entry->mutable_specifics()->CopyFrom(meta_entry->Get(SPECIFICS)); | 100 sync_entry->mutable_specifics()->CopyFrom(meta_entry->Get(SPECIFICS)); |
98 sync_entry->set_folder(meta_entry->Get(syncable::IS_DIR)); | 101 sync_entry->set_folder(meta_entry->Get(syncable::IS_DIR)); |
99 | 102 |
100 DCHECK(meta_entry->GetModelType() == sync_entry->GetModelType()); | 103 DCHECK(meta_entry->GetModelType() == GetModelType(*sync_entry)); |
akalin
2012/07/11 01:42:22
DCHECK_EQ?
| |
101 } | 104 } |
102 } // namespace | 105 } // namespace |
103 | 106 |
104 SyncerError BuildCommitCommand::ExecuteImpl(SyncSession* session) { | 107 SyncerError BuildCommitCommand::ExecuteImpl(SyncSession* session) { |
105 commit_message_->set_share(session->context()->account_name()); | 108 commit_message_->set_share(session->context()->account_name()); |
106 commit_message_->set_message_contents(ClientToServerMessage::COMMIT); | 109 commit_message_->set_message_contents(sync_pb::ClientToServerMessage::COMMIT); |
107 | 110 |
108 CommitMessage* commit_message = commit_message_->mutable_commit(); | 111 sync_pb::CommitMessage* commit_message = commit_message_->mutable_commit(); |
109 commit_message->set_cache_guid( | 112 commit_message->set_cache_guid( |
110 session->write_transaction()->directory()->cache_guid()); | 113 session->write_transaction()->directory()->cache_guid()); |
111 AddExtensionsActivityToMessage(session, commit_message); | 114 AddExtensionsActivityToMessage(session, commit_message); |
112 SyncerProtoUtil::AddRequestBirthday( | 115 SyncerProtoUtil::AddRequestBirthday( |
113 session->write_transaction()->directory(), commit_message_); | 116 session->write_transaction()->directory(), commit_message_); |
114 | 117 |
115 // Cache previously computed position values. Because |commit_ids| | 118 // Cache previously computed position values. Because |commit_ids| |
116 // is already in sibling order, we should always hit this map after | 119 // is already in sibling order, we should always hit this map after |
117 // the first sibling in a consecutive run of commit items. The | 120 // the first sibling in a consecutive run of commit items. The |
118 // entries in this map are (low, high) values describing the | 121 // entries in this map are (low, high) values describing the |
119 // space of positions that are immediate successors of the item | 122 // space of positions that are immediate successors of the item |
120 // whose ID is the map's key. | 123 // whose ID is the map's key. |
121 std::map<Id, std::pair<int64, int64> > position_map; | 124 std::map<Id, std::pair<int64, int64> > position_map; |
122 | 125 |
123 for (size_t i = 0; i < batch_commit_set_.Size(); i++) { | 126 for (size_t i = 0; i < batch_commit_set_.Size(); i++) { |
124 Id id = batch_commit_set_.GetCommitIdAt(i); | 127 Id id = batch_commit_set_.GetCommitIdAt(i); |
125 SyncEntity* sync_entry = | 128 sync_pb::SyncEntity* sync_entry = |
126 static_cast<SyncEntity*>(commit_message->add_entries()); | 129 static_cast<sync_pb::SyncEntity*>(commit_message->add_entries()); |
akalin
2012/07/11 01:42:22
no need for this static cast anymore, right? may
rlarocque
2012/07/11 19:22:16
Good point. Will fix.
| |
127 sync_entry->set_id(id); | 130 sync_entry->set_id_string(SyncableIdToProto(id)); |
128 MutableEntry meta_entry(session->write_transaction(), | 131 MutableEntry meta_entry(session->write_transaction(), |
129 syncable::GET_BY_ID, id); | 132 syncable::GET_BY_ID, id); |
130 CHECK(meta_entry.good()); | 133 CHECK(meta_entry.good()); |
131 | 134 |
132 DCHECK(0 != session->routing_info().count(meta_entry.GetModelType())) | 135 DCHECK(0 != session->routing_info().count(meta_entry.GetModelType())) |
133 << "Committing change to datatype that's not actively enabled."; | 136 << "Committing change to datatype that's not actively enabled."; |
134 | 137 |
135 string name = meta_entry.Get(syncable::NON_UNIQUE_NAME); | 138 string name = meta_entry.Get(syncable::NON_UNIQUE_NAME); |
136 CHECK(!name.empty()); // Make sure this isn't an update. | 139 CHECK(!name.empty()); // Make sure this isn't an update. |
137 TruncateUTF8ToByteSize(name, 255, &name); | 140 TruncateUTF8ToByteSize(name, 255, &name); |
(...skipping 12 matching lines...) Expand all Loading... | |
150 | 153 |
151 // Deleted items with server-unknown parent ids can be a problem so we set | 154 // Deleted items with server-unknown parent ids can be a problem so we set |
152 // the parent to 0. (TODO(sync): Still true in protocol?). | 155 // the parent to 0. (TODO(sync): Still true in protocol?). |
153 Id new_parent_id; | 156 Id new_parent_id; |
154 if (meta_entry.Get(syncable::IS_DEL) && | 157 if (meta_entry.Get(syncable::IS_DEL) && |
155 !meta_entry.Get(syncable::PARENT_ID).ServerKnows()) { | 158 !meta_entry.Get(syncable::PARENT_ID).ServerKnows()) { |
156 new_parent_id = session->write_transaction()->root_id(); | 159 new_parent_id = session->write_transaction()->root_id(); |
157 } else { | 160 } else { |
158 new_parent_id = meta_entry.Get(syncable::PARENT_ID); | 161 new_parent_id = meta_entry.Get(syncable::PARENT_ID); |
159 } | 162 } |
160 sync_entry->set_parent_id(new_parent_id); | 163 sync_entry->set_parent_id_string( |
164 SyncableIdToProto(new_parent_id)); | |
161 | 165 |
162 // If our parent has changed, send up the old one so the server | 166 // If our parent has changed, send up the old one so the server |
163 // can correctly deal with multiple parents. | 167 // can correctly deal with multiple parents. |
164 // TODO(nick): With the server keeping track of the primary sync parent, | 168 // TODO(nick): With the server keeping track of the primary sync parent, |
165 // it should not be necessary to provide the old_parent_id: the version | 169 // it should not be necessary to provide the old_parent_id: the version |
166 // number should suffice. | 170 // number should suffice. |
167 if (new_parent_id != meta_entry.Get(syncable::SERVER_PARENT_ID) && | 171 if (new_parent_id != meta_entry.Get(syncable::SERVER_PARENT_ID) && |
168 0 != meta_entry.Get(syncable::BASE_VERSION) && | 172 0 != meta_entry.Get(syncable::BASE_VERSION) && |
169 syncable::CHANGES_VERSION != meta_entry.Get(syncable::BASE_VERSION)) { | 173 syncable::CHANGES_VERSION != meta_entry.Get(syncable::BASE_VERSION)) { |
170 sync_entry->set_old_parent_id(meta_entry.Get(syncable::SERVER_PARENT_ID)); | 174 sync_entry->set_old_parent_id( |
175 SyncableIdToProto(meta_entry.Get(syncable::SERVER_PARENT_ID))); | |
171 } | 176 } |
172 | 177 |
173 int64 version = meta_entry.Get(syncable::BASE_VERSION); | 178 int64 version = meta_entry.Get(syncable::BASE_VERSION); |
174 if (syncable::CHANGES_VERSION == version || 0 == version) { | 179 if (syncable::CHANGES_VERSION == version || 0 == version) { |
175 // Undeletions are only supported for items that have a client tag. | 180 // Undeletions are only supported for items that have a client tag. |
176 DCHECK(!id.ServerKnows() || | 181 DCHECK(!id.ServerKnows() || |
177 !meta_entry.Get(syncable::UNIQUE_CLIENT_TAG).empty()) | 182 !meta_entry.Get(syncable::UNIQUE_CLIENT_TAG).empty()) |
178 << meta_entry; | 183 << meta_entry; |
179 | 184 |
180 // Version 0 means to create or undelete an object. | 185 // Version 0 means to create or undelete an object. |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
255 if (delta <= static_cast<uint64>(GetGap()*2)) | 260 if (delta <= static_cast<uint64>(GetGap()*2)) |
256 return lo + (static_cast<int64>(delta) + 7) / 8; // Interpolate. | 261 return lo + (static_cast<int64>(delta) + 7) / 8; // Interpolate. |
257 else if (lo == GetFirstPosition()) | 262 else if (lo == GetFirstPosition()) |
258 return hi - GetGap(); // Extend range just before successor. | 263 return hi - GetGap(); // Extend range just before successor. |
259 else | 264 else |
260 return lo + GetGap(); // Use or extend range just after predecessor. | 265 return lo + GetGap(); // Use or extend range just after predecessor. |
261 } | 266 } |
262 | 267 |
263 | 268 |
264 } // namespace syncer | 269 } // namespace syncer |
OLD | NEW |