2 * Copyright 2001-2005 Internet2
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
18 * MemoryStorageService.cpp
\r
20 * In-memory "persistent" storage, suitable for simple applications.
\r
23 #include "internal.h"
\r
24 #include "util/NDC.h"
\r
25 #include "util/StorageService.h"
\r
26 #include "util/Threads.h"
\r
27 #include "util/XMLHelper.h"
\r
29 #include <log4cpp/Category.hh>
\r
30 #include <xercesc/util/XMLUniDefs.hpp>
\r
32 using namespace xmltooling;
\r
33 using namespace log4cpp;
\r
34 using namespace std;
\r
36 namespace xmltooling {
\r
37 class XMLTOOL_DLLLOCAL MemoryStorageService : public StorageService
\r
40 MemoryStorageService(const DOMElement* e);
\r
41 virtual ~MemoryStorageService();
\r
43 StorageHandle* createHandle();
\r
45 void createString(StorageHandle* handle, const char* key, const char* value, time_t expiration);
\r
46 bool readString(StorageHandle* handle, const char* key, string& value, time_t modifiedSince=0);
\r
47 bool updateString(StorageHandle* handle, const char* key, const char* value=NULL, time_t expiration=0);
\r
48 bool deleteString(StorageHandle* handle, const char* key);
\r
50 void createText(StorageHandle* handle, const char* key, const char* value, time_t expiration) {
\r
51 return createString(handle, key, value, expiration);
\r
53 bool readText(StorageHandle* handle, const char* key, string& value, time_t modifiedSince=0) {
\r
54 return readString(handle, key, value, modifiedSince);
\r
56 bool updateText(StorageHandle* handle, const char* key, const char* value=NULL, time_t expiration=0) {
\r
57 return updateString(handle, key, value, expiration);
\r
59 bool deleteText(StorageHandle* handle, const char* key) {
\r
60 return deleteString(handle, key);
\r
63 void reap(StorageHandle* handle);
\r
65 void removeHandle(StorageHandle* handle);
\r
70 struct XMLTOOL_DLLLOCAL Record {
\r
71 Record() : modified(0), expiration(0) {}
\r
72 Record(string s, time_t t1, time_t t2) : data(s), modified(t1), expiration(t2) {}
\r
74 time_t modified, expiration;
\r
77 struct XMLTOOL_DLLLOCAL MemoryHandle : public StorageHandle {
\r
78 MemoryHandle(StorageService* storage) : StorageHandle(storage), m_lock(RWLock::create()) {}
\r
79 virtual ~MemoryHandle() {
\r
81 static_cast<MemoryStorageService*>(m_storage)->removeHandle(this);
\r
83 map<string,Record> m_dataMap;
\r
84 multimap<time_t,string> m_expMap;
\r
86 unsigned long reap();
\r
89 vector<MemoryHandle*> m_handles;
\r
91 CondWait* shutdown_wait;
\r
92 Thread* cleanup_thread;
\r
93 static void* cleanup_fn(void*);
\r
95 int m_cleanupInterval;
\r
99 StorageService* XMLTOOL_DLLLOCAL MemoryStorageServiceFactory(const DOMElement* const & e)
\r
101 return new MemoryStorageService(e);
\r
106 static const XMLCh cleanupInterval[] = UNICODE_LITERAL_15(c,l,e,a,n,u,p,I,n,t,e,r,v,a,l);
\r
108 MemoryStorageService::MemoryStorageService(const DOMElement* e)
\r
109 : mutex(NULL), shutdown_wait(NULL), cleanup_thread(NULL), shutdown(false), m_cleanupInterval(0),
\r
110 m_log(Category::getInstance(XMLTOOLING_LOGCAT".StorageService"))
\r
112 mutex = Mutex::create();
\r
113 shutdown_wait = CondWait::create();
\r
114 cleanup_thread = Thread::create(&cleanup_fn, (void*)this);
\r
116 const XMLCh* tag=e ? e->getAttributeNS(NULL,cleanupInterval) : NULL;
\r
118 m_cleanupInterval = XMLString::parseInt(tag);
\r
120 if (!m_cleanupInterval)
\r
121 m_cleanupInterval=300;
\r
124 MemoryStorageService::~MemoryStorageService()
\r
126 // Shut down the cleanup thread and let it know...
\r
128 shutdown_wait->signal();
\r
129 cleanup_thread->join(NULL);
\r
131 delete shutdown_wait;
\r
133 for_each(m_handles.begin(), m_handles.end(), xmltooling::cleanup<MemoryHandle>());
\r
136 StorageService::StorageHandle* MemoryStorageService::createHandle()
\r
138 Lock wrapper(mutex);
\r
139 MemoryHandle* ret = new MemoryHandle(this);
\r
140 m_handles.push_back(ret);
\r
144 void MemoryStorageService::removeHandle(StorageHandle* handle)
\r
146 Lock wrapper(mutex);
\r
147 for (vector<MemoryHandle*>::iterator i=m_handles.begin(); i!=m_handles.end(); ++i) {
\r
148 if (*i == handle) {
\r
149 m_handles.erase(i);
\r
155 void* MemoryStorageService::cleanup_fn(void* cache_p)
\r
157 MemoryStorageService* cache = reinterpret_cast<MemoryStorageService*>(cache_p);
\r
160 // First, let's block all signals
\r
161 Thread::mask_all_signals();
\r
164 // Now run the cleanup process.
\r
169 void MemoryStorageService::cleanup()
\r
172 NDC ndc("cleanup");
\r
176 Mutex* mutex = Mutex::create();
\r
179 m_log.info("cleanup thread started...running every %d seconds", m_cleanupInterval);
\r
181 while (!shutdown) {
\r
182 shutdown_wait->timedwait(mutex, m_cleanupInterval);
\r
186 unsigned long count=0;
\r
187 for (vector<MemoryHandle*>::iterator i=m_handles.begin(); i!=m_handles.end(); ++i)
\r
188 count += (*i)->reap();
\r
191 m_log.info("purged %d record(s) from storage", count);
\r
194 m_log.info("cleanup thread finished");
\r
198 Thread::exit(NULL);
\r
201 void MemoryStorageService::reap(StorageHandle* handle)
\r
203 if (!isValid(handle))
\r
204 throw IOException("Invalid storage handle.");
\r
205 static_cast<MemoryHandle*>(handle)->reap();
\r
208 unsigned long MemoryStorageService::MemoryHandle::reap()
\r
210 // Lock the "database".
\r
212 SharedLock wrapper(m_lock, false);
\r
214 // Garbage collect any expired entries.
\r
215 unsigned long count=0;
\r
216 time_t now=time(NULL)-XMLToolingConfig::getConfig().clock_skew_secs;
\r
217 multimap<time_t,string>::iterator stop=m_expMap.upper_bound(now);
\r
218 for (multimap<time_t,string>::iterator i=m_expMap.begin(); i!=stop; m_expMap.erase(i++)) {
\r
219 m_dataMap.erase(i->second);
\r
226 void MemoryStorageService::createString(StorageHandle* handle, const char* key, const char* value, time_t expiration)
\r
228 if (!isValid(handle))
\r
229 throw IOException("Invalid storage handle.");
\r
230 MemoryHandle* h = static_cast<MemoryHandle*>(handle);
\r
233 h->m_lock->wrlock();
\r
234 SharedLock wrapper(h->m_lock, false);
\r
236 // Check for a duplicate.
\r
237 map<string,Record>::iterator i=h->m_dataMap.find(key);
\r
238 if (i!=h->m_dataMap.end())
\r
239 throw IOException("attempted to insert a record with duplicate key ($1)", params(1,key));
\r
241 h->m_dataMap[key]=Record(value,time(NULL),expiration);
\r
242 h->m_expMap.insert(multimap<time_t,string>::value_type(expiration,key));
\r
244 m_log.debug("inserted record (%s)", key);
\r
247 bool MemoryStorageService::readString(StorageHandle* handle, const char* key, string& value, time_t modifiedSince)
\r
249 if (!isValid(handle))
\r
250 throw IOException("Invalid storage handle.");
\r
251 MemoryHandle* h = static_cast<MemoryHandle*>(handle);
\r
253 SharedLock wrapper(h->m_lock);
\r
254 map<string,Record>::iterator i=h->m_dataMap.find(key);
\r
255 if (i==h->m_dataMap.end())
\r
257 else if (modifiedSince >= i->second.modified)
\r
259 value = i->second.data;
\r
263 bool MemoryStorageService::updateString(StorageHandle* handle, const char* key, const char* value, time_t expiration)
\r
265 if (!isValid(handle))
\r
266 throw IOException("Invalid storage handle.");
\r
267 MemoryHandle* h = static_cast<MemoryHandle*>(handle);
\r
270 h->m_lock->wrlock();
\r
271 SharedLock wrapper(h->m_lock, false);
\r
273 map<string,Record>::iterator i=h->m_dataMap.find(key);
\r
274 if (i==h->m_dataMap.end())
\r
278 i->second.data = value;
\r
280 if (expiration && expiration != i->second.expiration) {
\r
281 // Update secondary map.
\r
282 pair<multimap<time_t,string>::iterator,multimap<time_t,string>::iterator> range =
\r
283 h->m_expMap.equal_range(i->second.expiration);
\r
284 for (; range.first != range.second; ++range.first) {
\r
285 if (range.first->second == i->first) {
\r
286 h->m_expMap.erase(range.first);
\r
290 i->second.expiration = expiration;
\r
291 h->m_expMap.insert(multimap<time_t,string>::value_type(expiration,key));
\r
294 i->second.modified = time(NULL);
\r
295 m_log.debug("updated record (%s)", key);
\r
299 bool MemoryStorageService::deleteString(StorageHandle* handle, const char* key)
\r
301 if (!isValid(handle))
\r
302 throw IOException("Invalid storage handle.");
\r
303 MemoryHandle* h = static_cast<MemoryHandle*>(handle);
\r
306 h->m_lock->wrlock();
\r
307 SharedLock wrapper(h->m_lock, false);
\r
309 // Find the record.
\r
310 map<string,Record>::iterator i=h->m_dataMap.find(key);
\r
311 if (i!=h->m_dataMap.end()) {
\r
312 // Now find the reversed index of expiration to key, so we can clear it.
\r
313 pair<multimap<time_t,string>::iterator,multimap<time_t,string>::iterator> range =
\r
314 h->m_expMap.equal_range(i->second.expiration);
\r
315 for (; range.first != range.second; ++range.first) {
\r
316 if (range.first->second == i->first) {
\r
317 h->m_expMap.erase(range.first);
\r
321 // And finally delete the record itself.
\r
322 h->m_dataMap.erase(i);
\r
323 m_log.debug("deleted record (%s)", key);
\r
327 m_log.debug("deleting record (%s)....not found", key);
\r