maps: bring the new search code within detekt's limits (#mapsearch)
- Commit
56d3faa198c2c2cc1886debfaf37db5695ce683e- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-13 21:40 UTC (22 days ago)
- Parents
f7e4bdd
The container run of `:maps:detekt` on the branch tip reported 7 errors, all in code added by this branch (the same task passed on origin/main + the first two commits): * ComplexCondition: forEachCellInBox's grid guard (now gridUsable()) and prefixRange's argument guard (split in two). * CyclomaticComplexMethod: CandidateSearch.collectMaterialized (16; split into intersectScanned + joinRings) and AdminTable.localityOf (16; now collects the parent chain once and picks city/state/country from it). * MaxLineLength (120): three lines in PoiSearchEngine.kt. No behaviour change: same results and page counts on the planet benchmark; POI unit tests (PoiIndexTest, PoiIndexSmartTest, PoiArchiveTest, PoiIndexAdminTest) pass standalone.
Diff
maps/src/main/java/com/vayunmathur/maps/util/PoiIndexAdmin.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/util/PoiIndexAdmin.kt b/maps/src/main/java/com/vayunmathur/maps/util/PoiIndexAdmin.ktindex f6be25eb87..ece783a84a 100644--- a/maps/src/main/java/com/vayunmathur/maps/util/PoiIndexAdmin.kt+++ b/maps/src/main/java/com/vayunmathur/maps/util/PoiIndexAdmin.kt@@ -197,28 +197,26 @@ private fun PoiIndex.Mapped.cachedBoundaryCity(region: Long, ordinal: Int): Stri /** The hierarchy walk for boundary [tagged]; see the file comment for the level rules. */ internal fun AdminTable.localityOf(tagged: Long, english: Boolean): String? {- var city: String? = null- var deepest: String? = null- var state: String? = null- var region: String? = null- var country: String? = null+ val chain = chainOf(tagged, english)+ if (chain.isEmpty()) return null+ // The first (deepest) name per level, walking up.+ fun first(levels: IntRange): String? =+ chain.firstOrNull { (level, name) -> level in levels && name != null }?.second+ val city = first(CITY_MIN_LEVEL..CITY_MAX_LEVEL) ?: first(CITY_MAX_LEVEL + 1..Int.MAX_VALUE)+ val state = first(STATE_LEVEL..STATE_LEVEL) ?: first(REGION_LEVEL..REGION_LEVEL)+ return joinLocality(city, state, first(COUNTRY_LEVEL..COUNTRY_LEVEL))+}++/** `(admin_level, name)` from boundary [tagged] up to the top, at most [MAX_CHAIN] long. */+private fun AdminTable.chainOf(tagged: Long, english: Boolean): List<Pair<Int, String?>> {+ val out = ArrayList<Pair<Int, String?>>() var at = find(tagged)- var steps = 0- while (at >= 0 && steps < MAX_CHAIN) {- val level = level(at)- val name = name(at, english)- when {- level == COUNTRY_LEVEL -> country = country ?: name- level == REGION_LEVEL -> region = region ?: name- level == STATE_LEVEL -> state = state ?: name- level in CITY_MIN_LEVEL..CITY_MAX_LEVEL -> city = city ?: name- level > CITY_MAX_LEVEL -> deepest = deepest ?: name- }+ while (at >= 0 && out.size < MAX_CHAIN) {+ out.add(level(at) to name(at, english)) val parent = parent(at) at = if (parent == REGION_NONE) -1 else find(parent)- steps++ }- return joinLocality(city ?: deepest, state ?: region, country)+ return out } /** The non-blank parts joined with ", ", consecutive repeats dropped ("Singapore, Singapore"). */
maps/src/main/java/com/vayunmathur/maps/util/PoiIndexSpatial.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/util/PoiIndexSpatial.kt b/maps/src/main/java/com/vayunmathur/maps/util/PoiIndexSpatial.ktindex 4b9e061e45..187e8dd1b4 100644--- a/maps/src/main/java/com/vayunmathur/maps/util/PoiIndexSpatial.kt+++ b/maps/src/main/java/com/vayunmathur/maps/util/PoiIndexSpatial.kt@@ -26,6 +26,10 @@ private fun PoiIndex.Mapped.axis(value: Int, origin: Int): Int { private fun PoiIndex.Mapped.row(latE7: Int): Int = axis(latE7, lat0E7) private fun PoiIndex.Mapped.col(lonE7: Int): Int = axis(lonE7, lon0E7).coerceAtMost(cols - 1) +/** Whether the CSR grid is present and its header describes a usable grid. */+private fun PoiIndex.Mapped.gridUsable(): Boolean =+ spatial != null && cellCount > 0 && minOf(cols, cellE7) > 0+ /** The [i]th entry of the ascending populated-cell-id array. */ private fun PoiIndex.Mapped.cellIdAt(i: Int): Int = spatial!!.getInt(PoiIndex.SPATIAL_HEADER_BYTES + CELL_SLOT_BYTES * i)@@ -56,7 +60,7 @@ internal fun PoiIndex.Mapped.forEachCellInBox( maxLonE7: Int, onCell: (cellIndex: Int) -> Boolean, ) {- if (spatial == null || cellCount <= 0 || cols <= 0 || cellE7 <= 0) return+ if (!gridUsable()) return val c0 = col(minLonE7) val c1 = col(maxLonE7) for (r in row(minLatE7)..row(maxLatE7)) {
maps/src/main/java/com/vayunmathur/maps/util/PoiIndexWordSearch.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/util/PoiIndexWordSearch.kt b/maps/src/main/java/com/vayunmathur/maps/util/PoiIndexWordSearch.ktindex 4d364c3492..d5efbc06eb 100644--- a/maps/src/main/java/com/vayunmathur/maps/util/PoiIndexWordSearch.kt+++ b/maps/src/main/java/com/vayunmathur/maps/util/PoiIndexWordSearch.kt@@ -25,7 +25,8 @@ private const val SCAN_POLL_MASK = 0xFFFF /** Entries `[first, last]` whose word starts with [key]; empty when none. */ internal fun PoiIndex.Mapped.prefixRange(key: ByteArray, within: IntRange = 0 until entryCount): IntRange {- if (nameIdx == null || entryCount == 0 || key.isEmpty() || within.isEmpty()) return IntRange.EMPTY+ if (nameIdx == null || entryCount == 0) return IntRange.EMPTY+ if (key.isEmpty() || within.isEmpty()) return IntRange.EMPTY val end = within.last + 1 val lo = lowerBoundWord(key, within.first, end) // Most spellings (an accented form nobody uses) are empty: one probe settles it
maps/src/main/java/com/vayunmathur/maps/util/PoiSearchEngine.kt
diff --git a/maps/src/main/java/com/vayunmathur/maps/util/PoiSearchEngine.kt b/maps/src/main/java/com/vayunmathur/maps/util/PoiSearchEngine.ktindex 97105db75e..884dd4acd1 100644--- a/maps/src/main/java/com/vayunmathur/maps/util/PoiSearchEngine.kt+++ b/maps/src/main/java/com/vayunmathur/maps/util/PoiSearchEngine.kt@@ -150,35 +150,52 @@ internal class CandidateSearch( /** The driver's whole range in a table, intersected with the other scanned tokens. */ private fun collectMaterialized(driver: Int) {- val table = HitTable(tokens.size, tokens[driver].size)- if (!m.scanToken(tokens[driver], cancelled) { o, e -> table.mark(table.add(o), driver, m.entryWordIdx(e)) }) return- for (t in scanned) {- if (t == driver) continue- val ok = m.scanToken(tokens[t], cancelled) { o, e ->- val i = table.indexOf(o)- if (i >= 0) table.mark(i, t, m.entryWordIdx(e))- }- if (!ok) return- }+ val table = intersectScanned(driver) ?: return val survivors = BooleanArray(table.size) { i -> scanned.all { table.mask(i, it) != 0L } } val count = survivors.count { it } if (count == 0) return // Near first: the grid's ordinals are a sequential read, a survivor's coordinates a // random one, so even a small survivor set is only placed whole when the boxes // around the bias point did not already fill the page.+ if (joinRings(table, survivors)) return+ if (count <= GLOBAL_ALL_CAP) {+ for (i in 0 until table.size) if (survivors[i]) addIndexed(table.ordinals[i], table, i)+ } else {+ collectRegional(table, survivors)+ }+ }++ /** The driver's entries in a [HitTable], marked by every scanned token; null when cancelled. */+ private fun intersectScanned(driver: Int): HitTable? {+ val table = HitTable(tokens.size, tokens[driver].size)+ val read = m.scanToken(tokens[driver], cancelled) { o, e ->+ table.mark(table.add(o), driver, m.entryWordIdx(e))+ }+ if (!read) return null+ for (t in scanned.filter { it != driver }) {+ val ok = m.scanToken(tokens[t], cancelled) { o, e ->+ val i = table.indexOf(o)+ if (i >= 0) table.mark(i, t, m.entryWordIdx(e))+ }+ if (!ok) return null+ }+ return table+ }++ /**+ * Survivors inside the growing boxes around the bias point; true when that settled the+ * search (the page is full after at least [MIN_RINGS] boxes, or it was cancelled).+ */+ private fun joinRings(table: HitTable, survivors: BooleanArray): Boolean { RING_HALF_KM.forEachIndexed { ring, half -> forEachOrdinalNear(half) { o -> val i = table.indexOf(o) if (i >= 0 && survivors[i]) addIndexed(o, table, i) true }- if ((ring + 1 >= MIN_RINGS && found.size >= bias.limit) || cancelled()) return- }- if (count <= GLOBAL_ALL_CAP) {- for (i in 0 until table.size) if (survivors[i]) addIndexed(table.ordinals[i], table, i)- } else {- collectRegional(table, survivors)+ if ((ring + 1 >= MIN_RINGS && found.size >= bias.limit) || cancelled()) return true }+ return false } /**@@ -253,7 +270,7 @@ internal class CandidateSearch( if (o !in found) { visited++ val name = m.nameAt(m.nameOff(o))- val tier = if (name == null) PoiSearch.NO_MATCH else PoiSearch.matchTier(name, queryNorm, queryWords)+ val tier = name?.let { PoiSearch.matchTier(it, queryNorm, queryWords) } ?: PoiSearch.NO_MATCH val accept = tier != PoiSearch.NO_MATCH && (acceptLoose || tier <= PoiSearch.TIER_TOKENS) if (accept) add(o, tier) }@@ -410,7 +427,8 @@ internal fun rankCandidates( val record = m.record(c.ordinal) val tier = PoiSearch.matchTier(record.name, queryNorm, queryWords) .takeIf { it != PoiSearch.NO_MATCH } ?: c.tier- val place = if (tier == PoiSearch.TIER_EXACT && record.type == PoiCategories.TYPE_PLACE) PLACE_EXACT_BONUS else 0.0+ val exactPlace = tier == PoiSearch.TIER_EXACT && record.type == PoiCategories.TYPE_PLACE+ val place = if (exactPlace) PLACE_EXACT_BONUS else 0.0 val score = tierPenalty(tier) + distancePenalty(c.distKm) - place - typeBonus(record.type) - boostBonus(boostWeight(bias.boost, record.name)) RankedRow(record, score, c.distKm)