From 79396331146a5da7aab113dcfe577fee75ff6ea5 Mon Sep 17 00:00:00 2001 From: "Roger A. Light" Date: Fri, 18 Mar 2022 10:51:28 +0000 Subject: [PATCH] Optimise persist client msg clearing. --- plugins/persist-sqlite/client_msgs.c | 35 +++++++++++++++++-------- plugins/persist-sqlite/init.c | 7 +++++ plugins/persist-sqlite/persist_sqlite.h | 1 + src/database.c | 13 +++++++-- src/linker-macosx.syms | 1 + src/linker.syms | 1 + 6 files changed, 45 insertions(+), 13 deletions(-) diff --git a/plugins/persist-sqlite/client_msgs.c b/plugins/persist-sqlite/client_msgs.c index 9cf75086..22736be9 100644 --- a/plugins/persist-sqlite/client_msgs.c +++ b/plugins/persist-sqlite/client_msgs.c @@ -119,19 +119,32 @@ int persist_sqlite__client_msg_clear_cb(int event, void *event_data, void *userd UNUSED(event); - if(sqlite3_bind_text(ms->client_msg_clear_stmt, 1, ed->client_id, (int)strlen(ed->client_id), SQLITE_STATIC) == SQLITE_OK - && sqlite3_bind_int64(ms->client_msg_clear_stmt, 2, ed->direction) == SQLITE_OK - ){ - - ms->event_count++; - rc = sqlite3_step(ms->client_msg_clear_stmt); - if(rc == SQLITE_DONE){ - rc = MOSQ_ERR_SUCCESS; - }else{ - rc = MOSQ_ERR_UNKNOWN; + if(ed->direction == mosq_bmd_all){ + if(sqlite3_bind_text(ms->client_msg_clear_all_stmt, 1, ed->client_id, (int)strlen(ed->client_id), SQLITE_STATIC) == SQLITE_OK){ + ms->event_count++; + rc = sqlite3_step(ms->client_msg_clear_all_stmt); + if(rc == SQLITE_DONE){ + rc = MOSQ_ERR_SUCCESS; + }else{ + rc = MOSQ_ERR_UNKNOWN; + } + } + sqlite3_reset(ms->client_msg_clear_all_stmt); + }else{ + if(sqlite3_bind_text(ms->client_msg_clear_stmt, 1, ed->client_id, (int)strlen(ed->client_id), SQLITE_STATIC) == SQLITE_OK + && sqlite3_bind_int64(ms->client_msg_clear_stmt, 2, ed->direction) == SQLITE_OK + ){ + + ms->event_count++; + rc = sqlite3_step(ms->client_msg_clear_stmt); + if(rc == SQLITE_DONE){ + rc = MOSQ_ERR_SUCCESS; + }else{ + rc = MOSQ_ERR_UNKNOWN; + } } + sqlite3_reset(ms->client_msg_clear_stmt); } - sqlite3_reset(ms->client_msg_clear_stmt); return rc; } diff --git a/plugins/persist-sqlite/init.c b/plugins/persist-sqlite/init.c index 81b9f06d..9d1e976a 100644 --- a/plugins/persist-sqlite/init.c +++ b/plugins/persist-sqlite/init.c @@ -203,6 +203,12 @@ static int prepare_statements(struct mosquitto_sqlite *ms) &ms->client_msg_clear_stmt, NULL); if(rc) goto fail; + rc = sqlite3_prepare_v3(ms->db, + "DELETE FROM client_msgs WHERE client_id=?", + -1, SQLITE_PREPARE_PERSISTENT, + &ms->client_msg_clear_all_stmt, NULL); + if(rc) goto fail; + /* Message store */ rc = sqlite3_prepare_v3(ms->db, "INSERT INTO base_msgs " @@ -297,6 +303,7 @@ void persist_sqlite__cleanup(struct mosquitto_sqlite *ms) sqlite3_finalize(ms->client_msg_remove_stmt); sqlite3_finalize(ms->client_msg_update_stmt); sqlite3_finalize(ms->client_msg_clear_stmt); + sqlite3_finalize(ms->client_msg_clear_all_stmt); sqlite3_finalize(ms->base_msg_add_stmt); sqlite3_finalize(ms->base_msg_remove_stmt); sqlite3_finalize(ms->base_msg_load_stmt); diff --git a/plugins/persist-sqlite/persist_sqlite.h b/plugins/persist-sqlite/persist_sqlite.h index 0811cc06..1eb7bffb 100644 --- a/plugins/persist-sqlite/persist_sqlite.h +++ b/plugins/persist-sqlite/persist_sqlite.h @@ -39,6 +39,7 @@ struct mosquitto_sqlite { sqlite3_stmt *client_msg_remove_stmt; sqlite3_stmt *client_msg_update_stmt; sqlite3_stmt *client_msg_clear_stmt; + sqlite3_stmt *client_msg_clear_all_stmt; sqlite3_stmt *base_msg_add_stmt; sqlite3_stmt *base_msg_remove_stmt; sqlite3_stmt *base_msg_load_stmt; diff --git a/src/database.c b/src/database.c index af9b53ce..de017953 100644 --- a/src/database.c +++ b/src/database.c @@ -718,7 +718,6 @@ int db__messages_delete_incoming(struct mosquitto *context) context->msgs_in.queued_bytes12 = 0; context->msgs_in.queued_count = 0; context->msgs_in.queued_count12 = 0; - plugin_persist__handle_client_msg_clear(context, mosq_md_in); return MOSQ_ERR_SUCCESS; } @@ -738,7 +737,6 @@ int db__messages_delete_outgoing(struct mosquitto *context) context->msgs_out.queued_bytes12 = 0; context->msgs_out.queued_count = 0; context->msgs_out.queued_count12 = 0; - plugin_persist__handle_client_msg_clear(context, mosq_md_out); return MOSQ_ERR_SUCCESS; } @@ -746,16 +744,27 @@ int db__messages_delete_outgoing(struct mosquitto *context) int db__messages_delete(struct mosquitto *context, bool force_free) { + bool clear_incoming = false, clear_outgoing = false; if(!context) return MOSQ_ERR_INVAL; if(force_free || context->clean_start || (context->bridge && context->bridge->clean_start)){ db__messages_delete_incoming(context); + clear_incoming = true; } if(force_free || (context->bridge && context->bridge->clean_start_local) || (context->bridge == NULL && context->clean_start)){ db__messages_delete_outgoing(context); + clear_outgoing = true; + } + + if(clear_incoming && clear_outgoing){ + plugin_persist__handle_client_msg_clear(context, mosq_bmd_all); + }else if(clear_incoming){ + plugin_persist__handle_client_msg_clear(context, mosq_bmd_in); + }else if(clear_outgoing){ + plugin_persist__handle_client_msg_clear(context, mosq_bmd_out); } return MOSQ_ERR_SUCCESS; diff --git a/src/linker-macosx.syms b/src/linker-macosx.syms index b08d366d..58cdfda6 100644 --- a/src/linker-macosx.syms +++ b/src/linker-macosx.syms @@ -27,6 +27,7 @@ _mosquitto_persist_base_msg_delete _mosquitto_persist_client_add _mosquitto_persist_client_delete _mosquitto_persist_client_msg_add +_mosquitto_persist_client_msg_clear _mosquitto_persist_client_msg_delete _mosquitto_persist_client_msg_update _mosquitto_persist_client_update diff --git a/src/linker.syms b/src/linker.syms index c4d3493a..6b8d85af 100644 --- a/src/linker.syms +++ b/src/linker.syms @@ -28,6 +28,7 @@ mosquitto_persist_client_add; mosquitto_persist_client_delete; mosquitto_persist_client_msg_add; + mosquitto_persist_client_msg_clear; mosquitto_persist_client_msg_delete; mosquitto_persist_client_msg_update; mosquitto_persist_client_update;