@konstit/dne 0.1.0 → 0.3.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 (82) hide show
  1. package/README.md +85 -352
  2. package/dist/address-search-index.d.ts +34 -0
  3. package/dist/address-search-index.js +326 -0
  4. package/dist/address-search-index.js.map +1 -0
  5. package/dist/address-search-pool-errors.d.ts +15 -0
  6. package/dist/address-search-pool-errors.js +25 -0
  7. package/dist/address-search-pool-errors.js.map +1 -0
  8. package/dist/address-search-pool.d.ts +40 -0
  9. package/dist/address-search-pool.js +215 -0
  10. package/dist/address-search-pool.js.map +1 -0
  11. package/dist/address-search-rank.d.ts +5 -0
  12. package/dist/address-search-rank.js +89 -0
  13. package/dist/address-search-rank.js.map +1 -0
  14. package/dist/address-search-text.d.ts +6 -0
  15. package/dist/address-search-text.js +16 -0
  16. package/dist/address-search-text.js.map +1 -0
  17. package/dist/address-search-worker-runtime.d.ts +41 -0
  18. package/dist/address-search-worker-runtime.js +38 -0
  19. package/dist/address-search-worker-runtime.js.map +1 -0
  20. package/dist/address-search-worker-source.d.ts +2 -0
  21. package/dist/address-search-worker-source.js +5 -0
  22. package/dist/address-search-worker-source.js.map +1 -0
  23. package/dist/address-search.d.ts +31 -0
  24. package/dist/address-search.js +152 -0
  25. package/dist/address-search.js.map +1 -0
  26. package/dist/bairro-runs.d.ts +20 -0
  27. package/dist/bairro-runs.js +202 -0
  28. package/dist/bairro-runs.js.map +1 -0
  29. package/dist/binary-db-errors.d.ts +1 -1
  30. package/dist/binary-db-errors.js +1 -1
  31. package/dist/binary-db-errors.js.map +1 -1
  32. package/dist/binary-db-format.d.ts +10 -2
  33. package/dist/binary-db-format.js +9 -1
  34. package/dist/binary-db-format.js.map +1 -1
  35. package/dist/binary-db-integrity.d.ts +4 -0
  36. package/dist/binary-db-integrity.js +20 -0
  37. package/dist/binary-db-integrity.js.map +1 -0
  38. package/dist/binary-db-reader.d.ts +46 -28
  39. package/dist/binary-db-reader.js +237 -360
  40. package/dist/binary-db-reader.js.map +1 -1
  41. package/dist/binary-db-validator.d.ts +13 -0
  42. package/dist/binary-db-validator.js +709 -0
  43. package/dist/binary-db-validator.js.map +1 -0
  44. package/dist/chunks/dynamic-completion-wbp85hh9-cytdw6ns.js +7 -0
  45. package/dist/chunks/dynamic-completion-wbp85hh9-cytdw6ns.js.map +10 -0
  46. package/dist/chunks/index-338dn00y.js +18 -0
  47. package/dist/chunks/index-338dn00y.js.map +10 -0
  48. package/dist/expand.d.ts +8 -0
  49. package/dist/expand.js +1484 -0
  50. package/dist/expand.js.map +1 -0
  51. package/dist/fsst.d.ts +18 -0
  52. package/dist/fsst.js +117 -0
  53. package/dist/fsst.js.map +1 -0
  54. package/dist/index.js +48 -48
  55. package/dist/index.js.map +30 -18
  56. package/dist/library.d.ts +9 -2
  57. package/dist/library.js +9 -2
  58. package/dist/library.js.map +1 -1
  59. package/dist/locality-runs.d.ts +18 -0
  60. package/dist/locality-runs.js +108 -0
  61. package/dist/locality-runs.js.map +1 -0
  62. package/dist/packed-bits.d.ts +6 -0
  63. package/dist/packed-bits.js +41 -0
  64. package/dist/packed-bits.js.map +1 -0
  65. package/dist/settings.d.ts +0 -2
  66. package/dist/settings.js +0 -2
  67. package/dist/settings.js.map +1 -1
  68. package/dist/shared-file.d.ts +2 -0
  69. package/dist/shared-file.js +35 -0
  70. package/dist/shared-file.js.map +1 -0
  71. package/dist/sqlite-db-reader.d.ts +13 -4
  72. package/dist/sqlite-db-reader.js +39 -17
  73. package/dist/sqlite-db-reader.js.map +1 -1
  74. package/dist/types.d.ts +29 -4
  75. package/dist/types.js +73 -4
  76. package/dist/types.js.map +1 -1
  77. package/package.json +13 -10
  78. package/data/dne.bin +0 -0
  79. package/dist/chunks/dynamic-completion-403da357-4jc4w04x.js +0 -7
  80. package/dist/chunks/dynamic-completion-403da357-4jc4w04x.js.map +0 -10
  81. package/dist/chunks/index-s0h4vdva.js +0 -17
  82. package/dist/chunks/index-s0h4vdva.js.map +0 -10
