Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Implemented qglobals replacement and/or alternative called "Data Buck…
…ets" see changelog for more details
  • Loading branch information
Akkadius committed Jul 8, 2018
1 parent 69f621f commit 41ab512
Show file tree
Hide file tree
Showing 12 changed files with 234 additions and 3 deletions.
4 changes: 4 additions & 0 deletions changelog.txt
@@ -1,5 +1,9 @@
EQEMu Changelog (Started on Sept 24, 2003 15:50)
-------------------------------------------------------
== 07/07/2018 ==
Akkadius: Implemented a much better replacement for qglobals called 'DataBuckets'
- A much more detailed example can be found at: https://github.com/EQEmu/Server/wiki/Data-Buckets

== 07/05/2018 ==
Uleat: Reintegration of inventory-based EQDictionary references
- Standardized 'CONSTANT_DECLARATION' and 'enumerationValue' tokens for most of the affected references
Expand Down
9 changes: 9 additions & 0 deletions common/database.cpp
Expand Up @@ -1752,6 +1752,15 @@ void Database::ClearRaidDetails(uint32 rid) {
std::cout << "Unable to clear raid details: " << results.ErrorMessage() << std::endl;
}

void Database::PurgeAllDeletedDataBuckets() {
std::string query = StringFormat(
"DELETE FROM `data_buckets` WHERE (`expires` < %lld AND `expires` > 0)",
(long long) std::time(nullptr)
);

QueryDatabase(query);
}

