From cd0facf0e91e005e28a30a9c8c4abf6067400b85 Mon Sep 17 00:00:00 2001 From: kanoi Date: Tue, 4 Nov 2014 14:42:11 +1100 Subject: [PATCH] ckdb - keep address history for txn payment processing --- src/ckdb.h | 2 +- src/ckdb_data.c | 1 + src/ckdb_dbio.c | 29 +++++++++++++++++------------ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/ckdb.h b/src/ckdb.h index dd478494..a826f09d 100644 --- a/src/ckdb.h +++ b/src/ckdb.h @@ -52,7 +52,7 @@ #define DB_VLOCK "1" #define DB_VERSION "0.9.2" -#define CKDB_VERSION DB_VERSION"-0.586" +#define CKDB_VERSION DB_VERSION"-0.590" #define WHERE_FFL " - from %s %s() line %d" #define WHERE_FFL_HERE __FILE__, __func__, __LINE__ diff --git a/src/ckdb_data.c b/src/ckdb_data.c index 30a113f3..32976426 100644 --- a/src/ckdb_data.c +++ b/src/ckdb_data.c @@ -1040,6 +1040,7 @@ cmp_t cmp_paymentaddresses(K_ITEM *a, K_ITEM *b) return c; } +// Only one for now ... K_ITEM *find_paymentaddresses(int64_t userid) { PAYMENTADDRESSES paymentaddresses, *pa; diff --git a/src/ckdb_dbio.c b/src/ckdb_dbio.c index 4b4f446c..5c7c1482 100644 --- a/src/ckdb_dbio.c +++ b/src/ckdb_dbio.c @@ -1424,7 +1424,9 @@ bool workers_fill(PGconn *conn) return ok; } -// Whatever the current paymentaddresses are, replace them with this one +/* Whatever the current paymentaddresses are, replace them with this one + * Code allows for zero, one or more current payment address + * even though there currently can only be zero or one */ K_ITEM *paymentaddresses_set(PGconn *conn, int64_t userid, char *payaddress, char *by, char *code, char *inet, tv_t *cd, K_TREE *trf_root) @@ -1529,12 +1531,13 @@ unitem: if (!ok) k_add_head(paymentaddresses_free, item); else { - // Remove from ram, old (unneeded) records + // Change the expiry on all the old ones pa.userid = userid; pa.expirydate.tv_sec = DATE_S_EOT; pa.payaddress[0] = '\0'; INIT_PAYMENTADDRESSES(&look); look.data = (void *)(&pa); + // Tree order is expirydate desc old = find_after_in_ktree(paymentaddresses_root, &look, cmp_paymentaddresses, ctx); while (old) { @@ -1543,9 +1546,15 @@ unitem: if (thispa->userid != userid) break; old = next_in_ktree(ctx); - paymentaddresses_root = remove_from_ktree(paymentaddresses_root, this, - cmp_paymentaddresses, ctx2); - k_add_head(paymentaddresses_free, this); + /* Tree remove+add below doesn't matter since + * this test will avoid reprocessing */ + if (CURRENT(&(thispa->expirydate))) { + paymentaddresses_root = remove_from_ktree(paymentaddresses_root, this, + cmp_paymentaddresses, ctx2); + copy_tv(&(thispa->expirydate), cd); + paymentaddresses_root = add_to_ktree(paymentaddresses_root, this, + cmp_paymentaddresses); + } } paymentaddresses_root = add_to_ktree(paymentaddresses_root, item, cmp_paymentaddresses); @@ -1565,8 +1574,7 @@ bool paymentaddresses_fill(PGconn *conn) PGresult *res; K_ITEM *item; PAYMENTADDRESSES *row; - char *params[1]; - int n, i, par = 0; + int n, i; char *field; char *sel; int fields = 4; @@ -1577,11 +1585,8 @@ bool paymentaddresses_fill(PGconn *conn) sel = "select " "paymentaddressid,userid,payaddress,payratio" HISTORYDATECONTROL - " from paymentaddresses where expirydate=$1"; - par = 0; - params[par++] = tv_to_buf((tv_t *)(&default_expiry), NULL, 0); - PARCHK(par, params); - res = PQexecParams(conn, sel, par, NULL, (const char **)params, NULL, NULL, 0, CKPQ_READ); + " from paymentaddresses"; + res = PQexec(conn, sel, CKPQ_READ); rescode = PQresultStatus(res); if (!PGOK(rescode)) { PGLOGERR("Select", rescode, conn);