Resolve FTR relation proxies consistently and preserve end times

This commit is contained in:
eyck committed 2026-11-14 17:27:49 +01:00
1 parent 82c4dcf552
commit c85a842dbe
2 files changed
+87 -21

No files matched your search

@@ -10,11 +10,11 @@
*******************************************************************************/ *******************************************************************************/
package com.minres.scviewer.database.ftr; package com.minres.scviewer.database.ftr;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import com.minres.scviewer.database.IWaveform; import com.minres.scviewer.database.IWaveform;
import com.minres.scviewer.database.LazyValue;
import com.minres.scviewer.database.RelationType; import com.minres.scviewer.database.RelationType;
import com.minres.scviewer.database.tx.ITx; import com.minres.scviewer.database.tx.ITx;
import com.minres.scviewer.database.tx.ITxAttribute; import com.minres.scviewer.database.tx.ITxAttribute;
@@ -82,7 +82,29 @@ class TxRelation implements ITxRelation {
final long txId; final long txId;
ITx tx = null; private final LazyValue<ITx> resolved = new LazyValue<>();
private ITx resolve() {
return resolved.get(() -> {
ITx transaction = loader.getTransaction(txId);
if (transaction != null) return transaction;
TxStream stream = loader.txStreams.get(fiberId);
if (stream != null) {
stream.loadStream();
transaction = loader.getTransaction(txId);
} else if (fiberId < 0) {
// Older FTR relations do not record the endpoint stream.
for (TxStream candidate : loader.txStreams.values()) {
candidate.loadStream();
transaction = loader.getTransaction(txId);
if (transaction != null) break;
}
}
if (transaction == null)
throw new IllegalStateException("Missing related transaction " + txId + " in stream " + fiberId);
return transaction;
});
}
public TxFacade(long fiberId, long txId) { public TxFacade(long fiberId, long txId) {
this.fiberId = fiberId; this.fiberId = fiberId;
@@ -91,7 +113,9 @@ class TxRelation implements ITxRelation {
@Override @Override
public int compareTo(ITx o) { public int compareTo(ITx o) {
return tx==null?-1:tx.compareTo(o); if (this == o) return 0;
int cmp = Long.compare(getBeginTime(), o.getBeginTime());
return cmp != 0 ? cmp : Long.compare(getId(), o.getId());
} }
@Override @Override
@@ -101,52 +125,42 @@ class TxRelation implements ITxRelation {
@Override @Override
public IWaveform getStream() { public IWaveform getStream() {
if(tx==null) { return resolve().getStream();
TxStream fiber = loader.txStreams.get(fiberId);
fiber.loadStream();
tx = loader.getTransaction(txId);
return fiber;
} else
return tx.getStream();
} }
@Override @Override
public IWaveform getGenerator() { public IWaveform getGenerator() {
if(tx==null) { return resolve().getGenerator();
loader.txStreams.get(fiberId).loadStream();
tx = loader.getTransaction(txId);
}
return tx!=null?tx.getGenerator():null;
} }
@Override @Override
public long getBeginTime() { public long getBeginTime() {
return tx==null?-1:tx.getBeginTime(); return resolve().getBeginTime();
} }
@Override @Override
public long getEndTime() { public long getEndTime() {
return tx==null?-1:tx.getBeginTime(); return resolve().getEndTime();
} }
@Override @Override
public List<ITxAttribute> getAttributes() { public List<ITxAttribute> getAttributes() {
return tx==null?new ArrayList<>():tx.getAttributes(); return resolve().getAttributes();
} }
@Override @Override
public Collection<ITxRelation> getIncomingRelations() { public Collection<ITxRelation> getIncomingRelations() {
return tx==null?new ArrayList<>():tx.getIncomingRelations(); return resolve().getIncomingRelations();
} }
@Override @Override
public Collection<ITxRelation> getOutgoingRelations() { public Collection<ITxRelation> getOutgoingRelations() {
return tx==null?new ArrayList<>():tx.getOutgoingRelations(); return resolve().getOutgoingRelations();
} }
@Override @Override
public String toString() { public String toString() {
return tx==null?("tx#" + getId() + "[not available]"):tx.toString(); return "tx#" + txId + "[related transaction]";
} }
} }
} }
@@ -0,0 +1,52 @@
package com.minres.scviewer.database.test;
import static org.junit.Assert.*;
import java.lang.reflect.Constructor;
import org.junit.Test;
import com.minres.scviewer.database.RelationType;
import com.minres.scviewer.database.RelationTypeFactory;
import com.minres.scviewer.database.ftr.FtrDbLoader;
import com.minres.scviewer.database.tx.ITx;
import com.minres.scviewer.database.tx.ITxRelation;
public class FtrRelationTest {
private static class Loader extends FtrDbLoader {
ITx available;
@Override public synchronized ITx getTransaction(long id) { return available; }
}
private ITx proxy(Loader loader) throws Exception {
Class<?> raw = Class.forName("com.minres.scviewer.database.ftr.FtrRelation");
Constructor<?> rawConstructor = raw.getDeclaredConstructor(RelationType.class, long.class,
long.class, long.class, long.class);
rawConstructor.setAccessible(true);
Object relation = rawConstructor.newInstance(RelationTypeFactory.create("test"), 1L, 1L, 0L, 0L);
Class<?> wrapper = Class.forName("com.minres.scviewer.database.ftr.TxRelation");
Constructor<?> constructor = wrapper.getDeclaredConstructor(FtrDbLoader.class, raw);
constructor.setAccessible(true);
return ((ITxRelation) constructor.newInstance(loader, relation)).getSource();
}
@Test
public void resolvesFromTimeGetterAndKeepsDuration() throws Exception {
Loader loader = new Loader();
ITx proxy = proxy(loader);
TxTestSupport.Tx real = new TxTestSupport.Tx(1, 10, 50);
loader.available = real;
assertEquals(50, proxy.getEndTime());
assertEquals(10, proxy.getBeginTime());
assertSame(real.getAttributes(), proxy.getAttributes());
assertEquals(0, proxy.compareTo(proxy));
assertEquals(0, proxy.compareTo(real));
assertEquals(0, real.compareTo(proxy));
TxTestSupport.Tx later = new TxTestSupport.Tx(2, 20, 60);
assertEquals(-Integer.signum(later.compareTo(proxy)), Integer.signum(proxy.compareTo(later)));
}
@Test
public void missingEndpointFailsExplicitly() throws Exception {
ITx proxy = proxy(new Loader());
try { proxy.getBeginTime(); fail("Expected missing endpoint"); }
catch (IllegalStateException e) { assertTrue(e.getMessage().contains("Missing related transaction")); }
}
}