Remove the memory database for the cache 84/299884/4
authorilho kim <ilho159.kim@samsung.com>
Wed, 11 Oct 2023 10:22:48 +0000 (19:22 +0900)
committerilho kim <ilho159.kim@samsung.com>
Thu, 12 Oct 2023 06:12:39 +0000 (15:12 +0900)
There is no need to maintain memory database because
pkginfo-server is already caching the handles of pkginfo and appinfo

Change-Id: I1158e980880c8945cad14b338ec8e0d70dc5b5ac
Signed-off-by: ilho kim <ilho159.kim@samsung.com>
src/server/database/abstract_db_handler.cc
src/server/database/cert_set_db_handler.cc
src/server/database/cert_set_db_handler.hh
src/server/database/db_handle_provider.cc
src/server/database/db_handle_provider.hh
src/server/database/pkg_set_db_handler.cc
src/server/database/remove_cache_db_handler.cc
src/server/worker_thread.cc

index 92b96c5..e67aac7 100644 (file)
@@ -112,13 +112,10 @@ AbstractDBHandler::~AbstractDBHandler() = default;
 std::vector<std::pair<std::string, uid_t>> AbstractDBHandler::GetDBPath() {
   std::vector<std::pair<std::string, uid_t>> db_path;
   if (db_type_ == pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB)
-    db_path = DBHandleProvider::GetInst(uid_).GetParserDBPath(pid_,
-        op_type_ == pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE);
+    db_path = DBHandleProvider::GetInst(uid_).GetParserDBPath();
   else if (db_type_ == pkgmgr_common::DBType::DB_TYPE_FILE_CERTDB)
     db_path.emplace_back(
-        std::make_pair(DBHandleProvider::GetInst(uid_).GetCertDBPath(pid_,
-            op_type_ == pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE),
-            uid_));
+        std::make_pair(DBHandleProvider::GetInst(uid_).GetCertDBPath(), uid_));
 
   return db_path;
 }
