Change db handler implementation to receive list when getting connection
authorJunghyun Yeon <jungh.yeon@samsung.com>
Sat, 6 Mar 2021 05:12:35 +0000 (14:12 +0900)
committerJunghyun Yeon <jungh.yeon@samsung.com>
Sat, 6 Mar 2021 05:12:35 +0000 (14:12 +0900)
Signed-off-by: Junghyun Yeon <jungh.yeon@samsung.com>
src/common/database/abstract_db_handler.cc
src/common/database/abstract_db_handler.hh
src/common/database/appinfo_db_handler.cc
src/common/database/cert_get_db_handler.cc
src/common/database/cert_set_db_handler.cc
src/common/database/db_handle_provider.cc
src/common/database/depinfo_db_handler.cc
src/common/database/pkg_get_db_handler.cc
src/common/database/pkg_set_db_handler.cc
src/common/database/query_handler.cc

index 707854f..d0e002b 100644 (file)
@@ -24,6 +24,7 @@
 #include <tzplatform_config.h>
 
 #include <string>
+#include <vector>
 
 #include "db_handle_provider.hh"
 #include "pkgmgr-info.h"
@@ -219,15 +220,16 @@ AbstractDBHandler::AbstractDBHandler(uid_t uid) { uid_ = uid; }
 
 AbstractDBHandler::~AbstractDBHandler() {
   // Is this necessary?
-  sqlite3_close(db_);
+  for (auto db_handle : db_handle_list_)
+    sqlite3_close(db_handle);
 }
 
