1 // Copyright 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_
12 #include "sync/base/sync_export.h"
13 #include "sync/internal_api/public/base/model_type.h"
14 #include "sync/internal_api/public/engine/model_safe_worker.h"
15 #include "sync/syncable/syncable_id.h"
20 // TODO(ncarter): This code is more generic than just Commit and can
21 // be reused elsewhere (e.g. ChangeReorderBuffer do similar things). Merge
22 // all these implementations.
23 class SYNC_EXPORT_PRIVATE OrderedCommitSet
{
25 // A list of indices into the full list of commit ids such that:
26 // 1 - each element is an index belonging to a particular ModelSafeGroup.
27 // 2 - the vector is in sorted (smallest to largest) order.
28 // 3 - each element is a valid index for GetCommitItemAt.
29 // See GetCommitIdProjection for usage.
30 typedef std::vector
<size_t> Projection
;
32 // TODO(chron): Reserve space according to batch size?
33 explicit OrderedCommitSet(const ModelSafeRoutingInfo
& routes
);
36 bool HaveCommitItem(const int64 metahandle
) const {
37 return inserted_metahandles_
.count(metahandle
) > 0;
40 void AddCommitItem(const int64 metahandle
, const syncable::Id
& commit_id
,
43 const std::vector
<syncable::Id
>& GetAllCommitIds() const {
47 // Return the Id at index |position| in this OrderedCommitSet. Note that
48 // the index uniquely identifies the same logical item in each of:
49 // 1) this OrderedCommitSet
50 // 2) the CommitRequest sent to the server
51 // 3) the list of EntryResponse objects in the CommitResponse.
52 // These together allow re-association of the pre-commit Id with the
53 // actual committed entry.
54 const syncable::Id
& GetCommitIdAt(const size_t position
) const {
55 return commit_ids_
[position
];
58 // Same as above, but for ModelType of the item.
59 ModelType
GetModelTypeAt(const size_t position
) const {
60 return types_
[position
];
63 // Get the projection of commit ids onto the space of commit ids
64 // belonging to |group|. This is useful when you need to process a commit
65 // response one ModelSafeGroup at a time. See GetCommitIdAt for how the
66 // indices contained in the returned Projection can be used.
67 const Projection
& GetCommitIdProjection(
68 ModelSafeGroup group
) const;
71 return commit_ids_
.size();
78 // Returns true iff any of the commit ids added to this set have model type
80 bool HasBookmarkCommitId() const;
82 void Append(const OrderedCommitSet
& other
);
83 void AppendReverse(const OrderedCommitSet
& other
);
84 void Truncate(size_t max_size
);
86 // Removes all entries from this set.
89 void operator=(const OrderedCommitSet
& other
);
91 // A set of CommitIdProjections associated with particular ModelSafeGroups.
92 typedef std::map
<ModelSafeGroup
, Projection
> Projections
;
94 // Helper container for return value of GetCommitItemAt.
101 CommitItem
GetCommitItemAt(const size_t position
) const;
103 // These lists are different views of the same items; e.g they are
105 std::set
<int64
> inserted_metahandles_
;
106 std::vector
<syncable::Id
> commit_ids_
;
107 std::vector
<int64
> metahandle_order_
;
108 Projections projections_
;
110 // We need this because of operations like AppendReverse that take ids from
111 // one OrderedCommitSet and insert into another -- we need to know the
112 // group for each ID so that the insertion can update the appropriate
113 // projection. We could store it in commit_ids_, but sometimes we want
114 // to just return the vector of Ids, so this is more straightforward
115 // and shouldn't take up too much extra space since commit lists are small.
116 std::vector
<ModelType
> types_
;
118 ModelSafeRoutingInfo routes_
;
121 } // namespace sessions
122 } // namespace syncer
124 #endif // SYNC_SESSIONS_ORDERED_COMMIT_SET_H_