Exemple #1
0
struct ssa_db *ssa_db_load(const char *path_dir, enum ssa_db_helper_mode mode)
{
	DIR *d;
	FILE *fd;
	struct dirent *dir;
	struct ssa_db *p_ssa_db = NULL;
	char *tbl_names;
	uint64_t data_tbls_n = 0;
	uint64_t i = 0;
	struct db_table_def table_def;
	char buffer[SSA_DB_HELPER_PATH_MAX] = {};

	ssa_log_func(SSA_LOG_DEFAULT);
	if (mode != SSA_DB_HELPER_STANDARD && mode != SSA_DB_HELPER_DEBUG) {
		ssa_log_err(SSA_LOG_DEFAULT, "mode (%d) not supported for loading\n", mode);
		return NULL;
	}

	d = opendir(path_dir);
	if (!d)
		return NULL;

	while ((dir = readdir(d)) != NULL) {
		if (dir->d_type != DT_DIR ||
		    !strcmp(dir->d_name, ".") ||
		    !strcmp(dir->d_name, ".."))
			continue;
		data_tbls_n++;
	}
	rewinddir(d);

	tbl_names = (char *) malloc(data_tbls_n * sizeof(*tbl_names) * DB_NAME_LEN);
	if (!tbl_names) {
		ssa_log_err(SSA_LOG_DEFAULT, "unable to allocate table names buffer\n");
		closedir(d);
		return NULL;
	}

	while ((dir = readdir(d)) != NULL) {
		if (dir->d_type != DT_DIR ||
		    !strcmp(dir->d_name, ".") ||
		    !strcmp(dir->d_name, ".."))
			continue;

		sprintf(buffer, "%s/%s/%s", path_dir, dir->d_name,
			SSA_DB_HELPER_TABLE_DEF_NAME);
		fd = fopen(buffer, SSA_DB_HELPER_FILE_READ_MODE_TXT);
		if (!fd) {
			ssa_log_err(SSA_LOG_DEFAULT, "Failed opening %s file\n", buffer);
			closedir(d);
			goto Error;
		}
		ssa_db_table_def_load(fd, &table_def);
		fclose(fd);

		strcpy((tbl_names + DB_NAME_LEN * table_def.id.table),
		       dir->d_name);
	}
	closedir(d);

	p_ssa_db = ssa_db_load_allocate_new(path_dir, tbl_names, data_tbls_n);
	if (!p_ssa_db) {
		ssa_log_err(SSA_LOG_DEFAULT, "Failed allocating SSA DB\n");
		goto Error;
	}

	sprintf(buffer, "%s/%s", path_dir, SSA_DB_HELPER_DB_DEF_NAME);
	fd = fopen(buffer, SSA_DB_HELPER_FILE_READ_MODE_TXT);
	if (!fd) {
		ssa_log_err(SSA_LOG_DEFAULT, "Failed opening %s file\n", buffer);
		goto Error;
	}
	ssa_db_db_def_load(fd, &p_ssa_db->db_def);
	fclose(fd);

	ssa_db_dataset_init(&p_ssa_db->db_table_def, DB_DS_VERSION,
			    sizeof(p_ssa_db->db_table_def),
			    DBT_ACCESS_NET_ORDER, p_ssa_db->db_def.id.db,
			    DBT_DEF_DS_ID, 0, 0 /* epoch */, 0 /* set_size */,
			    0 /* set_offset */, 0 /* set_count */);

	for (i = 0; i < data_tbls_n; i++) {
		sprintf(buffer, "%s/%s", path_dir, tbl_names + DB_NAME_LEN * i);
		ssa_db_tbl_load(buffer, p_ssa_db, i, mode);
		ssa_log(SSA_LOG_DEFAULT, "%s table was loaded\n",
			p_ssa_db->p_def_tbl[i].name);
	}
	free(tbl_names);

	return p_ssa_db;

Error:
	free(tbl_names);
	if (p_ssa_db)
		ssa_db_destroy(p_ssa_db);
	return NULL;
}
Exemple #2
0
/** =========================================================================
 */
