@twin.org/auditable-item-graph-service 0.10.1-next.3 → 0.10.1-next.5

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.
@@ -3,8 +3,7 @@
3
3
  import { HealthCategory, HealthStatus } from "@twin.org/api-models";
4
4
  import { AuditableItemGraphAuditMode, AuditableItemGraphContexts, AuditableItemGraphDataTypes, AuditableItemGraphMetricIds, AuditableItemGraphMetrics, AuditableItemGraphTopics, AuditableItemGraphTypes, VerifyDepth } from "@twin.org/auditable-item-graph-models";
5
5
  import { ContextIdHelper, ContextIdKeys, ContextIdStore } from "@twin.org/context";
6
- import { ArrayHelper, BaseError, Coerce, ComponentFactory, Converter, GeneralError, Guards, Is, JsonHelper, Mutex, NotFoundError, ObjectHelper, RandomHelper, StringHelper, Urn, Validation } from "@twin.org/core";
7
- import { Blake2b } from "@twin.org/crypto";
6
+ import { ArrayHelper, BaseError, Coerce, ComponentFactory, GeneralError, Guards, Is, JsonHelper, Mutex, NotFoundError, ObjectHelper, RandomHelper, StringHelper, Urn, Validation } from "@twin.org/core";
8
7
  import { DataTypeHelper } from "@twin.org/data-core";
9
8
  import { JsonLdDataTypes, JsonLdHelper, JsonLdProcessor } from "@twin.org/data-json-ld";
10
9
  import { ComparisonOperator, LogicalOperator, SortDirection } from "@twin.org/entity";
@@ -12,6 +11,7 @@ import { EntityStorageConnectorFactory } from "@twin.org/entity-storage-models";
12
11
  import { ImmutableProofContexts, ImmutableProofFailure, ImmutableProofTypes } from "@twin.org/immutable-proof-models";
13
12
  import { SchemaOrgContexts, SchemaOrgDataTypes, SchemaOrgTypes } from "@twin.org/standards-schema-org";
14
13
  import { MetricHelper } from "@twin.org/telemetry-models";
14
+ import { AuditableItemGraphVertexIndexHelper } from "./utils/auditableItemGraphVertexIndexHelper.js";
15
15
  /**
16
16
  * Class for performing auditable item graph operations.
17
17
  */
@@ -1625,7 +1625,7 @@ export class AuditableItemGraphService {
1625
1625
  property: "vertexId",
1626
1626
  comparison: ComparisonOperator.Equals,
1627
1627
  value: vertex.id
1628
- }, ["id", "type", "value", "dateCreated", "dateModified"]);
1628
+ }, ["id", "type", "value", "valueHash", "dateCreated", "dateModified"]);
1629
1629
  // Collapse by row id first. A paged read can return the same row on more than one page,
1630
1630
  // and without this a retained row could be queued for removal by its own duplicate.
1631
1631
  const existingById = new Map();
@@ -1644,16 +1644,13 @@ export class AuditableItemGraphService {
1644
1644
  const requiredEntry = required.get(key);
1645
1645
  if (!Is.empty(requiredEntry) && !retainedKeys.has(key)) {
1646
1646
  retainedKeys.add(key);
1647
- if (entry.dateCreated !== vertex.dateCreated || entry.dateModified !== indexDateModified) {
1648
- // Keep the row id so the copied dates are corrected in place rather than duplicated.
1649
- writeEntries.push({
1650
- id,
1651
- vertexId: vertex.id,
1652
- type: requiredEntry.type,
1653
- value: requiredEntry.value,
1654
- dateCreated: vertex.dateCreated,
1655
- dateModified: indexDateModified
1656
- });
1647
+ const requiredIndexEntry = AuditableItemGraphVertexIndexHelper.createIndexEntry(vertex.id, requiredEntry.type, requiredEntry.value, vertex.dateCreated, indexDateModified);
1648
+ if (entry.dateCreated !== requiredIndexEntry.dateCreated ||
1649
+ entry.dateModified !== requiredIndexEntry.dateModified ||
1650
+ entry.valueHash !== requiredIndexEntry.valueHash) {
1651
+ // Keep the row id so the copied dates and hash are corrected in place rather than
1652
+ // duplicated.
1653
+ writeEntries.push({ ...requiredIndexEntry, id });
1657
1654
  }
1658
1655
  }
1659
1656
  else {
@@ -1662,17 +1659,7 @@ export class AuditableItemGraphService {
1662
1659
  }
1663
1660
  for (const [key, entry] of required) {
1664
1661
  if (!retainedKeys.has(key)) {
1665
- const writeEntry = {
1666
- vertexId: vertex.id,
1667
- type: entry.type,
1668
- value: entry.value,
1669
- dateCreated: vertex.dateCreated,
1670
- dateModified: indexDateModified
1671
- };
1672
- writeEntries.push({
1673
- id: Converter.bytesToHex(Blake2b.sum256(ObjectHelper.toBytes(JsonHelper.canonicalize(writeEntry)))),
1674
- ...writeEntry
1675
- });
1662
+ writeEntries.push(AuditableItemGraphVertexIndexHelper.createIndexEntry(vertex.id, entry.type, entry.value, vertex.dateCreated, indexDateModified));
1676
1663
  }
1677
1664
  }
1678
1665
  if (removeIds.length > 0) {
@@ -1713,7 +1700,8 @@ export class AuditableItemGraphService {
1713
1700
  };
1714
1701
  }
1715
1702
  /**
1716
- * Build the condition matching one index type and value.
1703
+ * Build the condition matching one index type and value. Exact matches are looked up by the
1704
+ * value hash, a partial match can only be answered by the value itself.
1717
1705
  * @param type The index type to match.
1718
1706
  * @param comparison The comparison to apply to the value.
1719
1707
  * @param value The index value or values to match, already lower cased.
@@ -1721,11 +1709,20 @@ export class AuditableItemGraphService {
1721
1709
  * @internal
1722
1710
  */
1723
1711
  indexTypeValueCondition(type, comparison, value) {
1712
+ const valueCondition = comparison === ComparisonOperator.Includes
1713
+ ? { property: "value", comparison, value }
1714
+ : {
1715
+ property: "valueHash",
1716
+ comparison,
1717
+ value: Is.array(value)
1718
+ ? value.map(v => AuditableItemGraphVertexIndexHelper.hashValue(v))
1719
+ : AuditableItemGraphVertexIndexHelper.hashValue(value)
1720
+ };
1724
1721
  return {
1725
1722
  logicalOperator: LogicalOperator.And,
1726
1723
  conditions: [
1727
1724
  { property: "type", comparison: ComparisonOperator.Equals, value: type },
1728
- { property: "value", comparison, value }
1725
+ valueCondition
1729
1726
  ]
1730
1727
  };
1731
1728
  }
@@ -1817,9 +1814,9 @@ export class AuditableItemGraphService {
1817
1814
  value: AuditableItemGraphService._INDEX_TYPE_ALIAS
1818
1815
  },
1819
1816
  {
1820
- property: "value",
1817
+ property: "valueHash",
1821
1818
  comparison: ComparisonOperator.Equals,
1822
- value: aliasId.toLowerCase()
1819
+ value: AuditableItemGraphVertexIndexHelper.hashValue(aliasId)
1823
1820
  },
1824
1821
  {
1825
1822
  property: "vertexId",