@konstit/csv 0.4.1 → 0.4.3
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.
- package/README.md +136 -463
- package/dist/api.d.ts +356 -21
- package/dist/api.js +64 -93
- package/dist/batches.d.ts +291 -0
- package/dist/batches.js +72 -43
- package/dist/file-stream.d.ts +2 -2
- package/dist/file-stream.js +5 -2
- package/dist/files.d.ts +148 -2
- package/dist/files.js +30 -24
- package/dist/filter.d.ts +82 -0
- package/dist/latin1-rows.d.ts +3 -0
- package/dist/latin1-rows.js +84 -0
- package/dist/materialize-field.d.ts +1 -0
- package/dist/materialize-field.js +26 -0
- package/dist/native.d.ts +29 -0
- package/dist/native.js +44 -3
- package/dist/normalize.d.ts +18 -1
- package/dist/normalize.js +10 -0
- package/dist/options-snapshot.d.ts +3 -0
- package/dist/options-snapshot.js +50 -0
- package/dist/options.d.ts +44 -3
- package/dist/parser.d.ts +307 -1
- package/dist/parser.js +121 -8
- package/dist/strict-schema.d.ts +1 -1
- package/dist/strict-schema.js +4 -1
- package/dist/types.d.ts +435 -67
- package/dist/worker-count.d.ts +33 -1
- package/dist/worker-count.js +6 -1
- package/dist/worker-pool.d.ts +77 -1
- package/dist/worker-pool.js +11 -3
- package/dist/worker-rows.d.ts +31 -3
- package/dist/worker-rows.js +11 -2
- package/dist/workers/count.worker.js +13 -26
- package/dist/workers/rows.worker.js +23 -52
- package/package.json +23 -10
- package/prebuilds/darwin-arm64/libcsv_native.dylib +0 -0
- package/prebuilds/darwin-x64/libcsv_native.dylib +0 -0
- package/prebuilds/linux-x64/libcsv_native.so +0 -0
- package/prebuilds/manifest.json +8 -8
- package/prebuilds/provenance/darwin-arm64.json +1 -1
- package/prebuilds/provenance/darwin-x64.json +1 -1
- package/prebuilds/provenance/linux-x64.json +1 -1
package/dist/api.js
CHANGED
|
@@ -16,8 +16,14 @@ import {
|
|
|
16
16
|
not,
|
|
17
17
|
re
|
|
18
18
|
} from "./filter.js";
|
|
19
|
-
import {
|
|
20
|
-
import {
|
|
19
|
+
import { latin1Rows } from "./latin1-rows.js";
|
|
20
|
+
import { optionalLatin1RowsNative } from "./native.js";
|
|
21
|
+
import {
|
|
22
|
+
encodingCode,
|
|
23
|
+
normalizeColumns
|
|
24
|
+
} from "./normalize.js";
|
|
25
|
+
import { snapshotCsvOptions } from "./options-snapshot.js";
|
|
26
|
+
import { createPreparedParser } from "./parser.js";
|
|
21
27
|
import {
|
|
22
28
|
rejectStrictSchemaUnsupported,
|
|
23
29
|
strictSchemaValidator
|
|
@@ -28,29 +34,31 @@ import {
|
|
|
28
34
|
CsvWorkerPool
|
|
29
35
|
} from "./worker-pool.js";
|
|
30
36
|
import { parallelRows } from "./worker-rows.js";
|
|
31
|
-
export async function parse(buffer,
|
|
37
|
+
export async function parse(buffer, suppliedOptions = {}) {
|
|
38
|
+
const options = snapshotCsvOptions(suppliedOptions);
|
|
32
39
|
if ("compression" in options && options.compression !== void 0)
|
|
33
40
|
throw Error("parse() does not support compression; pass decompressed bytes or use a file API");
|
|
34
41
|
if (options.delimiter === "auto")
|
|
35
42
|
throw Error("parse() does not support automatic delimiter detection; specify delimiter or use a file API");
|
|
36
43
|
rejectFilteredStrictSchema(options);
|
|
37
44
|
const filters = toNativeFilters(options.where);
|
|
38
|
-
const
|
|
45
|
+
const columns = selectedColumns(options);
|
|
46
|
+
const projected = filters !== void 0 || options.strict !== !0 && columns !== void 0;
|
|
47
|
+
using parser = createPreparedParser(toParserOptions(options), projected ? columns : void 0, filters);
|
|
39
48
|
const validator = strictSchemaValidator(options);
|
|
49
|
+
const batch = parser.writeBatch(buffer, !0);
|
|
40
50
|
try {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
validator?.finish();
|
|
45
|
-
return materializeRows(batch, options);
|
|
46
|
-
} finally {
|
|
47
|
-
batch.close();
|
|
48
|
-
}
|
|
51
|
+
validator?.validateBatch(batch);
|
|
52
|
+
validator?.finish();
|
|
53
|
+
return materializeRows(batch, columns, projected);
|
|
49
54
|
} finally {
|
|
50
|
-
|
|
55
|
+
batch.close();
|
|
51
56
|
}
|
|
52
57
|
}
|
|
53
|
-
export
|
|
58
|
+
export function rows(path, options = {}) {
|
|
59
|
+
return iterateRows(path, snapshotCsvOptions(options));
|
|
60
|
+
}
|
|
61
|
+
async function* iterateRows(path, options) {
|
|
54
62
|
if ((options.workerCount ?? 1) > 1) {
|
|
55
63
|
rejectCompressedSharding(options, "parallel row parsing");
|
|
56
64
|
rejectAutoDelimiterSharding(options, "parallel row parsing");
|
|
@@ -59,15 +67,22 @@ export async function* rows(path, options = {}) {
|
|
|
59
67
|
}
|
|
60
68
|
rejectFilteredStrictSchema(options);
|
|
61
69
|
const filters = toNativeFilters(options.where);
|
|
70
|
+
const columns = selectedColumns(options);
|
|
71
|
+
const projected = filters !== void 0 || options.strict !== !0 && columns !== void 0;
|
|
62
72
|
await using input = await prepareCsvFileInput(path, options);
|
|
63
|
-
|
|
73
|
+
const parserOptions = toParserOptions(options, input.delimiter);
|
|
64
74
|
const validator = strictSchemaValidator(options);
|
|
75
|
+
if (options.strict !== !0 && options.fixedColumns === void 0 && validator === void 0 && encodingCode(options.encoding) === 1 && optionalLatin1RowsNative() !== null) {
|
|
76
|
+
yield* latin1Rows(input.chunks(), input.delimiter, columns, filters);
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
79
|
+
using parser = createPreparedParser(parserOptions, projected ? columns : void 0, filters);
|
|
65
80
|
for await (const chunk of input.chunks()) {
|
|
66
|
-
const batch =
|
|
81
|
+
const batch = parser.writeBatch(chunk);
|
|
67
82
|
if (batch.rowCount > 0)
|
|
68
83
|
try {
|
|
69
84
|
validator?.validateBatch(batch);
|
|
70
|
-
const values = materializeRows(batch,
|
|
85
|
+
const values = materializeRows(batch, columns, projected);
|
|
71
86
|
if (values.length > 0)
|
|
72
87
|
yield values;
|
|
73
88
|
} finally {
|
|
@@ -76,12 +91,12 @@ export async function* rows(path, options = {}) {
|
|
|
76
91
|
else
|
|
77
92
|
batch.close();
|
|
78
93
|
}
|
|
79
|
-
const batch =
|
|
94
|
+
const batch = parser.endBatch();
|
|
80
95
|
if (batch.rowCount > 0)
|
|
81
96
|
try {
|
|
82
97
|
validator?.validateBatch(batch);
|
|
83
98
|
validator?.finish();
|
|
84
|
-
const values = materializeRows(batch,
|
|
99
|
+
const values = materializeRows(batch, columns, projected);
|
|
85
100
|
if (values.length > 0)
|
|
86
101
|
yield values;
|
|
87
102
|
} finally {
|
|
@@ -95,14 +110,18 @@ export async function* rows(path, options = {}) {
|
|
|
95
110
|
|
|
96
111
|
export { parallelRows };
|
|
97
112
|
export { CsvWorkerPool };
|
|
98
|
-
export
|
|
113
|
+
export function batches(path, options = {}) {
|
|
114
|
+
return iterateBatches(path, snapshotCsvOptions(options));
|
|
115
|
+
}
|
|
116
|
+
async function* iterateBatches(path, options) {
|
|
99
117
|
rejectFilteredStrictSchema(options);
|
|
100
118
|
const filters = toNativeFilters(options.where);
|
|
119
|
+
const columns = filters === void 0 ? void 0 : selectedColumns(options);
|
|
101
120
|
await using input = await prepareCsvFileInput(path, options);
|
|
102
|
-
using parser =
|
|
121
|
+
using parser = createPreparedParser(toParserOptions(options, input.delimiter), columns, filters);
|
|
103
122
|
const validator = strictSchemaValidator(options);
|
|
104
123
|
for await (const chunk of input.chunks()) {
|
|
105
|
-
const batch =
|
|
124
|
+
const batch = parser.writeBatch(chunk);
|
|
106
125
|
if (batch.rowCount > 0) {
|
|
107
126
|
try {
|
|
108
127
|
validator?.validateBatch(batch);
|
|
@@ -114,7 +133,7 @@ export async function* batches(path, options = {}) {
|
|
|
114
133
|
} else
|
|
115
134
|
batch.close();
|
|
116
135
|
}
|
|
117
|
-
const batch =
|
|
136
|
+
const batch = parser.endBatch();
|
|
118
137
|
if (batch.rowCount > 0) {
|
|
119
138
|
try {
|
|
120
139
|
validator?.validateBatch(batch);
|
|
@@ -137,16 +156,18 @@ export async function withBatches(path, options, callback) {
|
|
|
137
156
|
batch.close();
|
|
138
157
|
}
|
|
139
158
|
}
|
|
140
|
-
export async function forEachColumnarBatches(path,
|
|
159
|
+
export async function forEachColumnarBatches(path, suppliedOptions = {}, callback) {
|
|
160
|
+
const options = snapshotCsvOptions(suppliedOptions);
|
|
141
161
|
ensureColumnarBatchesSupported(options);
|
|
142
162
|
const filters = toNativeFilters(options.where);
|
|
143
|
-
const
|
|
163
|
+
const columns = selectedColumns(options);
|
|
164
|
+
const scopedBatch = new ScopedCsvColumnarBatchView(columns);
|
|
144
165
|
let batchIndex = 0;
|
|
145
166
|
await using input = await prepareCsvFileInput(path, options);
|
|
146
|
-
using parser =
|
|
167
|
+
using parser = createPreparedParser(toParserOptions(options, input.delimiter), columns, filters);
|
|
147
168
|
const validator = strictSchemaValidator(options);
|
|
148
169
|
for await (const chunk of input.chunks()) {
|
|
149
|
-
const batch =
|
|
170
|
+
const batch = parser.writeBatch(chunk);
|
|
150
171
|
if (batch.rowCount > 0)
|
|
151
172
|
try {
|
|
152
173
|
validator?.validateBatch(batch);
|
|
@@ -165,7 +186,7 @@ export async function forEachColumnarBatches(path, options = {}, callback) {
|
|
|
165
186
|
else
|
|
166
187
|
batch.close();
|
|
167
188
|
}
|
|
168
|
-
const batch =
|
|
189
|
+
const batch = parser.endBatch();
|
|
169
190
|
if (batch.rowCount > 0)
|
|
170
191
|
try {
|
|
171
192
|
validator?.validateBatch(batch);
|
|
@@ -189,7 +210,8 @@ export async function forEachColumnarBatches(path, options = {}, callback) {
|
|
|
189
210
|
export function withColumnarBatches(path, options, callback) {
|
|
190
211
|
return forEachColumnarBatches(path, options, callback);
|
|
191
212
|
}
|
|
192
|
-
export async function forEachRowViews(path,
|
|
213
|
+
export async function forEachRowViews(path, suppliedOptions = {}, callback) {
|
|
214
|
+
const options = snapshotCsvOptions(suppliedOptions);
|
|
193
215
|
ensureRowViewsSupported(options);
|
|
194
216
|
const scopedRowView = new ScopedCsvRowView(selectedColumns(options));
|
|
195
217
|
for await (const batch of batches(path, options))
|
|
@@ -215,7 +237,8 @@ export async function forEachRowViews(path, options = {}, callback) {
|
|
|
215
237
|
export function withRowViews(path, options, callback) {
|
|
216
238
|
return forEachRowViews(path, options, callback);
|
|
217
239
|
}
|
|
218
|
-
export async function count(path,
|
|
240
|
+
export async function count(path, suppliedOptions = {}) {
|
|
241
|
+
const options = snapshotCsvOptions(suppliedOptions);
|
|
219
242
|
if ((options.workerCount ?? 1) > 1) {
|
|
220
243
|
rejectCompressedSharding(options, "parallel counting");
|
|
221
244
|
rejectAutoDelimiterSharding(options, "parallel counting");
|
|
@@ -223,7 +246,9 @@ export async function count(path, options = {}) {
|
|
|
223
246
|
}
|
|
224
247
|
const filters = toNativeFilters(options.where);
|
|
225
248
|
await using input = await prepareCsvFileInput(path, options);
|
|
226
|
-
|
|
249
|
+
if (options.strict === !0 && filters !== void 0)
|
|
250
|
+
throw Error("strict CSV validation is not supported for count filters");
|
|
251
|
+
using parser = createPreparedParser(toParserOptions(options, input.delimiter), void 0, filters);
|
|
227
252
|
const validator = strictSchemaValidator(options);
|
|
228
253
|
let total = 0;
|
|
229
254
|
if (options.strict === !0 && filters === void 0) {
|
|
@@ -247,18 +272,20 @@ export async function count(path, options = {}) {
|
|
|
247
272
|
return total;
|
|
248
273
|
}
|
|
249
274
|
for await (const chunk of input.chunks())
|
|
250
|
-
total += writeCount(
|
|
251
|
-
total +=
|
|
275
|
+
total += parser.writeCount(chunk);
|
|
276
|
+
total += parser.endCount();
|
|
252
277
|
return total;
|
|
253
278
|
}
|
|
254
279
|
|
|
255
280
|
export { parallelCount };
|
|
256
|
-
export function findCsvSafeSplitOffsets(path, shardCount,
|
|
281
|
+
export function findCsvSafeSplitOffsets(path, shardCount, suppliedOptions = {}) {
|
|
282
|
+
const options = snapshotCsvOptions(suppliedOptions);
|
|
257
283
|
rejectCompressedSharding(options, "CSV split offset scanning");
|
|
258
284
|
rejectAutoDelimiterSharding(options, "CSV split offset scanning");
|
|
259
285
|
return findCsvSafeSplitOffsetsNative(path, shardCount, options.delimiter ?? ",");
|
|
260
286
|
}
|
|
261
|
-
export function findCsvSafeShards(path, shardCount,
|
|
287
|
+
export function findCsvSafeShards(path, shardCount, suppliedOptions = {}) {
|
|
288
|
+
const options = snapshotCsvOptions(suppliedOptions);
|
|
262
289
|
rejectCompressedSharding(options, "CSV shard scanning");
|
|
263
290
|
rejectAutoDelimiterSharding(options, "CSV shard scanning");
|
|
264
291
|
return findCsvSafeShardsNative(path, shardCount, options.delimiter ?? ",");
|
|
@@ -306,57 +333,11 @@ function selectedColumns(options) {
|
|
|
306
333
|
normalizeColumns(columns);
|
|
307
334
|
return columns;
|
|
308
335
|
}
|
|
309
|
-
function materializeRows(batch,
|
|
310
|
-
|
|
311
|
-
if (usesProjectedMaterialization(options))
|
|
336
|
+
function materializeRows(batch, columns, projected) {
|
|
337
|
+
if (projected)
|
|
312
338
|
return batch.rowsInto([]);
|
|
313
339
|
return batch.rowsInto([], columns);
|
|
314
340
|
}
|
|
315
|
-
function usesProjectedMaterialization(options) {
|
|
316
|
-
return options.where !== void 0 || options.strict !== !0 && selectedColumns(options) !== void 0;
|
|
317
|
-
}
|
|
318
|
-
function writeRowsBatch(parser, chunk, options, filters, final = !1) {
|
|
319
|
-
if (filters !== void 0)
|
|
320
|
-
return parser.writeProjectedBatchWhere(chunk, selectedColumns(options), filters, final);
|
|
321
|
-
return parser.writeBatch(chunk, final);
|
|
322
|
-
}
|
|
323
|
-
function writeMaterializeBatch(parser, chunk, options, filters, final = !1) {
|
|
324
|
-
const columns = selectedColumns(options);
|
|
325
|
-
if (filters !== void 0)
|
|
326
|
-
return parser.writeProjectedBatchWhere(chunk, columns, filters, final);
|
|
327
|
-
if (options.strict !== !0 && columns !== void 0)
|
|
328
|
-
return parser.writeProjectedBatch(chunk, { selectedColumns: columns }, final);
|
|
329
|
-
return parser.writeBatch(chunk, final);
|
|
330
|
-
}
|
|
331
|
-
function writeColumnarBatch(parser, chunk, options, filters, final = !1) {
|
|
332
|
-
const columns = selectedColumns(options);
|
|
333
|
-
if (filters !== void 0)
|
|
334
|
-
return parser.writeProjectedBatchWhere(chunk, columns, filters, final);
|
|
335
|
-
if (columns !== void 0)
|
|
336
|
-
return parser.writeProjectedBatch(chunk, { selectedColumns: columns }, final);
|
|
337
|
-
return parser.writeBatch(chunk, final);
|
|
338
|
-
}
|
|
339
|
-
function finishRowsBatch(parser, options, filters) {
|
|
340
|
-
if (filters !== void 0)
|
|
341
|
-
return parser.endProjectedBatchWhere(selectedColumns(options), filters);
|
|
342
|
-
return parser.endBatch();
|
|
343
|
-
}
|
|
344
|
-
function finishMaterializeBatch(parser, options, filters) {
|
|
345
|
-
const columns = selectedColumns(options);
|
|
346
|
-
if (filters !== void 0)
|
|
347
|
-
return parser.endProjectedBatchWhere(columns, filters);
|
|
348
|
-
if (options.strict !== !0 && columns !== void 0)
|
|
349
|
-
return parser.endProjectedBatch({ selectedColumns: columns });
|
|
350
|
-
return parser.endBatch();
|
|
351
|
-
}
|
|
352
|
-
function finishColumnarBatch(parser, options, filters) {
|
|
353
|
-
const columns = selectedColumns(options);
|
|
354
|
-
if (filters !== void 0)
|
|
355
|
-
return parser.endProjectedBatchWhere(columns, filters);
|
|
356
|
-
if (columns !== void 0)
|
|
357
|
-
return parser.endProjectedBatch({ selectedColumns: columns });
|
|
358
|
-
return parser.endBatch();
|
|
359
|
-
}
|
|
360
341
|
function ensureRowViewsSupported(options) {
|
|
361
342
|
if ((options.workerCount ?? 1) > 1)
|
|
362
343
|
throw Error("row view callbacks do not support workerCount; use rows()/parallelRows() for materialized rows or withBatches() for low-level streaming");
|
|
@@ -500,16 +481,6 @@ class ScopedCsvColumnarBatchView {
|
|
|
500
481
|
return this.#batch;
|
|
501
482
|
}
|
|
502
483
|
}
|
|
503
|
-
function writeCount(parser, chunk, filters) {
|
|
504
|
-
if (filters === void 0)
|
|
505
|
-
return parser.writeCount(chunk);
|
|
506
|
-
return parser.writeCountWhere(chunk, filters);
|
|
507
|
-
}
|
|
508
|
-
function finishCount(parser, filters) {
|
|
509
|
-
if (filters === void 0)
|
|
510
|
-
return parser.endCount();
|
|
511
|
-
return parser.endCountWhere(filters);
|
|
512
|
-
}
|
|
513
484
|
function toNativeFilters(where) {
|
|
514
485
|
return nativeFilterProgramFor(where);
|
|
515
486
|
}
|
package/dist/batches.d.ts
CHANGED
|
@@ -1,47 +1,338 @@
|
|
|
1
1
|
import { type NativePointer } from './native.js';
|
|
2
2
|
import type { CsvColumnBytesCallback, CsvColumnRangeCallback, CsvColumns, CsvFieldRange, CsvRow, CsvScanColumnsCallback, NativeCsvRowCallback } from './types.js';
|
|
3
|
+
/**
|
|
4
|
+
* A reusable, low-allocation view over one row in a {@link NativeCsvBatch}.
|
|
5
|
+
*
|
|
6
|
+
* `NativeCsvBatch.forEachRow()` moves one instance across the batch instead of
|
|
7
|
+
* allocating a new view for every row. Read the row during the callback; the
|
|
8
|
+
* same object is moved before the next callback. Byte and buffer results are
|
|
9
|
+
* views over the batch's backing storage and must not be retained after the
|
|
10
|
+
* batch is closed.
|
|
11
|
+
*/
|
|
3
12
|
export declare class NativeCsvRowView {
|
|
4
13
|
#private;
|
|
14
|
+
/**
|
|
15
|
+
* Creates a row view over packed batch storage.
|
|
16
|
+
*
|
|
17
|
+
* Most callers should obtain a view from `NativeCsvBatch.forEachRow()`.
|
|
18
|
+
* Direct construction is useful when integrating with a compatible native
|
|
19
|
+
* batch representation.
|
|
20
|
+
*
|
|
21
|
+
* @param data Concatenated field bytes for the batch.
|
|
22
|
+
* @param rowOffsets Row boundaries expressed as indexes into `fieldOffsets`.
|
|
23
|
+
* The array must contain one entry per row plus a final end offset.
|
|
24
|
+
* @param fieldOffsets Field boundaries expressed as byte offsets into `data`.
|
|
25
|
+
* The array must contain one entry per field plus a final end offset.
|
|
26
|
+
* @param rowIndex Initial zero-based row index. The index is checked when a
|
|
27
|
+
* row property or accessor is first read.
|
|
28
|
+
*/
|
|
5
29
|
constructor(data: Buffer, rowOffsets: BigUint64Array, fieldOffsets: BigUint64Array, rowIndex?: number);
|
|
30
|
+
/** Zero-based index of the row currently selected by this view. */
|
|
6
31
|
get rowIndex(): number;
|
|
32
|
+
/** Number of fields in the currently selected row. */
|
|
7
33
|
get fieldCount(): number;
|
|
34
|
+
/**
|
|
35
|
+
* Moves this view to another row in the same batch.
|
|
36
|
+
*
|
|
37
|
+
* @param rowIndex Zero-based row index.
|
|
38
|
+
* @returns This view, so callers can chain the move with a read.
|
|
39
|
+
* @throws {RangeError} If `rowIndex` is outside the row-offset table.
|
|
40
|
+
*/
|
|
8
41
|
moveTo(rowIndex: number): this;
|
|
42
|
+
/**
|
|
43
|
+
* Returns the byte range for a field in the current row.
|
|
44
|
+
*
|
|
45
|
+
* Column indexes are zero-based and relative to the fields stored in the
|
|
46
|
+
* row. The returned offsets address `data` directly.
|
|
47
|
+
*
|
|
48
|
+
* @param columnIndex Zero-based field index.
|
|
49
|
+
* @returns A half-open byte range, or `null` when the row has no such field.
|
|
50
|
+
* @throws {RangeError} If the row or offset table is malformed.
|
|
51
|
+
*/
|
|
9
52
|
fieldRange(columnIndex: number): CsvFieldRange | null;
|
|
53
|
+
/**
|
|
54
|
+
* Returns the current row's field range as a half-open byte interval.
|
|
55
|
+
*
|
|
56
|
+
* @param columnIndex Zero-based field index in the current row.
|
|
57
|
+
* @returns Offsets into the backing data, or `null` when the field is absent.
|
|
58
|
+
*/
|
|
10
59
|
range(columnIndex: number): CsvFieldRange | null;
|
|
60
|
+
/**
|
|
61
|
+
* Returns a borrowed byte view for a field in the current row.
|
|
62
|
+
*
|
|
63
|
+
* The returned view shares the batch backing storage; consume it before the
|
|
64
|
+
* originating batch is closed.
|
|
65
|
+
*
|
|
66
|
+
* @param columnIndex Zero-based field index.
|
|
67
|
+
* @returns A borrowed byte view, or `null` when the field is absent.
|
|
68
|
+
*/
|
|
11
69
|
fieldBytes(columnIndex: number): Uint8Array | null;
|
|
70
|
+
/**
|
|
71
|
+
* Returns borrowed bytes for a field in the current row.
|
|
72
|
+
*
|
|
73
|
+
* @param columnIndex Zero-based field index in the current row.
|
|
74
|
+
* @returns A view sharing the batch storage, or `null` when the field is absent.
|
|
75
|
+
* The view is valid only while the originating batch remains open.
|
|
76
|
+
*/
|
|
12
77
|
bytes(columnIndex: number): Uint8Array | null;
|
|
78
|
+
/**
|
|
79
|
+
* Returns a borrowed `Buffer` view for a field in the current row.
|
|
80
|
+
*
|
|
81
|
+
* The returned buffer shares the batch backing storage and is invalid once
|
|
82
|
+
* the originating batch is closed.
|
|
83
|
+
*
|
|
84
|
+
* @param columnIndex Zero-based field index.
|
|
85
|
+
* @returns A borrowed buffer, or `null` when the field is absent.
|
|
86
|
+
*/
|
|
13
87
|
fieldBuffer(columnIndex: number): Buffer | null;
|
|
88
|
+
/**
|
|
89
|
+
* Returns a borrowed `Buffer` for a field in the current row.
|
|
90
|
+
*
|
|
91
|
+
* @param columnIndex Zero-based field index in the current row.
|
|
92
|
+
* @returns A buffer sharing the batch storage, or `null` when the field is absent.
|
|
93
|
+
* The buffer is valid only while the originating batch remains open.
|
|
94
|
+
*/
|
|
14
95
|
buffer(columnIndex: number): Buffer | null;
|
|
96
|
+
/**
|
|
97
|
+
* Decodes a field as UTF-8 text.
|
|
98
|
+
*
|
|
99
|
+
* @param columnIndex Zero-based field index.
|
|
100
|
+
* @returns The decoded field, or `null` when the field is absent.
|
|
101
|
+
*/
|
|
15
102
|
fieldString(columnIndex: number): string | null;
|
|
103
|
+
/**
|
|
104
|
+
* Reads a field from the current row as UTF-8 text.
|
|
105
|
+
*
|
|
106
|
+
* @param columnIndex Zero-based field index in the current row.
|
|
107
|
+
* @returns Decoded text, or `null` when the field is absent.
|
|
108
|
+
*/
|
|
16
109
|
getPhysical(columnIndex: number): string | null;
|
|
110
|
+
/**
|
|
111
|
+
* Reads a field from the current row as UTF-8 text.
|
|
112
|
+
*
|
|
113
|
+
* This is the concise accessor; use `getPhysical()` when the source-column
|
|
114
|
+
* interpretation should be explicit at the call site.
|
|
115
|
+
*
|
|
116
|
+
* @param columnIndex Zero-based field index in the current row.
|
|
117
|
+
* @returns Decoded text, or `null` when the field is absent.
|
|
118
|
+
*/
|
|
17
119
|
get(columnIndex: number): string | null;
|
|
120
|
+
/**
|
|
121
|
+
* Materializes selected fields from the current row in the requested order.
|
|
122
|
+
*
|
|
123
|
+
* Missing fields are represented by empty strings, matching `rowsInto()`.
|
|
124
|
+
*
|
|
125
|
+
* @param columns Zero-based field indexes. Duplicate indexes are allowed for
|
|
126
|
+
* this low-level accessor.
|
|
127
|
+
* @returns A newly allocated array of decoded UTF-8 strings.
|
|
128
|
+
*/
|
|
18
129
|
pickPhysical(columns: CsvColumns): string[];
|
|
130
|
+
/**
|
|
131
|
+
* Materializes selected fields from the current row in the requested order.
|
|
132
|
+
*
|
|
133
|
+
* @param columns Zero-based field indexes in the current row. Duplicate
|
|
134
|
+
* indexes are allowed for this low-level accessor.
|
|
135
|
+
* @returns A new array of decoded UTF-8 strings. Missing fields are `''`.
|
|
136
|
+
*/
|
|
19
137
|
pick(columns: CsvColumns): string[];
|
|
20
138
|
}
|
|
139
|
+
/**
|
|
140
|
+
* Owns one native parser output batch and exposes low-allocation field views.
|
|
141
|
+
*
|
|
142
|
+
* Batches returned by `NativeCsvParser` own native memory until `close()` or
|
|
143
|
+
* disposal. `dataView()`, offset arrays, and field byte views borrow that
|
|
144
|
+
* memory; release or consume them before closing the batch. Use `using` when
|
|
145
|
+
* iterating batches to make this lifetime explicit.
|
|
146
|
+
*
|
|
147
|
+
* A batch contains the fields emitted by the parser for that operation. When a
|
|
148
|
+
* projected operation was used, column indexes refer to the projected fields
|
|
149
|
+
* in their emitted order.
|
|
150
|
+
*/
|
|
21
151
|
export declare class NativeCsvBatch {
|
|
22
152
|
#private;
|
|
153
|
+
/**
|
|
154
|
+
* Wraps a native batch handle.
|
|
155
|
+
*
|
|
156
|
+
* Callers normally receive instances from `NativeCsvParser`; this constructor
|
|
157
|
+
* is exposed for integrations that already own a compatible native handle.
|
|
158
|
+
*
|
|
159
|
+
* @param handle Native batch pointer returned by the parser ABI. Ownership of
|
|
160
|
+
* the handle transfers to this wrapper; do not wrap the same handle in a
|
|
161
|
+
* second `NativeCsvBatch` instance.
|
|
162
|
+
*/
|
|
23
163
|
constructor(handle: NativePointer);
|
|
164
|
+
/** Whether this batch has released its native handle. */
|
|
24
165
|
get closed(): boolean;
|
|
166
|
+
/** Number of complete rows stored in this batch. */
|
|
25
167
|
get rowCount(): number;
|
|
168
|
+
/** Total number of fields across all rows in this batch. */
|
|
26
169
|
get totalFields(): number;
|
|
170
|
+
/** Number of bytes in the concatenated field-data buffer. */
|
|
27
171
|
get dataLength(): number;
|
|
172
|
+
/**
|
|
173
|
+
* Materializes every field as a UTF-8 string.
|
|
174
|
+
*
|
|
175
|
+
* @returns A newly allocated array containing one string array per row.
|
|
176
|
+
* Missing fields are not padded because this overload returns all stored fields.
|
|
177
|
+
*/
|
|
28
178
|
rows(): CsvRow[];
|
|
179
|
+
/**
|
|
180
|
+
* Visits each row through one reusable {@link NativeCsvRowView}.
|
|
181
|
+
*
|
|
182
|
+
* The callback is invoked synchronously. The view object and its borrowed
|
|
183
|
+
* byte results must not be retained for use after the callback or batch
|
|
184
|
+
* lifetime.
|
|
185
|
+
*
|
|
186
|
+
* @param callback Function invoked with the reusable row view and its row index.
|
|
187
|
+
*/
|
|
29
188
|
forEachRow(callback: NativeCsvRowCallback): void;
|
|
189
|
+
/**
|
|
190
|
+
* Materializes rows into a caller-owned reusable result array.
|
|
191
|
+
*
|
|
192
|
+
* `target` and its existing row arrays are resized and overwritten in place;
|
|
193
|
+
* this avoids allocating a new outer or inner array for every batch. If
|
|
194
|
+
* `columns` is supplied, each row is emitted in that requested order and a
|
|
195
|
+
* missing field is represented by `''`.
|
|
196
|
+
*
|
|
197
|
+
* @param target Array to fill. It is truncated or extended to `rowCount`.
|
|
198
|
+
* @param columns Optional zero-based field indexes relative to this batch.
|
|
199
|
+
* Duplicate indexes are rejected.
|
|
200
|
+
* @returns The same `target` array passed by the caller.
|
|
201
|
+
* @throws {RangeError} If a column index is invalid, repeated, or an offset
|
|
202
|
+
* exceeds the batch backing storage.
|
|
203
|
+
*/
|
|
30
204
|
rowsInto(target: CsvRow[], columns?: CsvColumns): CsvRow[];
|
|
205
|
+
/**
|
|
206
|
+
* Returns the concatenated field bytes as a borrowed `Buffer`.
|
|
207
|
+
*
|
|
208
|
+
* The buffer is backed by native batch storage and must not be used after
|
|
209
|
+
* `close()`.
|
|
210
|
+
*/
|
|
31
211
|
data(): Buffer;
|
|
212
|
+
/**
|
|
213
|
+
* Returns the concatenated field bytes as a borrowed `Uint8Array`.
|
|
214
|
+
*
|
|
215
|
+
* This view shares storage with `data()` and remains valid only while the
|
|
216
|
+
* batch is open.
|
|
217
|
+
*/
|
|
32
218
|
dataView(): Uint8Array;
|
|
219
|
+
/**
|
|
220
|
+
* Returns borrowed row boundaries as a `BigUint64Array`.
|
|
221
|
+
*
|
|
222
|
+
* Each value is a field index into `fieldOffsets`; the array has
|
|
223
|
+
* `rowCount + 1` entries. Offsets may exceed `Number.MAX_SAFE_INTEGER`, so
|
|
224
|
+
* keep them as `bigint` unless a checked conversion is appropriate.
|
|
225
|
+
*/
|
|
33
226
|
rowOffsets(): BigUint64Array;
|
|
227
|
+
/**
|
|
228
|
+
* Returns borrowed field byte boundaries as a `BigUint64Array`.
|
|
229
|
+
*
|
|
230
|
+
* Each value is a byte offset into `data()`; the array has
|
|
231
|
+
* `totalFields + 1` entries. Offsets may exceed `Number.MAX_SAFE_INTEGER`,
|
|
232
|
+
* so keep them as `bigint` unless a checked conversion is appropriate.
|
|
233
|
+
*/
|
|
34
234
|
fieldOffsets(): BigUint64Array;
|
|
235
|
+
/**
|
|
236
|
+
* Returns the number of fields in one row.
|
|
237
|
+
*
|
|
238
|
+
* @param rowIndex Zero-based row index.
|
|
239
|
+
* @throws {RangeError} If `rowIndex` is outside the batch.
|
|
240
|
+
*/
|
|
35
241
|
rowFieldCount(rowIndex: number): number;
|
|
242
|
+
/**
|
|
243
|
+
* Returns a field's byte range within the concatenated batch data.
|
|
244
|
+
*
|
|
245
|
+
* @param rowIndex Zero-based row index.
|
|
246
|
+
* @param columnIndex Zero-based field index relative to that row.
|
|
247
|
+
* @returns A half-open byte range, or `null` when the row has no such field.
|
|
248
|
+
* @throws {RangeError} If the row or offset tables are invalid.
|
|
249
|
+
*/
|
|
36
250
|
fieldRange(rowIndex: number, columnIndex: number): CsvFieldRange | null;
|
|
251
|
+
/**
|
|
252
|
+
* Returns a borrowed `Buffer` view for one field.
|
|
253
|
+
*
|
|
254
|
+
* @param rowIndex Zero-based row index.
|
|
255
|
+
* @param columnIndex Zero-based field index relative to that row.
|
|
256
|
+
* @returns A view backed by the batch data, or `null` when the field is absent.
|
|
257
|
+
*/
|
|
37
258
|
fieldBuffer(rowIndex: number, columnIndex: number): Buffer | null;
|
|
259
|
+
/**
|
|
260
|
+
* Returns a borrowed `Uint8Array` view for one field.
|
|
261
|
+
*
|
|
262
|
+
* @param rowIndex Zero-based row index.
|
|
263
|
+
* @param columnIndex Zero-based field index relative to that row.
|
|
264
|
+
* @returns A view backed by the batch data, or `null` when the field is absent.
|
|
265
|
+
*/
|
|
38
266
|
fieldBytes(rowIndex: number, columnIndex: number): Uint8Array | null;
|
|
267
|
+
/**
|
|
268
|
+
* Decodes one field as UTF-8 text.
|
|
269
|
+
*
|
|
270
|
+
* @param rowIndex Zero-based row index.
|
|
271
|
+
* @param columnIndex Zero-based field index relative to that row.
|
|
272
|
+
* @returns The decoded text, or `null` when the field is absent.
|
|
273
|
+
*/
|
|
39
274
|
fieldString(rowIndex: number, columnIndex: number): string | null;
|
|
275
|
+
/**
|
|
276
|
+
* Visits the byte range of one column for a contiguous row interval.
|
|
277
|
+
*
|
|
278
|
+
* Rows that do not contain `columnIndex` are skipped. The callback receives
|
|
279
|
+
* offsets into `data()` and is invoked synchronously.
|
|
280
|
+
*
|
|
281
|
+
* @param columnIndex Zero-based field index relative to each row.
|
|
282
|
+
* @param callback Receives `(rowIndex, start, end)` for each present field.
|
|
283
|
+
* @param startRow Inclusive first row; defaults to `0`.
|
|
284
|
+
* @param endRow Exclusive end row; defaults to `rowCount`.
|
|
285
|
+
* @throws {RangeError} If the row interval is invalid or an offset exceeds
|
|
286
|
+
* the batch backing storage.
|
|
287
|
+
*/
|
|
40
288
|
forEachColumnRange(columnIndex: number, callback: CsvColumnRangeCallback, startRow?: number, endRow?: number): void;
|
|
289
|
+
/**
|
|
290
|
+
* Visits one column's borrowed bytes for a contiguous row interval.
|
|
291
|
+
*
|
|
292
|
+
* Missing fields are skipped. Each `Uint8Array` shares the batch backing
|
|
293
|
+
* storage and must be consumed during the callback.
|
|
294
|
+
*
|
|
295
|
+
* @param columnIndex Zero-based field index relative to each row.
|
|
296
|
+
* @param callback Receives `(rowIndex, bytes)` for each present field.
|
|
297
|
+
* @param startRow Inclusive first row; defaults to `0`.
|
|
298
|
+
* @param endRow Exclusive end row; defaults to `rowCount`.
|
|
299
|
+
*/
|
|
41
300
|
forEachColumnBytes(columnIndex: number, callback: CsvColumnBytesCallback, startRow?: number, endRow?: number): void;
|
|
301
|
+
/**
|
|
302
|
+
* Scans several columns while reusing one range array per row.
|
|
303
|
+
*
|
|
304
|
+
* `ranges` contains two numeric offsets per requested column in the same
|
|
305
|
+
* order as `columns`: `[start0, end0, start1, end1, ...]`. A missing field is
|
|
306
|
+
* reported as `[-1, -1]`. Both `ranges` and `data` are reused/borrowed and
|
|
307
|
+
* must be consumed synchronously by the callback.
|
|
308
|
+
*
|
|
309
|
+
* @param columns Zero-based field indexes relative to each row.
|
|
310
|
+
* @param callback Receives `(rowIndex, ranges, data)` for each row.
|
|
311
|
+
* @param startRow Inclusive first row; defaults to `0`.
|
|
312
|
+
* @param endRow Exclusive end row; defaults to `rowCount`.
|
|
313
|
+
* @throws {RangeError} If the row interval or an offset is invalid.
|
|
314
|
+
*/
|
|
42
315
|
scanColumns(columns: CsvColumns, callback: CsvScanColumnsCallback, startRow?: number, endRow?: number): void;
|
|
316
|
+
/**
|
|
317
|
+
* Counts rows whose field equals a supplied byte value.
|
|
318
|
+
*
|
|
319
|
+
* A string value is encoded as UTF-8; `Buffer` and `Uint8Array` values are
|
|
320
|
+
* compared as supplied.
|
|
321
|
+
*
|
|
322
|
+
* @param columnIndex Zero-based field index relative to each row.
|
|
323
|
+
* @param value String or bytes to compare.
|
|
324
|
+
* @returns The number of matching rows.
|
|
325
|
+
* @throws {RangeError} If `columnIndex` is invalid.
|
|
326
|
+
*/
|
|
43
327
|
countWhereEquals(columnIndex: number, value: string | Buffer | Uint8Array): number;
|
|
328
|
+
/**
|
|
329
|
+
* Releases the native batch and invalidates borrowed data and offset views.
|
|
330
|
+
*
|
|
331
|
+
* Calling `close()` more than once is safe.
|
|
332
|
+
*/
|
|
44
333
|
close(): void;
|
|
334
|
+
/** Alias for {@link close}, suitable for explicit resource management. */
|
|
45
335
|
dispose(): void;
|
|
336
|
+
/** Implements `using` / explicit resource management. */
|
|
46
337
|
[Symbol.dispose](): void;
|
|
47
338
|
}
|