#include "dgrid_logger.h"
|
#include "dgridd_datastore_table.h"
|
|
namespace DGrid {
|
|
TableData_Std::TableData_Std(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)
|
:TableData(key_fields, fields, fields_len, fields_type), last_cursor_id(1)
|
{
|
|
}
|
|
TableData_Std::~TableData_Std() {}
|
TableType TableData_Std::type() { return TABLE_STANDARD; }
|
|
std::shared_ptr<TableRowData> TableData_Std::getDataRow(const uint64_t index)
|
{
|
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];
|
}
|
|
uint64_t TableData_Std::rowsCount() { return rows_data.size(); }
|
|
bool TableData_Std::getIndexByID(const RecordID id, uint64_t& index)
|
{
|
#warning !!! TODO: needs optimization !!!
|
uint64_t count = rowsCount();
|
for(uint64_t i=0; i<count; i++) {
|
if(id == rows_data[i]->id_record) {
|
index = i;
|
return true;
|
}
|
}
|
return false;
|
}
|
|
bool TableData_Std::sort(const SortKey& key, const bool stable)
|
{
|
if(stable)
|
std::stable_sort(rows_data.begin(), rows_data.end(), [&key](const RowPointer& a, const RowPointer& b) {
|
#ifdef TABLE_DEBUG
|
if( a->compare(*b, key) && b->compare(*a, key) ) {
|
writeLog(LOG_ERR, "COMPARATOR ERROR!!!");
|
return false;
|
}
|
#endif
|
return a->compare(*b, key);
|
});
|
else
|
std::sort(rows_data.begin(), rows_data.end(), [&key](const RowPointer& a, const RowPointer& b) {
|
#ifdef TABLE_DEBUG
|
if( a->compare(*b, key) && b->compare(*a, key) ) {
|
writeLog(LOG_ERR, "COMPARATOR ERROR!!!");
|
return false;
|
}
|
#endif
|
return a->compare(*b, key);
|
} );
|
return true;
|
}
|
|
bool TableData_Std::copyFrom(TableData *other)
|
{
|
uint64_t count = other->rowsCount();
|
uint64_t index;
|
clear();
|
rows_data.resize(count);
|
std::vector<std::string> values;
|
for(uint64_t i = 0; i<count; i++) {
|
other->selectDataRow(i, values, index);
|
RowPointer row = std::make_shared<TableRowData>(this, values, other);
|
rows_data[i] = row;
|
registerRow(row, 0, 0);
|
}
|
return true;
|
}
|
|
bool TableData_Std::copyTo(std::shared_ptr<TableData>& other)
|
{
|
if(TableData_Std *other_std = dynamic_cast<TableData_Std*>(other.get()))
|
return other_std->copyFrom(this);
|
if(TableData_Sorted *other_sorted = dynamic_cast<TableData_Sorted*>(other.get()))
|
return other_sorted->copyFrom(this);
|
|
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_Std::appendDataRow(const std::vector<std::string>& values, ModifyStatsData* stats)
|
{
|
std::shared_ptr<TableRowData> row = std::make_shared<TableRowData>(this, values);
|
rows_data.push_back(row);
|
registerRow(row,0,rows_data.size()-1);
|
if(stats) {
|
stats->last_index = rows_data.size() - 1;
|
#ifdef MODIFY_STATS_SHORT_RESULT
|
if(stats->list.empty())
|
#endif
|
stats->list.push_back(row->id_record);
|
}
|
// writeLog(LOG_DEBUG, "append: new table size: %d", rows.size());
|
return true;
|
}
|
|
bool TableData_Std::updateDataRow(const std::vector<std::string>& values, const uint64_t index)
|
{
|
if(index >= rows_data.size())
|
return false;
|
|
for(uint64_t i=0; i<values.size() && i<columns_count; i++) {
|
rows_data[index]->setCellData(i, values[i]);
|
}
|
return true;
|
}
|
|
bool TableData_Std::updateDataRow(const std::vector<std::string>& values, const std::vector<std::string>& key_values)
|
{
|
uint64_t index = 0;
|
SearchCondition condition;
|
conditionFromV(key_values, condition);
|
if(sequentialScan(condition, 0 /* id_prev */, index)) {
|
return updateDataRow(values, index);
|
}
|
return false;
|
}
|
|
bool TableData_Std::updateDataRowByID(const std::vector<std::string>& values, const RecordID id_record)
|
{
|
RowPointer row = getDataRowByID(id_record);
|
if(nullptr == row) {
|
writeLog(LOG_ERR, "%s: ID %llu not found", __func__, id_record);
|
return false;
|
}
|
row->setCellsData(values);
|
return true;
|
}
|
|
bool TableData_Std::insertDataRow(const std::vector<std::string>& values, const uint64_t index, ModifyStatsData *stats)
|
{
|
std::vector<std::vector<std::string> > v = { values };
|
return insertDataRows(v, index, stats);
|
}
|
|
bool TableData_Std::insertDataRows(const std::vector<std::vector<std::string> >& values, const uint64_t index, ModifyStatsData *stats)
|
{
|
if(index > rows_data.size())
|
return false;
|
|
uint64_t count = values.size();
|
uint64_t last_index = 0;
|
rows_data.resize(rows_data.size()+count);
|
uint64_t move_start = index + count;
|
for(size_t i=rows_data.size()-1; i >= move_start; i--) {
|
// writeLog(LOG_DEBUG, "move %d -> %d", i-count, i);
|
rows_data[i] = rows_data[i-count];
|
}
|
for(size_t i=0; i<count; i++) {
|
last_index = index + i;
|
std::shared_ptr<TableRowData> row = std::make_shared<TableRowData>(this, values[i]);
|
rows_data[last_index] = row;
|
registerRow(row, 0, last_index);
|
if(stats) {
|
#ifdef MODIFY_STATS_SHORT_RESULT
|
if(!i)
|
#endif
|
stats->list.push_back(row->id_record);
|
}
|
}
|
// writeLog(LOG_DEBUG, "insert: new table size: %d", rows.size());
|
if(stats) {
|
stats->count = count;
|
stats->last_index = last_index;
|
}
|
return true;
|
}
|
|
bool TableData_Std::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_Std::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_Std::sequentialScan(const std::vector<std::string>& keys, const std::vector<std::string>& search_values,
|
const RecordID id_prev, uint64_t& index, uint64_t index_upper)
|
{
|
#ifdef TABLE_DEBUG
|
writeLog(LOG_DEBUG, "sequentialScan: ");
|
std::string sl;
|
for(auto s: keys) { sl += " " + s; }
|
writeLog(LOG_DEBUG, " keys: %s", sl.c_str());
|
sl.clear();
|
for(auto s: search_values) { sl += " " + s; }
|
writeLog(LOG_DEBUG, " values: %s", sl.c_str());
|
#endif
|
SearchCondition condition;
|
conditionFromKV(keys, search_values, condition);
|
return sequentialScan(condition, id_prev, index, index_upper);
|
}
|
|
bool TableData_Std::sequentialScan(const SearchCondition& condition,
|
const RecordID id_prev, uint64_t& index, uint64_t index_upper)
|
{
|
if(index_upper>rows_data.size())
|
index_upper = rows_data.size();
|
|
writeLog(LOG_DEBUG, "comparing from row %llu...", index);
|
for(; index<index_upper; index++) {
|
if(id_prev == rows_data[index]->id_record)
|
continue;
|
if(rows_data[index]->checkCondition(condition))
|
return true;
|
}
|
return false;
|
}
|
|
bool TableData_Std::sequentialScan(const ComplexSearchCondition__int& condition,
|
const RecordID id_prev, uint64_t& index, uint64_t index_upper)
|
{
|
writeLog(LOG_DEBUG, "comparing from row %llu...", index);
|
if(index_upper>rows_data.size())
|
index_upper = rows_data.size();
|
|
for(; index<index_upper; index++) {
|
if(id_prev == rows_data[index]->id_record)
|
continue;
|
if(rows_data[index]->checkCondition(condition))
|
return true;
|
}
|
return false;
|
}
|
|
bool TableData_Std::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();
|
|
if(flags & FL_READ_TABLE_BINARY_SEARCH) {
|
writeLog(LOG_WARNING, "Using binary search in standard table: %d key field(s)", keys.size());
|
TableRowFreeKey key;
|
if(freeKeyFromValues(keys, search_values, key)) {
|
if(bsearch_RowIndex(key, 0 /* id_prev */, index)) {
|
return selectDataRow(index, values, id_record);
|
}
|
writeLog(LOG_DEBUG, "%s: no row found by binary search (expected index = %llu)", __func__, index);
|
// values.clear();
|
// values.resize(columns_count, "");
|
}
|
} else {
|
if(sequentialScan(keys, search_values, 0 /* id_prev */, index, index_upper)) {
|
return selectDataRow(index, values, id_record);
|
}
|
}
|
return false;
|
}
|
|
bool TableData_Std::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;
|
writeLog(LOG_DEBUG, " look for next row from %llu, prev id %llu", index, id_prev);
|
if(sequentialScan(condition, id_prev, index)) {
|
return selectDataRow(index, values, id_record);
|
}
|
return false;
|
}
|
|
bool TableData_Std::selectDataRows(std::vector<TableResponseRowData>& values)
|
{
|
uint64_t count = rowsCount();
|
values.resize(count);
|
for(uint64_t i = 0; i<count; i++) {
|
selectDataRow(i, values[i].data, values[i].id_record);
|
values[i].index = i + 1;
|
}
|
return true;
|
}
|
|
bool TableData_Std::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_Std::deleteDataRow(const uint64_t index)
|
{
|
uint64_t real_count;
|
return deleteDataRows(index, 1, real_count);
|
}
|
|
bool TableData_Std::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++) {
|
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_Std::deleteDataRows(const std::vector<std::string>& keys, const std::vector<std::string>& values, uint64_t& real_count)
|
{
|
real_count = 0;
|
SearchCondition condition;
|
conditionFromKV(keys, values, condition);
|
|
for(auto it = rows_data.begin(); it != rows_data.end(); ) {
|
if((*it)->checkCondition(condition)) {
|
deregisterRow((*it)->id_record, it - rows_data.begin());
|
real_count++;
|
it = rows_data.erase(it);
|
} else {
|
++it;
|
}
|
}
|
return true;
|
}
|
|
bool TableData_Std::deleteAdjacentDuplicates(std::vector<uint16_t>& indexes, uint64_t& rows_deleted)
|
{
|
rows_deleted = 0;
|
uint64_t i=0;
|
while(i+1 < rows_data.size()) {
|
if(rows_data[i]->equal(*rows_data[i+1], indexes)) {
|
deleteDataRow(i+1);
|
rows_deleted++;
|
} else {
|
i++;
|
}
|
}
|
return true;
|
}
|
|
void TableData_Std::clearData() {
|
rows_data.clear();
|
}
|
|
// cursors
|
TableData_Std::TableCursor::TableCursor()
|
:index(0),
|
index_from(0), index_to(npos)
|
{}
|
|
TableData_Std::TableCursor::TableCursor(const TableCursor&& o)
|
:index(o.index),
|
index_from(o.index_from), index_to(o.index_to),
|
condition(std::move(o.condition))
|
{}
|
|
TableData_Std::TableCursor::TableCursor(const ComplexSearchCondition__int&& cond)
|
:index(0),
|
index_from(0), index_to(npos),
|
condition(std::move(cond))
|
{}
|
TableData_Std::TableCursor::TableCursor(const ComplexSearchCondition__int&& cond, const uint64_t _from, const uint64_t _to)
|
:index(_from),
|
index_from(_from), index_to(_to ? _to : npos),
|
condition(std::move(cond))
|
{
|
writeLog(LOG_DEBUG, "range cursor: %llu - %llu", index_from, index_to);
|
}
|
|
TableData_Std::TableCursor::~TableCursor() {}
|
|
CursorID TableData_Std::nextCursorID()
|
{
|
while(cursors.find(last_cursor_id) != cursors.end())
|
last_cursor_id++;
|
return last_cursor_id++;
|
}
|
|
bool TableData_Std::cursorCreate(const ComplexSearchCondition__int&& cond, uint64_t& cursor_id, std::string& error_string)
|
{
|
cursor_id = nextCursorID();
|
cursors.emplace(cursor_id, TableCursor(std::move(cond)));
|
return true;
|
}
|
|
bool TableData_Std::cursorCreate(const ComplexSearchCondition__int&& cond, const uint64_t index_from, const uint64_t index_to, uint64_t& cursor_id, std::string& error_string)
|
{
|
cursor_id = nextCursorID();
|
cursors.emplace(cursor_id, TableCursor(std::move(cond), index_from, index_to));
|
return true;
|
}
|
|
bool TableData_Std::cursorDelete(const uint64_t cursor_id, std::string& error_string)
|
{
|
auto it = cursors.find(cursor_id);
|
if(it == cursors.end())
|
return false;
|
|
cursors.erase(it);
|
return true;
|
}
|
|
void TableData_Std::updateCursors(const uint64_t index, bool added)
|
{
|
for(auto& cursor: cursors) {
|
if(added) {
|
if(cursor.second.index > index)
|
cursor.second.index++;
|
} else {
|
if(cursor.second.index > index)
|
cursor.second.index--;
|
}
|
}
|
}
|
|
void TableData_Std::resetCursors()
|
{
|
for(auto& cursor: cursors)
|
cursor.second.index = cursor.second.index_from;
|
}
|
|
bool TableData_Std::selectNextDataRow_cursor(const uint64_t cursor_id, TableResponseRowData& values, std::string& error_string)
|
{
|
auto it = cursors.find(cursor_id);
|
if(it == cursors.end()) {
|
error_string = "Invalid cursor ID";
|
return false;
|
}
|
if(it->second.index > it->second.index_to) {
|
writeLog(LOG_DEBUG, "range cursor [%llu - %llu] limit [%llu]", it->second.index_from, it->second.index_to, it->second.index);
|
return false;
|
}
|
bool found = selectNextDataRow(it->second.condition, 0, it->second.index, values.data, values.id_record, values.index);
|
it->second.index = values.index+1;
|
return found;
|
}
|
|
} // namespace DGrid
|