#include <algorithm>
|
#include <experimental/random>
|
|
#include "dgrid_logger.h"
|
#include "dgridd_datastore_table.h"
|
|
namespace DGrid {
|
|
TableData_Sorted::TableData_Sorted(const std::vector<std::string>& key_fields, const std::vector<std::string>& fields, const std::vector<uint64_t>& fields_len, const std::vector<FieldType>& fields_type, bool uniq_key)
|
:TableData_Std(key_fields.empty() ? fields : key_fields, fields, fields_len, fields_type), uniq_key(uniq_key)
|
{
|
}
|
|
TableData_Sorted::~TableData_Sorted() {}
|
TableType TableData_Sorted::type() { return uniq_key ? TABLE_SORTED_UNIQ : TABLE_SORTED; }
|
|
bool TableData_Sorted::copyTo(std::shared_ptr<TableData>& other)
|
{
|
other->clear();
|
uint64_t count = rows_data.size();
|
for(uint64_t i = 0; i<count; i++) {
|
other->appendDataRow(rows_data[i]);
|
}
|
return true;
|
}
|
|
bool TableData_Sorted::insertDataRow_internal(std::shared_ptr<TableRowData>& row, const uint64_t index, const RecordID old_id)
|
{
|
if(index > rows_data.size()) {
|
writeLog(LOG_ERR, "index > rows size (%d > %d)", index, rows_data.size());
|
return false;
|
}
|
// writeLog(LOG_DEBUG, "insert row at %d, tot %d", index, rows_data.size());
|
rows_data.resize(rows_data.size()+1);
|
|
for(size_t i=rows_data.size()-1; i>index; i--) {
|
rows_data[i] = rows_data[i-1];
|
}
|
rows_data[index] = row;
|
registerRow(row, old_id, index);
|
return true;
|
}
|
|
bool TableData_Sorted::appendDataRow(const std::vector<std::string>& values, ModifyStatsData *stats)
|
{
|
RowPointer row = std::make_shared<TableRowData>(this, values);
|
uint64_t index = bsearch_NextIndex(row->getKey());
|
if(uniq_key && index>0 && row->getKey() == rows_data[index-1]->getKey()) {
|
writeLog(LOG_INFO, "%s: key already exists", __func__);
|
return false;
|
}
|
insertDataRow_internal(row, index);
|
// writeLog(LOG_DEBUG, "row inseted at %llu, new size: %llu", index, rows.size());
|
if(stats) {
|
stats->last_index = index;
|
#ifdef MODIFY_STATS_SHORT_RESULT
|
if(stats->list.empty())
|
#endif
|
stats->list.push_back(row->id_record);
|
}
|
return true;
|
}
|
|
bool TableData_Sorted::updateDataRowByID(const std::vector<std::string>& values, const RecordID id_record)
|
{
|
#ifdef TABLE_DEBUG
|
writeLog(LOG_DEBUG, "%s %d id %llu", __FILE__, __LINE__, id_record);
|
#endif
|
RowPointer old_row = getDataRowByID(id_record);
|
if(nullptr == old_row) {
|
writeLog(LOG_ERR, "%s: ID %llu not found", __func__, id_record);
|
return false;
|
}
|
RowPointer new_row = std::make_shared<TableRowData>(this, values);
|
bool key_changed = old_row->getKey() != new_row->getKey();
|
|
if(uniq_key && key_changed) {
|
writeLog(LOG_ERR, "%s: sorted table UNIQUE key can't be changed via update operation", __func__);
|
return false;
|
}
|
if(!key_changed) {
|
writeLog(LOG_DEBUG, "key not changed, just move data > id %llu", id_record);
|
old_row->moveCellsFrom(*new_row);
|
} else {
|
writeLog(LOG_DEBUG, "key changed, look for index for id %llu", id_record);
|
uint64_t old_index = 0;
|
if (!bsearch_RowIndex(old_row->getKey(), 0, old_index)) {
|
writeLog(LOG_WARNING, "no start index found for old record");
|
return false;
|
}
|
uint64_t old_index_high = bsearch_NextIndex(old_row->getKey(), old_index);
|
#ifdef TABLE_DEBUG
|
writeLog(LOG_DEBUG, "old index interval: %llu - %llu", old_index, old_index_high - 1);
|
#endif
|
while(id_record != rows_data[old_index]->id_record && old_index < old_index_high) {
|
old_index++;
|
}
|
if(old_index == old_index_high) {
|
writeLog(LOG_WARNING, "no actual index found for old record");
|
return false;
|
}
|
uint64_t new_index = bsearch_NextIndex(new_row->getKey());
|
if (old_index < new_index)
|
new_index--;
|
writeLog(LOG_DEBUG, "%s %d update %llu at index %llu -> %llu", __FILE__, __LINE__, id_record, old_index, new_index);
|
deleteDataRow(old_index);
|
insertDataRow_internal(new_row, new_index, id_record);
|
}
|
return true;
|
}
|
|
bool TableData_Sorted::updateDataRow(const std::vector<std::string>& values, const uint64_t index)
|
{
|
#ifdef TABLE_DEBUG
|
writeLog(LOG_DEBUG, "%s %d index %llu", __FILE__, __LINE__, index);
|
#endif
|
if(index >= rows_data.size()) {
|
writeLog(LOG_ERR, "%s: Index %d out of range", __func__, index);
|
return false;
|
}
|
RowPointer new_row = std::make_shared<TableRowData>(this, values);
|
bool key_changed = rows_data[index]->getKey() != new_row->getKey();
|
|
if(uniq_key && key_changed) {
|
writeLog(LOG_INFO, "%s: sorted table UNIQUE key can't be changed via update operation", __func__);
|
return false;
|
}
|
if(!key_changed) {
|
writeLog(LOG_DEBUG, "key not changed, just move data [%llu]", index);
|
rows_data[index]->moveCellsFrom(*new_row);
|
} else {
|
writeLog(LOG_DEBUG, "key changed, look for new index");
|
uint64_t new_index = bsearch_NextIndex(new_row->getKey());
|
RecordID id = rows_data[index]->id_record;
|
if (index < new_index)
|
new_index--;
|
writeLog(LOG_DEBUG, "%s %d update at index %llu -> %llu (id = %llu)", __FILE__, __LINE__, index, new_index, id);
|
deleteDataRow(index);
|
insertDataRow_internal(new_row, new_index, id);
|
}
|
return true;
|
}
|
|
bool TableData_Sorted::updateDataRow(const std::vector<std::string>& values, const std::vector<std::string>& old_key_data)
|
{
|
#ifdef TABLE_DEBUG
|
writeLog(LOG_DEBUG, "%s %d key", __FILE__, __LINE__);
|
#endif
|
std::shared_ptr<TableRowData> new_row = std::make_shared<TableRowData>(this, values);
|
TableRowKey new_key = new_row->getKey();
|
TableRowKey old_key = keyFromValues(old_key_data, true);
|
if(old_key.empty()) {
|
writeLog(LOG_ERR, "%s: empty key created!", __func__);
|
return false;
|
}
|
|
bool key_changed = old_key != new_row->getKey();
|
|
if(uniq_key && key_changed) {
|
writeLog(LOG_INFO, "%s: sorted table UNIQUE key can't be changed via update operation", __func__);
|
return false;
|
}
|
uint64_t old_index = 0;
|
if(!bsearch_RowIndex(old_key, 0 /* id_prev */, old_index)) {
|
writeLog(LOG_DEBUG, "%s: no row found by key", __func__);
|
return false;
|
}
|
if(!key_changed) {
|
rows_data[old_index]->moveCellsFrom(*new_row);
|
} else {
|
uint64_t new_index = bsearch_NextIndex(new_row->getKey());
|
RecordID id = rows_data[old_index]->id_record;
|
if (old_index < new_index)
|
new_index--;
|
writeLog(LOG_DEBUG, "%s %d update at index %llu -> %llu", __FILE__, __LINE__, old_index, new_index);
|
deleteDataRow(old_index);
|
insertDataRow_internal(new_row, new_index, id);
|
}
|
return true;
|
}
|
|
bool TableData_Sorted::insertDataRow(const std::vector<std::string>& values, const uint64_t index, ModifyStatsData *stats)
|
{
|
writeLog(LOG_WARNING, "%s: Indexed insert to sorted tables not allowed, ignoring index", __func__);
|
return appendDataRow(values, stats);
|
}
|
|
bool TableData_Sorted::insertDataRows(const std::vector<std::vector<std::string> >& values, const uint64_t index, ModifyStatsData *stats)
|
{
|
writeLog(LOG_WARNING, "%s: Indexed insert to sorted tables not allowed, ignoring index", __func__);
|
for(auto row: values) {
|
if (!appendDataRow(row, stats))
|
return false;
|
}
|
return true;
|
}
|
|
bool TableData_Sorted::selectDataRowByID(const RecordID id_record, std::vector<std::string>& values, uint64_t& index)
|
{
|
RowPointer row = getDataRowByID(id_record);
|
if(nullptr == row)
|
return false;
|
|
row->getCellsData(values);
|
return true;
|
}
|
|
bool TableData_Sorted::selectDataRow(const uint64_t index, std::vector<std::string>& values, RecordID& id_record)
|
{
|
if(index >= rows_data.size())
|
return false;
|
rows_data[index]->getCellsData(values);
|
id_record = rows_data[index]->id_record;
|
return true;
|
}
|
|
bool TableData_Sorted::selectDataRow(const std::vector<std::string>& keys, const std::vector<std::string>& search_values, uint16_t flags,
|
std::vector<std::string>& values, RecordID& id_record, uint64_t& index)
|
{
|
uint64_t index_upper = rows_data.size();
|
TableRowKey key;
|
if(keyFromValues(keys, search_values, key, true)) {
|
if(!bsearch_RowIndex(key, 0 /* id_prev */, index)) {
|
writeLog(LOG_DEBUG, "%s: no row found by key (expected index = %llu)", __func__, index);
|
// values.clear();
|
// values.resize(columns_count, "");
|
return false;
|
}
|
index_upper = bsearch_NextIndex(key, index);
|
} else {
|
writeLog(LOG_DEBUG, "%s: key fields missmatch, performing sequential scan...", __func__);
|
}
|
// compare all fields after key match
|
if(!sequentialScan(keys, search_values, 0 /* id_prev */, index, index_upper)) {
|
writeLog(LOG_DEBUG, "%s: no row found", __func__);
|
return false;
|
}
|
rows_data[index]->getCellsData(values);
|
id_record = rows_data[index]->id_record;
|
return true;
|
}
|
|
bool TableData_Sorted::selectNextDataRow(const ComplexSearchCondition__int& condition,
|
const RecordID id_prev, const uint64_t index_from,
|
std::vector<std::string>& values, RecordID& id_record, uint64_t& index)
|
{
|
index=index_from;
|
uint64_t index_upper = rows_data.size();
|
TableRowKey key;
|
writeLog(LOG_DEBUG, " look for next row from %llu, prev id %llu", index, id_prev);
|
if(!condition.field_conditions.empty() && keyFromCondition(condition, key, true)) {
|
if(!bsearch_RowIndex(key, id_prev, index)) {
|
writeLog(LOG_DEBUG, "%s: no more rows found by key", __func__);
|
return false;
|
}
|
index_upper = bsearch_NextIndex(key, index);
|
} else {
|
writeLog(LOG_DEBUG, "%s: key fields missmatch, performing sequential scan...", __func__);
|
}
|
// compare all fields after key match
|
if(!sequentialScan(condition, id_prev, index, index_upper)) {
|
writeLog(LOG_DEBUG, "%s: no more rows found", __func__);
|
return false;
|
}
|
rows_data[index]->getCellsData(values);
|
id_record = rows_data[index]->id_record;
|
return true;
|
}
|
|
bool TableData_Sorted::selectDataRows(std::vector<TableResponseRowData>& values)
|
{
|
return TableData_Std::selectDataRows(values);
|
}
|
|
bool TableData_Sorted::deleteDataRowByID(const RecordID id_record)
|
{
|
uint64_t index;
|
if(getIndexByID(id_record, index) && deleteDataRow(index)) {
|
deregisterRow(id_record, index);
|
return true;
|
}
|
return false;
|
}
|
|
bool TableData_Sorted::deleteDataRow(const uint64_t index)
|
{
|
uint64_t real_count;
|
return deleteDataRows(index, 1, real_count);
|
}
|
|
bool TableData_Sorted::deleteDataRows(const uint64_t index, const uint64_t& count, uint64_t& real_count)
|
{
|
real_count = 0;
|
if(index >= rows_data.size())
|
return false;
|
|
real_count = std::min(count, rows_data.size() - index);
|
for(size_t i=0; i<real_count; i++) {
|
// writeLog(LOG_DEBUG, "[%d] id %llu", index+i, rows[index+i]->id_record);
|
deregisterRow(rows_data[index+i]->id_record, index);
|
}
|
for(size_t i=index; i<rows_data.size()-real_count; i++) {
|
rows_data[i] = rows_data[i+real_count];
|
}
|
rows_data.resize(rows_data.size()-real_count);
|
return true;
|
}
|
|
bool TableData_Sorted::deleteDataRows(const std::vector<std::string>& keys, const std::vector<std::string>& search_values, uint64_t& real_count)
|
{
|
uint64_t index=0;
|
TableRowKey key;
|
if(keyFromValues(keys, search_values, key)) {
|
if(!bsearch_RowIndex(key, 0 /* id_prev */, index)) {
|
writeLog(LOG_DEBUG, "%s: no row found by key", __func__);
|
return false;
|
}
|
} else {
|
writeLog(LOG_DEBUG, "%s: key fields missmatch, performing sequential scan...", __func__);
|
// compare all fields after key match
|
uint64_t index_upper = rows_data.size();
|
if(!sequentialScan(keys, search_values, 0 /* id_prev */, index, index_upper)) {
|
writeLog(LOG_DEBUG, "%s: no row found", __func__);
|
return false;
|
}
|
}
|
return deleteDataRows(index, 1, real_count);
|
}
|
|
bool TableData_Sorted::deleteAdjacentDuplicates(std::vector<uint16_t>& indexes, uint64_t& rows_deleted)
|
{
|
return TableData_Std::deleteAdjacentDuplicates(indexes, rows_deleted);
|
}
|
|
void TableData_Sorted::clearData()
|
{
|
rows_data.clear();
|
}
|
|
uint64_t TableData_Sorted::rowsCount()
|
{
|
return rows_data.size();
|
}
|
|
} // namespace DGrid
|