From d2797dbad29aeb12460a99489fb9c02808f47ee8 Mon Sep 17 00:00:00 2001 From: Sameh Khamis Date: Wed, 18 Nov 2026 22:44:12 -0800 Subject: [PATCH] fix(places): keep the places index's build date only when every tile is unchanged; say the 500 km cap is for wide lookups Co-authored-by: Manjunath Paparaju --- changelog.d/1838-nearby-places-namesake.md | 6 ++--- docs/CURRENT-STATE.md | 4 ++-- scripts/build-wikidata-cities.mjs | 22 +++++++++++++++---- scripts/wikidataPlaceTiles.mjs | 4 +++- src/data/local_data/wikidata_places/README.md | 9 ++++---- src/data/wikidataCities.js | 9 ++++---- src/data/wikidataCities.test.mjs | 2 +- 7 files changed, 37 insertions(+), 19 deletions(-) diff --git a/changelog.d/1838-nearby-places-namesake.md b/changelog.d/1838-nearby-places-namesake.md index a9efb54a0c..ffda7e347e 100644 --- a/changelog.d/1838-nearby-places-namesake.md +++ b/changelog.d/1838-nearby-places-namesake.md @@ -1,4 +1,4 @@ - "Villages near" a name picks the place of that name in view over a far - bigger namesake, a view of the whole globe reads only the place tiles - around its centre, and rebuilding the places pack never leaves it half - removed (manjunath22466, samehkhamis, #1838). + bigger namesake, a place lookup reads the detail tiles within 500 km at + most, and rebuilding the places pack never leaves it half removed + (manjunath22466, samehkhamis, #1838). diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index f51d96efcb..ff51fca9fe 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -8254,8 +8254,8 @@ the city is listed. A place named in "villages near …" is the nearest of that name within 150 km of the view, small places included, so a village in view comes before a bigger namesake far away; with none near, the largest city of the name. Small places are looked for within 500 km at most, so a -view zoomed out to the whole globe reads only the detail tiles around its -centre. At most 64 detail tiles are kept in memory, the least +lookup asked for a wider radius reads only the detail tiles around its +centre (the search box asks for 150 km at most). At most 64 detail tiles are kept in memory, the least recently used dropped first. ### Place-search providers diff --git a/scripts/build-wikidata-cities.mjs b/scripts/build-wikidata-cities.mjs index 00dc36f0c0..1c66eea87b 100644 --- a/scripts/build-wikidata-cities.mjs +++ b/scripts/build-wikidata-cities.mjs @@ -505,11 +505,25 @@ if (tilesMode) { // The new files are all written before tiles from an earlier build that // this one has no rows for go, so a build that stops part way never leaves // an index naming a removed tile. - const indexFile = path.join(outDir, 'index.json'); - files.set( - 'index.json', - keepBuiltIfUnchanged(files.get('index.json'), await readIfThere(indexFile)), + // The index holds only row counts, so its build date is kept only when + // every tile is unchanged too and no earlier tile is left over. + const earlierTiles = (await readdir(outDir)).filter((name) => + /^-?\d+_-?\d+\.json$/.test(name), ); + let tilesUnchanged = earlierTiles.every((name) => files.has(name)); + for (const [name, text] of files) { + if (!tilesUnchanged) break; + if (name === 'index.json') continue; + tilesUnchanged = (await readIfThere(path.join(outDir, name))) === text; + } + if (tilesUnchanged) + files.set( + 'index.json', + keepBuiltIfUnchanged( + files.get('index.json'), + await readIfThere(path.join(outDir, 'index.json')), + ), + ); for (const [name, text] of files) await writeFile(path.join(outDir, name), text); for (const name of await readdir(outDir)) diff --git a/scripts/wikidataPlaceTiles.mjs b/scripts/wikidataPlaceTiles.mjs index bae1774bb7..98c2b10651 100644 --- a/scripts/wikidataPlaceTiles.mjs +++ b/scripts/wikidataPlaceTiles.mjs @@ -92,7 +92,9 @@ export function placeTileFiles(rows, meta) { /** * A pack file's new text, keeping the earlier build's `meta.built` when * nothing else in the file changed, so rebuilding unchanged data leaves the - * file as it was. + * file as it was. A file that does not hold all of its pack's data (the + * places index) is passed through this only when the rest of the pack is + * unchanged too. * @param {string} text The new file, JSON with a `meta.built` stamp. * @param {string | undefined} earlier The file as it is now, if there is one. */ diff --git a/src/data/local_data/wikidata_places/README.md b/src/data/local_data/wikidata_places/README.md index 144f907209..efad9ea1b3 100644 --- a/src/data/local_data/wikidata_places/README.md +++ b/src/data/local_data/wikidata_places/README.md @@ -20,8 +20,9 @@ entry in the index. **Loading:** nothing here loads with the app. A lookup reads `files.js` and the index once, then only the tiles its search box touches (one for a short radius well inside a tile, more near tile edges and for wider searches), -once each. The search box reaches 500 km at most (`PLACE_TILE_KM`), so a -view of the whole globe reads only the tiles around its centre: at most one +once each. A lookup reaches 500 km at most (`PLACE_TILE_KM`; the search box +asks for 150 km at most), so one asked for a wider radius reads only the +tiles around its centre: at most one tile past the centre's own on each side, a few more columns toward the poles, and never all 732. Cities still count to the full radius. At most 64 tiles are kept in memory, the least recently used @@ -48,8 +49,8 @@ It writes only to this folder (tiles, `index.json`, `files.js`, through `scripts/wikidataPlaceTiles.mjs`) and leaves `../wikidata_cities` alone. It runs one query at a time with pauses and takes a long while. The new files are all written before tiles that the new build has no rows for are -removed, and `meta.built` keeps its earlier date when nothing else in -`index.json` changed. +removed, and `meta.built` keeps its earlier date when no tile and +nothing else in `index.json` changed. **Reproducibility:** the step from rows to files is checked offline: `src/tooling/wikidataPlaceTiles.test.mjs` sorts the shipped rows as the diff --git a/src/data/wikidataCities.js b/src/data/wikidataCities.js index 2b62ac62b0..c36dd89c3a 100644 --- a/src/data/wikidataCities.js +++ b/src/data/wikidataCities.js @@ -219,10 +219,11 @@ const TILE_DEG = 5; const MAX_TILES = 64; /** * How far out small places are looked for, in km. 500 km is under the 556 km - * that 5 degrees of latitude span, so the search box reaches at most one tile - * past the point's own on each side: 3 rows, and 3 columns at the equator - * (more toward the poles, where tiles narrow). A wider view, out to the whole - * globe, still reads only those tiles; cities count to its full radius. + * that 5 degrees of latitude span, so a lookup's tile box reaches at most one + * tile past the point's own on each side: 3 rows, and 3 columns at the + * equator (more toward the poles, where tiles narrow). A lookup asked for a + * wider radius, up to the whole globe, still reads only those tiles; cities + * count to its full radius. */ export const PLACE_TILE_KM = 500; const NAMED = /\p{L}/u; diff --git a/src/data/wikidataCities.test.mjs b/src/data/wikidataCities.test.mjs index 10cec8c834..6e6f742007 100644 --- a/src/data/wikidataCities.test.mjs +++ b/src/data/wikidataCities.test.mjs @@ -282,7 +282,7 @@ test('a tile that cannot be read leaves the other places and the cities', async assert.ok(near.some((place) => place.longitude > -95)); }); -test('a view of the whole globe reads only the tiles near its centre', async () => { +test('a lookup asked for the whole globe reads only the tiles near its centre', async () => { const { placesNear, PLACE_TILE_KM } = await import('./wikidataCities.js?globe'); const fetched = [];