2 * Copyright 2001-2007 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
18 * MemoryStorageService.cpp
20 * In-memory "persistent" storage, suitable for simple applications.
26 #include "util/StorageService.h"
27 #include "util/Threads.h"
28 #include "util/XMLHelper.h"
30 #include <xercesc/util/XMLUniDefs.hpp>
32 using namespace xmltooling::logging;
33 using namespace xmltooling;
36 namespace xmltooling {
37 class XMLTOOL_DLLLOCAL MemoryStorageService : public StorageService
40 MemoryStorageService(const DOMElement* e);
41 virtual ~MemoryStorageService();
43 bool createString(const char* context, const char* key, const char* value, time_t expiration);
44 int readString(const char* context, const char* key, string* pvalue=NULL, time_t* pexpiration=NULL, int version=0);
45 int updateString(const char* context, const char* key, const char* value=NULL, time_t expiration=0, int version=0);
46 bool deleteString(const char* context, const char* key);
48 bool createText(const char* context, const char* key, const char* value, time_t expiration) {
49 return createString(context, key, value, expiration);
51 int readText(const char* context, const char* key, string* pvalue=NULL, time_t* pexpiration=NULL, int version=0) {
52 return readString(context, key, pvalue, pexpiration, version);
54 int updateText(const char* context, const char* key, const char* value=NULL, time_t expiration=0, int version=0) {
55 return updateString(context, key, value, expiration, version);
57 bool deleteText(const char* context, const char* key) {
58 return deleteString(context, key);
61 void reap(const char* context);
62 void updateContext(const char* context, time_t expiration);
63 void deleteContext(const char* context) {
65 m_contextMap.erase(context);
72 struct XMLTOOL_DLLLOCAL Record {
73 Record() : expiration(0), version(1) {}
74 Record(const string& s, time_t t) : data(s), expiration(t), version(1) {}
80 struct XMLTOOL_DLLLOCAL Context {
82 Context(const Context& src) {
83 m_dataMap = src.m_dataMap;
85 map<string,Record> m_dataMap;
86 unsigned long reap(time_t exp);
89 Context& readContext(const char* context) {
91 map<string,Context>::iterator i = m_contextMap.find(context);
92 if (i != m_contextMap.end())
96 return m_contextMap[context];
99 Context& writeContext(const char* context) {
101 return m_contextMap[context];
104 map<string,Context> m_contextMap;
106 CondWait* shutdown_wait;
107 Thread* cleanup_thread;
108 static void* cleanup_fn(void*);
110 int m_cleanupInterval;
114 StorageService* XMLTOOL_DLLLOCAL MemoryStorageServiceFactory(const DOMElement* const & e)
116 return new MemoryStorageService(e);
120 static const XMLCh cleanupInterval[] = UNICODE_LITERAL_15(c,l,e,a,n,u,p,I,n,t,e,r,v,a,l);
122 MemoryStorageService::MemoryStorageService(const DOMElement* e)
123 : m_lock(NULL), shutdown_wait(NULL), cleanup_thread(NULL), shutdown(false), m_cleanupInterval(0),
124 m_log(Category::getInstance(XMLTOOLING_LOGCAT".StorageService"))
126 const XMLCh* tag=e ? e->getAttributeNS(NULL,cleanupInterval) : NULL;
128 m_cleanupInterval = XMLString::parseInt(tag);
130 if (!m_cleanupInterval)
131 m_cleanupInterval=900;
133 m_lock = RWLock::create();
134 shutdown_wait = CondWait::create();
135 cleanup_thread = Thread::create(&cleanup_fn, (void*)this);
138 MemoryStorageService::~MemoryStorageService()
140 // Shut down the cleanup thread and let it know...
142 shutdown_wait->signal();
143 cleanup_thread->join(NULL);
145 delete shutdown_wait;
149 void* MemoryStorageService::cleanup_fn(void* cache_p)
151 MemoryStorageService* cache = reinterpret_cast<MemoryStorageService*>(cache_p);
154 // First, let's block all signals
155 Thread::mask_all_signals();
158 // Now run the cleanup process.
163 void MemoryStorageService::cleanup()
169 auto_ptr<Mutex> mutex(Mutex::create());
172 m_log.info("cleanup thread started...running every %d seconds", m_cleanupInterval);
175 shutdown_wait->timedwait(mutex.get(), m_cleanupInterval);
179 unsigned long count=0;
180 time_t now = time(NULL);
182 SharedLock locker(m_lock, false);
183 for (map<string,Context>::iterator i=m_contextMap.begin(); i!=m_contextMap.end(); ++i)
184 count += i->second.reap(now);
187 m_log.info("purged %d expired record(s) from storage", count);
190 m_log.info("cleanup thread finished");
196 void MemoryStorageService::reap(const char* context)
198 Context& ctx = writeContext(context);
199 SharedLock locker(m_lock, false);
200 ctx.reap(time(NULL));
203 unsigned long MemoryStorageService::Context::reap(time_t exp)
205 // Garbage collect any expired entries.
206 unsigned long count=0;
207 map<string,Record>::iterator cur = m_dataMap.begin();
208 map<string,Record>::iterator stop = m_dataMap.end();
209 while (cur != stop) {
210 if (cur->second.expiration <= exp) {
211 map<string,Record>::iterator tmp = cur++;
212 m_dataMap.erase(tmp);
222 bool MemoryStorageService::createString(const char* context, const char* key, const char* value, time_t expiration)
224 Context& ctx = writeContext(context);
225 SharedLock locker(m_lock, false);
227 // Check for a duplicate.
228 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
229 if (i!=ctx.m_dataMap.end()) {
231 if (time(NULL) < i->second.expiration)
233 // It's dead, so we can just remove it now and create the new record.
234 ctx.m_dataMap.erase(i);
237 ctx.m_dataMap[key]=Record(value,expiration);
239 m_log.debug("inserted record (%s) in context (%s)", key, context);
243 int MemoryStorageService::readString(const char* context, const char* key, string* pvalue, time_t* pexpiration, int version)
245 Context& ctx = readContext(context);
246 SharedLock locker(m_lock, false);
248 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
249 if (i==ctx.m_dataMap.end())
251 else if (time(NULL) >= i->second.expiration)
254 *pexpiration = i->second.expiration;
255 if (i->second.version == version)
256 return version; // nothing's changed, so just echo back the version
258 *pvalue = i->second.data;
259 return i->second.version;
262 int MemoryStorageService::updateString(const char* context, const char* key, const char* value, time_t expiration, int version)
264 Context& ctx = writeContext(context);
265 SharedLock locker(m_lock, false);
267 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
268 if (i==ctx.m_dataMap.end())
270 else if (time(NULL) >= i->second.expiration)
273 if (version > 0 && version != i->second.version)
274 return -1; // caller's out of sync
277 i->second.data = value;
278 ++(i->second.version);
281 if (expiration && expiration != i->second.expiration)
282 i->second.expiration = expiration;
284 m_log.debug("updated record (%s) in context (%s)", key, context);
285 return i->second.version;
288 bool MemoryStorageService::deleteString(const char* context, const char* key)
290 Context& ctx = writeContext(context);
291 SharedLock locker(m_lock, false);
294 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
295 if (i!=ctx.m_dataMap.end()) {
296 ctx.m_dataMap.erase(i);
297 m_log.debug("deleted record (%s) in context (%s)", key, context);
301 m_log.debug("deleting record (%s) in context (%s)....not found", key, context);
305 void MemoryStorageService::updateContext(const char* context, time_t expiration)
307 Context& ctx = writeContext(context);
308 SharedLock locker(m_lock, false);
310 time_t now = time(NULL);
311 map<string,Record>::iterator stop=ctx.m_dataMap.end();
312 for (map<string,Record>::iterator i = ctx.m_dataMap.begin(); i!=stop; ++i) {
313 if (now < i->second.expiration)
314 i->second.expiration = expiration;
317 m_log.debug("updated expiration of valid records in context (%s)", context);