From b0db3713e9ea46dda3de86ad4dc693a60bcc3cc7 Mon Sep 17 00:00:00 2001 From: Taejeong Lee Date: Wed, 15 May 2013 12:01:33 +0900 Subject: [PATCH] Add new type of getFilePathInWidgetPackageFromUrl() api for performance. - std::string getFilePathInWidgetPackageFromUrl(const std::string &tzAppId, const std::string &url) [Issue#] N/A [Problem] Performance [Cause] URI localization consumed too many CPU resource. [Solution] Optimization to getFilePathInWidgetPackageFromUrl() Change-Id: I6f0253067b082ab27b645682a789432d7773df1a --- .../dpl/localization/w3c_file_localization.h | 1 + modules/localization/src/w3c_file_localization.cpp | 171 +++++++++++++++++++++ 2 files changed, 172 insertions(+) diff --git a/modules/localization/include/dpl/localization/w3c_file_localization.h b/modules/localization/include/dpl/localization/w3c_file_localization.h index 52c3af9..f0e1997 100755 --- a/modules/localization/include/dpl/localization/w3c_file_localization.h +++ b/modules/localization/include/dpl/localization/w3c_file_localization.h @@ -44,6 +44,7 @@ DPL::Optional getFilePathInWidgetPackageFromUrl( DPL::Optional getFilePathInWidgetPackageFromUrl( WrtDB::WidgetDAOReadOnlyPtr dao, const DPL::String &url); +std::string getFilePathInWidgetPackageFromUrl(const std::string &tzAppId, const std::string &url); DPL::Optional getFilePathInWidgetPackage( WrtDB::DbWidgetHandle widgetHandle, diff --git a/modules/localization/src/w3c_file_localization.cpp b/modules/localization/src/w3c_file_localization.cpp index 152440e..66865ba 100755 --- a/modules/localization/src/w3c_file_localization.cpp +++ b/modules/localization/src/w3c_file_localization.cpp @@ -117,6 +117,177 @@ DPL::Optional GetFilePathInWidgetPackageInternal( } namespace W3CFileLocalization { +static bool isExistFileCached(const std::string& path) +{ + static std::map pathCache; + + // is cached? + if (pathCache.find(path) == pathCache.end()) + { + struct stat buf; + + if (0 == stat(path.c_str(), &buf)) + { + pathCache[path] = true; + + return true; + } + else + { + pathCache[path] = false; + + return false; + } + } + + return pathCache[path]; +} + +std::string getFilePathInWidgetPackageFromUrl(const std::string &tzAppId, const std::string &url) +{ + const std::string SCHEME_FILE = "file://"; + const std::string SCHEME_WIDGET = "widget://"; + const std::string SCHEM_APP = "app://"; + const std::string LOCALE_PATH = "locales/"; + + static std::string lastTzAppId; + static WidgetDAOReadOnlyPtr dao; + static std::string srcPath; + + std::string workingUrl = url; + bool found = false; + + // Dao caching + if (lastTzAppId != tzAppId) + { + lastTzAppId = tzAppId; + dao.reset(new WidgetDAOReadOnly(DPL::FromUTF8String(tzAppId))); + srcPath = DPL::ToUTF8String(dao->getPath()); + } + + // backup suffix string + std::string backupSuffix; + size_t pos = workingUrl.find_first_of("#?"); + + if (pos != std::string::npos) + { + backupSuffix = workingUrl.substr(pos); + workingUrl.resize(pos); + } + + // make basis path + if (workingUrl.compare(0, SCHEME_WIDGET.length(), SCHEME_WIDGET) == 0) + { + // remove "widget://" + workingUrl.erase(0, SCHEME_WIDGET.length()); + } + else if (workingUrl.compare(0, SCHEME_FILE.length(), SCHEME_FILE) == 0) + { + // remove "file://" + workingUrl.erase(0, SCHEME_FILE.length()); + + // remove src path + if (workingUrl.compare(0, srcPath.length(), srcPath) == 0) + { + workingUrl.erase(0, srcPath.length()); + } + + // remove locale path + if (workingUrl.compare(0, LOCALE_PATH.length(), LOCALE_PATH) == 0) + { + workingUrl.erase(0, LOCALE_PATH.length()); + + pos = workingUrl.find_first_of('/'); + + if (pos != std::string::npos && pos > 0) + { + workingUrl.erase(0, pos+1); + } + } + } + else if (workingUrl.compare(0, SCHEM_APP.length(), SCHEM_APP) == 0) + { + // remove "app://" + workingUrl.erase(0, SCHEM_APP.length()); + + // remove tizen app id + if (workingUrl.compare(0, tzAppId.length(), tzAppId) == 0) + { + workingUrl.erase(0, tzAppId.length()); + } + else + { + LogError("Tizen id does not match, ignoring"); + return ""; + } + } + + // remove '/' token + if (!workingUrl.empty() && workingUrl[0] == '/') + { + workingUrl.erase(0, 1); + } + + if (!workingUrl.empty() && workingUrl[workingUrl.length()-1] == '/') { + workingUrl.erase(workingUrl.length()-1, 1); + } + + if (workingUrl.empty()) + { + LogError("URL Localization Error!"); + return ""; + } + + const LanguageTags& ltags = LanguageTagsProviderSingleton::Instance().getLanguageTags(); + + FOREACH(it, ltags) + { + std::string path = srcPath; + + if (!it->empty()) + { + path += LOCALE_PATH; + + if (isExistFileCached(path) == false) + { + continue; + } + + path += DPL::ToUTF8String(*it) + "/"; + + if (isExistFileCached(path) == false) + { + continue; + } + } + + path += workingUrl; + + if (isExistFileCached(path) == true) + { + found = true; + workingUrl = path; + break; + } + } + + // restore suffix string + if (!found) + { + // return empty string + workingUrl = ""; + } + else + { + if (!backupSuffix.empty()) + { + workingUrl += backupSuffix; + } + } + + return workingUrl; +} + DPL::Optional getFilePathInWidgetPackageFromUrl( DbWidgetHandle widgetHandle, const DPL::String &url) -- 2.7.4