|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.index.mapper; |
| 10 | + |
| 11 | +import org.apache.lucene.search.TermInSetQuery; |
| 12 | +import org.apache.lucene.util.BytesRef; |
| 13 | + |
| 14 | +import java.util.AbstractSet; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.Collection; |
| 17 | +import java.util.Comparator; |
| 18 | +import java.util.Iterator; |
| 19 | +import java.util.List; |
| 20 | +import java.util.SortedSet; |
| 21 | +import java.util.function.Consumer; |
| 22 | +import java.util.function.Function; |
| 23 | +import java.util.function.Supplier; |
| 24 | + |
| 25 | +/** |
| 26 | + * Purposed for passing terms into {@link TermInSetQuery}. |
| 27 | + * If the given terms are sorted already, it wrap it with a SortedSet stub. |
| 28 | + * Otherwise, it passes terms as list. |
| 29 | + */ |
| 30 | +public class BytesRefsCollectionBuilder implements Consumer<BytesRef>, Supplier<Collection<BytesRef>> { |
| 31 | + |
| 32 | + /** |
| 33 | + * Strategy for building BytesRef collection. |
| 34 | + * */ |
| 35 | + protected interface ConsumerStrategy extends Function<BytesRef, ConsumerStrategy>, Supplier<Collection<BytesRef>> {} |
| 36 | + |
| 37 | + public BytesRefsCollectionBuilder(int sizeExpected) { |
| 38 | + terms = new ArrayList<>(sizeExpected); |
| 39 | + } |
| 40 | + |
| 41 | + protected final List<BytesRef> terms; |
| 42 | + protected ConsumerStrategy delegate = createStartStrategy(); |
| 43 | + |
| 44 | + @Override |
| 45 | + public void accept(BytesRef bytesRef) { |
| 46 | + delegate = delegate.apply(bytesRef); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public Collection<BytesRef> get() { |
| 51 | + Collection<BytesRef> result = delegate.get(); |
| 52 | + delegate = createFrozenStrategy(result); |
| 53 | + return result; |
| 54 | + } |
| 55 | + |
| 56 | + protected ConsumerStrategy createStartStrategy() { |
| 57 | + return new ConsumerStrategy() { |
| 58 | + @Override |
| 59 | + public ConsumerStrategy apply(BytesRef firstBytes) { |
| 60 | + terms.add(firstBytes); // firstly, just store |
| 61 | + return createSortedStrategy(firstBytes); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public Collection<BytesRef> get() { |
| 66 | + return terms; // empty list |
| 67 | + } |
| 68 | + }; |
| 69 | + } |
| 70 | + |
| 71 | + protected ConsumerStrategy createSortedStrategy(BytesRef firstBytes) { |
| 72 | + return new ConsumerStrategy() { |
| 73 | + BytesRef prev = firstBytes; |
| 74 | + |
| 75 | + @Override |
| 76 | + public ConsumerStrategy apply(BytesRef bytesRef) { |
| 77 | + terms.add(bytesRef); |
| 78 | + if (bytesRef.compareTo(prev) >= 0) { // keep checking sorted |
| 79 | + prev = bytesRef; |
| 80 | + return this; |
| 81 | + } else { // isn't sorted |
| 82 | + return createUnsortedStrategy(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public Collection<BytesRef> get() { |
| 88 | + return new SortedBytesSet(terms); |
| 89 | + } |
| 90 | + }; |
| 91 | + } |
| 92 | + |
| 93 | + protected ConsumerStrategy createUnsortedStrategy() { |
| 94 | + return new ConsumerStrategy() { |
| 95 | + @Override |
| 96 | + public ConsumerStrategy apply(BytesRef bytesRef) { // just storing |
| 97 | + terms.add(bytesRef); |
| 98 | + return this; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public Collection<BytesRef> get() { |
| 103 | + return terms; |
| 104 | + } |
| 105 | + }; |
| 106 | + } |
| 107 | + |
| 108 | + protected ConsumerStrategy createFrozenStrategy(Collection<BytesRef> result) { |
| 109 | + return new ConsumerStrategy() { |
| 110 | + |
| 111 | + @Override |
| 112 | + public ConsumerStrategy apply(BytesRef bytesRef) { |
| 113 | + throw new IllegalStateException("already build"); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public Collection<BytesRef> get() { |
| 118 | + return result; |
| 119 | + } |
| 120 | + }; |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * {@link SortedSet<BytesRef>} for passing into TermInSetQuery() |
| 125 | + * */ |
| 126 | + protected static class SortedBytesSet extends AbstractSet<BytesRef> implements SortedSet<BytesRef> { |
| 127 | + |
| 128 | + private final List<BytesRef> bytesRefs; |
| 129 | + |
| 130 | + public SortedBytesSet(List<BytesRef> bytesRefs) { |
| 131 | + this.bytesRefs = bytesRefs; |
| 132 | + } |
| 133 | + |
| 134 | + @Override |
| 135 | + public Iterator<BytesRef> iterator() { |
| 136 | + return bytesRefs.iterator(); |
| 137 | + } |
| 138 | + |
| 139 | + @Override |
| 140 | + public int size() { |
| 141 | + return bytesRefs.size(); |
| 142 | + } |
| 143 | + |
| 144 | + @Override |
| 145 | + public Comparator<? super BytesRef> comparator() { |
| 146 | + return null; |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public SortedSet<BytesRef> subSet(BytesRef fromElement, BytesRef toElement) { |
| 151 | + throw new UnsupportedOperationException(); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public SortedSet<BytesRef> headSet(BytesRef toElement) { |
| 156 | + throw new UnsupportedOperationException(); |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public SortedSet<BytesRef> tailSet(BytesRef fromElement) { |
| 161 | + throw new UnsupportedOperationException(); |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public BytesRef first() { |
| 166 | + throw new UnsupportedOperationException(); |
| 167 | + } |
| 168 | + |
| 169 | + @Override |
| 170 | + public BytesRef last() { |
| 171 | + throw new UnsupportedOperationException(); |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Dedicated for {@link TermInSetQuery#TermInSetQuery(String, Collection)}. |
| 176 | + */ |
| 177 | + @Override |
| 178 | + public <T> T[] toArray(T[] a) { |
| 179 | + return bytesRefs.toArray(a); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments