| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef SYNC_SESSIONS_ORDERED_COMMIT_SET_H_ | 5 #ifndef SYNC_SESSIONS_ORDERED_COMMIT_SET_H_ |
| 6 #define SYNC_SESSIONS_ORDERED_COMMIT_SET_H_ | 6 #define SYNC_SESSIONS_ORDERED_COMMIT_SET_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 11 matching lines...) Expand all Loading... |
| 22 class OrderedCommitSet { | 22 class OrderedCommitSet { |
| 23 public: | 23 public: |
| 24 // A list of indices into the full list of commit ids such that: | 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. | 25 // 1 - each element is an index belonging to a particular ModelSafeGroup. |
| 26 // 2 - the vector is in sorted (smallest to largest) order. | 26 // 2 - the vector is in sorted (smallest to largest) order. |
| 27 // 3 - each element is a valid index for GetCommitItemAt. | 27 // 3 - each element is a valid index for GetCommitItemAt. |
| 28 // See GetCommitIdProjection for usage. | 28 // See GetCommitIdProjection for usage. |
| 29 typedef std::vector<size_t> Projection; | 29 typedef std::vector<size_t> Projection; |
| 30 | 30 |
| 31 // TODO(chron): Reserve space according to batch size? | 31 // TODO(chron): Reserve space according to batch size? |
| 32 explicit OrderedCommitSet(const syncer::ModelSafeRoutingInfo& routes); | 32 explicit OrderedCommitSet(const ModelSafeRoutingInfo& routes); |
| 33 ~OrderedCommitSet(); | 33 ~OrderedCommitSet(); |
| 34 | 34 |
| 35 bool HaveCommitItem(const int64 metahandle) const { | 35 bool HaveCommitItem(const int64 metahandle) const { |
| 36 return inserted_metahandles_.count(metahandle) > 0; | 36 return inserted_metahandles_.count(metahandle) > 0; |
| 37 } | 37 } |
| 38 | 38 |
| 39 void AddCommitItem(const int64 metahandle, const syncable::Id& commit_id, | 39 void AddCommitItem(const int64 metahandle, const syncable::Id& commit_id, |
| 40 syncer::ModelType type); | 40 ModelType type); |
| 41 | 41 |
| 42 const std::vector<syncable::Id>& GetAllCommitIds() const { | 42 const std::vector<syncable::Id>& GetAllCommitIds() const { |
| 43 return commit_ids_; | 43 return commit_ids_; |
| 44 } | 44 } |
| 45 | 45 |
| 46 // Return the Id at index |position| in this OrderedCommitSet. Note that | 46 // Return the Id at index |position| in this OrderedCommitSet. Note that |
| 47 // the index uniquely identifies the same logical item in each of: | 47 // the index uniquely identifies the same logical item in each of: |
| 48 // 1) this OrderedCommitSet | 48 // 1) this OrderedCommitSet |
| 49 // 2) the CommitRequest sent to the server | 49 // 2) the CommitRequest sent to the server |
| 50 // 3) the list of EntryResponse objects in the CommitResponse. | 50 // 3) the list of EntryResponse objects in the CommitResponse. |
| 51 // These together allow re-association of the pre-commit Id with the | 51 // These together allow re-association of the pre-commit Id with the |
| 52 // actual committed entry. | 52 // actual committed entry. |
| 53 const syncable::Id& GetCommitIdAt(const size_t position) const { | 53 const syncable::Id& GetCommitIdAt(const size_t position) const { |
| 54 return commit_ids_[position]; | 54 return commit_ids_[position]; |
| 55 } | 55 } |
| 56 | 56 |
| 57 // Same as above, but for ModelType of the item. | 57 // Same as above, but for ModelType of the item. |
| 58 syncer::ModelType GetModelTypeAt(const size_t position) const { | 58 ModelType GetModelTypeAt(const size_t position) const { |
| 59 return types_[position]; | 59 return types_[position]; |
| 60 } | 60 } |
| 61 | 61 |
| 62 // Get the projection of commit ids onto the space of commit ids | 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 | 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 | 64 // response one ModelSafeGroup at a time. See GetCommitIdAt for how the |
| 65 // indices contained in the returned Projection can be used. | 65 // indices contained in the returned Projection can be used. |
| 66 const Projection& GetCommitIdProjection( | 66 const Projection& GetCommitIdProjection( |
| 67 syncer::ModelSafeGroup group) const; | 67 ModelSafeGroup group) const; |
| 68 | 68 |
| 69 size_t Size() const { | 69 size_t Size() const { |
| 70 return commit_ids_.size(); | 70 return commit_ids_.size(); |
| 71 } | 71 } |
| 72 | 72 |
| 73 bool Empty() const { | 73 bool Empty() const { |
| 74 return Size() == 0; | 74 return Size() == 0; |
| 75 } | 75 } |
| 76 | 76 |
| 77 // Returns true iff any of the commit ids added to this set have model type | 77 // Returns true iff any of the commit ids added to this set have model type |
| 78 // BOOKMARKS. | 78 // BOOKMARKS. |
| 79 bool HasBookmarkCommitId() const; | 79 bool HasBookmarkCommitId() const; |
| 80 | 80 |
| 81 void Append(const OrderedCommitSet& other); | 81 void Append(const OrderedCommitSet& other); |
| 82 void AppendReverse(const OrderedCommitSet& other); | 82 void AppendReverse(const OrderedCommitSet& other); |
| 83 void Truncate(size_t max_size); | 83 void Truncate(size_t max_size); |
| 84 | 84 |
| 85 // Removes all entries from this set. | 85 // Removes all entries from this set. |
| 86 void Clear(); | 86 void Clear(); |
| 87 | 87 |
| 88 void operator=(const OrderedCommitSet& other); | 88 void operator=(const OrderedCommitSet& other); |
| 89 private: | 89 private: |
| 90 // A set of CommitIdProjections associated with particular ModelSafeGroups. | 90 // A set of CommitIdProjections associated with particular ModelSafeGroups. |
| 91 typedef std::map<syncer::ModelSafeGroup, Projection> Projections; | 91 typedef std::map<ModelSafeGroup, Projection> Projections; |
| 92 | 92 |
| 93 // Helper container for return value of GetCommitItemAt. | 93 // Helper container for return value of GetCommitItemAt. |
| 94 struct CommitItem { | 94 struct CommitItem { |
| 95 int64 meta; | 95 int64 meta; |
| 96 syncable::Id id; | 96 syncable::Id id; |
| 97 syncer::ModelType group; | 97 ModelType group; |
| 98 }; | 98 }; |
| 99 | 99 |
| 100 CommitItem GetCommitItemAt(const size_t position) const; | 100 CommitItem GetCommitItemAt(const size_t position) const; |
| 101 | 101 |
| 102 // These lists are different views of the same items; e.g they are | 102 // These lists are different views of the same items; e.g they are |
| 103 // isomorphic. | 103 // isomorphic. |
| 104 std::set<int64> inserted_metahandles_; | 104 std::set<int64> inserted_metahandles_; |
| 105 std::vector<syncable::Id> commit_ids_; | 105 std::vector<syncable::Id> commit_ids_; |
| 106 std::vector<int64> metahandle_order_; | 106 std::vector<int64> metahandle_order_; |
| 107 Projections projections_; | 107 Projections projections_; |
| 108 | 108 |
| 109 // We need this because of operations like AppendReverse that take ids from | 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 | 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 | 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 | 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 | 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. | 114 // and shouldn't take up too much extra space since commit lists are small. |
| 115 std::vector<syncer::ModelType> types_; | 115 std::vector<ModelType> types_; |
| 116 | 116 |
| 117 syncer::ModelSafeRoutingInfo routes_; | 117 ModelSafeRoutingInfo routes_; |
| 118 }; | 118 }; |
| 119 | 119 |
| 120 } // namespace sessions | 120 } // namespace sessions |
| 121 } // namespace syncer | 121 } // namespace syncer |
| 122 | 122 |
| 123 #endif // SYNC_SESSIONS_ORDERED_COMMIT_SET_H_ | 123 #endif // SYNC_SESSIONS_ORDERED_COMMIT_SET_H_ |
| 124 | 124 |
| OLD | NEW |