Skip to content

Commit

Permalink
db_cluster: clang-format for coherent indentation and coding style
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxmaniac committed Nov 11, 2023
1 parent 86c03d4 commit bc9d2f7
Show file tree
Hide file tree
Showing 5 changed files with 700 additions and 734 deletions.
206 changes: 100 additions & 106 deletions src/modules/db_cluster/db_cluster_mod.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,34 +42,31 @@ int dbcl_max_query_length = 0;
* DB Cluster module interface
*/
static cmd_export_t cmds[] = {
{"db_bind_api", (cmd_function)db_cluster_bind_api, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}
};
{"db_bind_api", (cmd_function)db_cluster_bind_api, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0}};

/*! \brief
* Exported parameters
*/
static param_export_t params[] = {
{"connection", PARAM_STRING|USE_FUNC_PARAM, (void*)dbcl_con_param},
{"cluster", PARAM_STRING|USE_FUNC_PARAM, (void*)dbcl_cls_param},
{"inactive_interval", INT_PARAM, &dbcl_inactive_interval},
{"max_query_length", INT_PARAM, &dbcl_max_query_length},
{0, 0, 0}
};
{"connection", PARAM_STRING | USE_FUNC_PARAM, (void *)dbcl_con_param},
{"cluster", PARAM_STRING | USE_FUNC_PARAM, (void *)dbcl_cls_param},
{"inactive_interval", INT_PARAM, &dbcl_inactive_interval},
{"max_query_length", INT_PARAM, &dbcl_max_query_length}, {0, 0, 0}};

static rpc_export_t rpc_methods[];

struct module_exports exports = {
"db_cluster", /* module name */
DEFAULT_DLFLAGS, /* dlopen flags */
cmds, /* exported functions */
params, /* exported parameters */
0, /* RPC method exports */
0, /* exported pseudo-variables */
0, /* response handling function */
mod_init, /* module initialization function */
0, /* per-child init function */
0 /* module destroy function */
"db_cluster", /* module name */
DEFAULT_DLFLAGS, /* dlopen flags */
cmds, /* exported functions */
params, /* exported parameters */
0, /* RPC method exports */
0, /* exported pseudo-variables */
0, /* response handling function */
mod_init, /* module initialization function */
0, /* per-child init function */
0 /* module destroy function */
};


Expand All @@ -87,47 +84,47 @@ int mod_init(void)

int db_cluster_bind_api(db_func_t *dbb)
{
if(dbb==NULL)
if(dbb == NULL)
return -1;

memset(dbb, 0, sizeof(db_func_t));

dbb->use_table = db_cluster_use_table;
dbb->init = db_cluster_init;
dbb->close = db_cluster_close;
dbb->query = db_cluster_query;
dbb->fetch_result = db_cluster_fetch_result;
dbb->raw_query = db_cluster_raw_query;
dbb->free_result = db_cluster_free_result;
dbb->insert = db_cluster_insert;
dbb->delete = db_cluster_delete;
dbb->update = db_cluster_update;
dbb->replace = db_cluster_replace;
dbb->use_table = db_cluster_use_table;
dbb->init = db_cluster_init;
dbb->close = db_cluster_close;
dbb->query = db_cluster_query;
dbb->fetch_result = db_cluster_fetch_result;
dbb->raw_query = db_cluster_raw_query;
dbb->free_result = db_cluster_free_result;
dbb->insert = db_cluster_insert;
dbb->delete = db_cluster_delete;
dbb->update = db_cluster_update;
dbb->replace = db_cluster_replace;
dbb->last_inserted_id = db_cluster_last_inserted_id;
dbb->insert_async = db_cluster_insert_async;
dbb->insert_update = db_cluster_insert_update;
dbb->insert_delayed = db_cluster_insert_delayed;
dbb->affected_rows = db_cluster_affected_rows;
dbb->insert_async = db_cluster_insert_async;
dbb->insert_update = db_cluster_insert_update;
dbb->insert_delayed = db_cluster_insert_delayed;
dbb->affected_rows = db_cluster_affected_rows;

return 0;
}

int dbcl_con_param(modparam_t type, void *val)
{
return dbcl_parse_con_param((char*)val);
return dbcl_parse_con_param((char *)val);
}

int dbcl_cls_param(modparam_t type, void *val)
{
return dbcl_parse_cls_param((char*)val);
return dbcl_parse_cls_param((char *)val);
}

str dbcl_connection_active_str = str_init("active");
str dbcl_connection_disabled_str = str_init("disabled");

