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.
5 #include "sync/sessions/ordered_commit_set.h"
9 #include "base/logging.h"
14 OrderedCommitSet::OrderedCommitSet(const ModelSafeRoutingInfo
& routes
)
18 OrderedCommitSet::~OrderedCommitSet() {}
20 void OrderedCommitSet::AddCommitItem(const int64 metahandle
,
21 const syncable::Id
& commit_id
,
23 if (!HaveCommitItem(metahandle
)) {
24 inserted_metahandles_
.insert(metahandle
);
25 metahandle_order_
.push_back(metahandle
);
26 commit_ids_
.push_back(commit_id
);
27 projections_
[GetGroupForModelType(type
, routes_
)].push_back(
28 commit_ids_
.size() - 1);
29 types_
.push_back(type
);
33 const OrderedCommitSet::Projection
& OrderedCommitSet::GetCommitIdProjection(
34 ModelSafeGroup group
) const {
35 Projections::const_iterator i
= projections_
.find(group
);
36 DCHECK(i
!= projections_
.end());
40 void OrderedCommitSet::Append(const OrderedCommitSet
& other
) {
41 for (size_t i
= 0; i
< other
.Size(); ++i
) {
42 CommitItem item
= other
.GetCommitItemAt(i
);
43 AddCommitItem(item
.meta
, item
.id
, item
.group
);
47 void OrderedCommitSet::AppendReverse(const OrderedCommitSet
& other
) {
48 for (int i
= other
.Size() - 1; i
>= 0; i
--) {
49 CommitItem item
= other
.GetCommitItemAt(i
);
50 AddCommitItem(item
.meta
, item
.id
, item
.group
);
54 void OrderedCommitSet::Truncate(size_t max_size
) {
55 if (max_size
< metahandle_order_
.size()) {
56 for (size_t i
= max_size
; i
< metahandle_order_
.size(); ++i
) {
57 inserted_metahandles_
.erase(metahandle_order_
[i
]);
60 // Some projections may refer to indices that are getting chopped.
61 // Since projections are in increasing order, it's easy to fix. Except
62 // that you can't erase(..) using a reverse_iterator, so we use binary
63 // search to find the chop point.
64 Projections::iterator it
= projections_
.begin();
65 for (; it
!= projections_
.end(); ++it
) {
66 // For each projection, chop off any indices larger than or equal to
67 // max_size by looking for max_size using binary search.
68 Projection
& p
= it
->second
;
69 Projection::iterator element
= std::lower_bound(p
.begin(), p
.end(),
71 if (element
!= p
.end())
72 p
.erase(element
, p
.end());
74 commit_ids_
.resize(max_size
);
75 metahandle_order_
.resize(max_size
);
76 types_
.resize(max_size
);
80 void OrderedCommitSet::Clear() {
81 inserted_metahandles_
.clear();
83 metahandle_order_
.clear();
84 for (Projections::iterator it
= projections_
.begin();
85 it
!= projections_
.end(); ++it
) {
91 OrderedCommitSet::CommitItem
OrderedCommitSet::GetCommitItemAt(
92 const size_t position
) const {
93 DCHECK(position
< Size());
94 CommitItem return_item
= {metahandle_order_
[position
],
95 commit_ids_
[position
],
100 bool OrderedCommitSet::HasBookmarkCommitId() const {
101 ModelSafeRoutingInfo::const_iterator group
= routes_
.find(BOOKMARKS
);
102 if (group
== routes_
.end())
104 Projections::const_iterator proj
= projections_
.find(group
->second
);
105 if (proj
== projections_
.end())
107 DCHECK_LE(proj
->second
.size(), types_
.size());
108 for (size_t i
= 0; i
< proj
->second
.size(); i
++) {
109 if (types_
[proj
->second
[i
]] == BOOKMARKS
)
115 void OrderedCommitSet::operator=(const OrderedCommitSet
& other
) {
116 inserted_metahandles_
= other
.inserted_metahandles_
;
117 commit_ids_
= other
.commit_ids_
;
118 metahandle_order_
= other
.metahandle_order_
;
119 projections_
= other
.projections_
;
120 types_
= other
.types_
;
121 routes_
= other
.routes_
;
124 } // namespace sessions
125 } // namespace syncer