@mailwoman/resolver-wof-sqlite 11.0.0 → 11.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (557) hide show
  1. package/README.md +6 -6
  2. package/lib/address/duplicate.ts +133 -0
  3. package/lib/address/keys/attribution.ts +119 -0
  4. package/lib/address/keys/build.ts +768 -0
  5. package/lib/address/keys/layout.ts +166 -0
  6. package/lib/address/keys/local-runner.ts +18 -0
  7. package/lib/address/keys/lookup.ts +205 -0
  8. package/lib/address/keys/open.ts +33 -0
  9. package/lib/address/keys/point-lookup.ts +130 -0
  10. package/lib/address/keys/postcode.ts +27 -0
  11. package/lib/address/keys/schema.ts +264 -0
  12. package/lib/address/number-forms.ts +61 -0
  13. package/lib/address/point/interpolation.ts +28 -23
  14. package/lib/address/point/schema.ts +2 -5
  15. package/lib/address/point/spine-schema.ts +4 -4
  16. package/lib/address/point.ts +12 -79
  17. package/lib/address/record.ts +155 -0
  18. package/lib/address/stack.ts +20 -0
  19. package/lib/address/subdivision-bounds.ts +81 -0
  20. package/lib/address.ts +2 -5
  21. package/lib/ancestry/backfill.ts +18 -47
  22. package/lib/ancestry.ts +10 -10
  23. package/lib/build/candidate.ts +44 -48
  24. package/lib/build/population-corrections.ts +4 -4
  25. package/lib/build/slim.ts +3 -6
  26. package/lib/candidate/alias-bags.ts +58 -18
  27. package/lib/candidate/ancestors/schema.ts +63 -43
  28. package/lib/candidate/ancestors/sidecar.ts +3 -3
  29. package/lib/candidate/bare-admin-names.ts +3 -3
  30. package/lib/candidate/country-display-names.ts +2 -5
  31. package/lib/candidate/extract-fold.ts +2 -5
  32. package/lib/candidate/fts.ts +5 -5
  33. package/lib/candidate/importance.ts +30 -24
  34. package/lib/candidate/key-fold.ts +132 -0
  35. package/lib/candidate/lookup.ts +25 -18
  36. package/lib/candidate/name-roles.ts +4 -4
  37. package/lib/candidate/nickname-namesakes.ts +140 -0
  38. package/lib/candidate/own-name.ts +12 -10
  39. package/lib/candidate/place-attrs.ts +4 -4
  40. package/lib/candidate/schema.ts +2 -5
  41. package/lib/candidate/scoring.ts +3 -6
  42. package/lib/capital-schema.ts +11 -10
  43. package/lib/capitals.ts +15 -12
  44. package/lib/convention/schema.ts +7 -7
  45. package/lib/convention.ts +3 -3
  46. package/lib/coverage-manifest-schema.ts +4 -4
  47. package/lib/env.ts +7 -6
  48. package/lib/exact-match.ts +6 -7
  49. package/lib/extracts.ts +11 -9
  50. package/lib/fst/autocomplete.ts +19 -15
  51. package/lib/fst/binary-matcher.ts +36 -50
  52. package/lib/fst/builder.ts +32 -11
  53. package/lib/fst/deserialize-web.ts +9 -8
  54. package/lib/fst/format.ts +153 -14
  55. package/lib/fst/freshness.ts +3 -4
  56. package/lib/fst/matcher.ts +6 -6
  57. package/lib/fst/serialize.ts +94 -69
  58. package/lib/fst/types.ts +4 -4
  59. package/lib/fst.ts +2 -5
  60. package/lib/fts/query.ts +3 -4
  61. package/lib/fts.ts +2 -5
  62. package/lib/geonames/aliases.ts +189 -46
  63. package/lib/geonames/postal.ts +17 -15
  64. package/lib/geonames.ts +2 -5
  65. package/lib/importance-source.ts +210 -0
  66. package/lib/index.ts +4 -5
  67. package/lib/interpolation.ts +7 -6
  68. package/lib/landmark/rows.ts +5 -5
  69. package/lib/landmark.ts +6 -6
  70. package/lib/lookup.ts +8 -8
  71. package/lib/name-score.ts +7 -6
  72. package/lib/nsul/lookup.ts +19 -15
  73. package/lib/nsul/schema.ts +4 -5
  74. package/lib/nsul.ts +2 -5
  75. package/lib/paths.ts +7 -7
  76. package/lib/place-importance-schema.ts +5 -8
  77. package/lib/poi/lookup.ts +11 -10
  78. package/lib/poi/schema.ts +16 -10
  79. package/lib/poi.ts +2 -5
  80. package/lib/polygon-schema.ts +10 -9
  81. package/lib/postal/city/alias/lookup.ts +14 -13
  82. package/lib/postal/city/alias/schema.ts +12 -12
  83. package/lib/postal/city/candidate-schema.ts +15 -12
  84. package/lib/postal.ts +2 -5
  85. package/lib/postcode-point-lookup.ts +2 -5
  86. package/lib/primary-preference.ts +42 -7
  87. package/lib/proximity-rerank.ts +23 -19
  88. package/lib/ranking-weights.ts +8 -7
  89. package/lib/region-keys.ts +15 -14
  90. package/lib/reverse.ts +16 -17
  91. package/lib/schema.ts +20 -6
  92. package/lib/search-fetch.ts +15 -11
  93. package/lib/sqlite/convention-source.ts +2 -5
  94. package/lib/sqlite/utils.ts +4 -4
  95. package/lib/street/centroid/schema.ts +6 -6
  96. package/lib/street/centroid.ts +3 -3
  97. package/lib/street/locale.ts +64 -0
  98. package/lib/street/morphology/fst/builder.ts +21 -19
  99. package/lib/street/morphology/fst/loader.ts +15 -13
  100. package/lib/street/name-lookup.ts +15 -14
  101. package/lib/street/normalize.ts +62 -17
  102. package/lib/street/segment-schema.ts +16 -14
  103. package/lib/street/table.ts +276 -0
  104. package/lib/street.ts +3 -5
  105. package/lib/types.ts +3 -6
  106. package/lib/unified-schema.ts +31 -9
  107. package/lib/uprn/existence.ts +10 -10
  108. package/lib/uprn/lookup.ts +7 -8
  109. package/lib/uprn/schema.ts +5 -5
  110. package/lib/uprn.ts +2 -5
  111. package/lib/weights-overlay-linker.ts +5 -8
  112. package/out/address/duplicate.d.ts +74 -0
  113. package/out/address/duplicate.d.ts.map +1 -0
  114. package/out/address/duplicate.js +79 -0
  115. package/out/address/duplicate.js.map +1 -0
  116. package/out/address/keys/attribution.d.ts +56 -0
  117. package/out/address/keys/attribution.d.ts.map +1 -0
  118. package/out/address/keys/attribution.js +64 -0
  119. package/out/address/keys/attribution.js.map +1 -0
  120. package/out/address/keys/build.d.ts +154 -0
  121. package/out/address/keys/build.d.ts.map +1 -0
  122. package/out/address/keys/build.js +423 -0
  123. package/out/address/keys/build.js.map +1 -0
  124. package/out/address/keys/layout.d.ts +92 -0
  125. package/out/address/keys/layout.d.ts.map +1 -0
  126. package/out/address/keys/layout.js +87 -0
  127. package/out/address/keys/layout.js.map +1 -0
  128. package/out/address/keys/local-runner.d.ts +9 -0
  129. package/out/address/keys/local-runner.d.ts.map +1 -0
  130. package/out/address/keys/local-runner.js +13 -0
  131. package/out/address/keys/local-runner.js.map +1 -0
  132. package/out/address/keys/lookup.d.ts +80 -0
  133. package/out/address/keys/lookup.d.ts.map +1 -0
  134. package/out/address/keys/lookup.js +104 -0
  135. package/out/address/keys/lookup.js.map +1 -0
  136. package/out/address/keys/open.d.ts +10 -0
  137. package/out/address/keys/open.d.ts.map +1 -0
  138. package/out/address/keys/open.js +28 -0
  139. package/out/address/keys/open.js.map +1 -0
  140. package/out/address/keys/point-lookup.d.ts +25 -0
  141. package/out/address/keys/point-lookup.d.ts.map +1 -0
  142. package/out/address/keys/point-lookup.js +100 -0
  143. package/out/address/keys/point-lookup.js.map +1 -0
  144. package/out/address/keys/postcode.d.ts +9 -0
  145. package/out/address/keys/postcode.d.ts.map +1 -0
  146. package/out/address/keys/postcode.js +25 -0
  147. package/out/address/keys/postcode.js.map +1 -0
  148. package/out/address/keys/schema.d.ts +156 -0
  149. package/out/address/keys/schema.d.ts.map +1 -0
  150. package/out/address/keys/schema.js +119 -0
  151. package/out/address/keys/schema.js.map +1 -0
  152. package/out/address/number-forms.d.ts +19 -0
  153. package/out/address/number-forms.d.ts.map +1 -0
  154. package/out/address/number-forms.js +50 -0
  155. package/out/address/number-forms.js.map +1 -0
  156. package/out/address/point/interpolation.d.ts +24 -22
  157. package/out/address/point/interpolation.d.ts.map +1 -1
  158. package/out/address/point/interpolation.js +3 -31
  159. package/out/address/point/interpolation.js.map +1 -1
  160. package/out/address/point/schema.d.ts +0 -5
  161. package/out/address/point/schema.d.ts.map +1 -1
  162. package/out/address/point/schema.js +2 -5
  163. package/out/address/point/schema.js.map +1 -1
  164. package/out/address/point/spine-schema.d.ts +1 -4
  165. package/out/address/point/spine-schema.d.ts.map +1 -1
  166. package/out/address/point/spine-schema.js +3 -4
  167. package/out/address/point/spine-schema.js.map +1 -1
  168. package/out/address/point.d.ts +0 -3
  169. package/out/address/point.d.ts.map +1 -1
  170. package/out/address/point.js +12 -71
  171. package/out/address/point.js.map +1 -1
  172. package/out/address/record.d.ts +125 -0
  173. package/out/address/record.d.ts.map +1 -0
  174. package/out/address/record.js +49 -0
  175. package/out/address/record.js.map +1 -0
  176. package/out/address/stack.d.ts +14 -0
  177. package/out/address/stack.d.ts.map +1 -0
  178. package/out/address/stack.js +19 -0
  179. package/out/address/stack.js.map +1 -0
  180. package/out/address/subdivision-bounds.d.ts +32 -0
  181. package/out/address/subdivision-bounds.d.ts.map +1 -0
  182. package/out/address/subdivision-bounds.js +48 -0
  183. package/out/address/subdivision-bounds.js.map +1 -0
  184. package/out/address.d.ts +0 -5
  185. package/out/address.d.ts.map +1 -1
  186. package/out/address.js +2 -5
  187. package/out/address.js.map +1 -1
  188. package/out/ancestry/backfill.d.ts +11 -13
  189. package/out/ancestry/backfill.d.ts.map +1 -1
  190. package/out/ancestry/backfill.js +6 -44
  191. package/out/ancestry/backfill.js.map +1 -1
  192. package/out/ancestry.d.ts +0 -14
  193. package/out/ancestry.d.ts.map +1 -1
  194. package/out/ancestry.js +9 -10
  195. package/out/ancestry.js.map +1 -1
  196. package/out/build/candidate.d.ts +20 -22
  197. package/out/build/candidate.d.ts.map +1 -1
  198. package/out/build/candidate.js +16 -28
  199. package/out/build/candidate.js.map +1 -1
  200. package/out/build/population-corrections.d.ts +1 -4
  201. package/out/build/population-corrections.d.ts.map +1 -1
  202. package/out/build/population-corrections.js +2 -19
  203. package/out/build/population-corrections.js.map +1 -1
  204. package/out/build/slim.d.ts +0 -5
  205. package/out/build/slim.d.ts.map +1 -1
  206. package/out/build/slim.js +3 -6
  207. package/out/build/slim.js.map +1 -1
  208. package/out/candidate/alias-bags.d.ts +9 -4
  209. package/out/candidate/alias-bags.d.ts.map +1 -1
  210. package/out/candidate/alias-bags.js +46 -19
  211. package/out/candidate/alias-bags.js.map +1 -1
  212. package/out/candidate/ancestors/schema.d.ts +60 -43
  213. package/out/candidate/ancestors/schema.d.ts.map +1 -1
  214. package/out/candidate/ancestors/schema.js +62 -43
  215. package/out/candidate/ancestors/schema.js.map +1 -1
  216. package/out/candidate/ancestors/sidecar.d.ts +0 -3
  217. package/out/candidate/ancestors/sidecar.d.ts.map +1 -1
  218. package/out/candidate/ancestors/sidecar.js +2 -6
  219. package/out/candidate/ancestors/sidecar.js.map +1 -1
  220. package/out/candidate/bare-admin-names.d.ts +0 -6
  221. package/out/candidate/bare-admin-names.d.ts.map +1 -1
  222. package/out/candidate/bare-admin-names.js +2 -3
  223. package/out/candidate/bare-admin-names.js.map +1 -1
  224. package/out/candidate/country-display-names.d.ts +0 -5
  225. package/out/candidate/country-display-names.d.ts.map +1 -1
  226. package/out/candidate/country-display-names.js +2 -5
  227. package/out/candidate/country-display-names.js.map +1 -1
  228. package/out/candidate/extract-fold.d.ts +0 -5
  229. package/out/candidate/extract-fold.d.ts.map +1 -1
  230. package/out/candidate/extract-fold.js +2 -5
  231. package/out/candidate/extract-fold.js.map +1 -1
  232. package/out/candidate/fts.d.ts +2 -5
  233. package/out/candidate/fts.d.ts.map +1 -1
  234. package/out/candidate/fts.js +2 -7
  235. package/out/candidate/fts.js.map +1 -1
  236. package/out/candidate/importance.d.ts +3 -33
  237. package/out/candidate/importance.d.ts.map +1 -1
  238. package/out/candidate/importance.js +29 -24
  239. package/out/candidate/importance.js.map +1 -1
  240. package/out/candidate/key-fold.d.ts +55 -0
  241. package/out/candidate/key-fold.d.ts.map +1 -0
  242. package/out/candidate/key-fold.js +80 -0
  243. package/out/candidate/key-fold.js.map +1 -0
  244. package/out/candidate/lookup.d.ts +0 -5
  245. package/out/candidate/lookup.d.ts.map +1 -1
  246. package/out/candidate/lookup.js +24 -17
  247. package/out/candidate/lookup.js.map +1 -1
  248. package/out/candidate/name-roles.d.ts +0 -6
  249. package/out/candidate/name-roles.d.ts.map +1 -1
  250. package/out/candidate/name-roles.js +3 -4
  251. package/out/candidate/name-roles.js.map +1 -1
  252. package/out/candidate/nickname-namesakes.d.ts +32 -0
  253. package/out/candidate/nickname-namesakes.d.ts.map +1 -0
  254. package/out/candidate/nickname-namesakes.js +98 -0
  255. package/out/candidate/nickname-namesakes.js.map +1 -0
  256. package/out/candidate/own-name.d.ts +0 -18
  257. package/out/candidate/own-name.d.ts.map +1 -1
  258. package/out/candidate/own-name.js +11 -10
  259. package/out/candidate/own-name.js.map +1 -1
  260. package/out/candidate/place-attrs.d.ts +1 -4
  261. package/out/candidate/place-attrs.d.ts.map +1 -1
  262. package/out/candidate/place-attrs.js +2 -12
  263. package/out/candidate/place-attrs.js.map +1 -1
  264. package/out/candidate/schema.d.ts +0 -5
  265. package/out/candidate/schema.d.ts.map +1 -1
  266. package/out/candidate/schema.js +2 -5
  267. package/out/candidate/schema.js.map +1 -1
  268. package/out/candidate/scoring.d.ts +0 -5
  269. package/out/candidate/scoring.d.ts.map +1 -1
  270. package/out/candidate/scoring.js +3 -6
  271. package/out/candidate/scoring.js.map +1 -1
  272. package/out/capital-schema.d.ts +0 -14
  273. package/out/capital-schema.d.ts.map +1 -1
  274. package/out/capital-schema.js +10 -10
  275. package/out/capital-schema.js.map +1 -1
  276. package/out/capitals.d.ts +0 -16
  277. package/out/capitals.d.ts.map +1 -1
  278. package/out/capitals.js +14 -12
  279. package/out/capitals.js.map +1 -1
  280. package/out/convention/schema.d.ts +4 -7
  281. package/out/convention/schema.d.ts.map +1 -1
  282. package/out/convention/schema.js +2 -10
  283. package/out/convention/schema.js.map +1 -1
  284. package/out/convention.d.ts +0 -3
  285. package/out/convention.d.ts.map +1 -1
  286. package/out/convention.js +2 -8
  287. package/out/convention.js.map +1 -1
  288. package/out/coverage-manifest-schema.d.ts +1 -4
  289. package/out/coverage-manifest-schema.d.ts.map +1 -1
  290. package/out/coverage-manifest-schema.js +2 -3
  291. package/out/coverage-manifest-schema.js.map +1 -1
  292. package/out/env.d.ts +0 -8
  293. package/out/env.d.ts.map +1 -1
  294. package/out/env.js +6 -6
  295. package/out/env.js.map +1 -1
  296. package/out/exact-match.d.ts +2 -6
  297. package/out/exact-match.d.ts.map +1 -1
  298. package/out/exact-match.js +3 -9
  299. package/out/exact-match.js.map +1 -1
  300. package/out/extracts.d.ts +8 -9
  301. package/out/extracts.d.ts.map +1 -1
  302. package/out/extracts.js +9 -8
  303. package/out/extracts.js.map +1 -1
  304. package/out/fst/autocomplete.d.ts +1 -19
  305. package/out/fst/autocomplete.d.ts.map +1 -1
  306. package/out/fst/autocomplete.js +2 -18
  307. package/out/fst/autocomplete.js.map +1 -1
  308. package/out/fst/binary-matcher.d.ts +6 -8
  309. package/out/fst/binary-matcher.d.ts.map +1 -1
  310. package/out/fst/binary-matcher.js +12 -43
  311. package/out/fst/binary-matcher.js.map +1 -1
  312. package/out/fst/builder.d.ts +0 -12
  313. package/out/fst/builder.d.ts.map +1 -1
  314. package/out/fst/builder.js +23 -11
  315. package/out/fst/builder.js.map +1 -1
  316. package/out/fst/deserialize-web.d.ts +1 -12
  317. package/out/fst/deserialize-web.d.ts.map +1 -1
  318. package/out/fst/deserialize-web.js +8 -8
  319. package/out/fst/deserialize-web.js.map +1 -1
  320. package/out/fst/format.d.ts +66 -16
  321. package/out/fst/format.d.ts.map +1 -1
  322. package/out/fst/format.js +100 -16
  323. package/out/fst/format.js.map +1 -1
  324. package/out/fst/freshness.d.ts +0 -7
  325. package/out/fst/freshness.d.ts.map +1 -1
  326. package/out/fst/freshness.js +2 -4
  327. package/out/fst/freshness.js.map +1 -1
  328. package/out/fst/matcher.d.ts +3 -6
  329. package/out/fst/matcher.d.ts.map +1 -1
  330. package/out/fst/matcher.js +2 -9
  331. package/out/fst/matcher.js.map +1 -1
  332. package/out/fst/serialize.d.ts +0 -23
  333. package/out/fst/serialize.d.ts.map +1 -1
  334. package/out/fst/serialize.js +68 -60
  335. package/out/fst/serialize.js.map +1 -1
  336. package/out/fst/types.d.ts +1 -4
  337. package/out/fst/types.d.ts.map +1 -1
  338. package/out/fst/types.js +2 -6
  339. package/out/fst/types.js.map +1 -1
  340. package/out/fst.d.ts +0 -5
  341. package/out/fst.d.ts.map +1 -1
  342. package/out/fst.js +2 -5
  343. package/out/fst.js.map +1 -1
  344. package/out/fts/query.d.ts +0 -4
  345. package/out/fts/query.d.ts.map +1 -1
  346. package/out/fts/query.js +2 -7
  347. package/out/fts/query.js.map +1 -1
  348. package/out/fts.d.ts +0 -5
  349. package/out/fts.d.ts.map +1 -1
  350. package/out/fts.js +2 -5
  351. package/out/fts.js.map +1 -1
  352. package/out/geonames/aliases.d.ts +43 -5
  353. package/out/geonames/aliases.d.ts.map +1 -1
  354. package/out/geonames/aliases.js +108 -44
  355. package/out/geonames/aliases.js.map +1 -1
  356. package/out/geonames/postal.d.ts +1 -25
  357. package/out/geonames/postal.d.ts.map +1 -1
  358. package/out/geonames/postal.js +15 -14
  359. package/out/geonames/postal.js.map +1 -1
  360. package/out/geonames.d.ts +0 -5
  361. package/out/geonames.d.ts.map +1 -1
  362. package/out/geonames.js +2 -5
  363. package/out/geonames.js.map +1 -1
  364. package/out/importance-source.d.ts +90 -0
  365. package/out/importance-source.d.ts.map +1 -0
  366. package/out/importance-source.js +133 -0
  367. package/out/importance-source.js.map +1 -0
  368. package/out/index.d.ts +2 -5
  369. package/out/index.d.ts.map +1 -1
  370. package/out/index.js +4 -5
  371. package/out/index.js.map +1 -1
  372. package/out/interpolation.d.ts +0 -7
  373. package/out/interpolation.d.ts.map +1 -1
  374. package/out/interpolation.js +6 -6
  375. package/out/interpolation.js.map +1 -1
  376. package/out/landmark/rows.d.ts +2 -5
  377. package/out/landmark/rows.d.ts.map +1 -1
  378. package/out/landmark/rows.js +2 -8
  379. package/out/landmark/rows.js.map +1 -1
  380. package/out/landmark.d.ts +3 -6
  381. package/out/landmark.d.ts.map +1 -1
  382. package/out/landmark.js +2 -8
  383. package/out/landmark.js.map +1 -1
  384. package/out/lookup.d.ts +0 -7
  385. package/out/lookup.d.ts.map +1 -1
  386. package/out/lookup.js +7 -8
  387. package/out/lookup.js.map +1 -1
  388. package/out/name-score.d.ts +4 -6
  389. package/out/name-score.d.ts.map +1 -1
  390. package/out/name-score.js +6 -6
  391. package/out/name-score.js.map +1 -1
  392. package/out/nsul/lookup.d.ts +16 -15
  393. package/out/nsul/lookup.d.ts.map +1 -1
  394. package/out/nsul/lookup.js +2 -25
  395. package/out/nsul/lookup.js.map +1 -1
  396. package/out/nsul/schema.d.ts +1 -5
  397. package/out/nsul/schema.d.ts.map +1 -1
  398. package/out/nsul/schema.js +3 -8
  399. package/out/nsul/schema.js.map +1 -1
  400. package/out/nsul.d.ts +0 -5
  401. package/out/nsul.d.ts.map +1 -1
  402. package/out/nsul.js +2 -5
  403. package/out/nsul.js.map +1 -1
  404. package/out/paths.d.ts +1 -13
  405. package/out/paths.d.ts.map +1 -1
  406. package/out/paths.js +6 -7
  407. package/out/paths.js.map +1 -1
  408. package/out/place-importance-schema.d.ts +3 -8
  409. package/out/place-importance-schema.d.ts.map +1 -1
  410. package/out/place-importance-schema.js +3 -6
  411. package/out/place-importance-schema.js.map +1 -1
  412. package/out/poi/lookup.d.ts +0 -13
  413. package/out/poi/lookup.d.ts.map +1 -1
  414. package/out/poi/lookup.js +10 -10
  415. package/out/poi/lookup.js.map +1 -1
  416. package/out/poi/schema.d.ts +13 -10
  417. package/out/poi/schema.d.ts.map +1 -1
  418. package/out/poi/schema.js +2 -13
  419. package/out/poi/schema.js.map +1 -1
  420. package/out/poi.d.ts +0 -5
  421. package/out/poi.d.ts.map +1 -1
  422. package/out/poi.js +2 -5
  423. package/out/poi.js.map +1 -1
  424. package/out/polygon-schema.d.ts +7 -9
  425. package/out/polygon-schema.d.ts.map +1 -1
  426. package/out/polygon-schema.js +2 -12
  427. package/out/polygon-schema.js.map +1 -1
  428. package/out/postal/city/alias/lookup.d.ts +11 -13
  429. package/out/postal/city/alias/lookup.d.ts.map +1 -1
  430. package/out/postal/city/alias/lookup.js +13 -13
  431. package/out/postal/city/alias/lookup.js.map +1 -1
  432. package/out/postal/city/alias/schema.d.ts +9 -12
  433. package/out/postal/city/alias/schema.d.ts.map +1 -1
  434. package/out/postal/city/alias/schema.js +2 -17
  435. package/out/postal/city/alias/schema.js.map +1 -1
  436. package/out/postal/city/candidate-schema.d.ts +12 -12
  437. package/out/postal/city/candidate-schema.d.ts.map +1 -1
  438. package/out/postal/city/candidate-schema.js +14 -12
  439. package/out/postal/city/candidate-schema.js.map +1 -1
  440. package/out/postal.d.ts +0 -5
  441. package/out/postal.d.ts.map +1 -1
  442. package/out/postal.js +2 -5
  443. package/out/postal.js.map +1 -1
  444. package/out/postcode-point-lookup.d.ts +0 -5
  445. package/out/postcode-point-lookup.d.ts.map +1 -1
  446. package/out/postcode-point-lookup.js +2 -5
  447. package/out/postcode-point-lookup.js.map +1 -1
  448. package/out/primary-preference.d.ts +20 -6
  449. package/out/primary-preference.d.ts.map +1 -1
  450. package/out/primary-preference.js +33 -6
  451. package/out/primary-preference.js.map +1 -1
  452. package/out/proximity-rerank.d.ts +2 -26
  453. package/out/proximity-rerank.d.ts.map +1 -1
  454. package/out/proximity-rerank.js +22 -19
  455. package/out/proximity-rerank.js.map +1 -1
  456. package/out/ranking-weights.d.ts +5 -7
  457. package/out/ranking-weights.d.ts.map +1 -1
  458. package/out/ranking-weights.js +2 -9
  459. package/out/ranking-weights.js.map +1 -1
  460. package/out/region-keys.d.ts +0 -15
  461. package/out/region-keys.d.ts.map +1 -1
  462. package/out/region-keys.js +14 -14
  463. package/out/region-keys.js.map +1 -1
  464. package/out/reverse.d.ts +1 -24
  465. package/out/reverse.d.ts.map +1 -1
  466. package/out/reverse.js +14 -16
  467. package/out/reverse.js.map +1 -1
  468. package/out/schema.d.ts +17 -6
  469. package/out/schema.d.ts.map +1 -1
  470. package/out/schema.js +2 -15
  471. package/out/schema.js.map +1 -1
  472. package/out/search-fetch.d.ts +0 -13
  473. package/out/search-fetch.d.ts.map +1 -1
  474. package/out/search-fetch.js +14 -11
  475. package/out/search-fetch.js.map +1 -1
  476. package/out/sqlite/convention-source.d.ts +0 -5
  477. package/out/sqlite/convention-source.d.ts.map +1 -1
  478. package/out/sqlite/convention-source.js +2 -5
  479. package/out/sqlite/convention-source.js.map +1 -1
  480. package/out/sqlite/utils.d.ts +1 -7
  481. package/out/sqlite/utils.d.ts.map +1 -1
  482. package/out/sqlite/utils.js +3 -4
  483. package/out/sqlite/utils.js.map +1 -1
  484. package/out/street/centroid/schema.d.ts +3 -6
  485. package/out/street/centroid/schema.d.ts.map +1 -1
  486. package/out/street/centroid/schema.js +2 -7
  487. package/out/street/centroid/schema.js.map +1 -1
  488. package/out/street/centroid.d.ts +0 -3
  489. package/out/street/centroid.d.ts.map +1 -1
  490. package/out/street/centroid.js +2 -6
  491. package/out/street/centroid.js.map +1 -1
  492. package/out/street/locale.d.ts +12 -0
  493. package/out/street/locale.d.ts.map +1 -0
  494. package/out/street/locale.js +54 -0
  495. package/out/street/locale.js.map +1 -0
  496. package/out/street/morphology/fst/builder.d.ts +1 -26
  497. package/out/street/morphology/fst/builder.d.ts.map +1 -1
  498. package/out/street/morphology/fst/builder.js +19 -18
  499. package/out/street/morphology/fst/builder.js.map +1 -1
  500. package/out/street/morphology/fst/loader.d.ts +0 -20
  501. package/out/street/morphology/fst/loader.d.ts.map +1 -1
  502. package/out/street/morphology/fst/loader.js +14 -13
  503. package/out/street/morphology/fst/loader.js.map +1 -1
  504. package/out/street/name-lookup.d.ts +11 -13
  505. package/out/street/name-lookup.d.ts.map +1 -1
  506. package/out/street/name-lookup.js +14 -14
  507. package/out/street/name-lookup.js.map +1 -1
  508. package/out/street/normalize.d.ts +29 -12
  509. package/out/street/normalize.d.ts.map +1 -1
  510. package/out/street/normalize.js +53 -13
  511. package/out/street/normalize.js.map +1 -1
  512. package/out/street/segment-schema.d.ts +11 -12
  513. package/out/street/segment-schema.d.ts.map +1 -1
  514. package/out/street/segment-schema.js +13 -12
  515. package/out/street/segment-schema.js.map +1 -1
  516. package/out/street/table.d.ts +154 -0
  517. package/out/street/table.d.ts.map +1 -0
  518. package/out/street/table.js +122 -0
  519. package/out/street/table.js.map +1 -0
  520. package/out/street.d.ts +1 -5
  521. package/out/street.d.ts.map +1 -1
  522. package/out/street.js +3 -5
  523. package/out/street.js.map +1 -1
  524. package/out/types.d.ts +1 -6
  525. package/out/types.d.ts.map +1 -1
  526. package/out/types.js +2 -5
  527. package/out/types.js.map +1 -1
  528. package/out/unified-schema.d.ts +18 -7
  529. package/out/unified-schema.d.ts.map +1 -1
  530. package/out/unified-schema.js +14 -10
  531. package/out/unified-schema.js.map +1 -1
  532. package/out/uprn/existence.d.ts +7 -10
  533. package/out/uprn/existence.d.ts.map +1 -1
  534. package/out/uprn/existence.js +9 -10
  535. package/out/uprn/existence.js.map +1 -1
  536. package/out/uprn/lookup.d.ts +1 -8
  537. package/out/uprn/lookup.d.ts.map +1 -1
  538. package/out/uprn/lookup.js +6 -8
  539. package/out/uprn/lookup.js.map +1 -1
  540. package/out/uprn/schema.d.ts +2 -5
  541. package/out/uprn/schema.d.ts.map +1 -1
  542. package/out/uprn/schema.js +2 -7
  543. package/out/uprn/schema.js.map +1 -1
  544. package/out/uprn.d.ts +0 -5
  545. package/out/uprn.d.ts.map +1 -1
  546. package/out/uprn.js +2 -5
  547. package/out/uprn.js.map +1 -1
  548. package/out/weights-overlay-linker.d.ts +3 -8
  549. package/out/weights-overlay-linker.d.ts.map +1 -1
  550. package/out/weights-overlay-linker.js +5 -8
  551. package/out/weights-overlay-linker.js.map +1 -1
  552. package/package.json +12 -12
  553. package/lib/currency-backfill.ts +0 -289
  554. package/out/currency-backfill.d.ts +0 -68
  555. package/out/currency-backfill.d.ts.map +0 -1
  556. package/out/currency-backfill.js +0 -169
  557. package/out/currency-backfill.js.map +0 -1
