Skip to content

Commit 0580e38

Browse files
Change: Use new columns to speed up check_preference_names
2 parents a314502 + fe27460 commit 0580e38

File tree

7 files changed

+131
-73
lines changed

7 files changed

+131
-73
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ include (CPack)
9898

9999
## Variables
100100

101-
set (GVMD_DATABASE_VERSION 253)
101+
set (GVMD_DATABASE_VERSION 254)
102102

103103
set (GVMD_SCAP_DATABASE_VERSION 20)
104104

src/manage_configs.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,6 @@ config_timeout_iterator_nvt_name (iterator_t *);
168168
const char*
169169
config_timeout_iterator_value (iterator_t *);
170170

171-
void
172-
update_config_preference (const char *, const char *, const char *,
173-
const char *, gboolean);
174-
175171
gboolean
176172
configs_feed_dir_exists ();
177173

src/manage_migrators.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3059,6 +3059,72 @@ migrate_252_to_253 ()
30593059
}
30603060

30613061

3062+
/**
3063+
* @brief Alter and update for migrate_253_to_254.
3064+
*
3065+
* @param[in] trash Whether to alter trash tables.
3066+
*/
3067+
static void
3068+
migrate_253_to_254_alter (int trash)
3069+
{
3070+
sql ("ALTER TABLE config_preferences%s ADD COLUMN pref_nvt text;",
3071+
trash ? "_trash" : "");
3072+
sql ("UPDATE config_preferences%s"
3073+
" SET pref_nvt = substring (name, '^([^:]*)');",
3074+
trash ? "_trash" : "");
3075+
3076+
sql ("ALTER TABLE config_preferences%s ADD COLUMN pref_id integer;",
3077+
trash ? "_trash" : "");
3078+
sql ("UPDATE config_preferences%s"
3079+
" SET pref_id = CAST (substring (name, '^[^:]*:([^:]*)') AS integer);",
3080+
trash ? "_trash" : "");
3081+
3082+
sql ("ALTER table config_preferences%s ADD COLUMN pref_type text;",
3083+
trash ? "_trash" : "");
3084+
sql ("UPDATE config_preferences%s"
3085+
" SET pref_type = substring (name, '^[^:]*:[^:]*:([^:]*):');",
3086+
trash ? "_trash" : "");
3087+
3088+
sql ("ALTER table config_preferences%s ADD COLUMN pref_name text;",
3089+
trash ? "_trash" : "");
3090+
sql ("UPDATE config_preferences%s"
3091+
" SET pref_name = substring (name, '^[^:]*:[^:]*:[^:]*:(.*)');",
3092+
trash ? "_trash" : "");
3093+
}
3094+
3095+
/**
3096+
* @brief Migrate the database from version 253 to version 254.
3097+
*
3098+
* @return 0 success, -1 error.
3099+
*/
3100+
int
3101+
migrate_253_to_254 ()
3102+
{
3103+
sql_begin_immediate ();
3104+
3105+
/* Ensure that the database is currently version 253. */
3106+
3107+
if (manage_db_version () != 253)
3108+
{
3109+
sql_rollback ();
3110+
return -1;
3111+
}
3112+
3113+
/* Update the database. */
3114+
3115+
migrate_253_to_254_alter(0);
3116+
migrate_253_to_254_alter(1);
3117+
3118+
/* Set the database version to 254. */
3119+
3120+
set_db_version (254);
3121+
3122+
sql_commit ();
3123+
3124+
return 0;
3125+
}
3126+
3127+
30623128
#undef UPDATE_DASHBOARD_SETTINGS
30633129

30643130
/**
@@ -3118,6 +3184,7 @@ static migrator_t database_migrators[] = {
31183184
{251, migrate_250_to_251},
31193185
{252, migrate_251_to_252},
31203186
{253, migrate_252_to_253},
3187+
{254, migrate_253_to_254},
31213188
/* End marker. */
31223189
{-1, NULL}};
31233190

src/manage_pg.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,15 +2339,23 @@ create_tables ()
23392339
" type text,"
23402340
" name text,"
23412341
" value text,"
2342-
" default_value text);");
2342+
" default_value text,"
2343+
" pref_nvt text,"
2344+
" pref_id integer,"
2345+
" pref_type text,"
2346+
" pref_name text);");
23432347

23442348
sql ("CREATE TABLE IF NOT EXISTS config_preferences_trash"
23452349
" (id SERIAL PRIMARY KEY,"
23462350
" config integer REFERENCES configs_trash (id) ON DELETE RESTRICT,"
23472351
" type text,"
23482352
" name text,"
23492353
" value text,"
2350-
" default_value text);");
2354+
" default_value text,"
2355+
" pref_nvt text,"
2356+
" pref_id integer,"
2357+
" pref_type text,"
2358+
" pref_name text);");
23512359

