-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathSQLiteIndex.cpp
More file actions
379 lines (297 loc) · 15 KB
/
SQLiteIndex.cpp
File metadata and controls
379 lines (297 loc) · 15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "SQLiteIndex.h"
#include <winget/SQLiteStorageBase.h>
#include "ArpVersionValidation.h"
#include <winget/ManifestYamlParser.h>
namespace AppInstaller::Repository::Microsoft
{
namespace
{
size_t GetPageSizeFromOptions(SQLiteIndex::CreateOptions options)
{
return WI_IsFlagSet(options, SQLiteIndex::CreateOptions::LargePageSize) ? 65536 : 0;
}
}
SQLiteIndex SQLiteIndex::CreateNew(const std::string& filePath, SQLite::Version version, CreateOptions options)
{
AICLI_LOG(Repo, Info, << "Creating new SQLite Index with version [" << version << "] at '" << filePath << "'");
SQLiteIndex result{ filePath, version, options };
SQLite::Savepoint savepoint = SQLite::Savepoint::Create(result.m_dbconn, "sqliteindex_createnew");
// Use calculated version, as incoming version could be 'latest'
result.m_version.SetSchemaVersion(result.m_dbconn);
result.m_interface->CreateTables(result.m_dbconn, options);
result.SetLastWriteTime();
savepoint.Commit();
return result;
}
SQLiteIndex SQLiteIndex::Open(const std::string& filePath, OpenDisposition disposition, Utility::ManagedFile&& indexFile)
{
return { filePath, disposition, std::move(indexFile) };
}
SQLiteIndex SQLiteIndex::CopyFrom(const std::string& filePath, SQLiteIndex& source)
{
return { filePath, source };
}
SQLiteIndex::SQLiteIndex(const std::string& target, const SQLite::Version& version, CreateOptions options) : SQLiteStorageBase(target, version, GetPageSizeFromOptions(options))
{
m_dbconn.EnableICU();
m_interface = Schema::CreateISQLiteIndex(version);
m_version = m_interface->GetVersion();
SetDatabaseFilePath(target);
}
SQLiteIndex::SQLiteIndex(const std::string& target, SQLiteStorageBase::OpenDisposition disposition, Utility::ManagedFile&& indexFile) :
SQLiteStorageBase(target, disposition, std::move(indexFile))
{
m_dbconn.EnableICU();
AICLI_LOG(Repo, Info, << "Opened SQLite Index with version [" << m_version << "], last write [" << GetLastWriteTime() << "]");
m_interface = Schema::CreateISQLiteIndex(m_version);
THROW_HR_IF(APPINSTALLER_CLI_ERROR_CANNOT_WRITE_TO_UPLEVEL_INDEX, disposition == SQLiteStorageBase::OpenDisposition::ReadWrite && m_version != m_interface->GetVersion());
SetDatabaseFilePath(target);
}
SQLiteIndex::SQLiteIndex(const std::string& target, SQLiteIndex& source) :
SQLiteStorageBase(target, source)
{
m_dbconn.EnableICU();
m_interface = Schema::CreateISQLiteIndex(m_version);
SetDatabaseFilePath(target);
}
void SQLiteIndex::SetDatabaseFilePath(const std::string& target)
{
if (target != SQLITE_MEMORY_DB_CONNECTION_TARGET)
{
m_contextData.Add<Schema::Property::DatabaseFilePath>(Utility::ConvertToUTF16(target));
}
}
#ifndef AICLI_DISABLE_TEST_HOOKS
void SQLiteIndex::ForceVersion(const SQLite::Version& version)
{
m_interface = Schema::CreateISQLiteIndex(version);
}
SQLite::Version SQLiteIndex::GetLatestVersion()
{
return Schema::CreateISQLiteIndex(SQLite::Version::Latest())->GetVersion();
}
const Schema::SQLiteIndexContextData& SQLiteIndex::GetContextData() const
{
return m_contextData;
}
#endif
SQLiteIndex::IdType SQLiteIndex::AddManifest(const std::filesystem::path& manifestPath, const std::filesystem::path& relativePath)
{
AICLI_LOG(Repo, Verbose, << "Adding manifest from file [" << manifestPath << "]");
Manifest::Manifest manifest = Manifest::YamlParser::CreateFromPath(manifestPath);
return AddManifestInternal(manifest, relativePath);
}
SQLiteIndex::IdType SQLiteIndex::AddManifest(const Manifest::Manifest& manifest, const std::filesystem::path& relativePath)
{
return AddManifestInternal(manifest, relativePath);
}
SQLiteIndex::IdType SQLiteIndex::AddManifest(const Manifest::Manifest& manifest)
{
return AddManifestInternal(manifest, {});
}
SQLiteIndex::IdType SQLiteIndex::AddManifestInternal(const Manifest::Manifest& manifest, const std::optional<std::filesystem::path>& relativePath)
{
std::lock_guard<std::mutex> lockInterface{ *m_interfaceLock };
return AddManifestInternalHoldingLock(manifest, relativePath);
}
SQLiteIndex::IdType SQLiteIndex::AddManifestInternalHoldingLock(const Manifest::Manifest& manifest, const std::optional<std::filesystem::path>& relativePath)
{
AICLI_LOG(Repo, Verbose, << "Adding manifest for [" << manifest.Id << ", " << manifest.Version << "] at relative path [" << relativePath.value_or("") << "]");
SQLite::Savepoint savepoint = SQLite::Savepoint::Create(m_dbconn, "sqliteindex_addmanifest");
IdType result = m_interface->AddManifest(m_dbconn, manifest, relativePath);
SetLastWriteTime();
savepoint.Commit();
return result;
}
bool SQLiteIndex::UpdateManifest(const std::filesystem::path& manifestPath, const std::filesystem::path& relativePath)
{
AICLI_LOG(Repo, Verbose, << "Updating manifest from file [" << manifestPath << "]");
Manifest::Manifest manifest = Manifest::YamlParser::CreateFromPath(manifestPath);
return UpdateManifestInternal(manifest, relativePath);
}
bool SQLiteIndex::UpdateManifest(const Manifest::Manifest& manifest, const std::filesystem::path& relativePath)
{
return UpdateManifestInternal(manifest, relativePath);
}
bool SQLiteIndex::UpdateManifest(const Manifest::Manifest& manifest)
{
return UpdateManifestInternal(manifest, {});
}
bool SQLiteIndex::UpdateManifestInternal(const Manifest::Manifest& manifest, const std::optional<std::filesystem::path>& relativePath)
{
std::lock_guard<std::mutex> lockInterface{ *m_interfaceLock };
return UpdateManifestInternalHoldingLock(manifest, relativePath);
}
bool SQLiteIndex::UpdateManifestInternalHoldingLock(const Manifest::Manifest& manifest, const std::optional<std::filesystem::path>& relativePath)
{
AICLI_LOG(Repo, Verbose, << "Updating manifest for [" << manifest.Id << ", " << manifest.Version << "] at relative path [" << relativePath.value_or("") << "]");
SQLite::Savepoint savepoint = SQLite::Savepoint::Create(m_dbconn, "sqliteindex_updatemanifest");
bool result = m_interface->UpdateManifest(m_dbconn, manifest, relativePath).first;
if (result)
{
SetLastWriteTime();
savepoint.Commit();
}
return result;
}
bool SQLiteIndex::AddOrUpdateManifest(const std::filesystem::path& manifestPath, const std::filesystem::path& relativePath)
{
AICLI_LOG(Repo, Verbose, << "Adding or Updating manifest from file [" << manifestPath << "]");
Manifest::Manifest manifest = Manifest::YamlParser::CreateFromPath(manifestPath);
return AddOrUpdateManifestInternal(manifest, relativePath);
}
bool SQLiteIndex::AddOrUpdateManifest(const Manifest::Manifest& manifest, const std::filesystem::path& relativePath)
{
return AddOrUpdateManifestInternal(manifest, relativePath);
}
bool SQLiteIndex::AddOrUpdateManifest(const Manifest::Manifest& manifest)
{
return AddOrUpdateManifestInternal(manifest, {});
}
bool SQLiteIndex::AddOrUpdateManifestInternal(const Manifest::Manifest& manifest, const std::optional<std::filesystem::path>& relativePath)
{
std::lock_guard<std::mutex> lockInterface{ *m_interfaceLock };
AICLI_LOG(Repo, Verbose, << "Adding or Updating manifest for [" << manifest.Id << ", " << manifest.Version << "] at relative path [" << relativePath.value_or("") << "]");
if (m_interface->GetManifestIdByManifest(m_dbconn, manifest))
{
UpdateManifestInternalHoldingLock(manifest, relativePath);
return false;
}
else
{
AddManifestInternalHoldingLock(manifest, relativePath);
return true;
}
}
void SQLiteIndex::RemoveManifest(const std::filesystem::path& manifestPath, const std::filesystem::path& relativePath)
{
AICLI_LOG(Repo, Verbose, << "Removing manifest from file [" << manifestPath << "]");
Manifest::Manifest manifest = Manifest::YamlParser::CreateFromPath(manifestPath);
RemoveManifest(manifest, relativePath);
}
void SQLiteIndex::RemoveManifest(const Manifest::Manifest& manifest, const std::filesystem::path& relativePath)
{
AICLI_LOG(Repo, Verbose, << "Removing manifest for [" << manifest.Id << ", " << manifest.Version << "] at relative path [" << relativePath << "]");
RemoveManifest(manifest);
}
void SQLiteIndex::RemoveManifest(const Manifest::Manifest& manifest)
{
std::lock_guard<std::mutex> lockInterface{ *m_interfaceLock };
SQLite::Savepoint savepoint = SQLite::Savepoint::Create(m_dbconn, "sqliteindex_removemanifest");
m_interface->RemoveManifest(m_dbconn, manifest);
SetLastWriteTime();
savepoint.Commit();
}
void SQLiteIndex::RemoveManifestById(IdType manifestId)
{
SQLite::Savepoint savepoint = SQLite::Savepoint::Create(m_dbconn, "SQLiteIndex_RemoveManifestById");
m_interface->RemoveManifestById(m_dbconn, manifestId);
SetLastWriteTime();
savepoint.Commit();
}
void SQLiteIndex::PrepareForPackaging()
{
std::lock_guard<std::mutex> lockInterface{ *m_interfaceLock };
AICLI_LOG(Repo, Info, << "Preparing index for packaging");
m_interface->PrepareForPackaging(Schema::SQLiteIndexContext{ m_dbconn, m_contextData });
}
bool SQLiteIndex::CheckConsistency(bool log) const
{
std::lock_guard<std::mutex> lockInterface{ *m_interfaceLock };
AICLI_LOG(Repo, Info, << "Checking index consistency...");
bool result = m_interface->CheckConsistency(m_dbconn, log);
AICLI_LOG(Repo, Info, << "...index *WAS" << (result ? "*" : " NOT*") << " consistent.");
return result;
}
Schema::ISQLiteIndex::SearchResult SQLiteIndex::Search(const SearchRequest& request) const
{
std::lock_guard<std::mutex> lockInterface{ *m_interfaceLock };
AICLI_LOG(Repo, Verbose, << "Performing search: " << request.ToString());
return m_interface->Search(m_dbconn, request);
}
std::optional<std::string> SQLiteIndex::GetPropertyByPrimaryId(IdType primaryId, PackageVersionProperty property) const
{
std::lock_guard<std::mutex> lockInterface{ *m_interfaceLock };
return m_interface->GetPropertyByPrimaryId(m_dbconn, primaryId, property);
}
std::vector<std::string> SQLiteIndex::GetMultiPropertyByPrimaryId(IdType primaryId, PackageVersionMultiProperty property) const
{
std::lock_guard<std::mutex> lockInterface{ *m_interfaceLock };
return m_interface->GetMultiPropertyByPrimaryId(m_dbconn, primaryId, property);
}
std::optional<SQLiteIndex::IdType> SQLiteIndex::GetManifestIdByKey(IdType id, std::string_view version, std::string_view channel) const
{
std::lock_guard<std::mutex> lockInterface{ *m_interfaceLock };
return m_interface->GetManifestIdByKey(m_dbconn, id, version, channel);
}
std::optional<SQLiteIndex::IdType> SQLiteIndex::GetManifestIdByManifest(const Manifest::Manifest& manifest) const
{
return m_interface->GetManifestIdByManifest(m_dbconn, manifest);
}
std::vector<SQLiteIndex::VersionKey> SQLiteIndex::GetVersionKeysById(IdType id) const
{
std::lock_guard<std::mutex> lockInterface{ *m_interfaceLock };
return m_interface->GetVersionKeysById(m_dbconn, id);
}
SQLiteIndex::MetadataResult SQLiteIndex::GetMetadataByManifestId(SQLite::rowid_t manifestId) const
{
std::lock_guard<std::mutex> lockInterface{ *m_interfaceLock };
return m_interface->GetMetadataByManifestId(m_dbconn, manifestId);
}
void SQLiteIndex::SetMetadataByManifestId(IdType manifestId, PackageVersionMetadata metadata, std::string_view value)
{
std::lock_guard<std::mutex> lockInterface{ *m_interfaceLock };
m_interface->SetMetadataByManifestId(m_dbconn, manifestId, metadata, value);
}
Utility::NormalizedName SQLiteIndex::NormalizeName(std::string_view name, std::string_view publisher) const
{
std::lock_guard<std::mutex> lockInterface{ *m_interfaceLock };
return m_interface->NormalizeName(name, publisher);
}
std::set<std::pair<SQLite::rowid_t, Utility::NormalizedString>> SQLiteIndex::GetDependenciesByManifestRowId(SQLite::rowid_t manifestRowId) const
{
return m_interface->GetDependenciesByManifestRowId(m_dbconn, manifestRowId);
}
std::vector<std::pair<SQLite::rowid_t, Utility::NormalizedString>> SQLiteIndex::GetDependentsById(AppInstaller::Manifest::string_t packageId) const
{
return m_interface->GetDependentsById(m_dbconn, packageId);
}
bool SQLiteIndex::MigrateTo(SQLite::Version version)
{
std::lock_guard<std::mutex> lockInterface{ *m_interfaceLock };
SQLite::Savepoint savepoint = SQLite::Savepoint::Create(m_dbconn, "sqliteindex_migrate_to");
AICLI_LOG(Repo, Info, << "Attempting to migrate index from [" << m_interface->GetVersion() << "] to [" << version << "]...");
std::unique_ptr<Schema::ISQLiteIndex> newInterface = Schema::CreateISQLiteIndex(version);
bool result = newInterface->MigrateFrom(m_dbconn, m_interface.get());
AICLI_LOG(Repo, Info, << "...migration was " << (result ? "" : "NOT ") << "successful");
if (result)
{
version.SetSchemaVersion(m_dbconn);
SetLastWriteTime();
savepoint.Commit();
m_version = version;
m_interface = std::move(newInterface);
}
return result;
}
void SQLiteIndex::SetProperty(Property property, const std::string& value)
{
std::lock_guard<std::mutex> lockInterface{ *m_interfaceLock };
switch (property)
{
case Property::PackageUpdateTrackingBaseTime:
m_interface->SetProperty(m_dbconn, Schema::Property::PackageUpdateTrackingBaseTime, value);
break;
case Property::IntermediateFileOutputPath:
{
std::filesystem::path pathValue{ Utility::ConvertToUTF16(value) };
THROW_HR_IF(E_INVALIDARG, pathValue.empty() || pathValue.is_relative());
m_contextData.Add<Schema::Property::IntermediateFileOutputPath>(std::move(pathValue));
}
break;
}
}
}