RdbmsArchiveFileItorImpl.cpp 11.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * The CERN Tape Archive (CTA) project
 * Copyright (C) 2015  CERN
 *
 * 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.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

19
#include "catalogue/ArchiveFileItor.hpp"
20
#include "catalogue/RdbmsArchiveFileItorImpl.hpp"
21
#include "common/exception/Exception.hpp"
22
23
24
25

namespace cta {
namespace catalogue {

26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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
69
70
71
72
73
namespace {
  /**
   * Populates an ArchiveFile object with the current column values of the
   * specified result set.
   *
   * @param rset The result set to be used to populate the ArchiveFile object.
   * @return The populated ArchiveFile object.
   */
  static common::dataStructures::ArchiveFile populateArchiveFile(const rdbms::Rset &rset) {
      rset.columnUint64("ARCHIVE_FILE_ID");
    if(!rset.columnIsNull("VID")) {
        rset.columnUint64("COPY_NB");
    }
    common::dataStructures::ArchiveFile archiveFile;

    archiveFile.archiveFileID = rset.columnUint64("ARCHIVE_FILE_ID");
    archiveFile.diskInstance = rset.columnString("DISK_INSTANCE_NAME");
    archiveFile.diskFileId = rset.columnString("DISK_FILE_ID");
    archiveFile.diskFileInfo.path = rset.columnString("DISK_FILE_PATH");
    archiveFile.diskFileInfo.owner = rset.columnString("DISK_FILE_USER");
    archiveFile.diskFileInfo.group = rset.columnString("DISK_FILE_GROUP");
    archiveFile.diskFileInfo.recoveryBlob = rset.columnString("DISK_FILE_RECOVERY_BLOB");
    archiveFile.fileSize = rset.columnUint64("SIZE_IN_BYTES");
    archiveFile.checksumType = rset.columnString("CHECKSUM_TYPE");
    archiveFile.checksumValue = rset.columnString("CHECKSUM_VALUE");
    archiveFile.storageClass = rset.columnString("STORAGE_CLASS_NAME");
    archiveFile.creationTime = rset.columnUint64("ARCHIVE_FILE_CREATION_TIME");
    archiveFile.reconciliationTime = rset.columnUint64("RECONCILIATION_TIME");

    // If there is a tape file
    if (!rset.columnIsNull("VID")) {
      common::dataStructures::TapeFile tapeFile;
      tapeFile.vid = rset.columnString("VID");
      tapeFile.fSeq = rset.columnUint64("FSEQ");
      tapeFile.blockId = rset.columnUint64("BLOCK_ID");
      tapeFile.compressedSize = rset.columnUint64("COMPRESSED_SIZE_IN_BYTES");
      tapeFile.copyNb = rset.columnUint64("COPY_NB");
      tapeFile.creationTime = rset.columnUint64("TAPE_FILE_CREATION_TIME");
      tapeFile.checksumType = archiveFile.checksumType; // Duplicated for convenience
      tapeFile.checksumValue = archiveFile.checksumValue; // Duplicated for convenience

      archiveFile.tapeFiles[rset.columnUint64("COPY_NB")] = tapeFile;
    }

    return archiveFile;
  }
} // anonymous namespace

74
75
76
77
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
RdbmsArchiveFileItorImpl::RdbmsArchiveFileItorImpl(
78
  rdbms::ConnPool &connPool,
79
  const TapeFileSearchCriteria &searchCriteria):
80
  m_connPool(connPool),
81
  m_searchCriteria(searchCriteria),
82
83
  m_rsetIsEmpty(true),
  m_hasMoreHasBeenCalled(false) {
84
  try {
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
    std::string sql =
      "SELECT "
        "ARCHIVE_FILE.ARCHIVE_FILE_ID AS ARCHIVE_FILE_ID,"
        "ARCHIVE_FILE.DISK_INSTANCE_NAME AS DISK_INSTANCE_NAME,"
        "ARCHIVE_FILE.DISK_FILE_ID AS DISK_FILE_ID,"
        "ARCHIVE_FILE.DISK_FILE_PATH AS DISK_FILE_PATH,"
        "ARCHIVE_FILE.DISK_FILE_USER AS DISK_FILE_USER,"
        "ARCHIVE_FILE.DISK_FILE_GROUP AS DISK_FILE_GROUP,"
        "ARCHIVE_FILE.DISK_FILE_RECOVERY_BLOB AS DISK_FILE_RECOVERY_BLOB,"
        "ARCHIVE_FILE.SIZE_IN_BYTES AS SIZE_IN_BYTES,"
        "ARCHIVE_FILE.CHECKSUM_TYPE AS CHECKSUM_TYPE,"
        "ARCHIVE_FILE.CHECKSUM_VALUE AS CHECKSUM_VALUE,"
        "ARCHIVE_FILE.STORAGE_CLASS_NAME AS STORAGE_CLASS_NAME,"
        "ARCHIVE_FILE.CREATION_TIME AS ARCHIVE_FILE_CREATION_TIME,"
        "ARCHIVE_FILE.RECONCILIATION_TIME AS RECONCILIATION_TIME,"
        "TAPE_FILE.VID AS VID,"
        "TAPE_FILE.FSEQ AS FSEQ,"
        "TAPE_FILE.BLOCK_ID AS BLOCK_ID,"
        "TAPE_FILE.COMPRESSED_SIZE_IN_BYTES AS COMPRESSED_SIZE_IN_BYTES,"
        "TAPE_FILE.COPY_NB AS COPY_NB,"
        "TAPE_FILE.CREATION_TIME AS TAPE_FILE_CREATION_TIME, "
        "TAPE.TAPE_POOL_NAME AS TAPE_POOL_NAME "
        "FROM "
        "ARCHIVE_FILE "
        "LEFT OUTER JOIN TAPE_FILE ON "
        "ARCHIVE_FILE.ARCHIVE_FILE_ID = TAPE_FILE.ARCHIVE_FILE_ID "
        "LEFT OUTER JOIN TAPE ON "
        "TAPE_FILE.VID = TAPE.VID";
    if(
      searchCriteria.archiveFileId  ||
      searchCriteria.diskInstance   ||
      searchCriteria.diskFileId     ||
      searchCriteria.diskFilePath   ||
      searchCriteria.diskFileUser   ||
      searchCriteria.diskFileGroup  ||
      searchCriteria.storageClass   ||
      searchCriteria.vid            ||
      searchCriteria.tapeFileCopyNb ||
      searchCriteria.tapePool) {
      sql += " WHERE ";
    }

    bool addedAWhereConstraint = false;

    if(searchCriteria.archiveFileId) {
      sql += " ARCHIVE_FILE.ARCHIVE_FILE_ID = :ARCHIVE_FILE_ID";
      addedAWhereConstraint = true;
    }
    if(searchCriteria.diskInstance) {
      if(addedAWhereConstraint) sql += " AND ";
      sql += "ARCHIVE_FILE.DISK_INSTANCE_NAME = :DISK_INSTANCE_NAME";
      addedAWhereConstraint = true;
    }
    if(searchCriteria.diskFileId) {
      if(addedAWhereConstraint) sql += " AND ";
      sql += "ARCHIVE_FILE.DISK_FILE_ID = :DISK_FILE_ID";
      addedAWhereConstraint = true;
    }
    if(searchCriteria.diskFilePath) {
      if(addedAWhereConstraint) sql += " AND ";
      sql += "ARCHIVE_FILE.DISK_FILE_PATH = :DISK_FILE_PATH";
      addedAWhereConstraint = true;
    }
    if(searchCriteria.diskFileUser) {
      if(addedAWhereConstraint) sql += " AND ";
      sql += "ARCHIVE_FILE.DISK_FILE_USER = :DISK_FILE_USER";
      addedAWhereConstraint = true;
    }
    if(searchCriteria.diskFileGroup) {
      if(addedAWhereConstraint) sql += " AND ";
      sql += "ARCHIVE_FILE.DISK_FILE_GROUP = :DISK_FILE_GROUP";
      addedAWhereConstraint = true;
    }
    if(searchCriteria.storageClass) {
      if(addedAWhereConstraint) sql += " AND ";
      sql += "ARCHIVE_FILE.STORAGE_CLASS_NAME = :STORAGE_CLASS_NAME";
      addedAWhereConstraint = true;
    }
    if(searchCriteria.vid) {
      if(addedAWhereConstraint) sql += " AND ";
      sql += "TAPE_FILE.VID = :VID";
      addedAWhereConstraint = true;
    }
    if(searchCriteria.tapeFileCopyNb) {
      if(addedAWhereConstraint) sql += " AND ";
      sql += "TAPE_FILE.COPY_NB = :TAPE_FILE_COPY_NB";
      addedAWhereConstraint = true;
    }
    if(searchCriteria.tapePool) {
      if(addedAWhereConstraint) sql += " AND ";
      sql += "TAPE.TAPE_POOL_NAME = :TAPE_POOL_NAME";
    }

    sql += " ORDER BY ARCHIVE_FILE_ID, COPY_NB";

    auto conn = connPool.getConn();
    m_stmt = conn.createStmt(sql, rdbms::Stmt::AutocommitMode::OFF);
    if(searchCriteria.archiveFileId) {
      m_stmt->bindUint64(":ARCHIVE_FILE_ID", searchCriteria.archiveFileId.value());
    }
    if(searchCriteria.diskInstance) {
      m_stmt->bindString(":DISK_INSTANCE_NAME", searchCriteria.diskInstance.value());
    }
    if(searchCriteria.diskFileId) {
      m_stmt->bindString(":DISK_FILE_ID", searchCriteria.diskFileId.value());
190
    }
191
192
    if(searchCriteria.diskFilePath) {
      m_stmt->bindString(":DISK_FILE_PATH", searchCriteria.diskFilePath.value());
193
    }
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
    if(searchCriteria.diskFileUser) {
      m_stmt->bindString(":DISK_FILE_USER", searchCriteria.diskFileUser.value());
    }
    if(searchCriteria.diskFileGroup) {
      m_stmt->bindString(":DISK_FILE_GROUP", searchCriteria.diskFileGroup.value());
    }
    if(searchCriteria.storageClass) {
      m_stmt->bindString(":STORAGE_CLASS_NAME", searchCriteria.storageClass.value());
    }
    if(searchCriteria.vid) {
      m_stmt->bindString(":VID", searchCriteria.vid.value());
    }
    if(searchCriteria.tapeFileCopyNb) {
      m_stmt->bindUint64(":TAPE_FILE_COPY_NB", searchCriteria.tapeFileCopyNb.value());
    }
    if(searchCriteria.tapePool) {
      m_stmt->bindString(":TAPE_POOL_NAME", searchCriteria.tapePool.value());
    }
    m_rset = m_stmt->executeQuery();

    m_rsetIsEmpty = !m_rset.next();
215
  } catch(exception::Exception &ex) {
216
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
217
218
219
220
221
222
223
224
225
226
227
228
  }
}

//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
RdbmsArchiveFileItorImpl::~RdbmsArchiveFileItorImpl() {
}

//------------------------------------------------------------------------------
// hasMore
//------------------------------------------------------------------------------
229
230
231
232
233
234
235
236
237
238
239
240
241
bool RdbmsArchiveFileItorImpl::hasMore() {
  m_hasMoreHasBeenCalled = true;

  if(m_rsetIsEmpty) {
    // If there is an ArchiveFile object under construction
    if(nullptr != m_archiveFileBuilder.getArchiveFile()) {
      return true;
    } else {
      return false;
    }
  } else {
    return true;
  }
242
243
244
245
246
247
}

//------------------------------------------------------------------------------
// next
//------------------------------------------------------------------------------
common::dataStructures::ArchiveFile RdbmsArchiveFileItorImpl::next() {
248
249
250
251
252
253
254
255
256
257
258
  if(!m_hasMoreHasBeenCalled) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: hasMore() must be called before next()");
  }
  m_hasMoreHasBeenCalled = false;

