OLD | NEW |
| (Empty) |
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 #include "chrome/browser/sync/sessions/ordered_commit_set.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "base/logging.h" | |
10 | |
11 namespace browser_sync { | |
12 namespace sessions { | |
13 | |
14 OrderedCommitSet::OrderedCommitSet( | |
15 const browser_sync::ModelSafeRoutingInfo& routes) | |
16 : routes_(routes) { | |
17 } | |
18 | |
19 OrderedCommitSet::~OrderedCommitSet() {} | |
20 | |
21 void OrderedCommitSet::AddCommitItem(const int64 metahandle, | |
22 const syncable::Id& commit_id, | |
23 syncable::ModelType type) { | |
24 if (!HaveCommitItem(metahandle)) { | |
25 inserted_metahandles_.insert(metahandle); | |
26 metahandle_order_.push_back(metahandle); | |
27 commit_ids_.push_back(commit_id); | |
28 projections_[GetGroupForModelType(type, routes_)].push_back( | |
29 commit_ids_.size() - 1); | |
30 types_.push_back(type); | |
31 } | |
32 } | |
33 | |
34 void OrderedCommitSet::Append(const OrderedCommitSet& other) { | |
35 for (int i = 0; i < other.Size(); ++i) { | |
36 CommitItem item = other.GetCommitItemAt(i); | |
37 AddCommitItem(item.meta, item.id, item.group); | |
38 } | |
39 } | |
40 | |
41 void OrderedCommitSet::AppendReverse(const OrderedCommitSet& other) { | |
42 for (int i = other.Size() - 1; i >= 0; i--) { | |
43 CommitItem item = other.GetCommitItemAt(i); | |
44 AddCommitItem(item.meta, item.id, item.group); | |
45 } | |
46 } | |
47 | |
48 void OrderedCommitSet::Truncate(size_t max_size) { | |
49 if (max_size < metahandle_order_.size()) { | |
50 for (size_t i = max_size; i < metahandle_order_.size(); ++i) { | |
51 inserted_metahandles_.erase(metahandle_order_[i]); | |
52 } | |
53 | |
54 // Some projections may refer to indices that are getting chopped. | |
55 // Since projections are in increasing order, it's easy to fix. Except | |
56 // that you can't erase(..) using a reverse_iterator, so we use binary | |
57 // search to find the chop point. | |
58 Projections::iterator it = projections_.begin(); | |
59 for (; it != projections_.end(); ++it) { | |
60 // For each projection, chop off any indices larger than or equal to | |
61 // max_size by looking for max_size using binary search. | |
62 Projection& p = it->second; | |
63 Projection::iterator element = std::lower_bound(p.begin(), p.end(), | |
64 max_size); | |
65 if (element != p.end()) | |
66 p.erase(element, p.end()); | |
67 } | |
68 commit_ids_.resize(max_size); | |
69 metahandle_order_.resize(max_size); | |
70 types_.resize(max_size); | |
71 } | |
72 } | |
73 | |
74 OrderedCommitSet::CommitItem OrderedCommitSet::GetCommitItemAt( | |
75 const int position) const { | |
76 DCHECK(position < Size()); | |
77 CommitItem return_item = {metahandle_order_[position], | |
78 commit_ids_[position], | |
79 types_[position]}; | |
80 return return_item; | |
81 } | |
82 | |
83 bool OrderedCommitSet::HasBookmarkCommitId() const { | |
84 ModelSafeRoutingInfo::const_iterator group | |
85 = routes_.find(syncable::BOOKMARKS); | |
86 if (group == routes_.end()) | |
87 return false; | |
88 Projections::const_iterator proj = projections_.find(group->second); | |
89 if (proj == projections_.end()) | |
90 return false; | |
91 DCHECK_LE(proj->second.size(), types_.size()); | |
92 for (size_t i = 0; i < proj->second.size(); i++) { | |
93 if (types_[proj->second[i]] == syncable::BOOKMARKS) | |
94 return true; | |
95 } | |
96 return false; | |
97 } | |
98 | |
99 void OrderedCommitSet::operator=(const OrderedCommitSet& other) { | |
100 inserted_metahandles_ = other.inserted_metahandles_; | |
101 commit_ids_ = other.commit_ids_; | |
102 metahandle_order_ = other.metahandle_order_; | |
103 projections_ = other.projections_; | |
104 types_ = other.types_; | |
105 routes_ = other.routes_; | |
106 } | |
107 | |
108 } // namespace sessions | |
109 } // namespace browser_sync | |
110 | |
OLD | NEW |