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.
25 #include "util/StorageService.h"
26 #include "util/Threads.h"
27 #include "util/XMLHelper.h"
29 #include <log4cpp/Category.hh>
30 #include <xercesc/util/XMLUniDefs.hpp>
32 using namespace xmltooling;
33 using namespace log4cpp;
36 namespace xmltooling {
37 class XMLTOOL_DLLLOCAL MemoryStorageService : public StorageService
40 MemoryStorageService(const DOMElement* e);
41 virtual ~MemoryStorageService();
43 void 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 void 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 deleteContext(const char* context) {
63 Lock wrapper(contextLock);
64 m_contextMap.erase(context);
70 struct XMLTOOL_DLLLOCAL Record {
71 Record() : expiration(0), version(1) {}
72 Record(const string& s, time_t t) : data(s), expiration(t), version(1) {}
78 struct XMLTOOL_DLLLOCAL Context {
79 Context() : m_lock(RWLock::create()) {}
80 Context(const Context& src) {
81 m_dataMap = src.m_dataMap;
82 m_expMap = src.m_expMap;
83 m_lock = RWLock::create();
85 ~Context() { delete m_lock; }
86 map<string,Record> m_dataMap;
87 multimap<time_t,string> m_expMap;
92 Context& getContext(const char* context) {
93 Lock wrapper(contextLock);
94 return m_contextMap[context];
97 map<string,Context> m_contextMap;
99 CondWait* shutdown_wait;
100 Thread* cleanup_thread;
101 static void* cleanup_fn(void*);
103 int m_cleanupInterval;
107 StorageService* XMLTOOL_DLLLOCAL MemoryStorageServiceFactory(const DOMElement* const & e)
109 return new MemoryStorageService(e);
114 static const XMLCh cleanupInterval[] = UNICODE_LITERAL_15(c,l,e,a,n,u,p,I,n,t,e,r,v,a,l);
116 MemoryStorageService::MemoryStorageService(const DOMElement* e)
117 : contextLock(NULL), shutdown_wait(NULL), cleanup_thread(NULL), shutdown(false), m_cleanupInterval(0),
118 m_log(Category::getInstance(XMLTOOLING_LOGCAT".StorageService"))
120 const XMLCh* tag=e ? e->getAttributeNS(NULL,cleanupInterval) : NULL;
122 m_cleanupInterval = XMLString::parseInt(tag);
124 if (!m_cleanupInterval)
125 m_cleanupInterval=300;
127 contextLock = Mutex::create();
128 shutdown_wait = CondWait::create();
129 cleanup_thread = Thread::create(&cleanup_fn, (void*)this);
132 MemoryStorageService::~MemoryStorageService()
134 // Shut down the cleanup thread and let it know...
136 shutdown_wait->signal();
137 cleanup_thread->join(NULL);
139 delete shutdown_wait;
143 void* MemoryStorageService::cleanup_fn(void* cache_p)
145 MemoryStorageService* cache = reinterpret_cast<MemoryStorageService*>(cache_p);
148 // First, let's block all signals
149 Thread::mask_all_signals();
152 // Now run the cleanup process.
157 void MemoryStorageService::cleanup()
163 auto_ptr<Mutex> mutex(Mutex::create());
166 m_log.info("cleanup thread started...running every %d seconds", m_cleanupInterval);
169 shutdown_wait->timedwait(mutex.get(), m_cleanupInterval);
173 unsigned long count=0;
174 Lock wrapper(contextLock);
175 for (map<string,Context>::iterator i=m_contextMap.begin(); i!=m_contextMap.end(); ++i)
176 count += i->second.reap();
179 m_log.info("purged %d record(s) from storage", count);
182 m_log.info("cleanup thread finished");
188 void MemoryStorageService::reap(const char* context)
190 getContext(context).reap();
193 unsigned long MemoryStorageService::Context::reap()
195 // Lock the "database".
197 SharedLock wrapper(m_lock, false);
199 // Garbage collect any expired entries.
200 unsigned long count=0;
201 multimap<time_t,string>::iterator stop=m_expMap.upper_bound(time(NULL));
202 for (multimap<time_t,string>::iterator i=m_expMap.begin(); i!=stop; m_expMap.erase(i++)) {
203 m_dataMap.erase(i->second);
210 void MemoryStorageService::createString(const char* context, const char* key, const char* value, time_t expiration)
212 Context& ctx = getContext(context);
215 ctx.m_lock->wrlock();
216 SharedLock wrapper(ctx.m_lock, false);
218 // Check for a duplicate.
219 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
220 if (i!=ctx.m_dataMap.end()) {
222 if (time(NULL) < i->second.expiration)
223 throw IOException("attempted to insert a record with duplicate key ($1)", params(1,key));
224 // It's dead, so we can just remove it now and create the new record.
225 // Now find the reversed index of expiration to key, so we can clear it.
226 pair<multimap<time_t,string>::iterator,multimap<time_t,string>::iterator> range =
227 ctx.m_expMap.equal_range(i->second.expiration);
228 for (; range.first != range.second; ++range.first) {
229 if (range.first->second == i->first) {
230 ctx.m_expMap.erase(range.first);
234 // And finally delete the record itself.
235 ctx.m_dataMap.erase(i);
238 ctx.m_dataMap[key]=Record(value,expiration);
239 ctx.m_expMap.insert(multimap<time_t,string>::value_type(expiration,key));
241 m_log.debug("inserted record (%s) in context (%s)", key, context);
244 int MemoryStorageService::readString(const char* context, const char* key, string* pvalue, time_t* pexpiration, int version)
246 Context& ctx = getContext(context);
248 SharedLock wrapper(ctx.m_lock);
249 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
250 if (i==ctx.m_dataMap.end())
252 else if (time(NULL) >= i->second.expiration)
254 if (i->second.version == version)
255 return version; // nothing's changed, so just echo back the version
257 *pvalue = i->second.data;
259 *pexpiration = i->second.expiration;
260 return i->second.version;
263 int MemoryStorageService::updateString(const char* context, const char* key, const char* value, time_t expiration, int version)
265 Context& ctx = getContext(context);
268 ctx.m_lock->wrlock();
269 SharedLock wrapper(ctx.m_lock, false);
271 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
272 if (i==ctx.m_dataMap.end())
274 else if (time(NULL) >= i->second.expiration)
277 if (version > 0 && version != i->second.version)
278 return -1; // caller's out of sync
281 i->second.data = value;
282 ++(i->second.version);
285 if (expiration && expiration != i->second.expiration) {
286 // Update secondary map.
287 pair<multimap<time_t,string>::iterator,multimap<time_t,string>::iterator> range =
288 ctx.m_expMap.equal_range(i->second.expiration);
289 for (; range.first != range.second; ++range.first) {
290 if (range.first->second == i->first) {
291 ctx.m_expMap.erase(range.first);
295 i->second.expiration = expiration;
296 ctx.m_expMap.insert(multimap<time_t,string>::value_type(expiration,key));
299 m_log.debug("updated record (%s) in context (%s)", key, context);
300 return i->second.version;
303 bool MemoryStorageService::deleteString(const char* context, const char* key)
305 Context& ctx = getContext(context);
308 ctx.m_lock->wrlock();
309 SharedLock wrapper(ctx.m_lock, false);
312 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
313 if (i!=ctx.m_dataMap.end()) {
314 // Now find the reversed index of expiration to key, so we can clear it.
315 pair<multimap<time_t,string>::iterator,multimap<time_t,string>::iterator> range =
316 ctx.m_expMap.equal_range(i->second.expiration);
317 for (; range.first != range.second; ++range.first) {
318 if (range.first->second == i->first) {
319 ctx.m_expMap.erase(range.first);
323 // And finally delete the record itself.
324 ctx.m_dataMap.erase(i);
325 m_log.debug("deleted record (%s) in context (%s)", key, context);
329 m_log.debug("deleting record (%s) in context (%s)....not found", key, context);