cpoerschke commented on a change in pull request #243: URL: https://github.com/apache/solr/pull/243#discussion_r705511096
########## File path: solr/CHANGES.txt ########## @@ -466,6 +466,8 @@ Other Changes * SOLR-15599: woodstox-core-asl:4.4.1 (org.codehaus) replaced with woodstox-core:6.2.4 (com.fasterxml) (Houston Putman) +* SOLR-15480: Make Tuple copy constructor, clone and merge consistent w.r.t. markers (EOF, EXCEPTION), field names and labels. (John Durham via Mike Drob, Christine Poerschke) + Review comment: note to self and/or pull request merger: we need to relocate this entry to the 8.11.0 section once `origin/main` has that section (it will get it as part of the 8.10.0 release process which is now underway) and once `origin/main` with the section has been merged into this pull request's branch. assuming we're targetting 8.11 and not 9.0 version? ########## File path: solr/solrj/src/test/org/apache/solr/client/solrj/io/TupleTest.java ########## @@ -0,0 +1,199 @@ +/* + * 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 + * + * http://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. + */ +package org.apache.solr.client.solrj.io; + +import org.apache.solr.SolrTestCase; +import org.apache.solr.common.MapWriter.EntryWriter; +import org.apache.solr.common.params.StreamParams; +import org.junit.Test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public class TupleTest extends SolrTestCase { + + @Test + public void putAllSetsEOFMarker() { + final Map<String, Object> fields = new HashMap<>(); + fields.put("field-one", new Object()); + fields.put("field-two", new Object()); + fields.put(StreamParams.EOF, true); + + final Tuple tuple = new Tuple(); + tuple.putAll(fields); + + assertTrue(tuple.EOF); + } + + @Test + public void putAllSetsEXCEPTIONMarker() { + final Map<String, Object> fields = new HashMap<>(); + fields.put("field-one", new Object()); + fields.put("field-two", new Object()); + fields.put(StreamParams.EXCEPTION, "exception"); + + final Tuple tuple = new Tuple(); + tuple.putAll(fields); + + assertTrue(tuple.EXCEPTION); + } + + @Test + public void cloneTest() { + final Map<String, Object> fields = new HashMap<>(); + fields.put("field-one", new Object()); + fields.put("field-two", new Object()); + fields.put(StreamParams.EXCEPTION, "exception"); + fields.put(StreamParams.EOF, true); + final Tuple original = new Tuple(); + original.putAll(fields); + original.setFieldNames(new ArrayList<>(Arrays.asList("field-one", "field-two"))); + original.setFieldLabels(new HashMap<>(Map.ofEntries( + Map.entry("field-one", "field one"), + Map.entry("field-two", "field two") + ))); + + final Tuple clone = new Tuple(original); Review comment: How about ```suggestion final Tuple clone = original.clone(); ``` here to match the test name and a separate `public void copyTest()` test? Or a combined test e.g. via randomisation: ``` public void cloneOrCopyTest() { ... final Tuple duplicate = random().nextBoolean() ? original.clone() : new Tuple(original); ... } ``` ########## File path: solr/solrj/src/java/org/apache/solr/client/solrj/io/Tuple.java ########## @@ -275,15 +296,33 @@ public void setMetrics(Map<String, Map<?,?>> metrics) { } public Tuple clone() { - Tuple clone = new Tuple(); - clone.fields.putAll(fields); - // TODO This doesn't copy EOF/Exception https://issues.apache.org/jira/browse/SOLR-15480 + Tuple clone = new Tuple(this); return clone; } - + + /** + * The other tuples fields and fieldLabels will be putAll'd directly to this's fields and fieldLabels while + * other's fieldNames will be added such that duplicates aren't present. + * @param other Tuple to be merged into this. + */ Review comment: Thanks for adding the javadocs here! ########## File path: solr/solrj/src/test/org/apache/solr/client/solrj/io/TupleTest.java ########## @@ -0,0 +1,199 @@ +/* + * 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 + * + * http://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. + */ +package org.apache.solr.client.solrj.io; + +import org.apache.solr.SolrTestCase; +import org.apache.solr.common.MapWriter.EntryWriter; +import org.apache.solr.common.params.StreamParams; +import org.junit.Test; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.Map; + +public class TupleTest extends SolrTestCase { + + @Test + public void putAllSetsEOFMarker() { + final Map<String, Object> fields = new HashMap<>(); + fields.put("field-one", new Object()); + fields.put("field-two", new Object()); + fields.put(StreamParams.EOF, true); + + final Tuple tuple = new Tuple(); + tuple.putAll(fields); + + assertTrue(tuple.EOF); + } + + @Test + public void putAllSetsEXCEPTIONMarker() { + final Map<String, Object> fields = new HashMap<>(); + fields.put("field-one", new Object()); + fields.put("field-two", new Object()); + fields.put(StreamParams.EXCEPTION, "exception"); + + final Tuple tuple = new Tuple(); + tuple.putAll(fields); + + assertTrue(tuple.EXCEPTION); + } + + @Test + public void cloneTest() { + final Map<String, Object> fields = new HashMap<>(); + fields.put("field-one", new Object()); + fields.put("field-two", new Object()); + fields.put(StreamParams.EXCEPTION, "exception"); + fields.put(StreamParams.EOF, true); + final Tuple original = new Tuple(); + original.putAll(fields); + original.setFieldNames(new ArrayList<>(Arrays.asList("field-one", "field-two"))); + original.setFieldLabels(new HashMap<>(Map.ofEntries( + Map.entry("field-one", "field one"), + Map.entry("field-two", "field two") + ))); + + final Tuple clone = new Tuple(original); + + assertEquals(original.getFields().entrySet().size(), clone.getFields().entrySet().size()); + assertEquals(original.getFieldNames().size(), clone.getFieldNames().size()); + assertEquals(original.getFieldLabels().entrySet().size(), clone.getFieldLabels().entrySet().size()); + assertEquals(original.EOF, clone.EOF); + assertEquals(original.EXCEPTION, clone.EXCEPTION); + } + + @Test + public void mergeTest() { + final Map<String, Object> commonFields = new HashMap<>(); + commonFields.put("field-one", new Object()); + commonFields.put("field-two", new Object()); + commonFields.put("field-three", new Object()); + commonFields.put(StreamParams.EXCEPTION, "exception"); + commonFields.put(StreamParams.EOF, true); + + final Tuple tupleOne = new Tuple(); + tupleOne.putAll(commonFields); + tupleOne.setFieldNames(new ArrayList<>(Arrays.asList("field-one-name", "field-two-name", "field-three-name"))); + tupleOne.setFieldLabels(new HashMap<>(Map.ofEntries( + Map.entry("field-one-name", "field-one"), + Map.entry("field-two-nam", "field-two"), + Map.entry("field-three-name", "field-three") + ))); + + final Tuple tupleTwo = new Tuple(); + tupleTwo.putAll(commonFields); + tupleTwo.put("field-four", new Object()); + tupleTwo.put("new-field-two", new Object()); + tupleTwo.setFieldNames(new ArrayList<>(Arrays.asList("field-one-name", "field-two-name", "field-four-name"))); + tupleTwo.setFieldLabels(new HashMap<>(Map.ofEntries( + Map.entry("field-one-name", "field-one"), + Map.entry("field-two-name", "new-field-two"), + Map.entry("field-four-name", "field-four") + ))); + + tupleOne.merge(tupleTwo); + + assertEquals(7, tupleOne.getFields().size()); + assertEquals(4, tupleOne.getFieldNames().size()); + assertEquals(5, tupleOne.getFieldLabels().size()); Review comment: Just double-checking if `field-two-nam` vs. `field-two-name` was intended (if so suggest comments) or if it was a typo? ```suggestion Map.entry("field-two-nam", "field-two"), // "field-two-nam" different from "field-two-name" Map.entry("field-three-name", "field-three") ))); final Tuple tupleTwo = new Tuple(); tupleTwo.putAll(commonFields); tupleTwo.put("field-four", new Object()); tupleTwo.put("new-field-two", new Object()); tupleTwo.setFieldNames(new ArrayList<>(Arrays.asList("field-one-name", "field-two-name", "field-four-name"))); tupleTwo.setFieldLabels(new HashMap<>(Map.ofEntries( Map.entry("field-one-name", "field-one"), Map.entry("field-two-name", "new-field-two"), Map.entry("field-four-name", "field-four") ))); tupleOne.merge(tupleTwo); assertEquals(7, tupleOne.getFields().size()); assertEquals(4, tupleOne.getFieldNames().size()); assertEquals(5, tupleOne.getFieldLabels().size()); // field-one-name, field-two-nam, field-two-name, field-three-name, field-four-name ``` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org For additional commands, e-mail: issues-h...@solr.apache.org