[SyncFS] Build indexes from FileTracker entries on disk.
[chromium-blink-merge.git] / sync / engine / model_type_entity.cc
blob5acf5db48138795c81a57bd050d58ae2dd13c58c
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
3 // found in the LICENSE file.
5 #include "sync/engine/model_type_entity.h"
6 #include "sync/syncable/syncable_util.h"
8 namespace syncer {
10 scoped_ptr<ModelTypeEntity> ModelTypeEntity::NewLocalItem(
11 const std::string& client_tag,
12 const sync_pb::EntitySpecifics& specifics,
13 base::Time now) {
14 return scoped_ptr<ModelTypeEntity>(new ModelTypeEntity(
18 kUncommittedVersion,
19 true,
20 std::string(), // Sync thread will assign the initial ID.
21 syncable::GenerateSyncableHash(GetModelTypeFromSpecifics(specifics),
22 client_tag),
23 client_tag, // As non-unique name.
24 specifics,
25 false,
26 now,
27 now));
30 scoped_ptr<ModelTypeEntity> ModelTypeEntity::FromServerUpdate(
31 const std::string& id,
32 const std::string& client_tag_hash,
33 const std::string& non_unique_name,
34 int64 version,
35 const sync_pb::EntitySpecifics& specifics,
36 bool deleted,
37 base::Time ctime,
38 base::Time mtime) {
39 return scoped_ptr<ModelTypeEntity>(new ModelTypeEntity(0,
42 version,
43 true,
44 id,
45 client_tag_hash,
46 non_unique_name,
47 specifics,
48 deleted,
49 ctime,
50 mtime));
53 ModelTypeEntity::ModelTypeEntity(int64 sequence_number,
54 int64 commit_requested_sequence_number,
55 int64 acked_sequence_number,
56 int64 base_version,
57 bool is_dirty,
58 const std::string& id,
59 const std::string& client_tag_hash,
60 const std::string& non_unique_name,
61 const sync_pb::EntitySpecifics& specifics,
62 bool deleted,
63 base::Time ctime,
64 base::Time mtime)
65 : sequence_number_(sequence_number),
66 commit_requested_sequence_number_(commit_requested_sequence_number),
67 acked_sequence_number_(acked_sequence_number),
68 base_version_(base_version),
69 is_dirty_(is_dirty),
70 id_(id),
71 client_tag_hash_(client_tag_hash),
72 non_unique_name_(non_unique_name),
73 specifics_(specifics),
74 deleted_(deleted),
75 ctime_(ctime),
76 mtime_(mtime) {
79 ModelTypeEntity::~ModelTypeEntity() {
82 bool ModelTypeEntity::IsWriteRequired() const {
83 return is_dirty_;
86 bool ModelTypeEntity::IsUnsynced() const {
87 return sequence_number_ > acked_sequence_number_;
90 bool ModelTypeEntity::RequiresCommitRequest() const {
91 return sequence_number_ > commit_requested_sequence_number_;
94 bool ModelTypeEntity::UpdateIsReflection(int64 update_version) const {
95 return base_version_ >= update_version;
98 bool ModelTypeEntity::UpdateIsInConflict(int64 update_version) const {
99 return IsUnsynced() && !UpdateIsReflection(update_version);
102 void ModelTypeEntity::ApplyUpdateFromServer(
103 int64 update_version,
104 bool deleted,
105 const sync_pb::EntitySpecifics& specifics,
106 base::Time mtime) {
107 // There was a conflict and the server just won it.
108 // This implicitly acks all outstanding commits because a received update
109 // will clobber any pending commits on the sync thread.
110 acked_sequence_number_ = sequence_number_;
111 commit_requested_sequence_number_ = sequence_number_;
113 base_version_ = update_version;
114 specifics_ = specifics;
115 mtime_ = mtime;
118 void ModelTypeEntity::MakeLocalChange(
119 const sync_pb::EntitySpecifics& specifics) {
120 sequence_number_++;
121 specifics_ = specifics;
124 void ModelTypeEntity::Delete() {
125 sequence_number_++;
126 specifics_.Clear();
127 deleted_ = true;
130 void ModelTypeEntity::InitializeCommitRequestData(
131 CommitRequestData* request) const {
132 request->id = id_;
133 request->client_tag_hash = client_tag_hash_;
134 request->sequence_number = sequence_number_;
135 request->base_version = base_version_;
136 request->ctime = ctime_;
137 request->mtime = mtime_;
138 request->non_unique_name = non_unique_name_;
139 request->deleted = deleted_;
140 request->specifics.CopyFrom(specifics_);
143 void ModelTypeEntity::SetCommitRequestInProgress() {
144 commit_requested_sequence_number_ = sequence_number_;
147 void ModelTypeEntity::ReceiveCommitResponse(const std::string& id,
148 int64 sequence_number,
149 int64 response_version) {
150 id_ = id; // The server can assign us a new ID in a commit response.
151 acked_sequence_number_ = sequence_number;
152 base_version_ = response_version;
155 void ModelTypeEntity::ClearTransientSyncState() {
156 // If we have any unacknowledged commit requests outstatnding, they've been
157 // dropped and we should forget about them.
158 commit_requested_sequence_number_ = acked_sequence_number_;
161 void ModelTypeEntity::ClearSyncState() {
162 base_version_ = kUncommittedVersion;
163 is_dirty_ = true;
164 sequence_number_ = 1;
165 commit_requested_sequence_number_ = 0;
166 acked_sequence_number_ = 0;
167 id_.clear();
170 } // namespace syncer