1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "sync/syncable/write_transaction.h"
#include "sync/syncable/directory.h"
#include "sync/syncable/directory_change_delegate.h"
#include "sync/syncable/transaction_observer.h"
#include "sync/syncable/write_transaction_info.h"
namespace syncer {
namespace syncable {
WriteTransaction::WriteTransaction(const tracked_objects::Location& location,
WriterTag writer, Directory* directory)
: BaseTransaction(location, "WriteTransaction", writer, directory) {
Lock();
}
void WriteTransaction::SaveOriginal(const EntryKernel* entry) {
if (!entry) {
return;
}
// Insert only if it's not already there.
const int64 handle = entry->ref(META_HANDLE);
EntryKernelMutationMap::iterator it = mutations_.lower_bound(handle);
if (it == mutations_.end() || it->first != handle) {
EntryKernelMutation mutation;
mutation.original = *entry;
ignore_result(mutations_.insert(it, std::make_pair(handle, mutation)));
}
}
ImmutableEntryKernelMutationMap WriteTransaction::RecordMutations() {
directory_->kernel_->transaction_mutex.AssertAcquired();
for (syncable::EntryKernelMutationMap::iterator it = mutations_.begin();
it != mutations_.end();) {
EntryKernel* kernel = directory()->GetEntryByHandle(it->first);
if (!kernel) {
NOTREACHED();
continue;
}
if (kernel->is_dirty()) {
it->second.mutated = *kernel;
++it;
} else {
DCHECK(!it->second.original.is_dirty());
// Not actually mutated, so erase from |mutations_|.
mutations_.erase(it++);
}
}
return ImmutableEntryKernelMutationMap(&mutations_);
}
void WriteTransaction::UnlockAndNotify(
const ImmutableEntryKernelMutationMap& mutations) {
// Work while transaction mutex is held.
ModelTypeSet models_with_changes;
bool has_mutations = !mutations.Get().empty();
if (has_mutations) {
models_with_changes = NotifyTransactionChangingAndEnding(mutations);
}
Unlock();
// Work after mutex is relased.
if (has_mutations) {
NotifyTransactionComplete(models_with_changes);
}
}
ModelTypeSet WriteTransaction::NotifyTransactionChangingAndEnding(
const ImmutableEntryKernelMutationMap& mutations) {
directory_->kernel_->transaction_mutex.AssertAcquired();
DCHECK(!mutations.Get().empty());
WriteTransactionInfo write_transaction_info(
directory_->kernel_->next_write_transaction_id,
from_here_, writer_, mutations);
++directory_->kernel_->next_write_transaction_id;
ImmutableWriteTransactionInfo immutable_write_transaction_info(
&write_transaction_info);
DirectoryChangeDelegate* const delegate = directory_->kernel_->delegate;
if (writer_ == syncable::SYNCAPI) {
delegate->HandleCalculateChangesChangeEventFromSyncApi(
immutable_write_transaction_info, this);
} else {
delegate->HandleCalculateChangesChangeEventFromSyncer(
immutable_write_transaction_info, this);
}
ModelTypeSet models_with_changes =
delegate->HandleTransactionEndingChangeEvent(
immutable_write_transaction_info, this);
directory_->kernel_->transaction_observer.Call(FROM_HERE,
&TransactionObserver::OnTransactionWrite,
immutable_write_transaction_info, models_with_changes);
return models_with_changes;
}
void WriteTransaction::NotifyTransactionComplete(
ModelTypeSet models_with_changes) {
directory_->kernel_->delegate->HandleTransactionCompleteChangeEvent(
models_with_changes);
}
WriteTransaction::~WriteTransaction() {
const ImmutableEntryKernelMutationMap& mutations = RecordMutations();
directory()->CheckInvariantsOnTransactionClose(this, mutations.Get());
// |CheckTreeInvariants| could have thrown an unrecoverable error.
if (unrecoverable_error_set_) {
HandleUnrecoverableErrorIfSet();
Unlock();
return;
}
UnlockAndNotify(mutations);
}
#define ENUM_CASE(x) case x: return #x; break
std::string WriterTagToString(WriterTag writer_tag) {
switch (writer_tag) {
ENUM_CASE(INVALID);
ENUM_CASE(SYNCER);
ENUM_CASE(AUTHWATCHER);
ENUM_CASE(UNITTEST);
ENUM_CASE(VACUUM_AFTER_SAVE);
ENUM_CASE(PURGE_ENTRIES);
ENUM_CASE(SYNCAPI);
};
NOTREACHED();
return "";
}
#undef ENUM_CASE
} // namespace syncable
} // namespace syncer
|