Skip to content

Commit

Permalink
Change: Speed up nvt_preference_count
Browse files Browse the repository at this point in the history
  • Loading branch information
timopollmeier authored Jul 4, 2023
2 parents 52b2f00 + 487f51b commit 5e1f2c4
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 21 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 254)
set (GVMD_DATABASE_VERSION 255)

set (GVMD_SCAP_DATABASE_VERSION 20)

Expand Down
3 changes: 2 additions & 1 deletion src/manage.h
Original file line number Diff line number Diff line change
Expand Up @@ -1924,7 +1924,8 @@ nvt_selector_iterator_type (iterator_t*);
/* NVT preferences. */

void
manage_nvt_preference_add (const char*, const char*, int);
manage_nvt_preference_add (const char*, const char*, const char*, const char*,
const char*, const char*, int);

void
manage_nvt_preferences_enable ();
Expand Down
50 changes: 49 additions & 1 deletion src/manage_migrators.c
Original file line number Diff line number Diff line change
Expand Up @@ -3058,7 +3058,6 @@ migrate_252_to_253 ()
return 0;
}


/**
* @brief Alter and update for migrate_253_to_254.
*
Expand Down Expand Up @@ -3124,6 +3123,54 @@ migrate_253_to_254 ()
return 0;
}

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

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

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

/* Update the database. */

sql ("ALTER TABLE nvt_preferences ADD COLUMN pref_nvt text;");
sql ("UPDATE nvt_preferences"
" SET pref_nvt = substring (name, '^([^:]*)')"
" WHERE name LIKE '%:%';");

sql ("ALTER TABLE nvt_preferences ADD COLUMN pref_id integer;");
sql ("UPDATE nvt_preferences"
" SET pref_id = CAST (substring (name, '^[^:]*:([^:]*)') AS integer)"
" WHERE name LIKE '%:%';");

sql ("ALTER table nvt_preferences ADD COLUMN pref_type text;");
sql ("UPDATE nvt_preferences"
" SET pref_type = substring (name, '^[^:]*:[^:]*:([^:]*):')"
" WHERE name LIKE '%:%';");

sql ("ALTER table nvt_preferences ADD COLUMN pref_name text;");
sql ("UPDATE nvt_preferences"
" SET pref_name = substring (name, '^[^:]*:[^:]*:[^:]*:(.*)')"
" WHERE name LIKE '%:%';");

/* Set the database version to 255. */

set_db_version (255);

sql_commit ();

return 0;
}

#undef UPDATE_DASHBOARD_SETTINGS

Expand Down Expand Up @@ -3185,6 +3232,7 @@ static migrator_t database_migrators[] = {
{252, migrate_251_to_252},
{253, migrate_252_to_253},
{254, migrate_253_to_254},
{255, migrate_254_to_255},
/* End marker. */
{-1, NULL}};

Expand Down
4 changes: 4 additions & 0 deletions src/manage_pg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1774,6 +1774,10 @@ create_tables_nvt (const gchar *suffix)
sql ("CREATE TABLE IF NOT EXISTS nvt_preferences%s"
" (id SERIAL PRIMARY KEY,"
" name text UNIQUE NOT NULL,"
" pref_nvt text,"
" pref_id integer,"
" pref_type text,"
" pref_name text,"
" value text);",
suffix);

