Skip to content

Commit

Permalink
Free up memory when accessing non-existent sub columns.
Browse files Browse the repository at this point in the history
  • Loading branch information
broneill committed Sep 14, 2024
1 parent afa8006 commit 87f56b9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
18 changes: 18 additions & 0 deletions src/main/java/org/cojen/tupl/table/PathSplitter.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,22 @@ private Entry doFind(String path) {

return e;
}

public synchronized void remove(Entry entry) {
Entry[] entries = mEntries;
int index = entry.mPath.hashCode() & (entries.length - 1);
for (Entry e = entries[index], prev = null; e != null; e = e.mNext) {
if (e == entry) {
if (prev == null) {
entries[index] = e.mNext;
} else {
prev.mNext = e.mNext;
}
mSize--;
return;
} else {
prev = e;
}
}
}
}
14 changes: 11 additions & 3 deletions src/main/java/org/cojen/tupl/table/RowMethodsMaker.java
Original file line number Diff line number Diff line change
Expand Up @@ -373,14 +373,16 @@ private void nameSwitch(Lookup lookup, MethodType type,
labels[i].here();
var subVar = rowVar.invoke(mColumns[i].name);

Label notNull = mm.label();
subVar.ifNe(null, notNull);
Label ready = mm.label();

subVar.ifNe(null, ready);
if (type.returnType().isPrimitive()) {
mm.var(RowMethodsMaker.class).invoke("nullJoin", nameVar, tailVar).throw_();
} else {
mm.return_(null);
}
notNull.here();

ready.here();

String name = mm.name();

Expand All @@ -400,6 +402,12 @@ private void nameSwitch(Lookup lookup, MethodType type,
mm.return_(rowMethodsVar.invoke(name, tailVar, subVar));
}
}

mm.catch_(ready, IllegalArgumentException.class, exVar -> {
// Remove a bogus entry from the PathSplitter to free up memory.
splitterVar.invoke("remove", entryVar);
exVar.throw_();
});
}

default_.here();
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/org/cojen/tupl/table/join/BasicJoinTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,13 @@ public void generated() throws Exception {
assertEquals(1, row.get_int("dept.companyId"));
assertEquals("Sales", row.getString("dept.name"));

try {
row.get("dept.fake.id");
fail();
} catch (IllegalArgumentException e) {
assertEquals("Column name isn't found: fake.id", e.getMessage());
}

row.set("dept.id", 123L);
row.set("dept.companyId", (Integer) null);
row.set("dept.name", "none");
Expand Down

0 comments on commit 87f56b9

Please sign in to comment.