File: PackageIndexAndCollections.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (341 lines) | stat: -rw-r--r-- 14,425 bytes parent folder | download | duplicates (2)
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
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2022 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 the list of Swift project authors
//
//===----------------------------------------------------------------------===//

import Basics
import Dispatch
import struct Foundation.URL
import PackageModel

import protocol TSCBasic.Closable

public struct PackageIndexAndCollections: Closable {
    private let index: PackageIndexProtocol
    private let collections: PackageCollectionsProtocol
    private let observabilityScope: ObservabilityScope
    
    public init(
        indexConfiguration: PackageIndexConfiguration = .init(),
        collectionsConfiguration: PackageCollections.Configuration = .init(),
        fileSystem: FileSystem,
        observabilityScope: ObservabilityScope
    ) {
        let index = PackageIndex(
            configuration: indexConfiguration,
            callbackQueue: .sharedConcurrent,
            observabilityScope: observabilityScope
        )
        let metadataProvider = PackageIndexMetadataProvider(
            index: index,
            alternativeContainer: (
                provider: GitHubPackageMetadataProvider(
                    configuration: .init(authTokens: collectionsConfiguration.authTokens),
                    observabilityScope: observabilityScope
                ),
                managed: true
            )
        )
        
        self.index = index
        self.collections = PackageCollections(
            configuration: collectionsConfiguration,
            customMetadataProvider: metadataProvider,
            fileSystem: fileSystem,
            observabilityScope: observabilityScope
        )
        self.observabilityScope = observabilityScope
    }
    
    init(index: PackageIndexProtocol, collections: PackageCollectionsProtocol, observabilityScope: ObservabilityScope) {
        self.index = index
        self.collections = collections
        self.observabilityScope = observabilityScope
    }
    
    public func close() throws {
        if let index = self.index as? Closable {
            try index.close()
        }
        if let collections = self.collections as? Closable {
            try collections.close()
        }
    }
    
    // MARK: - Package collection specific APIs
    
    public func listCollections(
        identifiers: Set<PackageCollectionsModel.CollectionIdentifier>? = nil
    ) async throws -> [PackageCollectionsModel.Collection] {
        try await self.collections.listCollections(identifiers: identifiers)
    }

    
    public func refreshCollections() async throws -> [PackageCollectionsModel.CollectionSource] {
        try await self.collections.refreshCollections()
    }

    public func refreshCollection(_ source: PackageCollectionsModel.CollectionSource) async throws -> PackageCollectionsModel.Collection {
        try await self.collections.refreshCollection(source)
    }

    public func addCollection(
        _ source: PackageCollectionsModel.CollectionSource,
        order: Int? = nil,
        trustConfirmationProvider: ((PackageCollectionsModel.Collection, @escaping (Bool) -> Void) -> Void)? = nil
    ) async throws -> PackageCollectionsModel.Collection {
        try await self.collections.addCollection(
            source,
            order: order,
            trustConfirmationProvider: trustConfirmationProvider
        )
    }
    
    public func removeCollection(
        _ source: PackageCollectionsModel.CollectionSource
    ) async throws {
        try await self.collections.removeCollection(source)
    }

    public func getCollection(
        _ source: PackageCollectionsModel.CollectionSource
    ) async throws -> PackageCollectionsModel.Collection {
        try await self.collections.getCollection(source)
    }

    public func listPackages(
        collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil
    ) async throws -> PackageCollectionsModel.PackageSearchResult {
        try await self.collections.listPackages(collections: collections)
    }
    
    public func listTargets(
        collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil
    ) async throws -> PackageCollectionsModel.TargetListResult {
        try await self.collections.listTargets(collections: collections)
    }

    public func findTargets(
        _ query: String,
        searchType: PackageCollectionsModel.TargetSearchType? = nil,
        collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil
    ) async throws -> PackageCollectionsModel.TargetSearchResult {
        try await self.collections.findTargets(
            query,
            searchType: searchType,
            collections: collections
        )
    }


    // MARK: - Package index specific APIs

    /// Indicates if package index is configured.
    public func isIndexEnabled() -> Bool {
        self.index.isEnabled
    }
    
    public func listPackagesInIndex(
        offset: Int,
        limit: Int
    ) async throws -> PackageCollectionsModel.PaginatedPackageList {
        try await self.index.listPackages(offset: offset, limit: limit)
    }


    // MARK: - APIs that make use of both package index and collections

    /// Returns metadata for the package identified by the given `PackageIdentity`, using package index (if configured)
    /// and collections data.
    ///
    /// A failure is returned if the package is not found.
    ///
    /// - Parameters:
    ///   - identity: The package identity
    ///   - location: The package location (optional for deduplication)
    ///   - collections: Optional. If specified, only these collections are used to construct the result.
    public func getPackageMetadata(
        identity: PackageIdentity,
        location: String? = nil,
        collections: Set<PackageCollectionsModel.CollectionIdentifier>? = nil
    ) async throws -> PackageCollectionsModel.PackageMetadata {
        // Package index not available - fallback to collections
        guard self.index.isEnabled else {
            return try await self.collections.getPackageMetadata(identity: identity, location: location, collections: collections)
        }

        // This uses package index only
        async let indexResult = self.index.getPackageMetadata(identity: identity, location: location)

        // This uses either package index or "alternative" (e.g., GitHub) as metadata provider,
        // then merge the supplementary metadata with data coming from collections. The package
        // must belong to at least one collection.
        async let collectionsResult = self.collections.getPackageMetadata(identity: identity, location: location, collections: collections)


        do {
            let indexPackageMetadata = try await indexResult
            return PackageCollectionsModel.PackageMetadata(
                package: indexPackageMetadata.package,
                collections: (try? await collectionsResult)?.collections ?? [],
                provider: indexPackageMetadata.provider
            )
        } catch {
            self.observabilityScope.emit(warning: "PackageIndex.getPackageMetadata failed: \(error)")
            do {
                return try await collectionsResult
            } catch let collectionsError {
                self.observabilityScope.emit(warning: "PackageCollections.getPackageMetadata failed: \(collectionsError)")
            }
            throw error
        }
    }

