Create cache on a separate thread 30/265930/18
authorIlho Kim <ilho159.kim@samsung.com>
Mon, 1 Nov 2021 09:01:54 +0000 (18:01 +0900)
committerilho kim <ilho159.kim@samsung.com>
Wed, 5 Jan 2022 07:43:07 +0000 (07:43 +0000)
Change-Id: Ia00e4dea4b1b38cc7bd3876ac1d7695e6997c716
Signed-off-by: Ilho Kim <ilho159.kim@samsung.com>
25 files changed:
src/common/request_type.cc
src/common/request_type.hh
src/server/cache_flag.hh [new file with mode: 0644]
src/server/create_cache_request.cc [new file with mode: 0644]
src/server/create_cache_request.hh [new file with mode: 0644]
src/server/database/abstract_db_handler.hh
src/server/database/appinfo_cache_db_handler.cc [deleted file]
src/server/database/appinfo_cache_db_handler.hh [deleted file]
src/server/database/appinfo_db_handler.cc
src/server/database/appinfo_db_handler.hh
src/server/database/cache_db_handler.cc [new file with mode: 0644]
src/server/database/cache_db_handler.hh [new file with mode: 0644]
src/server/database/db_handle_provider.cc
src/server/database/db_handle_provider.hh
src/server/database/pkg_get_cache_db_handler.cc [deleted file]
src/server/database/pkg_get_cache_db_handler.hh [deleted file]
src/server/database/pkg_get_db_handler.cc
src/server/database/pkg_get_db_handler.hh
src/server/pkg_request.hh
src/server/request_handler/create_cache_request_handler.cc [new file with mode: 0644]
src/server/request_handler/create_cache_request_handler.hh [new file with mode: 0644]
src/server/request_handler/get_appinfo_request_handler.cc
src/server/request_handler/get_pkginfo_request_handler.cc
src/server/runner.cc
src/server/worker_thread.cc

index ffc06c67affccf523968f5926d337ca9e8ee063f..6a397601b2e63138f2fd0eadcd353e6eae9b3549 100644 (file)
@@ -29,6 +29,7 @@ const char* ReqTypeToString(ReqType type) {
       "QUERY",
       "COMMAND",
       "CREATE_DB",
+      "CREATE_CACHE",
       "ERROR_REQ_TYPE"
   };
 
index f3512db2853d85497448879b9acb454c52a7a2d8..23e6d00ecd71007a48e17f21833384c501fdd89d 100644 (file)
@@ -36,6 +36,7 @@ enum ReqType {
   QUERY,
   COMMAND,
   CREATE_DB,
+  CREATE_CACHE,
   MAX
 };
 
