@cobol-ts/cursor-file 0.0.0-stage → 0.0.1
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 +376 -2
- package/node.d.ts +1 -0
- package/node.js +2 -0
- package/package.json +65 -3
package/README.md
CHANGED
|
@@ -1,3 +1,377 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @cobol-ts/cursor-file
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Application-facing file cursor orchestration for the byte-backed `@cobol-ts` pipeline.
|
|
4
|
+
|
|
5
|
+
This package now owns the normal end-to-end source-opening path. Given a file definition, a physical source object and infrastructure, it:
|
|
6
|
+
|
|
7
|
+
- selects the parser from the parser registry;
|
|
8
|
+
- derives parser configuration from the file definition;
|
|
9
|
+
- creates pooled physical-record resources;
|
|
10
|
+
- runs optional parser preparation;
|
|
11
|
+
- compiles projection paths against the prepared source;
|
|
12
|
+
- creates a reusable projection factory;
|
|
13
|
+
- iterates parsed records;
|
|
14
|
+
- applies the selected row-error policy;
|
|
15
|
+
- manages successful result/projection lifetime.
|
|
16
|
+
|
|
17
|
+
## Position in the pipeline
|
|
18
|
+
|
|
19
|
+
```text
|
|
20
|
+
CursorFileDefinition + file + FileCursorInfrastructure
|
|
21
|
+
↓
|
|
22
|
+
parser registry lookup
|
|
23
|
+
↓
|
|
24
|
+
createDefaultPhysicalRecordConfig()
|
|
25
|
+
↓
|
|
26
|
+
openRecordCursor()
|
|
27
|
+
├── optional parser.prepare()
|
|
28
|
+
└── RecordCursor
|
|
29
|
+
↓
|
|
30
|
+
createProjection(
|
|
31
|
+
parser,
|
|
32
|
+
opened details,
|
|
33
|
+
preparation context,
|
|
34
|
+
canonical projection
|
|
35
|
+
)
|
|
36
|
+
↓
|
|
37
|
+
data rows
|
|
38
|
+
↓
|
|
39
|
+
pooled FileCursorResult
|
|
40
|
+
├── reusable bound projection
|
|
41
|
+
└── close()
|
|
42
|
+
↓
|
|
43
|
+
application code
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
## File definitions
|
|
47
|
+
|
|
48
|
+
A file definition contains only source-level information required by generic cursor orchestration:
|
|
49
|
+
|
|
50
|
+
```ts
|
|
51
|
+
interface CursorFileDefinition<
|
|
52
|
+
TProjection,
|
|
53
|
+
TFileType extends string
|
|
54
|
+
> {
|
|
55
|
+
readonly filename: string;
|
|
56
|
+
readonly fileType: TFileType;
|
|
57
|
+
readonly projection:
|
|
58
|
+
CanonicalProjection<
|
|
59
|
+
ProjectionDefinition<TProjection>
|
|
60
|
+
>;
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Parser-specific configuration can be added by a more specific application/file-definition type and extracted by the registry entry's `parserConfig(definition)` function.
|
|
65
|
+
|
|
66
|
+
For example, the integration tests add CSV separator/quote configuration to their CSV file definitions without making generic `CursorFileDefinition` understand CSV.
|
|
67
|
+
|
|
68
|
+
## Infrastructure
|
|
69
|
+
|
|
70
|
+
The generic infrastructure is:
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
interface FileCursorInfrastructure<
|
|
74
|
+
TFile,
|
|
75
|
+
TFileType extends string
|
|
76
|
+
> {
|
|
77
|
+
readonly parsers: ParserRegistry<TFileType>;
|
|
78
|
+
readonly bufferSize: number;
|
|
79
|
+
readonly fillFileBuffer: FillFileBuffer<TFile>;
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
The physical `TFile` abstraction is application-defined. The cursor requires only the ability to fill a supplied pooled `Uint8Array` from the current source position.
|
|
84
|
+
|
|
85
|
+
## Parser registry
|
|
86
|
+
|
|
87
|
+
Each file type resolves to a registration containing:
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
parser
|
|
91
|
+
parserConfig(definition)
|
|
92
|
+
createRecordState()
|
|
93
|
+
cleanRecordState?(state)
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
The registration is where a file-definition shape is connected to the parser's configuration and per-record state. The optional `cleanRecordState` hook runs on the physical holder's **final** pool release, after result/projection clearing. It is essential for pooled COBOL ODO geometry: use `cleanCobolRecordState` with `createCobolRecordState`. Formats without record-dependent cached positions need not supply it.
|
|
97
|
+
|
|
98
|
+
After runtime file-type lookup, generic cursor orchestration deliberately erases the concrete parser-family generics at that narrow registry boundary.
|
|
99
|
+
|
|
100
|
+
## Source setup
|
|
101
|
+
|
|
102
|
+
Before any data row is yielded, `cursor-file`:
|
|
103
|
+
|
|
104
|
+
1. looks up the parser;
|
|
105
|
+
2. obtains parser configuration;
|
|
106
|
+
3. creates byte-buffer and physical-record pools;
|
|
107
|
+
4. calls `openRecordCursor()`;
|
|
108
|
+
5. receives opened-source details and optional preparation context;
|
|
109
|
+
6. calls `createProjection()` and compiles every projection path once.
|
|
110
|
+
|
|
111
|
+
Configuration, parser preparation and projection-path compilation failures therefore happen before the normal data-row loop and **throw** for every cursor policy.
|
|
112
|
+
|
|
113
|
+
This is intentional: setup failures are not recoverable row data errors.
|
|
114
|
+
|
|
115
|
+
## The normal application model
|
|
116
|
+
|
|
117
|
+
Application code consumes only the values required by its calculation and closes each successful result when finished:
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
for await (const row of cursor) {
|
|
121
|
+
if (Array.isArray(row)) {
|
|
122
|
+
// Recoverable row Errors represented as string[].
|
|
123
|
+
continue;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
try {
|
|
127
|
+
use(
|
|
128
|
+
row.result.id(),
|
|
129
|
+
row.result.name()
|
|
130
|
+
);
|
|
131
|
+
} finally {
|
|
132
|
+
row.close();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Projection methods are lazy accessors.
|
|
138
|
+
|
|
139
|
+
## Cursor variants
|
|
140
|
+
|
|
141
|
+
### `createFileCursor`
|
|
142
|
+
|
|
143
|
+
Returns successful rows as:
|
|
144
|
+
|
|
145
|
+
```ts
|
|
146
|
+
FileCursorResult<TProjection>
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
and recoverable **data-row** parser errors as:
|
|
150
|
+
|
|
151
|
+
```ts
|
|
152
|
+
string[]
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
Only the `errors` array is exposed in this lowest-overhead policy.
|
|
156
|
+
|
|
157
|
+
Setup failures throw.
|
|
158
|
+
|
|
159
|
+
### `createErrorsOrFileCursor`
|
|
160
|
+
|
|
161
|
+
Returns each data row as:
|
|
162
|
+
|
|
163
|
+
```ts
|
|
164
|
+
ErrorsOr<FileCursorResult<TProjection>>
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Recoverable row errors retain the full `Errors` object, including fields such as `reference`.
|
|
168
|
+
|
|
169
|
+
Setup failures still throw.
|
|
170
|
+
|
|
171
|
+
### `createThrowingFileCursor`
|
|
172
|
+
|
|
173
|
+
Returns successful rows normally and converts recoverable row `Errors` into `ErrorsException`.
|
|
174
|
+
|
|
175
|
+
Setup failures also throw.
|
|
176
|
+
|
|
177
|
+
### `createIgnoringFileCursor`
|
|
178
|
+
|
|
179
|
+
Skips recoverable row errors and continues.
|
|
180
|
+
|
|
181
|
+
An optional callback receives the full `Errors` object for ignored rows.
|
|
182
|
+
|
|
183
|
+
Setup failures are never swallowed or passed to `onError`.
|
|
184
|
+
|
|
185
|
+
## `FileCursorResult`
|
|
186
|
+
|
|
187
|
+
A successful result has the small public shape:
|
|
188
|
+
|
|
189
|
+
```ts
|
|
190
|
+
interface FileCursorResult<TResult> {
|
|
191
|
+
readonly result: TResult;
|
|
192
|
+
close(): void;
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
`result` is the reusable lazy application projection.
|
|
197
|
+
|
|
198
|
+
`close()` ends its borrowed physical-record lifetime.
|
|
199
|
+
|
|
200
|
+
## Result and projection pooling
|
|
201
|
+
|
|
202
|
+
Each pooled `FileCursorResult` owns one reusable projection object for its entire lifetime.
|
|
203
|
+
|
|
204
|
+
The wrapper object's shape is permanent. Its `physicalRecord` slot is internally either:
|
|
205
|
+
|
|
206
|
+
```text
|
|
207
|
+
undefined
|
|
208
|
+
while inactive
|
|
209
|
+
|
|
210
|
+
PhysicalRecordLifetime
|
|
211
|
+
while an active lease owns a record
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
On a successful row:
|
|
215
|
+
|
|
216
|
+
1. the permanent projection is bound to the current parser representation and physical record;
|
|
217
|
+
2. the physical record is stored in the result wrapper;
|
|
218
|
+
3. the wrapper is returned to the application.
|
|
219
|
+
|
|
220
|
+
On `close()`:
|
|
221
|
+
|
|
222
|
+
1. the projection binding is cleared;
|
|
223
|
+
2. the wrapper's physical-record slot is reset to `undefined`;
|
|
224
|
+
3. the physical record is closed;
|
|
225
|
+
4. the unchanged wrapper returns to its pool.
|
|
226
|
+
|
|
227
|
+
No new projection object or accessor functions are required for normal row-to-row reuse.
|
|
228
|
+
|
|
229
|
+
## Why `close()` is explicit
|
|
230
|
+
|
|
231
|
+
A lazy projection may still depend on the current physical source bytes.
|
|
232
|
+
|
|
233
|
+
For CSV, the current representation stores field byte ranges in `recordState`, and an accessor decodes the requested range only when called.
|
|
234
|
+
|
|
235
|
+
The physical record therefore has to remain live until application code has finished using the projection.
|
|
236
|
+
|
|
237
|
+
Once `close()` has been called, both the result wrapper and projection reference are invalid for application use. The same pooled objects may later represent another row.
|
|
238
|
+
|
|
239
|
+
## Several live results
|
|
240
|
+
|
|
241
|
+
Several successful results may remain live simultaneously.
|
|
242
|
+
|
|
243
|
+
The pools grow to support the maximum simultaneous demand for:
|
|
244
|
+
|
|
245
|
+
- result wrappers;
|
|
246
|
+
- projection objects;
|
|
247
|
+
- physical-record holders;
|
|
248
|
+
- physical buffers.
|
|
249
|
+
|
|
250
|
+
Memory is therefore tied to live work rather than total input size, assuming callers eventually close results.
|
|
251
|
+
|
|
252
|
+
## Error lifetime
|
|
253
|
+
|
|
254
|
+
If `Parser.parse()` returns recoverable row `Errors`, no successful projection lease is created. `cursor-file` closes that physical record immediately and applies the selected error policy.
|
|
255
|
+
|
|
256
|
+
If successful parser output cannot be bound into a result, the physical record is also closed before the exception propagates.
|
|
257
|
+
|
|
258
|
+
Lazy projection accessor errors happen later while a successful result is open. They propagate as exceptions enriched by the projection layer with the currently bound source, record number, logical **indexed application** path (when selection context exists), COBOL declaration/controller path when applicable, and parser-specific physical rendering. Constructing the indexed path is deferred to the error path.
|
|
259
|
+
|
|
260
|
+
## Example
|
|
261
|
+
|
|
262
|
+
```ts
|
|
263
|
+
interface Customer {
|
|
264
|
+
id(): number;
|
|
265
|
+
name(): string;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const customerProjection =
|
|
269
|
+
defineProjection<Customer>({
|
|
270
|
+
id: {
|
|
271
|
+
type: "integer"
|
|
272
|
+
},
|
|
273
|
+
name: {
|
|
274
|
+
type: "string"
|
|
275
|
+
}
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
const customerFile = {
|
|
279
|
+
filename: "customers.jsonl",
|
|
280
|
+
fileType: "json" as const,
|
|
281
|
+
projection: customerProjection
|
|
282
|
+
};
|
|
283
|
+
|
|
284
|
+
const infrastructure = {
|
|
285
|
+
parsers: {
|
|
286
|
+
json: {
|
|
287
|
+
parser: jsonParser,
|
|
288
|
+
parserConfig: () => undefined,
|
|
289
|
+
createRecordState: () => undefined
|
|
290
|
+
}
|
|
291
|
+
},
|
|
292
|
+
bufferSize: 64 * 1024,
|
|
293
|
+
fillFileBuffer
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
const cursor =
|
|
297
|
+
createFileCursor(
|
|
298
|
+
customerFile,
|
|
299
|
+
file,
|
|
300
|
+
infrastructure
|
|
301
|
+
);
|
|
302
|
+
|
|
303
|
+
for await (const value of cursor) {
|
|
304
|
+
if (Array.isArray(value)) {
|
|
305
|
+
console.error(value);
|
|
306
|
+
continue;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
try {
|
|
310
|
+
consume(
|
|
311
|
+
value.result.id(),
|
|
312
|
+
value.result.name()
|
|
313
|
+
);
|
|
314
|
+
} finally {
|
|
315
|
+
value.close();
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
```
|
|
319
|
+
|
|
320
|
+
## Performance intent
|
|
321
|
+
|
|
322
|
+
The steady-state success path reuses:
|
|
323
|
+
|
|
324
|
+
```text
|
|
325
|
+
FileCursorResult wrapper
|
|
326
|
+
projection object
|
|
327
|
+
projection accessor functions
|
|
328
|
+
physical-record holder
|
|
329
|
+
physical byte buffers
|
|
330
|
+
parser record state
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
File-scoped setup performs parser preparation and projection-path compilation once before the row hot path.
|
|
334
|
+
|
|
335
|
+
For JSON, `JSON.parse()` has already eagerly materialised the source record. For CSV, the same application API sits over a genuinely byte-backed representation and materialises only requested field values.
|
|
336
|
+
|
|
337
|
+
## Node.js file-buffer adapter
|
|
338
|
+
|
|
339
|
+
`fillNodeFileBuffer(handle, pooledUint8Array)` is an optional Node adapter. It
|
|
340
|
+
retries short positive reads into the *same* destination until the buffer is
|
|
341
|
+
full or `read()` returns zero. It does not allocate a record or field buffer.
|
|
342
|
+
The caller owns and closes the `FileHandle`. The standard COBOL Node wrapper
|
|
343
|
+
(`@cobol-ts/cursor-loader-cobol/node`) manages that lifetime automatically.
|
|
344
|
+
|
|
345
|
+
## Synchronous record callback
|
|
346
|
+
|
|
347
|
+
`scanThrowingFileCursor(definition, file, infrastructure, onRecord)` is an
|
|
348
|
+
additive alternative to the async iterator APIs. It returns one Promise for
|
|
349
|
+
the complete scan and invokes `onRecord(projectedRow)` synchronously for each
|
|
350
|
+
record. Returning `false` stops the scan. The projected row is borrowed and
|
|
351
|
+
invalid once the callback returns; it is released automatically even when the
|
|
352
|
+
callback throws. A Promise-returning callback is rejected.
|
|
353
|
+
|
|
354
|
+
Parsers supporting synchronous framing of already-buffered bytes can supply
|
|
355
|
+
`parseBuffered`; a result of `undefined` means another buffer must be read.
|
|
356
|
+
No record-level promise or async generator is used on this fast path. Existing
|
|
357
|
+
parsers without this capability retain their original asynchronous parsing.
|
|
358
|
+
|
|
359
|
+
### Record-object pool injection
|
|
360
|
+
|
|
361
|
+
The callback API creates a new non-reference-counted physical-record object pool
|
|
362
|
+
per scan unless `infrastructure.recordPool` is provided. The caller may inject
|
|
363
|
+
`createSimpleRecordPool()` or `createReferenceCountedRecordPool()` from
|
|
364
|
+
`@cobol-ts/cursor-record`; the pool is an instance, not a pool factory.
|
|
365
|
+
Treat a supplied pool as scoped to one opened reader. Pooled physical records
|
|
366
|
+
capture that reader's buffer pool and cleanup functions; do not share the same
|
|
367
|
+
pool instance between distinct or concurrent file scans.
|
|
368
|
+
|
|
369
|
+
The optional `byteBufferPool` may also be injected at the top level, but it must
|
|
370
|
+
support `reuse`/`release` because neighbouring records can share a byte buffer.
|
|
371
|
+
Byte-buffer references are still reference-counted. A record can share its
|
|
372
|
+
final byte buffer with the following record, including when it spans buffers.
|
|
373
|
+
The existing iterator API retains its original reference-counted default.
|
|
374
|
+
|
|
375
|
+
## Optional operation metrics
|
|
376
|
+
|
|
377
|
+
Supply one `metrics?: IPutMetrics` in `FileCursorInfrastructure` (for instance a `MemoryMetrics` from `@cobol-ts/metrics`). The reader forwards that instance to pooled physical-record resources and the projection factory; the callback scanner additionally records completed callback-row visits. Omitting the option uses the allocation-free `NullMetrics`. No global mutable state is introduced. Counters are operation counts rather than execution times. See `@cobol-ts/metrics` and the benchmark's `README.metrics.md`.
|
package/node.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './dist/node';
|
package/node.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,68 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cobol-ts/cursor-file",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"description": "the cursor that physically reads the file and returns the byte arrays to the cursor loader",
|
|
4
|
+
"version": "0.0.1",
|
|
5
|
+
"main": "dist/index",
|
|
6
|
+
"types": "dist/index",
|
|
7
|
+
"browserslist": {
|
|
8
|
+
"development": [
|
|
9
|
+
"last 1 chrome version",
|
|
10
|
+
"last 1 firefox version",
|
|
11
|
+
"last 1 safari version"
|
|
12
|
+
],
|
|
13
|
+
"production": [
|
|
14
|
+
">0.2%",
|
|
15
|
+
"not dead",
|
|
16
|
+
"not op_mini all"
|
|
17
|
+
]
|
|
18
|
+
},
|
|
19
|
+
"dependencies": {
|
|
20
|
+
"@cobol-ts/cursor-loader-types": "0.0.1",
|
|
21
|
+
"@cobol-ts/cursor-record": "0.0.1",
|
|
22
|
+
"@cobol-ts/metrics": "0.0.1"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@testing-library/jest-dom": "^6.1.4",
|
|
26
|
+
"@types/jest": "^29.0.0",
|
|
27
|
+
"@types/node": "^14.0.0",
|
|
28
|
+
"jest": "^29.0.0",
|
|
29
|
+
"ts-jest": "^29.0.0",
|
|
30
|
+
"typescript": "^5.2.2"
|
|
31
|
+
},
|
|
32
|
+
"eslintConfig": {
|
|
33
|
+
"extends": [
|
|
34
|
+
"react-app",
|
|
35
|
+
"react-app/jest"
|
|
36
|
+
],
|
|
37
|
+
"overrides": [
|
|
38
|
+
{
|
|
39
|
+
"files": [
|
|
40
|
+
"**/*.stories.*"
|
|
41
|
+
],
|
|
42
|
+
"rules": {
|
|
43
|
+
"import/no-anonymous-default-export": "off"
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
]
|
|
47
|
+
},
|
|
48
|
+
"files": [
|
|
49
|
+
"dist/index.js",
|
|
50
|
+
"dist/index.d.ts",
|
|
51
|
+
"dist/src/",
|
|
52
|
+
"build/",
|
|
53
|
+
"dist/node.js",
|
|
54
|
+
"dist/node.d.ts",
|
|
55
|
+
"node.js",
|
|
56
|
+
"node.d.ts"
|
|
57
|
+
],
|
|
58
|
+
"keywords": [
|
|
59
|
+
"indexing"
|
|
60
|
+
],
|
|
61
|
+
"license": "MIT",
|
|
62
|
+
"repository": "git@github.com:intellimaintain/intellimaintain.git",
|
|
63
|
+
"scripts": {
|
|
64
|
+
"test": "jest --config jest.config.json --env node",
|
|
65
|
+
"test:watch": "npm run test -- --watch",
|
|
66
|
+
"start": "react-scripts start"
|
|
67
|
+
}
|
|
6
68
|
}
|