OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/entity_tracker.h" | 5 #include "sync/engine/entity_tracker.h" |
6 | 6 |
| 7 #include <stdint.h> |
| 8 |
7 #include "base/logging.h" | 9 #include "base/logging.h" |
8 #include "sync/internal_api/public/base/model_type.h" | 10 #include "sync/internal_api/public/base/model_type.h" |
9 #include "sync/internal_api/public/non_blocking_sync_common.h" | 11 #include "sync/internal_api/public/non_blocking_sync_common.h" |
10 #include "sync/syncable/syncable_util.h" | 12 #include "sync/syncable/syncable_util.h" |
11 #include "sync/util/time.h" | 13 #include "sync/util/time.h" |
12 | 14 |
13 namespace syncer_v2 { | 15 namespace syncer_v2 { |
14 | 16 |
15 scoped_ptr<EntityTracker> EntityTracker::FromUpdateResponse( | 17 scoped_ptr<EntityTracker> EntityTracker::FromUpdateResponse( |
16 const UpdateResponseData& data) { | 18 const UpdateResponseData& data) { |
17 return make_scoped_ptr(new EntityTracker( | 19 return make_scoped_ptr(new EntityTracker( |
18 data.entity->id, data.entity->client_tag_hash, 0, data.response_version)); | 20 data.entity->id, data.entity->client_tag_hash, 0, data.response_version)); |
19 } | 21 } |
20 | 22 |
21 scoped_ptr<EntityTracker> EntityTracker::FromCommitRequest( | 23 scoped_ptr<EntityTracker> EntityTracker::FromCommitRequest( |
22 const CommitRequestData& data) { | 24 const CommitRequestData& data) { |
23 return make_scoped_ptr( | 25 return make_scoped_ptr( |
24 new EntityTracker(data.entity->id, data.entity->client_tag_hash, 0, 0)); | 26 new EntityTracker(data.entity->id, data.entity->client_tag_hash, 0, 0)); |
25 } | 27 } |
26 | 28 |
27 EntityTracker::EntityTracker(const std::string& id, | 29 EntityTracker::EntityTracker(const std::string& id, |
28 const std::string& client_tag_hash, | 30 const std::string& client_tag_hash, |
29 int64 highest_commit_response_version, | 31 int64_t highest_commit_response_version, |
30 int64 highest_gu_response_version) | 32 int64_t highest_gu_response_version) |
31 : id_(id), | 33 : id_(id), |
32 client_tag_hash_(client_tag_hash), | 34 client_tag_hash_(client_tag_hash), |
33 highest_commit_response_version_(highest_commit_response_version), | 35 highest_commit_response_version_(highest_commit_response_version), |
34 highest_gu_response_version_(highest_gu_response_version), | 36 highest_gu_response_version_(highest_gu_response_version), |
35 sequence_number_(0), | 37 sequence_number_(0), |
36 base_version_(kUncommittedVersion) {} | 38 base_version_(kUncommittedVersion) {} |
37 | 39 |
38 EntityTracker::~EntityTracker() {} | 40 EntityTracker::~EntityTracker() {} |
39 | 41 |
40 bool EntityTracker::HasPendingCommit() const { | 42 bool EntityTracker::HasPendingCommit() const { |
41 return !!pending_commit_; | 43 return !!pending_commit_; |
42 } | 44 } |
43 | 45 |
44 void EntityTracker::PrepareCommitProto(sync_pb::SyncEntity* commit_entity, | 46 void EntityTracker::PrepareCommitProto(sync_pb::SyncEntity* commit_entity, |
45 int64* sequence_number) const { | 47 int64_t* sequence_number) const { |
46 DCHECK(HasPendingCommit()); | 48 DCHECK(HasPendingCommit()); |
47 DCHECK(!client_tag_hash_.empty()); | 49 DCHECK(!client_tag_hash_.empty()); |
48 | 50 |
49 if (!id_.empty()) { | 51 if (!id_.empty()) { |
50 commit_entity->set_id_string(id_); | 52 commit_entity->set_id_string(id_); |
51 } | 53 } |
52 | 54 |
53 const EntityData& entity = pending_commit_->entity.value(); | 55 const EntityData& entity = pending_commit_->entity.value(); |
54 DCHECK_EQ(client_tag_hash_, entity.client_tag_hash); | 56 DCHECK_EQ(client_tag_hash_, entity.client_tag_hash); |
55 | 57 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
108 if (IsInConflict()) { | 110 if (IsInConflict()) { |
109 ClearPendingCommit(); | 111 ClearPendingCommit(); |
110 return; | 112 return; |
111 } | 113 } |
112 | 114 |
113 // Otherwise, keep the data associated with this pending commit | 115 // Otherwise, keep the data associated with this pending commit |
114 // so it can be committed at the next possible opportunity. | 116 // so it can be committed at the next possible opportunity. |
115 } | 117 } |
116 | 118 |
117 void EntityTracker::ReceiveCommitResponse(const std::string& response_id, | 119 void EntityTracker::ReceiveCommitResponse(const std::string& response_id, |
118 int64 response_version, | 120 int64_t response_version, |
119 int64 sequence_number) { | 121 int64_t sequence_number) { |
120 // Commit responses, especially after the first commit, can update our ID. | 122 // Commit responses, especially after the first commit, can update our ID. |
121 id_ = response_id; | 123 id_ = response_id; |
122 | 124 |
123 DCHECK_GT(response_version, highest_commit_response_version_) | 125 DCHECK_GT(response_version, highest_commit_response_version_) |
124 << "Had expected higher response version." | 126 << "Had expected higher response version." |
125 << " id: " << id_; | 127 << " id: " << id_; |
126 | 128 |
127 // Commits are synchronous, so there's no reason why the sequence numbers | 129 // Commits are synchronous, so there's no reason why the sequence numbers |
128 // wouldn't match. | 130 // wouldn't match. |
129 DCHECK_EQ(sequence_number_, sequence_number) | 131 DCHECK_EQ(sequence_number_, sequence_number) |
130 << "Unexpected sequence number mismatch." | 132 << "Unexpected sequence number mismatch." |
131 << " id: " << id_; | 133 << " id: " << id_; |
132 | 134 |
133 highest_commit_response_version_ = response_version; | 135 highest_commit_response_version_ = response_version; |
134 | 136 |
135 // Because an in-progress commit blocks the sync thread, we can assume that | 137 // Because an in-progress commit blocks the sync thread, we can assume that |
136 // the item we just committed successfully is exactly the one we have now. | 138 // the item we just committed successfully is exactly the one we have now. |
137 // Nothing changed it while the commit was happening. Since we're now in | 139 // Nothing changed it while the commit was happening. Since we're now in |
138 // sync with the server, we can clear the pending commit. | 140 // sync with the server, we can clear the pending commit. |
139 ClearPendingCommit(); | 141 ClearPendingCommit(); |
140 } | 142 } |
141 | 143 |
142 void EntityTracker::ReceiveUpdate(int64 version) { | 144 void EntityTracker::ReceiveUpdate(int64_t version) { |
143 if (version <= highest_gu_response_version_) | 145 if (version <= highest_gu_response_version_) |
144 return; | 146 return; |
145 | 147 |
146 highest_gu_response_version_ = version; | 148 highest_gu_response_version_ = version; |
147 | 149 |
148 // Got an applicable update newer than any pending updates. It must be safe | 150 // Got an applicable update newer than any pending updates. It must be safe |
149 // to discard the old pending update, if there was one. | 151 // to discard the old pending update, if there was one. |
150 ClearPendingUpdate(); | 152 ClearPendingUpdate(); |
151 | 153 |
152 if (IsInConflict()) { | 154 if (IsInConflict()) { |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
203 | 205 |
204 bool EntityTracker::IsServerKnown() const { | 206 bool EntityTracker::IsServerKnown() const { |
205 return base_version_ != kUncommittedVersion; | 207 return base_version_ != kUncommittedVersion; |
206 } | 208 } |
207 | 209 |
208 void EntityTracker::ClearPendingCommit() { | 210 void EntityTracker::ClearPendingCommit() { |
209 pending_commit_.reset(); | 211 pending_commit_.reset(); |
210 } | 212 } |
211 | 213 |
212 } // namespace syncer_v2 | 214 } // namespace syncer_v2 |
OLD | NEW |