@wazootech/worlds-cloudflare 0.15.11 → 0.15.12

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.
@@ -310,4 +310,4 @@ function buildBulkInsertQuads(insertQuadRows, options = {}) {
310
310
  }
311
311
  return statements;
312
312
  }
313
- export { effectiveLookupChunkSize as _, buildSelectAssertedQuadsPage as a, buildDeleteDerivedQuadsByQuadIds as c, DEFAULT_D1_MATCH_PAGE_SIZE as d, buildInsertRow as f, DEFAULT_D1_MAX_WRITE_BATCH_SIZE as g, D1BatchExecutor as h, buildMatchQuadsQuery as i, buildDeleteQuadsByQuadIds as l, quadToInsertRow as m, buildCountAssertedQuads as n, buildSelectExistingQuadIds as o, quadFromD1Row as p, buildCountQuadsQuery as r, buildDeleteAllDerivedQuads as s, buildBulkInsertQuads as t, buildWipeAllGraphDataStatements as u };
313
+ export { DEFAULT_D1_MAX_WRITE_BATCH_SIZE as _, buildSelectAssertedQuadsPage as a, buildDeleteAllDerivedQuads as c, buildWipeAllGraphDataStatements as d, DEFAULT_D1_MATCH_PAGE_SIZE as f, D1BatchExecutor as g, quadToInsertRow as h, buildMatchQuadsQuery as i, buildDeleteDerivedQuadsByQuadIds as l, quadFromD1Row as m, buildCountAssertedQuads as n, buildSelectExistingQuadIds as o, buildInsertRow as p, buildCountQuadsQuery as r, buildD1QuadPatternWhereClause as s, buildBulkInsertQuads as t, buildDeleteQuadsByQuadIds as u, effectiveLookupChunkSize as v };
package/dist/index.js CHANGED
@@ -1,13 +1,13 @@
1
1
  import { RecursiveCharacterTextSplitter } from "@langchain/textsplitters";
2
2
  import { WorldsSdk } from "@wazootech/worlds-sdk";
3
- import { a as createD1SearchIndexRebuilder, c as emptyVectorSyncReport, d as recordVectorSyncError, f as vectorSyncSucceeded, h as sanitizeFtsQuery, i as CloudflareSearchIndex, l as logVectorSyncDrift, m as RRF_REFERENCE_CONSTANT, n as buildSearchResultId, o as rebuildD1SearchIndexFromQuads, p as D1SearchQueryBuilder, r as D1SearchIndexProjector, s as D1VectorSync, t as buildChunkFtsValue, u as mergeVectorSyncReports } from "./search-index-BgOTomws.js";
4
- import { a as InferenceSizeGuardError, n as D1ProvenanceStore, o as loadAssertedQuads, r as ConcurrentWriteError, s as planInference, t as D1QuadStore } from "./quad-store-BwIxBs51.js";
5
- import { a as DEFAULT_D1_MAX_BATCH_WEIGHT, o as d1StatementWeight } from "./project-search-chunks-options-qI9m5FWz.js";
3
+ import { a as createD1SearchIndexRebuilder, c as emptyVectorSyncReport, d as recordVectorSyncError, f as vectorSyncSucceeded, h as sanitizeFtsQuery, i as CloudflareSearchIndex, l as logVectorSyncDrift, m as RRF_REFERENCE_CONSTANT, n as buildSearchResultId, o as rebuildD1SearchIndexFromQuads, p as D1SearchQueryBuilder, r as D1SearchIndexProjector, s as D1VectorSync, t as buildChunkFtsValue, u as mergeVectorSyncReports } from "./search-index-Cw8CTAjm.js";
4
+ import { a as InferenceSizeGuardError, n as D1ProvenanceStore, o as loadAssertedQuads, r as ConcurrentWriteError, s as planInference, t as D1QuadStore } from "./quad-store-DW0XVyvC.js";
5
+ import { a as DEFAULT_D1_MAX_BATCH_WEIGHT, o as d1StatementWeight } from "./project-search-chunks-options-CAxR9V4v.js";
6
6
  import { WazooSparqlEngine } from "@wazootech/sparql-engine";
7
7
  import { RE2JS } from "re2js";
8
- import { i as D1QuadStream, n as assertD1SchemaCompatible, r as checkD1SchemaCompatibility, t as D1RdfjsStore } from "./rdfjs-store-DjRonoBl.js";
8
+ import { i as D1QuadStream, n as assertD1SchemaCompatible, r as checkD1SchemaCompatibility, t as D1RdfjsStore } from "./rdfjs-store-CES_vSLm.js";
9
9
  import { n as validateWorldId, t as D1SchemaBuilder } from "./d1-schema-builder-hkZBWMiA.js";
