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 void createString(const char* key, const char* value, time_t expiration);
\r
44 bool readString(const char* key, string& value, time_t modifiedSince=0);
\r
45 bool updateString(const char* key, const char* value=NULL, time_t expiration=0);
\r
46 bool deleteString(const char* key);
\r
48 void createText(const char* key, const char* value, time_t expiration) {
\r
49 return createString(key, value, expiration);
\r
51 bool readText(const char* key, string& value, time_t modifiedSince=0) {
\r
52 return readString(key, value, modifiedSince);
\r
54 bool updateText(const char* key, const char* value=NULL, time_t expiration=0) {
\r
55 return updateString(key, value, expiration);
\r
57 bool deleteText(const char* key) {
\r
58 return deleteString(key);
\r
62 shutdown_wait->signal();
\r
68 struct XMLTOOL_DLLLOCAL Record {
\r
69 Record() : modified(0), expiration(0) {}
\r
70 Record(string s, time_t t1, time_t t2) : data(s), modified(t1), expiration(t2) {}
\r
72 time_t modified, expiration;
\r
75 map<string,Record> m_dataMap;
\r
76 multimap<time_t,string> m_expMap;
\r
78 CondWait* shutdown_wait;
\r
79 Thread* cleanup_thread;
\r
80 static void* cleanup_fn(void*);
\r
82 int m_cleanupInterval;
\r
86 StorageService* XMLTOOL_DLLLOCAL MemoryStorageServiceFactory(const DOMElement* const & e)
\r
88 return new MemoryStorageService(e);
\r
93 static const XMLCh cleanupInterval[] = UNICODE_LITERAL_15(c,l,e,a,n,u,p,I,n,t,e,r,v,a,l);
\r
95 MemoryStorageService::MemoryStorageService(const DOMElement* e)
\r
96 : m_lock(NULL), shutdown_wait(NULL), cleanup_thread(NULL), shutdown(false), m_cleanupInterval(0),
\r
97 m_log(Category::getInstance(XMLTOOLING_LOGCAT".StorageService"))
\r
99 m_lock = RWLock::create();
\r
100 shutdown_wait = CondWait::create();
\r
101 cleanup_thread = Thread::create(&cleanup_fn, (void*)this);
\r
103 const XMLCh* tag=e ? e->getAttributeNS(NULL,cleanupInterval) : NULL;
\r
105 m_cleanupInterval = XMLString::parseInt(tag);
\r
107 if (!m_cleanupInterval)
\r
108 m_cleanupInterval=300;
\r
111 MemoryStorageService::~MemoryStorageService()
\r
113 // Shut down the cleanup thread and let it know...
\r
115 shutdown_wait->signal();
\r
116 cleanup_thread->join(NULL);
\r
119 delete shutdown_wait;
\r
122 void* MemoryStorageService::cleanup_fn(void* cache_p)
\r
124 MemoryStorageService* cache = reinterpret_cast<MemoryStorageService*>(cache_p);
\r
127 // First, let's block all signals
\r
128 Thread::mask_all_signals();
\r
131 // Now run the cleanup process.
\r
136 void MemoryStorageService::cleanup()
\r
139 NDC ndc("cleanup");
\r
143 Mutex* mutex = Mutex::create();
\r
146 m_log.info("cleanup thread started...running every %d seconds", m_cleanupInterval);
\r
148 while (!shutdown) {
\r
149 shutdown_wait->timedwait(mutex, m_cleanupInterval);
\r
153 // Lock the "database".
\r
156 // Garbage collect any expired entries.
\r
157 unsigned int count=0;
\r
158 time_t now=time(NULL)-XMLToolingConfig::getConfig().clock_skew_secs;
\r
159 multimap<time_t,string>::iterator stop=m_expMap.upper_bound(now);
\r
160 for (multimap<time_t,string>::iterator i=m_expMap.begin(); i!=stop; m_expMap.erase(i++)) {
\r
161 m_dataMap.erase(i->second);
\r
168 m_log.info("purged %d record(s) from storage", count);
\r
171 m_log.info("cleanup thread finished");
\r
175 Thread::exit(NULL);
\r
178 void MemoryStorageService::createString(const char* key, const char* value, time_t expiration)
\r
182 SharedLock wrapper(m_lock, false);
\r
184 // Check for a duplicate.
\r
185 map<string,Record>::iterator i=m_dataMap.find(key);
\r
186 if (i!=m_dataMap.end())
\r
187 throw IOException("attempted to insert a record with duplicate key ($1)", params(1,key));
\r
189 m_dataMap[key]=Record(value,time(NULL),expiration);
\r
190 m_expMap.insert(multimap<time_t,string>::value_type(expiration,key));
\r
192 m_log.debug("inserted record (%s)", key);
\r
195 bool MemoryStorageService::readString(const char* key, string& value, time_t modifiedSince)
\r
197 SharedLock wrapper(m_lock);
\r
198 map<string,Record>::iterator i=m_dataMap.find(key);
\r
199 if (i==m_dataMap.end())
\r
201 else if (modifiedSince >= i->second.modified)
\r
203 value = i->second.data;
\r
207 bool MemoryStorageService::updateString(const char* key, const char* value, time_t expiration)
\r
211 SharedLock wrapper(m_lock, false);
\r
213 map<string,Record>::iterator i=m_dataMap.find(key);
\r
214 if (i==m_dataMap.end())
\r
218 i->second.data = value;
\r
220 if (expiration && expiration != i->second.expiration) {
\r
221 // Update secondary map.
\r
222 pair<multimap<time_t,string>::iterator,multimap<time_t,string>::iterator> range=m_expMap.equal_range(i->second.expiration);
\r
223 for (; range.first != range.second; ++range.first) {
\r
224 if (range.first->second == i->first) {
\r
225 m_expMap.erase(range.first);
\r
229 i->second.expiration = expiration;
\r
230 m_expMap.insert(multimap<time_t,string>::value_type(expiration,key));
\r
233 i->second.modified = time(NULL);
\r
234 m_log.debug("updated record (%s)", key);
\r
238 bool MemoryStorageService::deleteString(const char* key)
\r
242 SharedLock wrapper(m_lock, false);
\r
244 // Find the record.
\r
245 map<string,Record>::iterator i=m_dataMap.find(key);
\r
246 if (i!=m_dataMap.end()) {
\r
247 // Now find the reversed index of expiration to key, so we can clear it.
\r
248 pair<multimap<time_t,string>::iterator,multimap<time_t,string>::iterator> range=m_expMap.equal_range(i->second.expiration);
\r
249 for (; range.first != range.second; ++range.first) {
\r
250 if (range.first->second == i->first) {
\r
251 m_expMap.erase(range.first);
\r
255 // And finally delete the record itself.
\r
256 m_dataMap.erase(i);
\r
257 m_log.debug("deleted record (%s)", key);
\r
261 m_log.debug("deleting record (%s)....not found", key);
\r