@suss/datalog 0.4.0 → 0.5.0

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.cjs CHANGED
@@ -423,7 +423,7 @@ var Database = class {
423
423
  for (const [column, index] of relation.indexes) {
424
424
  const value = tuple[column];
425
425
  if (value !== void 0) {
426
- addToBucket(index, tupleKey([value]), tuple);
426
+ addToBucket(index, value, tuple);
427
427
  }
428
428
  }
429
429
  return true;
@@ -454,12 +454,12 @@ var Database = class {
454
454
  for (const tuple of relation.tuples) {
455
455
  const at = tuple[column];
456
456
  if (at !== void 0) {
457
- addToBucket(index, tupleKey([at]), tuple);
457
+ addToBucket(index, at, tuple);
458
458
  }
459
459
  }
460
460
  relation.indexes.set(column, index);
461
461
  }
462
- return index.get(tupleKey([value])) ?? [];
462
+ return index.get(value) ?? [];
463
463
  }
464
464
  /**
465
465
  * Remove facts from a relation. Returns how many were actually there.
@@ -723,6 +723,11 @@ function shapeOf(rules) {
723
723
  shapes.set(rules, shape);
724
724
  return shape;
725
725
  }
726
+ function couldProduce(db, rule2) {
727
+ return rule2.body.every(
728
+ (literal) => literal.negated || db.size(literal.relation) > 0
729
+ );
730
+ }
726
731
  function runRules(db, rules) {
727
732
  const {
728
733
  signature,
@@ -781,6 +786,9 @@ function runRules(db, rules) {
781
786
  };
782
787
  const applyDelta = (seed, derivedOnly) => {
783
788
  for (const r of stratum) {
789
+ if (!couldProduce(db, r)) {
790
+ continue;
791
+ }
784
792
  const positives = r.body.filter((l) => !l.negated);
785
793
  for (let i = 0; i < positives.length; i++) {
786
794
  const literal = positives[i];
@@ -796,6 +804,9 @@ function runRules(db, rules) {
796
804
  };
797
805
  if (marks === void 0 || marks === null) {
798
806
  for (const r of stratum) {
807
+ if (!couldProduce(db, r)) {
808
+ continue;
809
+ }
799
810
  const all = /* @__PURE__ */ new Map();
800
811
  for (const l of r.body) {
801
812
  if (!l.negated) {
package/dist/index.js CHANGED
@@ -383,7 +383,7 @@ var Database = class {
383
383
  for (const [column, index] of relation.indexes) {
384
384
  const value = tuple[column];
385
385
  if (value !== void 0) {
386
- addToBucket(index, tupleKey([value]), tuple);
386
+ addToBucket(index, value, tuple);
387
387
  }
388
388
  }
389
389
  return true;
@@ -414,12 +414,12 @@ var Database = class {
414
414
  for (const tuple of relation.tuples) {
415
415
  const at = tuple[column];
416
416
  if (at !== void 0) {
417
- addToBucket(index, tupleKey([at]), tuple);
417
+ addToBucket(index, at, tuple);
418
418
  }
419
419
  }
420
420
  relation.indexes.set(column, index);
421
421
  }
422
- return index.get(tupleKey([value])) ?? [];
422
+ return index.get(value) ?? [];
423
423
  }
424
424
  /**
425
425
  * Remove facts from a relation. Returns how many were actually there.
@@ -683,6 +683,11 @@ function shapeOf(rules) {
683
683
  shapes.set(rules, shape);
684
684
  return shape;
685
685
  }
686
+ function couldProduce(db, rule2) {
687
+ return rule2.body.every(
688
+ (literal) => literal.negated || db.size(literal.relation) > 0
689
+ );
690
+ }
686
691
  function runRules(db, rules) {
687
692
  const {
688
693
  signature,
@@ -741,6 +746,9 @@ function runRules(db, rules) {
741
746
  };
742
747
  const applyDelta = (seed, derivedOnly) => {
743
748
  for (const r of stratum) {
749
+ if (!couldProduce(db, r)) {
750
+ continue;
751
+ }
744
752
  const positives = r.body.filter((l) => !l.negated);
745
753
  for (let i = 0; i < positives.length; i++) {
746
754
  const literal = positives[i];
@@ -756,6 +764,9 @@ function runRules(db, rules) {
756
764
  };
757
765
  if (marks === void 0 || marks === null) {
758
766
  for (const r of stratum) {
767
+ if (!couldProduce(db, r)) {
768
+ continue;
769
+ }
759
770
  const all = /* @__PURE__ */ new Map();
760
771
  for (const l of r.body) {
761
772
  if (!l.negated) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@suss/datalog",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "type": "module",
5
5
  "description": "A small semi-naïve Datalog evaluator with stratified negation — the rules engine behind suss's derived program facts.",
6
6
  "keywords": [