mirror of
https://github.com/minetest/minetestmapper.git
synced 2025-02-22 15:00:27 +01:00
Add support for new SQLite map schema
(added in Luanti 5.12.0-dev)
This commit is contained in:
parent
bbe2f8f404
commit
6947c5c4e4
@ -415,8 +415,8 @@ void TileGenerator::openDb(const std::string &input_path)
|
|||||||
} else if (m_exhaustiveSearch == EXH_FULL || m_exhaustiveSearch == EXH_Y) {
|
} else if (m_exhaustiveSearch == EXH_FULL || m_exhaustiveSearch == EXH_Y) {
|
||||||
if (m_db->preferRangeQueries()) {
|
if (m_db->preferRangeQueries()) {
|
||||||
std::cerr << "Note: The current database backend supports efficient "
|
std::cerr << "Note: The current database backend supports efficient "
|
||||||
"range queries, forcing exhaustive search should always result "
|
"range queries, forcing exhaustive search will generally result "
|
||||||
" in worse performance." << std::endl;
|
"in worse performance." << std::endl;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
assert(m_exhaustiveSearch != EXH_AUTO);
|
assert(m_exhaustiveSearch != EXH_AUTO);
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
#include <unistd.h> // for usleep
|
#include <unistd.h> // for usleep
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
#include <time.h>
|
#include <cassert>
|
||||||
#include "db-sqlite3.h"
|
#include "db-sqlite3.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
@ -43,6 +43,32 @@ DBSQLite3::DBSQLite3(const std::string &mapdir)
|
|||||||
#endif
|
#endif
|
||||||
SQLOK(sqlite3_open_v2(db_name.c_str(), &db, flags, 0));
|
SQLOK(sqlite3_open_v2(db_name.c_str(), &db, flags, 0));
|
||||||
|
|
||||||
|
// There's a simple, dumb way to check if we have a new or old database schema.
|
||||||
|
// If we prepare a statement that references columns that don't exist, it will
|
||||||
|
// error right there.
|
||||||
|
int result = sqlite3_prepare_v2(db, "SELECT x, y, z FROM blocks", -1,
|
||||||
|
&stmt_get_block_pos, NULL);
|
||||||
|
newFormat = result == SQLITE_OK;
|
||||||
|
#ifndef NDEBUG
|
||||||
|
std::cerr << "Detected " << (newFormat ? "new" : "old") << " SQLite schema" << std::endl;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (newFormat) {
|
||||||
|
SQLOK(sqlite3_prepare_v2(db,
|
||||||
|
"SELECT y, data FROM blocks WHERE "
|
||||||
|
"x = ? AND z = ? AND y BETWEEN ? AND ?",
|
||||||
|
-1, &stmt_get_blocks_xz_range, NULL));
|
||||||
|
|
||||||
|
SQLOK(sqlite3_prepare_v2(db,
|
||||||
|
"SELECT data FROM blocks WHERE x = ? AND y = ? AND z = ?",
|
||||||
|
-1, &stmt_get_block_exact, NULL));
|
||||||
|
|
||||||
|
SQLOK(sqlite3_prepare_v2(db,
|
||||||
|
"SELECT x, y, z FROM blocks WHERE "
|
||||||
|
"x >= ? AND y >= ? AND z >= ? AND "
|
||||||
|
"x < ? AND y < ? AND z < ?",
|
||||||
|
-1, &stmt_get_block_pos_range, NULL));
|
||||||
|
} else {
|
||||||
SQLOK(sqlite3_prepare_v2(db,
|
SQLOK(sqlite3_prepare_v2(db,
|
||||||
"SELECT pos, data FROM blocks WHERE pos BETWEEN ? AND ?",
|
"SELECT pos, data FROM blocks WHERE pos BETWEEN ? AND ?",
|
||||||
-1, &stmt_get_blocks_z, NULL));
|
-1, &stmt_get_blocks_z, NULL));
|
||||||
@ -57,19 +83,24 @@ DBSQLite3::DBSQLite3(const std::string &mapdir)
|
|||||||
|
|
||||||
SQLOK(sqlite3_prepare_v2(db,
|
SQLOK(sqlite3_prepare_v2(db,
|
||||||
"SELECT pos FROM blocks WHERE pos BETWEEN ? AND ?",
|
"SELECT pos FROM blocks WHERE pos BETWEEN ? AND ?",
|
||||||
-1, &stmt_get_block_pos_z, NULL));
|
-1, &stmt_get_block_pos_range, NULL));
|
||||||
|
}
|
||||||
|
|
||||||
|
#undef RANGE
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
DBSQLite3::~DBSQLite3()
|
DBSQLite3::~DBSQLite3()
|
||||||
{
|
{
|
||||||
sqlite3_finalize(stmt_get_blocks_z);
|
sqlite3_finalize(stmt_get_blocks_z);
|
||||||
|
sqlite3_finalize(stmt_get_blocks_xz_range);
|
||||||
sqlite3_finalize(stmt_get_block_pos);
|
sqlite3_finalize(stmt_get_block_pos);
|
||||||
sqlite3_finalize(stmt_get_block_pos_z);
|
sqlite3_finalize(stmt_get_block_pos_range);
|
||||||
sqlite3_finalize(stmt_get_block_exact);
|
sqlite3_finalize(stmt_get_block_exact);
|
||||||
|
|
||||||
if (sqlite3_close(db) != SQLITE_OK) {
|
if (sqlite3_close(db) != SQLITE_OK) {
|
||||||
std::cerr << "Error closing SQLite database." << std::endl;
|
std::cerr << "Error closing SQLite database: "
|
||||||
|
<< sqlite3_errmsg(db) << std::endl;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,10 +119,16 @@ std::vector<BlockPos> DBSQLite3::getBlockPos(BlockPos min, BlockPos max)
|
|||||||
int result;
|
int result;
|
||||||
sqlite3_stmt *stmt;
|
sqlite3_stmt *stmt;
|
||||||
|
|
||||||
if(min.z <= -2048 && max.z >= 2048) {
|
if (newFormat) {
|
||||||
|
stmt = stmt_get_block_pos_range;
|
||||||
|
int col = bind_pos(stmt, 1, min);
|
||||||
|
bind_pos(stmt, col, max);
|
||||||
|
} else {
|
||||||
|
// can handle range query on Z axis via SQL
|
||||||
|
if (min.z <= -2048 && max.z >= 2048) {
|
||||||
stmt = stmt_get_block_pos;
|
stmt = stmt_get_block_pos;
|
||||||
} else {
|
} else {
|
||||||
stmt = stmt_get_block_pos_z;
|
stmt = stmt_get_block_pos_range;
|
||||||
int64_t minPos, maxPos;
|
int64_t minPos, maxPos;
|
||||||
if (min.z < -2048)
|
if (min.z < -2048)
|
||||||
min.z = -2048;
|
min.z = -2048;
|
||||||
@ -101,14 +138,22 @@ std::vector<BlockPos> DBSQLite3::getBlockPos(BlockPos min, BlockPos max)
|
|||||||
SQLOK(sqlite3_bind_int64(stmt, 1, minPos));
|
SQLOK(sqlite3_bind_int64(stmt, 1, minPos));
|
||||||
SQLOK(sqlite3_bind_int64(stmt, 2, maxPos));
|
SQLOK(sqlite3_bind_int64(stmt, 2, maxPos));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
std::vector<BlockPos> positions;
|
std::vector<BlockPos> positions;
|
||||||
|
BlockPos pos;
|
||||||
while ((result = sqlite3_step(stmt)) != SQLITE_DONE) {
|
while ((result = sqlite3_step(stmt)) != SQLITE_DONE) {
|
||||||
SQLROW2()
|
SQLROW2()
|
||||||
|
|
||||||
int64_t posHash = sqlite3_column_int64(stmt, 0);
|
if (newFormat) {
|
||||||
BlockPos pos = decodeBlockPos(posHash);
|
pos.x = sqlite3_column_int(stmt, 0);
|
||||||
if(pos.x >= min.x && pos.x < max.x && pos.y >= min.y && pos.y < max.y)
|
pos.y = sqlite3_column_int(stmt, 1);
|
||||||
|
pos.z = sqlite3_column_int(stmt, 2);
|
||||||
|
} else {
|
||||||
|
pos = decodeBlockPos(sqlite3_column_int64(stmt, 0));
|
||||||
|
if (pos.x < min.x || pos.x >= max.x || pos.y < min.y || pos.y >= max.y)
|
||||||
|
continue;
|
||||||
|
}
|
||||||
positions.emplace_back(pos);
|
positions.emplace_back(pos);
|
||||||
}
|
}
|
||||||
SQLOK(sqlite3_reset(stmt));
|
SQLOK(sqlite3_reset(stmt));
|
||||||
@ -121,6 +166,8 @@ void DBSQLite3::loadBlockCache(int16_t zPos)
|
|||||||
int result;
|
int result;
|
||||||
blockCache.clear();
|
blockCache.clear();
|
||||||
|
|
||||||
|
assert(!newFormat);
|
||||||
|
|
||||||
int64_t minPos, maxPos;
|
int64_t minPos, maxPos;
|
||||||
getPosRange(minPos, maxPos, zPos, zPos);
|
getPosRange(minPos, maxPos, zPos, zPos);
|
||||||
|
|
||||||
@ -141,6 +188,25 @@ void DBSQLite3::loadBlockCache(int16_t zPos)
|
|||||||
void DBSQLite3::getBlocksOnXZ(BlockList &blocks, int16_t x, int16_t z,
|
void DBSQLite3::getBlocksOnXZ(BlockList &blocks, int16_t x, int16_t z,
|
||||||
int16_t min_y, int16_t max_y)
|
int16_t min_y, int16_t max_y)
|
||||||
{
|
{
|
||||||
|
// New format: use a real range query
|
||||||
|
if (newFormat) {
|
||||||
|
auto *stmt = stmt_get_blocks_xz_range;
|
||||||
|
SQLOK(sqlite3_bind_int(stmt, 1, x));
|
||||||
|
SQLOK(sqlite3_bind_int(stmt, 2, z));
|
||||||
|
SQLOK(sqlite3_bind_int(stmt, 3, min_y));
|
||||||
|
SQLOK(sqlite3_bind_int(stmt, 4, max_y - 1)); // BETWEEN is inclusive
|
||||||
|
|
||||||
|
int result;
|
||||||
|
while ((result = sqlite3_step(stmt)) != SQLITE_DONE) {
|
||||||
|
SQLROW2()
|
||||||
|
|
||||||
|
BlockPos pos(x, sqlite3_column_int(stmt, 0), z);
|
||||||
|
blocks.emplace_back(pos, read_blob(stmt, 1));
|
||||||
|
}
|
||||||
|
SQLOK(sqlite3_reset(stmt));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/* Cache the blocks on the given Z coordinate between calls, this only
|
/* Cache the blocks on the given Z coordinate between calls, this only
|
||||||
* works due to order in which the TileGenerator asks for blocks. */
|
* works due to order in which the TileGenerator asks for blocks. */
|
||||||
if (z != blockCachedZ) {
|
if (z != blockCachedZ) {
|
||||||
@ -180,8 +246,7 @@ void DBSQLite3::getBlocksByPos(BlockList &blocks,
|
|||||||
int result;
|
int result;
|
||||||
|
|
||||||
for (auto pos : positions) {
|
for (auto pos : positions) {
|
||||||
int64_t dbPos = encodeBlockPos(pos);
|
bind_pos(stmt_get_block_exact, 1, pos);
|
||||||
SQLOK(sqlite3_bind_int64(stmt_get_block_exact, 1, dbPos));
|
|
||||||
|
|
||||||
SQLROW1(stmt_get_block_exact)
|
SQLROW1(stmt_get_block_exact)
|
||||||
if (result == SQLITE_ROW)
|
if (result == SQLITE_ROW)
|
||||||
|
@ -14,13 +14,28 @@ public:
|
|||||||
const std::vector<BlockPos> &positions) override;
|
const std::vector<BlockPos> &positions) override;
|
||||||
~DBSQLite3() override;
|
~DBSQLite3() override;
|
||||||
|
|
||||||
bool preferRangeQueries() const override { return false; }
|
bool preferRangeQueries() const override { return newFormat; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
static inline void getPosRange(int64_t &min, int64_t &max, int16_t zPos,
|
static inline void getPosRange(int64_t &min, int64_t &max, int16_t zPos,
|
||||||
int16_t zPos2);
|
int16_t zPos2);
|
||||||
void loadBlockCache(int16_t zPos);
|
void loadBlockCache(int16_t zPos);
|
||||||
|
|
||||||
|
// bind pos to statement. returns index of next column.
|
||||||
|
inline int bind_pos(sqlite3_stmt *stmt, int iCol, BlockPos pos)
|
||||||
|
{
|
||||||
|
if (newFormat) {
|
||||||
|
sqlite3_bind_int(stmt, iCol, pos.x);
|
||||||
|
sqlite3_bind_int(stmt, iCol + 1, pos.y);
|
||||||
|
sqlite3_bind_int(stmt, iCol + 2, pos.z);
|
||||||
|
return iCol + 3;
|
||||||
|
} else {
|
||||||
|
sqlite3_bind_int64(stmt, iCol, encodeBlockPos(pos));
|
||||||
|
return iCol + 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// read blob from statement
|
||||||
static inline ustring read_blob(sqlite3_stmt *stmt, int iCol)
|
static inline ustring read_blob(sqlite3_stmt *stmt, int iCol)
|
||||||
{
|
{
|
||||||
auto *data = reinterpret_cast<const unsigned char *>(
|
auto *data = reinterpret_cast<const unsigned char *>(
|
||||||
@ -32,10 +47,12 @@ private:
|
|||||||
sqlite3 *db = NULL;
|
sqlite3 *db = NULL;
|
||||||
|
|
||||||
sqlite3_stmt *stmt_get_block_pos = NULL;
|
sqlite3_stmt *stmt_get_block_pos = NULL;
|
||||||
sqlite3_stmt *stmt_get_block_pos_z = NULL;
|
sqlite3_stmt *stmt_get_block_pos_range = NULL;
|
||||||
sqlite3_stmt *stmt_get_blocks_z = NULL;
|
sqlite3_stmt *stmt_get_blocks_z = NULL;
|
||||||
|
sqlite3_stmt *stmt_get_blocks_xz_range = NULL;
|
||||||
sqlite3_stmt *stmt_get_block_exact = NULL;
|
sqlite3_stmt *stmt_get_block_exact = NULL;
|
||||||
|
|
||||||
|
bool newFormat = false;
|
||||||
int16_t blockCachedZ = -10000;
|
int16_t blockCachedZ = -10000;
|
||||||
std::unordered_map<int16_t, BlockList> blockCache; // indexed by X
|
std::unordered_map<int16_t, BlockList> blockCache; // indexed by X
|
||||||
};
|
};
|
||||||
|
Loading…
x
Reference in New Issue
Block a user