@@ -0,0 +1,423 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+ /**
4
+ * Builds one region's address layer, `lookup.db` and `detail.db`, from a stream of address records.
5
+ *
6
+ * Records are staged once into a scratch database with one row per key they answer.
7
+ * Each key table is then written in key order with `INSERT … SELECT … ORDER BY`,
8
+ * so its B-tree pages hold contiguous keys.
9
+ */
10
+ import { ByteFormatter } from "@mailwoman/core/fs/formatters";
11
+ import { readFilesystemSpace, statPath } from "@mailwoman/core/fs/readers/stat";
12
+ import { makeDirectories, removePathIfPresent } from "@mailwoman/core/fs/writers";
13
+ import { parseJSONStrict, stringifyJSON } from "@mailwoman/core/json";
14
+ import { sourcePresentCoverageCells } from "@mailwoman/core/layers";
15
+ import { writeLayerCoverage, writeLayerManifest } from "@mailwoman/core/layers/manifest";
16
+ import { createLayerCoverageTable, createLayerManifestTable } from "@mailwoman/core/layers/schema";
17
+ import { shortCellToInt } from "@mailwoman/spatial";
18
+ import { DatabaseClient } from "@mailwoman/sqlite/client";
19
+ import { buildSealedArtifact } from "@mailwoman/sqlite/sealed/build";
20
+ import { latLngToCell } from "@sister.software/h3";
21
+ import { sql } from "kysely";
22
+ import { basename, dirname, resolvePath } from "path-ts";
23
+ import { ADDRESS_DUPLICATE_STACK_LIMIT, isDuplicateAddress, isNearbyDuplicateAddress, } from "#address/duplicate";
24
+ import { addressPostcodeKey } from "#address/keys/postcode";
25
+ import { ADDRESS_SCOPED_KEY_TABLES, ADDRESS_STREET_TABLES, createAddressDetailTable, createAddressPlaceKeyTable, createAddressRegionKeyTable, createAddressScopedKeyTable, createAddressSourceTable, createAddressStreetTable, } from "#address/keys/schema";
26
+ import { addressPointStacksSQL } from "#address/stack";
27
+ /**
28
+ * The SQLite page size of both files.
29
+ *
30
+ * At 8 KiB a key hit read 4 pages in 3 range chunks on the Wisconsin prototype.
31
+ */
32
+ export const ADDRESS_LAYER_PAGE_SIZE = 8192;
33
+ /**
34
+ * The H3 resolution `layer_coverage` counts points at, about 36 km² per cell.
35
+ */
36
+ export const ADDRESS_LAYER_COVERAGE_RESOLUTION = 6;
37
+ /**
38
+ * The level each staged key row answers.
39
+ * Each level is written to the key table of the same name.
40
+ */
41
+ const StagedLevel = {
42
+ Postcode: "key_postcode",
43
+ Locality: "key_locality",
44
+ Subregion: "key_subregion",
45
+ Region: "key_region",
46
+ };
47
+ /**
48
+ * The most distinct addresses one source may place at one point before the layer refuses them all.
49
+ *
50
+ * Across the 2026-11-08 US layer, point counts fall bin by bin from 2 to 1,000 addresses per point,
51
+ * and four points lie above 1,000: two hold 1,004 and 1,653 addresses, and two hold 10,525 and 12,868.
52
+ */
53
+ export const ADDRESS_LAYER_STACK_LIMIT = 1000;
54
+ async function createStagingTables(db) {
55
+ await db.schema
56
+ .createTable("stage")
57
+ .addColumn("level", "text", (c) => c.notNull())
58
+ .addColumn("scope", "text", (c) => c.notNull())
59
+ .addColumn("street", "text", (c) => c.notNull())
60
+ .addColumn("number", "text", (c) => c.notNull())
61
+ .addColumn("address_id", "integer", (c) => c.notNull())
62
+ .addColumn("lat", "real", (c) => c.notNull())
63
+ .addColumn("lon", "real", (c) => c.notNull())
64
+ .addColumn("source_id", "integer", (c) => c.notNull())
65
+ .execute();
66
+ await db.schema
67
+ .createTable("source_ids")
68
+ .addColumn("name", "text", (c) => c.notNull())
69
+ .addColumn("source_id", "integer", (c) => c.notNull())
70
+ .execute();
71
+ await createAddressDetailTable(db);
72
+ }
73
+ /**
74
+ * Stages every record, then writes `lookup.db` and `detail.db` beside their targets
75
+ * and swaps each into place.
76
+ *
77
+ * @throws When a record's source has no entry in `sources`, or when a key table's
78
+ * row count differs from the rows staged for its level.
79
+ */
80
+ export async function buildAddressLayer(options) {
81
+ const stagingName = `${basename(options.lookupPath.toString())}.stage-${process.pid}`;
82
+ const stagingPath = options.stagingDirectory
83
+ ? resolvePath(options.stagingDirectory, stagingName)
84
+ : `${options.lookupPath}.stage-${process.pid}`;
85
+ if (options.stagingDirectory) {
86
+ await makeDirectories(options.stagingDirectory);
87
+ }
88
+ await Promise.all([makeDirectories(dirname(options.lookupPath)), makeDirectories(dirname(options.detailPath))]);
89
+ await removePathIfPresent(stagingPath);
90
+ try {
91
+ const summary = await stageRecords(options, stagingPath);
92
+ const threads = sortThreadsPragma(options.sortThreads);
93
+ await assertOutputSpace(options.lookupPath, stagingPath);
94
+ await writeLookup(options.lookupPath, stagingPath, summary, options.sources, options.manifest, options.checks ?? [], threads);
95
+ await writeDetail(options.detailPath, stagingPath, summary, threads);
96
+ return summary;
97
+ }
98
+ finally {
99
+ await removePathIfPresent(stagingPath);
100
+ }
101
+ }
102
+ /**
103
+ * The `PRAGMA threads` statement for `count` sorter helper threads, a whole number from 0.
104
+ */
105
+ function sortThreadsPragma(count) {
106
+ return `PRAGMA threads = ${Math.max(0, Math.floor(count ?? 0))}`;
107
+ }
108
+ /**
109
+ * Throws before `lookup.db` and `detail.db` are written when their filesystem has
110
+ * less free space than the staging database occupies.
111
+ *
112
+ * The staging database holds every row both files are written from, so its size bounds what they need.
113
+ * A shortfall found here stops the build with the space it needs.
114
+ *
115
+ * Without the check, SQLite reports `database or disk is full` partway through writing.
116
+ */
117
+ async function assertOutputSpace(lookupPath, stagingPath) {
118
+ const needed = (await statPath(stagingPath)).size;
119
+ const space = await readFilesystemSpace(dirname(lookupPath));
120
+ if (space.availableBytes < needed) {
121
+ throw new Error(`${space.measuredAt} has ${ByteFormatter.formatIEC(space.availableBytes)} free, and writing ` +
122
+ `${basename(lookupPath.toString())} and detail.db from the staging database ${stagingPath} needs up to ` +
123
+ `${ByteFormatter.formatIEC(needed)}. Free space on that filesystem and build again.`);
124
+ }
125
+ }
126
+ async function stageRecords(options, stagingPath) {
127
+ using db = new DatabaseClient(stagingPath);
128
+ db.exec(`PRAGMA page_size = ${ADDRESS_LAYER_PAGE_SIZE}; PRAGMA journal_mode = OFF; PRAGMA synchronous = OFF; PRAGMA cache_size = -400000;`);
129
+ db.exec(sortThreadsPragma(options.sortThreads));
130
+ await createStagingTables(db);
131
+ const stage = db.prepare("INSERT INTO stage VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
132
+ const detail = db.prepare("INSERT INTO address VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
133
+ const sourceIDs = new Map();
134
+ const keys = { key_postcode: 0, key_locality: 0, key_subregion: 0, key_region: 0 };
135
+ const sources = {};
136
+ const coverage = new Map();
137
+ const refused = {};
138
+ let records = 0;
139
+ let localityNames = 0;
140
+ const refuse = (source, reason, count = 1) => {
141
+ const bySource = (refused[source] ??= {});
142
+ bySource[reason] = (bySource[reason] ?? 0) + count;
143
+ };
144
+ db.exec("BEGIN");
145
+ for await (const record of options.records) {
146
+ if (!options.sources.has(record.source)) {
147
+ throw new Error(`address record ${record.sourceRecordID} has source ${record.source}, which the build lists no terms for`);
148
+ }
149
+ const reason = options.refuseRecord?.(record);
150
+ if (reason) {
151
+ refuse(record.source, reason);
152
+ continue;
153
+ }
154
+ let sourceID = sourceIDs.get(record.source);
155
+ if (sourceID == null) {
156
+ sourceID = sourceIDs.size + 1;
157
+ sourceIDs.set(record.source, sourceID);
158
+ }
159
+ const addressID = ++records;
160
+ sources[record.source] = (sources[record.source] ?? 0) + 1;
161
+ const put = (level, scope) => {
162
+ stage.run(level, scope, record.streetKey, record.number, addressID, record.lat, record.lon, sourceID);
163
+ keys[level]++;
164
+ };
165
+ const postcode = addressPostcodeKey(record.country, record.postcode);
166
+ if (postcode) {
167
+ put(StagedLevel.Postcode, postcode);
168
+ }
169
+ for (const name of record.names) {
170
+ if (name.level === "locality" || name.level === "dependent_locality") {
171
+ put(StagedLevel.Locality, name.key);
172
+ localityNames++;
173
+ }
174
+ else if (name.level === "subregion") {
175
+ put(StagedLevel.Subregion, name.key);
176
+ }
177
+ }
178
+ put(StagedLevel.Region, "");
179
+ const cell = shortCellToInt(latLngToCell(record.lat, record.lon, ADDRESS_LAYER_COVERAGE_RESOLUTION));
180
+ coverage.set(cell, (coverage.get(cell) ?? 0) + 1);
181
+ detail.run(addressID, sourceID, record.sourceRecordID, record.country, record.number, record.unit, record.streetRaw, record.postcode, stringifyJSON(record.names.map(({ level, form, value }) => ({ level, form, value }))), record.adminCode, record.lat, record.lon, record.placement, record.authority);
182
+ if (records % 200_000 === 0) {
183
+ db.exec("COMMIT; BEGIN");
184
+ }
185
+ if (records % 1_000_000 === 0) {
186
+ options.onProgress?.(records);
187
+ }
188
+ }
189
+ db.exec("COMMIT");
190
+ const sourceRow = db.prepare("INSERT INTO source_ids VALUES (?, ?)");
191
+ for (const [name, id] of sourceIDs) {
192
+ sourceRow.run(name, id);
193
+ }
194
+ const sourceNames = new Map([...sourceIDs].map(([name, id]) => [id, name]));
195
+ // Removes the records a temporary table lists from the staged keys and details,
196
+ // counts them under `reason`, and lowers the counts the build's checks compare to match.
197
+ // The rows are streamed: one state can drop tens of millions of repeats,
198
+ // more than fit in the heap as objects.
199
+ const drop = (table, reason) => {
200
+ let dropped = 0;
201
+ for (const row of db
202
+ .prepare(`SELECT a.source_id, a.lat, a.lon FROM ${table} t JOIN address a USING (address_id)`)
203
+ .iterate()) {
204
+ dropped++;
205
+ const source = sourceNames.get(row.source_id);
206
+ const cell = shortCellToInt(latLngToCell(row.lat, row.lon, ADDRESS_LAYER_COVERAGE_RESOLUTION));
207
+ const remaining = (coverage.get(cell) ?? 0) - 1;
208
+ if (remaining > 0) {
209
+ coverage.set(cell, remaining);
210
+ }
211
+ else {
212
+ coverage.delete(cell);
213
+ }
214
+ sources[source] = (sources[source] ?? 0) - 1;
215
+ refuse(source, reason);
216
+ }
217
+ for (const { level, n } of db
218
+ .prepare(`SELECT level, count(*) AS n FROM stage WHERE address_id IN (SELECT address_id FROM ${table}) GROUP BY level`)
219
+ .all()) {
220
+ keys[level] -= n;
221
+ }
222
+ db.exec(`DELETE FROM stage WHERE address_id IN (SELECT address_id FROM ${table})`);
223
+ db.exec(`DELETE FROM address WHERE address_id IN (SELECT address_id FROM ${table})`);
224
+ db.exec(`DROP TABLE ${table}`);
225
+ records -= dropped;
226
+ };
227
+ // A point one source shares among more than `stackLimit` addresses locates none of them.
228
+ db.exec(`CREATE TEMP TABLE stacked AS
229
+ SELECT address_id FROM address
230
+ WHERE (source_id, round(lat, 5), round(lon, 5)) IN (
231
+ SELECT source_id, la, lo FROM (${addressPointStacksSQL(options.stackLimit ?? ADDRESS_LAYER_STACK_LIMIT)}))`);
232
+ drop("stacked", "stacked-point");
233
+ if (options.duplicates) {
234
+ markDuplicates(db, options.duplicates, sourceNames);
235
+ drop("duplicate", "duplicate");
236
+ }
237
+ return {
238
+ records,
239
+ keys,
240
+ sources,
241
+ localityFormsPerRecord: records ? localityNames / records : 0,
242
+ coverage,
243
+ refused,
244
+ };
245
+ }
246
+ /**
247
+ * Lists in a temporary `duplicate` table every staged record that repeats a higher-ranked
248
+ * record of its postcode key group, by {@link isDuplicateAddress}.
249
+ * Only groups holding two or more sources are read.
250
+ */
251
+ function markDuplicates(db, rule, sourceNames) {
252
+ // SQLite rounds the points for both the stack count and the join, so a record meets its own point's count.
253
+ db.exec(`CREATE TEMP TABLE stack AS ${addressPointStacksSQL(ADDRESS_DUPLICATE_STACK_LIMIT)}`);
254
+ db.exec("CREATE INDEX temp.stack_point ON stack (source_id, la, lo)");
255
+ db.exec("CREATE TEMP TABLE duplicate (address_id INTEGER PRIMARY KEY)");
256
+ const mark = db.prepare("INSERT OR IGNORE INTO duplicate VALUES (?)");
257
+ // Reads each street-and-number group the query yields, in key order, and marks every
258
+ // lower-ranked member that `repeats` judges a repeat of a higher-ranked one.
259
+ const settleGroups = (query, repeats) => {
260
+ let group = [];
261
+ let key = "";
262
+ const settle = () => {
263
+ for (const lower of group) {
264
+ if (group.some((higher) => higher.rank < lower.rank && repeats(higher, lower))) {
265
+ mark.run(lower.addressID);
266
+ }
267
+ }
268
+ };
269
+ for (const row of db.prepare(query).iterate()) {
270
+ const rowKey = `${row.scope}|${row.street}|${row.key_number}`;
271
+ if (rowKey !== key) {
272
+ settle();
273
+ group = [];
274
+ key = rowKey;
275
+ }
276
+ const source = sourceNames.get(row.source_id);
277
+ const names = parseJSONStrict(row.names);
278
+ group.push({
279
+ addressID: row.address_id,
280
+ rank: rule.rank(source),
281
+ source,
282
+ unit: row.unit,
283
+ number: row.number,
284
+ streetRaw: row.street_raw,
285
+ localities: names.filter((name) => name.level === "locality").map((name) => name.value),
286
+ lat: row.lat,
287
+ lon: row.lon,
288
+ stack: row.stack,
289
+ postcode: row.postcode === 1,
290
+ });
291
+ }
292
+ settle();
293
+ };
294
+ const memberColumns = `s.scope, s.street, s.number AS key_number, a.address_id, a.source_id, a.unit, a.number,
295
+ a.street_raw, a.names, a.lat, a.lon, coalesce(k.n, 1) AS stack`;
296
+ const stackJoin = "LEFT JOIN stack k ON k.source_id = a.source_id AND k.la = round(a.lat, 5) AND k.lo = round(a.lon, 5)";
297
+ db.exec("BEGIN");
298
+ settleGroups(`SELECT ${memberColumns}, 1 AS postcode
299
+ FROM stage s JOIN address a ON a.address_id = s.address_id ${stackJoin}
300
+ WHERE s.level = '${StagedLevel.Postcode}' AND (s.scope, s.street, s.number) IN (
301
+ SELECT scope, street, number FROM stage WHERE level = '${StagedLevel.Postcode}'
302
+ GROUP BY 1, 2, 3 HAVING count(DISTINCT source_id) > 1)
303
+ ORDER BY s.scope, s.street, s.number`, (higher, lower) => isDuplicateAddress(rule, higher, lower));
304
+ // A record with no postcode key shares no postcode group, so it is compared at the region level
305
+ // with every record of its street and number, and repeats one only when their points are near.
306
+ db.exec(`CREATE TEMP TABLE keyed (address_id INTEGER PRIMARY KEY);
307
+ INSERT OR IGNORE INTO keyed SELECT address_id FROM stage WHERE level = '${StagedLevel.Postcode}'`);
308
+ settleGroups(`SELECT ${memberColumns}, p.address_id IS NOT NULL AS postcode
309
+ FROM stage s JOIN address a ON a.address_id = s.address_id ${stackJoin}
310
+ LEFT JOIN keyed p ON p.address_id = s.address_id
311
+ WHERE s.level = '${StagedLevel.Region}' AND (s.street, s.number) IN (
312
+ SELECT g.street, g.number FROM stage g LEFT JOIN keyed q ON q.address_id = g.address_id
313
+ WHERE g.level = '${StagedLevel.Region}'
314
+ GROUP BY 1, 2 HAVING count(DISTINCT g.source_id) > 1 AND count(q.address_id) < count(*))
315
+ ORDER BY s.street, s.number`, (higher, lower) => !(higher.postcode && lower.postcode) && isNearbyDuplicateAddress(rule, higher, lower));
316
+ db.exec("DROP TABLE keyed");
317
+ db.exec("COMMIT");
318
+ db.exec("DROP TABLE stack");
319
+ }
320
+ async function writeLookup(lookupPath, stagingPath, summary, terms, manifest, checks, threads) {
321
+ await buildSealedArtifact({
322
+ out: lookupPath,
323
+ createTables: async (db) => {
324
+ db.exec(`PRAGMA page_size = ${ADDRESS_LAYER_PAGE_SIZE}`);
325
+ db.exec(threads);
326
+ await createLayerManifestTable(db);
327
+ await createLayerCoverageTable(db);
328
+ await createAddressSourceTable(db);
329
+ for (const table of ADDRESS_SCOPED_KEY_TABLES) {
330
+ await createAddressScopedKeyTable(db, table);
331
+ }
332
+ await createAddressPlaceKeyTable(db);
333
+ await createAddressRegionKeyTable(db);
334
+ for (const table of ADDRESS_STREET_TABLES) {
335
+ await createAddressStreetTable(db, table);
336
+ }
337
+ },
338
+ ingest: async (db) => {
339
+ db.exec(`ATTACH DATABASE '${stagingPath.replaceAll("'", "''")}' AS staged`);
340
+ const insertSource = db.prepare("INSERT INTO source VALUES (?, ?, ?, ?, ?)");
341
+ const ids = db.prepare("SELECT name, source_id FROM staged.source_ids ORDER BY source_id").all();
342
+ for (const { name, source_id } of ids) {
343
+ const term = terms.get(name);
344
+ insertSource.run(source_id, name, term.license, term.authority, summary.sources[name] ?? 0);
345
+ }
346
+ // A record's locality and dependent-locality names can share a key,
347
+ // so a key row repeated for one address is written once.
348
+ for (const table of ADDRESS_SCOPED_KEY_TABLES) {
349
+ db.exec(`INSERT OR IGNORE INTO ${table} SELECT scope, street, number, address_id, lat, lon, source_id
350
+ FROM staged.stage WHERE level = '${table}' ORDER BY scope, street, number, address_id`);
351
+ }
352
+ db.exec(`INSERT OR IGNORE INTO key_region SELECT street, number, address_id, lat, lon, source_id
353
+ FROM staged.stage WHERE level = 'key_region' ORDER BY street, number, address_id`);
354
+ for (const [table, level] of [
355
+ ["street_postcode", "key_postcode"],
356
+ ["street_locality", "key_locality"],
357
+ ]) {
358
+ db.exec(`INSERT INTO ${table} SELECT scope, street, avg(lat), avg(lon), min(lat), max(lat), min(lon), max(lon), count(*)
359
+ FROM staged.stage WHERE level = '${level}' GROUP BY scope, street ORDER BY scope, street`);
360
+ }
361
+ db.exec("DETACH DATABASE staged");
362
+ return true;
363
+ },
364
+ finish: async (db) => {
365
+ for (const table of [...ADDRESS_SCOPED_KEY_TABLES, "key_region"]) {
366
+ const { count } = (await db
367
+ .selectFrom(table)
368
+ .select(sql `count(*)`.as("count"))
369
+ .executeTakeFirstOrThrow());
370
+ if (count > summary.keys[table]) {
371
+ throw new Error(`${table} holds ${count} rows, more than the ${summary.keys[table]} staged for it`);
372
+ }
373
+ }
374
+ const unanswered = checks.filter((check) => {
375
+ const row = check.table === StagedLevel.Region
376
+ ? db
377
+ .prepare("SELECT 1 FROM key_region WHERE street = ? AND number = ? LIMIT 1")
378
+ .get(check.street, check.number)
379
+ : db
380
+ .prepare(`SELECT 1 FROM ${check.table} WHERE scope = ? AND street = ? AND number = ? LIMIT 1`)
381
+ .get(check.scope, check.street, check.number);
382
+ return row == null;
383
+ });
384
+ if (unanswered.length) {
385
+ const listed = unanswered.map((check) => `${check.table} ${check.scope} ${check.street} ${check.number}`);
386
+ throw new Error(`lookup.db misses ${unanswered.length} of ${checks.length} checked reads: ${listed.join("; ")}`);
387
+ }
388
+ await writeLayerCoverage(db, sourcePresentCoverageCells(summary.coverage));
389
+ await writeLayerManifest(db, {
390
+ ...manifest,
391
+ schemaVersion: 1,
392
+ spineKeys: { h3: { column: "layer_coverage.h3_cell", resolution: ADDRESS_LAYER_COVERAGE_RESOLUTION } },
393
+ sourceRecords: summary.sources,
394
+ });
395
+ },
396
+ });
397
+ }
398
+ async function writeDetail(detailPath, stagingPath, summary, threads) {
399
+ await buildSealedArtifact({
400
+ out: detailPath,
401
+ createTables: async (db) => {
402
+ db.exec(`PRAGMA page_size = ${ADDRESS_LAYER_PAGE_SIZE}`);
403
+ db.exec(threads);
404
+ await createAddressDetailTable(db);
405
+ },
406
+ ingest: async (db) => {
407
+ db.exec(`ATTACH DATABASE '${stagingPath.replaceAll("'", "''")}' AS staged`);
408
+ db.exec("INSERT INTO address SELECT * FROM staged.address ORDER BY address_id");
409
+ db.exec("DETACH DATABASE staged");
410
+ return true;
411
+ },
412
+ finish: async (db) => {
413
+ const { count } = (await db
414
+ .selectFrom("address")
415
+ .select(sql `count(*)`.as("count"))
416
+ .executeTakeFirstOrThrow());
417
+ if (count !== summary.records) {
418
+ throw new Error(`address holds ${count} rows, against ${summary.records} records staged`);
419
+ }
420
+ },
421
+ });
422
+ }
423
+ //# sourceMappingURL=build.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"build.js","sourceRoot":"","sources":["../../../lib/address/keys/build.ts"],"names":[],"mappings":"AAAA,kEAAkE;AAClE,kEAAkE;AAElE;;;;;;GAMG;AAEH,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAA;AAC7D,OAAO,EAAE,mBAAmB,EAAE,QAAQ,EAAE,MAAM,iCAAiC,CAAA;AAC/E,OAAO,EAAE,eAAe,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAA;AACjF,OAAO,EAAE,eAAe,EAAE,aAAa,EAAE,MAAM,sBAAsB,CAAA;AACrE,OAAO,EAAE,0BAA0B,EAAE,MAAM,wBAAwB,CAAA;AACnE,OAAO,EAAsB,kBAAkB,EAAE,kBAAkB,EAAE,MAAM,iCAAiC,CAAA;AAC5G,OAAO,EAAE,wBAAwB,EAAE,wBAAwB,EAAE,MAAM,+BAA+B,CAAA;AAClG,OAAO,EAAE,cAAc,EAAE,MAAM,oBAAoB,CAAA;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAA;AACzD,OAAO,EAAE,mBAAmB,EAAE,MAAM,gCAAgC,CAAA;AACpE,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAA;AAClD,OA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
3
+ /**
4
+ * The row count above which a country is written one file per first-level subdivision.
5
+ *
6
+ * At 10 M, France (26.0 M rows) and Germany (20.3 M) split and Taiwan (9.7 M) doesn't.
7
+ */
8
+ export declare const ADDRESS_LAYER_SPLIT_ROWS = 10000000;
9
+ /**
10
+ * One file set of a country's address layer: the whole country, or one of its subdivisions.
11
+ */
12
+ export interface AddressLayerRegion {
13
+ /**
14
+ * The region's path segment: the lower-case ISO 3166-1 code (`tw`), or the
15
+ * lower-case ISO 3166-2 code of a subdivision (`us-wi`).
16
+ */
17
+ region: string;
18
+ /**
19
+ * The ISO 3166-2 code the region's records hold, or `null` for a whole-country file.
20
+ */
21
+ subdivision: string | null;
22
+ records: number;
23
+ }
24
+ /**
25
+ * The files a country's address layer is written as, from its record count per subdivision.
26
+ *
27
+ * A country above `threshold` rows is written one file per subdivision.
28
+ * Records without a subdivision then go to a file of the country's own.
29
+ * A country at or below the threshold is one file.
30
+ *
31
+ * @param bySubdivision Records per ISO 3166-2 code, with `null` for records that state none.
32
+ */
33
+ export declare function planAddressLayerRegions(country: string, bySubdivision: ReadonlyMap<string | null, number>, threshold?: number): AddressLayerRegion[];
34
+ /**
35
+ * The two files of one region at one version, under `root`.
36
+ */
37
+ export declare function addressLayerFiles(root: PathBuilderLike, region: string, version: string): {
38
+ lookup: PathBuilder;
39
+ detail: PathBuilder;
40
+ };
41
+ /**
42
+ * Where a country's manifest for one version lives, under `root`.
43
+ */
44
+ export declare function addressLayerManifestPath(root: PathBuilderLike, country: string, version: string): PathBuilder;
45
+ /**
46
+ * A country's address-layer manifest for one version.
47
+ *
48
+ * Each file is listed by its path relative to the layer root.
49
+ */
50
+ export interface AddressLayerCountryManifest {
51
+ country: string;
52
+ version: string;
53
+ /**
54
+ * The split threshold the build applied, so a reader sees why a country is one file or several.
55
+ */
56
+ splitRows: number;
57
+ /**
58
+ * The most distinct addresses one source could place at one point before the build refused them.
59
+ */
60
+ stackLimit?: number;
61
+ /**
62
+ * The duplicate rule the build dropped repeated records by: the source ranking it applied,
63
+ * highest first, and the distance and stacked-point limits of `isDuplicateAddress`.
64
+ */
65
+ duplicateRule?: {
66
+ ranking: readonly string[];
67
+ distanceKm: number;
68
+ stackLimit: number;
69
+ };
70
+ regions: Array<AddressLayerRegion & {
71
+ lookup: RecordedFile;
72
+ detail: RecordedFile;
73
+ /**
74
+ * Records the build left out of the region, per source id and reason.
75
+ */
76
+ refused?: Record<string, Record<string, number>>;
77
+ }>;
78
+ }
79
+ /**
80
+ * One built file's path relative to `root`, its size and its SHA-256, for the manifest.
81
+ */
82
+ export declare function describeAddressLayerFile(root: PathBuilderLike, path: PathBuilderLike): Promise<RecordedFile>;
83
+ /**
84
+ * The newest manifest a country's address layer holds under `root`, by its dated
85
+ * version directory, or `null` when the country has no layer there.
86
+ */
87
+ export declare function readLatestAddressLayerManifest(root: PathBuilderLike, country: string): Promise<AddressLayerCountryManifest | null>;
88
+ /**
89
+ * Writes a country's manifest beside its files, after every region it lists is built.
90
+ */
91
+ export declare function writeAddressLayerManifest(root: PathBuilderLike, manifest: AddressLayerCountryManifest): Promise<PathBuilder>;
92
+ //# sourceMappingURL=layout.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"layout.d.ts","sourceRoot":"","sources":["../../../lib/address/keys/layout.ts"],"names":[],"mappings":"AAeA,OAAO,EAAE,KAAK,YAAY,EAAc,MAAM,sBAAsB,CAAA;AACpE,OAAO,EAAgC,KAAK,WAAW,EAAE,KAAK,eAAe,EAAE,MAAM,SAAS,CAAA;AAE9F;;;;GAIG;AACH,eAAO,MAAM,wBAAwB,WAAa,CAAA;AAElD;;GAEG;AACH,MAAM,WAAW,kBAAkB;IAClC;;;OAGG;IACH,MAAM,EAAE,MAAM,CAAA;IACd;;OAEG;IACH,WAAW,EAAE,MAAM,GAAG,IAAI,CAAA;IAC1B,OAAO,EAAE,MAAM,CAAA;CACf;AAED;;;;;;;;GAQG;AACH,wBAAgB,uBAAuB,CACtC,OAAO,EAAE,MAAM,EAC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CAAC,CAS7C;AAED;;GAEG;AACH,wBAAsB,yBAAyB,CAC9C,IAAI,EAAE,eAAe,EACrB,QAAQ,EAAE,2BAA2B,GACnC,OAAO,CAAC,WAAW,CAAC,CAMtB"}
@@ -0,0 +1,87 @@
1
+ // SPDX-FileCopyrightText: Teffen Ellis and Mailwoman contributors
2
+ // SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
3
+ /**
4
+ * Where a country's address-layer files live, how a country is split into them,
5
+ * and the manifest that lists them.
6
+ *
7
+ * Each file sits under a dated prefix, `addresses/<region>/<version>/`, and is never overwritten.
8
+ * A reader learns a country's files from its manifest, `addresses/<cc>/<version>/manifest.json`,
9
+ * so code holds no list of countries or subdivisions.
10
+ */
11
+ import { readLocalJSONFile, sortedGlob } from "@mailwoman/core/fs/readers";
12
+ import { pathExists, statPath } from "@mailwoman/core/fs/readers/stat";
13
+ import { writeLocalJSONFile } from "@mailwoman/core/fs/writers";
14
+ import { sha256File } from "@mailwoman/core/hash";
15
+ import { relative, resolvePathBuilder } from "path-ts";
16
+ /**
17
+ * The row count above which a country is written one file per first-level subdivision.
18
+ *
19
+ * At 10 M, France (26.0 M rows) and Germany (20.3 M) split and Taiwan (9.7 M) doesn't.
20
+ */
21
+ export const ADDRESS_LAYER_SPLIT_ROWS = 10_000_000;
22
+ /**
23
+ * The files a country's address layer is written as, from its record count per subdivision.
24
+ *
25
+ * A country above `threshold` rows is written one file per subdivision.
26
+ * Records without a subdivision then go to a file of the country's own.
27
+ * A country at or below the threshold is one file.
28
+ *
29
+ * @param bySubdivision Records per ISO 3166-2 code, with `null` for records that state none.
30
+ */
31
+ export function planAddressLayerRegions(country, bySubdivision, threshold = ADDRESS_LAYER_SPLIT_ROWS) {
32
+ const cc = country.toLowerCase();
33
+ let total = 0;
34
+ for (const count of bySubdivision.values()) {
35
+ total += count;
36
+ }
37
+ if (total <= threshold)
38
+ return [{ region: cc, subdivision: null, records: total }];
39
+ return [...bySubdivision]
40
+ .map(([subdivision, records]) => ({
41
+ region: subdivision ? subdivision.toLowerCase() : cc,
42
+ subdivision,
43
+ records,
44
+ }))
45
+ .toSorted((left, right) => left.region.localeCompare(right.region));
46
+ }
47
+ /**
48
+ * The two files of one region at one version, under `root`.
49
+ */
50
+ export function addressLayerFiles(root, region, version) {
51
+ const directory = resolvePathBuilder(root, "addresses", region, version);
52
+ return { lookup: directory("lookup.db"), detail: directory("detail.db") };
53
+ }
54
+ /**
55
+ * Where a country's manifest for one version lives, under `root`.
56
+ */
57
+ export function addressLayerManifestPath(root, country, version) {
58
+ return resolvePathBuilder(root, "addresses", country.toLowerCase(), version, "manifest.json");
59
+ }
60
+ /**
61
+ * One built file's path relative to `root`, its size and its SHA-256, for the manifest.
62
+ */
63
+ export async function describeAddressLayerFile(root, path) {
64
+ const [stats, sha256] = await Promise.all([statPath(path), sha256File(path)]);
65
+ return { path: relative(root, path).toString(), bytes: stats.size, sha256 };
66
+ }
67
+ /**
68
+ * The newest manifest a country's address layer holds under `root`, by its dated
69
+ * version directory, or `null` when the country has no layer there.
70
+ */
71
+ export async function readLatestAddressLayerManifest(root, country) {
72
+ const countryDir = resolvePathBuilder(root, "addresses", country.toLowerCase());
73
+ if (!(await pathExists(countryDir)))
74
+ return null;
75
+ // Versions are ISO dates, so code-point order is date order and the last manifest is the newest.
76
+ const newest = (await sortedGlob(countryDir, "*/manifest.json")).at(-1);
77
+ return newest ? readLocalJSONFile(newest) : null;
78
+ }
79
+ /**
80
+ * Writes a country's manifest beside its files, after every region it lists is built.
81
+ */
82
+ export async function writeAddressLayerManifest(root, manifest) {
83
+ const path = addressLayerManifestPath(root, manifest.country, manifest.version);
84
+ await writeLocalJSONFile(manifest, path);
85
+ return path;
86
+ }
87
+ //# sourceMappingURL=layout.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"layout.js","sourceRoot":"","sources":["../../../lib/address/keys/layout.ts"],"names":[],"mappings":"AAAA,kEAAkE;AAClE,kEAAkE;AAElE;;;;;;;GAOG;AA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C,CAAA;IAEvE,OAAO,MAAM,CAAC,CAAC,CAAC,iBAAiB,CAA8B,MAAM,CAAC,CAAC,CAAC,CAAC,IAAI,CAAA;AAC9E,CAAC;AAED;;GAEG;AACH,MAAM,CAAC,KAAK,UAAU,yBAAyB,CAC9C,IAAqB,EACrB,QAAqC;IAErC,MAAM,IAAI,GAAG,wBAAwB,CAAC,IAAI,EAAE,QAAQ,CAAC,OAAO,EAAE,QAAQ,CAAC,OAAO,CAAC,CAAA;IAE/E,MAAM,kBAAkB,CAA8B,QAAQ,EAAE,IAAI,CAAC,CAAA;IAErE,OAAO,IAAI,CAAA;AACZ,CAAC"}
@@ -0,0 +1,9 @@
1
+ import type { DatabaseClient } from "@mailwoman/sqlite/client";
2
+ import type { AddressStatementRunner } from "#address/keys/lookup";
3
+ /**
4
+ * An {@link AddressStatementRunner} over a local `lookup.db`, for the CLI and the Node API.
5
+ *
6
+ * It lives apart from the reader, so the browser loads the reader without `node:sqlite`.
7
+ */
8
+ export declare function localAddressStatementRunner(db: DatabaseClient<unknown>): AddressStatementRunner;
9
+ //# sourceMappingURL=local-runner.d.ts.map