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 #ifndef SYNC_SYNCABLE_PARENT_CHILD_INDEX
6 #define SYNC_SYNCABLE_PARENT_CHILD_INDEX
11 #include "base/basictypes.h"
12 #include "base/memory/scoped_vector.h"
13 #include "sync/base/sync_export.h"
14 #include "sync/internal_api/public/base/model_type.h"
15 #include "sync/syncable/syncable_id.h"
21 class ParentChildIndex
;
23 // A node ordering function.
24 struct SYNC_EXPORT_PRIVATE ChildComparator
{
25 bool operator() (const EntryKernel
* a
, const EntryKernel
* b
) const;
28 // An ordered set of nodes.
29 typedef std::set
<EntryKernel
*, ChildComparator
> OrderedChildSet
;
31 // Container that tracks parent-child relationships.
32 // Provides fast lookup of all items under a given parent.
33 class SYNC_EXPORT_PRIVATE ParentChildIndex
{
38 // Returns whether or not this entry belongs in the index.
39 // True for all non-deleted, non-root entries.
40 static bool ShouldInclude(const EntryKernel
* e
);
42 // Inserts a given child into the index.
43 bool Insert(EntryKernel
* e
);
45 // Removes a given child from the index.
46 void Remove(EntryKernel
* e
);
48 // Returns true if this item is in the index as a child.
49 bool Contains(EntryKernel
* e
) const;
51 // Returns all children of the entry with the given Id. Returns NULL if the
52 // node has no children or the Id does not identify a valid directory node.
53 const OrderedChildSet
* GetChildren(const Id
& id
) const;
55 // Returns all children of the entry. Returns NULL if the node has no
57 const OrderedChildSet
* GetChildren(EntryKernel
* e
) const;
59 // Returns all siblings of the entry.
60 const OrderedChildSet
* GetSiblings(EntryKernel
* e
) const;
63 friend class ParentChildIndexTest
;
65 typedef std::map
<Id
, OrderedChildSet
*> ParentChildrenMap
;
66 typedef std::vector
<Id
> TypeRootIds
;
67 typedef ScopedVector
<OrderedChildSet
> TypeRootChildSets
;
69 static bool ShouldUseParentId(const Id
& parent_id
, ModelType model_type
);
71 // Returns OrderedChildSet that should contain the specified entry
72 // based on the entry's Parent ID or model type.
73 const OrderedChildSet
* GetChildSet(EntryKernel
* e
) const;
75 // Returns OrderedChildSet that contain entries of the |model_type| type.
76 const OrderedChildSet
* GetModelTypeChildSet(ModelType model_type
) const;
78 // Returns mutable OrderedChildSet that contain entries of the |model_type|
79 // type. Create one as necessary.
80 OrderedChildSet
* GetOrCreateModelTypeChildSet(ModelType model_type
);
82 // Returns previously cached model type root ID for the given |model_type|.
83 const Id
& GetModelTypeRootId(ModelType model_type
) const;
85 // A map of parent IDs to children.
86 // Parents with no children are not included in this map.
87 ParentChildrenMap parent_children_map_
;
89 // This array tracks model type roots IDs.
90 TypeRootIds model_type_root_ids_
;
92 // This array contains pre-defined child sets for
93 // non-hierarchical types (types with flat hierarchy) that support entries
94 // with implicit parent.
95 TypeRootChildSets type_root_child_sets_
;
97 DISALLOW_COPY_AND_ASSIGN(ParentChildIndex
);
100 } // namespace syncable
101 } // namespace syncer
103 #endif // SYNC_SYNCABLE_PARENT_CHILD_INDEX