2 * Copyright 2001-2010 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"
31 #include <xercesc/util/XMLUniDefs.hpp>
33 using namespace xmltooling::logging;
34 using namespace xmltooling;
37 using xercesc::DOMElement;
39 namespace xmltooling {
40 class XMLTOOL_DLLLOCAL MemoryStorageService : public StorageService
43 MemoryStorageService(const DOMElement* e);
44 virtual ~MemoryStorageService();
46 bool createString(const char* context, const char* key, const char* value, time_t expiration);
47 int readString(const char* context, const char* key, string* pvalue=nullptr, time_t* pexpiration=nullptr, int version=0);
48 int updateString(const char* context, const char* key, const char* value=nullptr, time_t expiration=0, int version=0);
49 bool deleteString(const char* context, const char* key);
51 bool createText(const char* context, const char* key, const char* value, time_t expiration) {
52 return createString(context, key, value, expiration);
54 int readText(const char* context, const char* key, string* pvalue=nullptr, time_t* pexpiration=nullptr, int version=0) {
55 return readString(context, key, pvalue, pexpiration, version);
57 int updateText(const char* context, const char* key, const char* value=nullptr, time_t expiration=0, int version=0) {
58 return updateString(context, key, value, expiration, version);
60 bool deleteText(const char* context, const char* key) {
61 return deleteString(context, key);
64 void reap(const char* context);
65 void updateContext(const char* context, time_t expiration);
66 void deleteContext(const char* context) {
68 m_contextMap.erase(context);
73 struct XMLTOOL_DLLLOCAL Record {
74 Record() : expiration(0), version(1) {}
75 Record(const string& s, time_t t) : data(s), expiration(t), version(1) {}
81 struct XMLTOOL_DLLLOCAL Context {
83 Context(const Context& src) {
84 m_dataMap = src.m_dataMap;
86 map<string,Record> m_dataMap;
87 unsigned long reap(time_t exp);
90 Context& readContext(const char* context) {
92 map<string,Context>::iterator i = m_contextMap.find(context);
93 if (i != m_contextMap.end())
97 return m_contextMap[context];
100 Context& writeContext(const char* context) {
102 return m_contextMap[context];
105 map<string,Context> m_contextMap;
107 CondWait* shutdown_wait;
108 Thread* cleanup_thread;
109 static void* cleanup_fn(void*);
111 int m_cleanupInterval;
115 StorageService* XMLTOOL_DLLLOCAL MemoryStorageServiceFactory(const DOMElement* const & e)
117 return new MemoryStorageService(e);
121 static const XMLCh cleanupInterval[] = UNICODE_LITERAL_15(c,l,e,a,n,u,p,I,n,t,e,r,v,a,l);
123 MemoryStorageService::MemoryStorageService(const DOMElement* e)
124 : m_lock(nullptr), shutdown_wait(nullptr), cleanup_thread(nullptr), shutdown(false), m_cleanupInterval(0),
125 m_log(Category::getInstance(XMLTOOLING_LOGCAT".StorageService"))
127 const XMLCh* tag=e ? e->getAttributeNS(nullptr,cleanupInterval) : nullptr;
129 m_cleanupInterval = XMLString::parseInt(tag);
131 if (!m_cleanupInterval)
132 m_cleanupInterval=900;
134 m_lock = RWLock::create();
135 shutdown_wait = CondWait::create();
136 cleanup_thread = Thread::create(&cleanup_fn, (void*)this);
139 MemoryStorageService::~MemoryStorageService()
141 // Shut down the cleanup thread and let it know...
143 shutdown_wait->signal();
144 cleanup_thread->join(nullptr);
146 delete cleanup_thread;
147 delete shutdown_wait;
151 void* MemoryStorageService::cleanup_fn(void* pv)
153 MemoryStorageService* cache = reinterpret_cast<MemoryStorageService*>(pv);
156 // First, let's block all signals
157 Thread::mask_all_signals();
164 auto_ptr<Mutex> mutex(Mutex::create());
167 cache->m_log.info("cleanup thread started...running every %d seconds", cache->m_cleanupInterval);
169 while (!cache->shutdown) {
170 cache->shutdown_wait->timedwait(mutex.get(), cache->m_cleanupInterval);
174 unsigned long count=0;
175 time_t now = time(nullptr);
176 cache->m_lock->wrlock();
177 SharedLock locker(cache->m_lock, false);
178 for (map<string,Context>::iterator i=cache->m_contextMap.begin(); i!=cache->m_contextMap.end(); ++i)
179 count += i->second.reap(now);
182 cache->m_log.info("purged %d expired record(s) from storage", count);
185 cache->m_log.info("cleanup thread finished");
191 void MemoryStorageService::reap(const char* context)
193 Context& ctx = writeContext(context);
194 SharedLock locker(m_lock, false);
195 ctx.reap(time(nullptr));
198 unsigned long MemoryStorageService::Context::reap(time_t exp)
200 // Garbage collect any expired entries.
201 unsigned long count=0;
202 map<string,Record>::iterator cur = m_dataMap.begin();
203 map<string,Record>::iterator stop = m_dataMap.end();
204 while (cur != stop) {
205 if (cur->second.expiration <= exp) {
206 map<string,Record>::iterator tmp = cur++;
207 m_dataMap.erase(tmp);
217 bool MemoryStorageService::createString(const char* context, const char* key, const char* value, time_t expiration)
219 Context& ctx = writeContext(context);
220 SharedLock locker(m_lock, false);
222 // Check for a duplicate.
223 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
224 if (i!=ctx.m_dataMap.end()) {
226 if (time(nullptr) < i->second.expiration)
228 // It's dead, so we can just remove it now and create the new record.
229 ctx.m_dataMap.erase(i);
232 ctx.m_dataMap[key]=Record(value,expiration);
234 m_log.debug("inserted record (%s) in context (%s)", key, context);
238 int MemoryStorageService::readString(const char* context, const char* key, string* pvalue, time_t* pexpiration, int version)
240 Context& ctx = readContext(context);
241 SharedLock locker(m_lock, false);
243 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
244 if (i==ctx.m_dataMap.end())
246 else if (time(nullptr) >= i->second.expiration)
249 *pexpiration = i->second.expiration;
250 if (i->second.version == version)
251 return version; // nothing's changed, so just echo back the version
253 *pvalue = i->second.data;
254 return i->second.version;
257 int MemoryStorageService::updateString(const char* context, const char* key, const char* value, time_t expiration, int version)
259 Context& ctx = writeContext(context);
260 SharedLock locker(m_lock, false);
262 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
263 if (i==ctx.m_dataMap.end())
265 else if (time(nullptr) >= i->second.expiration)
268 if (version > 0 && version != i->second.version)
269 return -1; // caller's out of sync
272 i->second.data = value;
273 ++(i->second.version);
276 if (expiration && expiration != i->second.expiration)
277 i->second.expiration = expiration;
279 m_log.debug("updated record (%s) in context (%s)", key, context);
280 return i->second.version;
283 bool MemoryStorageService::deleteString(const char* context, const char* key)
285 Context& ctx = writeContext(context);
286 SharedLock locker(m_lock, false);
289 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
290 if (i!=ctx.m_dataMap.end()) {
291 ctx.m_dataMap.erase(i);
292 m_log.debug("deleted record (%s) in context (%s)", key, context);
296 m_log.debug("deleting record (%s) in context (%s)....not found", key, context);
300 void MemoryStorageService::updateContext(const char* context, time_t expiration)
302 Context& ctx = writeContext(context);
303 SharedLock locker(m_lock, false);
305 time_t now = time(nullptr);
306 map<string,Record>::iterator stop=ctx.m_dataMap.end();
307 for (map<string,Record>::iterator i = ctx.m_dataMap.begin(); i!=stop; ++i) {
308 if (now < i->second.expiration)
309 i->second.expiration = expiration;
312 m_log.debug("updated expiration of valid records in context (%s)", context);