Directory: | cvmfs/ |
---|---|
File: | cvmfs/reflog_sql.cc |
Date: | 2025-06-22 02:36:02 |
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 | 638 | bool ReflogDatabase::CreateEmptyDatabase() { | |
27 |
3/6✓ Branch 2 taken 638 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 638 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 638 times.
✗ Branch 9 not taken.
|
1276 | 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 638 times.
✗ Branch 2 not taken.
|
1276 | .Execute(); |
32 | } | ||
33 | |||
34 | |||
35 | 131 | bool ReflogDatabase::CheckSchemaCompatibility() { | |
36 |
1/2✗ Branch 2 not taken.
✓ Branch 3 taken 131 times.
|
131 | assert(IsEqualSchema(schema_version(), kLatestSupportedSchema)); |
37 | 131 | return true; // only one schema version at the moment | |
38 | } | ||
39 | |||
40 | |||
41 | 131 | bool ReflogDatabase::LiveSchemaUpgradeIfNecessary() { | |
42 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 131 times.
|
131 | assert(schema_revision() == kLatestSchemaRevision); |
43 | 131 | return true; // only one schema revision at the moment, i.e. no migration... | |
44 | } | ||
45 | |||
46 | |||
47 | 638 | bool ReflogDatabase::InsertInitialValues(const std::string &repo_name) { | |
48 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 638 times.
|
638 | assert(read_write()); |
49 |
1/2✓ Branch 2 taken 638 times.
✗ Branch 3 not taken.
|
638 | 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 | 4889 | shash::Suffix SqlReflog::ToSuffix(const ReferenceType type) { | |
71 |
4/5✓ Branch 0 taken 4281 times.
✓ Branch 1 taken 203 times.
✓ Branch 2 taken 201 times.
✓ Branch 3 taken 204 times.
✗ Branch 4 not taken.
|
4889 | switch (type) { |
72 | 4281 | case kRefCatalog: | |
73 | 4281 | return shash::kSuffixCatalog; | |
74 | 203 | case kRefCertificate: | |
75 | 203 | return shash::kSuffixCertificate; | |
76 | 201 | case kRefHistory: | |
77 | 201 | return shash::kSuffixHistory; | |
78 | 204 | case kRefMetainfo: | |
79 | 204 | return shash::kSuffixMetainfo; | |
80 | ✗ | default: | |
81 | ✗ | assert(false && "unknown reference type"); | |
82 | } | ||
83 | } | ||
84 | |||
85 | |||
86 | //------------------------------------------------------------------------------ | ||
87 | |||
88 | |||
89 | 769 | SqlInsertReference::SqlInsertReference(const ReflogDatabase *database) { | |
90 |
10/20✓ Branch 0 taken 49 times.
✓ Branch 1 taken 720 times.
✓ Branch 3 taken 49 times.
✗ Branch 4 not taken.
✓ Branch 7 taken 49 times.
✗ Branch 8 not taken.
✓ Branch 11 taken 49 times.
✗ Branch 12 not taken.
✓ Branch 15 taken 49 times.
✗ Branch 16 not taken.
✓ Branch 19 taken 49 times.
✗ Branch 20 not taken.
✓ Branch 23 taken 49 times.
✗ Branch 24 not taken.
✓ Branch 26 taken 49 times.
✗ Branch 27 not taken.
✓ Branch 29 taken 49 times.
✗ Branch 30 not taken.
✗ Branch 55 not taken.
✗ Branch 56 not taken.
|
769 | MAKE_STATEMENTS("INSERT OR REPLACE INTO refs (@DB_FIELDS@) " |
91 | "VALUES (@DB_PLACEHOLDERS@);"); | ||
92 |
2/4✓ Branch 2 taken 769 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 769 times.
✗ Branch 6 not taken.
|
769 | DEFERRED_INITS(database); |
93 | 769 | } | |
94 | |||
95 | 28 | bool SqlInsertReference::BindReference(const shash::Any &reference_hash, | |
96 | const ReferenceType type) { | ||
97 |
0/2✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
28 | return BindTextTransient(1, reference_hash.ToString()) |
98 |
2/4✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 28 times.
✗ Branch 4 not taken.
|
28 | && BindInt64(2, static_cast<uint64_t>(type)) |
99 |
6/15✓ Branch 1 taken 28 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 28 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 28 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✗ Branch 9 not taken.
✓ Branch 10 taken 28 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 28 times.
✗ Branch 13 not taken.
✓ Branch 14 taken 28 times.
✗ Branch 15 not taken.
|
84 | && BindInt64(3, static_cast<uint64_t>(time(NULL))); |
100 | } | ||
101 | |||
102 | |||
103 | //------------------------------------------------------------------------------ | ||
104 | |||
105 | |||
106 | 769 | SqlCountReferences::SqlCountReferences(const ReflogDatabase *database) { | |
107 |
2/4✓ Branch 1 taken 769 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 769 times.
✗ Branch 5 not taken.
|
769 | DeferredInit(database->sqlite_db(), "SELECT count(*) as count FROM refs;"); |
108 | 769 | } | |
109 | |||
110 | 8 | uint64_t SqlCountReferences::RetrieveCount() { | |
111 | 8 | return static_cast<uint64_t>(RetrieveInt64(0)); | |
112 | } | ||
113 | |||
114 | |||
115 | //------------------------------------------------------------------------------ | ||
116 | |||
117 | |||
118 | 769 | SqlListReferences::SqlListReferences(const ReflogDatabase *database) { | |
119 |
2/4✓ Branch 1 taken 769 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 769 times.
✗ Branch 5 not taken.
|
769 | DeferredInit(database->sqlite_db(), "SELECT hash, type FROM refs " |
120 | "WHERE type = :type AND " | ||
121 | "timestamp < :timestamp " | ||
122 | "ORDER BY timestamp DESC;"); | ||
123 | 769 | } | |
124 | |||
125 | 7 | bool SqlListReferences::BindType(const ReferenceType type) { | |
126 | 7 | return BindInt64(1, static_cast<uint64_t>(type)); | |
127 | } | ||
128 | |||
129 | 7 | bool SqlListReferences::BindOlderThan(const uint64_t timestamp) { | |
130 | 7 | int64_t sqlite_timestamp = static_cast<uint64_t>(timestamp); | |
131 |
2/2✓ Branch 0 taken 5 times.
✓ Branch 1 taken 2 times.
|
7 | if (sqlite_timestamp < 0) { |
132 | 5 | sqlite_timestamp = std::numeric_limits<int64_t>::max(); | |
133 | } | ||
134 | 7 | return BindInt64(2, sqlite_timestamp); | |
135 | } | ||
136 | |||
137 | 13 | shash::Any SqlListReferences::RetrieveHash() const { | |
138 | 13 | const ReferenceType type = static_cast<ReferenceType>(RetrieveInt64(1)); | |
139 | 13 | const shash::Suffix suffix = ToSuffix(type); | |
140 |
1/2✓ Branch 3 taken 13 times.
✗ Branch 4 not taken.
|
13 | return shash::MkFromHexPtr(shash::HexPtr(RetrieveString(0)), suffix); |
141 | } | ||
142 | |||
143 | |||
144 | //------------------------------------------------------------------------------ | ||
145 | |||
146 | |||
147 | 769 | SqlRemoveReference::SqlRemoveReference(const ReflogDatabase *database) { | |
148 |
2/4✓ Branch 1 taken 769 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 769 times.
✗ Branch 5 not taken.
|
769 | DeferredInit(database->sqlite_db(), "DELETE FROM refs WHERE hash = :hash " |
149 | "AND type = :type;"); | ||
150 | 769 | } | |
151 | |||
152 | 4 | bool SqlRemoveReference::BindReference(const shash::Any &reference_hash, | |
153 | const ReferenceType type) { | ||
154 |
0/2✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
4 | return BindTextTransient(1, reference_hash.ToString()) |
155 |
6/14✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 4 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 4 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 4 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 4 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 4 times.
✗ Branch 14 not taken.
|
8 | && BindInt64(2, static_cast<uint64_t>(type)); |
156 | } | ||
157 | |||
158 | |||
159 | //------------------------------------------------------------------------------ | ||
160 | |||
161 | |||
162 | 769 | SqlContainsReference::SqlContainsReference(const ReflogDatabase *database) { | |
163 |
2/4✓ Branch 1 taken 769 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 769 times.
✗ Branch 5 not taken.
|
769 | DeferredInit(database->sqlite_db(), "SELECT count(*) as answer FROM refs " |
164 | "WHERE type = :type " | ||
165 | " AND hash = :hash"); | ||
166 | 769 | } | |
167 | |||
168 | 16 | bool SqlContainsReference::BindReference(const shash::Any &reference_hash, | |
169 | const ReferenceType type) { | ||
170 | 32 | return BindInt64(1, static_cast<uint64_t>(type)) | |
171 |
6/16✓ Branch 1 taken 16 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 16 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 16 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 16 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 16 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 16 times.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
|
16 | && BindTextTransient(2, reference_hash.ToString()); |
172 | } | ||
173 | |||
174 | 16 | bool SqlContainsReference::RetrieveAnswer() { | |
175 | 16 | const int64_t count = RetrieveInt64(0); | |
176 |
3/4✓ Branch 0 taken 8 times.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
|
16 | assert(count == 0 || count == 1); |
177 | 16 | return count > 0; | |
178 | } | ||
179 | |||
180 | |||
181 | //------------------------------------------------------------------------------ | ||
182 | |||
183 | |||
184 | 769 | SqlGetTimestamp::SqlGetTimestamp(const ReflogDatabase *database) { | |
185 |
2/4✓ Branch 1 taken 769 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 769 times.
✗ Branch 5 not taken.
|
769 | DeferredInit(database->sqlite_db(), "SELECT timestamp FROM refs " |
186 | "WHERE type = :type " | ||
187 | " AND hash = :hash"); | ||
188 | 769 | } | |
189 | |||
190 | 2 | bool SqlGetTimestamp::BindReference(const shash::Any &reference_hash, | |
191 | const ReferenceType type) { | ||
192 | 4 | return BindInt64(1, static_cast<uint64_t>(type)) | |
193 |
6/16✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
✓ Branch 9 taken 2 times.
✗ Branch 10 not taken.
✓ Branch 11 taken 2 times.
✗ Branch 12 not taken.
✓ Branch 13 taken 2 times.
✗ Branch 14 not taken.
✗ Branch 16 not taken.
✗ Branch 17 not taken.
|
2 | && BindTextTransient(2, reference_hash.ToString()); |
194 | } | ||
195 | |||
196 | 1 | uint64_t SqlGetTimestamp::RetrieveTimestamp() { return RetrieveInt64(0); } | |
197 |