#include #include #include #include #include #include "dgrid_config.h" #include "dgrid_logger.h" #include "dgridd_datastore_table.h" #include "dgridd_node_arbiter.h" namespace DGrid { TableDescription::TableDescription(const std::string& session, const std::string& space, const std::string& tname, const NodeID& master_node) :session(session), space(space), tname(tname), master_node(master_node) {} TableDescription::~TableDescription() {} TableDescription& TableDescription::operator=(const TableDescription& o) { session = o.session; space = o.space; tname = o.tname; master_node = o.master_node; fallback_nodes = o.fallback_nodes; return *this; } bool TableDescription::compare(const std::string& ss, const std::string& sp, const std::string& tn) { return ss == session && sp == space && tn == tname; } // table fields settings TableFieldParams::TableFieldParams() : max_length(0), data_type(FIELD_TYPE_RAW), sort_order(SORT_ASC) {} TableFieldParams::~TableFieldParams() {} // cell data TableCellData::TableCellData(const FieldType data_type) : type(data_type){} TableCellData::TableCellData(const FieldType data_type, const std::string& s) : type(data_type), str(s){} TableCellData::~TableCellData() {} void TableCellData::set_type(const FieldType data_type) { type = data_type; } void TableCellData::from_string(const std::string& s) { str = s; } const std::string& TableCellData::to_string() const { return str; } bool TableCellData::operator==(const TableCellData& o) const { return TableRowKeyItem::equal(type, o.str, str); } bool TableCellData::operator!=(const TableCellData& o) const { return !TableRowKeyItem::equal(type, o.str, str); } bool TableCellData::compare(const TableCellData& o, const ComparisionType op) const { return compare(o.str, op, false); } bool TableCellData::compare(const std::string& s, const ComparisionType op, bool s_is_plain) const { return compare(str, s, op, type); } bool TableCellData::compare(const std::string& str /*field data*/, const std::string& s /*condition data*/, const ComparisionType op, const FieldType type) { switch (op) { case COMPARE_EQ: return TableRowKeyItem::equal(type, str, s); case COMPARE_NE: return !TableRowKeyItem::equal(type, str, s); case COMPARE_LT: return TableRowKeyItem::compare(type, s, str); // str < s; case COMPARE_LE: return TableRowKeyItem::equal(type, str, s) || TableRowKeyItem::compare(type, s, str); // str <= s; case COMPARE_GT: return TableRowKeyItem::compare(type, str, s); // str > s; case COMPARE_GE: return TableRowKeyItem::equal(type, str, s) || TableRowKeyItem::compare(type, str, s); // str >= s; } return false; } // table row TableRowData::TableRowData(TableData* t) :table(t), key_valid(false) { if(!t) return; id_record = t->nextRecordID(); uint32_t cols = columnsCount(); cells.resize(cols, FIELD_TYPE_RAW); for(uint32_t i=0; igetFieldType(i)); } TableRowData::TableRowData(TableData* t, const std::vector& fields, TableData* src_table) : id_record(__LONG_LONG_MAX__), table(t), key_valid(false) { if(!t) return; uint32_t cols = columnsCount(); cells.resize(cols, FIELD_TYPE_RAW); for(uint32_t i=0; igetFieldType(i)); if(nullptr != src_table && cols != src_table->columnsCount()) { std::vector fields_dst; TableData::convertRowStructure(src_table, fields, t, fields_dst); setCellsData(fields_dst); } else { setCellsData(fields); } } TableRowData::~TableRowData() {} uint32_t TableRowData::columnsCount() const { return table ? table->columnsCount() : 0; } void TableRowData::setCellsData(const std::vector& fields) { uint32_t i=0; for(auto s: fields) { if(i other, const std::vector& findexes) { return equal(*other, findexes); } bool TableRowData::equal(const TableRowData& other, const std::vector& findexes) { if(cells.size() != other.cells.size()) { writeLog(LOG_WARNING, "TableRowData::equal() fields count differs"); return false; } if (findexes.empty()) { for(uint16_t i=0; i=cells.size()) { writeLog(LOG_WARNING, "Column index out of range: %d", i); return false; } if(other.cells[i] != cells[i]) return false; } } return true; } bool TableRowData::compare(const TableRowData& o, const SortKey& key) const { for(const SortKeyItem& it: key) { bool asc = !(it.flags & SORT_FLAG_DESC); if(cells[it.field_idx].compare(o.cells[it.field_idx], asc ? COMPARE_LT : COMPARE_GT)) return true; if(cells[it.field_idx].compare(o.cells[it.field_idx], asc ? COMPARE_GT : COMPARE_LT)) return false; } return false; } TableRowKey& TableRowData::getKey() { if(!key_valid) { key.clear(); std::vector cols = table->keyColumns(); for(int16_t col: cols) { if((uint16_t)colfields[col].data_type).c_str(), cells[col].to_string().c_str(), sortOrder2string(table->fields[col].sort_order).c_str()); key.push_back( TableRowKeyItem( cells[col].to_string(), table->fields[col].data_type, table->fields[col].sort_order) ); } } key_valid = true; } return key; } TableRowKey TableRowData::getKey(const std::vector& components) { TableRowKey k; k.clear(); std::vector cols = table->keyColumns(components); for(int16_t col: cols) { if((uint16_t)colfields[col].data_type).c_str(), cells[col].to_string().c_str(), sortOrder2string(table->fields[col].sort_order).c_str()); k.push_back( TableRowKeyItem( cells[col].to_string(), table->fields[col].data_type, table->fields[col].sort_order) ); } } return k; } std::vector TableRowData::getCellsData() { std::vector values; getCellsData(values); return values; } void TableRowData::getCellsData(std::vector& values) { size_t count(cells.size()); values.resize(count); for(size_t i=0; icolumnsCount(); for(uint32_t i=0; i>(ISerializer& ds, TableRowData& o) { uint32_t columns_count; std::string s; ds >> columns_count; for(uint32_t i=0; i> s; o.setCellData(i, s); } return ds; } OSerializer& operator<<(OSerializer& ds, const TableRowData& o) { uint32_t columns_count = o.columnsCount(); std::string s; ds << columns_count; for(uint32_t i=0; i(const TableRowKey& a, const TableRowKey& b) { size_t s = std::min(a.size(), b.size()); for(size_t i=0; i b[i]) return true; } return false; } bool operator==(const TableRowKey& a, const TableRowKey& b) { size_t s = std::min(a.size(), b.size()); for(size_t i=0; i(const TableRowFreeKey& a, const RowPointer& b) { for(auto i: a) { if(i.first >= b->columnsCount()) return false; if(i.second < b->getCellData(i.first)) return false; if(i.second > b->getCellData(i.first)) return true; } return false; } bool operator==(const TableRowFreeKey& a, const RowPointer& b) { for(auto i: a) { if(i.first >= b->columnsCount()) return false; if(i.second != b->getCellData(i.first)) return false; } return true; } // table TableData::TableData(const std::vector& key_fields, const std::vector& field_names, const std::vector& fields_len, const std::vector& fields_type) :columns_count(field_names.size()), key_fields(key_fields), last_record_id(1) { TableFieldParams p; fields.resize(field_names.size()); if(field_names.size() != fields_len.size()) { writeLog(LOG_WARNING, "TableData(): field_names.size() != fields_len.size()"); } if(field_names.size() != fields_type.size()) { writeLog(LOG_WARNING, "TableData(): field_names.size() != fields_type.size()"); } for(size_t i=0; i(this, field_names); } TableData::~TableData() {} int16_t TableData::columnIndex(const std::string& fname) const { for(size_t i=0; i TableData::keyColumns() const { std::vector c; for(auto col: key_fields) { c.push_back(columnIndex(col)); } if(c.empty()) writeLog(LOG_ERR, "No key fields defined"); return c; } std::vector TableData::keyColumns(const std::vector& components) const { std::vector c; for(auto col: components) { c.push_back(columnIndex(col)); } if(c.empty()) writeLog(LOG_ERR, "No key fields defined"); return c; } std::shared_ptr TableData::getHeaderLine() { return header_line; } RecordID TableData::nextRecordID() { return last_record_id++; } TableType TableData::type() { return TABLE_TYPE_INVALID; } void TableData::convertRowStructure(const TableData* const src_table, const std::vector& src_values, const TableData* const dst_table, std::vector& dst_values) { if (1 == src_table->columnsCount()) { // распаковка из table_line dst_table->unpack_row(src_values[0], dst_values); } else if (1 == dst_table->columnsCount()) { // упаковка в table_line dst_values.resize(1); src_table->pack_row(src_values, dst_values[0]); } else { // копирование соответствующих полей, а-ля move corresponding dst_values.resize(dst_table->columnsCount()); const std::vector src_fields = src_table->getFields(); for(size_t src_idx=0; src_idxcolumnIndex(src_fields[src_idx]); if(dst_idx >= 0) dst_values[dst_idx] = src_values[src_idx]; } } } void TableData::pack_row(const std::vector& src_values, std::string& packed_row) const { size_t offs=0; packed_row.clear(); for(size_t i=0; i=src_values.size()) return; packed_row += src_values[i]; offs += fields[i].max_length; packed_row.resize(offs, ' '); } } void TableData::unpack_row(const std::string& packed_row, std::vector& dst_values) const { size_t offs=0; size_t data_size = packed_row.size(); dst_values.resize(fields.size()); for(size_t i=0; i=data_size) { writeLog(LOG_WARNING, "No more data: offs=%llu", offs); return; } size_t flen = fields[i].max_length; size_t max_flen = data_size - offs; if(flen>max_flen) flen = max_flen; dst_values[i] = packed_row.substr(offs, flen); offs += fields[i].max_length; } } bool TableData::appendDataRow(const std::shared_ptr data, ModifyStatsData* stats) { if(nullptr != data->table && data->table != this && data->columnsCount() && columnsCount()) { std::vector fields_src = data->getCellsData(); std::vector fields_dst; convertRowStructure(data->table, fields_src, this, fields_dst); return appendDataRow(fields_dst, stats); } else { return appendDataRow(data->getCellsData(), stats); } } bool TableData::conditionFromKV(const std::vector& keys, const std::vector& values, SearchCondition& condition) { condition.clear(); if(keys.size() != values.size()) { writeLog(LOG_ERR, "conditionFromKV: keys.size() != values.size()"); return false; } for(size_t i=0; i& values, SearchCondition& condition) { condition.clear(); if(key_fields.size() != values.size()) { writeLog(LOG_ERR, "conditionFromV: keys.size() != values.size()"); return false; } for(size_t i=0; i& values, bool may_be_partial) { TableRowKey key; keyFromValues(key_fields, values, key, may_be_partial); return key; } bool TableData::keyFromValues(const std::vector& values, TableRowKey& key, bool may_be_partial) { return keyFromValues(key_fields, values, key, may_be_partial); } bool TableData::keyFromValues(const std::vector& keys, const std::vector& values, TableRowKey& key, bool may_be_partial, bool force_components) { std::map condition; key.clear(); size_t target_key_size = keys.size(); if(keys.size() != values.size()) { if (!may_be_partial) { writeLog(LOG_ERR, "keyFromValues: keys.size() != values.size()"); return false; } target_key_size = std::min(keys.size(), values.size()); } for(size_t i=0; i *components = force_components ? &keys : &key_fields; for(auto field: *components) { if(condition.find(field) == condition.end()) { writeLog(LOG_DEBUG, "key field '%s' not found in condition, fields used: %d", field.c_str(), key.size()); return may_be_partial && !key.empty(); } int16_t col_idx = columnIndex(field); if(col_idx < 0) { writeLog(LOG_WARNING, "column '%s' not found in table!", field); } else { key.push_back( TableRowKeyItem(condition[field], fields[col_idx].data_type, fields[col_idx].sort_order) ); } } return true; } bool TableData::freeKeyFromValues(const std::vector& keys, const std::vector& values, TableRowFreeKey& key) { key.clear(); if(keys.size() != values.size()) { writeLog(LOG_ERR, "keyFromValues: keys.size() != values.size()"); return false; } for(size_t i=0; i condition; key.clear(); // search for fields with EQUAL condition for(auto fc: cond.field_conditions) { if(COMPARE_EQ == fc.comp_type) condition[fc.field] = fc.value; } for(auto field: key_fields) { if(condition.find(field) == condition.end()) { writeLog(LOG_DEBUG, "key field '%s' not found in condition", field.c_str()); return may_be_partial && !key.empty(); } int16_t col_idx = columnIndex(field); if(col_idx < 0) { writeLog(LOG_WARNING, "column '%s' not found in table!", field); } else { key.push_back( TableRowKeyItem(condition[field], fields[col_idx].data_type, fields[col_idx].sort_order) ); } } return true; } DataIndex* TableData::findSecondaryIndex(const std::string& name) { auto it = sec_index_map.find(name); if(it != sec_index_map.end()) { return it->second.get(); } it = sec_index_map_alias.find(name); if(it != sec_index_map_alias.end()) { return it->second.get(); } return nullptr; } DataIndex* TableData::findSecondaryIndex(const std::vector& components) { for(auto& it: sec_index_map) { if(it.second->checkComponents(components)) return it.second.get(); } return nullptr; } const std::vector TableData::getKeyFields() const { return key_fields; } const std::vector TableData::getFields() const { std::vector ret; ret.resize(fields.size()); for(size_t i=0; i TableData::getFieldsLen() const { std::vector ret; ret.resize(fields.size()); for(size_t i=0; i TableData::getFieldsType() const { std::vector ret; ret.resize(fields.size()); for(size_t i=0; i=fields.size()) return FIELD_TYPE_RAW; return fields[idx].data_type; } const std::vector TableData::getSecondaryKeys() const { return sec_keys; } void TableData::setSecondaryKeys(const std::vector& keys) { resetSecondaryIndex(); sec_keys = keys; sec_index_map.clear(); for(TableKeyInfo& sk: sec_keys) { std::shared_ptr index = nullptr; if(sk.is_hashed()) index = std::make_shared(this, sk); else index = std::make_shared(this, sk); sec_index_map[sk.name] = index; if(!sk.alias.empty()) sec_index_map_alias[sk.alias] = index; } } bool TableData::resetSecondaryIndex(const std::string& name) { std::string error_string; return resetSecondaryIndex(name, error_string); } bool TableData::resetSecondaryIndex(const std::string& name, std::string& error_string) { if(name.empty()) { for(auto& index: sec_index_map) { index.second->reset(); } return true; } else { DataIndex* index = findSecondaryIndex(name); if(index) { index->reset(); return true; } error_string = "No secondary index '" + name + "'"; } return false; } bool TableData::rebuildSecondaryIndex(const std::string& name, std::string& error_string) { if(name.empty()) { for(auto& index: sec_index_map) { index.second->rebuild(); } return true; } else { DataIndex* index = findSecondaryIndex(name); if(index) { index->rebuild(); return true; } error_string = "No secondary index '" + name + "'"; } return false; } TableRowData* TableData::getDataRow_raw(const uint64_t index) const { if(index >= rows_data.size()) { writeLog(LOG_WARNING, "%s: index %llu out of range (max = %llu)", __func__, index, rows_data.size() - 1); return nullptr; } return rows_data[index].get(); } RowPointer TableData::getDataRowByID(const RecordID id) { auto it = rows_id_map.find(id); if(it != rows_id_map.end()) { return it->second; } return nullptr; } const RecordID TableData::registerRow(RowPointer row, const RecordID id, const uint64_t index) { row->id_record = id ? id : nextRecordID(); rows_id_map[row->id_record] = row; // writeLog(LOG_DEBUG, "%sregistered id = %llu", id ? "re" : "", row->id_record); // for(auto r: rows_id_map) // writeLog(LOG_DEBUG, " - %llu", r.first); if(!id) updateCursors(index, true); return row->id_record; } bool TableData::deregisterRow(const RecordID id, const uint64_t index) { auto it = rows_id_map.find(id); if(it != rows_id_map.end()) { rows_id_map.erase(it); // writeLog(LOG_DEBUG, "DEregistered id = %llu", id); // for(auto r: rows_id_map) // writeLog(LOG_DEBUG, " - %llu", r.first); updateCursors(index, false); return true; } return false; } void TableData::clearRegisteredRows() { rows_id_map.clear(); last_record_id = 1; resetCursors(); } void TableData::clear() { clearRegisteredRows(); clearData(); } // table store DGriddDataStoreTable::DGriddDataStoreTable(std::string session, DGriddNodeArbiter* a) :_node_arbiter(a), _session(session) {} DGriddDataStoreTable::~DGriddDataStoreTable() {} void DGriddDataStoreTable::setNodeArbiter(DGriddNodeArbiter* a) { _node_arbiter = a; } void DGriddDataStoreTable::showStats() { writeLog(LOG_INFO, "> Table datastote stats:"); writeLog(LOG_INFO, " Tables count: %d", getTablesCount()); } // table requests uint64_t DGriddDataStoreTable::getTablesCount() { return tables_std.size(); } std::vector DGriddDataStoreTable::getTablesList() { std::vector list; for(auto it: tables_std) { list.push_back(it.first); } return list; } DGridResult DGriddDataStoreTable::getTableSize(const std::string& tname, uint64_t& count, std::string& error_string) { auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; count = table->rowsCount(); return DGRID_OK; } TableDataPtr DGriddDataStoreTable::getTable(const std::string& tname, std::string& error_string) { auto it = tables_std.find(tname); if(it == tables_std.end()) { error_string = "Table '" + tname + "' not exists"; return nullptr; } return it->second; } DGridResult DGriddDataStoreTable::createTable(const std::string& tname, const TableType type, const std::vector& fields, const std::vector& fields_len, const std::vector& fields_type, const std::vector& keys, const std::vector& sec_keys, std::string& error_string) { auto it = tables_std.find(tname); if(it != tables_std.end()) { error_string = "Table '" + tname + "' already exists"; return DGRID_ALREADY_EXISTS; } #ifdef TABLE_DEBUG writeLog(LOG_INFO, "Creating %s table '%s': %d keys, %d fields", tableType2string(type).c_str(), tname.c_str(), keys.size(), fields.size()); std::string sl; for(auto s: keys) { sl += " " + s; } writeLog(LOG_DEBUG, " keys: %s", sl.c_str()); sl.clear(); for(auto s: fields) { sl += " " + s; } writeLog(LOG_DEBUG, " fields: %s", sl.c_str()); #endif // bool std_forced = keys.empty(); if(TABLE_STANDARD != type && keys.empty()) { writeLog(LOG_ERR, "Create %s table w/o key fields! Forced all fields as key", tableType2string(type).c_str()); } TableDataPtr table = nullptr; if(TABLE_STANDARD == type) { table = std::make_shared(keys, fields, fields_len, fields_type); } else if(TABLE_HASHED == type) { table = std::make_shared(keys.empty() ? fields : keys, fields, fields_len, fields_type); } else if(TABLE_SORTED == type || TABLE_SORTED_UNIQ == type) { table = std::make_shared(keys.empty() ? fields : keys, fields, fields_len, fields_type, TABLE_SORTED_UNIQ == type); } else { error_string = "Table type #" + std::to_string(type) + " (" + tableType2string(type) + ") not implemented"; return DGRID_NOT_IMPLEMENTED; } table->setSecondaryKeys(sec_keys); tables_std[tname] = table; return DGRID_OK; } DGridResult DGriddDataStoreTable::getStructure(const std::string& tname, TableType& type, std::vector& fields, std::vector& fields_len, std::vector& fields_type, std::vector& keys, std::vector& sec_keys, std::string& error_string) { auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; type = table->type(); keys = table->getKeyFields(); fields = table->getFields(); fields_len = table->getFieldsLen(); fields_type = table->getFieldsType(); sec_keys = table->getSecondaryKeys(); return DGRID_OK; } DGridResult DGriddDataStoreTable::dropTable(const std::string& tname, std::string& error_string) { if (!getTable(tname, error_string)) return DGRID_NOT_FOUND; tables_std.erase(tname); return DGRID_OK; } DGridResult DGriddDataStoreTable::sortTable(const std::string& tname, const SortOrderList& sort_order, const uint16_t flags, std::string& error_string) { auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; if (!table->rowsCount()) return DGRID_OK; if(!dynamic_cast(table.get())) { error_string = "Custom sort supported on STANDARD tables only!"; return DGRID_INVALID_ARGUMENT; } SortKey key; if(!createSortKey(table, sort_order, key, error_string)) { return DGRID_FAIL; } if(!table->sort(key, (flags & SORT_FLAG_STABLE) == SORT_FLAG_STABLE)) { error_string = "Internal sort error"; return DGRID_FAIL; } return DGRID_OK; } DGridResult DGriddDataStoreTable::resetSecondaryIndex(const std::string& tname, const std::vector& key_names, std::string& error_string) { auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; if (!table->rowsCount()) return DGRID_OK; if(key_names.empty()) { writeLog(LOG_DEBUG, "Reset all secondary indexes..."); if(!table->resetSecondaryIndex()) return DGRID_FAIL; } else { writeLog(LOG_DEBUG, "Reset %d secondary index(es)...", key_names.size()); for(const auto& name: key_names) { if(!table->resetSecondaryIndex(name, error_string)) return DGRID_FAIL; } } return DGRID_OK; } DGridResult DGriddDataStoreTable::rebuildSecondaryIndex(const std::string& tname, const std::vector& key_names, std::string& error_string) { auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; if (!table->rowsCount()) return DGRID_OK; if(key_names.empty()) { error_string = "Rebuilding all secondary indexes not allowed"; return DGRID_INVALID_ARGUMENT; } else { writeLog(LOG_DEBUG, "Rebuild %d secondary index(es)...", key_names.size()); for(const auto& name: key_names) { if(!table->rebuildSecondaryIndex(name, error_string)) return DGRID_FAIL; } } return DGRID_OK; } DGridResult DGriddDataStoreTable::appendRow(const std::string& tname, const std::vector& values, ModifyStatsData& stats, std::string& error_string) { auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; // RecordID id; if(!table->appendDataRow(values, &stats)) { return DGRID_ALREADY_EXISTS; } stats.count = 1; return DGRID_OK; } DGridResult DGriddDataStoreTable::appendRows(const std::string& tname, const std::vector >& rows, ModifyStatsData& stats, std::string& error_string) { auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; // RecordID id; for(auto values: rows) { if(!table->appendDataRow(values, &stats)) { return DGRID_ALREADY_EXISTS; } stats.count++; } return DGRID_OK; } DGridResult DGriddDataStoreTable::updateRowByID(const std::string& tname, const std::vector& values, const RecordID id_record, std::string& error_string) { auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; if(!table->updateDataRowByID(values, id_record)) { error_string = "No record id=" + std::to_string(id_record) + " in " + tname; return DGRID_NOT_FOUND; } return DGRID_OK; } DGridResult DGriddDataStoreTable::updateRow(const std::string& tname, const std::vector& values, const uint64_t index, std::string& error_string) { auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; if(index >= table->rowsCount()) { error_string = "Table '" + tname + "': row number " + std::to_string(index+1) + " out of range"; return DGRID_OUT_OF_RANGE; } table->updateDataRow(values, index); return DGRID_OK; } DGridResult DGriddDataStoreTable::updateRow(const std::string& tname, const std::vector& values, const std::vector& key_values, std::string& error_string) { auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; if(!table->updateDataRow(values, key_values)) { error_string = "No records found by condition in '" + tname + "'"; return DGRID_NOT_FOUND; } return DGRID_OK; } DGridResult DGriddDataStoreTable::insertRow(const std::string& tname, const std::vector& values, const uint64_t index, ModifyStatsData& stats, std::string& error_string) { auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; if(index > table->rowsCount()) { error_string = "Table '" + tname + "': row number " + std::to_string(index+1) + " out of range"; return DGRID_OUT_OF_RANGE; } // RecordID id; if(!table->insertDataRow(values, index, &stats)) { return DGRID_ALREADY_EXISTS; } stats.count = 1; return DGRID_OK; } DGridResult DGriddDataStoreTable::insertRows(const std::string& tname, const std::vector >& values, const uint64_t index, ModifyStatsData& stats, std::string& error_string) { auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; if(index > table->rowsCount()) { error_string = "Table '" + tname + "': row number " + std::to_string(index+1) + " out of range"; return DGRID_OUT_OF_RANGE; } table->insertDataRows(values, index, &stats); return DGRID_OK; } DGridResult DGriddDataStoreTable::selectRowByID(const std::string& tname, const RecordID id_record, TableResponseRowData& values, std::string& error_string) { auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; if(!table->selectDataRowByID(id_record, values.data, values.index)) { error_string = "No record id=" + std::to_string(id_record) + " in '" + tname + "'"; return DGRID_NOT_FOUND; } values.id_record = id_record; return DGRID_OK; } DGridResult DGriddDataStoreTable::selectRow(const std::string& tname, const uint64_t index, TableResponseRowData& values, std::string& error_string) { auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; if(index >= table->rowsCount()) { error_string = "Table '" + tname + "': row number " + std::to_string(index+1) + " out of range"; return DGRID_OUT_OF_RANGE; } table->selectDataRow(index, values.data, values.id_record); values.index = index; return DGRID_OK; } DGridResult DGriddDataStoreTable::selectRows(const std::string& tname, const uint64_t index_from, const uint64_t count, std::vector& values, std::string& error_string) { auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; if(index_from > table->rowsCount()) { writeLog(LOG_ERR, "table '%s': row number %d out of range", tname.c_str(), index_from+1); return DGRID_OUT_OF_RANGE; } uint64_t index_to = std::min(table->rowsCount(), index_from + count) -1; uint64_t count_real = index_to - index_from + 1; values.resize(count_real); for(uint64_t i = 0; i < count_real; i++ ) { table->selectDataRow(index_from + i, values[i].data, values[i].id_record); values[i].index = index_from + i + 1; } return DGRID_OK; } DGridResult DGriddDataStoreTable::selectRows(const std::string& tname, std::vector& values, std::string& error_string) { auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; table->selectDataRows(values); return DGRID_OK; } DGridResult DGriddDataStoreTable::selectRow(const std::string& tname, const std::vector& keys, const std::vector& search_values, const uint16_t flags, TableResponseRowData& values, std::string& error_string) { auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; values.index = 0; DataIndex* sec_index = table->findSecondaryIndex(keys); if(sec_index && sec_index->isValid()) { // неявно используем вторичный индекс только если он актуален writeLog(LOG_DEBUG, "%s: using %s secondary index '%s' (components match)", __func__, sec_index->type().c_str(), sec_index->name().c_str()); uint64_t row_index = 0; if(sec_index->search(search_values, row_index)) { TableRowData* row = table->getDataRow_raw(row_index); row->getCellsData(values.data); values.id_record = row->id_record; if(!dynamic_cast(table.get())) values.index = row_index; return DGRID_OK; } return DGRID_NOT_FOUND; } if(!table->selectDataRow(keys, search_values, flags, values.data, values.id_record, values.index)) { error_string = "No records found by condition in '" + tname + "'"; return (!(flags & FL_READ_TABLE_BINARY_SEARCH) || dynamic_cast(table.get())) ? DGRID_NOT_FOUND : DGRID_NOT_FOUND_INDEX_SUGGESTED; } return DGRID_OK; } DGridResult DGriddDataStoreTable::selectNextRow(const std::string& tname, const ComplexSearchCondition& condition, const RecordID id_prev, const uint64_t index_from, TableResponseRowData& values, std::string& error_string) { auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; ComplexSearchCondition__int condition_int(condition); if(!fillFieldsIndexes(condition_int, table, error_string)) return DGRID_INVALID_ARGUMENT; if(!table->selectNextDataRow(condition_int, id_prev, index_from > 0 ? index_from-1 : 0, values.data, values.id_record, values.index)) { error_string = "No more records found by condition in '" + tname + "'"; return DGRID_NOT_FOUND; } values.index++; return DGRID_OK; } DGridResult DGriddDataStoreTable::deleteRowByID(const std::string& tname, const RecordID id_record, std::string& error_string) { auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; if (!table->deleteDataRowByID(id_record)) { error_string = "No record id=" + std::to_string(id_record) + " in " + tname; return DGRID_NOT_FOUND; } return DGRID_OK; } DGridResult DGriddDataStoreTable::deleteRow(const std::string& tname, const uint64_t index, std::string& error_string) { auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; if(index >= table->rowsCount()) { error_string = "Table '" + tname + "': row number " + std::to_string(index+1) + " out of range"; return DGRID_OUT_OF_RANGE; } table->deleteDataRow(index); return DGRID_OK; } DGridResult DGriddDataStoreTable::deleteRows(const std::string& tname, std::vector& indexes, uint64_t& rows_affected, std::string& error_string) { rows_affected = 0; auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; std::sort(indexes.begin(), indexes.end(), std::greater()); for(uint64_t index: indexes) { if(index < table->rowsCount() && table->deleteDataRow(index)) rows_affected++; } return DGRID_OK; } DGridResult DGriddDataStoreTable::deleteRows(const std::string& tname, const uint64_t index, const uint64_t& count, uint64_t& rows_affected, std::string& error_string) { rows_affected = 0; auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; if(index > table->rowsCount()-1) return DGRID_NOT_FOUND; table->deleteDataRows(index, count, rows_affected); return DGRID_OK; } DGridResult DGriddDataStoreTable::deleteRows(const std::string& tname, const std::vector& keys, const std::vector& search_values, uint64_t& rows_affected, std::string& error_string) { rows_affected = 0; auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; table->deleteDataRows(keys, search_values, rows_affected); return DGRID_OK; } DGridResult DGriddDataStoreTable::deleteAdjacentDuplicates(const std::string& tname, std::vector& indexes, uint64_t& rows_affected, std::string& error_string) { rows_affected = 0; auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; table->deleteAdjacentDuplicates(indexes, rows_affected); return DGRID_OK; } DGridResult DGriddDataStoreTable::clearTable(const std::string& tname, std::string& error_string) { auto table = getTable(tname, error_string); if (!table) return DGRID_NOT_FOUND; table->clear(); return DGRID_OK; } DGridResult DGriddDataStoreTable::copyTableData(const std::string& tname_src, const std::string& tname_dst, std::string& error_string) { return copyTableData(tname_src, tname_dst, this, error_string); } DGridResult DGriddDataStoreTable::copyTableData(const std::string& tname_src, const std::string& tname_dst, DGriddDataStoreTable* tstore_dst, std::string& error_string) { if(!tstore_dst) tstore_dst = this; if (this == tstore_dst && tname_src == tname_dst) { error_string = "Can't copy table '" + tname_src + "' to itself"; return DGRID_INVALID_ARGUMENT; } TableDataPtr src = getTable(tname_src, error_string); TableDataPtr dst = tstore_dst->getTable(tname_dst, error_string); if(!src || !dst) { return DGRID_NOT_FOUND; } // COPY DATA ... src->copyTo(dst); return DGRID_OK; } DGridResult DGriddDataStoreTable::cursorCreate(const std::string& tname, const ComplexSearchCondition& condition, TableCursorID& cursor_id, std::string& error_string) { auto table = getTable(tname, error_string); if(!table) return DGRID_NOT_FOUND; ComplexSearchCondition__int condition_int(condition); if(!fillFieldsIndexes(condition_int, table, error_string)) return DGRID_INVALID_ARGUMENT; if(!table->cursorCreate(std::move(condition_int), cursor_id.id, error_string)) return DGRID_FAIL; return DGRID_OK; } DGridResult DGriddDataStoreTable::cursorCreate(const std::string& tname, const ComplexSearchCondition& condition, const uint64_t index_from, const uint64_t index_to, TableCursorID& cursor_id, std::string& error_string) { auto table = getTable(tname, error_string); if(!table) return DGRID_NOT_FOUND; ComplexSearchCondition__int condition_int(condition); if(!fillFieldsIndexes(condition_int, table, error_string)) return DGRID_INVALID_ARGUMENT; if(!table->cursorCreate(std::move(condition_int), index_from, index_to, cursor_id.id, error_string)) return DGRID_FAIL; return DGRID_OK; } DGridResult DGriddDataStoreTable::cursorDelete(const std::string& tname, const TableCursorID& cursor_id, std::string& error_string) { auto table = getTable(tname, error_string); if(!table) return DGRID_NOT_FOUND; if(!table->cursorDelete(cursor_id.id, error_string)) return DGRID_FAIL; return DGRID_OK; } DGridResult DGriddDataStoreTable::cursorSelectNextRow(const std::string& tname, const TableCursorID& cursor_id, TableResponseRowData& values, std::string& error_string) { auto table = getTable(tname, error_string); if(!table) return DGRID_NOT_FOUND; if(!table->selectNextDataRow_cursor(cursor_id.id, values, error_string)) { if(error_string.empty()) error_string = "No more records found by condition in '" + tname + "'"; return DGRID_NOT_FOUND; } values.index++; return DGRID_OK; } bool DGriddDataStoreTable::fillFieldsIndexes(ComplexSearchCondition__int& condition, TableDataPtr& table, std::string& error_string) { if(!table) return false; for(FieldCondition__int& it: condition.field_conditions) { it.field_idx = table->columnIndex(it.field); if(it.field_idx < 0) { error_string = "fillFieldsIndexes: no field '" + it.field + "' found"; return false; } } return true; } bool DGriddDataStoreTable::createSortKey(TableDataPtr& table, const SortOrderList& order, SortKey& key, std::string& error_string) { SortKeyItem it; for(const auto& o: order) { it.field_idx = table->columnIndex(o.field); if(it.field_idx < 0) { error_string = "fillFieldsIndexes: no field '" + o.field + "' found"; return false; } it.flags = o.flags; it.data_type = table->getFieldType(it.field_idx); key.push_back(it); } return true; } } // namespace DGrid