Skip to content

Commit 41ab512

Browse files
committed
Implemented qglobals replacement and/or alternative called "Data Buckets" see changelog for more details
1 parent 69f621f commit 41ab512

File tree

12 files changed

+234
-3
lines changed

12 files changed

+234
-3
lines changed

changelog.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
EQEMu Changelog (Started on Sept 24, 2003 15:50)
22
-------------------------------------------------------
3+
== 07/07/2018 ==
4+
Akkadius: Implemented a much better replacement for qglobals called 'DataBuckets'
5+
- A much more detailed example can be found at: https://github.com/EQEmu/Server/wiki/Data-Buckets
6+
37
== 07/05/2018 ==
48
Uleat: Reintegration of inventory-based EQDictionary references
59
- Standardized 'CONSTANT_DECLARATION' and 'enumerationValue' tokens for most of the affected references

common/database.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1752,6 +1752,15 @@ void Database::ClearRaidDetails(uint32 rid) {
17521752
std::cout << "Unable to clear raid details: " << results.ErrorMessage() << std::endl;
17531753
}
17541754

1755+
void Database::PurgeAllDeletedDataBuckets() {
1756+
std::string query = StringFormat(
1757+
"DELETE FROM `data_buckets` WHERE (`expires` < %lld AND `expires` > 0)",
1758+
(long long) std::time(nullptr)
1759+
);
1760+
1761+
QueryDatabase(query);
1762+
}
1763+
17551764
// returns 0 on error or no raid for that character, or
17561765
// the raid id that the character is a member of.
17571766
uint32 Database::GetRaidID(const char* name)