str *dbcl_get_status_str(int status)
{
if (status & DBCL_CON_INACTIVE)
if(status & DBCL_CON_INACTIVE)
return &dbcl_connection_disabled_str;
else
return &dbcl_connection_active_str;
Expand All @@ -137,31 +134,28 @@ str *dbcl_get_status_str(int status)

static int dbcl_active_count_connections(str cluster)
{
dbcl_cls_t *cls=NULL;
dbcl_cls_t *cls = NULL;
int count = 0;
int i, j;

cls = dbcl_get_cluster(&cluster);

if(cls==NULL)
{
if(cls == NULL) {
LM_ERR("cluster not found [%.*s]\n", cluster.len, cluster.s);
return 0;
}

for(i=1; i<DBCL_PRIO_SIZE; i++)
{
for(j=0; j<cls->rlist[i].clen; j++)
{
if(cls->rlist[i].clist[j] != NULL)
{
LM_INFO("read connection [%.*s]\n", cls->rlist[i].clist[j]->name.len, cls->rlist[i].clist[j]->name.s);
for(i = 1; i < DBCL_PRIO_SIZE; i++) {
for(j = 0; j < cls->rlist[i].clen; j++) {
if(cls->rlist[i].clist[j] != NULL) {
LM_INFO("read connection [%.*s]\n",
cls->rlist[i].clist[j]->name.len,
cls->rlist[i].clist[j]->name.s);

if(cls->rlist[i].clist[j]->sinfo==NULL)
if(cls->rlist[i].clist[j]->sinfo == NULL)
return 0;

if(cls->rlist[i].clist[j]->sinfo->state == 0)
{
if(cls->rlist[i].clist[j]->sinfo->state == 0) {
count++;
}
}
Expand All @@ -174,50 +168,49 @@ static int dbcl_active_count_connections(str cluster)
static void dbcl_rpc_list_connections(rpc_t *rpc, void *c)
{
void *handle;
dbcl_cls_t *cls=NULL;
dbcl_cls_t *cls = NULL;
str cluster;
int i, j;
unsigned int ticks;

if (rpc->scan(c, "S", &cluster) != 1) {
if(rpc->scan(c, "S", &cluster) != 1) {
rpc->fault(c, 500, "Not enough parameters (cluster)");
return;
}

cls = dbcl_get_cluster(&cluster);

if(cls==NULL)
{
if(cls == NULL) {
LM_ERR("cluster not found [%.*s]\n", cluster.len, cluster.s);
rpc->fault(c, 500, "Cluster not found");
return;
}

for(i=1; i<DBCL_PRIO_SIZE; i++)
{
for(j=0; j<cls->rlist[i].clen; j++)
{
if(cls->rlist[i].clist[j] != NULL)
{
LM_INFO("read connection [%.*s]\n", cls->rlist[i].clist[j]->name.len, cls->rlist[i].clist[j]->name.s);
for(i = 1; i < DBCL_PRIO_SIZE; i++) {
for(j = 0; j < cls->rlist[i].clen; j++) {
if(cls->rlist[i].clist[j] != NULL) {
LM_INFO("read connection [%.*s]\n",
cls->rlist[i].clist[j]->name.len,
cls->rlist[i].clist[j]->name.s);

if(rpc->add(c, "{", &handle) < 0)
goto error;

if(cls->rlist[i].clist[j]->sinfo==NULL)
if(cls->rlist[i].clist[j]->sinfo == NULL)
goto error;

if (cls->rlist[i].clist[j]->sinfo->aticks != 0)
if(cls->rlist[i].clist[j]->sinfo->aticks != 0)
ticks = cls->rlist[i].clist[j]->sinfo->aticks - get_ticks();
else
ticks = 0;

if(rpc->struct_add(handle, "SSdSdd", "connection", &cls->rlist[i].clist[j]->name,
"url", &cls->rlist[i].clist[j]->db_url,
"flags", cls->rlist[i].clist[j]->flags,
"state", dbcl_get_status_str(cls->rlist[i].clist[j]->sinfo->state),
"ticks", ticks,
"ref", cls->ref)
if(rpc->struct_add(handle, "SSdSdd", "connection",
&cls->rlist[i].clist[j]->name, "url",
&cls->rlist[i].clist[j]->db_url, "flags",
cls->rlist[i].clist[j]->flags, "state",
dbcl_get_status_str(
cls->rlist[i].clist[j]->sinfo->state),
"ticks", ticks, "ref", cls->ref)
< 0)
goto error;
}
Expand All @@ -234,43 +227,41 @@ static void dbcl_rpc_list_connections(rpc_t *rpc, void *c)

static void dbcl_rpc_disable_connection(rpc_t *rpc, void *c)
{
dbcl_cls_t *cls=NULL;
dbcl_con_t *con=NULL;
dbcl_cls_t *cls = NULL;
dbcl_con_t *con = NULL;
str cluster;
str connection;
int seconds;

if (rpc->scan(c, "SSd", &cluster, &connection, &seconds) < 3) {
rpc->fault(c, 500, "Not enough parameters (cluster) (connection) (seconds)");
if(rpc->scan(c, "SSd", &cluster, &connection, &seconds) < 3) {
rpc->fault(c, 500,
"Not enough parameters (cluster) (connection) (seconds)");
return;
}

cls = dbcl_get_cluster(&cluster);

if(cls==NULL)
{
if(cls == NULL) {
LM_INFO("cluster not found [%.*s]\n", cluster.len, cluster.s);
rpc->fault(c, 500, "Cluster not found");
return;
}

con = dbcl_get_connection(&connection);

if(con==NULL)
{
if(con == NULL) {
LM_INFO("connection not found [%.*s]\n", connection.len, connection.s);
rpc->fault(c, 500, "Cluster connection not found");
return;
}

if(con->sinfo==NULL) {
if(con->sinfo == NULL) {
rpc->fault(c, 500, "Cluster state info missing.");
return;
}

/* Overwrite the number of seconds if the connection is already disabled. */
if (con->sinfo->state & DBCL_CON_INACTIVE)
{
if(con->sinfo->state & DBCL_CON_INACTIVE) {
if(dbcl_disable_con(con, seconds) < 0) {
rpc->fault(c, 500, "Failed disabling cluster connection.");
return;
Expand All @@ -279,9 +270,9 @@ static void dbcl_rpc_disable_connection(rpc_t *rpc, void *c)
return;
}

if (dbcl_active_count_connections(cluster) <= 1)
{
rpc->fault(c, 500, "Cannot disable last active connection in a cluster");
if(dbcl_active_count_connections(cluster) <= 1) {
rpc->fault(
c, 500, "Cannot disable last active connection in a cluster");
return;
}

Expand All @@ -295,29 +286,27 @@ static void dbcl_rpc_disable_connection(rpc_t *rpc, void *c)

static void dbcl_rpc_enable_connection(rpc_t *rpc, void *c)
{
dbcl_cls_t *cls=NULL;
dbcl_con_t *con=NULL;
dbcl_cls_t *cls = NULL;
dbcl_con_t *con = NULL;
str cluster;
str connection;

if (rpc->scan(c, "SS", &cluster, &connection) < 2) {
if(rpc->scan(c, "SS", &cluster, &connection) < 2) {
rpc->fault(c, 500, "Not enough parameters (cluster) (connection)");
return;
}

cls = dbcl_get_cluster(&cluster);

if(cls==NULL)
{
if(cls == NULL) {
LM_INFO("cluster not found [%.*s]\n", cluster.len, cluster.s);
rpc->fault(c, 500, "Cluster not found");
return;
}

con = dbcl_get_connection(&connection);

if(con==NULL)
{
if(con == NULL) {
LM_INFO("connection not found [%.*s]\n", connection.len, connection.s);
rpc->fault(c, 500, "Cluster connection not found");
return;
Expand All @@ -333,20 +322,19 @@ static void dbcl_rpc_enable_connection(rpc_t *rpc, void *c)
static void dbcl_rpc_list_clusters(rpc_t *rpc, void *c)
{
void *handle;
dbcl_cls_t *cls=NULL;
dbcl_cls_t *cls = NULL;

cls = dbcl_get_cluster_root();

if(cls==NULL)
{
if(cls == NULL) {
LM_ERR("root not set\n");
rpc->fault(c, 500, "Clusters not found");
return;
}

while(cls)
{
LM_INFO("cluster found ID [%u] NAME [%.*s]\n", cls->clsid, cls->name.len, cls->name.s);
while(cls) {
LM_INFO("cluster found ID [%u] NAME [%.*s]\n", cls->clsid,
cls->name.len, cls->name.s);

if(rpc->add(c, "{", &handle) < 0)
goto error;
Expand All @@ -367,14 +355,20 @@ static void dbcl_rpc_list_clusters(rpc_t *rpc, void *c)


static const char *dbcl_rpc_list_clusters_doc[2] = {"Print all clusters", 0};
static const char *dbcl_rpc_list_connections_doc[2] = {"Print all database connections of a cluster", 0};
static const char *dbcl_rpc_disable_connection_doc[2] = {"Disable a connection of a cluster for a period", 0};
static const char *dbcl_rpc_enable_connection_doc[2] = {"Enable a connection of a cluster", 0};
static const char *dbcl_rpc_list_connections_doc[2] = {
"Print all database connections of a cluster", 0};
static const char *dbcl_rpc_disable_connection_doc[2] = {
"Disable a connection of a cluster for a period", 0};
static const char *dbcl_rpc_enable_connection_doc[2] = {
"Enable a connection of a cluster", 0};

static rpc_export_t rpc_methods[] = {
{"dbcl.list_clusters", dbcl_rpc_list_clusters, dbcl_rpc_list_clusters_doc, RET_ARRAY},
{"dbcl.list_connections", dbcl_rpc_list_connections, dbcl_rpc_list_connections_doc, RET_ARRAY},
{"dbcl.disable_connection", dbcl_rpc_disable_connection, dbcl_rpc_disable_connection_doc, 0},
{"dbcl.enable_connection", dbcl_rpc_enable_connection, dbcl_rpc_enable_connection_doc, 0},
{0, 0, 0, 0}
};
{"dbcl.list_clusters", dbcl_rpc_list_clusters,
dbcl_rpc_list_clusters_doc, RET_ARRAY},
{"dbcl.list_connections", dbcl_rpc_list_connections,
dbcl_rpc_list_connections_doc, RET_ARRAY},
{"dbcl.disable_connection", dbcl_rpc_disable_connection,
dbcl_rpc_disable_connection_doc, 0},
{"dbcl.enable_connection", dbcl_rpc_enable_connection,
dbcl_rpc_enable_connection_doc, 0},
{0, 0, 0, 0}};

0 comments on commit bc9d2f7

Please sign in to comment.