@linked.archi/kernel 0.1.5 → 0.1.6

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.
@@ -23,13 +23,16 @@
23
23
  *
24
24
  * The Oxigraph copy is made only when the shapes have SPARQL-based
25
25
  * constraints, and only where Oxigraph is ready (its WebAssembly loaded in the
26
- * worker): about 0.7 KB a quad (78 MB at 100 000 triples, measured in Node).
26
+ * worker): about 0.7 KB a quad (78 MB at 100 000 triples, measured in Node). It
27
+ * is built in one native load, and built again rather than followed for a
28
+ * change too large to validate incrementally — a model opened (KD-29).
27
29
  */
28
30
  import { DataFactory, Store as N3Store, termFromId, termToId } from 'n3';
29
31
  import * as oxigraph from 'oxigraph';
30
32
  import type { Quad, Term } from '@rdfjs/types';
31
33
  import { RdfShaclValidator } from './shacl-validator.js';
32
34
  import { isOxigraphReady, toOxQuad } from './rdf-store.js';
35
+ import { nTriples, splitForNativeLoad } from './native-load.js';
33
36
  import { SparqlRunner, predicatesNamedBy, sparqlConstraintsOf, type ShapeTargets } from './sparql-constraints.js';
34
37
  import { REFERENCE_PREDICATES, missingReferences } from '../model-manager/missing-references.js';
35
38
  import { predicatesRead, shapesReach, type ShapesReach } from './shape-reach.js';
@@ -91,9 +94,23 @@ export interface PlainReport {
91
94
  * the references; and how many nodes were validated again — `all`, or after
92
95
  * an edit only those it can change.
93
96
  */
94
- timing: { shacl: number; tables: number; sparql?: number; references?: number; nodes: number | 'all' };
97
+ timing: {
98
+ shacl: number;
99
+ tables: number;
100
+ sparql?: number;
101
+ references?: number;
102
+ nodes: number | 'all';
103
+ /** The SPARQL-based constraints that took {@link SLOW_RULE_MS} or more, by shape, slowest first (KD-30). */
104
+ slowRules?: Array<{ shape: string; ms: number }>;
105
+ };
95
106
  }
96
107
 
108
+ /**
109
+ * A SPARQL-based constraint this slow, in ms, is named in the report's timing (KD-30): two published rules once took
110
+ * 111 of a validation's 120 s, and the report said only "SPARQL-based rules 110 901 ms".
111
+ */
112
+ export const SLOW_RULE_MS = 500;
113
+
97
114
  /** After this many validations of what edits changed, the whole model is validated again. */
98
115
  const FULL_EVERY = 100;
99
116
  /** When an edit can change the findings of more nodes than one in this many of the model's quads, the whole model is validated. */
@@ -253,24 +270,47 @@ export class ValidationEngine {
253
270
  this.changed.all = true;
254
271
  }
255
272
 