10
- import { g as DEFAULT_D1_MAX_WRITE_BATCH_SIZE, h as D1BatchExecutor, m as quadToInsertRow, s as buildDeleteAllDerivedQuads, t as buildBulkInsertQuads, u as buildWipeAllGraphDataStatements } from "./d1-quad-insert-sql-B4ZiZZe8.js";
10
+ import { _ as DEFAULT_D1_MAX_WRITE_BATCH_SIZE, c as buildDeleteAllDerivedQuads, d as buildWipeAllGraphDataStatements, g as D1BatchExecutor, h as quadToInsertRow, t as buildBulkInsertQuads } from "./d1-quad-insert-sql-Cp5oSqIG.js";
11
11
  // src/cloudflare/search-index/vector-search/physical-vector-id.ts
12
12
  /**
13
13
  * physicalVectorId namespaces a contract search-result id by world. Vectorize
@@ -0,0 +1,128 @@
1
+ import { m as quadFromD1Row, s as buildD1QuadPatternWhereClause } from "./d1-quad-insert-sql-Cp5oSqIG.js";
2
+ // src/cloudflare/quad-store/d1-quad-page-sql.ts
3
+ /** TEXT_COLUMNS hold the text of a quad, which can be any length. */
4
+ const TEXT_COLUMNS = [
5
+ "s",
6
+ "p",
7
+ "o",
8
+ "o_datatype",
9
+ "o_lang",
10
+ "g"
11
+ ];
12
+ /**
13
+ * ROW_BYTES is how many bytes of text a row holds. SQLite stores the length of
14
+ * a value before the value, so octet_length does not read the text itself.
15
+ */
16
+ const ROW_BYTES = TEXT_COLUMNS.map((column) => column === "o_datatype" || column === "o_lang" ? `COALESCE(octet_length(${column}), 0)` : `octet_length(${column})`).join(" + ");
17
+ /**
18
+ * buildMatchQuadsPage reads one page of the quads matching the pattern, in the
19
+ * order of their ids. A row with more text than `maxRowBytes` comes without its
20
+ * text, and with its size as `large_bytes` (otherwise null).
21
+ */
22
+ function buildMatchQuadsPage(pattern, page, worldId) {
23
+ const { conditions, args } = buildD1QuadPatternWhereClause(pattern);
24
+ if (worldId) {
25
+ conditions.push("world_id = ?");
26
+ args.push(worldId);
27
+ }
28
+ if (page.afterQuadId) {
29
+ conditions.push("quad_id > ?");
30
+ args.push(page.afterQuadId);
31
+ }
32
+ args.push(String(Math.max(1, Math.floor(page.limit))));
33
+ const fits = `${ROW_BYTES} <= ${Math.max(0, Math.floor(page.maxRowBytes))}`;
34
+ return {
35
+ sql: `SELECT quad_id AS id, s_type, o_type, g_type, ${TEXT_COLUMNS.map((column) => `CASE WHEN ${fits} THEN ${column} END AS ${column}`).join(", ")}, CASE WHEN NOT (${fits}) THEN ${ROW_BYTES} END AS large_bytes FROM quads ${conditions.length > 0 ? `WHERE ${conditions.join(" AND ")} ` : ""}ORDER BY quad_id ASC LIMIT ?`,
36
+ args
37
+ };
38
+ }
39
+ /**
40
+ * buildSelectQuadsById reads whole rows by their ids, in any order. CROSS JOIN
41
+ * makes SQLite look up each id, and never scan the quads instead.
42
+ */
43
+ function buildSelectQuadsById(quadIds, worldId) {
44
+ return {
45
+ sql: `SELECT quad_id AS id, s, s_type, p, o, o_type, o_datatype, o_lang, g, g_type FROM json_each(?) AS ids CROSS JOIN quads ON quad_id = ids.value${worldId ? " AND world_id = ?" : ""}`,
46
+ args: [JSON.stringify(quadIds), ...worldId ? [worldId] : []]
47
+ };
48
+ }
49
+ // src/cloudflare/rdfjs-store/match-d1-pages.ts
50
+ /**
51
+ * D1_READ_BYTES is the most bytes of text that one query of a read returns,
52
+ * unless a single quad has more.
53
+ */
54
+ const D1_READ_BYTES = 4e6;
55
+ /** MAX_IDS is the most rows one query looks up, so that their ids fit in a parameter. */
56
+ const MAX_IDS = 1e3;
57
+ /** matchD1Pages keyset-pages the quads matching a pattern, in the order of their ids. */
58
+ async function* matchD1Pages(connection, matchPageSize, worldId, subject, predicate, object, graph, readBytes = D1_READ_BYTES) {
59
+ const pattern = {
60
+ subject: subject ?? null,
61
+ predicate: predicate ?? null,
62
+ object: object ?? null,
63
+ graph: graph ?? null
64
+ };
65
+ const limit = Math.max(1, Math.floor(matchPageSize));
66
+ let afterQuadId;
67
+ for (;;) {
68
+ const { rows } = await connection.execute(buildMatchQuadsPage(pattern, {
69
+ afterQuadId,
70
+ limit,
71
+ maxRowBytes: readBytes / limit
72
+ }, worldId));
73
+ const large = new LargeRows(connection, rows, worldId, readBytes);
74
+ for (const [index, row] of rows.entries()) {
75
+ const whole = row.large_bytes === null ? row : await large.read(index);
76
+ if (whole) yield quadFromD1Row(whole);
77
+ }
78
+ if (rows.length < limit) return;
79
+ afterQuadId = String(rows[rows.length - 1].id);
80
+ }
81
+ }
82
+ /**
83
+ * LargeRows reads the rows of a page that came without their text, when the page
84
+ * gets to them: as many at a time as fit in `readBytes`, or one that does not fit
85
+ * on its own.
86
+ */
87
+ var LargeRows = class {
88
+ connection;
89
+ page;
90
+ worldId;
91
+ readBytes;
92
+ loaded = /* @__PURE__ */ new Map();
93
+ loadedUntil = 0;
94
+ constructor(connection, page, worldId, readBytes) {
95
+ this.connection = connection;
96
+ this.page = page;
97
+ this.worldId = worldId;
98
+ this.readBytes = readBytes;
99
+ }
100
+ /** read returns the row at `index`, or undefined if it was deleted since. */
101
+ async read(index) {
102
+ if (index >= this.loadedUntil) {
103
+ const { rows } = await this.connection.execute(buildSelectQuadsById(this.nextIds(index), this.worldId));
104
+ for (const row of rows) this.loaded.set(String(row.id), row);
105
+ }
106
+ const id = String(this.page[index].id);
107
+ const row = this.loaded.get(id);
108
+ this.loaded.delete(id);
109
+ return row;
110
+ }
111
+ /** nextIds takes the large rows from `index` on that fit in one read. */
112
+ nextIds(index) {
113
+ const ids = [];
114
+ let bytes = 0;
115
+ let next = index;
116
+ for (; next < this.page.length; next++) {
117
+ const row = this.page[next];
118
+ if (row.large_bytes === null) continue;
119
+ bytes += Number(row.large_bytes);
120
+ if (ids.length > 0 && bytes > this.readBytes) break;
121
+ if (ids.length === MAX_IDS) break;
122
+ ids.push(String(row.id));
123
+ }
124
+ this.loadedUntil = next;
125
+ return ids;
126
+ }
127
+ };
128
+ export { matchD1Pages as t };
@@ -1,4 +1,4 @@
1
- import { _ as effectiveLookupChunkSize, a as buildSelectAssertedQuadsPage, c as buildDeleteDerivedQuadsByQuadIds, f as buildInsertRow, i as buildMatchQuadsQuery, l as buildDeleteQuadsByQuadIds, o as buildSelectExistingQuadIds, p as quadFromD1Row, t as buildBulkInsertQuads, u as buildWipeAllGraphDataStatements } from "./d1-quad-insert-sql-B4ZiZZe8.js";
1
+ import { a as buildSelectAssertedQuadsPage, d as buildWipeAllGraphDataStatements, i as buildMatchQuadsQuery, l as buildDeleteDerivedQuadsByQuadIds, m as quadFromD1Row, o as buildSelectExistingQuadIds, p as buildInsertRow, t as buildBulkInsertQuads, u as buildDeleteQuadsByQuadIds, v as effectiveLookupChunkSize } from "./d1-quad-insert-sql-Cp5oSqIG.js";
2
2
  import { buildChunkFtsValue } from "@wazootech/worlds-sqlite/sql-core";
