aboutsummaryrefslogtreecommitdiff
path: root/src/states/state_change_queue.cc
blob: dcb19df3d07296c286ce54ac2b32d7eebb1bf293 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Copyright 2015 The Weave Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "src/states/state_change_queue.h"

#include <base/logging.h>

namespace weave {

StateChangeQueue::StateChangeQueue(size_t max_queue_size)
    : max_queue_size_(max_queue_size) {
  CHECK_GT(max_queue_size_, 0U) << "Max queue size must not be zero";
}

bool StateChangeQueue::NotifyPropertiesUpdated(
    base::Time timestamp,
    const base::DictionaryValue& changed_properties) {
  auto& stored_changes = state_changes_[timestamp];
  // Merge the old property set.
  if (stored_changes)
    stored_changes->MergeDictionary(&changed_properties);
  else
    stored_changes = changed_properties.CreateDeepCopy();

  while (state_changes_.size() > max_queue_size_) {
    // Queue is full.
    // Merge the two oldest records into one. The merge strategy is:
    //  - Move non-existent properties from element [old] to [new].
    //  - If both [old] and [new] specify the same property,
    //    keep the value of [new].
    //  - Keep the timestamp of [new].
    auto element_old = state_changes_.begin();
    auto element_new = std::next(element_old);
    // This will skip elements that exist in both [old] and [new].
    element_old->second->MergeDictionary(element_new->second.get());
    std::swap(element_old->second, element_new->second);
    state_changes_.erase(element_old);
  }
  return true;
}

std::vector<StateChange> StateChangeQueue::GetAndClearRecordedStateChanges() {
  std::vector<StateChange> changes;
  changes.reserve(state_changes_.size());
  for (auto& pair : state_changes_) {
    changes.push_back(StateChange{pair.first, std::move(pair.second)});
  }
  state_changes_.clear();
  return changes;
}

}  // namespace weave