Modified StatefulPublisher to optionally have state, and piggybacking on for state
This commit is contained in:
1 parent
e7dfe4f8ce
commit
399e1a6c33
4 files changed
+146
-138
No files matched your search
@@ -10,24 +10,29 @@ namespace api {
|
||||
|
||||
template <class T, class TKey>
|
||||
class StatefulPublisher {
|
||||
const bool _keep_state;
|
||||
std::vector<T> _added;
|
||||
std::vector<T> _updated;
|
||||
std::set<TKey> _erased;
|
||||
std::unordered_set<TKey> _state;
|
||||
std::map<TKey,T> _state_data;
|
||||
typedef std::function<void(std::vector<T>*, std::vector<T>*, std::set<TKey>*)> PublishFn;
|
||||
const PublishFn _publish;
|
||||
typedef std::function<TKey(T)> KeyFn;
|
||||
const KeyFn _key;
|
||||
public:
|
||||
StatefulPublisher(const PublishFn& fn, const KeyFn& key) :
|
||||
_added(), _updated(), _erased(), _state(), _publish(fn), _key(key)
|
||||
StatefulPublisher(const PublishFn& fn, const KeyFn& key,bool keep_state=false) :
|
||||
_keep_state(keep_state), _added(), _updated(), _erased(), _state(), _state_data(), _publish(fn), _key(key)
|
||||
{ }
|
||||
|
||||
void add_or_update(const T& m) {
|
||||
auto key = _key(m);
|
||||
if (_state.find(key) == _state.end()) {
|
||||
if (!_has_key(key)) {
|
||||
_added.push_back(m);
|
||||
_state.insert(key);
|
||||
if(_keep_state)
|
||||
_state_data[key]=m;
|
||||
else
|
||||
_state.insert(key);
|
||||
}
|
||||
else {
|
||||
// If an item has already been added update to new value
|
||||
@@ -48,7 +53,7 @@ class StatefulPublisher {
|
||||
}
|
||||
}
|
||||
void erase(TKey m) {
|
||||
if (_state.find(m) != _state.end()) {
|
||||
if (_has_key(m)) {
|
||||
_erased.insert(m);
|
||||
}
|
||||
}
|
||||
@@ -58,6 +63,15 @@ class StatefulPublisher {
|
||||
_updated.clear();
|
||||
_erased.clear();
|
||||
}
|
||||
const std::map<TKey,T>& state_data() const{
|
||||
return _state_data;
|
||||
}
|
||||
private:
|
||||
bool _has_key(TKey k) const{
|
||||
return _keep_state ? _state_data.find(k) != _state_data.end() :
|
||||
_state.find(k) != _state.end();
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
}}//namespace
|
||||
@@ -10,9 +10,8 @@ import "google/protobuf/timestamp.proto";
|
||||
|
||||
|
||||
message PersistenceRecordCommon{
|
||||
uint32 id = 1;
|
||||
bool is_active = 10;
|
||||
int32 creator_user_id = 20;
|
||||
uint32 creator_user_id = 20;
|
||||
google.protobuf.Timestamp creation_time = 30;
|
||||
google.protobuf.Timestamp last_edit_time = 40;
|
||||
google.protobuf.Timestamp deletion_time = 50;
|
||||
@@ -25,8 +24,9 @@ message ComspositeAlgoDefinition {
|
||||
}
|
||||
|
||||
message ComspositeAlgoRecord {
|
||||
PersistenceRecordCommon common = 10;
|
||||
ComspositeAlgoDefinition algo_definition = 20;
|
||||
string key = 10 [(bts.api.key)=true];
|
||||
PersistenceRecordCommon common = 20;
|
||||
ComspositeAlgoDefinition algo_definition = 30;
|
||||
}
|
||||
|
||||
message AlgoTemplate{
|
||||
@@ -40,16 +40,17 @@ message AlgoTemplate{
|
||||
}
|
||||
|
||||
message AlgoTemplateRecord{
|
||||
PersistenceRecordCommon common = 10;
|
||||
AlgoTemplate algo_template = 20;
|
||||
string key = 10 [(bts.api.key)=true];
|
||||
PersistenceRecordCommon common = 20;
|
||||
AlgoTemplate algo_template = 30;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////
|
||||
// API ////////////////////////////////////////////////////
|
||||
|
||||
message ComspositeAlgoCreateRequest{
|
||||
int32 user_id = 10;
|
||||
ComspositeAlgoDefinition algo_definition = 20;
|
||||
uint32 user_id = 10;
|
||||
ComspositeAlgoDefinition new_algo = 20;
|
||||
}
|
||||
|
||||
message ComspositeAlgoCreateResult{
|
||||
@@ -59,7 +60,7 @@ message ComspositeAlgoCreateResult{
|
||||
}
|
||||
|
||||
message TemplateCreateRequest{
|
||||
int32 user_id = 10;
|
||||
uint32 user_id = 10;
|
||||
AlgoTemplate new_template = 20;
|
||||
}
|
||||
|
||||
@@ -70,12 +71,12 @@ message TemplateCreateResult{
|
||||
}
|
||||
|
||||
message TemplateUpdateRequest{
|
||||
int32 template_id = 10;
|
||||
string key = 10;
|
||||
AlgoTemplate updated_template = 20;
|
||||
}
|
||||
|
||||
message AlgoServiceRemoveRequest{
|
||||
uint32 id = 10;
|
||||
string key = 10;
|
||||
}
|
||||
|
||||
message AlgoServiceReply{
|
||||
@@ -93,4 +94,7 @@ service AlgoService {
|
||||
rpc update_template (TemplateUpdateRequest) returns (AlgoServiceReply);
|
||||
// remove template
|
||||
rpc remove_template (AlgoServiceRemoveRequest) returns (AlgoServiceReply);
|
||||
// streams
|
||||
rpc algos(bts.api.VoidMessage) returns (stream ComspositeAlgoRecord);
|
||||
rpc algo_templates(bts.api.VoidMessage) returns (stream AlgoTemplateRecord);
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "AlgoServiceServer.h"
|
||||
#include "lib/api/service_helpers/AlgoServiceUtils.h"
|
||||
#include "lib/api/public/ServiceContext.h"
|
||||
|
||||
#include "lib/persistence/SqliteStore.h"
|
||||
#include "lib/logging/logging.h"
|
||||
@@ -19,14 +20,35 @@ namespace orderman {
|
||||
using namespace std;
|
||||
using namespace bts::api::orderman;
|
||||
|
||||
struct AlgoServiceServer::PImpl{
|
||||
const std::string template_table_name,
|
||||
algo_table_name;
|
||||
std::unique_ptr<bts::persistence::IStore> store;
|
||||
bts::api::ServiceContext& context;
|
||||
AlgoServiceBase::AlgosStatefulPublisher algo_publisher;
|
||||
AlgoServiceBase::Algo_templatesStatefulPublisher template_publisher;
|
||||
PImpl(bts::api::ServiceContext& context,
|
||||
const AlgoServiceBase::AlgosStatefulPublisher& algo_publisher,
|
||||
const AlgoServiceBase::Algo_templatesStatefulPublisher& template_publisher)
|
||||
:
|
||||
template_table_name("templates"),
|
||||
algo_table_name("composite_algos"),
|
||||
store(new bts::persistence::SqliteStore("algo")),
|
||||
context(context),
|
||||
algo_publisher(algo_publisher),
|
||||
template_publisher(template_publisher){
|
||||
}
|
||||
};
|
||||
|
||||
AlgoServiceServer::AlgoServiceServer(bts::api::ServiceContext& context)
|
||||
:
|
||||
bts::api::orderman::AlgoServiceBase (context),
|
||||
_db_name("algo"),
|
||||
_template_table_name("templates"),
|
||||
_algo_table_name("composite_algos"),
|
||||
_store(new bts::persistence::SqliteStore(_db_name)){
|
||||
_check_data_on_start ();
|
||||
AlgoServiceBase (context),
|
||||
me(new PImpl(context,
|
||||
make_algos_stateful_publisher(),
|
||||
make_algo_templates_stateful_publisher()))
|
||||
{
|
||||
_init_publish_algos();
|
||||
_init_publish_templates();
|
||||
}
|
||||
AlgoServiceServer::~AlgoServiceServer(){}
|
||||
|
||||
@@ -35,20 +57,21 @@ void AlgoServiceServer::create_composite_algo(
|
||||
const api::Response<ComspositeAlgoCreateResult>& response){
|
||||
ComspositeAlgoCreateResult result;
|
||||
result.set_success(true);
|
||||
if (_find_algo(request.algo_definition().name()) == nullptr){
|
||||
PersistenceRecordCommon common;
|
||||
common.set_id(_get_max_algo_id()+1);
|
||||
common.set_creator_user_id(request.user_id());
|
||||
string key = request.new_algo().name();
|
||||
if (!_has_algo(key)){
|
||||
ComspositeAlgoRecord new_algo_rec;
|
||||
new_algo_rec.mutable_common()->CopyFrom(common);
|
||||
new_algo_rec.mutable_algo_definition()->CopyFrom(request.algo_definition());
|
||||
// copy it to the DB
|
||||
if(_store_and_publish_aglo(new_algo_rec))
|
||||
new_algo_rec.mutable_common()->CopyFrom(_new_common(request.user_id()));
|
||||
new_algo_rec.mutable_algo_definition()->CopyFrom(request.new_algo());
|
||||
if(_store_and_publish_algo(new_algo_rec))
|
||||
result.mutable_record()->CopyFrom(new_algo_rec);
|
||||
else{
|
||||
result.set_success(false);
|
||||
result.set_err_msg("Could not store algo");
|
||||
}
|
||||
}
|
||||
else{
|
||||
result.set_success(false);
|
||||
result.set_err_msg("Algo with this name already exists");
|
||||
result.set_err_msg("Algo with this name already exists");
|
||||
}
|
||||
response(result);
|
||||
}
|
||||
@@ -58,24 +81,23 @@ void AlgoServiceServer::create_template(
|
||||
const api::Response<TemplateCreateResult>& response){
|
||||
TemplateCreateResult result;
|
||||
result.set_success(true);
|
||||
std::string key = template_key(request.new_template());
|
||||
AlgoTemplateRecord* r = _find_template(key);
|
||||
if(r == nullptr){
|
||||
uint32_t new_id=_get_max_template_id()+1;
|
||||
PersistenceRecordCommon c;
|
||||
c.set_id(new_id);
|
||||
c.set_is_active(true);
|
||||
c.set_creator_user_id(request.user_id());
|
||||
string key = template_key(request.new_template());
|
||||
const AlgoTemplateRecord* ex_rec = _find_template(key);
|
||||
if(ex_rec == nullptr){
|
||||
AlgoTemplateRecord new_record;
|
||||
new_record.mutable_common()->CopyFrom(c);
|
||||
new_record.mutable_common()->CopyFrom(_new_common(request.user_id()));
|
||||
new_record.mutable_algo_template()->CopyFrom(request.new_template());
|
||||
_store_and_publish_template(true,new_record);
|
||||
result.mutable_record()->CopyFrom(new_record);
|
||||
if(_store_and_publish_template(true,new_record))
|
||||
result.mutable_record()->CopyFrom(new_record);
|
||||
else{
|
||||
result.set_success(false);
|
||||
result.set_err_msg("Could not store algo");
|
||||
}
|
||||
}
|
||||
else{
|
||||
result.set_success(false);
|
||||
result.set_err_msg("Template already exists");
|
||||
result.mutable_record()->CopyFrom(*r);
|
||||
result.mutable_record()->CopyFrom(*ex_rec);
|
||||
}
|
||||
response(result);
|
||||
}
|
||||
@@ -85,22 +107,22 @@ void AlgoServiceServer::update_template(
|
||||
const api::Response<AlgoServiceReply>& response){
|
||||
AlgoServiceReply result;
|
||||
result.set_success(true);
|
||||
std::string key = template_key(request.updated_template());
|
||||
AlgoTemplateRecord* r = _find_template(key);
|
||||
if(r == nullptr){
|
||||
const AlgoTemplateRecord* ex_rec = _find_template(request.key());
|
||||
if(ex_rec == nullptr){
|
||||
result.set_success(false);
|
||||
result.set_err_msg("Could not locate template");
|
||||
}
|
||||
else{
|
||||
r->mutable_algo_template()->CopyFrom(request.updated_template());
|
||||
_store_and_publish_template(false,*r);
|
||||
const AlgoTemplateRecord updated_rec = *ex_rec;
|
||||
if(!_store_and_publish_template(false,updated_rec)){
|
||||
result.set_success(false);
|
||||
result.set_err_msg("Could not serialize template");
|
||||
}
|
||||
// TODO: need to make sure
|
||||
// a. template's algo is not changed
|
||||
// b. template is consistent with the algo
|
||||
|
||||
}
|
||||
response(result);
|
||||
|
||||
}
|
||||
|
||||
void AlgoServiceServer::remove_template(
|
||||
@@ -108,124 +130,99 @@ void AlgoServiceServer::remove_template(
|
||||
const api::Response<AlgoServiceReply>& response){
|
||||
AlgoServiceReply result;
|
||||
result.set_success(true);
|
||||
AlgoTemplateRecord* r = _find_template(request.id());
|
||||
if(r == nullptr){
|
||||
const AlgoTemplateRecord* ex_rec = _find_template(request.key());
|
||||
if(ex_rec == nullptr){
|
||||
result.set_success(false);
|
||||
result.set_err_msg("Could not locate template");
|
||||
}
|
||||
else{
|
||||
set<string> keys;
|
||||
keys.insert(template_key(r->algo_template()));
|
||||
_store->remove(_template_table_name,keys);
|
||||
keys.insert(request.key());
|
||||
me->store->remove(me->template_table_name,keys);
|
||||
}
|
||||
response(result);
|
||||
}
|
||||
|
||||
void AlgoServiceServer::_load_algos(){
|
||||
_algos.clear();
|
||||
map<string,map<string,string>> all_data = _store->retrieve_all();
|
||||
auto table=all_data.find(_algo_table_name);
|
||||
PersistenceRecordCommon AlgoServiceServer::_new_common(uint32_t user_id){
|
||||
PersistenceRecordCommon rv;
|
||||
rv.set_is_active(true);
|
||||
rv.set_creator_user_id(user_id);
|
||||
return rv;
|
||||
}
|
||||
|
||||
// algo helper functions
|
||||
void AlgoServiceServer::_init_publish_algos(){
|
||||
map<string,map<string,string>> all_data = me->store->retrieve_all();
|
||||
auto table=all_data.find(me->algo_table_name);
|
||||
if(table!=all_data.end()){
|
||||
for (const auto& p : table->second){
|
||||
ComspositeAlgoRecord algo;
|
||||
if(algo.ParseFromString(p.second))
|
||||
_algos.push_back(algo);
|
||||
me->algo_publisher.add_or_update(algo);
|
||||
else
|
||||
LOG(Gen, Error, "AlgoServiceServer._load_algos "
|
||||
"could not load {}", table->first);
|
||||
}
|
||||
me->algo_publisher.flush();
|
||||
}
|
||||
}
|
||||
|
||||
ComspositeAlgoRecord*AlgoServiceServer::_find_algo(
|
||||
const std::string& name){
|
||||
for (ComspositeAlgoRecord& r : _algos)
|
||||
if(name == r.algo_definition().name())
|
||||
return &r;
|
||||
return nullptr;
|
||||
bool AlgoServiceServer::_has_algo(const std::string& key){
|
||||
const auto& algo_map = me->algo_publisher.state_data();
|
||||
return algo_map.find(key) != algo_map.end();
|
||||
}
|
||||
|
||||
void AlgoServiceServer::_check_data_on_start (){
|
||||
}
|
||||
|
||||
uint32_t AlgoServiceServer::_get_max_algo_id (){
|
||||
uint32_t rv=0;
|
||||
for(const auto& p : _algos)
|
||||
if (p.common().id() > rv)
|
||||
rv = p.common().id();
|
||||
return rv;
|
||||
}
|
||||
|
||||
bool AlgoServiceServer::_store_and_publish_aglo (const ComspositeAlgoRecord& rec){
|
||||
bool AlgoServiceServer::_store_and_publish_algo (
|
||||
const ComspositeAlgoRecord& rec){
|
||||
string ser;
|
||||
if(rec.SerializeToString(&ser)){
|
||||
_store->insert(_algo_table_name,
|
||||
rec.algo_definition().name(),
|
||||
ser);
|
||||
_algos.push_back(rec);
|
||||
me->store->insert(me->algo_table_name,
|
||||
rec.algo_definition().name(),
|
||||
ser);
|
||||
me->algo_publisher.add_or_update(rec);
|
||||
me->algo_publisher.flush();
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
void AlgoServiceServer::_load_templates(){
|
||||
_templates.clear();
|
||||
map<string,map<string,string>> all_data = _store->retrieve_all();
|
||||
auto table=all_data.find(_template_table_name);
|
||||
void AlgoServiceServer::_init_publish_templates(){
|
||||
map<string,map<string,string>> all_data = me->store->retrieve_all();
|
||||
auto table=all_data.find(me->template_table_name);
|
||||
if(table!=all_data.end()){
|
||||
for (const auto& p : table->second){
|
||||
AlgoTemplateRecord t_record;
|
||||
if(t_record.ParseFromString(p.second))
|
||||
_templates.push_back(t_record);
|
||||
me->template_publisher.add_or_update(t_record);
|
||||
else
|
||||
LOG(Gen, Error, "AlgoServiceServer._load_templates "
|
||||
"could not load template {}", table->first);
|
||||
}
|
||||
me->template_publisher.flush();
|
||||
}
|
||||
}
|
||||
|
||||
AlgoTemplateRecord* AlgoServiceServer::_find_template(
|
||||
const std::string& key){
|
||||
for (AlgoTemplateRecord& r : _templates)
|
||||
if(key == template_key(r.algo_template()))
|
||||
return &r;
|
||||
const AlgoTemplateRecord* AlgoServiceServer::_find_template(
|
||||
const std::string& key){
|
||||
const auto& template_map = me->template_publisher.state_data();
|
||||
const auto it = template_map.find(key);
|
||||
if(it != template_map.end())
|
||||
return &(it->second);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
AlgoTemplateRecord* AlgoServiceServer::_find_template(
|
||||
uint32_t id){
|
||||
for (AlgoTemplateRecord& r : _templates)
|
||||
if(r.common().id()==id)
|
||||
return &r;
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
uint32_t AlgoServiceServer::_get_max_template_id(){
|
||||
uint32_t rv = 0;
|
||||
for (const AlgoTemplateRecord& r : _templates)
|
||||
if(r.common().id()>rv)
|
||||
rv = r.common().id();
|
||||
return rv;
|
||||
}
|
||||
|
||||
bool AlgoServiceServer::_store_and_publish_template (
|
||||
bool insert,
|
||||
const AlgoTemplateRecord& rec){
|
||||
string ser;
|
||||
if(rec.SerializeToString(&ser)){
|
||||
string key=template_key(rec.algo_template());
|
||||
if(insert){
|
||||
_store->insert(_template_table_name,key,ser);
|
||||
_templates.push_back(rec);
|
||||
}
|
||||
else{
|
||||
// need to replace, so find
|
||||
AlgoTemplateRecord* ex_rec = _find_template(key);
|
||||
bts_assert(ex_rec != nullptr);
|
||||
_store->update(_template_table_name,key,ser);
|
||||
ex_rec->CopyFrom(rec);
|
||||
}
|
||||
if(rec.SerializeToString(&ser)){
|
||||
if(insert)
|
||||
me->store->insert(me->template_table_name,rec.key(),ser);
|
||||
else
|
||||
me->store->update(me->template_table_name,rec.key(),ser);
|
||||
me->template_publisher.add_or_update(rec);
|
||||
me->template_publisher.flush();
|
||||
return true;
|
||||
}
|
||||
else
|
||||
|
||||
@@ -14,11 +14,8 @@ namespace orderman {
|
||||
class AlgoServiceServer : public api::orderman::AlgoServiceBase
|
||||
{
|
||||
private:
|
||||
const std::string _db_name;
|
||||
const std::string _template_table_name, _algo_table_name;
|
||||
std::unique_ptr<bts::persistence::IStore> _store;
|
||||
std::vector<bts::api::orderman::ComspositeAlgoRecord> _algos;
|
||||
std::vector<bts::api::orderman::AlgoTemplateRecord> _templates;
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me;
|
||||
public:
|
||||
AlgoServiceServer(bts::api::ServiceContext& context);
|
||||
~AlgoServiceServer();
|
||||
@@ -39,21 +36,17 @@ public:
|
||||
const bts::api::orderman::AlgoServiceRemoveRequest& request,
|
||||
const api::Response<bts::api::orderman::AlgoServiceReply>& reply) override;
|
||||
private:
|
||||
// loads algos from the db
|
||||
void _load_algos();
|
||||
bts::api::orderman::ComspositeAlgoRecord* _find_algo(
|
||||
const std::string& name);
|
||||
void _check_data_on_start();
|
||||
uint32_t _get_max_algo_id ();
|
||||
bool _store_and_publish_aglo (
|
||||
bts::api::orderman::PersistenceRecordCommon _new_common(uint32_t user_id);
|
||||
// loads algos from the db and publises them
|
||||
void _init_publish_algos();
|
||||
bool _has_algo(const std::string& name);
|
||||
bool _store_and_publish_algo (
|
||||
const bts::api::orderman::ComspositeAlgoRecord& rec);
|
||||
// template helpers
|
||||
void _load_templates();
|
||||
bts::api::orderman::AlgoTemplateRecord* _find_template(
|
||||
const std::string& key);
|
||||
bts::api::orderman::AlgoTemplateRecord* _find_template(
|
||||
uint32_t id);
|
||||
uint32_t _get_max_template_id();
|
||||
// loads templates from the db and publises them
|
||||
void _init_publish_templates();
|
||||
const bts::api::orderman::AlgoTemplateRecord* _find_template(
|
||||
const std::string& key);
|
||||
bool _store_and_publish_template (
|
||||
bool insert,
|
||||
const bts::api::orderman::AlgoTemplateRecord& rec);
|
||||
|
||||
Reference in new issue
Block a user