LArSoft  v09_90_00
Liquid Argon Software toolkit - https://larsoft.org/
art::InputFileCatalog Class Reference

#include "InputFileCatalog.h"

Inheritance diagram for art::InputFileCatalog:
art::FileCatalog

Classes

struct  Config
 

Public Member Functions

 InputFileCatalog (fhicl::TableFragment< Config > const &config)
 
virtual ~InputFileCatalog ()=default
 
std::size_t size () const noexcept
 
FileCatalogItem const & currentFile () const
 
size_t currentIndex () const noexcept
 
bool getNextFile (int attempts=5)
 
bool hasNextFile (int attempts=5)
 
void rewind ()
 
void rewindTo (size_t index)
 
bool isSearchable () const noexcept
 
bool empty () const noexcept
 
void finish ()
 
std::vector< std::string > const & fileSources () const noexcept
 
void commitCatalog ()
 
std::string & url () noexcept
 
std::string const & url () const noexcept
 
void setActive () noexcept
 
bool active () const noexcept
 

Static Public Member Functions

static bool isPhysical (std::string const &name) noexcept
 

Static Public Attributes

static constexpr size_t indexEnd {std::numeric_limits<size_t>::max()}
 

Private Member Functions

bool retrieveNextFile (FileCatalogItem &item, int attempts, bool transferOnly=false)
 
FileCatalogStatus retrieveNextFileFromCacheOrService (FileCatalogItem &item)
 
FileCatalogStatus transferNextFile (FileCatalogItem &item)
 

Private Attributes

std::vector< std::string > fileSources_
 
std::vector< FileCatalogItemfileCatalogItems_ {{}}
 
FileCatalogItem nextItem_ {}
 
size_t fileIdx_ {indexEnd}
 
size_t maxIdx_ {}
 
bool searchable_ {false}
 
bool nextFileProbed_ {false}
 
bool hasNextFile_ {false}
 
ServiceHandle< CatalogInterfaceci_
 
ServiceHandle< FileTransferft_
 

Detailed Description

Definition at line 30 of file InputFileCatalog.h.

Constructor & Destructor Documentation

art::InputFileCatalog::InputFileCatalog ( fhicl::TableFragment< Config > const &  config)
explicit

Definition at line 20 of file InputFileCatalog.cc.

References art::errors::CatalogServiceError, ci_, fileCatalogItems_, fileSources_, and searchable_.

22  : fileSources_{config().namesParameter()}
23  {
24 
25  if (fileSources_.empty()) {
27  "InputFileCatalog::InputFileCatalog()\n")
28  << "Empty '" << config().namesParameter.name()
29  << "' parameter specified for input source.\n";
30  }
31 
32  // Configure FileDelivery service
33  ci_->configure(fileSources_);
34  searchable_ = ci_->isSearchable();
35 
36  if (searchable_)
37  fileCatalogItems_.resize(fileSources_.size());
38  }
std::vector< std::string > fileSources_
ServiceHandle< CatalogInterface > ci_
std::vector< FileCatalogItem > fileCatalogItems_
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
virtual art::InputFileCatalog::~InputFileCatalog ( )
virtualdefault

Member Function Documentation

bool art::FileCatalog::active ( ) const
inlinenoexceptinherited

Definition at line 113 of file FileCatalog.h.

114  {
115  return active_;
116  }
void art::FileCatalog::commitCatalog ( )
inlineinherited

Definition at line 90 of file FileCatalog.h.

91  {}
FileCatalogItem const & art::InputFileCatalog::currentFile ( ) const

Definition at line 41 of file InputFileCatalog.cc.

References fileCatalogItems_, fileIdx_, indexEnd, art::errors::LogicError, and maxIdx_.

Referenced by finish().

42  {
43  if (fileIdx_ == indexEnd) {
44  throw Exception(
46  "Cannot access the current file while the file catalog is empty!");
47  }
48  assert(fileIdx_ <= maxIdx_);
50  }
static constexpr size_t indexEnd
std::vector< FileCatalogItem > fileCatalogItems_
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
size_t art::InputFileCatalog::currentIndex ( ) const
noexcept

Definition at line 53 of file InputFileCatalog.cc.

References fileIdx_.

