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