#include <set>
|
#include "dgrid_config.h"
|
#include "dgrid_logger.h"
|
#include "dgridd_datastore_simple.h"
|
#include "dgridd_node_arbiter.h"
|
|
namespace DGrid {
|
|
DGriddDataStoreSimple::DGriddDataStoreSimple(std::string session, DGriddNodeArbiter* a)
|
:_node_arbiter(a), _session(session)
|
{
|
}
|
|
DGriddDataStoreSimple::~DGriddDataStoreSimple()
|
{
|
}
|
|
void DGriddDataStoreSimple::setNodeArbiter(DGriddNodeArbiter* a)
|
{
|
_node_arbiter = a;
|
}
|
|
void DGriddDataStoreSimple::showStats()
|
{
|
writeLog(LOG_INFO, "> Simple datastote stats:");
|
writeLog(LOG_INFO, " Themes total: %d", getThemesCount());
|
writeLog(LOG_INFO, " Keys total: %d", getKeysCount());
|
}
|
|
uint32_t DGriddDataStoreSimple::getThemesCount()
|
{
|
std::lock_guard<std::mutex> lock(io_mutex);
|
return internal_data.size();
|
}
|
|
uint64_t DGriddDataStoreSimple::getKeysCount()
|
{
|
uint64_t cnt(0);
|
std::lock_guard<std::mutex> lock(io_mutex);
|
for(auto theme: internal_data) {
|
cnt += theme.second.size();
|
}
|
return cnt;
|
}
|
|
/*
|
client-controlled methods: stats
|
*/
|
|
DGridResult DGriddDataStoreSimple::get_stats(const std::string& theme, std::vector<std::string>& list, std::string& error_string)
|
{
|
DGridResult r(DGRID_OK);
|
list.clear();
|
|
io_mutex.lock();
|
if(theme.empty()) {
|
for(auto it: internal_data) {
|
list.push_back(it.first);
|
}
|
} else {
|
auto theme_it = internal_data.find(theme);
|
if(theme_it == internal_data.end()) {
|
error_string = "Unknown theme '" + theme + "'";
|
r = DGRID_NOT_FOUND;
|
} else {
|
for(auto it: internal_data[theme]) {
|
list.push_back(it.first);
|
}
|
}
|
}
|
io_mutex.unlock();
|
return r;
|
}
|
|
/*
|
client-controlled methods
|
*/
|
|
DGridResult DGriddDataStoreSimple::set_value(const std::string& theme, const std::string& key, /*const*/ SimpleValueData& value, std::string& error_string)
|
{
|
DGridResult r(DGRID_OK);
|
bool replicate(false);
|
NodeID old_source;
|
error_string.clear();
|
value.source_node = Config::node_id();
|
|
io_mutex.lock();
|
{
|
auto it = internal_data[theme].find(key);
|
if(it != internal_data[theme].end()) {
|
if(it->second.ts > value.ts) {
|
writeLog(LOG_NOTICE, "\E[33;60m older record received (%s/%s), ignoring", theme.c_str(), key.c_str());
|
r = DGRID_TOO_OLD;
|
} else {
|
if(!it->second.source_node.empty() && it->second.source_node != value.source_node) {
|
writeLog(LOG_DEBUG, "\E[33;60m value '%s/%s' source changed (was '%s'), have to notify old owner",
|
theme.c_str(), key.c_str(), raw_to_hexstring(it->second.source_node).c_str());
|
}
|
old_source = it->second.source_node;
|
it->second = value;
|
replicate = true;
|
}
|
} else {
|
internal_data[theme][key] = value;
|
replicate = true;
|
}
|
}
|
io_mutex.unlock();
|
if(replicate) {
|
// send new data to other nodes
|
writeLog(LOG_DEBUG, "\E[33;60m send new key (%s/%s) to other nodes...", theme.c_str(), key.c_str());
|
if(_node_arbiter) {
|
SimpleValueMap data;
|
data[key] = value;
|
_node_arbiter->append_theme_simple(_session, theme, data, old_source);
|
}
|
}
|
return r;
|
}
|
|
DGridResult DGriddDataStoreSimple::get_value(const std::string& theme, const std::string& key, SimpleValueData& value, std::string& error_string)
|
{
|
DGridResult r(DGRID_NOT_FOUND);
|
error_string.clear();
|
NodeID old_source;
|
|
io_mutex.lock();
|
{
|
auto theme_it = internal_data.find(theme);
|
if(theme_it != internal_data.end()) {
|
auto key_it = theme_it->second.find(key);
|
if(key_it != theme_it->second.end()) {
|
value = key_it->second;
|
if(Config::node_id() != value.source_node) {
|
old_source = value.source_node;
|
}
|
r = DGRID_OK;
|
}
|
}
|
}
|
io_mutex.unlock();
|
// check data in nodes
|
if(_node_arbiter != nullptr && (DGRID_NOT_FOUND == r || !old_source.empty())) {
|
SimpleValueMap data;
|
data[key] = SimpleValueData();
|
if(old_source.empty())
|
writeLog(LOG_DEBUG, "\E[33;60m key (%s/%s) not found, checking other nodes...", theme.c_str(), key.c_str());
|
else
|
writeLog(LOG_DEBUG, "\E[33;60m key (%s/%s) found, but owner differs, check source...", theme.c_str(), key.c_str());
|
if (DGRID_OK == _node_arbiter->get_theme_simple(_session, theme, data, old_source)) {
|
io_mutex.lock();
|
for(auto& it: data) {
|
internal_data[theme][it.first] = it.second;
|
}
|
io_mutex.unlock();
|
auto key_it = data.find(key);
|
if(key_it != data.end()) {
|
value = key_it->second;
|
r = DGRID_OK;
|
}
|
}
|
}
|
if (DGRID_NOT_FOUND == r) {
|
error_string = "key (" + theme + "/" + key + ") not found";
|
}
|
return r;
|
}
|
|
DGridResult DGriddDataStoreSimple::del_value(const std::string& theme, const std::string& key, std::string& error_string)
|
{
|
DGridResult r(DGRID_NOT_FOUND);
|
error_string.clear();
|
std::lock_guard<std::mutex> lock(io_mutex);
|
|
auto theme_it = internal_data.find(theme);
|
if(theme_it != internal_data.end()) {
|
if(theme_it->second.erase(key) > 0)
|
r = DGRID_OK;
|
}
|
// TODO _node_arbiter->erase()
|
|
if (DGRID_NOT_FOUND == r) {
|
error_string = "key (" + theme + "/" + key + ") not found";
|
}
|
return r;
|
}
|
|
/*
|
node interaction methods
|
*/
|
|
// ответ на запрос темы (в data можно указать ключи, если нужна не вся тема)
|
DGridResult DGriddDataStoreSimple::get_theme_simple(const std::string& name, SimpleValueMap& data)
|
{
|
std::lock_guard<std::mutex> lock(io_mutex);
|
auto theme_it = internal_data.find(name);
|
if(theme_it != internal_data.end()) {
|
if(data.empty()) {
|
data = theme_it->second;
|
return DGRID_OK;
|
} else {
|
std::set<std::string> requested_keys;
|
// collect requested keys
|
for(auto& it: data) {
|
requested_keys.insert(it.first);
|
}
|
data.clear();
|
// search internals & fill data for requested keys
|
for(auto& key: requested_keys) {
|
auto it = theme_it->second.find(key);
|
if(it != theme_it->second.end()) {
|
data[it->first] = it->second;
|
}
|
}
|
if(!data.empty())
|
return DGRID_OK;
|
}
|
}
|
return DGRID_NOT_FOUND;
|
}
|
|
// сохраняем копию в локальное хранилище
|
DGridResult DGriddDataStoreSimple::append_theme_simple(const std::string& name, const SimpleValueMap& data, NodeID& source_node)
|
{
|
std::lock_guard<std::mutex> lock(io_mutex);
|
bool source_checked(false);
|
for(auto& it: data) {
|
if (internal_data[name][it.first].ts > it.second.ts) {
|
writeLog(LOG_NOTICE, "\E[33;60m older record received (%s/%s), ignoring", name.c_str(), it.first.c_str());
|
} else {
|
if(!source_checked) {
|
auto key_it = internal_data[name].find(it.first);
|
if(key_it != internal_data[name].end())
|
source_node = key_it->second.source_node;
|
source_checked = true;
|
}
|
internal_data[name][it.first] = it.second;
|
}
|
}
|
return DGRID_OK;
|
}
|
|
// дописываем в локальное хранилище и возвращаем разницу
|
DGridResult DGriddDataStoreSimple::sync_theme_simple(const std::string& name, SimpleValueMap& data)
|
{
|
std::lock_guard<std::mutex> lock(io_mutex);
|
auto theme_it = internal_data.find(name);
|
if(theme_it == internal_data.end()) {
|
internal_data[name] = data;
|
data.clear();
|
} else {
|
std::set<std::string> ext_keys;
|
// append local storage
|
for(auto& it: data) {
|
if (internal_data[name][it.first].ts < it.second.ts) {
|
// save new version
|
theme_it->second[it.first] = it.second;
|
ext_keys.insert(it.first);
|
} else if (internal_data[name][it.first].ts == it.second.ts) {
|
// just mark to shorten result
|
ext_keys.insert(it.first);
|
}
|
// return new version if older received
|
}
|
// return diff
|
data.clear();
|
for(auto& it: theme_it->second) {
|
if(ext_keys.find(it.first) == ext_keys.end())
|
data[it.first] = it.second;
|
}
|
}
|
return DGRID_OK;
|
}
|
|
} // namespace DGrid
|