Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Implemented qglobals replacement and/or alternative called "Data Buck…
…ets" see changelog for more details
- Loading branch information
Showing
with
234 additions
and 3 deletions.
- +4 −0 changelog.txt
- +9 −0 common/database.cpp
- +2 −0 common/database.h
- +1 −1 common/version.h
- +1 −0 utils/sql/db_update_manifest.txt
- +8 −0 utils/sql/git/required/2018_07_07_data_buckets.sql
- +7 −2 world/net.cpp
- +2 −0 zone/CMakeLists.txt
- +105 −0 zone/data_bucket.cpp
- +22 −0 zone/data_bucket.h
- +52 −0 zone/embparser_api.cpp
- +21 −0 zone/lua_general.cpp
@@ -0,0 +1,8 @@ | ||
CREATE TABLE `data_buckets` ( | ||
`id` bigint(11) unsigned NOT NULL AUTO_INCREMENT, | ||
`key` varchar(100) DEFAULT NULL, | ||
`value` text, | ||
`expires` int(11) unsigned DEFAULT '0', | ||
PRIMARY KEY (`id`), | ||
KEY `key_index` (`key`) USING BTREE | ||
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4; |
@@ -0,0 +1,105 @@ | ||
#include "data_bucket.h" | ||
#include <utility> | ||
#include "../common/string_util.h" | ||
#include "zonedb.h" | ||
#include <ctime> | ||
|
||
/** | ||
* Persists data via bucket_name as key | ||
* @param bucket_key | ||
* @param bucket_value | ||
*/ | ||
void DataBucket::SetData(std::string bucket_key, std::string bucket_value, uint32 expires_at_unix) { | ||
uint64 bucket_id = DataBucket::DoesBucketExist(bucket_key); | ||
|
||
std::string query; | ||
|
||
if (bucket_id > 0) { | ||
std::string update_expired_time; | ||
if (expires_at_unix > 0) { | ||
update_expired_time = StringFormat(", `expires` = %u ", expires_at_unix); | ||
} | ||
|
||
query = StringFormat( | ||
"UPDATE `data_buckets` SET `value` = '%s' %s WHERE `id` = %i", | ||
EscapeString(bucket_value).c_str(), | ||
EscapeString(update_expired_time).c_str(), | ||
bucket_id | ||
); | ||
} | ||
else { | ||
query = StringFormat( | ||
"INSERT INTO `data_buckets` (`key`, `value`, `expires`) VALUES ('%s', '%s', '%u')", | ||
EscapeString(bucket_key).c_str(), | ||
EscapeString(bucket_value).c_str(), | ||
expires_at_unix | ||
); | ||
} | ||
|
||
database.QueryDatabase(query); | ||
} | ||
|
||
/** | ||
* Retrieves data via bucket_name as key | ||
* @param bucket_key | ||
* @return | ||
*/ | ||
std::string DataBucket::GetData(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]); | ||
} | ||
|
||
/** | ||
* Checks for bucket existence by bucket_name key | ||
* @param bucket_key | ||
* @return | ||
*/ | ||
uint64 DataBucket::DoesBucketExist(std::string bucket_key) { | ||
std::string query = StringFormat( | ||
"SELECT `id` from `data_buckets` WHERE `key` = '%s' AND (`expires` > %lld OR `expires` = 0) LIMIT 1", | ||
EscapeString(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 std::stoull(row[0]); | ||
} | ||
|
||
/** | ||
* Deletes data bucket by key | ||
* @param bucket_key | ||
* @return | ||
*/ | ||
bool DataBucket::DeleteData(std::string bucket_key) { | ||
std::string query = StringFormat( | ||
"DELETE FROM `data_buckets` WHERE `key` = '%s' AND (`expires` > %lld OR `expires` = 0)", | ||
EscapeString(bucket_key).c_str() | ||
); | ||
|
||
auto results = database.QueryDatabase(query); | ||
|
||
return results.Success(); | ||
} |
@@ -0,0 +1,22 @@ | ||
// | ||
// Created by Akkadius on 7/7/18. | ||
// | ||
|
||
#ifndef EQEMU_DATABUCKET_H | ||
#define EQEMU_DATABUCKET_H | ||
|
||
|
||
#include <string> | ||
#include "../common/types.h" | ||
|
||
class DataBucket { | ||
public: | ||
static void SetData(std::string bucket_key, std::string bucket_value, uint32 expires_at_unix = 0); | ||
static bool DeleteData(std::string bucket_key); | ||
static std::string GetData(std::string bucket_key); | ||
private: | ||
static uint64 DoesBucketExist(std::string bucket_key); | ||
}; | ||
|
||
|
||
#endif //EQEMU_DATABUCKET_H |