Skip to content

Commit 1fed102

Browse files
committed
Optionally specify limit for number of entities in a record.
This is a brute-force approach to dealing with OOM situations when Alma records have an excessive number of items (e.g. 99374518570506441: >12000 entities = ~10 GB heap for the Record instance). Use Metafix instance setter `setMaxEntityCount(int)` or set system property `org.metafacture.metafix.maxEntityCount=<int>`. Alternative options: - Increase maximum heap size for JVM. - Significantly reduce memory requirement for Record instances.
1 parent e981f30 commit 1fed102

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

metafix-runner/build.gradle

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,13 @@ application {
4949
]
5050
}
5151
}
52+
53+
tasks.withType(JavaExec) {
54+
doFirst {
55+
def prefix = project.group + '.'
56+
57+
System.properties.each { k, v ->
58+
if (k.startsWith(prefix)) systemProperties[k] = v
59+
}
60+
}
61+
}

metafix/src/main/java/org/metafacture/metafix/Metafix.java

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ public class Metafix implements StreamPipe<StreamReceiver>, Maps {
101101
private boolean repeatedFieldsToEntities;
102102
private boolean strictnessHandlesProcessExceptions;
103103
private int entityCount;
104+
private int maxEntityCount = Integer.getInteger("org.metafacture.metafix.maxEntityCount", -1);
104105

105106
public Metafix() {
106107
this(NO_VARS);
@@ -313,22 +314,36 @@ public void startEntity(final String name) {
313314
throw new IllegalArgumentException("Entity name must not be null.");
314315
}
315316

317+
++entityCount;
318+
if (maxEntityCountExceeded()) {
319+
LOG.debug("Maximum number of entities exceeded: {}/{}", entityCount, maxEntityCount);
320+
return;
321+
}
322+
316323
final Value value = isArrayName(name) ? Value.newArray() : Value.newHash();
317324
addValue(name, value);
318325
entities.add(value);
319326

320-
entityCountStack.push(++entityCount);
327+
entityCountStack.push(entityCount);
321328
flattener.startEntity(name);
322329
}
323330

324331
@Override
325332
public void endEntity() {
333+
if (maxEntityCountExceeded()) {
334+
return;
335+
}
336+
326337
entityCountStack.pop();
327338
flattener.endEntity();
328339
}
329340

330341
@Override
331342
public void literal(final String name, final String value) {
343+
if (entityCountStack.size() > 1 && maxEntityCountExceeded()) {
344+
return;
345+
}
346+
332347
LOG.debug("Putting '{}': '{}'", name, value);
333348
flattener.literal(name, value);
334349
}
@@ -438,6 +453,18 @@ public String getEntityMemberName() {
438453
return entityMemberName;
439454
}
440455

456+
public void setMaxEntityCount(final int maxEntityCount) {
457+
this.maxEntityCount = maxEntityCount;
458+
}
459+
460+
public int getMaxEntityCount() {
461+
return maxEntityCount;
462+
}
463+
464+
private boolean maxEntityCountExceeded() {
465+
return maxEntityCount >= 0 && entityCount > maxEntityCount;
466+
}
467+
441468
public enum Strictness {
442469

443470
/**

0 commit comments

Comments
 (0)