diff --git a/src/server/cache_flag.hh b/src/server/cache_flag.hh
new file mode 100644 (file)
index 0000000..67aa9a0
--- /dev/null
@@ -0,0 +1,81 @@
+/*
+ * 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 CACHE_FLAG_HH_
+#define CACHE_FLAG_HH_
+
+#include <mutex>
+#include <shared_mutex>
+
+namespace pkgmgr_server {
+
+#ifndef EXPORT_API
+#define EXPORT_API __attribute__((visibility("default")))
+#endif
+
+class EXPORT_API CacheFlag {
+ public:
+  enum class EXPORT_API Status {
+    UNPREPARED,
+    PREPARING,
+    PREPARED
+  };
+
+  static bool SetPreparing() {
+    bool ret = false;
+    std::unique_lock<std::mutex> u(status_lock_);
+    if (status_ == Status::UNPREPARED) {
+      status_ = Status::PREPARING;
+      ret = true;
+    }
+    return ret;
+  }
+
+  static void SetStatus(Status status) {
+    std::unique_lock<std::mutex> u(status_lock_);
+    status_ = status;
+  }
+
+  static Status GetStatus() {
+    std::unique_lock<std::mutex> u(status_lock_);
+    return status_;
+  }
+
+  static std::unique_lock<std::shared_timed_mutex> GetWriterLock() {
+    return std::unique_lock<std::shared_timed_mutex>(lock_);
+  }
+
+  static void WriterUnLock() {
+    lock_.unlock();
+  }
+
+  static std::shared_lock<std::shared_timed_mutex> GetReaderLock() {
+    return std::shared_lock<std::shared_timed_mutex>(lock_, std::defer_lock);
+  }
+
+  static void ReaderUnLock() {
+    lock_.unlock_shared();
+  }
+
+ private:
+  static inline Status status_;
+  static inline std::mutex status_lock_;
+  static inline std::shared_timed_mutex lock_;
+};
+
+}  // namespace pkgmgr_server
+
+#endif  // CACHE_FLAG_HH_
diff --git a/src/server/create_cache_request.cc b/src/server/create_cache_request.cc
new file mode 100644 (file)
index 0000000..43bef9e
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * 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 "create_cache_request.hh"
+
+#include <sys/types.h>
+#include <unistd.h>
+
+namespace pkgmgr_server {
+
+unsigned char* CreateCacheRequest::GetData() {
+  return nullptr;
+}
+
+int CreateCacheRequest::GetSize() {
+  return 0;
+}
+
+pid_t CreateCacheRequest::GetSenderPID() {
+  return getpid();
+}
+
+uid_t CreateCacheRequest::GetSenderUID() {
+  return uid_;
+}
+
+pkgmgr_common::ReqType CreateCacheRequest::GetRequestType() {
+  return pkgmgr_common::ReqType::CREATE_CACHE;
+}
+
+bool CreateCacheRequest::SendData(unsigned char* data, int size) {
+  return true;
+}
+
+}  // namespace pkgmgr_server
diff --git a/src/server/create_cache_request.hh b/src/server/create_cache_request.hh
new file mode 100644 (file)
index 0000000..a428f4e
--- /dev/null
@@ -0,0 +1,47 @@
+/*
+ * 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_CREATE_CACHE_REQUEST_HH_
+#define SERVER_CREATE_CACHE_REQUEST_HH_
+
+#include <sys/types.h>
+
+#include "pkg_request.hh"
+#include "request_type.hh"
+
+namespace pkgmgr_server {
+
+#ifndef EXPORT_API
+#define EXPORT_API __attribute__((visibility("default")))
+#endif
+
+class EXPORT_API CreateCacheRequest : public PkgRequest {
+ public:
+  CreateCacheRequest(uid_t uid) : uid_(uid) {}
+  unsigned char* GetData() override;
+  int GetSize() override;
+  pid_t GetSenderPID() override;
+  uid_t GetSenderUID() override;
+  pkgmgr_common::ReqType GetRequestType() override;
+  bool SendData(unsigned char* data, int size) override;
+
+ private:
+  uid_t uid_;
+};
+
+}  // namespace pkgmgr_server
+
+#endif  // SERVER_CREATE_CACHE_REQUEST_HH_
index afbc4aee43474e5bcae773187944cc70b9f8231f..ced7b5eb28f2e561f6da0f4f80fff7f320e96455 100644 (file)
@@ -59,7 +59,6 @@ class EXPORT_API AbstractDBHandler {
   static std::shared_timed_mutex lock_;
 
  private:
-
   pkgmgr_common::DBType db_type_;
   pkgmgr_common::DBOperationType op_type_;
   uid_t uid_;
diff --git a/src/server/database/appinfo_cache_db_handler.cc b/src/server/database/appinfo_cache_db_handler.cc
deleted file mode 100644 (file)
index 64462b2..0000000
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * 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 "appinfo_cache_db_handler.hh"
-
-#include <shared_mutex>
-#include <vector>
-
-#include "db_handle_provider.hh"
-#include "pkgmgrinfo_basic.h"
-#include "pkgmgrinfo_debug.h"
-#include "utils/logging.hh"
-
-namespace pkgmgr_server {
-namespace database {
-
-AppInfoCacheDBHandler::AppInfoCacheDBHandler(uid_t uid, int pid)
-    : AppInfoDBHandler(uid, pid) {}
-
-int AppInfoCacheDBHandler::Execute() {
-  std::shared_lock<std::shared_timed_mutex> s(lock_);
-  SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_READ);
-  SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
-  bool write =
-      GetOpType() == pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE;
-
-  std::vector<std::pair<sqlite3*, uid_t>> conn_list;
-  if (!Connect())
-    return PMINFO_R_ERROR;
-
-  conn_list = GetConnection();
-
-  int ret = PMINFO_R_OK;
-  std::string application;
-
-  for (auto* it = filter_->list; it != nullptr; it = g_slist_next(it)) {
-    auto node = reinterpret_cast<pkgmgrinfo_node_x*>(it->data);
-    if (node->prop == E_PMINFO_APPINFO_PROP_APP_ID) {
-      application = node->value;
-      break;
-    }
-  }
-
-  std::vector<std::shared_ptr<application_x>> app_list;
-  for (auto& conn : conn_list) {
-    ret = DBHandleProvider::GetInst(conn.second)
-              .UpdateCache(conn.first, GetPID(), uid_, write, GetLocale());
-    if (ret != PMINFO_R_OK) {
-      LOG(DEBUG) << "Failed to update appinfo cache: " << ret;
-      break;
-    }
-
-    app_list = DBHandleProvider::GetInst(conn.second)
-                   .GetApplications(GetPID(), write, filter_, application);
-
-    handle_list_.reserve(app_list.size() + handle_list_.size());
-    std::move(std::begin(app_list), std::end(app_list),
-              std::back_inserter(handle_list_));
-  }
-
-  if (handle_list_.empty())
-    ret = PMINFO_R_ENOENT;
-
-  return ret;
-}
-
-}  // namespace database
-}  // namespace pkgmgr_server
diff --git a/src/server/database/appinfo_cache_db_handler.hh b/src/server/database/appinfo_cache_db_handler.hh
deleted file mode 100644 (file)
index ec29b64..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * 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 APPINFO_CACHE_DB_HANDLER_HH_
-#define APPINFO_CACHE_DB_HANDLER_HH_
-
-#include <vector>
-
-#include <sys/types.h>
-
-#include "appinfo_db_handler.hh"
-#include "pkgmgrinfo_basic.h"
-#include "pkgmgrinfo_private.h"
-
-namespace pkgmgr_server {
-namespace database {
-
-#ifndef EXPORT_API
-#define EXPORT_API __attribute__((visibility("default")))
-#endif
-
-class EXPORT_API AppInfoCacheDBHandler : public AppInfoDBHandler {
- public:
-  AppInfoCacheDBHandler(uid_t uid, int pid);
-  int Execute() override;
-};
-
-}  // namespace database
-}  // namespace pkgmgr_server
-
-#endif  // APPINFO_CACHE_DB_HANDLER_HH_
index 8fe5e9f2633a5c59556469750d56a9085327cc0f..fec4d2c595304138def7be643e9bd7e48f322a05 100644 (file)
 #include <shared_mutex>
 #include <vector>
 
+#include "cache_flag.hh"
+#include "db_handle_provider.hh"
 #include "utils/logging.hh"
 
 #include "pkgmgrinfo_basic.h"
-#include "pkgmgrinfo_debug.h"
 #include "pkgmgrinfo_internal.h"
 
 namespace {
@@ -55,17 +56,10 @@ void AppInfoDBHandler::SetFilter(pkgmgrinfo_filter_x* filter) {
   filter_ = filter;
 }
 
-int AppInfoDBHandler::Execute() {
-  std::shared_lock<std::shared_timed_mutex> s(lock_);
-  SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_READ);
-  SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
-
-  if (!Connect())
-    return PMINFO_R_ERROR;
-
+int AppInfoDBHandler::GetHandleFromDB(
+    std::vector<std::pair<sqlite3*, uid_t>>& conn_list) {
   GHashTable* list =
       g_hash_table_new_full(g_str_hash, g_str_equal, nullptr, nullptr);
-  std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
   int ret = PMINFO_R_OK;
   for (auto& conn : conn_list) {
     ret = appinfo_internal_filter_get_list(conn.first, filter_, conn.second,
@@ -86,5 +80,65 @@ int AppInfoDBHandler::Execute() {
   return ret;
 }
 
+int AppInfoDBHandler::GetHandleFromCache(
+    std::vector<std::pair<sqlite3*, uid_t>>& conn_list) {
+  bool is_write_op =
+      GetOpType() == pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE;
+  int ret = PMINFO_R_OK;
+  std::string application;
+
+  for (auto* it = filter_->list; it != nullptr; it = g_slist_next(it)) {
+    auto node = reinterpret_cast<pkgmgrinfo_node_x*>(it->data);
+    if (node->prop == E_PMINFO_APPINFO_PROP_APP_ID) {
+      application = node->value;
+      break;
+    }
+  }
+
+  std::vector<std::shared_ptr<application_x>> app_list;
+  for (auto& conn : conn_list) {
+    app_list = DBHandleProvider::GetInst(conn.second)
+                  .GetApplications(
+                      GetPID(), is_write_op, filter_, application);
+
+    handle_list_.reserve(app_list.size() + handle_list_.size());
+    std::move(std::begin(app_list), std::end(app_list),
+              std::back_inserter(handle_list_));
+  }
+
+  if (handle_list_.empty())
+    ret = PMINFO_R_ENOENT;
+
+  return ret;
+}
+
+int AppInfoDBHandler::Execute() {
+  std::shared_lock<std::shared_timed_mutex> s(lock_);
+  SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_READ);
+  SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
+
+  if (!Connect())
+    return PMINFO_R_ERROR;
+
+  std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
+
+  bool is_writer = false;
+  for (auto& conn : conn_list) {
+    if (DBHandleProvider::GetInst(conn.second).IsWriter(GetPID()))
+      is_writer = true;
+  }
+  if (is_writer)
+    return GetHandleFromDB(conn_list);
+
+  if (CacheFlag::GetStatus() == CacheFlag::Status::PREPARED) {
+    auto cache_lock = CacheFlag::GetReaderLock();
+    if (cache_lock.try_lock() &&
+        CacheFlag::GetStatus() == CacheFlag::Status::PREPARED)
+      return GetHandleFromCache(conn_list);
+  }
+
+  return GetHandleFromDB(conn_list);
+}
+
 }  // namespace database
 }  // namespace pkgmgr_server
index 4cbf651f0d3128a0d8ee1f5cf6450e80af3c85d0..a095a58712676032fa69d0e2a78369d0c298ab52 100644 (file)
@@ -36,12 +36,14 @@ namespace database {
 class EXPORT_API AppInfoDBHandler : public AbstractDBHandler{
  public:
   AppInfoDBHandler(uid_t uid, int pid);
-  ~AppInfoDBHandler();
+  virtual ~AppInfoDBHandler();
   std::vector<std::shared_ptr<application_x>> GetAppHandle();
   void SetFilter(pkgmgrinfo_filter_x* filter);
   virtual int Execute();
 
  protected:
+  int GetHandleFromDB(std::vector<std::pair<sqlite3*, uid_t>>& conn_list);
+  int GetHandleFromCache(std::vector<std::pair<sqlite3*, uid_t>>& conn_list);
   pkgmgrinfo_filter_x* filter_;
   std::vector<std::shared_ptr<application_x>> handle_list_;
   uid_t uid_;
diff --git a/src/server/database/cache_db_handler.cc b/src/server/database/cache_db_handler.cc
new file mode 100644 (file)
index 0000000..aaf1592
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * 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 "cache_db_handler.hh"
+
+#include <shared_mutex>
+#include <vector>
+
+#include "cache_flag.hh"
+#include "db_handle_provider.hh"
+#include "utils/logging.hh"
+
+namespace pkgmgr_server {
+namespace database {
+
+CacheDBHandler::CacheDBHandler(uid_t uid, int pid)
+    : AbstractDBHandler(uid, pid), uid_(uid) {}
+
+CacheDBHandler::~CacheDBHandler() {}
+
+int CacheDBHandler::Execute() {
+  std::shared_lock<std::shared_timed_mutex> s(lock_);
+  SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_READ);
+  SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
+
+  std::vector<std::pair<sqlite3*, uid_t>> conn_list;
+  if (!Connect()) {
+    CacheFlag::SetStatus(CacheFlag::Status::UNPREPARED);
+    return PMINFO_R_ERROR;
+  }
+
+  conn_list = GetConnection();
+  auto lock = CacheFlag::GetWriterLock();
+  int ret = PMINFO_R_OK;
+  for (auto& conn : conn_list) {
+    ret = DBHandleProvider::GetInst(conn.second)
+              .UpdateCache(conn.first, GetPID(), uid_, false, GetLocale());
+    if (ret != PMINFO_R_OK) {
+      LOG(ERROR) << "Failed to update pkginfo cache : " << ret;
+      break;
+    }
+  }
+
+  CacheFlag::SetStatus(ret == PMINFO_R_OK ?
+      CacheFlag::Status::PREPARED :
+      CacheFlag::Status::UNPREPARED);
+
+  return ret;
+}
+
+}  // namespace database
+}  // namespace pkgmgr_server
diff --git a/src/server/database/cache_db_handler.hh b/src/server/database/cache_db_handler.hh
new file mode 100644 (file)
index 0000000..0e73b0b
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * 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 CACHE_DB_HANDLER_HH_
+#define CACHE_DB_HANDLER_HH_
+
+#include <vector>
+
+#include <sys/types.h>
+
+#include "abstract_db_handler.hh"
+#include "pkgmgrinfo_basic.h"
+#include "pkgmgrinfo_private.h"
+
+namespace pkgmgr_server {
+namespace database {
+
+#ifndef EXPORT_API
+#define EXPORT_API __attribute__((visibility("default")))
+#endif
+
+class EXPORT_API CacheDBHandler : public AbstractDBHandler {
+ public:
+  CacheDBHandler(uid_t uid, int pid);
+  ~CacheDBHandler();
+  int Execute();
+
+ protected:
+  pkgmgrinfo_filter_x* filter_ = nullptr;
+  std::vector<std::shared_ptr<package_x>> handle_list_;
+  uid_t uid_;
+};
+
+}  // namespace database
+}  // namespace pkgmgr_server
+
+#endif  // CACHE_DB_HANDLER_HH_
index 43f00b2835a295d87c4daa4bd2452401925412c9..6b3579fe6a5bbb05f379bce4d89d21cbabf5c623 100644 (file)
@@ -26,6 +26,7 @@
 #include <string>
 #include <vector>
 
+#include "cache_flag.hh"
 #include "utils/logging.hh"
 
 #include "pkgmgr-info.h"
@@ -79,6 +80,7 @@ std::string DBHandleProvider::global_parser_file_db_path_;
 std::string DBHandleProvider::cert_file_db_path_;
 std::unordered_set<pid_t> DBHandleProvider::pid_list_;
 std::recursive_mutex DBHandleProvider::lock_;
+std::mutex DBHandleProvider::pid_list_lock_;
 
 DBHandleProvider::DBHandleProvider(uid_t uid)
     : uid_(uid),
@@ -118,7 +120,7 @@ DBHandleProvider& DBHandleProvider::GetInst(uid_t uid) {
 }
 
 bool DBHandleProvider::IsCrashedWriteRequest() {
-  std::unique_lock<std::recursive_mutex> u(lock_);
+  std::unique_lock<std::mutex> u(pid_list_lock_);
 
   if (pid_list_.empty())
     return false;
@@ -240,15 +242,11 @@ void DBHandleProvider::SetMemoryMode(pid_t pid) {
     if (cert_db != nullptr)
       cert_memory_db_handle_.reset(cert_db);
 
-    if (pid_list_.find(pid) == pid_list_.end())
-      pid_list_.insert(pid);
+    InsertPID(pid);
   }
 
   is_memory_ = true;
   is_memory_global_ = true;
-  old_pkg_map_ = std::move(pkg_map_);
-  old_app_map_ = std::move(app_map_);
-  released_ = true;
   LOG(DEBUG) << "Set Memory mode : Memory";
 }
 
@@ -257,24 +255,26 @@ void DBHandleProvider::UnsetMemoryMode(pid_t pid) {
   if (!is_memory_global_ && !is_memory_)
     return;
 
+  auto lock = CacheFlag::GetWriterLock();
+  CacheFlag::SetStatus(CacheFlag::Status::PREPARING);
+
   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
+  if (!ErasePID(pid))
     LOG(ERROR) << "Given pid is not exists in pid list : " << pid;
 
   is_memory_ = false;
   is_memory_global_ = false;
-  old_pkg_map_.clear();
-  old_app_map_.clear();
+  pkg_map_.clear();
+  app_map_.clear();
+  CacheFlag::SetStatus(CacheFlag::Status::UNPREPARED);
   LOG(DEBUG) << "Set Memory mode : File";
 }
 
 bool DBHandleProvider::IsMemoryDBActive(pid_t pid, bool write) {
+  std::unique_lock<std::mutex> u(pid_list_lock_);
   return (is_memory_ && pid_list_.find(pid) == pid_list_.end() && !write);
 }
 
@@ -285,27 +285,25 @@ void DBHandleProvider::TrimCache() {
 }
 
 void DBHandleProvider::ReleaseCache() {
+  auto lock = CacheFlag::GetWriterLock();
+  CacheFlag::SetStatus(CacheFlag::Status::PREPARING);
+
   app_map_.clear();
   pkg_map_.clear();
+  CacheFlag::SetStatus(CacheFlag::Status::UNPREPARED);
+
   released_ = true;
 }
 
-bool DBHandleProvider::NeedUpdate(pid_t pid, bool write,
-    const std::string& locale) {
-  std::unique_lock<std::recursive_mutex> u(lock_);
-  return (!IsMemoryDBActive(pid, write) && (locale_ != locale || released_));
+bool DBHandleProvider::IsWriter(pid_t pid) {
+  std::unique_lock<std::mutex> u(pid_list_lock_);
+  return pid_list_.find(pid) != pid_list_.end();
 }
 
 int DBHandleProvider::UpdateCache(sqlite3* db, pid_t pid, uid_t uid, bool write,
     const std::string& locale) {
-  if (!NeedUpdate(pid, write, locale))
-    return PMINFO_R_OK;
-
-  std::unique_lock<std::recursive_mutex> u(lock_);
-  if (!released_)
-    ReleaseCache();
-
-  locale_ = locale;
+  pkg_map_.clear();
+  app_map_.clear();
 
   GHashTable* list = g_hash_table_new(g_str_hash, g_str_equal);
   if (list == nullptr) {
@@ -322,7 +320,7 @@ int DBHandleProvider::UpdateCache(sqlite3* db, pid_t pid, uid_t uid, bool write,
   }
 
   int ret = pkginfo_internal_filter_get_list(db, tmp_filter, uid_,
-                                             locale_.c_str(), list);
+                                             locale.c_str(), list);
   if (ret == PMINFO_R_OK) {
     GHashTableIter iter;
     gpointer value;
@@ -341,7 +339,7 @@ int DBHandleProvider::UpdateCache(sqlite3* db, pid_t pid, uid_t uid, bool write,
 
   list = g_hash_table_new(g_str_hash, g_str_equal);
   ret = appinfo_internal_filter_get_list(db, tmp_filter, uid_, uid,
-                                         locale_.c_str(), list);
+                                         locale.c_str(), list);
   free(tmp_filter);
   if (ret == PMINFO_R_OK) {
     GHashTableIter iter;
@@ -362,10 +360,8 @@ int DBHandleProvider::UpdateCache(sqlite3* db, pid_t pid, uid_t uid, bool write,
 std::vector<std::shared_ptr<package_x>> DBHandleProvider::GetPackages(
     pid_t pid, bool write, pkgmgrinfo_filter_x* filter,
     const std::string& package) {
-  auto& pkg_map = (IsMemoryDBActive(pid, write) ? old_pkg_map_ : pkg_map_);
-
   std::vector<std::shared_ptr<package_x>> ret;
-  for (auto& info : pkg_map[package]) {
+  for (auto& info : pkg_map_[package]) {
     bool pass = true;
     for (auto* it = filter->list; it != nullptr; it = g_slist_next(it)) {
       auto node = reinterpret_cast<pkgmgrinfo_node_x*>(it->data);
@@ -392,8 +388,6 @@ void DBHandleProvider::AddPackage(std::string package, package_x* info) {
 std::vector<std::shared_ptr<application_x>> DBHandleProvider::GetApplications(
     pid_t pid, bool write, pkgmgrinfo_filter_x* filter,
     const std::string& app) {
-  auto& app_map = (IsMemoryDBActive(pid, write) ? old_app_map_ : app_map_);
-
   /* make metadata filter map */
   std::unordered_map<std::string, std::string> metadata_map;
   for (auto* it = filter->list_metadata; it != nullptr; it = g_slist_next(it)) {
@@ -405,7 +399,7 @@ std::vector<std::shared_ptr<application_x>> DBHandleProvider::GetApplications(
   }
 
   std::vector<std::shared_ptr<application_x>> ret;
-  for (auto& info : app_map[app]) {
+  for (auto& info : app_map_[app]) {
     bool pass = true;
     for (auto* it = filter->list; it != nullptr; it = g_slist_next(it)) {
       auto node = reinterpret_cast<pkgmgrinfo_node_x*>(it->data);
@@ -448,5 +442,17 @@ void DBHandleProvider::AddApplication(std::string app, application_x* info) {
   app_map_[""].push_back(std::move(ptr));
 }
 
+void DBHandleProvider::InsertPID(pid_t pid) {
+  std::unique_lock<std::mutex> u(pid_list_lock_);
+
+  pid_list_.insert(pid);
+}
+
+bool DBHandleProvider::ErasePID(pid_t pid) {
+  std::unique_lock<std::mutex> u(pid_list_lock_);
+
+  return pid_list_.erase(pid) == 1;
+}
+
 }  // namespace database
 }  // namespace pkgmgr_server
index 0258a08d4d2775f77e01dec65add70cc100b45f9..1a7dff65015a27cfe9a92910b7ecf4b035cad367 100644 (file)
@@ -50,7 +50,6 @@ class EXPORT_API DBHandleProvider {
   std::string GetCertDBPath(int pid, bool write);
   void SetMemoryMode(pid_t pid);
   void UnsetMemoryMode(pid_t pid);
-  bool NeedUpdate(pid_t pid, bool write, const std::string& locale);
   int UpdateCache(sqlite3* db, pid_t pid, uid_t uid, bool write,
                   const std::string& locale);
   std::vector<std::shared_ptr<package_x>> GetPackages(
@@ -60,6 +59,7 @@ class EXPORT_API DBHandleProvider {
       pid_t pid, bool write, pkgmgrinfo_filter_x* filter,
       const std::string& app);
   void TrimCache();
+  bool IsWriter(pid_t pid);
 
  private:
   explicit DBHandleProvider(uid_t uid);
@@ -69,6 +69,8 @@ class EXPORT_API DBHandleProvider {
   void ReleaseCache();
   void AddPackage(std::string package, package_x* info);
   void AddApplication(std::string app, application_x* info);
+  void InsertPID(pid_t pid);
+  bool ErasePID(pid_t pid);
 
  private:
   static std::unordered_map<uid_t, std::unique_ptr<DBHandleProvider>> provider_;
@@ -81,6 +83,7 @@ class EXPORT_API DBHandleProvider {
   static std::string cert_file_db_path_;
   static std::unordered_set<pid_t> pid_list_;
   static std::recursive_mutex lock_;
+  static std::mutex pid_list_lock_;
 
   uid_t uid_;
   bool is_memory_;
@@ -88,16 +91,11 @@ class EXPORT_API DBHandleProvider {
       parser_memory_db_handle_;
   std::string parser_memory_db_path_;
   std::string parser_file_db_path_;
-  std::string locale_;
   bool released_ = true;
   std::unordered_map<std::string, std::vector<std::shared_ptr<package_x>>>
       pkg_map_;
-  std::unordered_map<std::string, std::vector<std::shared_ptr<package_x>>>
-      old_pkg_map_;
   std::unordered_map<std::string, std::vector<std::shared_ptr<application_x>>>
       app_map_;
-  std::unordered_map<std::string, std::vector<std::shared_ptr<application_x>>>
-      old_app_map_;
 };
 
 }  // namespace database
diff --git a/src/server/database/pkg_get_cache_db_handler.cc b/src/server/database/pkg_get_cache_db_handler.cc
deleted file mode 100644 (file)
index ef2bd6d..0000000
+++ /dev/null
@@ -1,81 +0,0 @@
-/*
- * 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 "pkg_get_cache_db_handler.hh"
-
-#include <shared_mutex>
-#include <vector>
-
-#include "utils/logging.hh"
-
-#include "db_handle_provider.hh"
-#include "pkgmgrinfo_debug.h"
-
-namespace pkgmgr_server {
-namespace database {
-
-PkgGetCacheDBHandler::PkgGetCacheDBHandler(uid_t uid, int pid)
-    : PkgGetDBHandler(uid, pid) {}
-
-int PkgGetCacheDBHandler::Execute() {
-  std::shared_lock<std::shared_timed_mutex> s(lock_);
-  SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_READ);
-  SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
-  bool write =
-      GetOpType() == pkgmgr_common::DBOperationType::OPERATION_TYPE_WRITE;
-
-  std::vector<std::pair<sqlite3*, uid_t>> conn_list;
-  if (!Connect())
-    return PMINFO_R_ERROR;
-
-  conn_list = GetConnection();
-
-  int ret = PMINFO_R_OK;
-  std::string package;
-
-  for (auto* it = filter_->list; it != nullptr; it = g_slist_next(it)) {
-    auto node = reinterpret_cast<pkgmgrinfo_node_x*>(it->data);
-    if (node->prop == E_PMINFO_PKGINFO_PROP_PACKAGE_ID) {
-      package = node->value;
-      break;
-    }
-  }
-
-  std::vector<std::shared_ptr<package_x>> pkg_list;
-  for (auto& conn : conn_list) {
-    ret = DBHandleProvider::GetInst(conn.second)
-              .UpdateCache(conn.first, GetPID(), uid_, write, GetLocale());
-    if (ret != PMINFO_R_OK) {
-      LOG(ERROR) << "Failed to update pkginfo cache : " << ret;
-      break;
-    }
-
-    pkg_list = DBHandleProvider::GetInst(conn.second)
-                        .GetPackages(GetPID(), write, filter_, package);
-
-    handle_list_.reserve(pkg_list.size() + handle_list_.size());
-    std::move(std::begin(pkg_list), std::end(pkg_list),
-              std::back_inserter(handle_list_));
-  }
-
-  if (handle_list_.empty())
-    return PMINFO_R_ENOENT;
-
-  return ret;
-}
-
-}  // namespace database
-}  // namespace pkgmgr_server
diff --git a/src/server/database/pkg_get_cache_db_handler.hh b/src/server/database/pkg_get_cache_db_handler.hh
deleted file mode 100644 (file)
index 56200fa..0000000
+++ /dev/null
@@ -1,44 +0,0 @@
-/*
- * 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 PKG_GET_CACHE_DB_HANDLER_HH_
-#define PKG_GET_CACHE_DB_HANDLER_HH_
-
-#include <sys/types.h>
-
-#include <vector>
-
-#include "pkg_get_db_handler.hh"
-#include "pkgmgrinfo_basic.h"
-#include "pkgmgrinfo_private.h"
-
-namespace pkgmgr_server {
-namespace database {
-
-#ifndef EXPORT_API
-#define EXPORT_API __attribute__((visibility("default")))
-#endif
-
-class EXPORT_API PkgGetCacheDBHandler : public PkgGetDBHandler {
- public:
-  PkgGetCacheDBHandler(uid_t uid, int pid);
-  int Execute() override;
-};
-
-}  // namespace database
-}  // namespace pkgmgr_server
-
-#endif  // PKG_GET_CACHE_DB_HANDLER_HH_
index 8a1043d0dc3d1f677315f2490f21513164e47195..0e67de7cecf3c047abac4d24a6fc645fccb502b6 100644 (file)
@@ -19,6 +19,9 @@
 #include <shared_mutex>
 #include <vector>
 
+#include "cache_flag.hh"
+#include "db_handle_provider.hh"
+
 #include "pkgmgrinfo_debug.h"
 #include "pkgmgrinfo_internal.h"
 #include "utils/logging.hh"
@@ -52,18 +55,10 @@ void PkgGetDBHandler::SetFilter(pkgmgrinfo_filter_x* filter) {
   filter_ = filter;
 }
 
-int PkgGetDBHandler::Execute() {
-  std::shared_lock<std::shared_timed_mutex> s(lock_);
-  SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_READ);
-  SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
-  if (!Connect()) {
-    LOG(ERROR) << "Failed to connect database";
-    return PMINFO_R_ERROR;
-  }
-
+int PkgGetDBHandler::GetHandleFromDB(
+    std::vector<std::pair<sqlite3*, uid_t>>& conn_list) {
   GHashTable* list =
       g_hash_table_new_full(g_str_hash, g_str_equal, nullptr, nullptr);
-  std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
   int ret = PMINFO_R_OK;
   for (auto& conn : conn_list) {
     ret = pkginfo_internal_filter_get_list(conn.first, filter_, conn.second,
@@ -85,5 +80,63 @@ int PkgGetDBHandler::Execute() {
   return ret;
 }
 
+int PkgGetDBHandler::GetHandleFromCache(
+    std::vector<std::pair<sqlite3*, uid_t>>& conn_list) {
+  int ret = PMINFO_R_OK;
+  std::string package;
+
+  for (auto* it = filter_->list; it != nullptr; it = g_slist_next(it)) {
+    auto node = reinterpret_cast<pkgmgrinfo_node_x*>(it->data);
+    if (node->prop == E_PMINFO_PKGINFO_PROP_PACKAGE_ID) {
+      package = node->value;
+      break;
+    }
+  }
+
+  std::vector<std::shared_ptr<package_x>> pkg_list;
+  for (auto& conn : conn_list) {
+    pkg_list = DBHandleProvider::GetInst(conn.second)
+                        .GetPackages(GetPID(), false, filter_, package);
+
+    handle_list_.reserve(pkg_list.size() + handle_list_.size());
+    std::move(std::begin(pkg_list), std::end(pkg_list),
+              std::back_inserter(handle_list_));
+  }
+
+  if (handle_list_.empty())
+    return PMINFO_R_ENOENT;
+
+  return ret;
+}
+
+int PkgGetDBHandler::Execute() {
+  std::shared_lock<std::shared_timed_mutex> s(lock_);
+  SetOpType(pkgmgr_common::DBOperationType::OPERATION_TYPE_READ);
+  SetDBType(pkgmgr_common::DBType::DB_TYPE_FILE_PKGDB);
+  if (!Connect()) {
+    LOG(ERROR) << "Failed to connect database";
+    return PMINFO_R_ERROR;
+  }
+
+  std::vector<std::pair<sqlite3*, uid_t>> conn_list = GetConnection();
+
+  bool is_writer = false;
+  for (auto& conn : conn_list) {
+    if (DBHandleProvider::GetInst(conn.second).IsWriter(GetPID()))
+      is_writer = true;
+  }
+  if (is_writer)
+    return GetHandleFromDB(conn_list);
+
+  if (CacheFlag::GetStatus() == CacheFlag::Status::PREPARED) {
+    auto cache_lock = CacheFlag::GetReaderLock();
+    if (cache_lock.try_lock() &&
+        CacheFlag::GetStatus() == CacheFlag::Status::PREPARED)
+      return GetHandleFromCache(conn_list);
+  }
+
+  return GetHandleFromDB(conn_list);
+}
+
 }  // namespace database
 }  // namespace pkgmgr_server
index bc04570e1414deb701d308185436f3ceb4d1c6fb..ba1618726260753e695e0461204f311b5441debc 100644 (file)
@@ -35,12 +35,14 @@ namespace database {
 class EXPORT_API PkgGetDBHandler : public AbstractDBHandler {
  public:
   PkgGetDBHandler(uid_t uid, int pid);
-  ~PkgGetDBHandler();
+  virtual ~PkgGetDBHandler();
   std::vector<std::shared_ptr<package_x>> GetPkgHandle();
   void SetFilter(pkgmgrinfo_filter_x* filter);
   virtual int Execute();
 
  protected:
+  int GetHandleFromDB(std::vector<std::pair<sqlite3*, uid_t>>& conn_list);
+  int GetHandleFromCache(std::vector<std::pair<sqlite3*, uid_t>>& conn_list);
   pkgmgrinfo_filter_x* filter_ = nullptr;
   std::vector<std::shared_ptr<package_x>> handle_list_;
   uid_t uid_;
index f22f6088c832b2205167a616507775e6b0e999d8..ed9ac008ab057e563e1d16702a92b35c9450594d 100644 (file)
@@ -32,16 +32,19 @@ namespace pkgmgr_server {
 
 class EXPORT_API PkgRequest {
  public:
+  PkgRequest() = default;
   PkgRequest(int fd);
   ~PkgRequest();
-  unsigned char* GetData();
-  int GetSize();
+
   int GetFd();
-  pid_t GetSenderPID();
-  uid_t GetSenderUID();
-  pkgmgr_common::ReqType GetRequestType();
   bool ReceiveData();
-  bool SendData(unsigned char* data, int size);
+
+  virtual unsigned char* GetData();
+  virtual int GetSize();
+  virtual pid_t GetSenderPID();
+  virtual uid_t GetSenderUID();
+  virtual pkgmgr_common::ReqType GetRequestType();
+  virtual bool SendData(unsigned char* data, int size);
 
  private:
   std::unique_ptr<pkgmgr_common::socket::DataSocket> socket_;
diff --git a/src/server/request_handler/create_cache_request_handler.cc b/src/server/request_handler/create_cache_request_handler.cc
new file mode 100644 (file)
index 0000000..4e60054
--- /dev/null
@@ -0,0 +1,32 @@
+// Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by an apache-2.0 license that can be
+// found in the LICENSE file.
+
+#include "create_cache_request_handler.hh"
+
+#include <string>
+
+#include "cache_db_handler.hh"
+
+namespace psd = pkgmgr_server::database;
+
+namespace pkgmgr_server {
+namespace request_handler {
+
+bool CreateCacheRequestHandler::HandleRequest(unsigned char* data, int size,
+                                            const std::string& locale) {
+  // TODO(ilho159.kim) need to get logined user id
+  psd::CacheDBHandler db(5001, GetPID());
+  db.SetLocale(locale);
+
+  int ret = db.Execute();
+
+  return ret == PMINFO_R_OK;
+}
+
+std::vector<uint8_t> CreateCacheRequestHandler::ExtractResult() {
+  return {};
+}
+
+}  // namespace request_handler
+}  // namespace pkgmgr_server
diff --git a/src/server/request_handler/create_cache_request_handler.hh b/src/server/request_handler/create_cache_request_handler.hh
new file mode 100644 (file)
index 0000000..2c7a548
--- /dev/null
@@ -0,0 +1,33 @@
+// Copyright (c) 2021 Samsung Electronics Co., Ltd All Rights Reserved
+// Use of this source code is governed by an apache-2.0 license that can be
+// found in the LICENSE file.
+
+#ifndef CREATE_CACHE_REQUEST_HANDLER_HH_
+#define CREATE_CACHE_REQUEST_HANDLER_HH_
+
+#include "abstract_request_handler.hh"
+#include "result_parcelable.hh"
+
+#include <string>
+
+namespace pkgmgr_server {
+namespace request_handler {
+
+#ifndef EXPORT_API
+#define EXPORT_API __attribute__((visibility("default")))
+#endif
+
+class EXPORT_API CreateCacheRequestHandler : public AbstractRequestHandler {
+ public:
+  bool HandleRequest(unsigned char* data, int size,
+      const std::string& locale) override;
+  std::vector<uint8_t> ExtractResult() override;
+
+ private:
+  std::shared_ptr<pkgmgr_common::parcel::ResultParcelable> result_;
+};
+
+}  // namespace request_handler
+}  // namespace pkgmgr_server
+
+#endif  // CREATE_CACHE_REQUEST_HANDLER_HH_
\ No newline at end of file
index 2f5cc1dec1c6624fbd3b6560095d27b59ebf9209..bc3842c66ad1d7ddef3d6a23ec6a2009d2451b9a 100644 (file)
@@ -6,7 +6,6 @@
 
 #include <string>
 
-#include "appinfo_cache_db_handler.hh"
 #include "appinfo_db_handler.hh"
 #include "filter_parcelable.hh"
 #include "parcelable_factory.hh"
@@ -44,19 +43,13 @@ bool GetAppinfoRequestHandler::HandleRequest(unsigned char* data, int size,
     return false;
   }
 
-  psd::AppInfoDBHandler* db;
-  if (cache_)
-    db = new psd::AppInfoCacheDBHandler(parcel->GetUid(), GetPID());
-  else
-    db = new psd::AppInfoDBHandler(parcel->GetUid(), GetPID());
+  psd::AppInfoDBHandler db(parcel->GetUid(), GetPID());
+  db.SetLocale(locale);
+  db.SetFilter(const_cast<pkgmgrinfo_filter_x*>(parcel->GetFilter()));
+  int ret = db.Execute();
 
-  db->SetLocale(locale);
-  db->SetFilter(const_cast<pkgmgrinfo_filter_x*>(parcel->GetFilter()));
-  int ret = db->Execute();
+  result_ = std::make_shared<pcp::AppInfoParcelable>(ret, db.GetAppHandle());
 
-  result_ = std::make_shared<pcp::AppInfoParcelable>(ret, db->GetAppHandle());
-
-  delete db;
   return true;
 }
 
index 1d545170771152dafc4bcd4b7a7b84117fc268ff..d99b6f89090e38de5b225a3deb90226c06871999 100644 (file)
@@ -6,9 +6,9 @@
 
 #include <string>
 
+#include "abstract_db_handler.hh"
 #include "filter_parcelable.hh"
 #include "parcelable_factory.hh"
-#include "pkg_get_cache_db_handler.hh"
 #include "pkg_get_db_handler.hh"
 #include "pkgmgrinfo_debug.h"
 #include "utils/logging.hh"
@@ -44,19 +44,13 @@ bool GetPkginfoRequestHandler::HandleRequest(unsigned char* data, int size,
     return false;
   }
 
-  psd::PkgGetDBHandler* db;
-  if (cache_)
-    db = new psd::PkgGetCacheDBHandler(parcel->GetUid(), GetPID());
-  else
-    db = new psd::PkgGetDBHandler(parcel->GetUid(), GetPID());
+  psd::PkgGetDBHandler db(parcel->GetUid(), GetPID());
+  db.SetLocale(locale);
+  db.SetFilter(const_cast<pkgmgrinfo_filter_x*>(parcel->GetFilter()));
+  int ret = db.Execute();
 
-  db->SetLocale(locale);
-  db->SetFilter(const_cast<pkgmgrinfo_filter_x*>(parcel->GetFilter()));
-  int ret = db->Execute();
+  result_ = std::make_shared<pcp::PkgInfoParcelable>(ret, db.GetPkgHandle());
 
-  result_ = std::make_shared<pcp::PkgInfoParcelable>(ret, db->GetPkgHandle());
-
-  delete db;
   return true;
 }
 
index 1c2b07704d2db878bba8740330b3b008809c3a15..52e8211aa39bd22068091c0f64610cc2628e9fa2 100644 (file)
@@ -24,6 +24,8 @@
 #include <unordered_map>
 #include <vector>
 
+#include "cache_flag.hh"
+#include "create_cache_request.hh"
 #include "cynara_checker.hh"
 #include "pkg_request.hh"
 #include "runner.hh"
@@ -94,6 +96,10 @@ int Runner::OnReceiveRequest(int fd, GIOCondition cond, void* user_data) {
   auto req = std::make_shared<PkgRequest>(client_fd);
   if (req->ReceiveData()) {
     pkgmgr_common::ReqType type = req->GetRequestType();
+    if (CacheFlag::SetPreparing()) {
+      runner->QueueRequest(
+          std::make_shared<CreateCacheRequest>(req->GetSenderUID()));
+    }
     std::vector<std::string>&& privileges = GetPrivileges(type);
     CynaraChecker::GetInst().CheckPrivilege(runner, req, privileges);
   }
index 28680c602bcf2da27f505b7b68cb782d9aaaa7aa..a1b038707f519042edebcad4ac640daba049c3b6 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "abstract_parcelable.hh"
 #include "command_request_handler.hh"
+#include "create_cache_request_handler.hh"
 #include "create_db_request_handler.hh"
 #include "db_handle_provider.hh"
 #include "get_appinfo_request_handler.hh"
@@ -91,6 +92,8 @@ void WorkerThread::Run() {
       new request_handler::CommandRequestHandler());
   handler[pkgmgr_common::ReqType::CREATE_DB].reset(
       new request_handler::CreateDBRequestHandler());
+  handler[pkgmgr_common::ReqType::CREATE_CACHE].reset(
+      new request_handler::CreateCacheRequestHandler());
 
   LOG(DEBUG) << "Initialize request handlers";
   while (true) {