Change collection types passed to Storage for changing policies 56/24656/2
authorLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Thu, 17 Jul 2014 15:52:13 +0000 (17:52 +0200)
committerLukasz Wojciechowski <l.wojciechow@partner.samsung.com>
Fri, 18 Jul 2014 06:49:07 +0000 (08:49 +0200)
Old methods shall be removed in another commit, after unit test
are updated.

Change-Id: Id8a7a48859b743f897a651ebff2156846d8f0e58

src/service/storage/Storage.cpp
src/service/storage/Storage.h

index 9e9f00d..3792721 100644 (file)
@@ -47,7 +47,7 @@ PolicyResult Storage::minimalPolicy(const PolicyBucket &bucket, const PolicyKey
     const auto &policies = bucket.policyCollection();
 
     auto proposeMinimal = [&minimal, &hasMinimal](const PolicyResult &candidate) {
-        if(hasMinimal == false) {
+        if (hasMinimal == false) {
             minimal = candidate;
         } else if (candidate < minimal) {
             minimal = candidate;
@@ -80,19 +80,34 @@ PolicyResult Storage::minimalPolicy(const PolicyBucket &bucket, const PolicyKey
     return minimal;
 }
 
+//todo to be removed, after tests get updated
 void Storage::insertPolicies(const std::vector<PolicyPolicyBucket> &policies) {
-    for(const auto &policyTuple : policies) {
+    for (const auto &policyTuple : policies) {
         PolicyBucketId bucketId;
         PolicyPtr policyPtr;
         std::tie(policyPtr, bucketId) = policyTuple;
         auto existingPolicies = m_backend.searchBucket(bucketId, policyPtr->key());
-        for(auto existingPolicy : existingPolicies.policyCollection()) {
+        for (auto existingPolicy : existingPolicies.policyCollection()) {
             m_backend.deletePolicy(bucketId, existingPolicy->key());
         }
         m_backend.insertPolicy(bucketId, policyPtr);
     }
 }
 
+void Storage::insertPolicies(const std::map<PolicyBucketId, std::vector<Policy>> &policies) {
+    for (const auto &bucket : policies) {
+        const PolicyBucketId &bucketId = bucket.first;
+        for (const auto &policy : bucket.second) {
+                PolicyPtr policyPtr = std::make_shared<Policy>(policy);
+                auto existingPolicies = m_backend.searchBucket(bucketId, policyPtr->key());
+                for (auto existingPolicy : existingPolicies.policyCollection()) {
+                        m_backend.deletePolicy(bucketId, existingPolicy->key());
+                }
+                m_backend.insertPolicy(bucketId, policyPtr);
+        }
+    }
+}
+
 void Storage::addOrUpdateBucket(const PolicyBucketId &bucketId, const PolicyResult &defaultBucketPolicy) {
     if (m_backend.hasBucket(bucketId)) {
         m_backend.updateBucket(bucketId, defaultBucketPolicy);
@@ -112,10 +127,20 @@ void Storage::deleteBucket(const PolicyBucketId &bucketId) {
     m_backend.deleteBucket(bucketId);
 }
 
+//todo to be removed, after tests get updated
 void Storage::deletePolicies(const std::vector<PolicyKeyBucket> &policies) {
-    for(const auto &policy : policies) {
+    for (const auto &policy : policies) {
         m_backend.deletePolicy(std::get<1>(policy), std::get<0>(policy));
     }
 }
 
+void Storage::deletePolicies(const std::map<PolicyBucketId, std::vector<PolicyKey>> &policies) {
+    for (const auto &bucket : policies) {
+        const PolicyBucketId &bucketId = bucket.first;
+        for (const auto &policyKey : bucket.second) {
+            m_backend.deletePolicy(bucketId, policyKey);
+        }
+    }
+}
+
 } // namespace Cynara
index 177765c..f7e9c78 100644 (file)
 #ifndef CYNARA_SERVICE_STORAGE_STORAGE_H
 #define CYNARA_SERVICE_STORAGE_STORAGE_H
 
+#include <map>
+#include <memory>
+#include <tuple>
+#include <vector>
+
 #include "types/pointers.h"
 #include "types/PolicyBucketId.h"
 #include "types/PolicyResult.h"
 #include "types/PolicyKey.h"
 
-#include <memory>
-#include <vector>
-#include <tuple>
-
 namespace Cynara {
 
 class StorageBackend;
@@ -42,6 +43,7 @@ class Storage
 {
 public:
     // TODO: These tuples are ugly -- refactorize
+//todo to be removed, after tests get updated
     typedef std::tuple<PolicyPtr, PolicyBucketId> PolicyPolicyBucket;
     typedef std::tuple<PolicyKey, PolicyBucketId> PolicyKeyBucket;
 
@@ -49,9 +51,14 @@ public:
 
     PolicyResult checkPolicy(const PolicyKey &key);
 
+//todo below to functions should be removed, after tests get updated
     void insertPolicies(const std::vector<PolicyPolicyBucket> &policies);
-    void addOrUpdateBucket(const PolicyBucketId &bucketId, const PolicyResult &defaultBucketPolicy);
     void deletePolicies(const std::vector<PolicyKeyBucket> &policies);
+
+    void insertPolicies(const std::map<PolicyBucketId, std::vector<Policy>> &policies);
+    void deletePolicies(const std::map<PolicyBucketId, std::vector<PolicyKey>> &policies);
+
+    void addOrUpdateBucket(const PolicyBucketId &bucketId, const PolicyResult &defaultBucketPolicy);
     void deleteBucket(const PolicyBucketId &bucketId);
 
 protected: