ArchiveFileBuilder.hpp 6.55 KB
Newer Older
1
/*
2
 * @project        The CERN Tape Archive (CTA)
3
 * @copyright      Copyright(C) 2015-2021 CERN
4
5
6
7
 * @license        This program is free software: you can redistribute it and/or modify
 *                 it under the terms of the GNU General Public License as published by
 *                 the Free Software Foundation, either version 3 of the License, or
 *                 (at your option) any later version.
8
 *
9
10
11
12
 *                 This program is distributed in the hope that it will be useful,
 *                 but WITHOUT ANY WARRANTY; without even the implied warranty of
 *                 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *                 GNU General Public License for more details.
13
 *
14
15
 *                 You should have received a copy of the GNU General Public License
 *                 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
17
18
19
20
 */

#pragma once

#include "common/dataStructures/ArchiveFile.hpp"
21
#include "common/log/Logger.hpp"
22
23
24
25
26
27
28
29
30

#include <memory>

namespace cta {
namespace catalogue {

/**
 * Builds ArchiveFile objects from a stream of tape files ordered by archive ID
 * and then copy number.
31
 * The template T is an ArchiveFile or a DeletedArchiveFile
32
 */
33
template<typename T>
34
35
36
class ArchiveFileBuilder {
public:

37
38
39
40
41
42
43
  /**
   * Constructor.
   *
   * @param log Object representing the API to the CTA logging system.
   */
  ArchiveFileBuilder(log::Logger &log);

44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  /**
   * Appends the specified tape file to the ArchiveFile object currently
   * construction.
   *
   * If this append method is called with the tape file of the next ArchiveFile
   * to be constructed then this means the current ArchiveFile under
   * construction is complete and this method will therefore return the current
   * and complete ArchiveFile object.  The appened tape file will be remembered
   * by this builder object and used to start the construction of the next
   * ArchiveFile object.
   *
   * If this append method is called with an ArchiveFile with no tape files at
   * all then this means the current ArchiveFile under
   * construction is complete and this method will therefore return the current
   * and complete ArchiveFile object.  The appened tape file will be remembered
   * by this builder object and used to start the construction of the next
   * ArchiveFile object.
   *
   * If the call to this append does not complete the ArchiveFile object
   * currently under construction then this method will returns an empty unique
   * pointer.
   *
   * @param tapeFile The tape file to be appended or an archive file with no
   * tape files at all.
   */
69
  std::unique_ptr<T> append(const T &tapeFile);
70
71
72
73
74
75
76
77
78

  /**
   * Returns a pointer to the ArchiveFile object currently under construction.
   * A return value of nullptr means there there is no ArchiveFile object
   * currently under construction.
   *
   * @return The ArchiveFile object currently under construction or nullptr
   * if there isn't one.
   */
79
  T *getArchiveFile();
80
81
82
83
84
85
86
87

  /**
   * If there is an ArchiveFile under construction then it is forgotten.
   */
  void clear();

private:

88
89
90
91
92
  /**
   * Object representing the API to the CTA logging system.
   */
  log::Logger &m_log;

93
94
95
  /**
   * The Archivefile object currently under construction.
   */
96
  std::unique_ptr<T> m_archiveFile;
97
98
99

}; // class ArchiveFileBuilder

100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
template<typename T>
ArchiveFileBuilder<T>::ArchiveFileBuilder(log::Logger &log):
  m_log(log) {
}

//------------------------------------------------------------------------------
// append
//------------------------------------------------------------------------------
template<typename T>
std::unique_ptr<T> ArchiveFileBuilder<T>::append(
  const T &tapeFile) {

  // If there is currently no ArchiveFile object under construction
  if(nullptr == m_archiveFile.get()) {
    // If the tape file represents an ArchiveFile object with no tape files
    if(tapeFile.tapeFiles.empty()) {
      // Archive file is already complete
      return std::unique_ptr<T>(new T(tapeFile));
    }

    // If the tape file exists then it must be alone
    if(tapeFile.tapeFiles.size() != 1) {
      exception::Exception ex;
      ex.getMessage() << __FUNCTION__ << " failed: Expected exactly one tape file to be appended at a time: actual=" <<
        tapeFile.tapeFiles.size();
      throw ex;
    }

    // Start constructing one
    m_archiveFile.reset(new T(tapeFile));

    // There could be more tape files so return incomplete
    return std::unique_ptr<T>();
  }

  // If the tape file represents an ArchiveFile object with no tape files
  if(tapeFile.tapeFiles.empty()) {
    // The ArchiveFile object under construction is complete,
    // therefore return it and start the construction of the next
    std::unique_ptr<T> tmp;
    tmp = std::move(m_archiveFile);
    m_archiveFile.reset(new T(tapeFile));
    return tmp;
  }

  // If the tape file to be appended belongs to the ArchiveFile object
  // currently under construction
  if(tapeFile.archiveFileID == m_archiveFile->archiveFileID) {

    // The tape file must exist and must be alone
    if(tapeFile.tapeFiles.size() != 1) {
      exception::Exception ex;
      ex.getMessage() << __FUNCTION__ << " failed: Expected exactly one tape file to be appended at a time: actual=" <<
        tapeFile.tapeFiles.size() << " archiveFileID=" << tapeFile.archiveFileID;
      throw ex;
    }

    // Append the tape file
    m_archiveFile->tapeFiles.push_back(tapeFile.tapeFiles.front());

    // There could be more tape files so return incomplete
    return std::unique_ptr<T>();
  }

  // Reaching this point means the tape file to be appended belongs to the next
  // ArchiveFile to be constructed.

  // ArchiveFile object under construction is complete,
  // therefore return it and start the construction of the next
  std::unique_ptr<T> tmp;
  tmp = std::move(m_archiveFile);
  m_archiveFile.reset(new T(tapeFile));
  return tmp;
}

//------------------------------------------------------------------------------
// getArchiveFile
//------------------------------------------------------------------------------
template<typename T>
T *ArchiveFileBuilder<T>::getArchiveFile() {
  return m_archiveFile.get();
}

//------------------------------------------------------------------------------
// clear
//------------------------------------------------------------------------------
template<typename T>
void ArchiveFileBuilder<T>::clear() {
  m_archiveFile.reset();
}


195
196
} // namespace catalogue
} // namespace cta