54  {
55  return fileIdx_;
56  }
bool art::InputFileCatalog::empty ( ) const
inlinenoexcept

Definition at line 55 of file InputFileCatalog.h.

56  {
57  return fileCatalogItems_.empty();
58  }
std::vector< FileCatalogItem > fileCatalogItems_
std::vector<std::string> const& art::InputFileCatalog::fileSources ( ) const
inlinenoexcept

Definition at line 61 of file InputFileCatalog.h.

62  {
63  return fileSources_;
64  }
std::vector< std::string > fileSources_
void art::InputFileCatalog::finish ( )

Definition at line 242 of file InputFileCatalog.cc.

References ci_, art::CONSUMED, currentFile(), fileCatalogItems_, fileIdx_, and indexEnd.

Referenced by retrieveNextFile().

243  {
244  if (fileIdx_ != indexEnd // there is a current file
245  && !currentFile().skipped() // not skipped
246  && !currentFile().consumed()) // not consumed
247  {
248  ci_->updateStatus(currentFile().uri(), FileDisposition::CONSUMED);
249  fileCatalogItems_[fileIdx_].consume();
250  }
251  }
static constexpr size_t indexEnd
ServiceHandle< CatalogInterface > ci_
FileCatalogItem const & currentFile() const
std::vector< FileCatalogItem > fileCatalogItems_
bool art::InputFileCatalog::getNextFile ( int  attempts = 5)

Definition at line 59 of file InputFileCatalog.cc.

References fileCatalogItems_, fileIdx_, hasNextFile_, indexEnd, maxIdx_, nextFileProbed_, nextItem_, retrieveNextFile(), and searchable_.

60  {
61  // get next file from FileDelivery service and give it to
62  // currentFile_ object returns false if theres no more file
63  //
64  // If hasNextFile() has been called prior to getNextFile(), it
65  // does not actually go fetch the next file from FileDelivery
66  // service, instead, it advances the iterator by one and make the
67  // "hidden" next file current.
68 
70  return false;
71 
72  if ((nextFileProbed_ && hasNextFile_) ||
73  retrieveNextFile(nextItem_, attempts)) {
74  nextFileProbed_ = false;
75  fileIdx_ =
76  (fileIdx_ == indexEnd) ? 0 : (searchable_ ? (fileIdx_ + 1) : 0);
77  if (fileIdx_ > maxIdx_)
78  maxIdx_ = fileIdx_;
80  return true;
81  }
82 
83  return false;
84  }
static constexpr size_t indexEnd
bool retrieveNextFile(FileCatalogItem &item, int attempts, bool transferOnly=false)
std::vector< FileCatalogItem > fileCatalogItems_
FileCatalogItem nextItem_
bool art::InputFileCatalog::hasNextFile ( int  attempts = 5)

Definition at line 87 of file InputFileCatalog.cc.

References hasNextFile_, nextFileProbed_, nextItem_, and retrieveNextFile().

88  {
89  // A probe. It tries (and actually does) retrieve the next file
90  // from the FileDelivery service. But does not advance the current
91  // file pointer
92  if (nextFileProbed_)
93  return hasNextFile_;
94 
96  nextFileProbed_ = true;
97 
98  return hasNextFile_;
99  }
bool retrieveNextFile(FileCatalogItem &item, int attempts, bool transferOnly=false)
FileCatalogItem nextItem_
static bool art::FileCatalog::isPhysical ( std::string const &  name)
inlinestaticnoexceptinherited

Definition at line 93 of file FileCatalog.h.

Referenced by transferNextFile().

94  {
95  return (name.empty() || name.find(':') != std::string::npos);
96  }
bool art::InputFileCatalog::isSearchable ( ) const
inlinenoexcept

Definition at line 50 of file InputFileCatalog.h.

51  {
52  return searchable_;
53  }
bool art::InputFileCatalog::retrieveNextFile ( FileCatalogItem item,
int  attempts,
bool  transferOnly = false 
)
private

Definition at line 102 of file InputFileCatalog.cc.

References ci_, art::errors::Configuration, art::DELIVERY_ERROR, finish(), art::NO_MORE_FILES, retrieveNextFileFromCacheOrService(), art::SKIPPED, art::SUCCESS, art::TRANSFER_ERROR, transferNextFile(), art::TRANSFERRED, and art::FileCatalogItem::uri().

