mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 01:44:13 +00:00
fix(maps): stop the index costing a second search, a duplicate and a lost cache
Six things the merged search and the offline cache got wrong. An index failure took the OpenStreetMap answer down with it. Only the OSM half of the Promise.all had a catch, so a rejection on the index side discarded ten results that had already arrived and sent the same query to Nominatim a second time, behind its 1.1 s process-wide throttle. The index refusing a query is ordinary traffic rather than an outage — a common single word without coordinates is turned down upstream as too expensive — so this fired in normal use. Each side now catches its own failure, and the keyless fallback reuses the answer OpenStreetMap already gave instead of asking again for a list that cannot have changed. Dedup never fired for a non-Latin name. namesOverlap splits on [^a-z0-9], so 長谷寺, Кремль and مسجد الشيخ زايد all tokenise to nothing and the function returned false — the same place came back once from each source, one row under the other, and picking either gave it a different id. That is the corpus this whole feature was measured on. Two names with no Latin words are now compared strictly, folded through NFKC so the width variants the sources disagree on still match; sharing a character is deliberately not enough, because deduping 東京タワー against 東京タワー駅前郵便局 would swallow a real result. expand=1 answered nothing for an index place while expand=0 answered in full. The expanded lookup degrades OSM ids to the plain one and had no branch for the new family, so the richer request was the poorer one, over REST and MCP alike. The bias circle could exceed Google's 50 km ceiling. A trip spread over more than that produced a radius Google rejects outright, turning a search into an error toast. Clamped on both sides — the client because it draws the box, the server because the radius arrives over the wire. The offline cache was rebuilt on every sync and never deleted. syncTrip wrote its meta row with put(), which replaced the whole record and dropped the fingerprint saying the cached area was current, so every login, reconnect and manual sync re-downloaded megabytes that had not changed. And clearTripData never touched areaPlaces, so a trip switched off for offline use kept answering offline searches and its rows stayed for the life of the install. clearCachedPlaces, which claimed to do that job, was called from nowhere; its only test asserted with .every() on an empty table and passed on nothing. Picking an offline suggestion ended in an error toast. The details lookup had no cache path, so it failed and the callers fell back to searching for "name, address" — which the cache matches on the folded name alone, so it found nothing for a place that was sitting right there. A miss stays a miss.
This commit is contained in:
@@ -10,8 +10,8 @@ import { offlineDb, clearAll } from '../db/offlineDb'
|
||||
import {
|
||||
AREA_PLACE_LIMIT,
|
||||
cachedToPlaceRecord,
|
||||
clearCachedPlaces,
|
||||
foldName,
|
||||
getCachedPlace,
|
||||
prefetchPlacesForTrip,
|
||||
searchCachedPlaces,
|
||||
} from './placePrefetcher'
|
||||
@@ -167,14 +167,25 @@ describe('searchCachedPlaces', () => {
|
||||
rating: null,
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('clearCachedPlaces', () => {
|
||||
it('FE-PLACEPRE-016: drops one trip and leaves the others', async () => {
|
||||
await prefetchPlacesForTrip(7, TRIP_PLACES)
|
||||
await offlineDb.areaPlaces.toCollection().modify({ tripId: 8 })
|
||||
await prefetchPlacesForTrip(7, TRIP_PLACES, true)
|
||||
await clearCachedPlaces(7)
|
||||
expect((await offlineDb.areaPlaces.toArray()).every((r) => r.tripId === 8)).toBe(true)
|
||||
it('FE-PLACEPRE-016: finds a place by the id its own suggestion handed out', async () => {
|
||||
// This closes the loop offline. The suggestion list gives out `gers:` ids;
|
||||
// picking one calls the details lookup with exactly that string, and before
|
||||
// this the lookup had no cache path, failed, and the callers fell back to
|
||||
// searching for "name, address" — which matches on the folded NAME alone,
|
||||
// so it found nothing and the user got an error for a place that was
|
||||
// sitting in the cache.
|
||||
const [hit] = await searchCachedPlaces('osteria')
|
||||
const found = await getCachedPlace(`gers:${hit.gers}`)
|
||||
expect(found?.name).toBe("L'Osteria")
|
||||
})
|
||||
|
||||
it('FE-PLACEPRE-017: an id the cache does not hold is a miss, not somebody else record', async () => {
|
||||
// A miss has to stay a miss: answering the wrong place would be worse than
|
||||
// the failure it replaces.
|
||||
expect(await getCachedPlace('gers:not-cached')).toBeNull()
|
||||
// Ids from sources the cache never holds are not looked up at all.
|
||||
expect(await getCachedPlace('node:5255005321')).toBeNull()
|
||||
expect(await getCachedPlace('ChIJLU7jZClu5kcR4PcOOO6p3I0')).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
@@ -133,7 +133,18 @@ export async function prefetchPlacesForTrip(
|
||||
if (rows.length) await offlineDb.areaPlaces.bulkPut(rows)
|
||||
})
|
||||
|
||||
if (meta) await offlineDb.syncMeta.put({ ...meta, areaPlacesKey: key })
|
||||
// Written whether or not a sync row exists yet. Skipping it when there is
|
||||
// none left the fingerprint unwritten for a trip cached before its first
|
||||
// sync, so the next run re-downloaded the same area for nothing.
|
||||
await offlineDb.syncMeta.put({
|
||||
tripId,
|
||||
lastSyncedAt: meta?.lastSyncedAt ?? 0,
|
||||
status: meta?.status ?? 'idle',
|
||||
tilesBbox: meta?.tilesBbox ?? null,
|
||||
filesCachedCount: meta?.filesCachedCount ?? 0,
|
||||
...meta,
|
||||
areaPlacesKey: key,
|
||||
})
|
||||
if (rows.length) {
|
||||
console.info(
|
||||
`[placePrefetch] trip ${tripId}: cached ${rows.length} places${area.truncated ? ' (area trimmed)' : ''}`,
|
||||
@@ -177,6 +188,19 @@ export async function searchCachedPlaces(query: string, limit = 10): Promise<Cac
|
||||
}
|
||||
|
||||
/** A cached row in the shape every search caller already reads. */
|
||||
/**
|
||||
* One cached place by the id the suggestion list handed out.
|
||||
*
|
||||
* Takes the `gers:`-prefixed form the server puts on `osm_id`, because that is
|
||||
* what a suggestion carries and what the details lookup is called with. Any
|
||||
* other id shape belongs to a source the cache does not hold, and is a miss
|
||||
* rather than an error.
|
||||
*/
|
||||
export async function getCachedPlace(placeId: string): Promise<CachedAreaPlace | null> {
|
||||
if (!placeId.startsWith('gers:')) return null
|
||||
return (await offlineDb.areaPlaces.get(placeId.slice('gers:'.length))) ?? null
|
||||
}
|
||||
|
||||
export function cachedToPlaceRecord(p: CachedAreaPlace): Record<string, unknown> {
|
||||
return {
|
||||
google_place_id: null,
|
||||
@@ -193,7 +217,3 @@ export function cachedToPlaceRecord(p: CachedAreaPlace): Record<string, unknown>
|
||||
}
|
||||
}
|
||||
|
||||
/** Drop a trip's cached area. Called when the trip leaves the offline set. */
|
||||
export async function clearCachedPlaces(tripId: number): Promise<void> {
|
||||
await offlineDb.areaPlaces.where('tripId').equals(tripId).delete()
|
||||
}
|
||||
|
||||
@@ -124,7 +124,16 @@ async function syncTrip(tripId: number): Promise<void> {
|
||||
await upsertTripFiles(bundle.files)
|
||||
await upsertAccommodations(bundle.accommodations || [])
|
||||
await upsertTripMembers(tripId, bundle.members || [])
|
||||
// Merged onto the existing row, not written over it: `put` replaces the whole
|
||||
// record, and the row also carries `areaPlacesKey` — the fingerprint that says
|
||||
// the cached places for this trip's area are still current. Losing it on every
|
||||
// sync meant the prefetch re-downloaded the whole area (megabytes, and a bbox
|
||||
// query upstream) on every login, every reconnect and every manual sync, for
|
||||
// data that had not changed. The two fields below keep being reset on purpose,
|
||||
// exactly as before.
|
||||
const previous = await offlineDb.syncMeta.get(tripId)
|
||||
await upsertSyncMeta({
|
||||
...previous,
|
||||
tripId,
|
||||
lastSyncedAt: Date.now(),
|
||||
status: 'idle',
|
||||
|
||||
Reference in New Issue
Block a user