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
3 // found in the LICENSE file.
5 #include "sync/syncable/parent_child_index.h"
7 #include "base/stl_util.h"
9 #include "sync/syncable/entry_kernel.h"
10 #include "sync/syncable/syncable_id.h"
15 bool ChildComparator::operator()(const EntryKernel
* a
,
16 const EntryKernel
* b
) const {
17 const UniquePosition
& a_pos
= a
->ref(UNIQUE_POSITION
);
18 const UniquePosition
& b_pos
= b
->ref(UNIQUE_POSITION
);
20 if (a_pos
.IsValid() && b_pos
.IsValid()) {
21 // Position is important to this type.
22 return a_pos
.LessThan(b_pos
);
23 } else if (a_pos
.IsValid() && !b_pos
.IsValid()) {
24 // TODO(rlarocque): Remove this case.
25 // An item with valid position as sibling of one with invalid position.
26 // We should not support this, but the tests rely on it. For now, just
27 // move all invalid position items to the right.
29 } else if (!a_pos
.IsValid() && b_pos
.IsValid()) {
30 // TODO(rlarocque): Remove this case.
31 // Mirror of the above case.
34 // Position doesn't matter.
35 DCHECK(!a
->ref(UNIQUE_POSITION
).IsValid());
36 DCHECK(!b
->ref(UNIQUE_POSITION
).IsValid());
37 // Sort by META_HANDLE to ensure consistent order for testing.
38 return a
->ref(META_HANDLE
) < b
->ref(META_HANDLE
);
42 ParentChildIndex::ParentChildIndex() {
45 ParentChildIndex::~ParentChildIndex() {
46 STLDeleteContainerPairSecondPointers(
47 parent_children_map_
.begin(), parent_children_map_
.end());
50 bool ParentChildIndex::ShouldInclude(const EntryKernel
* entry
) {
51 // This index excludes deleted items and the root item. The root
52 // item is excluded so that it doesn't show up as a child of itself.
53 return !entry
->ref(IS_DEL
) && !entry
->ref(ID
).IsRoot();
56 bool ParentChildIndex::Insert(EntryKernel
* entry
) {
57 DCHECK(ShouldInclude(entry
));
59 const Id
& parent_id
= GetParentId(entry
);
60 // Store type root ID when inserting a type root entry.
61 if (parent_id
.IsRoot()) {
62 ModelType model_type
= GetModelType(entry
);
63 // TODO(stanisc): there are some unit tests that create entries
64 // at the root and don't bother initializing specifics which
65 // produces TOP_LEVEL_FOLDER type here.
66 if (syncer::IsRealDataType(model_type
)) {
67 model_type_root_ids_
[model_type
] = entry
->ref(ID
);
71 OrderedChildSet
* children
= NULL
;
72 ParentChildrenMap::iterator i
= parent_children_map_
.find(parent_id
);
73 if (i
!= parent_children_map_
.end()) {
76 children
= new OrderedChildSet();
77 parent_children_map_
.insert(std::make_pair(parent_id
, children
));
80 return children
->insert(entry
).second
;
83 // Like the other containers used to help support the syncable::Directory, this
84 // one does not own any EntryKernels. This function removes references to the
85 // given EntryKernel but does not delete it.
86 void ParentChildIndex::Remove(EntryKernel
* e
) {
87 const Id
& parent_id
= GetParentId(e
);
89 ParentChildrenMap::iterator parent
= parent_children_map_
.find(parent_id
);
90 DCHECK(parent
!= parent_children_map_
.end());
92 OrderedChildSet
* children
= parent
->second
;
93 OrderedChildSet::iterator j
= children
->find(e
);
94 DCHECK(j
!= children
->end());
97 if (children
->empty()) {
99 parent_children_map_
.erase(parent
);
103 bool ParentChildIndex::Contains(EntryKernel
*e
) const {
104 const Id
& parent_id
= GetParentId(e
);
105 ParentChildrenMap::const_iterator parent
=
106 parent_children_map_
.find(parent_id
);
107 if (parent
== parent_children_map_
.end()) {
110 const OrderedChildSet
* children
= parent
->second
;
111 DCHECK(children
&& !children
->empty());
112 return children
->count(e
) > 0;
115 const OrderedChildSet
* ParentChildIndex::GetChildren(const Id
& id
) const {
116 DCHECK(!id
.IsNull());
118 ParentChildrenMap::const_iterator parent
= parent_children_map_
.find(id
);
119 if (parent
== parent_children_map_
.end()) {
123 // A successful lookup implies at least some children exist.
124 DCHECK(!parent
->second
->empty());
125 return parent
->second
;
128 const OrderedChildSet
* ParentChildIndex::GetChildren(EntryKernel
* e
) const {
129 return GetChildren(e
->ref(ID
));
132 const OrderedChildSet
* ParentChildIndex::GetSiblings(EntryKernel
* e
) const {
134 const OrderedChildSet
* siblings
= GetChildren(GetParentId(e
));
135 DCHECK(siblings
&& !siblings
->empty());
139 const Id
& ParentChildIndex::GetParentId(EntryKernel
* e
) const {
140 const Id
& parent_id
= e
->ref(PARENT_ID
);
141 if (!parent_id
.IsNull()) {
144 return GetModelTypeRootId(GetModelType(e
));
147 ModelType
ParentChildIndex::GetModelType(EntryKernel
* e
) {
148 // TODO(stanisc): is there a more effective way to find out model type?
149 ModelType model_type
= e
->GetModelType();
150 if (!syncer::IsRealDataType(model_type
)) {
151 model_type
= e
->GetServerModelType();
156 const Id
& ParentChildIndex::GetModelTypeRootId(ModelType model_type
) const {
157 // TODO(stanisc): Review whether this approach is reliable enough.
158 // Should this method simply enumerate children of root node ("r") instead?
159 return model_type_root_ids_
[model_type
];
162 } // namespace syncable
163 } // namespace syncer