Files
spark/src/lib/api/public/Update.h
T

113 lines
4.1 KiB
C++

#pragma once
#include "RawUpdate.h"
#include <rxcpp/rx.hpp>
#include <functional>
#include <vector>
#include <unordered_map>
namespace bts {
namespace api {
template <typename T>
class Change {
T _old, _new;
public:
Change(const T& old_value, const T& new_value) : _old(old_value), _new(new_value) { }
const T& previous() const { return _old; }
const T& current() const { return _new; }
};
template <typename T, typename TKey, typename ServiceType>
struct Update {
Update(std::vector<T>& all, std::vector<T>& added, std::vector<Change<T>> changed, std::vector<T>& removed) :
_all_items(std::move(all)),
_added_items(std::move(added)),
_changed_items(std::move(changed)),
_removed_items(std::move(removed))
{}
Update(const Update& other) : _all_items(other._all_items),
_added_items(other._added_items),
_changed_items(other._changed_items),
_removed_items(other._removed_items)
{}
Update(Update&& other) :
_all_items(std::move(other._all_items)),
_added_items(std::move(other._added_items)),
_changed_items(std::move(other._changed_items)),
_removed_items(std::move(other._removed_items))
{}
TKey key(const T& t) const { return ServiceType::key(t); }
const std::vector<T>& all() const { return _all_items; }
const std::vector<T>& added() const { return _added_items; }
const std::vector<Change<T>>& changed() const { return _changed_items; }
const std::vector<T>& removed() const { return _removed_items; }
private:
std::vector<T> _all_items;
std::vector<T> _added_items;
std::vector<Change<T>> _changed_items;
std::vector<T> _removed_items;
};
template <typename T, typename TKey, typename ServiceType>
std::function<rxcpp::observable<Update<T,TKey,ServiceType>>(const rxcpp::observable<RawUpdate<T>>&)> reconstitute() {
return [](const rxcpp::observable<RawUpdate<T>>& input) {
auto state = std::make_shared<std::unordered_map<int, T>>();
return input.map([state](RawUpdate<T>& raw) {
if (raw.is_reset) {
std::vector<T> removed;
removed.reserve(state->size());
for (auto& t : *state) {
removed.push_back(std::move(t.second));
}
state->clear();
std::vector<T> all, added;
std::vector<Change<T>> changed;
return Update<T, TKey, ServiceType>(all, added, changed, removed);
}
else {
//
// Remove
std::vector<T> removed;
removed.reserve(raw.removed_keys.size());
for (int key : raw.removed_keys) {
auto iter = state->find(key);
if (iter != state->end()) {
removed.push_back(iter->second);
state->erase(iter);
}
}
//
// Add
for (size_t x = 0; x < raw.added_keys.size(); ++x) {
(*state)[raw.added_keys[x]] = raw.added_items[x];
}
//
// Change
std::vector<Change<T>> changed;
changed.reserve(raw.changed_keys.size());
for (size_t x = 0; x < raw.changed_keys.size(); ++x) {
changed.push_back(Change<T>{(*state)[raw.changed_keys[x]], raw.changed_items[x]});
(*state)[raw.changed_keys[x]] = raw.changed_items[x];
}
//
// All
std::vector<T> all;
all.reserve(state->size());
for (const auto& item : *state) {
all.push_back(item.second);
}
return Update<T, TKey, ServiceType>(all, raw.added_items, changed, removed);
}
});
};
}
}}//namespace