2 * Copyright 2001-2005 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 bool readString(const char* context, const char* key, string* pvalue=NULL, time_t* pexpiration=NULL);
45 bool updateString(const char* context, const char* key, const char* value=NULL, time_t expiration=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 bool readText(const char* context, const char* key, string* pvalue=NULL, time_t* pexpiration=NULL) {
52 return readString(context, key, pvalue, pexpiration);
54 bool updateText(const char* context, const char* key, const char* value=NULL, time_t expiration=0) {
55 return updateString(context, key, value, expiration);
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) {}
72 Record(string s, time_t t) : data(s), expiration(t) {}
77 struct XMLTOOL_DLLLOCAL Context {
78 Context() : m_lock(RWLock::create()) {}
79 Context(const Context& src) {
80 m_dataMap = src.m_dataMap;
81 m_expMap = src.m_expMap;
82 m_lock = RWLock::create();
84 ~Context() { delete m_lock; }
85 map<string,Record> m_dataMap;
86 multimap<time_t,string> m_expMap;
91 Context& getContext(const char* context) {
92 Lock wrapper(contextLock);
93 return m_contextMap[context];
96 map<string,Context> m_contextMap;
98 CondWait* shutdown_wait;
99 Thread* cleanup_thread;
100 static void* cleanup_fn(void*);
102 int m_cleanupInterval;
106 StorageService* XMLTOOL_DLLLOCAL MemoryStorageServiceFactory(const DOMElement* const & e)
108 return new MemoryStorageService(e);
113 static const XMLCh cleanupInterval[] = UNICODE_LITERAL_15(c,l,e,a,n,u,p,I,n,t,e,r,v,a,l);
115 MemoryStorageService::MemoryStorageService(const DOMElement* e)
116 : contextLock(NULL), shutdown_wait(NULL), cleanup_thread(NULL), shutdown(false), m_cleanupInterval(0),
117 m_log(Category::getInstance(XMLTOOLING_LOGCAT".StorageService"))
119 const XMLCh* tag=e ? e->getAttributeNS(NULL,cleanupInterval) : NULL;
121 m_cleanupInterval = XMLString::parseInt(tag);
123 if (!m_cleanupInterval)
124 m_cleanupInterval=300;
126 contextLock = Mutex::create();
127 shutdown_wait = CondWait::create();
128 cleanup_thread = Thread::create(&cleanup_fn, (void*)this);
131 MemoryStorageService::~MemoryStorageService()
133 // Shut down the cleanup thread and let it know...
135 shutdown_wait->signal();
136 cleanup_thread->join(NULL);
138 delete shutdown_wait;
142 void* MemoryStorageService::cleanup_fn(void* cache_p)
144 MemoryStorageService* cache = reinterpret_cast<MemoryStorageService*>(cache_p);
147 // First, let's block all signals
148 Thread::mask_all_signals();
151 // Now run the cleanup process.
156 void MemoryStorageService::cleanup()
163 Mutex* mutex = Mutex::create();
166 m_log.info("cleanup thread started...running every %d seconds", m_cleanupInterval);
169 shutdown_wait->timedwait(mutex, 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");
189 void MemoryStorageService::reap(const char* context)
191 getContext(context).reap();
194 unsigned long MemoryStorageService::Context::reap()
196 // Lock the "database".
198 SharedLock wrapper(m_lock, false);
200 // Garbage collect any expired entries.
201 unsigned long count=0;
202 multimap<time_t,string>::iterator stop=m_expMap.upper_bound(time(NULL));
203 for (multimap<time_t,string>::iterator i=m_expMap.begin(); i!=stop; m_expMap.erase(i++)) {
204 m_dataMap.erase(i->second);
211 void MemoryStorageService::createString(const char* context, const char* key, const char* value, time_t expiration)
213 Context& ctx = getContext(context);
216 ctx.m_lock->wrlock();
217 SharedLock wrapper(ctx.m_lock, false);
219 // Check for a duplicate.
220 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
221 if (i!=ctx.m_dataMap.end()) {
223 if (time(NULL) < i->second.expiration)
224 throw IOException("attempted to insert a record with duplicate key ($1)", params(1,key));
225 // It's dead, so we can just remove it now and create the new record.
226 // Now find the reversed index of expiration to key, so we can clear it.
227 pair<multimap<time_t,string>::iterator,multimap<time_t,string>::iterator> range =
228 ctx.m_expMap.equal_range(i->second.expiration);
229 for (; range.first != range.second; ++range.first) {
230 if (range.first->second == i->first) {
231 ctx.m_expMap.erase(range.first);
235 // And finally delete the record itself.
236 ctx.m_dataMap.erase(i);
239 ctx.m_dataMap[key]=Record(value,expiration);
240 ctx.m_expMap.insert(multimap<time_t,string>::value_type(expiration,key));
242 m_log.debug("inserted record (%s) in context (%s)", key, context);
245 bool MemoryStorageService::readString(const char* context, const char* key, string* pvalue, time_t* pexpiration)
247 Context& ctx = getContext(context);
249 SharedLock wrapper(ctx.m_lock);
250 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
251 if (i==ctx.m_dataMap.end())
253 else if (time(NULL) >= i->second.expiration)
256 *pvalue = i->second.data;
258 *pexpiration = i->second.expiration;
262 bool MemoryStorageService::updateString(const char* context, const char* key, const char* value, time_t expiration)
264 Context& ctx = getContext(context);
267 ctx.m_lock->wrlock();
268 SharedLock wrapper(ctx.m_lock, false);
270 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
271 if (i==ctx.m_dataMap.end())
273 else if (time(NULL) >= i->second.expiration)
277 i->second.data = value;
279 if (expiration && expiration != i->second.expiration) {
280 // Update secondary map.
281 pair<multimap<time_t,string>::iterator,multimap<time_t,string>::iterator> range =
282 ctx.m_expMap.equal_range(i->second.expiration);
283 for (; range.first != range.second; ++range.first) {
284 if (range.first->second == i->first) {
285 ctx.m_expMap.erase(range.first);
289 i->second.expiration = expiration;
290 ctx.m_expMap.insert(multimap<time_t,string>::value_type(expiration,key));
293 m_log.debug("updated record (%s) in context (%s)", key, context);
297 bool MemoryStorageService::deleteString(const char* context, const char* key)
299 Context& ctx = getContext(context);
302 ctx.m_lock->wrlock();
303 SharedLock wrapper(ctx.m_lock, false);
306 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
307 if (i!=ctx.m_dataMap.end()) {
308 // Now find the reversed index of expiration to key, so we can clear it.
309 pair<multimap<time_t,string>::iterator,multimap<time_t,string>::iterator> range =
310 ctx.m_expMap.equal_range(i->second.expiration);
311 for (; range.first != range.second; ++range.first) {
312 if (range.first->second == i->first) {
313 ctx.m_expMap.erase(range.first);
317 // And finally delete the record itself.
318 ctx.m_dataMap.erase(i);
319 m_log.debug("deleted record (%s) in context (%s)", key, context);
323 m_log.debug("deleting record (%s) in context (%s)....not found", key, context);