Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 5 | #include "src/states/state_change_queue.h" |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 6 | |
Alex Deymo | f6cbe32 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 7 | #include <base/logging.h> |
Alex Vakulenko | 8a05beb | 2015-11-24 17:13:20 -0800 | [diff] [blame] | 8 | #include <base/values.h> |
Alex Deymo | f6cbe32 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 9 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 10 | namespace weave { |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 11 | |
| 12 | StateChangeQueue::StateChangeQueue(size_t max_queue_size) |
| 13 | : max_queue_size_(max_queue_size) { |
Mike Frysinger | 42e3a72 | 2014-11-15 06:48:08 -0500 | [diff] [blame] | 14 | CHECK_GT(max_queue_size_, 0U) << "Max queue size must not be zero"; |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 15 | } |
| 16 | |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 17 | bool StateChangeQueue::NotifyPropertiesUpdated( |
| 18 | base::Time timestamp, |
| 19 | std::unique_ptr<base::DictionaryValue> changed_properties) { |
Vitaly Buka | 70b697f | 2015-07-28 14:36:29 -0700 | [diff] [blame] | 20 | auto& stored_changes = state_changes_[timestamp]; |
| 21 | // Merge the old property set. |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 22 | if (stored_changes) |
| 23 | stored_changes->MergeDictionary(changed_properties.get()); |
| 24 | else |
| 25 | stored_changes = std::move(changed_properties); |
Vitaly Buka | 70b697f | 2015-07-28 14:36:29 -0700 | [diff] [blame] | 26 | |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 27 | while (state_changes_.size() > max_queue_size_) { |
| 28 | // Queue is full. |
| 29 | // Merge the two oldest records into one. The merge strategy is: |
| 30 | // - Move non-existent properties from element [old] to [new]. |
| 31 | // - If both [old] and [new] specify the same property, |
| 32 | // keep the value of [new]. |
| 33 | // - Keep the timestamp of [new]. |
| 34 | auto element_old = state_changes_.begin(); |
| 35 | auto element_new = std::next(element_old); |
| 36 | // This will skip elements that exist in both [old] and [new]. |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 37 | element_old->second->MergeDictionary(element_new->second.get()); |
| 38 | std::swap(element_old->second, element_new->second); |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 39 | state_changes_.erase(element_old); |
| 40 | } |
Alex Vakulenko | e7a7007 | 2015-06-25 11:36:07 -0700 | [diff] [blame] | 41 | ++last_change_id_; |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 42 | return true; |
| 43 | } |
| 44 | |
| 45 | std::vector<StateChange> StateChangeQueue::GetAndClearRecordedStateChanges() { |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 46 | std::vector<StateChange> changes; |
| 47 | changes.reserve(state_changes_.size()); |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 48 | for (auto& pair : state_changes_) { |
| 49 | changes.push_back(StateChange{pair.first, std::move(pair.second)}); |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 50 | } |
| 51 | state_changes_.clear(); |
| 52 | return changes; |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 53 | } |
| 54 | |
Alex Vakulenko | be4254b | 2015-06-26 11:34:03 -0700 | [diff] [blame] | 55 | StateChangeQueueInterface::Token StateChangeQueue::AddOnStateUpdatedCallback( |
| 56 | const base::Callback<void(UpdateID)>& callback) { |
| 57 | if (state_changes_.empty()) |
| 58 | callback.Run(last_change_id_); |
| 59 | return Token{callbacks_.Add(callback).release()}; |
| 60 | } |
| 61 | |
| 62 | void StateChangeQueue::NotifyStateUpdatedOnServer(UpdateID update_id) { |
| 63 | callbacks_.Notify(update_id); |
| 64 | } |
| 65 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 66 | } // namespace weave |