2014-03-05 21:41:27 +01:00
|
|
|
#include <stdexcept>
|
2014-03-05 21:46:40 +01:00
|
|
|
#include <unistd.h> // for usleep
|
2016-10-08 21:09:52 +02:00
|
|
|
#include <iostream>
|
2020-03-27 16:12:26 +01:00
|
|
|
#include <algorithm>
|
|
|
|
#include <time.h>
|
2014-04-19 08:13:34 +02:00
|
|
|
#include "db-sqlite3.h"
|
|
|
|
#include "types.h"
|
2014-03-05 21:41:27 +01:00
|
|
|
|
2014-03-28 18:42:37 +01:00
|
|
|
#define SQLRES(f, good) \
|
|
|
|
result = (sqlite3_##f);\
|
|
|
|
if (result != good) {\
|
2014-03-28 21:47:19 +01:00
|
|
|
throw std::runtime_error(sqlite3_errmsg(db));\
|
2014-03-28 18:42:37 +01:00
|
|
|
}
|
|
|
|
#define SQLOK(f) SQLRES(f, SQLITE_OK)
|
|
|
|
|
2014-04-19 08:13:34 +02:00
|
|
|
DBSQLite3::DBSQLite3(const std::string &mapdir)
|
|
|
|
{
|
2014-03-28 18:42:37 +01:00
|
|
|
int result;
|
2014-03-05 21:41:27 +01:00
|
|
|
std::string db_name = mapdir + "map.sqlite";
|
2014-03-28 18:42:37 +01:00
|
|
|
|
2014-04-19 08:13:34 +02:00
|
|
|
SQLOK(open_v2(db_name.c_str(), &db, SQLITE_OPEN_READONLY |
|
|
|
|
SQLITE_OPEN_PRIVATECACHE, 0))
|
2014-03-28 18:42:37 +01:00
|
|
|
|
2014-03-28 21:47:19 +01:00
|
|
|
SQLOK(prepare_v2(db,
|
2014-04-19 08:13:34 +02:00
|
|
|
"SELECT pos, data FROM blocks WHERE pos BETWEEN ? AND ?",
|
|
|
|
-1, &stmt_get_blocks_z, NULL))
|
2014-03-28 18:42:37 +01:00
|
|
|
|
2020-03-27 21:10:00 +01:00
|
|
|
SQLOK(prepare_v2(db,
|
|
|
|
"SELECT data FROM blocks WHERE pos = ?",
|
|
|
|
-1, &stmt_get_block_exact, NULL))
|
|
|
|
|
2014-03-28 21:47:19 +01:00
|
|
|
SQLOK(prepare_v2(db,
|
2014-03-28 18:42:37 +01:00
|
|
|
"SELECT pos FROM blocks",
|
|
|
|
-1, &stmt_get_block_pos, NULL))
|
2020-03-27 16:12:26 +01:00
|
|
|
|
|
|
|
SQLOK(prepare_v2(db,
|
|
|
|
"SELECT pos FROM blocks WHERE pos BETWEEN ? AND ?",
|
|
|
|
-1, &stmt_get_block_pos_z, NULL))
|
2014-03-05 21:41:27 +01:00
|
|
|
}
|
|
|
|
|
2014-04-19 08:13:34 +02:00
|
|
|
|
|
|
|
DBSQLite3::~DBSQLite3()
|
|
|
|
{
|
2016-10-08 21:09:52 +02:00
|
|
|
sqlite3_finalize(stmt_get_blocks_z);
|
|
|
|
sqlite3_finalize(stmt_get_block_pos);
|
2020-03-27 16:12:26 +01:00
|
|
|
sqlite3_finalize(stmt_get_block_pos_z);
|
2020-03-27 21:10:00 +01:00
|
|
|
sqlite3_finalize(stmt_get_block_exact);
|
2014-03-28 18:42:37 +01:00
|
|
|
|
2016-10-08 21:09:52 +02:00
|
|
|
if (sqlite3_close(db) != SQLITE_OK) {
|
|
|
|
std::cerr << "Error closing SQLite database." << std::endl;
|
|
|
|
};
|
2014-03-05 21:41:27 +01:00
|
|
|
}
|
|
|
|
|
2020-03-27 16:12:26 +01:00
|
|
|
|
|
|
|
inline void DBSQLite3::getPosRange(int64_t &min, int64_t &max, int16_t zPos,
|
|
|
|
int16_t zPos2) const
|
|
|
|
{
|
|
|
|
/* The range of block positions is [-2048, 2047], which turns into [0, 4095]
|
|
|
|
* when casted to unsigned. This didn't actually help me understand the
|
|
|
|
* numbers below, but I wanted to write it down.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Magic numbers!
|
|
|
|
min = encodeBlockPos(BlockPos(0, -2048, zPos));
|
|
|
|
max = encodeBlockPos(BlockPos(0, 2048, zPos2)) - 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::vector<BlockPos> DBSQLite3::getBlockPos(BlockPos min, BlockPos max)
|
2014-04-19 08:13:34 +02:00
|
|
|
{
|
2014-03-28 21:47:19 +01:00
|
|
|
int result;
|
2020-03-27 16:12:26 +01:00
|
|
|
sqlite3_stmt *stmt;
|
|
|
|
|
|
|
|
if(min.z <= -2048 && max.z >= 2048) {
|
|
|
|
stmt = stmt_get_block_pos;
|
|
|
|
} else {
|
|
|
|
stmt = stmt_get_block_pos_z;
|
|
|
|
int64_t minPos, maxPos;
|
|
|
|
if (min.z < -2048)
|
|
|
|
min.z = -2048;
|
|
|
|
if (max.z > 2048)
|
|
|
|
max.z = 2048;
|
|
|
|
getPosRange(minPos, maxPos, min.z, max.z - 1);
|
|
|
|
SQLOK(bind_int64(stmt, 1, minPos))
|
|
|
|
SQLOK(bind_int64(stmt, 2, maxPos))
|
|
|
|
}
|
|
|
|
|
2014-04-19 08:13:34 +02:00
|
|
|
std::vector<BlockPos> positions;
|
2020-03-27 16:12:26 +01:00
|
|
|
while ((result = sqlite3_step(stmt)) != SQLITE_DONE) {
|
|
|
|
if (result == SQLITE_BUSY) { // Wait some time and try again
|
2014-03-28 18:42:37 +01:00
|
|
|
usleep(10000);
|
2020-03-27 16:12:26 +01:00
|
|
|
} else if (result != SQLITE_ROW) {
|
2014-03-28 21:47:19 +01:00
|
|
|
throw std::runtime_error(sqlite3_errmsg(db));
|
2014-03-05 21:41:27 +01:00
|
|
|
}
|
2020-03-27 16:12:26 +01:00
|
|
|
|
|
|
|
int64_t posHash = sqlite3_column_int64(stmt, 0);
|
|
|
|
BlockPos pos = decodeBlockPos(posHash);
|
|
|
|
if(pos.x >= min.x && pos.x < max.x && pos.y >= min.y && pos.y < max.y)
|
|
|
|
positions.emplace_back(pos);
|
2014-03-05 21:41:27 +01:00
|
|
|
}
|
2020-03-27 16:12:26 +01:00
|
|
|
SQLOK(reset(stmt));
|
2014-04-19 08:13:34 +02:00
|
|
|
return positions;
|
2014-03-05 21:41:27 +01:00
|
|
|
}
|
|
|
|
|
2014-04-19 08:13:34 +02:00
|
|
|
|
2020-03-27 16:12:26 +01:00
|
|
|
void DBSQLite3::loadBlockCache(int16_t zPos)
|
2014-03-05 21:41:27 +01:00
|
|
|
{
|
2014-04-19 08:13:34 +02:00
|
|
|
int result;
|
2020-03-27 16:12:26 +01:00
|
|
|
blockCache.clear();
|
2014-03-05 21:41:27 +01:00
|
|
|
|
2020-03-27 16:12:26 +01:00
|
|
|
int64_t minPos, maxPos;
|
|
|
|
getPosRange(minPos, maxPos, zPos, zPos);
|
2014-03-05 21:41:27 +01:00
|
|
|
|
2014-04-19 08:13:34 +02:00
|
|
|
SQLOK(bind_int64(stmt_get_blocks_z, 1, minPos));
|
|
|
|
SQLOK(bind_int64(stmt_get_blocks_z, 2, maxPos));
|
2014-03-05 21:41:27 +01:00
|
|
|
|
2014-04-19 08:13:34 +02:00
|
|
|
while ((result = sqlite3_step(stmt_get_blocks_z)) != SQLITE_DONE) {
|
2020-03-27 16:12:26 +01:00
|
|
|
if (result == SQLITE_BUSY) { // Wait some time and try again
|
2014-03-05 21:46:40 +01:00
|
|
|
usleep(10000);
|
2020-03-27 16:12:26 +01:00
|
|
|
} else if (result != SQLITE_ROW) {
|
2014-03-28 21:47:19 +01:00
|
|
|
throw std::runtime_error(sqlite3_errmsg(db));
|
2014-03-05 21:41:27 +01:00
|
|
|
}
|
2020-03-27 16:12:26 +01:00
|
|
|
|
|
|
|
int64_t posHash = sqlite3_column_int64(stmt_get_blocks_z, 0);
|
|
|
|
BlockPos pos = decodeBlockPos(posHash);
|
|
|
|
const unsigned char *data = reinterpret_cast<const unsigned char *>(
|
|
|
|
sqlite3_column_blob(stmt_get_blocks_z, 1));
|
|
|
|
size_t size = sqlite3_column_bytes(stmt_get_blocks_z, 1);
|
|
|
|
blockCache[pos.x].emplace_back(pos, ustring(data, size));
|
2014-03-05 21:41:27 +01:00
|
|
|
}
|
2020-03-27 16:12:26 +01:00
|
|
|
SQLOK(reset(stmt_get_blocks_z))
|
2014-03-05 21:41:27 +01:00
|
|
|
}
|
|
|
|
|
2014-03-28 18:42:37 +01:00
|
|
|
|
2020-03-27 16:12:26 +01:00
|
|
|
void DBSQLite3::getBlocksOnXZ(BlockList &blocks, int16_t x, int16_t z,
|
|
|
|
int16_t min_y, int16_t max_y)
|
|
|
|
{
|
|
|
|
/* Cache the blocks on the given Z coordinate between calls, this only
|
|
|
|
* works due to order in which the TileGenerator asks for blocks. */
|
|
|
|
if (z != blockCachedZ) {
|
|
|
|
loadBlockCache(z);
|
|
|
|
blockCachedZ = z;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto it = blockCache.find(x);
|
|
|
|
if (it == blockCache.end())
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (it->second.empty()) {
|
|
|
|
/* We have swapped this list before, this is not supposed to happen
|
|
|
|
* because it's bad for performance. But rather than silently breaking
|
|
|
|
* do the right thing and load the blocks again. */
|
|
|
|
#ifndef NDEBUG
|
|
|
|
std::cout << "Warning: suboptimal access pattern for sqlite3 backend" << std::endl;
|
|
|
|
#endif
|
|
|
|
loadBlockCache(z);
|
|
|
|
}
|
|
|
|
// Swap lists to avoid copying contents
|
|
|
|
blocks.clear();
|
|
|
|
std::swap(blocks, it->second);
|
|
|
|
|
|
|
|
for (auto it = blocks.begin(); it != blocks.end(); ) {
|
|
|
|
if (it->first.y < min_y || it->first.y >= max_y)
|
|
|
|
it = blocks.erase(it);
|
|
|
|
else
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
}
|
2020-03-27 21:10:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
void DBSQLite3::getBlocksByPos(BlockList &blocks,
|
|
|
|
const std::vector<BlockPos> &positions)
|
|
|
|
{
|
|
|
|
int result;
|
|
|
|
|
|
|
|
for (auto pos : positions) {
|
|
|
|
int64_t dbPos = encodeBlockPos(pos);
|
|
|
|
SQLOK(bind_int64(stmt_get_block_exact, 1, dbPos));
|
|
|
|
|
|
|
|
while ((result = sqlite3_step(stmt_get_block_exact)) == SQLITE_BUSY) {
|
|
|
|
usleep(10000); // Wait some time and try again
|
|
|
|
}
|
|
|
|
if (result == SQLITE_DONE) {
|
|
|
|
// no data
|
|
|
|
} else if (result != SQLITE_ROW) {
|
|
|
|
throw std::runtime_error(sqlite3_errmsg(db));
|
|
|
|
} else {
|
|
|
|
const unsigned char *data = reinterpret_cast<const unsigned char *>(
|
|
|
|
sqlite3_column_blob(stmt_get_block_exact, 0));
|
|
|
|
size_t size = sqlite3_column_bytes(stmt_get_block_exact, 0);
|
|
|
|
blocks.emplace_back(pos, ustring(data, size));
|
|
|
|
}
|
|
|
|
|
|
|
|
SQLOK(reset(stmt_get_block_exact))
|
|
|
|
}
|
|
|
|
}
|