Skip to content

Commit

Permalink
Small refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Кирилл committed Aug 6, 2024
1 parent df675d6 commit c7d474f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ open class GenericUpstreamCreator(
private val connectorFactoryCreatorResolver: ConnectorFactoryCreatorResolver,
private val versionRules: Supplier<CompatibleVersionsRules?>,
) : UpstreamCreator(chainsConfig, indexConfig, callTargets) {
private val hashes: MutableMap<Short, Boolean> = HashMap()
private val hashes = HashSet<Short>()

override fun createUpstream(
upstreamsConfig: UpstreamsConfig.Upstream<*>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,28 +22,28 @@ abstract class UpstreamCreator(
protected val log: Logger = LoggerFactory.getLogger(this::class.java)

companion object {
fun getHash(nodeId: Int?, obj: Any, hashes: MutableMap<Short, Boolean>): Short =
nodeId?.toShort()?.also {
hashes[it] = true
} ?: (obj.hashCode() % 65535).let {
if (it == 0) 1 else it
}.let { nonZeroHash ->
listOf<Function<Int, Int>>(
Function { i -> i },
Function { i -> (-i) },
Function { i -> 32767 - abs(i) },
Function { i -> abs(i) - 32768 },
).map {
it.apply(nonZeroHash).toShort()
}.firstOrNull {
hashes[it] != true
}?.let {
hashes[it] = true
it
} ?: (Short.MIN_VALUE..Short.MAX_VALUE).first {
it != 0 && hashes[it.toShort()] != true
}.toShort().also { hashes[it] = true }
}
fun getHash(nodeId: Int?, obj: Any, hashes: MutableSet<Short>): Short {
val hash = nodeId?.toShort()
?: run {
(obj.hashCode() % 65535)
.let { if (it == 0) 1 else it }
.let { nonZeroHash ->
listOf<Function<Int, Int>>(
Function { i -> i },
Function { i -> (-i) },
Function { i -> 32767 - abs(i) },
Function { i -> abs(i) - 32768 },
)
.map { it.apply(nonZeroHash).toShort() }
.firstOrNull { !hashes.contains(it) }
}
?: (Short.MIN_VALUE..Short.MAX_VALUE).first {
it != 0 && !hashes.contains(it.toShort())
}.toShort()
}

return hash.also { hashes.add(it) }
}
}

fun createUpstream(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class GrpcUpstreamCreator(
@Value("\${spring.application.max-metadata-size}")
private var maxMetadataSize: Int = Defaults.maxMetadataSize

private val hashes: MutableMap<Short, Boolean> = HashMap()
private val hashes = HashSet<Short>()

companion object {
val grpcUpstreamsScheduler: Scheduler = Schedulers.fromExecutorService(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ class UpstreamCreatorTest {
fun `test getHash`(
inputHash: Int?,
obj: Any,
answer: Short
answer: Short,
) {
val hash = UpstreamCreator.getHash(inputHash, obj, hashes)

assertThat(hash).isEqualTo(answer)
}

companion object {
private val hashes: MutableMap<Short, Boolean> = HashMap()
private val hashes = HashSet<Short>()

@JvmStatic
fun data(): List<Arguments> =
Expand All @@ -32,7 +32,7 @@ class UpstreamCreatorTest {
Arguments.of(null, 49, (32718).toShort()),
Arguments.of(null, 49, (-32719).toShort()),
Arguments.of(null, 49, (-32768).toShort()),
Arguments.of(null, 49, (-32767).toShort())
Arguments.of(null, 49, (-32767).toShort()),
)
}
}

0 comments on commit c7d474f

Please sign in to comment.