index bd0144c..381133c 100644 (file)
@@ -26,8 +26,7 @@ namespace pkgmgr_server {
 namespace database {
 
 CertSetDBHandler::CertSetDBHandler(uid_t uid, int pid, bool is_offline)
-    : AbstractDBHandler(uid, pid), uid_(uid),
-      is_offline_(is_offline), handle_(nullptr) {}
+    : AbstractDBHandler(uid, pid), uid_(uid), handle_(nullptr) {}
 
 CertSetDBHandler::~CertSetDBHandler() {}
 
@@ -44,8 +43,6 @@ int CertSetDBHandler::Execute() {
   if (!Connect())
     return PMINFO_R_ERROR;
 
-  if (!is_offline_)
-    DBHandleProvider::GetInst(uid_).SetMemoryMode(GetPID());
   const auto& db = GetConnection().front().first;
 
   return internal::CertInfoSet(db, handle_->pkgid, handle_, uid_);
index e45db18..bbc86e3 100644 (file)
@@ -41,7 +41,6 @@ class EXPORT_API CertSetDBHandler : public AbstractDBHandler{
 
  private:
   uid_t uid_;
-  bool is_offline_;
   pkgmgr_certinfo_x* handle_;
 };
 
index 8f33241..6f2b25e 100644 (file)
@@ -76,12 +76,6 @@ bool GetModifiedTime(const char* dbpath, timespec* t) {
   return true;
 }
 
-static const std::string global_parser_memdb_path =
-    "file:parserdb?mode=memory&cache=shared";
-
-static const std::string cert_memdb_path =
-    "file:certdb?mode=memory&cache=shared";
-
 }  // namespace
 
 namespace pkgmgr_server {
@@ -89,22 +83,13 @@ namespace database {
 
 std::unordered_map<uid_t, std::unique_ptr<DBHandleProvider>>
     DBHandleProvider::provider_map_;
-bool DBHandleProvider::is_global_memdb_set_ = false;
-std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
-    DBHandleProvider::global_parser_memdb_handle_(nullptr,
-                                                  sqlite3_close_v2);
-std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
-    DBHandleProvider::cert_memdb_handle_(nullptr, sqlite3_close_v2);
 std::string DBHandleProvider::global_parser_filedb_path_;
 std::string DBHandleProvider::cert_filedb_path_;
 std::unordered_set<pid_t> DBHandleProvider::writer_pid_list_;
 std::recursive_mutex DBHandleProvider::lock_;
 std::shared_mutex DBHandleProvider::pid_list_lock_;
 
-DBHandleProvider::DBHandleProvider(uid_t uid)
-    : uid_(uid),
-      is_user_memdb_set_(false),
-      parser_memdb_handle_(nullptr, sqlite3_close_v2) {
+DBHandleProvider::DBHandleProvider(uid_t uid) : uid_(uid) {
   char* tmp_path;
 
   if (global_parser_filedb_path_.empty()) {
@@ -122,10 +107,6 @@ DBHandleProvider::DBHandleProvider(uid_t uid)
   tmp_path = getUserPkgParserDBPathUID(uid_);
   user_parser_filedb_path_ = tmp_path;
   free(tmp_path);
-
-  user_parser_memdb_path_ = "file:parserdb" +
-                            std::to_string(static_cast<int>(uid_)) +
-                            "?mode=memory&cache=shared";
 }
 
 DBHandleProvider& DBHandleProvider::GetInst(uid_t uid) {
@@ -174,27 +155,15 @@ bool DBHandleProvider::IsCrashedWriteRequest() {
   return ret;
 }
 
-std::vector<std::pair<std::string, uid_t>> DBHandleProvider::GetParserDBPath(
-    pid_t pid, bool write) {
+std::vector<std::pair<std::string, uid_t>> DBHandleProvider::GetParserDBPath() {
   std::unique_lock<std::recursive_mutex> u(lock_);
   std::vector<std::pair<std::string, uid_t>> db_path_list;
 
-  if (is_user_memdb_set_ != is_global_memdb_set_)
-    is_global_memdb_set_ ? SetMemoryMode(pid) : UnsetMemoryMode(pid);
-
-  if (IsMemoryDBActive(pid, write)) {
-    if (uid_ > REGULAR_USER)
-      db_path_list.emplace_back(std::make_pair(user_parser_memdb_path_, uid_));
+  if (uid_ > REGULAR_USER)
+    db_path_list.emplace_back(std::make_pair(user_parser_filedb_path_, uid_));
 
-    db_path_list.emplace_back(
-        std::make_pair(global_parser_memdb_path, GetGlobalUID()));
-  } else {
-    if (uid_ > REGULAR_USER)
-      db_path_list.emplace_back(std::make_pair(user_parser_filedb_path_, uid_));
-
-    db_path_list.emplace_back(
-        std::make_pair(global_parser_filedb_path_, GetGlobalUID()));
-  }
+  db_path_list.emplace_back(
+      std::make_pair(global_parser_filedb_path_, GetGlobalUID()));
 
   if (db_path_list.size() == 1) {
     LOG(DEBUG) << "global db path : " << db_path_list[0].first;
@@ -206,112 +175,8 @@ std::vector<std::pair<std::string, uid_t>> DBHandleProvider::GetParserDBPath(
   return db_path_list;
 }
 
-std::string DBHandleProvider::GetCertDBPath(pid_t pid, bool write) {
-  std::unique_lock<std::recursive_mutex> u(lock_);
-  if (is_user_memdb_set_ != is_global_memdb_set_)
-    is_global_memdb_set_ ? SetMemoryMode(pid) : UnsetMemoryMode(pid);
-
-  if (IsMemoryDBActive(pid, write))
-    return cert_memdb_path;
-  else
-    return cert_filedb_path_;
-}
-
-sqlite3* DBHandleProvider::CreateMemoryDBHandle(const std::string& filedb_path,
-                                             const std::string& memorydb_path) {
-  sqlite3* memorydb = nullptr;
-  sqlite3* filedb = nullptr;
-  LOG(INFO) << "Create memory db handle : " << memorydb_path;
-  int ret = sqlite3_open_v2(memorydb_path.c_str(), &memorydb,
-                            SQLITE_OPEN_READONLY | SQLITE_OPEN_URI, nullptr);
-  if (ret != SQLITE_OK) {
-    LOG(ERROR) << "Failed to open memory DB " << ret << ": " << memorydb_path;
-    return nullptr;
-  }
-
-  ret = sqlite3_open_v2(filedb_path.c_str(), &filedb, SQLITE_OPEN_READONLY,
-                        nullptr);
-  if (ret != SQLITE_OK) {
-    LOG(ERROR) << "Failed to open file DB " << ret << ": " << filedb_path;
-    sqlite3_close_v2(memorydb);
-    return nullptr;
-  }
-
-  sqlite3_backup* backup =
-      sqlite3_backup_init(memorydb, "main", filedb, "main");
-  if (backup == nullptr) {
-    LOG(ERROR) << "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);
-  sqlite3_close_v2(filedb);
-  LOG(INFO) << "Create memory db handle done";
-  return memorydb;
-}
-
-void DBHandleProvider::SetMemoryMode(pid_t pid) {
-  std::unique_lock<std::recursive_mutex> u(lock_);
-  if (is_global_memdb_set_ && is_user_memdb_set_)
-    return;
-
-  sqlite3* parser_db =
-      CreateMemoryDBHandle(user_parser_filedb_path_, user_parser_memdb_path_);
-  if (parser_db != nullptr)
-    parser_memdb_handle_.reset(parser_db);
-
-  if (is_user_memdb_set_ == is_global_memdb_set_) {
-    sqlite3* global_parser_file_db = CreateMemoryDBHandle(
-        global_parser_filedb_path_, global_parser_memdb_path);
-    if (global_parser_file_db)
-      global_parser_memdb_handle_.reset(global_parser_file_db);
-
-    sqlite3* cert_db =
-        CreateMemoryDBHandle(cert_filedb_path_, cert_memdb_path);
-    if (cert_db != nullptr)
-      cert_memdb_handle_.reset(cert_db);
-
-    InsertPID(pid);
-  }
-
-  is_user_memdb_set_ = true;
-  is_global_memdb_set_ = true;
-  LOG(INFO) << "Set Memory mode : Memory";
-}
-
-void DBHandleProvider::UnsetMemoryMode(pid_t pid) {
-  std::unique_lock<std::recursive_mutex> u(lock_);
-  if (!is_global_memdb_set_ && !is_user_memdb_set_)
-    return;
-
-  parser_memdb_handle_.reset(nullptr);
-  cert_memdb_handle_.reset(nullptr);
-  global_parser_memdb_handle_.reset(nullptr);
-
-  if (!ErasePID(pid))
-    LOG(ERROR) << "Given pid is not exists in pid list : " << pid;
-
-  is_user_memdb_set_ = false;
-  is_global_memdb_set_ = false;
-
-  LOG(DEBUG) << "Set Memory mode : File";
-}
-
-bool DBHandleProvider::IsMemoryDBActive(pid_t pid, bool write) {
-  std::unique_lock<std::shared_mutex> u(pid_list_lock_);
-  if (!is_user_memdb_set_)
-    return false;
-
-  if (write)
-    return false;
-
-  if (writer_pid_list_.find(pid) != writer_pid_list_.end())
-    return false;
-
-  return true;
+std::string DBHandleProvider::GetCertDBPath() {
+  return cert_filedb_path_;
 }
 
 void DBHandleProvider::TrimCache() {
@@ -346,15 +211,10 @@ int DBHandleProvider::UpdateCache(const tizen_base::Database& db, pid_t pid,
   pkg_app_map_.clear();
 
   const char* dbpath = sqlite3_db_filename(db.GetRaw(), "main");
-  bool is_inmemory_db = false;
-  if (dbpath == nullptr || strlen(dbpath) == 0) {
-    LOG(INFO) << "database is inmemory db";
-    is_inmemory_db = true;
-  }
 
   timespec start_time = { 0, };
   timespec end_time = { 0, };
-  if (!is_inmemory_db && !GetModifiedTime(dbpath, &start_time))
+  if (!GetModifiedTime(dbpath, &start_time))
     return PMINFO_R_ERROR;
 
   pkgmgrinfo_filter_x tmp_filter = { 0, };
@@ -366,7 +226,7 @@ int DBHandleProvider::UpdateCache(const tizen_base::Database& db, pid_t pid,
       AddPackage(std::move(key), std::move(val));
   }
 
-  if (!is_inmemory_db && !GetModifiedTime(dbpath, &end_time))
+  if (!GetModifiedTime(dbpath, &end_time))
     return PMINFO_R_ERROR;
 
   if (start_time.tv_sec != end_time.tv_sec ||
@@ -378,7 +238,7 @@ int DBHandleProvider::UpdateCache(const tizen_base::Database& db, pid_t pid,
   std::vector<std::shared_ptr<application_x>> app_list;
   ret = internal::GetAppInfo(db, &tmp_filter, uid_, uid, locale, app_list);
 
-  if (!is_inmemory_db && !GetModifiedTime(dbpath, &end_time))
+  if (!GetModifiedTime(dbpath, &end_time))
     return PMINFO_R_ERROR;
 
   if (start_time.tv_sec != end_time.tv_sec ||
index e8ec9ee..6634917 100644 (file)
@@ -47,11 +47,8 @@ class EXPORT_API DBHandleProvider {
   static DBHandleProvider& GetInst(uid_t uid);
   static bool IsCrashedWriteRequest();
   static bool IsWriter(pid_t pid);
-  std::vector<std::pair<std::string, uid_t>> GetParserDBPath(int pid,
-                                                             bool write);
-  std::string GetCertDBPath(int pid, bool write);
-  void SetMemoryMode(pid_t pid);
-  void UnsetMemoryMode(pid_t pid);
+  std::vector<std::pair<std::string, uid_t>> GetParserDBPath();
+  std::string GetCertDBPath();
   int UpdateCache(const tizen_base::Database& db, pid_t pid, uid_t uid,
       bool write, const std::string& locale);
   std::vector<std::shared_ptr<package_x>> GetPackages(
@@ -73,9 +70,6 @@ class EXPORT_API DBHandleProvider {
 
  private:
   explicit DBHandleProvider(uid_t uid);
-  sqlite3* CreateMemoryDBHandle(const std::string& filedb_path,
-      const std::string& memorydb_path);
-  bool IsMemoryDBActive(pid_t pid, bool write);
   void ReleaseCache();
   void AddPackage(std::string package, std::shared_ptr<package_x> info);
   void AddApplication(std::string app, std::shared_ptr<application_x> info);
@@ -86,11 +80,6 @@ class EXPORT_API DBHandleProvider {
  private:
   static std::unordered_map<uid_t,
         std::unique_ptr<DBHandleProvider>> provider_map_;
-  static bool is_global_memdb_set_;
-  static std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
-      global_parser_memdb_handle_;
-  static std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
-      cert_memdb_handle_;
   static std::string global_parser_filedb_path_;
   static std::string cert_filedb_path_;
   static std::unordered_set<pid_t> writer_pid_list_;
@@ -98,9 +87,6 @@ class EXPORT_API DBHandleProvider {
   static std::shared_mutex pid_list_lock_;
 
   uid_t uid_;
-  bool is_user_memdb_set_;
-  std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
-      parser_memdb_handle_;
   std::string user_parser_memdb_path_;
   std::string user_parser_filedb_path_;
   bool released_ = true;
index 1379ebe..445828d 100644 (file)
@@ -57,9 +57,6 @@ int PkgSetDBHandler::Execute() {
   if (!Connect())
     return PMINFO_R_ERROR;
 
-  if (!is_offline_)
-    DBHandleProvider::GetInst(uid_).SetMemoryMode(GetPID());
-
   const auto& db = GetConnection().front().first;
   int ret = 0;
 
index 20e542b..c37e410 100644 (file)
@@ -30,8 +30,6 @@ int RemoveCacheDBHandler::Execute() {
   SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_READ);
   SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
 
-  database::DBHandleProvider::GetInst(GetUID()).UnsetMemoryMode(GetPID());
-
   if (!Connect()) {
     database::DBHandleProvider::GetInst(GetUID()).TrimCache();
     return PMINFO_R_ERROR;
index 010111d..ab9b637 100644 (file)
@@ -181,9 +181,6 @@ gboolean WorkerThread::TrimMemory(void* data) {
     h->timer_ = 0;
   }
 
-  if (database::DBHandleProvider::IsCrashedWriteRequest())
-    database::DBHandleProvider::GetInst(getuid()).UnsetMemoryMode(getpid());
-
   sqlite3_release_memory(-1);
   malloc_trim(0);
   return G_SOURCE_REMOVE;