From ed0bcee86bb684053b791fa23f6d0e29e582fe37 Mon Sep 17 00:00:00 2001 From: kanoi Date: Mon, 14 Mar 2016 21:02:51 +1100 Subject: [PATCH] ckdb - cmd_high action=store to store all current shares_hi not yet in the db --- src/ckdb.c | 7 +++--- src/ckdb.h | 3 ++- src/ckdb_cmd.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+), 5 deletions(-) diff --git a/src/ckdb.c b/src/ckdb.c index 7909b458..30141ca4 100644 --- a/src/ckdb.c +++ b/src/ckdb.c @@ -10,11 +10,8 @@ #include "ckdb.h" -/* TODO: any tree/list accessed in new threads needs - * to ensure all code using those trees/lists use locks - * This code's lock implementation is equivalent to table level locking +/* This code's lock implementation is equivalent to table level locking * Consider adding row level locking (a per kitem usage count) if needed - * TODO: verify all tables with multithread access are locked */ /* Startup @@ -4449,6 +4446,7 @@ static void *socketer(__maybe_unused void *arg) case CMD_USERINFO: case CMD_LOCKS: case CMD_EVENTS: + case CMD_HIGH: msgline->sockd = sockd; sockd = -1; K_WLOCK(workqueue_free); @@ -4709,6 +4707,7 @@ static void reload_line(PGconn *conn, char *filename, uint64_t count, char *buf) case CMD_QUERY: case CMD_LOCKS: case CMD_EVENTS: + case CMD_HIGH: LOGERR("%s() INVALID message line %"PRIu64 " ignored '%.42s...", __func__, count, diff --git a/src/ckdb.h b/src/ckdb.h index f3fdec29..dd30fc1d 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.981" +#define CKDB_VERSION DB_VERSION"-1.982" #define WHERE_FFL " - from %s %s() line %d" #define WHERE_FFL_HERE __FILE__, __func__, __LINE__ @@ -687,6 +687,7 @@ enum cmd_values { CMD_QUERY, CMD_LOCKS, CMD_EVENTS, + CMD_HIGH, CMD_END }; diff --git a/src/ckdb_cmd.c b/src/ckdb_cmd.c index e595e032..9cf3d9a6 100644 --- a/src/ckdb_cmd.c +++ b/src/ckdb_cmd.c @@ -8039,6 +8039,72 @@ static char *cmd_events(__maybe_unused PGconn *conn, char *cmd, char *id, return buf; } +// High Share actions +static char *cmd_high(PGconn *conn, char *cmd, char *id, + __maybe_unused tv_t *now, __maybe_unused char *by, + __maybe_unused char *code, __maybe_unused char *inet, + __maybe_unused tv_t *cd, K_TREE *trf_root) +{ + bool conned = false; + K_TREE_CTX ctx[1]; + K_ITEM *i_action, *s_item = NULL; + char *action; + char reply[1024] = ""; + size_t siz = sizeof(reply); + char *buf = NULL; + int count = 0; + bool ok, did; + + LOGDEBUG("%s(): cmd '%s'", __func__, cmd); + + i_action = require_name(trf_root, "action", 1, NULL, reply, siz); + if (!i_action) + return strdup(reply); + action = transfer_data(i_action); + + if (strcasecmp(action, "store") == 0) { + /* Store the shares_hi_root list in the db now, + * rather than wait for a shift process to do it */ + if (!conn) { + conn = dbconnect(); + conned = true; + } + count = 0; + do { + did = false; + K_WLOCK(shares_free); + s_item = first_in_ktree(shares_hi_root, ctx); + K_WUNLOCK(shares_free); + if (s_item) { + did = true; + ok = shares_db(conn, s_item); + if (!ok) + break; + count++; + } + } while (did); + if (conned) + PQfinish(conn); + if (count) { + LOGWARNING("%s() Stored: %d high shares", + __func__, count); + } else + LOGWARNING("%s() No high shares to store", __func__); + + if (ok) + snprintf(reply, siz, "ok.stored %d", count); + else + snprintf(reply, siz, "DBERR.stored %d", count); + return strdup(reply); + } else { + snprintf(reply, siz, "unknown action '%s'", action); + LOGERR("%s() %s.%s", __func__, id, reply); + return strdup(reply); + } + + return buf; +} + /* The socket command format is as follows: * Basic structure: * cmd.ID.fld1=value1 FLDSEP fld2=value2 FLDSEP fld3=... @@ -8150,5 +8216,6 @@ struct CMDS ckdb_cmds[] = { { CMD_QUERY, "query", false, false, cmd_query, SEQ_NONE, ACCESS_SYSTEM }, { CMD_LOCKS, "locks", false, false, cmd_locks, SEQ_NONE, ACCESS_SYSTEM }, { CMD_EVENTS, "events", false, false, cmd_events, SEQ_NONE, ACCESS_SYSTEM }, + { CMD_HIGH, "high", false, false, cmd_high, SEQ_NONE, ACCESS_SYSTEM }, { CMD_END, NULL, false, false, NULL, SEQ_NONE, 0 } };