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