CatalogueFactory.cpp 3.05 KB
Newer Older
Steven Murray's avatar
Steven Murray committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * 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/CatalogueFactory.hpp"
20
#include "catalogue/CatalogueRetryWrapper.hpp"
21
22
#include "catalogue/InMemoryCatalogue.hpp"
#include "catalogue/OracleCatalogue.hpp"
23
#include "catalogue/SqliteCatalogue.hpp"
Tao Lin's avatar
Tao Lin committed
24
#include "catalogue/MysqlCatalogue.hpp"
Steven Murray's avatar
Steven Murray committed
25
#include "common/exception/Exception.hpp"
26
#include "common/make_unique.hpp"
Steven Murray's avatar
Steven Murray committed
27
28
29
30
31
32
33

namespace cta {
namespace catalogue {

//------------------------------------------------------------------------------
// create
//------------------------------------------------------------------------------
34
35
36
37
std::unique_ptr<Catalogue> CatalogueFactory::create(
  log::Logger &log,
  const rdbms::Login &login,
  const uint64_t nbConns,
38
39
  const uint64_t nbArchiveFileListingConns,
  const uint32_t maxTriesToConnect) {
Steven Murray's avatar
Steven Murray committed
40
  try {
41
42
    switch(login.dbType) {
    case rdbms::Login::DBTYPE_IN_MEMORY:
43
44
45
46
      {
        auto c = cta::make_unique<InMemoryCatalogue>(log, nbConns, nbArchiveFileListingConns, maxTriesToConnect);
        return cta::make_unique<CatalogueRetryWrapper>(log, std::move(c), maxTriesToConnect);
      }
47
    case rdbms::Login::DBTYPE_ORACLE:
48
49
50
51
52
      {
        auto c = cta::make_unique<OracleCatalogue>(log, login.username, login.password, login.database, nbConns,
          nbArchiveFileListingConns, maxTriesToConnect);
        return cta::make_unique<CatalogueRetryWrapper>(log, std::move(c), maxTriesToConnect);
      }
53
    case rdbms::Login::DBTYPE_SQLITE:
54
55
56
57
58
      {
        auto c = cta::make_unique<SqliteCatalogue>(log, login.database, nbConns, nbArchiveFileListingConns,
          maxTriesToConnect);
        return cta::make_unique<CatalogueRetryWrapper>(log, std::move(c), maxTriesToConnect);
      }
Tao Lin's avatar
Tao Lin committed
59
60
61
62
63
64
    case rdbms::Login::DBTYPE_MYSQL:
      {
        auto c = cta::make_unique<MysqlCatalogue>(log, login, nbConns, nbArchiveFileListingConns,
          maxTriesToConnect);
        return cta::make_unique<CatalogueRetryWrapper>(log, std::move(c), maxTriesToConnect);
      }
65
    case rdbms::Login::DBTYPE_NONE:
Steven Murray's avatar
Steven Murray committed
66
67
68
69
      throw exception::Exception("Cannot create a catalogue without a database type");
    default:
      {
        exception::Exception ex;
70
        ex.getMessage() << "Unknown database type: value=" << login.dbType;
Steven Murray's avatar
Steven Murray committed
71
72
73
74
75
76
77
78
79
80
        throw ex;
      }
    }
  } catch(exception::Exception &ex) {
    throw exception::Exception(std::string(__FUNCTION__) + " failed: " + ex.getMessage().str());
  }
}

} // namespace catalogue
} // namespace cta