mirror of
https://github.com/davidgiven/fluxengine.git
synced 2025-10-24 11:11:02 -07:00
Allow upgrading of flux files to the new format.
This commit is contained in:
27
lib/sql.cc
27
lib/sql.cc
@@ -87,6 +87,7 @@ void sql_bind_string(sqlite3* db, sqlite3_stmt* stmt, const char* name, const ch
|
||||
void sqlPrepareFlux(sqlite3* db)
|
||||
{
|
||||
sqlStmt(db, "PRAGMA synchronous = OFF;");
|
||||
sqlStmt(db, "PRAGMA auto_vacuum = FULL;");
|
||||
sqlStmt(db, "BEGIN;");
|
||||
sqlStmt(db, "CREATE TABLE IF NOT EXISTS properties ("
|
||||
" key TEXT UNIQUE NOT NULL PRIMARY KEY,"
|
||||
@@ -162,6 +163,32 @@ std::unique_ptr<Fluxmap> sqlReadFlux(sqlite3* db, int track, int side)
|
||||
return fluxmap;
|
||||
}
|
||||
|
||||
std::vector<std::pair<unsigned, unsigned>> sqlFindFlux(sqlite3* db)
|
||||
{
|
||||
std::vector<std::pair<unsigned, unsigned>> output;
|
||||
|
||||
sqlite3_stmt* stmt;
|
||||
sqlCheck(db, sqlite3_prepare_v2(db,
|
||||
"SELECT track, side FROM zdata",
|
||||
-1, &stmt, NULL));
|
||||
|
||||
for (;;)
|
||||
{
|
||||
int i = sqlite3_step(stmt);
|
||||
if (i == SQLITE_DONE)
|
||||
break;
|
||||
if (i != SQLITE_ROW)
|
||||
Error() << "failed to read from database: " << sqlite3_errmsg(db);
|
||||
|
||||
unsigned track = sqlite3_column_int(stmt, 0);
|
||||
unsigned side = sqlite3_column_int(stmt, 1);
|
||||
output.push_back(std::make_pair(track, side));
|
||||
}
|
||||
sqlCheck(db, sqlite3_finalize(stmt));
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
void sqlWriteStringProperty(sqlite3* db, const std::string& name, const std::string& value)
|
||||
{
|
||||
if (!hasProperties(db))
|
||||
|
||||
@@ -9,8 +9,9 @@ enum
|
||||
{
|
||||
FLUX_VERSION_0, /* without properties table */
|
||||
FLUX_VERSION_1,
|
||||
FLUX_VERSION_2, /* new bytecode with index marks */
|
||||
|
||||
FLUX_VERSION_CURRENT = 1,
|
||||
FLUX_VERSION_CURRENT = 2,
|
||||
};
|
||||
|
||||
extern void sqlCheck(sqlite3* db, int i);
|
||||
@@ -22,6 +23,7 @@ extern int sqlGetVersion(sqlite3* db);
|
||||
extern void sqlPrepareFlux(sqlite3* db);
|
||||
extern void sqlWriteFlux(sqlite3* db, int track, int side, const Fluxmap& fluxmap);
|
||||
extern std::unique_ptr<Fluxmap> sqlReadFlux(sqlite3* db, int track, int side);
|
||||
extern std::vector<std::pair<unsigned, unsigned>> sqlFindFlux(sqlite3* db);
|
||||
|
||||
extern void sqlWriteStringProperty(sqlite3* db, const std::string& name, const std::string& value);
|
||||
extern std::string sqlReadStringProperty(sqlite3* db, const std::string& name);
|
||||
|
||||
@@ -1,10 +1,37 @@
|
||||
#include "globals.h"
|
||||
#include "flags.h"
|
||||
#include "sql.h"
|
||||
#include "fluxmap.h"
|
||||
#include "fmt/format.h"
|
||||
|
||||
static sqlite3* db;
|
||||
|
||||
static void update_version_1_to_2()
|
||||
{
|
||||
for (const auto i : sqlFindFlux(db))
|
||||
{
|
||||
Fluxmap after;
|
||||
const auto before = sqlReadFlux(db, i.first, i.second);
|
||||
|
||||
/* Remember, before does not contain valid opcodes! */
|
||||
unsigned pending = 0;
|
||||
for (uint8_t b : before->rawBytes())
|
||||
{
|
||||
if (b < 0x80)
|
||||
{
|
||||
after.appendInterval(b + pending);
|
||||
pending = 0;
|
||||
}
|
||||
else
|
||||
pending += 0x80;
|
||||
}
|
||||
|
||||
sqlWriteFlux(db, i.first, i.second, after);
|
||||
std::cout << '.' << std::flush;
|
||||
}
|
||||
std::cout << std::endl;
|
||||
}
|
||||
|
||||
int main(int argc, const char* argv[])
|
||||
{
|
||||
if (argc != 2)
|
||||
@@ -28,7 +55,7 @@ int main(int argc, const char* argv[])
|
||||
|
||||
if (version == FLUX_VERSION_0)
|
||||
{
|
||||
std::cout << "Updating to version 1\n";
|
||||
std::cout << "Upgrading to version 1\n";
|
||||
sqlPrepareFlux(db);
|
||||
sqlStmt(db, "BEGIN;");
|
||||
sqlStmt(db,
|
||||
@@ -36,11 +63,23 @@ int main(int argc, const char* argv[])
|
||||
" SELECT track, side, data, 0 AS compression FROM rawdata;"
|
||||
);
|
||||
sqlStmt(db, "DROP TABLE rawdata;");
|
||||
sqlWriteIntProperty(db, "version", FLUX_VERSION_1);
|
||||
sqlStmt(db, "COMMIT;");
|
||||
version = FLUX_VERSION_1;
|
||||
sqlWriteIntProperty(db, "version", version);
|
||||
sqlStmt(db, "COMMIT;");
|
||||
}
|
||||
|
||||
if (version == FLUX_VERSION_1)
|
||||
{
|
||||
std::cout << "Upgrading to version 2\n";
|
||||
sqlStmt(db, "BEGIN;");
|
||||
update_version_1_to_2();
|
||||
version = FLUX_VERSION_2;
|
||||
sqlWriteIntProperty(db, "version", version);
|
||||
sqlStmt(db, "COMMIT;");
|
||||
}
|
||||
|
||||
std::cout << "Vacuuming\n";
|
||||
sqlStmt(db, "VACUUM;");
|
||||
std::cout << "Upgrade done\n";
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user