Cleanup code
authorJunghyun Yeon <jungh.yeon@samsung.com>
Fri, 18 Jun 2021 12:46:40 +0000 (21:46 +0900)
committer연정현/Tizen Platform Lab(SR)/Staff Engineer/삼성전자 <jungh.yeon@samsung.com>
Wed, 23 Jun 2021 03:31:11 +0000 (12:31 +0900)
- Minor refactor codes.

Signed-off-by: Junghyun Yeon <jungh.yeon@samsung.com>
src/server/database/cert_set_db_handler.cc
src/server/database/db_handle_provider.cc
src/server/database/db_handle_provider.hh
src/server/database/pkg_set_db_handler.cc
src/server/request_handler/command_request_handler.cc
src/server/worker_thread.cc

index 9edc508..d89d027 100644 (file)
@@ -43,7 +43,7 @@ int CertSetDBHandler::Execute() {
   if (!Connect())
     return PMINFO_R_ERROR;
 
-  DBHandleProvider::GetInst(uid_).SetMemoryMode(GetPID(), true);
+  DBHandleProvider::GetInst(uid_).SetMemoryMode(GetPID());
   sqlite3* conn = GetConnection().front().first;
 
   return certinfo_internal_set(conn, handle_->pkgid, handle_, uid_);
index a2122b2..61663e0 100644 (file)
@@ -48,6 +48,12 @@ uid_t ConvertUID(uid_t uid) {
     return uid;
 }
 
+static const std::string global_parser_memory_db_path =
+    "file:parserdb?mode=memory&cache=shared";
+
+static const std::string cert_memory_db_path =
+    "file:certdb?mode=memory&cache=shared";
+
 }  // namespace
 
 namespace pkgmgr_server {
@@ -61,18 +67,15 @@ std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
         nullptr, sqlite3_close_v2);
 std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
     DBHandleProvider::cert_memory_db_handle_(nullptr, sqlite3_close_v2);
-std::string DBHandleProvider::global_parser_memory_db_path_ =
-    "file:parserdb?mode=memory&cache=shared";
 std::string DBHandleProvider::global_parser_file_db_path_;
