SPARK-2248 Update.h should have add/remove/change instead of just add/remove

This commit is contained in:
Eric Harding committed 2019-04-09 09:59:00 -04:00
1 parent b664c42cae
commit 9fd7db55ca
4 files changed
+86 -28

No files matched your search

+30 -17
View File
@@ -5,6 +5,8 @@
#include "ApiException.h"
#include "RawUpdate.h"
#include "ServiceContext.h"
#include "lib/logging/logging.h"
#include <rxcpp/rx.hpp>
#include <functional>
#include <google/protobuf/io/zero_copy_stream_impl_lite.h>
@@ -12,6 +14,17 @@
namespace bts {
namespace api {
template <typename T>
std::shared_ptr<T> deserialize(const char* buffer, size_t length) {
google::protobuf::io::ArrayInputStream stream(buffer, length);
auto item = std::make_shared<T>();
if (item->ParseFromZeroCopyStream(&stream)) {
return item;
}
LOG(Api,Error,"Failed to deserialize proto from api stream. This is likely fatal.");
return nullptr;
}
class ClientChannel
{
int _id;
@@ -80,29 +93,29 @@ public:
}
else {
bool first_update = true;
auto cb = [observer, first_update](EventType event_type, const StreamUpdate* changes, int change_count) mutable {
auto cb = [observer, first_update, method, qualifier](EventType event_type, const StreamUpdate* changes, int change_count) mutable {
if (event_type == EventType::TABLE_UPDATED) {
std::vector<std::shared_ptr<const T>> added_items;
std::vector<std::shared_ptr<const T>> added_items, changed_items;
std::vector<int> added_keys,
removed_keys;
changed_keys,
removed_keys;
for (size_t x = 0; x < change_count; ++x) {
const StreamUpdate& change = changes[x];
if (change.type != StreamUpdate::REMOVE) {
added_keys.reserve(change_count);
added_items.reserve(change_count);
added_keys.push_back(change.key);
google::protobuf::io::ArrayInputStream stream(change.buffer, change.buffer_len);
auto item = std::make_shared<T>();
if (item->ParseFromZeroCopyStream(&stream)) {
added_items.push_back(move(item));
}
}
if (change.type != StreamUpdate::CREATE) {
removed_keys.reserve(change_count);
removed_keys.push_back(change.key);
switch(change.type) {
case StreamUpdate::REMOVE:
removed_keys.push_back(change.key);
break;
case StreamUpdate::REPLACE:
changed_items.push_back(deserialize<T>(change.buffer, change.buffer_len));
changed_keys.push_back(change.key);
break;
case StreamUpdate::CREATE:
added_items.push_back(deserialize<T>(change.buffer, change.buffer_len));
added_keys.push_back(change.key);
break;
}
}
observer.on_next(RawUpdate<std::shared_ptr<const T>>(added_items, added_keys, removed_keys));
observer.on_next(RawUpdate<std::shared_ptr<const T>>(added_items, added_keys, changed_items, changed_keys, removed_keys));
}
else if (event_type == EventType::TABLE_CREATED) {
if (!first_update) {
+12 -2
View File
@@ -6,10 +6,16 @@ namespace api {
template <typename T>
struct RawUpdate {
RawUpdate(std::vector<T>& added_items, std::vector<int>& added_keys, std::vector<int>& removed_keys) :
RawUpdate(std::vector<T>& added_items,
std::vector<int>& added_keys,
std::vector<T>& changed_items,
std::vector<int>& changed_keys,
std::vector<int>& removed_keys) :
is_reset(false),
added_items(std::move(added_items)),
added_keys(std::move(added_keys)),
changed_items(std::move(changed_items)),
changed_keys(std::move(changed_keys)),
removed_keys(std::move(removed_keys))
{}
@@ -20,12 +26,16 @@ namespace api {
bool is_reset;
std::vector<T> added_items;
std::vector<int> added_keys;
std::vector<T> changed_items;
std::vector<int> changed_keys;
std::vector<int> removed_keys;
private:
RawUpdate() :
is_reset(true),
added_items(), added_keys(), removed_keys()
added_items(), added_keys(), changed_items(), changed_keys(), removed_keys()
{}
};
+29 -3
View File
@@ -9,12 +9,21 @@
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& old() { return _old; }
const T& current() { return _new; }
};
template <typename T, typename TKey, typename ServiceType>
struct Update {
Update(std::vector<T>& all, std::vector<T>& added, std::vector<T>& removed) :
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))
{}
@@ -22,17 +31,20 @@ namespace api {
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;
};
@@ -49,9 +61,12 @@ namespace api {
}
state->clear();
std::vector<T> all, added;
return Update<T, TKey, ServiceType>(all, added, removed);
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) {
@@ -61,6 +76,8 @@ namespace api {
state->erase(iter);
}
}
//
// Add
for (size_t x = 0; x < raw.added_keys.size(); ++x) {
(*state)[raw.added_keys[x]] = raw.added_items[x];
}
@@ -69,7 +86,16 @@ namespace api {
for (const auto& item : *state) {
all.push_back(item.second);
}
return Update<T, TKey, ServiceType>(all, raw.added_items, removed);
//
// 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];
}
return Update<T, TKey, ServiceType>(all, raw.added_items, changed, removed);
}
});
};
+15 -6
View File
@@ -86,14 +86,23 @@ MBOServiceServer::MBOServiceServer(
md::MDClient& c
) :
MBOServiceBase(ctx),
_me(new MBOServiceImpl(ctx,d,c))
_me(new MBOServiceImpl(ctx,d,c,make_piq_publisher()))
{
// subscribe to mdclient
// make publishers
// subscribe to orders
// _me = utils::make_unique<MBOServiceImpl>();
// subscribe to mdclient ✔
// make publishers ✔
// subscribe to orders => stash active orders dict
_me->orders_sub =
_me->orders.subscribe_orders().subscribe([=](const auto& update) {
const auto& added = update.added();
const auto& removed = update.removed();
// api thread
_me->dispatcher.post([this,added,removed]() {
// dispatcher thread
// handle add/remove/change
});
});
}
MBOServiceServer::~MBOServiceServer() { }