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/syncable/write_transaction.h" | 5 #include "sync/syncable/write_transaction.h" |
6 | 6 |
7 #include "sync/syncable/directory.h" | 7 #include "sync/syncable/directory.h" |
8 #include "sync/syncable/directory_change_delegate.h" | 8 #include "sync/syncable/directory_change_delegate.h" |
| 9 #include "sync/syncable/mutable_entry.h" |
9 #include "sync/syncable/transaction_observer.h" | 10 #include "sync/syncable/transaction_observer.h" |
10 #include "sync/syncable/write_transaction_info.h" | 11 #include "sync/syncable/write_transaction_info.h" |
11 | 12 |
12 namespace syncer { | 13 namespace syncer { |
13 namespace syncable { | 14 namespace syncable { |
14 | 15 |
| 16 const int64 kInvalidTransactionVersion = -1; |
| 17 |
15 WriteTransaction::WriteTransaction(const tracked_objects::Location& location, | 18 WriteTransaction::WriteTransaction(const tracked_objects::Location& location, |
16 WriterTag writer, Directory* directory) | 19 WriterTag writer, Directory* directory) |
17 : BaseTransaction(location, "WriteTransaction", writer, directory) { | 20 : BaseTransaction(location, "WriteTransaction", writer, directory), |
| 21 transaction_version_(NULL) { |
18 Lock(); | 22 Lock(); |
19 } | 23 } |
20 | 24 |
| 25 WriteTransaction::WriteTransaction(const tracked_objects::Location& location, |
| 26 Directory* directory, |
| 27 int64* transaction_version) |
| 28 : BaseTransaction(location, "WriteTransaction", SYNCAPI, directory), |
| 29 transaction_version_(transaction_version) { |
| 30 Lock(); |
| 31 if (transaction_version_) |
| 32 *transaction_version_ = kInvalidTransactionVersion; |
| 33 } |
| 34 |
21 void WriteTransaction::SaveOriginal(const EntryKernel* entry) { | 35 void WriteTransaction::SaveOriginal(const EntryKernel* entry) { |
22 if (!entry) { | 36 if (!entry) { |
23 return; | 37 return; |
24 } | 38 } |
25 // Insert only if it's not already there. | 39 // Insert only if it's not already there. |
26 const int64 handle = entry->ref(META_HANDLE); | 40 const int64 handle = entry->ref(META_HANDLE); |
27 EntryKernelMutationMap::iterator it = mutations_.lower_bound(handle); | 41 EntryKernelMutationMap::iterator it = mutations_.lower_bound(handle); |
28 if (it == mutations_.end() || it->first != handle) { | 42 if (it == mutations_.end() || it->first != handle) { |
29 EntryKernelMutation mutation; | 43 EntryKernelMutation mutation; |
30 mutation.original = *entry; | 44 mutation.original = *entry; |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 DCHECK(!mutations.Get().empty()); | 89 DCHECK(!mutations.Get().empty()); |
76 | 90 |
77 WriteTransactionInfo write_transaction_info( | 91 WriteTransactionInfo write_transaction_info( |
78 directory_->kernel_->next_write_transaction_id, | 92 directory_->kernel_->next_write_transaction_id, |
79 from_here_, writer_, mutations); | 93 from_here_, writer_, mutations); |
80 ++directory_->kernel_->next_write_transaction_id; | 94 ++directory_->kernel_->next_write_transaction_id; |
81 | 95 |
82 ImmutableWriteTransactionInfo immutable_write_transaction_info( | 96 ImmutableWriteTransactionInfo immutable_write_transaction_info( |
83 &write_transaction_info); | 97 &write_transaction_info); |
84 DirectoryChangeDelegate* const delegate = directory_->kernel_->delegate; | 98 DirectoryChangeDelegate* const delegate = directory_->kernel_->delegate; |
| 99 std::vector<int64> entry_changed; |
85 if (writer_ == syncable::SYNCAPI) { | 100 if (writer_ == syncable::SYNCAPI) { |
86 delegate->HandleCalculateChangesChangeEventFromSyncApi( | 101 delegate->HandleCalculateChangesChangeEventFromSyncApi( |
87 immutable_write_transaction_info, this); | 102 immutable_write_transaction_info, this, &entry_changed); |
88 } else { | 103 } else { |
89 delegate->HandleCalculateChangesChangeEventFromSyncer( | 104 delegate->HandleCalculateChangesChangeEventFromSyncer( |
90 immutable_write_transaction_info, this); | 105 immutable_write_transaction_info, this, &entry_changed); |
91 } | 106 } |
| 107 UpdateTransactionVersion(entry_changed); |
92 | 108 |
93 ModelTypeSet models_with_changes = | 109 ModelTypeSet models_with_changes = |
94 delegate->HandleTransactionEndingChangeEvent( | 110 delegate->HandleTransactionEndingChangeEvent( |
95 immutable_write_transaction_info, this); | 111 immutable_write_transaction_info, this); |
96 | 112 |
97 directory_->kernel_->transaction_observer.Call(FROM_HERE, | 113 directory_->kernel_->transaction_observer.Call(FROM_HERE, |
98 &TransactionObserver::OnTransactionWrite, | 114 &TransactionObserver::OnTransactionWrite, |
99 immutable_write_transaction_info, models_with_changes); | 115 immutable_write_transaction_info, models_with_changes); |
100 | 116 |
101 return models_with_changes; | 117 return models_with_changes; |
102 } | 118 } |
103 | 119 |
104 void WriteTransaction::NotifyTransactionComplete( | 120 void WriteTransaction::NotifyTransactionComplete( |
105 ModelTypeSet models_with_changes) { | 121 ModelTypeSet models_with_changes) { |
106 directory_->kernel_->delegate->HandleTransactionCompleteChangeEvent( | 122 directory_->kernel_->delegate->HandleTransactionCompleteChangeEvent( |
107 models_with_changes); | 123 models_with_changes); |
108 } | 124 } |
109 | 125 |
| 126 void WriteTransaction::UpdateTransactionVersion( |
| 127 const std::vector<int64>& entry_changed) { |
| 128 syncer::ModelTypeSet type_seen; |
| 129 for (uint32 i = 0; i < entry_changed.size(); ++i) { |
| 130 MutableEntry entry(this, GET_BY_HANDLE, entry_changed[i]); |
| 131 if (entry.good()) { |
| 132 ModelType type = GetModelTypeFromSpecifics(entry.Get(SPECIFICS)); |
| 133 if (type < FIRST_REAL_MODEL_TYPE) |
| 134 continue; |
| 135 if (!type_seen.Has(type)) { |
| 136 directory_->IncrementTransactionVersion(type); |
| 137 type_seen.Put(type); |
| 138 } |
| 139 entry.Put(TRANSACTION_VERSION, directory_->GetTransactionVersion(type)); |
| 140 } |
| 141 } |
| 142 |
| 143 if (!type_seen.Empty() && transaction_version_) { |
| 144 DCHECK_EQ(1u, type_seen.Size()); |
| 145 *transaction_version_ = directory_->GetTransactionVersion( |
| 146 type_seen.First().Get()); |
| 147 } |
| 148 } |
| 149 |
110 WriteTransaction::~WriteTransaction() { | 150 WriteTransaction::~WriteTransaction() { |
111 const ImmutableEntryKernelMutationMap& mutations = RecordMutations(); | 151 const ImmutableEntryKernelMutationMap& mutations = RecordMutations(); |
112 directory()->CheckInvariantsOnTransactionClose(this, mutations.Get()); | 152 directory()->CheckInvariantsOnTransactionClose(this, mutations.Get()); |
113 | 153 |
114 // |CheckTreeInvariants| could have thrown an unrecoverable error. | 154 // |CheckTreeInvariants| could have thrown an unrecoverable error. |
115 if (unrecoverable_error_set_) { | 155 if (unrecoverable_error_set_) { |
116 HandleUnrecoverableErrorIfSet(); | 156 HandleUnrecoverableErrorIfSet(); |
117 Unlock(); | 157 Unlock(); |
118 return; | 158 return; |
119 } | 159 } |
(...skipping 14 matching lines...) Expand all Loading... |
134 ENUM_CASE(SYNCAPI); | 174 ENUM_CASE(SYNCAPI); |
135 }; | 175 }; |
136 NOTREACHED(); | 176 NOTREACHED(); |
137 return ""; | 177 return ""; |
138 } | 178 } |
139 | 179 |
140 #undef ENUM_CASE | 180 #undef ENUM_CASE |
141 | 181 |
142 } // namespace syncable | 182 } // namespace syncable |
143 } // namespace syncer | 183 } // namespace syncer |
OLD | NEW |