This source file includes following definitions.
- ShouldInclude
- Insert
- Remove
- Contains
- GetChildren
#include "sync/syncable/parent_child_index.h"
#include "base/stl_util.h"
#include "sync/syncable/entry_kernel.h"
#include "sync/syncable/syncable_id.h"
namespace syncer {
namespace syncable {
bool ChildComparator::operator()(
const syncable::EntryKernel* a,
const syncable::EntryKernel* b) const {
const UniquePosition& a_pos = a->ref(UNIQUE_POSITION);
const UniquePosition& b_pos = b->ref(UNIQUE_POSITION);
if (a_pos.IsValid() && b_pos.IsValid()) {
return a_pos.LessThan(b_pos);
} else if (a_pos.IsValid() && !b_pos.IsValid()) {
return true;
} else if (!a_pos.IsValid() && b_pos.IsValid()) {
return false;
} else {
DCHECK(!a->ref(UNIQUE_POSITION).IsValid());
DCHECK(!b->ref(UNIQUE_POSITION).IsValid());
return a->ref(ID) < b->ref(ID);
}
}
ParentChildIndex::ParentChildIndex() {
}
ParentChildIndex::~ParentChildIndex() {
STLDeleteContainerPairSecondPointers(
parent_children_map_.begin(), parent_children_map_.end());
}
bool ParentChildIndex::ShouldInclude(const EntryKernel* entry) {
return !entry->ref(IS_DEL) && !entry->ref(ID).IsRoot();
}
bool ParentChildIndex::Insert(EntryKernel* entry) {
DCHECK(ShouldInclude(entry));
const syncable::Id& parent_id = entry->ref(PARENT_ID);
OrderedChildSet* children = NULL;
ParentChildrenMap::iterator i = parent_children_map_.find(parent_id);
if (i != parent_children_map_.end()) {
children = i->second;
} else {
children = new OrderedChildSet();
parent_children_map_.insert(std::make_pair(parent_id, children));
}
return children->insert(entry).second;
}
void ParentChildIndex::Remove(EntryKernel* e) {
ParentChildrenMap::iterator parent =
parent_children_map_.find(e->ref(PARENT_ID));
DCHECK(parent != parent_children_map_.end());
OrderedChildSet* children = parent->second;
OrderedChildSet::iterator j = children->find(e);
DCHECK(j != children->end());
children->erase(j);
if (children->empty()) {
delete children;
parent_children_map_.erase(parent);
}
}
bool ParentChildIndex::Contains(EntryKernel *e) const {
const syncable::Id& parent_id = e->ref(PARENT_ID);
ParentChildrenMap::const_iterator parent =
parent_children_map_.find(parent_id);
if (parent == parent_children_map_.end()) {
return false;
}
const OrderedChildSet* children = parent->second;
DCHECK(children && !children->empty());
return children->count(e) > 0;
}
const OrderedChildSet* ParentChildIndex::GetChildren(const syncable::Id& id) {
ParentChildrenMap::iterator parent = parent_children_map_.find(id);
if (parent == parent_children_map_.end()) {
return NULL;
}
DCHECK(!parent->second->empty());
return parent->second;
}
}
}