Skip to content

Commit

Permalink
fix #321: properly handle baseline cleanup on entity delete
Browse files Browse the repository at this point in the history
update clarity to 3.1.0
  • Loading branch information
spheenik committed Sep 21, 2024
1 parent ecafd00 commit 31e4040
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 17 deletions.
23 changes: 9 additions & 14 deletions src/main/java/skadistats/clarity/model/state/BaselineRegistry.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public class BaselineRegistry {
private final int classCount;
private final int[] classBaselineIdx;
private final EntityState[] baselineStateCache;
private final int[][] entityBaselineDtClass;
private final int[] entityAlternateBaselineIdx;
private final EntityState[][] entityBaselineStateCache;

Expand All @@ -27,11 +26,8 @@ public BaselineRegistry(StringTable baselineTable, int entityCount) {
}

this.entityAlternateBaselineIdx = new int[entityCount];
this.entityBaselineDtClass = new int[entityCount][2];
this.entityBaselineStateCache = new EntityState[entityCount][2];
for (var i = 0; i < entityCount; i++) {
entityBaselineDtClass[i][0] = -1;
entityBaselineDtClass[i][1] = -1;
this.entityAlternateBaselineIdx[i] = -1;
}
}
Expand All @@ -41,14 +37,16 @@ public void clear() {
baselineStateCache[i] = null;
}
for (var i = 0; i < entityCount; i++) {
entityBaselineDtClass[i][0] = -1;
entityBaselineDtClass[i][1] = -1;
entityAlternateBaselineIdx[i] = -1;
entityBaselineStateCache[i][0] = null;
entityBaselineStateCache[i][1] = null;
clearEntity(i);
}
}

public void clearEntity(int entityIdx) {
entityAlternateBaselineIdx[entityIdx] = -1;
entityBaselineStateCache[entityIdx][0] = null;
entityBaselineStateCache[entityIdx][1] = null;
}

public void updateClassBaselineIndex(int dtClassId, int baselineIdx) {
classBaselineIdx[dtClassId] = baselineIdx;
}
Expand Down Expand Up @@ -81,24 +79,21 @@ public void setClassBaselineState(int baselineIdx, EntityState state) {
baselineStateCache[baselineIdx] = state;
}

public EntityState getEntityBaselineState(int entityIdx, int baseline, int dtClassId) {
public EntityState getEntityBaselineState(int entityIdx, int baseline) {
// FIXME: not sure whether to ignore entity baselines when an alternate baseline is found
// FIXME: do so for now, remove next line if entity baseline should be preferred
if (entityAlternateBaselineIdx[entityIdx] != -1) return null;
if (entityBaselineDtClass[entityIdx][baseline] != dtClassId) return null;
return entityBaselineStateCache[entityIdx][baseline];
}

public void updateEntityBaseline(int iFrom, int entityIdx, int dtClassId, EntityState state) {
public void updateEntityBaseline(int iFrom, int entityIdx, EntityState state) {
var iTo = 1 - iFrom;
entityBaselineDtClass[entityIdx][iTo] = dtClassId;
entityBaselineStateCache[entityIdx][iTo] = state;
}

public void switchEntityBaselines(int iFrom) {
var iTo = 1 - iFrom;
for (var j = 0; j < entityCount; j++) {
entityBaselineDtClass[j][iTo] = entityBaselineDtClass[j][iFrom];
entityBaselineStateCache[j][iTo] = entityBaselineStateCache[j][iFrom];
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,6 @@ private void executeEntityCreate(int eIdx, int serial, DTClass dtClass, CommonNe
baselineRegistry.updateEntityBaseline(
message.getBaseline(),
eIdx,
dtClass.getClassId(),
newState.copy()
);
}
Expand All @@ -484,7 +483,6 @@ private void executeEntityRecreate(Entity entity, CommonNetMessages.CSVCMsg_Pack
baselineRegistry.updateEntityBaseline(
message.getBaseline(),
eIdx,
dtClass.getClassId(),
newState.copy()
);
}
Expand Down Expand Up @@ -541,6 +539,7 @@ private void executeEntityDelete(Entity entity) {
assert entity.isExistent();
entity.setExistent(false);
entities.removeEntity(entity);
baselineRegistry.clearEntity(entity.getIndex());
logModification("DELETE", entity);
emitDeletedEvent(entity);
}
Expand Down Expand Up @@ -595,7 +594,7 @@ private void logModification(String which, Entity entity) {
private EntityState getBaseline(int clsId, int baseline, int entityIdx, boolean delta) {
EntityState s;
if (delta) {
s = baselineRegistry.getEntityBaselineState(entityIdx, baseline, clsId);
s = baselineRegistry.getEntityBaselineState(entityIdx, baseline);
if (s != null) {
return s;
}
Expand Down

0 comments on commit 31e4040

Please sign in to comment.