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