-
Notifications
You must be signed in to change notification settings - Fork 122
/
DataService.h
562 lines (483 loc) · 18.5 KB
/
DataService.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
#ifndef MANTID_KERNEL_DATASERVICE_H_
#define MANTID_KERNEL_DATASERVICE_H_
//----------------------------------------------------------------------
// Includes
//----------------------------------------------------------------------
#include <boost/shared_ptr.hpp>
#include <boost/algorithm/string.hpp>
#include <Poco/NotificationCenter.h>
#include <Poco/Notification.h>
#include "MantidKernel/Logger.h"
#include "MantidKernel/Exception.h"
#include "MantidKernel/ConfigService.h"
namespace Mantid
{
namespace Kernel
{
/** DataService is the base class for storing DataObjects. It stores instances of DataObjects
(Workspace, Instrument, MappingTables,...). This is a templated class, implemented as a
singleton. For simplicity and naming conventions, specialized classes must be constructed. The specialized
classes (see example MantidAPI/InstrumentDataService.h) must simply :
1) call the BaseClass constructor with the Name of the service
2) Support the SingletonHolder templated class.
This is the primary data service that the users will interact with either through writing scripts or directly
through the API. It is implemented as a singleton class.
Copyright © 2008-2013 ISIS Rutherford Appleton Laboratory & NScD Oak Ridge National Laboratory
This file is part of Mantid.
Mantid is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
Mantid is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
File change history is stored at: <https://github.com/mantidproject/mantid>.
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
template <typename T>
class DLLExport DataService
{
private:
/// Typedef for the map holding the names of and pointers to the data objects
typedef std::map<std::string, boost::shared_ptr<T> > svcmap;
/// Iterator for the data store map
typedef typename svcmap::iterator svc_it;
/// Const iterator for the data store map
typedef typename svcmap::const_iterator svc_constit;
public:
/// Class for named object notifications
class NamedObjectNotification: public Poco::Notification
{
public:
NamedObjectNotification(const std::string& name) :
Poco::Notification(), m_name(name) {}
/// Returns the name of the object
std::string object_name()const{return m_name;}
private:
std::string m_name;///< object's name
};
/// Base class for DataService notifications that also stores a pointer to the object
class DataServiceNotification : public NamedObjectNotification
{
public:
/// Constructor
DataServiceNotification(const std::string& name, const boost::shared_ptr<T> obj)
: NamedObjectNotification(name), m_object(obj){}
/// Returns the const pointer to the object concerned or 0 if it is a general notification
const boost::shared_ptr<T> object()const{return m_object;}
private:
boost::shared_ptr<T> m_object;///< shared pointer to the object
};
/// AddNotification is sent after an new object is added to the data service.
/// name() and object() return name and pointer to the new object.
class AddNotification: public DataServiceNotification
{
public:
AddNotification(const std::string& name,const boost::shared_ptr<T> obj):DataServiceNotification(name,obj){}///< Constructor
};
/// BeforeReplaceNotification is sent before an object is replaced in the addOrReplace() function.
class BeforeReplaceNotification: public DataServiceNotification
{
public:
/** Constructor.
@param name :: The name of the replaced object
@param obj :: The pointer to the old object
@param new_obj :: The pointer to the new object
Both old and new objects are guaranteed to exist when an observer receives the notification.
*/
BeforeReplaceNotification(const std::string& name, const boost::shared_ptr<T> obj,const boost::shared_ptr<T> new_obj):DataServiceNotification(name,obj),m_new_object(new_obj){}
const boost::shared_ptr<T> new_object()const{return m_new_object;}///< Returns the pointer to the new object.
private:
boost::shared_ptr<T> m_new_object;///< shared pointer to the object
};
/// AfterReplaceNotification is sent after an object is replaced in the addOrReplace() function.
class AfterReplaceNotification: public DataServiceNotification
{
public:
/** Constructor.
* @param name :: The name of the replaced object
* @param new_obj :: The pointer to the new object
* Only new objects are guaranteed to exist when an observer receives the notification.
*/
AfterReplaceNotification(const std::string& name, const boost::shared_ptr<T> new_obj):DataServiceNotification(name,new_obj) {}
};
/// PreDeleteNotification is sent before an object is deleted from the data service.
/// name() and object() return name and pointer to the object being deleted.
/// The object still exists when the notification is received by an observer.
class PreDeleteNotification: public DataServiceNotification
{
public:
/// Constructor
PreDeleteNotification(const std::string& name,const boost::shared_ptr<T> obj):DataServiceNotification(name,obj){}
};
/// PostDeleteNotification is sent after an object is deleted from the data service.
/// name() returns the name of the object being deleted.
/// The object no longer exists when the notification is received by an observer.
class PostDeleteNotification: public NamedObjectNotification
{
public:
/// Constructor
PostDeleteNotification(const std::string& name):NamedObjectNotification(name) {}
};
/// Clear notification is sent when the service is cleared
class ClearNotification: public NamedObjectNotification
{
public:
///Constructor
ClearNotification() : NamedObjectNotification("") {}
};
/// Rename notification is sent from Renameworkspaces algorithm after a workspace is renamed
class RenameNotification: public NamedObjectNotification
{
public:
/// Constructor
RenameNotification(const std::string& name,const std::string& newwsname) :
NamedObjectNotification(name), m_outwsname(newwsname){}
///name of the new workspace
const std::string& new_objectname()const{return m_outwsname;}
private:
std::string m_outwsname; ///< output workspace name
};
//--------------------------------------------------------------------------
/** Add an object to the service
* @param name :: name of the object
* @param Tobject :: shared pointer to object to add
* @throw std::runtime_error if name is empty
* @throw std::runtime_error if name exists in the map
* @throw std::runtime_error if a null pointer is passed for the object
*/
virtual void add( const std::string& name, const boost::shared_ptr<T>& Tobject)
{
checkForEmptyName(name);
checkForNullPointer(Tobject);
// Make DataService access thread-safe
m_mutex.lock();
// At the moment, you can't overwrite a workspace (i.e. pass in a name
// that's already in the map with a pointer to a different workspace).
// Also, there's nothing to stop the same workspace from being added
// more than once with different names.
if ( ! datamap.insert(typename svcmap::value_type(name, Tobject)).second)
{
std::string error=" add : Unable to insert Data Object : '"+name+"'";
g_log.error(error);
m_mutex.unlock();
throw std::runtime_error(error);
}
else
{
g_log.debug() << "Add Data Object " << name << " successful" << std::endl;
m_mutex.unlock();
notificationCenter.postNotification(new AddNotification(name,Tobject));
}
return;
}
//--------------------------------------------------------------------------
/** Add or replace an object to the service.
* Does NOT throw if the name was already used.
*
* @param name :: name of the object
* @param Tobject :: shared pointer to object to add
* @throw std::runtime_error if name is empty
*/
virtual void addOrReplace( const std::string& name, const boost::shared_ptr<T>& Tobject)
{
checkForNullPointer(Tobject);
// Make DataService access thread-safe
m_mutex.lock();
//find if the Tobject already exists
std::string foundName;
svc_it it = findNameWithCaseSearch(name, foundName);
if (it!=datamap.end())
{
g_log.debug("Data Object '"+ foundName +"' replaced in data service.\n");
m_mutex.unlock();
notificationCenter.postNotification(new BeforeReplaceNotification(name,it->second,Tobject));
m_mutex.lock();
datamap[foundName] = Tobject;
m_mutex.unlock();
notificationCenter.postNotification(new AfterReplaceNotification(name,Tobject));
}
else
{
// Avoid double-locking
m_mutex.unlock();
DataService::add(name,Tobject);
}
return;
}
//--------------------------------------------------------------------------
/** Remove an object from the service.
* @param name :: name of the object */
void remove( const std::string& name)
{
// Make DataService access thread-safe
m_mutex.lock();
std::string foundName;
svc_it it = findNameWithCaseSearch(name, foundName);
if (it==datamap.end())
{
g_log.debug(" remove '" + name + "' cannot be found");
m_mutex.unlock();
return;
}
m_mutex.unlock();
notificationCenter.postNotification(new PreDeleteNotification(foundName,it->second));
m_mutex.lock();
g_log.information("Data Object '"+ foundName +"' deleted from data service.");
datamap.erase(it);
m_mutex.unlock();
notificationCenter.postNotification(new PostDeleteNotification(foundName));
return;
}
//--------------------------------------------------------------------------
/** Rename an object within the service.
* @param oldName :: The old name of the object
* @param newName :: The new name of the object
*/
void rename( const std::string& oldName, const std::string& newName)
{
checkForEmptyName(newName);
// Make DataService access thread-safe
m_mutex.lock();
std::string foundName;
svc_it it = findNameWithCaseSearch(oldName, foundName);
if (it==datamap.end())
{
g_log.warning(" rename '" + oldName + "' cannot be found");
m_mutex.unlock();
return;
}
// delete the object with the old name
auto object = it->second;
m_mutex.lock();
datamap.erase( it );
m_mutex.unlock();
// if there is another object which has newName delete it
// this is actually a replace operation, so we send out there notifications as well.
it = datamap.find( newName );
bool replace = (it != datamap.end());
if ( replace )
{
notificationCenter.postNotification(new BeforeReplaceNotification(newName, it->second, object));
m_mutex.lock();
datamap.erase( it );
m_mutex.unlock();
}
// insert the old object with the new name
if ( ! datamap.insert(typename svcmap::value_type(newName, object)).second )
{
std::string error=" add : Unable to insert Data Object : '"+newName+"'";
g_log.error(error);
m_mutex.unlock();
throw std::runtime_error(error);
}
//if we're replacing, send out the post replace notification
if( replace )
{
notificationCenter.postNotification(new AfterReplaceNotification(newName,object));
}
g_log.information("Data Object '"+ foundName +"' renamed to '" + newName + "'");
m_mutex.unlock();
notificationCenter.postNotification(new RenameNotification(oldName, newName));
return;
}
//--------------------------------------------------------------------------
/// Empty the service
void clear()
{
// Make DataService access thread-safe
m_mutex.lock();
datamap.clear();
m_mutex.unlock();
notificationCenter.postNotification(new ClearNotification());
g_log.debug() << typeid(this).name() << " cleared.\n";
}
//--------------------------------------------------------------------------
/** Get a shared pointer to a stored data object
* @param name :: name of the object */
boost::shared_ptr<T> retrieve( const std::string& name) const
{
// Make DataService access thread-safe
Poco::Mutex::ScopedLock _lock(m_mutex);
std::string foundName;
svc_it it = findNameWithCaseSearch(name, foundName);
if (it != datamap.end())
{
return it->second;
}
else
{
throw Kernel::Exception::NotFoundError("Data Object",name);
}
}
/// Check to see if a data object exists in the store
bool doesExist(const std::string& name) const
{
// Make DataService access thread-safe
Poco::Mutex::ScopedLock _lock(m_mutex);
std::string foundName;
svc_it it = findNameWithCaseSearch(name, foundName);
if (it!=datamap.end())
return true;
return false;
}
/// Return the number of objects stored by the data service
size_t size() const
{
Poco::Mutex::ScopedLock _lock(m_mutex);
if ( showingHiddenObjects() )
{
return datamap.size();
}
else
{
size_t count = 0;
for( svc_constit it = datamap.begin(); it != datamap.end(); ++it)
{
if ( ! isHiddenDataServiceObject(it->first) ) ++count;
}
return count;
}
}
/// Get the names of the data objects stored by the service
std::set<std::string> getObjectNames() const
{
if ( showingHiddenObjects() ) return getObjectNamesInclHidden();
Poco::Mutex::ScopedLock _lock(m_mutex);
std::set<std::string> names;
for( svc_constit it = datamap.begin(); it != datamap.end(); ++it)
{
if ( ! isHiddenDataServiceObject(it->first) )
{
names.insert(it->first);
}
}
return names;
}
/// Get the names of the data objects stored by the service
std::set<std::string> getObjectNamesInclHidden() const
{
Poco::Mutex::ScopedLock _lock(m_mutex);
std::set<std::string> names;
for( svc_constit it = datamap.begin(); it != datamap.end(); ++it)
{
names.insert(it->first);
}
return names;
}
/// Get a vector of the pointers to the data objects stored by the service
std::vector< boost::shared_ptr<T> > getObjects() const
{
Poco::Mutex::ScopedLock _lock(m_mutex);
const bool showingHidden = showingHiddenObjects();
std::vector< boost::shared_ptr<T> > objects;
objects.reserve( datamap.size() );
for(auto it = datamap.begin(); it != datamap.end(); ++it)
{
if ( showingHidden || ! isHiddenDataServiceObject(it->first) )
{
objects.push_back( it->second );
}
}
return objects;
}
inline static std::string prefixToHide()
{
return "__";
}
inline static bool isHiddenDataServiceObject(const std::string& name)
{
return boost::starts_with(name, prefixToHide());
}
static bool showingHiddenObjects()
{
int showingHiddenFlag;
const int success = ConfigService::Instance().getValue("MantidOptions.InvisibleWorkspaces",showingHiddenFlag);
if ( success == 1 && showingHiddenFlag == 1 )
{
return true;
}
else
{
return false;
}
}
/// Sends notifications to observers. Observers can subscribe to notificationCenter
/// using Poco::NotificationCenter::addObserver(...)
///@return nothing
Poco::NotificationCenter notificationCenter;
protected:
/// Protected constructor (singleton)
DataService(const std::string& name) : svc_name(name),g_log(Kernel::Logger::get(svc_name)) {}
virtual ~DataService(){}
private:
/// Private, unimplemented copy constructor
DataService(const DataService&);
/// Private, unimplemented copy assignment operator
DataService& operator=(const DataService&);
void checkForEmptyName(const std::string& name)
{
if (name.empty())
{
const std::string error="Add Data Object with empty name";
g_log.debug() << error << std::endl;
throw std::runtime_error(error);
}
}
void checkForNullPointer(const boost::shared_ptr<T>& Tobject)
{
if (!Tobject)
{
const std::string error="Attempt to add empty shared pointer";
g_log.debug() << error << std::endl;
throw std::runtime_error(error);
}
}
/**
* Find a name in the map and return an iterator pointing to it. This takes
* the string and if it does not find it then it looks for a match disregarding
* the case (const version)
* @param name The string name to search for
* @param foundName [Output] Stores the name here if one was found. It will be empty if not
* @returns An iterator pointing at the element or the end iterator
*/
svc_it findNameWithCaseSearch(const std::string & name, std::string &foundName) const
{
const svcmap& constdata = datamap;
svcmap& data = const_cast<svcmap&>(constdata);
if(name.empty()) return data.end();
//Exact match
foundName = name;
svc_it match = data.find(name);
if( match != data.end() ) return match;
// Try UPPER case
std::transform(foundName.begin(), foundName.end(), foundName.begin(),toupper);
match = data.find(foundName);
if( match != data.end() ) return match;
// Try lower case
std::transform(foundName.begin(), foundName.end(), foundName.begin(),tolower);
match = data.find(foundName);
if( match != data.end() ) return match;
// Try Sentence case
foundName = name;
// Upper cases the first letter
std::transform(foundName.begin(), foundName.begin() + 1, foundName.begin(),toupper);
match = data.find(foundName);
if( match == data.end() ) foundName = "";
return match;
}
/// DataService name. This is set only at construction. DataService name should be provided when construction of derived classes
const std::string svc_name;
/// Map of objects in the data service
svcmap datamap;
/// Recursive mutex to avoid simultaneous access or notifications
mutable Poco::Mutex m_mutex;
/// Reference to the logger for this DataService
Logger& g_log;
}; // End Class Data service
} // Namespace Kernel
} // Namespace Mantid
#endif /*MANTID_KERNEL_DATASERVICE_H_*/