ArchiveFileBuilder.cpp 6.23 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * 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/>.
 */

#include "catalogue/ArchiveFileBuilder.hpp"
#include "common/exception/Exception.hpp"

22
23
#include <fstream>

24
25
26
namespace cta {
namespace catalogue {

27
28
29
30
31
32
33
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
ArchiveFileBuilder::ArchiveFileBuilder(log::Logger &log):
  m_log(log) {
}

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
74
75
76
77
78
79
80
81
82
83
84
85
86
//------------------------------------------------------------------------------
// append
//------------------------------------------------------------------------------
std::unique_ptr<common::dataStructures::ArchiveFile> ArchiveFileBuilder::append(
  const common::dataStructures::ArchiveFile &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<common::dataStructures::ArchiveFile>(new common::dataStructures::ArchiveFile(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 common::dataStructures::ArchiveFile(tapeFile));

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

  // 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<common::dataStructures::ArchiveFile> tmp;
    tmp = std::move(m_archiveFile);
    m_archiveFile.reset(new common::dataStructures::ArchiveFile(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
    const auto tapeFileMapItor = tapeFile.tapeFiles.begin();
87
88
89
    const auto vid = tapeFileMapItor->second.vid;
    const auto fSeq = tapeFileMapItor->second.fSeq;
    const auto blockId = tapeFileMapItor->second.blockId;
90
91
    const auto copyNbOfTapeFileToAppend = tapeFileMapItor->first;
    if(m_archiveFile->tapeFiles.find(copyNbOfTapeFileToAppend) != m_archiveFile->tapeFiles.end()) {
92
93
94
95
96
97
98
99
100
      // Found two tape files for the same archive file with the same copy
      // number

      // Ignore for now any inconsistencies in the copy number of the tape file
      // exception::Exception ex;
      // ex.getMessage() << __FUNCTION__ << " failed: Found two tape files for the same archive file with the same copy"
      //   " numbers: archiveFileID=" << tapeFile.archiveFileID << " copyNb=" << copyNbOfTapeFileToAppend;
      // throw ex;

101
      // Create a unique copy number to replace the original duplicate
102
103
104
105
106
107
      uint64_t maxCopyNb = 0;
      for(const auto maplet: m_archiveFile->tapeFiles) {
        if(maplet.first > maxCopyNb) {
          maxCopyNb = maplet.first;
        }
      }
108
109
110
111
112
113
      const uint64_t workaroundCopyNb = maxCopyNb + 1;
      {
        std::list<cta::log::Param> params;
        params.push_back(cta::log::Param("archiveFileID", tapeFile.archiveFileID));
        params.push_back(cta::log::Param("duplicateCopyNb", copyNbOfTapeFileToAppend));
        params.push_back(cta::log::Param("workaroundCopyNb", workaroundCopyNb));
114
        params.push_back(cta::log::Param("tapeVid", vid));
115
116
117
118
119
        params.push_back(cta::log::Param("fSeq", fSeq));
        params.push_back(cta::log::Param("blockId", blockId));
        m_log(cta::log::WARNING, "Found a duplicate tape copy number when listing archive files", params);
      }
      m_archiveFile->tapeFiles[workaroundCopyNb] = tapeFileMapItor->second;
120
121
    } else {
      m_archiveFile->tapeFiles[copyNbOfTapeFileToAppend] = tapeFileMapItor->second;
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
    }

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

  // 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<common::dataStructures::ArchiveFile> tmp;
  tmp = std::move(m_archiveFile);
  m_archiveFile.reset(new common::dataStructures::ArchiveFile(tapeFile));
  return tmp;
}

//------------------------------------------------------------------------------
// getArchiveFile
//------------------------------------------------------------------------------
common::dataStructures::ArchiveFile *ArchiveFileBuilder::getArchiveFile() {
  return m_archiveFile.get();
}

//------------------------------------------------------------------------------
// clear
//------------------------------------------------------------------------------
void ArchiveFileBuilder::clear() {
  m_archiveFile.reset();
}

} // namespace catalogue
} // namespace cta