[asterisk-scf-commits] asterisk-scf/release/ice-util-cpp.git branch "storage" created.

Commits to the Asterisk SCF project code repositories asterisk-scf-commits at lists.digium.com
Thu Nov 18 19:07:42 CST 2010


branch "storage" has been created
        at  87b29cd962f3d264943c9f5fa234162ff22b057f (commit)

- Log -----------------------------------------------------------------
commit 87b29cd962f3d264943c9f5fa234162ff22b057f
Author: Mark Michelson <mmichelson at digium.com>
Date:   Thu Nov 18 19:06:00 2010 -0600

    Initial change to allow for different storage methods in State Replicators.
    
    This change creates a new abstract class called StateReplicatorStorage
    with basic get, set, and remove methods. A derived type,
    VectorStateReplicatorStorage, has been created with the same functionality
    as the original state replicator.
    
    The next step in the process will be to determine the proper method for
    determining the type of storage class to be used by a state replicator.
    Once that is done, I can modify the state replicator tests to be able
    to iterate through known storage backends so that all may be tested.

diff --git a/StateReplicator/src/CMakeLists.txt b/StateReplicator/src/CMakeLists.txt
index 1dc097d..0c4b5a9 100644
--- a/StateReplicator/src/CMakeLists.txt
+++ b/StateReplicator/src/CMakeLists.txt
@@ -4,6 +4,9 @@ asterisk_scf_component_init(StateReplicator CXX)
 
 asterisk_scf_component_add_file(StateReplicator StateReplicator.h)
 asterisk_scf_component_add_file(StateReplicator StateReplicator.cpp)
+asterisk_scf_component_add_file(StateReplicator StateReplicatorStorage.h)
+asterisk_scf_component_add_file(StateReplicator VectorStateReplicatorStorage.h)
+asterisk_scf_component_add_file(StateReplicator VectorStateReplicatorStorage.cpp)
 asterisk_scf_component_add_boost_libraries(StateReplicator thread)
 
 asterisk_scf_component_build_library(StateReplicator)
diff --git a/StateReplicator/src/StateReplicator.h b/StateReplicator/src/StateReplicator.h
index b778aeb..7f70ec4 100644
--- a/StateReplicator/src/StateReplicator.h
+++ b/StateReplicator/src/StateReplicator.h
@@ -19,6 +19,8 @@
 #include <boost/thread/thread.hpp>
 #include <boost/thread/shared_mutex.hpp>
 
+#include "StateReplicatorStorage.h"
+
 namespace AsteriskSCF
 {
 namespace StateReplication
@@ -118,15 +120,6 @@ public:
         T mListener;
     };
 
-    template<typename T, typename D> class MatchItem
-    {
-    public:
-        MatchItem(const D& key) : mKey(key) {}
-        ~MatchItem() {}
-        bool operator() (T x) {return (x->key == mKey); }
-        D mKey;
-    };
-
 public:
     StateReplicator() {};
     virtual ~StateReplicator() {};
