@cobol-ts/cursor-projection 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 +185 -2
- package/dist/index.d.ts +2 -0
- package/dist/index.js +19 -0
- package/package.json +61 -3
package/README.md
CHANGED
|
@@ -1,3 +1,186 @@
|
|
|
1
|
-
#
|
|
1
|
+
# `@cobol-ts/cursor-projection`
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Compile-once, lazy, reusable **typed projections** over parser-owned representations. A projection gives application code a familiar interface without requiring a fresh tree of decoded values for each record.
|
|
4
|
+
|
|
5
|
+
## Why this package exists
|
|
6
|
+
|
|
7
|
+
The architecture deliberately separates three responsibilities:
|
|
8
|
+
|
|
9
|
+
1. **Physical input and lifetime** (`cursor-record`, `cursor-file`): borrow bytes, delimit records, manage leases and pools.
|
|
10
|
+
2. **Format-specific interpretation** (CSV, JSONL, COBOL): compile logical paths and read or select values in the representation.
|
|
11
|
+
3. **Application projection** (this package): declare desired members, compile them once, expose scalar methods, nested objects and projected arrays.
|
|
12
|
+
|
|
13
|
+
For byte-backed formats this is more than an optimisation. A COBOL copybook and the bytes do not always identify one unambiguous *business* object. `REDEFINES` may require business context—possibly outside the record—to select an interpretation. This package does not attempt to make that decision.
|
|
14
|
+
|
|
15
|
+
## Application-facing model
|
|
16
|
+
|
|
17
|
+
```ts
|
|
18
|
+
import type {ProjectedArray} from "@cobol-ts/arrays";
|
|
19
|
+
import {defineProjection, createProjection} from "@cobol-ts/cursor-projection";
|
|
20
|
+
|
|
21
|
+
interface Item {
|
|
22
|
+
sku(): string;
|
|
23
|
+
quantity(): number;
|
|
24
|
+
}
|
|
25
|
+
interface Order {
|
|
26
|
+
id(): number;
|
|
27
|
+
readonly items: ProjectedArray<Item>;
|
|
28
|
+
}
|
|
29
|
+
interface Address {
|
|
30
|
+
town(): string;
|
|
31
|
+
}
|
|
32
|
+
interface Customer {
|
|
33
|
+
name(): string;
|
|
34
|
+
readonly address: Address;
|
|
35
|
+
readonly orders: ProjectedArray<Order>;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const customerDefinition = defineProjection<Customer>({
|
|
39
|
+
name: {type: "string"},
|
|
40
|
+
address: {type: "object", fields: {
|
|
41
|
+
town: {type: "string"}
|
|
42
|
+
}},
|
|
43
|
+
orders: {type: "array", element: {
|
|
44
|
+
id: {type: "integer"},
|
|
45
|
+
items: {type: "array", element: {
|
|
46
|
+
sku: {type: "string"},
|
|
47
|
+
quantity: {type: "integer"}
|
|
48
|
+
}}
|
|
49
|
+
}}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
// Given a live, bound Customer projection:
|
|
53
|
+
customer.orders[0].items[1].sku();
|
|
54
|
+
customer.orders.map(order => order.id());
|
|
55
|
+
customer.address.town();
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
Scalar members are functions returning `string` or `number`. Nested objects and arrays are **readonly properties**. Supported scalar types are `integer`, `float`, `character` and `string`. Arrays may contain further projected objects and arrays, including multiple sibling arrays within one parent.
|
|
59
|
+
|
|
60
|
+
`defineProjection<T>()` type-checks the definition against the application's interface and canonicalises missing paths, so `name: {type:"string"}` becomes a member with the logical path `["name"]`. Child paths are **relative to the containing projection**. A path can contain `string` names and `number` literal selectors; whether those selectors are valid depends on the parser.
|
|
61
|
+
|
|
62
|
+
## Compile once, then bind many records
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
const factory = createProjection(
|
|
66
|
+
parser, // format's ProjectionSupport and debug renderer
|
|
67
|
+
preparedDetails, // one opened source
|
|
68
|
+
preparationContext, // often undefined
|
|
69
|
+
customerDefinition
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const customer = factory.create(); // one reusable, initially unbound view
|
|
73
|
+
factory.bind(customer, representation, physicalRecord);
|
|
74
|
+
// Use customer while its owning record lease is live.
|
|
75
|
+
factory.clear(customer); // remove borrowed bindings
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
Compilation recursively visits all scalar, object and array members. Each logical path goes through the parser's `compileProjectionPath(details, context, path, parent?)` **once at factory creation**, including relative child paths. The compiled schema keeps numeric COBOL field geometry, numeric CSV column indexes or JSON property-path tokens. It is independent of the current row.
|
|
79
|
+
|
|
80
|
+
The factory installs reusable scalar accessor functions and getters on view objects. Calling a scalar accessor passes the selected representation, source details and compiled path to the parser. A projected array getter asks the parser for an **indexed source**: `length()` and `element(index)`. It never requires a materialised array of underlying record values.
|
|
81
|
+
|
|
82
|
+
## Array selection and reusable child views
|
|
83
|
+
|
|
84
|
+
The array interface and proxy live in `@cobol-ts/arrays`; record-aware projection construction lives here. At runtime:
|
|
85
|
+
|
|
86
|
+
```text
|
|
87
|
+
customer.orders
|
|
88
|
+
└─ source: length(), element(index)
|
|
89
|
+
├─ [0] → cached Order projection, bound to selected representation
|
|
90
|
+
│ └─ items → its own cached ProjectedArray<Item>
|
|
91
|
+
└─ [1] → independent cached Order projection
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
A projected array has **two different quantities**:
|
|
95
|
+
|
|
96
|
+
- **Active length**: how many elements the current representation exposes (native JSON array length or a COBOL OCCURS count).
|
|
97
|
+
- **Retained element slots**: projection objects created on earlier accesses and kept for reuse. Slots are allocated lazily; accessing index 9 need not construct indexes 0–8.
|
|
98
|
+
|
|
99
|
+
The active length determines bounds and iteration. Retained slots outside that length are inaccessible but are not discarded. The same rule applies independently to each nested array. This high-water reuse avoids repeatedly rebuilding the projection hierarchy.
|
|
100
|
+
|
|
101
|
+
**Where state lives:** root array runtimes are stored in `record.recordState.projectionViews`, keyed by the **compiled array-member object**. Different members (`orders`, `addresses`, etc.) therefore cannot collide. Nested arrays are retained by their respective child projection instances. No new physical-record class or independent child-object pool is required. The physical-record pool is source/file-scoped; its generic record state persists while holders are reused.
|
|
102
|
+
|
|
103
|
+
A name such as `root.orders` accompanies array runtimes for debugging; the name is not their cache key. The normal cursor integration binds one root projection to its leased physical-record holder.
|
|
104
|
+
|
|
105
|
+
## Borrowed lifetime and pooling
|
|
106
|
+
|
|
107
|
+
`FileCursorResult` owns a live physical record and a reusable root projection. Closing the result clears projection bindings, including accessed children, before releasing the physical record and its byte-buffer leases. **Keep neither root nor child views beyond that close.** Pooled JavaScript objects may later refer to different rows; a retained reference is not a permanently safe snapshot.
|
|
108
|
+
|
|
109
|
+
Two simultaneously live file results have distinct physical record holders and retain independent projected children. Selecting `orders[0]` more than once during a lease returns the same projected child object. **Repeated nested-object and array property getters reuse an already bound selection without calling the parser again.** This relies on the borrowed representation remaining structurally stable for the lease; clearing the parent invalidates child bindings before reuse. Reuse on a subsequent lease avoids recompilation and, for previously accessed indexes, avoids recreating child objects.
|
|
110
|
+
|
|
111
|
+
## Format-specific behaviour
|
|
112
|
+
|
|
113
|
+
| Parser | Scalar access | Object and array selection |
|
|
114
|
+
| --- | --- | --- |
|
|
115
|
+
| JSONL | Traversal of already parsed JSON | Native object selection and `ProjectedArraySource` over a JSON array |
|
|
116
|
+
| COBOL, fixed layout | Numeric field coordinates over original bytes | Selected group plus occurrence displacement; no copied COBOL array |
|
|
117
|
+
| COBOL, ODO layout | Symbolically compiled original-byte locations | Lazy ODO boundary prefixes in record state; fixed-location controllers required |
|
|
118
|
+
| CSV | Compiled column number and lazy byte-range decoding | Intentionally flat: no nested object/array capability |
|
|
119
|
+
|
|
120
|
+
Legacy runtime-indexed **scalar** methods also remain supported where the parser implements them; they are not equivalent to first-class projected arrays.
|
|
121
|
+
|
|
122
|
+
## Errors and diagnostics
|
|
123
|
+
|
|
124
|
+
Runtime getter failures, scalar access failures and deferred projected-array `length` / element failures are enriched with the currently bound source filename, record number, parser-specific reason and physical record rendering. The runtime also retains parent/member/index links for borrowed views, allowing an application-level path such as `orders[1].items[2].sku` to be constructed **only on the exceptional path**. This complements the compiled COBOL declaration/controller path in the underlying reason. An error raised before a selection is made cannot report unknown indices.
|
|
125
|
+
|
|
126
|
+
Diagnostics should be exercised after record reuse: the error must refer to the currently bound source record, not the previous lease. Successful scalar reads do not build error-path strings.
|
|
127
|
+
|
|
128
|
+
## Tests and boundaries
|
|
129
|
+
|
|
130
|
+
Relevant tests are in `src/projection.definition.spec.ts`, `src/create.projection.spec.ts`, `src/nested.projection.type-tests.ts` and `src/create.projection.reuse.spec.ts`, `src/create.projection.array.diagnostics.spec.ts`; parser packages also exercise this factory through real file cursors. Run the package's normal Jest script and TypeScript compilation separately.
|
|
131
|
+
|
|
132
|
+
The generic factory intentionally does not parse COBOL, infer business semantics, copy record payloads or resolve ODO controllers. The COBOL adapter now supports nested ODO through parser-owned record-scoped position caches; those details do not affect the generic projection contract.
|
|
133
|
+
|
|
134
|
+
## Rebinding without transient selections
|
|
135
|
+
|
|
136
|
+
`createProjection` checks cached bound object and array views before asking the
|
|
137
|
+
parser to reselect them. When a projected hierarchy is rebound to a later record,
|
|
138
|
+
its parser receives the previously retained selected representation or array
|
|
139
|
+
source as an optional reuse parameter. Parsers not supporting reuse ignore that
|
|
140
|
+
parameter. The COBOL parser uses it to update already-retained selections and
|
|
141
|
+
provider state without constructing per-lease array closures or index arrays.
|
|
142
|
+
|
|
143
|
+
Projected arrays keep their sparse indexed high-water child slots, plus an
|
|
144
|
+
`ArrayWithSize` prefix of the child views *bound during the current lease*.
|
|
145
|
+
Clearing a runtime visits this active prefix rather than traversing the full
|
|
146
|
+
historical sparse array. Previously visited children retain their allocation.
|
|
147
|
+
Successful scalar reads do not format access paths; errors can include a full
|
|
148
|
+
indexed path built from the retained parent/member/index links.
|
|
149
|
+
|
|
150
|
+
## Optional operation metrics
|
|
151
|
+
|
|
152
|
+
`createProjection(parser, details, context, definition, metrics?)` accepts an optional `IPutMetrics`; the public four-argument call remains valid. Instrumentation counts compiled member paths, constructed views, binds, clears, projected-array property accesses, child allocations versus cache reuse, child binds versus already-bound hits, and the actual number of array runtimes, child projections, and nested objects traversed at cleanup. The default is `NullMetrics`. The projected-array Proxy receives the same instance to separate property, length and indexed-access work. The counters do not modify selection or high-water retention semantics.
|
|
153
|
+
|
|
154
|
+
## Child clearing across leases
|
|
155
|
+
|
|
156
|
+
For the **lazy Proxy implementation**, a record close recursively clears the active projected-array children but retains
|
|
157
|
+
allocated child slots for future records. On a later selection, a retained child
|
|
158
|
+
whose physical-record binding is already `undefined` is rebound directly; it is
|
|
159
|
+
not recursively cleared a second time. A child that is still bound is cleared
|
|
160
|
+
before rebinding, preserving nested-array and borrowed-record lifetimes.
|
|
161
|
+
|
|
162
|
+
For the older lazy Proxy two-element benchmark, this reduces `projection.view.clear` from
|
|
163
|
+
five calls to three per record (root plus the two selected children). In that lazy path, the
|
|
164
|
+
`array.child.clear_visit` counter remains two per record: those are the actual
|
|
165
|
+
children visited on close. No change is made to Proxy selection or field layout.
|
|
166
|
+
|
|
167
|
+
### Small fixed arrays
|
|
168
|
+
|
|
169
|
+
When the parser provides immutable fixed-position occurrence descriptors, a
|
|
170
|
+
scalar-only array of at most eight elements uses a frozen native JavaScript
|
|
171
|
+
array of reusable views. Its child views share the array runtime's current
|
|
172
|
+
record lease rather than each being rebound. The original record bytes remain
|
|
173
|
+
authoritative and scalar values are decoded on demand. Other array shapes use
|
|
174
|
+
lazy Proxy selection. Holding a child after the owning record closes is invalid.
|
|
175
|
+
|
|
176
|
+
## Fixed-position shared binding (current default)
|
|
177
|
+
|
|
178
|
+
For COBOL providers that advertise a supported small fixed array, the array getter now returns a real JavaScript array of retained scalar child projection views. This is a separate representation from the Proxy described above: it constructs eligible child views once and keeps their occurrence geometry immutable. A record lease updates a shared physical-record binding, rather than individually rebinding each array element. On close, clearing the shared binding makes previously retained child accessors invalid. No COBOL field values are copied or decoded at bind time.
|
|
179
|
+
|
|
180
|
+
The current eligibility guard is fixed-position geometry, scalar-only children and at most eight occurrences. A fixed count positioned after symbolic variable-length storage is not automatically eligible. Nested and ODO-dependent arrays continue down the lazy Proxy path. The older discussion of high-water child slots above applies specifically to that **lazy** path; the native array retains all eligible view slots from construction. Both paths implement the same typed projection intent, but native-array operations and internal cache sizes are not interchangeable diagnostic contracts. Tests covering independent leases and stale child access belong with this module and the COBOL integration tests.
|
|
181
|
+
|
|
182
|
+
### Experimental shared-binding ODO arrays
|
|
183
|
+
|
|
184
|
+
The generic projection runtime also recognises an **opt-in variable-count shared source**. It constructs a fixed set of retained scalar child views once and exposes a native JavaScript array whose visible prefix follows the active ODO count. Binding updates one shared lease (physical record and active count); it does not bind every child. A saved child validates its occurrence index against the current count before decoding any bytes, including after a record is rebound with fewer occurrences. Closing the lease invalidates all children without walking them.
|
|
185
|
+
|
|
186
|
+
The visible array is resized during a lease change and is **not frozen** (unlike the fixed-count shared array). Treat it as a borrowed `ReadonlyArray`, not as mutable application-owned storage. A retained reference to the array is not a snapshot: its contents reflect the current lease. This experimental representation is selected only when the COBOL provider explicitly advertises stable, small ODO geometry; other arrays use the existing lazy Proxy.
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./src/projection.definition"), exports);
|
|
18
|
+
__exportStar(require("./src/create.projection"), exports);
|
|
19
|
+
//# sourceMappingURL=index.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,64 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cobol-ts/cursor-projection",
|
|
3
|
-
"
|
|
4
|
-
"
|
|
5
|
-
"
|
|
3
|
+
"description": "Defines declarative projections and projection support contracts for byte-backed cursor representations",
|
|
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/arrays": "0.0.1",
|
|
21
|
+
"@cobol-ts/cursor-loader-types": "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
|
+
],
|
|
54
|
+
"keywords": [
|
|
55
|
+
"indexing"
|
|
56
|
+
],
|
|
57
|
+
"license": "MIT",
|
|
58
|
+
"repository": "git@github.com:intellimaintain/intellimaintain.git",
|
|
59
|
+
"scripts": {
|
|
60
|
+
"test": "jest --config jest.config.json --env node",
|
|
61
|
+
"test:watch": "npm run test -- --watch",
|
|
62
|
+
"start": "react-scripts start"
|
|
63
|
+
}
|
|
6
64
|
}
|