From 4f536c3d6ea449b695552299cff5820b6f743561 Mon Sep 17 00:00:00 2001 From: casey langen Date: Sun, 2 Feb 2020 08:42:48 -0800 Subject: [PATCH] Added the ability to run queries directly, without going through the library. --- src/core/c_context.cpp | 7 +++++++ src/core/c_interface_wrappers.cpp | 9 ++++++--- src/core/library/LocalLibrary.h | 1 + src/core/musikcore_c.h | 5 +++-- src/core_c_demo/main.c | 9 +++++---- 5 files changed, 22 insertions(+), 9 deletions(-) diff --git a/src/core/c_context.cpp b/src/core/c_context.cpp index ca31fa339..254ac5f5f 100644 --- a/src/core/c_context.cpp +++ b/src/core/c_context.cpp @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -53,6 +54,7 @@ using namespace musik; using namespace musik::core; +using namespace musik::core::library;; using namespace musik::core::db::local; using namespace musik::core::audio; using namespace musik::core::sdk; @@ -165,6 +167,11 @@ mcsdk_export void mcsdk_context_init(mcsdk_context** context) { c->playback.opaque = internal->playback; c->library.opaque = internal->library.get(); + auto localLibrary = dynamic_cast(internal->library.get()); + if (localLibrary) { + c->db.opaque = localLibrary; + } + auto indexer = internal->library->Indexer(); auto indexer_internal = new mcsdk_svc_indexer_context_internal(); indexer_internal->indexer = indexer; diff --git a/src/core/c_interface_wrappers.cpp b/src/core/c_interface_wrappers.cpp index 223ef3dae..e57e09f2a 100644 --- a/src/core/c_interface_wrappers.cpp +++ b/src/core/c_interface_wrappers.cpp @@ -1252,16 +1252,18 @@ class mcsdk_db_wrapped_query: public LocalQueryBase { mcsdk_db_wrapped_query( mcsdk_svc_library library, const std::string& name, + void* user_context, mcsdk_svc_library_run_query_callback cb) { this->library = library; this->name = name; + this->user_context = user_context; this->cb = cb; } protected: virtual bool OnRun(musik::core::db::Connection& db) { - return cb(this->library, { &db }); + return cb(this->library, { &db }, this->user_context); } virtual std::string Name() { @@ -1271,11 +1273,12 @@ class mcsdk_db_wrapped_query: public LocalQueryBase { private: mcsdk_svc_library library; std::string name; + void* user_context; mcsdk_svc_library_run_query_callback cb; }; -mcsdk_export void mcsdk_svc_library_run_query(mcsdk_svc_library l, const char* name, mcsdk_svc_library_run_query_callback cb, mcsdk_svc_library_query_flag flags) { - LIBRARY(l)->Enqueue(std::make_shared(l, name, cb)); +mcsdk_export void mcsdk_svc_library_run_query(mcsdk_svc_library l, const char* name, void* user_context, mcsdk_svc_library_run_query_callback cb, mcsdk_svc_library_query_flag flags) { + LIBRARY(l)->Enqueue(std::make_shared(l, name, user_context, cb)); } mcsdk_export int mcsdk_svc_library_get_id(mcsdk_svc_library l) { diff --git a/src/core/library/LocalLibrary.h b/src/core/library/LocalLibrary.h index 13213a9e5..a586154f0 100644 --- a/src/core/library/LocalLibrary.h +++ b/src/core/library/LocalLibrary.h @@ -82,6 +82,7 @@ namespace musik { namespace core { namespace library { virtual void ProcessMessage(musik::core::runtime::IMessage &message) override; /* implementation specific */ + db::Connection& GetConnection() { return this->db; } std::string GetLibraryDirectory(); std::string GetDatabaseFilename(); static void CreateDatabase(db::Connection &db); diff --git a/src/core/musikcore_c.h b/src/core/musikcore_c.h index 88a334def..72408d688 100644 --- a/src/core/musikcore_c.h +++ b/src/core/musikcore_c.h @@ -281,7 +281,7 @@ typedef struct mcsdk_audio_player_gain { float peakValid; } mcsdk_audio_player_gain; -typedef bool (*mcsdk_svc_library_run_query_callback)(mcsdk_svc_library l, mcsdk_db_connection db); +typedef bool (*mcsdk_svc_library_run_query_callback)(mcsdk_svc_library l, mcsdk_db_connection db, void* user_context); /* * instance context @@ -292,6 +292,7 @@ typedef struct mcsdk_context { mcsdk_svc_playback playback; mcsdk_svc_indexer indexer; mcsdk_svc_library library; + mcsdk_db_connection db; mcsdk_prefs preferences; mcsdk_internal internal; } mcsdk_context; @@ -654,7 +655,7 @@ mcsdk_export void mcsdk_svc_indexer_remove_callbacks(mcsdk_svc_indexer in, mcsdk * ILibrary */ -mcsdk_export void mcsdk_svc_library_run_query(mcsdk_svc_library l, const char* name, mcsdk_svc_library_run_query_callback cb, mcsdk_svc_library_query_flag flags); +mcsdk_export void mcsdk_svc_library_run_query(mcsdk_svc_library l, const char* name, void* user_context, mcsdk_svc_library_run_query_callback cb, mcsdk_svc_library_query_flag flags); mcsdk_export int mcsdk_svc_library_get_id(mcsdk_svc_library l); mcsdk_export int mcsdk_svc_library_get_name(mcsdk_svc_library l, char* dst, int len); diff --git a/src/core_c_demo/main.c b/src/core_c_demo/main.c index 44a789cb7..48b06e335 100644 --- a/src/core_c_demo/main.c +++ b/src/core_c_demo/main.c @@ -52,16 +52,16 @@ static void indexer_finished_callback(mcsdk_svc_indexer in, int total_updated_co printf("[indexer_finished_callback] %d\n", total_updated_count); } -static bool run_test_query_callback(mcsdk_svc_library library, mcsdk_db_connection connection) { +static bool run_test_query_callback(mcsdk_svc_library library, mcsdk_db_connection connection, void* user_context) { bool result = false; mcsdk_db_statement stmt = mcsdk_db_statement_create(connection, "SELECT COUNT(*) FROM tracks"); if (mcsdk_db_statement_step(stmt) == mcsdk_db_result_row) { int total_tracks = mcsdk_db_statement_column_int64(stmt, 0); - printf("[run_test_query_callback] success! %d total tracks", total_tracks); + printf("[run_test_query_callback] success! %d total tracks\n", total_tracks); result = true; } else { - printf("[run_test_query_callback] failed"); + printf("[run_test_query_callback] failed\n"); } mcsdk_db_statement_release(stmt); return result; @@ -181,6 +181,7 @@ static void test_library(mcsdk_context* context) { mcsdk_svc_library_run_query( context->library, "test query", + NULL, run_test_query_callback, mcsdk_svc_library_query_flag_synchronous); } @@ -203,9 +204,9 @@ int main(int argc, char** argv) { mcsdk_context_init(&context); if (context) { printf("[main] context initialized\n"); + test_library(context); test_indexer(context); test_metadata(context); - test_library(context); test_low_level_playback(); test_high_level_playback(context); test_decode_encode();