Change Cached Data type from map to ResourceAttributes.
authorjyong2.kim <jyong2.kim@samsung.com>
Wed, 17 Jun 2015 11:05:37 +0000 (20:05 +0900)
committerUze Choi <uzchoi@samsung.com>
Fri, 19 Jun 2015 01:37:30 +0000 (01:37 +0000)
change cache callback function parameter.
change getCachedData return type.
update getter member method to const member method.
change constant value to defined value.

Change-Id: Iec5af33301dfedf625fd9fcb87a989a05246c687
Signed-off-by: jyong2.kim <jyong2.kim@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/1325
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Uze Choi <uzchoi@samsung.com>
Tested-by: Uze Choi <uzchoi@samsung.com>
service/basis/resourceCache/include/CacheTypes.h [changed mode: 0644->0755]
service/basis/resourceCache/include/DataCache.h [changed mode: 0644->0755]
service/basis/resourceCache/include/ResourceCacheManager.h [changed mode: 0644->0755]
service/basis/resourceCache/src/DataCache.cpp [changed mode: 0644->0755]
service/basis/resourceCache/src/ResourceCacheManager.cpp [changed mode: 0644->0755]

old mode 100644 (file)
new mode 100755 (executable)
index 8793cb8..d222891
 #include "OCResource.h"
 
 #include "PrimitiveResource.h"
+#include "ResourceAttributes.h"
 
 #define CACHE_TAG  PCF("CACHE")
+#define DEFAULT_REPORT_TIME 30
 
 using namespace OIC::Service;
 
@@ -64,8 +66,7 @@ enum class CACHE_STATE
 
 typedef int CacheID;
 
-typedef std::map<std::string, std::string> CachedData;
-typedef std::function<OCStackResult(std::shared_ptr<PrimitiveResource>, std::shared_ptr<CachedData>)> CacheCB;
+typedef std::function<OCStackResult(std::shared_ptr<PrimitiveResource>, const ResourceAttributes &)> CacheCB;
 typedef std::map<int, std::pair<Report_Info, CacheCB>> SubscriberInfo;
 typedef std::pair<int, std::pair<Report_Info, CacheCB>> SubscriberInfoPair;
 
@@ -74,7 +75,6 @@ typedef PrimitiveResource::GetCallback GetCB;
 typedef PrimitiveResource::ObserveCallback ObserveCB;
 
 typedef std::shared_ptr<DataCache> DataCachePtr;
-typedef std::shared_ptr<CachedData> CachedDataPtr;
 typedef std::shared_ptr<PrimitiveResource> PrimitiveResourcePtr;
 
 #endif
old mode 100644 (file)
new mode 100755 (executable)
index 4d0ffb1..45f4898
@@ -23,7 +23,7 @@
 
 #include <list>
 #include <string>
-#include <boost/progress.hpp>
+#include <memory>
 
 #include "OCResource.h"
 #include "logger.h"
@@ -43,9 +43,9 @@ public:
     CacheID addSubscriber(CacheCB func, REPORT_FREQUENCY rf, long repeatTime);
     CacheID deleteSubscriber(CacheID id);
 
-    CachedDataPtr getCachedData();
-    CACHE_STATE getCacheState();
-    PrimitiveResourcePtr getPrimitiveResource();
+    CACHE_STATE getCacheState() const;
+    const ResourceAttributes getCachedData() const;
+    const PrimitiveResourcePtr getPrimitiveResource() const;
 
     SubscriberInfoPair findSubscriber(CacheID id);
 
@@ -59,8 +59,7 @@ private:
     std::shared_ptr<BaseResource> baseHandler;
 
     // cached data info
-    CachedDataPtr data;
-    std::shared_ptr<ResourceAttributes> attributes;
+    ResourceAttributes attributes;
     long updateTime;
     CACHE_STATE state;
 
old mode 100644 (file)
new mode 100755 (executable)
index 5c5bab0..fa54b1f
@@ -23,6 +23,7 @@
 
 #include <list>
 #include <string>
+#include <mutex>
 
 #include "OCPlatform.h"
 #include "CacheTypes.h"