// returns 0 on error or no raid for that character, or
// the raid id that the character is a member of.
uint32 Database::GetRaidID(const char* name)
Expand Down
2 changes: 2 additions & 0 deletions common/database.h
Expand Up @@ -221,6 +221,8 @@ class Database : public DBcore {
void GetRaidLeadershipInfo(uint32 rid, char* maintank = nullptr, char* assist = nullptr, char* puller = nullptr, char *marknpc = nullptr, RaidLeadershipAA_Struct* RLAA = nullptr);
void SetRaidGroupLeaderInfo(uint32 gid, uint32 rid);

void PurgeAllDeletedDataBuckets();

/* Database Conversions 'database_conversions.cpp' */

bool CheckDatabaseConversions();
Expand Down
2 changes: 1 addition & 1 deletion common/version.h
Expand Up @@ -30,7 +30,7 @@
Manifest: https://github.com/EQEmu/Server/blob/master/utils/sql/db_update_manifest.txt
*/

#define CURRENT_BINARY_DATABASE_VERSION 9122
#define CURRENT_BINARY_DATABASE_VERSION 9123
#ifdef BOTS
#define CURRENT_BINARY_BOTS_DATABASE_VERSION 9019
#else
Expand Down
1 change: 1 addition & 0 deletions utils/sql/db_update_manifest.txt
Expand Up @@ -376,6 +376,7 @@
9120|2018_02_13_Heading.sql|SELECT value FROM variables WHERE varname = 'fixed_heading'|empty|
9121|2018_02_18_bug_reports.sql|SHOW TABLES LIKE 'bug_reports'|empty|
9122|2018_03_07_ucs_command.sql|SELECT * FROM `command_settings` WHERE `command` LIKE 'ucs'|empty|
9123|2018_07_07_data_buckets.sql|SHOW TABLES LIKE 'data_buckets'|empty|

# Upgrade conditions:
# This won't be needed after this system is implemented, but it is used database that are not
Expand Down
8 changes: 8 additions & 0 deletions utils/sql/git/required/2018_07_07_data_buckets.sql
@@ -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;
9 changes: 7 additions & 2 deletions world/net.cpp
Expand Up @@ -85,6 +85,7 @@ union semun {
#include "console.h"

#include "../common/net/servertalk_server.h"
#include "../zone/data_bucket.h"

ClientList client_list;
GroupLFPList LFPGroupList;
Expand Down Expand Up @@ -309,6 +310,9 @@ int main(int argc, char** argv) {
}
}

Log(Logs::General, Logs::World_Server, "Purging expired data buckets...");
database.PurgeAllDeletedDataBuckets();

Log(Logs::General, Logs::World_Server, "Loading zones..");
database.LoadZoneNames();
Log(Logs::General, Logs::World_Server, "Clearing groups..");
Expand Down Expand Up @@ -389,6 +393,7 @@ int main(int argc, char** argv) {

Log(Logs::General, Logs::World_Server, "Purging expired instances");
database.PurgeExpiredInstances();

Timer PurgeInstanceTimer(450000);
PurgeInstanceTimer.Start(450000);

Expand Down Expand Up @@ -545,9 +550,9 @@ int main(int argc, char** argv) {

client_list.Process();

if (PurgeInstanceTimer.Check())
{
if (PurgeInstanceTimer.Check()) {
database.PurgeExpiredInstances();
database.PurgeAllDeletedDataBuckets();
}

if (EQTimeTimer.Check()) {
Expand Down
2 changes: 2 additions & 0 deletions zone/CMakeLists.txt
Expand Up @@ -19,6 +19,7 @@ SET(zone_sources
client_process.cpp
command.cpp
corpse.cpp
data_bucket.cpp
doors.cpp
effects.cpp
embparser.cpp
Expand Down Expand Up @@ -155,6 +156,7 @@ SET(zone_headers
command.h
common.h
corpse.h
data_bucket.h
doors.h
embparser.h
embperl.h
Expand Down
105 changes: 105 additions & 0 deletions zone/data_bucket.cpp
@@ -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();
}
22 changes: 22 additions & 0 deletions zone/data_bucket.h
@@ -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
52 changes: 52 additions & 0 deletions zone/embparser_api.cpp
Expand Up @@ -31,6 +31,7 @@
#include "queryserv.h"
#include "questmgr.h"
#include "zone.h"
#include "data_bucket.h"

extern Zone *zone;
extern QueryServ *QServ;
Expand Down Expand Up @@ -3501,6 +3502,54 @@ XS(XS__UpdateZoneHeader) {
XSRETURN_EMPTY;
}

XS(XS__get_data);
XS(XS__get_data) {
dXSARGS;
if (items != 1)
Perl_croak(aTHX_ "Usage: quest::get_data(string bucket_key)");

dXSTARG;
std::string key = (std::string) SvPV_nolen(ST(0));

sv_setpv(TARG, DataBucket::GetData(key).c_str());
XSprePUSH;
PUSHTARG;
XSRETURN(1);
}

XS(XS__set_data);
XS(XS__set_data) {
dXSARGS;
if (items != 2 && items != 3) {
Perl_croak(aTHX_ "Usage: quest::set_data(string key, string value, [uint32 expire_time_unix = 0])");
} else {
std::string key = (std::string) SvPV_nolen(ST(0));
std::string value = (std::string) SvPV_nolen(ST(1));

uint32 expires_at_unix = 0;
if (items == 3)
expires_at_unix = (uint32) SvIV(ST(2));

DataBucket::SetData(key, value, expires_at_unix);
}
XSRETURN_EMPTY;
}

XS(XS__delete_data);
XS(XS__delete_data) {
dXSARGS;
if (items != 1)
Perl_croak(aTHX_ "Usage: quest::delete_data(string bucket_key)");

dXSTARG;
std::string key = (std::string) SvPV_nolen(ST(0));

XSprePUSH;
PUSHu((IV) DataBucket::DeleteData(key));

XSRETURN(1);
}


/*
This is the callback perl will look for to setup the
Expand Down Expand Up @@ -3548,6 +3597,9 @@ EXTERN_C XS(boot_quest) {
newXS(strcpy(buf, "GetTimeSeconds"), XS__GetTimeSeconds, file);
newXS(strcpy(buf, "GetZoneID"), XS__GetZoneID, file);
newXS(strcpy(buf, "GetZoneLongName"), XS__GetZoneLongName, file);
newXS(strcpy(buf, "get_data"), XS__get_data, file);
newXS(strcpy(buf, "set_data"), XS__set_data, file);
newXS(strcpy(buf, "delete_data"), XS__delete_data, file);
newXS(strcpy(buf, "IsBeneficialSpell"), XS__IsBeneficialSpell, file);
newXS(strcpy(buf, "IsEffectInSpell"), XS__IsEffectInSpell, file);
newXS(strcpy(buf, "IsRunning"), XS__IsRunning, file);
Expand Down
21 changes: 21 additions & 0 deletions zone/lua_general.cpp
Expand Up @@ -22,6 +22,7 @@
#include "qglobals.h"
#include "encounter.h"
#include "lua_encounter.h"
#include "data_bucket.h"

struct Events { };
struct Factions { };
Expand Down Expand Up @@ -817,6 +818,22 @@ std::string lua_say_link(const char *phrase) {
return std::string(text);
}

std::string lua_get_data(std::string bucket_key) {
return DataBucket::GetData(bucket_key);
}

void lua_set_data(std::string bucket_key, std::string bucket_value) {
DataBucket::SetData(bucket_key, bucket_value);
}

void lua_set_data(std::string bucket_key, std::string bucket_value, uint32 expires_at_unix) {
DataBucket::SetData(bucket_key, bucket_value, expires_at_unix);
}

bool lua_delete_data(std::string bucket_key) {
return DataBucket::DeleteData(bucket_key);
}

const char *lua_get_guild_name_by_id(uint32 guild_id) {
return quest_manager.getguildnamebyid(guild_id);
}
Expand Down Expand Up @@ -1658,6 +1675,10 @@ luabind::scope lua_register_general() {
luabind::def("say_link", (std::string(*)(const char*,bool,const char*))&lua_say_link),
luabind::def("say_link", (std::string(*)(const char*,bool))&lua_say_link),
luabind::def("say_link", (std::string(*)(const char*))&lua_say_link),
luabind::def("get_data", (std::string(*)(std::string))&lua_get_data),
luabind::def("set_data", (void(*)(std::string, std::string))&lua_set_data),
luabind::def("set_data", (void(*)(std::string, std::string, uint32))&lua_set_data),
luabind::def("delete_data", (bool(*)(std::string))&lua_delete_data),
luabind::def("get_guild_name_by_id", &lua_get_guild_name_by_id),
luabind::def("create_instance", &lua_create_instance),
luabind::def("destroy_instance", &lua_destroy_instance),
Expand Down

0 comments on commit 41ab512

Please sign in to comment.