Simplify storage context mgmt.
authorScott Cantor <cantor.2@osu.edu>
Fri, 22 Sep 2006 19:28:38 +0000 (19:28 +0000)
committerScott Cantor <cantor.2@osu.edu>
Fri, 22 Sep 2006 19:28:38 +0000 (19:28 +0000)
xmltooling/impl/MemoryStorageService.cpp
xmltooling/util/StorageService.h
xmltoolingtest/MemoryStorageServiceTest.h

index 4acd7c3..0179897 100644 (file)
@@ -40,29 +40,29 @@ namespace xmltooling {
         MemoryStorageService(const DOMElement* e);\r
         virtual ~MemoryStorageService();\r
         \r
-        StorageHandle* createHandle();\r
+        void createString(const char* context, const char* key, const char* value, time_t expiration);\r
+        bool readString(const char* context, const char* key, string& value, time_t modifiedSince=0);\r
+        bool updateString(const char* context, const char* key, const char* value=NULL, time_t expiration=0);\r
+        bool deleteString(const char* context, const char* key);\r
         \r
-        void createString(StorageHandle* handle, const char* key, const char* value, time_t expiration);\r
-        bool readString(StorageHandle* handle, const char* key, string& value, time_t modifiedSince=0);\r
-        bool updateString(StorageHandle* handle, const char* key, const char* value=NULL, time_t expiration=0);\r
-        bool deleteString(StorageHandle* handle, const char* key);\r
-        \r
-        void createText(StorageHandle* handle, const char* key, const char* value, time_t expiration) {\r
-            return createString(handle, key, value, expiration);\r
+        void createText(const char* context, const char* key, const char* value, time_t expiration) {\r
+            return createString(context, key, value, expiration);\r
         }\r
-        bool readText(StorageHandle* handle, const char* key, string& value, time_t modifiedSince=0) {\r
-            return readString(handle, key, value, modifiedSince);\r
+        bool readText(const char* context, const char* key, string& value, time_t modifiedSince=0) {\r
+            return readString(context, key, value, modifiedSince);\r
         }\r
-        bool updateText(StorageHandle* handle, const char* key, const char* value=NULL, time_t expiration=0) {\r
-            return updateString(handle, key, value, expiration);\r
+        bool updateText(const char* context, const char* key, const char* value=NULL, time_t expiration=0) {\r
+            return updateString(context, key, value, expiration);\r
         }\r
-        bool deleteText(StorageHandle* handle, const char* key) {\r
-            return deleteString(handle, key);\r
+        bool deleteText(const char* context, const char* key) {\r
+            return deleteString(context, key);\r
         }\r
         \r
-        void reap(StorageHandle* handle);\r
-\r
-        void removeHandle(StorageHandle* handle);\r
+        void reap(const char* context);\r
+        void deleteContext(const char* context) {\r
+            Lock wrapper(contextLock);\r
+            m_contextMap.erase(context);\r
+        }\r
 \r
     private:\r
         void cleanup();\r
@@ -74,20 +74,27 @@ namespace xmltooling {
             time_t modified, expiration;\r
         };\r
         \r
-        struct XMLTOOL_DLLLOCAL MemoryHandle : public StorageHandle {\r
-            MemoryHandle(StorageService* storage) : StorageHandle(storage), m_lock(RWLock::create()) {}\r
-            virtual ~MemoryHandle() {\r
-                delete m_lock;\r
-                static_cast<MemoryStorageService*>(m_storage)->removeHandle(this);\r
+        struct XMLTOOL_DLLLOCAL Context {\r
+            Context() : m_lock(RWLock::create()) {}\r
+            Context(const Context& src) {\r
+                m_dataMap = src.m_dataMap;\r
+                m_expMap = src.m_expMap;\r
+                m_lock = RWLock::create();\r
             }\r
+            ~Context() { delete m_lock; }\r
             map<string,Record> m_dataMap;\r
             multimap<time_t,string> m_expMap;\r
             RWLock* m_lock;\r
             unsigned long reap();\r
         };\r
-        \r
-        vector<MemoryHandle*> m_handles;\r
-        Mutex* mutex;\r
+\r
+        Context& getContext(const char* context) {\r
+            Lock wrapper(contextLock);\r
+            return m_contextMap[context];\r
+        }\r
+\r
+        map<string,Context> m_contextMap;\r
+        Mutex* contextLock;\r
         CondWait* shutdown_wait;\r
         Thread* cleanup_thread;\r
         static void* cleanup_fn(void*);\r
@@ -106,10 +113,10 @@ namespace xmltooling {
 static const XMLCh cleanupInterval[] = UNICODE_LITERAL_15(c,l,e,a,n,u,p,I,n,t,e,r,v,a,l);\r
 \r
 MemoryStorageService::MemoryStorageService(const DOMElement* e)\r
-    : mutex(NULL), shutdown_wait(NULL), cleanup_thread(NULL), shutdown(false), m_cleanupInterval(0),\r
+    : contextLock(NULL), shutdown_wait(NULL), cleanup_thread(NULL), shutdown(false), m_cleanupInterval(0),\r
         m_log(Category::getInstance(XMLTOOLING_LOGCAT".StorageService"))\r
 {\r
-    mutex = Mutex::create();\r
+    contextLock = Mutex::create();\r
     shutdown_wait = CondWait::create();\r
     cleanup_thread = Thread::create(&cleanup_fn, (void*)this);\r
 \r
@@ -129,27 +136,7 @@ MemoryStorageService::~MemoryStorageService()
     cleanup_thread->join(NULL);\r
 \r
     delete shutdown_wait;\r
-    delete mutex;\r
-    for_each(m_handles.begin(), m_handles.end(), xmltooling::cleanup<MemoryHandle>());\r
-}\r
-\r
-StorageService::StorageHandle* MemoryStorageService::createHandle()\r
-{\r
-    Lock wrapper(mutex);\r
-    MemoryHandle* ret = new MemoryHandle(this);\r
-    m_handles.push_back(ret);\r
-    return ret;\r
-}\r
-\r
-void MemoryStorageService::removeHandle(StorageHandle* handle)\r
-{\r
-    Lock wrapper(mutex);\r
-    for (vector<MemoryHandle*>::iterator i=m_handles.begin(); i!=m_handles.end(); ++i) {\r
-        if (*i == handle) {\r
-            m_handles.erase(i);\r
-            return;\r
-        }\r
-    }\r
+    delete contextLock;\r
 }\r
 \r
 void* MemoryStorageService::cleanup_fn(void* cache_p)\r
@@ -184,8 +171,9 @@ void MemoryStorageService::cleanup()
             break;\r
         \r
         unsigned long count=0;\r
-        for (vector<MemoryHandle*>::iterator i=m_handles.begin(); i!=m_handles.end(); ++i)\r
-            count += (*i)->reap();\r
+        Lock wrapper(contextLock);\r
+        for (map<string,Context>::iterator i=m_contextMap.begin(); i!=m_contextMap.end(); ++i)\r
+            count += i->second.reap();\r
         \r
         if (count)\r
             m_log.info("purged %d record(s) from storage", count);\r
@@ -198,14 +186,12 @@ void MemoryStorageService::cleanup()
     Thread::exit(NULL);\r
 }\r
 \r
-void MemoryStorageService::reap(StorageHandle* handle)\r
+void MemoryStorageService::reap(const char* context)\r
 {\r
-    if (!isValid(handle))\r
-        throw IOException("Invalid storage handle.");\r
-    static_cast<MemoryHandle*>(handle)->reap();\r
+    getContext(context).reap();\r
 }\r
 \r
-unsigned long MemoryStorageService::MemoryHandle::reap()\r
+unsigned long MemoryStorageService::Context::reap()\r
 {\r
     // Lock the "database".\r
     m_lock->wrlock();\r
@@ -223,36 +209,32 @@ unsigned long MemoryStorageService::MemoryHandle::reap()
     return count;\r
 }\r
 \r
-void MemoryStorageService::createString(StorageHandle* handle, const char* key, const char* value, time_t expiration)\r
+void MemoryStorageService::createString(const char* context, const char* key, const char* value, time_t expiration)\r
 {\r
-    if (!isValid(handle))\r
-        throw IOException("Invalid storage handle.");\r
-    MemoryHandle* h = static_cast<MemoryHandle*>(handle);\r
+    Context& ctx = getContext(context);\r
 \r
     // Lock the maps.\r
-    h->m_lock->wrlock();\r
-    SharedLock wrapper(h->m_lock, false);\r
+    ctx.m_lock->wrlock();\r
+    SharedLock wrapper(ctx.m_lock, false);\r
     \r
     // Check for a duplicate.\r
-    map<string,Record>::iterator i=h->m_dataMap.find(key);\r
-    if (i!=h->m_dataMap.end())\r
+    map<string,Record>::iterator i=ctx.m_dataMap.find(key);\r
+    if (i!=ctx.m_dataMap.end())\r
         throw IOException("attempted to insert a record with duplicate key ($1)", params(1,key));\r
     \r
-    h->m_dataMap[key]=Record(value,time(NULL),expiration);\r
-    h->m_expMap.insert(multimap<time_t,string>::value_type(expiration,key));\r
+    ctx.m_dataMap[key]=Record(value,time(NULL),expiration);\r
+    ctx.m_expMap.insert(multimap<time_t,string>::value_type(expiration,key));\r
     \r
     m_log.debug("inserted record (%s)", key);\r
 }\r
 \r
-bool MemoryStorageService::readString(StorageHandle* handle, const char* key, string& value, time_t modifiedSince)\r
+bool MemoryStorageService::readString(const char* context, const char* key, string& value, time_t modifiedSince)\r
 {\r
-    if (!isValid(handle))\r
-        throw IOException("Invalid storage handle.");\r
-    MemoryHandle* h = static_cast<MemoryHandle*>(handle);\r
+    Context& ctx = getContext(context);\r
 \r
-    SharedLock wrapper(h->m_lock);\r
-    map<string,Record>::iterator i=h->m_dataMap.find(key);\r
-    if (i==h->m_dataMap.end())\r
+    SharedLock wrapper(ctx.m_lock);\r
+    map<string,Record>::iterator i=ctx.m_dataMap.find(key);\r
+    if (i==ctx.m_dataMap.end())\r
         return false;\r
     else if (modifiedSince >= i->second.modified)\r
         return false;\r
@@ -260,18 +242,16 @@ bool MemoryStorageService::readString(StorageHandle* handle, const char* key, st
     return true;\r
 }\r
 \r
-bool MemoryStorageService::updateString(StorageHandle* handle, const char* key, const char* value, time_t expiration)\r
+bool MemoryStorageService::updateString(const char* context, const char* key, const char* value, time_t expiration)\r
 {\r
-    if (!isValid(handle))\r
-        throw IOException("Invalid storage handle.");\r
-    MemoryHandle* h = static_cast<MemoryHandle*>(handle);\r
+    Context& ctx = getContext(context);\r
 \r
     // Lock the maps.\r
-    h->m_lock->wrlock();\r
-    SharedLock wrapper(h->m_lock, false);\r
+    ctx.m_lock->wrlock();\r
+    SharedLock wrapper(ctx.m_lock, false);\r
 \r
-    map<string,Record>::iterator i=h->m_dataMap.find(key);\r
-    if (i==h->m_dataMap.end())\r
+    map<string,Record>::iterator i=ctx.m_dataMap.find(key);\r
+    if (i==ctx.m_dataMap.end())\r
         return false;\r
         \r
     if (value)\r
@@ -280,15 +260,15 @@ bool MemoryStorageService::updateString(StorageHandle* handle, const char* key,
     if (expiration && expiration != i->second.expiration) {\r
         // Update secondary map.\r
         pair<multimap<time_t,string>::iterator,multimap<time_t,string>::iterator> range =\r
-            h->m_expMap.equal_range(i->second.expiration);\r
+            ctx.m_expMap.equal_range(i->second.expiration);\r
         for (; range.first != range.second; ++range.first) {\r
             if (range.first->second == i->first) {\r
-                h->m_expMap.erase(range.first);\r
+                ctx.m_expMap.erase(range.first);\r
                 break;\r
             }\r
         }\r
         i->second.expiration = expiration;\r
-       h->m_expMap.insert(multimap<time_t,string>::value_type(expiration,key));\r
+       ctx.m_expMap.insert(multimap<time_t,string>::value_type(expiration,key));\r
     }\r
 \r
     i->second.modified = time(NULL);\r
@@ -296,30 +276,28 @@ bool MemoryStorageService::updateString(StorageHandle* handle, const char* key,
     return true;\r
 }\r
 \r
-bool MemoryStorageService::deleteString(StorageHandle* handle, const char* key)\r
+bool MemoryStorageService::deleteString(const char* context, const char* key)\r
 {\r
-    if (!isValid(handle))\r
-        throw IOException("Invalid storage handle.");\r
-    MemoryHandle* h = static_cast<MemoryHandle*>(handle);\r
+    Context& ctx = getContext(context);\r
 \r
     // Lock the maps.\r
-    h->m_lock->wrlock();\r
-    SharedLock wrapper(h->m_lock, false);\r
+    ctx.m_lock->wrlock();\r
+    SharedLock wrapper(ctx.m_lock, false);\r
     \r
     // Find the record.\r
-    map<string,Record>::iterator i=h->m_dataMap.find(key);\r
-    if (i!=h->m_dataMap.end()) {\r
+    map<string,Record>::iterator i=ctx.m_dataMap.find(key);\r
+    if (i!=ctx.m_dataMap.end()) {\r
         // Now find the reversed index of expiration to key, so we can clear it.\r
         pair<multimap<time_t,string>::iterator,multimap<time_t,string>::iterator> range =\r
-            h->m_expMap.equal_range(i->second.expiration);\r
+            ctx.m_expMap.equal_range(i->second.expiration);\r
         for (; range.first != range.second; ++range.first) {\r
             if (range.first->second == i->first) {\r
-                h->m_expMap.erase(range.first);\r
+                ctx.m_expMap.erase(range.first);\r
                 break;\r
             }\r
         }\r
         // And finally delete the record itself.\r
-        h->m_dataMap.erase(i);\r
+        ctx.m_dataMap.erase(i);\r
         m_log.debug("deleted record (%s)", key);\r
         return true;\r
     }\r
index 7a2a707..fe6318f 100644 (file)
@@ -17,7 +17,7 @@
 /**\r
  * @file xmltooling/util/StorageService.h\r
  * \r
- * Generic data storage facility for use by services that require persistence.\r
+ * Generic data storage interface\r
  */\r
 \r
 #ifndef __xmltooling_storage_h__\r
 namespace xmltooling {\r
 \r
     /**\r
-     * Generic data storage facility for use by services that require persistence.\r
+     * Generic data storage facility for use by services that require\r
+     * some degree of persistence. Implementations will vary in how much\r
+     * persistence they can supply.\r
+     * \r
+     * <p>Storage is divided into "contexts" identified by a string label.\r
+     * Keys need to be unique only within a given context, so multiple\r
+     * components can share a single storage service safely as long as they\r
+     * use different labels.\r
      */\r
     class XMLTOOL_API StorageService\r
     {\r
@@ -39,47 +46,21 @@ namespace xmltooling {
         virtual ~StorageService() {}\r
         \r
         /**\r
-         * A "context" for accessing a StorageService instance.\r
-         * Handles are created and freed using the StorageService interface,\r
-         * and can be kept for the life of the service, and shared by multiple\r
-         * threads.\r
-         */\r
-        class XMLTOOL_API StorageHandle {\r
-            MAKE_NONCOPYABLE(StorageHandle);\r
-            friend class XMLTOOL_API StorageService;\r
-        public:\r
-            virtual ~StorageHandle() {}\r
-        protected:\r
-            StorageHandle(StorageService* storage) : m_storage(storage) {}\r
-            StorageService* m_storage;\r
-        };\r
-        \r
-        \r
-        /**\r
-         * Returns a new handle for the storage service.\r
-         * The caller <strong>MUST</strong> delete the handle\r
-         * before freeing the StorageService itself.\r
-         * \r
-         * @return  a new handle\r
-         */\r
-        virtual StorageHandle* createHandle()=0;\r
-        \r
-        /**\r
          * Creates a new "short" record in the storage service.\r
          * \r
-         * @param handle        a valid storage handle\r
+         * @param context       a storage context label\r
          * @param key           null-terminated unique key of up to 255 bytes\r
          * @param value         null-terminated value of up to 255 bytes to store\r
          * @param expiration    an expiration timestamp, after which the record can be purged\r
          * \r
          * @throws IOException  raised if errors occur in the insertion process \r
          */\r
-        virtual void createString(StorageHandle* handle, const char* key, const char* value, time_t expiration)=0;\r
+        virtual void createString(const char* context, const char* key, const char* value, time_t expiration)=0;\r
         \r
         /**\r
          * Returns an existing "short" record from the storage service.\r
          * \r
-         * @param handle        a valid storage handle\r
+         * @param context       a storage context label\r
          * @param key           null-terminated unique key of up to 255 bytes\r
          * @param value         location in which to return the record value\r
          * @param modifiedSince the record should not be returned if unmodified since this time,\r
@@ -88,12 +69,12 @@ namespace xmltooling {
          * \r
          * @throws IOException  raised if errors occur in the read process \r
          */\r
-        virtual bool readString(StorageHandle* handle, const char* key, std::string& value, time_t modifiedSince=0)=0;\r
+        virtual bool readString(const char* context, const char* key, std::string& value, time_t modifiedSince=0)=0;\r
 \r
         /**\r
          * Updates an existing "short" record in the storage service.\r
          * \r
-         * @param handle        a valid storage handle\r
+         * @param context       a storage context label\r
          * @param key           null-terminated unique key of up to 255 bytes\r
          * @param value         null-terminated value of up to 255 bytes to store, or NULL to leave alone\r
          * @param expiration    a new expiration timestamp, or 0 to leave alone\r
@@ -101,35 +82,35 @@ namespace xmltooling {
          *    \r
          * @throws IOException  raised if errors occur in the update process \r
          */\r
-        virtual bool updateString(StorageHandle* handle, const char* key, const char* value=NULL, time_t expiration=0)=0;\r
+        virtual bool updateString(const char* context, const char* key, const char* value=NULL, time_t expiration=0)=0;\r
         \r
         /**\r
          * Deletes an existing "short" record from the storage service.\r
          * \r
-         * @param handle        a valid storage handle\r
+         * @param context       a storage context label\r
          * @param key           null-terminated unique key of up to 255 bytes\r
          * @return true iff the record existed and was deleted\r
          *    \r
          * @throws IOException  raised if errors occur in the deletion process \r
          */\r
-        virtual bool deleteString(StorageHandle* handle, const char* key)=0;\r
+        virtual bool deleteString(const char* context, const char* key)=0;\r
         \r
         /**\r
          * Creates a new "long" record in the storage service.\r
          * \r
-         * @param handle        a valid storage handle\r
+         * @param context       a storage context label\r
          * @param key           null-terminated unique key of up to 255 bytes\r
          * @param value         null-terminated value of arbitrary length\r
          * @param expiration    an expiration timestamp, after which the record can be purged\r
          * \r
          * @throws IOException  raised if errors occur in the insertion process \r
          */\r
-        virtual void createText(StorageHandle* handle, const char* key, const char* value, time_t expiration)=0;\r
+        virtual void createText(const char* context, const char* key, const char* value, time_t expiration)=0;\r
         \r
         /**\r
          * Returns an existing "long" record from the storage service.\r
          * \r
-         * @param handle        a valid storage handle\r
+         * @param context       a storage context label\r
          * @param key           null-terminated unique key of up to 255 bytes\r
          * @param value         location in which to return the record value\r
          * @param modifiedSince the record should not be returned if unmodified since this time,\r
@@ -138,12 +119,12 @@ namespace xmltooling {
          *    \r
          * @throws IOException  raised if errors occur in the read process \r
          */\r
-        virtual bool readText(StorageHandle* handle, const char* key, std::string& value, time_t modifiedSince=0)=0;\r
+        virtual bool readText(const char* context, const char* key, std::string& value, time_t modifiedSince=0)=0;\r
 \r
         /**\r
          * Updates an existing "long" record in the storage service.\r
          * \r
-         * @param handle        a valid storage handle\r
+         * @param context       a storage context label\r
          * @param key           null-terminated unique key of up to 255 bytes\r
          * @param value         null-terminated value of arbitrary length to store, or NULL to leave alone\r
          * @param expiration    a new expiration timestamp, or 0 to leave alone\r
@@ -151,34 +132,38 @@ namespace xmltooling {
          *    \r
          * @throws IOException  raised if errors occur in the update process \r
          */\r
-        virtual bool updateText(StorageHandle* handle, const char* key, const char* value=NULL, time_t expiration=0)=0;\r
+        virtual bool updateText(const char* context, const char* key, const char* value=NULL, time_t expiration=0)=0;\r
         \r
         /**\r
          * Deletes an existing "long" record from the storage service.\r
          * \r
-         * @param handle        a valid storage handle\r
+         * @param context       a storage context label\r
          * @param key           null-terminated unique key of up to 255 bytes\r
          * @return true iff the record existed and was deleted\r
          *    \r
          * @throws IOException  raised if errors occur in the deletion process \r
          */\r
-        virtual bool deleteText(StorageHandle* handle, const char* key)=0;\r
+        virtual bool deleteText(const char* context, const char* key)=0;\r
         \r
         /**\r
          * Manually trigger a cleanup of expired records.\r
          * The method <strong>MAY</strong> return without guaranteeing that\r
          * cleanup has already occurred.\r
          * \r
-         * @param handle        a valid storage handle\r
+         * @param context       a storage context label\r
+         */\r
+        virtual void reap(const char* context)=0;\r
+        \r
+        /**\r
+         * Forcibly removes all records in a given context along with any\r
+         * associated resources devoted to maintaining the context.\r
+         * \r
+         * @param context       a storage context label\r
          */\r
-        virtual void reap(StorageHandle* handle)=0;\r
+        virtual void deleteContext(const char* context)=0;\r
 \r
     protected:\r
         StorageService() {}\r
-        \r
-        virtual bool isValid(StorageHandle* handle) {\r
-            return this == (handle ? handle->m_storage : NULL);\r
-        }\r
     };\r
 \r
     /**\r
index 99e2e0e..a673e9a 100644 (file)
@@ -30,19 +30,17 @@ public:
         auto_ptr<StorageService> storage(\r
             XMLToolingConfig::getConfig().StorageServiceManager.newPlugin(MEMORY_STORAGE_SERVICE,NULL)\r
             );\r
-        auto_ptr<StorageService::StorageHandle> handle(storage->createHandle());\r
 \r
         string data;\r
-        TSM_ASSERT("Record found in storage.", !storage->readString(handle.get(), "foo1", data));\r
-        storage->createString(handle.get(), "foo1", "bar1", time(NULL) - 300);\r
-        storage->createString(handle.get(), "foo2", "bar2", time(NULL));\r
-        TSM_ASSERT("Record not found in storage.", storage->readString(handle.get(), "foo1", data));\r
+        TSM_ASSERT("Record found in storage.", !storage->readString("context", "foo1", data));\r
+        storage->createString("context", "foo1", "bar1", time(NULL) - 300);\r
+        storage->createString("context", "foo2", "bar2", time(NULL));\r
+        TSM_ASSERT("Record not found in storage.", storage->readString("context", "foo1", data));\r
         TSM_ASSERT_EQUALS("Record value doesn't match.", data, "bar1");\r
-        TSM_ASSERT("Update failed.", storage->updateString(handle.get(), "foo2", "bar1"));\r
-        TSM_ASSERT("Record not found in storage.", storage->readString(handle.get(), "foo2", data));\r
+        TSM_ASSERT("Update failed.", storage->updateString("context", "foo2", "bar1"));\r
+        TSM_ASSERT("Record not found in storage.", storage->readString("context", "foo2", data));\r
         TSM_ASSERT_EQUALS("Record value doesn't match.", data, "bar1");\r
-        TSM_ASSERT("Delete failed.", storage->deleteString(handle.get(), "foo2"));\r
-        storage->reap(handle.get());\r
-        Thread::sleep(1);\r
+        TSM_ASSERT("Delete failed.", storage->deleteString("context", "foo2"));\r
+        storage->reap("context");\r
     }\r
 };\r