Skip to content
This repository was archived by the owner on May 16, 2025. It is now read-only.

Commit f70996f

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 2d57726 commit f70996f

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
@@ -47,3 +47,13 @@ application {
4747
applicationDefaultJvmArgs = ["-agentlib:hprof=heap=sites,cpu=samples,depth=${depth},cutoff=${cutoff},file=${file}.hprof.txt"]
4848
}
4949
}
50+
51+
tasks.withType(JavaExec) {
52+
doFirst {
53+
def prefix = project.group + '.'
54+
55+
System.properties.each { k, v ->
56+
if (k.startsWith(prefix)) systemProperties[k] = v
57+
}
58+
}
59+
}

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

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ public class Metafix implements StreamPipe<StreamReceiver>, Maps { // checkstyle
9393
private boolean repeatedFieldsToEntities;
9494
private boolean strictnessHandlesProcessExceptions;
9595
private int entityCount;
96+
private int maxEntityCount = Integer.getInteger("org.metafacture.metafix.maxEntityCount", -1);
9697

9798
public Metafix() {
9899
this(NO_VARS);
@@ -305,22 +306,36 @@ public void startEntity(final String name) {
305306
throw new IllegalArgumentException("Entity name must not be null.");
306307
}
307308

309+
++entityCount;
310+
if (maxEntityCountExceeded()) {
311+
LOG.debug("Maximum number of entities exceeded: {}/{}", entityCount, maxEntityCount);
312+
return;
313+
}
314+
308315
final Value value = isArrayName(name) ? Value.newArray() : Value.newHash();
309316
addValue(name, value);
310317
entities.add(value);
311318

312-
entityCountStack.push(++entityCount);
319+
entityCountStack.push(entityCount);
313320
flattener.startEntity(name);
314321
}
315322

316323
@Override
317324
public void endEntity() {
325+
if (maxEntityCountExceeded()) {
326+
return;
327+
}
328+
318329
entityCountStack.pop();
319330
flattener.endEntity();
320331
}
321332

322333
@Override
323334
public void literal(final String name, final String value) {
335+
if (entityCountStack.size() > 1 && maxEntityCountExceeded()) {
336+
return;
337+
}
338+
324339
LOG.debug("Putting '{}': '{}'", name, value);
325340
flattener.literal(name, value);
326341
}
@@ -430,6 +445,18 @@ public String getEntityMemberName() {
430445
return entityMemberName;
431446
}
432447

448+
public void setMaxEntityCount(final int maxEntityCount) {
449+
this.maxEntityCount = maxEntityCount;
450+
}
451+
452+
public int getMaxEntityCount() {
453+
return maxEntityCount;
454+
}
455+
456+
private boolean maxEntityCountExceeded() {
457+
return maxEntityCount >= 0 && entityCount > maxEntityCount;
458+
}
459+
433460
public enum Strictness {
434461

435462
/**

0 commit comments

Comments
 (0)