|
12 | 12 |
|
13 | 13 | import SKCore
|
14 | 14 |
|
15 |
| -/// A task that either prepares targets or updates the index store for a set of files. |
16 |
| -public enum IndexTaskDescription: TaskDescriptionProtocol { |
17 |
| - case updateIndexStore(UpdateIndexStoreTaskDescription) |
18 |
| - case preparation(PreparationTaskDescription) |
| 15 | +/// Protocol of tasks that are executed on the index task scheduler. |
| 16 | +/// |
| 17 | +/// It is assumed that `IndexTaskDescription` of different types are allowed to execute in parallel. |
| 18 | +protocol IndexTaskDescription: TaskDescriptionProtocol { |
| 19 | + /// A string that is unique to this type of `IndexTaskDescription`. It is used to produce unique IDs for tasks of |
| 20 | + /// different types in `AnyIndexTaskDescription` |
| 21 | + static var idPrefix: String { get } |
| 22 | + |
| 23 | + var id: UInt32 { get } |
| 24 | +} |
| 25 | + |
| 26 | +extension IndexTaskDescription { |
| 27 | + func dependencies( |
| 28 | + to currentlyExecutingTasks: [AnyIndexTaskDescription] |
| 29 | + ) -> [TaskDependencyAction<AnyIndexTaskDescription>] { |
| 30 | + return self.dependencies(to: currentlyExecutingTasks.compactMap { $0.wrapped as? Self }) |
| 31 | + .map { |
| 32 | + switch $0 { |
| 33 | + case .cancelAndRescheduleDependency(let td): |
| 34 | + return .cancelAndRescheduleDependency(AnyIndexTaskDescription(td)) |
| 35 | + case .waitAndElevatePriorityOfDependency(let td): |
| 36 | + return .waitAndElevatePriorityOfDependency(AnyIndexTaskDescription(td)) |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +/// Type-erased wrapper of an `IndexTaskDescription`. |
| 44 | +public struct AnyIndexTaskDescription: TaskDescriptionProtocol { |
| 45 | + let wrapped: any IndexTaskDescription |
| 46 | + |
| 47 | + init(_ wrapped: any IndexTaskDescription) { |
| 48 | + self.wrapped = wrapped |
| 49 | + } |
19 | 50 |
|
20 | 51 | public var isIdempotent: Bool {
|
21 |
| - switch self { |
22 |
| - case .updateIndexStore(let taskDescription): return taskDescription.isIdempotent |
23 |
| - case .preparation(let taskDescription): return taskDescription.isIdempotent |
24 |
| - } |
| 52 | + return wrapped.isIdempotent |
25 | 53 | }
|
26 | 54 |
|
27 | 55 | public var estimatedCPUCoreCount: Int {
|
28 |
| - switch self { |
29 |
| - case .updateIndexStore(let taskDescription): return taskDescription.estimatedCPUCoreCount |
30 |
| - case .preparation(let taskDescription): return taskDescription.estimatedCPUCoreCount |
31 |
| - } |
| 56 | + return wrapped.estimatedCPUCoreCount |
32 | 57 | }
|
33 | 58 |
|
34 | 59 | public var id: String {
|
35 |
| - switch self { |
36 |
| - case .updateIndexStore(let taskDescription): return "indexing-\(taskDescription.id)" |
37 |
| - case .preparation(let taskDescription): return "preparation-\(taskDescription.id)" |
38 |
| - } |
| 60 | + return "\(type(of: wrapped).idPrefix)-\(wrapped.id)" |
39 | 61 | }
|
40 | 62 |
|
41 | 63 | public var description: String {
|
42 |
| - switch self { |
43 |
| - case .updateIndexStore(let taskDescription): return taskDescription.description |
44 |
| - case .preparation(let taskDescription): return taskDescription.description |
45 |
| - } |
| 64 | + return wrapped.description |
46 | 65 | }
|
47 | 66 |
|
48 | 67 | public var redactedDescription: String {
|
49 |
| - switch self { |
50 |
| - case .updateIndexStore(let taskDescription): return taskDescription.redactedDescription |
51 |
| - case .preparation(let taskDescription): return taskDescription.redactedDescription |
52 |
| - } |
| 68 | + return wrapped.redactedDescription |
53 | 69 | }
|
54 | 70 |
|
55 | 71 | public func execute() async {
|
56 |
| - switch self { |
57 |
| - case .updateIndexStore(let taskDescription): return await taskDescription.execute() |
58 |
| - case .preparation(let taskDescription): return await taskDescription.execute() |
59 |
| - } |
| 72 | + return await wrapped.execute() |
60 | 73 | }
|
61 | 74 |
|
62 | 75 | /// Forward to the underlying task to compute the dependencies. Preparation and index tasks don't have any
|
63 | 76 | /// dependencies that are managed by `TaskScheduler`. `SemanticIndexManager` awaits the preparation of a target before
|
64 | 77 | /// indexing files within it.
|
65 | 78 | public func dependencies(
|
66 |
| - to currentlyExecutingTasks: [IndexTaskDescription] |
67 |
| - ) -> [TaskDependencyAction<IndexTaskDescription>] { |
68 |
| - switch self { |
69 |
| - case .updateIndexStore(let taskDescription): |
70 |
| - let currentlyExecutingTasks = |
71 |
| - currentlyExecutingTasks |
72 |
| - .compactMap { (currentlyExecutingTask) -> UpdateIndexStoreTaskDescription? in |
73 |
| - if case .updateIndexStore(let currentlyExecutingTask) = currentlyExecutingTask { |
74 |
| - return currentlyExecutingTask |
75 |
| - } |
76 |
| - return nil |
77 |
| - } |
78 |
| - return taskDescription.dependencies(to: currentlyExecutingTasks).map { |
79 |
| - switch $0 { |
80 |
| - case .waitAndElevatePriorityOfDependency(let td): |
81 |
| - return .waitAndElevatePriorityOfDependency(.updateIndexStore(td)) |
82 |
| - case .cancelAndRescheduleDependency(let td): |
83 |
| - return .cancelAndRescheduleDependency(.updateIndexStore(td)) |
84 |
| - } |
85 |
| - } |
86 |
| - case .preparation(let taskDescription): |
87 |
| - let currentlyExecutingTasks = |
88 |
| - currentlyExecutingTasks |
89 |
| - .compactMap { (currentlyExecutingTask) -> PreparationTaskDescription? in |
90 |
| - if case .preparation(let currentlyExecutingTask) = currentlyExecutingTask { |
91 |
| - return currentlyExecutingTask |
92 |
| - } |
93 |
| - return nil |
94 |
| - } |
95 |
| - return taskDescription.dependencies(to: currentlyExecutingTasks).map { |
96 |
| - switch $0 { |
97 |
| - case .waitAndElevatePriorityOfDependency(let td): |
98 |
| - return .waitAndElevatePriorityOfDependency(.preparation(td)) |
99 |
| - case .cancelAndRescheduleDependency(let td): |
100 |
| - return .cancelAndRescheduleDependency(.preparation(td)) |
101 |
| - } |
102 |
| - } |
103 |
| - } |
| 79 | + to currentlyExecutingTasks: [AnyIndexTaskDescription] |
| 80 | + ) -> [TaskDependencyAction<AnyIndexTaskDescription>] { |
| 81 | + return wrapped.dependencies(to: currentlyExecutingTasks) |
104 | 82 | }
|
105 | 83 | }
|
0 commit comments