Referenced by getNextFile(), and hasNextFile().

105  {
106  // Tell the service the current opened file (if there is one) is consumed
107  finish();
108 
109  // retrieve (deliver and transfer) next file from service
110  // or, do the transfer only
111  FileCatalogStatus status;
112  if (transferOnly) {
113  status = transferNextFile(item);
114  } else {
115  status = retrieveNextFileFromCacheOrService(item);
116  }
117 
118  switch (status) {
120  // mark the file as transferred
121  ci_->updateStatus(item.uri(), FileDisposition::TRANSFERRED);
122  return true;
123  }
125  return false;
126  }
128  if (attempts <= 1) {
130  "InputFileCatalog::retrieveNextFile()\n")
131  << "Delivery error encountered after reaching maximum number of "
132  "attemtps!";
133  }
134  return retrieveNextFile(item, attempts - 1, false);
135  }
137  if (attempts <= 1) {
138  // if we end up with a transfer error, the method returns
139  // with a true flag and empty filename. Weired enough, but
140  // the next file does exist we just cannot retrieve it. Therefore
141  // we notify the service that the file has been skipped
142  ci_->updateStatus(item.uri(), FileDisposition::SKIPPED);
143  return true;
144  }
145  return retrieveNextFile(item, attempts - 1, true);
146  }
147  }
148  assert(false);
149  return false; // Unreachable, but keeps the compiler happy
150  }
FileCatalogStatus retrieveNextFileFromCacheOrService(FileCatalogItem &item)
bool retrieveNextFile(FileCatalogItem &item, int attempts, bool transferOnly=false)
ServiceHandle< CatalogInterface > ci_
FileCatalogStatus
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
FileCatalogStatus transferNextFile(FileCatalogItem &item)
FileCatalogStatus art::InputFileCatalog::retrieveNextFileFromCacheOrService ( FileCatalogItem item)
private

Definition at line 153 of file InputFileCatalog.cc.

References ci_, art::DELIVERY_ERROR, fileCatalogItems_, fileIdx_, maxIdx_, art::NO_MORE_FILES, art::SUCCESS, and transferNextFile().

Referenced by retrieveNextFile().

154  {
155  // Try to get it from cached files
156  if (fileIdx_ < maxIdx_) {
157  item = fileCatalogItems_[fileIdx_ + 1];
159  }
160 
161  // Try to get it from the service
162  std::string uri;
163  double wait = 0.0;
164 
165  // get file delivered
166  int const result = ci_->getNextFileURI(uri, wait);
167 
168  if (result == FileDeliveryStatus::NO_MORE_FILES)
170 
171  if (result != FileDeliveryStatus::SUCCESS)
173 
174  item = FileCatalogItem("", "", uri);
175 
176  // get file transfered
177  return transferNextFile(item);
178  }
ServiceHandle< CatalogInterface > ci_
std::vector< FileCatalogItem > fileCatalogItems_
FileCatalogStatus transferNextFile(FileCatalogItem &item)
void art::InputFileCatalog::rewind ( )

Definition at line 213 of file InputFileCatalog.cc.

References fileIdx_, art::errors::LogicError, and searchable_.

214  {
215  if (!searchable_) {
216  throw Exception(errors::LogicError, "InputFileCatalog::rewind()\n")
217  << "A non-searchable catalog is not allowed to rewind!";
218  }
219  fileIdx_ = 0;
220  }
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
void art::InputFileCatalog::rewindTo ( size_t  index)

Definition at line 223 of file InputFileCatalog.cc.

References fileIdx_, art::errors::InvalidNumber, art::errors::LogicError, maxIdx_, and searchable_.

224  {
225  // rewind to a previous file location in the catalog service is
226  // not rewinded. only usable when
227  // FileDeliveryService::areFilesPersistent() is true
228  if (!searchable_) {
229  throw Exception(errors::LogicError, "InputFileCatalog::rewindTo()\n")
230  << "A non-searchable catalog is not allowed to rewind!";
231  }
232 
233  if (index > maxIdx_) {
234  throw Exception(errors::InvalidNumber, "InputFileCatalog::rewindTo()\n")
235  << "Index " << index << " is out of range!";
236  }
237 
238  fileIdx_ = index;
239  }
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
void art::FileCatalog::setActive ( )
inlinenoexceptinherited

