More bring up of ui code on iOS.
[chromium-blink-merge.git] / sync / sessions / ordered_commit_set.h
blobcc6940a6172686e918d13fd6667904369391bc12
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 #ifndef SYNC_SESSIONS_ORDERED_COMMIT_SET_H_
6 #define SYNC_SESSIONS_ORDERED_COMMIT_SET_H_
8 #include <map>
9 #include <set>
10 #include <vector>
12 #include "sync/internal_api/public/base/model_type.h"
13 #include "sync/internal_api/public/engine/model_safe_worker.h"
14 #include "sync/syncable/syncable_id.h"
16 namespace syncer {
17 namespace sessions {
19 // TODO(ncarter): This code is more generic than just Commit and can
20 // be reused elsewhere (e.g. ChangeReorderBuffer do similar things). Merge
21 // all these implementations.
22 class OrderedCommitSet {
23 public:
24 // A list of indices into the full list of commit ids such that:
25 // 1 - each element is an index belonging to a particular ModelSafeGroup.
26 // 2 - the vector is in sorted (smallest to largest) order.
27 // 3 - each element is a valid index for GetCommitItemAt.
28 // See GetCommitIdProjection for usage.
29 typedef std::vector<size_t> Projection;
31 // TODO(chron): Reserve space according to batch size?
32 explicit OrderedCommitSet(const ModelSafeRoutingInfo& routes);
33 ~OrderedCommitSet();
35 bool HaveCommitItem(const int64 metahandle) const {
36 return inserted_metahandles_.count(metahandle) > 0;
39 void AddCommitItem(const int64 metahandle, const syncable::Id& commit_id,
40 ModelType type);
42 const std::vector<syncable::Id>& GetAllCommitIds() const {
43 return commit_ids_;
46 // Return the Id at index |position| in this OrderedCommitSet. Note that
47 // the index uniquely identifies the same logical item in each of:
48 // 1) this OrderedCommitSet
49 // 2) the CommitRequest sent to the server
50 // 3) the list of EntryResponse objects in the CommitResponse.
51 // These together allow re-association of the pre-commit Id with the
52 // actual committed entry.
53 const syncable::Id& GetCommitIdAt(const size_t position) const {
54 return commit_ids_[position];
57 // Same as above, but for ModelType of the item.
58 ModelType GetModelTypeAt(const size_t position) const {
59 return types_[position];
62 // Get the projection of commit ids onto the space of commit ids
63 // belonging to |group|. This is useful when you need to process a commit
64 // response one ModelSafeGroup at a time. See GetCommitIdAt for how the
65 // indices contained in the returned Projection can be used.
66 const Projection& GetCommitIdProjection(
67 ModelSafeGroup group) const;
69 size_t Size() const {
70 return commit_ids_.size();
73 bool Empty() const {
74 return Size() == 0;
77 // Returns true iff any of the commit ids added to this set have model type
78 // BOOKMARKS.
79 bool HasBookmarkCommitId() const;
81 void Append(const OrderedCommitSet& other);
82 void AppendReverse(const OrderedCommitSet& other);
83 void Truncate(size_t max_size);
85 // Removes all entries from this set.
86 void Clear();
88 void operator=(const OrderedCommitSet& other);
89 private:
90 // A set of CommitIdProjections associated with particular ModelSafeGroups.
91 typedef std::map<ModelSafeGroup, Projection> Projections;
93 // Helper container for return value of GetCommitItemAt.
94 struct CommitItem {
95 int64 meta;
96 syncable::Id id;
97 ModelType group;
100 CommitItem GetCommitItemAt(const size_t position) const;
102 // These lists are different views of the same items; e.g they are
103 // isomorphic.
104 std::set<int64> inserted_metahandles_;
105 std::vector<syncable::Id> commit_ids_;
106 std::vector<int64> metahandle_order_;
107 Projections projections_;
109 // We need this because of operations like AppendReverse that take ids from
110 // one OrderedCommitSet and insert into another -- we need to know the
111 // group for each ID so that the insertion can update the appropriate
112 // projection. We could store it in commit_ids_, but sometimes we want
113 // to just return the vector of Ids, so this is more straightforward
114 // and shouldn't take up too much extra space since commit lists are small.
115 std::vector<ModelType> types_;
117 ModelSafeRoutingInfo routes_;
120 } // namespace sessions
121 } // namespace syncer
123 #endif // SYNC_SESSIONS_ORDERED_COMMIT_SET_H_