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

Optionally specify limit for number of entities in a record. #373

Merged
merged 4 commits into from
Dec 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 10 additions & 0 deletions metafix-runner/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,13 @@ application {
]
}
}

tasks.withType(JavaExec) {
doFirst {
def prefix = project.group + '.'

System.properties.each { k, v ->
if (k.startsWith(prefix)) systemProperties[k] = v
}
}
}
26 changes: 23 additions & 3 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 Down Expand Up @@ -239,7 +241,7 @@ public void startRecord(final String identifier) {
flattener.startRecord(identifier);
entityCountStack.clear();
entityCount = 0;
entityCountStack.add(Integer.valueOf(entityCount));
entityCountStack.add(entityCount);
recordIdentifier = identifier;
entities = new ArrayList<>();
}
Expand Down Expand Up @@ -313,22 +315,36 @@ public void startEntity(final String name) {
throw new IllegalArgumentException("Entity name must not be null.");
}

++entityCount;
if (maxEntityCountExceeded()) {
LOG.debug("Maximum number of entities exceeded: {}/{}", entityCount, MAX_ENTITY_COUNT);
blackwinter marked this conversation as resolved.
Show resolved Hide resolved
return;
}

final Value value = isArrayName(name) ? Value.newArray() : Value.newHash();
addValue(name, value);
entities.add(value);

entityCountStack.push(Integer.valueOf(++entityCount));
entityCountStack.push(entityCount);
flattener.startEntity(name);
}

@Override
public void endEntity() {
entityCountStack.pop().intValue();
if (maxEntityCountExceeded()) {
return;
}

entityCountStack.pop();
flattener.endEntity();
}

@Override
public void literal(final String name, final String value) {
if (maxEntityCountExceeded()) {
return;
}

LOG.debug("Putting '{}': '{}'", name, value);
flattener.literal(name, value);
}
Expand Down Expand Up @@ -438,6 +454,10 @@ public String getEntityMemberName() {
return entityMemberName;
}

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

public enum Strictness {

/**
Expand Down
Loading