Skip to content

Commit

Permalink
Last fixes for the patch db
Browse files Browse the repository at this point in the history
1. Schema Migration works without locking and crashing again
2. AUTHOR= and CATEGORY= searches

Closes surge-synthesizer#5253
  • Loading branch information
baconpaul committed Nov 19, 2021
1 parent 55b18c2 commit b4e1e6c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 20 deletions.
52 changes: 34 additions & 18 deletions src/common/PatchDB.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ struct Exception : public std::runtime_error
{
explicit Exception(sqlite3 *h) : std::runtime_error(sqlite3_errmsg(h)), rc(sqlite3_errcode(h))
{
// Surge::Debug::stackTraceToStdout();
Surge::Debug::stackTraceToStdout();
}
Exception(int rc, const std::string &msg) : std::runtime_error(msg), rc(rc)
{
Surge::Debug::stackTraceToStdout();
}
Exception(int rc, const std::string &msg) : std::runtime_error(msg), rc(rc) {}
const char *what() const noexcept override
{
static char msg[1024];
Expand Down Expand Up @@ -222,11 +225,7 @@ struct TxnGuard

struct PatchDB::WriterWorker
{
static constexpr const char *schema_version = "10"; // I will rebuild if this is not my verion

/*
* Obviously a lot of thought needs to go into this
*/
static constexpr const char *schema_version = "11"; // I will rebuild if this is not my verion

// language=SQL
static constexpr const char *setup_sql = R"SQL(
Expand Down Expand Up @@ -390,14 +389,17 @@ CREATE TABLE IF NOT EXISTS Favorites (
struct EnQSetup : EnQAble
{
EnQSetup() {}
void go(WriterWorker &w) { w.setupDatabase(); }
void go(WriterWorker &w)
{
w.setupDatabase();
std::cout << "Done with EnQSetup" << std::endl;
}
};

std::atomic<bool> hasSetup{false};
void setupDatabase()
{
std::cout << "PatchDB : Setup Database " << dbname << std::endl;
hasSetup = true;
/*
* OK check my version
*/
Expand Down Expand Up @@ -450,6 +452,8 @@ CREATE TABLE IF NOT EXISTS Favorites (
storage->reportError(e.what(), "PatchDB Setup Error");
}
}

hasSetup = true;
}

bool haveOpenedForWriteOnce{false};
Expand All @@ -466,7 +470,7 @@ CREATE TABLE IF NOT EXISTS Favorites (
pathQ.push_back(new EnQSetup());
}
qCV.notify_all();
while (!hasSetup)
while (!waiting)
{
}
}
Expand Down Expand Up @@ -585,6 +589,7 @@ CREATE TABLE IF NOT EXISTS Favorites (
/*
* Functions for the write thread
*/
std::atomic<bool> waiting{false};
void loadQueueFunction()
{
static constexpr auto transChunkSize = 10; // How many FXP to load in a single txn
Expand All @@ -599,7 +604,9 @@ CREATE TABLE IF NOT EXISTS Favorites (
{
if (dbh)
closeDb();
waiting = true;
qCV.wait(lk);
waiting = false;
}

if (keepRunning)
Expand Down Expand Up @@ -634,15 +641,12 @@ CREATE TABLE IF NOT EXISTS Favorites (
}
catch (SQL::LockedException &le)
{
std::cout << "LOCKED EXCEPTION" << std::endl;
storage->reportError(le.what(), "Patch DB");
// OK so in this case, we reload the doThis onto the front of the queue
// and sleep
lock_retries++;
if (lock_retries < 10)
{
std::cout << "Pushing and retrying - sleep for " << lock_retries * 3
<< std::endl;
{
std::unique_lock<std::mutex> lk(qLock);
std::reverse(doThis.begin(), doThis.end());
Expand Down Expand Up @@ -1347,7 +1351,18 @@ std::string PatchDB::sqlWhereClauseFor(const std::unique_ptr<PatchDBQueryParser:
oss << "(1 == 0)";
break;
case PatchDBQueryParser::KEYWORD_EQUALS:
oss << "(1 == 1)";
if (t->content == "AUTHOR" && !t->children[0]->content.empty())
{
oss << "(author LIKE '%" << t->children[0]->content << "%' )";
}
else if (t->content == "CATEGORY" && !t->children[0]->content.empty())
{
oss << "(category LIKE '%" << t->children[0]->content << "%' )";
}
else
{
oss << "(1 == 1)";
}
break;
case PatchDBQueryParser::LITERAL:
oss << "( p.name LIKE '%" << t->content << "%' )";
Expand Down Expand Up @@ -1377,10 +1392,11 @@ PatchDB::queryFromQueryString(const std::unique_ptr<PatchDBQueryParser::Token> &
std::vector<PatchDB::patchRecord> res;

// FIXME - cache this by pushing it to the worker
std::string query = "select p.id, p.path, p.category, p.name, pf.feature_svalue from Patches "
"as p, PatchFeature as pf where pf.patch_id == p.id and pf.feature LIKE "
"'AUTHOR' and " +
sqlWhereClauseFor(t) + " ORDER BY p.category_type, p.category, p.name";
std::string query =
"select p.id, p.path, p.category as category, p.name, pf.feature_svalue as author from Patches "
"as p, PatchFeature as pf where pf.patch_id == p.id and pf.feature LIKE "
"'AUTHOR' and " +
sqlWhereClauseFor(t) + " ORDER BY p.category_type, p.category, p.name";

// std::cout << "QUERY IS \n" << query << "\n";
try
Expand Down
8 changes: 6 additions & 2 deletions src/common/SurgeStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,12 @@ void SurgeStorage::initializePatchDb(bool force)

patchDBInitialized = true;

/*
* We do this here because if there is a schema upgrade we need to do it before
* we do a patch read, even though our next activity is a read
*/
patchDB->prepareForWrites();

auto awid = patchDB->readAllPatchPathsWithIdAndModTime();
std::vector<Patch> addThese;
for (const auto p : patch_list)
Expand All @@ -650,8 +656,6 @@ void SurgeStorage::initializePatchDb(bool force)
}
}

patchDB->prepareForWrites();

auto catToType = [this](int q) {
auto t = Surge::PatchStorage::PatchDB::CatType::FACTORY;
if (q >= firstThirdPartyCategory)
Expand Down

0 comments on commit b4e1e6c

Please sign in to comment.