Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

# IntelliJ IDEA
.idea/
*.iml

# Mobile Tools for Java (J2ME)
.mtj.tmp/
Expand Down
28 changes: 28 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>biscuit-java</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
<filteredResources>
<filter>
<id>1795780263878</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
2 changes: 2 additions & 0 deletions .settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
encoding/<project>=UTF-8
4 changes: 4 additions & 0 deletions .settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
63 changes: 63 additions & 0 deletions biscuit-core/.classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/classes" path="target/generated-sources/protobuf">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
<attributes>
<attribute name="maven.pomderived" value="true"/>
<attribute name="test" value="true"/>
<attribute name="optional" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-11">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" path="target/generated-sources/annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes" path="target/generated-test-sources/test-annotations">
<attributes>
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
<attribute name="ignore_optional_problems" value="true"/>
<attribute name="m2e-apt" value="true"/>
<attribute name="test" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes"/>
</classpath>
34 changes: 34 additions & 0 deletions biscuit-core/.project
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>biscuit-core</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
<buildCommand>
<name>org.eclipse.m2e.core.maven2Builder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
<nature>org.eclipse.m2e.core.maven2Nature</nature>
</natures>
<filteredResources>
<filter>
<id>1795780263877</id>
<name></name>
<type>30</type>
<matcher>
<id>org.eclipse.core.resources.regexFilterMatcher</id>
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
</matcher>
</filter>
</filteredResources>
</projectDescription>
4 changes: 4 additions & 0 deletions biscuit-core/.settings/org.eclipse.core.resources.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
eclipse.preferences.version=1
encoding//src/main/java=UTF-8
encoding//target/generated-sources/protobuf=UTF-8
encoding/<project>=UTF-8
2 changes: 2 additions & 0 deletions biscuit-core/.settings/org.eclipse.jdt.apt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
eclipse.preferences.version=1
org.eclipse.jdt.apt.aptEnabled=false
9 changes: 9 additions & 0 deletions biscuit-core/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
org.eclipse.jdt.core.compiler.compliance=11
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
org.eclipse.jdt.core.compiler.problem.forbiddenReference=error
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
org.eclipse.jdt.core.compiler.processAnnotations=disabled
org.eclipse.jdt.core.compiler.release=enabled
org.eclipse.jdt.core.compiler.source=11
4 changes: 4 additions & 0 deletions biscuit-core/.settings/org.eclipse.m2e.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
activeProfiles=
eclipse.preferences.version=1
resolveWorkspaceProjects=true
version=1
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ public Result<Void, Error> verify(PublicKey root)
if (this.next.getPublicKey().equals(currentKey)) {
return Result.ok(null);
} else {
return Result.err(
new Error.FormatError.Signature.InvalidSignature(
"signature error: Verification equation was not satisfied"));
return Result.err(new Error.FormatError.Signature.InvalidSignature("signature error"));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

package org.eclipse.biscuit.datalog;

import static biscuit.format.schema.Schema.CheckV2.Kind.All;
import static biscuit.format.schema.Schema.CheckV2.Kind.Reject;
import static biscuit.format.schema.Schema.Check.Kind.All;
import static biscuit.format.schema.Schema.Check.Kind.Reject;

import biscuit.format.schema.Schema;
import java.util.ArrayList;
Expand Down Expand Up @@ -42,8 +42,8 @@ public List<Rule> queries() {
return queries;
}

public Schema.CheckV2 serialize() {
Schema.CheckV2.Builder b = Schema.CheckV2.newBuilder();
public Schema.Check serialize() {
Schema.Check.Builder b = Schema.Check.newBuilder();

// do not set the kind to One to keep compatibility with older library versions
switch (this.kind) {
Expand All @@ -63,8 +63,8 @@ public Schema.CheckV2 serialize() {
return b.build();
}

public static Result<Check, Error.FormatError> deserializeV2(
Schema.CheckV2 check, DeserializationLimit.PerBlock limits, SymbolTable symbols) {
public static Result<Check, Error.FormatError> deserialize(
Schema.Check check, DeserializationLimit.PerBlock limits, SymbolTable symbols) {
ArrayList<Rule> queries = new ArrayList<>();

Kind kind;
Expand Down Expand Up @@ -95,8 +95,8 @@ public static Result<Check, Error.FormatError> deserializeV2(
Error.FormatError.DeserializationLimitError.Reason.TOO_MANY_QUERIES));
}

for (Schema.RuleV2 query : check.getQueriesList()) {
var res = Rule.deserializeV2(query, limits, symbols);
for (Schema.Rule query : check.getQueriesList()) {
var res = Rule.deserialize(query, limits, symbols);
if (res.isErr()) {
return Result.err(res.getErr());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@

package org.eclipse.biscuit.datalog;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Optional;
import java.util.Set;
import java.util.TreeSet;
import org.eclipse.biscuit.error.Error;
import org.eclipse.biscuit.error.Result;

Expand Down Expand Up @@ -62,7 +62,7 @@ public Optional<Result<Pair<Origin, Map<Long, Term>>, Error>> getNext() {
// we should return a value, but only once. To prevent further
// successful calls, we create a set of variables that cannot
// possibly be completed, so the next call will fail
Set<Long> set = new HashSet<>();
Set<Long> set = new TreeSet<>();
set.add((long) 0);

this.variables = new MatchedVariables(set);
Expand Down
14 changes: 9 additions & 5 deletions biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Fact.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.eclipse.biscuit.datalog;

import biscuit.format.schema.Schema;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;
import org.eclipse.biscuit.error.Error;
Expand Down Expand Up @@ -53,13 +54,16 @@ public String toString() {
return this.predicate.toString();
}

public Schema.FactV2 serialize() {
return Schema.FactV2.newBuilder().setPredicate(this.predicate.serialize()).build();
public Schema.Fact serialize() {
return Schema.Fact.newBuilder().setPredicate(this.predicate.serialize()).build();
}

public static Result<Fact, Error.FormatError> deserializeV2(
Schema.FactV2 fact, DeserializationLimit.PerBlock limits, SymbolTable symbols) {
var res = Predicate.deserializeV2(fact.getPredicate(), limits, symbols);
public static Comparator<Fact> comparator =
Comparator.comparing(Fact::predicate, Predicate.comparator);

public static Result<Fact, Error.FormatError> deserialize(
Schema.Fact fact, DeserializationLimit.PerBlock limits, SymbolTable symbols) {
var res = Predicate.deserialize(fact.getPredicate(), limits, symbols);
if (res.isErr()) {
return Result.err(res.getErr());
} else {
Expand Down
56 changes: 32 additions & 24 deletions biscuit-core/src/main/java/org/eclipse/biscuit/datalog/FactSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,36 @@
package org.eclipse.biscuit.datalog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.stream.Stream;

public final class FactSet {
/** facts grouped by (predicate name, arity), then by origin */
private final HashMap<Pair<Long, Integer>, HashMap<Origin, HashSet<Fact>>> facts;
private final SortedMap<Pair<Long, Integer>, SortedMap<Origin, SortedSet<Fact>>> facts;

private static final Comparator<Pair<Long, Integer>> KEY_COMPARATOR = Pair.naturalComparator();

public FactSet() {
facts = new HashMap<>();
facts = new TreeMap<>(KEY_COMPARATOR);
}

public FactSet(Origin o, HashSet<Fact> factSet) {
facts = new HashMap<>();
public FactSet(Origin o, SortedSet<Fact> factSet) {
facts = new TreeMap<>(KEY_COMPARATOR);
for (Fact fact : factSet) {
add(o, fact);
}
}

public Map<Origin, List<Fact>> byOrigin() {
Map<Origin, List<Fact>> origins = new HashMap<>();
for (HashMap<Origin, HashSet<Fact>> group : facts.values()) {
for (Map.Entry<Origin, HashSet<Fact>> entry : group.entrySet()) {
Map<Origin, List<Fact>> origins = new TreeMap<>(Origin.comparator);
for (SortedMap<Origin, SortedSet<Fact>> group : facts.values()) {
for (Map.Entry<Origin, SortedSet<Fact>> entry : group.entrySet()) {
origins.computeIfAbsent(entry.getKey(), k -> new ArrayList<>()).addAll(entry.getValue());
}
}
Expand All @@ -43,14 +48,15 @@ private static Pair<Long, Integer> keyOf(Predicate predicate) {

public void add(Origin origin, Fact fact) {
Pair<Long, Integer> key = keyOf(fact.predicate());
HashMap<Origin, HashSet<Fact>> group = facts.computeIfAbsent(key, k -> new HashMap<>());
group.computeIfAbsent(origin, k -> new HashSet<>()).add(fact);
SortedMap<Origin, SortedSet<Fact>> group =
facts.computeIfAbsent(key, k -> new TreeMap<>(Origin.comparator));
group.computeIfAbsent(origin, k -> new TreeSet<>(Fact.comparator)).add(fact);
}

public int size() {
int size = 0;
for (HashMap<Origin, HashSet<Fact>> group : facts.values()) {
for (HashSet<Fact> h : group.values()) {
for (SortedMap<Origin, SortedSet<Fact>> group : facts.values()) {
for (SortedSet<Fact> h : group.values()) {
size += h.size();
}
}
Expand All @@ -61,11 +67,11 @@ public int size() {
public FactSet clone() {
FactSet newFacts = new FactSet();

for (Map.Entry<Pair<Long, Integer>, HashMap<Origin, HashSet<Fact>>> group :
for (Map.Entry<Pair<Long, Integer>, SortedMap<Origin, SortedSet<Fact>>> group :
this.facts.entrySet()) {
HashMap<Origin, HashSet<Fact>> newGroup = new HashMap<>();
for (Map.Entry<Origin, HashSet<Fact>> entry : group.getValue().entrySet()) {
newGroup.put(entry.getKey(), new HashSet<>(entry.getValue()));
SortedMap<Origin, SortedSet<Fact>> newGroup = new TreeMap<>(Origin.comparator);
for (Map.Entry<Origin, SortedSet<Fact>> entry : group.getValue().entrySet()) {
newGroup.put(entry.getKey(), new TreeSet<>(entry.getValue()));
}
newFacts.facts.put(group.getKey(), newGroup);
}
Expand All @@ -74,19 +80,21 @@ public FactSet clone() {
}

public void merge(FactSet other) {
for (Map.Entry<Pair<Long, Integer>, HashMap<Origin, HashSet<Fact>>> group :
for (Map.Entry<Pair<Long, Integer>, SortedMap<Origin, SortedSet<Fact>>> group :
other.facts.entrySet()) {
HashMap<Origin, HashSet<Fact>> ownGroup =
facts.computeIfAbsent(group.getKey(), k -> new HashMap<>());
for (Map.Entry<Origin, HashSet<Fact>> entry : group.getValue().entrySet()) {
ownGroup.computeIfAbsent(entry.getKey(), k -> new HashSet<>()).addAll(entry.getValue());
SortedMap<Origin, SortedSet<Fact>> ownGroup =
facts.computeIfAbsent(group.getKey(), k -> new TreeMap<>(Origin.comparator));
for (Map.Entry<Origin, SortedSet<Fact>> entry : group.getValue().entrySet()) {
ownGroup
.computeIfAbsent(entry.getKey(), k -> new TreeSet<>(Fact.comparator))
.addAll(entry.getValue());
}
}
}

/** trusted facts sharing the predicate's name and arity, the terms are not checked */
public Stream<Pair<Origin, Fact>> stream(Predicate predicate, TrustedOrigins blockIds) {
HashMap<Origin, HashSet<Fact>> group = facts.get(keyOf(predicate));
SortedMap<Origin, SortedSet<Fact>> group = facts.get(keyOf(predicate));
if (group == null) {
return Stream.empty();
}
Expand All @@ -98,7 +106,7 @@ public Stream<Pair<Origin, Fact>> stream(Predicate predicate, TrustedOrigins blo
public Stream<Fact> stream() {
return facts.values().stream()
.flatMap(group -> group.values().stream())
.flatMap(HashSet::stream);
.flatMap(SortedSet::stream);
}

@Override
Expand Down
Loading