[Data Buckets] Implement scoped data buckets (#3498)

* [Data Buckets] Implement scoped data buckets

* Update database_update_manifest.cpp

* Update data_bucket.cpp

* Update data_bucket.cpp

* Update data_bucket.cpp

* Update data_bucket.h

* Update database_update_manifest.cpp

* Add GetScopedDbFilters references

* Scope transfer
This commit is contained in:
Chris Miles
2023-07-16 13:52:28 -05:00
committed by GitHub
parent 59537ae977
commit 3f3bbe98b5
14 changed files with 350 additions and 259 deletions
+184 -158
View File
@@ -1,173 +1,130 @@
#include "data_bucket.h"
#include "zonedb.h"
#include "mob.h"
#include <ctime>
#include <cctype>
#include "../common/repositories/data_buckets_repository.h"
/**
* Persists data via bucket_name as key
* @param bucket_key
* @param bucket_value
* @param expires_time
*/
void DataBucket::SetData(const std::string& bucket_key, const std::string& bucket_value, std::string expires_time) {
uint64 bucket_id = DataBucket::DoesBucketExist(bucket_key);
void DataBucket::SetData(const std::string &bucket_key, const std::string &bucket_value, std::string expires_time)
{
auto k = DataBucketKey{
.key = bucket_key,
.value = bucket_value,
.expires = expires_time,
.character_id = 0,
.npc_id = 0,
.bot_id = 0
};
std::string query;
DataBucket::SetData(k);
}
void DataBucket::SetData(const DataBucketKey &k)
{
auto r = DataBucketsRepository::GetWhere(
database,
fmt::format(
"{} `key` = '{}' AND (`expires` > {} OR `expires` = 0) LIMIT 1",
DataBucket::GetScopedDbFilters(k),
Strings::Escape(k.key),
(long long) std::time(nullptr)
)
);
// if we have an entry, use it
auto b = DataBucketsRepository::NewEntity();
if (!r.empty()) {
b = r[0];
}
if (k.character_id > 0) {
b.character_id = k.character_id;
} else if (k.npc_id > 0) {
b.npc_id = k.npc_id;
} else if (k.bot_id > 0) {
b.bot_id = k.bot_id;
}
uint64 bucket_id = b.id;
long long expires_time_unix = 0;
if (!expires_time.empty()) {
if (isalpha(expires_time[0]) || isalpha(expires_time[expires_time.length() - 1])) {
expires_time_unix = (long long) std::time(nullptr) + Strings::TimeToSeconds(expires_time);
} else {
expires_time_unix = (long long) std::time(nullptr) + Strings::ToInt(expires_time);
if (!k.expires.empty()) {
expires_time_unix = (long long) std::time(nullptr) + Strings::ToInt(k.expires);
if (isalpha(k.expires[0]) || isalpha(k.expires[k.expires.length() - 1])) {
expires_time_unix = (long long) std::time(nullptr) + Strings::TimeToSeconds(k.expires);
}
}
if (bucket_id > 0) {
std::string update_expired_time;
if (expires_time_unix > 0) {
update_expired_time = StringFormat(", `expires` = %lld ", expires_time_unix);
}
query = StringFormat(
"UPDATE `data_buckets` SET `value` = '%s' %s WHERE `id` = %i",
Strings::Escape(bucket_value).c_str(),
Strings::Escape(update_expired_time).c_str(),
bucket_id
);
b.expires = expires_time_unix;
b.value = k.value;
DataBucketsRepository::UpdateOne(database, b);
}
else {
query = StringFormat(
"INSERT INTO `data_buckets` (`key`, `value`, `expires`) VALUES ('%s', '%s', '%lld')",
Strings::Escape(bucket_key).c_str(),
Strings::Escape(bucket_value).c_str(),
expires_time_unix
);
b.expires = expires_time_unix;
b.key_ = k.key;
b.value = k.value;
DataBucketsRepository::InsertOne(database, b);
}
database.QueryDatabase(query);
}
/**
* Retrieves data via bucket_name as key
* @param bucket_key
* @return
*/
std::string DataBucket::GetData(const std::string& bucket_key) {
std::string query = StringFormat(
"SELECT `value` from `data_buckets` WHERE `key` = '%s' AND (`expires` > %lld OR `expires` = 0) LIMIT 1",
bucket_key.c_str(),
(long long) std::time(nullptr)
);
auto results = database.QueryDatabase(query);
if (!results.Success()) {
return std::string();
}
if (results.RowCount() != 1)
return std::string();
auto row = results.begin();
return std::string(row[0]);
}
/**
* Retrieves data expires time via bucket_name as key
* @param bucket_key
* @return
*/
std::string DataBucket::GetDataExpires(const std::string& bucket_key) {
std::string query = StringFormat(
"SELECT `expires` from `data_buckets` WHERE `key` = '%s' AND (`expires` > %lld OR `expires` = 0) LIMIT 1",
bucket_key.c_str(),
(long long) std::time(nullptr)
);
auto results = database.QueryDatabase(query);
if (!results.Success()) {
return std::string();
}
if (results.RowCount() != 1)
return std::string();
auto row = results.begin();
return std::string(row[0]);
}
std::string DataBucket::GetDataRemaining(const std::string& bucket_key) {
if (DataBucket::GetDataExpires(bucket_key).empty()) {
return "0";
}
std::string query = fmt::format(
"SELECT (`expires` - UNIX_TIMESTAMP()) AS `remaining` from `data_buckets` WHERE `key` = '{}' AND (`expires` > {} OR `expires` = 0) LIMIT 1",
bucket_key.c_str(),
(long long) std::time(nullptr)
);
auto results = database.QueryDatabase(query);
if (!results.Success()) {
return std::string();
}
if (results.RowCount() != 1)
return std::string();
auto row = results.begin();
return std::string(row[0]);
}
/**
* Checks for bucket existence by bucket_name key
* @param bucket_key
* @return
*/
uint64 DataBucket::DoesBucketExist(const std::string& bucket_key) {
std::string query = StringFormat(
"SELECT `id` from `data_buckets` WHERE `key` = '%s' AND (`expires` > %lld OR `expires` = 0) LIMIT 1",
Strings::Escape(bucket_key).c_str(),
(long long) std::time(nullptr)
);
auto results = database.QueryDatabase(query);
if (!results.Success()) {
return 0;
}
auto row = results.begin();
if (results.RowCount() != 1)
return 0;
return Strings::ToUnsignedBigInt(row[0]);
}
/**
* Deletes data bucket by key
* @param bucket_key
* @return
*/
bool DataBucket::DeleteData(const std::string& bucket_key) {
std::string query = StringFormat(
"DELETE FROM `data_buckets` WHERE `key` = '%s'",
Strings::Escape(bucket_key).c_str()
);
auto results = database.QueryDatabase(query);
return results.Success();
}
bool DataBucket::GetDataBuckets(Mob* mob)
std::string DataBucket::GetData(const std::string &bucket_key)
{
auto l = BaseDataBucketsRepository::GetWhere(
DataBucketKey k = {};
k.key = bucket_key;
return GetData(k);
}
std::string DataBucket::GetData(const DataBucketKey &k)
{
auto r = DataBucketsRepository::GetWhere(
database,
fmt::format(
"`key` LIKE '{}-%'",
Strings::Escape(mob->GetBucketKey())
"{} `key` = '{}' AND (`expires` > {} OR `expires` = 0) LIMIT 1",
DataBucket::GetScopedDbFilters(k),
k.key,
(long long) std::time(nullptr)
)
);
if (r.empty()) {
return {};
}
return r[0].value;
}
std::string DataBucket::GetDataExpires(const std::string &bucket_key)
{
DataBucketKey k = {};
k.key = bucket_key;
return GetDataExpires(k);
}
std::string DataBucket::GetDataRemaining(const std::string &bucket_key)
{
DataBucketKey k = {};
k.key = bucket_key;
return GetDataRemaining(k);
}
bool DataBucket::DeleteData(const std::string &bucket_key)
{
DataBucketKey r = {};
r.key = bucket_key;
return DeleteData(r);
}
bool DataBucket::GetDataBuckets(Mob *mob)
{
DataBucketKey k = mob->GetScopedBucketKeys();
auto l = BaseDataBucketsRepository::GetWhere(
database,
fmt::format(
"{} (`expires` > {} OR `expires` = 0)",
DataBucket::GetScopedDbFilters(k),
(long long) std::time(nullptr)
)
);
@@ -179,10 +136,10 @@ bool DataBucket::GetDataBuckets(Mob* mob)
DataBucketCache d;
for (const auto& e : l) {
d.bucket_id = e.id;
d.bucket_key = e.key_;
d.bucket_value = e.value;
for (const auto &e: l) {
d.bucket_id = e.id;
d.bucket_key = e.key_;
d.bucket_value = e.value;
d.bucket_expires = e.expires;
mob->m_data_bucket_cache.emplace_back(d);
@@ -191,11 +148,11 @@ bool DataBucket::GetDataBuckets(Mob* mob)
return true;
}
std::string DataBucket::CheckBucketKey(const Mob* mob, std::string_view full_name)
std::string DataBucket::CheckBucketKey(const Mob *mob, const DataBucketKey &k)
{
std::string bucket_value;
for (const auto &d : mob->m_data_bucket_cache) {
if (d.bucket_key == full_name) {
std::string bucket_value;
for (const auto &d: mob->m_data_bucket_cache) {
if (d.bucket_key == k.key) {
bucket_value = d.bucket_value;
break;
}
@@ -203,3 +160,72 @@ std::string DataBucket::CheckBucketKey(const Mob* mob, std::string_view full_nam
return bucket_value;
}
bool DataBucket::DeleteData(const DataBucketKey &k)
{
return DataBucketsRepository::DeleteWhere(
database,
fmt::format(
"{} `key` = '{}'",
DataBucket::GetScopedDbFilters(k),
k.key
)
);
}
std::string DataBucket::GetDataExpires(const DataBucketKey &k)
{
auto r = DataBucketsRepository::GetWhere(
database,
fmt::format(
"{} `key` = '{}' AND (`expires` > {} OR `expires` = 0) LIMIT 1",
DataBucket::GetScopedDbFilters(k),
k.key,
(long long) std::time(nullptr)
)
);
if (r.empty()) {
return {};
}
return fmt::format("{}", r[0].expires);
}
std::string DataBucket::GetDataRemaining(const DataBucketKey &k)
{
auto r = DataBucketsRepository::GetWhere(
database,
fmt::format(
"{} `key` = '{}' AND (`expires` > {} OR `expires` = 0) LIMIT 1",
DataBucket::GetScopedDbFilters(k),
k.key,
(long long) std::time(nullptr)
)
);
if (r.empty()) {
return "0";
}
return fmt::format("{}", r[0].expires - (long long) std::time(nullptr));
}
std::string DataBucket::GetScopedDbFilters(const DataBucketKey &k)
{
std::vector<std::string> query = {};
if (k.character_id > 0) {
query.emplace_back(fmt::format("character_id = {}", k.character_id));
}
else if (k.npc_id > 0) {
query.emplace_back(fmt::format("npc_id = {}", k.npc_id));
}
else if (k.bot_id > 0) {
query.emplace_back(fmt::format("bot_id = {}", k.bot_id));
}
return fmt::format(
"{} {}",
Strings::Join(query, " AND "),
!query.empty() ? "AND" : ""
);
}