List of usage examples for org.apache.lucene.util BytesRefArray get
public BytesRef get(BytesRefBuilder spare, int index)
From source file:de.unihildesheim.iw.lucene.query.QueryUtils.java
License:Open Source License
/** * Remove terms from the given collection, if they are not found in the * collection.// ww w .j av a 2 s.co m * * @param dataProv IndexDataProvider * @param terms Collection of terms to check against the collection * @return Passed in terms with non-collection terms removed */ @SuppressFBWarnings("LO_APPENDED_STRING_IN_FORMAT_STRING") private static BytesRefArray removeUnknownTerms(@NotNull final IndexDataProvider dataProv, @NotNull final BytesRefArray terms) { final StringBuilder sb = new StringBuilder("Skipped terms (stopword or not in collection): ["); final FixedBitSet bits = new FixedBitSet(terms.size()); final BytesRefBuilder spare = new BytesRefBuilder(); BytesRef term; if (terms.size() == 0) { return terms; } else { for (int i = terms.size() - 1; i >= 0; i--) { term = terms.get(spare, i); if (dataProv.getTermFrequency(term) <= 0L) { sb.append(term.utf8ToString()).append(' '); bits.set(i); } } if (bits.cardinality() > 0) { LOG.warn(sb.toString().trim() + "]."); final BytesRefArray cleanTerms = new BytesRefArray(Counter.newCounter(false)); for (int i = terms.size() - 1; i >= 0; i--) { if (!bits.get(i)) { term = terms.get(spare, i); cleanTerms.append(term); // copies bytes } } return cleanTerms; } return terms; } }
From source file:org.codelibs.elasticsearch.common.util.CollectionUtils.java
License:Apache License
private static void sort(final BytesRefBuilder scratch, final BytesRefBuilder scratch1, final BytesRefArray bytes, final int[] indices) { final int numValues = bytes.size(); assert indices.length >= numValues; if (numValues > 1) { new InPlaceMergeSorter() { final Comparator<BytesRef> comparator = Comparator.naturalOrder(); @Override// w w w .java 2 s . co m protected int compare(int i, int j) { return comparator.compare(bytes.get(scratch, indices[i]), bytes.get(scratch1, indices[j])); } @Override protected void swap(int i, int j) { int value_i = indices[i]; indices[i] = indices[j]; indices[j] = value_i; } }.sort(0, numValues); } }
From source file:org.codelibs.elasticsearch.common.util.CollectionUtils.java
License:Apache License
public static int sortAndDedup(final BytesRefArray bytes, final int[] indices) { final BytesRefBuilder scratch = new BytesRefBuilder(); final BytesRefBuilder scratch1 = new BytesRefBuilder(); final int numValues = bytes.size(); assert indices.length >= numValues; if (numValues <= 1) { return numValues; }/*from www.j ava 2 s. com*/ sort(scratch, scratch1, bytes, indices); int uniqueCount = 1; BytesRefBuilder previous = scratch; BytesRefBuilder current = scratch1; bytes.get(previous, indices[0]); for (int i = 1; i < numValues; ++i) { bytes.get(current, indices[i]); if (!previous.get().equals(current.get())) { indices[uniqueCount++] = indices[i]; } BytesRefBuilder tmp = previous; previous = current; current = tmp; } return uniqueCount; }
From source file:org.elasticsearch.common.util.CollectionUtilsTests.java
License:Apache License
@Test public void testSortAndDedupByteRefArray() { SortedSet<BytesRef> set = new TreeSet<>(); final int numValues = scaledRandomIntBetween(0, 10000); List<BytesRef> tmpList = new ArrayList<>(); BytesRefArray array = new BytesRefArray(Counter.newCounter()); for (int i = 0; i < numValues; i++) { String s = randomRealisticUnicodeOfCodepointLengthBetween(1, 100); set.add(new BytesRef(s)); tmpList.add(new BytesRef(s)); array.append(new BytesRef(s)); }//from w ww. ja v a 2s .c o m if (randomBoolean()) { Collections.shuffle(tmpList, getRandom()); for (BytesRef ref : tmpList) { array.append(ref); } } int[] indices = new int[array.size()]; for (int i = 0; i < indices.length; i++) { indices[i] = i; } int numUnique = CollectionUtils.sortAndDedup(array, indices); assertThat(numUnique, equalTo(set.size())); Iterator<BytesRef> iterator = set.iterator(); BytesRefBuilder spare = new BytesRefBuilder(); for (int i = 0; i < numUnique; i++) { assertThat(iterator.hasNext(), is(true)); assertThat(array.get(spare, indices[i]), equalTo(iterator.next())); } }
From source file:org.elasticsearch.common.util.CollectionUtilsTests.java
License:Apache License
@Test public void testSortByteRefArray() { List<BytesRef> values = new ArrayList<>(); final int numValues = scaledRandomIntBetween(0, 10000); BytesRefArray array = new BytesRefArray(Counter.newCounter()); for (int i = 0; i < numValues; i++) { String s = randomRealisticUnicodeOfCodepointLengthBetween(1, 100); values.add(new BytesRef(s)); array.append(new BytesRef(s)); }/*from w w w .j ava 2s.c o m*/ if (randomBoolean()) { Collections.shuffle(values, getRandom()); } int[] indices = new int[array.size()]; for (int i = 0; i < indices.length; i++) { indices[i] = i; } CollectionUtils.sort(array, indices); Collections.sort(values); Iterator<BytesRef> iterator = values.iterator(); BytesRefBuilder spare = new BytesRefBuilder(); for (int i = 0; i < values.size(); i++) { assertThat(iterator.hasNext(), is(true)); assertThat(array.get(spare, indices[i]), equalTo(iterator.next())); } }