Preserve FTR attribute identity and register relation types

This commit is contained in:
eyck committed 2026-11-14 17:29:48 +01:00
1 parent c85a842dbe
commit d31fe15000
2 files changed
+49 -7

No files matched your search

@@ -76,7 +76,8 @@ public class FtrDbLoader implements IWaveformDbLoader {
final Map<Long, FtrTx> transactions = UnifiedMap.newMap();
/** The attribute types. */
final Map<String, TxAttributeType> attributeTypes = UnifiedMap.newMap();
private record AttributeKey(String name, DataType type, AssociationType association) { }
final Map<AttributeKey, TxAttributeType> attributeTypes = UnifiedMap.newMap();
/** The relations in. */
final HashMultimap<Long, FtrRelation> relationsIn = HashMultimap.create();
@@ -350,11 +351,9 @@ public class FtrDbLoader implements IWaveformDbLoader {
}
private synchronized TxAttributeType getOrAddAttributeType(long tag, DataType type, String attrName) {
if(!attributeTypes.containsKey(attrName)) {
attributeTypes.put(attrName, new TxAttributeType(attrName, type, AssociationType.values()[(int)tag-7]));
}
TxAttributeType attrType = attributeTypes.get(attrName);
return attrType;
AssociationType association = AssociationType.values()[(int) tag - 7];
AttributeKey key = new AttributeKey(attrName, type, association);
return attributeTypes.computeIfAbsent(key, k -> new TxAttributeType(k.name(), k.type(), k.association()));
}
/**
* Dispose.
@@ -536,7 +535,7 @@ public class FtrDbLoader implements IWaveformDbLoader {
long from_fiber = sz>3?cborDecoder.readInt():-1;
long to_fiber = sz>3?cborDecoder.readInt():-1;
String rel_name = strDict.get((int)type_id);
FtrRelation ftrRel = new FtrRelation(relationTypes.getOrDefault(rel_name, RelationTypeFactory.create(rel_name)), from_id, to_id, from_fiber, to_fiber);
FtrRelation ftrRel = new FtrRelation(relationTypes.computeIfAbsent(rel_name, RelationTypeFactory::create), from_id, to_id, from_fiber, to_fiber);
relationsOut.put(from_id, ftrRel);
relationsIn.put(to_id, ftrRel);
next = cborDecoder.peekType();
@@ -0,0 +1,43 @@
package com.minres.scviewer.database.test;
import static org.junit.Assert.*;
import java.lang.reflect.Method;
import org.junit.Test;
import com.minres.scviewer.database.AssociationType;
import com.minres.scviewer.database.DataType;
import com.minres.scviewer.database.ftr.FtrDbLoader;
import com.minres.scviewer.database.tx.ITxAttributeType;
public class FtrMetadataTest {
@Test
public void sameNameKeepsTypeAndAssociation() throws Exception {
FtrDbLoader loader = new FtrDbLoader();
Method method = FtrDbLoader.class.getDeclaredMethod("getOrAddAttributeType", long.class, DataType.class, String.class);
method.setAccessible(true);
ITxAttributeType begin = (ITxAttributeType) method.invoke(loader, 7L, DataType.INTEGER, "value");
ITxAttributeType end = (ITxAttributeType) method.invoke(loader, 9L, DataType.INTEGER, "value");
ITxAttributeType string = (ITxAttributeType) method.invoke(loader, 7L, DataType.STRING, "value");
assertEquals(AssociationType.BEGIN, begin.getType());
assertEquals(AssociationType.END, end.getType());
assertEquals(DataType.STRING, string.getDataType());
assertNotSame(begin, end);
assertNotSame(begin, string);
assertSame(begin, method.invoke(loader, 7L, DataType.INTEGER, "value"));
}
@Test
public void parsedRelationsAreRegistered() throws Exception {
FtrDbLoader loader = new FtrDbLoader();
try {
loader.load(TxTestSupport.input("my_db.ftr"));
assertFalse(loader.getAllRelationTypes().isEmpty());
for (com.minres.scviewer.database.IWaveform wave : loader.getAllWaves())
for (com.minres.scviewer.database.EventEntry entry : wave.getEvents())
for (com.minres.scviewer.database.IEvent e : entry.events) {
com.minres.scviewer.database.tx.ITx tx = ((com.minres.scviewer.database.tx.ITxEvent)e).getTransaction();
for (com.minres.scviewer.database.tx.ITxRelation relation : tx.getOutgoingRelations())
assertTrue(loader.getAllRelationTypes().contains(relation.getRelationType()));
}
} finally { loader.dispose(); }
}
}