Release version 0.22.40
[platform/core/appfw/pkgmgr-info.git] / src / server / database / db_handle_provider.hh
index 0258a08..e165e05 100644 (file)
@@ -22,7 +22,7 @@
 #include <sys/types.h>
 
 #include <memory>
-#include <mutex>
+#include <shared_mutex>
 #include <string>
 #include <unordered_map>
 #include <unordered_set>
@@ -45,59 +45,67 @@ class EXPORT_API DBHandleProvider {
   ~DBHandleProvider() = default;
   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);
-  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(
-      pid_t pid, bool write, pkgmgrinfo_filter_x* filter,
+      pid_t pid, pkgmgrinfo_filter_x* filter,
       const std::string& package);
   std::vector<std::shared_ptr<application_x>> GetApplications(
-      pid_t pid, bool write, pkgmgrinfo_filter_x* filter,
+      pid_t pid, pkgmgrinfo_filter_x* filter,
       const std::string& app);
   void TrimCache();
+  void RegisterPendingPackageInfo(package_x* info);
+  bool UpdatePendingPackageInfo(sqlite3* db,
+      pid_t pid, uid_t uid, const std::string& locale);
+  bool UpdateCachePkg(sqlite3* db, uid_t uid, const std::string& pkgid,
+      const std::string& locale);
+  bool UpdateCacheApp(sqlite3* db, uid_t uid, const std::string& appid,
+      const std::string& locale);
+  bool UpdateCacheAppByPkgid(sqlite3* db, uid_t uid, const std::string& pkgid,
+      const std::string& locale);
 
  private:
   explicit DBHandleProvider(uid_t uid);
-  sqlite3* GetMemoryDBHandle(const std::string& filedb_path,
+  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, package_x* info);
-  void AddApplication(std::string app, application_x* info);
+  void AddApplication(std::string app, std::shared_ptr<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_;
-  static bool is_memory_global_;
+  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_memory_db_handle_;
+      global_parser_memdb_handle_;
   static std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
-      cert_memory_db_handle_;
-  static std::string global_parser_file_db_path_;
-  static std::string cert_file_db_path_;
-  static std::unordered_set<pid_t> pid_list_;
+      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_;
   static std::recursive_mutex lock_;
+  static std::shared_mutex pid_list_lock_;
 
   uid_t uid_;
-  bool is_memory_;
+  bool is_user_memdb_set_;
   std::unique_ptr<sqlite3, decltype(sqlite3_close_v2)*>
-      parser_memory_db_handle_;
-  std::string parser_memory_db_path_;
-  std::string parser_file_db_path_;
-  std::string locale_;
+      parser_memdb_handle_;
+  std::string user_parser_memdb_path_;
+  std::string user_parser_filedb_path_;
   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_;
+  std::unordered_map<std::string, std::shared_ptr<package_x>> pkg_map_;
+  std::unordered_map<std::string, std::shared_ptr<application_x>> app_map_;
+  std::unordered_map<std::string, std::unordered_set<std::string>> pkg_app_map_;
+  std::unordered_set<std::string> pending_pkg_;
 };
 
 }  // namespace database