-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
sqlite: Observer database changes through WAL hook
- Loading branch information
Showing
12 changed files
with
311 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,4 +94,5 @@ Api::_setup_db() { | |
for (const auto& cmd : setup_commands) { | ||
m_sqlite->exec(cmd); | ||
} | ||
m_sqlite->enable_wal(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,4 @@ | ||
#include "query_diff.hpp" | ||
#include <algorithm> | ||
|
||
namespace mx3 { namespace sqlite { | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
#include "query_monitor.hpp" | ||
|
||
namespace mx3 { | ||
namespace sqlite { | ||
|
||
class QueryMonitor::only_for_internal_use_by_make_shared_t {}; | ||
|
||
shared_ptr<QueryMonitor> QueryMonitor::create_shared(const shared_ptr<Db> & write_db) { | ||
return make_shared<QueryMonitor>(only_for_internal_use_by_make_shared_t{}, write_db); | ||
} | ||
|
||
QueryMonitor::QueryMonitor( | ||
only_for_internal_use_by_make_shared_t, | ||
const shared_ptr<Db> & write_db) : m_write_db(write_db) | ||
{ | ||
_install_hooks(); | ||
} | ||
|
||
|
||
QueryMonitor::~QueryMonitor() { | ||
m_write_db->update_hook(nullptr); | ||
m_write_db->commit_hook(nullptr); | ||
m_write_db->rollback_hook(nullptr); | ||
m_write_db->wal_hook(nullptr); | ||
} | ||
|
||
void QueryMonitor::listen_to_changes(const function<void()> & fn) { | ||
m_listeners.push_back(fn); | ||
} | ||
|
||
void QueryMonitor::_install_hooks() { | ||
m_write_db->update_hook([this] (Db::Change change) { | ||
this->_on_update(std::move(change)); | ||
}); | ||
m_write_db->commit_hook([this] () { | ||
return this->_on_commit(); | ||
}); | ||
m_write_db->rollback_hook([this] () { | ||
this->_on_rollback(); | ||
}); | ||
m_write_db->wal_hook([this] (const string& db_name, int pages) { | ||
this->_on_wal_commit(db_name, pages); | ||
}); | ||
} | ||
|
||
|
||
void QueryMonitor::_on_update(Db::Change) {} | ||
bool QueryMonitor::_on_commit() { | ||
return true; | ||
} | ||
void QueryMonitor::_on_rollback() {} | ||
bool QueryMonitor::_on_wal_commit(const string& db_name, int pages) { | ||
if (pages > 1000) { | ||
m_write_db->wal_checkpoint_v2(db_name, Checkpoint::PASSIVE); | ||
} | ||
// Grab size before iterating in case m_listeners is modified in a callback. | ||
// In actuality this should be posted to an event loop, but oh well - its only a demo app | ||
const size_t total = m_listeners.size(); | ||
for (size_t i=0; i < total; i++) { | ||
m_listeners[i](); | ||
} | ||
return true; | ||
} | ||
|
||
} } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
#pragma once | ||
#include "../sqlite/sqlite.hpp" | ||
|
||
namespace mx3 { | ||
namespace sqlite { | ||
|
||
/* This class listens to changes in WAL mode databases. | ||
* And will notify listeners when changes are made. | ||
*/ | ||
class QueryMonitor final { | ||
public: | ||
static shared_ptr<QueryMonitor> create_shared(const shared_ptr<Db> & write_db); | ||
~QueryMonitor(); | ||
void listen_to_changes(const function<void()> & fn); | ||
private: | ||
class only_for_internal_use_by_make_shared_t; | ||
public: | ||
// make_shared constructor | ||
QueryMonitor(only_for_internal_use_by_make_shared_t flag, const shared_ptr<Db> & write_db); | ||
private: | ||
void _install_hooks(); | ||
void _on_update(Db::Change); | ||
bool _on_commit(); | ||
void _on_rollback(); | ||
bool _on_wal_commit(const string& db_name, int pages); | ||
const shared_ptr<Db> m_write_db; | ||
vector<function<void()>> m_listeners; | ||
}; | ||
|
||
} } |
Oops, something went wrong.