  // If there are no more rows in the result set
  if(m_rsetIsEmpty) {
    // There must be an ArchiveFile object currently under construction
    if(nullptr == m_archiveFileBuilder.getArchiveFile()) {
      throw exception::Exception(std::string(__FUNCTION__) +
        " failed: next() was called with no more rows in the result set and no ArchiveFile object under construction");
259
260
    }

261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
    // Return the ArchiveFile object that must now be complete and clear the
    // ArchiveFile builder
    auto tmp = *m_archiveFileBuilder.getArchiveFile();
    m_archiveFileBuilder.clear();
    return tmp;
  }

  while(true) {
    auto archiveFile = populateArchiveFile(m_rset);

    auto completeArchiveFile = m_archiveFileBuilder.append(archiveFile);

    m_rsetIsEmpty = !m_rset.next();

    // If the ArchiveFile object under construction is complete
    if (nullptr != completeArchiveFile.get()) {
277

278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
      return *completeArchiveFile;

    // The ArchiveFile object under construction is not complete
    } else {
      if(m_rsetIsEmpty) {
        // There must be an ArchiveFile object currently under construction
        if (nullptr == m_archiveFileBuilder.getArchiveFile()) {
          throw exception::Exception(std::string(__FUNCTION__) + " failed:"
            " next() was called with no more rows in the result set and no ArchiveFile object under construction");
        }

        // Return the ArchiveFile object that must now be complete and clear the
        // ArchiveFile builder
        auto tmp = *m_archiveFileBuilder.getArchiveFile();
        m_archiveFileBuilder.clear();
        return tmp;
294
295
296
297
298
299
300
      }
    }
  }
}

} // namespace catalogue
} // namespace cta