From b5edf43491d0041eb99bafbad0b77e1dcb71390c Mon Sep 17 00:00:00 2001 From: Richard Eckart de Castilho Date: Fri, 29 Jul 2022 23:11:16 +0200 Subject: [PATCH 1/2] Issue #238 - Form 6 serializes non-reachable FSes but should not - Instead of only setting the default v2IdRef flag (which has no effect), set the flag on the CAS currently being processed - Added unit test --- .../uima/cas/impl/BinaryCasSerDes6.java | 2 +- .../org/apache/uima/util/CasIOUtilsTest.java | 40 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/uimaj-core/src/main/java/org/apache/uima/cas/impl/BinaryCasSerDes6.java b/uimaj-core/src/main/java/org/apache/uima/cas/impl/BinaryCasSerDes6.java index 6d0cb9fbca..e6bcec21c2 100644 --- a/uimaj-core/src/main/java/org/apache/uima/cas/impl/BinaryCasSerDes6.java +++ b/uimaj-core/src/main/java/org/apache/uima/cas/impl/BinaryCasSerDes6.java @@ -3044,7 +3044,7 @@ private void processIndexedFeatureStructures(final CASImpl cas1, boolean isWrite if (!isWrite) { // always have form 6 do just reachables, to mimic what v2 did AllFSs allFSs; - try (AutoCloseableNoException a = LowLevelCAS.ll_defaultV2IdRefs(false)) { + try (AutoCloseableNoException a = cas1.ll_enableV2IdRefs(false)) { allFSs = new AllFSs(cas1, mark, isTypeMapping ? fs -> isTypeInTgt(fs) : null, isTypeMapping ? typeMapper : null).getAllFSsAllViews_sofas_reachable(); // AllFSs internally already causes _save_to_cas_data() to be called, so we have to add all diff --git a/uimaj-core/src/test/java/org/apache/uima/util/CasIOUtilsTest.java b/uimaj-core/src/test/java/org/apache/uima/util/CasIOUtilsTest.java index 345ebf0f2d..d386b1c2c6 100644 --- a/uimaj-core/src/test/java/org/apache/uima/util/CasIOUtilsTest.java +++ b/uimaj-core/src/test/java/org/apache/uima/util/CasIOUtilsTest.java @@ -19,6 +19,7 @@ package org.apache.uima.util; import static java.util.Arrays.asList; +import static org.apache.uima.cas.SerialFormat.COMPRESSED_FILTERED_TSI; import static org.assertj.core.api.Assertions.assertThat; import java.io.ByteArrayInputStream; @@ -32,14 +33,18 @@ import java.util.Collection; import java.util.Collections; import java.util.Iterator; +import java.util.LinkedHashSet; import java.util.List; +import java.util.Set; import org.apache.uima.UIMAFramework; import org.apache.uima.cas.CAS; import org.apache.uima.cas.CASRuntimeException; import org.apache.uima.cas.FeatureStructure; import org.apache.uima.cas.SerialFormat; +import org.apache.uima.cas.impl.CASImpl; import org.apache.uima.jcas.cas.TOP; +import org.apache.uima.jcas.tcas.Annotation; import org.apache.uima.resource.metadata.FsIndexDescription; import org.apache.uima.resource.metadata.TypeDescription; import org.apache.uima.resource.metadata.TypeSystemDescription; @@ -337,6 +342,41 @@ public void testDocumentAnnotationIsNotResurrected() throws Exception { .extracting(fs -> fs.getType().getName()).containsExactly(customDocAnnoTypeName); } + @Test + public void thatBinaryForm6DoesOnlyIncludeReachableFSes() throws Exception { + CASImpl cas = (CASImpl) CasCreationUtils.createCas(); + byte[] buf; + try (AutoCloseableNoException a = cas.ll_enableV2IdRefs(true)) { + Annotation ann = cas.createAnnotation(cas.getAnnotationType(), 0, 1); + ann.addToIndexes(); + ann.removeFromIndexes(); + + Set allFSes = new LinkedHashSet<>(); + cas.walkReachablePlusFSsSorted(allFSes::add, null, null, null); + + assertThat(allFSes) // + .as("The annotation that was added and then removed before serialization should be found") // + .containsExactly(cas.getSofa(), ann); + + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + CasIOUtils.save(cas, bos, COMPRESSED_FILTERED_TSI); + buf = bos.toByteArray(); + } + + cas.reset(); + + try (AutoCloseableNoException a = cas.ll_enableV2IdRefs(true)) { + CasIOUtils.load(new ByteArrayInputStream(buf), cas); + + Set allFSes = new LinkedHashSet<>(); + cas.walkReachablePlusFSsSorted(allFSes::add, null, null, null); + + assertThat(allFSes) // + .as("The annotation that was added and then removed before serialization should not be found") // + .containsExactly(cas.getSofa()); + } + } + @AfterEach public void tearDown() throws Exception { cas.release(); From 1384ee2dc84410cfb1de3cb75c2dc998fbaf8d87 Mon Sep 17 00:00:00 2001 From: Richard Eckart de Castilho Date: Sat, 30 Jul 2022 00:30:44 +0200 Subject: [PATCH 2/2] #238 - Form 6 serializes non-reachable FSes but should not - Removed test which actually tries to verify that non-indexed FSes get serialized and de-serialized - this is not the desired behavior --- .../java/org/apache/uima/cas/impl/SerDesForm6Test.java | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/uimaj-core/src/test/java/org/apache/uima/cas/impl/SerDesForm6Test.java b/uimaj-core/src/test/java/org/apache/uima/cas/impl/SerDesForm6Test.java index 65f8344121..5b963b694d 100644 --- a/uimaj-core/src/test/java/org/apache/uima/cas/impl/SerDesForm6Test.java +++ b/uimaj-core/src/test/java/org/apache/uima/cas/impl/SerDesForm6Test.java @@ -66,7 +66,6 @@ import org.apache.uima.jcas.cas.TOP; import org.apache.uima.resource.ResourceInitializationException; import org.apache.uima.resource.metadata.TypeSystemDescription; -import org.apache.uima.util.AutoCloseableNoException; import org.apache.uima.util.CasCreationUtils; import org.apache.uima.util.impl.SerializationMeasures; import org.junit.jupiter.api.AfterEach; @@ -420,15 +419,6 @@ public void testAllKinds() { } } - @Test - public void testAllKindsV2() { - try (AutoCloseableNoException a = LowLevelCAS.ll_defaultV2IdRefs(); - AutoCloseableNoException b = casSrc.ll_enableV2IdRefs()) { // because casSrc set in - // setup - testAllKinds(); - } - } - // Test chains going through filtered type // Repeat below with OneType, and TwoTypes with filtered slot == fsRef