Skip to content
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
70 changes: 57 additions & 13 deletions src/main/java/com/mapconductor/core/OverlayCollector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ interface OverlayCollectorInterface<T : ComponentState> {
class OverlayCollector<T : ComponentState>(
private val updateDebounce: Duration,
scope: CoroutineScope = CoroutineScope(Dispatchers.Main.immediate),
) : OverlayCollectorInterface<T>,
StateMutationListener {
) : OverlayCollectorInterface<T> {
private val scope = scope
private val addSharedFlow = MutableSharedFlow<T>(1000)
private val removeSharedFlow = MutableSharedFlow<String>(1000)
Expand Down Expand Up @@ -112,6 +111,38 @@ class OverlayCollector<T : ComponentState>(
/** The membership [listenToWrittenStates] last listened to. */
@Volatile private var published: Map<String, T> = emptyMap()

/**
* What this collector listens through, replaced at every
* [listenToWrittenStates] pass. A state's listener says whether it reports
* here (a [PassToken] of this collector) and whether this pass has seen it
* (the current one): telling which states left a bulk list takes one read
* per state, not a lookup of each id in the other map.
*/
@Volatile private var token = PassToken(this)

@InternalMapConductorApi
private class PassToken(
val collector: OverlayCollector<*>,
) : StateMutationListener {
override fun onMutated(state: ComponentState) = collector.onMutated(state, this)
}

/** Each state of [states] with its id, walking an [OverlayStateList]'s list rather than its index. */
private inline fun forEachState(
states: Map<String, T>,
action: (id: String, state: T) -> Unit,
) {
if (states is OverlayStateList<*>) {
@Suppress("UNCHECKED_CAST")
(states as OverlayStateList<T>).forEachState { action(it.id, it) }
} else {
for ((id, state) in states) action(id, state)
}
}

private fun tokenOf(state: T): PassToken? =
(state.mutations.currentListener as? PassToken)?.takeIf { it.collector === this }

init {
scope.launch {
addSharedFlow.debounceBatch(5.milliseconds, 100).collect { states ->
Expand Down Expand Up @@ -147,23 +178,31 @@ class OverlayCollector<T : ComponentState>(
private fun listenToWrittenStates(states: Map<String, T>) {
val previous = published
published = states
val pass = PassToken(this)
token = pass
val anyPending = pendingAdds.isNotEmpty()
for ((id, state) in states) {
forEachState(states) { id, state ->
if (anyPending) {
val pending = pendingAdds.remove(id)
if (pending !== null && pending !== state) stop(pending)
}
// Already reporting here: whatever held its id before was stopped
// when it started. Most of a bulk list is, from the last pass.
if (state.mutations.isListenedBy(this)) continue
val old = previous[id]
if (old !== null && old !== state) stop(old)
listen(state)
if (tokenOf(state) == null) {
val old = previous[id]
if (old !== null && old !== state) stop(old)
}
state.mutations.listen(pass, state)
}
// What the last pass listened to and this one did not see has left.
// Only stop what this path started: a state added through [add] may not
// have reached [flow] yet (membership is debounced).
for ((id, state) in previous) {
if (id !in states && pendingAdds[id] !== state) stop(state)
val stillPending = pendingAdds.isNotEmpty()
forEachState(previous) { id, state ->
val heard = tokenOf(state)
if (heard != null && heard !== pass && (!stillPending || pendingAdds[id] !== state)) {
state.mutations.stopListening(heard)
}
}
}

Expand All @@ -181,11 +220,11 @@ class OverlayCollector<T : ComponentState>(
}

private fun listen(state: T) {
state.mutations.listen(this, state)
state.mutations.listen(token, state)
}

private fun stop(state: T) {
state.mutations.stopListening(this)
tokenOf(state)?.let { state.mutations.stopListening(it) }
}

/**
Expand All @@ -194,10 +233,15 @@ class OverlayCollector<T : ComponentState>(
* state left listening by a membership change this collector never saw
* (a [flow] value replaced before it was collected) is dropped here.
*/
override fun onMutated(state: ComponentState) {
private fun onMutated(
state: ComponentState,
via: PassToken,
) {
val channel = mutated ?: return
val id = state.id
if (pendingAdds[id] !== state && flow.value[id] !== state && published[id] !== state) return
// Heard through the latest pass's token: the latest published membership
// has it. Otherwise look it up (pending adds, a newer flow value).
if (via !== token && pendingAdds[id] !== state && flow.value[id] !== state && published[id] !== state) return
@Suppress("UNCHECKED_CAST")
channel.trySend(state as T)
}
Expand Down
67 changes: 67 additions & 0 deletions src/main/java/com/mapconductor/core/OverlayStateMaps.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,70 @@ fun overlayStateIds(states: Iterable<ComponentState>): Set<String> =
persistentHashSetOf<String>().builder().apply {
for (state in states) add(state.id)
}.build()

/**
* A collector's membership handed over as the list a collection was given,
* for collections of hundreds of thousands of states: building and walking a
* map of them cost seconds per hand-over, and the readers that matter (the
* collector and the overlays) only walk the states in order.
*
* [states] is the list itself. Looking a key up builds an index on first use.
* Like every published generation it is not to be written to; a write goes to
* the index and [states] then follows the index.
*
* The ids in [list] must be distinct: [states] and [forEachState] do not
* deduplicate.
*/
@InternalMapConductorApi
class OverlayStateList<T : ComponentState>(
private val list: List<T>,
) : AbstractMutableMap<String, T>() {
@Volatile private var index: LinkedHashMap<String, T>? = null

@Volatile private var written = false

private fun index(): LinkedHashMap<String, T> =
index ?: synchronized(this) {
index ?: LinkedHashMap<String, T>((list.size / 0.75f).toInt() + 1).also { map ->
for (state in list) map[state.id] = state
index = map
}
}

/** The states in order: the list handed over, unless this map was written to. */
fun states(): List<T> = if (written) ArrayList(index().values) else list

/** Calls [action] for each state in order, without building the index. */
inline fun forEachState(action: (T) -> Unit) {
for (state in states()) action(state)
}

/** The ids, through the index. */
val ids: Set<String> get() = index().keys

override val size: Int get() = if (written) index().size else list.size

override val entries: MutableSet<MutableMap.MutableEntry<String, T>> get() = index().entries

override fun get(key: String): T? = index()[key]

override fun containsKey(key: String): Boolean = index().containsKey(key)

override fun put(
key: String,
value: T,
): T? {
written = true
return index().put(key, value)
}

override fun remove(key: String): T? {
written = true
return index().remove(key)
}

override fun clear() {
written = true
index().clear()
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/mapconductor/core/StateMutationSignal.kt
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ class StateMutationSignal {
this.listener = listener
}

/** Whether writes are reported to [listener]. */
internal fun isListenedBy(listener: StateMutationListener): Boolean = this.listener === listener
/** Who writes are reported to now: a `() -> Unit`, a [StateMutationListener], or null. */
internal val currentListener: Any? get() = listener

/** Stops reporting, but only if [listener] is the one reported to. */
internal fun stopListening(listener: StateMutationListener) {
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/com/mapconductor/core/circle/CircleOverlay.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mapconductor.core.circle

import com.mapconductor.core.OverlayAddedTracker
import com.mapconductor.core.OverlayStateList
import com.mapconductor.core.controller.BaseMapViewController
import com.mapconductor.core.controller.MapViewControllerInterface
import com.mapconductor.core.map.MapOverlayInterface
Expand Down Expand Up @@ -29,8 +30,10 @@ class CircleOverlay
tilingOptions?.value?.let { options ->
(controller as? BaseMapViewController)?.circleTilers()?.forEach { it.options = options }
}
val states = data.values.toList()
@Suppress("UNCHECKED_CAST")
val list = data as? OverlayStateList<CircleState>
val states = list?.states() ?: data.values.toList()
capable.compositionCircles(states)
added.synced(controller, states, capable::hasCircle, sourceIds = data.keys)
added.synced(controller, states, capable::hasCircle, sourceIds = list?.ids ?: data.keys)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,56 @@ class OverlayCollectorMutationTest {
assertEquals(setOf("single", "bulk"), collector.flow.value.keys)
}

@Test
fun aBulkListThatDropsAndRestoresSomeStatesKeepsTheRestReporting() = runTest {
val collector = collector(StandardTestDispatcher(testScheduler))
val seen = mutableListOf<String>()
collector.setUpdateHandler { seen.add("${it.id}=${it.label}") }
val states = List(5) { Thing("s$it", "0") }
fun publish(list: List<Thing>) {
collector.flow.value = copyOverlayStates(list.associateBy { it.id })
}
publish(states)
advanceUntilIdle()
publish(states.filterIndexed { index, _ -> index % 2 == 0 })
advanceUntilIdle()
states.forEach { it.label = "1" }
advanceUntilIdle()
assertEquals(listOf("s0=1", "s2=1", "s4=1"), seen.sorted())
seen.clear()
publish(states)
advanceUntilIdle()
states.forEach { it.label = "2" }
advanceUntilIdle()
assertEquals(states.map { "${it.id}=2" }, seen.sorted())
}

@Test
fun aListHandOverReportsWritesOfItsStatesOnly() = runTest {
val collector = collector(StandardTestDispatcher(testScheduler))
val seen = mutableListOf<String>()
collector.setUpdateHandler { seen.add("${it.id}=${it.label}") }
val states = List(4) { Thing("l$it", "0") }
collector.flow.value = OverlayStateList(states)
advanceUntilIdle()
collector.flow.value = OverlayStateList(states.take(2))
advanceUntilIdle()
states.forEach { it.label = "1" }
advanceUntilIdle()
assertEquals(listOf("l0=1", "l1=1"), seen.sorted())
}

@Test
fun aWrittenListFollowsItsIndex() {
val states = List(3) { Thing("w$it", "0") }
val list = OverlayStateList(states)
assertEquals(states, list.states())
assertEquals(states[1], list["w1"])
list.remove("w1")
assertEquals(listOf(states[0], states[2]), list.states())
assertEquals(setOf("w0", "w2"), list.keys)
}

private fun collector(dispatcher: TestDispatcher) =
OverlayCollector<Thing>(
updateDebounce = window,
Expand Down
Loading