Skip to content

Finish moving package collections to async/await #7746

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,6 @@ import class Foundation.JSONDecoder
import class Foundation.JSONEncoder
import struct Foundation.URL

extension DispatchQueue {
func awaitingAsync<T>(_ closure: @escaping () throws -> T) async throws -> T {
try await withCheckedThrowingContinuation { continuation in
self.async {
do {
try continuation.resume(returning: closure())
} catch {
continuation.resume(throwing: error)
}
}
}
}
}

struct FilePackageCollectionsSourcesStorage: PackageCollectionsSourcesStorage {
let fileSystem: FileSystem
let path: AbsolutePath
Expand All @@ -48,63 +34,51 @@ struct FilePackageCollectionsSourcesStorage: PackageCollectionsSourcesStorage {
}

func list() async throws -> [PackageCollectionsModel.CollectionSource] {
Copy link
Contributor

@MaxDesiatov MaxDesiatov Aug 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are no async calls in this and other methods that would require await, maybe async effect and locking from it should be removed, while the type itself made an actor to synchronize access?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have two concerns with this.
1- It looks like motivating reason for the callbacks was parallel decoding of large collections. Maybe that is nontrivial necessary and was premature optimization.
2- This could be made an actor, but it does not solve synchronization between processes. If more than one process is accessing the same storage on disk then the synchronization needs to be done with file locks not swift concurrency isolation

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I missed those were file locks, thanks for the clarification

try await DispatchQueue.sharedConcurrent.awaitingAsync {
try self.withLock {
try self.loadFromDisk()
}
try self.withLock {
try self.loadFromDisk()
}
}

func add(source: PackageCollectionsModel.CollectionSource, order: Int? = nil) async throws {
try await DispatchQueue.sharedConcurrent.awaitingAsync {
try self.withLock {
var sources = try self.loadFromDisk()
sources = sources.filter { $0 != source }
let order = order.flatMap { $0 >= 0 && $0 < sources.endIndex ? order : sources.endIndex } ?? sources.endIndex
sources.insert(source, at: order)
try self.saveToDisk(sources)
}
try self.withLock {
var sources = try self.loadFromDisk()
sources = sources.filter { $0 != source }
let order = order.flatMap { $0 >= 0 && $0 < sources.endIndex ? order : sources.endIndex } ?? sources.endIndex
sources.insert(source, at: order)
try self.saveToDisk(sources)
}
}

func remove(source: PackageCollectionsModel.CollectionSource) async throws {
try await DispatchQueue.sharedConcurrent.awaitingAsync {
try self.withLock {
var sources = try self.loadFromDisk()
sources = sources.filter { $0 != source }
try self.saveToDisk(sources)
}
try self.withLock {
var sources = try self.loadFromDisk()
sources = sources.filter { $0 != source }
try self.saveToDisk(sources)
}
}

func move(source: PackageCollectionsModel.CollectionSource, to order: Int) async throws {
try await DispatchQueue.sharedConcurrent.awaitingAsync {
try self.withLock {
var sources = try self.loadFromDisk()
sources = sources.filter { $0 != source }
let order = order >= 0 && order < sources.endIndex ? order : sources.endIndex
sources.insert(source, at: order)
try self.saveToDisk(sources)
}
try self.withLock {
var sources = try self.loadFromDisk()
sources = sources.filter { $0 != source }
let order = order >= 0 && order < sources.endIndex ? order : sources.endIndex
sources.insert(source, at: order)
try self.saveToDisk(sources)
}
}

func exists(source: PackageCollectionsModel.CollectionSource) async throws -> Bool {
try await DispatchQueue.sharedConcurrent.awaitingAsync {
try self.withLock {
try self.loadFromDisk()
}.contains(source)
}
try self.withLock {
try self.loadFromDisk()
}.contains(source)
}

func update(source: PackageCollectionsModel.CollectionSource) async throws {
try await DispatchQueue.sharedConcurrent.awaitingAsync {
try self.withLock {
var sources = try self.loadFromDisk()
if let index = sources.firstIndex(where: { $0 == source }) {
sources[index] = source
try self.saveToDisk(sources)
}
try self.withLock {
var sources = try self.loadFromDisk()
if let index = sources.firstIndex(where: { $0 == source }) {
sources[index] = source
try self.saveToDisk(sources)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,48 +19,35 @@ public protocol PackageCollectionsStorage {
///
/// - Parameters:
/// - collection: The `PackageCollection`
/// - callback: The closure to invoke when result becomes available
@available(*, noasync, message: "Use the async alternative")
func put(collection: PackageCollectionsModel.Collection,
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void)
func put(collection: PackageCollectionsModel.Collection) async throws -> PackageCollectionsModel.Collection

/// Removes `PackageCollection` from storage.
///
/// - Parameters:
/// - identifier: The identifier of the `PackageCollection`
/// - callback: The closure to invoke when result becomes available
@available(*, noasync, message: "Use the async alternative")
func remove(identifier: PackageCollectionsModel.CollectionIdentifier,
callback: @escaping (Result<Void, Error>) -> Void)
func remove(identifier: PackageCollectionsModel.CollectionIdentifier) async throws

/// Returns `PackageCollection` for the given identifier.
///
/// - Parameters:
/// - identifier: The identifier of the `PackageCollection`
/// - callback: The closure to invoke when result becomes available
@available(*, noasync, message: "Use the async alternative")
func get(identifier: PackageCollectionsModel.CollectionIdentifier,
callback: @escaping (Result<PackageCollectionsModel.Collection, Error>) -> Void)
func get(identifier: PackageCollectionsModel.CollectionIdentifier) async throws -> PackageCollectionsModel.Collection

/// 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
@available(*, noasync, message: "Use the async alternative")
func list(identifiers: [PackageCollectionsModel.CollectionIdentifier]?,
callback: @escaping (Result<[PackageCollectionsModel.Collection], Error>) -> Void)
func list(identifiers: [PackageCollectionsModel.CollectionIdentifier]?) async throws -> [PackageCollectionsModel.Collection]

/// 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
@available(*, noasync, message: "Use the async alternative")
func searchPackages(identifiers: [PackageCollectionsModel.CollectionIdentifier]?,
query: String,
callback: @escaping (Result<PackageCollectionsModel.PackageSearchResult, Error>) -> Void)
func searchPackages(
identifiers: [PackageCollectionsModel.CollectionIdentifier]?,
query: String
) async throws -> PackageCollectionsModel.PackageSearchResult

/// Returns packages for the given package identity.
///
Expand All @@ -69,72 +56,20 @@ public protocol PackageCollectionsStorage {
/// - Parameters:
/// - identifier: The package identifier
/// - collectionIdentifiers: Optional. The identifiers of the `PackageCollection`s
/// - callback: The closure to invoke when result becomes available
@available(*, noasync, message: "Use the async alternative")
func findPackage(identifier: PackageIdentity,
collectionIdentifiers: [PackageCollectionsModel.CollectionIdentifier]?,
callback: @escaping (Result<(packages: [PackageCollectionsModel.Package], collections: [PackageCollectionsModel.CollectionIdentifier]), Error>) -> Void)
func findPackage(
identifier: PackageIdentity,
collectionIdentifiers: [PackageCollectionsModel.CollectionIdentifier]?
) async throws -> (packages: [PackageCollectionsModel.Package], collections: [PackageCollectionsModel.CollectionIdentifier])

/// 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
@available(*, noasync, message: "Use the async alternative")
func searchTargets(identifiers: [PackageCollectionsModel.CollectionIdentifier]?,
query: String,
type: PackageCollectionsModel.TargetSearchType,
callback: @escaping (Result<PackageCollectionsModel.TargetSearchResult, Error>) -> Void)
}

public extension PackageCollectionsStorage {
func put(collection: PackageCollectionsModel.Collection) async throws -> PackageCollectionsModel.Collection {
try await withCheckedThrowingContinuation {
self.put(collection: collection, callback: $0.resume(with:))
}
}
func remove(identifier: PackageCollectionsModel.CollectionIdentifier) async throws {
try await withCheckedThrowingContinuation {
self.remove(identifier: identifier, callback: $0.resume(with:))
}
}
func get(identifier: PackageCollectionsModel.CollectionIdentifier) async throws -> PackageCollectionsModel.Collection {
try await withCheckedThrowingContinuation {
self.get(identifier: identifier, callback: $0.resume(with:))
}
}
func list(identifiers: [PackageCollectionsModel.CollectionIdentifier]? = nil) async throws -> [PackageCollectionsModel.Collection] {
try await withCheckedThrowingContinuation {
self.list(identifiers: identifiers, callback: $0.resume(with:))
}
}

func searchPackages(
identifiers: [PackageCollectionsModel.CollectionIdentifier]? = nil,
query: String
) async throws -> PackageCollectionsModel.PackageSearchResult {
try await withCheckedThrowingContinuation {
self.searchPackages(identifiers: identifiers, query: query, callback: $0.resume(with:))
}
}
func findPackage(
identifier: PackageIdentity,
collectionIdentifiers: [PackageCollectionsModel.CollectionIdentifier]? = nil
) async throws -> (packages: [PackageCollectionsModel.Package], collections: [PackageCollectionsModel.CollectionIdentifier]) {
try await withCheckedThrowingContinuation {
self.findPackage(identifier: identifier, collectionIdentifiers: collectionIdentifiers, callback: $0.resume(with:))
}
}

func searchTargets(
identifiers: [PackageCollectionsModel.CollectionIdentifier]? = nil,
identifiers: [PackageCollectionsModel.CollectionIdentifier]?,
query: String,
type: PackageCollectionsModel.TargetSearchType
) async throws -> PackageCollectionsModel.TargetSearchResult {
try await withCheckedThrowingContinuation {
self.searchTargets(identifiers: identifiers, query: query, type: type, callback: $0.resume(with:))
}
}
) async throws -> PackageCollectionsModel.TargetSearchResult
}
Loading