Persist plugin: Simplify cases where we don't take a copy.

Some members of the persist plugin interface are only used to find a
client struct, for example. There is no need for the plugin to allocate
a new copy and the broker to free it in that case.
pull/2485/head
Roger A. Light 4 years ago
parent 3122d3e2c5
commit 0a4a029fbf

@ -255,7 +255,6 @@ struct mosquitto_evt_persist_subscription {
* it may change in a future minor release. */ * it may change in a future minor release. */
struct mosquitto_evt_persist_client_msg { struct mosquitto_evt_persist_client_msg {
const char *client_id; const char *client_id;
char *plugin_client_id;
uint64_t cmsg_id; uint64_t cmsg_id;
uint64_t store_id; uint64_t store_id;
uint32_t subscription_identifier; uint32_t subscription_identifier;
@ -282,8 +281,6 @@ struct mosquitto_evt_persist_base_msg {
const char *source_username; const char *source_username;
char *plugin_topic; char *plugin_topic;
void *plugin_payload; void *plugin_payload;
char *plugin_source_id;
char *plugin_source_username;
const mosquitto_property *properties; const mosquitto_property *properties;
mosquitto_property *plugin_properties; mosquitto_property *plugin_properties;
uint32_t payloadlen; uint32_t payloadlen;

@ -216,25 +216,8 @@ static int msg_restore(struct mosquitto_sqlite *ms)
continue; continue;
} }
} }
str = (const char *)sqlite3_column_text(stmt, 4); msg.source_id = (const char *)sqlite3_column_text(stmt, 4);
if(str){ msg.source_username = (const char *)sqlite3_column_text(stmt, 5);
msg.plugin_source_id = strdup(str);
if(!msg.plugin_source_id){
free(msg.plugin_topic);
failed++;
continue;
}
}
str = (const char *)sqlite3_column_text(stmt, 5);
if(str){
msg.plugin_source_username = strdup(str);
if(!msg.plugin_source_username){
free(msg.plugin_topic);
free(msg.plugin_source_id);
failed++;
continue;
}
}
payload = (const void *)sqlite3_column_blob(stmt, 3); payload = (const void *)sqlite3_column_blob(stmt, 3);
msg.payloadlen = (uint32_t)sqlite3_column_int(stmt, 6); msg.payloadlen = (uint32_t)sqlite3_column_int(stmt, 6);
if(payload && msg.payloadlen){ if(payload && msg.payloadlen){
@ -242,8 +225,6 @@ static int msg_restore(struct mosquitto_sqlite *ms)
if(!msg.plugin_payload){ if(!msg.plugin_payload){
free(msg.plugin_topic); free(msg.plugin_topic);
free(msg.plugin_topic); free(msg.plugin_topic);
free(msg.plugin_source_id);
free(msg.plugin_source_username);
failed++; failed++;
continue; continue;
} }
@ -277,7 +258,6 @@ static int client_msg_restore(struct mosquitto_sqlite *ms)
struct mosquitto_evt_persist_client_msg msg; struct mosquitto_evt_persist_client_msg msg;
int rc; int rc;
long count = 0, failed = 0; long count = 0, failed = 0;
const char *str;
rc = sqlite3_prepare_v2(ms->db, rc = sqlite3_prepare_v2(ms->db,
"SELECT client_id, store_id, dup, direction, mid, qos, retain, state " "SELECT client_id, store_id, dup, direction, mid, qos, retain, state "
@ -291,10 +271,7 @@ static int client_msg_restore(struct mosquitto_sqlite *ms)
memset(&msg, 0, sizeof(msg)); memset(&msg, 0, sizeof(msg));
while(sqlite3_step(stmt) == SQLITE_ROW){ while(sqlite3_step(stmt) == SQLITE_ROW){
str = (const char *)sqlite3_column_text(stmt, 0); msg.client_id = (const char *)sqlite3_column_text(stmt, 0);
if(str){
msg.plugin_client_id = strdup(str);
}
msg.store_id = (uint64_t)sqlite3_column_int64(stmt, 1); msg.store_id = (uint64_t)sqlite3_column_int64(stmt, 1);
msg.dup = sqlite3_column_int(stmt, 2); msg.dup = sqlite3_column_int(stmt, 2);
msg.direction = (uint8_t)sqlite3_column_int(stmt, 3); msg.direction = (uint8_t)sqlite3_column_int(stmt, 3);

@ -614,13 +614,11 @@ int mosquitto_persist_client_msg_add(struct mosquitto_evt_persist_client_msg *cl
struct mosquitto *context; struct mosquitto *context;
struct mosquitto_base_msg *base_msg; struct mosquitto_base_msg *base_msg;
if(client_msg == NULL || client_msg->plugin_client_id == NULL){ if(client_msg == NULL || client_msg->client_id == NULL){
free(client_msg->plugin_client_id);
return MOSQ_ERR_INVAL; return MOSQ_ERR_INVAL;
} }
HASH_FIND(hh_id, db.contexts_by_id, client_msg->plugin_client_id, strlen(client_msg->plugin_client_id), context); HASH_FIND(hh_id, db.contexts_by_id, client_msg->client_id, strlen(client_msg->client_id), context);
free(client_msg->plugin_client_id);
if(context == NULL){ if(context == NULL){
return MOSQ_ERR_NOT_FOUND; return MOSQ_ERR_NOT_FOUND;
} }
@ -646,9 +644,9 @@ int mosquitto_persist_client_msg_delete(struct mosquitto_evt_persist_client_msg
{ {
struct mosquitto *context; struct mosquitto *context;
if(client_msg == NULL || client_msg->plugin_client_id == NULL) return MOSQ_ERR_INVAL; if(client_msg == NULL || client_msg->client_id == NULL) return MOSQ_ERR_INVAL;
HASH_FIND(hh_id, db.contexts_by_id, client_msg->plugin_client_id, strlen(client_msg->plugin_client_id), context); HASH_FIND(hh_id, db.contexts_by_id, client_msg->client_id, strlen(client_msg->client_id), context);
if(context == NULL){ if(context == NULL){
return MOSQ_ERR_NOT_FOUND; return MOSQ_ERR_NOT_FOUND;
} }
@ -666,9 +664,9 @@ int mosquitto_persist_client_msg_update(struct mosquitto_evt_persist_client_msg
{ {
struct mosquitto *context; struct mosquitto *context;
if(client_msg == NULL || client_msg->plugin_client_id == NULL) return MOSQ_ERR_INVAL; if(client_msg == NULL || client_msg->client_id == NULL) return MOSQ_ERR_INVAL;
HASH_FIND(hh_id, db.contexts_by_id, client_msg->plugin_client_id, strlen(client_msg->plugin_client_id), context); HASH_FIND(hh_id, db.contexts_by_id, client_msg->client_id, strlen(client_msg->client_id), context);
if(context == NULL){ if(context == NULL){
return MOSQ_ERR_NOT_FOUND; return MOSQ_ERR_NOT_FOUND;
} }
@ -730,8 +728,10 @@ int mosquitto_persist_base_msg_add(struct mosquitto_evt_persist_base_msg *msg)
memset(&context, 0, sizeof(context)); memset(&context, 0, sizeof(context));
context.id = msg->plugin_source_id; /* db__message_store only takes a copy of .id and .username, so it is reasonably safe
context.username = msg->plugin_source_username; * to cast the const char * to char * */
context.id = (char *)msg->source_id;
context.username = (char *)msg->source_username;
if(msg->expiry_time == 0){ if(msg->expiry_time == 0){
message_expiry_interval = 0; message_expiry_interval = 0;
@ -771,8 +771,6 @@ int mosquitto_persist_base_msg_add(struct mosquitto_evt_persist_base_msg *msg)
} }
} }
rc = db__message_store(&context, base_msg, message_expiry_interval, msg->store_id, mosq_mo_broker); rc = db__message_store(&context, base_msg, message_expiry_interval, msg->store_id, mosq_mo_broker);
free(context.id);
free(context.username);
return rc; return rc;
error: error:

Loading…
Cancel
Save