Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS 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 | |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 5 | #include "buffet/states/state_change_queue.h" |
| 6 | |
Alex Deymo | f6cbe32 | 2014-11-10 19:55:35 -0800 | [diff] [blame] | 7 | #include <base/logging.h> |
| 8 | |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 9 | namespace buffet { |
| 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 | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 16 | bool StateChangeQueue::NotifyPropertiesUpdated( |
| 17 | base::Time timestamp, |
Anton Muhin | 0182945 | 2014-11-21 02:16:04 +0400 | [diff] [blame] | 18 | native_types::Object changed_properties) { |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 19 | DCHECK(thread_checker_.CalledOnValidThread()); |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 20 | auto it = state_changes_.lower_bound(timestamp); |
| 21 | if (it == state_changes_.end() || it->first != timestamp) { |
| 22 | // This timestamp doesn't exist. Insert a new element. |
| 23 | state_changes_.emplace_hint(it, timestamp, std::move(changed_properties)); |
| 24 | } else { |
| 25 | // Merge the old property set and the new one. |
| 26 | // For properties that exist in both old and new property sets, keep the |
| 27 | // new values. |
| 28 | changed_properties.insert(it->second.begin(), it->second.end()); |
| 29 | it->second = std::move(changed_properties); |
| 30 | } |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 31 | while (state_changes_.size() > max_queue_size_) { |
| 32 | // Queue is full. |
| 33 | // Merge the two oldest records into one. The merge strategy is: |
| 34 | // - Move non-existent properties from element [old] to [new]. |
| 35 | // - If both [old] and [new] specify the same property, |
| 36 | // keep the value of [new]. |
| 37 | // - Keep the timestamp of [new]. |
| 38 | auto element_old = state_changes_.begin(); |
| 39 | auto element_new = std::next(element_old); |
| 40 | // This will skip elements that exist in both [old] and [new]. |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 41 | element_new->second.insert(element_old->second.begin(), |
| 42 | element_old->second.end()); |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 43 | state_changes_.erase(element_old); |
| 44 | } |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | std::vector<StateChange> StateChangeQueue::GetAndClearRecordedStateChanges() { |
| 49 | DCHECK(thread_checker_.CalledOnValidThread()); |
Alex Vakulenko | ff73cf2 | 2014-10-29 09:53:52 -0700 | [diff] [blame] | 50 | std::vector<StateChange> changes; |
| 51 | changes.reserve(state_changes_.size()); |
| 52 | for (const auto& pair : state_changes_) { |
| 53 | changes.emplace_back(pair.first, std::move(pair.second)); |
| 54 | } |
| 55 | state_changes_.clear(); |
| 56 | return changes; |
Alex Vakulenko | 57123b2 | 2014-10-28 13:50:16 -0700 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | } // namespace buffet |