-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[QD-7638] Support multiple (& possibly different) fingerprint versions (
#13)
- Loading branch information
Showing
11 changed files
with
281 additions
and
136 deletions.
There are no files selected for viewing
103 changes: 103 additions & 0 deletions
103
sarif/src/main/java/com/jetbrains/qodana/sarif/baseline/Baseline.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package com.jetbrains.qodana.sarif.baseline | ||
|
||
import com.jetbrains.qodana.sarif.baseline.BaselineCalculation.Options | ||
import com.jetbrains.qodana.sarif.model.Result | ||
import com.jetbrains.qodana.sarif.model.Result.BaselineState | ||
import com.jetbrains.qodana.sarif.model.Run | ||
|
||
private typealias FingerprintIndex = MultiMap<String, Result> | ||
|
||
private fun <T : Any> Iterable<T?>?.noNulls(): Sequence<T> = | ||
this?.asSequence().orEmpty().filterNotNull() | ||
|
||
private val Result.equalIndicators: Sequence<String> | ||
get() = partialFingerprints?.getValues(BaselineCalculation.EQUAL_INDICATOR)?.entries | ||
.noNulls() | ||
.map { (k, v) -> "$k:$v" } | ||
.sortedDescending() // higher versions should have higher priority | ||
|
||
internal class DiffState(private val options: Options) { | ||
var new = 0 | ||
private set | ||
var unchanged = 0 | ||
private set | ||
var absent = 0 | ||
private set | ||
|
||
val results = mutableListOf<Result>() | ||
|
||
fun put(result: Result, state: BaselineState): Boolean { | ||
if (state == BaselineState.UNCHANGED && !options.includeUnchanged) return false | ||
if (state == BaselineState.ABSENT && !options.includeAbsent) return false | ||
|
||
results.add(result.withBaselineState(if (options.fillBaselineState) state else null)) | ||
when (state) { | ||
BaselineState.NEW -> new++ | ||
BaselineState.UNCHANGED -> unchanged++ | ||
BaselineState.ABSENT -> absent++ | ||
BaselineState.UPDATED -> Unit | ||
} | ||
return true | ||
} | ||
} | ||
|
||
/** CAUTION: This mutates results in report and baseline **/ | ||
internal fun applyBaseline(report: Run, baseline: Run, options: Options): DiffState { | ||
val state = DiffState(options) | ||
|
||
val reportDescriptors = DescriptorLookup(report) | ||
val baselineDescriptors = DescriptorLookup(baseline) | ||
val reportIndex = FingerprintIndex() | ||
val baselineCounter = Counter<ResultKey>() | ||
|
||
val undecidedFromReport = report.results.noNulls() | ||
.filterNot { it.baselineState == BaselineState.ABSENT } | ||
.onEach { result -> | ||
result.equalIndicators | ||
.forEach { print -> reportIndex.add(print, result) } | ||
} | ||
.toCollection(IdentitySet(report.results?.size ?: 0)) | ||
|
||
val undecidedFromBaseline = buildList { | ||
baseline.results.noNulls() | ||
.filterNot { it.baselineState == BaselineState.ABSENT } | ||
.onEach { result -> baselineCounter.increment(ResultKey(result)) } | ||
.forEach { result -> | ||
val removedFromReport = result.equalIndicators | ||
.flatMap(reportIndex::getOrEmpty) | ||
.any(undecidedFromReport::remove) | ||
|
||
if (removedFromReport || !options.wasChecked.apply(result)) { | ||
state.put(result, BaselineState.UNCHANGED) | ||
} else { | ||
add(result) | ||
} | ||
} | ||
} | ||
|
||
|
||
undecidedFromReport.forEach { result -> | ||
val key = ResultKey(result) | ||
val inBaseline = baselineCounter[key] | ||
if (inBaseline <= 0) { | ||
state.put(result, BaselineState.NEW) | ||
} else { | ||
baselineCounter.decrement(key) | ||
state.put(result, BaselineState.UNCHANGED) | ||
} | ||
} | ||
|
||
undecidedFromBaseline | ||
.asSequence() | ||
.filter { baselineCounter.decrement(ResultKey(it)) >= 0 } | ||
.forEach { result -> | ||
val added = state.put(result, BaselineState.ABSENT) | ||
if (added && reportDescriptors.findById(result.ruleId) == null) { | ||
baselineDescriptors.findById(result.ruleId)?.addTo(report) | ||
} | ||
} | ||
|
||
report.withResults(state.results) | ||
|
||
return state | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
sarif/src/main/java/com/jetbrains/qodana/sarif/baseline/BaselineCollections.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.jetbrains.qodana.sarif.baseline | ||
|
||
import java.util.* | ||
|
||
internal class MultiMap<K, V> private constructor( | ||
private val underlying: MutableMap<K, MutableList<V>> | ||
) : Iterable<Map.Entry<K, List<V>>> { | ||
constructor() : this(mutableMapOf()) | ||
|
||
fun add(key: K, value: V) { | ||
underlying.compute(key) { _, old -> old?.apply { add(value) } ?: mutableListOf(value) } | ||
} | ||
|
||
fun getOrEmpty(key: K): MutableList<V> = underlying[key] ?: mutableListOf() | ||
|
||
override fun iterator(): Iterator<Map.Entry<K, List<V>>> = underlying.iterator() | ||
} | ||
|
||
internal class IdentitySet<T> private constructor( | ||
private val map: IdentityHashMap<T, Int> | ||
) : MutableSet<T> by map.keys { | ||
constructor(expectedSize: Int) : this(IdentityHashMap(expectedSize)) | ||
|
||
private var insertCounter = 0 | ||
|
||
override fun add(element: T): Boolean = map.putIfAbsent(element, insertCounter++) == null | ||
override fun addAll(elements: Collection<T>): Boolean = elements.fold(false) { r, e -> add(e) || r } | ||
|
||
override fun iterator(): MutableIterator<T> = object : MutableIterator<T> { | ||
private val underlying = map.entries.sortedBy(MutableMap.MutableEntry<T, Int>::value).iterator() | ||
|
||
override fun hasNext(): Boolean = underlying.hasNext() | ||
override fun next(): T = underlying.next().key | ||
override fun remove() = throw UnsupportedOperationException("Cannot remove elements from this iterator") | ||
} | ||
} | ||
|
||
internal class Counter<T> { | ||
private val underlying: MutableMap<T, Int> = mutableMapOf() | ||
|
||
operator fun get(key: T) = underlying.getOrDefault(key, 0) | ||
fun increment(key: T) = underlying.compute(key) { _, o -> (o ?: 0).inc() }!! | ||
fun decrement(key: T) = underlying.compute(key) { _, o -> (o ?: 0).dec() }!! | ||
} |
Oops, something went wrong.