2 * Licensed to the University Corporation for Advanced Internet
3 * Development, Inc. (UCAID) under one or more contributor license
4 * agreements. See the NOTICE file distributed with this work for
5 * additional information regarding copyright ownership.
7 * UCAID licenses this file to you under the Apache License,
8 * Version 2.0 (the "License"); you may not use this file except
9 * in compliance with the License. You may obtain a copy of the
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing,
15 * software distributed under the License is distributed on an
16 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17 * either express or implied. See the License for the specific
18 * language governing permissions and limitations under the License.
22 * MemoryStorageService.cpp
24 * In-memory "persistent" storage, suitable for simple applications.
30 #include "util/StorageService.h"
31 #include "util/Threads.h"
32 #include "util/XMLHelper.h"
35 #include <xercesc/util/XMLUniDefs.hpp>
37 using namespace xmltooling::logging;
38 using namespace xmltooling;
41 using xercesc::DOMElement;
43 namespace xmltooling {
44 class XMLTOOL_DLLLOCAL MemoryStorageService : public StorageService
47 MemoryStorageService(const DOMElement* e);
48 virtual ~MemoryStorageService();
50 bool createString(const char* context, const char* key, const char* value, time_t expiration);
51 int readString(const char* context, const char* key, string* pvalue=nullptr, time_t* pexpiration=nullptr, int version=0);
52 int updateString(const char* context, const char* key, const char* value=nullptr, time_t expiration=0, int version=0);
53 bool deleteString(const char* context, const char* key);
55 bool createText(const char* context, const char* key, const char* value, time_t expiration) {
56 return createString(context, key, value, expiration);
58 int readText(const char* context, const char* key, string* pvalue=nullptr, time_t* pexpiration=nullptr, int version=0) {
59 return readString(context, key, pvalue, pexpiration, version);
61 int updateText(const char* context, const char* key, const char* value=nullptr, time_t expiration=0, int version=0) {
62 return updateString(context, key, value, expiration, version);
64 bool deleteText(const char* context, const char* key) {
65 return deleteString(context, key);
68 void reap(const char* context);
69 void updateContext(const char* context, time_t expiration);
70 void deleteContext(const char* context) {
72 m_contextMap.erase(context);
77 struct XMLTOOL_DLLLOCAL Record {
78 Record() : expiration(0), version(1) {}
79 Record(const string& s, time_t t) : data(s), expiration(t), version(1) {}
85 struct XMLTOOL_DLLLOCAL Context {
87 Context(const Context& src) {
88 m_dataMap = src.m_dataMap;
90 map<string,Record> m_dataMap;
91 unsigned long reap(time_t exp);
94 Context& readContext(const char* context) {
96 map<string,Context>::iterator i = m_contextMap.find(context);
97 if (i != m_contextMap.end())
101 return m_contextMap[context];
104 Context& writeContext(const char* context) {
106 return m_contextMap[context];
109 map<string,Context> m_contextMap;
111 CondWait* shutdown_wait;
112 Thread* cleanup_thread;
113 static void* cleanup_fn(void*);
115 int m_cleanupInterval;
119 StorageService* XMLTOOL_DLLLOCAL MemoryStorageServiceFactory(const DOMElement* const & e)
121 return new MemoryStorageService(e);
125 static const XMLCh cleanupInterval[] = UNICODE_LITERAL_15(c,l,e,a,n,u,p,I,n,t,e,r,v,a,l);
127 MemoryStorageService::MemoryStorageService(const DOMElement* e)
128 : m_lock(nullptr), shutdown_wait(nullptr), cleanup_thread(nullptr), shutdown(false),
129 m_cleanupInterval(XMLHelper::getAttrInt(e, 900, cleanupInterval)),
130 m_log(Category::getInstance(XMLTOOLING_LOGCAT".StorageService"))
132 m_lock = RWLock::create();
133 shutdown_wait = CondWait::create();
134 cleanup_thread = Thread::create(&cleanup_fn, (void*)this);
137 MemoryStorageService::~MemoryStorageService()
139 // Shut down the cleanup thread and let it know...
141 shutdown_wait->signal();
142 cleanup_thread->join(nullptr);
144 delete cleanup_thread;
145 delete shutdown_wait;
149 void* MemoryStorageService::cleanup_fn(void* pv)
151 MemoryStorageService* cache = reinterpret_cast<MemoryStorageService*>(pv);
154 // First, let's block all signals
155 Thread::mask_all_signals();
162 auto_ptr<Mutex> mutex(Mutex::create());
165 cache->m_log.info("cleanup thread started...running every %d seconds", cache->m_cleanupInterval);
167 while (!cache->shutdown) {
168 cache->shutdown_wait->timedwait(mutex.get(), cache->m_cleanupInterval);
172 unsigned long count=0;
173 time_t now = time(nullptr);
174 cache->m_lock->wrlock();
175 SharedLock locker(cache->m_lock, false);
176 for (map<string,Context>::iterator i=cache->m_contextMap.begin(); i!=cache->m_contextMap.end(); ++i)
177 count += i->second.reap(now);
180 cache->m_log.info("purged %d expired record(s) from storage", count);
183 cache->m_log.info("cleanup thread finished");
189 void MemoryStorageService::reap(const char* context)
191 Context& ctx = writeContext(context);
192 SharedLock locker(m_lock, false);
193 ctx.reap(time(nullptr));
196 unsigned long MemoryStorageService::Context::reap(time_t exp)
198 // Garbage collect any expired entries.
199 unsigned long count=0;
200 map<string,Record>::iterator cur = m_dataMap.begin();
201 map<string,Record>::iterator stop = m_dataMap.end();
202 while (cur != stop) {
203 if (cur->second.expiration <= exp) {
204 map<string,Record>::iterator tmp = cur++;
205 m_dataMap.erase(tmp);
215 bool MemoryStorageService::createString(const char* context, const char* key, const char* value, time_t expiration)
217 Context& ctx = writeContext(context);
218 SharedLock locker(m_lock, false);
220 // Check for a duplicate.
221 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
222 if (i!=ctx.m_dataMap.end()) {
224 if (time(nullptr) < i->second.expiration)
226 // It's dead, so we can just remove it now and create the new record.
227 ctx.m_dataMap.erase(i);
230 ctx.m_dataMap[key]=Record(value,expiration);
232 m_log.debug("inserted record (%s) in context (%s) with expiration (%lu)", key, context, expiration);
236 int MemoryStorageService::readString(const char* context, const char* key, string* pvalue, time_t* pexpiration, int version)
238 Context& ctx = readContext(context);
239 SharedLock locker(m_lock, false);
241 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
242 if (i==ctx.m_dataMap.end())
244 else if (time(nullptr) >= i->second.expiration)
247 *pexpiration = i->second.expiration;
248 if (i->second.version == version)
249 return version; // nothing's changed, so just echo back the version
251 *pvalue = i->second.data;
252 return i->second.version;
255 int MemoryStorageService::updateString(const char* context, const char* key, const char* value, time_t expiration, int version)
257 Context& ctx = writeContext(context);
258 SharedLock locker(m_lock, false);
260 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
261 if (i==ctx.m_dataMap.end())
263 else if (time(nullptr) >= i->second.expiration)
266 if (version > 0 && version != i->second.version)
267 return -1; // caller's out of sync
270 i->second.data = value;
271 ++(i->second.version);
274 if (expiration && expiration != i->second.expiration)
275 i->second.expiration = expiration;
277 m_log.debug("updated record (%s) in context (%s) with expiration (%lu)", key, context, i->second.expiration);
278 return i->second.version;
281 bool MemoryStorageService::deleteString(const char* context, const char* key)
283 Context& ctx = writeContext(context);
284 SharedLock locker(m_lock, false);
287 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
288 if (i!=ctx.m_dataMap.end()) {
289 ctx.m_dataMap.erase(i);
290 m_log.debug("deleted record (%s) in context (%s)", key, context);
294 m_log.debug("deleting record (%s) in context (%s)....not found", key, context);
298 void MemoryStorageService::updateContext(const char* context, time_t expiration)
300 Context& ctx = writeContext(context);
301 SharedLock locker(m_lock, false);
303 time_t now = time(nullptr);
304 map<string,Record>::iterator stop=ctx.m_dataMap.end();
305 for (map<string,Record>::iterator i = ctx.m_dataMap.begin(); i!=stop; ++i) {
306 if (now < i->second.expiration)
307 i->second.expiration = expiration;
310 m_log.debug("updated expiration of valid records in context (%s) to (%lu)", context, expiration);