Skip to content

Commit

Permalink
Implement optimizations for limiting number of entities. (#373)
Browse files Browse the repository at this point in the history
  • Loading branch information
blackwinter committed Oct 18, 2024
1 parent 1fed102 commit dc4f1af
Showing 1 changed file with 5 additions and 12 deletions.
17 changes: 5 additions & 12 deletions metafix/src/main/java/org/metafacture/metafix/Metafix.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ public class Metafix implements StreamPipe<StreamReceiver>, Maps {

public static final Map<String, String> NO_VARS = Collections.emptyMap();

public static final int MAX_ENTITY_COUNT = Integer.getInteger("org.metafacture.metafix.maxEntityCount", -1);

private static final Logger LOG = LoggerFactory.getLogger(Metafix.class);

private static final String ENTITIES_NOT_BALANCED = "Entity starts and ends are not balanced";
Expand All @@ -101,7 +103,6 @@ public class Metafix implements StreamPipe<StreamReceiver>, Maps {
private boolean repeatedFieldsToEntities;
private boolean strictnessHandlesProcessExceptions;
private int entityCount;
private int maxEntityCount = Integer.getInteger("org.metafacture.metafix.maxEntityCount", -1);

public Metafix() {
this(NO_VARS);
Expand Down Expand Up @@ -316,7 +317,7 @@ public void startEntity(final String name) {

++entityCount;
if (maxEntityCountExceeded()) {
LOG.debug("Maximum number of entities exceeded: {}/{}", entityCount, maxEntityCount);
LOG.debug("Maximum number of entities exceeded: {}/{}", entityCount, MAX_ENTITY_COUNT);
return;
}

Expand All @@ -340,7 +341,7 @@ public void endEntity() {

@Override
public void literal(final String name, final String value) {
if (entityCountStack.size() > 1 && maxEntityCountExceeded()) {
if (maxEntityCountExceeded()) {
return;
}

Expand Down Expand Up @@ -453,16 +454,8 @@ public String getEntityMemberName() {
return entityMemberName;
}

public void setMaxEntityCount(final int maxEntityCount) {
this.maxEntityCount = maxEntityCount;
}

public int getMaxEntityCount() {
return maxEntityCount;
}

private boolean maxEntityCountExceeded() {
return maxEntityCount >= 0 && entityCount > maxEntityCount;
return MAX_ENTITY_COUNT >= 0 && entityCount > MAX_ENTITY_COUNT;
}

public enum Strictness {
Expand Down

0 comments on commit dc4f1af

Please sign in to comment.