#include "dgrid_logger.h"
|
#include "dgridd_datastore_table.h"
|
|
namespace DGrid {
|
|
#define HASHED_TABLE_SEED 0x136C936C
|
#define VACUUM_FACTOR 2
|
|
uint64_t TableRowKeyHasher::operator()(const TableRowKey& k) const {
|
uint64_t hash(HASHED_TABLE_SEED);
|
for(auto ki: k) {
|
hash = ((hash << 1) | (hash >> 63)) ^ ki.val.size();
|
uint8_t *c = (uint8_t*)ki.val.c_str();
|
while(*c) {
|
hash = ((hash << 1) | (hash >> 63)) ^ *c;
|
c++;
|
}
|
}
|
return hash;
|
}
|
|
TableData_Hashed::TableData_Hashed(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.empty() ? fields : key_fields, fields, fields_len, fields_type)
|
{
|
|
}
|
|
TableData_Hashed::~TableData_Hashed() {}
|
TableType TableData_Hashed::type() { return TABLE_HASHED; }
|
|
HashedRows::iterator TableData_Hashed::sequentialScan(HashedRows::iterator begin, const std::vector<std::string>& keys, const std::vector<std::string>& search_values)
|
{
|
SearchCondition condition;
|
if(!conditionFromKV(keys, search_values, condition))
|
return rows_pkey.end();
|
|
for(auto it=begin; it!=rows_pkey.end(); it++) {
|
if(getDataRow(it->second)->checkCondition(condition))
|
return it;
|
}
|
return rows_pkey.end();
|
}
|
|
uint64_t TableData_Hashed::sequentialScan(const uint64_t begin, const std::vector<std::string>& keys, const std::vector<std::string>& search_values)
|
{
|
SearchCondition condition;
|
if(!conditionFromKV(keys, search_values, condition))
|
return npos;
|
|
uint64_t size = rows_data.size();
|
for(auto it=begin; it < size; it++) {
|
if(rows_data[it] != nullptr && rows_data[it]->checkCondition(condition))
|
return it;
|
}
|
return npos;
|
}
|
|
HashedRows::iterator TableData_Hashed::sequentialScan(HashedRows::iterator begin, const ComplexSearchCondition__int& condition, uint64_t* index)
|
{
|
writeLog(LOG_DEBUG, "sequentialScan...");
|
|
for(auto it=begin; it!=rows_pkey.end(); it++) {
|
if(index)
|
(*index)++;
|
if(getDataRow(it->second)->checkCondition(condition))
|
return it;
|
}
|
return rows_pkey.end();
|
}
|
|
uint64_t TableData_Hashed::sequentialScan(const uint64_t begin, const ComplexSearchCondition__int& condition)
|
{
|
writeLog(LOG_DEBUG, "sequentialScan...");
|
uint64_t size = rows_data.size();
|
for(auto it=begin; it < size; it++) {
|
if(rows_data[it] != nullptr && rows_data[it]->checkCondition(condition))
|
return it;
|
}
|
return npos;
|
}
|
|
std::shared_ptr<TableRowData> TableData_Hashed::getDataRow(const uint64_t idx)
|
{
|
if(rows_data.size() > idx)
|
return rows_data[idx];
|
return nullptr;
|
}
|
|
bool TableData_Hashed::copyTo(std::shared_ptr<TableData>& other)
|
{
|
other->clear();
|
for(RowPointer& it: rows_data) {
|
if(it != nullptr)
|
other->appendDataRow(it);
|
}
|
return true;
|
}
|
|
bool TableData_Hashed::appendDataRow(const std::vector<std::string>& values, ModifyStatsData *stats)
|
{
|
std::shared_ptr<TableRowData> row = std::make_shared<TableRowData>(this, values);
|
TableRowKey key = row->getKey();
|
if(rows_pkey.find(key) != rows_pkey.end()) {
|
writeLog(LOG_WARNING, "%s: key already exists", __func__);
|
return false;
|
}
|
uint64_t row_idx = rows_data.size();
|
rows_pkey.emplace(key, row_idx);
|
rows_data.push_back(row);
|
#ifdef HASH_TABLE_DEBUG
|
TableRowKeyHasher hasher;
|
hash_set.insert(hasher(key));
|
#endif
|
registerRow(row, 0, row_idx);
|
if(stats) {
|
#ifdef MODIFY_STATS_SHORT_RESULT
|
if(stats->list.empty())
|
#endif
|
stats->list.push_back(row->id_record);
|
}
|
return true;
|
}
|
|
bool TableData_Hashed::updateDataRowByID(const std::vector<std::string>& values, const RecordID id_record)
|
{
|
RowPointer old_row = getDataRowByID(id_record);
|
if(nullptr == old_row) {
|
writeLog(LOG_DEBUG, "%s: invalid row id: %lu", __func__, id_record);
|
return false;
|
}
|
std::shared_ptr<TableRowData> row = std::make_shared<TableRowData>(this, values);
|
TableRowKey key = row->getKey();
|
TableRowKey old_key = old_row->getKey();
|
auto it = rows_pkey.find(key);
|
if(old_key != key) {
|
writeLog(LOG_INFO, "%s: key fields change not allowed", __func__);
|
return false;
|
}
|
rows_data[it->second] = row;
|
registerRow(row, old_row->id_record, it->second);
|
return true;
|
}
|
|
bool TableData_Hashed::updateDataRow(const std::vector<std::string>& values, const uint64_t index)
|
{
|
writeLog(LOG_ERR, "%s: Indexed access to hashed tables not allowed", __func__);
|
return false;
|
}
|
|
bool TableData_Hashed::updateDataRow(const std::vector<std::string>& values, const std::vector<std::string>& old_key_values)
|
{
|
std::shared_ptr<TableRowData> row = std::make_shared<TableRowData>(this, values);
|
TableRowKey key = row->getKey();
|
TableRowKey old_key = keyFromValues(old_key_values);
|
if(rows_pkey.find(old_key) == rows_pkey.end()) {
|
writeLog(LOG_DEBUG, "%s: no row found by key", __func__);
|
return false;
|
}
|
auto it = rows_pkey.find(key);
|
if(old_key != key) {
|
writeLog(LOG_INFO, "%s: key fields change not allowed", __func__);
|
return false;
|
}
|
RecordID id = rows_data[it->second]->id_record;
|
rows_data[it->second] = row;
|
registerRow(row, id, it->second);
|
return true;
|
}
|
|
bool TableData_Hashed::insertDataRow(const std::vector<std::string>& values, const uint64_t index, ModifyStatsData *stats)
|
{
|
writeLog(LOG_WARNING, "%s: Indexed access to hashed tables not allowed, ingoring index", __func__);
|
return appendDataRow(values, stats);
|
}
|
|
bool TableData_Hashed::insertDataRows(const std::vector<std::vector<std::string> >& values, const uint64_t index, ModifyStatsData *stats)
|
{
|
|
writeLog(LOG_ERR, "%s: Indexed access to hashed tables not allowed", __func__);
|
return false;
|
}
|
|
bool TableData_Hashed::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_Hashed::selectDataRow(const uint64_t index, std::vector<std::string>& values, RecordID& id_record)
|
{
|
writeLog(LOG_ERR, "%s: Indexed access to hashed tables not allowed", __func__);
|
return false;
|
}
|
|
bool TableData_Hashed::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)
|
{
|
TableRowKey key;
|
id_record = 0;
|
index = 0;
|
HashedRows::iterator it = rows_pkey.end();
|
if(keyFromValues(keys, search_values, key)) {
|
it = rows_pkey.find(key);
|
} else {
|
writeLog(LOG_DEBUG, "%s: key fields missmatch, performing sequential scan...", __func__);
|
it = sequentialScan(rows_pkey.begin(), keys, search_values);
|
}
|
if(it == rows_pkey.end()) {
|
writeLog(LOG_DEBUG, "%s: no row found by key", __func__);
|
return false;
|
}
|
RowPointer row = rows_data[it->second];
|
row->getCellsData(values);
|
id_record = row->id_record;
|
return true;
|
}
|
|
bool TableData_Hashed::selectNextDataRow(const ComplexSearchCondition__int& condition,
|
const uint64_t index_from, const RecordID id_prev,
|
std::vector<std::string>& values, RecordID& id_record, uint64_t& index)
|
{
|
writeLog(LOG_ERR, "%s: Indexed access to hashed tables not allowed", __func__);
|
return false;
|
}
|
|
bool TableData_Hashed::selectDataRows(std::vector<TableResponseRowData>& values)
|
{
|
uint64_t count = rowsCount();
|
values.resize(count);
|
// HashedRows::iterator it = rows.begin();
|
for(uint64_t i = 0, j = 0; i<count && j<rows_data.size(); i++, j++) {
|
if(rows_data[j] == nullptr) {
|
j++;
|
continue;
|
}
|
// RowPointer& row = it->second;
|
RowPointer& row = rows_data[j];
|
row->getCellsData(values[i].data);
|
values[i].id_record = row->id_record;
|
values[i].index = 0;
|
}
|
return true;
|
}
|
|
bool TableData_Hashed::deleteDataRowByID(const RecordID id_record)
|
{
|
RowPointer row = getDataRowByID(id_record);
|
if(nullptr == row) {
|
writeLog(LOG_DEBUG, "%s: invalid row id: %llu", __func__, id_record);
|
return false;
|
}
|
TableRowKey key = row->getKey();
|
auto it = rows_pkey.find(key);
|
if(it == rows_pkey.end()) {
|
writeLog(LOG_ERR, "%s: internal error, row %llu not found!", __func__);
|
return false;
|
}
|
uint64_t idx = it->second;
|
deregisterRow(row->id_record, 0);
|
rows_pkey.erase(it);
|
rows_data[idx] = nullptr;
|
if(idx == rows_data.size() - 1)
|
rows_data.resize(idx);
|
return true;
|
}
|
|
bool TableData_Hashed::deleteDataRow(const uint64_t index)
|
{
|
writeLog(LOG_ERR, "%s: Indexed access to hashed tables not allowed", __func__);
|
return false;
|
}
|
|
bool TableData_Hashed::deleteDataRows(const uint64_t index, const uint64_t& count, uint64_t& real_count)
|
{
|
writeLog(LOG_ERR, "%s: Indexed access to hashed tables not allowed", __func__);
|
return false;
|
}
|
|
bool TableData_Hashed::deleteDataRows(const std::vector<std::string>& keys, const std::vector<std::string>& search_values, uint64_t& real_count)
|
{
|
TableRowKey key;
|
real_count = 0;
|
HashedRows::iterator it = rows_pkey.end();
|
if(keyFromValues(keys, search_values, key)) {
|
it = rows_pkey.find(key);
|
if(it == rows_pkey.end()) {
|
writeLog(LOG_DEBUG, "%s: no row found by key", __func__);
|
return false;
|
}
|
} else {
|
writeLog(LOG_DEBUG, "%s: key fields missmatch, performing sequential scan...", __func__);
|
it = sequentialScan(rows_pkey.begin(), keys, search_values);
|
if(it == rows_pkey.end()) {
|
writeLog(LOG_DEBUG, "%s: no row found", __func__);
|
return false;
|
}
|
}
|
uint64_t idx = it->second;
|
deregisterRow(rows_data[idx]->id_record, 0);
|
rows_pkey.erase(it);
|
rows_data[idx] = nullptr;
|
if(idx == rows_data.size() - 1)
|
rows_data.resize(idx);
|
real_count = 1;
|
return true;
|
}
|
|
bool TableData_Hashed::deleteAdjacentDuplicates(std::vector<uint16_t>& indexes, uint64_t& rows_deleted)
|
{
|
rows_deleted = 0;
|
auto it = rows_pkey.begin();
|
auto it_next = it;
|
it_next++;
|
while(it_next != rows_pkey.end()) {
|
RowPointer& row = rows_data[it->second];
|
RowPointer& row_next = rows_data[it_next->second];
|
if(row->equal(row_next, indexes)) {
|
deregisterRow(row_next->id_record, 0);
|
rows_data[it_next->second] = nullptr;
|
it_next = rows_pkey.erase(it_next);
|
rows_deleted++;
|
} else {
|
it++;
|
it_next = it;
|
it_next++;
|
}
|
}
|
return true;
|
}
|
|
void TableData_Hashed::clearData()
|
{
|
rows_pkey.clear();
|
rows_data.clear();
|
}
|
|
uint64_t TableData_Hashed::rowsCount()
|
{
|
#ifdef HASH_TABLE_DEBUG
|
writeLog(LOG_INFO, "rows: %lld, uniq hashes: %lld", rows.size(), hash_set.size());
|
#endif
|
return rows_pkey.size();
|
}
|
|
void TableData_Hashed::vacuum()
|
{
|
if(rows_pkey.size() < rows_data.size() / VACUUM_FACTOR)
|
return;
|
#warning TODO: VACUUM
|
}
|
|
// cursors
|
TableData_Hashed::TableCursor::TableCursor() :index(npos)
|
{}
|
TableData_Hashed::TableCursor::TableCursor(const ComplexSearchCondition__int&& cond) :index(npos), condition(std::move(cond))
|
{}
|
TableData_Hashed::TableCursor::~TableCursor() {}
|
|
CursorID TableData_Hashed::nextCursorID()
|
{
|
while(cursors.find(last_cursor_id) != cursors.end())
|
last_cursor_id++;
|
return last_cursor_id++;
|
}
|
|
bool TableData_Hashed::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_Hashed::cursorCreate(const ComplexSearchCondition__int&& cond, const uint64_t index_from, const uint64_t index_to, uint64_t& cursor_id, std::string& error_string)
|
{
|
error_string = "Indexed access to hashed tables not allowed";
|
writeLog(LOG_ERR, "%s: %s", __func__, error_string.c_str());
|
return false;
|
}
|
|
bool TableData_Hashed::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_Hashed::updateCursors(const uint64_t index, bool added)
|
{
|
// writeLog(LOG_WARNING, "%s: Cursors not fully implemented for hashed tables", __func__);
|
}
|
|
void TableData_Hashed::resetCursors()
|
{
|
for(auto& cursor: cursors) {
|
cursor.second.index = npos;
|
}
|
}
|
|
bool TableData_Hashed::selectNextDataRow_cursor(const uint64_t cursor_id, TableResponseRowData& values, std::string& error_string)
|
{
|
auto cur = cursors.find(cursor_id);
|
if(cur == cursors.end()) {
|
error_string = "Invalid cursor ID";
|
return false;
|
}
|
if(cur->second.index == npos) {
|
cur->second.index = 0;
|
}
|
cur->second.index = sequentialScan(cur->second.index, cur->second.condition);
|
if(cur->second.index != npos) {
|
RowPointer& row = rows_data[cur->second.index];
|
row->getCellsData(values.data);
|
values.id_record = row->id_record;
|
values.index = 0;
|
cur->second.index++;
|
return true;
|
}
|
return false;
|
}
|
|
} // namespace DGrid
|