3
3
  import { filterQuads } from "@wazootech/worlds-sdk/quad-store";
4
4
  // src/cloudflare/commit/d1-commit-dialect.ts
@@ -1,11 +1,11 @@
1
1
  import { InMemoryProvenanceStore, Transaction, exportFromRdfjsStore, filterQuads, hashQuads, importViaTransaction, isReplaceImportCommit, preparePatch, selectNovelQuads } from "@wazootech/worlds-sdk/quad-store";
2
2
  import { planSearchIndexing } from "@wazootech/worlds-sdk/search-index";
3
3
  import { commitSqlPatch } from "@wazootech/worlds-sqlite/sql-core";
4
- import { _ as effectiveLookupChunkSize, a as buildSelectAssertedQuadsPage, d as DEFAULT_D1_MATCH_PAGE_SIZE, f as buildInsertRow, n as buildCountAssertedQuads, p as quadFromD1Row } from "./d1-quad-insert-sql-B4ZiZZe8.js";
4
+ import { a as buildSelectAssertedQuadsPage, f as DEFAULT_D1_MATCH_PAGE_SIZE, m as quadFromD1Row, n as buildCountAssertedQuads, p as buildInsertRow, v as effectiveLookupChunkSize } from "./d1-quad-insert-sql-Cp5oSqIG.js";
5
5
  import { a as buildSelectQuadRecords, d as selectWorldVersion, f as SHAPES_GRAPH, i as buildSelectProvenanceChains, l as isVersionConflict, o as chunk, r as buildSelectProvenance, s as buildRaiseVersion, t as buildInsertProvenance, u as readWorldVersion } from "./d1-provenance-sql-CV6Mly6b.js";
