Skip to content

Commit 68a69ed

Browse files
solve importation configuration bug (#29)
Co-authored-by: scanossmining <[email protected]>
1 parent 343ed1a commit 68a69ed

3 files changed

Lines changed: 45 additions & 24 deletions

File tree

src/import.c

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,7 +1110,7 @@ const char * config_parameters[] = {
11101110
.keys_number = 1,\
11111111
.overwrite = 0,\
11121112
.sort = 1,\
1113-
.version_validation = 0,\
1113+
.version_validation = 1,\
11141114
.verbose = 0,\
11151115
.is_mz_table = 0,\
11161116
.binary_mode = 0,\
@@ -1192,6 +1192,23 @@ bool ldb_importation_config_parse(import_params_t * opt, char * line)
11921192
return true;
11931193
}
11941194

1195+
//cmd_in take precedency of the global cfg
1196+
static void opt_add(const import_params_t * cmd_in, import_params_t * cfg)
1197+
{
1198+
import_params_t def = {.params = LDB_IMPORTATION_CONFIG_DEFAULT};
1199+
for (int i = 0; i < IMPORT_PARAMS_NUMBER; i++)
1200+
{
1201+
if (i == IMPORT_PARAMS_NUMBER -1 && strcmp(cmd_in->params.tmp_path, def.params.tmp_path))
1202+
{
1203+
strcpy(cfg->params.tmp_path, cmd_in->params.tmp_path);
1204+
}
1205+
else if (cmd_in->params_arr[i] > -1)
1206+
{
1207+
cfg->params_arr[i] = cmd_in->params_arr[i];
1208+
}
1209+
}
1210+
}
1211+
11951212
bool ldb_create_db_config_default(char * dbname)
11961213
{
11971214
#define DEFAULT_CONFIG_STRING "GLOBAL: (VALIDATE_FIELDS=1, VALIDATE_VERSION=1, SORT=1, FILE_DEL=0, OVERWRITE=0, WFP=0, MZ=0, VERBOSE=0, THREADS=%d, COLLATE=0, MAX_RECORD=2048, MAX_RAM_PERCENT=50, TMP_PATH=/tmp)\n"\
@@ -1244,6 +1261,9 @@ static int load_import_config(ldb_importation_config_t * config, import_params_t
12441261
sprintf(config_path,"%s%s.conf", LDB_CFG_PATH, config->dbname);
12451262
int result = -1;
12461263
bool found = false;
1264+
import_params_t def = {.params = LDB_IMPORTATION_CONFIG_DEFAULT};
1265+
config->opt = def;
1266+
common->params = def.params;
12471267

12481268
if (ldb_file_exists(config_path))
12491269
{
@@ -1264,13 +1284,13 @@ static int load_import_config(ldb_importation_config_t * config, import_params_t
12641284
strncpy(table_test, line, separator - line);
12651285

12661286
const char global_def[]= "GLOBAL";
1267-
if (!strcmp(table_test, global_def))
1287+
if (!strcmp(table_test, global_def) && result < 0) // force global config on the top of the file
12681288
{
12691289
// if (config->opt.params.verbose)
12701290
// fprintf(stderr, "The table %s is defined at %s as GLOBAL, some parameter may be overwritten\n", config->table, config_path);
12711291

12721292
ldb_importation_config_parse(common, line + strlen(global_def));
1273-
config->opt.params = common->params;
1293+
opt_add(common, &config->opt);
12741294
}
12751295
else
12761296
{
@@ -1540,23 +1560,6 @@ struct ldb_importation_jobs_s
15401560
import_params_t * global_opt;
15411561
};
15421562

1543-
//cmd_in take precedency of the global cfg
1544-
static void opt_add(const import_params_t * cmd_in, import_params_t * cfg)
1545-
{
1546-
import_params_t def = {.params = LDB_IMPORTATION_CONFIG_DEFAULT};
1547-
for (int i = 0; i < IMPORT_PARAMS_NUMBER; i++)
1548-
{
1549-
if (i == IMPORT_PARAMS_NUMBER -1 && strcmp(cmd_in->params.tmp_path, def.params.tmp_path))
1550-
{
1551-
strcpy(cfg->params.tmp_path, cmd_in->params.tmp_path);
1552-
}
1553-
else if (cmd_in->params_arr[i] > -1)
1554-
{
1555-
cfg->params_arr[i] = cmd_in->params_arr[i];
1556-
}
1557-
}
1558-
}
1559-
15601563
static void recurse_directory(struct ldb_importation_jobs_s * jobs, char *name, char * father)
15611564
{
15621565
DIR *dir;
@@ -1609,9 +1612,9 @@ static void recurse_directory(struct ldb_importation_jobs_s * jobs, char *name,
16091612

16101613
/*dir of the job*/
16111614
snprintf(jobs->job[jobs->number]->path, LDB_MAX_PATH, "%s", dirname(path));
1615+
16121616
//load of the configuration of the table (if it is defined).
16131617
int sort = load_import_config(jobs->job[jobs->number], jobs->global_opt);
1614-
16151618
//force stdin command priority
16161619
opt_add(jobs->user_opt, &jobs->job[jobs->number]->opt);
16171620

@@ -1862,6 +1865,8 @@ bool ldb_import_command(char * dbtable, char * path, char * config)
18621865
if (job.opt.params.version_validation && !version_present)
18631866
{
18641867
logger_basic("Failed to validate version.json, check if it is present in %s and it has the correct format\n", job.path);
1868+
logger_basic("You can avoid this error by setting \"VALIDATE_VERSION = 0\" in the import configuration options\n", job.path);
1869+
18651870
exit(EXIT_FAILURE);
18661871
}
18671872

@@ -1938,7 +1943,7 @@ bool ldb_import_command(char * dbtable, char * path, char * config)
19381943
logger_basic("Failed to validate version.json, check if it is present in %s and it has the correct format\n", job.path);
19391944
exit(EXIT_FAILURE);
19401945
}
1941-
//import_params_t global_opt_def = {.params = LDB_IMPORTATION_CONFIG_DEFAULT};
1946+
19421947
load_import_config(&job, &global_opt);
19431948
opt_add(&user_opt, &global_opt);
19441949
opt_add(&user_opt, &job.opt);

src/ldb.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
#include "./ldb/types.h"
2828
#include "./ldb/mz.h"
2929

30-
#define LDB_VERSION "4.0.5"
30+
#define LDB_VERSION "4.0.6"
3131

3232
#define LDB_TABLE_DEFINITION_UNDEFINED -1
3333
#define LDB_TABLE_DEFINITION_STANDARD 0

test/test_kb

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,31 @@ test_09_version_validation() {
7070
test_10_configuration() {
7171
config="GLOBAL: (KEYS=-1, VALIDATE_FIELDS=1, FIELDS=1, VALIDATE_VERSION=1, SORT=1, FILE_DEL=0, OVERWRITE=0, WFP=0, MZ=0, VERBOSE=0, THREADS=20, COLLATE=0, MAX_RECORD=2048, MAX_RAM_PERCENT=50, TMP_PATH=/tmp)"
7272
sed -i "1s|.*|$config|" /usr/local/etc/scanoss/ldb/test_kb.conf
73+
7374
../ldb -q -u source/mined2 -n test_kb -q
7475
result=$(tac /var/log/scanoss/ldb/test_kb.log | grep -m 1 "GLOBAL configuration:")
7576
config="GLOBAL configuration: (KEYS=-1, VALIDATE_FIELDS=1, FIELDS=1, VALIDATE_VERSION=1, SORT=1, FILE_DEL=0, OVERWRITE=0, WFP=0, MZ=0, VERBOSE=0, THREADS=20, COLLATE=0, MAX_RECORD=2048, MAX_RAM_PERCENT=50, TMP_PATH=/tmp)"
7677
assert_equals "$config" "$result"
7778

78-
echo "bulk insert test_kb/file from source/mined/file with (TMP_PATH=/var,FILE_DEL=0,THREADS=30)" | ../ldb
79+
echo "bulk insert test_kb/file from source/mined/file with (TMP_PATH=/var,FILE_DEL=0,THREADS=30)" | ../ldb -q
7980
config="file configuration: (KEYS=2, VALIDATE_FIELDS=1, FIELDS=3, VALIDATE_VERSION=1, SORT=1, FILE_DEL=0, OVERWRITE=0, WFP=0, MZ=0, VERBOSE=0, THREADS=30, COLLATE=0, MAX_RECORD=2048, MAX_RAM_PERCENT=50, TMP_PATH=/var)"
8081
result=$(tac /var/log/scanoss/ldb/test_kb.log | grep -m 1 "file configuration:")
8182
assert_equals "$config" "$result"
83+
84+
config="GLOBAL: (COLLATE=0, MAX_RECORD=2048, MAX_RAM_PERCENT=50, TMP_PATH=/tmp)"
85+
cores=$(nproc)
86+
threads=$((cores / 2))
87+
sed -i "1s|.*|$config|" /usr/local/etc/scanoss/ldb/test_kb.conf
88+
89+
echo "bulk insert test_kb/file from source/mined/file with (TMP_PATH=/var,FILE_DEL=0)" | ../ldb -q
90+
config="file configuration: (KEYS=2, VALIDATE_FIELDS=1, FIELDS=3, VALIDATE_VERSION=1, SORT=1, FILE_DEL=0, OVERWRITE=0, WFP=0, MZ=0, VERBOSE=0, THREADS=$threads, COLLATE=0, MAX_RECORD=2048, MAX_RAM_PERCENT=50, TMP_PATH=/var)"
91+
result=$(tac /var/log/scanoss/ldb/test_kb.log | grep -m 1 "file configuration:")
92+
assert_equals "$config" "$result"
93+
94+
echo "bulk insert test_kb from source/mined with (TMP_PATH=/var,FILE_DEL=0,VALIDATE_VERSION=0)" | ../ldb -q
95+
config="file configuration: (KEYS=2, VALIDATE_FIELDS=1, FIELDS=3, VALIDATE_VERSION=0, SORT=1, FILE_DEL=0, OVERWRITE=0, WFP=0, MZ=0, VERBOSE=0, THREADS=$threads, COLLATE=0, MAX_RECORD=2048, MAX_RAM_PERCENT=50, TMP_PATH=/var)"
96+
result=$(tac /var/log/scanoss/ldb/test_kb.log | grep -m 1 "file configuration:")
97+
assert_equals "$config" "$result"
8298
}
8399

84100
setup_suite () {

0 commit comments

Comments
 (0)