@@ -38,16 +39,14 @@ public:
     CacheID requestResourceCache(
             PrimitiveResourcePtr pResource,
                 CacheCB func = NULL, REPORT_FREQUENCY rf = REPORT_FREQUENCY::NONE, long time = 0l);
-
     OCStackResult cancelResourceCache(PrimitiveResourcePtr pResource, CacheID id);
-
     OCStackResult updateResourceCache(PrimitiveResourcePtr pResource);
 
-    CachedDataPtr getCachedData(PrimitiveResourcePtr pResource);
-    CachedDataPtr getCachedData(CacheID id);
+    const ResourceAttributes getCachedData(PrimitiveResourcePtr pResource) const;
+    const ResourceAttributes getCachedData(CacheID id) const;
 
-    CACHE_STATE getResourceCacheState(PrimitiveResourcePtr pResource);
-    CACHE_STATE getResourceCacheState(CacheID id);
+    CACHE_STATE getResourceCacheState(PrimitiveResourcePtr pResource) const;
+    CACHE_STATE getResourceCacheState(CacheID id) const;
 
     ~ResourceCacheManager();
 private:
@@ -57,8 +56,8 @@ private:
     static std::mutex s_mutexForCreation;
     static std::unique_ptr<std::list<DataCachePtr>> s_cacheDataList;
 
-    DataCachePtr findDataCache(PrimitiveResourcePtr pResource);
-    DataCachePtr findDataCache(CacheID id);
+    DataCachePtr findDataCache(PrimitiveResourcePtr pResource) const;
+    DataCachePtr findDataCache(CacheID id) const;
 };
 
 #endif /* RESOURCECACHEMANAGER_H_ */
