Skip to content

Apache Lucene open-source search software

Call ArrayUtil.copyArray instead of ArrayUtil.copySubArray for full array copy. (#13360)

Bruno Roustantbcb62f56ad1e
diff --git a/lucene/core/src/java/org/apache/lucene/codecs/hnsw/ScalarQuantizedVectorScorer.java b/lucene/core/src/java/org/apache/lucene/codecs/hnsw/ScalarQuantizedVectorScorer.java
index 9914d2cb537..3590acab2ef 100644
--- a/lucene/core/src/java/org/apache/lucene/codecs/hnsw/ScalarQuantizedVectorScorer.java
+++ b/lucene/core/src/java/org/apache/lucene/codecs/hnsw/ScalarQuantizedVectorScorer.java
@@ -44,7 +44,7 @@ public class ScalarQuantizedVectorScorer implements FlatVectorsScorer {
switch (similarityFunction) {
case EUCLIDEAN, DOT_PRODUCT, MAXIMUM_INNER_PRODUCT -> query;
case COSINE -> {
- float[] queryCopy = ArrayUtil.copyOfSubArray(query, 0, query.length);
+ float[] queryCopy = ArrayUtil.copyArray(query);
VectorUtil.l2normalize(queryCopy);
yield queryCopy;
}
diff --git a/lucene/core/src/java/org/apache/lucene/search/BlendedTermQuery.java b/lucene/core/src/java/org/apache/lucene/search/BlendedTermQuery.java
index 05d86819486..8b7d3e80fcd 100644
--- a/lucene/core/src/java/org/apache/lucene/search/BlendedTermQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/BlendedTermQuery.java
@@ -268,7 +268,7 @@ public final class BlendedTermQuery extends Query {
@Override
public final Query rewrite(IndexSearcher indexSearcher) throws IOException {
- final TermStates[] contexts = ArrayUtil.copyOfSubArray(this.contexts, 0, this.contexts.length);
+ final TermStates[] contexts = ArrayUtil.copyArray(this.contexts);
for (int i = 0; i < contexts.length; ++i) {
if (contexts[i] == null
|| contexts[i].wasBuiltFor(indexSearcher.getTopReaderContext()) == false) {
diff --git a/lucene/core/src/java/org/apache/lucene/search/KnnByteVectorQuery.java b/lucene/core/src/java/org/apache/lucene/search/KnnByteVectorQuery.java
index e3d733e516f..5b60e680e10 100644
--- a/lucene/core/src/java/org/apache/lucene/search/KnnByteVectorQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/KnnByteVectorQuery.java
@@ -128,6 +128,6 @@ public class KnnByteVectorQuery extends AbstractKnnVectorQuery {
* @return the target query vector of the search. Each vector element is a byte.
*/
public byte[] getTargetCopy() {
- return ArrayUtil.copyOfSubArray(target, 0, target.length);
+ return ArrayUtil.copyArray(target);
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/search/KnnFloatVectorQuery.java b/lucene/core/src/java/org/apache/lucene/search/KnnFloatVectorQuery.java
index 91cf4474e1c..b06e81eb5d8 100644
--- a/lucene/core/src/java/org/apache/lucene/search/KnnFloatVectorQuery.java
+++ b/lucene/core/src/java/org/apache/lucene/search/KnnFloatVectorQuery.java
@@ -131,6 +131,6 @@ public class KnnFloatVectorQuery extends AbstractKnnVectorQuery {
* @return the target query vector of the search. Each vector element is a float.
*/
public float[] getTargetCopy() {
- return ArrayUtil.copyOfSubArray(target, 0, target.length);
+ return ArrayUtil.copyArray(target);
}
}
diff --git a/lucene/core/src/java/org/apache/lucene/util/ArrayUtil.java b/lucene/core/src/java/org/apache/lucene/util/ArrayUtil.java
index 336b5b3e6bb..722df2ff687 100644
--- a/lucene/core/src/java/org/apache/lucene/util/ArrayUtil.java
+++ b/lucene/core/src/java/org/apache/lucene/util/ArrayUtil.java
@@ -623,6 +623,11 @@ public final class ArrayUtil {
}.select(from, to, k);
}
+ /** Copies an array into a new array. */
+ public static byte[] copyArray(byte[] array) {
+ return copyOfSubArray(array, 0, array.length);
+ }
+
/**
* Copies the specified range of the given array into a new sub array.
*
@@ -636,6 +641,11 @@ public final class ArrayUtil {
return copy;
}
+ /** Copies an array into a new array. */
+ public static char[] copyArray(char[] array) {
+ return copyOfSubArray(array, 0, array.length);
+ }
+
/**
* Copies the specified range of the given array into a new sub array.
*
@@ -649,6 +659,11 @@ public final class ArrayUtil {
return copy;
}
+ /** Copies an array into a new array. */
+ public static short[] copyArray(short[] array) {
+ return copyOfSubArray(array, 0, array.length);
+ }
+
/**
* Copies the specified range of the given array into a new sub array.
*
@@ -662,6 +677,11 @@ public final class ArrayUtil {
return copy;
}
+ /** Copies an array into a new array. */
+ public static int[] copyArray(int[] array) {
+ return copyOfSubArray(array, 0, array.length);
+ }
+
/**
* Copies the specified range of the given array into a new sub array.
*
@@ -675,6 +695,11 @@ public final class ArrayUtil {
return copy;
}
+ /** Copies an array into a new array. */
+ public static long[] copyArray(long[] array) {
+ return copyOfSubArray(array, 0, array.length);
+ }
+
/**
* Copies the specified range of the given array into a new sub array.
*
@@ -688,6 +713,11 @@ public final class ArrayUtil {
return copy;
}
+ /** Copies an array into a new array. */
+ public static float[] copyArray(float[] array) {
+ return copyOfSubArray(array, 0, array.length);
+ }
+
/**
* Copies the specified range of the given array into a new sub array.
*
@@ -701,6 +731,11 @@ public final class ArrayUtil {
return copy;
}
+ /** Copies an array into a new array. */
+ public static double[] copyArray(double[] array) {
+ return copyOfSubArray(array, 0, array.length);
+ }
+
/**
* Copies the specified range of the given array into a new sub array.
*
@@ -714,6 +749,11 @@ public final class ArrayUtil {
return copy;
}
+ /** Copies an array into a new array. */
+ public static <T> T[] copyArray(T[] array) {
+ return copyOfSubArray(array, 0, array.length);
+ }
+
/**
* Copies the specified range of the given array into a new sub array.
*
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestBoolean2.java b/lucene/core/src/test/org/apache/lucene/search/TestBoolean2.java
index ccb8c954c28..b0634e56da4 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestBoolean2.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestBoolean2.java
@@ -229,7 +229,7 @@ public class TestBoolean2 extends LuceneTestCase {
// adjust the expected doc numbers according to our filler docs
if (0 < NUM_FILLER_DOCS) {
- expDocNrs = ArrayUtil.copyOfSubArray(expDocNrs, 0, expDocNrs.length);
+ expDocNrs = ArrayUtil.copyArray(expDocNrs);
for (int i = 0; i < expDocNrs.length; i++) {
expDocNrs[i] = PRE_FILLER_DOCS + ((NUM_FILLER_DOCS + 1) * expDocNrs[i]);
}
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestPhraseQuery.java b/lucene/core/src/test/org/apache/lucene/search/TestPhraseQuery.java
index 05cb9df5397..f91f1a5e6f4 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestPhraseQuery.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestPhraseQuery.java
@@ -755,7 +755,7 @@ public class TestPhraseQuery extends LuceneTestCase {
public void testTopPhrases() throws IOException {
Directory dir = newDirectory();
IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());
- String[] docs = ArrayUtil.copyOfSubArray(DOCS, 0, DOCS.length);
+ String[] docs = ArrayUtil.copyArray(DOCS);
Collections.shuffle(Arrays.asList(docs), random());
for (String value : DOCS) {
Document doc = new Document();
diff --git a/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanationsWithFillerDocs.java b/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanationsWithFillerDocs.java
index 93dd4402c4c..f799cb896a5 100644
--- a/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanationsWithFillerDocs.java
+++ b/lucene/core/src/test/org/apache/lucene/search/TestSimpleExplanationsWithFillerDocs.java
@@ -103,7 +103,7 @@ public class TestSimpleExplanationsWithFillerDocs extends TestSimpleExplanations
@Override
public void qtest(Query q, int[] expDocNrs) throws Exception {
- expDocNrs = ArrayUtil.copyOfSubArray(expDocNrs, 0, expDocNrs.length);
+ expDocNrs = ArrayUtil.copyArray(expDocNrs);
for (int i = 0; i < expDocNrs.length; i++) {
expDocNrs[i] = PRE_FILLER_DOCS + ((NUM_FILLER_DOCS + 1) * expDocNrs[i]);
}
diff --git a/lucene/core/src/test/org/apache/lucene/util/BaseSortTestCase.java b/lucene/core/src/test/org/apache/lucene/util/BaseSortTestCase.java
index e887448578f..ba4ed6fa82b 100644
--- a/lucene/core/src/test/org/apache/lucene/util/BaseSortTestCase.java
+++ b/lucene/core/src/test/org/apache/lucene/util/BaseSortTestCase.java
@@ -49,7 +49,7 @@ public abstract class BaseSortTestCase extends LuceneTestCase {
public void assertSorted(Entry[] original, Entry[] sorted) {
assertEquals(original.length, sorted.length);
- Entry[] actuallySorted = ArrayUtil.copyOfSubArray(original, 0, original.length);
+ Entry[] actuallySorted = ArrayUtil.copyArray(original);
Arrays.sort(actuallySorted);
for (int i = 0; i < original.length; ++i) {
assertEquals(actuallySorted[i].value, sorted[i].value);
diff --git a/lucene/core/src/test/org/apache/lucene/util/hnsw/MockByteVectorValues.java b/lucene/core/src/test/org/apache/lucene/util/hnsw/MockByteVectorValues.java
index fdc2566c022..a3b17b9a621 100644
--- a/lucene/core/src/test/org/apache/lucene/util/hnsw/MockByteVectorValues.java
+++ b/lucene/core/src/test/org/apache/lucene/util/hnsw/MockByteVectorValues.java
@@ -50,10 +50,7 @@ class MockByteVectorValues extends AbstractMockVectorValues<byte[]>
@Override
public MockByteVectorValues copy() {
return new MockByteVectorValues(
- ArrayUtil.copyOfSubArray(values, 0, values.length),
- dimension,
- ArrayUtil.copyOfSubArray(denseValues, 0, denseValues.length),
- numVectors);
+ ArrayUtil.copyArray(values), dimension, ArrayUtil.copyArray(denseValues), numVectors);
}
@Override
diff --git a/lucene/core/src/test/org/apache/lucene/util/hnsw/MockVectorValues.java b/lucene/core/src/test/org/apache/lucene/util/hnsw/MockVectorValues.java
index c10f80e20a8..f183f6c99a6 100644
--- a/lucene/core/src/test/org/apache/lucene/util/hnsw/MockVectorValues.java
+++ b/lucene/core/src/test/org/apache/lucene/util/hnsw/MockVectorValues.java
@@ -50,10 +50,7 @@ class MockVectorValues extends AbstractMockVectorValues<float[]>
@Override
public MockVectorValues copy() {
return new MockVectorValues(
- ArrayUtil.copyOfSubArray(values, 0, values.length),
- dimension,
- ArrayUtil.copyOfSubArray(denseValues, 0, denseValues.length),
- numVectors);
+ ArrayUtil.copyArray(values), dimension, ArrayUtil.copyArray(denseValues), numVectors);
}
@Override
diff --git a/lucene/core/src/test/org/apache/lucene/util/quantization/TestScalarQuantizedVectorSimilarity.java b/lucene/core/src/test/org/apache/lucene/util/quantization/TestScalarQuantizedVectorSimilarity.java
index 809cb8d13b2..bdba822d4ec 100644
--- a/lucene/core/src/test/org/apache/lucene/util/quantization/TestScalarQuantizedVectorSimilarity.java
+++ b/lucene/core/src/test/org/apache/lucene/util/quantization/TestScalarQuantizedVectorSimilarity.java
@@ -229,7 +229,7 @@ public class TestScalarQuantizedVectorSimilarity extends LuceneTestCase {
int i = 0;
float[] offsets = new float[floats.length];
for (float[] f : floats) {
- float[] v = ArrayUtil.copyOfSubArray(f, 0, f.length);
+ float[] v = ArrayUtil.copyArray(f);
VectorUtil.l2normalize(v);
quantized[i] = new byte[v.length];
offsets[i] = scalarQuantizer.quantize(v, quantized[i], similarityFunction);
@@ -246,7 +246,7 @@ public class TestScalarQuantizedVectorSimilarity extends LuceneTestCase {
if (curDoc == -1 || curDoc >= floats.length) {
throw new IOException("Current doc not set or too many iterations");
}
- float[] v = ArrayUtil.copyOfSubArray(floats[curDoc], 0, floats[curDoc].length);
+ float[] v = ArrayUtil.copyArray(floats[curDoc]);
VectorUtil.l2normalize(v);
return v;
}
diff --git a/lucene/spatial-extras/src/test/org/apache/lucene/spatial/TestDistanceStrategy.java b/lucene/spatial-extras/src/test/org/apache/lucene/spatial/TestDistanceStrategy.java
index e3cf440d2b1..31212621e4a 100644
--- a/lucene/spatial-extras/src/test/org/apache/lucene/spatial/TestDistanceStrategy.java
+++ b/lucene/spatial-extras/src/test/org/apache/lucene/spatial/TestDistanceStrategy.java
@@ -108,7 +108,7 @@ public class TestDistanceStrategy extends StrategyTestCase {
void checkDistValueSource(Point pt, float... distances) throws IOException {
float multiplier = random().nextFloat() * 100f;
- float[] dists2 = ArrayUtil.copyOfSubArray(distances, 0, distances.length);
+ float[] dists2 = ArrayUtil.copyArray(distances);
for (int i = 0; i < dists2.length; i++) {
dists2[i] *= multiplier;
}