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 #ifndef CHROME_BROWSER_SYNC_ENGINE_GET_COMMIT_IDS_COMMAND_H_ |
| 6 #define CHROME_BROWSER_SYNC_ENGINE_GET_COMMIT_IDS_COMMAND_H_ |
| 7 |
| 8 #include <vector> |
| 9 #include <utility> |
| 10 |
| 11 #include "chrome/browser/sync/engine/syncer_command.h" |
| 12 #include "chrome/browser/sync/engine/syncer_util.h" |
| 13 #include "chrome/browser/sync/engine/syncer_session.h" |
| 14 #include "chrome/browser/sync/util/sync_types.h" |
| 15 |
| 16 using std::pair; |
| 17 using std::vector; |
| 18 |
| 19 namespace browser_sync { |
| 20 |
| 21 class GetCommitIdsCommand : public SyncerCommand { |
| 22 friend class SyncerTest; |
| 23 |
| 24 public: |
| 25 explicit GetCommitIdsCommand(int commit_batch_size); |
| 26 virtual ~GetCommitIdsCommand(); |
| 27 |
| 28 virtual void ExecuteImpl(SyncerSession *session); |
| 29 |
| 30 // Returns a vector of IDs that should be committed. |
| 31 void BuildCommitIds(SyncerSession *session); |
| 32 |
| 33 // These classes are public for testing. |
| 34 // TODO(ncarter): This code is more generic than just Commit and can |
| 35 // be reused elsewhere (e.g. PositionalRunBuilder, ChangeReorderBuffer |
| 36 // do similar things). Merge all these implementations. |
| 37 class OrderedCommitSet { |
| 38 public: |
| 39 // TODO(chron): Reserve space according to batch size? |
| 40 OrderedCommitSet() {} |
| 41 ~OrderedCommitSet() {} |
| 42 |
| 43 bool HaveCommitItem(const int64 metahandle) const { |
| 44 return inserted_metahandles_.count(metahandle) > 0; |
| 45 } |
| 46 |
| 47 void AddCommitItem(const int64 metahandle, const syncable::Id& commit_id) { |
| 48 if (!HaveCommitItem(metahandle)) { |
| 49 inserted_metahandles_.insert(metahandle); |
| 50 metahandle_order_.push_back(metahandle); |
| 51 commit_ids_.push_back(commit_id); |
| 52 } |
| 53 } |
| 54 |
| 55 const vector<syncable::Id>& GetCommitIds() const { |
| 56 return commit_ids_; |
| 57 } |
| 58 |
| 59 pair<int64, syncable::Id> GetCommitItemAt(const int position) const { |
| 60 DCHECK(position < Size()); |
| 61 return pair<int64, syncable::Id> ( |
| 62 metahandle_order_[position], commit_ids_[position]); |
| 63 } |
| 64 |
| 65 int Size() const { |
| 66 return commit_ids_.size(); |
| 67 } |
| 68 |
| 69 void AppendReverse(const OrderedCommitSet& other) { |
| 70 for (int i = other.Size() - 1; i >= 0; i--) { |
| 71 pair<int64, syncable::Id> item = other.GetCommitItemAt(i); |
| 72 AddCommitItem(item.first, item.second); |
| 73 } |
| 74 } |
| 75 |
| 76 void Truncate(size_t max_size) { |
| 77 if (max_size < metahandle_order_.size()) { |
| 78 for (size_t i = max_size; i < metahandle_order_.size(); ++i) { |
| 79 inserted_metahandles_.erase(metahandle_order_[i]); |
| 80 } |
| 81 commit_ids_.resize(max_size); |
| 82 metahandle_order_.resize(max_size); |
| 83 } |
| 84 } |
| 85 |
| 86 private: |
| 87 // These three lists are different views of the same data; e.g they are |
| 88 // isomorphic. |
| 89 syncable::MetahandleSet inserted_metahandles_; |
| 90 vector<syncable::Id> commit_ids_; |
| 91 vector<int64> metahandle_order_; |
| 92 |
| 93 DISALLOW_COPY_AND_ASSIGN(OrderedCommitSet); |
| 94 }; |
| 95 |
| 96 |
| 97 // TODO(chron): Remove writes from this iterator. As a warning, this |
| 98 // iterator causes writes to entries and so isn't a pure iterator. |
| 99 // It will do Put(IS_UNSYNCED) as well as add things to the blocked |
| 100 // session list. Refactor this out later. |
| 101 class CommitMetahandleIterator { |
| 102 public: |
| 103 // TODO(chron): Cache ValidateCommitEntry responses across iterators to save |
| 104 // UTF8 conversion and filename checking |
| 105 CommitMetahandleIterator(SyncerSession* session, |
| 106 OrderedCommitSet* commit_set) |
| 107 : session_(session), |
| 108 commit_set_(commit_set) { |
| 109 handle_iterator_ = session->unsynced_handles().begin(); |
| 110 |
| 111 // TODO(chron): Remove writes from this iterator. |
| 112 DCHECK(session->has_open_write_transaction()); |
| 113 |
| 114 if (Valid() && !ValidateMetahandleForCommit(*handle_iterator_)) |
| 115 Increment(); |
| 116 } |
| 117 ~CommitMetahandleIterator() {} |
| 118 |
| 119 int64 Current() const { |
| 120 DCHECK(Valid()); |
| 121 return *handle_iterator_; |
| 122 } |
| 123 |
| 124 bool Increment() { |
| 125 if (!Valid()) |
| 126 return false; |
| 127 |
| 128 for (++handle_iterator_; |
| 129 handle_iterator_ != session_->unsynced_handles().end(); |
| 130 ++handle_iterator_) { |
| 131 if (ValidateMetahandleForCommit(*handle_iterator_)) |
| 132 return true; |
| 133 } |
| 134 |
| 135 return false; |
| 136 } |
| 137 |
| 138 bool Valid() const { |
| 139 return !(handle_iterator_ == session_->unsynced_handles().end()); |
| 140 } |
| 141 |
| 142 private: |
| 143 bool ValidateMetahandleForCommit(int64 metahandle) { |
| 144 if (commit_set_->HaveCommitItem(metahandle)) |
| 145 return false; |
| 146 |
| 147 // We should really not WRITE in this iterator, but we can fix that |
| 148 // later. ValidateCommitEntry writes to the DB, and we add the |
| 149 // blocked items. We should move that somewhere else later. |
| 150 syncable::MutableEntry entry(session_->write_transaction(), |
| 151 syncable::GET_BY_HANDLE, metahandle); |
| 152 VerifyCommitResult verify_result = |
| 153 SyncerUtil::ValidateCommitEntry(&entry); |
| 154 if (verify_result == VERIFY_BLOCKED) { |
| 155 session_->AddBlockedItem(entry.Get(syncable::ID)); // TODO(chron): Ew. |
| 156 } else if (verify_result == VERIFY_UNSYNCABLE) { |
| 157 // drop unsyncable entries. |
| 158 entry.Put(syncable::IS_UNSYNCED, false); |
| 159 } |
| 160 return verify_result == VERIFY_OK; |
| 161 } |
| 162 |
| 163 SyncerSession* session_; |
| 164 vector<int64>::const_iterator handle_iterator_; |
| 165 OrderedCommitSet* commit_set_; |
| 166 |
| 167 DISALLOW_COPY_AND_ASSIGN(CommitMetahandleIterator); |
| 168 }; |
| 169 |
| 170 private: |
| 171 void AddUncommittedParentsAndTheirPredecessors( |
| 172 syncable::BaseTransaction* trans, |
| 173 syncable::Id parent_id); |
| 174 |
| 175 // OrderedCommitSet helpers for adding predecessors in order. |
| 176 // TODO(ncarter): Refactor these so that the |result| parameter goes |
| 177 // away, and AddItem doesn't need to consider two OrderedCommitSets. |
| 178 bool AddItem(syncable::Entry* item, OrderedCommitSet* result); |
| 179 bool AddItemThenPredecessors(syncable::BaseTransaction* trans, |
| 180 syncable::Entry* item, |
| 181 syncable::IndexedBitField inclusion_filter, |
| 182 OrderedCommitSet* result); |
| 183 void AddPredecessorsThenItem(syncable::BaseTransaction* trans, |
| 184 syncable::Entry* item, |
| 185 syncable::IndexedBitField inclusion_filter); |
| 186 |
| 187 bool IsCommitBatchFull(); |
| 188 |
| 189 void AddCreatesAndMoves(SyncerSession *session); |
| 190 |
| 191 void AddDeletes(SyncerSession *session); |
| 192 |
| 193 OrderedCommitSet ordered_commit_set_; |
| 194 |
| 195 int requested_commit_batch_size_; |
| 196 |
| 197 DISALLOW_COPY_AND_ASSIGN(GetCommitIdsCommand); |
| 198 }; |
| 199 |
| 200 } // namespace browser_sync |
| 201 |
| 202 #endif // CHROME_BROWSER_SYNC_ENGINE_GET_COMMIT_IDS_COMMAND_H_ |
OLD | NEW |