    /// Finds and returns packages that match the query.
    ///
    /// - Parameters:
    ///   - query: The search query
    ///   - in: Indicates whether to search in the index only, collections only, or both.
    ///         The optional `Set<CollectionIdentifier>` in some enum cases restricts search within those collections only.
    public func findPackages(
        _ query: String,
        in searchIn: SearchIn = .both(collections: nil)
    ) async throws -> PackageCollectionsModel.PackageSearchResult {
        switch searchIn {
        case .index:
            guard self.index.isEnabled else {
                self.observabilityScope.emit(debug: "Package index is not enabled. Returning empty result.")
                return PackageCollectionsModel.PackageSearchResult(items: [])
            }
            return try await self.index.findPackages(query)
        case .collections(let collections):
            return try await self.collections.findPackages(query, collections: collections)
        case .both(let collections):
            // Find packages in both package index and collections
            async let pendingIndexPackages = self.index.findPackages(query)
            async let pendingcollectionPackages = self.collections.findPackages(query, collections: collections)

            do {
                let indexSearchResult = try await pendingIndexPackages
                do {
                    let collectionsSearchResult = try await pendingcollectionPackages

                    let indexItems = Dictionary(uniqueKeysWithValues: indexSearchResult.items.map {
                        (SearchResultItemKey(identity: $0.package.identity, location: $0.package.location), $0)
                    })
                    let collectionItems = Dictionary(uniqueKeysWithValues: collectionsSearchResult.items.map {
                        (SearchResultItemKey(identity: $0.package.identity, location: $0.package.location), $0)
                    })

                    // An array of combined results, with index items listed first.
                    var items = [PackageCollectionsModel.PackageSearchResult.Item]()
                    // Iterating through the dictionary would simplify the code, but we want to keep the ordering of the search result.
                    indexSearchResult.items.forEach {
                        var item = $0
                        let key = SearchResultItemKey(identity: $0.package.identity, location: $0.package.location)
                        // This item is found in collections too
                        if let collectionsMatch = collectionItems[key] {
                            item.collections = collectionsMatch.collections
                        }
                        items.append(item)
                    }
                    collectionsSearchResult.items.forEach {
                        let key = SearchResultItemKey(identity: $0.package.identity, location: $0.package.location)
                        // This item is found in index as well, but skipping since it has already been handled in the loop above.
                        guard indexItems[key] == nil else {
                            return
                        }
                        items.append($0)
                    }
                    return PackageCollectionsModel.PackageSearchResult(items: items)

                } catch let collectionsError {
                    self.observabilityScope.emit(warning: "PackageCollections.findPackages failed: \(collectionsError)")

                    // Collections query failed, try another way to find the collections that an item belongs to.
                    do {
                        let collectionsSearchResult = try await self.collections.listPackages(collections: collections)
                        let items = indexSearchResult.items.map { item in
                            PackageCollectionsModel.PackageSearchResult.Item(
                                package: item.package,
                                collections: collectionsSearchResult.items.first(where: {
                                    item.package.identity == $0.package.identity && item.package.location == $0.package.location
                                })?.collections ?? [],
                                indexes: item.indexes
                            )
                        }
                        return PackageCollectionsModel.PackageSearchResult(items: items)
                    } catch {
                        return indexSearchResult
                    }
                }
            } catch let indexError {
                self.observabilityScope.emit(warning: "PackageIndex.findPackages failed: \(indexError)")
                do {
                    return try await pendingcollectionPackages
                } catch let collectionsError {
                    // Failed to find packages through `PackageIndex` and `PackageCollections`.
                    // Return index's error.
                    self.observabilityScope.emit(warning: "PackageCollections.findPackages failed: \(collectionsError)")
                    throw indexError
                }
            }

            struct SearchResultItemKey: Hashable {
                let identity: PackageIdentity
                let location: String
            }
        }
    }
    
    private enum Source: Hashable {
        case index
        case collections
    }
}

struct PackageIndexMetadataProvider: PackageMetadataProvider, Closable {
    typealias ProviderContainer = (provider: PackageMetadataProvider, managed: Bool)
    
    let index: PackageIndex
    let alternativeContainer: ProviderContainer
    
    var alternative: PackageMetadataProvider {
        self.alternativeContainer.provider
    }

    func get(
        identity: PackageIdentity,
        location: String
    ) async -> (Result<PackageCollectionsModel.PackageBasicMetadata, Error>, PackageMetadataProviderContext?) {
        if self.index.isEnabled {
            return await self.index.get(identity: identity, location: location)
        } else {
            return await self.alternative.get(identity: identity, location: location)
        }
    }
    
    func close() throws {
        guard self.alternativeContainer.managed else {
            return
        }
        if let alternative = self.alternative as? Closable {
            try alternative.close()
        }
    }
}

extension PackageIndexAndCollections {
    public enum SearchIn {
        case index
        case collections(Set<PackageCollectionsModel.CollectionIdentifier>?)
        case both(collections: Set<PackageCollectionsModel.CollectionIdentifier>?)
    }
}