From 6764600465797280e7f0f3b86d930ae40735ebd1 Mon Sep 17 00:00:00 2001 From: kankanzhou <121850056+kankanzhou@users.noreply.github.com> Date: Sun, 15 Nov 2026 20:30:49 -0700 Subject: [PATCH] GH-4409: Fix DefaultDataBag iteration at spill threshold --- .../jena/atlas/data/DefaultDataBag.java | 2 +- .../org/apache/jena/atlas/data/TS_Data.java | 1 + .../jena/atlas/data/TestDefaultDataBag.java | 50 +++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 jena-arq/src/test/java/org/apache/jena/atlas/data/TestDefaultDataBag.java diff --git a/jena-arq/src/main/java/org/apache/jena/atlas/data/DefaultDataBag.java b/jena-arq/src/main/java/org/apache/jena/atlas/data/DefaultDataBag.java index 69dbdf3e837..f1d7b23b619 100644 --- a/jena-arq/src/main/java/org/apache/jena/atlas/data/DefaultDataBag.java +++ b/jena-arq/src/main/java/org/apache/jena/atlas/data/DefaultDataBag.java @@ -148,7 +148,7 @@ public Iterator iterator() { closeWriter(); // Create a new reader - if ( policy.isThresholdExceeded() ) { + if ( spilled ) { File spillFile = getSpillFiles().get(0); InputStream in; diff --git a/jena-arq/src/test/java/org/apache/jena/atlas/data/TS_Data.java b/jena-arq/src/test/java/org/apache/jena/atlas/data/TS_Data.java index e30bd36d9cf..bd4b008c1d9 100644 --- a/jena-arq/src/test/java/org/apache/jena/atlas/data/TS_Data.java +++ b/jena-arq/src/test/java/org/apache/jena/atlas/data/TS_Data.java @@ -26,6 +26,7 @@ @Suite @SelectClasses({ + TestDefaultDataBag.class, TestSortedDataBag.class, TestDistinctDataBag.class, TestDistinctDataNet.class, diff --git a/jena-arq/src/test/java/org/apache/jena/atlas/data/TestDefaultDataBag.java b/jena-arq/src/test/java/org/apache/jena/atlas/data/TestDefaultDataBag.java new file mode 100644 index 00000000000..01cd89fb762 --- /dev/null +++ b/jena-arq/src/test/java/org/apache/jena/atlas/data/TestDefaultDataBag.java @@ -0,0 +1,50 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + * + * SPDX-License-Identifier: Apache-2.0 + */ + +package org.apache.jena.atlas.data; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.List; + +import org.junit.jupiter.api.Test; + +import org.apache.jena.atlas.iterator.Iter; +import org.apache.jena.graph.NodeFactory; +import org.apache.jena.sparql.core.Var; +import org.apache.jena.sparql.engine.binding.Binding; +import org.apache.jena.sparql.engine.binding.BindingFactory; +import org.apache.jena.sparql.system.SerializationFactoryFinder; + +public class TestDefaultDataBag { + @Test + public void iteratorAtThresholdUsesMemory() { + Binding binding = BindingFactory.binding(Var.alloc("x"), NodeFactory.createLiteralString("value")); + DefaultDataBag bag = new DefaultDataBag<>(new ThresholdPolicyCount<>(1), + SerializationFactoryFinder.bindingSerializationFactory()); + try { + bag.add(binding); + assertEquals(List.of(binding), Iter.toList(bag.iterator())); + } finally { + bag.close(); + } + } +}