Add DBHandleProvider
authorChanggyu Choi <changyu.choi@samsung.com>
Thu, 4 Mar 2021 05:17:48 +0000 (14:17 +0900)
committer연정현/Tizen Platform Lab(SR)/Staff Engineer/삼성전자 <jungh.yeon@samsung.com>
Fri, 5 Mar 2021 00:05:55 +0000 (09:05 +0900)
Signed-off-by: Changgyu Choi <changyu.choi@samsung.com>
src/common/database/abstract_db_handler.cc
src/common/database/cert_set_db_handler.cc
src/common/database/db_handle_provider.cc [new file with mode: 0644]
src/common/database/db_handle_provider.hh [new file with mode: 0644]
src/common/database/pkg_set_db_handler.cc

index 91585c4..5404efe 100644 (file)
@@ -25,6 +25,7 @@
 
 #include <string>
 
+#include "db_handle_provider.hh"
 #include "pkgmgr-info.h"
 #include "pkgmgrinfo_debug.h"
 #include "pkgmgrinfo_private.h"
@@ -230,22 +231,10 @@ AbstractDBHandler::~AbstractDBHandler() {
 
 std::string AbstractDBHandler::GetDBPath() {
   std::string db_path;
-  if (db_type_ == DB_TYPE_MEMORY_PKGDB) {
-    db_path += kMemoryDBPrefix;
-    db_path += std::to_string(ConvertUID(uid_));
-    db_path += kMemoryDBPostFix;
-  } else if (db_type_ == DB_TYPE_FILE_PKGDB) {
-    char* tmp_dbpath = getUserPkgParserDBPathUID(uid_);
-    if (!tmp_dbpath)
-      return "";
-    db_path = tmp_dbpath;
-    free(tmp_dbpath);
+  if (db_type_ == DB_TYPE_FILE_PKGDB) {
+    db_path = DBHandleProvider::GetInst(uid_).GetParserDBPath();
   } else if (db_type_ == DB_TYPE_FILE_CERTDB) {
-    char *tmp_dbpath = getUserPkgCertDBPath();
-    if (!tmp_dbpath)
-      return "";
-    db_path = tmp_dbpath;
-    free(tmp_dbpath);
+    db_path = DBHandleProvider::GetInst(uid_).GetCertDBPath();
   }
 
   return db_path;
index 16a26ea..3002258 100644 (file)
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include "db_handle_provider.hh"
 #include "cert_set_db_handler.hh"
 
 #include "pkgmgrinfo_internal.h"
@@ -34,6 +35,7 @@ bool CertSetDBHandler::Execute() {
   SetOpType(OPERATION_TYPE_WRITE);
   SetDBType(DB_TYPE_FILE_CERTDB);
 
+  DBHandleProvider::GetInst(uid_).SetMemoryMode(true);
   int ret = certinfo_internal_set(
       GetConnection(), handle_->pkgid, handle_, uid_);
   if (ret != PMINFO_R_OK)
diff --git a/src/common/database/db_handle_provider.cc b/src/common/database/db_handle_provider.cc
new file mode 100644 (file)
index 0000000..6acb4b2
--- /dev/null
@@ -0,0 +1,128 @@
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "db_handle_provider.hh"
+#include "pkgmgr-info.h"
+#include "pkgmgrinfo_debug.h"
+
+#ifdef LOG_TAG
+#undef LOG_TAG
+#endif
+#define LOG_TAG "PKGMGR_INFO"
+
+namespace pkgmgr_common {
+
+std::unordered_map<uid_t, std::unique_ptr<DBHandleProvider>> DBHandleProvider::provider_;
+
+const char kMemoryDBPrefix[] = "file:memdb_";
+const char kMemoryDBPostFix[] = "?mode=memory&cache=shared";
+DBHandleProvider::DBHandleProvider(uid_t uid) : uid_(uid), is_memory_(false),
+    parser_memory_db_handle_(nullptr, sqlite3_close_v2),
+    cert_memory_db_handle_(nullptr, sqlite3_close_v2) {
+  parser_file_db_path_ = getUserPkgParserDBPathUID(uid_);
+  cert_file_db_path_ = getUserPkgCertDBPath();
+
+  parser_memory_db_path_ = "file:parserdb" +
+      std::to_string(static_cast<int>(uid_)) + "?mode=memory&cache=shared";
+  cert_memory_db_path_ = "file:certdb" +
+      std::to_string(static_cast<int>(uid_)) + "?mode=memory&cache=shared";
+}
+
+DBHandleProvider& DBHandleProvider::GetInst(uid_t uid) {
+  static std::mutex singleton_lock;
+  std::unique_lock<std::mutex> u(singleton_lock);
+  auto& prov = provider_[uid];
+  if (prov == nullptr) {
+    prov.reset(new DBHandleProvider(uid));
+  }
+
+  return *prov;
+}
+
+std::string DBHandleProvider::GetParserDBPath() {
+  std::unique_lock<std::mutex> u(lock_);
+  if (is_memory_)
+    return parser_memory_db_path_;
+  else
+    return parser_file_db_path_;
+}
+
+std::string DBHandleProvider::GetCertDBPath() {
+  std::unique_lock<std::mutex> u(lock_);
+  if (is_memory_)
+    return cert_memory_db_path_;
+  else
+    return cert_file_db_path_;
+}
+
+sqlite3* DBHandleProvider::GetMemoryDBHandle(const std::string& filedb_path,
+    const std::string& memorydb_path) {
+  sqlite3* memorydb = nullptr;
+  sqlite3* filedb = nullptr;
+
+  if (sqlite3_open_v2(memorydb_path.c_str(), &memorydb,
+      SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL) != SQLITE_OK) {
+    LOGE("Failed to open memory DB");
+    return nullptr;
+  }
+
+  if (sqlite3_open_v2(filedb_path.c_str(), &filedb,
+      SQLITE_OPEN_READONLY, NULL) != SQLITE_OK) {
+    LOGE("Failed to open file DB");
+    sqlite3_close_v2(memorydb);
+    return nullptr;
+  }
+  sqlite3_backup* backup = sqlite3_backup_init(memorydb, "main",
+      filedb, "main");
+  if (backup == nullptr) {
+    LOGE("Failed to backup for memory DB");
+    sqlite3_close_v2(memorydb);
+    sqlite3_close_v2(filedb);
+    return nullptr;
+  }
+
+  sqlite3_backup_step(backup, -1);
+  sqlite3_backup_finish(backup);
+  LOGD("Set memory DB");
+  sqlite3_close_v2(filedb);
+  return memorydb;
+}
+
+void DBHandleProvider::SetMemoryMode(bool flag) {
+  is_memory_ = false;
+  std::unique_lock<std::mutex> u(lock_);
+  if (is_memory_ == flag)
+    return;
+
+  is_memory_ = flag;
+  if (is_memory_ == true) {
+    sqlite3* parser_db = GetMemoryDBHandle(parser_file_db_path_,
+        parser_memory_db_path_);
+    if (parser_db != nullptr)
+      parser_memory_db_handle_.reset(parser_db);
+    sqlite3* cert_db = GetMemoryDBHandle(cert_file_db_path_,
+        cert_memory_db_path_);
+    if (cert_db != nullptr)
+      cert_memory_db_handle_.reset(cert_db);
+  } else {
+    parser_memory_db_handle_.reset(nullptr);
+    cert_memory_db_handle_.reset(nullptr);
+  }
+
+  LOGD("Set Memory mode : %s", flag ? "Memory" : "File");
+}
+
+}  // namespace pkgmgr_common
diff --git a/src/common/database/db_handle_provider.hh b/src/common/database/db_handle_provider.hh
new file mode 100644 (file)
index 0000000..a855ad7
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SERVER_DB_HANDLE_PROVIDER_HH_
+#define SERVER_DB_HANDLE_PROVIDER_HH_
+
+#include <sqlite3.h>
+#include <stdlib.h>
+
+#include <string>
+#include <memory>
+#include <mutex>
+#include <unordered_map>
+
+#include "shared_object.hh"
+
+namespace pkgmgr_common {
+
+class DBHandleProvider {
+ public:
+  ~DBHandleProvider() = default;
+  static DBHandleProvider& GetInst(uid_t uid);
+  std::string GetParserDBPath();
+  std::string GetCertDBPath();
+  void SetMemoryMode(bool flag);
+
+ private:
+  DBHandleProvider(uid_t uid);
+  sqlite3* GetMemoryDBHandle(const std::string& filedb_path,
+      const std::string& memorydb_path);
+
+ private:
+  static std::unordered_map<uid_t, std::unique_ptr<DBHandleProvider>> provider_;
+  uid_t uid_;
+  std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*> parser_memory_db_handle_;
+  std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*> cert_memory_db_handle_;
+  std::string parser_memory_db_path_;
+  std::string parser_file_db_path_;
+  std::string cert_memory_db_path_;
+  std::string cert_file_db_path_;
+  bool is_memory_;
+  std::mutex lock_;
+};
+
+}  // namespace pkgmgr_common
+
+#endif  // SERVER_DB_HANDLE_PROVIDER_HH_
index b1d21a5..5423724 100644 (file)
@@ -14,6 +14,7 @@
  * limitations under the License.
  */
 
+#include "db_handle_provider.hh"
 #include "pkg_set_db_handler.hh"
 
 #include "pkgmgr_parser_db.h"
@@ -47,6 +48,7 @@ bool PkgSetDBHandler::Execute() {
 
   int ret = 0;
 
+  DBHandleProvider::GetInst(uid_).SetMemoryMode(true);
   if (write_type_ == Insert) {
     ret = pkgmgr_parser_insert_pkg_info(GetConnection(), package_, uid_);
   } else if (write_type_ == Update) {