ATLAS Offline Software
Loading...
Searching...
No Matches
Collection.cxx
Go to the documentation of this file.
1/*
2 Copyright (C) 2002-2026 CERN for the benefit of the ATLAS collaboration
3*/
4
5#include "Collection.h"
6
7#include "CoralBase/Attribute.h"
8
15
16#include <cstdint>
17#include <format>
18#include <set>
19#include <stdexcept>
20#include <utility>
21#include <vector>
22
24 : APRMessaging( "Collection"), m_idx(-1)
25{
26 if( !fileDescr.dbc() ) {
27 throw std::runtime_error( "Collection: file must already be connected: " + fileDescr.PFN() );
28 }
29
30 // all errors are thrown as exceptions
31 ATH_MSG_VERBOSE( "Opening collection '" << fileDescr.PFN() << "' in READ mode" );
32
33 DbContainer dhCont;
34 std::vector<const Token*> containerTokens;
35 DbDatabase db( fileDescr.dbc()->handle() );
36 if( !db.containers(containerTokens, false).isSuccess() ) {
37 throw std::runtime_error( "Collection: error reading " + fileDescr.PFN() );
38 }
39 const std::string containerPrefix = APRDefaults::ReadConfig::getEventTagName( fileDescr.FID() );
40 const std::string dhName = APRDefaults::ReadConfig::getDataHeaderName( fileDescr.FID() );
41 const std::string newDHContName = std::format("{}({})", dhName, APRDefaults::DataHeaderTypeName);
42 const std::string oldDHContName = std::format("{}_{}", dhName, APRDefaults::DataHeaderTypeName);
43 ATH_MSG_DEBUG("Opening Collection '" << fileDescr.PFN() << "' using container prefix: " << containerPrefix );
44 std::string tagContName = containerPrefix + "(";
45 // it seems merged files report multiple instances of the same container - avoid trying to process duplicates
46 std::set<std::string> seenContainers;
47 for( const Token *t : containerTokens ) {
48 Token token(t); // need a non-const Token
49 const std::string& contName = db.cntName(token);
50 const DbTypeInfo* typ_info = db.objectShape( token.classID() );
51 if( contName.starts_with( tagContName ) and !seenContainers.contains( contName ) ) {
52 seenContainers.insert( contName );
53 const std::string& attrName = contName.substr( tagContName.size(), contName.size() - tagContName.size() - 1 );
54 ATH_MSG_DEBUG(" :container " << contName << " with attribute " << attrName << " of type: " << typ_info->clazz().Name());
55 DbContainer cnt( db.type() );
56 if( cnt.open( db, contName, typ_info, token.technology(), db.openMode() ).isSuccess() ) {
57 if( attrName != APRDefaults::TokenTypeName ) {
58 if( m_containerMap.contains(attrName) ) {
59 throw std::runtime_error( "Column with name `" + attrName + "' already exists." );
60 }
61 m_attributeList.extend( attrName, typ_info->clazz().Name() );
62 }
63 m_containerMap.emplace( attrName, std::move(cnt) );
64 } else {
65 ATH_MSG_WARNING("EventTag container " << contName << " could not be opened");
66 }
67 } else if( contName == newDHContName || contName == oldDHContName ) {
68 ATH_MSG_DEBUG(" :container " << contName << " is the DataHeader container");
69 DbContainer cnt( db.type() );
70 if( cnt.open( db, contName, typ_info, token.technology(), db.openMode() ).isSuccess() ) {
71 dhCont = std::move(cnt);
72 } else {
73 ATH_MSG_WARNING("EventTag container " << contName << " could not be opened");
74 }
75 }
76 }
77 if( m_containerMap.empty() ) {
78 m_containerMap.emplace( APRDefaults::TokenTypeName, std::move(dhCont) );
79 }
80 m_tokenContainer = m_containerMap[APRDefaults::TokenTypeName];
81}
82
83
85{
86 if( ++m_idx >= size() ) {
87 return false;
88 }
89 // read the row
90 Token cnt_token(m_tokenContainer.token());
91 Token::OID_t oid { cnt_token.oid().first, (std::int64_t)m_idx };
92 for( auto& attr : m_attributeList ) {
93 const std::string& attr_name = attr.specification().name();
94 DbContainer &container = m_containerMap.at( attr_name );
95 void *obj = attr.addressOfData();
96 if( !container.load( &obj, nullptr, oid ).isSuccess() ) {
97 return false;
98 }
99 }
100
101 const std::string& newDHContName = std::format("{}(DataHeader)", APRDefaults::ReadConfig::getDataHeaderName( cnt_token.dbID().toString() ));
102 const std::string& oldDHContName = std::format("{}_DataHeader", APRDefaults::ReadConfig::getDataHeaderName( cnt_token.dbID().toString() ));
103 std::string tokenStr;
104 if( cnt_token.contID() == newDHContName || cnt_token.contID() == oldDHContName ) {
105 cnt_token.setOid(oid);
106 tokenStr = cnt_token.toString();
107 } else {
108 // read the Token
109 void *token_addr = &tokenStr;
110 if( !m_tokenContainer.load( &token_addr, nullptr, oid ).isSuccess() ) {
111 return false;
112 }
113 }
114 m_token.fromString( tokenStr );
115 return true;
116}
117
118
119std::size_t Collection::size()
120{
121 return m_tokenContainer.size();
122}
123
124
125bool Collection::seek(std::size_t position)
126{
127 if( position >= size() ) {
128 return false;
129 }
130 m_idx = position-1;
131 return true;
132}
133
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_VERBOSE(x,...)
This file contains the class definition for the Token class (migrated from POOL).
APRMessaging(const std::string &name)
bool next()
Advances to the next row.
std::map< std::string, DbContainer > m_containerMap
Attribute-name to container mapping for EventTag attributes.
Definition Collection.h:45
Token m_token
Definition Collection.h:47
std::size_t size()
Returns the number of rows without advancing the reader.
coral::AttributeList m_attributeList
Definition Collection.h:48
bool seek(std::size_t position)
Positions the reader before the requested row; next() reads it.
std::size_t m_idx
Definition Collection.h:49
DbContainer m_tokenContainer
Definition Collection.h:46
Collection(FileDescriptor &fileDescr)
Construct from an already-connected file descriptor.
DbDatabaseObj * handle()
Access object identifier.
Description: This class is a handle managing access and lifetime for the Container implementation obj...
Definition DbContainer.h:31
Description: Handle managing a DbDatabaseObj, a generic Database object.
Definition DbDatabase.h:39
Description:
DbConnection * dbc()
Access to file descriptor (READ).
const std::string & PFN() const
Access to physical file name (READ).
const FileID & FID() const
Access to file identifier (READ).
constexpr void toString(std::span< char, StrLen > buf, bool uppercase=true) const noexcept
Automatic conversion to string representation.
This class provides a token that identifies in a unique way objects on the persistent storage.
Definition Token.h:22
const std::string & contID() const
Access container identifier.
Definition Token.h:70
std::string toString() const
Retrieve the string representation of the token.
Definition Token.cxx:136
Token & setOid(const OID_t &oid)
Set object identifier.
Definition Token.h:86
const OID_t & oid() const
Access object identifier.
Definition Token.h:82
const Guid & dbID() const
Access database identifier.
Definition Token.h:65
std::string getDataHeaderName(std::string_view databaseName)
std::string getEventTagName(std::string_view databaseName)
static constexpr const char * DataHeaderTypeName
static constexpr const char * TokenTypeName