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 #ifndef CHROME_BROWSER_SYNC_SESSIONS_ORDERED_COMMIT_SET_H_ | |
6 #define CHROME_BROWSER_SYNC_SESSIONS_ORDERED_COMMIT_SET_H_ | |
7 #pragma once | |
8 | |
9 #include <map> | |
10 #include <set> | |
11 #include <vector> | |
12 | |
13 #include "chrome/browser/sync/engine/model_safe_worker.h" | |
14 #include "chrome/browser/sync/syncable/model_type.h" | |
15 #include "chrome/browser/sync/syncable/syncable_id.h" | |
16 | |
17 namespace browser_sync { | |
18 namespace sessions { | |
19 | |
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 OrderedCommitSet { | |
24 public: | |
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; | |
31 | |
32 // TODO(chron): Reserve space according to batch size? | |
33 explicit OrderedCommitSet(const browser_sync::ModelSafeRoutingInfo& routes); | |
34 ~OrderedCommitSet(); | |
35 | |
36 bool HaveCommitItem(const int64 metahandle) const { | |
37 return inserted_metahandles_.count(metahandle) > 0; | |
38 } | |
39 | |
40 void AddCommitItem(const int64 metahandle, const syncable::Id& commit_id, | |
41 syncable::ModelType type); | |
42 | |
43 const std::vector<syncable::Id>& GetAllCommitIds() const { | |
44 return commit_ids_; | |
45 } | |
46 | |
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]; | |
56 } | |
57 | |
58 // Same as above, but for ModelType of the item. | |
59 syncable::ModelType GetModelTypeAt(const size_t position) const { | |
60 return types_[position]; | |
61 } | |
62 | |
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(browser_sync::ModelSafeGroup group) { | |
68 return projections_[group]; | |
69 } | |
70 | |
71 int Size() const { | |
72 return commit_ids_.size(); | |
73 } | |
74 | |
75 // Returns true iff any of the commit ids added to this set have model type | |
76 // BOOKMARKS. | |
77 bool HasBookmarkCommitId() const; | |
78 | |
79 void Append(const OrderedCommitSet& other); | |
80 void AppendReverse(const OrderedCommitSet& other); | |
81 void Truncate(size_t max_size); | |
82 | |
83 void operator=(const OrderedCommitSet& other); | |
84 private: | |
85 // A set of CommitIdProjections associated with particular ModelSafeGroups. | |
86 typedef std::map<browser_sync::ModelSafeGroup, Projection> Projections; | |
87 | |
88 // Helper container for return value of GetCommitItemAt. | |
89 struct CommitItem { | |
90 int64 meta; | |
91 syncable::Id id; | |
92 syncable::ModelType group; | |
93 }; | |
94 | |
95 CommitItem GetCommitItemAt(const int position) const; | |
96 | |
97 // These lists are different views of the same items; e.g they are | |
98 // isomorphic. | |
99 std::set<int64> inserted_metahandles_; | |
100 std::vector<syncable::Id> commit_ids_; | |
101 std::vector<int64> metahandle_order_; | |
102 Projections projections_; | |
103 | |
104 // We need this because of operations like AppendReverse that take ids from | |
105 // one OrderedCommitSet and insert into another -- we need to know the | |
106 // group for each ID so that the insertion can update the appropriate | |
107 // projection. We could store it in commit_ids_, but sometimes we want | |
108 // to just return the vector of Ids, so this is more straightforward | |
109 // and shouldn't take up too much extra space since commit lists are small. | |
110 std::vector<syncable::ModelType> types_; | |
111 | |
112 browser_sync::ModelSafeRoutingInfo routes_; | |
113 }; | |
114 | |
115 } // namespace sessions | |
116 } // namespace browser_sync | |
117 | |
118 #endif // CHROME_BROWSER_SYNC_SESSIONS_ORDERED_COMMIT_SET_H_ | |
119 | |
OLD | NEW |