| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // Defines ChangeReorderBuffer, which can be used to sort a list of item | 5 // Defines ChangeReorderBuffer, which can be used to sort a list of item |
| 6 // actions to achieve the ordering constraint required by the SyncObserver | 6 // actions to achieve the ordering constraint required by the SyncObserver |
| 7 // interface of the SyncAPI. | 7 // interface of the SyncAPI. |
| 8 | 8 |
| 9 #ifndef CHROME_BROWSER_SYNC_INTERNAL_API_CHANGE_REORDER_BUFFER_H_ | 9 #ifndef CHROME_BROWSER_SYNC_INTERNAL_API_CHANGE_REORDER_BUFFER_H_ |
| 10 #define CHROME_BROWSER_SYNC_INTERNAL_API_CHANGE_REORDER_BUFFER_H_ | 10 #define CHROME_BROWSER_SYNC_INTERNAL_API_CHANGE_REORDER_BUFFER_H_ |
| 11 #pragma once | 11 #pragma once |
| 12 | 12 |
| 13 #include <map> | 13 #include <map> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/memory/linked_ptr.h" | 16 #include "base/memory/linked_ptr.h" |
| 17 #include "chrome/browser/sync/internal_api/base_transaction.h" | 17 #include "chrome/browser/sync/internal_api/base_transaction.h" |
| 18 #include "chrome/browser/sync/internal_api/sync_manager.h" | 18 #include "chrome/browser/sync/internal_api/change_record.h" |
| 19 #include "chrome/browser/sync/protocol/sync.pb.h" | 19 #include "chrome/browser/sync/protocol/sync.pb.h" |
| 20 | 20 |
| 21 namespace sync_api { | 21 namespace sync_api { |
| 22 | 22 |
| 23 // ChangeReorderBuffer is a utility type which accepts an unordered set | 23 // ChangeReorderBuffer is a utility type which accepts an unordered set |
| 24 // of changes (via its Push methods), and yields a vector of ChangeRecords | 24 // of changes (via its Push methods), and yields an ImmutableChangeRecordList |
| 25 // (via the GetAllChangesInTreeOrder method) that are in the order that | 25 // (via the GetAllChangesInTreeOrder method) that are in the order that |
| 26 // the SyncObserver expects them to be. A buffer is initially empty. | 26 // the SyncObserver expects them to be. A buffer is initially empty. |
| 27 // | 27 // |
| 28 // The ordering produced by ChangeReorderBuffer is as follows: | 28 // The ordering produced by ChangeReorderBuffer is as follows: |
| 29 // (a) All Deleted items appear first. | 29 // (a) All Deleted items appear first. |
| 30 // (b) For Updated and/or Added items, parents appear before their children. | 30 // (b) For Updated and/or Added items, parents appear before their children. |
| 31 // (c) When there are changes to the sibling order (this means Added items, | 31 // (c) When there are changes to the sibling order (this means Added items, |
| 32 // or Updated items with the |position_changed| parameter set to true), | 32 // or Updated items with the |position_changed| parameter set to true), |
| 33 // all siblings under a parent will appear in the output, even if they | 33 // all siblings under a parent will appear in the output, even if they |
| 34 // are not explicitly pushed. The sibling order will be preserved in | 34 // are not explicitly pushed. The sibling order will be preserved in |
| 35 // the output list -- items will appear before their sibling-order | 35 // the output list -- items will appear before their sibling-order |
| 36 // successors. | 36 // successors. |
| 37 // (d) When there are no changes to the sibling order under a parent node, | 37 // (d) When there are no changes to the sibling order under a parent node, |
| 38 // the sibling order is not necessarily preserved in the output for | 38 // the sibling order is not necessarily preserved in the output for |
| 39 // its children. | 39 // its children. |
| 40 class ChangeReorderBuffer { | 40 class ChangeReorderBuffer { |
| 41 public: | 41 public: |
| 42 typedef SyncManager::ChangeRecord ChangeRecord; | |
| 43 typedef SyncManager::ExtraPasswordChangeRecordData ExtraChangeRecordData; | |
| 44 | |
| 45 ChangeReorderBuffer(); | 42 ChangeReorderBuffer(); |
| 46 ~ChangeReorderBuffer(); | 43 ~ChangeReorderBuffer(); |
| 47 | 44 |
| 48 // Insert an item, identified by the metahandle |id|, into the reorder | 45 // Insert an item, identified by the metahandle |id|, into the reorder |
| 49 // buffer. This item will appear in the output list as an ACTION_ADD | 46 // buffer. This item will appear in the output list as an ACTION_ADD |
| 50 // ChangeRecord. | 47 // ChangeRecord. |
| 51 void PushAddedItem(int64 id) { | 48 void PushAddedItem(int64 id) { |
| 52 operations_[id] = OP_ADD; | 49 operations_[id] = OP_ADD; |
| 53 } | 50 } |
| 54 | 51 |
| 55 // Insert an item, identified by the metahandle |id|, into the reorder | 52 // Insert an item, identified by the metahandle |id|, into the reorder |
| 56 // buffer. This item will appear in the output list as an ACTION_DELETE | 53 // buffer. This item will appear in the output list as an ACTION_DELETE |
| 57 // ChangeRecord. | 54 // ChangeRecord. |
| 58 void PushDeletedItem(int64 id) { | 55 void PushDeletedItem(int64 id) { |
| 59 operations_[id] = OP_DELETE; | 56 operations_[id] = OP_DELETE; |
| 60 } | 57 } |
| 61 | 58 |
| 62 // Insert an item, identified by the metahandle |id|, into the reorder | 59 // Insert an item, identified by the metahandle |id|, into the reorder |
| 63 // buffer. This item will appear in the output list as an ACTION_UPDATE | 60 // buffer. This item will appear in the output list as an ACTION_UPDATE |
| 64 // ChangeRecord. Also, if |position_changed| is true, all siblings of this | 61 // ChangeRecord. Also, if |position_changed| is true, all siblings of this |
| 65 // item will appear in the output list as well; if it wasn't explicitly | 62 // item will appear in the output list as well; if it wasn't explicitly |
| 66 // pushed, the siblings will have an ACTION_UPDATE ChangeRecord. | 63 // pushed, the siblings will have an ACTION_UPDATE ChangeRecord. |
| 67 void PushUpdatedItem(int64 id, bool position_changed) { | 64 void PushUpdatedItem(int64 id, bool position_changed) { |
| 68 operations_[id] = position_changed ? OP_UPDATE_POSITION_AND_PROPERTIES : | 65 operations_[id] = position_changed ? OP_UPDATE_POSITION_AND_PROPERTIES : |
| 69 OP_UPDATE_PROPERTIES_ONLY; | 66 OP_UPDATE_PROPERTIES_ONLY; |
| 70 } | 67 } |
| 71 | 68 |
| 72 void SetExtraDataForId(int64 id, ExtraChangeRecordData* extra) { | 69 void SetExtraDataForId(int64 id, ExtraPasswordChangeRecordData* extra) { |
| 73 extra_data_[id] = make_linked_ptr<ExtraChangeRecordData>(extra); | 70 extra_data_[id] = make_linked_ptr<ExtraPasswordChangeRecordData>(extra); |
| 74 } | 71 } |
| 75 | 72 |
| 76 void SetSpecificsForId(int64 id, const sync_pb::EntitySpecifics& specifics) { | 73 void SetSpecificsForId(int64 id, const sync_pb::EntitySpecifics& specifics) { |
| 77 specifics_[id] = specifics; | 74 specifics_[id] = specifics; |
| 78 } | 75 } |
| 79 | 76 |
| 80 // Reset the buffer, forgetting any pushed items, so that it can be used | 77 // Reset the buffer, forgetting any pushed items, so that it can be used |
| 81 // again to reorder a new set of changes. | 78 // again to reorder a new set of changes. |
| 82 void Clear() { | 79 void Clear() { |
| 83 operations_.clear(); | 80 operations_.clear(); |
| 84 } | 81 } |
| 85 | 82 |
| 86 bool IsEmpty() const { | 83 bool IsEmpty() const { |
| 87 return operations_.empty(); | 84 return operations_.empty(); |
| 88 } | 85 } |
| 89 | 86 |
| 90 // Output a reordered list of changes to |changelist| using the items that | 87 // Output a reordered list of changes to |changelist| using the items that |
| 91 // were pushed into the reorder buffer. |sync_trans| is used to determine the | 88 // were pushed into the reorder buffer. |sync_trans| is used to determine the |
| 92 // ordering. | 89 // ordering. |
| 93 void GetAllChangesInTreeOrder(const BaseTransaction* sync_trans, | 90 ImmutableChangeRecordList |
| 94 std::vector<ChangeRecord>* changelist); | 91 GetAllChangesInTreeOrder(const BaseTransaction* sync_trans); |
| 95 | 92 |
| 96 private: | 93 private: |
| 97 class Traversal; | 94 class Traversal; |
| 98 enum Operation { | 95 enum Operation { |
| 99 OP_ADD, // AddedItem. | 96 OP_ADD, // AddedItem. |
| 100 OP_DELETE, // DeletedItem. | 97 OP_DELETE, // DeletedItem. |
| 101 OP_UPDATE_PROPERTIES_ONLY, // UpdatedItem with position_changed=0. | 98 OP_UPDATE_PROPERTIES_ONLY, // UpdatedItem with position_changed=0. |
| 102 OP_UPDATE_POSITION_AND_PROPERTIES, // UpdatedItem with position_changed=1. | 99 OP_UPDATE_POSITION_AND_PROPERTIES, // UpdatedItem with position_changed=1. |
| 103 }; | 100 }; |
| 104 typedef std::map<int64, Operation> OperationMap; | 101 typedef std::map<int64, Operation> OperationMap; |
| 105 typedef std::map<int64, sync_pb::EntitySpecifics> SpecificsMap; | 102 typedef std::map<int64, sync_pb::EntitySpecifics> SpecificsMap; |
| 106 typedef std::map<int64, linked_ptr<ExtraChangeRecordData> > ExtraDataMap; | 103 typedef std::map<int64, linked_ptr<ExtraPasswordChangeRecordData> > |
| 104 ExtraDataMap; |
| 107 | 105 |
| 108 // Stores the items that have been pushed into the buffer, and the type of | 106 // Stores the items that have been pushed into the buffer, and the type of |
| 109 // operation that was associated with them. | 107 // operation that was associated with them. |
| 110 OperationMap operations_; | 108 OperationMap operations_; |
| 111 | 109 |
| 112 // Stores entity-specific ChangeRecord data per-ID. | 110 // Stores entity-specific ChangeRecord data per-ID. |
| 113 SpecificsMap specifics_; | 111 SpecificsMap specifics_; |
| 114 | 112 |
| 115 // Stores type-specific extra data per-ID. | 113 // Stores type-specific extra data per-ID. |
| 116 ExtraDataMap extra_data_; | 114 ExtraDataMap extra_data_; |
| 117 | 115 |
| 118 DISALLOW_COPY_AND_ASSIGN(ChangeReorderBuffer); | 116 DISALLOW_COPY_AND_ASSIGN(ChangeReorderBuffer); |
| 119 }; | 117 }; |
| 120 | 118 |
| 121 } // namespace sync_api | 119 } // namespace sync_api |
| 122 | 120 |
| 123 #endif // CHROME_BROWSER_SYNC_INTERNAL_API_CHANGE_REORDER_BUFFER_H_ | 121 #endif // CHROME_BROWSER_SYNC_INTERNAL_API_CHANGE_REORDER_BUFFER_H_ |
| OLD | NEW |