diff --git a/src/ckdb.c b/src/ckdb.c index ffede0a4..07e8b056 100644 --- a/src/ckdb.c +++ b/src/ckdb.c @@ -737,6 +737,8 @@ static bool getdata3() } if (!(ok = workinfo_fill(conn)) || everyone_die) goto sukamudai; + if (!(ok = marks_fill(conn)) || everyone_die) + goto sukamudai; if (!(ok = workmarkers_fill(conn)) || everyone_die) goto sukamudai; if (!(ok = markersummary_fill(conn)) || everyone_die) @@ -1039,6 +1041,11 @@ static void alloc_storage() workmarkers_root = new_ktree(); workmarkers_workinfoid_root = new_ktree(); workmarkers_free->dsp_func = dsp_workmarkers; + + marks_free = k_new_list("Marks", sizeof(MARKS), + ALLOC_MARKS, LIMIT_MARKS, true); + marks_store = k_new_store(workmarkers_free); + marks_root = new_ktree(); } static void free_workinfo_data(K_ITEM *item) @@ -1145,6 +1152,8 @@ static void dealloc_storage() { FREE_LISTS(logqueue); + FREE_ALL(marks); + FREE_TREE(workmarkers_workinfoid); FREE_TREE(workmarkers); FREE_STORE_DATA(workmarkers); diff --git a/src/ckdb.h b/src/ckdb.h index c05e0cc4..b25150c2 100644 --- a/src/ckdb.h +++ b/src/ckdb.h @@ -52,7 +52,7 @@ #define DB_VLOCK "1" #define DB_VERSION "0.9.5" -#define CKDB_VERSION DB_VERSION"-0.649" +#define CKDB_VERSION DB_VERSION"-0.650" #define WHERE_FFL " - from %s %s() line %d" #define WHERE_FFL_HERE __FILE__, __func__, __LINE__ @@ -1557,6 +1557,7 @@ extern cmp_t cmp_workmarkers(K_ITEM *a, K_ITEM *b); extern cmp_t cmp_workmarkers_workinfoid(K_ITEM *a, K_ITEM *b); extern K_ITEM *find_workmarkers(int64_t workinfoid); extern K_ITEM *find_workmarkerid(int64_t markerid); +extern cmp_t cmp_marks(K_ITEM *a, K_ITEM *b); // *** // *** PostgreSQL functions ckdb_dbio.c @@ -1700,6 +1701,7 @@ extern bool userstats_add(char *poolinstance, char *elapsed, char *username, extern bool userstats_fill(PGconn *conn); extern bool markersummary_fill(PGconn *conn); extern bool workmarkers_fill(PGconn *conn); +extern bool marks_fill(PGconn *conn); extern bool check_db_version(PGconn *conn); // *** diff --git a/src/ckdb_data.c b/src/ckdb_data.c index e176acfe..241fdac3 100644 --- a/src/ckdb_data.c +++ b/src/ckdb_data.c @@ -2434,3 +2434,16 @@ K_ITEM *find_workmarkerid(int64_t markerid) return wm_item; } +// order by expirydate asc,workinfoid asc +// TODO: add poolinstance +cmp_t cmp_marks(K_ITEM *a, K_ITEM *b) +{ + MARKS *ma, *mb; + DATA_MARKS(ma, a); + DATA_MARKS(mb, b); + cmp_t c = CMP_TV(ma->expirydate, mb->expirydate); + if (c == 0) + c = CMP_BIGINT(ma->workinfoid, mb->workinfoid); + return c; +} + diff --git a/src/ckdb_dbio.c b/src/ckdb_dbio.c index f4e4681a..c5a23fda 100644 --- a/src/ckdb_dbio.c +++ b/src/ckdb_dbio.c @@ -5108,6 +5108,100 @@ bool workmarkers_fill(PGconn *conn) return ok; } +bool marks_fill(PGconn *conn) +{ + ExecStatusType rescode; + PGresult *res; + K_ITEM *item; + int n, i; + MARKS *row; + char *field; + char *sel; + int fields = 5; + bool ok; + + LOGDEBUG("%s(): select", __func__); + + // TODO: limit how far back + sel = "select " + "poolinstance,workinfoid,description,marktype,status" + HISTORYDATECONTROL + " from marks"; + res = PQexec(conn, sel, CKPQ_READ); + rescode = PQresultStatus(res); + if (!PGOK(rescode)) { + PGLOGERR("Select", rescode, conn); + PQclear(res); + return false; + } + + n = PQnfields(res); + if (n != (fields + HISTORYDATECOUNT)) { + LOGERR("%s(): Invalid field count - should be %d, but is %d", + __func__, fields + HISTORYDATECOUNT, n); + PQclear(res); + return false; + } + + n = PQntuples(res); + LOGDEBUG("%s(): tree build count %d", __func__, n); + ok = true; + for (i = 0; i < n; i++) { + item = k_unlink_head(marks_free); + DATA_MARKS(row, item); + + if (everyone_die) { + ok = false; + break; + } + + PQ_GET_FLD(res, i, "poolinstance", field, ok); + if (!ok) + break; + TXT_TO_PTR("poolinstance", field, row->poolinstance); + + PQ_GET_FLD(res, i, "workinfoid", field, ok); + if (!ok) + break; + TXT_TO_BIGINT("workinfoid", field, row->workinfoid); + + PQ_GET_FLD(res, i, "description", field, ok); + if (!ok) + break; + TXT_TO_PTR("description", field, row->description); + + PQ_GET_FLD(res, i, "marktype", field, ok); + if (!ok) + break; + TXT_TO_STR("marktype", field, row->marktype); + + PQ_GET_FLD(res, i, "status", field, ok); + if (!ok) + break; + TXT_TO_STR("status", field, row->status); + + HISTORYDATEFLDS(res, i, row, ok); + if (!ok) + break; + + marks_root = add_to_ktree(marks_root, item, cmp_marks); + k_add_head(marks_store, item); + + tick(); + } + if (!ok) + k_add_head(marks_free, item); + + PQclear(res); + + if (ok) { + LOGDEBUG("%s(): built", __func__); + LOGWARNING("%s(): loaded %d marks records", __func__, n); + } + + return ok; +} + bool check_db_version(PGconn *conn) { ExecStatusType rescode;