@utaba/deep-memory-storage-cosmosdb 0.9.0 → 0.9.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/dist/index.js CHANGED
@@ -882,6 +882,7 @@ async function exploreNeighborhood(conn, repositoryId, entityId, options) {
882
882
  for (let depth = 0; depth < options.depth; depth++) {
883
883
  const layer = {};
884
884
  const nextFrontier = /* @__PURE__ */ new Set();
885
+ const layerCache = /* @__PURE__ */ new Map();
885
886
  for (const currentId of frontier) {
886
887
  const rels = await getRelationshipsForEntity(conn, repositoryId, currentId, options);
887
888
  for (const rel of rels) {
@@ -895,20 +896,20 @@ async function exploreNeighborhood(conn, repositoryId, entityId, options) {
895
896
  } else {
896
897
  connectedId = rel.sourceEntityId;
897
898
  }
898
- if (!visited.has(connectedId)) {
899
- if (options.entityTypes && options.entityTypes.length > 0) {
900
- const entity = await getEntityLight(conn, repositoryId, connectedId);
901
- if (!entity || !options.entityTypes.includes(entity.entityType)) continue;
902
- layer[relType].entities.push(entity);
899
+ if (visited.has(connectedId)) continue;
900
+ let entity = layerCache.get(connectedId);
901
+ if (entity === void 0) {
902
+ const loaded = await getEntityLight(conn, repositoryId, connectedId);
903
+ if (loaded && (!options.entityTypes || options.entityTypes.length === 0 || options.entityTypes.includes(loaded.entityType))) {
904
+ entity = loaded;
903
905
  } else {
904
- const entity = await getEntityLight(conn, repositoryId, connectedId);
905
- if (entity) {
906
- layer[relType].entities.push(entity);
907
- }
906
+ entity = null;
908
907
  }
909
- visited.add(connectedId);
910
- nextFrontier.add(connectedId);
908
+ layerCache.set(connectedId, entity);
909
+ if (entity) nextFrontier.add(connectedId);
911
910
  }
911
+ if (!entity) continue;
912
+ layer[relType].entities.push(entity);
912
913
  layer[relType].relationships.push(rel);
913
914
  layer[relType].total = layer[relType].entities.length;
914
915
  }
@@ -923,6 +924,9 @@ async function exploreNeighborhood(conn, repositoryId, entityId, options) {
923
924
  if (Object.keys(layer).length > 0) {
924
925
  layers.push(layer);
925
926
  }
927
+ for (const id of nextFrontier) {
928
+ visited.add(id);
929
+ }
926
930
  frontier = nextFrontier;
927
931
  if (frontier.size === 0) break;
928
932
  }