#include "dgrid_config.h" #include "dgrid_logger.h" #include "dgridd_datastore_table.h" #include "dgridd_datastore_table_index.h" namespace DGrid { // table row key TableRowKeyItem::TableRowKeyItem(const TableRowKeyItem& o) :val(o.val), data_type(o.data_type), sort_order(o.sort_order) { } TableRowKeyItem::TableRowKeyItem(const std::string& val, const FieldType data_type, const SortOrder sort_order) :val(val), data_type(data_type), sort_order(sort_order) { } TableRowKeyItem::~TableRowKeyItem() {} bool TableRowKeyItem::equal(const FieldType data_type, const std::string& a, const std::string& b) { bool ret = false; #warning TODO: упрощённая реализация, на P и N будет потеря точности и потенциальная ошибка сравнения try { switch(data_type) { case FIELD_TYPE_INTEGER: ret = std::stoll(a) == std::stoll(b); break; case FIELD_TYPE_FLOAT: ret = std::stold(a) == std::stold(b); break; case FIELD_TYPE_NUMERIC: case FIELD_TYPE_PACKED: ret = std::stold(a) == std::stold(b); break; default: ret = a == b; break; } } catch (...) { writeLog(LOG_ERR, "Typed compare(equal) failed (%s) '%s' == '%s'", fieldType2string(data_type).c_str(), a.c_str(), b.c_str()); ret = a == b; } #ifdef TABLE_DEBUG writeLog(LOG_DEBUG, "equal (%s) '%s' == '%s' : %s", fieldType2string(data_type).c_str(), a.c_str(), b.c_str(), ret ? "true" : "false"); #endif return ret; } bool TableRowKeyItem::compare(const FieldType data_type, const std::string& a, const std::string& b) { bool ret = false; #warning TODO: упрощённая реализация, на P и N будет потеря точности и потенциальная ошибка сравнения try { switch(data_type) { case FIELD_TYPE_INTEGER: ret = std::stoll(a) > std::stoll(b); break; case FIELD_TYPE_FLOAT: ret = std::stold(a) > std::stold(b); break; case FIELD_TYPE_NUMERIC: case FIELD_TYPE_PACKED: ret = std::stold(a) > std::stold(b); break; default: ret = a > b; break; } } catch (...) { writeLog(LOG_ERR, "Typed compare failed (%s) '%s' > '%s'", fieldType2string(data_type).c_str(), a.c_str(), b.c_str()); ret = a > b; } #ifdef TABLE_DEBUG writeLog(LOG_DEBUG, "compare (%s) '%s' > '%s' : %s", fieldType2string(data_type).c_str(), a.c_str(), b.c_str(), ret ? "true" : "false"); #endif return ret; } DataIndex::DataIndex(TableData* table, const TableKeyInfo& key_info) : table(table), key_info(key_info), valid(false) {} DataIndex::~DataIndex() {} bool DataIndex::isValid() { return valid; } const std::string& DataIndex::name() const { return key_info.name; } bool DataIndex::checkComponents(const std::vector& components) const { return components == key_info.components; } bool DataIndex::checkOrRebuild() { return valid || rebuild(); } const std::string DataIndex_sorted::_type = "SORTED"; DataIndex_sorted::DataIndex_sorted(TableData* table, const TableKeyInfo& key_info) : DataIndex(table, key_info) {} DataIndex_sorted::~DataIndex_sorted() {} bool DataIndex_sorted::rebuild() { #warning TODO: unique sorted index support writeLog(LOG_DEBUG, "-- sorted index rebuild (%s)", key_info.name.c_str()); if(!table) { writeLog(LOG_ERR, "null table pointer!"); return false; } uint64_t data_cnt = table->rows_data.size(); rows_index.resize(data_cnt); for(uint64_t i=0; irows_data[i].get(); const TableRowKey& key = row->getKey(key_info.components); rows_index[i] = { key, i }; } std::sort(rows_index.begin(), rows_index.end(), [](const sortedKeyItem& a, const sortedKeyItem& b) { return a.first < b.first; }); valid = true; return valid; } void DataIndex_sorted::reset() { writeLog(LOG_DEBUG, "-- sorted index reset (%s)", key_info.name.c_str()); valid = false; rows_index.clear(); } void DataIndex_sorted::removeRow(TableRowData* row) { if(!valid) return; } bool DataIndex_sorted::search(const std::vector& values, uint64_t& index) { TableRowKey key; if(!checkOrRebuild()) return false; return table->keyFromValues(key_info.components, values, key, false /* full key only */, true /* force componentes list */) && bsearch(key, index); } bool DataIndex_sorted::bsearch(const TableRowKey& key, uint64_t& index) { uint64_t l(0); uint64_t count = rows_index.size() - l; uint64_t step; while(count > 0) { step = count / 2; uint64_t mid = l + step; #ifdef TABLE_DEBUG writeLog(LOG_DEBUG, "r comparing %lld", mid); #endif if(key > rows_index[mid].first) { l = mid + 1; count -= step + 1; } else count = step; } index = l; return (lrows_data.size(); for(uint64_t i=0; irows_data[i].get(); if(row == nullptr) continue; const TableRowKey& key = row->getKey(key_info.components); if(rows_index.find(key) != rows_index.end()) { writeLog(LOG_WARNING, "%s: duplicated key found", __func__); return false; } rows_index.emplace(key, i); } valid = true; return valid; } void DataIndex_hashed::reset() { writeLog(LOG_DEBUG, "-- hashed index reset (%s)", key_info.name.c_str()); valid = false; rows_index.clear(); } void DataIndex_hashed::removeRow(TableRowData* row) { if(!valid) return; const TableRowKey& key = row->getKey(key_info.components); auto it = rows_index.find(key); if(it != rows_index.end()) rows_index.erase(it); } bool DataIndex_hashed::search(const std::vector& values, uint64_t& index) { TableRowKey key; HashedRows::iterator it = rows_index.end(); if(!checkOrRebuild()) return false; if(table->keyFromValues(key_info.components, values, key, false /* full key only */, true /* force componentes list */)) { it = rows_index.find(key); } if(it != rows_index.end()) { index = it->second; return true; } return false; } } // namespace DGrid