common/database.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,8 @@ class Database : public DBcore {
221221
void GetRaidLeadershipInfo(uint32 rid, char* maintank = nullptr, char* assist = nullptr, char* puller = nullptr, char *marknpc = nullptr, RaidLeadershipAA_Struct* RLAA = nullptr);
222222
void SetRaidGroupLeaderInfo(uint32 gid, uint32 rid);
223223

224+
void PurgeAllDeletedDataBuckets();
225+
224226
/* Database Conversions 'database_conversions.cpp' */
225227

226228
bool CheckDatabaseConversions();

common/version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
Manifest: https://github.com/EQEmu/Server/blob/master/utils/sql/db_update_manifest.txt
3131
*/
3232

33-
#define CURRENT_BINARY_DATABASE_VERSION 9122
33+
#define CURRENT_BINARY_DATABASE_VERSION 9123
3434
#ifdef BOTS
3535
#define CURRENT_BINARY_BOTS_DATABASE_VERSION 9019
3636
#else

utils/sql/db_update_manifest.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,7 @@
376376
9120|2018_02_13_Heading.sql|SELECT value FROM variables WHERE varname = 'fixed_heading'|empty|
377377
9121|2018_02_18_bug_reports.sql|SHOW TABLES LIKE 'bug_reports'|empty|
378378
9122|2018_03_07_ucs_command.sql|SELECT * FROM `command_settings` WHERE `command` LIKE 'ucs'|empty|
379+
9123|2018_07_07_data_buckets.sql|SHOW TABLES LIKE 'data_buckets'|empty|
379380

380381
# Upgrade conditions:
381382
# This won't be needed after this system is implemented, but it is used database that are not
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATE TABLE `data_buckets` (
2+
`id` bigint(11) unsigned NOT NULL AUTO_INCREMENT,
3+
`key` varchar(100) DEFAULT NULL,
4+
`value` text,
5+
`expires` int(11) unsigned DEFAULT '0',
6+
PRIMARY KEY (`id`),
7+
KEY `key_index` (`key`) USING BTREE
8+
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4;

world/net.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ union semun {
8585
#include "console.h"
8686

8787
#include "../common/net/servertalk_server.h"
88+
#include "../zone/data_bucket.h"
8889

8990
ClientList client_list;
9091
GroupLFPList LFPGroupList;
@@ -309,6 +310,9 @@ int main(int argc, char** argv) {
309310
}
310311
}
311312

313+
Log(Logs::General, Logs::World_Server, "Purging expired data buckets...");
314+
database.PurgeAllDeletedDataBuckets();
315+
312316
Log(Logs::General, Logs::World_Server, "Loading zones..");
313317
database.LoadZoneNames();
314318
Log(Logs::General, Logs::World_Server, "Clearing groups..");
@@ -389,6 +393,7 @@ int main(int argc, char** argv) {
389393

390394
Log(Logs::General, Logs::World_Server, "Purging expired instances");
391395
database.PurgeExpiredInstances();
396+
392397
Timer PurgeInstanceTimer(450000);
393398
PurgeInstanceTimer.Start(450000);
394399

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

546551
client_list.Process();
547552

548-
if (PurgeInstanceTimer.Check())
549-
{
553+
if (PurgeInstanceTimer.Check()) {
550554
database.PurgeExpiredInstances();
555+
database.PurgeAllDeletedDataBuckets();
551556
}
552557

553558
if (EQTimeTimer.Check()) {

zone/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ SET(zone_sources
1919
client_process.cpp
2020
command.cpp
2121
corpse.cpp
22+
data_bucket.cpp
2223
doors.cpp
2324
effects.cpp
2425
embparser.cpp
@@ -155,6 +156,7 @@ SET(zone_headers
155156
command.h
156157
common.h
157158
corpse.h
159+
data_bucket.h
158160
doors.h
159161
embparser.h
160162
embperl.h

zone/data_bucket.cpp

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#include "data_bucket.h"
2+
#include <utility>
3+
#include "../common/string_util.h"
4+
#include "zonedb.h"
5+
#include <ctime>
6+
7+
/**
8+
* Persists data via bucket_name as key
9+
* @param bucket_key
10+
* @param bucket_value
11+
*/
12+
void DataBucket::SetData(std::string bucket_key, std::string bucket_value, uint32 expires_at_unix) {
13+
uint64 bucket_id = DataBucket::DoesBucketExist(bucket_key);
14+
15+
std::string query;
16+
17+
if (bucket_id > 0) {
18+
std::string update_expired_time;
19+
if (expires_at_unix > 0) {
20+
update_expired_time = StringFormat(", `expires` = %u ", expires_at_unix);
21+
}
22+
23+
query = StringFormat(
24+
"UPDATE `data_buckets` SET `value` = '%s' %s WHERE `id` = %i",
25+
EscapeString(bucket_value).c_str(),
26+
EscapeString(update_expired_time).c_str(),
27+
bucket_id
28+
);
29+
}
30+
else {
31+
query = StringFormat(
32+
"INSERT INTO `data_buckets` (`key`, `value`, `expires`) VALUES ('%s', '%s', '%u')",
33+
EscapeString(bucket_key).c_str(),
34+
EscapeString(bucket_value).c_str(),
35+
expires_at_unix
36+
);
37+
}
38+
39+
database.QueryDatabase(query);
40+
}
41+
42+
/**
43+
* Retrieves data via bucket_name as key
44+
* @param bucket_key
45+
* @return
46+
*/
47+
std::string DataBucket::GetData(std::string bucket_key) {
48+
std::string query = StringFormat(
49+
"SELECT `value` from `data_buckets` WHERE `key` = '%s' AND (`expires` > %lld OR `expires` = 0) LIMIT 1",
50+
bucket_key.c_str(),
51+
(long long) std::time(nullptr)
52+
);
53+
54+
auto results = database.QueryDatabase(query);
55+
if (!results.Success()) {
56+
return std::string();
57+
}
58+
59+
if (results.RowCount() != 1)
60+
return std::string();
61+
62+
auto row = results.begin();
63+
64+
return std::string(row[0]);
65+
}
66+
67+
/**
68+
* Checks for bucket existence by bucket_name key
69+
* @param bucket_key
70+
* @return
71+
*/
72+
uint64 DataBucket::DoesBucketExist(std::string bucket_key) {
73+
std::string query = StringFormat(
74+
"SELECT `id` from `data_buckets` WHERE `key` = '%s' AND (`expires` > %lld OR `expires` = 0) LIMIT 1",
75+
EscapeString(bucket_key).c_str(),
76+
(long long) std::time(nullptr)
77+
);
78+
79+
auto results = database.QueryDatabase(query);
80+
if (!results.Success()) {
81+
return 0;
82+
}
83+
84+
auto row = results.begin();
85+
if (results.RowCount() != 1)
86+
return 0;
87+
88+
return std::stoull(row[0]);
89+
}
90+
91+
/**
92+
* Deletes data bucket by key
93+
* @param bucket_key
94+
* @return
95+
*/
96+
bool DataBucket::DeleteData(std::string bucket_key) {
97+
std::string query = StringFormat(
98+
"DELETE FROM `data_buckets` WHERE `key` = '%s' AND (`expires` > %lld OR `expires` = 0)",
99+
EscapeString(bucket_key).c_str()
100+
);
101+
102+
auto results = database.QueryDatabase(query);
103+
104+
return results.Success();
105+
}

zone/data_bucket.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//
2+
// Created by Akkadius on 7/7/18.
3+
//
4+
5+
#ifndef EQEMU_DATABUCKET_H
6+
#define EQEMU_DATABUCKET_H
7+
8+
9+
#include <string>
10+
#include "../common/types.h"
11+
12+
class DataBucket {
13+
public:
14+
static void SetData(std::string bucket_key, std::string bucket_value, uint32 expires_at_unix = 0);
15+
static bool DeleteData(std::string bucket_key);
16+
static std::string GetData(std::string bucket_key);
17+
private:
18+
static uint64 DoesBucketExist(std::string bucket_key);
19+
};
20+
21+
22+
#endif //EQEMU_DATABUCKET_H

0 commit comments

Comments
 (0)