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
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,7 @@ public static byte[] generateBlockSignaturePayloadV0(
}
var toVerify = ByteBuffer.allocate(capacity).order(ByteOrder.LITTLE_ENDIAN);
toVerify.put(payload);
if (externalSignature.isPresent()) {
toVerify.put(externalSignature.get().getSignature());
}
externalSignature.ifPresent(signature -> toVerify.put(signature.getSignature()));
toVerify.putInt(nextKey.getAlgorithm().getNumber());
toVerify.put(nextKeyBytes);
toVerify.flip();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public Schema.CheckV2 serialize() {
default:
}

for (int i = 0; i < this.queries.size(); i++) {
b.addQueries(this.queries.get(i).serialize());
for (Rule query : this.queries) {
b.addQueries(query.serialize());
}

return b.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ public boolean hasNext() {

@Override
public Pair<Origin, Map<Long, Term>> next() {
if (this.nextElement == null || !this.nextElement.isPresent()) {
if (this.nextElement == null || this.nextElement.isEmpty()) {
this.nextElement = getNext();
}
if (this.nextElement == null || !this.nextElement.isPresent()) {
if (this.nextElement == null || this.nextElement.isEmpty()) {
throw new NoSuchElementException();
} else {
Pair<Origin, Map<Long, Term>> t = this.nextElement.get();
Expand Down Expand Up @@ -136,10 +136,6 @@ public Optional<Pair<Origin, Map<Long, Term>>> getNext() {
}
}

if (this.currentIt == null) {
return Optional.empty();
}

Optional<Pair<Origin, Map<Long, Term>>> opt = this.currentIt.getNext();

if (opt.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void merge(FactSet other) {
}
}

public Stream stream(TrustedOrigins blockIds) {
public Stream<Pair<Origin, Fact>> stream(TrustedOrigins blockIds) {
return facts.entrySet().stream()
.filter(
entry -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public Optional<Term> get(final long key) {
}

public boolean isComplete() {
return this.variables.values().stream().allMatch((v) -> v.isPresent());
return this.variables.values().stream().allMatch(Optional::isPresent);
}

public Optional<Map<Long, Term>> complete() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ public boolean match(final Predicate rulePredicate) {
}

public Predicate clone() {
final List<Term> terms = new ArrayList<>();
terms.addAll(this.terms);
final List<Term> terms = new ArrayList<>(this.terms);
return new Predicate(this.name, terms);
}

Expand Down Expand Up @@ -78,19 +77,17 @@ public int hashCode() {
public String toString() {
return this.name
+ "("
+ String.join(
", ",
this.terms.stream()
.map((i) -> (i == null) ? "(null)" : i.toString())
.collect(Collectors.toList()))
+ this.terms.stream()
.map((i) -> (i == null) ? "(null)" : i.toString())
.collect(Collectors.joining(", "))
+ ")";
}

public Schema.PredicateV2 serialize() {
Schema.PredicateV2.Builder builder = Schema.PredicateV2.newBuilder().setName(this.name);

for (int i = 0; i < this.terms.size(); i++) {
builder.addTerms(this.terms.get(i).serializeTerm());
for (Term term : this.terms) {
builder.addTerms(term.serializeTerm());
}

return builder.build();
Expand Down
12 changes: 6 additions & 6 deletions biscuit-core/src/main/java/org/eclipse/biscuit/datalog/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ public Stream<Result<Pair<Origin, Fact>, Error>> apply(
if (!generatedVariables.containsKey(var.value())) {
// throw new Error("variables that appear in the head should appear in the body
// as well");
return Stream.of(Result.<Pair<Origin, Fact>, Error>err(new Error.InternalError()));
return Stream.of(Result.err(new Error.InternalError()));
}
p.terms().set(index, generatedVariables.get(var.value()));
}
}

origin.add(ruleOrigin);
return Stream.of(Result.<Pair<Origin, Fact>, Error>ok(new Pair<>(origin, new Fact(p))));
return Stream.of(Result.ok(new Pair<>(origin, new Fact(p))));
});
}

Expand Down Expand Up @@ -194,12 +194,12 @@ public Rule(
public Schema.RuleV2 serialize() {
Schema.RuleV2.Builder b = Schema.RuleV2.newBuilder().setHead(this.head.serialize());

for (int i = 0; i < this.body.size(); i++) {
b.addBody(this.body.get(i).serialize());
for (Predicate predicate : this.body) {
b.addBody(predicate.serialize());
}

for (int i = 0; i < this.expressions.size(); i++) {
b.addExpressions(this.expressions.get(i).serialize());
for (Expression expression : this.expressions) {
b.addExpressions(expression.serialize());
}

for (Scope scope : this.scopes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.eclipse.biscuit.datalog;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import org.eclipse.biscuit.crypto.PublicKey;
import org.eclipse.biscuit.datalog.expressions.Expression;
Expand Down Expand Up @@ -130,10 +131,8 @@ private static boolean containsV33Op(List<Expression> expressions) {
for (Op op : e.getOps()) {
if (op instanceof Op.Unary) {
Op.Unary b = (Op.Unary) op;
switch (b.getOp()) {
case TypeOf:
return true;
default:
if (Objects.requireNonNull(b.getOp()) == Op.UnaryOp.TypeOf) {
return true;
}
} else if (op instanceof Op.Binary) {
Op.Binary b = (Op.Binary) op;
Expand Down Expand Up @@ -174,9 +173,6 @@ private static boolean containsV33Term(Term term) {
if (term instanceof Term.Array) {
return true;
}
if (term instanceof Term.Map) {
return true;
}
return false;
return term instanceof Term.Map;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public enum Kind {
PublicKey
}

private Kind kind;
private long publicKey;
private final Kind kind;
private final long publicKey;

private Scope(Kind kind, long publicKey) {
this.kind = kind;
Expand Down Expand Up @@ -106,7 +106,7 @@ public boolean equals(Object o) {
@Override
public int hashCode() {
int result = kind != null ? kind.hashCode() : 0;
result = 31 * result + (int) (publicKey ^ (publicKey >>> 32));
result = 31 * result + Long.hashCode(publicKey);
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public String fromEpochIsoDate(long epochSec) {
private final List<PublicKey> publicKeys;

public long insert(final String symbol) {
int index = this.DEFAULT_SYMBOLS.indexOf(symbol);
int index = DEFAULT_SYMBOLS.indexOf(symbol);
if (index == -1) {
index = this.symbols.indexOf(symbol);
if (index == -1) {
Expand Down Expand Up @@ -114,7 +114,7 @@ public Term add(final String symbol) {

public Optional<Long> get(final String symbol) {
// looking for symbol in default symbols
long index = this.DEFAULT_SYMBOLS.indexOf(symbol);
long index = DEFAULT_SYMBOLS.indexOf(symbol);
if (index == -1) {
// looking for symbol in usages defined symbols
index = this.symbols.indexOf(symbol);
Expand All @@ -129,8 +129,8 @@ public Optional<Long> get(final String symbol) {
}

public Optional<String> getSymbol(int i) {
if (i >= 0 && i < this.DEFAULT_SYMBOLS.size() && i < DEFAULT_SYMBOLS_OFFSET) {
return Optional.of(this.DEFAULT_SYMBOLS.get(i));
if (i >= 0 && i < DEFAULT_SYMBOLS.size() && i < DEFAULT_SYMBOLS_OFFSET) {
return Optional.of(DEFAULT_SYMBOLS.get(i));
} else if (i >= DEFAULT_SYMBOLS_OFFSET && i < this.symbols.size() + DEFAULT_SYMBOLS_OFFSET) {
return Optional.of(this.symbols.get(i - DEFAULT_SYMBOLS_OFFSET));
} else {
Expand All @@ -155,9 +155,9 @@ public String formatRule(final Rule r) {

public String formatRuleBody(final Rule r) {
final List<String> preds =
r.body().stream().map((p) -> this.formatPredicate(p)).collect(Collectors.toList());
r.body().stream().map(this::formatPredicate).collect(Collectors.toList());
final List<String> expressions =
r.expressions().stream().map((c) -> this.formatExpression(c)).collect(Collectors.toList());
r.expressions().stream().map(this::formatExpression).collect(Collectors.toList());

String res = String.join(", ", preds);
if (!expressions.isEmpty()) {
Expand All @@ -170,7 +170,7 @@ public String formatRuleBody(final Rule r) {
if (!r.scopes().isEmpty()) {
res += " trusting ";
final List<String> scopes =
r.scopes().stream().map((s) -> this.formatScope(s)).collect(Collectors.toList());
r.scopes().stream().map(this::formatScope).collect(Collectors.toList());
res += String.join(", ", scopes);
}
return res;
Expand Down Expand Up @@ -199,13 +199,7 @@ public String formatScope(final Scope scope) {
}

public String formatPredicate(final Predicate p) {
List<String> ids =
p.terms().stream()
.map(
(t) -> {
return this.formatTerm(t);
})
.collect(Collectors.toList());
List<String> ids = p.terms().stream().map(this::formatTerm).collect(Collectors.toList());
return Optional.ofNullable(this.formatSymbol((int) p.name())).orElse("<?>")
+ "("
+ String.join(", ", ids)
Expand All @@ -220,15 +214,14 @@ public String formatTerm(final Term i) {
} else if (i instanceof Term.Date) {
return fromEpochIsoDate(((Term.Date) i).value());
} else if (i instanceof Term.Integer) {
return "" + ((Term.Integer) i).value();
return String.valueOf(((Term.Integer) i).value());
} else if (i instanceof Term.Str) {
return "\"" + this.formatSymbol((int) ((Term.Str) i).value()) + "\"";
} else if (i instanceof Term.Bytes) {
return "hex:" + Utils.byteArrayToHexString(((Term.Bytes) i).value()).toLowerCase();
} else if (i instanceof Term.Set) {
final List<String> values =
((Term.Set) i)
.value().stream().map((v) -> this.formatTerm(v)).collect(Collectors.toList());
((Term.Set) i).value().stream().map(this::formatTerm).collect(Collectors.toList());
if (values.isEmpty()) {
return "{,}";
} else {
Expand All @@ -239,11 +232,10 @@ public String formatTerm(final Term i) {
return "null";
} else if (i instanceof Term.Array) {
final List<String> values =
((Term.Array) i)
.value().stream().map((v) -> this.formatTerm(v)).collect(Collectors.toList());
((Term.Array) i).value().stream().map(this::formatTerm).collect(Collectors.toList());
return "[" + String.join(", ", values) + "]";
} else if (i instanceof Term.Map) {
ArrayList<String> entries = new ArrayList();
ArrayList<String> entries = new ArrayList<>();
for (Map.Entry<MapKey, Term> entry : ((Term.Map) i).value().entrySet()) {
entries.add(formatTerm(entry.getKey()) + ": " + formatTerm(entry.getValue()));
}
Expand Down Expand Up @@ -275,15 +267,15 @@ public String formatCheck(final Check c) {
break;
}
final List<String> queries =
c.queries().stream().map((q) -> this.formatRuleBody(q)).collect(Collectors.toList());
c.queries().stream().map(this::formatRuleBody).collect(Collectors.toList());
return prefix + String.join(" or ", queries);
}

public String formatWorld(final World w) {
final List<String> facts =
w.getFacts().stream().map((f) -> this.formatFact(f)).collect(Collectors.toList());
w.getFacts().stream().map(this::formatFact).collect(Collectors.toList());
final List<String> rules =
w.getRules().stream().map((r) -> this.formatRule(r)).collect(Collectors.toList());
w.getRules().stream().map(this::formatRule).collect(Collectors.toList());

StringBuilder b = new StringBuilder();
b.append("World {\n\tfacts: [\n\t\t");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
import java.util.Optional;

public final class TemporarySymbolTable {
private SymbolTable base;
private int offset;
private List<String> symbols;
private final SymbolTable base;
private final int offset;
private final List<String> symbols;

public TemporarySymbolTable(SymbolTable base) {
this.offset = DEFAULT_SYMBOLS_OFFSET + base.currentOffset();
Expand Down Expand Up @@ -42,7 +42,7 @@ public long insert(final String symbol) {

int index = this.symbols.indexOf(symbol);
if (index != -1) {
return (long) (this.offset + index);
return this.offset + index;
}
this.symbols.add(symbol);
return this.symbols.size() - 1 + this.offset;
Expand Down
Loading