Make it so DataBuckets set_data can take time formats such as 1s, 1m, 1d, 1y

This commit is contained in:
Akkadius
2018-07-10 02:24:55 -05:00
parent 623b7b3eee
commit e4d8915c9d
5 changed files with 77 additions and 13 deletions
+57 -5
View File
@@ -3,21 +3,34 @@
#include "../common/string_util.h"
#include "zonedb.h"
#include <ctime>
#include <cctype>
#include <algorithm>
/**
* Persists data via bucket_name as key
* @param bucket_key
* @param bucket_value
* @param expires_time
*/
void DataBucket::SetData(std::string bucket_key, std::string bucket_value, uint32 expires_at_unix) {
void DataBucket::SetData(std::string bucket_key, std::string bucket_value, std::string expires_time) {
uint64 bucket_id = DataBucket::DoesBucketExist(bucket_key);
std::string query;
long long expires_time_unix = 0;
if (!expires_time.empty()) {
if (isalpha(expires_time[0]) || isalpha(expires_time[expires_time.length() - 1])) {
Log(Logs::General, Logs::Normal, "String check successful");
expires_time_unix = (long long) std::time(nullptr) + DataBucket::ParseStringTimeToInt(expires_time);
} else {
expires_time_unix = (long long) std::time(nullptr) + atoi(expires_time.c_str());
}
}
if (bucket_id > 0) {
std::string update_expired_time;
if (expires_at_unix > 0) {
update_expired_time = StringFormat(", `expires` = %u ", expires_at_unix);
if (expires_time_unix > 0) {
update_expired_time = StringFormat(", `expires` = %lld ", expires_time_unix);
}
query = StringFormat(
@@ -29,10 +42,10 @@ void DataBucket::SetData(std::string bucket_key, std::string bucket_value, uint3
}
else {
query = StringFormat(
"INSERT INTO `data_buckets` (`key`, `value`, `expires`) VALUES ('%s', '%s', '%u')",
"INSERT INTO `data_buckets` (`key`, `value`, `expires`) VALUES ('%s', '%s', '%lld')",
EscapeString(bucket_key).c_str(),
EscapeString(bucket_value).c_str(),
expires_at_unix
expires_time_unix
);
}
@@ -102,4 +115,43 @@ bool DataBucket::DeleteData(std::string bucket_key) {
auto results = database.QueryDatabase(query);
return results.Success();
}
/**
* Converts string to integer for use when setting expiration times
* @param time_string
* @return
*/
uint32 DataBucket::ParseStringTimeToInt(std::string time_string)
{
uint32 duration = 0;
std::transform(time_string.begin(), time_string.end(), time_string.begin(), ::tolower);
if (time_string.length() < 1)
return 0;
std::string time_unit = time_string;
time_unit.erase(remove_if(time_unit.begin(), time_unit.end(), [](char c) { return !isdigit(c); }), time_unit.end());
Log(Logs::General, Logs::Normal, "ParseStringTimeToInt after erase %s", time_unit.c_str());
uint32 unit = static_cast<uint32>(atoi(time_unit.c_str()));
Log(Logs::General, Logs::Normal, "ParseStringTimeToInt seconds %u string %s", unit, time_string.c_str());
if (time_string.find('s') != std::string::npos)
duration = unit;
if (time_string.find('m') != std::string::npos)
duration = unit * 60;
if (time_string.find('h') != std::string::npos)
duration = unit * 3600;
if (time_string.find('d') != std::string::npos)
duration = unit * 86400;
if (time_string.find('y') != std::string::npos)
duration = unit * 31556926;
Log(Logs::General, Logs::Normal, "ParseStringTimeToInt returning %u", duration);
return duration;
}