[ServiceDB] interface to handle DB data
authorJaeyun Jung <jy1210.jung@samsung.com>
Tue, 23 Jan 2024 04:35:05 +0000 (13:35 +0900)
committerwooksong <wook16.song@samsung.com>
Tue, 23 Jan 2024 10:04:06 +0000 (19:04 +0900)
Add c-style DB interface and init/close DB when running daemon.

Signed-off-by: Jaeyun Jung <jy1210.jung@samsung.com>
daemon/main.c
daemon/meson.build
daemon/model-dbus-impl.cc
daemon/pipeline-dbus-impl.cc
daemon/pkg-mgr.cc
daemon/resource-dbus-impl.cc
daemon/service-db-util.h [new file with mode: 0644]
daemon/service-db.cc
daemon/service-db.hh
tests/daemon/unittest_service_db.cc
tests/meson.build

index 1a70e53aab0ea7fd189e05674985f5da97c4e3f7..0968d569bb3e4b076f244859c3875bce0a66f1f0 100644 (file)
@@ -22,6 +22,7 @@
 #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;
@@ -100,10 +101,16 @@ parse_args (gint *argc, gchar ***argv)
 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);
 
@@ -126,5 +133,9 @@ main (int argc, char **argv)
   if (pkg_mgr_deinit () < 0)
     ml_logw ("cannot finalize package manager");
 
-  return 0;
+error:
+  svcdb_finalize ();
+
+  is_session = verbose = FALSE;
+  return ret;
 }
index aed6e4112f8527d2ce84882591195ca2ad98a4fc..3b6db6a594293ba967150f888d7bbab05c5e8c49 100644 (file)
@@ -56,14 +56,19 @@ if get_option('default_library') == 'static'
   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
 )
 
index 904eaa4c48eb27a1074bdc7996204f2fc8984efd..c474db7342ede39f2cd92a6f4470af1b58099379 100644 (file)
@@ -19,7 +19,7 @@
 #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;
 
@@ -57,22 +57,10 @@ gdbus_cb_model_register (MachinelearningServiceModel *obj,
     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;
@@ -93,21 +81,9 @@ gdbus_cb_model_update_description (MachinelearningServiceModel *obj,
     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;
@@ -126,21 +102,9 @@ static gboolean
 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;
@@ -158,23 +122,11 @@ static gboolean
 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;
 }
@@ -191,24 +143,11 @@ static gboolean
 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;
 }
@@ -225,24 +164,11 @@ static gboolean
 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;
 }
@@ -260,21 +186,9 @@ static gboolean
 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;
index d761d277ed15f49f1fc6fc432e7858a29892ce6e..537480a824367aa562409493865328ad182d06c6 100644 (file)
@@ -27,7 +27,7 @@
 #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;
@@ -95,27 +95,8 @@ dbus_cb_core_set_pipeline (MachinelearningServicePipeline *obj, GDBusMethodInvoc
     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;
@@ -129,28 +110,10 @@ dbus_cb_core_get_pipeline (MachinelearningServicePipeline *obj,
     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;
 }
@@ -163,29 +126,8 @@ dbus_cb_core_delete_pipeline (MachinelearningServicePipeline *obj,
     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;
@@ -199,73 +141,57 @@ dbus_cb_core_launch_pipeline (MachinelearningServicePipeline *obj,
     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;
 }
@@ -414,8 +340,6 @@ dbus_cb_core_get_state (MachinelearningServicePipeline *obj,
   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);
index 14b15a1a56d4696e0760884e2305c83d9ede88f5..e8fd873438f9f1a37db1ad2d067be0599b96ab2a 100755 (executable)
@@ -16,7 +16,7 @@
 #include <stdio.h>
 
 #include "pkg-mgr.h"
-#include "service-db.hh"
+#include "service-db-util.h"
 
 /**
  * @brief Internal enumeration for data types of json.
@@ -71,6 +71,7 @@ static void
 _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:
@@ -118,104 +119,96 @@ _parse_json (const gchar *json_path, mlsvc_json_type_e json_type, const gchar *a
   }
 
   /* 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 ();
 }
 
 /**
index 6105ef2126045741ec585745ce8da870500a3872..28fa7ca78b6242ef732942ded25665f6d2715ba9 100644 (file)
@@ -19,7 +19,7 @@
 #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;
 
@@ -54,21 +54,9 @@ static gboolean
 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;
@@ -85,23 +73,11 @@ static gboolean
 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;
 }
@@ -117,21 +93,9 @@ static gboolean
 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;
diff --git a/daemon/service-db-util.h b/daemon/service-db-util.h
new file mode 100644 (file)
index 0000000..2a01809
--- /dev/null
@@ -0,0 +1,37 @@
+/* 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__ */
index 14046f49e89366b9a24e6a4347959b250a669514..d1163a9a060849f8cd8e52eb1970b69d48233128 100644 (file)
@@ -11,6 +11,7 @@
  */
 
 #include "service-db.hh"
+#include "service-db-util.h"
 #include "log.h"
 
 #define sqlite3_clear_errmsg(m) \
@@ -21,8 +22,6 @@
     }                           \
   } while (0)
 
