RdbmsCatalogue.cpp 245 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/ArchiveFileRow.hpp"
20
#include "catalogue/RdbmsCatalogue.hpp"
21
#include "catalogue/RdbmsCatalogueGetArchiveFilesItor.hpp"
22
#include "catalogue/RdbmsCatalogueGetArchiveFilesForRepackItor.hpp"
23
#include "catalogue/retryOnLostConnection.hpp"
24
#include "catalogue/SqliteCatalogueSchema.hpp"
25
#include "catalogue/UserSpecifiedANonEmptyLogicalLibrary.hpp"
26
#include "catalogue/UserSpecifiedANonEmptyTape.hpp"
27
#include "catalogue/UserSpecifiedANonExistentLogicalLibrary.hpp"
28
#include "catalogue/UserSpecifiedANonExistentTape.hpp"
29
#include "catalogue/UserSpecifiedAnEmptyStringComment.hpp"
30
#include "catalogue/UserSpecifiedAnEmptyStringDiskInstanceName.hpp"
31
#include "catalogue/UserSpecifiedAnEmptyStringLogicalLibraryName.hpp"
32
#include "catalogue/UserSpecifiedAnEmptyStringMediaType.hpp"
33
#include "catalogue/UserSpecifiedAnEmptyStringStorageClassName.hpp"
34
#include "catalogue/UserSpecifiedAnEmptyStringSupply.hpp"
35
#include "catalogue/UserSpecifiedAnEmptyStringTapePoolName.hpp"
36
#include "catalogue/UserSpecifiedAnEmptyStringUsername.hpp"
37
#include "catalogue/UserSpecifiedAnEmptyStringVendor.hpp"
38
#include "catalogue/UserSpecifiedAnEmptyStringVid.hpp"
39
#include "catalogue/UserSpecifiedAnEmptyStringVo.hpp"
40
#include "catalogue/UserSpecifiedAnEmptyTapePool.hpp"
41
#include "catalogue/UserSpecifiedAZeroCapacity.hpp"
42
#include "catalogue/UserSpecifiedAZeroCopyNb.hpp"
43
44
#include "catalogue/UserSpecifiedStorageClassUsedByArchiveFiles.hpp"
#include "catalogue/UserSpecifiedStorageClassUsedByArchiveRoutes.hpp"
45
#include "common/dataStructures/TapeFile.hpp"
46
#include "common/exception/Exception.hpp"
47
#include "common/exception/UserError.hpp"
48
#include "common/make_unique.hpp"
49
#include "common/threading/MutexLocker.hpp"
50
#include "common/Timer.hpp"
51
#include "common/utils/utils.hpp"
52
#include "rdbms/AutoRollback.hpp"
53

54
#include <ctype.h>
55
56
#include <memory>
#include <time.h>
57
#include <common/exception/LostDatabaseConnection.hpp>
58

