From 1adb23f5b8912cdf4fa832d83f115d19e0542217 Mon Sep 17 00:00:00 2001 From: EntireTwix Date: Sun, 27 Jun 2021 21:08:10 -0700 Subject: [PATCH] :racehorse: GetLog() snapshot --- include/bank_api.h | 2 +- include/change_flag.h | 3 +++ include/log.h | 12 ++++++++++-- include/user.h | 4 ++-- src/bank.cpp | 12 ++---------- src/change_flag.cpp | 7 +++++++ src/log.cpp | 32 ++++++++++++++++++++++++++++++++ 7 files changed, 57 insertions(+), 15 deletions(-) diff --git a/include/bank_api.h b/include/bank_api.h index c9d4e40..e630316 100644 --- a/include/bank_api.h +++ b/include/bank_api.h @@ -36,7 +36,7 @@ public: //Usage 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::VerifyPassword, "/v1/user/verify_password", Get, Options, "UserFilter"); //done diff --git a/include/change_flag.h b/include/change_flag.h index 338ef29..333a378 100644 --- a/include/change_flag.h +++ b/include/change_flag.h @@ -7,6 +7,9 @@ private: std::atomic change_flag = false; //if true changes have been made public: + ChangeFlag() noexcept; + ChangeFlag(ChangeFlag &&) noexcept; + void SetChangesOn() noexcept; void SetChangesOff() noexcept; bool GetChangeState() const noexcept; diff --git a/include/log.h b/include/log.h index d7ff7dd..cd1cd8a 100644 --- a/include/log.h +++ b/include/log.h @@ -1,13 +1,21 @@ #pragma once -#include +#include // to be removed later #include #include +#include "change_flag.h" #include "consts.hpp" #include "transaction.h" struct Log { +private: + ChangeFlag log_flag; + Json::Value log_snapshot; + +public: + const Json::Value &GetLog(); + std::vector data; void AddTrans(Transaction &&t); - Json::Value Serialize() const; + Json::Value Serialize() const; // to be removed later }; diff --git a/include/user.h b/include/user.h index eda2bb5..efec7b3 100644 --- a/include/user.h +++ b/include/user.h @@ -1,5 +1,5 @@ #pragma once -#include +#include //to be removed later #include #include #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, const Json::Value &log_j); - Json::Value Serialize() const; + Json::Value Serialize() const; //to be removed later }; diff --git a/src/bank.cpp b/src/bank.cpp index 1b83090..a629c9c 100644 --- a/src/bank.cpp +++ b/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 res; - if (!users.if_contains(name, [&res](const User &u) { - Json::Value temp; - 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)}; + if (!users.modify_if(name, [&res](User &u) { + res = {k200OK, u.log.GetLog()}; })) { return BankResponse(k404NotFound, "User not found"); diff --git a/src/change_flag.cpp b/src/change_flag.cpp index f0ca41c..8ecb87b 100644 --- a/src/change_flag.cpp +++ b/src/change_flag.cpp @@ -1,5 +1,12 @@ #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 { return change_flag.store(1, std::memory_order_release); diff --git a/src/log.cpp b/src/log.cpp index 145f329..2650729 100644 --- a/src/log.cpp +++ b/src/log.cpp @@ -16,7 +16,35 @@ void Log::AddTrans(Transaction &&t) 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 + 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 res; @@ -25,7 +53,11 @@ Json::Value Log::Serialize() const res[i]["to"] = data[i].to; res[i]["from"] = data[i].from; 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; +#endif } return res; }