Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow arbitrary expression IDs (closes #163) #164

Merged
merged 2 commits into from
Nov 20, 2023
Merged
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
29 changes: 14 additions & 15 deletions src/main/java/com/gliwka/hyperscan/wrapper/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* Database containing compiled expressions ready for scanning using the Scanner
*/
public class Database implements Closeable {
private final Expression[] expressions;
private final Map<Integer, Expression> expressions;
private final int expressionCount;

private NativeDatabase database;
Expand All @@ -38,13 +38,17 @@ private Database(NativeDatabase database, List<Expression> expressions) {

boolean hasIds = expressions.get(0).getId() != null;

this.expressions = new HashMap<>(expressionCount);
if (hasIds) {
int maxId = expressions.stream().mapToInt(Expression::getId).max().getAsInt();
this.expressions = new Expression[maxId + 1];

expressions.forEach(expression -> this.expressions[expression.getId()] = expression);
for (Expression expression : expressions) {
if (this.expressions.put(expression.getId(), expression) != null)
throw new IllegalStateException("Expression ID must be unique within a Database.");
}
} else {
this.expressions = expressions.toArray(new Expression[0]);
int i = 0;
for (Expression expression : expressions) {
this.expressions.put(i++, expression);
}
}
}

Expand Down Expand Up @@ -94,7 +98,6 @@ public static Database compile(List<Expression> expressions) throws CompileError
throw new IllegalStateException("You can't mix expressions with and without id's in a single database");
}


for (int i = 0; i < expressionsSize; i++) {
flags[i] = expressions.get(i).getFlagBits();

Expand Down Expand Up @@ -141,11 +144,7 @@ public long getSize() {
}

Expression getExpression(int id) {
return expressions[id];
}

int getExpressionCount() {
return expressionCount;
return expressions.get(id);
}

@Override
Expand Down Expand Up @@ -179,7 +178,7 @@ public void save(OutputStream expressionsOut, OutputStream databaseOut) throws I
DataOutputStream expressionsDataOut = new DataOutputStream(expressionsOut);
// How many expressions will be present. We need this to know when to stop reading.
expressionsDataOut.writeInt(expressionCount);
for (Expression expression : expressions) {
for (Expression expression : expressions.values()) {
if (expression == null) {
continue;
}
Expand Down Expand Up @@ -289,13 +288,13 @@ public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Database database = (Database) o;
return expressionCount == database.expressionCount && Arrays.deepEquals(expressions, database.expressions);
return expressionCount == database.expressionCount && expressions.equals(database.expressions);
}

@Override
public int hashCode() {
int result = Objects.hash(expressionCount);
result = 31 * result + Arrays.hashCode(expressions);
result = 31 * result + expressions.hashCode();
return result;
}
}
22 changes: 10 additions & 12 deletions src/main/java/com/gliwka/hyperscan/wrapper/Expression.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
@ToString
public class Expression {
@Getter @NonNull private final String expression;
@Getter private EnumSet<ExpressionFlag> flags = EnumSet.of(ExpressionFlag.NO_FLAG);
@Getter private Integer id;
@Getter private final EnumSet<ExpressionFlag> flags;
@Getter private final Integer id;

/**
* Represents the validation results for a expression
Expand All @@ -37,34 +37,32 @@ public static class ValidationResult {
}

public Expression(@NonNull String expression) {
this.expression = expression;
this(expression, EnumSet.of(ExpressionFlag.NO_FLAG), null);
}

public Expression(@NonNull String expression, Integer id) {
this.expression = expression;
this.id = id;
this(expression, EnumSet.of(ExpressionFlag.NO_FLAG), id);
}

public Expression(@NonNull String expression, @NonNull EnumSet<ExpressionFlag> flags) {
this.expression = expression;
this.flags = flags;
this(expression, flags, null);
}

public Expression(@NonNull String expression, @NonNull ExpressionFlag flag) {
this.expression = expression;
this.flags = EnumSet.of(flag);
this(expression, EnumSet.of(flag), null);
}

public Expression(@NonNull String expression, @NonNull EnumSet<ExpressionFlag> flags, Integer id) {
if (id != null && id < 0)
throw new IllegalArgumentException("id must be >=0: " + id);

this.expression = expression;
this.flags = flags;
this.id = id;
}

public Expression(@NonNull String expression, @NonNull ExpressionFlag flag, Integer id) {
this.expression = expression;
this.flags = EnumSet.of(flag);
this.id = id;
this(expression, EnumSet.of(flag), id);
}

public ValidationResult validate() {
Expand Down
Loading