@ontology-kit/reader 0.2.0-bootstrap.0 → 0.3.0-alpha.210
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/dist/lock.d.ts +9 -0
- package/dist/lock.js +106 -32
- package/dist/model.d.ts +20 -3
- package/dist/model.js +70 -6
- package/package.json +2 -2
package/dist/lock.d.ts
CHANGED
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
export declare const LOCK_FILE = ".okb.lock";
|
|
2
|
+
/**
|
|
3
|
+
* Test seam (internal: not exported from the package index). `at` is called at the named steps of a takeover so a test
|
|
4
|
+
* can run other contenders in between; `now` and `sleep` let it run without real waiting.
|
|
5
|
+
*/
|
|
6
|
+
export declare const testSeam: {
|
|
7
|
+
at: (_step: string) => void;
|
|
8
|
+
now: () => number;
|
|
9
|
+
sleep: (ms: number) => void;
|
|
10
|
+
};
|
|
2
11
|
/** Whether a command holds the lock right now. */
|
|
3
12
|
export declare function isLocked(root: string): boolean;
|
|
4
13
|
/** Wait (up to a few seconds) until no command holds the lock. Used by readers, who don't take it. */
|
package/dist/lock.js
CHANGED
|
@@ -3,17 +3,34 @@
|
|
|
3
3
|
* command loads the whole folder and saves it back). The lock is the file `.okb.lock`, created
|
|
4
4
|
* exclusively and holding the owner's process id. Part of @ontology-kit/reader.
|
|
5
5
|
*/
|
|
6
|
-
import { randomBytes } from "node:crypto";
|
|
7
|
-
import { existsSync,
|
|
6
|
+
import { createHash, randomBytes } from "node:crypto";
|
|
7
|
+
import { existsSync, readFileSync, statSync, unlinkSync, writeFileSync } from "node:fs";
|
|
8
8
|
import { hostname } from "node:os";
|
|
9
|
-
import { join } from "node:path";
|
|
9
|
+
import { basename, join } from "node:path";
|
|
10
10
|
import { OkbError } from "./errors.js";
|
|
11
11
|
export const LOCK_FILE = ".okb.lock";
|
|
12
12
|
const WAIT_MS = 10_000;
|
|
13
13
|
/** A lock this old is stale whatever its owner (another machine on a shared folder, a reused process id). */
|
|
14
14
|
const MAX_AGE_MS = 10 * 60_000;
|
|
15
|
+
/**
|
|
16
|
+
* A claim is held for milliseconds (one read, one check, one delete), so one this old is abandoned whoever owns it: the
|
|
17
|
+
* empty file a crash leaves between its creation and its write, one that can't be parsed, or one from another machine.
|
|
18
|
+
* Only a claim whose owner is a live process on this machine is never old enough.
|
|
19
|
+
*/
|
|
20
|
+
const CLAIM_MAX_AGE_MS = 30_000;
|
|
21
|
+
/** How many abandoned claims on one stale lock a contender steps over before it gives up and waits. */
|
|
22
|
+
const MAX_CLAIMS = 20;
|
|
23
|
+
/**
|
|
24
|
+
* Test seam (internal: not exported from the package index). `at` is called at the named steps of a takeover so a test
|
|
25
|
+
* can run other contenders in between; `now` and `sleep` let it run without real waiting.
|
|
26
|
+
*/
|
|
27
|
+
export const testSeam = {
|
|
28
|
+
at: (_step) => { },
|
|
29
|
+
now: () => Date.now(),
|
|
30
|
+
sleep: (ms) => { Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms); },
|
|
31
|
+
};
|
|
15
32
|
function sleep(ms) {
|
|
16
|
-
|
|
33
|
+
testSeam.sleep(ms);
|
|
17
34
|
}
|
|
18
35
|
/** What the lock file says. `unreadable` is a read that failed for a reason other than the file being gone (EACCES, EIO). */
|
|
19
36
|
function readOwner(path) {
|
|
@@ -42,9 +59,10 @@ function alive(pid) {
|
|
|
42
59
|
/**
|
|
43
60
|
* The lock's owner is gone: a crashed command (same machine, process not running), or, when the owner can't be
|
|
44
61
|
* checked (another machine on a shared folder, a lock no one can read), a lock older than ten minutes.
|
|
45
|
-
* A command still running on this machine keeps its lock however long it takes.
|
|
62
|
+
* A command still running on this machine keeps its lock however long it takes. `maxAgeMs` is the age that counts as
|
|
63
|
+
* stale when the owner can't be checked (ten minutes for a lock, a few seconds' worth of work for a claim).
|
|
46
64
|
*/
|
|
47
|
-
function isStale(path) {
|
|
65
|
+
function isStale(path, maxAgeMs = MAX_AGE_MS) {
|
|
48
66
|
const { owner, unreadable } = readOwner(path);
|
|
49
67
|
if (unreadable)
|
|
50
68
|
return false; // not knowing who holds it is no proof that nobody does
|
|
@@ -52,7 +70,7 @@ function isStale(path) {
|
|
|
52
70
|
if (mine)
|
|
53
71
|
return !alive(owner.pid);
|
|
54
72
|
try {
|
|
55
|
-
return
|
|
73
|
+
return testSeam.now() - statSync(path).mtimeMs > maxAgeMs;
|
|
56
74
|
}
|
|
57
75
|
catch {
|
|
58
76
|
return false; // released while we looked
|
|
@@ -64,35 +82,90 @@ export function isLocked(root) {
|
|
|
64
82
|
return existsSync(path) && !isStale(path);
|
|
65
83
|
}
|
|
66
84
|
/**
|
|
67
|
-
*
|
|
68
|
-
*
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
85
|
+
* Take over the stale lock at `path`, whose contents were `seen`. Returns whether the stale lock is gone.
|
|
86
|
+
*
|
|
87
|
+
* A file can't be deleted "only if it is still this one": `unlink` and `rename` take whatever is at the path now. So
|
|
88
|
+
* the takeover is made exclusive instead. The contender first creates, with `wx`, a claim file named after the stale
|
|
89
|
+
* lock's contents (`.okb.lock.claim-<hash of seen>-<n>`). Only one contender can hold the claim for a given stale
|
|
90
|
+
* lock. The holder reads the lock again and deletes it only if it still holds exactly `seen` and is still stale.
|
|
91
|
+
* Nothing else can replace a stale lock in that window: a create (`wx`) fails while the file exists, a dead
|
|
92
|
+
* owner can't release it, and every other contender is shut out by the claim. So the delete can never hit another
|
|
93
|
+
* command's fresh lock, and no moment exists with the spot empty and a lock being put back. The holder never creates
|
|
94
|
+
* the real lock here; the caller does, with the same `wx` as everyone else, so exactly one contender wins it.
|
|
95
|
+
*
|
|
96
|
+
* A claim can be abandoned by a crash. It is judged like a lock (owner gone on this machine; a live local owner is never
|
|
97
|
+
* stale; one that can't be read is never stale), except that when the owner can't be checked (an empty or garbled
|
|
98
|
+
* claim, another machine) it counts as abandoned after 30 seconds instead of ten minutes, since a claim is only held for
|
|
99
|
+
* milliseconds. A claim is never deleted by someone else, since that would be the same race again. A contender that
|
|
100
|
+
* finds an abandoned claim moves on to the next number (`-1`, `-2`, ...), which only one contender can create. The
|
|
101
|
+
* winner removes its own claim and the ones it superseded once the stale lock is gone, and a lock token never comes
|
|
102
|
+
* back, so a claim for it can't matter again. Every early exit below happens while the stale lock still exists, so the
|
|
103
|
+
* superseded claims stay: deleting one would free its number for a new contender who could then claim the same stale
|
|
104
|
+
* lock beside the live claim holder. If all `MAX_CLAIMS` numbers are abandoned, the takeover stops with an error that
|
|
105
|
+
* names the files, so the user can delete them.
|
|
106
|
+
*
|
|
107
|
+
* What remains: a lock judged stale by age alone (another machine on a shared folder, or an owner no one can check) has
|
|
108
|
+
* an owner that may still be running. If that owner releases its lock in the instant between the check and the delete,
|
|
109
|
+
* and a third command creates a new lock in that instant, the delete removes the new lock. The same goes for two
|
|
110
|
+
* contenders who both judge a claim abandoned by age (30 seconds) while its holder, on another machine or with a
|
|
111
|
+
* garbled record, is merely very slow: both then claim the next number. That needs a holder stalled for over 30
|
|
112
|
+
* seconds in a millisecond step, or an owner on another machine silent for ten minutes that then acts within
|
|
113
|
+
* microseconds of the takeover; a plain filesystem has no compare-and-delete to close that. For a command on this
|
|
114
|
+
* machine, the owner's process id decides, and the race is closed.
|
|
72
115
|
*/
|
|
73
|
-
function
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
116
|
+
function takeOver(path, seen, me) {
|
|
117
|
+
const id = createHash("sha1").update(seen).digest("hex").slice(0, 16);
|
|
118
|
+
const superseded = [];
|
|
119
|
+
let claim;
|
|
120
|
+
for (let n = 0; n < MAX_CLAIMS && !claim; n++) {
|
|
121
|
+
const candidate = `${path}.claim-${id}-${n}`;
|
|
122
|
+
try {
|
|
123
|
+
writeFileSync(candidate, JSON.stringify(me), { flag: "wx" });
|
|
124
|
+
claim = candidate;
|
|
125
|
+
}
|
|
126
|
+
catch (e) {
|
|
127
|
+
if (e.code !== "EEXIST")
|
|
128
|
+
throw e;
|
|
129
|
+
if (!isStale(candidate, CLAIM_MAX_AGE_MS))
|
|
130
|
+
return false; // another contender is taking this lock over right now
|
|
131
|
+
superseded.push(candidate);
|
|
132
|
+
}
|
|
77
133
|
}
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
throw new OkbError(`Can't remove the stale lock ${path}: ${e.message}. If no okb command is running, delete it yourself.`);
|
|
134
|
+
if (!claim) {
|
|
135
|
+
throw new OkbError(`Can't take over the stale lock ${path}: ${superseded.length} leftover claim files block it (${superseded.map((f) => basename(f)).join(", ")}). ` +
|
|
136
|
+
"If no okb command is running, delete them.");
|
|
82
137
|
}
|
|
83
|
-
const moved = readOwner(aside);
|
|
84
138
|
try {
|
|
85
|
-
|
|
86
|
-
|
|
139
|
+
testSeam.at("claimed");
|
|
140
|
+
const now = readOwner(path);
|
|
141
|
+
if (now.unreadable)
|
|
142
|
+
return false;
|
|
143
|
+
if (now.raw === seen && isStale(path)) {
|
|
144
|
+
testSeam.at("verified");
|
|
87
145
|
try {
|
|
88
|
-
|
|
146
|
+
unlinkSync(path);
|
|
147
|
+
}
|
|
148
|
+
catch (e) {
|
|
149
|
+
if (e.code !== "ENOENT") {
|
|
150
|
+
throw new OkbError(`Can't remove the stale lock ${path}: ${e.message}. If no okb command is running, delete it yourself.`);
|
|
151
|
+
}
|
|
89
152
|
}
|
|
90
|
-
|
|
153
|
+
testSeam.at("unlinked");
|
|
91
154
|
}
|
|
155
|
+
else if (existsSync(path) && now.raw === seen) {
|
|
156
|
+
return false; // not stale after all
|
|
157
|
+
}
|
|
158
|
+
// The stale lock is gone (by us, or replaced by a newer one). Claims named after it are done.
|
|
159
|
+
for (const old of superseded)
|
|
160
|
+
try {
|
|
161
|
+
unlinkSync(old);
|
|
162
|
+
}
|
|
163
|
+
catch { /* gone */ }
|
|
164
|
+
return true;
|
|
92
165
|
}
|
|
93
166
|
finally {
|
|
94
167
|
try {
|
|
95
|
-
unlinkSync(
|
|
168
|
+
unlinkSync(claim);
|
|
96
169
|
}
|
|
97
170
|
catch { /* gone */ }
|
|
98
171
|
}
|
|
@@ -100,8 +173,8 @@ function removeStale(path, seen, token) {
|
|
|
100
173
|
/** Wait (up to a few seconds) until no command holds the lock. Used by readers, who don't take it. */
|
|
101
174
|
export function waitUnlocked(root, ms = 2000) {
|
|
102
175
|
const path = join(root, LOCK_FILE);
|
|
103
|
-
const end =
|
|
104
|
-
while (existsSync(path) && !isStale(path) &&
|
|
176
|
+
const end = testSeam.now() + ms;
|
|
177
|
+
while (existsSync(path) && !isStale(path) && testSeam.now() < end)
|
|
105
178
|
sleep(25);
|
|
106
179
|
}
|
|
107
180
|
/** Take the folder's lock, waiting briefly if another command has it. Returns the function that releases it. */
|
|
@@ -109,8 +182,9 @@ export function acquireLock(root, waitMs = Number(process.env.OKB_LOCK_WAIT_MS)
|
|
|
109
182
|
const path = join(root, LOCK_FILE);
|
|
110
183
|
const token = randomBytes(8).toString("hex");
|
|
111
184
|
const mine = { pid: process.pid, host: hostname(), token };
|
|
112
|
-
const end =
|
|
185
|
+
const end = testSeam.now() + waitMs;
|
|
113
186
|
for (;;) {
|
|
187
|
+
testSeam.at("create");
|
|
114
188
|
try {
|
|
115
189
|
writeFileSync(path, JSON.stringify(mine), { flag: "wx" });
|
|
116
190
|
return () => {
|
|
@@ -130,11 +204,11 @@ export function acquireLock(root, waitMs = Number(process.env.OKB_LOCK_WAIT_MS)
|
|
|
130
204
|
// Read the lock before judging it, so the takeover can tell it from a fresh one that took its place.
|
|
131
205
|
const seen = readOwner(path).raw;
|
|
132
206
|
if (isStale(path)) {
|
|
133
|
-
|
|
134
|
-
if (
|
|
207
|
+
testSeam.at("judged");
|
|
208
|
+
if (takeOver(path, seen, mine) && testSeam.now() < end)
|
|
135
209
|
continue;
|
|
136
210
|
}
|
|
137
|
-
if (
|
|
211
|
+
if (testSeam.now() >= end) {
|
|
138
212
|
const { owner } = readOwner(path);
|
|
139
213
|
throw new OkbError(`Another okb command${owner?.pid ? ` (process ${owner.pid})` : ""} has been writing in ${root} for more than ${Math.max(1, Math.round(waitMs / 1000))} second(s). ` +
|
|
140
214
|
`Try again in a moment. If no okb command is running, delete ${path}.`);
|
package/dist/model.d.ts
CHANGED
|
@@ -19,6 +19,15 @@ export declare class Ontology {
|
|
|
19
19
|
readonly rel: Relationships;
|
|
20
20
|
private outIdx;
|
|
21
21
|
private incIdx;
|
|
22
|
+
/**
|
|
23
|
+
* Name index: `naming.key()` of each name-like field (see lookupNames) -> the nodes with that key, in node order.
|
|
24
|
+
* `find()` reads it instead of scanning every node. Kept current by reindex (load, removal), addNode and touch
|
|
25
|
+
* (every op that changes a node's name calls touch, see `rename`).
|
|
26
|
+
*/
|
|
27
|
+
private nameIdx;
|
|
28
|
+
/** The keys each node is indexed under, and its position in node order (buckets are kept sorted by it). */
|
|
29
|
+
private nameKeys;
|
|
30
|
+
private nextSeq;
|
|
22
31
|
constructor(root: string, manifest: Manifest, nodes: readonly OkbNode[], edges: readonly GraphEdge[]);
|
|
23
32
|
get nodes(): readonly OkbNode[];
|
|
24
33
|
get edges(): readonly GraphEdge[];
|
|
@@ -26,6 +35,12 @@ export declare class Ontology {
|
|
|
26
35
|
private static rootOf;
|
|
27
36
|
private static read;
|
|
28
37
|
static fromData(nodes: readonly OkbNode[], edges: readonly GraphEdge[], manifest?: Partial<Manifest>): Ontology;
|
|
38
|
+
/** Index a node under the keys of its current names, as the last in node order. */
|
|
39
|
+
private indexName;
|
|
40
|
+
/** A node's name changed in place: move it to the keys of its new names, keeping buckets in node order. */
|
|
41
|
+
private reindexName;
|
|
42
|
+
/** The nodes (in node order) any of whose names has this key. */
|
|
43
|
+
private byKey;
|
|
29
44
|
private indexEdge;
|
|
30
45
|
/** The Ontology node (scope, conventions). A stand-in if the file lacks one; struct-well-formed reports that. */
|
|
31
46
|
get meta(): OntologyNode;
|
|
@@ -57,9 +72,11 @@ export declare class Ontology {
|
|
|
57
72
|
ancestors(id: string): Set<string>;
|
|
58
73
|
descendants(id: string): Set<string>;
|
|
59
74
|
/**
|
|
60
|
-
* Everything reachable with `next`,
|
|
61
|
-
*
|
|
62
|
-
*
|
|
75
|
+
* Everything reachable with `next`, breadth-first, so nearer classes come before farther ones. Each
|
|
76
|
+
* class's own parents (or children) are visited in id order. The order is deterministic, so it is the same
|
|
77
|
+
* in memory and after a save and reload, but it is not sorted by id across all the classes at one distance:
|
|
78
|
+
* for a class whose parents are `b` and `c`, where `b` has the parent `z` and `c` has the parent `a`,
|
|
79
|
+
* the walk gives `b, c, z, a`. Cycle-safe.
|
|
63
80
|
*/
|
|
64
81
|
private walk;
|
|
65
82
|
/** id plus all its ancestors. */
|
package/dist/model.js
CHANGED
|
@@ -59,7 +59,8 @@ export function nodeLabel(n) {
|
|
|
59
59
|
}
|
|
60
60
|
/** Every name-like field a node can be looked up by (a quote's text counts: its id has a random tag). */
|
|
61
61
|
function lookupNames(n) {
|
|
62
|
-
|
|
62
|
+
// A hand-edited file can hold a number or object here; those can't be looked up by name, and must not reach key().
|
|
63
|
+
return ["name", "text", "title", "quote"].map((f) => n[f]).filter((v) => typeof v === "string" && v !== "");
|
|
63
64
|
}
|
|
64
65
|
export { isRelationship };
|
|
65
66
|
export function findRoot(start) {
|
|
@@ -87,6 +88,15 @@ export class Ontology {
|
|
|
87
88
|
rel = new Relationships(this);
|
|
88
89
|
outIdx = new Map();
|
|
89
90
|
incIdx = new Map();
|
|
91
|
+
/**
|
|
92
|
+
* Name index: `naming.key()` of each name-like field (see lookupNames) -> the nodes with that key, in node order.
|
|
93
|
+
* `find()` reads it instead of scanning every node. Kept current by reindex (load, removal), addNode and touch
|
|
94
|
+
* (every op that changes a node's name calls touch, see `rename`).
|
|
95
|
+
*/
|
|
96
|
+
nameIdx = new Map();
|
|
97
|
+
/** The keys each node is indexed under, and its position in node order (buckets are kept sorted by it). */
|
|
98
|
+
nameKeys = new Map();
|
|
99
|
+
nextSeq = 0;
|
|
90
100
|
constructor(root, manifest, nodes, edges) {
|
|
91
101
|
this.root = root;
|
|
92
102
|
this.manifest = manifest;
|
|
@@ -186,6 +196,56 @@ export class Ontology {
|
|
|
186
196
|
this.incIdx = new Map();
|
|
187
197
|
for (const e of this.edges)
|
|
188
198
|
this.indexEdge(e);
|
|
199
|
+
this.nameIdx = new Map();
|
|
200
|
+
this.nameKeys = new Map();
|
|
201
|
+
this.nextSeq = 0;
|
|
202
|
+
for (const n of this.nodes)
|
|
203
|
+
this.indexName(n);
|
|
204
|
+
}
|
|
205
|
+
/** Index a node under the keys of its current names, as the last in node order. */
|
|
206
|
+
indexName(n) {
|
|
207
|
+
const keys = [...new Set(lookupNames(n).map(naming.key).filter(Boolean))];
|
|
208
|
+
this.nameKeys.set(n, { keys, seq: this.nextSeq++ });
|
|
209
|
+
for (const k of keys) {
|
|
210
|
+
const bucket = this.nameIdx.get(k);
|
|
211
|
+
if (bucket)
|
|
212
|
+
bucket.push(n);
|
|
213
|
+
else
|
|
214
|
+
this.nameIdx.set(k, [n]);
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
/** A node's name changed in place: move it to the keys of its new names, keeping buckets in node order. */
|
|
218
|
+
reindexName(n) {
|
|
219
|
+
const entry = this.nameKeys.get(n);
|
|
220
|
+
if (!entry)
|
|
221
|
+
return; // not (or no longer) one of this ontology's nodes
|
|
222
|
+
const keys = [...new Set(lookupNames(n).map(naming.key).filter(Boolean))];
|
|
223
|
+
for (const k of entry.keys) {
|
|
224
|
+
if (keys.includes(k))
|
|
225
|
+
continue;
|
|
226
|
+
const bucket = this.nameIdx.get(k);
|
|
227
|
+
const at = bucket?.indexOf(n) ?? -1;
|
|
228
|
+
if (bucket && at >= 0)
|
|
229
|
+
bucket.splice(at, 1);
|
|
230
|
+
if (bucket && !bucket.length)
|
|
231
|
+
this.nameIdx.delete(k);
|
|
232
|
+
}
|
|
233
|
+
for (const k of keys) {
|
|
234
|
+
if (entry.keys.includes(k))
|
|
235
|
+
continue;
|
|
236
|
+
const bucket = this.nameIdx.get(k);
|
|
237
|
+
if (!bucket)
|
|
238
|
+
this.nameIdx.set(k, [n]);
|
|
239
|
+
else {
|
|
240
|
+
bucket.push(n);
|
|
241
|
+
bucket.sort((a, b) => (this.nameKeys.get(a)?.seq ?? 0) - (this.nameKeys.get(b)?.seq ?? 0));
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
entry.keys = keys;
|
|
245
|
+
}
|
|
246
|
+
/** The nodes (in node order) any of whose names has this key. */
|
|
247
|
+
byKey(k) {
|
|
248
|
+
return k ? this.nameIdx.get(k) ?? [] : [];
|
|
189
249
|
}
|
|
190
250
|
indexEdge(e) {
|
|
191
251
|
const ko = `${e.from}\u0000${e.type}`;
|
|
@@ -257,7 +317,7 @@ export class Ontology {
|
|
|
257
317
|
if (direct && ok(direct))
|
|
258
318
|
return direct;
|
|
259
319
|
const k = naming.key(ref);
|
|
260
|
-
let hits = this.
|
|
320
|
+
let hits = this.byKey(k).filter(ok);
|
|
261
321
|
if (hits.length === 0 && /^\d+$/.test(ref) && ts) {
|
|
262
322
|
// A bare number is a position in `okb cq list` / `okb decision list` (display order; ids carry no number),
|
|
263
323
|
// or, for ids made before okb-lpg/3, the number inside `cq.3` / `d.3`.
|
|
@@ -290,7 +350,7 @@ export class Ontology {
|
|
|
290
350
|
const what = ts ? ts.join(" or ") : "node";
|
|
291
351
|
if (ts) {
|
|
292
352
|
const wanted = ts;
|
|
293
|
-
const other = this.
|
|
353
|
+
const other = this.byKey(k).find((n) => !wanted.includes(n.type) && lookupNames(n).slice(0, 2).some((x) => naming.key(x) === k));
|
|
294
354
|
if (other) {
|
|
295
355
|
const extra = other.type === "Instance" && ts.includes("Class")
|
|
296
356
|
? " Instances can't have subclasses or be used as a class (rule inst-are-leaves); if you need that, make it a class."
|
|
@@ -379,6 +439,7 @@ export class Ontology {
|
|
|
379
439
|
// Never before createdAt: a question or decision added in the same second as another was stamped a second later.
|
|
380
440
|
const now = this.opTime ?? timestamp();
|
|
381
441
|
node.updatedAt = node.createdAt && node.createdAt > now ? node.createdAt : now;
|
|
442
|
+
this.reindexName(node);
|
|
382
443
|
}
|
|
383
444
|
/** @internal */
|
|
384
445
|
addNode(node) {
|
|
@@ -396,6 +457,7 @@ export class Ontology {
|
|
|
396
457
|
node.updatedAt = now;
|
|
397
458
|
this._nodes.push(node);
|
|
398
459
|
this.byId.set(node.id, node);
|
|
460
|
+
this.indexName(node);
|
|
399
461
|
return node;
|
|
400
462
|
}
|
|
401
463
|
/** @internal */
|
|
@@ -460,9 +522,11 @@ export class Ontology {
|
|
|
460
522
|
return this.walk(id, (x) => this.children(x));
|
|
461
523
|
}
|
|
462
524
|
/**
|
|
463
|
-
* Everything reachable with `next`,
|
|
464
|
-
*
|
|
465
|
-
*
|
|
525
|
+
* Everything reachable with `next`, breadth-first, so nearer classes come before farther ones. Each
|
|
526
|
+
* class's own parents (or children) are visited in id order. The order is deterministic, so it is the same
|
|
527
|
+
* in memory and after a save and reload, but it is not sorted by id across all the classes at one distance:
|
|
528
|
+
* for a class whose parents are `b` and `c`, where `b` has the parent `z` and `c` has the parent `a`,
|
|
529
|
+
* the walk gives `b, c, z, a`. Cycle-safe.
|
|
466
530
|
*/
|
|
467
531
|
walk(id, next) {
|
|
468
532
|
const seen = new Set();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ontology-kit/reader",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0-alpha.210",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Read the okb file format: an ontology folder's typed nodes and edges, loading with migrations, the reserved edge types and relationship values. No CLI, no meta-KB.",
|
|
6
6
|
"homepage": "https://ontology-kit.dev",
|
|
@@ -41,7 +41,7 @@
|
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
43
|
"@types/node": "^26.6.6",
|
|
44
|
-
"typescript": "^
|
|
44
|
+
"typescript": "^7.0.2"
|
|
45
45
|
},
|
|
46
46
|
"scripts": {
|
|
47
47
|
"typecheck": "tsc",
|