From 346b7de870bbba9a244ade9304d519580ba1fbb0 Mon Sep 17 00:00:00 2001 From: Lukasz Wojciechowski Date: Thu, 17 Jul 2014 17:52:13 +0200 Subject: [PATCH] Change collection types passed to Storage for changing policies Old methods shall be removed in another commit, after unit test are updated. Change-Id: Id8a7a48859b743f897a651ebff2156846d8f0e58 --- src/service/storage/Storage.cpp | 33 +++++++++++++++++++++++++++++---- src/service/storage/Storage.h | 17 ++++++++++++----- 2 files changed, 41 insertions(+), 9 deletions(-) diff --git a/src/service/storage/Storage.cpp b/src/service/storage/Storage.cpp index 9e9f00d..3792721 100644 --- a/src/service/storage/Storage.cpp +++ b/src/service/storage/Storage.cpp @@ -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 &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> &policies) { + for (const auto &bucket : policies) { + const PolicyBucketId &bucketId = bucket.first; + for (const auto &policy : bucket.second) { + PolicyPtr policyPtr = std::make_shared(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 &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> &policies) { + for (const auto &bucket : policies) { + const PolicyBucketId &bucketId = bucket.first; + for (const auto &policyKey : bucket.second) { + m_backend.deletePolicy(bucketId, policyKey); + } + } +} + } // namespace Cynara diff --git a/src/service/storage/Storage.h b/src/service/storage/Storage.h index 177765c..f7e9c78 100644 --- a/src/service/storage/Storage.h +++ b/src/service/storage/Storage.h @@ -24,15 +24,16 @@ #ifndef CYNARA_SERVICE_STORAGE_STORAGE_H #define CYNARA_SERVICE_STORAGE_STORAGE_H +#include +#include +#include +#include + #include "types/pointers.h" #include "types/PolicyBucketId.h" #include "types/PolicyResult.h" #include "types/PolicyKey.h" -#include -#include -#include - 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 PolicyPolicyBucket; typedef std::tuple 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 &policies); - void addOrUpdateBucket(const PolicyBucketId &bucketId, const PolicyResult &defaultBucketPolicy); void deletePolicies(const std::vector &policies); + + void insertPolicies(const std::map> &policies); + void deletePolicies(const std::map> &policies); + + void addOrUpdateBucket(const PolicyBucketId &bucketId, const PolicyResult &defaultBucketPolicy); void deleteBucket(const PolicyBucketId &bucketId); protected: -- 2.7.4