Expand Down
1 change: 1 addition & 0 deletions src/manage_preferences.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ preference_free (preference_t *preference)
{
free (preference->id);
free (preference->name);
free (preference->pref_name);
free (preference->type);
free (preference->value);
free (preference->nvt_name);
Expand Down
3 changes: 2 additions & 1 deletion src/manage_preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
*/
typedef struct
{
char *name; ///< Name of preference.
char *name; ///< Full name of preference, including OID etc.
char *pref_name; ///< Name of preference.
char *id; ///< ID of preference.
char *type; ///< Type of preference (radio, password, ...).
char *value; ///< Value of preference.
Expand Down
40 changes: 26 additions & 14 deletions src/manage_sql_configs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1699,13 +1699,22 @@ check_config_families ()
* @param[in] rebuild Whether a rebuild is happening.
*/
void
manage_nvt_preference_add (const char* name, const char* value, int rebuild)
manage_nvt_preference_add (const char *name, const char *value,
const char *oid, const char *id,
const char *type, const char *pref_name,
int rebuild)
{
gchar* quoted_name = sql_quote (name);
gchar* quoted_value = sql_quote (value);

if (strcmp (name, "port_range"))
{
gchar *quoted_name, *quoted_value, *quoted_oid, *quoted_type;
gchar *quoted_pref_name;

quoted_name = sql_quote (name);
quoted_value = sql_quote (value);
quoted_oid = sql_quote (oid);
quoted_type = sql_quote (type);
quoted_pref_name = sql_quote (pref_name);

if (rebuild == 0) {
if (sql_int ("SELECT EXISTS"
" (SELECT * FROM nvt_preferences"
Expand All @@ -1714,14 +1723,19 @@ manage_nvt_preference_add (const char* name, const char* value, int rebuild)
sql ("DELETE FROM nvt_preferences WHERE name = '%s';", quoted_name);
}

sql ("INSERT into nvt_preferences%s (name, value)"
" VALUES ('%s', '%s');",
sql ("INSERT into nvt_preferences%s"
" (name, value, pref_nvt, pref_id, pref_type, pref_name)"
" VALUES ('%s', '%s', '%s', %i, '%s', '%s');",
rebuild ? "_rebuild" : "",
quoted_name, quoted_value);
}
quoted_name, quoted_value, quoted_oid, atoi (id), quoted_type,
quoted_pref_name);

g_free (quoted_name);
g_free (quoted_value);
g_free (quoted_name);
g_free (quoted_value);
g_free (quoted_oid);
g_free (quoted_type);
g_free (quoted_pref_name);
}
}

/**
Expand Down Expand Up @@ -1940,10 +1954,8 @@ nvt_preference_count (const char *oid)
{
gchar *quoted_oid = sql_quote (oid);
int ret = sql_int ("SELECT COUNT(*) FROM nvt_preferences"
" WHERE name != '%s:0:entry:Timeout'"
" AND name %s '%s:%%';",
quoted_oid,
sql_ilike_op (),
" WHERE NOT (pref_type = 'entry' AND pref_name = 'Timeout')"
" AND pref_nvt = '%s';",
quoted_oid);
g_free (quoted_oid);
return ret;
Expand Down
13 changes: 10 additions & 3 deletions src/manage_sql_nvts.c
Original file line number Diff line number Diff line change
Expand Up @@ -1289,7 +1289,10 @@ insert_nvt_preference (gpointer nvt_preference, gpointer rebuild)
return;

preference = (preference_t*) nvt_preference;
manage_nvt_preference_add (preference->name, preference->value, GPOINTER_TO_INT (rebuild));
manage_nvt_preference_add (preference->name, preference->value,
preference->nvt_oid, preference->id,
preference->type, preference->pref_name,
GPOINTER_TO_INT (rebuild));
}

/**
Expand Down Expand Up @@ -1402,15 +1405,19 @@ update_preferences_from_vt (element_t vt, const gchar *oid, GList **preferences)
id,
type,
text);
g_free (text);

blank_control_chars (full_name);
preference = g_malloc0 (sizeof (preference_t));
preference->free_strings = 1;
preference->name = full_name;
if (def)
preference->value = element_text (def);
else
preference->value = g_strdup ("");
preference->nvt_oid = g_strdup (oid);
preference->id = g_strdup (id);
preference->type = g_strdup (type);
preference->pref_name = text;
*preferences = g_list_prepend (*preferences, preference);
}

Expand Down Expand Up @@ -1798,7 +1805,7 @@ update_nvts_from_vts (element_t *get_vts_response,
rebuild ? "_rebuild" : "",
nvti_oid (nvti));
insert_nvt_preferences_list (preferences, rebuild);
g_list_free_full (preferences, g_free);
g_list_free_full (preferences, (GDestroyNotify) preference_free);

nvti_free (nvti);
vt = element_next (vt);
Expand Down

0 comments on commit 5e1f2c4

Please sign in to comment.