change_reorder_buffer.h revision 7d4cd473f85ac64c3747c96c277f9e506a0d2246
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// 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 SYNC_INTERNAL_API_CHANGE_REORDER_BUFFER_H_ 10#define SYNC_INTERNAL_API_CHANGE_REORDER_BUFFER_H_ 11 12#include <map> 13#include <vector> 14 15#include "base/compiler_specific.h" 16#include "base/memory/linked_ptr.h" 17#include "sync/internal_api/public/change_record.h" 18#include "sync/protocol/sync.pb.h" 19 20namespace syncer { 21 22class BaseTransaction; 23 24// ChangeReorderBuffer is a utility type which accepts an unordered set 25// of changes (via its Push methods), and yields an ImmutableChangeRecordList 26// (via the GetAllChangesInTreeOrder method) that are in the order that 27// the SyncObserver expects them to be. A buffer is initially empty. 28// 29// The ordering produced by ChangeReorderBuffer is as follows: 30// (a) All Deleted items appear first. 31// (b) For Updated and/or Added items, parents appear before their children. 32// 33// The sibling order is not necessarily preserved. 34class ChangeReorderBuffer { 35 public: 36 ChangeReorderBuffer(); 37 ~ChangeReorderBuffer(); 38 39 // Insert an item, identified by the metahandle |id|, into the reorder buffer. 40 // This item will appear in the output list as an ACTION_ADD ChangeRecord. 41 void PushAddedItem(int64 id); 42 43 // Insert an item, identified by the metahandle |id|, into the reorder buffer. 44 // This item will appear in the output list as an ACTION_DELETE ChangeRecord. 45 void PushDeletedItem(int64 id); 46 47 // Insert an item, identified by the metahandle |id|, into the reorder buffer. 48 // This item will appear in the output list as an ACTION_UPDATE ChangeRecord. 49 void PushUpdatedItem(int64 id); 50 51 void SetExtraDataForId(int64 id, ExtraPasswordChangeRecordData* extra); 52 53 void SetSpecificsForId(int64 id, const sync_pb::EntitySpecifics& specifics); 54 55 // Reset the buffer, forgetting any pushed items, so that it can be used again 56 // to reorder a new set of changes. 57 void Clear(); 58 59 bool IsEmpty() const; 60 61 // Output a reordered list of changes to |changes| using the items 62 // that were pushed into the reorder buffer. |sync_trans| is used to 63 // determine the ordering. Returns true if successful, or false if 64 // an error was encountered. 65 bool GetAllChangesInTreeOrder( 66 const BaseTransaction* sync_trans, 67 ImmutableChangeRecordList* changes) WARN_UNUSED_RESULT; 68 69 private: 70 class Traversal; 71 typedef std::map<int64, ChangeRecord::Action> OperationMap; 72 typedef std::map<int64, sync_pb::EntitySpecifics> SpecificsMap; 73 typedef std::map<int64, linked_ptr<ExtraPasswordChangeRecordData> > 74 ExtraDataMap; 75 76 // Stores the items that have been pushed into the buffer, and the type of 77 // operation that was associated with them. 78 OperationMap operations_; 79 80 // Stores entity-specific ChangeRecord data per-ID. 81 SpecificsMap specifics_; 82 83 // Stores type-specific extra data per-ID. 84 ExtraDataMap extra_data_; 85 86 DISALLOW_COPY_AND_ASSIGN(ChangeReorderBuffer); 87}; 88 89} // namespace syncer 90 91#endif // SYNC_INTERNAL_API_CHANGE_REORDER_BUFFER_H_ 92