old mode 100644 (file)
new mode 100755 (executable)
index 38ca2ea..531b5d3
@@ -40,7 +40,6 @@ DataCache::DataCache(
             ):sResource(pResource)
 {
     subscriberList = std::unique_ptr<SubscriberInfo>(new SubscriberInfo());
-    data = std::make_shared<CachedData>();
 
     state = CACHE_STATE::READY_YET;
     updateTime = 0l;
@@ -65,7 +64,6 @@ DataCache::DataCache(
 DataCache::~DataCache()
 {
     // TODO Auto-generated destructor stub
-    data.reset();
 
     // TODO delete all node!!
     subscriberList->clear();
@@ -128,18 +126,19 @@ SubscriberInfoPair DataCache::findSubscriber(CacheID id)
     return ret;
 }
 
-CachedDataPtr DataCache::getCachedData()
+const PrimitiveResourcePtr DataCache::getPrimitiveResource() const
 {
-    if(state != CACHE_STATE::READY)
-    {
-        return nullptr;
-    }
-    return data;
+    return sResource;
 }
 
-std::shared_ptr<PrimitiveResource> DataCache::getPrimitiveResource()
+const ResourceAttributes DataCache::getCachedData() const
 {
-    return sResource;
+    if(state != CACHE_STATE::READY)
+    {
+        return ResourceAttributes();
+    }
+    const ResourceAttributes retAtt = attributes;
+    return retAtt;
 }
 
 void DataCache::onObserve(
@@ -159,26 +158,15 @@ void DataCache::onObserve(
 
     state = CACHE_STATE::READY;
 
-    ResourceAttributes att = _rep.getAttributes();
-
-    // set data
-    data->clear();
-
-
-    for(auto & i : att)
-    {
-        const std::string &key = i.key();
-        std::string value = i.value().toString();
-        data->insert(CachedData::value_type(key, value));
-    }
+    attributes = _rep.getAttributes();
 
     // notify!!
-
+    ResourceAttributes retAtt = attributes;
     for(auto & i : * subscriberList)
     {
         if(i.second.first.rf == REPORT_FREQUENCY::UPTODATE)
         {
-            i.second.second(this->sResource, data);
+            i.second.second(this->sResource, retAtt);
         }
     }
 }
@@ -189,18 +177,7 @@ void DataCache::onGet(const HeaderOptions& _hos,
     if(state == CACHE_STATE::READY_YET)
     {
         state = CACHE_STATE::READY;
-        ResourceAttributes att = _rep.getAttributes();
-
-        // set data
-        data->clear();
-
-
-        for(auto & i : att)
-        {
-            const std::string &key = i.key();
-            std::string value = i.value().toString();
-            data->insert(CachedData::value_type(key, value));
-        }
+        attributes = _rep.getAttributes();
     }
     else
     {
@@ -208,7 +185,7 @@ void DataCache::onGet(const HeaderOptions& _hos,
     }
 }
 
-CACHE_STATE DataCache::getCacheState()
+CACHE_STATE DataCache::getCacheState() const
 {
     return state;
 }
old mode 100644 (file)
new mode 100755 (executable)
index 8e178ca..c174919
@@ -50,7 +50,7 @@ ResourceCacheManager * ResourceCacheManager::getInstance()
         s_mutexForCreation.lock();
         if(s_instance == nullptr)
         {
-            s_instance = new ResourceCacheManager();;
+            s_instance = new ResourceCacheManager();
         }
         s_mutexForCreation.unlock();
     }
@@ -72,7 +72,7 @@ CacheID ResourceCacheManager::requestResourceCache(
         if(!reportTime)
         {
             // default setting
-            reportTime = 30;
+            reportTime = DEFAULT_REPORT_TIME;
         }
     }
 
@@ -116,7 +116,7 @@ OCStackResult ResourceCacheManager::cancelResourceCache(PrimitiveResourcePtr pRe
     return ret;
 }
 
-DataCachePtr ResourceCacheManager::findDataCache(PrimitiveResourcePtr pResource)
+DataCachePtr ResourceCacheManager::findDataCache(PrimitiveResourcePtr pResource) const
 {
     DataCachePtr retHandler = nullptr;
     for (auto & i : * s_cacheDataList)
@@ -131,7 +131,7 @@ DataCachePtr ResourceCacheManager::findDataCache(PrimitiveResourcePtr pResource)
     return retHandler;
 }
 
-DataCachePtr ResourceCacheManager::findDataCache(CacheID id)
+DataCachePtr ResourceCacheManager::findDataCache(CacheID id) const
 {
     DataCachePtr retHandler = nullptr;
     for (auto & i : * s_cacheDataList)
@@ -155,27 +155,27 @@ OCStackResult ResourceCacheManager::updateResourceCache(PrimitiveResourcePtr pRe
     return ret;
 }
 
-CachedDataPtr ResourceCacheManager::getCachedData(PrimitiveResourcePtr pResource)
+const ResourceAttributes ResourceCacheManager::getCachedData(PrimitiveResourcePtr pResource) const
 {
     DataCachePtr handler = findDataCache(pResource);
     if(handler == nullptr)
     {
-        return nullptr;
+        return ResourceAttributes();
     }
     return handler->getCachedData();
 }
 
-CachedDataPtr ResourceCacheManager::getCachedData(CacheID id)
+const ResourceAttributes ResourceCacheManager::getCachedData(CacheID id) const
 {
     DataCachePtr handler = findDataCache(id);
     if(handler == nullptr)
     {
-        return nullptr;
+        return ResourceAttributes();
     }
     return handler->getCachedData();
 }
 
-CACHE_STATE ResourceCacheManager::getResourceCacheState(PrimitiveResourcePtr pResource)
+CACHE_STATE ResourceCacheManager::getResourceCacheState(PrimitiveResourcePtr pResource) const
 {
     DataCachePtr handler = findDataCache(pResource);
     if(handler == nullptr)
@@ -184,7 +184,7 @@ CACHE_STATE ResourceCacheManager::getResourceCacheState(PrimitiveResourcePtr pRe
     }
     return handler->getCacheState();
 }
-CACHE_STATE ResourceCacheManager::getResourceCacheState(CacheID id)
+CACHE_STATE ResourceCacheManager::getResourceCacheState(CacheID id) const
 {
     DataCachePtr handler = findDataCache(id);
     if(handler == nullptr)