Make a cache for getting appsvc alias appid 59/286659/4
authorHwankyu Jhun <h.jhun@samsung.com>
Wed, 11 Jan 2023 09:38:04 +0000 (09:38 +0000)
committerHwanKyu Jhun <h.jhun@samsung.com>
Wed, 11 Jan 2023 10:30:04 +0000 (10:30 +0000)
To reduce the file access, the aul library stores the alias info to the memory.

Change-Id: I34d2b66aafe84723ebb7e63d477127a30ba8b60c
Signed-off-by: Hwankyu Jhun <h.jhun@samsung.com>
src/aul_svc.cc

index 325423f..028a5a7 100644 (file)
@@ -28,7 +28,9 @@
 
 #include <atomic>
 #include <memory>
+#include <mutex>
 #include <string>
+#include <unordered_map>
 #include <vector>
 
 #include "aul/app_control/resolve_info.hh"
@@ -92,6 +94,54 @@ class AliasInfo {
   std::string appid_;
 };
 
+class AppSvcAlias {
+ public:
+  AppSvcAlias() = default;
+
+  void Load() {
+    std::lock_guard<std::mutex> lock(mutex_);
+    if (loaded_)
+      return;
+
+    dictionary* dict = iniparser_load("/usr/share/appsvc/alias.ini");
+    if (dict == nullptr) {
+      loaded_ = true;
+      return;
+    }
+
+    std::string delimiter = "Alias:";
+    for (int i = 0; i < dict->n; ++i) {
+      if (dict->key[i] == nullptr || dict->val[i] == nullptr)
+        continue;
+
+      std::string key = dict->key[i];
+      key.erase(0, key.find(delimiter) + delimiter.length() + 1);
+      std::string value = dict->val[i];
+      map_[key] = value;
+    }
+
+    iniparser_freedict(dict);
+    loaded_ = true;
+  }
+
+  std::string GetAliasAppId(const std::string& alias_appid) {
+    auto found = map_.find(alias_appid);
+    if (found == map_.end())
+      return {};
+
+    SECURE_LOGD("alias_appid: %s, appid: %s",
+        alias_appid.c_str(), found->second.c_str());
+    return found->second;
+  }
+
+ private:
+  bool loaded_ = false;
+  std::unordered_map<std::string, std::string> map_;
+  std::mutex mutex_;
+};
+
+AppSvcAlias appsvc_alias;
+
 int SetBundle(bundle* b, const char* key, const char* value) {
   if (bundle_get_type(b, key) != BUNDLE_TYPE_NONE) {
     if (bundle_del(b, key) != BUNDLE_ERROR_NONE)
@@ -127,20 +177,8 @@ int SetBundleArray(bundle* b, const char* key, const char** value,
 }
 
 std::string GetAliasAppId(const char* appid) {
-  dictionary* dic = iniparser_load("/usr/share/appsvc/alias.ini");
-  if (dic == nullptr)
-    return {};
-
-  auto dic_ptr = std::unique_ptr<dictionary, decltype(iniparser_freedict)*>(
-      dic, iniparser_freedict);
-
-  std::string key = std::string("Alias:") + appid;
-  const char* value = iniparser_getstring(dic, key.c_str(), nullptr);
-  SECURE_LOGD("appid(%s), alias_id(%s)", appid, value);
-  if (value == nullptr)
-    return {};
-
-  return std::string(value);
+  appsvc_alias.Load();
+  return appsvc_alias.GetAliasAppId(appid);
 }
 
 bool IsSpecialApp(const char* appid) {