Skip to content

Commit ab7f4e3

Browse files
committed
Improve resolution speed
1 parent 4678783 commit ab7f4e3

File tree

4 files changed

+34
-18
lines changed

4 files changed

+34
-18
lines changed

.idea/.gitignore

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/main/kotlin/org/cosmic/ide/dependency/resolver/Main.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ import kotlin.time.measureTime
66

77
@OptIn(ExperimentalTime::class)
88
suspend fun main() {
9-
val artifact = getArtifact("dev.kord", "kord-core", "0.13.1")
9+
val artifact = getArtifact("com.onesignal", "OneSignal", "5.1.13")
1010
val dir = File("test")
1111
dir.deleteRecursively()
1212
dir.mkdir()
1313
val time = measureTime {
1414
println("Starting...")
15-
artifact?.downloadArtifact(dir)
15+
artifact?.resolve()
1616
}
1717
println("Total time: $time")
1818
}

lib/src/main/kotlin/org/cosmic/ide/dependency/resolver/api/Artifact.kt

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import java.io.File
1515
import java.io.InputStream
1616
import java.net.SocketException
1717
import java.net.URL
18-
import java.util.concurrent.ConcurrentHashMap
1918
import java.util.concurrent.ConcurrentLinkedQueue
2019
import javax.xml.parsers.DocumentBuilderFactory
2120

@@ -49,27 +48,24 @@ data class Artifact(
4948
}
5049
}
5150

52-
suspend fun resolve(resolved: MutableSet<Artifact> = ConcurrentHashMap.newKeySet()): ConcurrentLinkedQueue<Artifact> {
51+
suspend fun resolve(resolved: ConcurrentLinkedQueue<Artifact> = ConcurrentLinkedQueue<Artifact>()): ConcurrentLinkedQueue<Artifact> {
5352
val pom = getPOM() ?: return ConcurrentLinkedQueue()
54-
val deps = ConcurrentLinkedQueue(pom.resolvePOM(ConcurrentLinkedQueue(resolved)))
53+
val deps = ConcurrentLinkedQueue(pom.resolvePOM(resolved))
5554
val artifacts = ConcurrentLinkedQueue<Artifact>()
5655
deps.parallelForEach { dep ->
5756
eventReciever.onResolving(this, dep)
58-
if (dep.version.isBlank()) {
59-
eventReciever.onFetchingLatestVersion(dep)
60-
val factory = DocumentBuilderFactory.newInstance()
61-
val builder = factory.newDocumentBuilder()
62-
val doc = builder.parse(dep.getMavenMetadata().byteInputStream())
63-
val v = doc.getElementsByTagName("release").item(0)
64-
if (v != null) {
65-
dep.version = v.textContent
66-
eventReciever.onFetchedLatestVersion(dep, dep.version)
57+
58+
val saved = resolved.find { it.groupId == dep.groupId && it.artifactId == dep.artifactId }
59+
60+
if (saved != null) {
61+
val max = listOf(saved.version, dep.version).maxOrNull()
62+
if (saved.version != max) {
63+
println("Updating max of ${saved.version} to $max")
64+
saved.version = max ?: ""
6765
}
68-
}
69-
artifacts.add(dep)
70-
if (resolved.any { it.groupId == dep.groupId && it.artifactId == dep.artifactId && it.version >= dep.version }) {
7166
return@parallelForEach
7267
}
68+
artifacts.add(dep)
7369
resolved.add(dep)
7470
val depArtifacts = dep.resolve(resolved)
7571
eventReciever.onResolutionComplete(dep)

lib/src/main/kotlin/org/cosmic/ide/dependency/resolver/utils.kt

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,14 @@ suspend fun InputStream.resolvePOM(resolved: ConcurrentLinkedQueue<Artifact>): C
9292
}
9393
val item = dependencyElement.getElementsByTagName("version").item(0)?.textContent ?: ""
9494

95-
if (resolved.any { it.groupId == groupId && it.artifactId == artifactId && it.version >= item }) {
95+
if (resolved.any { it.groupId == groupId && it.artifactId == artifactId }) {
96+
val found = resolved.find { it.groupId == groupId && it.artifactId == artifactId }!!
97+
98+
if (found.version.isBlank() && item.isNotBlank()) {
99+
found.version = item
100+
} else {
101+
found.version = listOf(found.version, item).maxOrNull() ?: ""
102+
}
96103
eventReciever.onSkippingResolution(Artifact(groupId, artifactId, item))
97104
return@async
98105
}
@@ -126,6 +133,17 @@ suspend fun InputStream.resolvePOM(resolved: ConcurrentLinkedQueue<Artifact>): C
126133
}
127134
version = tag.textContent
128135
}
136+
137+
if (version.isBlank()) {
138+
eventReciever.onFetchingLatestVersion(artifact)
139+
val factory = DocumentBuilderFactory.newInstance()
140+
val builder = factory.newDocumentBuilder()
141+
val doc = builder.parse(metadata.byteInputStream())
142+
val v = doc.getElementsByTagName("release").item(0)
143+
if (v != null) {
144+
version = v.textContent
145+
}
146+
}
129147
artifact.version = version
130148
artifacts.add(artifact)
131149
}

0 commit comments

Comments
 (0)