diff --git a/plugins/persist-sqlite/init.c b/plugins/persist-sqlite/init.c index 9d1e976a..cb4ff58e 100644 --- a/plugins/persist-sqlite/init.c +++ b/plugins/persist-sqlite/init.c @@ -268,12 +268,13 @@ int persist_sqlite__init(struct mosquitto_sqlite *ms) ms->db_file, sqlite3_errstr(rc)); return MOSQ_ERR_UNKNOWN; } + snprintf(buf, sizeof(buf), "PRAGMA page_size=%u;", ms->page_size); + rc = sqlite3_exec(ms->db, buf, NULL, NULL, NULL); + if(rc) goto fail; rc = sqlite3_exec(ms->db, "PRAGMA journal_mode=WAL;", NULL, NULL, NULL); if(rc) goto fail; rc = sqlite3_exec(ms->db, "PRAGMA foreign_keys = ON;", NULL, NULL, NULL); if(rc) goto fail; - rc = sqlite3_exec(ms->db, "PRAGMA page_size=32768;", NULL, NULL, NULL); - if(rc) goto fail; snprintf(buf, sizeof(buf), "PRAGMA synchronous=%d;", ms->synchronous); rc = sqlite3_exec(ms->db, buf, NULL, NULL, NULL); if(rc) goto fail; diff --git a/plugins/persist-sqlite/persist_sqlite.h b/plugins/persist-sqlite/persist_sqlite.h index 1eb7bffb..6568a34d 100644 --- a/plugins/persist-sqlite/persist_sqlite.h +++ b/plugins/persist-sqlite/persist_sqlite.h @@ -47,7 +47,9 @@ struct mosquitto_sqlite { sqlite3_stmt *retain_msg_remove_stmt; time_t last_transaction; int synchronous; - int event_count; + unsigned int event_count; + unsigned int flush_period; + unsigned int page_size; }; int persist_sqlite__init(struct mosquitto_sqlite *ms); diff --git a/plugins/persist-sqlite/plugin.c b/plugins/persist-sqlite/plugin.c index 93661f66..299dce5a 100644 --- a/plugins/persist-sqlite/plugin.c +++ b/plugins/persist-sqlite/plugin.c @@ -34,6 +34,29 @@ Contributors: static mosquitto_plugin_id_t *plg_id = NULL; static struct mosquitto_sqlite plg_data; +static int conf_parse_uint(const char *in, const char *name, unsigned int *value) +{ + int v = atoi(in); + if(v <= 0){ + mosquitto_log_printf(MOSQ_LOG_ERR, "Error: Invalid '%s' value in configuration.", name); + return MOSQ_ERR_INVAL; + } + + *value = v; + return MOSQ_ERR_SUCCESS; +} + +static void set_defaults(void) +{ + /* "normal" synchronous mode. */ + plg_data.synchronous = 1; + + /* 5 seconds */ + plg_data.flush_period = 5; + + plg_data.page_size = 4 * 1024; +} + int mosquitto_plugin_version(int supported_version_count, const int *supported_versions) { int i; @@ -54,8 +77,7 @@ int mosquitto_plugin_init(mosquitto_plugin_id_t *identifier, void **user_data, s UNUSED(user_data); memset(&plg_data, 0,sizeof(struct mosquitto_sqlite)); - /* Default to "normal" synchronous mode. */ - plg_data.synchronous = 1; + set_defaults(); for(i=0; inow_s > ms->last_transaction + 5 && ms->event_count > 0){ + if(ed->now_s > ms->last_transaction + ms->flush_period && ms->event_count > 0){ ms->last_transaction = ed->now_s; ms->event_count = 0; sqlite3_exec(ms->db, "END;", NULL, NULL, NULL);