|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.arrow.algorithm.search; |
| 19 | + |
| 20 | +import java.util.concurrent.CompletableFuture; |
| 21 | +import java.util.concurrent.ExecutionException; |
| 22 | +import java.util.concurrent.ExecutorService; |
| 23 | + |
| 24 | +import org.apache.arrow.algorithm.sort.VectorValueComparator; |
| 25 | +import org.apache.arrow.vector.ValueVector; |
| 26 | +import org.apache.arrow.vector.compare.Range; |
| 27 | +import org.apache.arrow.vector.compare.RangeEqualsVisitor; |
| 28 | + |
| 29 | +/** |
| 30 | + * Search for a value in the vector by multiple threads. |
| 31 | + * This is often used in scenarios where the vector is large or |
| 32 | + * low response time is required. |
| 33 | + * @param <V> the vector type. |
| 34 | + */ |
| 35 | +public class ParallelSearcher<V extends ValueVector> { |
| 36 | + |
| 37 | + /** |
| 38 | + * The target vector to search. |
| 39 | + */ |
| 40 | + private final V vector; |
| 41 | + |
| 42 | + /** |
| 43 | + * The thread pool. |
| 44 | + */ |
| 45 | + private final ExecutorService threadPool; |
| 46 | + |
| 47 | + /** |
| 48 | + * The number of threads to use. |
| 49 | + */ |
| 50 | + private final int numThreads; |
| 51 | + |
| 52 | + /** |
| 53 | + * The position of the key in the target vector, if any. |
| 54 | + */ |
| 55 | + private int keyPosition = -1; |
| 56 | + |
| 57 | + /** |
| 58 | + * Constructs a parallel searcher. |
| 59 | + * @param vector the vector to search. |
| 60 | + * @param threadPool the thread pool to use. |
| 61 | + * @param numThreads the number of threads to use. |
| 62 | + */ |
| 63 | + public ParallelSearcher(V vector, ExecutorService threadPool, int numThreads) { |
| 64 | + this.vector = vector; |
| 65 | + this.threadPool = threadPool; |
| 66 | + this.numThreads = numThreads; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Search for the key in the target vector. |
| 71 | + * @param keyVector the vector containing the search key. |
| 72 | + * @param keyIndex the index of the search key in the key vector. |
| 73 | + * @return the position of a matched value in the target vector, |
| 74 | + * or -1 if none is found. Please note that if there are multiple |
| 75 | + * matches of the key in the target vector, this method makes no |
| 76 | + * guarantees about which instance is returned. |
| 77 | + * For an alternative search implementation that always finds the first match of the key, |
| 78 | + * see {@link VectorSearcher#linearSearch(ValueVector, VectorValueComparator, ValueVector, int)}. |
| 79 | + * @throws ExecutionException if an exception occurs in a thread. |
| 80 | + * @throws InterruptedException if a thread is interrupted. |
| 81 | + */ |
| 82 | + public int search(V keyVector, int keyIndex) throws ExecutionException, InterruptedException { |
| 83 | + keyPosition = -1; |
| 84 | + final int valueCount = vector.getValueCount(); |
| 85 | + final CompletableFuture<Boolean>[] futures = new CompletableFuture[numThreads]; |
| 86 | + for (int i = 0; i < futures.length; i++) { |
| 87 | + futures[i] = new CompletableFuture<>(); |
| 88 | + } |
| 89 | + |
| 90 | + for (int i = 0; i < numThreads; i++) { |
| 91 | + final int tid = i; |
| 92 | + threadPool.submit(() -> { |
| 93 | + // convert to long to avoid overflow |
| 94 | + int start = (int) (((long) valueCount) * tid / numThreads); |
| 95 | + int end = (int) ((long) valueCount) * (tid + 1) / numThreads; |
| 96 | + |
| 97 | + if (start >= end) { |
| 98 | + // no data assigned to this task. |
| 99 | + futures[tid].complete(false); |
| 100 | + return; |
| 101 | + } |
| 102 | + |
| 103 | + RangeEqualsVisitor visitor = new RangeEqualsVisitor(vector, keyVector, false); |
| 104 | + Range range = new Range(0, 0, 1); |
| 105 | + for (int pos = start; pos < end; pos++) { |
| 106 | + if (keyPosition != -1) { |
| 107 | + // the key has been found by another task |
| 108 | + futures[tid].complete(false); |
| 109 | + return; |
| 110 | + } |
| 111 | + range.setLeftStart(pos).setRightStart(keyIndex); |
| 112 | + if (visitor.rangeEquals(range)) { |
| 113 | + keyPosition = pos; |
| 114 | + futures[tid].complete(true); |
| 115 | + return; |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + // no match value is found. |
| 120 | + futures[tid].complete(false); |
| 121 | + }); |
| 122 | + } |
| 123 | + |
| 124 | + CompletableFuture.allOf(futures).get(); |
| 125 | + return keyPosition; |
| 126 | + } |
| 127 | +} |
0 commit comments