From 520a93836995be6ac4573c5c48c6af0f5fd04742 Mon Sep 17 00:00:00 2001 From: kanoi Date: Sun, 6 Mar 2016 19:35:08 +1100 Subject: [PATCH] ckdb - db shares fix createdate - it's used as the key --- src/ckdb.h | 7 ++++++- src/ckdb_data.c | 29 +++++++++++++++++++++++++++-- src/ckdb_dbio.c | 32 ++++++++++++++++---------------- 3 files changed, 49 insertions(+), 19 deletions(-) diff --git a/src/ckdb.h b/src/ckdb.h index 659a086e..50cdf576 100644 --- a/src/ckdb.h +++ b/src/ckdb.h @@ -51,7 +51,7 @@ #define DB_VLOCK "1" #define DB_VERSION "1.0.5" -#define CKDB_VERSION DB_VERSION"-1.970" +#define CKDB_VERSION DB_VERSION"-1.980" #define WHERE_FFL " - from %s %s() line %d" #define WHERE_FFL_HERE __FILE__, __func__, __LINE__ @@ -252,6 +252,7 @@ enum data_type { TYPE_BIGINT, TYPE_INT, TYPE_TV, + TYPE_TVDB, TYPE_BTV, TYPE_TVS, TYPE_CTV, @@ -269,6 +270,7 @@ enum data_type { #define TXT_TO_BIGINT(__nam, __fld, __data) txt_to_bigint(__nam, __fld, &(__data), sizeof(__data)) #define TXT_TO_INT(__nam, __fld, __data) txt_to_int(__nam, __fld, &(__data), sizeof(__data)) #define TXT_TO_TV(__nam, __fld, __data) txt_to_tv(__nam, __fld, &(__data), sizeof(__data)) +#define TXT_TO_TVDB(__nam, __fld, __data) txt_to_tvdb(__nam, __fld, &(__data), sizeof(__data)) #define TXT_TO_CTV(__nam, __fld, __data) txt_to_ctv(__nam, __fld, &(__data), sizeof(__data)) #define TXT_TO_BLOB(__nam, __fld, __data) txt_to_blob(__nam, __fld, &(__data)) #define TXT_TO_DOUBLE(__nam, __fld, __data) txt_to_double(__nam, __fld, &(__data), sizeof(__data)) @@ -1684,6 +1686,7 @@ typedef struct shares { #define LIMIT_SHARES 0 #define INIT_SHARES(_item) INIT_GENERIC(_item, shares) #define DATA_SHARES(_var, _item) DATA_GENERIC(_var, _item, shares, true) +#define DATA_SHARES_NULL(_var, _item) DATA_GENERIC(_var, _item, shares, false) extern K_TREE *shares_root; extern K_LIST *shares_free; @@ -2644,6 +2647,7 @@ extern void _txt_to_data(enum data_type typ, char *nam, char *fld, void *data, s #define txt_to_bigint(_nam, _fld, _data, _siz) _txt_to_bigint(_nam, _fld, _data, _siz, WHERE_FFL_HERE) #define txt_to_int(_nam, _fld, _data, _siz) _txt_to_int(_nam, _fld, _data, _siz, WHERE_FFL_HERE) #define txt_to_tv(_nam, _fld, _data, _siz) _txt_to_tv(_nam, _fld, _data, _siz, WHERE_FFL_HERE) +#define txt_to_tvdb(_nam, _fld, _data, _siz) _txt_to_tvdb(_nam, _fld, _data, _siz, WHERE_FFL_HERE) #define txt_to_ctv(_nam, _fld, _data, _siz) _txt_to_ctv(_nam, _fld, _data, _siz, WHERE_FFL_HERE) #define txt_to_blob(_nam, _fld, _data) _txt_to_blob(_nam, _fld, _data, WHERE_FFL_HERE) #define txt_to_double(_nam, _fld, _data, _siz) _txt_to_double(_nam, _fld, _data, _siz, WHERE_FFL_HERE) @@ -2653,6 +2657,7 @@ extern void _txt_to_str(char *nam, char *fld, char data[], size_t siz, WHERE_FFL extern void _txt_to_bigint(char *nam, char *fld, int64_t *data, size_t siz, WHERE_FFL_ARGS); extern void _txt_to_int(char *nam, char *fld, int32_t *data, size_t siz, WHERE_FFL_ARGS); extern void _txt_to_tv(char *nam, char *fld, tv_t *data, size_t siz, WHERE_FFL_ARGS); +extern void _txt_to_tvdb(char *nam, char *fld, tv_t *data, size_t siz, WHERE_FFL_ARGS); // Convert msg S,nS to tv_t extern void _txt_to_ctv(char *nam, char *fld, tv_t *data, size_t siz, WHERE_FFL_ARGS); extern void _txt_to_blob(char *nam, char *fld, char **data, WHERE_FFL_ARGS); diff --git a/src/ckdb_data.c b/src/ckdb_data.c index 470ec20b..c8e8b3c9 100644 --- a/src/ckdb_data.c +++ b/src/ckdb_data.c @@ -282,7 +282,7 @@ bool like_address(char *username) void _txt_to_data(enum data_type typ, char *nam, char *fld, void *data, size_t siz, WHERE_FFL_ARGS) { - char *tmp; + char *dot, *tmp; switch (typ) { case TYPE_STR: @@ -310,6 +310,7 @@ void _txt_to_data(enum data_type typ, char *nam, char *fld, void *data, size_t s *((int32_t *)data) = atoi(fld); break; case TYPE_TV: + case TYPE_TVDB: if (siz != sizeof(tv_t)) { quithere(1, "Field %s tv_t incorrect structure size %d - should be %d" WHERE_FFL, @@ -319,7 +320,7 @@ void _txt_to_data(enum data_type typ, char *nam, char *fld, void *data, size_t s char pm[2]; struct tm tm; time_t tim; - int n; + int n, d; // A timezone looks like: +10 or +09:30 or -05 etc n = sscanf(fld, "%u-%u-%u %u:%u:%u%1[+-]%u:%u", &yyyy, &mm, &dd, &HH, &MM, &SS, pm, &tz, &tzm); @@ -334,6 +335,23 @@ void _txt_to_data(enum data_type typ, char *nam, char *fld, void *data, size_t s if (n < 10) tzm = 0; + + // DB uS isn't zero padded on the right + if (typ == TYPE_TVDB && uS < 100000) { + dot = strchr(fld, '.'); + if (!dot) { + // impossible? + quithere(1, "Field %s tv_t missing '.' in date " + "'%s' (%d)" WHERE_FFL, + nam, fld, n, WHERE_FFL_PASS); + } + tmp = dot; + while (*tmp && *tmp != '+' && *tmp != '-') + tmp++; + d = (int)(tmp - dot); + while (d++ < 7) + uS *= 10; + } } else if (n < 9) tzm = 0; tm.tm_sec = (int)SS; @@ -425,6 +443,11 @@ void _txt_to_tv(char *nam, char *fld, tv_t *data, size_t siz, WHERE_FFL_ARGS) _txt_to_data(TYPE_TV, nam, fld, (void *)data, siz, WHERE_FFL_PASS); } +void _txt_to_tvdb(char *nam, char *fld, tv_t *data, size_t siz, WHERE_FFL_ARGS) +{ + _txt_to_data(TYPE_TVDB, nam, fld, (void *)data, siz, WHERE_FFL_PASS); +} + // Convert msg S[,nS] to tv_t void _txt_to_ctv(char *nam, char *fld, tv_t *data, size_t siz, WHERE_FFL_ARGS) { @@ -459,6 +482,7 @@ char *_data_to_buf(enum data_type typ, void *data, char *buf, size_t siz, WHERE_ siz = INT_BUFSIZ; break; case TYPE_TV: + case TYPE_TVDB: case TYPE_TVS: case TYPE_BTV: case TYPE_T: @@ -495,6 +519,7 @@ char *_data_to_buf(enum data_type typ, void *data, char *buf, size_t siz, WHERE_ snprintf(buf, siz, "%"PRId32, *((uint32_t *)data)); break; case TYPE_TV: + case TYPE_TVDB: gmtime_r(&(((tv_t *)data)->tv_sec), &tm); snprintf(buf, siz, "%d-%02d-%02d %02d:%02d:%02d.%06ld+00", tm.tm_year + 1900, diff --git a/src/ckdb_dbio.c b/src/ckdb_dbio.c index 0a22b4ab..45e2e52d 100644 --- a/src/ckdb_dbio.c +++ b/src/ckdb_dbio.c @@ -56,7 +56,7 @@ char *pqerrmsg(PGconn *conn) PQ_GET_FLD(_res, _row, CDDB, _fld, _ok); \ if (!_ok) \ break; \ - TXT_TO_TV(CDDB, _fld, (_data)->createdate); \ + TXT_TO_TVDB(CDDB, _fld, (_data)->createdate); \ PQ_GET_FLD(_res, _row, BYDB, _fld, _ok); \ if (!_ok) \ break; \ @@ -72,7 +72,7 @@ char *pqerrmsg(PGconn *conn) PQ_GET_FLD(_res, _row, EDDB, _fld, _ok); \ if (!_ok) \ break; \ - TXT_TO_TV(EDDB, _fld, (_data)->expirydate); \ + TXT_TO_TVDB(EDDB, _fld, (_data)->expirydate); \ } while (0) #define HISTORYDATEPARAMS(_params, _his_pos, _row) do { \ @@ -89,7 +89,7 @@ char *pqerrmsg(PGconn *conn) PQ_GET_FLD(_res, _row, CDDB, _fld, _ok); \ if (!_ok) \ break; \ - TXT_TO_TV(CDDB, _fld, (_data)->createdate); \ + TXT_TO_TVDB(CDDB, _fld, (_data)->createdate); \ PQ_GET_FLD(_res, _row, BYDB, _fld, _ok); \ if (!_ok) \ break; \ @@ -105,7 +105,7 @@ char *pqerrmsg(PGconn *conn) PQ_GET_FLD(_res, _row, MDDB, _fld, _ok); \ if (!_ok) \ break; \ - TXT_TO_TV(MDDB, _fld, (_data)->modifydate); \ + TXT_TO_TVDB(MDDB, _fld, (_data)->modifydate); \ PQ_GET_FLD(_res, _row, MBYDB, _fld, _ok); \ if (!_ok) \ break; \ @@ -144,7 +144,7 @@ char *pqerrmsg(PGconn *conn) PQ_GET_FLD(_res, _row, CDDB, _fld, _ok); \ if (!_ok) \ break; \ - TXT_TO_TV(CDDB, _fld, (_data)->createdate); \ + TXT_TO_TVDB(CDDB, _fld, (_data)->createdate); \ PQ_GET_FLD(_res, _row, BYDB, _fld, _ok); \ if (!_ok) \ break; \ @@ -857,7 +857,7 @@ bool users_fill(PGconn *conn) PQ_GET_FLD(res, i, "joineddate", field, ok); if (!ok) break; - TXT_TO_TV("joineddate", field, row->joineddate); + TXT_TO_TVDB("joineddate", field, row->joineddate); PQ_GET_FLD(res, i, "passwordhash", field, ok); if (!ok) @@ -1268,12 +1268,12 @@ bool useratts_fill(PGconn *conn) PQ_GET_FLD(res, i, "attdate", field, ok); if (!ok) break; - TXT_TO_TV("attdate", field, row->attdate); + TXT_TO_TVDB("attdate", field, row->attdate); PQ_GET_FLD(res, i, "attdate2", field, ok); if (!ok) break; - TXT_TO_TV("attdate2", field, row->attdate2); + TXT_TO_TVDB("attdate2", field, row->attdate2); HISTORYDATEFLDS(res, i, row, ok); if (!ok) @@ -2310,7 +2310,7 @@ bool payments_fill(PGconn *conn) PQ_GET_FLD(res, i, "paydate", field, ok); if (!ok) break; - TXT_TO_TV("paydate", field, row->paydate); + TXT_TO_TVDB("paydate", field, row->paydate); PQ_GET_FLD(res, i, "payaddress", field, ok); if (!ok) @@ -2958,7 +2958,7 @@ bool optioncontrol_fill(PGconn *conn) PQ_GET_FLD(res, i, "activationdate", field, ok); if (!ok) break; - TXT_TO_TV("activationdate", field, row->activationdate); + TXT_TO_TVDB("activationdate", field, row->activationdate); PQ_GET_FLD(res, i, "activationheight", field, ok); if (!ok) @@ -3733,7 +3733,7 @@ bool shares_add(PGconn *conn, char *workinfoid, char *username, char *workername // Store them in advance - always add_to_ktree(shares_hi_root, s2_item); add_to_ktree(shares_db_root, s2_item); - k_add_head(shares_hi_store, s_item); + k_add_head(shares_hi_store, s2_item); } else { k_add_head(shares_free, s2_item); s2_item = NULL; @@ -6667,7 +6667,7 @@ bool payouts_fill(PGconn *conn) PQ_GET_FLD(res, i, "lastshareacc", field, ok); if (!ok) break; - TXT_TO_TV("lastshareacc", field, row->lastshareacc); + TXT_TO_TVDB("lastshareacc", field, row->lastshareacc); PQ_GET_FLD(res, i, "stats", field, ok); if (!ok) @@ -7884,22 +7884,22 @@ bool markersummary_fill(PGconn *conn) PQ_GET_FLD(res, i, "firstshare", field, ok); if (!ok) break; - TXT_TO_TV("firstshare", field, row->firstshare); + TXT_TO_TVDB("firstshare", field, row->firstshare); PQ_GET_FLD(res, i, "lastshare", field, ok); if (!ok) break; - TXT_TO_TV("lastshare", field, row->lastshare); + TXT_TO_TVDB("lastshare", field, row->lastshare); PQ_GET_FLD(res, i, "firstshareacc", field, ok); if (!ok) break; - TXT_TO_TV("firstshareacc", field, row->firstshareacc); + TXT_TO_TVDB("firstshareacc", field, row->firstshareacc); PQ_GET_FLD(res, i, "lastshareacc", field, ok); if (!ok) break; - TXT_TO_TV("lastshareacc", field, row->lastshareacc); + TXT_TO_TVDB("lastshareacc", field, row->lastshareacc); PQ_GET_FLD(res, i, "lastdiffacc", field, ok); if (!ok)