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> |
| 8 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 9 | namespace weave { |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 10 | |
| 11 | StateChangeQueue::StateChangeQueue(size_t max_queue_size) |
| 12 | : max_queue_size_(max_queue_size) { |
Mike Frysinger | 42e3a72 | 2014-11-15 06:48:08 -0500 | [diff] [blame] | 13 | 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] | 14 | } |
| 15 | |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 16 | bool StateChangeQueue::NotifyPropertiesUpdated( |
| 17 | base::Time timestamp, |
Alex Vakulenko | 6869ed7 | 2015-12-04 13:59:23 -0800 | [diff] [blame] | 18 | const base::DictionaryValue& changed_properties) { |
Vitaly Buka | 70b697f | 2015-07-28 14:36:29 -0700 | [diff] [blame] | 19 | auto& stored_changes = state_changes_[timestamp]; |
| 20 | // Merge the old property set. |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 21 | if (stored_changes) |
Alex Vakulenko | 6869ed7 | 2015-12-04 13:59:23 -0800 | [diff] [blame] | 22 | stored_changes->MergeDictionary(&changed_properties); |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 23 | else |
Alex Vakulenko | 6869ed7 | 2015-12-04 13:59:23 -0800 | [diff] [blame] | 24 | stored_changes.reset(changed_properties.DeepCopy()); |
Vitaly Buka | 70b697f | 2015-07-28 14:36:29 -0700 | [diff] [blame] | 25 | |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 26 | while (state_changes_.size() > max_queue_size_) { |
| 27 | // Queue is full. |
| 28 | // Merge the two oldest records into one. The merge strategy is: |
| 29 | // - Move non-existent properties from element [old] to [new]. |
| 30 | // - If both [old] and [new] specify the same property, |
| 31 | // keep the value of [new]. |
| 32 | // - Keep the timestamp of [new]. |
| 33 | auto element_old = state_changes_.begin(); |
| 34 | auto element_new = std::next(element_old); |
| 35 | // This will skip elements that exist in both [old] and [new]. |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 36 | element_old->second->MergeDictionary(element_new->second.get()); |
| 37 | std::swap(element_old->second, element_new->second); |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 38 | state_changes_.erase(element_old); |
| 39 | } |
| 40 | return true; |
| 41 | } |
| 42 | |
| 43 | std::vector<StateChange> StateChangeQueue::GetAndClearRecordedStateChanges() { |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 44 | std::vector<StateChange> changes; |
| 45 | changes.reserve(state_changes_.size()); |
Alex Vakulenko | 7d66921 | 2015-11-23 16:05:24 -0800 | [diff] [blame] | 46 | for (auto& pair : state_changes_) { |
| 47 | changes.push_back(StateChange{pair.first, std::move(pair.second)}); |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 48 | } |
| 49 | state_changes_.clear(); |
| 50 | return changes; |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 51 | } |
| 52 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 53 | } // namespace weave |