Definition at line 108 of file FileCatalog.h.

109  {
110  active_ = true;
111  }
std::size_t art::InputFileCatalog::size ( void  ) const
inlinenoexcept

Definition at line 39 of file InputFileCatalog.h.

40  {
41  return fileCatalogItems_.size();
42  }
std::vector< FileCatalogItem > fileCatalogItems_
FileCatalogStatus art::InputFileCatalog::transferNextFile ( FileCatalogItem item)
private

Definition at line 181 of file InputFileCatalog.cc.

References art::errors::Configuration, art::FileCatalogItem::fileName(), ft_, art::FileCatalog::isPhysical(), art::FileCatalogItem::logicalFileName(), art::FileCatalogItem::skip(), art::SUCCESS, art::TRANSFER_ERROR, and art::FileCatalogItem::uri().

Referenced by retrieveNextFile(), and retrieveNextFileFromCacheOrService().

182  {
183  std::string pfn;
184  int const result = ft_->translateToLocalFilename(item.uri(), pfn);
185 
186  if (result != FileTransferStatus::SUCCESS) {
187  item.fileName("");
188  item.logicalFileName("");
189  item.skip();
191  }
192 
193  // successfully retrieved the file
194  std::string lfn = pfn;
195  boost::trim(pfn);
196 
197  if (pfn.empty()) {
199  "InputFileCatalog::retrieveNextFileFromCacheService()\n")
200  << "An empty string specified in parameter for input source.\n";
201  }
202 
203  if (isPhysical(pfn)) {
204  lfn.clear();
205  }
206 
207  item.fileName(pfn);
208  item.logicalFileName(lfn);
210  }
ServiceHandle< FileTransfer > ft_
cet::coded_exception< errors::ErrorCodes, ExceptionDetail::translate > Exception
Definition: Exception.h:66
static bool isPhysical(std::string const &name) noexcept
Definition: FileCatalog.h:93
std::string& art::FileCatalog::url ( )
inlinenoexceptinherited

Definition at line 98 of file FileCatalog.h.

99  {
100  return url_;
101  }
std::string url_
Definition: FileCatalog.h:119
std::string const& art::FileCatalog::url ( ) const
inlinenoexceptinherited

Definition at line 103 of file FileCatalog.h.

104  {
105  return url_;
106  }
std::string url_
Definition: FileCatalog.h:119

Member Data Documentation

ServiceHandle<CatalogInterface> art::InputFileCatalog::ci_
private
std::vector<FileCatalogItem> art::InputFileCatalog::fileCatalogItems_ {{}}
private
size_t art::InputFileCatalog::fileIdx_ {indexEnd}
private
std::vector<std::string> art::InputFileCatalog::fileSources_
private

Definition at line 75 of file InputFileCatalog.h.

Referenced by InputFileCatalog().

ServiceHandle<FileTransfer> art::InputFileCatalog::ft_
private

Definition at line 85 of file InputFileCatalog.h.

Referenced by transferNextFile().

bool art::InputFileCatalog::hasNextFile_ {false}
private

Definition at line 82 of file InputFileCatalog.h.

Referenced by getNextFile(), and hasNextFile().

constexpr size_t art::InputFileCatalog::indexEnd {std::numeric_limits<size_t>::max()}
static

Definition at line 66 of file InputFileCatalog.h.

Referenced by currentFile(), finish(), and getNextFile().

size_t art::InputFileCatalog::maxIdx_ {}
private
bool art::InputFileCatalog::nextFileProbed_ {false}
private

Definition at line 81 of file InputFileCatalog.h.

Referenced by getNextFile(), and hasNextFile().

FileCatalogItem art::InputFileCatalog::nextItem_ {}
private

Definition at line 77 of file InputFileCatalog.h.

Referenced by getNextFile(), and hasNextFile().

bool art::InputFileCatalog::searchable_ {false}
private

Definition at line 80 of file InputFileCatalog.h.

Referenced by getNextFile(), InputFileCatalog(), rewind(), and rewindTo().


The documentation for this class was generated from the following files: