mirror of
https://github.com/Expand-sys/CCash
synced 2025-12-16 16:12:14 +11:00
🐎 GetLog() snapshot
This commit is contained in:
parent
ba20e461f6
commit
1adb23f5b8
7 changed files with 57 additions and 15 deletions
|
|
@ -36,7 +36,7 @@ public:
|
||||||
|
|
||||||
//Usage
|
//Usage
|
||||||
METHOD_ADD(api::GetBal, "/v1/user/bal?name={name}", Get, Options); //done
|
METHOD_ADD(api::GetBal, "/v1/user/bal?name={name}", Get, Options); //done
|
||||||
METHOD_ADD(api::GetLog, "/v1/user/log", Get, Options, "UserFilter"); //snapshot not implemented
|
METHOD_ADD(api::GetLog, "/v1/user/log", Get, Options, "UserFilter"); //done (could be optimized further)
|
||||||
METHOD_ADD(api::SendFunds, "/v1/user/transfer", Post, Options, "UserFilter"); //responses incomplete
|
METHOD_ADD(api::SendFunds, "/v1/user/transfer", Post, Options, "UserFilter"); //responses incomplete
|
||||||
METHOD_ADD(api::VerifyPassword, "/v1/user/verify_password", Get, Options, "UserFilter"); //done
|
METHOD_ADD(api::VerifyPassword, "/v1/user/verify_password", Get, Options, "UserFilter"); //done
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -7,6 +7,9 @@ private:
|
||||||
std::atomic<bool> change_flag = false; //if true changes have been made
|
std::atomic<bool> change_flag = false; //if true changes have been made
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
ChangeFlag() noexcept;
|
||||||
|
ChangeFlag(ChangeFlag &&) noexcept;
|
||||||
|
|
||||||
void SetChangesOn() noexcept;
|
void SetChangesOn() noexcept;
|
||||||
void SetChangesOff() noexcept;
|
void SetChangesOff() noexcept;
|
||||||
bool GetChangeState() const noexcept;
|
bool GetChangeState() const noexcept;
|
||||||
|
|
|
||||||
|
|
@ -1,13 +1,21 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <json/json.h>
|
#include <json/json.h> // to be removed later
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
|
#include "change_flag.h"
|
||||||
#include "consts.hpp"
|
#include "consts.hpp"
|
||||||
#include "transaction.h"
|
#include "transaction.h"
|
||||||
|
|
||||||
struct Log
|
struct Log
|
||||||
{
|
{
|
||||||
|
private:
|
||||||
|
ChangeFlag log_flag;
|
||||||
|
Json::Value log_snapshot;
|
||||||
|
|
||||||
|
public:
|
||||||
|
const Json::Value &GetLog();
|
||||||
|
|
||||||
std::vector<Transaction> data;
|
std::vector<Transaction> data;
|
||||||
void AddTrans(Transaction &&t);
|
void AddTrans(Transaction &&t);
|
||||||
Json::Value Serialize() const;
|
Json::Value Serialize() const; // to be removed later
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <json/json.h>
|
#include <json/json.h> //to be removed later
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <xxhash.h>
|
#include <xxhash.h>
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
@ -15,5 +15,5 @@ struct User
|
||||||
User(uint32_t init_bal, uint64_t init_pass);
|
User(uint32_t init_bal, uint64_t init_pass);
|
||||||
User(uint32_t init_bal, uint64_t init_pass, const Json::Value &log_j);
|
User(uint32_t init_bal, uint64_t init_pass, const Json::Value &log_j);
|
||||||
|
|
||||||
Json::Value Serialize() const;
|
Json::Value Serialize() const; //to be removed later
|
||||||
};
|
};
|
||||||
|
|
|
||||||
12
src/bank.cpp
12
src/bank.cpp
|
|
@ -15,16 +15,8 @@ BankResponse Bank::GetBal(const std::string &name) const noexcept
|
||||||
BankResponse Bank::GetLogs(const std::string &name) noexcept
|
BankResponse Bank::GetLogs(const std::string &name) noexcept
|
||||||
{
|
{
|
||||||
BankResponse res;
|
BankResponse res;
|
||||||
if (!users.if_contains(name, [&res](const User &u) {
|
if (!users.modify_if(name, [&res](User &u) {
|
||||||
Json::Value temp;
|
res = {k200OK, u.log.GetLog()};
|
||||||
for (uint32_t i = u.log.data.size(); i > 0; --i)
|
|
||||||
{
|
|
||||||
temp[i - 1]["to"] = u.log.data[u.log.data.size() - i].to;
|
|
||||||
temp[i - 1]["from"] = u.log.data[u.log.data.size() - i].from;
|
|
||||||
temp[i - 1]["amount"] = (Json::UInt)u.log.data[u.log.data.size() - i].amount;
|
|
||||||
temp[i - 1]["time"] = (Json::UInt64)u.log.data[u.log.data.size() - i].time;
|
|
||||||
}
|
|
||||||
res = {k200OK, std::move(temp)};
|
|
||||||
}))
|
}))
|
||||||
{
|
{
|
||||||
return BankResponse(k404NotFound, "User not found");
|
return BankResponse(k404NotFound, "User not found");
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,12 @@
|
||||||
#include "change_flag.h"
|
#include "change_flag.h"
|
||||||
|
|
||||||
|
ChangeFlag::ChangeFlag() noexcept {}
|
||||||
|
|
||||||
|
ChangeFlag::ChangeFlag(ChangeFlag &&f) noexcept
|
||||||
|
{
|
||||||
|
change_flag.store(f.GetChangeState(), std::memory_order_release); //is this safe?
|
||||||
|
}
|
||||||
|
|
||||||
void ChangeFlag::SetChangesOn() noexcept
|
void ChangeFlag::SetChangesOn() noexcept
|
||||||
{
|
{
|
||||||
return change_flag.store(1, std::memory_order_release);
|
return change_flag.store(1, std::memory_order_release);
|
||||||
|
|
|
||||||
32
src/log.cpp
32
src/log.cpp
|
|
@ -16,7 +16,35 @@ void Log::AddTrans(Transaction &&t)
|
||||||
data.reserve(data.capacity() + pre_log_size); // Reserve more memory
|
data.reserve(data.capacity() + pre_log_size); // Reserve more memory
|
||||||
}
|
}
|
||||||
data.push_back(std::move(t)); // In either case we have space under max length, move to new spot
|
data.push_back(std::move(t)); // In either case we have space under max length, move to new spot
|
||||||
|
log_flag.SetChangesOn();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const Json::Value &Log::GetLog()
|
||||||
|
{
|
||||||
|
if (log_flag.GetChangeState()) //if there are changes
|
||||||
|
{
|
||||||
|
//re-generate snapshot
|
||||||
|
Json::Value res;
|
||||||
|
for (uint32_t i = data.size(); i > 0; --i)
|
||||||
|
{
|
||||||
|
res[i - 1]["to"] = data[data.size() - i].to;
|
||||||
|
res[i - 1]["from"] = data[data.size() - i].from;
|
||||||
|
res[i - 1]["amount"] = (Json::UInt)data[data.size() - i].amount;
|
||||||
|
#ifdef _USE_32BIT_TIME_T
|
||||||
|
res[i - 1]["time"] = (Json::UInt)data[data.size() - i].time;
|
||||||
|
#else
|
||||||
|
res[i - 1]["time"] = (Json::UInt64)data[data.size() - i].time;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
log_flag.SetChangesOff();
|
||||||
|
return log_snapshot = res;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
return log_snapshot;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Json::Value Log::Serialize() const
|
Json::Value Log::Serialize() const
|
||||||
{
|
{
|
||||||
Json::Value res;
|
Json::Value res;
|
||||||
|
|
@ -25,7 +53,11 @@ Json::Value Log::Serialize() const
|
||||||
res[i]["to"] = data[i].to;
|
res[i]["to"] = data[i].to;
|
||||||
res[i]["from"] = data[i].from;
|
res[i]["from"] = data[i].from;
|
||||||
res[i]["amount"] = (Json::UInt)data[i].amount;
|
res[i]["amount"] = (Json::UInt)data[i].amount;
|
||||||
|
#ifdef _USE_32BIT_TIME_T
|
||||||
|
res[i]["time"] = (Json::UInt)data[i].time;
|
||||||
|
#else
|
||||||
res[i]["time"] = (Json::UInt64)data[i].time;
|
res[i]["time"] = (Json::UInt64)data[i].time;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue