| Directory: | cvmfs/ |
|---|---|
| File: | cvmfs/reflog_sql.cc |
| Date: | 2025-11-30 02:35:17 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 72 | 74 | 97.3% |
| Branches: | 66 | 142 | 46.5% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /** | ||
| 2 | * This file is part of the CernVM File System. | ||
| 3 | */ | ||
| 4 | |||
| 5 | #include "reflog_sql.h" | ||
| 6 | |||
| 7 | #include <cassert> | ||
| 8 | #include <limits> | ||
| 9 | |||
| 10 | #include "util/string.h" | ||
| 11 | |||
| 12 | const float ReflogDatabase::kLatestSchema = 1.0; | ||
| 13 | const float ReflogDatabase::kLatestSupportedSchema = 1.0; | ||
| 14 | const unsigned ReflogDatabase::kLatestSchemaRevision = 0; | ||
| 15 | |||
| 16 | /** | ||
| 17 | * Database Schema ChangeLog: | ||
| 18 | * | ||
| 19 | * Schema Version 1.0 | ||
| 20 | * -> Revision 0: initial revision | ||
| 21 | */ | ||
| 22 | |||
| 23 | |||
| 24 | const std::string ReflogDatabase::kFqrnKey = "fqrn"; | ||
| 25 | |||
| 26 | 414 | bool ReflogDatabase::CreateEmptyDatabase() { | |
| 27 |
3/6✓ Branch 2 taken 414 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 414 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 414 times.
✗ Branch 9 not taken.
|
828 | return sqlite::Sql(sqlite_db(), |
| 28 | "CREATE TABLE refs (hash TEXT, type INTEGER, " | ||
| 29 | "timestamp INTEGER, " | ||
| 30 | "CONSTRAINT pk_refs PRIMARY KEY (hash));") | ||
| 31 |
1/2✓ Branch 1 taken 414 times.
✗ Branch 2 not taken.
|
828 | .Execute(); |
| 32 | } | ||
| 33 | |||
| 34 | |||
| 35 | 127 | bool ReflogDatabase::CheckSchemaCompatibility() { | |
| 36 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 127 times.
|
127 | assert(IsEqualSchema(schema_version(), kLatestSupportedSchema)); |
| 37 | 127 | return true; // only one schema version at the moment | |
| 38 | } | ||
| 39 | |||
| 40 | |||
| 41 | 127 | bool ReflogDatabase::LiveSchemaUpgradeIfNecessary() { | |
| 42 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 127 times.
|
127 | assert(schema_revision() == kLatestSchemaRevision); |
| 43 | 127 | return true; // only one schema revision at the moment, i.e. no migration... | |
| 44 | } | ||
| 45 | |||
| 46 | |||
| 47 | 414 | bool ReflogDatabase::InsertInitialValues(const std::string &repo_name) { | |
| 48 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 414 times.
|
414 | assert(read_write()); |
| 49 |
1/2✓ Branch 2 taken 414 times.
✗ Branch 3 not taken.
|
414 | return this->SetProperty(kFqrnKey, repo_name); |
| 50 | } | ||
| 51 | |||
| 52 | |||
| 53 | //------------------------------------------------------------------------------ | ||
| 54 | |||
| 55 | #define DB_FIELDS_V1R0 "hash, type, timestamp" | ||
| 56 | #define DB_PLACEHOLDERS ":hash, :type, :timestamp" | ||
| 57 | |||
| 58 | #define MAKE_STATEMENT(STMT_TMPL, REV) \ | ||
| 59 | static const std::string REV = ReplaceAll( \ | ||
| 60 | ReplaceAll(STMT_TMPL, "@DB_FIELDS@", DB_FIELDS_##REV), \ | ||
| 61 | "@DB_PLACEHOLDERS@", DB_PLACEHOLDERS) | ||
| 62 | |||
| 63 | #define MAKE_STATEMENTS(STMT_TMPL) MAKE_STATEMENT(STMT_TMPL, V1R0) | ||
| 64 | |||
| 65 | #define DEFERRED_INIT(DB, REV) DeferredInit((DB)->sqlite_db(), (REV).c_str()) | ||
| 66 | |||
| 67 | #define DEFERRED_INITS(DB) DEFERRED_INIT((DB), V1R0) | ||
| 68 | |||
| 69 | |||
| 70 | 4907 | shash::Suffix SqlReflog::ToSuffix(const ReferenceType type) { | |
| 71 |
4/5✓ Branch 0 taken 4773 times.
✓ Branch 1 taken 49 times.
✓ Branch 2 taken 23 times.
✓ Branch 3 taken 62 times.
✗ Branch 4 not taken.
|
4907 | switch (type) { |
| 72 | 4773 | case kRefCatalog: | |
| 73 | 4773 | return shash::kSuffixCatalog; | |
| 74 | 49 | case kRefCertificate: | |
| 75 | 49 | return shash::kSuffixCertificate; | |
| 76 | 23 | case kRefHistory: | |
| 77 | 23 | return shash::kSuffixHistory; | |
| 78 | 62 | case kRefMetainfo: | |
| 79 | 62 | return shash::kSuffixMetainfo; | |
| 80 | ✗ | default: | |
| 81 | ✗ | assert(false && "unknown reference type"); | |
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | //------------------------------------------------------------------------------ | ||
| 87 | |||
| 88 | |||
| 89 | 541 | SqlInsertReference::SqlInsertReference(const ReflogDatabase *database) { | |
| 90 |
10/20✓ Branch 0 taken 29 times.
✓ Branch 1 taken 512 times.
✓ Branch 3 taken 29 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 29 times.
✗ Branch 8 not taken.
✓ Branch 11 taken 29 times.
✗ Branch 12 not taken.
✓ Branch 15 taken 29 times.
✗ Branch 16 not taken.
✓ Branch 19 taken 29 times.
✗ Branch 20 not taken.
✓ Branch 23 taken 29 times.
✗ Branch 24 not taken.
✓ Branch 26 taken 29 times.
✗ Branch 27 not taken.
✓ Branch 29 taken 29 times.
✗ Branch 30 not taken.
✗ Branch 55 not taken.
✗ Branch 56 not taken.
|
541 | MAKE_STATEMENTS("INSERT OR REPLACE INTO refs (@DB_FIELDS@) " |
| 91 | "VALUES (@DB_PLACEHOLDERS@);"); | ||
| 92 |
2/4✓ Branch 2 taken 541 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 541 times.
✗ Branch 6 not taken.
|
541 | DEFERRED_INITS(database); |
| 93 | 541 | } | |
| 94 | |||
| 95 | 364 | bool SqlInsertReference::BindReference(const shash::Any &reference_hash, | |
| 96 | const ReferenceType type) { | ||
| 97 |
0/2✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
364 | return BindTextTransient(1, reference_hash.ToString()) |
| 98 |
2/4✓ Branch 1 taken 364 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 364 times.
✗ Branch 4 not taken.
|
364 | && BindInt64(2, static_cast<uint64_t>(type)) |
| 99 |
6/15✓ Branch 1 taken 364 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 364 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 364 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 364 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 364 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 364 times.
✗ Branch 15 not taken.
|
1092 | && BindInt64(3, static_cast<uint64_t>(time(NULL))); |
| 100 | } | ||
| 101 | |||
| 102 | |||
| 103 | //------------------------------------------------------------------------------ | ||
| 104 | |||
| 105 | |||
| 106 | 541 | SqlCountReferences::SqlCountReferences(const ReflogDatabase *database) { | |
| 107 |
2/4✓ Branch 1 taken 541 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 541 times.
✗ Branch 5 not taken.
|
541 | DeferredInit(database->sqlite_db(), "SELECT count(*) as count FROM refs;"); |
| 108 | 541 | } | |
| 109 | |||
| 110 | 104 | uint64_t SqlCountReferences::RetrieveCount() { | |
| 111 | 104 | return static_cast<uint64_t>(RetrieveInt64(0)); | |
| 112 | } | ||
| 113 | |||
| 114 | |||
| 115 | //------------------------------------------------------------------------------ | ||
| 116 | |||
| 117 | |||
| 118 | 541 | SqlListReferences::SqlListReferences(const ReflogDatabase *database) { | |
| 119 |
2/4✓ Branch 1 taken 541 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 541 times.
✗ Branch 5 not taken.
|
541 | DeferredInit(database->sqlite_db(), "SELECT hash, type FROM refs " |
| 120 | "WHERE type = :type AND " | ||
| 121 | "timestamp < :timestamp " | ||
| 122 | "ORDER BY timestamp DESC;"); | ||
| 123 | 541 | } | |
| 124 | |||
| 125 | 91 | bool SqlListReferences::BindType(const ReferenceType type) { | |
| 126 | 91 | return BindInt64(1, static_cast<uint64_t>(type)); | |
| 127 | } | ||
| 128 | |||
| 129 | 91 | bool SqlListReferences::BindOlderThan(const uint64_t timestamp) { | |
| 130 | 91 | int64_t sqlite_timestamp = static_cast<uint64_t>(timestamp); | |
| 131 |
2/2✓ Branch 0 taken 65 times.
✓ Branch 1 taken 26 times.
|
91 | if (sqlite_timestamp < 0) { |
| 132 | 65 | sqlite_timestamp = std::numeric_limits<int64_t>::max(); | |
| 133 | } | ||
| 134 | 91 | return BindInt64(2, sqlite_timestamp); | |
| 135 | } | ||
| 136 | |||
| 137 | 169 | shash::Any SqlListReferences::RetrieveHash() const { | |
| 138 | 169 | const ReferenceType type = static_cast<ReferenceType>(RetrieveInt64(1)); | |
| 139 | 169 | const shash::Suffix suffix = ToSuffix(type); | |
| 140 |
1/2✓ Branch 3 taken 169 times.
✗ Branch 4 not taken.
|
169 | return shash::MkFromHexPtr(shash::HexPtr(RetrieveString(0)), suffix); |
| 141 | } | ||
| 142 | |||
| 143 | |||
| 144 | //------------------------------------------------------------------------------ | ||
| 145 | |||
| 146 | |||
| 147 | 541 | SqlRemoveReference::SqlRemoveReference(const ReflogDatabase *database) { | |
| 148 |
2/4✓ Branch 1 taken 541 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 541 times.
✗ Branch 5 not taken.
|
541 | DeferredInit(database->sqlite_db(), "DELETE FROM refs WHERE hash = :hash " |
| 149 | "AND type = :type;"); | ||
| 150 | 541 | } | |
| 151 | |||
| 152 | 52 | bool SqlRemoveReference::BindReference(const shash::Any &reference_hash, | |
| 153 | const ReferenceType type) { | ||
| 154 |
0/2✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
52 | return BindTextTransient(1, reference_hash.ToString()) |
| 155 |
6/14✓ Branch 1 taken 52 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 52 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 52 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 52 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 52 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 52 times.
✗ Branch 14 not taken.
|
104 | && BindInt64(2, static_cast<uint64_t>(type)); |
| 156 | } | ||
| 157 | |||
| 158 | |||
| 159 | //------------------------------------------------------------------------------ | ||
| 160 | |||
| 161 | |||
| 162 | 541 | SqlContainsReference::SqlContainsReference(const ReflogDatabase *database) { | |
| 163 |
2/4✓ Branch 1 taken 541 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 541 times.
✗ Branch 5 not taken.
|
541 | DeferredInit(database->sqlite_db(), "SELECT count(*) as answer FROM refs " |
| 164 | "WHERE type = :type " | ||
| 165 | " AND hash = :hash"); | ||
| 166 | 541 | } | |
| 167 | |||
| 168 | 208 | bool SqlContainsReference::BindReference(const shash::Any &reference_hash, | |
| 169 | const ReferenceType type) { | ||
| 170 | 416 | return BindInt64(1, static_cast<uint64_t>(type)) | |
| 171 |
6/16✓ Branch 1 taken 208 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 208 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 208 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 208 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 208 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 208 times.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
|
208 | && BindTextTransient(2, reference_hash.ToString()); |
| 172 | } | ||
| 173 | |||
| 174 | 208 | bool SqlContainsReference::RetrieveAnswer() { | |
| 175 | 208 | const int64_t count = RetrieveInt64(0); | |
| 176 |
3/4✓ Branch 0 taken 104 times.
✓ Branch 1 taken 104 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 104 times.
|
208 | assert(count == 0 || count == 1); |
| 177 | 208 | return count > 0; | |
| 178 | } | ||
| 179 | |||
| 180 | |||
| 181 | //------------------------------------------------------------------------------ | ||
| 182 | |||
| 183 | |||
| 184 | 541 | SqlGetTimestamp::SqlGetTimestamp(const ReflogDatabase *database) { | |
| 185 |
2/4✓ Branch 1 taken 541 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 541 times.
✗ Branch 5 not taken.
|
541 | DeferredInit(database->sqlite_db(), "SELECT timestamp FROM refs " |
| 186 | "WHERE type = :type " | ||
| 187 | " AND hash = :hash"); | ||
| 188 | 541 | } | |
| 189 | |||
| 190 | 26 | bool SqlGetTimestamp::BindReference(const shash::Any &reference_hash, | |
| 191 | const ReferenceType type) { | ||
| 192 | 52 | return BindInt64(1, static_cast<uint64_t>(type)) | |
| 193 |
6/16✓ Branch 1 taken 26 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 26 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 26 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 26 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 26 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 26 times.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
|
26 | && BindTextTransient(2, reference_hash.ToString()); |
| 194 | } | ||
| 195 | |||
| 196 | 13 | uint64_t SqlGetTimestamp::RetrieveTimestamp() { return RetrieveInt64(0); } | |
| 197 |