Add c-style DB interface and init/close DB when running daemon.
Signed-off-by: Jaeyun Jung <jy1210.jung@samsung.com>
#include "log.h"
#include "dbus-interface.h"
#include "pkg-mgr.h"
+#include "service-db-util.h"
static GMainLoop *g_mainloop = NULL;
static gboolean verbose = FALSE;
int
main (int argc, char **argv)
{
+ int ret = 0;
+
if (parse_args (&argc, &argv)) {
- return -EINVAL;
+ ret = -EINVAL;
+ goto error;
}
+ /* path to database */
+ svcdb_initialize (DB_PATH);
+
g_mainloop = g_main_loop_new (NULL, FALSE);
gdbus_get_system_connection (is_session);
if (pkg_mgr_deinit () < 0)
ml_logw ("cannot finalize package manager");
- return 0;
+error:
+ svcdb_finalize ();
+
+ is_session = verbose = FALSE;
+ return ret;
}
ml_agent_lib = ml_agent_static_lib
endif
+ml_agent_dep = declare_dependency(
+ dependencies: ml_agent_deps,
+ include_directories: ml_agent_incs,
+ link_with: ml_agent_lib
+)
+
ml_agent_main_file = files('main.c')
ml_agent_executable = executable('machine-learning-agent',
ml_agent_main_file,
- link_with: ml_agent_lib,
- dependencies: ml_agent_deps,
- include_directories: ml_agent_incs,
+ dependencies: ml_agent_dep,
install: true,
install_dir: ml_agent_install_bindir,
+ c_args: [ml_agent_db_path_arg, ml_agent_db_key_prefix_arg],
pie: true
)
#include "log.h"
#include "model-dbus.h"
#include "modules.h"
-#include "service-db.hh"
+#include "service-db-util.h"
static MachinelearningServiceModel *g_gdbus_instance = NULL;
GDBusMethodInvocation *invoc, const gchar *name, const gchar *path,
const bool is_active, const gchar *description, const gchar *app_info)
{
- int ret = 0;
+ gint ret = 0;
guint version = 0U;
- MLServiceDB &db = MLServiceDB::getInstance ();
-
- try {
- db.connectDB ();
- db.set_model (name, path, is_active, description, app_info, &version);
- } catch (const std::invalid_argument &e) {
- ml_loge ("%s", e.what ());
- ret = -EINVAL;
- } catch (const std::exception &e) {
- ml_loge ("%s", e.what ());
- ret = -EIO;
- }
- db.disconnectDB ();
+ ret = svcdb_model_add (name, path, is_active, description, app_info, &version);
machinelearning_service_model_complete_register (obj, invoc, version, ret);
return TRUE;
GDBusMethodInvocation *invoc, const gchar *name, const guint version,
const gchar *description)
{
- int ret = 0;
- MLServiceDB &db = MLServiceDB::getInstance ();
-
- try {
- db.connectDB ();
- db.update_model_description (name, version, description);
- } catch (const std::invalid_argument &e) {
- ml_loge ("%s", e.what ());
- ret = -EINVAL;
- } catch (const std::exception &e) {
- ml_loge ("%s", e.what ());
- ret = -EIO;
- }
+ gint ret = 0;
- db.disconnectDB ();
+ ret = svcdb_model_update_description (name, version, description);
machinelearning_service_model_complete_update_description (obj, invoc, ret);
return TRUE;
gdbus_cb_model_activate (MachinelearningServiceModel *obj,
GDBusMethodInvocation *invoc, const gchar *name, const guint version)
{
- int ret = 0;
- MLServiceDB &db = MLServiceDB::getInstance ();
-
- try {
- db.connectDB ();
- db.activate_model (name, version);
- } catch (const std::invalid_argument &e) {
- ml_loge ("%s", e.what ());
- ret = -EINVAL;
- } catch (const std::exception &e) {
- ml_loge ("%s", e.what ());
- ret = -EIO;
- }
+ gint ret = 0;
- db.disconnectDB ();
+ ret = svcdb_model_activate (name, version);
machinelearning_service_model_complete_activate (obj, invoc, ret);
return TRUE;
gdbus_cb_model_get (MachinelearningServiceModel *obj,
GDBusMethodInvocation *invoc, const gchar *name, const guint version)
{
- int ret = 0;
- std::string model_info;
- MLServiceDB &db = MLServiceDB::getInstance ();
-
- try {
- db.connectDB ();
- db.get_model (name, model_info, version);
- } catch (const std::invalid_argument &e) {
- ml_loge ("%s", e.what ());
- ret = -EINVAL;
- } catch (const std::exception &e) {
- ml_loge ("%s", e.what ());
- ret = -EIO;
- }
+ gint ret = 0;
+ g_autofree gchar *model_info = NULL;
- db.disconnectDB ();
- machinelearning_service_model_complete_get (obj, invoc, model_info.c_str (), ret);
+ ret = svcdb_model_get (name, version, &model_info);
+ machinelearning_service_model_complete_get (obj, invoc, model_info, ret);
return TRUE;
}
gdbus_cb_model_get_activated (MachinelearningServiceModel *obj,
GDBusMethodInvocation *invoc, const gchar *name)
{
- int ret = 0;
- std::string model_info;
- MLServiceDB &db = MLServiceDB::getInstance ();
-
- try {
- db.connectDB ();
- db.get_model (name, model_info, -1);
- } catch (const std::invalid_argument &e) {
- ml_loge ("%s", e.what ());
- ret = -EINVAL;
- } catch (const std::exception &e) {
- ml_loge ("%s", e.what ());
- ret = -EIO;
- }
+ gint ret = 0;
+ g_autofree gchar *model_info = NULL;
- db.disconnectDB ();
- machinelearning_service_model_complete_get_activated (
- obj, invoc, model_info.c_str (), ret);
+ ret = svcdb_model_get_activated (name, &model_info);
+ machinelearning_service_model_complete_get_activated (obj, invoc, model_info, ret);
return TRUE;
}
gdbus_cb_model_get_all (MachinelearningServiceModel *obj,
GDBusMethodInvocation *invoc, const gchar *name)
{
- int ret = 0;
- MLServiceDB &db = MLServiceDB::getInstance ();
- std::string all_model_list;
-
- try {
- db.connectDB ();
- db.get_model (name, all_model_list, 0);
- } catch (const std::invalid_argument &e) {
- ml_loge ("%s", e.what ());
- ret = -EINVAL;
- } catch (const std::exception &e) {
- ml_loge ("%s", e.what ());
- ret = -EIO;
- }
+ gint ret = 0;
+ g_autofree gchar *model_info = NULL;
- db.disconnectDB ();
-
- machinelearning_service_model_complete_get_all (obj, invoc, all_model_list.c_str (), ret);
+ ret = svcdb_model_get_all (name, &model_info);
+ machinelearning_service_model_complete_get_all (obj, invoc, model_info, ret);
return TRUE;
}
gdbus_cb_model_delete (MachinelearningServiceModel *obj,
GDBusMethodInvocation *invoc, const gchar *name, const guint version)
{
- int ret = 0;
- MLServiceDB &db = MLServiceDB::getInstance ();
-
- try {
- db.connectDB ();
- db.delete_model (name, version);
- } catch (const std::invalid_argument &e) {
- ml_loge ("%s", e.what ());
- ret = -EINVAL;
- } catch (const std::exception &e) {
- ml_loge ("%s", e.what ());
- ret = -EIO;
- }
+ gint ret = 0;
- db.disconnectDB ();
+ ret = svcdb_model_delete (name, version);
machinelearning_service_model_complete_delete (obj, invoc, ret);
return TRUE;
#include "log.h"
#include "modules.h"
#include "pipeline-dbus.h"
-#include "service-db.hh"
+#include "service-db-util.h"
static MachinelearningServicePipeline *g_gdbus_instance = NULL;
static GHashTable *pipeline_table = NULL;
const gchar *service_name, const gchar *pipeline_desc, gpointer user_data)
{
gint result = 0;
- MLServiceDB &db = MLServiceDB::getInstance ();
-
- try {
- db.connectDB ();
- db.set_pipeline (service_name, pipeline_desc);
- } catch (const std::invalid_argument &e) {
- ml_loge ("An exception occurred during write to the DB. Error message: %s", e.what ());
- result = -EINVAL;
- } catch (const std::exception &e) {
- ml_loge ("An exception occurred during write to the DB. Error message: %s", e.what ());
- result = -EIO;
- }
-
- db.disconnectDB ();
-
- if (result) {
- ml_loge ("Failed to set pipeline description of %s", service_name);
- machinelearning_service_pipeline_complete_set_pipeline (obj, invoc, result);
- return TRUE;
- }
+ result = svcdb_pipeline_set (service_name, pipeline_desc);
machinelearning_service_pipeline_complete_set_pipeline (obj, invoc, result);
return TRUE;
GDBusMethodInvocation *invoc, const gchar *service_name, gpointer user_data)
{
gint result = 0;
- std::string stored_pipeline_description;
- MLServiceDB &db = MLServiceDB::getInstance ();
-
- try {
- db.connectDB ();
- db.get_pipeline (service_name, stored_pipeline_description);
- } catch (const std::invalid_argument &e) {
- ml_loge ("An exception occurred during read the DB. Error message: %s", e.what ());
- result = -EINVAL;
- } catch (const std::exception &e) {
- ml_loge ("An exception occurred during read the DB. Error message: %s", e.what ());
- result = -EIO;
- }
+ g_autofree gchar *desc = NULL;
- db.disconnectDB ();
-
- if (result) {
- ml_loge ("Failed to get pipeline description of %s", service_name);
- }
-
- machinelearning_service_pipeline_complete_get_pipeline (
- obj, invoc, result, stored_pipeline_description.c_str ());
+ result = svcdb_pipeline_get (service_name, &desc);
+ machinelearning_service_pipeline_complete_get_pipeline (obj, invoc, result, desc);
return TRUE;
}
GDBusMethodInvocation *invoc, const gchar *service_name, gpointer user_data)
{
gint result = 0;
- MLServiceDB &db = MLServiceDB::getInstance ();
-
- try {
- db.connectDB ();
- db.delete_pipeline (service_name);
- } catch (const std::invalid_argument &e) {
- ml_loge ("An exception occurred during delete an item in the DB. Error message: %s",
- e.what ());
- result = -EINVAL;
- } catch (const std::exception &e) {
- ml_loge ("An exception occurred during delete an item in the DB. Error message: %s",
- e.what ());
- result = -EIO;
- }
-
- db.disconnectDB ();
-
- if (result) {
- ml_loge ("Failed to delete the pipeline description of %s", service_name);
- machinelearning_service_pipeline_complete_delete_pipeline (obj, invoc, result);
- return TRUE;
- }
+ result = svcdb_pipeline_delete (service_name);
machinelearning_service_pipeline_complete_delete_pipeline (obj, invoc, result);
return TRUE;
GDBusMethodInvocation *invoc, const gchar *service_name, gpointer user_data)
{
gint result = 0;
+ gint64 id = -1;
GError *err = NULL;
GstStateChangeReturn sc_ret;
GstElement *pipeline = NULL;
pipeline_s *p;
+ g_autofree gchar *desc = NULL;
- MLServiceDB &db = MLServiceDB::getInstance ();
- std::string stored_pipeline_description;
-
- /** get pipeline description from the DB */
- try {
- db.connectDB ();
- db.get_pipeline (service_name, stored_pipeline_description);
- } catch (const std::invalid_argument &e) {
- ml_loge ("An exception occurred during read the DB. Error message: %s", e.what ());
- result = -EINVAL;
- } catch (const std::exception &e) {
- ml_loge ("An exception occurred during read the DB. Error message: %s", e.what ());
- result = -EIO;
- }
-
- db.disconnectDB ();
-
- if (result) {
- ml_loge ("Failed to launch pipeline of %s", service_name);
- machinelearning_service_pipeline_complete_launch_pipeline (obj, invoc, result, -1);
- return TRUE;
+ result = svcdb_pipeline_get (service_name, &desc);
+ if (result != 0) {
+ ml_loge ("Failed to launch pipeline of '%s'.", service_name);
+ goto error;
}
- pipeline = gst_parse_launch (stored_pipeline_description.c_str (), &err);
+ pipeline = gst_parse_launch (desc, &err);
if (!pipeline || err) {
- ml_loge ("gst_parse_launch with %s Failed. error msg: %s",
- stored_pipeline_description.c_str (), (err) ? err->message : "unknown reason");
+ ml_loge ("Failed to launch pipeline '%s' (error msg: %s).",
+ desc, (err) ? err->message : "unknown reason");
g_clear_error (&err);
if (pipeline)
gst_object_unref (pipeline);
result = -ESTRPIPE;
- machinelearning_service_pipeline_complete_launch_pipeline (obj, invoc, result, -1);
- return TRUE;
+ goto error;
}
/** now set pipeline as paused state */
sc_ret = gst_element_set_state (pipeline, GST_STATE_PAUSED);
if (sc_ret == GST_STATE_CHANGE_FAILURE) {
- ml_loge ("Failed to set the state of the pipeline to PAUSED. For the detail, please check the GStreamer log message. The input pipeline was %s",
- stored_pipeline_description.c_str ());
+ ml_loge ("Failed to set the state of the pipeline to PAUSED. For the detail, please check the GStreamer log message. The input pipeline was '%s'.",
+ desc);
gst_object_unref (pipeline);
result = -ESTRPIPE;
- machinelearning_service_pipeline_complete_launch_pipeline (obj, invoc, result, -1);
- return TRUE;
+ goto error;
}
/** now fill the struct and store into hash table */
p = g_new0 (pipeline_s, 1);
p->element = pipeline;
- p->description = g_strdup (stored_pipeline_description.c_str ());
+ p->description = g_strdup (desc);
p->service_name = g_strdup (service_name);
g_mutex_init (&p->lock);
G_LOCK (pipeline_table_lock);
- p->id = g_get_monotonic_time ();
+ id = p->id = g_get_monotonic_time ();
g_hash_table_insert (pipeline_table, GINT_TO_POINTER (p->id), p);
G_UNLOCK (pipeline_table_lock);
- machinelearning_service_pipeline_complete_launch_pipeline (obj, invoc, result, p->id);
+error:
+ machinelearning_service_pipeline_complete_launch_pipeline (obj, invoc, result, id);
return TRUE;
}
if (sc_ret == GST_STATE_CHANGE_FAILURE) {
ml_loge ("Failed to get the state of the pipline whose service name is %s.", p->service_name);
result = -ESTRPIPE;
- machinelearning_service_pipeline_complete_get_state (obj, invoc, result, (gint) state);
- return TRUE;
}
machinelearning_service_pipeline_complete_get_state (obj, invoc, result, (gint) state);
#include <stdio.h>
#include "pkg-mgr.h"
-#include "service-db.hh"
+#include "service-db-util.h"
/**
* @brief Internal enumeration for data types of json.
_parse_json (const gchar *json_path, mlsvc_json_type_e json_type, const gchar *app_info)
{
g_autofree gchar *json_file = NULL;
+ gint ret;
switch (json_type) {
case MLSVC_JSON_MODEL:
}
/* Update ML service database. */
- MLServiceDB &db = MLServiceDB::getInstance ();
- try {
- db.connectDB ();
-
- for (guint i = 0; i < json_len; ++i) {
- if (array)
- object = json_array_get_object_element (array, i);
- else
- object = json_node_get_object (root);
-
- switch (json_type) {
- case MLSVC_JSON_MODEL:
- {
- const gchar *name = json_object_get_string_member (object, "name");
- const gchar *model = json_object_get_string_member (object, "model");
- const gchar *desc = json_object_get_string_member (object, "description");
- const gchar *activate = json_object_get_string_member (object, "activate");
- const gchar *clear = json_object_get_string_member (object, "clear");
-
- if (!name || !model) {
- ml_loge ("Failed to get name or model from json file '%s'.", json_file);
- continue;
- }
-
- guint version;
- bool active = (activate && g_ascii_strcasecmp (activate, "true") == 0);
- bool clear_old = (clear && g_ascii_strcasecmp (clear, "true") == 0);
-
- /* Remove old model from database. */
- if (clear_old) {
- try {
- db.delete_model (name, 0U);
- } catch (const std::exception &e) {
- /* Ignore error case. */
- ml_logw ("%s", e.what ());
- }
- }
-
- db.set_model (name, model, active, desc ? desc : "",
- app_info ? app_info : "", &version);
+ for (guint i = 0; i < json_len; ++i) {
+ if (array)
+ object = json_array_get_object_element (array, i);
+ else
+ object = json_node_get_object (root);
+
+ switch (json_type) {
+ case MLSVC_JSON_MODEL:
+ {
+ const gchar *name = json_object_get_string_member (object, "name");
+ const gchar *model = json_object_get_string_member (object, "model");
+ const gchar *desc = json_object_get_string_member (object, "description");
+ const gchar *activate = json_object_get_string_member (object, "activate");
+ const gchar *clear = json_object_get_string_member (object, "clear");
+
+ if (!name || !model) {
+ ml_loge ("Failed to get name or model from json file '%s'.", json_file);
+ continue;
+ }
- ml_logi ("The model with name '%s' is registered as version '%u'.", name, version);
+ guint version;
+ bool active = (activate && g_ascii_strcasecmp (activate, "true") == 0);
+ bool clear_old = (clear && g_ascii_strcasecmp (clear, "true") == 0);
+
+ /* Remove old model from database. */
+ if (clear_old) {
+ /* Ignore error case. */
+ svcdb_model_delete (name, 0U);
}
- break;
- case MLSVC_JSON_PIPELINE:
- {
- const gchar *name = json_object_get_string_member (object, "name");
- const gchar *desc = json_object_get_string_member (object, "description");
- if (!name || !desc) {
- ml_loge ("Failed to get name or description from json file '%s'.", json_file);
- continue;
- }
+ ret = svcdb_model_add (name, model, active, desc ? desc : "",
+ app_info ? app_info : "", &version);
- db.set_pipeline (name, desc);
+ if (ret == 0)
+ ml_logi ("The model with name '%s' is registered as version '%u'.", name, version);
+ else
+ ml_loge ("Failed to register the model with name '%s'.", name);
+ }
+ break;
+ case MLSVC_JSON_PIPELINE:
+ {
+ const gchar *name = json_object_get_string_member (object, "name");
+ const gchar *desc = json_object_get_string_member (object, "description");
+
+ if (!name || !desc) {
+ ml_loge ("Failed to get name or description from json file '%s'.", json_file);
+ continue;
+ }
+
+ ret = svcdb_pipeline_set (name, desc);
+ if (ret == 0)
ml_logi ("The pipeline description with name '%s' is registered.", name);
+ else
+ ml_loge ("Failed to register pipeline with name '%s'.", name);
+ }
+ break;
+ case MLSVC_JSON_RESOURCE:
+ {
+ const gchar *name = json_object_get_string_member (object, "name");
+ const gchar *path = json_object_get_string_member (object, "path");
+ const gchar *desc = json_object_get_string_member (object, "description");
+ const gchar *clear = json_object_get_string_member (object, "clear");
+
+ if (!name || !path) {
+ ml_loge ("Failed to get name or path from json file '%s'.", json_file);
+ continue;
}
- break;
- case MLSVC_JSON_RESOURCE:
- {
- const gchar *name = json_object_get_string_member (object, "name");
- const gchar *path = json_object_get_string_member (object, "path");
- const gchar *desc = json_object_get_string_member (object, "description");
- const gchar *clear = json_object_get_string_member (object, "clear");
-
- if (!name || !path) {
- ml_loge ("Failed to get name or path from json file '%s'.", json_file);
- continue;
- }
-
- bool clear_old = (clear && g_ascii_strcasecmp (clear, "true") == 0);
-
- /* Remove old resource from database. */
- if (clear_old) {
- try {
- db.delete_resource (name);
- } catch (const std::exception &e) {
- /* Ignore error case. */
- ml_logw ("%s", e.what ());
- }
- }
-
- db.set_resource (name, path, desc ? desc : "", app_info ? app_info : "");
- ml_logi ("The resource with name '%s' is registered.", name);
+ bool clear_old = (clear && g_ascii_strcasecmp (clear, "true") == 0);
+
+ /* Remove old resource from database. */
+ if (clear_old) {
+ /* Ignore error case. */
+ svcdb_resource_delete (name);
}
- break;
- default:
- ml_loge ("Unknown data type '%d', internal error?", json_type);
- break;
- }
+
+ ret = svcdb_resource_add (name, path, desc ? desc : "", app_info ? app_info : "");
+
+ if (ret == 0)
+ ml_logi ("The resource with name '%s' is registered.", name);
+ else
+ ml_loge ("Failed to register the resource with name '%s'.", name);
+ }
+ break;
+ default:
+ ml_loge ("Unknown data type '%d', internal error?", json_type);
+ break;
}
- } catch (const std::exception &e) {
- ml_loge ("%s", e.what ());
}
-
- db.disconnectDB ();
}
/**
#include "log.h"
#include "modules.h"
#include "resource-dbus.h"
-#include "service-db.hh"
+#include "service-db-util.h"
static MachinelearningServiceResource *g_gdbus_res_instance = NULL;
gdbus_cb_resource_add (MachinelearningServiceResource *obj, GDBusMethodInvocation *invoc,
const gchar *name, const gchar *path, const gchar *description, const gchar *app_info)
{
- int ret = 0;
- MLServiceDB &db = MLServiceDB::getInstance ();
-
- try {
- db.connectDB ();
- db.set_resource (name, path, description, app_info);
- } catch (const std::invalid_argument &e) {
- ml_loge ("%s", e.what ());
- ret = -EINVAL;
- } catch (const std::exception &e) {
- ml_loge ("%s", e.what ());
- ret = -EIO;
- }
+ gint ret = 0;
- db.disconnectDB ();
+ ret = svcdb_resource_add (name, path, description, app_info);
machinelearning_service_resource_complete_add (obj, invoc, ret);
return TRUE;
gdbus_cb_resource_get (MachinelearningServiceResource *obj,
GDBusMethodInvocation *invoc, const gchar *name)
{
- int ret = 0;
- std::string res_info;
- MLServiceDB &db = MLServiceDB::getInstance ();
-
- try {
- db.connectDB ();
- db.get_resource (name, res_info);
- } catch (const std::invalid_argument &e) {
- ml_loge ("%s", e.what ());
- ret = -EINVAL;
- } catch (const std::exception &e) {
- ml_loge ("%s", e.what ());
- ret = -EIO;
- }
+ gint ret = 0;
+ g_autofree gchar *res_info = NULL;
- db.disconnectDB ();
- machinelearning_service_resource_complete_get (obj, invoc, res_info.c_str (), ret);
+ ret = svcdb_resource_get (name, &res_info);
+ machinelearning_service_resource_complete_get (obj, invoc, res_info, ret);
return TRUE;
}
gdbus_cb_resource_delete (MachinelearningServiceResource *obj,
GDBusMethodInvocation *invoc, const gchar *name)
{
- int ret = 0;
- MLServiceDB &db = MLServiceDB::getInstance ();
-
- try {
- db.connectDB ();
- db.delete_resource (name);
- } catch (const std::invalid_argument &e) {
- ml_loge ("%s", e.what ());
- ret = -EINVAL;
- } catch (const std::exception &e) {
- ml_loge ("%s", e.what ());
- ret = -EIO;
- }
+ gint ret = 0;
- db.disconnectDB ();
+ ret = svcdb_resource_delete (name);
machinelearning_service_resource_complete_delete (obj, invoc, ret);
return TRUE;
--- /dev/null
+/* SPDX-License-Identifier: Apache-2.0 */
+/**
+ * Copyright (c) 2022 Samsung Electronics Co., Ltd. All Rights Reserved.
+ *
+ * @file service-db-util.h
+ * @date 28 Mar 2022
+ * @brief NNStreamer/Service Database Interface
+ * @see https://github.com/nnstreamer/deviceMLOps.MLAgent
+ * @author Sangjung Woo <sangjung.woo@samsung.com>
+ * @bug No known bugs except for NYI items
+ */
+
+#ifndef __SERVICE_DB_UTIL_H__
+#define __SERVICE_DB_UTIL_H__
+
+#include <glib.h>
+
+G_BEGIN_DECLS
+
+void svcdb_initialize (const gchar *path);
+void svcdb_finalize (void);
+gint svcdb_pipeline_set (const gchar *name, const gchar *description);
+gint svcdb_pipeline_get (const gchar *name, gchar **description);
+gint svcdb_pipeline_delete (const gchar *name);
+gint svcdb_model_add (const gchar *name, const gchar *path, const bool is_active, const gchar *description, const gchar *app_info, guint *version);
+gint svcdb_model_update_description (const gchar *name, const guint version, const gchar *description);
+gint svcdb_model_activate (const gchar *name, const guint version);
+gint svcdb_model_get (const gchar *name, const guint version, gchar **model_info);
+gint svcdb_model_get_activated (const gchar *name, gchar **model_info);
+gint svcdb_model_get_all (const gchar *name, gchar **model_info);
+gint svcdb_model_delete (const gchar *name, const guint version);
+gint svcdb_resource_add (const gchar *name, const gchar *path, const gchar *description, const gchar *app_info);
+gint svcdb_resource_get (const gchar *name, gchar **res_info);
+gint svcdb_resource_delete (const gchar *name);
+
+G_END_DECLS
+#endif /* __SERVICE_DB_UTIL_H__ */
*/
#include "service-db.hh"
+#include "service-db-util.h"
#include "log.h"
#define sqlite3_clear_errmsg(m) \
} \
} while (0)
-#define ML_DATABASE_PATH DB_PATH "/.ml-service.db"
-
/**
* @brief The version of pipeline table schema. It should be a positive integer.
*/
const char **g_mlsvc_table_schema = g_mlsvc_table_schema_v1;
-/**
- * @brief Get an instance of MLServiceDB, which is created only once at runtime.
- * @return MLServiceDB& MLServiceDB instance
- */
-MLServiceDB &
-MLServiceDB::getInstance (void)
-{
- static MLServiceDB instance (ML_DATABASE_PATH);
-
- return instance;
-}
-
/**
* @brief Construct a new MLServiceDB object.
* @param path database path
if (_db != nullptr)
return;
- rc = sqlite3_open (_path.c_str (), &_db);
+ g_autofree gchar *db_path = g_strdup_printf ("%s/.ml-service.db", _path.c_str ());
+ rc = sqlite3_open (db_path, &_db);
if (rc != SQLITE_OK) {
- ml_loge ("Failed to open database: %s (%d)", sqlite3_errmsg (_db), rc);
+ ml_loge ("Failed to open database: %s (ret: %d, path: %s)",
+ sqlite3_errmsg (_db), rc, _path.c_str ());
goto error;
}
* @param[out] description The pipeline corresponding with the given name.
*/
void
-MLServiceDB::get_pipeline (const std::string name, std::string &description)
+MLServiceDB::get_pipeline (const std::string name, gchar **description)
{
char *value = nullptr;
sqlite3_stmt *res;
- if (name.empty ())
- throw std::invalid_argument ("Invalid name parameters!");
+ if (name.empty () || !description)
+ throw std::invalid_argument ("Invalid name or description parameter!");
std::string key_with_prefix = DB_KEY_PREFIX + std::string ("_pipeline_");
key_with_prefix += name;
sqlite3_finalize (res);
if (value) {
- description = std::string (value);
- g_free (value);
+ *description = value;
} else {
throw std::invalid_argument ("Failed to get pipeline description of " + name);
}
/**
* @brief Delete the pipeline description with a given name.
- * @param[in] name The unique name to delete
+ * @param[in] name The unique name to delete.
*/
void
MLServiceDB::delete_pipeline (const std::string name)
* @param[in] model The model to be stored.
* @param[in] is_active The model is active or not.
* @param[in] description The model description.
+ * @param[in] app_info The application information.
* @param[out] version The version of the model.
*/
void
/**
* @brief Get the model with the given name.
* @param[in] name The unique name to retrieve.
- * @param[out] model The model corresponding with the given name.
* @param[in] version The version of the model. If it is 0, all models will return, if it is -1, return the active model.
+ * @param[out] model The model corresponding with the given name.
*/
void
-MLServiceDB::get_model (const std::string name, std::string &model, const gint version)
+MLServiceDB::get_model (const std::string name, const gint version, gchar **model)
{
const char model_info_json[]
= "json_object('version', CAST(version AS TEXT), 'active', active, 'path', path, 'description', description, 'app_info', app_info)";
char *value = nullptr;
sqlite3_stmt *res;
- if (name.empty ())
- throw std::invalid_argument ("Invalid name parameters!");
+ if (name.empty () || !model)
+ throw std::invalid_argument ("Invalid name or model parameters!");
std::string key_with_prefix = DB_KEY_PREFIX + std::string ("_model_");
key_with_prefix += name;
g_free (sql);
if (value) {
- model = std::string (value);
- g_free (value);
+ *model = value;
} else {
throw std::invalid_argument ("Failed to get model with name " + name
+ " and version " + std::to_string (version));
/**
* @brief Delete the model.
- * @param[in] name The unique name to delete
- * @param[in] version The version of the model to delete
+ * @param[in] name The unique name to delete.
+ * @param[in] version The version of the model to delete.
*/
void
MLServiceDB::delete_model (const std::string name, const guint version)
* @param[in] name Unique name of ml-resource.
* @param[in] path The path to be stored.
* @param[in] description The description for ml-resource.
+ * @param[in] app_info The application information.
*/
void
MLServiceDB::set_resource (const std::string name, const std::string path,
* @param[out] resource The resource corresponding with the given name.
*/
void
-MLServiceDB::get_resource (const std::string name, std::string &resource)
+MLServiceDB::get_resource (const std::string name, gchar **resource)
{
const char res_info_json[]
= "json_object('path', path, 'description', description, 'app_info', app_info)";
char *value = nullptr;
sqlite3_stmt *res;
- if (name.empty ())
- throw std::invalid_argument ("Invalid name parameters!");
+ if (name.empty () || !resource)
+ throw std::invalid_argument ("Invalid name or resource parameters!");
std::string key_with_prefix = DB_KEY_PREFIX + std::string ("_resource_");
key_with_prefix += name;
if (!value)
throw std::invalid_argument ("Failed to get resource with name " + name);
- resource = std::string (value);
- g_free (value);
+ *resource = value;
}
/**
* @brief Delete the resource.
- * @param[in] name The unique name to delete
+ * @param[in] name The unique name to delete.
*/
void
MLServiceDB::delete_resource (const std::string name)
if (sqlite3_changes (_db) == 0)
throw std::invalid_argument ("There is no resource with name " + name);
}
+
+static MLServiceDB *g_svcdb_instance = nullptr;
+
+/**
+ * @brief Get the service-db instance.
+ */
+static MLServiceDB *
+svcdb_get (void)
+{
+ g_assert (g_svcdb_instance);
+ return g_svcdb_instance;
+}
+
+G_BEGIN_DECLS
+/**
+ * @brief Initialize the service-db.
+ */
+void
+svcdb_initialize (const gchar *path)
+{
+ if (g_svcdb_instance) {
+ ml_logw ("ML service DB is already opened, close old DB.");
+ delete g_svcdb_instance;
+ }
+
+ g_svcdb_instance = new MLServiceDB (path);
+ g_svcdb_instance->connectDB ();
+}
+
+/**
+ * @brief Close the service-db.
+ */
+void
+svcdb_finalize (void)
+{
+ if (g_svcdb_instance) {
+ g_svcdb_instance->disconnectDB ();
+ delete g_svcdb_instance;
+ }
+
+ g_svcdb_instance = nullptr;
+}
+
+/**
+ * @brief Set the pipeline description with given name.
+ * @note If the name already exists, the pipeline description is overwritten.
+ * @param[in] name Unique name to set the associated pipeline description.
+ * @param[in] description The pipeline description to be stored.
+ * @return @c 0 on success. Otherwise a negative error value.
+ */
+gint
+svcdb_pipeline_set (const gchar *name, const gchar *description)
+{
+ gint ret = 0;
+ MLServiceDB *db = svcdb_get ();
+
+ try {
+ db->set_pipeline (name, description);
+ } catch (const std::invalid_argument &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EINVAL;
+ } catch (const std::exception &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EIO;
+ }
+
+ return ret;
+}
+
+/**
+ * @brief Get the pipeline description with given name.
+ * @param[in] name The unique name to retrieve.
+ * @param[out] description The pipeline corresponding with given name.
+ * @return @c 0 on success. Otherwise a negative error value.
+ */
+gint
+svcdb_pipeline_get (const gchar *name, gchar **description)
+{
+ gint ret = 0;
+ MLServiceDB *db = svcdb_get ();
+
+ try {
+ db->get_pipeline (name, description);
+ } catch (const std::invalid_argument &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EINVAL;
+ } catch (const std::exception &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EIO;
+ }
+
+ return ret;
+}
+
+/**
+ * @brief Delete the pipeline description with a given name.
+ * @param[in] name The unique name to delete.
+ * @return @c 0 on success. Otherwise a negative error value.
+ */
+gint
+svcdb_pipeline_delete (const gchar *name)
+{
+ gint ret = 0;
+ MLServiceDB *db = svcdb_get ();
+
+ try {
+ db->delete_pipeline (name);
+ } catch (const std::invalid_argument &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EINVAL;
+ } catch (const std::exception &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EIO;
+ }
+
+ return ret;
+}
+
+/**
+ * @brief Add the model with given name.
+ * @param[in] name Unique name for model.
+ * @param[in] model The model to be stored.
+ * @param[in] is_active The model is active or not.
+ * @param[in] description The model description.
+ * @param[in] app_info The application information.
+ * @param[out] version The version of the model.
+ * @return @c 0 on success. Otherwise a negative error value.
+ */
+gint
+svcdb_model_add (const gchar *name, const gchar *path, const bool is_active,
+ const gchar *description, const gchar *app_info, guint *version)
+{
+ gint ret = 0;
+ MLServiceDB *db = svcdb_get ();
+
+ try {
+ db->set_model (name, path, is_active, description, app_info, version);
+ } catch (const std::invalid_argument &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EINVAL;
+ } catch (const std::exception &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EIO;
+ }
+
+ return ret;
+}
+
+/**
+ * @brief Update the model description with given name and version.
+ * @param[in] name Unique name for model.
+ * @param[in] version The version of the model.
+ * @param[in] description The model description.
+ * @return @c 0 on success. Otherwise a negative error value.
+ */
+gint
+svcdb_model_update_description (const gchar *name, const guint version,
+ const gchar *description)
+{
+ gint ret = 0;
+ MLServiceDB *db = svcdb_get ();
+
+ try {
+ db->update_model_description (name, version, description);
+ } catch (const std::invalid_argument &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EINVAL;
+ } catch (const std::exception &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EIO;
+ }
+
+ return ret;
+}
+
+/**
+ * @brief Activate the model with given name.
+ * @param[in] name Unique name for model.
+ * @param[in] version The version of the model.
+ * @return @c 0 on success. Otherwise a negative error value.
+ */
+gint
+svcdb_model_activate (const gchar *name, const guint version)
+{
+ gint ret = 0;
+ MLServiceDB *db = svcdb_get ();
+
+ try {
+ db->activate_model (name, version);
+ } catch (const std::invalid_argument &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EINVAL;
+ } catch (const std::exception &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EIO;
+ }
+
+ return ret;
+}
+
+/**
+ * @brief Get the model information with given name and version.
+ * @param[in] name The unique name to retrieve.
+ * @param[in] version The version of the model. If it is 0, all models will return, if it is -1, return the active model.
+ * @param[out] model_info The model information.
+ * @return @c 0 on success. Otherwise a negative error value.
+ */
+gint
+svcdb_model_get (const gchar *name, const guint version, gchar **model_info)
+{
+ gint ret = 0;
+ MLServiceDB *db = svcdb_get ();
+
+ try {
+ db->get_model (name, version, model_info);
+ } catch (const std::invalid_argument &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EINVAL;
+ } catch (const std::exception &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EIO;
+ }
+
+ return ret;
+}
+
+/**
+ * @brief Get the activated model information with given name.
+ * @param[in] name The unique name to retrieve.
+ * @param[out] model_info The model information.
+ * @return @c 0 on success. Otherwise a negative error value.
+ */
+gint
+svcdb_model_get_activated (const gchar *name, gchar **model_info)
+{
+ gint ret = 0;
+ MLServiceDB *db = svcdb_get ();
+
+ try {
+ db->get_model (name, -1, model_info);
+ } catch (const std::invalid_argument &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EINVAL;
+ } catch (const std::exception &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EIO;
+ }
+
+ return ret;
+}
+
+/**
+ * @brief Get the model information with given name.
+ * @param[in] name The unique name to retrieve.
+ * @param[out] model_info The model information.
+ * @return @c 0 on success. Otherwise a negative error value.
+ */
+gint
+svcdb_model_get_all (const gchar *name, gchar **model_info)
+{
+ gint ret = 0;
+ MLServiceDB *db = svcdb_get ();
+
+ try {
+ db->get_model (name, 0, model_info);
+ } catch (const std::invalid_argument &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EINVAL;
+ } catch (const std::exception &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EIO;
+ }
+
+ return ret;
+}
+
+/**
+ * @brief Delete the model.
+ * @param[in] name The unique name to delete.
+ * @param[in] version The version of the model to delete.
+ * @return @c 0 on success. Otherwise a negative error value.
+ */
+gint
+svcdb_model_delete (const gchar *name, const guint version)
+{
+ gint ret = 0;
+ MLServiceDB *db = svcdb_get ();
+
+ try {
+ db->delete_model (name, version);
+ } catch (const std::invalid_argument &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EINVAL;
+ } catch (const std::exception &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EIO;
+ }
+
+ return ret;
+}
+
+/**
+ * @brief Set the resource with given name.
+ * @param[in] name Unique name of ml-resource.
+ * @param[in] path The path to be stored.
+ * @param[in] description The description for ml-resource.
+ * @param[in] app_info The application information.
+ * @return @c 0 on success. Otherwise a negative error value.
+ */
+gint
+svcdb_resource_add (const gchar *name, const gchar *path,
+ const gchar *description, const gchar *app_info)
+{
+ gint ret = 0;
+ MLServiceDB *db = svcdb_get ();
+
+ try {
+ db->set_resource (name, path, description, app_info);
+ } catch (const std::invalid_argument &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EINVAL;
+ } catch (const std::exception &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EIO;
+ }
+
+ return ret;
+}
+
+/**
+ * @brief Get the resource with given name.
+ * @param[in] name The unique name to retrieve.
+ * @param[out] resource The resource information.
+ * @return @c 0 on success. Otherwise a negative error value.
+ */
+gint
+svcdb_resource_get (const gchar *name, gchar **res_info)
+{
+ gint ret = 0;
+ MLServiceDB *db = svcdb_get ();
+
+ try {
+ db->get_resource (name, res_info);
+ } catch (const std::invalid_argument &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EINVAL;
+ } catch (const std::exception &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EIO;
+ }
+
+ return ret;
+}
+
+/**
+ * @brief Delete the resource.
+ * @param[in] name The unique name to delete.
+ * @return @c 0 on success. Otherwise a negative error value.
+ */
+gint
+svcdb_resource_delete (const gchar *name)
+{
+ gint ret = 0;
+ MLServiceDB *db = svcdb_get ();
+
+ try {
+ db->delete_resource (name);
+ } catch (const std::invalid_argument &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EINVAL;
+ } catch (const std::exception &e) {
+ ml_loge ("%s", e.what ());
+ ret = -EIO;
+ }
+
+ return ret;
+}
+G_END_DECLS
virtual void connectDB ();
virtual void disconnectDB ();
virtual void set_pipeline (const std::string name, const std::string description);
- virtual void get_pipeline (const std::string name, std::string &description);
+ virtual void get_pipeline (const std::string name, gchar **description);
virtual void delete_pipeline (const std::string name);
virtual void set_model (const std::string name, const std::string model, const bool is_active,
const std::string description, const std::string app_info, guint *version);
virtual void update_model_description (const std::string name,
const guint version, const std::string description);
virtual void activate_model (const std::string name, const guint version);
- virtual void get_model (const std::string name, std::string &model, const gint version);
+ virtual void get_model (const std::string name, const gint version, gchar **model);
virtual void delete_model (const std::string name, const guint version);
virtual void set_resource (const std::string name, const std::string path,
const std::string description, const std::string app_info);
- virtual void get_resource (const std::string name, std::string &resource);
+ virtual void get_resource (const std::string name, gchar **resource);
virtual void delete_resource (const std::string name);
- static MLServiceDB &getInstance (void);
-
- private:
MLServiceDB (std::string path);
virtual ~MLServiceDB ();
+ private:
void initDB ();
int get_table_version (const std::string tbl_name, const int default_ver);
bool set_table_version (const std::string tbl_name, const int tbl_ver);
#include "log.h"
#include "service-db.hh"
+#include "service-db-util.h"
+
+#define TEST_DB_PATH "."
/**
* @brief Negative test for set_pipeline. Invalid param case (empty name or description).
*/
TEST (serviceDB, set_pipeline_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
db.connectDB ();
*/
TEST (serviceDB, get_pipeline_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
db.connectDB ();
try {
- std::string pipeline_description;
- db.get_pipeline ("", pipeline_description);
+ gchar *pipeline_description;
+ db.get_pipeline ("", &pipeline_description);
+ FAIL ();
+ } catch (const std::exception &e) {
+ /* expected */
+ }
+
+ try {
+ db.get_pipeline ("test", NULL);
FAIL ();
} catch (const std::exception &e) {
/* expected */
*/
TEST (serviceDB, delete_pipeline_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
db.connectDB ();
*/
TEST (serviceDB, set_model_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
guint version;
db.connectDB ();
*/
TEST (serviceDB, update_model_scenario)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
db.connectDB ();
/* No exception to add, get, and delete model with name 'test'. */
try {
- std::string model_info;
+ gchar *model_info;
gchar *pos;
guint version, version_active;
db.set_model ("test", "test_model2", false, "model2_description", "", &version);
/* Check model info contains added string. */
- db.get_model ("test", model_info, 0);
- pos = g_strstr_len (model_info.c_str (), -1, "test_model1");
+ db.get_model ("test", 0, &model_info);
+ pos = g_strstr_len (model_info, -1, "test_model1");
EXPECT_TRUE (pos != NULL);
- pos = g_strstr_len (model_info.c_str (), -1, "test_model2");
+ pos = g_strstr_len (model_info, -1, "test_model2");
EXPECT_TRUE (pos != NULL);
- pos = g_strstr_len (model_info.c_str (), -1, "model1_description");
+ pos = g_strstr_len (model_info, -1, "model1_description");
EXPECT_TRUE (pos != NULL);
- pos = g_strstr_len (model_info.c_str (), -1, "model2_description");
+ pos = g_strstr_len (model_info, -1, "model2_description");
EXPECT_TRUE (pos != NULL);
+ g_free (model_info);
- db.get_model ("test", model_info, version);
- pos = g_strstr_len (model_info.c_str (), -1, "test_model2");
+ db.get_model ("test", version, &model_info);
+ pos = g_strstr_len (model_info, -1, "test_model2");
EXPECT_TRUE (pos != NULL);
- pos = g_strstr_len (model_info.c_str (), -1, "model2_description");
+ pos = g_strstr_len (model_info, -1, "model2_description");
EXPECT_TRUE (pos != NULL);
+ g_free (model_info);
- db.get_model ("test", model_info, -1);
- pos = g_strstr_len (model_info.c_str (), -1, "test_model1");
+ db.get_model ("test", -1, &model_info);
+ pos = g_strstr_len (model_info, -1, "test_model1");
EXPECT_TRUE (pos != NULL);
- pos = g_strstr_len (model_info.c_str (), -1, "model1_description");
+ pos = g_strstr_len (model_info, -1, "model1_description");
EXPECT_TRUE (pos != NULL);
+ g_free (model_info);
db.activate_model ("test", version);
db.update_model_description ("test", version, "updated_desc_model2");
- db.get_model ("test", model_info, -1);
- pos = g_strstr_len (model_info.c_str (), -1, "test_model2");
+ db.get_model ("test", -1, &model_info);
+ pos = g_strstr_len (model_info, -1, "test_model2");
EXPECT_TRUE (pos != NULL);
- pos = g_strstr_len (model_info.c_str (), -1, "updated_desc_model2");
+ pos = g_strstr_len (model_info, -1, "updated_desc_model2");
EXPECT_TRUE (pos != NULL);
+ g_free (model_info);
db.delete_model ("test", 0);
} catch (const std::exception &e) {
*/
TEST (serviceDB, get_model_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
db.connectDB ();
try {
- std::string model_description;
- db.get_model ("", model_description, 0);
+ gchar *model_description;
+ db.get_model ("", 0, &model_description);
FAIL ();
} catch (const std::exception &e) {
/* expected */
}
try {
- std::string model_description;
- db.get_model ("test", model_description, -54321);
+ gchar *model_description;
+ db.get_model ("test", -54321, &model_description);
+ FAIL ();
+ } catch (const std::exception &e) {
+ /* expected */
+ }
+
+ try {
+ db.get_model ("test", 0, NULL);
FAIL ();
} catch (const std::exception &e) {
/* expected */
*/
TEST (serviceDB, update_model_description_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
db.connectDB ();
*/
TEST (serviceDB, activate_model_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
db.connectDB ();
*/
TEST (serviceDB, delete_model_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
db.connectDB ();
*/
TEST (serviceDB, delete_model_unregistered_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
guint version;
db.connectDB ();
*/
TEST (serviceDB, delete_model_activated_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
guint version;
db.connectDB ();
*/
TEST (serviceDBNotInitalized, set_pipeline_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
try {
db.set_pipeline ("test", "videotestsrc ! fakesink");
*/
TEST (serviceDBNotInitalized, get_pipeline_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
try {
- std::string pd;
- db.get_pipeline ("test", pd);
+ gchar *pd;
+ db.get_pipeline ("test", &pd);
FAIL ();
} catch (const std::exception &e) {
/* expected */
*/
TEST (serviceDBNotInitalized, delete_pipeline_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
try {
db.delete_pipeline ("test");
*/
TEST (serviceDBNotInitalized, set_model_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
try {
guint version;
*/
TEST (serviceDBNotInitalized, update_model_description_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
try {
db.update_model_description ("test", 0, "description");
*/
TEST (serviceDBNotInitalized, activate_model_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
try {
db.activate_model ("test", 0);
*/
TEST (serviceDBNotInitalized, get_model_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
try {
- std::string model_path;
- db.get_model ("test", model_path, 0);
+ gchar *model_path;
+ db.get_model ("test", 0, &model_path);
FAIL ();
} catch (const std::exception &e) {
/* expected */
*/
TEST (serviceDBNotInitalized, delete_model_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
try {
db.delete_model ("test", 0U);
*/
TEST (serviceDB, set_resource_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
db.connectDB ();
*/
TEST (serviceDB, update_resource_scenario)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
db.connectDB ();
/* No exception to add, get, and delete resources with name 'test'. */
try {
- std::string res_info;
+ gchar *res_info;
gchar *pos;
db.set_resource ("test", "test_resource1", "res1_description", "");
db.set_resource ("test", "test_resource2", "res2_description", "");
/* Check res info contains added string. */
- db.get_resource ("test", res_info);
- pos = g_strstr_len (res_info.c_str (), -1, "test_resource1");
+ db.get_resource ("test", &res_info);
+ pos = g_strstr_len (res_info, -1, "test_resource1");
EXPECT_TRUE (pos != NULL);
- pos = g_strstr_len (res_info.c_str (), -1, "test_resource2");
+ pos = g_strstr_len (res_info, -1, "test_resource2");
EXPECT_TRUE (pos != NULL);
- pos = g_strstr_len (res_info.c_str (), -1, "res1_description");
+ pos = g_strstr_len (res_info, -1, "res1_description");
EXPECT_TRUE (pos != NULL);
- pos = g_strstr_len (res_info.c_str (), -1, "res2_description");
+ pos = g_strstr_len (res_info, -1, "res2_description");
EXPECT_TRUE (pos != NULL);
+ g_free (res_info);
db.set_resource ("test", "test_resource2", "updated_desc_res2", "");
- db.get_resource ("test", res_info);
- pos = g_strstr_len (res_info.c_str (), -1, "updated_desc_res2");
+ db.get_resource ("test", &res_info);
+ pos = g_strstr_len (res_info, -1, "updated_desc_res2");
EXPECT_TRUE (pos != NULL);
+ g_free (res_info);
db.delete_resource ("test");
} catch (const std::exception &e) {
*/
TEST (serviceDB, get_resource_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
db.connectDB ();
try {
- std::string res_description;
- db.get_resource ("", res_description);
+ gchar *res_description;
+ db.get_resource ("", &res_description);
+ FAIL ();
+ } catch (const std::exception &e) {
+ /* expected */
+ }
+
+ try {
+ db.get_resource ("test", NULL);
FAIL ();
} catch (const std::exception &e) {
/* expected */
*/
TEST (serviceDB, get_resource_unregistered_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
db.connectDB ();
db.delete_resource ("test");
try {
- std::string res_description;
- db.get_resource ("test", res_description);
+ gchar *res_description;
+ db.get_resource ("test", &res_description);
FAIL ();
} catch (const std::exception &e) {
/* expected */
*/
TEST (serviceDB, delete_resource_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
db.connectDB ();
*/
TEST (serviceDB, delete_resource_unregistered_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
db.connectDB ();
*/
TEST (serviceDBNotInitalized, set_resource_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
try {
db.set_resource ("test", "resource", "description", "");
*/
TEST (serviceDBNotInitalized, get_resource_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
try {
- std::string res_description;
- db.get_resource ("test", res_description);
+ gchar *res_description;
+ db.get_resource ("test", &res_description);
FAIL ();
} catch (const std::exception &e) {
/* expected */
*/
TEST (serviceDBNotInitalized, delete_resource_n)
{
- MLServiceDB &db = MLServiceDB::getInstance ();
+ MLServiceDB db (TEST_DB_PATH);
try {
db.delete_resource ("test");
dependencies: ml_agent_test_dep,
install: get_option('install-test'),
install_dir: unittest_base_dir,
+ c_args: ['-DDB_PATH="."', ml_agent_db_key_prefix_arg],
objects: ml_agent_main_objs
)