6
6
  import { Reasoner } from "@wazootech/reasoner";
7
- import { i as createD1CommitDriver, o as d1StatementWeight, s as createD1CommitDialect, t as isFullRebuild } from "./project-search-chunks-options-qI9m5FWz.js";
8
- import { r as matchD1Pages } from "./d1-rdfjs-store-read-2QTEEe_a.js";
7
+ import { i as createD1CommitDriver, o as d1StatementWeight, s as createD1CommitDialect, t as isFullRebuild } from "./project-search-chunks-options-CAxR9V4v.js";
8
+ import { t as matchD1Pages } from "./match-d1-pages-hRKVbawI.js";
9
9
  /** LISTS_PER_STATEMENT keeps a statement inside D1's 100 parameters and a batch's byte budget. */
10
10
  const LISTS_PER_STATEMENT = 16;
11
11
  /** packLists splits items over as many JSON lists as D1's value size needs, and at least one. */
@@ -1 +1 @@
1
- export { t as D1QuadStore } from "./quad-store-BwIxBs51.js";
1
+ export { t as D1QuadStore } from "./quad-store-DW0XVyvC.js";
@@ -1,8 +1,8 @@
1
1
  import { DataFactory } from "@wazootech/sparql-engine";
2
- import { _ as effectiveLookupChunkSize, h as D1BatchExecutor, l as buildDeleteQuadsByQuadIds, m as quadToInsertRow, o as buildSelectExistingQuadIds, t as buildBulkInsertQuads, u as buildWipeAllGraphDataStatements } from "./d1-quad-insert-sql-B4ZiZZe8.js";
2
+ import { d as buildWipeAllGraphDataStatements, g as D1BatchExecutor, h as quadToInsertRow, o as buildSelectExistingQuadIds, r as buildCountQuadsQuery, t as buildBulkInsertQuads, u as buildDeleteQuadsByQuadIds, v as effectiveLookupChunkSize } from "./d1-quad-insert-sql-Cp5oSqIG.js";
3
3
  import { n as validateWorldId, t as D1SchemaBuilder } from "./d1-schema-builder-hkZBWMiA.js";
4
4
  import { hashQuads, isReplaceImportCommit } from "@wazootech/worlds-sdk/quad-store";
5
- import { i as removeMatchedQuads, n as countD1Matches, r as matchD1Pages, t as collectQuads } from "./d1-rdfjs-store-read-2QTEEe_a.js";
5
+ import { t as matchD1Pages } from "./match-d1-pages-hRKVbawI.js";
6
6
  // src/cloudflare/rdfjs-store/d1-quad-stream.ts
