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;
44 // Reasonably extended sizes to avoid callers needing to shrink unduly.
45 static const XMLTOOL_DLLLOCAL StorageService::Capabilities g_memCaps(0x4000, 0x4000, 0x4000);
48 namespace xmltooling {
49 class XMLTOOL_DLLLOCAL MemoryStorageService : public StorageService
52 MemoryStorageService(const DOMElement* e);
53 virtual ~MemoryStorageService();
55 const Capabilities& getCapabilities() const {
59 bool createString(const char* context, const char* key, const char* value, time_t expiration);
60 int readString(const char* context, const char* key, string* pvalue=nullptr, time_t* pexpiration=nullptr, int version=0);
61 int updateString(const char* context, const char* key, const char* value=nullptr, time_t expiration=0, int version=0);
62 bool deleteString(const char* context, const char* key);
64 bool createText(const char* context, const char* key, const char* value, time_t expiration) {
65 return createString(context, key, value, expiration);
67 int readText(const char* context, const char* key, string* pvalue=nullptr, time_t* pexpiration=nullptr, int version=0) {
68 return readString(context, key, pvalue, pexpiration, version);
70 int updateText(const char* context, const char* key, const char* value=nullptr, time_t expiration=0, int version=0) {
71 return updateString(context, key, value, expiration, version);
73 bool deleteText(const char* context, const char* key) {
74 return deleteString(context, key);
77 void reap(const char* context);
78 void updateContext(const char* context, time_t expiration);
79 void deleteContext(const char* context) {
81 m_contextMap.erase(context);
86 struct XMLTOOL_DLLLOCAL Record {
87 Record() : expiration(0), version(1) {}
88 Record(const string& s, time_t t) : data(s), expiration(t), version(1) {}
94 struct XMLTOOL_DLLLOCAL Context {
96 Context(const Context& src) {
97 m_dataMap = src.m_dataMap;
99 map<string,Record> m_dataMap;
100 unsigned long reap(time_t exp);
103 Context& readContext(const char* context) {
105 map<string,Context>::iterator i = m_contextMap.find(context);
106 if (i != m_contextMap.end())
110 return m_contextMap[context];
113 Context& writeContext(const char* context) {
115 return m_contextMap[context];
118 map<string,Context> m_contextMap;
120 CondWait* shutdown_wait;
121 Thread* cleanup_thread;
122 static void* cleanup_fn(void*);
124 int m_cleanupInterval;
128 StorageService* XMLTOOL_DLLLOCAL MemoryStorageServiceFactory(const DOMElement* const & e)
130 return new MemoryStorageService(e);
134 static const XMLCh cleanupInterval[] = UNICODE_LITERAL_15(c,l,e,a,n,u,p,I,n,t,e,r,v,a,l);
136 MemoryStorageService::MemoryStorageService(const DOMElement* e)
137 : m_lock(nullptr), shutdown_wait(nullptr), cleanup_thread(nullptr), shutdown(false),
138 m_cleanupInterval(XMLHelper::getAttrInt(e, 900, cleanupInterval)),
139 m_log(Category::getInstance(XMLTOOLING_LOGCAT".StorageService"))
141 m_lock = RWLock::create();
142 shutdown_wait = CondWait::create();
143 cleanup_thread = Thread::create(&cleanup_fn, (void*)this);
146 MemoryStorageService::~MemoryStorageService()
148 // Shut down the cleanup thread and let it know...
150 shutdown_wait->signal();
151 cleanup_thread->join(nullptr);
153 delete cleanup_thread;
154 delete shutdown_wait;
158 void* MemoryStorageService::cleanup_fn(void* pv)
160 MemoryStorageService* cache = reinterpret_cast<MemoryStorageService*>(pv);
163 // First, let's block all signals
164 Thread::mask_all_signals();
171 auto_ptr<Mutex> mutex(Mutex::create());
174 cache->m_log.info("cleanup thread started...running every %d seconds", cache->m_cleanupInterval);
176 while (!cache->shutdown) {
177 cache->shutdown_wait->timedwait(mutex.get(), cache->m_cleanupInterval);
181 unsigned long count=0;
182 time_t now = time(nullptr);
183 cache->m_lock->wrlock();
184 SharedLock locker(cache->m_lock, false);
185 for (map<string,Context>::iterator i=cache->m_contextMap.begin(); i!=cache->m_contextMap.end(); ++i)
186 count += i->second.reap(now);
189 cache->m_log.info("purged %d expired record(s) from storage", count);
192 cache->m_log.info("cleanup thread finished");
198 void MemoryStorageService::reap(const char* context)
200 Context& ctx = writeContext(context);
201 SharedLock locker(m_lock, false);
202 ctx.reap(time(nullptr));
205 unsigned long MemoryStorageService::Context::reap(time_t exp)
207 // Garbage collect any expired entries.
208 unsigned long count=0;
209 map<string,Record>::iterator cur = m_dataMap.begin();
210 map<string,Record>::iterator stop = m_dataMap.end();
211 while (cur != stop) {
212 if (cur->second.expiration <= exp) {
213 map<string,Record>::iterator tmp = cur++;
214 m_dataMap.erase(tmp);
224 bool MemoryStorageService::createString(const char* context, const char* key, const char* value, time_t expiration)
226 Context& ctx = writeContext(context);
227 SharedLock locker(m_lock, false);
229 // Check for a duplicate.
230 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
231 if (i!=ctx.m_dataMap.end()) {
233 if (time(nullptr) < i->second.expiration)
235 // It's dead, so we can just remove it now and create the new record.
236 ctx.m_dataMap.erase(i);
239 ctx.m_dataMap[key]=Record(value,expiration);
241 m_log.debug("inserted record (%s) in context (%s) with expiration (%lu)", key, context, expiration);
245 int MemoryStorageService::readString(const char* context, const char* key, string* pvalue, time_t* pexpiration, int version)
247 Context& ctx = readContext(context);
248 SharedLock locker(m_lock, false);
250 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
251 if (i==ctx.m_dataMap.end())
253 else if (time(nullptr) >= i->second.expiration)
256 *pexpiration = i->second.expiration;
257 if (i->second.version == version)
258 return version; // nothing's changed, so just echo back the version
260 *pvalue = i->second.data;
261 return i->second.version;
264 int MemoryStorageService::updateString(const char* context, const char* key, const char* value, time_t expiration, int version)
266 Context& ctx = writeContext(context);
267 SharedLock locker(m_lock, false);
269 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
270 if (i==ctx.m_dataMap.end())
272 else if (time(nullptr) >= i->second.expiration)
275 if (version > 0 && version != i->second.version)
276 return -1; // caller's out of sync
279 i->second.data = value;
280 ++(i->second.version);
283 if (expiration && expiration != i->second.expiration)
284 i->second.expiration = expiration;
286 m_log.debug("updated record (%s) in context (%s) with expiration (%lu)", key, context, i->second.expiration);
287 return i->second.version;
290 bool MemoryStorageService::deleteString(const char* context, const char* key)
292 Context& ctx = writeContext(context);
293 SharedLock locker(m_lock, false);
296 map<string,Record>::iterator i=ctx.m_dataMap.find(key);
297 if (i!=ctx.m_dataMap.end()) {
298 ctx.m_dataMap.erase(i);
299 m_log.debug("deleted record (%s) in context (%s)", key, context);
303 m_log.debug("deleting record (%s) in context (%s)....not found", key, context);
307 void MemoryStorageService::updateContext(const char* context, time_t expiration)
309 Context& ctx = writeContext(context);
310 SharedLock locker(m_lock, false);
312 time_t now = time(nullptr);
313 map<string,Record>::iterator stop=ctx.m_dataMap.end();
314 for (map<string,Record>::iterator i = ctx.m_dataMap.begin(); i!=stop; ++i) {
315 if (now < i->second.expiration)
316 i->second.expiration = expiration;
319 m_log.debug("updated expiration of valid records in context (%s) to (%lu)", context, expiration);