Xerces 3 revisions.
[shibboleth/cpp-xmltooling.git] / xmltooling / impl / MemoryStorageService.cpp
index 4acd7c3..7e9b845 100644 (file)
@@ -1,6 +1,6 @@
 /*\r
- *  Copyright 2001-2005 Internet2\r
- * \r
+ *  Copyright 2001-2007 Internet2\r
+ *\r
  * Licensed under the Apache License, Version 2.0 (the "License");\r
  * you may not use this file except in compliance with the License.\r
  * You may obtain a copy of the License at\r
 \r
 /**\r
  * MemoryStorageService.cpp\r
- * \r
+ *\r
  * In-memory "persistent" storage, suitable for simple applications.\r
  */\r
 \r
 #include "internal.h"\r
+#include "logging.h"\r
 #include "util/NDC.h"\r
 #include "util/StorageService.h"\r
 #include "util/Threads.h"\r
 #include "util/XMLHelper.h"\r
 \r
-#include <log4cpp/Category.hh>\r
+#include <memory>\r
 #include <xercesc/util/XMLUniDefs.hpp>\r
 \r
+using namespace xmltooling::logging;\r
 using namespace xmltooling;\r
-using namespace log4cpp;\r
 using namespace std;\r
 \r
+using xercesc::DOMElement;\r
+\r
 namespace xmltooling {\r
     class XMLTOOL_DLLLOCAL MemoryStorageService : public StorageService\r
     {\r
     public:\r
         MemoryStorageService(const DOMElement* e);\r
         virtual ~MemoryStorageService();\r
-        \r
-        StorageHandle* createHandle();\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
+\r
+        bool createString(const char* context, const char* key, const char* value, time_t expiration);\r
+        int readString(const char* context, const char* key, string* pvalue=NULL, time_t* pexpiration=NULL, int version=0);\r
+        int updateString(const char* context, const char* key, const char* value=NULL, time_t expiration=0, int version=0);\r
+        bool deleteString(const char* context, const char* key);\r
+\r
+        bool 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
+        int readText(const char* context, const char* key, string* pvalue=NULL, time_t* pexpiration=NULL, int version=0) {\r
+            return readString(context, key, pvalue, pexpiration, version);\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
+        int updateText(const char* context, const char* key, const char* value=NULL, time_t expiration=0, int version=0) {\r
+            return updateString(context, key, value, expiration, version);\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 updateContext(const char* context, time_t expiration);\r
+        void deleteContext(const char* context) {\r
+            m_lock->wrlock();\r
+            m_contextMap.erase(context);\r
+            m_lock->unlock();\r
+        }\r
 \r
     private:\r
         void cleanup();\r
-    \r
+\r
         struct XMLTOOL_DLLLOCAL Record {\r
-            Record() : modified(0), expiration(0) {}\r
-            Record(string s, time_t t1, time_t t2) : data(s), modified(t1), expiration(t2) {}\r
+            Record() : expiration(0), version(1) {}\r
+            Record(const string& s, time_t t) : data(s), expiration(t), version(1) {}\r
             string data;\r
-            time_t modified, expiration;\r
+            time_t expiration;\r
+            int version;\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
+\r
+        struct XMLTOOL_DLLLOCAL Context {\r
+            Context() {}\r
+            Context(const Context& src) {\r
+                m_dataMap = src.m_dataMap;\r
             }\r
             map<string,Record> m_dataMap;\r
-            multimap<time_t,string> m_expMap;\r
-            RWLock* m_lock;\r
-            unsigned long reap();\r
+            unsigned long reap(time_t exp);\r
         };\r
-        \r
-        vector<MemoryHandle*> m_handles;\r
-        Mutex* mutex;\r
+\r
+        Context& readContext(const char* context) {\r
+            m_lock->rdlock();\r
+            map<string,Context>::iterator i = m_contextMap.find(context);\r
+            if (i != m_contextMap.end())\r
+                return i->second;\r
+            m_lock->unlock();\r
+            m_lock->wrlock();\r
+            return m_contextMap[context];\r
+        }\r
+\r
+        Context& writeContext(const char* context) {\r
+            m_lock->wrlock();\r
+            return m_contextMap[context];\r
+        }\r
+\r
+        map<string,Context> m_contextMap;\r
+        RWLock* m_lock;\r
         CondWait* shutdown_wait;\r
         Thread* cleanup_thread;\r
         static void* cleanup_fn(void*);\r
@@ -100,25 +118,24 @@ namespace xmltooling {
     {\r
         return new MemoryStorageService(e);\r
     }\r
-\r
 };\r
 \r
 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
+    : m_lock(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
-    shutdown_wait = CondWait::create();\r
-    cleanup_thread = Thread::create(&cleanup_fn, (void*)this);\r
-\r
     const XMLCh* tag=e ? e->getAttributeNS(NULL,cleanupInterval) : NULL;\r
     if (tag && *tag) {\r
         m_cleanupInterval = XMLString::parseInt(tag);\r
     }\r
     if (!m_cleanupInterval)\r
-        m_cleanupInterval=300;\r
+        m_cleanupInterval=900;\r
+\r
+    m_lock = RWLock::create();\r
+    shutdown_wait = CondWait::create();\r
+    cleanup_thread = Thread::create(&cleanup_fn, (void*)this);\r
 }\r
 \r
 MemoryStorageService::~MemoryStorageService()\r
@@ -128,28 +145,9 @@ MemoryStorageService::~MemoryStorageService()
     shutdown_wait->signal();\r
     cleanup_thread->join(NULL);\r
 \r
+    delete cleanup_thread;\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 m_lock;\r
 }\r
 \r
 void* MemoryStorageService::cleanup_fn(void* cache_p)\r
@@ -157,7 +155,7 @@ void* MemoryStorageService::cleanup_fn(void* cache_p)
     MemoryStorageService* cache = reinterpret_cast<MemoryStorageService*>(cache_p);\r
 \r
 #ifndef WIN32\r
-    // First, let's block all signals \r
+    // First, let's block all signals\r
     Thread::mask_all_signals();\r
 #endif\r
 \r
@@ -171,159 +169,154 @@ void MemoryStorageService::cleanup()
 #ifdef _DEBUG\r
     NDC ndc("cleanup");\r
 #endif\r
-    \r
 \r
-    Mutex* mutex = Mutex::create();\r
+    auto_ptr<Mutex> mutex(Mutex::create());\r
     mutex->lock();\r
 \r
     m_log.info("cleanup thread started...running every %d seconds", m_cleanupInterval);\r
 \r
     while (!shutdown) {\r
-        shutdown_wait->timedwait(mutex, m_cleanupInterval);\r
+        shutdown_wait->timedwait(mutex.get(), m_cleanupInterval);\r
         if (shutdown)\r
             break;\r
-        \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
-        \r
+        time_t now = time(NULL);\r
+        m_lock->wrlock();\r
+        SharedLock locker(m_lock, false);\r
+        for (map<string,Context>::iterator i=m_contextMap.begin(); i!=m_contextMap.end(); ++i)\r
+            count += i->second.reap(now);\r
+\r
         if (count)\r
-            m_log.info("purged %d record(s) from storage", count);\r
+            m_log.info("purged %d expired record(s) from storage", count);\r
     }\r
 \r
     m_log.info("cleanup thread finished");\r
 \r
     mutex->unlock();\r
-    delete mutex;\r
     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
+    Context& ctx = writeContext(context);\r
+    SharedLock locker(m_lock, false);\r
+    ctx.reap(time(NULL));\r
 }\r
 \r
-unsigned long MemoryStorageService::MemoryHandle::reap()\r
+unsigned long MemoryStorageService::Context::reap(time_t exp)\r
 {\r
-    // Lock the "database".\r
-    m_lock->wrlock();\r
-    SharedLock wrapper(m_lock, false);\r
-    \r
     // Garbage collect any expired entries.\r
     unsigned long count=0;\r
-    time_t now=time(NULL)-XMLToolingConfig::getConfig().clock_skew_secs;\r
-    multimap<time_t,string>::iterator stop=m_expMap.upper_bound(now);\r
-    for (multimap<time_t,string>::iterator i=m_expMap.begin(); i!=stop; m_expMap.erase(i++)) {\r
-        m_dataMap.erase(i->second);\r
-        ++count;\r
+    map<string,Record>::iterator cur = m_dataMap.begin();\r
+    map<string,Record>::iterator stop = m_dataMap.end();\r
+    while (cur != stop) {\r
+        if (cur->second.expiration <= exp) {\r
+            map<string,Record>::iterator tmp = cur++;\r
+            m_dataMap.erase(tmp);\r
+            ++count;\r
+        }\r
+        else {\r
+            cur++;\r
+        }\r
     }\r
-\r
     return count;\r
 }\r
 \r
-void MemoryStorageService::createString(StorageHandle* handle, const char* key, const char* value, time_t expiration)\r
+bool 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
-\r
-    // Lock the maps.\r
-    h->m_lock->wrlock();\r
-    SharedLock wrapper(h->m_lock, false);\r
-    \r
+    Context& ctx = writeContext(context);\r
+    SharedLock locker(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
-        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
-    \r
-    m_log.debug("inserted record (%s)", key);\r
+    map<string,Record>::iterator i=ctx.m_dataMap.find(key);\r
+    if (i!=ctx.m_dataMap.end()) {\r
+        // Not yet expired?\r
+        if (time(NULL) < i->second.expiration)\r
+            return false;\r
+        // It's dead, so we can just remove it now and create the new record.\r
+        ctx.m_dataMap.erase(i);\r
+    }\r
+\r
+    ctx.m_dataMap[key]=Record(value,expiration);\r
+\r
+    m_log.debug("inserted record (%s) in context (%s)", key, context);\r
+    return true;\r
 }\r
 \r
-bool MemoryStorageService::readString(StorageHandle* handle, const char* key, string& value, time_t modifiedSince)\r
+int MemoryStorageService::readString(const char* context, const char* key, string* pvalue, time_t* pexpiration, int version)\r
 {\r
-    if (!isValid(handle))\r
-        throw IOException("Invalid storage handle.");\r
-    MemoryHandle* h = static_cast<MemoryHandle*>(handle);\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
-        return false;\r
-    else if (modifiedSince >= i->second.modified)\r
-        return false;\r
-    value = i->second.data;\r
-    return true;\r
+    Context& ctx = readContext(context);\r
+    SharedLock locker(m_lock, false);\r
+\r
+    map<string,Record>::iterator i=ctx.m_dataMap.find(key);\r
+    if (i==ctx.m_dataMap.end())\r
+        return 0;\r
+    else if (time(NULL) >= i->second.expiration)\r
+        return 0;\r
+    if (pexpiration)\r
+        *pexpiration = i->second.expiration;\r
+    if (i->second.version == version)\r
+        return version; // nothing's changed, so just echo back the version\r
+    if (pvalue)\r
+        *pvalue = i->second.data;\r
+    return i->second.version;\r
 }\r
 \r
-bool MemoryStorageService::updateString(StorageHandle* handle, const char* key, const char* value, time_t expiration)\r
+int MemoryStorageService::updateString(const char* context, const char* key, const char* value, time_t expiration, int version)\r
 {\r
-    if (!isValid(handle))\r
-        throw IOException("Invalid storage handle.");\r
-    MemoryHandle* h = static_cast<MemoryHandle*>(handle);\r
-\r
-    // Lock the maps.\r
-    h->m_lock->wrlock();\r
-    SharedLock wrapper(h->m_lock, false);\r
-\r
-    map<string,Record>::iterator i=h->m_dataMap.find(key);\r
-    if (i==h->m_dataMap.end())\r
-        return false;\r
-        \r
-    if (value)\r
+    Context& ctx = writeContext(context);\r
+    SharedLock locker(m_lock, false);\r
+\r
+    map<string,Record>::iterator i=ctx.m_dataMap.find(key);\r
+    if (i==ctx.m_dataMap.end())\r
+        return 0;\r
+    else if (time(NULL) >= i->second.expiration)\r
+        return 0;\r
+\r
+    if (version > 0 && version != i->second.version)\r
+        return -1;  // caller's out of sync\r
+\r
+    if (value) {\r
         i->second.data = value;\r
-        \r
-    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
-        for (; range.first != range.second; ++range.first) {\r
-            if (range.first->second == i->first) {\r
-                h->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
+        ++(i->second.version);\r
     }\r
 \r
-    i->second.modified = time(NULL);\r
-    m_log.debug("updated record (%s)", key);\r
-    return true;\r
+    if (expiration && expiration != i->second.expiration)\r
+        i->second.expiration = expiration;\r
+\r
+    m_log.debug("updated record (%s) in context (%s)", key, context);\r
+    return i->second.version;\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
-\r
-    // Lock the maps.\r
-    h->m_lock->wrlock();\r
-    SharedLock wrapper(h->m_lock, false);\r
-    \r
+    Context& ctx = writeContext(context);\r
+    SharedLock locker(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
-        // 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
-        for (; range.first != range.second; ++range.first) {\r
-            if (range.first->second == i->first) {\r
-                h->m_expMap.erase(range.first);\r
-                break;\r
-            }\r
-        }\r
-        // And finally delete the record itself.\r
-        h->m_dataMap.erase(i);\r
-        m_log.debug("deleted record (%s)", key);\r
+    map<string,Record>::iterator i=ctx.m_dataMap.find(key);\r
+    if (i!=ctx.m_dataMap.end()) {\r
+        ctx.m_dataMap.erase(i);\r
+        m_log.debug("deleted record (%s) in context (%s)", key, context);\r
         return true;\r
     }\r
 \r
-    m_log.debug("deleting record (%s)....not found", key);\r
+    m_log.debug("deleting record (%s) in context (%s)....not found", key, context);\r
     return false;\r
 }\r
+\r
+void MemoryStorageService::updateContext(const char* context, time_t expiration)\r
+{\r
+    Context& ctx = writeContext(context);\r
+    SharedLock locker(m_lock, false);\r
+\r
+    time_t now = time(NULL);\r
+    map<string,Record>::iterator stop=ctx.m_dataMap.end();\r
+    for (map<string,Record>::iterator i = ctx.m_dataMap.begin(); i!=stop; ++i) {\r
+        if (now < i->second.expiration)\r
+            i->second.expiration = expiration;\r
+    }\r
+\r
+    m_log.debug("updated expiration of valid records in context (%s)", context);\r
+}\r