7
7
  /**
8
8
  * D1QuadStream is a minimal RDF/JS Stream backed by an async page generator
@@ -135,12 +135,12 @@ var D1QuadStream = class {
135
135
  if (this._ended) return;
136
136
  try {
137
137
  for await (const quad of this.quads()) {
138
- if (this._ended) return;
139
138
  if (this._flowing) this.emit("data", quad);
140
- else if (this._readStarted) {
139
+ else if (this._readStarted && !this._ended) {
141
140
  this._buffer.push(quad);
142
141
  this.emit("readable");
143
142
  }
143
+ if (this._ended) return;
144
144
  }
145
145
  if (!this._ended) this._end();
146
146
  } catch (error) {
@@ -515,6 +515,42 @@ var D1WriteQueue = class {
515
515
  };
516
516
  /** flush() reports at most this many of the failures it aggregates. */
517
517
  const MAX_REPORTED_FAILURES = 10;
518
+ // src/cloudflare/rdfjs-store/d1-rdfjs-store-read.ts
519
+ /** collectQuads drains a quad stream into an array. */
520
+ async function collectQuads(stream) {
521
+ const quads = [];
522
+ await new Promise((resolve, reject) => {
523
+ stream.on("data", (quad) => quads.push(quad));
524
+ stream.on("end", () => resolve());
525
+ stream.on("error", reject);
526
+ });
527
+ return quads;
528
+ }
529
+ /** countD1Matches returns the number of quads matching the pattern. */
530
+ async function countD1Matches(connection, worldId, subject, predicate, object, graph) {
531
+ const { sql, args } = buildCountQuadsQuery({
532
+ subject: subject ?? null,
533
+ predicate: predicate ?? null,
534
+ object: object ?? null,
535
+ graph: graph ?? null
536
+ }, worldId);
537
+ const result = await connection.execute({
538
+ sql,
539
+ args
540
+ });
541
+ return Number(result.rows[0]?.count ?? 0);
542
+ }
543
+ /** removeMatchedQuads deletes every matching quad in one patch and yields the removed quads. */
544
+ async function* removeMatchedQuads(store, subject, predicate, object, graph) {
545
+ const matches = await store.getQuads(subject, predicate, object, graph);
546
+ if (matches.length > 0) {
547
+ await store.applyPatch({
548
+ insertions: [],
549
+ deletions: matches
550
+ });
551
+ for (const quad of matches) yield quad;
552
+ }
553
+ }
518
554
  // src/cloudflare/rdfjs-store/d1-rdfjs-store.ts
