forked from swiftlang/swift-package-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPackageCollectionsStorage.swift
370 lines (329 loc) · 14.4 KB
/
PackageCollectionsStorage.swift
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
/*
This source file is part of the Swift.org open source project
Copyright (c) 2020 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See http://swift.org/LICENSE.txt for license information
See http://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
import Basics
import Dispatch
import struct Foundation.Data
import class Foundation.JSONDecoder
import class Foundation.JSONEncoder
import struct Foundation.URL
import PackageModel
import TSCBasic
import TSCUtility
// MARK: - PackageCollectionsStorage
public protocol PackageCollectionsStorage {
/// Writes `PackageCollection` to storage.
///
/// - Parameters:
/// - collection: The `PackageCollection`
/// - callback: The closure to invoke when result becomes available
func put(collection: PackageCollectionsModel.Collection,
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void)
/// Removes `PackageCollection` from storage.
///
/// - Parameters:
/// - identifier: The identifier of the `PackageCollection`
/// - callback: The closure to invoke when result becomes available
func remove(identifier: PackageCollectionsModel.CollectionIdentifier,
callback: @escaping (Result<Void, Error>) -> Void)
/// Returns `PackageCollection` for the given identifier.
///
/// - Parameters:
/// - identifier: The identifier of the `PackageCollection`
/// - callback: The closure to invoke when result becomes available
func get(identifier: PackageCollectionsModel.CollectionIdentifier,
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void)
/// Returns `PackageCollection`s for the given identifiers, or all if none specified.
///
/// - Parameters:
/// - identifiers: Optional. The identifiers of the `PackageCollection`
/// - callback: The closure to invoke when result becomes available
func list(identifiers: [PackageCollectionsModel.CollectionIdentifier]?,
callback: @escaping (Result<[PackageCollectionsModel.Collection], Error>) -> Void)
/// Returns `PackageSearchResult` for the given search criteria.
///
/// - Parameters:
/// - identifiers: Optional. The identifiers of the `PackageCollection`s
/// - query: The search query expression
/// - callback: The closure to invoke when result becomes available
func searchPackages(identifiers: [PackageCollectionsModel.CollectionIdentifier]?,
query: String,
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult, Error>) -> Void)
/// Returns optional `PackageSearchResult.Item` for the given package identity.
///
/// - Parameters:
/// - identifier: The package identifier
/// - collectionIdentifiers: Optional. The identifiers of the `PackageCollection`s
/// - callback: The closure to invoke when result becomes available
func findPackage(identifier: PackageReference.Identity,
collectionIdentifiers: [PackageCollectionsModel.CollectionIdentifier]?,
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult.Item, Error>) -> Void)
/// Returns `TargetSearchResult` for the given search criteria.
///
/// - Parameters:
/// - identifiers: Optional. The identifiers of the `PackageCollection`
/// - query: The search query expression
/// - type: The search type
/// - callback: The closure to invoke when result becomes available
func searchTargets(identifiers: [PackageCollectionsModel.CollectionIdentifier]?,
query: String,
type: PackageCollectionsModel.TargetSearchType,
callback: @escaping (Result<PackageCollectionsModel.TargetSearchResult, Error>) -> Void)
}
// MARK: - SQLitePackageCollectionsStorage
final class SQLitePackageCollectionsStorage: PackageCollectionsStorage, Closable {
static let batchSize = 100
let fileSystem: FileSystem
let location: SQLite.Location
private let jsonEncoder: JSONEncoder
private let jsonDecoder: JSONDecoder
// for concurrent for DB access
private let queue = DispatchQueue(label: "org.swift.swiftpm.SQLitePackageCollectionsStorage", attributes: .concurrent)
private var state = State.idle
private let stateLock = Lock()
init(location: SQLite.Location? = nil) {
self.location = location ?? .path(localFileSystem.swiftPMCacheDirectory.appending(components: "package-collection.db"))
switch self.location {
case .path, .temporary:
self.fileSystem = localFileSystem
case .memory:
self.fileSystem = InMemoryFileSystem()
}
self.jsonEncoder = JSONEncoder()
self.jsonDecoder = JSONDecoder()
}
convenience init(path: AbsolutePath) {
self.init(location: .path(path))
}
deinit {
guard case .disconnected = (self.stateLock.withLock { self.state }) else {
return assertionFailure("db should be closed")
}
}
func close() throws {
try self.stateLock.withLock {
if case .connected(let db) = self.state {
try db.close()
}
self.state = .disconnected
}
}
func put(collection: PackageCollectionsModel.Collection,
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void)
{
self.queue.async {
do {
let query = "INSERT OR IGNORE INTO PACKAGES_COLLECTIONS VALUES (?, ?);"
try self.executeStatement(query) { statement -> Void in
let data = try self.jsonEncoder.encode(collection)
let bindings: [SQLite.SQLiteValue] = [
.string(collection.identifier.databaseKey()),
.blob(data),
]
try statement.bind(bindings)
try statement.step()
}
callback(.success(collection))
} catch {
callback(.failure(error))
}
}
}
func remove(identifier: PackageCollectionsModel.CollectionIdentifier,
callback: @escaping (Result<Void, Error>) -> Void)
{
self.queue.async {
do {
let query = "DELETE FROM PACKAGES_COLLECTIONS WHERE key == ?;"
try self.executeStatement(query) { statement -> Void in
let bindings: [SQLite.SQLiteValue] = [
.string(identifier.databaseKey()),
]
try statement.bind(bindings)
try statement.step()
}
callback(.success(()))
} catch {
callback(.failure(error))
}
}
}
func get(identifier: PackageCollectionsModel.CollectionIdentifier,
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void)
{
self.queue.async {
do {
let query = "SELECT value FROM PACKAGES_COLLECTIONS WHERE key == ? LIMIT 1;"
let collection = try self.executeStatement(query) { statement -> PackageCollectionsModel.Collection in
try statement.bind([.string(identifier.databaseKey())])
let row = try statement.step()
guard let data = row?.blob(at: 0) else {
throw NotFoundError("\(identifier)")
}
let collection = try self.jsonDecoder.decode(PackageCollectionsModel.Collection.self, from: data)
return collection
}
callback(.success(collection))
} catch {
callback(.failure(error))
}
}
}
func list(identifiers: [PackageCollectionsModel.CollectionIdentifier]? = nil,
callback: @escaping (Result<[PackageCollectionsModel.Collection], Error>) -> Void)
{
self.queue.async {
do {
var blobs = [Data]()
if let identifiers = identifiers {
// TODO: consider running these in parallel
var index = 0
while index < identifiers.count {
let slice = identifiers[index ..< min(index + Self.batchSize, identifiers.count)]
let query = "SELECT value FROM PACKAGES_COLLECTIONS WHERE key in (\(slice.map { _ in "?" }.joined(separator: ",")));"
try self.executeStatement(query) { statement in
try statement.bind(slice.compactMap { .string($0.databaseKey()) })
while let row = try statement.step() {
blobs.append(row.blob(at: 0))
}
}
index += Self.batchSize
}
} else {
let query = "SELECT value FROM PACKAGES_COLLECTIONS;"
try self.executeStatement(query) { statement in
while let row = try statement.step() {
blobs.append(row.blob(at: 0))
}
}
}
// TODO: consider some diagnostics / warning for invalid data
let collections = blobs.compactMap { data -> PackageCollectionsModel.Collection? in
try? self.jsonDecoder.decode(PackageCollectionsModel.Collection.self, from: data)
}
callback(.success(collections))
} catch {
callback(.failure(error))
}
}
}
// FIXME: implement this
func searchPackages(identifiers: [PackageCollectionsModel.CollectionIdentifier]? = nil,
query: String,
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult, Error>) -> Void)
{
fatalError("not implemented")
}
// FIXME: this is PoC for search, need a more performant version of this
func findPackage(identifier: PackageReference.Identity,
collectionIdentifiers: [PackageCollectionsModel.CollectionIdentifier]?,
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult.Item, Error>) -> Void)
{
self.list(identifiers: collectionIdentifiers) { result in
switch result {
case .failure(let error):
return callback(.failure(error))
case .success(let collections):
// sorting by collection processing date so the latest metadata is first
let collectionPackages = collections.sorted(by: { lhs, rhs in lhs.lastProcessedAt > rhs.lastProcessedAt }).compactMap { collection in
collection.packages
.first(where: { $0.reference.identity == identifier })
.flatMap { (collection: collection.identifier, package: $0) }
}
// first package should have latest processing date
guard let package = collectionPackages.first?.package else {
return callback(.failure(NotFoundError("\(identifier)")))
}
let collections = collectionPackages.map { $0.collection }
callback(.success(.init(package: package, collections: collections)))
}
}
}
// FIXME: implement this
func searchTargets(identifiers: [PackageCollectionsModel.CollectionIdentifier]? = nil,
query: String,
type: PackageCollectionsModel.TargetSearchType,
callback: @escaping (Result<PackageCollectionsModel.TargetSearchResult, Error>) -> Void)
{
fatalError("not implemented")
}
private func createSchemaIfNecessary(db: SQLite) throws {
let table = """
CREATE TABLE IF NOT EXISTS PACKAGES_COLLECTIONS (
key STRING PRIMARY KEY NOT NULL,
value BLOB NOT NULL
);
"""
try db.exec(query: table)
try db.exec(query: "PRAGMA journal_mode=WAL;")
}
private enum State {
case idle
case connected(SQLite)
case disconnected
case error
}
private func executeStatement<T>(_ query: String, _ body: (SQLite.PreparedStatement) throws -> T) throws -> T {
try self.withDB { db in
let result: Result<T, Error>
let statement = try db.prepare(query: query)
do {
result = .success(try body(statement))
} catch {
result = .failure(error)
}
try statement.finalize()
switch result {
case .failure(let error):
throw error
case .success(let value):
return value
}
}
}
private func withDB<T>(_ body: (SQLite) throws -> T) throws -> T {
let createDB = { () throws -> SQLite in
let db = try SQLite(location: self.location)
try self.createSchemaIfNecessary(db: db)
return db
}
let db = try stateLock.withLock { () -> SQLite in
let db: SQLite
switch (self.location, self.state) {
case (.path(let path), .connected(let database)):
if self.fileSystem.exists(path) {
db = database
} else {
try database.close()
try self.fileSystem.createDirectory(path.parentDirectory, recursive: true)
db = try createDB()
}
case (.path(let path), _):
if !self.fileSystem.exists(path) {
try self.fileSystem.createDirectory(path.parentDirectory, recursive: true)
}
db = try createDB()
case (_, .connected(let database)):
db = database
case (_, _):
db = try createDB()
}
self.state = .connected(db)
return db
}
return try body(db)
}
}
// MARK: - Utility
private extension PackageCollectionsModel.Collection.Identifier {
func databaseKey() -> String {
switch self {
case .feed(let url):
return url.absoluteString
}
}
}