| The Gaudi Framework v30r0 (c919700c)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

RegistryEntry.cpp
Go to the documentation of this file.
1 //====================================================================
2 // RegistryEntry.cpp
3 //--------------------------------------------------------------------
4 //
5 // Package : DataSvc ( The LHCb Offline System)
6 //
7 // Description: implementation of the Transient data store
8 //
9 // Author : M.Frank
10 // History :
11 // +---------+----------------------------------------------+---------
12 // | Date | Comment | Who
13 // +---------+----------------------------------------------+---------
14 // | 29/10/98| Initial version | MF
15 // | 03/02/99| Protect dynamic_cast with try-catch clauses | MF
16 // +---------+----------------------------------------------+---------
17 //
18 //====================================================================
19 #define DATASVC_REGISTRYENTRY_CPP
20
21 // STL include files
22 #include <algorithm>
23
24 // Interfaces
25 #include "GaudiKernel/IDataStoreAgent.h"
26 #include "GaudiKernel/IOpaqueAddress.h"
27
28 // Framework include files
29 #include "GaudiKernel/DataObject.h"
30 #include "GaudiKernel/RegistryEntry.h"
31
32 // If you absolutely need optimization: switch off dynamic_cast.
33 // This improves access to the data store roughly by more than 10 %
34 // for balanced trees.
35 //
36 // M.Frank
37 //
38 #define CAST_REGENTRY( x, y ) dynamic_cast<x>( y )
39 //#define CAST_REGENTRY(x,y) (x)(y)
40 constexpr char SEPARATOR{'/'};
41
43 DataSvcHelpers::RegistryEntry::RegistryEntry( std::string path, RegistryEntry* parent )
44 : m_path( std::move( path ) ), m_pParent( parent )
45 {
46 std::string::size_type sep = m_path.rfind( SEPARATOR );
47 if ( m_path.front() != SEPARATOR ) m_path.insert( 0, 1, SEPARATOR );
48 if ( sep != std::string::npos ) m_path.erase( 0, sep );
49 assemblePath( m_fullpath );
50 addRef();
51 }
52
54 DataSvcHelpers::RegistryEntry::~RegistryEntry()
55 {
56 deleteElements();
57 if ( m_pObject ) {
58 if ( !m_isSoft ) m_pObject->setRegistry( nullptr );
59 m_pObject->release();
60 }
61 if ( m_pAddress ) {
62 if ( !m_isSoft ) m_pAddress->setRegistry( nullptr );
63 m_pAddress->release();
64 }
65 }
66
68 unsigned long DataSvcHelpers::RegistryEntry::release()
69 {
70 unsigned long cnt = --m_refCount;
71 if ( !m_refCount ) delete this;
72 return cnt;
73 }
74
76 void DataSvcHelpers::RegistryEntry::setParent( RegistryEntry* pParent )
77 {
78 m_pParent = pParent;
79 m_fullpath.clear();
80 assemblePath( m_fullpath );
81 }
82
84 void DataSvcHelpers::RegistryEntry::makeSoft( DataObject* pObject )
85 {
86 m_isSoft = true;
87 setObject( pObject );
88 // if ( 0 != m_pObject ) { // Useless: This justs sets my own address again...
89 // setAddress(m_pObject->address());
90 // }
91 }
92
94 void DataSvcHelpers::RegistryEntry::makeSoft( IOpaqueAddress* pAddress )
95 {
96 m_isSoft = true;
97 setAddress( pAddress );
98 }
99
101 void DataSvcHelpers::RegistryEntry::makeHard( DataObject* pObject )
102 {
103 makeSoft( pObject );
104 m_isSoft = false;
105 if ( m_pObject ) m_pObject->setRegistry( this );
106 if ( m_pAddress ) m_pAddress->setRegistry( this );
107 }
108
110 void DataSvcHelpers::RegistryEntry::makeHard( IOpaqueAddress* pAddress )
111 {
112 m_isSoft = false;
113 setAddress( pAddress );
114 }
115
117 void DataSvcHelpers::RegistryEntry::setAddress( IOpaqueAddress* pAddress )
118 {
119 if ( pAddress ) {
120 pAddress->addRef();
121 pAddress->setRegistry( this );
122 }
123 if ( m_pAddress ) m_pAddress->release();
124 m_pAddress = pAddress;
125 }
126
128 void DataSvcHelpers::RegistryEntry::setObject( DataObject* pObject )
129 {
130 if ( pObject ) {
131 pObject->addRef();
132 if ( !isSoft() ) pObject->setRegistry( this );
133 }
134 if ( m_pObject ) m_pObject->release();
135 m_pObject = pObject;
136 }
137
139 long DataSvcHelpers::RegistryEntry::remove( IRegistry* obj )
140 {
141 try {
142 RegistryEntry* pEntry = dynamic_cast<RegistryEntry*>( obj );
143 auto i = std::remove( std::begin( m_store ), std::end( m_store ), pEntry );
144 if ( i != std::end( m_store ) ) {
145 pEntry->release();
146 m_store.erase( i, std::end( m_store ) );
147 }
148 } catch ( ... ) {
149 }
150 return m_store.size();
151 }
152
154 StatusCode DataSvcHelpers::RegistryEntry::remove( boost::string_ref path )
155 {
156 if ( path.front() == SEPARATOR ) path.remove_prefix( 1 );
157 auto i = std::find_if( m_store.begin(), m_store.end(), [&]( const auto* j ) {
158 boost::string_ref name{j->name()};
159 name.remove_prefix( 1 ); // skip leading SEPARATOR
160 return name == path;
161 } );
162 // if the requested object is not present, this is an error....
163 if ( i == m_store.end() ) return StatusCode::FAILURE;
164 remove( *i );
165 return StatusCode::SUCCESS;
166 }
167
169 DataSvcHelpers::RegistryEntry* DataSvcHelpers::RegistryEntry::i_create( std::string nam )
170 {
171 if ( nam.front() != SEPARATOR ) nam.insert( 0, 1, SEPARATOR );
172 // if this object is already present, this is an error....
173 auto not_present =
174 std::none_of( std::begin( m_store ), std::end( m_store ), [&]( IRegistry* i ) { return nam == i->name(); } );
175 return not_present ? new RegistryEntry( std::move( nam ), this ) : nullptr;
176 }
177
179 long DataSvcHelpers::RegistryEntry::add( IRegistry* obj )
180 {
181 RegistryEntry* pEntry = CAST_REGENTRY( RegistryEntry*, obj );
182 return i_add( pEntry );
183 }
184
186 long DataSvcHelpers::RegistryEntry::i_add( RegistryEntry* pEntry )
187 {
188 // TODO: if this is the sole place where items are added to m_store,
189 // and we know here that they must be RegisteryEntry, can we
190 // drop the dynamic_cast every where else???
191 // TODO: if so, can we also change m_store to be std::vector<RegistryEntry*>
192 // instead
193 // TODO: if so, can we not make it std::vector<RegistryEntry> instead?
194 // TODO: if so, should make sure that a RegistryEntry can be std::move'ed
195 try {
196 pEntry->setDataSvc( m_pDataProviderSvc );
197 m_store.push_back( pEntry );
198 pEntry->setParent( this );
199 if ( !pEntry->isSoft() && pEntry->address() ) {
200 pEntry->address()->setRegistry( pEntry );
201 }
202 } catch ( ... ) {
203 }
204 return m_store.size();
205 }
206
208 StatusCode DataSvcHelpers::RegistryEntry::add( std::string name, DataObject* pObject, bool is_soft )
209 {
210 RegistryEntry* entry = i_create( std::move( name ) );
211 if ( !entry ) return StatusCode::FAILURE;
212 ( is_soft ) ? entry->makeSoft( pObject ) : entry->makeHard( pObject );
213 i_add( entry );
214 return StatusCode::SUCCESS;
215 }
216
218 StatusCode DataSvcHelpers::RegistryEntry::add( std::string name, IOpaqueAddress* pAddress, bool is_soft )
219 {
220 RegistryEntry* entry = i_create( std::move( name ) );
221 if ( !entry ) return StatusCode::FAILURE;
222 ( is_soft ) ? entry->makeSoft( pAddress ) : entry->makeHard( pAddress );
223 i_add( entry );
224 return StatusCode::SUCCESS;
225 }
226
228 long DataSvcHelpers::RegistryEntry::deleteElements()
229 {
230 for ( auto& i : m_store ) {
231 RegistryEntry* entry = CAST_REGENTRY( RegistryEntry*, i );
232 if ( entry ) {
233 entry->deleteElements();
234 entry->release();
235 }
236 }
237 m_store.erase( m_store.begin(), m_store.end() );
238 return 0;
239 }
240
242 IRegistry* DataSvcHelpers::RegistryEntry::i_find( const IRegistry* obj ) const
243 {
244 auto i = std::find( m_store.begin(), m_store.end(), obj );
245 return ( i != m_store.end() ) ? ( *i ) : nullptr;
246 }
247
249 DataSvcHelpers::RegistryEntry* DataSvcHelpers::RegistryEntry::i_find( boost::string_ref path ) const
250 {
251 if ( path.front() == SEPARATOR ) path.remove_prefix( 1 ); // strip leading '/', if present
252 while ( !path.empty() ) {
253 // check that the chars of path prior to / are the same as regEnt->name()
254 // (i.e. match { nam:"/Ab" path:"/Ab/C"}
255 // but not { nam:"/Abc" path:"/Ab/C"})
256 auto loc1 = path.find( SEPARATOR );
257 auto cpath = path.substr( 0, loc1 );
258 if ( loc1 != boost::string_ref::npos ) {
259 path.remove_prefix( loc1 + 1 );
260 } else {
261 path.clear();
262 }
263 auto i = std::find_if( std::begin( m_store ), std::end( m_store ),
264 [&]( const auto& reg ) { return cpath == boost::string_ref{reg->name()}.substr( 1 ); } );
265 if ( i != std::end( m_store ) ) {
266 RegistryEntry* regEnt = CAST_REGENTRY( RegistryEntry*, *i );
267 return path.empty() ? regEnt : regEnt->i_find( path );
268 }
269 // If this node is "/NodeA", this part allows to find "/NodeA/NodeB" as
270 // our "/NodeB" child.
271 if ( cpath != boost::string_ref{m_path}.substr( 1 ) ) break;
272 }
273 return nullptr;
274 }
275
277 DataSvcHelpers::RegistryEntry* DataSvcHelpers::RegistryEntry::i_find( const DataObject* key ) const
278 {
279 if ( key ) {
280 if ( key == m_pObject ) return const_cast<RegistryEntry*>( this );
281 // Look in the immediate level:
282 RegistryEntry* result = CAST_REGENTRY( RegistryEntry*, i_find( key->registry() ) );
283 if ( result ) return result;
284 // Go levels down
285 for ( const auto& i : m_store ) {
286 try {
287 const RegistryEntry* entry = CAST_REGENTRY( RegistryEntry*, i );
288 result = entry->i_find( key );
289 if ( result ) return result;
290 } catch ( ... ) {
291 }
292 }
293 }
294 return nullptr;
295 }
296
297 // Traverse registry tree
298 StatusCode DataSvcHelpers::RegistryEntry::traverseTree( IDataStoreAgent* pAgent, int level )
299 {
300 bool go_down = pAgent->analyse( this, level );
301 StatusCode status;
302 if ( go_down ) {
303 for ( auto& i : m_store ) {
304 try {
305 RegistryEntry* entry = CAST_REGENTRY( RegistryEntry*, i );
306 entry->traverseTree( pAgent, level + 1 ).ignore();
307 } catch ( ... ) {
308 status = StatusCode::FAILURE;
309 }
310 }
311 }
312 return status;
313 }
314
315 // Recursive helper to assemble the full path name of the entry
316 void DataSvcHelpers::RegistryEntry::assemblePath( std::string& buffer ) const
317 {
318 if ( m_pParent ) m_pParent->assemblePath( buffer );
319 buffer += m_path;
320 }
StatusCode::SUCCESS
Definition: StatusCode.h:29
DataSvcHelpers::RegistryEntry::deleteElements
virtual long deleteElements()
Delete all contained elements.
Definition: RegistryEntry.cpp:228
GaudiPython.HistoUtils.path
path
Definition: HistoUtils.py:895
SEPARATOR
constexpr char SEPARATOR
Definition: RegistryEntry.cpp:40
DataSvcHelpers::RegistryEntry::name
const std::string & name() const override
Retrieve name of the entry.
Definition: RegistryEntry.h:116
std::string::front
T front(T...args)
DataSvcHelpers::RegistryEntry::traverseTree
virtual StatusCode traverseTree(IDataStoreAgent *pAgent, int level=0)
traverse data tree
Definition: RegistryEntry.cpp:298
IDataStoreAgent::analyse
virtual bool analyse(IRegistry *pObject, int level)=0
Analyse the data object.
DataSvcHelpers::RegistryEntry::addRef
unsigned long addRef() override
IInterface implementation: Dereference the object.
Definition: RegistryEntry.h:114
std::string::rfind
T rfind(T...args)
DataSvcHelpers::RegistryEntry::m_pParent
RegistryEntry * m_pParent
Pointer to parent.
Definition: RegistryEntry.h:59
DataSvcHelpers::RegistryEntry::~RegistryEntry
~RegistryEntry() override
Standard Destructor.
Definition: RegistryEntry.cpp:54
std
STL namespace.
DataObject::setRegistry
void setRegistry(IRegistry *pRegistry)
Set pointer to Registry.
Definition: DataObject.h:71
DataSvcHelpers::RegistryEntry::release
unsigned long release() override
IInterface implementation: Reference the object.
Definition: RegistryEntry.cpp:68
CAST_REGENTRY
#define CAST_REGENTRY(x, y)
Definition: RegistryEntry.cpp:38
IRegistry::name
virtual const name_type & name() const =0
Name of the directory (or key)
std::end
T end(T...args)
IOpaqueAddress::release
virtual unsigned long release()=0
release reference to object
std::remove
T remove(T...args)
DataSvcHelpers::RegistryEntry::setAddress
void setAddress(IOpaqueAddress *pAddress) override
Set/Update Opaque address.
Definition: RegistryEntry.cpp:117
DataObject::registry
IRegistry * registry() const
Get pointer to Registry.
Definition: DataObject.h:73
DataSvcHelpers::RegistryEntry::i_add
long i_add(RegistryEntry *entry)
Internal method to add entries.
Definition: RegistryEntry.cpp:186
std::string
STL class.
DataSvcHelpers::RegistryEntry::m_fullpath
std::string m_fullpath
String containing full path of the object (volatile)
Definition: RegistryEntry.h:55
DataSvcHelpers::RegistryEntry::assemblePath
void assemblePath(std::string &buffer) const
The following entries serve two aspects: 1) They are faster for recursive calls, because they are non...
Definition: RegistryEntry.cpp:316
DataSvcHelpers::RegistryEntry::isSoft
virtual bool isSoft() const
Is the link soft or hard.
Definition: RegistryEntry.h:128
std::vector::push_back
T push_back(T...args)
DataSvcHelpers::RegistryEntry::setParent
void setParent(RegistryEntry *pParent)
Set new parent pointer.
Definition: RegistryEntry.cpp:76
DataObject::addRef
virtual unsigned long addRef()
Add reference to object.
Definition: DataObject.cpp:59
DataSvcHelpers::RegistryEntry::setObject
void setObject(DataObject *obj)
Set/Update object address.
Definition: RegistryEntry.cpp:128
StatusCode
This class is used for returning status codes from appropriate routines.
Definition: StatusCode.h:26
std::string::erase
T erase(T...args)
DataSvcHelpers::RegistryEntry::m_pDataProviderSvc
IDataProviderSvc * m_pDataProviderSvc
Pointer to hosting transient store.
Definition: RegistryEntry.h:65
DataSvcHelpers::RegistryEntry::setDataSvc
void setDataSvc(IDataProviderSvc *s)
Set the transient data store.
Definition: RegistryEntry.h:90
IRegistry
The IRegistry represents the entry door to the environment any data object residing in a transient da...
Definition: IRegistry.h:22
std::string::clear
T clear(T...args)
DataSvcHelpers::RegistryEntry::address
IOpaqueAddress * address() const override
Retrieve opaque storage address.
Definition: RegistryEntry.h:124
DataSvcHelpers::RegistryEntry::m_pAddress
IOpaqueAddress * m_pAddress
Pointer to opaque address (load info)
Definition: RegistryEntry.h:61
DataSvcHelpers::RegistryEntry::makeSoft
void makeSoft(DataObject *pObject)
Initialize link as soft link.
Definition: RegistryEntry.cpp:84
DataSvcHelpers::RegistryEntry::i_create
RegistryEntry * i_create(std::string name)
Internal method to create entries.
Definition: RegistryEntry.cpp:169
IDataStoreAgent.h
std::move
T move(T...args)
DataSvcHelpers::RegistryEntry::m_isSoft
bool m_isSoft
Is the link soft or hard?
Definition: RegistryEntry.h:53
IOpaqueAddress.h
RegistryEntry.h
DataSvcHelpers::RegistryEntry
Definition of an entry in the transient data store.
Definition: RegistryEntry.h:36
DataObject::release
virtual unsigned long release()
release reference to object
Definition: DataObject.cpp:51
std::string::insert
T insert(T...args)
DataSvcHelpers::RegistryEntry::m_pObject
DataObject * m_pObject
Pointer to object.
Definition: RegistryEntry.h:63
std::find_if
T find_if(T...args)
DataSvcHelpers::RegistryEntry::m_refCount
unsigned long m_refCount
Reference counter.
Definition: RegistryEntry.h:51
std::vector::size
T size(T...args)
DataSvcHelpers::RegistryEntry::add
virtual StatusCode add(std::string name, DataObject *pObject, bool is_soft=false)
Add entry to data store.
Definition: RegistryEntry.cpp:208
IDataStoreAgent
Generic data agent interface.
Definition: IDataStoreAgent.h:16
std::begin
T begin(T...args)
std::none_of
T none_of(T...args)
DataSvcHelpers::RegistryEntry::m_store
Store m_store
Store of leaves.
Definition: RegistryEntry.h:67
DataSvcHelpers::RegistryEntry::RegistryEntry
RegistryEntry(std::string path, RegistryEntry *parent=nullptr)
Standard Constructor.
Definition: RegistryEntry.cpp:43
DataSvcHelpers::RegistryEntry::m_path
std::string m_path
Path name.
Definition: RegistryEntry.h:57
std::string::substr
T substr(T...args)
gaudirun.level
level
Definition: gaudirun.py:271
DataSvcHelpers::RegistryEntry::remove
virtual StatusCode remove(boost::string_ref name)
Remove an entry from the store.
Definition: RegistryEntry.cpp:154
StatusCode::FAILURE
Definition: StatusCode.h:29
IOpaqueAddress::setRegistry
virtual void setRegistry(IRegistry *r)=0
Update directory pointer.
IOpaqueAddress
Opaque address interface definition.
Definition: IOpaqueAddress.h:23
StatusCode::ignore
void ignore() const
Definition: StatusCode.h:84
DataObject
A DataObject is the base class of any identifiable object on any data store.
Definition: DataObject.h:30
DataSvcHelpers::RegistryEntry::i_find
IRegistry * i_find(const IRegistry *pDirectory) const
Internal method to retrieve data directory.
Definition: RegistryEntry.cpp:242
DataObject.h
IOpaqueAddress::addRef
virtual unsigned long addRef()=0
Add reference to object.
DataSvcHelpers::RegistryEntry::makeHard
void makeHard(DataObject *pObject)
Initialize link as hard link.
Definition: RegistryEntry.cpp:101