-#define ML_DATABASE_PATH DB_PATH "/.ml-service.db"
-
 /**
  * @brief The version of pipeline table schema. It should be a positive integer.
  */
@@ -57,18 +56,6 @@ const char *g_mlsvc_table_schema_v1[] = { [TBL_DB_INFO] = "tblMLDBInfo (name TEX
 
 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
@@ -164,9 +151,11 @@ MLServiceDB::connectDB ()
   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;
   }
 
@@ -315,13 +304,13 @@ MLServiceDB::set_pipeline (const std::string name, const std::string description
  * @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;
@@ -336,8 +325,7 @@ MLServiceDB::get_pipeline (const std::string name, std::string &description)
   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);
   }
@@ -345,7 +333,7 @@ MLServiceDB::get_pipeline (const std::string name, std::string &description)
 
 /**
  * @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)
@@ -449,6 +437,7 @@ MLServiceDB::is_resource_registered (const std::string key)
  * @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
@@ -630,11 +619,11 @@ MLServiceDB::activate_model (const std::string name, const guint version)
 /**
  * @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)";
@@ -642,8 +631,8 @@ MLServiceDB::get_model (const std::string name, std::string &model, const gint v
   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;
@@ -675,8 +664,7 @@ MLServiceDB::get_model (const std::string name, std::string &model, const gint v
   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));
@@ -685,8 +673,8 @@ MLServiceDB::get_model (const std::string name, std::string &model, const gint v
 
 /**
  * @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)
@@ -740,6 +728,7 @@ 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,
@@ -786,7 +775,7 @@ 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)";
@@ -794,8 +783,8 @@ MLServiceDB::get_resource (const std::string name, std::string &resource)
   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;
@@ -819,13 +808,12 @@ MLServiceDB::get_resource (const std::string name, std::string &resource)
   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)
@@ -859,3 +847,381 @@ 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
index 5491d5cfa67d67b278c3c2d2f365dd94de04f010..00fba39b2843fa764f493d33963a71b32ed54f5f 100644 (file)
@@ -31,26 +31,24 @@ class MLServiceDB
   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);
index cefff21156d2c5611223bb8babd5b76956aef34e..a1c009c6d6263e80bf92e6bb84bbeaae0b45f029 100755 (executable)
 
 #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 ();
 
@@ -44,13 +47,20 @@ TEST (serviceDB, set_pipeline_n)
  */
 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 */
