11 #include "ThreadLocalStorage.h" 13 #include "boost/algorithm/string/predicate.hpp" 15 #include "tbb/concurrent_queue.h" 16 #include "tbb/mutex.h" 17 #include "tbb/recursive_mutex.h" 24 #include <type_traits> 25 #include <unordered_map> 42 if ( m_data ) m_data->setRegistry(
this );
45 Entry(
const Entry& ) =
delete;
46 Entry& operator=(
const Entry& rhs ) =
delete;
47 Entry( Entry&& rhs ) =
delete;
48 Entry& operator=( Entry&& rhs ) =
delete;
51 unsigned long addRef()
override {
return -1; }
52 unsigned long release()
override {
return -1; }
53 const name_type&
name()
const override {
return m_identifier; }
54 const id_type&
identifier()
const override {
return m_identifier; }
56 DataObject*
object()
const override {
return const_cast<DataObject*>( m_data.get() ); }
59 m_addr.
reset( iAddr );
68 template <
typename Map = UnorderedMap>
71 static_assert( std::is_same_v<typename Map::key_type, std::string_view> );
78 auto nh = m_store.extract( i );
79 nh.key() = nh.mapped().identifier();
80 auto r = m_store.insert(
std::move( nh ) );
82 return r.position->second;
91 const Entry* d =
find( k );
92 return d ? d->object() :
nullptr;
94 const Entry*
find( std::string_view k )
const noexcept {
95 auto i = m_store.find( k );
96 return i != m_store.end() ? &( i->second ) :
nullptr;
99 auto begin() const noexcept {
return m_store.begin(); }
100 auto end() const noexcept {
return m_store.end(); }
101 void clear() noexcept { m_store.clear(); }
102 auto erase( std::string_view k ) {
return m_store.erase( k ); }
103 template <
typename Predicate>
104 void erase_if( Predicate p ) {
105 auto i = m_store.begin();
106 auto end = m_store.end();
108 if ( std::invoke( p, std::as_const( *i ) ) )
109 i = m_store.erase( i );
123 std::string_view normalize_path( std::string_view
path, std::string_view
prefix ) {
126 if ( !
path.empty() &&
path.front() ==
'/' )
path.remove_prefix( 1 );
132 auto status = cnv.
createObj( &addr, pObject );
134 if ( status.isFailure() )
object.reset();
139 struct Partition final {
141 int eventNumber = -1;
144 template <
typename T,
typename Mutex = tbb::recursive_mutex,
typename ReadLock =
typename Mutex::scoped_lock,
145 typename WriteLock = ReadLock>
151 template <
typename F>
152 decltype(
auto ) with_lock( F&& f ) {
153 WriteLock
lock{m_mtx};
156 template <
typename F>
157 decltype(
auto ) with_lock( F&& f )
const {
158 ReadLock
lock{m_mtx};
163 template <
typename Fun>
164 auto with_lock( Fun&& f ) {
165 return [f = std::forward<Fun>( f )](
auto& p ) -> decltype(
auto ) {
return p.with_lock( f ); };
168 TTHREAD_TLS( Synced<Partition>* ) s_current =
nullptr;
170 template <
typename Fun>
172 return s_current ? s_current->with_lock( std::forward<Fun>( f ) )
191 Gaudi::Property<bool> m_forceLeaves{
this,
"ForceLeaves",
false,
"force creation of default leaves on registerObject"};
206 using extends::extends;
208 CLID rootCLID()
const override;
212 size_t allocateStore(
int evtnumber )
override;
213 StatusCode freeStore(
size_t partition )
override;
214 size_t freeSlots()
override {
return m_freeSlots.unsafe_size(); }
215 StatusCode selectStore(
size_t partition )
override;
217 StatusCode clearStore(
size_t partition )
override;
218 StatusCode setNumberOfStores(
size_t slots )
override;
220 size_t getPartitionNumber(
int eventnumber )
const override;
223 return findObject(
id.fullKey(), pObject ).isSuccess();
231 StatusCode clearSubTree( std::string_view )
override;
250 StatusCode registerObject( std::string_view parentPath, std::string_view objectPath,
DataObject* pObject )
override;
253 StatusCode unregisterObject( std::string_view )
override;
258 return !obj ? unregisterObject(
sr )
286 Entry::setDataProviderSvc(
this );
287 extends::initialize().ignore();
288 if ( !setNumberOfStores( m_slots ).isSuccess() ) {
289 error() <<
"Cannot set number of slots" <<
endmsg;
293 for (
size_t i = 0; i < m_slots; i++ ) { m_freeSlots.push( i ); }
294 selectStore( 0 ).ignore();
298 error() <<
"Cannot get IConversionSvc " << m_loader.value() <<
endmsg;
301 return setDataLoader(
loader,
nullptr );
304 setDataLoader(
nullptr,
nullptr ).
ignore();
305 return extends::finalize();
323 size_t slot = std::string::npos;
325 assert( slot != std::string::npos );
327 [[maybe_unused]]
auto prev =
m_partitions[slot].with_lock(
328 [evtnumber]( Partition& p ) {
return std::exchange( p.eventNumber, evtnumber ); } );
329 assert( prev == -1 );
335 if ( slots <
size_t{1} ) {
336 error() <<
"Invalid number of slots (" << slots <<
")" <<
endmsg;
340 error() <<
"Too late to change the number of slots!" <<
endmsg;
350 with_lock( [eventnumber](
const Partition& p ) {
return p.eventNumber == eventnumber; } ) );
361 auto prev =
m_partitions[partition].with_lock( []( Partition& p ) {
return std::exchange( p.eventNumber, -1 ); } );
368 return m_partitions[partition].with_lock( []( Partition& p ) {
374 top = normalize_path( top,
rootName() );
375 return fwd( [&]( Partition& p ) {
376 p.store.erase_if( [top](
const auto& value ) {
return boost::algorithm::starts_with( value.first, top ); } );
381 return fwd( []( Partition& p ) {
387 return fwd( [&]( Partition& p ) {
388 top = normalize_path( top,
rootName() );
389 auto cmp = [](
const Entry* lhs,
const Entry* rhs ) {
return lhs->identifier() < rhs->identifier(); };
391 for (
const auto& v : p.store ) {
392 if ( boost::algorithm::starts_with( v.second.identifier(), top ) ) keys.insert( &v.second );
394 auto k = keys.begin();
395 while ( k != keys.end() ) {
396 const auto&
id = ( *k )->identifier();
398 bool accept = pAgent->
analyse( const_cast<Entry*>( *( k++ ) ),
level );
401 [&id](
const auto& e ) {
return boost::algorithm::starts_with( e->identifier(), id ); } );
409 debug() <<
"setRoot( " << root_path <<
", (DataObject*)" << (
void*)pObject <<
" )" <<
endmsg;
417 debug() <<
"setRoot( " << root_path <<
", (IOpaqueAddress*)" << (
void*)rootAddr.get() <<
" )" <<
endmsg;
420 if ( !rootAddr )
return Status::INVALID_OBJ_ADDR;
427 if ( !
object )
return Status::INVALID_OBJECT;
429 auto dummy = Entry{root_path, {}, {}};
430 object->setRegistry( &dummy );
431 rootAddr->setRegistry( &dummy );
433 if ( status.isSuccess() ) {
434 auto pObject =
object.get();
446 debug() <<
"registerAddress( (IRegistry*)" << (
void*)pReg <<
", " <<
path <<
", (IOpaqueAddress*)" << addr.get()
447 <<
"[ " << addr->
par()[0] <<
", " << addr->
par()[1] <<
" ]" 450 if ( !addr )
return Status::INVALID_OBJ_ADDR;
453 if ( !
object )
return Status::INVALID_OBJECT;
456 auto dummy = Entry{fullpath, {}, {}};
457 object->setRegistry( &dummy );
460 if ( !status.isSuccess() )
return status;
465 auto ptr =
object.get();
466 debug() <<
"registerAddress: " << std::quoted( normalize_path( fullpath,
rootName() ) ) <<
" (DataObject*)" 470 fwd( [&]( Partition& p ) {
478 return parentPath.empty()
488 for (
auto i = dir.rfind(
'/' ); i != std::string_view::npos; i = dir.rfind(
'/' ) ) {
489 dir = dir.substr( 0, i );
490 if ( !p.store.find( dir ) ) {
492 debug() <<
"registerObject: adding directory " << std::quoted( dir ) <<
endmsg;
499 auto ptr =
object.get();
500 debug() <<
"registerObject: " << std::quoted(
path ) <<
" (DataObject*)" << static_cast<void*>( ptr )
509 return fwd( [&]( Partition& p ) {
511 pObject = const_cast<DataObject*>( p.store.get(
path ) );
513 debug() <<
"retrieveObject: " << std::quoted(
path ) <<
" (DataObject*)" << (
void*)pObject
virtual const std::string * par() const =0
Retrieve String parameters.
Out1 * put(const DataObjectHandle< Out1 > &out_handle, Out2 &&out)
StatusCode setNumberOfStores(size_t slots) override
Set the number of event slots (copies of DataSvc objects).
StatusCode setDataLoader(IConversionSvc *svc, IDataProviderSvc *dpsvc) override
IRegistry * registry() const
Get pointer to Registry.
virtual StatusCode createObj(IOpaqueAddress *pAddress, DataObject *&refpObject)=0
Create the transient representation of an object.
Implementation of property with value of concrete type.
Gaudi::StateMachine::State FSMState() const override
GAUDI_API const std::string typeinfoName(const std::type_info &)
Get platform independent information about the class type.
virtual bool analyse(IRegistry *pObject, int level)=0
Analyse the data object.
StatusCode resetPreLoad() override
StatusCode updateObject(IRegistry *) override
StatusCode initialize() override
SmartIF< IConversionSvc > m_dataLoader
virtual StatusCode setDataProvider(IDataProviderSvc *pService)=0
Set Data provider service.
constexpr static const auto SUCCESS
std::vector< DataStoreItem > m_preLoads
Items to be pre-loaded.
Gaudi::Property< std::string > m_rootName
virtual const name_type & name() const =0
Name of the directory (or key)
StatusCode clearSubTree(DataObject *obj) override
tbb::concurrent_queue< size_t > m_freeSlots
StatusCode removePreLoadItem(const DataStoreItem &) override
StatusCode traverseSubTree(std::string_view, IDataStoreAgent *) override
StatusCode retrieveObject(IRegistry *pDirectory, std::string_view path, DataObject *&pObject) override
auto get(const Handle &handle, const Algo &, const EventContext &) -> decltype(details::deref(handle.get()))
StatusCode traverseTree(IDataStoreAgent *pAgent) override
Data provider interface definition.
Description of the DataStoreItem class.
Gaudi::Property< size_t > m_slots
GAUDI_API int backTrace(void **addresses, const int depth)
size_t freeSlots() override
bool exists(const DataObjID &id) override
StatusCode objectParent(const DataObject *, IRegistry *&) override
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Invalid root path object cannot be retrieved or stored.
StatusCode linkObject(std::string_view, DataObject *) override
#define DECLARE_COMPONENT(type)
Gaudi::Property< bool > m_forceLeaves
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
StatusCode preLoad() override
std::vector< Synced< Partition > > m_partitions
The actual store(s)
StatusCode clearSubTree(std::string_view) override
This class is used for returning status codes from appropriate routines.
StatusCode updateObject(DataObject *) override
StatusCode finalize() override
virtual unsigned long addRef()=0
Add reference to object.
size_t allocateStore(int evtnumber) override
Allocate a store partition for a given event number.
The IRegistry represents the entry door to the environment any data object residing in a transient da...
size_t getNumberOfStores() const override
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
unsigned int CLID
Class ID definition.
StatusCode unlinkObject(DataObject *, std::string_view) override
StatusCode unregisterObject(std::string_view) override
virtual IOpaqueAddress * address() const =0
Retrieve opaque storage address.
virtual DataObject * object() const =0
Retrieve object behind the link.
StatusCode objectParent(const IRegistry *, IRegistry *&) override
TTHREAD_TLS(Synced< Partition > *) s_current
StatusCode unregisterAddress(IRegistry *, std::string_view) override
StatusCode objectLeaves(const IRegistry *, std::vector< IRegistry * > &) override
StatusCode unregisterObject(DataObject *obj, std::string_view sr) override
StatusCode unregisterObject(DataObject *obj) override
StatusCode linkObject(IRegistry *, std::string_view, DataObject *) override
StatusCode clearStore() override
const StatusCode & ignore() const
Ignore/check StatusCode.
virtual StatusCode fillObjRefs(IOpaqueAddress *pAddress, DataObject *pObject)=0
Resolve the references of the created transient object.
StatusCode selectStore(size_t partition) override
Activate a partition object. The identifies the partition uniquely.
Generic data agent interface.
size_t getPartitionNumber(int eventnumber) const override
Get the partition number corresponding to a given event.
Base class used to extend a class implementing other interfaces.
virtual unsigned long release()=0
release reference to object
virtual void setAddress(IOpaqueAddress *pAddress)=0
Set/Update Opaque storage address.
StatusCode freeStore(size_t partition) override
Free a store partition.
StatusCode registerAddress(std::string_view fullPath, IOpaqueAddress *pAddress) override
StatusCode objectLeaves(const DataObject *, std::vector< IRegistry * > &) override
StatusCode unlinkObject(IRegistry *, std::string_view) override
StatusCode setRoot(std::string root_name, DataObject *pObject) override
constexpr static const auto FAILURE
virtual IDataProviderSvc * dataSvc() const =0
Retrieve pointer to Transient Store.
StatusCode unregisterAddress(std::string_view) override
virtual const id_type & identifier() const =0
Full identifier (or key)
AttribStringParser::Iterator begin(const AttribStringParser &parser)
virtual void setRegistry(IRegistry *r)=0
Update directory pointer.
Opaque address interface definition.
StatusCode registerObject(std::string_view parentPath, std::string_view objectPath, DataObject *pObject) override
A DataObject is the base class of any identifiable object on any data store.
KeyedObjectManager< map > Map
Forward declaration of specialized std::map-like object manager.
Use a minimal event store implementation, and adds everything required to satisfy the IDataProviderSv...
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
StatusCode addPreLoadItem(const DataStoreItem &) override
StatusCode findObject(IRegistry *pDirectory, std::string_view path, DataObject *&pObject) override
static GAUDI_API void setNumConcEvents(const std::size_t &nE)
const std::string & rootName() const override
StatusCode traverseSubTree(DataObject *obj, IDataStoreAgent *pAgent) override
StatusCode unlinkObject(std::string_view) override