diff --git a/plugins/com.minres.scviewer.database.ftr/src/com/minres/scviewer/database/ftr/FtrDbLoader.java b/plugins/com.minres.scviewer.database.ftr/src/com/minres/scviewer/database/ftr/FtrDbLoader.java index 41364bd..4b3203c 100644 --- a/plugins/com.minres.scviewer.database.ftr/src/com/minres/scviewer/database/ftr/FtrDbLoader.java +++ b/plugins/com.minres.scviewer.database.ftr/src/com/minres/scviewer/database/ftr/FtrDbLoader.java @@ -76,7 +76,8 @@ public class FtrDbLoader implements IWaveformDbLoader { final Map transactions = UnifiedMap.newMap(); /** The attribute types. */ - final Map attributeTypes = UnifiedMap.newMap(); + private record AttributeKey(String name, DataType type, AssociationType association) { } + final Map attributeTypes = UnifiedMap.newMap(); /** The relations in. */ final HashMultimap 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(); diff --git a/tests/com.minres.scviewer.database.test/src/com/minres/scviewer/database/test/FtrMetadataTest.java b/tests/com.minres.scviewer.database.test/src/com/minres/scviewer/database/test/FtrMetadataTest.java new file mode 100644 index 0000000..adf82a8 --- /dev/null +++ b/tests/com.minres.scviewer.database.test/src/com/minres/scviewer/database/test/FtrMetadataTest.java @@ -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(); } + } +}