File: parent_child_index.h

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (107 lines) | stat: -rw-r--r-- 3,520 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_SYNC_SYNCABLE_PARENT_CHILD_INDEX_H_
#define COMPONENTS_SYNC_SYNCABLE_PARENT_CHILD_INDEX_H_

#include <map>
#include <memory>
#include <set>
#include <vector>

#include "base/macros.h"
#include "components/sync/base/model_type.h"
#include "components/sync/syncable/syncable_id.h"

namespace syncer {
namespace syncable {

struct EntryKernel;
class ParentChildIndex;

// A node ordering function.
struct ChildComparator {
  bool operator()(const EntryKernel* a, const EntryKernel* b) const;
};

// An ordered set of nodes.
typedef std::set<EntryKernel*, ChildComparator> OrderedChildSet;
typedef std::shared_ptr<OrderedChildSet> OrderedChildSetRef;

// Container that tracks parent-child relationships.
// Provides fast lookup of all items under a given parent.
class ParentChildIndex {
 public:
  ParentChildIndex();
  ~ParentChildIndex();

  // Returns whether or not this entry belongs in the index.
  // True for all non-deleted, non-root entries.
  static bool ShouldInclude(const EntryKernel* e);

  // Inserts a given child into the index.
  bool Insert(EntryKernel* e);

  // Removes a given child from the index.
  void Remove(EntryKernel* e);

  // Returns true if this item is in the index as a child.
  bool Contains(EntryKernel* e) const;

  // Returns all children of the entry with the given Id.  Returns null if the
  // node has no children or the Id does not identify a valid directory node.
  const OrderedChildSet* GetChildren(const Id& id) const;

  // Returns all children of the entry.  Returns null if the node has no
  // children.
  const OrderedChildSet* GetChildren(EntryKernel* e) const;

  // Returns all siblings of the entry.
  const OrderedChildSet* GetSiblings(EntryKernel* e) const;

  // Estimates amount of heap memory used.
  size_t EstimateMemoryUsage() const;

 private:
  friend class ParentChildIndexTest;

  typedef std::map<Id, OrderedChildSetRef> ParentChildrenMap;
  typedef std::vector<Id> TypeRootIds;
  typedef std::vector<OrderedChildSetRef> TypeRootChildSets;

  static bool ShouldUseParentId(const Id& parent_id, ModelType model_type);

  // Returns OrderedChildSet that should contain the specified entry
  // based on the entry's Parent ID or model type.
  const OrderedChildSetRef GetChildSet(EntryKernel* e) const;

  // Returns OrderedChildSet that contain entries of the |model_type| type.
  const OrderedChildSetRef GetModelTypeChildSet(ModelType model_type) const;

  // Returns mutable OrderedChildSet that contain entries of the |model_type|
  // type. Create one as necessary.
  OrderedChildSetRef GetOrCreateModelTypeChildSet(ModelType model_type);

  // Returns previously cached model type root ID for the given |model_type|.
  const Id& GetModelTypeRootId(ModelType model_type) const;

  // A map of parent IDs to children.
  // Parents with no children are not included in this map.
  ParentChildrenMap parent_children_map_;

  // This array tracks model type roots IDs.
  TypeRootIds model_type_root_ids_;

  // This array contains pre-defined child sets for
  // non-hierarchical types (types with flat hierarchy) that support entries
  // with implicit parent.
  TypeRootChildSets type_root_child_sets_;

  DISALLOW_COPY_AND_ASSIGN(ParentChildIndex);
};

}  // namespace syncable
}  // namespace syncer

#endif  // COMPONENTS_SYNC_SYNCABLE_PARENT_CHILD_INDEX_H_