void ssa_db_init(struct ssa_db * p_ssa_db, char * name, uint8_t db_id,
		 const struct db_table_def *def_tbl,
		 const struct db_dataset *dataset_tbl,
		 const struct db_dataset *field_dataset_tbl,
		 const struct db_field_def *field_tbl)
{
	const struct db_table_def *p_tbl_def = NULL;
	const struct db_dataset *p_dataset = NULL;
	const struct db_field_def *p_field_def = NULL;

	if (!p_ssa_db)
		return;

	/*
	 * Database definition initialization
	 */
	ssa_db_def_init(&p_ssa_db->db_def, DB_DEF_VERSION,
			sizeof(p_ssa_db->db_def), db_id,
			0, 0, name, sizeof(*p_ssa_db->p_def_tbl));

	/*
	 * Definition tables dataset initialization
	 */
	ssa_db_dataset_init(&p_ssa_db->db_table_def, DB_DS_VERSION,
			    sizeof(p_ssa_db->db_table_def),
			    DBT_ACCESS_NET_ORDER, db_id, DBT_DEF_DS_ID,
			    0, 0 /* epoch */, 0 /* set_size */,
			    0 /* set_offset */, 0 /* set_count */);

	/* adding table definitions */
	for (p_tbl_def = def_tbl; p_tbl_def->version; p_tbl_def++)
		ssa_db_table_def_insert(p_ssa_db->p_def_tbl,
					&p_ssa_db->db_table_def,
					p_tbl_def->version, p_tbl_def->size,
					p_tbl_def->type, p_tbl_def->access,
					p_tbl_def->id.db, p_tbl_def->id.table,
					p_tbl_def->id.field, p_tbl_def->name,
					ntohl(p_tbl_def->record_size),
					ntohl(p_tbl_def->ref_table_id));

	/* data tables datasets initialization */
	for (p_dataset = dataset_tbl; p_dataset->version; p_dataset++)
		ssa_db_dataset_init(&p_ssa_db->p_db_tables[p_dataset->id.table],
				    p_dataset->version, p_dataset->size,
				    p_dataset->access, p_dataset->id.db,
				    p_dataset->id.table, p_dataset->id.field,
				    p_dataset->epoch, p_dataset->set_size,
				    p_dataset->set_offset,
				    p_dataset->set_count);

	/* field tables datasets initialization */
	for (p_dataset = dataset_tbl; p_dataset->version; p_dataset++)
		ssa_db_dataset_init(&p_ssa_db->p_db_field_tables[p_dataset->id.table],
				    p_dataset->version, p_dataset->size,
				    p_dataset->access, p_dataset->id.db,
				    p_dataset->id.table, p_dataset->id.field,
				    p_dataset->epoch, p_dataset->set_size,
				    p_dataset->set_offset,
				    p_dataset->set_count);

	/* field tables initialization */
	for (p_tbl_def = def_tbl; p_tbl_def->version; p_tbl_def++) {
		uint8_t tbl_id = ntohl(p_tbl_def->ref_table_id);
		if (p_tbl_def->type != DBT_TYPE_DEF)
			continue;
		for (p_field_def = field_tbl; p_field_def->version; p_field_def++) {
                        if (p_field_def->id.table == p_tbl_def->id.table) {
                                ssa_db_field_def_insert(p_ssa_db->pp_field_tables[tbl_id],
                                                        &p_ssa_db->p_db_field_tables[tbl_id],
                                                        p_field_def->version, p_field_def->type,
                                                        p_field_def->id.db, p_field_def->id.table,
                                                        p_field_def->id.field, p_field_def->name,
                                                        ntohl(p_field_def->field_size),
                                                        ntohl(p_field_def->field_offset));
			}
                }
        }
}
Exemple #3
0
static int ssa_db_tbl_load(char *dir_path, struct ssa_db *p_ssa_db,
			   uint64_t tbl_idx, enum ssa_db_helper_mode mode)
{
	FILE *fd;
	struct db_table_def table_def, field_table_def;
	struct db_dataset dataset, field_dataset;
	int var_size_recs = 0;
	char buffer[SSA_DB_HELPER_PATH_MAX] = {};

	/* table definition loading */
	sprintf(buffer, "%s/%s", dir_path, SSA_DB_HELPER_TABLE_DEF_NAME);
	fd = fopen(buffer, SSA_DB_HELPER_FILE_READ_MODE_TXT);
	if (!fd) {
		ssa_log_err(SSA_LOG_DEFAULT, "Failed opening %s file\n", buffer);
		return -1;
	}
	ssa_db_table_def_load(fd, &table_def);
	fclose(fd);

	ssa_db_table_def_insert(p_ssa_db->p_def_tbl,
				&p_ssa_db->db_table_def,
				table_def.version, table_def.size,
				table_def.type, table_def.access,
				table_def.id.db, table_def.id.table,
				table_def.id.field, table_def.name,
				ntohl(table_def.record_size),
				ntohl(table_def.ref_table_id));

	if (table_def.record_size == DB_VARIABLE_SIZE)
		var_size_recs = 1;

	/* data dataset loading */
	sprintf(buffer, "%s/%s", dir_path, SSA_DB_HELPER_DATASET_NAME);
	fd = fopen(buffer, SSA_DB_HELPER_FILE_READ_MODE_TXT);
	if (!fd) {
		ssa_log_err(SSA_LOG_DEFAULT, "Failed opening %s file\n", buffer);
		return -1;
	}
	ssa_db_dataset_load(fd, &dataset);
	fclose(fd);

	ssa_db_dataset_init(&p_ssa_db->p_db_tables[tbl_idx],
			    dataset.version, dataset.size,
			    dataset.access, dataset.id.db,
			    dataset.id.table, dataset.id.field,
			    ntohll(dataset.epoch), ntohll(dataset.set_size),
			    ntohll(dataset.set_offset),
			    ntohll(dataset.set_count));

	if (!var_size_recs) {
		/* field table definition loading */
		sprintf(buffer, "%s/%s", dir_path, SSA_DB_HELPER_FIELD_DEF_NAME);
		fd = fopen(buffer, SSA_DB_HELPER_FILE_READ_MODE_TXT);
		if (!fd) {
			ssa_log_err(SSA_LOG_DEFAULT, "Failed opening %s file\n", buffer);
			return -1;
		}
		ssa_db_table_def_load(fd, &field_table_def);
		fclose(fd);

		ssa_db_table_def_insert(p_ssa_db->p_def_tbl,
					&p_ssa_db->db_table_def,
					field_table_def.version,
					field_table_def.size,
					field_table_def.type,
					field_table_def.access,
					field_table_def.id.db,
					field_table_def.id.table,
					field_table_def.id.field,
					field_table_def.name,
					ntohl(field_table_def.record_size),
					ntohl(field_table_def.ref_table_id));

		/* field dataset loading */
		sprintf(buffer, "%s/%s", dir_path, SSA_DB_HELPER_FIELDS_DATASET_NAME);
		fd = fopen(buffer, SSA_DB_HELPER_FILE_READ_MODE_TXT);
		if (!fd) {
			ssa_log_err(SSA_LOG_DEFAULT, "Failed opening %s file\n", buffer);
			return -1;
		}
		ssa_db_dataset_load(fd, &field_dataset);
		fclose(fd);

		ssa_db_dataset_init(&p_ssa_db->p_db_field_tables[tbl_idx],
				    field_dataset.version, field_dataset.size,
				    field_dataset.access, field_dataset.id.db,
				    field_dataset.id.table, field_dataset.id.field,
				    ntohll(field_dataset.epoch),
				    ntohll(field_dataset.set_size),
				    ntohll(field_dataset.set_offset),
				    ntohll(field_dataset.set_count));

		sprintf(buffer, "%s/%s", dir_path, SSA_DB_HELPER_FIELDS_NAME);
		fd = fopen(buffer, SSA_DB_HELPER_FILE_READ_MODE_TXT);
		if (!fd) {
			ssa_log_err(SSA_LOG_DEFAULT, "Failed opening %s file\n", buffer);
			return -1;
		}
		ssa_db_field_tbl_load(fd, &field_dataset,
				      p_ssa_db->pp_field_tables[tbl_idx]);
		fclose(fd);
	}

	/* TODO (optional): add distinguish between added and removed records */
	sprintf(buffer, "%s/%s", dir_path, SSA_DB_HELPER_DATA_NAME);
	if (mode == SSA_DB_HELPER_STANDARD)
		fd = fopen(buffer, SSA_DB_HELPER_FILE_READ_MODE_BIN);
	else
		fd = fopen(buffer, SSA_DB_HELPER_FILE_READ_MODE_TXT);
	if (!fd) {
		ssa_log_err(SSA_LOG_DEFAULT, "Failed opening %s file\n", buffer);
		return -1;
	}
	if (var_size_recs)
		ssa_db_rec_tbl_load_var_size(fd, mode, &table_def,&dataset,
					     p_ssa_db->pp_tables[tbl_idx]);
	else
		ssa_db_rec_tbl_load(fd, mode, &table_def,
				    &dataset, p_ssa_db->pp_tables[tbl_idx],
				    &field_dataset,
				    (struct db_field_def *)p_ssa_db->pp_field_tables[tbl_idx]);

	fclose(fd);

	return 0;
}