Skip to content

Commit

Permalink
Change: Use new columns to speed up check_preference_names
Browse files Browse the repository at this point in the history
  • Loading branch information
timopollmeier authored Jun 5, 2023
2 parents a314502 + fe27460 commit 0580e38
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 73 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ include (CPack)

## Variables

set (GVMD_DATABASE_VERSION 253)
set (GVMD_DATABASE_VERSION 254)

set (GVMD_SCAP_DATABASE_VERSION 20)

Expand Down
4 changes: 0 additions & 4 deletions src/manage_configs.h
Original file line number Diff line number Diff line change
Expand Up @@ -168,10 +168,6 @@ config_timeout_iterator_nvt_name (iterator_t *);
const char*
config_timeout_iterator_value (iterator_t *);

void
update_config_preference (const char *, const char *, const char *,
const char *, gboolean);

gboolean
configs_feed_dir_exists ();

Expand Down
67 changes: 67 additions & 0 deletions src/manage_migrators.c
Original file line number Diff line number Diff line change
Expand Up @@ -3059,6 +3059,72 @@ migrate_252_to_253 ()
}


/**
* @brief Alter and update for migrate_253_to_254.
*
* @param[in] trash Whether to alter trash tables.
*/
static void
migrate_253_to_254_alter (int trash)
{
sql ("ALTER TABLE config_preferences%s ADD COLUMN pref_nvt text;",
trash ? "_trash" : "");
sql ("UPDATE config_preferences%s"
" SET pref_nvt = substring (name, '^([^:]*)');",
trash ? "_trash" : "");

sql ("ALTER TABLE config_preferences%s ADD COLUMN pref_id integer;",
trash ? "_trash" : "");
sql ("UPDATE config_preferences%s"
" SET pref_id = CAST (substring (name, '^[^:]*:([^:]*)') AS integer);",
trash ? "_trash" : "");

sql ("ALTER table config_preferences%s ADD COLUMN pref_type text;",
trash ? "_trash" : "");
sql ("UPDATE config_preferences%s"
" SET pref_type = substring (name, '^[^:]*:[^:]*:([^:]*):');",
trash ? "_trash" : "");

sql ("ALTER table config_preferences%s ADD COLUMN pref_name text;",
trash ? "_trash" : "");
sql ("UPDATE config_preferences%s"
" SET pref_name = substring (name, '^[^:]*:[^:]*:[^:]*:(.*)');",
trash ? "_trash" : "");
}

/**
* @brief Migrate the database from version 253 to version 254.
*
* @return 0 success, -1 error.
*/
int
migrate_253_to_254 ()
{
sql_begin_immediate ();

/* Ensure that the database is currently version 253. */

if (manage_db_version () != 253)
{
sql_rollback ();
return -1;
}

/* Update the database. */

migrate_253_to_254_alter(0);
migrate_253_to_254_alter(1);

/* Set the database version to 254. */

set_db_version (254);

sql_commit ();

return 0;
}


#undef UPDATE_DASHBOARD_SETTINGS

/**
Expand Down Expand Up @@ -3118,6 +3184,7 @@ static migrator_t database_migrators[] = {
{251, migrate_250_to_251},
{252, migrate_251_to_252},
{253, migrate_252_to_253},
{254, migrate_253_to_254},
/* End marker. */
{-1, NULL}};

Expand Down
12 changes: 10 additions & 2 deletions src/manage_pg.c
Original file line number Diff line number Diff line change
Expand Up @@ -2339,15 +2339,23 @@ create_tables ()
" type text,"
" name text,"
" value text,"
" default_value text);");
" default_value text,"
" pref_nvt text,"
" pref_id integer,"
" pref_type text,"
" pref_name text);");

sql ("CREATE TABLE IF NOT EXISTS config_preferences_trash"
" (id SERIAL PRIMARY KEY,"
" config integer REFERENCES configs_trash (id) ON DELETE RESTRICT,"
" type text,"
" name text,"
" value text,"
" default_value text);");
" default_value text,"
" pref_nvt text,"
" pref_id integer,"
" pref_type text,"
" pref_name text);");