-std::string DBHandleProvider::cert_memory_db_path_ =
-    "file:certdb?mode=memory&cache=shared";
 std::string DBHandleProvider::cert_file_db_path_;
 std::unordered_set<pid_t> DBHandleProvider::pid_list_;
 std::recursive_mutex DBHandleProvider::lock_;
 
 DBHandleProvider::DBHandleProvider(uid_t uid) : uid_(uid),
     is_memory_(false), parser_memory_db_handle_(nullptr, sqlite3_close_v2) {
-  char* tmp_path = nullptr;
+  char* tmp_path;
+
   if (global_parser_file_db_path_.empty()) {
     tmp_path = getUserPkgParserDBPathUID(GetGlobalUID());
     global_parser_file_db_path_ = tmp_path;
@@ -94,6 +97,7 @@ DBHandleProvider::DBHandleProvider(uid_t uid) : uid_(uid),
 DBHandleProvider& DBHandleProvider::GetInst(uid_t uid) {
   static std::mutex singleton_lock;
   std::unique_lock<std::mutex> u(singleton_lock);
+
   uid = ConvertUID(uid);
   auto& prov = provider_[uid];
   if (prov == nullptr)
@@ -104,8 +108,10 @@ DBHandleProvider& DBHandleProvider::GetInst(uid_t uid) {
 
 bool DBHandleProvider::IsCrashedWriteRequest() {
   std::unique_lock<std::recursive_mutex> u(lock_);
+
   if (pid_list_.empty())
     return false;
+
   bool ret = true;
   LOGD("Check process count : %zu", pid_list_.size());
   std::vector<pid_t> remove_pids;
@@ -132,14 +138,15 @@ std::vector<std::pair<std::string, uid_t>> DBHandleProvider::GetParserDBPath(
     pid_t pid, bool write) {
   std::unique_lock<std::recursive_mutex> u(lock_);
   std::vector<std::pair<std::string, uid_t>> db_path_list;
+
   if (is_memory_ != is_memory_global_)
-    SetMemoryMode(pid, is_memory_global_);
+    is_memory_global_ ? SetMemoryMode(pid) : UnsetMemoryMode(pid);
 
-  if (is_memory_ && pid_list_.find(pid) == pid_list_.end() && !write) {
+  if (IsMemoryDBActive(pid, write)) {
     if (uid_ > REGULAR_USER)
       db_path_list.emplace_back(std::make_pair(parser_memory_db_path_, uid_));
     db_path_list.emplace_back(
-        std::make_pair(global_parser_memory_db_path_, GetGlobalUID()));
+        std::make_pair(global_parser_memory_db_path, GetGlobalUID()));
   } else {
     if (uid_ > REGULAR_USER)
       db_path_list.emplace_back(std::make_pair(parser_file_db_path_, uid_));
@@ -160,10 +167,11 @@ std::vector<std::pair<std::string, uid_t>> DBHandleProvider::GetParserDBPath(
 std::string DBHandleProvider::GetCertDBPath(pid_t pid, bool write) {
   std::unique_lock<std::recursive_mutex> u(lock_);
   if (is_memory_ != is_memory_global_)
-    SetMemoryMode(pid, is_memory_global_);
+    is_memory_global_ ?
+        SetMemoryMode(pid) : UnsetMemoryMode(is_memory_global_);
 
-  if ((is_memory_ && pid_list_.find(pid) == pid_list_.end()) && !write)
-    return cert_memory_db_path_;
+  if (IsMemoryDBActive(pid, write))
+    return cert_memory_db_path;
   else
     return cert_file_db_path_;
 }
@@ -186,6 +194,7 @@ sqlite3* DBHandleProvider::GetMemoryDBHandle(const std::string& filedb_path,
     sqlite3_close_v2(memorydb);
     return nullptr;
   }
+
   sqlite3_backup* backup = sqlite3_backup_init(memorydb, "main",
       filedb, "main");
   if (backup == nullptr) {
@@ -201,44 +210,57 @@ sqlite3* DBHandleProvider::GetMemoryDBHandle(const std::string& filedb_path,
   return memorydb;
 }
 
-void DBHandleProvider::SetMemoryMode(pid_t pid, bool flag) {
+void DBHandleProvider::SetMemoryMode(pid_t pid) {
   std::unique_lock<std::recursive_mutex> u(lock_);
-  if (flag == is_memory_global_ && flag == is_memory_)
+  if (is_memory_global_ && is_memory_)
     return;
 
-  if (flag == true) {
-    sqlite3* parser_db = GetMemoryDBHandle(parser_file_db_path_,
-        parser_memory_db_path_);
-    if (parser_db != nullptr)
-      parser_memory_db_handle_.reset(parser_db);
-
-    if (is_memory_ == is_memory_global_) {  /* first call */
-      sqlite3* global_parser_file_db = GetMemoryDBHandle(
-          global_parser_file_db_path_, global_parser_memory_db_path_);
-      if (global_parser_file_db)
-        global_parser_memory_db_handle_.reset(global_parser_file_db);
-      sqlite3* cert_db = GetMemoryDBHandle(cert_file_db_path_,
-          cert_memory_db_path_);
-      if (cert_db != nullptr)
-        cert_memory_db_handle_.reset(cert_db);
-
-      if (pid_list_.find(pid) == pid_list_.end())
-        pid_list_.insert(pid);
-    }
-  } else {
-    parser_memory_db_handle_.reset(nullptr);
-    cert_memory_db_handle_.reset(nullptr);
-    global_parser_memory_db_handle_.reset(nullptr);
-
-    auto it = pid_list_.find(pid);
-    if (it != pid_list_.end())
-      pid_list_.erase(it);
-    else
-      LOGE("Given pid is not exists in pid list : %d", pid);
+  sqlite3* parser_db = GetMemoryDBHandle(parser_file_db_path_,
+      parser_memory_db_path_);
+  if (parser_db != nullptr)
+    parser_memory_db_handle_.reset(parser_db);
+
+  if (is_memory_ == is_memory_global_) {  /* first call */
+    sqlite3* global_parser_file_db = GetMemoryDBHandle(
+        global_parser_file_db_path_, global_parser_memory_db_path);
+    if (global_parser_file_db)
+      global_parser_memory_db_handle_.reset(global_parser_file_db);
+    sqlite3* cert_db = GetMemoryDBHandle(cert_file_db_path_,
+        cert_memory_db_path);
+    if (cert_db != nullptr)
+      cert_memory_db_handle_.reset(cert_db);
+
+    if (pid_list_.find(pid) == pid_list_.end())
+      pid_list_.insert(pid);
   }
-  is_memory_ = flag;
-  is_memory_global_ = flag;
-  LOGD("Set Memory mode : %s", flag ? "Memory" : "File");
+
+  is_memory_ = true;
+  is_memory_global_ = true;
+  LOGD("Set Memory mode : Memory");
+}
+
+void DBHandleProvider::UnsetMemoryMode(pid_t pid) {
+  std::unique_lock<std::recursive_mutex> u(lock_);
+  if (!is_memory_global_ && !is_memory_)
+    return;
+
+  parser_memory_db_handle_.reset(nullptr);
+  cert_memory_db_handle_.reset(nullptr);
+  global_parser_memory_db_handle_.reset(nullptr);
+
+  auto it = pid_list_.find(pid);
+  if (it != pid_list_.end())
+    pid_list_.erase(it);
+  else
+    LOGE("Given pid is not exists in pid list : %d", pid);
+
+  is_memory_ = false;
+  is_memory_global_ = false;
+  LOGD("Set Memory mode : File");
+}
+
+bool DBHandleProvider::IsMemoryDBActive(pid_t pid, bool write) {
+  return (is_memory_ && pid_list_.find(pid) == pid_list_.end() && !write);
 }
 
 }  // namespace database
index 522bfe4..329e9ca 100644 (file)
@@ -45,12 +45,14 @@ class EXPORT_API DBHandleProvider {
   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, bool flag);
+  void SetMemoryMode(pid_t pid);
+  void UnsetMemoryMode(pid_t pid);
 
  private:
   DBHandleProvider(uid_t uid);
   sqlite3* GetMemoryDBHandle(const std::string& filedb_path,
       const std::string& memorydb_path);
+  bool IsMemoryDBActive(pid_t pid, bool write);
 
  private:
   static std::unordered_map<uid_t,
@@ -60,9 +62,7 @@ class EXPORT_API DBHandleProvider {
       global_parser_memory_db_handle_;
   static std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
       cert_memory_db_handle_;
-  static std::string global_parser_memory_db_path_;
   static std::string global_parser_file_db_path_;
-  static std::string cert_memory_db_path_;
   static std::string cert_file_db_path_;
   static std::unordered_set<pid_t> pid_list_;
   static std::recursive_mutex lock_;
index a3cd241..d4c46b1 100644 (file)
@@ -55,7 +55,7 @@ int PkgSetDBHandler::Execute() {
   if (!Connect())
     return PMINFO_R_ERROR;
 
-  DBHandleProvider::GetInst(uid_).SetMemoryMode(GetPID(), true);
+  DBHandleProvider::GetInst(uid_).SetMemoryMode(GetPID());
   std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
   sqlite3* conn = conn_list.front().first;
   int ret = 0;
index 2cc6781..4c8375b 100644 (file)
@@ -41,7 +41,7 @@ bool CommandRequestHandler::HandleRequest(unsigned char* data, int size,
 
   if (parcel->GetCmd() == CommandType::RemoveCache) {
     database::DBHandleProvider::GetInst(
-        parcel->GetUid()).SetMemoryMode(GetPID(), false);
+        parcel->GetUid()).UnsetMemoryMode(GetPID());
     result_ = std::make_shared<pcp::ResultParcelable>(
         PMINFO_R_OK, std::vector<std::vector<std::string>>{});
     return true;
index 7bbd42a..983b4f4 100644 (file)
@@ -170,7 +170,7 @@ gboolean WorkerThread::TrimMemory(void* data) {
 
   if (database::DBHandleProvider::IsCrashedWriteRequest())
     database::DBHandleProvider::
-        GetInst(getuid()).SetMemoryMode(getpid(), false);
+        GetInst(getuid()).UnsetMemoryMode(getpid());
 
   return false;
 }