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 6 #define SYNC_SYNCABLE_PARENT_CHILD_INDEX 7 8 #include <map> 9 #include <set> 10 11 #include "base/basictypes.h" 12 #include "sync/base/sync_export.h" 13 14 namespace syncer { 15 namespace syncable { 16 17 struct EntryKernel; 18 class Id; 19 class ParentChildIndex; 20 21 // A node ordering function. 22 struct SYNC_EXPORT_PRIVATE ChildComparator { 23 bool operator() (const EntryKernel* a, const EntryKernel* b) const; 24 }; 25 26 // An ordered set of nodes. 27 typedef std::set<EntryKernel*, ChildComparator> OrderedChildSet; 28 29 // Container that tracks parent-child relationships. 30 // Provides fast lookup of all items under a given parent. 31 class SYNC_EXPORT_PRIVATE ParentChildIndex { 32 public: 33 ParentChildIndex(); 34 ~ParentChildIndex(); 35 36 // Returns whether or not this entry belongs in the index. 37 // True for all non-deleted, non-root entries. 38 static bool ShouldInclude(const EntryKernel* e); 39 40 // Inserts a given child into the index. 41 bool Insert(EntryKernel* e); 42 43 // Removes a given child from the index. 44 void Remove(EntryKernel* e); 45 46 // Returns true if this item is in the index as a child. 47 bool Contains(EntryKernel* e) const; 48 49 // Returns all children of the entry with the given Id. Returns NULL if the 50 // node has no children or the Id does not identify a valid directory node. 51 const OrderedChildSet* GetChildren(const Id& id); 52 53 private: 54 typedef std::map<syncable::Id, OrderedChildSet*> ParentChildrenMap; 55 56 // A map of parent IDs to children. 57 // Parents with no children are not included in this map. 58 ParentChildrenMap parent_children_map_; 59 60 DISALLOW_COPY_AND_ASSIGN(ParentChildIndex); 61 }; 62 63 } // namespace syncable 64 } // namespace syncer 65 66 #endif // SYNC_SYNCABLE_PARENT_CHILD_INDEX 67