sql ("CREATE TABLE IF NOT EXISTS schedules"
" (id SERIAL PRIMARY KEY,"
Expand Down
6 changes: 4 additions & 2 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -46348,8 +46348,10 @@ manage_restore (const char *id)
config = sql_last_insert_id ();

sql ("INSERT INTO config_preferences"
" (config, type, name, value, default_value)"
" SELECT %llu, type, name, value, default_value"
" (config, type, name, value, default_value, pref_nvt, pref_id,"
" pref_type, pref_name)"
" SELECT %llu, type, name, value, default_value, pref_nvt, pref_id,"
" pref_type, pref_name"
" FROM config_preferences_trash WHERE config = %llu;",
config,
resource);
Expand Down
95 changes: 36 additions & 59 deletions src/manage_sql_configs.c
Original file line number Diff line number Diff line change
Expand Up @@ -2366,14 +2366,20 @@ config_insert_preferences (config_t config,
/* NVT preference */
/* OID:PrefID:PrefType:PrefName value */
sql ("INSERT INTO config_preferences"
" (config, type, name, value)"
" VALUES (%llu, 'PLUGINS_PREFS', '%s:%s:%s:%s', '%s');",
" (config, type, name, value, pref_nvt, pref_id, pref_type,"
" pref_name)"
" VALUES (%llu, 'PLUGINS_PREFS', '%s:%s:%s:%s', '%s', '%s',"
" %i, '%s', '%s');",
config,
quoted_nvt_oid,
quoted_preference_id,
quoted_type,
quoted_preference_name,
quoted_value);
quoted_value,
quoted_nvt_oid,
atoi (preference->id),
quoted_type,
quoted_preference_name);

g_free (quoted_nvt_oid);
g_free (quoted_preference_name);
Expand Down Expand Up @@ -2861,8 +2867,10 @@ copy_config (const char* name, const char* comment, const char *config_id,
sql ("UPDATE configs SET predefined = 0 WHERE id = %llu;", new);

sql ("INSERT INTO config_preferences (config, type, name, value,"
" default_value)"
" SELECT %llu, type, name, value, default_value"
" default_value, pref_nvt, pref_id,"
" pref_type, pref_name)"
" SELECT %llu, type, name, value, default_value, pref_nvt, pref_id,"
" pref_type, pref_name"
" FROM config_preferences"
" WHERE config = %llu;", new, old);

Expand Down Expand Up @@ -3041,8 +3049,10 @@ delete_config (const char *config_id, int ultimate)
trash_config = sql_last_insert_id ();

sql ("INSERT INTO config_preferences_trash"
" (config, type, name, value, default_value)"
" SELECT %llu, type, name, value, default_value"
" (config, type, name, value, default_value, pref_nvt, pref_id,"
" pref_type, pref_name)"
" SELECT %llu, type, name, value, default_value, pref_nvt, pref_id,"
" pref_type, pref_name"
" FROM config_preferences WHERE config = %llu;",
trash_config,
config);
Expand Down Expand Up @@ -3515,8 +3525,12 @@ modify_config_preference (config_t config, const char* nvt,
const char* name, const char* value_64)
{
gchar *quoted_name, *quoted_value, *value, **splits;
gchar *quoted_pref_nvt, *quoted_pref_type, *quoted_pref_name;
int pref_id;

quoted_name = sql_quote (name);
quoted_pref_nvt = quoted_pref_type = quoted_pref_name = NULL;
pref_id = 0;

if (strlen (value_64))
{
Expand All @@ -3543,6 +3557,11 @@ modify_config_preference (config_t config, const char* nvt,
return 2;
}

quoted_pref_nvt = sql_quote (splits[0]);
pref_id = atoi (splits[1]);
quoted_pref_type = sql_quote (splits[2]);
quoted_pref_name = sql_quote (splits[3]);

/* A radio. Put the new value on the front of the list of options. */

old_value = sql_string ("SELECT value FROM config_preferences"
Expand Down Expand Up @@ -3605,9 +3624,17 @@ modify_config_preference (config_t config, const char* nvt,
nvt ? "= 'PLUGINS_PREFS'" : "= 'SERVER_PREFS'",
quoted_name);
sql ("INSERT INTO config_preferences"
" (config, type, name, value) VALUES (%llu, %s, '%s', '%s');",
" (config, type, name, value, pref_nvt, pref_id, pref_type, pref_name)"
" VALUES (%llu, %s, '%s', '%s', '%s', %i, '%s', '%s');",
config, nvt ? "'PLUGINS_PREFS'" : "'SERVER_PREFS'", quoted_name,
quoted_value);
quoted_value, quoted_pref_nvt, pref_id, quoted_pref_type,
quoted_pref_name);

g_free (quoted_value);
g_free (quoted_name);
g_free (quoted_pref_nvt);
g_free (quoted_pref_type);
g_free (quoted_pref_name);

return 0;
}
Expand Down Expand Up @@ -4159,56 +4186,6 @@ DEF_ACCESS (config_timeout_iterator_nvt_name, 2);
*/
DEF_ACCESS (config_timeout_iterator_value, 3);

/**
* @brief Update or optionally insert a NVT preference.
*
* @param[in] config_id UUID of the config to set the preference in
* @param[in] type Type of the preference, e.g. "PLUGINS_PREFS"
* @param[in] preference_name Full name of the preference
* @param[in] new_value The new value to set
* @param[in] insert Whether to insert the preference if missing
*/
void
update_config_preference (const char *config_id,
const char *type,
const char *preference_name,
const char *new_value,
gboolean insert)
{
gchar *quoted_config_id = sql_quote (config_id);
gchar *quoted_type = sql_quote (type);
gchar *quoted_name = sql_quote (preference_name);
gchar *quoted_value = sql_quote (new_value);

if (sql_int ("SELECT count (*) FROM config_preferences"
" WHERE config = (SELECT id FROM configs WHERE uuid = '%s')"
" AND type = '%s'"
" AND name = '%s';",
quoted_config_id, quoted_type, quoted_name) == 0)
{
if (insert)
{
sql ("INSERT INTO config_preferences (config, type, name, value)"
" VALUES ((SELECT id FROM configs WHERE uuid = '%s'),"
" '%s', '%s', '%s');",
quoted_config_id, quoted_type, quoted_name, quoted_value);
}
}
else
{
sql ("UPDATE config_preferences SET value = '%s'"
" WHERE config = (SELECT id FROM configs WHERE uuid = '%s')"
" AND type = '%s'"
" AND name = '%s';",
quoted_value, quoted_config_id, quoted_type, quoted_name);
}

g_free (quoted_config_id);
g_free (quoted_type);
g_free (quoted_name);
g_free (quoted_value);
}

/**
* @brief Update the cached count and growing information in a config.
*
Expand Down
18 changes: 13 additions & 5 deletions src/manage_sql_nvts.c
Original file line number Diff line number Diff line change
Expand Up @@ -1787,7 +1787,10 @@ check_preference_names (int trash, time_t modification_time)
init_iterator (&prefs,
"WITH new_pref_matches AS"
" (SELECT substring (nvt_preferences.name,"
" '^([^:]*:[^:]*)') || ':%%' AS match,"
" '^([^:]*):') AS pref_nvt,"
" CAST (substring (nvt_preferences.name,"
" '^[^:]*:([^:]*):')"
" AS integer) AS pref_id,"
" name AS new_name"
" FROM nvt_preferences"
" WHERE substr (name, 0, position (':' IN name))"
Expand All @@ -1797,7 +1800,8 @@ check_preference_names (int trash, time_t modification_time)
" configs%s.uuid AS config_id"
" FROM config_preferences%s AS c_prefs"
" JOIN new_pref_matches"
" ON c_prefs.name LIKE new_pref_matches.match"
" ON c_prefs.pref_nvt = new_pref_matches.pref_nvt"
" AND c_prefs.pref_id = new_pref_matches.pref_id"
" JOIN configs%s ON configs%s.id = c_prefs.config"
" WHERE c_prefs.name != new_name;",
modification_time,
Expand All @@ -1809,13 +1813,14 @@ check_preference_names (int trash, time_t modification_time)
while (next (&prefs))
{
resource_t preference;
const char *old_name, *new_name, *config_id;
gchar *quoted_new_name;
const char *old_name, *new_name, *config_id, *new_pref_name;
gchar *quoted_new_name, *quoted_new_pref_name;

preference = iterator_int64 (&prefs, 0);
old_name = iterator_string (&prefs, 1);
new_name = iterator_string (&prefs, 2);
config_id = iterator_string (&prefs, 3);
new_pref_name = iterator_string (&prefs, 4);

g_message ("Preference '%s' of %sconfig %s changed to '%s'",
old_name,
Expand All @@ -1824,15 +1829,18 @@ check_preference_names (int trash, time_t modification_time)
new_name);

quoted_new_name = sql_quote (new_name);
quoted_new_pref_name = sql_quote (new_pref_name);

sql ("UPDATE config_preferences%s"
" SET name = '%s'"
" SET name = '%s', pref_name = '%s'"
" WHERE id = %llu",
trash ? "_trash " : "",
quoted_new_name,
quoted_new_pref_name,
preference);

g_free (quoted_new_name);
g_free (quoted_new_pref_name);
}

sql_commit ();
Expand Down

0 comments on commit 0580e38

Please sign in to comment.