slowly getting ready for parallel processing
This commit is contained in:
72
table.cpp
72
table.cpp
@@ -43,11 +43,44 @@ ColDefNode Table::get_column_def(int col_index) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// std::mutex insert_guard;
|
||||||
Row& Table::create_empty_row() {
|
Row& Table::create_empty_row() {
|
||||||
|
// std::unique_lock guard(insert_guard);
|
||||||
|
|
||||||
m_rows.emplace_back(columns_count(), false);
|
m_rows.emplace_back(columns_count(), false);
|
||||||
return m_rows.back();
|
return m_rows.back();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Table::create_row_from_vector(const std::vector<ColDefNode> &colDefs, const std::vector<std::string> &csv_line) {
|
||||||
|
// std::unique_lock guard(insert_guard);
|
||||||
|
|
||||||
|
// prepare empty new_row
|
||||||
|
Row& new_row = create_empty_row();
|
||||||
|
|
||||||
|
// copy values
|
||||||
|
for (size_t i = 0; i < std::min<size_t>(columns_count(), csv_line.size()); i++) {
|
||||||
|
const ColDefNode & col_def = colDefs[i];
|
||||||
|
|
||||||
|
if (csv_line[i].empty()) {
|
||||||
|
new_row.setColumnNull(col_def.order);
|
||||||
|
} else if (col_def.type == ColumnType::integer_type) {
|
||||||
|
new_row.setIntColumnValue(col_def.order, Settings::string_to_long(csv_line[i]));
|
||||||
|
} else if (col_def.type == ColumnType::float_type) {
|
||||||
|
new_row.setFloatColumnValue(col_def.order, Settings::string_to_double(csv_line[i]));
|
||||||
|
} else if (col_def.type == ColumnType::varchar_type) {
|
||||||
|
new_row.setStringColumnValue(col_def.order, csv_line[i]);
|
||||||
|
} else if (col_def.type == ColumnType::date_type) {
|
||||||
|
new_row.setDateColumnValue(col_def.order, csv_line[i]);
|
||||||
|
} else if (col_def.type == ColumnType::bool_type) {
|
||||||
|
new_row.setBoolColumnValue(col_def.order, csv_line[i]);
|
||||||
|
} else
|
||||||
|
throw Exception("unsupported column type");
|
||||||
|
}
|
||||||
|
|
||||||
|
// append new_row
|
||||||
|
commit_row(new_row);
|
||||||
|
}
|
||||||
|
|
||||||
std::string Table::csv_string() {
|
std::string Table::csv_string() {
|
||||||
const size_t k_row_size_est = m_col_defs.size() * 16;
|
const size_t k_row_size_est = m_col_defs.size() * 16;
|
||||||
|
|
||||||
@@ -85,7 +118,7 @@ int Table::load_csv_string(const std::string &content) {
|
|||||||
std::vector<ColDefNode> &colDefs = m_col_defs;
|
std::vector<ColDefNode> &colDefs = m_col_defs;
|
||||||
|
|
||||||
CsvReader csvparser{};
|
CsvReader csvparser{};
|
||||||
int row_cnt = csvparser.parseCSV2(content, colDefs, *this);
|
int row_cnt = csvparser.parseCSVString(content, colDefs, *this);
|
||||||
|
|
||||||
return row_cnt;
|
return row_cnt;
|
||||||
}
|
}
|
||||||
@@ -114,39 +147,11 @@ int Table::load_csv_file(const std::string &filename) {
|
|||||||
|
|
||||||
// load rows
|
// load rows
|
||||||
CsvReader csvparser{};
|
CsvReader csvparser{};
|
||||||
int row_cnt = csvparser.parseCSV(filename, colDefs, *this);
|
int row_cnt = csvparser.parseCSVFile(filename, colDefs, *this);
|
||||||
|
|
||||||
return row_cnt;
|
return row_cnt;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Table::create_row_from_vector(const std::vector<ColDefNode> &colDefs, const std::vector<std::string> &csv_line) {
|
|
||||||
// prepare empty new_row
|
|
||||||
Row& new_row = create_empty_row();
|
|
||||||
|
|
||||||
// copy values
|
|
||||||
for (size_t i = 0; i < std::min<size_t>(columns_count(), csv_line.size()); i++) {
|
|
||||||
const ColDefNode & col_def = colDefs[i];
|
|
||||||
|
|
||||||
if (csv_line[i].empty()) {
|
|
||||||
new_row.setColumnNull(col_def.order);
|
|
||||||
} else if (col_def.type == ColumnType::integer_type) {
|
|
||||||
new_row.setIntColumnValue(col_def.order, Settings::string_to_long(csv_line[i]));
|
|
||||||
} else if (col_def.type == ColumnType::float_type) {
|
|
||||||
new_row.setFloatColumnValue(col_def.order, Settings::string_to_double(csv_line[i]));
|
|
||||||
} else if (col_def.type == ColumnType::varchar_type) {
|
|
||||||
new_row.setStringColumnValue(col_def.order, csv_line[i]);
|
|
||||||
} else if (col_def.type == ColumnType::date_type) {
|
|
||||||
new_row.setDateColumnValue(col_def.order, csv_line[i]);
|
|
||||||
} else if (col_def.type == ColumnType::bool_type) {
|
|
||||||
new_row.setBoolColumnValue(col_def.order, csv_line[i]);
|
|
||||||
} else
|
|
||||||
throw Exception("unsupported column type");
|
|
||||||
}
|
|
||||||
|
|
||||||
// append new_row
|
|
||||||
commit_row(new_row);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Table::print() {
|
void Table::print() {
|
||||||
std::string out{"| "};
|
std::string out{"| "};
|
||||||
std::string out2{"+-"};
|
std::string out2{"+-"};
|
||||||
@@ -226,12 +231,11 @@ void Table::validate_column(const ColDefNode *col_def, ValueNode *col_val) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Table::validate_column(const ColDefNode *col_def, ColValue &col_val) {
|
void Table::validate_column(const ColDefNode *col_def, ColValue &col_val) {
|
||||||
if (!col_def->null && col_val.isNull()) {
|
if (!col_def->null && col_val.isNull())
|
||||||
throw Exception("Column " + col_def->name + " cannot be null");
|
throw Exception("Column " + col_def->name + " cannot be null");
|
||||||
}
|
|
||||||
if (col_def->type == ColumnType::varchar_type && !col_val.isNull() && col_val.getStringValue().size() > col_def->length) {
|
if (col_def->type == ColumnType::varchar_type && !col_val.isNull() && col_val.getStringValue().size() > col_def->length)
|
||||||
throw Exception("Column value of " + col_def->name + " is too long (" + col_val.getStringValue() + ")");
|
throw Exception("Column value of " + col_def->name + " is too long (" + col_val.getStringValue() + ")");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Table::validate_row(Row &row) {
|
void Table::validate_row(Row &row) {
|
||||||
|
|||||||
Reference in New Issue
Block a user