23522360
sql ("CREATE TABLE IF NOT EXISTS schedules"
23532361
" (id SERIAL PRIMARY KEY,"

src/manage_sql.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46348,8 +46348,10 @@ manage_restore (const char *id)
4634846348
config = sql_last_insert_id ();
4634946349

4635046350
sql ("INSERT INTO config_preferences"
46351-
" (config, type, name, value, default_value)"
46352-
" SELECT %llu, type, name, value, default_value"
46351+
" (config, type, name, value, default_value, pref_nvt, pref_id,"
46352+
" pref_type, pref_name)"
46353+
" SELECT %llu, type, name, value, default_value, pref_nvt, pref_id,"
46354+
" pref_type, pref_name"
4635346355
" FROM config_preferences_trash WHERE config = %llu;",
4635446356
config,
4635546357
resource);

src/manage_sql_configs.c

Lines changed: 36 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2366,14 +2366,20 @@ config_insert_preferences (config_t config,
23662366
/* NVT preference */
23672367
/* OID:PrefID:PrefType:PrefName value */
23682368
sql ("INSERT INTO config_preferences"
2369-
" (config, type, name, value)"
2370-
" VALUES (%llu, 'PLUGINS_PREFS', '%s:%s:%s:%s', '%s');",
2369+
" (config, type, name, value, pref_nvt, pref_id, pref_type,"
2370+
" pref_name)"
2371+
" VALUES (%llu, 'PLUGINS_PREFS', '%s:%s:%s:%s', '%s', '%s',"
2372+
" %i, '%s', '%s');",
23712373
config,
23722374
quoted_nvt_oid,
23732375
quoted_preference_id,
23742376
quoted_type,
23752377
quoted_preference_name,
2376-
quoted_value);
2378+
quoted_value,
2379+
quoted_nvt_oid,
2380+
atoi (preference->id),
2381+
quoted_type,
2382+
quoted_preference_name);
23772383

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

28632869
sql ("INSERT INTO config_preferences (config, type, name, value,"
2864-
" default_value)"
2865-
" SELECT %llu, type, name, value, default_value"
2870+
" default_value, pref_nvt, pref_id,"
2871+
" pref_type, pref_name)"
2872+
" SELECT %llu, type, name, value, default_value, pref_nvt, pref_id,"
2873+
" pref_type, pref_name"
28662874
" FROM config_preferences"
28672875
" WHERE config = %llu;", new, old);
28682876

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

30433051
sql ("INSERT INTO config_preferences_trash"
3044-
" (config, type, name, value, default_value)"
3045-
" SELECT %llu, type, name, value, default_value"
3052+
" (config, type, name, value, default_value, pref_nvt, pref_id,"
3053+
" pref_type, pref_name)"
3054+
" SELECT %llu, type, name, value, default_value, pref_nvt, pref_id,"
3055+
" pref_type, pref_name"
30463056
" FROM config_preferences WHERE config = %llu;",
30473057
trash_config,
30483058
config);
@@ -3515,8 +3525,12 @@ modify_config_preference (config_t config, const char* nvt,
35153525
const char* name, const char* value_64)
35163526
{
35173527
gchar *quoted_name, *quoted_value, *value, **splits;
3528+
gchar *quoted_pref_nvt, *quoted_pref_type, *quoted_pref_name;
3529+
int pref_id;
35183530

35193531
quoted_name = sql_quote (name);
3532+
quoted_pref_nvt = quoted_pref_type = quoted_pref_name = NULL;
3533+
pref_id = 0;
35203534

35213535
if (strlen (value_64))
35223536
{
@@ -3543,6 +3557,11 @@ modify_config_preference (config_t config, const char* nvt,
35433557
return 2;
35443558
}
35453559

3560+
quoted_pref_nvt = sql_quote (splits[0]);
3561+
pref_id = atoi (splits[1]);
3562+
quoted_pref_type = sql_quote (splits[2]);
3563+
quoted_pref_name = sql_quote (splits[3]);
3564+
35463565
/* A radio. Put the new value on the front of the list of options. */
35473566

35483567
old_value = sql_string ("SELECT value FROM config_preferences"
@@ -3605,9 +3624,17 @@ modify_config_preference (config_t config, const char* nvt,
36053624
nvt ? "= 'PLUGINS_PREFS'" : "= 'SERVER_PREFS'",
36063625
quoted_name);
36073626
sql ("INSERT INTO config_preferences"
3608-
" (config, type, name, value) VALUES (%llu, %s, '%s', '%s');",
3627+
" (config, type, name, value, pref_nvt, pref_id, pref_type, pref_name)"
3628+
" VALUES (%llu, %s, '%s', '%s', '%s', %i, '%s', '%s');",
36093629
config, nvt ? "'PLUGINS_PREFS'" : "'SERVER_PREFS'", quoted_name,
3610-
quoted_value);
3630+
quoted_value, quoted_pref_nvt, pref_id, quoted_pref_type,
3631+
quoted_pref_name);
3632+
3633+
g_free (quoted_value);
3634+
g_free (quoted_name);
3635+
g_free (quoted_pref_nvt);
3636+
g_free (quoted_pref_type);
3637+
g_free (quoted_pref_name);
36113638

36123639
return 0;
36133640
}
@@ -4159,56 +4186,6 @@ DEF_ACCESS (config_timeout_iterator_nvt_name, 2);
41594186
*/
41604187
DEF_ACCESS (config_timeout_iterator_value, 3);
41614188

4162-
/**
4163-
* @brief Update or optionally insert a NVT preference.
4164-
*
4165-
* @param[in] config_id UUID of the config to set the preference in
4166-
* @param[in] type Type of the preference, e.g. "PLUGINS_PREFS"
4167-
* @param[in] preference_name Full name of the preference
4168-
* @param[in] new_value The new value to set
4169-
* @param[in] insert Whether to insert the preference if missing
4170-
*/
4171-
void
4172-
update_config_preference (const char *config_id,
4173-
const char *type,
4174-
const char *preference_name,
4175-
const char *new_value,
4176-
gboolean insert)
4177-
{
4178-
gchar *quoted_config_id = sql_quote (config_id);
4179-
gchar *quoted_type = sql_quote (type);
4180-
gchar *quoted_name = sql_quote (preference_name);
4181-
gchar *quoted_value = sql_quote (new_value);
4182-
4183-
if (sql_int ("SELECT count (*) FROM config_preferences"
4184-
" WHERE config = (SELECT id FROM configs WHERE uuid = '%s')"
4185-
" AND type = '%s'"
4186-
" AND name = '%s';",
4187-
quoted_config_id, quoted_type, quoted_name) == 0)
4188-
{
4189-
if (insert)
4190-
{
4191-
sql ("INSERT INTO config_preferences (config, type, name, value)"
4192-
" VALUES ((SELECT id FROM configs WHERE uuid = '%s'),"
4193-
" '%s', '%s', '%s');",
4194-
quoted_config_id, quoted_type, quoted_name, quoted_value);
4195-
}
4196-
}
4197-
else
4198-
{
4199-
sql ("UPDATE config_preferences SET value = '%s'"
4200-
" WHERE config = (SELECT id FROM configs WHERE uuid = '%s')"
4201-
" AND type = '%s'"
4202-
" AND name = '%s';",
4203-
quoted_value, quoted_config_id, quoted_type, quoted_name);
4204-
}
4205-
4206-
g_free (quoted_config_id);
4207-
g_free (quoted_type);
4208-
g_free (quoted_name);
4209-
g_free (quoted_value);
4210-
}
4211-
42124189
/**
42134190
* @brief Update the cached count and growing information in a config.
42144191
*

src/manage_sql_nvts.c

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1787,7 +1787,10 @@ check_preference_names (int trash, time_t modification_time)
17871787
init_iterator (&prefs,
17881788
"WITH new_pref_matches AS"
17891789
" (SELECT substring (nvt_preferences.name,"
1790-
" '^([^:]*:[^:]*)') || ':%%' AS match,"
1790+
" '^([^:]*):') AS pref_nvt,"
1791+
" CAST (substring (nvt_preferences.name,"
1792+
" '^[^:]*:([^:]*):')"
1793+
" AS integer) AS pref_id,"
17911794
" name AS new_name"
17921795
" FROM nvt_preferences"
17931796
" WHERE substr (name, 0, position (':' IN name))"
@@ -1797,7 +1800,8 @@ check_preference_names (int trash, time_t modification_time)
17971800
" configs%s.uuid AS config_id"
17981801
" FROM config_preferences%s AS c_prefs"
17991802
" JOIN new_pref_matches"
1800-
" ON c_prefs.name LIKE new_pref_matches.match"
1803+
" ON c_prefs.pref_nvt = new_pref_matches.pref_nvt"
1804+
" AND c_prefs.pref_id = new_pref_matches.pref_id"
18011805
" JOIN configs%s ON configs%s.id = c_prefs.config"
18021806
" WHERE c_prefs.name != new_name;",
18031807
modification_time,
@@ -1809,13 +1813,14 @@ check_preference_names (int trash, time_t modification_time)
18091813
while (next (&prefs))
18101814
{
18111815
resource_t preference;
1812-
const char *old_name, *new_name, *config_id;
1813-
gchar *quoted_new_name;
1816+
const char *old_name, *new_name, *config_id, *new_pref_name;
1817+
gchar *quoted_new_name, *quoted_new_pref_name;
18141818

18151819
preference = iterator_int64 (&prefs, 0);
18161820
old_name = iterator_string (&prefs, 1);
18171821
new_name = iterator_string (&prefs, 2);
18181822
config_id = iterator_string (&prefs, 3);
1823+
new_pref_name = iterator_string (&prefs, 4);
18191824

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

18261831
quoted_new_name = sql_quote (new_name);
1832+
quoted_new_pref_name = sql_quote (new_pref_name);
18271833

18281834
sql ("UPDATE config_preferences%s"
1829-
" SET name = '%s'"
1835+
" SET name = '%s', pref_name = '%s'"
18301836
" WHERE id = %llu",
18311837
trash ? "_trash " : "",
18321838
quoted_new_name,
1839+
quoted_new_pref_name,
18331840
preference);
18341841

18351842
g_free (quoted_new_name);
1843+
g_free (quoted_new_pref_name);
18361844
}
18371845

18381846
sql_commit ();

0 commit comments

Comments
 (0)