Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(140)

Side by Side Diff: sync/syncable/parent_child_index.cc

Issue 2168273002: [Sync] Fix behavior when there are two type roots for a type (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix nit Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « sync/syncable/parent_child_index.h ('k') | sync/syncable/parent_child_index_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/parent_child_index.h" 5 #include "sync/syncable/parent_child_index.h"
6 6
7 #include <memory>
8
7 #include "base/stl_util.h" 9 #include "base/stl_util.h"
8
9 #include "sync/syncable/entry_kernel.h" 10 #include "sync/syncable/entry_kernel.h"
10 #include "sync/syncable/syncable_id.h" 11 #include "sync/syncable/syncable_id.h"
11 12
12 namespace syncer { 13 namespace syncer {
13 namespace syncable { 14 namespace syncable {
14 15
15 bool ChildComparator::operator()(const EntryKernel* a, 16 bool ChildComparator::operator()(const EntryKernel* a,
16 const EntryKernel* b) const { 17 const EntryKernel* b) const {
17 const UniquePosition& a_pos = a->ref(UNIQUE_POSITION); 18 const UniquePosition& a_pos = a->ref(UNIQUE_POSITION);
18 const UniquePosition& b_pos = b->ref(UNIQUE_POSITION); 19 const UniquePosition& b_pos = b->ref(UNIQUE_POSITION);
(...skipping 19 matching lines...) Expand all
38 return a->ref(META_HANDLE) < b->ref(META_HANDLE); 39 return a->ref(META_HANDLE) < b->ref(META_HANDLE);
39 } 40 }
40 } 41 }
41 42
42 ParentChildIndex::ParentChildIndex() { 43 ParentChildIndex::ParentChildIndex() {
43 // Pre-allocate these two vectors to the number of model types. 44 // Pre-allocate these two vectors to the number of model types.
44 model_type_root_ids_.resize(MODEL_TYPE_COUNT); 45 model_type_root_ids_.resize(MODEL_TYPE_COUNT);
45 type_root_child_sets_.resize(MODEL_TYPE_COUNT); 46 type_root_child_sets_.resize(MODEL_TYPE_COUNT);
46 } 47 }
47 48
48 ParentChildIndex::~ParentChildIndex() { 49 ParentChildIndex::~ParentChildIndex() {}
49 for (int i = 0; i < MODEL_TYPE_COUNT; i++) {
50 // Normally all OrderedChildSet instances in |type_root_child_sets_|
51 // are shared with |parent_children_map_|. Null out shared instances and
52 // ScopedVector will take care of the ones that are not shared (if any).
53 if (!model_type_root_ids_[i].IsNull()) {
54 DCHECK_EQ(type_root_child_sets_[i],
55 parent_children_map_.find(model_type_root_ids_[i])->second);
56 type_root_child_sets_[i] = nullptr;
57 }
58 }
59
60 STLDeleteContainerPairSecondPointers(parent_children_map_.begin(),
61 parent_children_map_.end());
62 }
63 50
64 bool ParentChildIndex::ShouldInclude(const EntryKernel* entry) { 51 bool ParentChildIndex::ShouldInclude(const EntryKernel* entry) {
65 // This index excludes deleted items and the root item. The root 52 // This index excludes deleted items and the root item. The root
66 // item is excluded so that it doesn't show up as a child of itself. 53 // item is excluded so that it doesn't show up as a child of itself.
67 return !entry->ref(IS_DEL) && !entry->ref(ID).IsRoot(); 54 return !entry->ref(IS_DEL) && !entry->ref(ID).IsRoot();
68 } 55 }
69 56
70 bool ParentChildIndex::Insert(EntryKernel* entry) { 57 bool ParentChildIndex::Insert(EntryKernel* entry) {
71 DCHECK(ShouldInclude(entry)); 58 DCHECK(ShouldInclude(entry));
72 59
73 OrderedChildSet* siblings = nullptr; 60 OrderedChildSetRef siblings = nullptr;
74 const Id& parent_id = entry->ref(PARENT_ID); 61 const Id& parent_id = entry->ref(PARENT_ID);
75 ModelType model_type = entry->GetModelType(); 62 ModelType model_type = entry->GetModelType();
76 63
77 if (ShouldUseParentId(parent_id, model_type)) { 64 if (ShouldUseParentId(parent_id, model_type)) {
78 // Hierarchical type, lookup child set in the map. 65 // Hierarchical type, lookup child set in the map.
79 DCHECK(!parent_id.IsNull()); 66 DCHECK(!parent_id.IsNull());
80 ParentChildrenMap::iterator it = parent_children_map_.find(parent_id); 67 ParentChildrenMap::iterator it = parent_children_map_.find(parent_id);
81 if (it != parent_children_map_.end()) { 68 if (it != parent_children_map_.end()) {
82 siblings = it->second; 69 siblings = it->second;
83 } else { 70 } else {
84 siblings = new OrderedChildSet(); 71 siblings = OrderedChildSetRef(new OrderedChildSet());
85 parent_children_map_.insert(std::make_pair(parent_id, siblings)); 72 parent_children_map_.insert(std::make_pair(parent_id, siblings));
86 } 73 }
87 } else { 74 } else {
88 // Non-hierarchical type, return a pre-defined collection by type. 75 // Non-hierarchical type, return a pre-defined collection by type.
89 siblings = GetOrCreateModelTypeChildSet(model_type); 76 siblings = GetOrCreateModelTypeChildSet(model_type);
90 } 77 }
91 78
92 // If this is one of type root folder for a non-hierarchical type, associate 79 // If this is one of type root folder for a non-hierarchical type, associate
93 // its ID with the model type and the type's pre-defined child set with the 80 // its ID with the model type and the type's pre-defined child set with the
94 // type root ID. 81 // type root ID.
95 // TODO(stanisc): crbug/438313: Just TypeSupportsHierarchy condition should 82 // TODO(stanisc): crbug/438313: Just TypeSupportsHierarchy condition should
96 // theoretically be sufficient but in practice many tests don't properly 83 // theoretically be sufficient but in practice many tests don't properly
97 // initialize entries so TypeSupportsHierarchy ends up failing. Consider 84 // initialize entries so TypeSupportsHierarchy ends up failing. Consider
98 // tweaking TypeSupportsHierarchy and fixing all related test code. 85 // tweaking TypeSupportsHierarchy and fixing all related test code.
99 if (parent_id.IsRoot() && entry->ref(IS_DIR) && 86 if (parent_id.IsRoot() && entry->ref(IS_DIR) &&
100 syncer::IsRealDataType(model_type) && 87 syncer::IsRealDataType(model_type) &&
101 !TypeSupportsHierarchy(model_type)) { 88 !TypeSupportsHierarchy(model_type)) {
102 const Id& type_root_id = entry->ref(ID); 89 const Id& type_root_id = entry->ref(ID);
103 // Disassociate the type root child set with the previous ID if any. 90
104 const Id& prev_type_root_id = model_type_root_ids_[model_type]; 91 // If the entry exists in the map it must already have the same
105 if (!prev_type_root_id.IsNull()) { 92 // model type specific child set. It's possible another type root exists
106 ParentChildrenMap::iterator it = 93 // in parent_children_map_, but that's okay as the new type root will
107 parent_children_map_.find(prev_type_root_id); 94 // point to the same OrderedChildSet. As such, we just blindly store the
108 if (it != parent_children_map_.end()) { 95 // new type root ID and associate it to the (possibly existing) child set.
109 // If the entry exists in the map it must already have the same
110 // model type specific child set. This child set will be re-inserted
111 // in the map under the new ID below so it is safe to simply erase
112 // the entry here.
113 DCHECK_EQ(it->second, GetModelTypeChildSet(model_type));
114 parent_children_map_.erase(it);
115 }
116 }
117 // Store the new type root ID and associate the child set.
118 // Note that the child set isn't owned by the map in this case.
119 model_type_root_ids_[model_type] = type_root_id; 96 model_type_root_ids_[model_type] = type_root_id;
120 parent_children_map_.insert( 97 parent_children_map_.insert(std::make_pair(
121 std::make_pair(type_root_id, GetOrCreateModelTypeChildSet(model_type))); 98 type_root_id, GetOrCreateModelTypeChildSet(model_type)));
122 } 99 }
123 100
124 // Finally, insert the entry in the child set. 101 // Finally, insert the entry in the child set.
125 return siblings->insert(entry).second; 102 return siblings->insert(entry).second;
126 } 103 }
127 104
128 // Like the other containers used to help support the syncable::Directory, this 105 // Like the other containers used to help support the syncable::Directory, this
129 // one does not own any EntryKernels. This function removes references to the 106 // one does not own any EntryKernels. This function removes references to the
130 // given EntryKernel but does not delete it. 107 // given EntryKernel but does not delete it.
131 void ParentChildIndex::Remove(EntryKernel* e) { 108 void ParentChildIndex::Remove(EntryKernel* e) {
132 OrderedChildSet* siblings = nullptr; 109 OrderedChildSetRef siblings = nullptr;
133 ModelType model_type = e->GetModelType(); 110 ModelType model_type = e->GetModelType();
134 const Id& parent_id = e->ref(PARENT_ID); 111 const Id& parent_id = e->ref(PARENT_ID);
135 bool should_erase = false; 112 bool should_erase = false;
136 ParentChildrenMap::iterator it; 113 ParentChildrenMap::iterator sibling_iterator;
137 114
138 if (ShouldUseParentId(parent_id, model_type)) { 115 if (ShouldUseParentId(parent_id, model_type)) {
139 // Hierarchical type, lookup child set in the map. 116 // Hierarchical type, lookup child set in the map.
140 DCHECK(!parent_id.IsNull()); 117 DCHECK(!parent_id.IsNull());
141 it = parent_children_map_.find(parent_id); 118 sibling_iterator = parent_children_map_.find(parent_id);
142 DCHECK(it != parent_children_map_.end()); 119 DCHECK(sibling_iterator != parent_children_map_.end());
143 siblings = it->second; 120 siblings = sibling_iterator->second;
144 should_erase = true; 121 should_erase = true;
145 } else { 122 } else {
146 // Non-hierarchical type, return a pre-defined child set by type. 123 // Non-hierarchical type, return a pre-defined child set by type.
147 siblings = type_root_child_sets_[model_type]; 124 siblings = type_root_child_sets_[model_type];
148 } 125 }
149 126
150 OrderedChildSet::iterator j = siblings->find(e); 127 OrderedChildSet::iterator j = siblings->find(e);
151 DCHECK(j != siblings->end()); 128 DCHECK(j != siblings->end());
152 129
153 // Erase the entry from the child set. 130 // Erase the entry from the child set.
154 siblings->erase(j); 131 siblings->erase(j);
155 // If the set is now empty and isn't shareable with |type_root_child_sets_|, 132 // If the set is now empty and isn't shareable with |type_root_child_sets_|,
156 // erase it from the map. 133 // erase it from the map.
157 if (siblings->empty() && should_erase) { 134 if (siblings->empty() && should_erase) {
158 delete siblings; 135 parent_children_map_.erase(sibling_iterator);
159 parent_children_map_.erase(it);
160 } 136 }
161 } 137 }
162 138
163 bool ParentChildIndex::Contains(EntryKernel *e) const { 139 bool ParentChildIndex::Contains(EntryKernel *e) const {
164 const OrderedChildSet* siblings = GetChildSet(e); 140 const OrderedChildSetRef siblings = GetChildSet(e);
165 return siblings && siblings->count(e) > 0; 141 return siblings && siblings->count(e) > 0;
166 } 142 }
167 143
168 const OrderedChildSet* ParentChildIndex::GetChildren(const Id& id) const { 144 const OrderedChildSet* ParentChildIndex::GetChildren(const Id& id) const {
169 DCHECK(!id.IsNull()); 145 DCHECK(!id.IsNull());
170 146
171 ParentChildrenMap::const_iterator parent = parent_children_map_.find(id); 147 ParentChildrenMap::const_iterator parent = parent_children_map_.find(id);
172 if (parent == parent_children_map_.end()) { 148 if (parent == parent_children_map_.end()) {
173 return nullptr; 149 return nullptr;
174 } 150 }
175 151
176 OrderedChildSet* children = parent->second; 152 OrderedChildSetRef children = parent->second;
177 // The expectation is that the function returns nullptr instead of an empty 153 // The expectation is that the function returns nullptr instead of an empty
178 // child set 154 // child set
179 if (children && children->empty()) 155 if (children && children->empty())
180 children = nullptr; 156 children = nullptr;
181 return children; 157 return children.get();
182 } 158 }
183 159
184 const OrderedChildSet* ParentChildIndex::GetChildren(EntryKernel* e) const { 160 const OrderedChildSet* ParentChildIndex::GetChildren(EntryKernel* e) const {
185 return GetChildren(e->ref(ID)); 161 return GetChildren(e->ref(ID));
186 } 162 }
187 163
188 const OrderedChildSet* ParentChildIndex::GetSiblings(EntryKernel* e) const { 164 const OrderedChildSet* ParentChildIndex::GetSiblings(EntryKernel* e) const {
189 // This implies the entry is in the index. 165 // This implies the entry is in the index.
190 DCHECK(Contains(e)); 166 DCHECK(Contains(e));
191 const OrderedChildSet* siblings = GetChildSet(e); 167 const OrderedChildSetRef siblings = GetChildSet(e);
192 DCHECK(siblings && !siblings->empty()); 168 DCHECK(siblings && !siblings->empty());
193 return siblings; 169 return siblings.get();
194 } 170 }
195 171
196 /* static */ 172 /* static */
197 bool ParentChildIndex::ShouldUseParentId(const Id& parent_id, 173 bool ParentChildIndex::ShouldUseParentId(const Id& parent_id,
198 ModelType model_type) { 174 ModelType model_type) {
199 // For compatibility with legacy unit tests, in addition to hierarchical 175 // For compatibility with legacy unit tests, in addition to hierarchical
200 // entries, this returns true any entries directly under root and for entries 176 // entries, this returns true any entries directly under root and for entries
201 // of UNSPECIFIED model type. 177 // of UNSPECIFIED model type.
202 return parent_id.IsRoot() || TypeSupportsHierarchy(model_type) || 178 return parent_id.IsRoot() || TypeSupportsHierarchy(model_type) ||
203 !syncer::IsRealDataType(model_type); 179 !syncer::IsRealDataType(model_type);
204 } 180 }
205 181
206 const OrderedChildSet* ParentChildIndex::GetChildSet(EntryKernel* e) const { 182 const OrderedChildSetRef ParentChildIndex::GetChildSet(EntryKernel* e) const {
207 ModelType model_type = e->GetModelType(); 183 ModelType model_type = e->GetModelType();
208 184
209 const Id& parent_id = e->ref(PARENT_ID); 185 const Id& parent_id = e->ref(PARENT_ID);
210 if (ShouldUseParentId(parent_id, model_type)) { 186 if (ShouldUseParentId(parent_id, model_type)) {
211 // Hierarchical type, lookup child set in the map. 187 // Hierarchical type, lookup child set in the map.
212 ParentChildrenMap::const_iterator it = parent_children_map_.find(parent_id); 188 ParentChildrenMap::const_iterator it = parent_children_map_.find(parent_id);
213 if (it == parent_children_map_.end()) 189 if (it == parent_children_map_.end())
214 return nullptr; 190 return nullptr;
215 return it->second; 191 return it->second;
216 } 192 }
217 193
218 // Non-hierarchical type, return a collection indexed by type. 194 // Non-hierarchical type, return a collection indexed by type.
219 return GetModelTypeChildSet(model_type); 195 return GetModelTypeChildSet(model_type);
220 } 196 }
221 197
222 const OrderedChildSet* ParentChildIndex::GetModelTypeChildSet( 198 const OrderedChildSetRef ParentChildIndex::GetModelTypeChildSet(
223 ModelType model_type) const { 199 ModelType model_type) const {
224 return type_root_child_sets_[model_type]; 200 return type_root_child_sets_[model_type];
225 } 201 }
226 202
227 OrderedChildSet* ParentChildIndex::GetOrCreateModelTypeChildSet( 203 OrderedChildSetRef ParentChildIndex::GetOrCreateModelTypeChildSet(
228 ModelType model_type) { 204 ModelType model_type) {
229 if (!type_root_child_sets_[model_type]) 205 if (!type_root_child_sets_[model_type])
230 type_root_child_sets_[model_type] = new OrderedChildSet(); 206 type_root_child_sets_[model_type] =
207 OrderedChildSetRef(new OrderedChildSet());
231 return type_root_child_sets_[model_type]; 208 return type_root_child_sets_[model_type];
232 } 209 }
233 210
234 const Id& ParentChildIndex::GetModelTypeRootId(ModelType model_type) const { 211 const Id& ParentChildIndex::GetModelTypeRootId(ModelType model_type) const {
235 return model_type_root_ids_[model_type]; 212 return model_type_root_ids_[model_type];
236 } 213 }
237 214
238 } // namespace syncable 215 } // namespace syncable
239 } // namespace syncer 216 } // namespace syncer
OLDNEW
« no previous file with comments | « sync/syncable/parent_child_index.h ('k') | sync/syncable/parent_child_index_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698