MemoryStorageService(const DOMElement* e);\r
virtual ~MemoryStorageService();\r
\r
- void createString(const char* key, const char* value, time_t expiration);\r
- bool readString(const char* key, string& value, time_t modifiedSince=0);\r
- bool updateString(const char* key, const char* value=NULL, time_t expiration=0);\r
- bool deleteString(const char* key);\r
+ StorageHandle* createHandle();\r
\r
- void createText(const char* key, const char* value, time_t expiration) {\r
- return createString(key, value, expiration);\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 readText(const char* key, string& value, time_t modifiedSince=0) {\r
- return readString(key, value, modifiedSince);\r
+ bool readText(StorageHandle* handle, const char* key, string& value, time_t modifiedSince=0) {\r
+ return readString(handle, key, value, modifiedSince);\r
}\r
- bool updateText(const char* key, const char* value=NULL, time_t expiration=0) {\r
- return updateString(key, value, expiration);\r
+ bool updateText(StorageHandle* handle, const char* key, const char* value=NULL, time_t expiration=0) {\r
+ return updateString(handle, key, value, expiration);\r
}\r
- bool deleteText(const char* key) {\r
- return deleteString(key);\r
+ bool deleteText(StorageHandle* handle, const char* key) {\r
+ return deleteString(handle, key);\r
}\r
\r
- void reap() {\r
- shutdown_wait->signal();\r
- }\r
+ void reap(StorageHandle* handle);\r
+\r
+ void removeHandle(StorageHandle* handle);\r
\r
private:\r
void cleanup();\r
time_t modified, expiration;\r
};\r
\r
- map<string,Record> m_dataMap;\r
- multimap<time_t,string> m_expMap;\r
- RWLock* m_lock;\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
+ 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
CondWait* shutdown_wait;\r
Thread* cleanup_thread;\r
static void* cleanup_fn(void*);\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
- : m_lock(NULL), shutdown_wait(NULL), cleanup_thread(NULL), shutdown(false), m_cleanupInterval(0),\r
+ : mutex(NULL), shutdown_wait(NULL), cleanup_thread(NULL), shutdown(false), m_cleanupInterval(0),\r
m_log(Category::getInstance(XMLTOOLING_LOGCAT".StorageService"))\r
{\r
- m_lock = RWLock::create();\r
+ mutex = Mutex::create();\r
shutdown_wait = CondWait::create();\r
cleanup_thread = Thread::create(&cleanup_fn, (void*)this);\r
\r
shutdown_wait->signal();\r
cleanup_thread->join(NULL);\r
\r
- delete m_lock;\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
}\r
\r
void* MemoryStorageService::cleanup_fn(void* cache_p)\r
shutdown_wait->timedwait(mutex, m_cleanupInterval);\r
if (shutdown)\r
break;\r
-\r
- // Lock the "database".\r
- m_lock->wrlock();\r
- \r
- // Garbage collect any expired entries.\r
- unsigned int 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
- }\r
\r
- m_lock->unlock();\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
if (count)\r
m_log.info("purged %d record(s) from storage", count);\r
Thread::exit(NULL);\r
}\r
\r
-void MemoryStorageService::createString(const char* key, const char* value, time_t expiration)\r
+void MemoryStorageService::reap(StorageHandle* handle)\r
{\r
- // Lock the maps.\r
+ if (!isValid(handle))\r
+ throw IOException("Invalid storage handle.");\r
+ static_cast<MemoryHandle*>(handle)->reap();\r
+}\r
+\r
+unsigned long MemoryStorageService::MemoryHandle::reap()\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
+ }\r
+\r
+ return count;\r
+}\r
+\r
+void MemoryStorageService::createString(StorageHandle* handle, 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
// Check for a duplicate.\r
- map<string,Record>::iterator i=m_dataMap.find(key);\r
- if (i!=m_dataMap.end())\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
- m_dataMap[key]=Record(value,time(NULL),expiration);\r
- m_expMap.insert(multimap<time_t,string>::value_type(expiration,key));\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
}\r
\r
-bool MemoryStorageService::readString(const char* key, string& value, time_t modifiedSince)\r
+bool MemoryStorageService::readString(StorageHandle* handle, const char* key, string& value, time_t modifiedSince)\r
{\r
- SharedLock wrapper(m_lock);\r
- map<string,Record>::iterator i=m_dataMap.find(key);\r
- if (i==m_dataMap.end())\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
return true;\r
}\r
\r
-bool MemoryStorageService::updateString(const char* key, const char* value, time_t expiration)\r
+bool MemoryStorageService::updateString(StorageHandle* handle, 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
- m_lock->wrlock();\r
- SharedLock wrapper(m_lock, false);\r
+ h->m_lock->wrlock();\r
+ SharedLock wrapper(h->m_lock, false);\r
\r
- map<string,Record>::iterator i=m_dataMap.find(key);\r
- if (i==m_dataMap.end())\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
\r
if (expiration && expiration != i->second.expiration) {\r
// Update secondary map.\r
- pair<multimap<time_t,string>::iterator,multimap<time_t,string>::iterator> range=m_expMap.equal_range(i->second.expiration);\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
- m_expMap.erase(range.first);\r
+ h->m_expMap.erase(range.first);\r
break;\r
}\r
}\r
i->second.expiration = expiration;\r
- m_expMap.insert(multimap<time_t,string>::value_type(expiration,key));\r
+ h->m_expMap.insert(multimap<time_t,string>::value_type(expiration,key));\r
}\r
\r
i->second.modified = time(NULL);\r
return true;\r
}\r
\r
-bool MemoryStorageService::deleteString(const char* key)\r
+bool MemoryStorageService::deleteString(StorageHandle* handle, 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
- m_lock->wrlock();\r
- SharedLock wrapper(m_lock, false);\r
+ h->m_lock->wrlock();\r
+ SharedLock wrapper(h->m_lock, false);\r
\r
// Find the record.\r
- map<string,Record>::iterator i=m_dataMap.find(key);\r
- if (i!=m_dataMap.end()) {\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=m_expMap.equal_range(i->second.expiration);\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
- m_expMap.erase(range.first);\r
+ h->m_expMap.erase(range.first);\r
break;\r
}\r
}\r
// And finally delete the record itself.\r
- m_dataMap.erase(i);\r
+ h->m_dataMap.erase(i);\r
m_log.debug("deleted record (%s)", key);\r
return true;\r
}\r
class XMLTOOL_API StorageService\r
{\r
MAKE_NONCOPYABLE(StorageService);\r
- protected:\r
- StorageService() {}\r
- \r
public:\r
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 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(const char* key, const char* value, time_t expiration)=0;\r
+ virtual void createString(StorageHandle* handle, 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 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
* \r
* @throws IOException raised if errors occur in the read process \r
*/\r
- virtual bool readString(const char* key, std::string& value, time_t modifiedSince=0)=0;\r
+ virtual bool readString(StorageHandle* handle, 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 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
* \r
* @throws IOException raised if errors occur in the update process \r
*/\r
- virtual bool updateString(const char* key, const char* value=NULL, time_t expiration=0)=0;\r
+ virtual bool updateString(StorageHandle* handle, 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 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(const char* key)=0;\r
+ virtual bool deleteString(StorageHandle* handle, 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 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(const char* key, const char* value, time_t expiration)=0;\r
+ virtual void createText(StorageHandle* handle, 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 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
* \r
* @throws IOException raised if errors occur in the read process \r
*/\r
- virtual bool readText(const char* key, std::string& value, time_t modifiedSince=0)=0;\r
+ virtual bool readText(StorageHandle* handle, 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 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
* \r
* @throws IOException raised if errors occur in the update process \r
*/\r
- virtual bool updateText(const char* key, const char* value=NULL, time_t expiration=0)=0;\r
+ virtual bool updateText(StorageHandle* handle, 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 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(const char* key)=0;\r
+ virtual bool deleteText(StorageHandle* handle, 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
*/\r
- virtual void reap()=0;\r
+ virtual void reap(StorageHandle* handle)=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