From 4d7a54d12e88b86a0b373b0322585fe94d5ed03b Mon Sep 17 00:00:00 2001 From: kanoi Date: Fri, 18 Mar 2016 21:55:54 +1100 Subject: [PATCH] ckdb - modify -M to allow more options - days or shifts --- src/ckdb.c | 27 ++++++++++++++-- src/ckdb.h | 5 +-- src/ckdb_dbio.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 108 insertions(+), 9 deletions(-) diff --git a/src/ckdb.c b/src/ckdb.c index 75085444..5ed5b1d5 100644 --- a/src/ckdb.c +++ b/src/ckdb.c @@ -595,7 +595,8 @@ K_TREE *markersummary_pool_root; K_STORE *markersummary_pool_store; // The markerid load start for markersummary -char *mark_start = NULL; +char mark_start_type = '\0'; +int64_t mark_start = -1; // WORKMARKERS K_TREE *workmarkers_root; @@ -5996,7 +5997,29 @@ int main(int argc, char **argv) markersummary_auto = true; break; case 'M': - mark_start = strdup(optarg); + { + bool ok = true; + switch (optarg[0]) { + case 'D': // Days * mark_start + mark_start_type = 'D'; + mark_start = atoll(optarg+1); + break; + case 'S': // Shifts * mark_start + mark_start_type = 'S'; + mark_start = atoll(optarg+1); + break; + case 'M': // Markerid = mark_start + mark_start_type = 'M'; + mark_start = atoll(optarg+1); + break; + default: + ok = false; + break; + } + if (!ok || mark_start <= 0) + quit(1, "Invalid -M must be D, S or" + " M followed by a number>0"); + } break; case 'n': ckp.name = strdup(optarg); diff --git a/src/ckdb.h b/src/ckdb.h index 0d831a6b..9e2f71eb 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.987" +#define CKDB_VERSION DB_VERSION"-1.988" #define WHERE_FFL " - from %s %s() line %d" #define WHERE_FFL_HERE __FILE__, __func__, __LINE__ @@ -2428,7 +2428,8 @@ extern K_TREE *markersummary_pool_root; extern K_STORE *markersummary_pool_store; // The markerid load start for markersummary -extern char *mark_start; +extern char mark_start_type; +extern int64_t mark_start; // WORKMARKERS typedef struct workmarkers { diff --git a/src/ckdb_dbio.c b/src/ckdb_dbio.c index 80117809..f4c33b50 100644 --- a/src/ckdb_dbio.c +++ b/src/ckdb_dbio.c @@ -7755,17 +7755,86 @@ bool markersummary_fill(PGconn *conn) { ExecStatusType rescode; PGresult *res; - K_ITEM *item = NULL, *p_item; + K_ITEM *item = NULL, *p_item, *wm_item = NULL; + K_TREE_CTX ctx[1]; + char cd_buf[DATE_BUFSIZ]; + char *cd = NULL, *what = NULL; int n, t, i, p_n; MARKERSUMMARY *row, *p_row; + WORKMARKERS *workmarkers; char *params[1]; char *field; char *sel; int fields = 20, par = 0; + int64_t ms = 0, amt = 0; bool ok = false; + tv_t old; LOGDEBUG("%s(): select", __func__); + if (mark_start < 0) + mark_start = 0; + else { + amt = ms = mark_start; + switch (mark_start_type) { + case 'D': // mark_start days + setnow(&old); + old.tv_sec -= 60 * 60 * 24 * ms; + K_RLOCK(workmarkers_free); + wm_item = last_in_ktree(workmarkers_root, ctx); + while (wm_item) { + // Newest processed workmarker <= old + DATA_WORKMARKERS(workmarkers, wm_item); + if (CURRENT(&(workmarkers->expirydate)) && + WMPROCESSED(workmarkers->status) && + !tv_newer(&old, &(workmarkers->createdate))) + break; + wm_item = prev_in_ktree(ctx); + } + if (!wm_item) + mark_start = 0; + else { + mark_start = workmarkers->markerid; + tv_to_buf(&(workmarkers->createdate), + cd_buf, sizeof(cd_buf)); + cd = cd_buf; + what = "days"; + } + K_RUNLOCK(workmarkers_free); + break; + case 'S': // mark_start shifts (workmarkers) + K_RLOCK(workmarkers_free); + wm_item = last_in_ktree(workmarkers_root, ctx); + while (wm_item) { + DATA_WORKMARKERS(workmarkers, wm_item); + if (CURRENT(&(workmarkers->expirydate)) && + WMPROCESSED(workmarkers->status)) { + ms--; + if (ms <= 0) + break; + } + wm_item = prev_in_ktree(ctx); + } + if (!wm_item) + mark_start = 0; + else { + mark_start = workmarkers->markerid; + tv_to_buf(&(workmarkers->createdate), + cd_buf, sizeof(cd_buf)); + cd = cd_buf; + what = "shifts"; + } + K_RUNLOCK(workmarkers_free); + break; + case 'M': // markerid = mark_start + break; + default: + /* Not possible unless ckdb.c is different + * in which case it will just use mark_start */ + break; + } + } + // TODO: limit how far back sel = "declare ws cursor for select " "markerid,userid,workername,diffacc,diffsta,diffdup,diffhi," @@ -7774,14 +7843,16 @@ bool markersummary_fill(PGconn *conn) "lastshareacc,lastdiffacc" MODIFYDATECONTROL " from markersummary where markerid>=$1"; + par = 0; - if (mark_start) - params[par++] = mark_start; - else - params[par++] = "0"; + params[par++] = bigint_to_buf(mark_start, NULL, 0); PARCHK(par, params); LOGWARNING("%s(): loading from markerid>=%s", __func__, params[0]); + if (cd) { + LOGWARNING(" ... %s = %s >= %"PRId64" %s", + params[0], cd, amt, what); + } printf(TICK_PREFIX"ms 0\r"); fflush(stdout); @@ -8016,6 +8087,10 @@ flail: res = PQexec(conn, "Commit", CKPQ_READ); PQclear(res); + for (i = 0; i < par; i++) + free(params[i]); + par = 0; + if (ok) { LOGDEBUG("%s(): built", __func__); LOGWARNING("%s(): fetched %d markersummary records", __func__, n);