@@ -0,0 +1,326 @@
1
+ import { createHash } from 'node:crypto';
2
+ import { addressTokens } from "./address-search-text.js";
3
+ import { integerByteWidth, readByteWidth, readPackedInteger, writePackedInteger, } from "./binary-db-format.js";
4
+ import { ALL_UFS, } from "./types.js";
5
+ const MAGIC = 'DNESRCH1';
6
+ const VERSION = 2;
7
+ const HEADER_SIZE = 80;
8
+ const DIGEST_SIZE = 32;
9
+ /** Builds an optional, versioned search index bound to the reader's exact database snapshot. */
10
+ export function buildAddressSearchIndex(database) {
11
+ const checksum = database.checksum();
12
+ const rowCount = database.rowCount();
13
+ const ceps = new Uint32Array(rowCount);
14
+ const rowMunicipalities = new Uint32Array(rowCount);
15
+ const rowFields = new Uint32Array(rowCount * 4);
16
+ const municipalities = [];
17
+ const municipalityIds = new Map();
18
+ const terms = [];
19
+ const termIds = new Map();
20
+ const lists = [];
21
+ const fields = [[]];
22
+ const fieldIds = new Map([['', 0]]);
23
+ const localityTokens = new Map();
24
+ const tokenizeLocality = (text) => {
25
+ let tokens = localityTokens.get(text);
26
+ if (!tokens) {
27
+ tokens = addressTokens(text);
28
+ localityTokens.set(text, tokens);
29
+ }
30
+ return tokens;
31
+ };
32
+ let rowId = 0;
33
+ for (const row of database.entries()) {
34
+ ceps[rowId] = Number(row.cep);
35
+ let municipality = municipalityIds.get(row.municipio_cod_ibge);
36
+ if (municipality === undefined) {
37
+ municipality = municipalities.length;
38
+ municipalityIds.set(row.municipio_cod_ibge, municipality);
39
+ municipalities.push({ code: row.municipio_cod_ibge, uf: row.uf });
40
+ }
41
+ rowMunicipalities[rowId] = municipality;
42
+ const rowTerms = new Set();
43
+ const texts = [
44
+ addressTokens(row.logradouro ?? ''),
45
+ addressTokens(row.nome ?? ''),
46
+ tokenizeLocality(row.bairro ?? ''),
47
+ tokenizeLocality(row.municipio),
48
+ ];
49
+ for (const [column, words,] of texts.entries()) {
50
+ const text = words.join(' ');
51
+ let field = fieldIds.get(text);
52
+ if (field === undefined) {
53
+ field = fields.length;
54
+ fieldIds.set(text, field);
55
+ fields.push(words.map((word) => {
56
+ let term = termIds.get(word);
57
+ if (term === undefined) {
58
+ term = terms.length;
59
+ termIds.set(word, term);
60
+ terms.push(word);
61
+ lists.push([]);
62
+ }
63
+ return term;
64
+ }));
65
+ }
66
+ rowFields[rowId * 4 + column] = field;
67
+ for (const term of fields[field]) {
68
+ rowTerms.add(term);
69
+ }
70
+ }
71
+ for (const term of rowTerms) {
72
+ lists[term].push(rowId);
73
+ }
74
+ rowId++;
75
+ }
76
+ const sorted = [...terms.keys()].sort((a, b) => terms[a] < terms[b] ? -1 : 1);
77
+ const remap = new Uint32Array(terms.length);
78
+ for (const [id, original,] of sorted.entries()) {
79
+ remap[original] = id;
80
+ }
81
+ const lexicon = new TextEncoder().encode(JSON.stringify(sorted.map((id) => terms[id])));
82
+ const offsets = [0];
83
+ const chunks = sorted.map((id) => {
84
+ const chunk = encodePosting(lists[id]);
85
+ offsets.push(offsets[offsets.length - 1] + chunk.length);
86
+ return chunk;
87
+ });
88
+ const postingsLength = offsets[offsets.length - 1];
89
+ const fieldWidth = integerByteWidth(fields.length - 1);
90
+ const tokenWidth = integerByteWidth(terms.length - 1);
91
+ const municipalityWidth = integerByteWidth(municipalities.length - 1);
92
+ const tokenCount = fields.reduce((sum, field) => sum + field.length, 0);
93
+ const rowMunicipalitiesOffset = HEADER_SIZE + rowCount * 4;
94
+ const rowFieldsOffset = rowMunicipalitiesOffset + rowCount * municipalityWidth;
95
+ const municipalitiesOffset = rowFieldsOffset + rowCount * 4 * fieldWidth;
96
+ const fieldOffsets = municipalitiesOffset + municipalities.length * 6;
97
+ const fieldTokens = fieldOffsets + (fields.length + 1) * 4;
98
+ const postingOffsets = fieldTokens + tokenCount * tokenWidth;
99
+ const lexiconOffset = postingOffsets + offsets.length * 4;
100
+ const bytes = new Uint8Array(lexiconOffset + lexicon.length + postingsLength + DIGEST_SIZE);
101
+ const data = new DataView(bytes.buffer);
102
+ bytes.set(new TextEncoder().encode(MAGIC));
103
+ for (const [offset, value,] of [
104
+ [8, VERSION],
105
+ [12, rowCount],
106
+ [16, lexicon.length],
107
+ [20, terms.length],
108
+ [24, postingsLength],
109
+ [28, fields.length],
110
+ [64, tokenCount],
111
+ [68, municipalities.length],
112
+ ]) {
113
+ data.setUint32(offset, value, true);
114
+ }
115
+ bytes.set(Buffer.from(checksum, 'hex'), 32);
116
+ bytes.set([fieldWidth, tokenWidth, municipalityWidth], 72);
117
+ for (let id = 0; id < rowCount; id++) {
118
+ data.setUint32(HEADER_SIZE + id * 4, ceps[id], true);
119
+ writePackedInteger(bytes, rowMunicipalitiesOffset + id * municipalityWidth, rowMunicipalities[id], municipalityWidth);
120
+ for (let column = 0; column < 4; column++) {
121
+ writePackedInteger(bytes, rowFieldsOffset + (id * 4 + column) * fieldWidth, rowFields[id * 4 + column], fieldWidth);
122
+ }
123
+ }
124
+ for (const [id, { code, uf },] of municipalities.entries()) {
125
+ data.setUint32(municipalitiesOffset + id * 6, code, true);
126
+ bytes.set([uf.charCodeAt(0), uf.charCodeAt(1)], municipalitiesOffset + id * 6 + 4);
127
+ }
128
+ let position = 0;
129
+ for (const [id, field,] of fields.entries()) {
130
+ data.setUint32(fieldOffsets + id * 4, position, true);
131
+ for (const term of field) {
132
+ writePackedInteger(bytes, fieldTokens + position++ * tokenWidth, remap[term], tokenWidth);
133
+ }
134
+ }
135
+ data.setUint32(fieldOffsets + fields.length * 4, position, true);
136
+ for (const [id, offset,] of offsets.entries()) {
137
+ data.setUint32(postingOffsets + id * 4, offset, true);
138
+ }
139
+ bytes.set(lexicon, lexiconOffset);
140
+ let offset = lexiconOffset + lexicon.length;
141
+ for (const chunk of chunks) {
142
+ bytes.set(chunk, offset);
143
+ offset += chunk.length;
144
+ }
145
+ bytes.set(digest(bytes.subarray(0, -DIGEST_SIZE)), offset);
146
+ parseAddressSearchIndex(bytes, checksum, rowCount);
147
+ return bytes;
148
+ }
149
+ /** Validates integrity, source identity, and every range before queries can use the index. */
150
+ export function parseAddressSearchIndex(bytes, checksum, rowCount) {
151
+ if (bytes.length < HEADER_SIZE + DIGEST_SIZE || new TextDecoder().decode(bytes.subarray(0, 8)) !== MAGIC) {
152
+ throw new Error('Invalid address search index header');
153
+ }
154
+ const data = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
155
+ if (data.getUint32(8, true) !== VERSION) {
156
+ throw new Error('Unsupported address search index version; rebuild the search index');
157
+ }
158
+ if (!digest(bytes.subarray(0, -DIGEST_SIZE)).equals(bytes.subarray(-DIGEST_SIZE))) {
159
+ throw new Error('Invalid address search index checksum');
160
+ }
161
+ if (Buffer.from(bytes.subarray(32, 64)).toString('hex') !== checksum || data.getUint32(12, true) !== rowCount) {
162
+ throw new Error('Address search index belongs to a different database');
163
+ }
164
+ const lexiconLength = data.getUint32(16, true);
165
+ const count = data.getUint32(20, true);
166
+ const postingsLength = data.getUint32(24, true);
167
+ const fieldCount = data.getUint32(28, true);
168
+ const tokenCount = data.getUint32(64, true);
169
+ const municipalityCount = data.getUint32(68, true);
170
+ const fieldWidth = readByteWidth(data.getUint8(72), 'search field');
171
+ const tokenWidth = readByteWidth(data.getUint8(73), 'search token');
172
+ const municipalityWidth = readByteWidth(data.getUint8(74), 'search municipality');
173
+ const rowMunicipalities = HEADER_SIZE + rowCount * 4;
174
+ const rowFields = rowMunicipalities + rowCount * municipalityWidth;
175
+ const municipalities = rowFields + rowCount * 4 * fieldWidth;
176
+ const fieldOffsets = municipalities + municipalityCount * 6;
177
+ const fieldTokens = fieldOffsets + (fieldCount + 1) * 4;
178
+ const offsets = fieldTokens + tokenCount * tokenWidth;
179
+ const lexiconOffset = offsets + (count + 1) * 4;
180
+ const postingsOffset = lexiconOffset + lexiconLength;
181
+ if (!fieldCount || !municipalityCount || data.getUint8(75) !== 0 || data.getUint32(76, true) !== 0
182
+ || postingsOffset + postingsLength + DIGEST_SIZE !== bytes.length) {
183
+ throw new Error('Invalid address search index size');
184
+ }
185
+ const terms = JSON.parse(new TextDecoder('utf-8', { fatal: true }).decode(bytes.subarray(lexiconOffset, postingsOffset)));
186
+ if (!Array.isArray(terms) || terms.length !== count
187
+ || terms.some((term, id) => typeof term !== 'string' || !/^[a-z0-9]+$/.test(term) || (id > 0 && terms[id - 1] >= term))) {
188
+ throw new Error('Invalid address search vocabulary');
189
+ }
190
+ const index = {
191
+ data,
192
+ terms,
193
+ offsets,
194
+ postings: bytes.subarray(postingsOffset, -DIGEST_SIZE),
195
+ rowCount,
196
+ rowMunicipalities,
197
+ rowFields,
198
+ fieldOffsets,
199
+ fieldCount,
200
+ fieldTokens,
201
+ fieldWidth,
202
+ tokenWidth,
203
+ municipalityWidth,
204
+ municipalityCodes: [],
205
+ municipalityUfs: [],
206
+ municipalityByCode: new Map(),
207
+ };
208
+ for (let id = 0; id < municipalityCount; id++) {
209
+ const code = data.getUint32(municipalities + id * 6, true);
210
+ const uf = String.fromCharCode(data.getUint8(municipalities + id * 6 + 4), data.getUint8(municipalities + id * 6 + 5));
211
+ if (code < 1_000_000 || code > 9_999_999 || !ALL_UFS.includes(uf) || index.municipalityByCode.has(code)) {
212
+ throw new Error('Invalid address search municipality');
213
+ }
214
+ index.municipalityCodes.push(code);
215
+ index.municipalityUfs.push(uf);
216
+ index.municipalityByCode.set(code, id);
217
+ }
218
+ let previous = -1;
219
+ for (let id = 0; id < rowCount; id++) {
220
+ const cep = data.getUint32(HEADER_SIZE + id * 4, true);
221
+ if (cep <= previous || cep > 99_999_999) {
222
+ throw new Error('Invalid address search CEP');
223
+ }
224
+ previous = cep;
225
+ if (searchRowMunicipality(index, id) >= municipalityCount) {
226
+ throw new Error('Invalid address search municipality reference');
227
+ }
228
+ for (let column = 0; column < 4; column++) {
229
+ if (readPackedInteger(data, rowFields + (id * 4 + column) * fieldWidth, fieldWidth) >= fieldCount) {
230
+ throw new Error('Invalid address search field reference');
231
+ }
232
+ }
233
+ }
234
+ if (data.getUint32(fieldOffsets, true) !== 0 || data.getUint32(fieldOffsets + fieldCount * 4, true) !== tokenCount) {
235
+ throw new Error('Invalid address search field offsets');
236
+ }
237
+ for (let id = 0; id < fieldCount; id++) {
238
+ const start = data.getUint32(fieldOffsets + id * 4, true);
239
+ const end = data.getUint32(fieldOffsets + (id + 1) * 4, true);
240
+ if (end < start || end > tokenCount) {
241
+ throw new Error('Invalid address search field range');
242
+ }
243
+ }
244
+ for (let id = 0; id < tokenCount; id++) {
245
+ if (readPackedInteger(data, fieldTokens + id * tokenWidth, tokenWidth) >= count) {
246
+ throw new Error('Invalid address search field token');
247
+ }
248
+ }
249
+ if (data.getUint32(offsets, true) !== 0 || data.getUint32(offsets + count * 4, true) !== postingsLength) {
250
+ throw new Error('Invalid address search posting offsets');
251
+ }
252
+ for (let id = 0; id < count; id++) {
253
+ visitSearchPosting(index, id);
254
+ }
255
+ return index;
256
+ }
257
+ /** Visits an ordered posting without materializing it, checking delta and range invariants. */
258
+ export function visitSearchPosting(index, id, visit) {
259
+ let offset = index.data.getUint32(index.offsets + id * 4, true);
260
+ const end = index.data.getUint32(index.offsets + (id + 1) * 4, true);
261
+ if (end <= offset || end > index.postings.length) {
262
+ throw new Error('Invalid address search posting range');
263
+ }
264
+ let previous = -1;
265
+ while (offset < end) {
266
+ let delta = 0;
267
+ let factor = 1;
268
+ let byte;
269
+ do {
270
+ if (offset >= end || factor > 2 ** 28) {
271
+ throw new Error('Invalid address search posting encoding');
272
+ }
273
+ byte = index.postings[offset++];
274
+ delta += (byte & 127) * factor;
275
+ factor *= 128;
276
+ } while (byte & 128);
277
+ const row = previous + delta;
278
+ if (delta === 0 || row >= index.rowCount) {
279
+ throw new Error('Invalid address search posting row');
280
+ }
281
+ visit?.(row);
282
+ previous = row;
283
+ }
284
+ }
285
+ /** Resolves an index row ID to the eight-digit CEP used by the database reader. */
286
+ export function searchRowCep(index, id) {
287
+ return String(index.data.getUint32(HEADER_SIZE + id * 4, true)).padStart(8, '0');
288
+ }
289
+ /** Reads geographic membership directly, without enumerating any geographic posting list. */
290
+ export function searchRowMunicipality(index, id) {
291
+ return readPackedInteger(index.data, index.rowMunicipalities + id * index.municipalityWidth, index.municipalityWidth);
292
+ }
293
+ /** Finds a CEP anchor using the search index, without decoding an address. */
294
+ export function findSearchCep(index, cep) {
295
+ const wanted = Number(cep.replace('-', ''));
296
+ let low = 0;
297
+ let high = index.rowCount;
298
+ while (low < high) {
299
+ const middle = low + ((high - low) >>> 1);
300
+ if (index.data.getUint32(HEADER_SIZE + middle * 4, true) < wanted) {
301
+ low = middle + 1;
302
+ }
303
+ else {
304
+ high = middle;
305
+ }
306
+ }
307
+ return low < index.rowCount && index.data.getUint32(HEADER_SIZE + low * 4, true) === wanted ? low : -1;
308
+ }
309
+ function encodePosting(rows) {
310
+ const bytes = [];
311
+ let previous = -1;
312
+ for (const row of rows) {
313
+ let delta = row - previous;
314
+ while (delta >= 128) {
315
+ bytes.push((delta % 128) | 128);
316
+ delta = Math.floor(delta / 128);
317
+ }
318
+ bytes.push(delta);
319
+ previous = row;
320
+ }
321
+ return new Uint8Array(bytes);
322
+ }
323
+ function digest(bytes) {
324
+ return createHash('sha256').update(bytes).digest();
325
+ }
326
+ //# sourceMappingURL=address-search-index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"address-search-index.js","sourceRoot":"","sources":["../src/address-search-index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,aAAa,EAAE,MAAM,0BAA0B,CAAC;AACzD,OAAO,EACL,gBAAgB,EAChB,aAAa,EACb,iBAAiB,EACjB,kBAAkB,GAEnB,MAAM,uBAAuB,CAAC;AAE/B,OAAO,EACL,OAAO,GAER,MAAM,YAAY,CAAC;AAEpB,MAAM,KAAK,GAAG,UAAU,CAAC;AACzB,MAAM,OAAO,GAAG,CAAC,CAAC;AAClB,MAAM,WAAW,GAAG,EAAE,CAAC;AACvB,MAAM,WAAW,GAAG,EAAE,CAAC;AAsBvB,gGAAgG;AAChG,MAAM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Q,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,MAAM,EAAE,CAAC;AACrD,CAAC"}
@@ -0,0 +1,15 @@
1
+ /** All workers are busy and the bounded waiting queue is full. */
2
+ export declare class DneAddressSearchQueueFullError extends Error {
3
+ readonly code = "SEARCH_QUEUE_FULL";
4
+ constructor();
5
+ }
6
+ /** The pool no longer accepts new searches. */
7
+ export declare class DneAddressSearchPoolClosedError extends Error {
8
+ readonly code = "SEARCH_POOL_CLOSED";
9
+ constructor();
10
+ }
11
+ /** A worker could not start or stopped unexpectedly. The entire pool is closed. */
12
+ export declare class DneAddressSearchWorkerError extends Error {
13
+ readonly code = "SEARCH_WORKER_FAILED";
14
+ constructor(cause: unknown);
15
+ }
@@ -0,0 +1,25 @@
1
+ /** All workers are busy and the bounded waiting queue is full. */
2
+ export class DneAddressSearchQueueFullError extends Error {
3
+ code = 'SEARCH_QUEUE_FULL';
4
+ constructor() {
5
+ super('Address search queue is full');
6
+ this.name = 'DneAddressSearchQueueFullError';
7
+ }
8
+ }
9
+ /** The pool no longer accepts new searches. */
10
+ export class DneAddressSearchPoolClosedError extends Error {
11
+ code = 'SEARCH_POOL_CLOSED';
12
+ constructor() {
13
+ super('Address search pool is closed');
14
+ this.name = 'DneAddressSearchPoolClosedError';
15
+ }
16
+ }
17
+ /** A worker could not start or stopped unexpectedly. The entire pool is closed. */
18
+ export class DneAddressSearchWorkerError extends Error {
19
+ code = 'SEARCH_WORKER_FAILED';
20
+ constructor(cause) {
21
+ super('Address search worker failed', { cause });
22
+ this.name = 'DneAddressSearchWorkerError';
23
+ }
24
+ }
25
+ //# sourceMappingURL=address-search-pool-errors.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"address-search-pool-errors.js","sourceRoot":"","sources":["../src/address-search-pool-errors.ts"],"names":[],"mappings":"AAAA,kEAAkE;AAClE,MAAM,OAAO,8BAA+B,SAAQ,KAAK;IAC9C,IAAI,GAAG,mBAAmB,CAAC;IAEpC;QACE,KAAK,CAAC,8BAA8B,CAAC,CAAC;QACtC,IAAI,CAAC,IAAI,GAAG,gCAAgC,CAAC;IAC/C,CAAC;CACF;AAED,+CAA+C;AAC/C,MAAM,OAAO,+BAAgC,SAAQ,KAAK;IAC/C,IAAI,GAAG,oBAAoB,CAAC;IAErC;QACE,KAAK,CAAC,+BAA+B,CAAC,CAAC;QACvC,IAAI,CAAC,IAAI,GAAG,iCAAiC,CAAC;IAChD,CAAC;CACF;AAED,mFAAmF;AACnF,MAAM,OAAO,2BAA4B,SAAQ,KAAK;IAC3C,IAAI,GAAG,sBAAsB,CAAC;IAEvC,YAAY,KAAc;QACxB,KAAK,CAAC,8BAA8B,EAAE,EAAE,KAAK,EAAE,CAAC,CAAC;QACjD,IAAI,CAAC,IAAI,GAAG,6BAA6B,CAAC;IAC5C,CAAC;CACF"}
@@ -0,0 +1,40 @@
1
+ import type { AddressSearchOptions } from './address-search.ts';
2
+ import type { DneRow } from './types.ts';
3
+ /** Native Bun workers share immutable database/index bytes and own their search workspaces. */
4
+ export type AddressSearchPoolOptions = {
5
+ /** Path to the binary database. Relative paths use the caller's current directory at open(). */
6
+ databasePath: string;
7
+ /** Path to the prebuilt index matching the database. */
8
+ indexPath: string;
9
+ /** Positive integer. Defaults to 1; additional workers consume additional memory. */
10
+ workers?: number;
11
+ /** Maximum waiting searches, excluding running searches. Defaults to 64; 0 disables waiting. */
12
+ maxQueue?: number;
13
+ };
14
+ export type AddressSearchAsyncOptions = AddressSearchOptions & {
15
+ /** Rejects with signal.reason. Running calculations finish, but their results are discarded. */
16
+ signal?: AbortSignal;
17
+ };
18
+ /** Persistent workers keep synchronous address searches off the HTTP thread. Close the pool when the application stops. */
19
+ export declare class DneAddressSearchPool {
20
+ private readonly maxQueue;
21
+ private readonly slots;
22
+ private readonly queue;
23
+ private readonly drained;
24
+ private accepting;
25
+ private terminating;
26
+ private closing?;
27
+ private failure?;
28
+ private constructor();
29
+ /** Resolves only after every worker has loaded and validated its database and index. Cleans up all workers on failure. */
30
+ static open(options: AddressSearchPoolOptions): Promise<DneAddressSearchPool>;
31
+ /** Matches the synchronous API's results and ranking. All failures are promise rejections. */
32
+ searchAddresses(text: string, options?: AddressSearchAsyncOptions): Promise<DneRow[]>;
33
+ /** Stops admission, drains accepted searches (including cancelled calculations), then waits for every worker to exit. Idempotent. */
34
+ close(): Promise<void>;
35
+ private spawn;
36
+ private dispatch;
37
+ private receive;
38
+ private finishDrain;
39
+ private fail;
40
+ }
@@ -0,0 +1,215 @@
1
+ import { resolve } from 'node:path';
2
+ import { DneAddressSearchPoolClosedError, DneAddressSearchQueueFullError, DneAddressSearchWorkerError, } from "./address-search-pool-errors.js";
3
+ import workerSource from "./address-search-worker-source.js";
4
+ /** Persistent workers keep synchronous address searches off the HTTP thread. Close the pool when the application stops. */
5
+ export class DneAddressSearchPool {
6
+ maxQueue;
7
+ slots = [];
8
+ queue = [];
9
+ drained = Promise.withResolvers();
10
+ accepting = true;
11
+ terminating = false;
12
+ closing;
13
+ failure;
14
+ constructor(maxQueue) {
15
+ this.maxQueue = maxQueue;
16
+ }
17
+ /** Resolves only after every worker has loaded and validated its database and index. Cleans up all workers on failure. */
18
+ static async open(options) {
19
+ const { workers = 1, maxQueue = 64 } = options;
20
+ if (!Number.isSafeInteger(workers) || workers < 1 || !Number.isSafeInteger(maxQueue) || maxQueue < 0) {
21
+ throw new RangeError('Address search pool requires a positive worker count and a nonnegative queue size');
22
+ }
23
+ if (typeof options.indexPath !== 'string' || !options.indexPath.trim()
24
+ || typeof options.databasePath !== 'string' || !options.databasePath.trim()) {
25
+ throw new TypeError('Address search pool requires nonempty file paths');
26
+ }
27
+ const pool = new DneAddressSearchPool(maxQueue);
28
+ try {
29
+ const data = {
30
+ type: 'load',
31
+ databasePath: resolve(options.databasePath),
32
+ indexPath: resolve(options.indexPath),
33
+ };
34
+ const snapshot = await pool.spawn(data);
35
+ await Promise.all(Array.from({ length: workers - 1 }, () => pool.spawn({ type: 'init', snapshot: snapshot })));
36
+ return pool;
37
+ }
38
+ catch (error) {
39
+ pool.fail(error);
40
+ await pool.close();
41
+ throw pool.failure;
42
+ }
43
+ }
44
+ /** Matches the synchronous API's results and ranking. All failures are promise rejections. */
45
+ async searchAddresses(text, options = {}) {
46
+ if (!this.accepting) {
47
+ throw this.failure ?? new DneAddressSearchPoolClosedError();
48
+ }
49
+ const { signal, uf, municipioCodIbge, limit } = options;
50
+ if (signal?.aborted) {
51
+ throw signal.reason;
52
+ }
53
+ const idle = this.slots.find((slot) => !slot.current);
54
+ if (!idle && this.queue.length >= this.maxQueue) {
55
+ throw new DneAddressSearchQueueFullError();
56
+ }
57
+ return new Promise((resolve, reject) => {
58
+ const abort = () => {
59
+ const position = this.queue.indexOf(job);
60
+ if (position !== -1) {
61
+ this.queue.splice(position, 1);
62
+ }
63
+ job.cleanup();
64
+ reject(signal?.reason);
65
+ // An aborted running job still occupies its worker until the reply arrives.
66
+ this.finishDrain();
67
+ };
68
+ const job = {
69
+ request: { type: 'search', text, options: { uf, municipioCodIbge, limit } },
70
+ resolve,
71
+ reject,
72
+ cleanup: () => signal?.removeEventListener('abort', abort),
73
+ };
74
+ signal?.addEventListener('abort', abort, { once: true });
75
+ if (idle) {
76
+ this.dispatch(idle, job);
77
+ }
78
+ else {
79
+ this.queue.push(job);
80
+ }
81
+ });
82
+ }
83
+ /** Stops admission, drains accepted searches (including cancelled calculations), then waits for every worker to exit. Idempotent. */
84
+ close() {
85
+ this.accepting = false;
86
+ this.closing ??= this.drained.promise.then(async () => {
87
+ this.terminating = true;
88
+ await Promise.all(this.slots.map((slot) => {
89
+ if (!slot.hasClosed) {
90
+ if (this.failure) {
91
+ slot.worker.terminate();
92
+ }
93
+ else {
94
+ try {
95
+ slot.worker.postMessage({ type: 'close' });
96
+ }
97
+ catch {
98
+ slot.worker.terminate();
99
+ }
100
+ }
101
+ }
102
+ return slot.closed.promise;
103
+ }));
104
+ // Readiness promises retain the published snapshot until these slots are released.
105
+ this.slots.length = 0;
106
+ });
107
+ this.finishDrain();
108
+ return this.closing;
109
+ }
110
+ async spawn(data) {
111
+ const url = URL.createObjectURL(new Blob([workerSource], { type: 'application/javascript' }));
112
+ let worker;
113
+ try {
114
+ worker = new Worker(url);
115
+ }
116
+ catch (error) {
117
+ URL.revokeObjectURL(url);
118
+ throw error;
119
+ }
120
+ const slot = {
121
+ worker,
122
+ ready: Promise.withResolvers(),
123
+ closed: Promise.withResolvers(),
124
+ hasClosed: false,
125
+ };
126
+ this.slots.push(slot);
127
+ worker.addEventListener('message', (event) => this.receive(slot, event.data));
128
+ worker.addEventListener('error', (event) => {
129
+ event.preventDefault();
130
+ this.fail(event.error ?? new Error(event.message));
131
+ });
132
+ worker.addEventListener('close', (event) => {
133
+ URL.revokeObjectURL(url);
134
+ slot.hasClosed = true;
135
+ slot.closed.resolve();
136
+ if (!this.terminating) {
137
+ this.fail(new Error(`Address search worker exited unexpectedly with code ${event.code}`));
138
+ }
139
+ });
140
+ try {
141
+ worker.postMessage(data);
142
+ }
143
+ catch (error) {
144
+ this.fail(error);
145
+ }
146
+ return slot.ready.promise;
147
+ }
148
+ dispatch(slot, job) {
149
+ slot.current = job;
150
+ try {
151
+ slot.worker.postMessage(job.request);
152
+ }
153
+ catch (error) {
154
+ this.fail(error);
155
+ }
156
+ }
157
+ receive(slot, reply) {
158
+ if (this.failure) {
159
+ return;
160
+ }
161
+ if (reply.type === 'ready') {
162
+ slot.ready.resolve(reply.snapshot);
163
+ return;
164
+ }
165
+ if (reply.type === 'failed') {
166
+ const error = new Error(reply.error.message);
167
+ error.name = reply.error.name;
168
+ this.fail(error);
169
+ return;
170
+ }
171
+ const job = slot.current;
172
+ slot.current = undefined;
173
+ job.cleanup();
174
+ if (reply.type === 'result') {
175
+ job.resolve(reply.rows);
176
+ }
177
+ else {
178
+ const { name, message } = reply.error;
179
+ const error = name === 'RangeError' ? new RangeError(message) : name === 'TypeError' ? new TypeError(message) : new Error(message);
180
+ error.name = name;
181
+ job.reject(error);
182
+ }
183
+ const next = this.queue.shift();
184
+ if (next) {
185
+ this.dispatch(slot, next);
186
+ }
187
+ this.finishDrain();
188
+ }
189
+ finishDrain() {
190
+ if (!this.accepting && this.queue.length === 0 && this.slots.every((slot) => !slot.current)) {
191
+ this.drained.resolve();
192
+ }
193
+ }
194
+ fail(cause) {
195
+ if (this.failure || this.terminating) {
196
+ return;
197
+ }
198
+ const error = new DneAddressSearchWorkerError(cause);
199
+ this.failure = error;
200
+ for (const slot of this.slots) {
201
+ slot.ready.reject(error);
202
+ if (slot.current) {
203
+ slot.current.cleanup();
204
+ slot.current.reject(error);
205
+ slot.current = undefined;
206
+ }
207
+ }
208
+ for (const job of this.queue.splice(0)) {
209
+ job.cleanup();
210
+ job.reject(error);
211
+ }
212
+ void this.close();
213
+ }
214
+ }
215
+ //# sourceMappingURL=address-search-pool.js.map