@@ -146,7 +139,7 @@ public:
         boost::shared_lock<boost::shared_mutex> lock(mStateLock);
         try
         {
-            listener->stateSet(mStateItems);
+            listener->stateSet(mStorage->getAll());
         }
         catch(const Ice::Exception&)
         {
@@ -189,11 +182,10 @@ public:
 
         {   // critical scope
             boost::unique_lock<boost::shared_mutex> lock(mStateLock);
-            for(typename std::vector<S>::const_iterator it = mStateItems.begin(); it != mStateItems.end(); ++it)
-            {
-                allIds.push_back((*it)->key);
-            }
-            mStateItems.clear();
+            std::vector<K> allIds;
+
+            allIds = mStorage->getAllKeys();
+            mStorage->clear();
         }
 
         boost::shared_lock<boost::shared_mutex> lock(mListenerLock);
@@ -209,20 +201,7 @@ public:
         {    // critical scope
             boost::unique_lock<boost::shared_mutex> lock(mStateLock);
 
-            for (typename std::vector<S>::const_iterator iter = items.begin();
-                 iter != items.end(); ++iter)
-            {
-                typename std::vector<S>::iterator it = std::find_if(mStateItems.begin(), mStateItems.end(), MatchItem<S,K >((*iter)->key));
-
-                if (it != mStateItems.end())
-                {
-                    (*it) = (*iter);
-                }
-                else
-                {
-                    mStateItems.push_back(*iter);
-                }
-            }
+            mStorage->set(items);
         }
 
         boost::shared_lock<boost::shared_mutex> lock(mListenerLock);
@@ -238,15 +217,7 @@ public:
         {   // critical scope
             boost::unique_lock<boost::shared_mutex> lock(mStateLock);
 
-            for (typename std::vector<K>::const_iterator keyIter = ids.begin(); keyIter != ids.end(); ++keyIter)
-            {
-                typename std::vector<S>::iterator locateIt = std::find_if(mStateItems.begin(), mStateItems.end(), MatchItem<S,K >(*keyIter));
-
-                if (locateIt != mStateItems.end())
-                {
-                    mStateItems.erase(locateIt);
-                }
-            }
+            mStorage->remove(ids);
         }
 
         boost::shared_lock<boost::shared_mutex> lock(mListenerLock);
@@ -259,22 +230,8 @@ public:
      */
     std::vector<S> getState(const std::vector<K>& itemKeys, const Ice::Current& = ::Ice::Current())
     {
-        std::vector<S> results;
-
-        {   // critical scope
-            boost::shared_lock<boost::shared_mutex> lock(mStateLock);
-
-            for(typename std::vector<K>::const_iterator keyIt = itemKeys.begin(); keyIt != itemKeys.end(); ++keyIt)
-            {
-                typename std::vector<S>::iterator locateIt = std::find_if(mStateItems.begin(), mStateItems.end(), MatchItem<S,K >(*keyIt));
-
-                if (locateIt != mStateItems.end())
-                {
-                    results.push_back(*locateIt);
-                }
-            }
-        }
-        return results;
+        boost::shared_lock<boost::shared_mutex> lock(mStateLock);
+        return mStorage->get(itemKeys);
     }
 
     /**
@@ -284,7 +241,7 @@ public:
     std::vector<S> getAllState(const Ice::Current& = ::Ice::Current())
     {
         boost::shared_lock<boost::shared_mutex> lock(mStateLock);
-        return mStateItems;
+        return mStorage->getAll();
     }
 
     /**
@@ -298,7 +255,7 @@ public:
 
 private:
     std::vector<L> mListeners;
-    std::vector<S> mStateItems;
+    StateReplicatorStorage<S,K> *mStorage;
 
     boost::shared_mutex mListenerLock;
     boost::shared_mutex mStateLock;
diff --git a/StateReplicator/src/StateReplicatorStorage.h b/StateReplicator/src/StateReplicatorStorage.h
new file mode 100644
index 0000000..e6cc390
--- /dev/null
+++ b/StateReplicator/src/StateReplicatorStorage.h
@@ -0,0 +1,39 @@
+/*
+ * Asterisk SCF -- An open-source communications framework.
+ *
+ * Copyright (C) 2010, Digium, Inc.
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk SCF project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE.txt file
+ * at the top of the source tree.
+ */
+#pragma once
+
+#include <vector>
+
+namespace AsteriskSCF
+{
+
+namespace StateReplication
+{
+
+template <class S, class K>
+class StateReplicatorStorage
+{
+public:
+	virtual void set(const std::vector<S> &items) = 0;
+	virtual std::vector<S> get(const std::vector<K> &keys) = 0;
+	virtual std::vector<S> getAll() = 0;
+	virtual void remove(const std::vector<K> &keys) = 0;
+    virtual void clear() = 0;
+	virtual std::vector<K> getAllKeys() = 0;
+};
+
+};
+};
diff --git a/StateReplicator/src/VectorStateReplicatorStorage.cpp b/StateReplicator/src/VectorStateReplicatorStorage.cpp
new file mode 100644
index 0000000..ad6d2df
--- /dev/null
+++ b/StateReplicator/src/VectorStateReplicatorStorage.cpp
@@ -0,0 +1,155 @@
+/*
+ * Asterisk SCF -- An open-source communications framework.
+ *
+ * Copyright (C) 2010, Digium, Inc.
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk SCF project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE.txt file
+ * at the top of the source tree.
+ */
+
+#include <algorithm>
+#include "VectorStateReplicatorStorage.h"
+
+namespace AsteriskSCF
+{
+
+namespace StateReplication
+{
+
+template <class S, class K>
+class VectorStateReplicatorStorageImpl
+{
+
+private:
+    template<typename T, typename D> class MatchItem
+    {
+    public:
+        MatchItem(const D& key) : mKey(key) {}
+        ~MatchItem() {}
+        bool operator() (T x) {return (x->key == mKey); }
+        D mKey;
+    };
+
+public:
+    VectorStateReplicatorStorageImpl() {};
+    ~VectorStateReplicatorStorageImpl() {};
+
+    void setImpl(const std::vector<S> &items)
+    {
+        for (typename std::vector<S>::const_iterator iter = items.begin();
+                 iter != items.end(); ++iter)
+        {
+            typename std::vector<S>::iterator it = std::find_if(mStateItems.begin(), mStateItems.end(), MatchItem<S,K >((*iter)->key));
+
+            if (it != mStateItems.end())
+            {
+                (*it) = (*iter);
+            }
+            else
+            {
+                mStateItems.push_back(*iter);
+            }
+        }
+    }
+
+    std::vector<S> getImpl(const std::vector<K> &itemKeys)
+    {
+        std::vector<S> results;
+        for(typename std::vector<K>::const_iterator keyIt = itemKeys.begin(); keyIt != itemKeys.end(); ++keyIt)
+        {
+            typename std::vector<S>::iterator locateIt = std::find_if(mStateItems.begin(), mStateItems.end(), MatchItem<S,K >(*keyIt));
+
+            if (locateIt != mStateItems.end())
+            {
+                results.push_back(*locateIt);
+            }
+        }
+        return results;
+    }
+
+    std::vector<S> getAllImpl()
+    {
+        return mStateItems;
+    }
+
+    void removeImpl(const std::vector<K> &itemKeys)
+    {
+        for (typename std::vector<K>::const_iterator keyIter = itemKeys.begin(); keyIter != itemKeys.end(); ++keyIter)
+        {
+            typename std::vector<S>::iterator locateIt = std::find_if(mStateItems.begin(), mStateItems.end(), MatchItem<S,K >(*keyIter));
+
+            if (locateIt != mStateItems.end())
+            {
+                mStateItems.erase(locateIt);
+            }
+        }
+    }
+
+    void clearImpl()
+    {
+        mStateItems.clear();
+    }
+
+
+    std::vector<K> getAllKeysImpl()
+    {
+        std::vector<K> allIds;
+        for(typename std::vector<S>::const_iterator it = mStateItems.begin(); it != mStateItems.end(); ++it)
+        {
+            allIds.push_back((*it)->key);
+        }
+        return allIds;
+    }
+
+private:
+    std::vector<S> mStateItems;
+};
+
+template <class S, class K>
+VectorStateReplicatorStorage<S, K>::VectorStateReplicatorStorage() : mImpl(new VectorStateReplicatorStorageImpl<S,K>) {}
+
+template <class S, class K>
+void VectorStateReplicatorStorage<S, K>::set(const std::vector<S> &items)
+{
+    mImpl->setImpl(items);
+}
+
+template <class S, class K>
+std::vector<S> VectorStateReplicatorStorage<S, K>::get(const std::vector<K> &keys)
+{
+    mImpl->getImpl(keys);
+}
+
+template <class S, class K>
+std::vector<S> VectorStateReplicatorStorage<S, K>::getAll()
+{
+    mImpl->getAllImpl();
+}
+
+template <class S, class K>
+void VectorStateReplicatorStorage<S, K>::remove(const std::vector<K> &keys)
+{
+    mImpl->removeImpl(keys);
+}
+
+template <class S, class K>
+void VectorStateReplicatorStorage<S, K>::clear()
+{
+    mImpl->clearImpl();
+}
+
+template <class S, class K>
+std::vector<K> VectorStateReplicatorStorage<S, K>::getAllKeys()
+{
+    mImpl->getAllKeysImpl();
+}
+
+}; //end namespace StateReplication
+}; //end namespace AsteriskSCF
diff --git a/StateReplicator/src/VectorStateReplicatorStorage.h b/StateReplicator/src/VectorStateReplicatorStorage.h
new file mode 100644
index 0000000..be5b9df
--- /dev/null
+++ b/StateReplicator/src/VectorStateReplicatorStorage.h
@@ -0,0 +1,45 @@
+/*
+ * Asterisk SCF -- An open-source communications framework.
+ *
+ * Copyright (C) 2010, Digium, Inc.
+ *
+ * See http://www.asterisk.org for more information about
+ * the Asterisk SCF project. Please do not directly contact
+ * any of the maintainers of this project for assistance;
+ * the project provides a web site, mailing lists and IRC
+ * channels for your use.
+ *
+ * This program is free software, distributed under the terms of
+ * the GNU General Public License Version 2. See the LICENSE.txt file
+ * at the top of the source tree.
+ */
+#pragma once
+
+#include "StateReplicatorStorage.h"
+
+namespace AsteriskSCF
+{
+
+namespace StateReplication
+{
+
+template <class S, class K>
+class VectorStateReplicatorStorageImpl;
+
+template <class S, class K>
+class VectorStateReplicatorStorage : public StateReplicatorStorage<S, K>
+{
+public:
+    VectorStateReplicatorStorage();
+	void set(const std::vector<S> &items);
+	std::vector<S> get(const std::vector<K> &keys);
+	std::vector<S> getAll();
+	void remove(const std::vector<K> &keys);
+    void clear();
+	std::vector<K> getAllKeys();
+private:
+	VectorStateReplicatorStorageImpl<S, K> *mImpl;
+};
+
+};
+};

-----------------------------------------------------------------------


-- 
asterisk-scf/release/ice-util-cpp.git



More information about the asterisk-scf-commits mailing list