519
555
  /**
520
556
  * logWriteFailure reports a fire-and-forget write failure (addQuad/removeQuad
@@ -1 +1 @@
1
- export { i as D1QuadStream, t as D1RdfjsStore } from "./rdfjs-store-DjRonoBl.js";
1
+ export { i as D1QuadStream, t as D1RdfjsStore } from "./rdfjs-store-CES_vSLm.js";
@@ -1,7 +1,7 @@
1
1
  import { RRF_FUSION_K, buildChunkFtsValue, buildIncludeExcludeFilterClauses, buildKeywordFtsStatement, buildSearchResultId, buildSearchResultId as buildSearchResultId$1, generatePlaceholders, rebuildSqlSearchIndex, sanitizeFtsQuery, sanitizeFtsQuery as sanitizeFtsQuery$1 } from "@wazootech/worlds-sqlite/sql-core";
2
2
  import { normalizeRrfScore } from "@wazootech/worlds-sdk/search-index";
3
- import { _ as effectiveLookupChunkSize } from "./d1-quad-insert-sql-B4ZiZZe8.js";
4
- import { i as createD1CommitDriver, n as toProjectionOptions, r as vectorSyncEnabled, s as createD1CommitDialect, t as isFullRebuild } from "./project-search-chunks-options-qI9m5FWz.js";
3
+ import { v as effectiveLookupChunkSize } from "./d1-quad-insert-sql-Cp5oSqIG.js";
4
+ import { i as createD1CommitDriver, n as toProjectionOptions, r as vectorSyncEnabled, s as createD1CommitDialect, t as isFullRebuild } from "./project-search-chunks-options-CAxR9V4v.js";
5
5
  import { filterQuads, hashQuads } from "@wazootech/worlds-sdk/quad-store";
6
6
  import { DataFactory } from "@wazootech/sparql-engine/data-model";
7
7
  // src/cloudflare/search-index/d1-world-scoped-fts.ts
@@ -1 +1 @@
1
- export { i as CloudflareSearchIndex, r as D1SearchIndexProjector, p as D1SearchQueryBuilder, s as D1VectorSync, m as RRF_REFERENCE_CONSTANT, t as buildChunkFtsValue, n as buildSearchResultId, a as createD1SearchIndexRebuilder, c as emptyVectorSyncReport, l as logVectorSyncDrift, u as mergeVectorSyncReports, o as rebuildD1SearchIndexFromQuads, d as recordVectorSyncError, h as sanitizeFtsQuery, f as vectorSyncSucceeded } from "./search-index-BgOTomws.js";
1
+ export { i as CloudflareSearchIndex, r as D1SearchIndexProjector, p as D1SearchQueryBuilder, s as D1VectorSync, m as RRF_REFERENCE_CONSTANT, t as buildChunkFtsValue, n as buildSearchResultId, a as createD1SearchIndexRebuilder, c as emptyVectorSyncReport, l as logVectorSyncDrift, u as mergeVectorSyncReports, o as rebuildD1SearchIndexFromQuads, d as recordVectorSyncError, h as sanitizeFtsQuery, f as vectorSyncSucceeded } from "./search-index-Cw8CTAjm.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wazootech/worlds-cloudflare",
3
- "version": "0.15.11",
3
+ "version": "0.15.12",
4
4
  "description": "Store a Worlds knowledge graph in Cloudflare D1, with keyword search, optional Vectorize search, SPARQL and provenance. Made for Cloudflare Workers.",
5
5
  "keywords": [
6
6
  "wazoo",
@@ -72,7 +72,7 @@
72
72
  "@langchain/textsplitters": "^1.0.1",
73
73
  "@rdfjs/types": "^2.0.1",
74
74
  "@wazootech/reasoner": "^0.4.4",
75
- "@wazootech/sparql-engine": "^0.5.12",
75
+ "@wazootech/sparql-engine": "^0.5.13",
76
76
  "@wazootech/worlds-sdk": "^0.11.6",
77
77
  "@wazootech/worlds-sqlite": "^0.8.5",
78
78
  "re2js": "2.10.0"
@@ -1,64 +0,0 @@
1
- import { i as buildMatchQuadsQuery, p as quadFromD1Row, r as buildCountQuadsQuery } from "./d1-quad-insert-sql-B4ZiZZe8.js";
2
- // src/cloudflare/rdfjs-store/d1-rdfjs-store-read.ts
3
- /** matchD1Pages keyset-pages the quads matching a pattern, one SQL round-trip per page. */
4
- async function* matchD1Pages(connection, matchPageSize, worldId, subject, predicate, object, graph) {
5
- const pattern = {
6
- subject: subject ?? null,
7
- predicate: predicate ?? null,
8
- object: object ?? null,
9
- graph: graph ?? null
10
- };
11
- let afterQuadId;
12
- for (;;) {
13
- const { sql, args } = buildMatchQuadsQuery(pattern, {
14
- afterQuadId,
15
- limit: matchPageSize
16
- }, worldId);
17
- const result = await connection.execute({
18
- sql,
19
- args
20
- });
21
- if (result.rows.length === 0) return;
22
- for (const row of result.rows) {
23
- afterQuadId = String(row.id);
24
- yield quadFromD1Row(row);
25
- }
26
- if (result.rows.length < matchPageSize) return;
27
- }
28
- }
29
- /** collectQuads drains a quad stream into an array. */
30
- async function collectQuads(stream) {
31
- const quads = [];
32
- await new Promise((resolve, reject) => {
33
- stream.on("data", (quad) => quads.push(quad));
34
- stream.on("end", () => resolve());
35
- stream.on("error", reject);
36
- });
37
- return quads;
38
- }
39
- /** countD1Matches returns the number of quads matching the pattern. */
40
- async function countD1Matches(connection, worldId, subject, predicate, object, graph) {
41
- const { sql, args } = buildCountQuadsQuery({
42
- subject: subject ?? null,
43
- predicate: predicate ?? null,
44
- object: object ?? null,
45
- graph: graph ?? null
46
- }, worldId);
47
- const result = await connection.execute({
48
- sql,
49
- args
50
- });
51
- return Number(result.rows[0]?.count ?? 0);
52
- }
53
- /** removeMatchedQuads deletes every matching quad in one patch and yields the removed quads. */
54
- async function* removeMatchedQuads(store, subject, predicate, object, graph) {
55
- const matches = await store.getQuads(subject, predicate, object, graph);
56
- if (matches.length > 0) {
57
- await store.applyPatch({
58
- insertions: [],
59
- deletions: matches
60
- });
61
- for (const quad of matches) yield quad;
62
- }
63
- }
64
- export { removeMatchedQuads as i, countD1Matches as n, matchD1Pages as r, collectQuads as t };