n3 1.21.1 → 1.21.2

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/lib/N3Store.js CHANGED
@@ -976,9 +976,33 @@ class N3Store {
976
976
  }
977
977
 
978
978
  /**
979
- * A class that implements both DatasetCore and Readable.
979
+ * Returns a subset of the `index` with that part of the index
980
+ * matching the `ids` array. `ids` contains 3 elements that are
981
+ * either numerical ids; or `null`.
982
+ *
983
+ * `false` is returned when there are no matching indices; this should
984
+ * *not* be set as the value for an index.
980
985
  */
981
986
  exports.default = N3Store;
987
+ function indexMatch(index, ids, depth = 0) {
988
+ const ind = ids[depth];
989
+ if (ind && !(ind in index)) return false;
990
+ let target = false;
991
+ for (const key in ind ? {
992
+ [ind]: index[ind]
993
+ } : index) {
994
+ const result = depth === 2 ? null : indexMatch(index[key], ids, depth + 1);
995
+ if (result !== false) {
996
+ target = target || Object.create(null);
997
+ target[key] = result;
998
+ }
999
+ }
1000
+ return target;
1001
+ }
1002
+
1003
+ /**
1004
+ * A class that implements both DatasetCore and Readable.
1005
+ */
982
1006
  class DatasetCoreAndReadableStream extends _readableStream.Readable {
983
1007
  constructor(n3Store, subject, predicate, object, graph, options) {
984
1008
  super({
@@ -1006,7 +1030,22 @@ class DatasetCoreAndReadableStream extends _readableStream.Readable {
1006
1030
  factory: n3Store._factory,
1007
1031
  entityIndex: this.options.entityIndex
1008
1032
  });
1009
- for (const quad of n3Store.readQuads(subject, predicate, object, graph)) newStore.addQuad(quad);
1033
+ const graphs = n3Store._getGraphs(graph);
1034
+ let subjectId, predicateId, objectId;
1035
+
1036
+ // Translate IRIs to internal index keys.
1037
+ if (subject && !(subjectId = newStore._termToNumericId(subject)) || predicate && !(predicateId = newStore._termToNumericId(predicate)) || object && !(objectId = newStore._termToNumericId(object))) return newStore;
1038
+ for (const graph in graphs) {
1039
+ const subjects = indexMatch(graphs[graph].subjects, [subjectId, predicateId, objectId]);
1040
+ if (subjects) {
1041
+ newStore._graphs[graph] = {
1042
+ subjects,
1043
+ predicates: indexMatch(graphs[graph].predicates, [predicateId, objectId, subjectId]),
1044
+ objects: indexMatch(graphs[graph].objects, [objectId, subjectId, predicateId])
1045
+ };
1046
+ }
1047
+ }
1048
+ newStore._size = null;
1010
1049
  }
1011
1050
  return this._filtered;
1012
1051
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n3",
3
- "version": "1.21.1",
3
+ "version": "1.21.2",
4
4
  "description": "Lightning fast, asynchronous, streaming Turtle / N3 / RDF library.",
5
5
  "author": "Ruben Verborgh <ruben.verborgh@gmail.com>",
6
6
  "keywords": [
package/src/N3Store.js CHANGED
@@ -1026,6 +1026,31 @@ export default class N3Store {
1026
1026
  }
1027
1027
  }
1028
1028
 
1029
+ /**
1030
+ * Returns a subset of the `index` with that part of the index
1031
+ * matching the `ids` array. `ids` contains 3 elements that are
1032
+ * either numerical ids; or `null`.
1033
+ *
1034
+ * `false` is returned when there are no matching indices; this should
1035
+ * *not* be set as the value for an index.
1036
+ */
1037
+ function indexMatch(index, ids, depth = 0) {
1038
+ const ind = ids[depth];
1039
+ if (ind && !(ind in index))
1040
+ return false;
1041
+
1042
+ let target = false;
1043
+ for (const key in (ind ? { [ind]: index[ind] } : index)) {
1044
+ const result = depth === 2 ? null : indexMatch(index[key], ids, depth + 1);
1045
+
1046
+ if (result !== false) {
1047
+ target = target || Object.create(null);
1048
+ target[key] = result;
1049
+ }
1050
+ }
1051
+ return target;
1052
+ }
1053
+
1029
1054
  /**
1030
1055
  * A class that implements both DatasetCore and Readable.
1031
1056
  */
@@ -1039,8 +1064,27 @@ class DatasetCoreAndReadableStream extends Readable {
1039
1064
  if (!this._filtered) {
1040
1065
  const { n3Store, graph, object, predicate, subject } = this;
1041
1066
  const newStore = this._filtered = new N3Store({ factory: n3Store._factory, entityIndex: this.options.entityIndex });
1042
- for (const quad of n3Store.readQuads(subject, predicate, object, graph))
1043
- newStore.addQuad(quad);
1067
+ const graphs = n3Store._getGraphs(graph);
1068
+
1069
+ let subjectId, predicateId, objectId;
1070
+
1071
+ // Translate IRIs to internal index keys.
1072
+ if (subject && !(subjectId = newStore._termToNumericId(subject)) ||
1073
+ predicate && !(predicateId = newStore._termToNumericId(predicate)) ||
1074
+ object && !(objectId = newStore._termToNumericId(object)))
1075
+ return newStore;
1076
+
1077
+ for (const graph in graphs) {
1078
+ const subjects = indexMatch(graphs[graph].subjects, [subjectId, predicateId, objectId]);
1079
+ if (subjects) {
1080
+ newStore._graphs[graph] = {
1081
+ subjects,
1082
+ predicates: indexMatch(graphs[graph].predicates, [predicateId, objectId, subjectId]),
1083
+ objects: indexMatch(graphs[graph].objects, [objectId, subjectId, predicateId]),
1084
+ };
1085
+ }
1086
+ }
1087
+ newStore._size = null;
1044
1088
  }
1045
1089
  return this._filtered;
1046
1090
  }