-std::string AbstractDBHandler::GetDBPath() {
-  std::string db_path;
+std::vector<std::string> AbstractDBHandler::GetDBPath() {
+  std::vector<std::string> db_path;
   if (db_type_ == DB_TYPE_FILE_PKGDB) {
     db_path = DBHandleProvider::GetInst(uid_).GetParserDBPath();
   } else if (db_type_ == DB_TYPE_FILE_CERTDB) {
-    db_path = DBHandleProvider::GetInst(uid_).GetCertDBPath();
+    db_path.emplace_back(DBHandleProvider::GetInst(uid_).GetCertDBPath());
   }
 
   return db_path;
@@ -238,8 +240,25 @@ bool AbstractDBHandler::Connect() {
     // error log
     return false;
   }
-  std::string db_path = GetDBPath();
+  auto dbpath_list = GetDBPath();
   int ret = 0;
+  sqlite3* db;
+  for (auto dbpath : dbpath_list) {
+    if (op_type_ == OPERATION_TYPE_READ)
+      ret = __open_read_db(dbpath.c_str(), &db, SQLITE_OPEN_READONLY |
+          SQLITE_OPEN_URI);
+    else
+      ret = __open_write_db(uid_, dbpath.c_str(), &db,
+          SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE);
+    // TODO: if write request has received, only one database should be delivered
+    if (ret != SQLITE_OK)
+      return false;
+
+    db_handle_list_.emplace_back(db);
+  }
+
+  return true;
+/*
   if (op_type_ == OPERATION_TYPE_READ)
     ret = __open_read_db(db_path.c_str(), &db_, SQLITE_OPEN_READONLY |
         SQLITE_OPEN_URI);
@@ -252,9 +271,12 @@ bool AbstractDBHandler::Connect() {
     return false;
   }
   return true;
+*/
 }
 
-sqlite3* AbstractDBHandler::GetConnection() { return db_; }
+std::vector<sqlite3*> AbstractDBHandler::GetConnection() {
+  return db_handle_list_;
+}
 
 void AbstractDBHandler::SetOpType(OperationType type) {
   op_type_ = type;
index 85d0217..daa33f6 100644 (file)
@@ -18,6 +18,7 @@
 #define ABSTRACT_DB_HANDLER_HH_
 
 #include <string>
+#include <vector>
 
 #include <sys/types.h>
 #include <sqlite3.h>
@@ -53,16 +54,16 @@ class EXPORT_API AbstractDBHandler {
 
  protected:
   bool Connect();
-  sqlite3* GetConnection();
+  std::vector<sqlite3*> GetConnection();
   std::string GetLocale();
 
  private:
-  std::string GetDBPath();
+  std::vector<std::string> GetDBPath();
   DBType db_type_ = DB_TYPE_NONE;
   OperationType op_type_ = OPERATION_TYPE_NONE;
   uid_t uid_;
   std::string locale_;
-  sqlite3* db_ = nullptr;
+  std::vector<sqlite3*> db_handle_list_;
 };
 
 }  // namespace database
index 8d569bf..50cc841 100644 (file)
 
 #include "appinfo_db_handler.hh"
 
+#include <vector>
+
 #include "pkgmgrinfo_internal.h"
 
+
 namespace {
 
 void _move(gpointer key, gpointer value, gpointer user_data) {
@@ -51,9 +54,18 @@ int AppInfoDBHandler::Execute() {
   if (!Connect()) return PMINFO_R_ERROR;
 
   GHashTable* list = nullptr;
+  std::vector<sqlite3*> conn_list = GetConnection();
+  int ret = PMINFO_R_OK;
+  for (auto conn : conn_list) {
+    ret = appinfo_internal_filter_get_list(conn, filter_, uid_,
+                                             GetLocale().c_str(), &list);
+    if (ret != PMINFO_R_OK)
+      break;
+  }
+/*
   int ret = appinfo_internal_filter_get_list(GetConnection(), filter_, uid_,
                                              GetLocale().c_str(), &list);
-
+*/
   g_hash_table_foreach(list, _move, &handle_list_);
   g_hash_table_destroy(list);
   return ret;
index cf5076f..f505116 100644 (file)
@@ -16,6 +16,8 @@
 
 #include "cert_get_db_handler.hh"
 
+#include <vector>
+
 #include "pkgmgrinfo_internal.h"
 
 namespace pkgmgr_common {
@@ -44,8 +46,9 @@ int CertGetDBHandler::Execute() {
     return ret;
 
   handle_ = static_cast<pkgmgr_certinfo_x*>(handle);
-  // TODO: db handle should be delivered
-  ret = certinfo_internal_get(GetConnection(), pkgid_.c_str(), uid_, handle_);
+  std::vector<sqlite3*> conn_list = GetConnection();
+  for (auto& conn : conn_list)
+    ret = certinfo_internal_get(conn, pkgid_.c_str(), uid_, handle_);
 
   return ret;
 }
index 3ad6600..3077c3a 100644 (file)
@@ -17,6 +17,8 @@
 #include "db_handle_provider.hh"
 #include "cert_set_db_handler.hh"
 
+#include <vector>
+
 #include "pkgmgrinfo_internal.h"
 
 namespace pkgmgr_common {
@@ -36,8 +38,11 @@ int CertSetDBHandler::Execute() {
   SetDBType(DB_TYPE_FILE_CERTDB);
 
   DBHandleProvider::GetInst(uid_).SetMemoryMode(true);
-  int ret = certinfo_internal_set(
-      GetConnection(), handle_->pkgid, handle_, uid_);
+  std::vector<sqlite3*> conn_list = GetConnection();
+  int ret;
+  for (auto& conn : conn_list)
+    ret = certinfo_internal_set(
+        conn, handle_->pkgid, handle_, uid_);
 
   return ret;
 }
index 714c43a..56a42ce 100644 (file)
@@ -35,6 +35,10 @@ uid_t GetGlobalUID() {
   return tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
 }
 
+bool IsUserRequest(uid_t uid) {
+  return (uid > REGULAR_USER);
+}
+
 uid_t ConvertUID(uid_t uid) {
   if (uid < REGULAR_USER)
     return tzplatform_getuid(TZ_SYS_GLOBALAPP_USER);
@@ -89,10 +93,12 @@ std::vector<std::string> DBHandleProvider::GetParserDBPath() {
   std::vector<std::string> db_path_list;
   if (is_memory_) {
     db_path_list.emplace_back(parser_memory_db_path_);
-    db_path_list.emplace_back(global_parser_memory_db_path_);
+    if (IsUserRequest(uid_))
+      db_path_list.emplace_back(global_parser_memory_db_path_);
   } else {
     db_path_list.emplace_back(parser_file_db_path_);
-    db_path_list.emplace_back(global_parser_file_db_path_);
+    if (IsUserRequest(uid_))
+      db_path_list.emplace_back(global_parser_file_db_path_);
   }
   return db_path_list;
 }
index 5ca5320..d9bf3e6 100644 (file)
@@ -16,6 +16,8 @@
 
 #include "depinfo_db_handler.hh"
 
+#include <vector>
+
 #include "pkgmgrinfo_internal.h"
 
 namespace pkgmgr_common {
@@ -40,9 +42,14 @@ int DepInfoGetDBHandler::Execute() {
 
   if (!Connect()) return PMINFO_R_ERROR;
   GList *list = nullptr;
-  int ret = pkginfo_internal_filter_get_depends_on(
-      GetConnection(), pkgid_.c_str(), &list);
-
+  std::vector<sqlite3*> conn_list = GetConnection();
+  int ret;
+  for (auto& conn : conn_list) {
+    ret = pkginfo_internal_filter_get_depends_on(
+        conn, pkgid_.c_str(), &list);
+    if (ret != PMINFO_R_OK)
+      break;
+  }
   return ret;
 }
 
index cf9c1fa..99086fd 100644 (file)
@@ -16,6 +16,8 @@
 
 #include "pkg_get_db_handler.hh"
 
+#include <vector>
+
 #include "pkgmgrinfo_internal.h"
 
 namespace {
@@ -52,13 +54,18 @@ int PkgGetDBHandler::Execute() {
 
   // TODO: db handle should be delivered
   GHashTable* list;
-  int ret = pkginfo_internal_filter_get_list(GetConnection(), filter_, uid_,
-                                             GetLocale().c_str(), &list);
-  if (ret != PMINFO_R_OK) {
-    // TODO: error log
-    return ret;
+  std::vector<sqlite3*> conn_list = GetConnection();
+  int ret;
+  for (auto& conn : conn_list) {
+    ret = pkginfo_internal_filter_get_list(conn, filter_, uid_,
+                                            GetLocale().c_str(), &list);
+    if (ret != PMINFO_R_OK) {
+      g_hash_table_destroy(list);
+      return ret;
+    }
   }
 
+  // TODO: possible memory leak?
   g_hash_table_foreach(list, _move, &handle_list_);
   g_hash_table_destroy(list);
 
index 8b5cd26..16a1c81 100644 (file)
@@ -17,6 +17,8 @@
 #include "db_handle_provider.hh"
 #include "pkg_set_db_handler.hh"
 
+#include <vector>
+
 #include "pkgmgr_parser_db.h"
 
 namespace pkgmgr_common {
@@ -49,12 +51,14 @@ int PkgSetDBHandler::Execute() {
   int ret = 0;
 
   DBHandleProvider::GetInst(uid_).SetMemoryMode(true);
+  std::vector<sqlite3*> conn_list = GetConnection();
+  sqlite3* conn = conn_list.front();
   if (write_type_ == Insert) {
-    ret = pkgmgr_parser_insert_pkg_info(GetConnection(), package_, uid_);
+    ret = pkgmgr_parser_insert_pkg_info(conn, package_, uid_);
   } else if (write_type_ == Update) {
-    ret = pkgmgr_parser_update_pkg_info(GetConnection(), package_, uid_);
+    ret = pkgmgr_parser_update_pkg_info(conn, package_, uid_);
   } else if (write_type_ == Delete) {
-    ret = pkgmgr_parser_delete_pkg_info(GetConnection(), package_->package, uid_);
+    ret = pkgmgr_parser_delete_pkg_info(conn, package_->package, uid_);
   } else {
 
   }
index 6d81daa..a984771 100644 (file)
@@ -16,6 +16,8 @@
 
 #include "query_handler.hh"
 
+#include <vector>
+
 #include "pkgmgrinfo_internal.h"
 
 namespace pkgmgr_common {
@@ -47,24 +49,28 @@ int QueryHandler::Execute() {
   if (query_.size() == 0)
     return PMINFO_R_ERROR;
 
+  int ret;
+  std::vector<sqlite3*> conn_list = GetConnection();
   if (GetOpType() == OPERATION_TYPE_READ) {
-    int ret = get_query_result(GetConnection(), query_[0].c_str(), &list, &row, &col);
-    if (ret != PMINFO_R_OK) {
-      // TODO: error log
-      return ret;
-    }
+    for (auto& conn : conn_list) {
+      ret = get_query_result(conn, query_[0].c_str(), &list, &row, &col);
+      if (ret != PMINFO_R_OK) {
+        // TODO: error log
+        return ret;
+      }
 
-    result_.clear();
-    result_.resize(row);
-    GList* tmp = list;
-    for (int i = 0; i < row; ++i) {
-      for (int j = 0; j < col; ++j) {
-        result_[i].emplace_back(reinterpret_cast<char *>(tmp->data));
-        tmp = tmp->next;
+      result_.clear();
+      result_.resize(row);
+      GList* tmp = list;
+      for (int i = 0; i < row; ++i) {
+        for (int j = 0; j < col; ++j) {
+          result_[i].emplace_back(reinterpret_cast<char *>(tmp->data));
+          tmp = tmp->next;
+        }
       }
-    }
 
-    g_list_free(list);
+      g_list_free(list);
+    }
     return ret;
   } else {
     const char **queries = (const char **)calloc(query_.size(), sizeof(char *));
@@ -75,8 +81,9 @@ int QueryHandler::Execute() {
     for (const auto& query : query_)
       queries[i++] = query.c_str();
 
-    int ret = execute_write_queries(GetConnection(), queries, query_.size());
-
+    for (auto& conn : conn_list) {
+      ret = execute_write_queries(conn, queries, query_.size());
+    }
     free(queries);
     return ret;
   }