@@ -64,7 +74,7 @@ TEST (serviceDB, get_pipeline_n)
  */
 TEST (serviceDB, delete_pipeline_n)
 {
-  MLServiceDB &db = MLServiceDB::getInstance ();
+  MLServiceDB db (TEST_DB_PATH);
 
   db.connectDB ();
 
@@ -83,7 +93,7 @@ TEST (serviceDB, delete_pipeline_n)
  */
 TEST (serviceDB, set_model_n)
 {
-  MLServiceDB &db = MLServiceDB::getInstance ();
+  MLServiceDB db (TEST_DB_PATH);
   guint version;
 
   db.connectDB ();
@@ -117,13 +127,13 @@ TEST (serviceDB, set_model_n)
  */
 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;
 
@@ -131,35 +141,39 @@ TEST (serviceDB, update_model_scenario)
     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) {
@@ -174,21 +188,28 @@ TEST (serviceDB, update_model_scenario)
  */
 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 */
@@ -202,7 +223,7 @@ TEST (serviceDB, get_model_n)
  */
 TEST (serviceDB, update_model_description_n)
 {
-  MLServiceDB &db = MLServiceDB::getInstance ();
+  MLServiceDB db (TEST_DB_PATH);
 
   db.connectDB ();
 
@@ -235,7 +256,7 @@ TEST (serviceDB, update_model_description_n)
  */
 TEST (serviceDB, activate_model_n)
 {
-  MLServiceDB &db = MLServiceDB::getInstance ();
+  MLServiceDB db (TEST_DB_PATH);
 
   db.connectDB ();
 
@@ -261,7 +282,7 @@ TEST (serviceDB, activate_model_n)
  */
 TEST (serviceDB, delete_model_n)
 {
-  MLServiceDB &db = MLServiceDB::getInstance ();
+  MLServiceDB db (TEST_DB_PATH);
 
   db.connectDB ();
 
@@ -280,7 +301,7 @@ TEST (serviceDB, delete_model_n)
  */
 TEST (serviceDB, delete_model_unregistered_n)
 {
-  MLServiceDB &db = MLServiceDB::getInstance ();
+  MLServiceDB db (TEST_DB_PATH);
   guint version;
 
   db.connectDB ();
@@ -304,7 +325,7 @@ TEST (serviceDB, delete_model_unregistered_n)
  */
 TEST (serviceDB, delete_model_activated_n)
 {
-  MLServiceDB &db = MLServiceDB::getInstance ();
+  MLServiceDB db (TEST_DB_PATH);
   guint version;
 
   db.connectDB ();
@@ -328,7 +349,7 @@ TEST (serviceDB, delete_model_activated_n)
  */
 TEST (serviceDBNotInitalized, set_pipeline_n)
 {
-  MLServiceDB &db = MLServiceDB::getInstance ();
+  MLServiceDB db (TEST_DB_PATH);
 
   try {
     db.set_pipeline ("test", "videotestsrc ! fakesink");
@@ -343,11 +364,11 @@ TEST (serviceDBNotInitalized, set_pipeline_n)
  */
 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 */
@@ -359,7 +380,7 @@ TEST (serviceDBNotInitalized, get_pipeline_n)
  */
 TEST (serviceDBNotInitalized, delete_pipeline_n)
 {
-  MLServiceDB &db = MLServiceDB::getInstance ();
+  MLServiceDB db (TEST_DB_PATH);
 
   try {
     db.delete_pipeline ("test");
@@ -374,7 +395,7 @@ TEST (serviceDBNotInitalized, delete_pipeline_n)
  */
 TEST (serviceDBNotInitalized, set_model_n)
 {
-  MLServiceDB &db = MLServiceDB::getInstance ();
+  MLServiceDB db (TEST_DB_PATH);
 
   try {
     guint version;
@@ -390,7 +411,7 @@ TEST (serviceDBNotInitalized, set_model_n)
  */
 TEST (serviceDBNotInitalized, update_model_description_n)
 {
-  MLServiceDB &db = MLServiceDB::getInstance ();
+  MLServiceDB db (TEST_DB_PATH);
 
   try {
     db.update_model_description ("test", 0, "description");
@@ -405,7 +426,7 @@ TEST (serviceDBNotInitalized, update_model_description_n)
  */
 TEST (serviceDBNotInitalized, activate_model_n)
 {
-  MLServiceDB &db = MLServiceDB::getInstance ();
+  MLServiceDB db (TEST_DB_PATH);
 
   try {
     db.activate_model ("test", 0);
@@ -420,11 +441,11 @@ TEST (serviceDBNotInitalized, activate_model_n)
  */
 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 */
@@ -436,7 +457,7 @@ TEST (serviceDBNotInitalized, get_model_n)
  */
 TEST (serviceDBNotInitalized, delete_model_n)
 {
-  MLServiceDB &db = MLServiceDB::getInstance ();
+  MLServiceDB db (TEST_DB_PATH);
 
   try {
     db.delete_model ("test", 0U);
@@ -451,7 +472,7 @@ TEST (serviceDBNotInitalized, delete_model_n)
  */
 TEST (serviceDB, set_resource_n)
 {
-  MLServiceDB &db = MLServiceDB::getInstance ();
+  MLServiceDB db (TEST_DB_PATH);
 
   db.connectDB ();
 
@@ -477,33 +498,35 @@ TEST (serviceDB, set_resource_n)
  */
 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) {
@@ -518,13 +541,20 @@ TEST (serviceDB, update_resource_scenario)
  */
 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 */
@@ -538,7 +568,7 @@ TEST (serviceDB, get_resource_n)
  */
 TEST (serviceDB, get_resource_unregistered_n)
 {
-  MLServiceDB &db = MLServiceDB::getInstance ();
+  MLServiceDB db (TEST_DB_PATH);
 
   db.connectDB ();
 
@@ -547,8 +577,8 @@ TEST (serviceDB, get_resource_unregistered_n)
   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 */
@@ -562,7 +592,7 @@ TEST (serviceDB, get_resource_unregistered_n)
  */
 TEST (serviceDB, delete_resource_n)
 {
-  MLServiceDB &db = MLServiceDB::getInstance ();
+  MLServiceDB db (TEST_DB_PATH);
 
   db.connectDB ();
 
@@ -581,7 +611,7 @@ TEST (serviceDB, delete_resource_n)
  */
 TEST (serviceDB, delete_resource_unregistered_n)
 {
-  MLServiceDB &db = MLServiceDB::getInstance ();
+  MLServiceDB db (TEST_DB_PATH);
 
   db.connectDB ();
 
@@ -604,7 +634,7 @@ TEST (serviceDB, delete_resource_unregistered_n)
  */
 TEST (serviceDBNotInitalized, set_resource_n)
 {
-  MLServiceDB &db = MLServiceDB::getInstance ();
+  MLServiceDB db (TEST_DB_PATH);
 
   try {
     db.set_resource ("test", "resource", "description", "");
@@ -619,11 +649,11 @@ TEST (serviceDBNotInitalized, set_resource_n)
  */
 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 */
@@ -635,7 +665,7 @@ TEST (serviceDBNotInitalized, get_resource_n)
  */
 TEST (serviceDBNotInitalized, delete_resource_n)
 {
-  MLServiceDB &db = MLServiceDB::getInstance ();
+  MLServiceDB db (TEST_DB_PATH);
 
   try {
     db.delete_resource ("test");
index b3dd19e18f582e73836f19b2cdadde515b07a120..4a86785432b032dc0d573baee4e37e975296fe74 100644 (file)
@@ -35,6 +35,7 @@ executable('machine-learning-agent-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
 )