256
- /** Apply a change to the model. */
273
+ /**
274
+ * Apply a change to the model. One past the incremental limit — a model opened, or imported, is one — would have
275
+ * the whole model validated anyway, and is applied as a reload (KD-29): the copy and the references as ever, but
276
+ * nothing noted of what changed, and the Oxigraph copy built again at the end in one native load rather than
277
+ * followed quad by quad (55 s and 570 MiB for 365 000 triples, against 2.1 s and 402 MiB, in Node).
278
+ */
257
279
  apply(added: readonly EncodedQuad[], removed: readonly EncodedQuad[]): void {
258
- for (const q of decodeQuads(removed)) {
259
- const k = extraKey(q);
260
- if (k !== undefined && this.extras.has(k)) this.alsoModel.delete(k);
261
- else this.drop(q);
262
- this.refer(q, -1);
263
- this.touch(q);
280
+ const reload = added.length + removed.length > this.limit;
281
+ if (reload) {
282
+ this.changed.all = true;
283
+ this.rebuilding = true;
264
284
  }
265
- for (const q of decodeQuads(added)) {
266
- const k = extraKey(q);
267
- if (k !== undefined && this.extras.has(k)) this.alsoModel.add(k);
268
- else this.put(q);
269
- this.refer(q, 1);
270
- this.touch(q);
285
+ try {
286
+ for (const q of decodeQuads(removed)) {
287
+ const k = extraKey(q);
288
+ if (k !== undefined && this.extras.has(k)) this.alsoModel.delete(k);
289
+ else this.drop(q);
290
+ this.refer(q, -1);
291
+ this.touch(q);
292
+ }
293
+ for (const q of decodeQuads(added)) {
294
+ const k = extraKey(q);
295
+ if (k !== undefined && this.extras.has(k)) this.alsoModel.add(k);
296
+ else this.put(q);
297
+ this.refer(q, 1);
298
+ this.touch(q);
299
+ }
300
+ } finally {
301
+ if (reload) {
302
+ this.rebuilding = false;
303
+ this.rebuildSparqlData();
304
+ }
271
305
  }
272
306
  }
273
307
 
308
+ /** Free the Oxigraph copy: WebAssembly memory is not collected with the engine. */
309
+ dispose(): void {
310
+ (this.sparqlData as { free?(): void } | undefined)?.free?.();
311
+ this.sparqlData = undefined;
312
+ }
313
+
274
314
  /** How many quads the model has. */
275
315
  get size(): number {
276
316
  return this.data.size - this.extras.size + this.alsoModel.size;
@@ -311,7 +351,16 @@ export class ValidationEngine {
311
351
  const t4 = now();
312
352
  this.findings = { shacl: byFocus(shacl), tables: byFocus(byTables), sparql: byFocus(sparql), references: byFocus(references) };
313
353
  this.sinceWhole = 0;
314
- return { shacl: Math.round(t1 - t0), tables: Math.round(t2 - t1), sparql: Math.round(t3 - t2), references: Math.round(t4 - t3), nodes: 'all' };
354
+ return { shacl: Math.round(t1 - t0), tables: Math.round(t2 - t1), sparql: Math.round(t3 - t2), references: Math.round(t4 - t3), nodes: 'all', ...this.slowRules() };
355
+ }
356
+
357
+ /** The SPARQL-based constraints of the last run that took {@link SLOW_RULE_MS} or more, slowest first — if any. */
358
+ private slowRules(): { slowRules?: Array<{ shape: string; ms: number }> } {
359
+ const slow = (this.sparqlRunner?.lastTimings ?? [])
360
+ .filter((t) => t.ms >= SLOW_RULE_MS)
361
+ .sort((a, b) => b.ms - a.ms)
362
+ .map((t) => ({ shape: t.shape, ms: Math.round(t.ms) }));
363
+ return slow.length > 0 ? { slowRules: slow } : {};
315
364
  }
316
365
 
317
366
  private validateNodes(nodes: Map<string, Term>, sparqlNodes?: Map<string, Term>): PlainReport['timing'] {
@@ -344,7 +393,7 @@ export class ValidationEngine {
344
393
  for (const [key, results] of byFocus(sparql)) findings.sparql.set(key, results);
345
394
  for (const [key, results] of byFocus(references)) findings.references.set(key, results);
346
395
  this.sinceWhole++;
347
- return { shacl: Math.round(t1 - t0), tables: Math.round(t2 - t1), sparql: Math.round(t3 - t2), references: Math.round(t4 - t3), nodes: nodes.size };
396
+ return { shacl: Math.round(t1 - t0), tables: Math.round(t2 - t1), sparql: Math.round(t3 - t2), references: Math.round(t4 - t3), nodes: nodes.size, ...this.slowRules() };
348
397
  }
349
398
 
350
399
  /** The report from the findings kept — none about a resource the model only refers to. */
@@ -590,31 +639,47 @@ export class ValidationEngine {
590
639
  return pick?.object.value;
591
640
  }
592
641
 
593
- /** A quad into the data — and its Oxigraph copy. */
642
+ /** A quad into the data — and its Oxigraph copy, unless that is to be built again. */
594
643
  private put(q: Quad): void {
595
644
  this.data.addQuad(q as never);
596
- this.sparqlData?.add(toOxQuad(q));
645
+ if (!this.rebuilding) this.sparqlData?.add(toOxQuad(q));
597
646
  }
598
647
 
599
- /** A quad out of the data — and its Oxigraph copy. */
648
+ /** A quad out of the data — and its Oxigraph copy, unless that is to be built again. */
600
649
  private drop(q: Quad): void {
601
650
  this.data.removeQuad(q as never);
602
- this.sparqlData?.delete(toOxQuad(q));
651
+ if (!this.rebuilding) this.sparqlData?.delete(toOxQuad(q));
603
652
  }
604
653
 
605
- /** The Oxigraph copy made again from the data and the context — or none, without SPARQL-based constraints to run. */
654
+ /** A change being applied as a reload: the Oxigraph copy is built again after it. */
655
+ private rebuilding = false;
656
+
657
+ /**
658
+ * The Oxigraph copy made again from the data and the context — or none, without SPARQL-based constraints to run:
659
+ * in one native load of what N-Triples carries as it is, the rest one by one (KD-29). The copy it replaces is
660
+ * freed, so that its WebAssembly memory is used again rather than grown past.
661
+ */
606
662
  private rebuildSparqlData(): void {
607
- if (!this.sparqlRunner) {
608
- this.sparqlData = undefined;
609
- return;
610
- }
663
+ this.dispose();
664
+ if (!this.sparqlRunner) return;
611
665
  const store = new oxigraph.Store();
612
- for (const q of this.data) store.add(toOxQuad(q as unknown as Quad));
613
- const context = oxigraph.namedNode(CONTEXT_GRAPH);
614
- for (const q of this.sparqlContext) {
615
- const ox = toOxQuad(q);
616
- store.add(oxigraph.quad(ox.subject, ox.predicate, ox.object, context));
617
- }
666
+ const give = (quads: Iterable<Quad>, graph?: oxigraph.NamedNode) => {
667
+ const { native, oneByOne } = splitForNativeLoad(quads);
668
+ try {
669
+ if (native.length > 0) {
670
+ store.load(nTriples(native), { format: 'application/n-triples', no_transaction: true, ...(graph ? { to_graph_name: graph } : {}) });
671
+ }
672
+ } catch {
673
+ // Statements already loaded are added again harmlessly; one at a time, past the argument limit (KReq 1.6).
674
+ for (const q of native) oneByOne.push(q);
675
+ }
676
+ for (const q of oneByOne) {
677
+ const ox = toOxQuad(q);
678
+ store.add(graph ? oxigraph.quad(ox.subject, ox.predicate, ox.object, graph) : ox);
679
+ }
680
+ };
681
+ give(this.data as unknown as Iterable<Quad>);
682
+ give(this.sparqlContext, oxigraph.namedNode(CONTEXT_GRAPH));
618
683
  this.sparqlData = store;
619
684
  }
620
685
  }
@@ -62,7 +62,9 @@ export class InlineValidationService implements ValidationService {
62
62
 
63
63
  readonly busy = false;
64
64
 
65
- dispose(): void {}
65
+ dispose(): void {
66
+ this.engine.dispose();
67
+ }
66
68
  }
67
69
 
68
70
  /** What the service needs of a worker: posting, and hearing back. */