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
108
109
110
|
// 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.
#include "sync/sessions/ordered_commit_set.h"
#include <algorithm>
#include "base/logging.h"
namespace browser_sync {
namespace sessions {
OrderedCommitSet::OrderedCommitSet(
const browser_sync::ModelSafeRoutingInfo& routes)
: routes_(routes) {
}
OrderedCommitSet::~OrderedCommitSet() {}
void OrderedCommitSet::AddCommitItem(const int64 metahandle,
const syncable::Id& commit_id,
syncable::ModelType type) {
if (!HaveCommitItem(metahandle)) {
inserted_metahandles_.insert(metahandle);
metahandle_order_.push_back(metahandle);
commit_ids_.push_back(commit_id);
projections_[GetGroupForModelType(type, routes_)].push_back(
commit_ids_.size() - 1);
types_.push_back(type);
}
}
void OrderedCommitSet::Append(const OrderedCommitSet& other) {
for (int i = 0; i < other.Size(); ++i) {
CommitItem item = other.GetCommitItemAt(i);
AddCommitItem(item.meta, item.id, item.group);
}
}
void OrderedCommitSet::AppendReverse(const OrderedCommitSet& other) {
for (int i = other.Size() - 1; i >= 0; i--) {
CommitItem item = other.GetCommitItemAt(i);
AddCommitItem(item.meta, item.id, item.group);
}
}
void OrderedCommitSet::Truncate(size_t max_size) {
if (max_size < metahandle_order_.size()) {
for (size_t i = max_size; i < metahandle_order_.size(); ++i) {
inserted_metahandles_.erase(metahandle_order_[i]);
}
// Some projections may refer to indices that are getting chopped.
// Since projections are in increasing order, it's easy to fix. Except
// that you can't erase(..) using a reverse_iterator, so we use binary
// search to find the chop point.
Projections::iterator it = projections_.begin();
for (; it != projections_.end(); ++it) {
// For each projection, chop off any indices larger than or equal to
// max_size by looking for max_size using binary search.
Projection& p = it->second;
Projection::iterator element = std::lower_bound(p.begin(), p.end(),
max_size);
if (element != p.end())
p.erase(element, p.end());
}
commit_ids_.resize(max_size);
metahandle_order_.resize(max_size);
types_.resize(max_size);
}
}
OrderedCommitSet::CommitItem OrderedCommitSet::GetCommitItemAt(
const int position) const {
DCHECK(position < Size());
CommitItem return_item = {metahandle_order_[position],
commit_ids_[position],
types_[position]};
return return_item;
}
bool OrderedCommitSet::HasBookmarkCommitId() const {
ModelSafeRoutingInfo::const_iterator group
= routes_.find(syncable::BOOKMARKS);
if (group == routes_.end())
return false;
Projections::const_iterator proj = projections_.find(group->second);
if (proj == projections_.end())
return false;
DCHECK_LE(proj->second.size(), types_.size());
for (size_t i = 0; i < proj->second.size(); i++) {
if (types_[proj->second[i]] == syncable::BOOKMARKS)
return true;
}
return false;
}
void OrderedCommitSet::operator=(const OrderedCommitSet& other) {
inserted_metahandles_ = other.inserted_metahandles_;
commit_ids_ = other.commit_ids_;
metahandle_order_ = other.metahandle_order_;
projections_ = other.projections_;
types_ = other.types_;
routes_ = other.routes_;
}
} // namespace sessions
} // namespace browser_sync
|