59
60
61
namespace cta {
namespace catalogue {

62
63
64
//------------------------------------------------------------------------------
// constructor
//------------------------------------------------------------------------------
65
RdbmsCatalogue::RdbmsCatalogue(
66
  log::Logger &log,
67
  const rdbms::Login &login,
68
  const uint64_t nbConns,
69
  const uint64_t nbArchiveFileListingConns):
70
  m_log(log),
71
  m_connPool(login, nbConns),
72
  m_archiveFileListingConnPool(login, nbArchiveFileListingConns),
Steven Murray's avatar
Steven Murray committed
73
74
75
  m_tapeCopyToPoolCache(10),
  m_groupMountPolicyCache(10),
  m_userMountPolicyCache(10),
76
  m_expectedNbArchiveRoutesCache(10),
77
78
  m_isAdminCache(10),
  m_activitiesFairShareWeights(10) {}
79
80
81
82

//------------------------------------------------------------------------------
// destructor
//------------------------------------------------------------------------------
83
RdbmsCatalogue::~RdbmsCatalogue() {
84
85
86
87
88
}

//------------------------------------------------------------------------------
// createAdminUser
//------------------------------------------------------------------------------
89
void RdbmsCatalogue::createAdminUser(
Steven Murray's avatar
Steven Murray committed
90
  const common::dataStructures::SecurityIdentity &admin,
91
  const std::string &username,
92
  const std::string &comment) {
Steven Murray's avatar
Steven Murray committed
93
  try {
94
95
96
97
98
    if(username.empty()) {
      throw UserSpecifiedAnEmptyStringUsername("Cannot create admin user because the username is an empty string");
    }

    if(comment.empty()) {
99
      throw UserSpecifiedAnEmptyStringComment("Cannot create admin user because the comment is an empty string");
100
101
    }

102
    auto conn = m_connPool.getConn();
103
    if (adminUserExists(conn, username)) {
104
      throw exception::UserError(std::string("Cannot create admin user " + username +
105
106
        " because an admin user with the same name already exists"));
    }
107
    const uint64_t now = time(nullptr);
Steven Murray's avatar
Steven Murray committed
108
109
110
111
112
113
114
115
116
117
118
119
120
    const char *const sql =
      "INSERT INTO ADMIN_USER("
        "ADMIN_USER_NAME,"

        "USER_COMMENT,"

        "CREATION_LOG_USER_NAME,"
        "CREATION_LOG_HOST_NAME,"
        "CREATION_LOG_TIME,"

        "LAST_UPDATE_USER_NAME,"
        "LAST_UPDATE_HOST_NAME,"
        "LAST_UPDATE_TIME)"
121
      "VALUES("
Steven Murray's avatar
Steven Murray committed
122
123
124
125
126
127
128
129
        ":ADMIN_USER_NAME,"

        ":USER_COMMENT,"

        ":CREATION_LOG_USER_NAME,"
        ":CREATION_LOG_HOST_NAME,"
        ":CREATION_LOG_TIME,"

130
131
132
        ":LAST_UPDATE_USER_NAME,"
        ":LAST_UPDATE_HOST_NAME,"
        ":LAST_UPDATE_TIME)";
133
    auto stmt = conn.createStmt(sql);
Steven Murray's avatar
Steven Murray committed
134

135
    stmt.bindString(":ADMIN_USER_NAME", username);
Steven Murray's avatar
Steven Murray committed
136

137
    stmt.bindString(":USER_COMMENT", comment);
Steven Murray's avatar
Steven Murray committed
138

139
140
141
    stmt.bindString(":CREATION_LOG_USER_NAME", admin.username);
    stmt.bindString(":CREATION_LOG_HOST_NAME", admin.host);
    stmt.bindUint64(":CREATION_LOG_TIME", now);
Steven Murray's avatar
Steven Murray committed
142

143
144
145
    stmt.bindString(":LAST_UPDATE_USER_NAME", admin.username);
    stmt.bindString(":LAST_UPDATE_HOST_NAME", admin.host);
    stmt.bindUint64(":LAST_UPDATE_TIME", now);
146

147
    stmt.executeNonQuery();
148
149
150
151
  } catch(exception::UserError &) {
    throw;
  } catch(exception::Exception &ex) {
    ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
152
153
154
155
156
157
158
    throw;
  }
}

//------------------------------------------------------------------------------
// adminUserExists
//------------------------------------------------------------------------------
159
bool RdbmsCatalogue::adminUserExists(rdbms::Conn &conn, const std::string adminUsername) const {
160
161
162
163
164
165
166
  try {
    const char *const sql =
      "SELECT "
        "ADMIN_USER_NAME AS ADMIN_USER_NAME "
      "FROM "
        "ADMIN_USER "
      "WHERE "
167
        "ADMIN_USER_NAME = :ADMIN_USER_NAME";
168
    auto stmt = conn.createStmt(sql);
169
    stmt.bindString(":ADMIN_USER_NAME", adminUsername);
170
    auto rset = stmt.executeQuery();
171
    return rset.next();
172
173
  } catch(exception::UserError &) {
    throw;
174
175
176
  } catch(exception::Exception &ex) {
    ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
    throw;
Steven Murray's avatar
Steven Murray committed
177
  }
178
179
180
181
182
}

//------------------------------------------------------------------------------
// deleteAdminUser
//------------------------------------------------------------------------------
183
void RdbmsCatalogue::deleteAdminUser(const std::string &username) {
184
  try {
185
    const char *const sql = "DELETE FROM ADMIN_USER WHERE ADMIN_USER_NAME = :ADMIN_USER_NAME";
186
    auto conn = m_connPool.getConn();
187
    auto stmt = conn.createStmt(sql);
188
    stmt.bindString(":ADMIN_USER_NAME", username);
189
    stmt.executeNonQuery();
190

191
    if(0 == stmt.getNbAffectedRows()) {
192
      throw exception::UserError(std::string("Cannot delete admin-user ") + username + " because they do not exist");
193
    }
194
195
196
197
  } catch(exception::UserError &) {
    throw;
  } catch(exception::Exception &ex) {
    ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
198
199
    throw;
  }
Steven Murray's avatar
Steven Murray committed
200
}
201
202
203
204

//------------------------------------------------------------------------------
// getAdminUsers
//------------------------------------------------------------------------------
205
std::list<common::dataStructures::AdminUser> RdbmsCatalogue::getAdminUsers() const {
Steven Murray's avatar
Steven Murray committed
206
207
208
209
210
  try {
    std::list<common::dataStructures::AdminUser> admins;
    const char *const sql =
      "SELECT "
        "ADMIN_USER_NAME AS ADMIN_USER_NAME,"
211

Steven Murray's avatar
Steven Murray committed
212
        "USER_COMMENT AS USER_COMMENT,"
213

214
215
216
        "CREATION_LOG_USER_NAME AS CREATION_LOG_USER_NAME,"
        "CREATION_LOG_HOST_NAME AS CREATION_LOG_HOST_NAME,"
        "CREATION_LOG_TIME AS CREATION_LOG_TIME,"
217

218
219
220
        "LAST_UPDATE_USER_NAME AS LAST_UPDATE_USER_NAME,"
        "LAST_UPDATE_HOST_NAME AS LAST_UPDATE_HOST_NAME,"
        "LAST_UPDATE_TIME AS LAST_UPDATE_TIME "
221
      "FROM "
222
223
224
        "ADMIN_USER "
      "ORDER BY "
        "ADMIN_USER_NAME";
225
    auto conn = m_connPool.getConn();
226
    auto stmt = conn.createStmt(sql);
227
    auto rset = stmt.executeQuery();
228
    while (rset.next()) {
Steven Murray's avatar
Steven Murray committed
229
      common::dataStructures::AdminUser admin;
230

231
232
233
234
235
236
237
238
      admin.name = rset.columnString("ADMIN_USER_NAME");
      admin.comment = rset.columnString("USER_COMMENT");
      admin.creationLog.username = rset.columnString("CREATION_LOG_USER_NAME");
      admin.creationLog.host = rset.columnString("CREATION_LOG_HOST_NAME");
      admin.creationLog.time = rset.columnUint64("CREATION_LOG_TIME");
      admin.lastModificationLog.username = rset.columnString("LAST_UPDATE_USER_NAME");
      admin.lastModificationLog.host = rset.columnString("LAST_UPDATE_HOST_NAME");
      admin.lastModificationLog.time = rset.columnUint64("LAST_UPDATE_TIME");
239

Steven Murray's avatar
Steven Murray committed
240
241
      admins.push_back(admin);
    }
242

Steven Murray's avatar
Steven Murray committed
243
    return admins;
244
245
  } catch(exception::UserError &) {
    throw;
246
247
248
  } catch(exception::Exception &ex) {
    ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
    throw;
Steven Murray's avatar
Steven Murray committed
249
  }
250
251
252
253
254
}

//------------------------------------------------------------------------------
// modifyAdminUserComment
//------------------------------------------------------------------------------
Steven Murray's avatar
Steven Murray committed
255
void RdbmsCatalogue::modifyAdminUserComment(const common::dataStructures::SecurityIdentity &admin,
256
257
  const std::string &username, const std::string &comment) {
  try {
258
259
260
261
262
    if(username.empty()) {
      throw UserSpecifiedAnEmptyStringUsername("Cannot modify admin user because the username is an empty string");
    }

    if(comment.empty()) {
263
      throw UserSpecifiedAnEmptyStringComment("Cannot modify admin user because the comment is an empty string");
264
265
    }

266
267
268
269
270
271
272
    const time_t now = time(nullptr);
    const char *const sql =
      "UPDATE ADMIN_USER SET "
        "USER_COMMENT = :USER_COMMENT,"
        "LAST_UPDATE_USER_NAME = :LAST_UPDATE_USER_NAME,"
        "LAST_UPDATE_HOST_NAME = :LAST_UPDATE_HOST_NAME,"
        "LAST_UPDATE_TIME = :LAST_UPDATE_TIME "
273
      "WHERE "
274
275
        "ADMIN_USER_NAME = :ADMIN_USER_NAME";
    auto conn = m_connPool.getConn();
276
    auto stmt = conn.createStmt(sql);
277
278
279
280
281
    stmt.bindString(":USER_COMMENT", comment);
    stmt.bindString(":LAST_UPDATE_USER_NAME", admin.username);
    stmt.bindString(":LAST_UPDATE_HOST_NAME", admin.host);
    stmt.bindUint64(":LAST_UPDATE_TIME", now);
    stmt.bindString(":ADMIN_USER_NAME", username);
282
    stmt.executeNonQuery();
283
284

    if(0 == stmt.getNbAffectedRows()) {
285
286
287
288
      throw exception::UserError(std::string("Cannot modify admin user ") + username + " because they do not exist");
    }
  } catch(exception::UserError &) {
    throw;
289
290
291
  } catch(exception::Exception &ex) {
    ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
    throw;
292
  }
Steven Murray's avatar
Steven Murray committed
293
}
294
295
296
297

//------------------------------------------------------------------------------
// createStorageClass
//------------------------------------------------------------------------------
298
void RdbmsCatalogue::createStorageClass(
Steven Murray's avatar
Steven Murray committed
299
  const common::dataStructures::SecurityIdentity &admin,
300
  const common::dataStructures::StorageClass &storageClass) {
Steven Murray's avatar
Steven Murray committed
301
  try {
302
    if(storageClass.diskInstance.empty()) {
303
304
      throw UserSpecifiedAnEmptyStringDiskInstanceName("Cannot create storage class because the disk instance name is"
        " an empty string");
305
306
307
    }

    if(storageClass.name.empty()) {
308
309
      throw UserSpecifiedAnEmptyStringStorageClassName("Cannot create storage class because the storage class name is"
        " an empty string");
310
311
312
    }

    if(storageClass.comment.empty()) {
313
      throw UserSpecifiedAnEmptyStringComment("Cannot create storage class because the comment is an empty string");
314
315
    }

316
    auto conn = m_connPool.getConn();
317
    if(storageClassExists(conn, storageClass.diskInstance, storageClass.name)) {
318
319
      throw exception::UserError(std::string("Cannot create storage class ") + storageClass.diskInstance + ":" +
        storageClass.name + " because it already exists");
320
    }
321
    const uint64_t storageClassId = getNextStorageClassId(conn);
322
    const time_t now = time(nullptr);
Steven Murray's avatar
Steven Murray committed
323
324
    const char *const sql =
      "INSERT INTO STORAGE_CLASS("
325
        "STORAGE_CLASS_ID,"
326
        "DISK_INSTANCE_NAME,"
Steven Murray's avatar
Steven Murray committed
327
328
329
330
331
332
333
334
335
336
337
338
        "STORAGE_CLASS_NAME,"
        "NB_COPIES,"

        "USER_COMMENT,"

        "CREATION_LOG_USER_NAME,"
        "CREATION_LOG_HOST_NAME,"
        "CREATION_LOG_TIME,"

        "LAST_UPDATE_USER_NAME,"
        "LAST_UPDATE_HOST_NAME,"
        "LAST_UPDATE_TIME)"
339
      "VALUES("
340
        ":STORAGE_CLASS_ID,"
341
        ":DISK_INSTANCE_NAME,"
Steven Murray's avatar
Steven Murray committed
342
343
344
345
346
347
348
349
350
        ":STORAGE_CLASS_NAME,"
        ":NB_COPIES,"

        ":USER_COMMENT,"

        ":CREATION_LOG_USER_NAME,"
        ":CREATION_LOG_HOST_NAME,"
        ":CREATION_LOG_TIME,"

351
352
353
        ":LAST_UPDATE_USER_NAME,"
        ":LAST_UPDATE_HOST_NAME,"
        ":LAST_UPDATE_TIME)";
354
    auto stmt = conn.createStmt(sql);
Steven Murray's avatar
Steven Murray committed
355

356
    stmt.bindUint64(":STORAGE_CLASS_ID", storageClassId);
357
358
359
    stmt.bindString(":DISK_INSTANCE_NAME", storageClass.diskInstance);
    stmt.bindString(":STORAGE_CLASS_NAME", storageClass.name);
    stmt.bindUint64(":NB_COPIES", storageClass.nbCopies);
Steven Murray's avatar
Steven Murray committed
360

361
    stmt.bindString(":USER_COMMENT", storageClass.comment);
Steven Murray's avatar
Steven Murray committed
362

363
364
365
    stmt.bindString(":CREATION_LOG_USER_NAME", admin.username);
    stmt.bindString(":CREATION_LOG_HOST_NAME", admin.host);
    stmt.bindUint64(":CREATION_LOG_TIME", now);
Steven Murray's avatar
Steven Murray committed
366

367
368
369
    stmt.bindString(":LAST_UPDATE_USER_NAME", admin.username);
    stmt.bindString(":LAST_UPDATE_HOST_NAME", admin.host);
    stmt.bindUint64(":LAST_UPDATE_TIME", now);
370

371
    stmt.executeNonQuery();
372
  } catch(exception::UserError &) {
373
    throw;
374
375
376
  } catch(exception::Exception &ex) {
    ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
    throw;
377
378
379
380
381
382
  }
}

//------------------------------------------------------------------------------
// storageClassExists
//------------------------------------------------------------------------------
383
bool RdbmsCatalogue::storageClassExists(rdbms::Conn &conn, const std::string &diskInstanceName,
384
  const std::string &storageClassName) const {
385
386
387
  try {
    const char *const sql =
      "SELECT "
388
        "DISK_INSTANCE_NAME AS DISK_INSTANCE_NAME, "
389
390
391
392
        "STORAGE_CLASS_NAME AS STORAGE_CLASS_NAME "
      "FROM "
        "STORAGE_CLASS "
      "WHERE "
393
        "DISK_INSTANCE_NAME = :DISK_INSTANCE_NAME AND "
394
        "STORAGE_CLASS_NAME = :STORAGE_CLASS_NAME";
395
    auto stmt = conn.createStmt(sql);
396
397
    stmt.bindString(":DISK_INSTANCE_NAME", diskInstanceName);
    stmt.bindString(":STORAGE_CLASS_NAME", storageClassName);
398
    auto rset = stmt.executeQuery();
399
    return rset.next();
400
401
  } catch(exception::UserError &) {
    throw;
402
403
404
  } catch(exception::Exception &ex) {
    ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
    throw;
Steven Murray's avatar
Steven Murray committed
405
  }
406
407
408
409
410
}

//------------------------------------------------------------------------------
// deleteStorageClass
//------------------------------------------------------------------------------
411
void RdbmsCatalogue::deleteStorageClass(const std::string &diskInstanceName, const std::string &storageClassName) {
412
  try {
413
414
415
416
417
418
419
420
421
422
423
424
    auto conn = m_connPool.getConn();

    if(storageClassIsUsedByArchiveRoutes(conn, storageClassName)) {
      throw UserSpecifiedStorageClassUsedByArchiveRoutes(std::string("The ") + storageClassName +
        " storage class is being used by one or more archive routes");
    }

    if(storageClassIsUsedByArchiveFiles(conn, storageClassName)) {
      throw UserSpecifiedStorageClassUsedByArchiveFiles(std::string("The ") + storageClassName +
        " storage class is being used by one or more archive files");
    }

425
426
427
428
    const char *const sql =
      "DELETE FROM "
        "STORAGE_CLASS "
      "WHERE "
429
        "DISK_INSTANCE_NAME = :DISK_INSTANCE_NAME AND "
430
        "STORAGE_CLASS_NAME = :STORAGE_CLASS_NAME";
431
    auto stmt = conn.createStmt(sql);
432

433
434
    stmt.bindString(":DISK_INSTANCE_NAME", diskInstanceName);
    stmt.bindString(":STORAGE_CLASS_NAME", storageClassName);
435

436
    stmt.executeNonQuery();
437
    if(0 == stmt.getNbAffectedRows()) {
438
439
      throw exception::UserError(std::string("Cannot delete storage-class ") + diskInstanceName + ":" +
        storageClassName + " because it does not exist");
440
    }
441
  } catch(exception::UserError &) {
442
    throw;
443
444
445
  } catch(exception::Exception &ex) {
    ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
    throw;
446
  }
Steven Murray's avatar
Steven Murray committed
447
}
448
449

//------------------------------------------------------------------------------
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
// storageClassIsUsedByArchiveRoutes
//------------------------------------------------------------------------------
bool RdbmsCatalogue::storageClassIsUsedByArchiveRoutes(rdbms::Conn &conn, const std::string &storageClassName) const {
  try {
    const char *const sql =
      "SELECT "
        "STORAGE_CLASS.STORAGE_CLASS_NAME AS STORAGE_CLASS_NAME "
      "FROM "
        "ARCHIVE_ROUTE "
      "INNER JOIN "
        "STORAGE_CLASS "
      "ON "
        "ARCHIVE_ROUTE.STORAGE_CLASS_ID = STORAGE_CLASS.STORAGE_CLASS_ID "
      "WHERE "
        "STORAGE_CLASS_NAME = :STORAGE_CLASS_NAME";
    auto stmt = conn.createStmt(sql);
    stmt.bindString(":STORAGE_CLASS_NAME", storageClassName);
    auto rset = stmt.executeQuery();
    return rset.next();
  } catch(exception::UserError &) {
    throw;
  } catch(exception::Exception &ex) {
    ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
    throw;
  }
}

//------------------------------------------------------------------------------
// storageClassIsUsedByARchiveFiles
//------------------------------------------------------------------------------
bool RdbmsCatalogue::storageClassIsUsedByArchiveFiles(rdbms::Conn &conn, const std::string &storageClassName) const {
  try {
    const char *const sql =
      "SELECT "
        "STORAGE_CLASS.STORAGE_CLASS_NAME AS STORAGE_CLASS_NAME "
      "FROM "
        "ARCHIVE_FILE "
      "INNER JOIN "
        "STORAGE_CLASS "
      "ON "
        "ARCHIVE_FILE.STORAGE_CLASS_ID = STORAGE_CLASS.STORAGE_CLASS_ID "
      "WHERE "
        "STORAGE_CLASS_NAME = :STORAGE_CLASS_NAME";
    auto stmt = conn.createStmt(sql);
    stmt.bindString(":STORAGE_CLASS_NAME", storageClassName);
    auto rset = stmt.executeQuery();
    return rset.next();
  } catch(exception::UserError &) {
    throw;
  } catch(exception::Exception &ex) {
    ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
    throw;
  }
}

//------------------------------------------------------------------------------
506
507
// getStorageClasses
//------------------------------------------------------------------------------
508
std::list<common::dataStructures::StorageClass> RdbmsCatalogue::getStorageClasses() const {
Steven Murray's avatar
Steven Murray committed
509
510
511
512
  try {
    std::list<common::dataStructures::StorageClass> storageClasses;
    const char *const sql =
      "SELECT "
513
        "DISK_INSTANCE_NAME AS DISK_INSTANCE_NAME,"
Steven Murray's avatar
Steven Murray committed
514
        "STORAGE_CLASS_NAME AS STORAGE_CLASS_NAME,"
515
        "NB_COPIES AS NB_COPIES,"
Steven Murray's avatar
Steven Murray committed
516
517
518

        "USER_COMMENT AS USER_COMMENT,"

519
520
521
        "CREATION_LOG_USER_NAME AS CREATION_LOG_USER_NAME,"
        "CREATION_LOG_HOST_NAME AS CREATION_LOG_HOST_NAME,"
        "CREATION_LOG_TIME AS CREATION_LOG_TIME,"
Steven Murray's avatar
Steven Murray committed
522

523
524
525
        "LAST_UPDATE_USER_NAME AS LAST_UPDATE_USER_NAME,"
        "LAST_UPDATE_HOST_NAME AS LAST_UPDATE_HOST_NAME,"
        "LAST_UPDATE_TIME AS LAST_UPDATE_TIME "
526
      "FROM "
527
528
529
        "STORAGE_CLASS "
      "ORDER BY "
        "DISK_INSTANCE_NAME, STORAGE_CLASS_NAME";
530
    auto conn = m_connPool.getConn();
531
    auto stmt = conn.createStmt(sql);
532
    auto rset = stmt.executeQuery();
533
    while (rset.next()) {
Steven Murray's avatar
Steven Murray committed
534
535
      common::dataStructures::StorageClass storageClass;

536
537
538
539
540
541
542
543
544
545
      storageClass.diskInstance = rset.columnString("DISK_INSTANCE_NAME");
      storageClass.name = rset.columnString("STORAGE_CLASS_NAME");
      storageClass.nbCopies = rset.columnUint64("NB_COPIES");
      storageClass.comment = rset.columnString("USER_COMMENT");
      storageClass.creationLog.username = rset.columnString("CREATION_LOG_USER_NAME");
      storageClass.creationLog.host = rset.columnString("CREATION_LOG_HOST_NAME");
      storageClass.creationLog.time = rset.columnUint64("CREATION_LOG_TIME");
      storageClass.lastModificationLog.username = rset.columnString("LAST_UPDATE_USER_NAME");
      storageClass.lastModificationLog.host = rset.columnString("LAST_UPDATE_HOST_NAME");
      storageClass.lastModificationLog.time = rset.columnUint64("LAST_UPDATE_TIME");
Steven Murray's avatar
Steven Murray committed
546
547

      storageClasses.push_back(storageClass);
548
549
    }

Steven Murray's avatar
Steven Murray committed
550
    return storageClasses;
551
552
  } catch(exception::UserError &) {
    throw;
553
554
555
  } catch(exception::Exception &ex) {
    ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
    throw;
556
557
558
559
560
561
  }
}

//------------------------------------------------------------------------------
// modifyStorageClassNbCopies
//------------------------------------------------------------------------------
Steven Murray's avatar
Steven Murray committed
562
void RdbmsCatalogue::modifyStorageClassNbCopies(const common::dataStructures::SecurityIdentity &admin,
563
564
565
566
567
568
569
570
571
  const std::string &instanceName, const std::string &name, const uint64_t nbCopies) {
  try {
    const time_t now = time(nullptr);
    const char *const sql =
      "UPDATE STORAGE_CLASS SET "
        "NB_COPIES = :NB_COPIES,"
        "LAST_UPDATE_USER_NAME = :LAST_UPDATE_USER_NAME,"
        "LAST_UPDATE_HOST_NAME = :LAST_UPDATE_HOST_NAME,"
        "LAST_UPDATE_TIME = :LAST_UPDATE_TIME "
572
      "WHERE "
573
574
575
        "DISK_INSTANCE_NAME = :DISK_INSTANCE_NAME AND "
        "STORAGE_CLASS_NAME = :STORAGE_CLASS_NAME";
    auto conn = m_connPool.getConn();
576
    auto stmt = conn.createStmt(sql);
577
578
579
580
581
582
    stmt.bindUint64(":NB_COPIES", nbCopies);
    stmt.bindString(":LAST_UPDATE_USER_NAME", admin.username);
    stmt.bindString(":LAST_UPDATE_HOST_NAME", admin.host);
    stmt.bindUint64(":LAST_UPDATE_TIME", now);
    stmt.bindString(":DISK_INSTANCE_NAME", instanceName);
    stmt.bindString(":STORAGE_CLASS_NAME", name);
583
    stmt.executeNonQuery();
584
585

    if(0 == stmt.getNbAffectedRows()) {
586
587
588
589
590
      throw exception::UserError(std::string("Cannot modify storage class ") + instanceName + ":" + name +
        " because it does not exist");
    }
  } catch(exception::UserError &) {
    throw;
591
592
593
  } catch(exception::Exception &ex) {
    ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
    throw;
594
  }
Steven Murray's avatar
Steven Murray committed
595
}
596
597
598
599

//------------------------------------------------------------------------------
// modifyStorageClassComment
//------------------------------------------------------------------------------
Steven Murray's avatar
Steven Murray committed
600
void RdbmsCatalogue::modifyStorageClassComment(const common::dataStructures::SecurityIdentity &admin,
601
602
603
604
605
606
607
608
609
  const std::string &instanceName, const std::string &name, const std::string &comment) {
  try {
    const time_t now = time(nullptr);
    const char *const sql =
      "UPDATE STORAGE_CLASS SET "
        "USER_COMMENT = :USER_COMMENT,"
        "LAST_UPDATE_USER_NAME = :LAST_UPDATE_USER_NAME,"
        "LAST_UPDATE_HOST_NAME = :LAST_UPDATE_HOST_NAME,"
        "LAST_UPDATE_TIME = :LAST_UPDATE_TIME "
610
      "WHERE "
611
612
613
        "DISK_INSTANCE_NAME = :DISK_INSTANCE_NAME AND "
        "STORAGE_CLASS_NAME = :STORAGE_CLASS_NAME";
    auto conn = m_connPool.getConn();
614
    auto stmt = conn.createStmt(sql);
615
616
617
618
619
620
    stmt.bindString(":USER_COMMENT", comment);
    stmt.bindString(":LAST_UPDATE_USER_NAME", admin.username);
    stmt.bindString(":LAST_UPDATE_HOST_NAME", admin.host);
    stmt.bindUint64(":LAST_UPDATE_TIME", now);
    stmt.bindString(":DISK_INSTANCE_NAME", instanceName);
    stmt.bindString(":STORAGE_CLASS_NAME", name);
621
    stmt.executeNonQuery();
622
623

    if(0 == stmt.getNbAffectedRows()) {
624
625
626
627
628
      throw exception::UserError(std::string("Cannot modify storage class ") + instanceName + ":" + name +
        " because it does not exist");
    }
  } catch(exception::UserError &) {
    throw;
629
  } catch(exception::Exception &ex) {
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
    ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
    throw;
  }
}

//------------------------------------------------------------------------------
// modifyStorageClassName
//------------------------------------------------------------------------------
void RdbmsCatalogue::modifyStorageClassName(const common::dataStructures::SecurityIdentity &admin,
  const std::string &instanceName, const std::string &currentName, const std::string &newName) {
  try {
    const time_t now = time(nullptr);
    const char *const sql =
      "UPDATE STORAGE_CLASS SET "
        "STORAGE_CLASS_NAME = :NEW_STORAGE_CLASS_NAME,"
        "LAST_UPDATE_USER_NAME = :LAST_UPDATE_USER_NAME,"
        "LAST_UPDATE_HOST_NAME = :LAST_UPDATE_HOST_NAME,"
        "LAST_UPDATE_TIME = :LAST_UPDATE_TIME "
      "WHERE "
        "DISK_INSTANCE_NAME = :DISK_INSTANCE_NAME AND "
        "STORAGE_CLASS_NAME = :CURRENT_STORAGE_CLASS_NAME";
    auto conn = m_connPool.getConn();
    auto stmt = conn.createStmt(sql);
    stmt.bindString(":NEW_STORAGE_CLASS_NAME", newName);
    stmt.bindString(":LAST_UPDATE_USER_NAME", admin.username);
    stmt.bindString(":LAST_UPDATE_HOST_NAME", admin.host);
    stmt.bindUint64(":LAST_UPDATE_TIME", now);
    stmt.bindString(":DISK_INSTANCE_NAME", instanceName);
    stmt.bindString(":CURRENT_STORAGE_CLASS_NAME", currentName);
    stmt.executeNonQuery();

    if(0 == stmt.getNbAffectedRows()) {
      throw exception::UserError(std::string("Cannot modify storage class ") + instanceName + ":" + currentName +
        " because it does not exist");
    }
  } catch(exception::UserError &) {
    throw;
  } catch(exception::Exception &ex) {
668
669
    ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
    throw;
670
  }
Steven Murray's avatar
Steven Murray committed
671
}
672
673
674
675

//------------------------------------------------------------------------------
// createTapePool
//------------------------------------------------------------------------------
676
void RdbmsCatalogue::createTapePool(
Steven Murray's avatar
Steven Murray committed
677
  const common::dataStructures::SecurityIdentity &admin,
678
  const std::string &name,
679
  const std::string &vo,
680
681
  const uint64_t nbPartialTapes,
  const bool encryptionValue,
682
  const cta::optional<std::string> &supply,
683
  const std::string &comment) {
Steven Murray's avatar
Steven Murray committed
684
  try {
685
686
687
688
689
    if(name.empty()) {
      throw UserSpecifiedAnEmptyStringTapePoolName("Cannot create tape pool because the tape pool name is an empty string");
    }

    if(vo.empty()) {
690
      throw UserSpecifiedAnEmptyStringVo("Cannot create tape pool because the VO is an empty string");
691
692
693
    }

    if(comment.empty()) {
694
      throw UserSpecifiedAnEmptyStringComment("Cannot create tape pool because the comment is an empty string");
695
696
    }

697
    auto conn = m_connPool.getConn();
698

699
    if(tapePoolExists(conn, name)) {
700
      throw exception::UserError(std::string("Cannot create tape pool ") + name +
701
702
        " because a tape pool with the same name already exists");
    }
703
    const time_t now = time(nullptr);
Steven Murray's avatar
Steven Murray committed
704
705
706
    const char *const sql =
      "INSERT INTO TAPE_POOL("
        "TAPE_POOL_NAME,"
707
        "VO,"
Steven Murray's avatar
Steven Murray committed
708
709
        "NB_PARTIAL_TAPES,"
        "IS_ENCRYPTED,"
710
        "SUPPLY,"
Steven Murray's avatar
Steven Murray committed
711
712
713
714
715
716
717
718
719
720

        "USER_COMMENT,"

        "CREATION_LOG_USER_NAME,"
        "CREATION_LOG_HOST_NAME,"
        "CREATION_LOG_TIME,"

        "LAST_UPDATE_USER_NAME,"
        "LAST_UPDATE_HOST_NAME,"
        "LAST_UPDATE_TIME)"
721
      "VALUES("
Steven Murray's avatar
Steven Murray committed
722
        ":TAPE_POOL_NAME,"
723
        ":VO,"
Steven Murray's avatar
Steven Murray committed
724
725
        ":NB_PARTIAL_TAPES,"
        ":IS_ENCRYPTED,"
726
        ":SUPPLY,"
Steven Murray's avatar
Steven Murray committed
727
728
729
730
731
732
733

        ":USER_COMMENT,"

        ":CREATION_LOG_USER_NAME,"
        ":CREATION_LOG_HOST_NAME,"
        ":CREATION_LOG_TIME,"

734
735
736
        ":LAST_UPDATE_USER_NAME,"
        ":LAST_UPDATE_HOST_NAME,"
        ":LAST_UPDATE_TIME)";
737
    auto stmt = conn.createStmt(sql);
Steven Murray's avatar
Steven Murray committed
738

739
    stmt.bindString(":TAPE_POOL_NAME", name);
740
    stmt.bindString(":VO", vo);
741
742
    stmt.bindUint64(":NB_PARTIAL_TAPES", nbPartialTapes);
    stmt.bindBool(":IS_ENCRYPTED", encryptionValue);
743
    stmt.bindOptionalString(":SUPPLY", supply);
Steven Murray's avatar
Steven Murray committed
744

745
    stmt.bindString(":USER_COMMENT", comment);
Steven Murray's avatar
Steven Murray committed
746

747
748
749
    stmt.bindString(":CREATION_LOG_USER_NAME", admin.username);
    stmt.bindString(":CREATION_LOG_HOST_NAME", admin.host);
    stmt.bindUint64(":CREATION_LOG_TIME", now);
Steven Murray's avatar
Steven Murray committed
750

751
752
753
    stmt.bindString(":LAST_UPDATE_USER_NAME", admin.username);
    stmt.bindString(":LAST_UPDATE_HOST_NAME", admin.host);
    stmt.bindUint64(":LAST_UPDATE_TIME", now);
754

755
    stmt.executeNonQuery();
756
  } catch(exception::UserError &) {
757
    throw;
758
759
760
  } catch(exception::Exception &ex) {
    ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
    throw;
761
762
763
764
765
766
767
768
769
770
  }
}

//------------------------------------------------------------------------------
// tapePoolExists
//------------------------------------------------------------------------------
bool RdbmsCatalogue::tapePoolExists(const std::string &tapePoolName) const {
  try {
    auto conn = m_connPool.getConn();
    return tapePoolExists(conn, tapePoolName);
771
772
  } catch(exception::UserError &) {
    throw;
773
774
775
  } catch(exception::Exception &ex) {
    ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
    throw;
Steven Murray's avatar
Steven Murray committed
776
  }
777
778
}

779
780
781
//------------------------------------------------------------------------------
// tapePoolExists
//------------------------------------------------------------------------------
782
bool RdbmsCatalogue::tapePoolExists(rdbms::Conn &conn, const std::string &tapePoolName) const {
783
784
785
786
787
788
789
  try {
    const char *const sql =
      "SELECT "
        "TAPE_POOL_NAME AS TAPE_POOL_NAME "
      "FROM "
        "TAPE_POOL "
      "WHERE "
790
        "TAPE_POOL_NAME = :TAPE_POOL_NAME";
791
    auto stmt = conn.createStmt(sql);
792
    stmt.bindString(":TAPE_POOL_NAME", tapePoolName);
793
    auto rset = stmt.executeQuery();
794
    return rset.next();
795
796
  } catch(exception::UserError &) {
    throw;
797
798
799
  } catch(exception::Exception &ex) {
    ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
    throw;
800
801
802
  }
}

803
804
805
//------------------------------------------------------------------------------
// archiveFileExists
//------------------------------------------------------------------------------
806
bool RdbmsCatalogue::archiveFileIdExists(rdbms::Conn &conn, const uint64_t archiveFileId) const {
807
808
809
810
811
812
813
814
  try {
    const char *const sql =
      "SELECT "
        "ARCHIVE_FILE_ID AS ARCHIVE_FILE_ID "
      "FROM "
        "ARCHIVE_FILE "
      "WHERE "
        "ARCHIVE_FILE_ID = :ARCHIVE_FILE_ID";
815
    auto stmt = conn.createStmt(sql);
816
    stmt.bindUint64(":ARCHIVE_FILE_ID", archiveFileId);
817
    auto rset = stmt.executeQuery();
818
    return rset.next();
819
820
  } catch(exception::UserError &) {
    throw;
821
822
823
  } catch(exception::Exception &ex) {
    ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
    throw;
824
825
826
  }
}

827
828
829
//------------------------------------------------------------------------------
// diskFileIdExists
//------------------------------------------------------------------------------
830
bool RdbmsCatalogue::diskFileIdExists(rdbms::Conn &conn, const std::string &diskInstanceName,
831
832
833
834
835
836
837
838
839
840
841
  const std::string &diskFileId) const {
  try {
    const char *const sql =
      "SELECT "
        "DISK_INSTANCE_NAME AS DISK_INSTANCE_NAME, "
        "DISK_FILE_ID AS DISK_FILE_ID "
      "FROM "
        "ARCHIVE_FILE "
      "WHERE "
        "DISK_INSTANCE_NAME = :DISK_INSTANCE_NAME AND "
        "DISK_FILE_ID = :DISK_FILE_ID";
842
    auto stmt = conn.createStmt(sql);
843
844
    stmt.bindString(":DISK_INSTANCE_NAME", diskInstanceName);
    stmt.bindString(":DISK_FILE_ID", diskFileId);
845
    auto rset = stmt.executeQuery();
846
    return rset.next();
847
848
  } catch(exception::UserError &) {
    throw;
849
850
851
  } catch(exception::Exception &ex) {
    ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
    throw;
852
853
854
  }
}

855
856
857
//------------------------------------------------------------------------------
// diskFilePathExists
//------------------------------------------------------------------------------
858
bool RdbmsCatalogue::diskFilePathExists(rdbms::Conn &conn, const std::string &diskInstanceName,
859
860
861
862
863
864
865
866
867
868
869
  const std::string &diskFilePath) const {
  try {
    const char *const sql =
      "SELECT "
        "DISK_INSTANCE_NAME AS DISK_INSTANCE_NAME, "
        "DISK_FILE_PATH AS DISK_FILE_PATH "
      "FROM "
        "ARCHIVE_FILE "
      "WHERE "
        "DISK_INSTANCE_NAME = :DISK_INSTANCE_NAME AND "
        "DISK_FILE_PATH = :DISK_FILE_PATH";
870
    auto stmt = conn.createStmt(sql);
871
872
    stmt.bindString(":DISK_INSTANCE_NAME", diskInstanceName);
    stmt.bindString(":DISK_FILE_PATH", diskFilePath);
873
    auto rset = stmt.executeQuery();
874
    return rset.next();
875
876
  } catch(exception::UserError &) {
    throw;
877
878
879
  } catch(exception::Exception &ex) {
    ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
    throw;
880
881
882
  }
}

883
884
885
//------------------------------------------------------------------------------
// diskFileUserExists
//------------------------------------------------------------------------------
886
bool RdbmsCatalogue::diskFileUserExists(rdbms::Conn &conn, const std::string &diskInstanceName,
887
  uint32_t diskFileOwnerUid) const {
888
889
890
891
  try {
    const char *const sql =
      "SELECT "
        "DISK_INSTANCE_NAME AS DISK_INSTANCE_NAME, "
892
        "DISK_FILE_UID AS DISK_FILE_UID "
893
894
895
896
      "FROM "
        "ARCHIVE_FILE "
      "WHERE "
        "DISK_INSTANCE_NAME = :DISK_INSTANCE_NAME AND "
897
        "DISK_FILE_UID = :DISK_FILE_UID";
898
    auto stmt = conn.createStmt(sql);
899
    stmt.bindString(":DISK_INSTANCE_NAME", diskInstanceName);
900
    stmt.bindUint64(":DISK_FILE_UID", diskFileOwnerUid);
901
    auto rset = stmt.executeQuery();
902
    return rset.next();
903
904
  } catch(exception::UserError &) {
    throw;
905
906
907
  } catch(exception::Exception &ex) {
    ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
    throw;
908
909
910
  }
}

911
912
913
//------------------------------------------------------------------------------
// diskFileGroupExists
//------------------------------------------------------------------------------
914
bool RdbmsCatalogue::diskFileGroupExists(rdbms::Conn &conn, const std::string &diskInstanceName,
915
  uint32_t diskFileGid) const {
916
917
918
919
  try {
    const char *const sql =
      "SELECT "
        "DISK_INSTANCE_NAME AS DISK_INSTANCE_NAME, "
920
        "DISK_FILE_GID AS DISK_FILE_GID "
921
922
923
924
      "FROM "
        "ARCHIVE_FILE "
      "WHERE "
        "DISK_INSTANCE_NAME = :DISK_INSTANCE_NAME AND "
925
        "DISK_FILE_GID = :DISK_FILE_GID";
926
    auto stmt = conn.createStmt(sql);
927
    stmt.bindString(":DISK_INSTANCE_NAME", diskInstanceName);
928
    stmt.bindUint64(":DISK_FILE_GID", diskFileGid);
929
    auto rset = stmt.executeQuery();
930
    return rset.next();
931
932
  } catch(exception::UserError &) {
    throw;
933
934
935
  } catch(exception::Exception &ex) {
    ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
    throw;
936
937
938
  }
}

939
940
941
//------------------------------------------------------------------------------
// archiveRouteExists
//------------------------------------------------------------------------------
942
bool RdbmsCatalogue::archiveRouteExists(rdbms::Conn &conn, const std::string &diskInstanceName,
943
  const std::string &storageClassName, const uint32_t copyNb) const {
944
945
946
  try {
    const char *const sql =
      "SELECT "
947
948
        "ARCHIVE_ROUTE.STORAGE_CLASS_ID AS STORAGE_CLASS_ID,"
        "ARCHIVE_ROUTE.COPY_NB AS COPY_NB "
949
950
      "FROM "
        "ARCHIVE_ROUTE "
951
952
      "INNER JOIN STORAGE_CLASS ON "
        "ARCHIVE_ROUTE.STORAGE_CLASS_ID = STORAGE_CLASS.STORAGE_CLASS_ID "
953
      "WHERE "
954
955
956
        "STORAGE_CLASS.DISK_INSTANCE_NAME = :DISK_INSTANCE_NAME AND "
        "STORAGE_CLASS.STORAGE_CLASS_NAME = :STORAGE_CLASS_NAME AND "
        "ARCHIVE_ROUTE.COPY_NB = :COPY_NB";
957
    auto stmt = conn.createStmt(sql);
958
959
960
    stmt.bindString(":DISK_INSTANCE_NAME", diskInstanceName);
    stmt.bindString(":STORAGE_CLASS_NAME", storageClassName);
    stmt.bindUint64(":COPY_NB", copyNb);
961
    auto rset = stmt.executeQuery();
962
    return rset.next();
963
964
  } catch(exception::UserError &) {
    throw;
965
966
967
  } catch(exception::Exception &ex) {
    ex.getMessage().str(std::string(__FUNCTION__) + ": " + ex.getMessage().str());
    throw;
968
969
970
  }
}

971
972
973
//------------------------------------------------------------------------------
// deleteTapePool
//------------------------------------------------------------------------------
974
void RdbmsCatalogue::deleteTapePool(const std::string &name) {