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#include "CollectionCursor.h"
7
8#include "CoralBase/AttributeList.h"
9
16
17#include "CollectionColumn.h"
18
19#include <algorithm>
20#include <format>
21#include <set>
22#include <stdexcept>
23#include <utility>
24#include <vector>
25
26namespace pool {
27
28 Collection::~Collection() = default;
29
31 : APRMessaging( "Collection")
32 {
33 if( !fileDescr.dbc() ) {
34 throw std::runtime_error( "Collection: file must already be connected: " + fileDescr.PFN() );
35 }
36
37 // all errors are thrown as exceptions
38 ATH_MSG_VERBOSE( "Opening collection '" << fileDescr.PFN() << "' in READ mode" );
39
40 std::vector<const Token*> containerTokens;
41 DbDatabase db( fileDescr.dbc()->handle() );
42 if( !db.containers(containerTokens, false).isSuccess() ) {
43 throw std::runtime_error( "Collection: error reading " + fileDescr.PFN() );
44 }
45 const std::string containerPrefix = APRDefaults::ReadConfig::getEventTagName( fileDescr.FID() );
46 const std::string dhName = APRDefaults::ReadConfig::getDataHeaderName( fileDescr.FID() );
47 const std::string newDHContName = std::format("{}({})", dhName, APRDefaults::DataHeaderTypeName);
48 const std::string oldDHContName = std::format("{}_{}", dhName, APRDefaults::DataHeaderTypeName);
49 ATH_MSG_DEBUG("Opening Collection '" << fileDescr.PFN() << "' using container prefix: " << containerPrefix );
50 std::string tagContName = containerPrefix + "(";
51 // it seems merged files report multiple instances of the same container - avoid trying to process duplicates
52 std::set<std::string> seenContainers;
53 for( const Token *t : containerTokens ) {
54 Token token(t); // need a non-const Token
55 const std::string& contName = db.cntName(token);
56 const DbTypeInfo* typ_info = db.objectShape( token.classID() );
57 if( contName.starts_with( tagContName ) and !seenContainers.contains( contName ) ) {
58 seenContainers.insert( contName );
59 const std::string& attrName = contName.substr( tagContName.size(), contName.size() - tagContName.size() - 1 );
60 ATH_MSG_DEBUG(" :container " << contName << " with attribute " << attrName << " of type: " << typ_info->clazz().Name());
61 DbContainer cnt( db.type() );
62 if( cnt.open( db, contName, typ_info, token.technology(), db.openMode() ).isSuccess() ) {
63 if( attrName != CollectionColumn::tokenColumn().name() ) {
64 const auto duplicate = std::find_if(
65 m_attributeColumns.begin(), m_attributeColumns.end(),
66 [&attrName]( const CollectionColumn& column ) {
67 return column.name() == attrName;
68 } );
69 if( duplicate != m_attributeColumns.end() ) {
70 throw std::runtime_error( "Column with name `" + attrName + "' already exists." );
71 }
72 m_attributeColumns.emplace_back( attrName, typ_info->clazz().Name() );
73 }
74 m_containerMap.emplace( attrName, std::move(cnt) );
75 } else {
76 ATH_MSG_WARNING("EventTag container " << contName << " could not be opened");
77 }
78 } else if( contName == newDHContName || contName == oldDHContName ) {
79 ATH_MSG_DEBUG(" :container " << contName << " is the DataHeader container");
80 DbContainer cnt( db.type() );
81 if( cnt.open( db, contName, typ_info, token.technology(), db.openMode() ).isSuccess() ) {
82 m_dhCont = std::move(cnt);
83 } else {
84 ATH_MSG_WARNING("EventTag container " << contName << " could not be opened");
85 }
86 }
87 }
88 }
89
90
91 std::unique_ptr<CollectionCursor> Collection::cursor() {
92 if( m_containerMap.empty() ) {
94 }
95 pool::CollectionRowBuffer collectionRowBuffer;
96 coral::AttributeList attributeList;
97 for( const auto& attrCol : m_attributeColumns ) {
98 attributeList.extend( attrCol.name(), attrCol.type() );
99 }
100 collectionRowBuffer.setAttributeList( attributeList );
101 return std::make_unique<CollectionCursor>( collectionRowBuffer, m_containerMap);
102 }
103
104} //namespace pool
#define ATH_MSG_DEBUG(x,...)
#define ATH_MSG_VERBOSE(x,...)
This file contains the class definition for the Token class (migrated from POOL).
This class provides a token that identifies in a unique way objects on the persistent storage.
Definition Token.h:22
APRMessaging(const std::string &name)
A class representing a row of a collection.
void setAttributeList(const coral::AttributeList &attributeList)
Sets the Attribute list schema.
~Collection()
Defined out of line because CollectionColumn is incomplete here.
std::vector< CollectionColumn > m_attributeColumns
Definition Collection.h:42
std::unique_ptr< CollectionCursor > cursor()
Returns a cursor for the collection.
Collection(FileDescriptor &fileDescr)
Construct from an already-connected file descriptor.
std::map< std::string, DbContainer > m_containerMap
Map of container names to container handles for all EventTag attributes in the file.
Definition Collection.h:45
DbContainer m_dhCont
The DataHeader container, if present, in the file.
Definition Collection.h:47
DbDatabaseObj * handle()
Access object identifier.
Description: Handle managing a DbDatabaseObj, a generic Database object.
Definition DbDatabase.h:54
const FileID & FID() const
Access to file identifier (READ).
DbConnection * dbc()
Access to file descriptor (READ).
const std::string & PFN() const
Access to physical file name (READ).
std::string getDataHeaderName(std::string_view databaseName)
std::string getEventTagName(std::string_view databaseName)
static constexpr const char * DataHeaderTypeName
static constexpr const char * TokenTypeName