Search in sources :

Example 96 with BytesRefBuilder

use of org.apache.lucene.util.BytesRefBuilder in project elasticsearch by elastic.

the class BytesRefHashTests method testSize.

// START - tests borrowed from LUCENE
/**
     * Test method for {@link org.apache.lucene.util.BytesRefHash#size()}.
     */
public void testSize() {
    BytesRefBuilder ref = new BytesRefBuilder();
    int num = scaledRandomIntBetween(2, 20);
    for (int j = 0; j < num; j++) {
        final int mod = 1 + randomInt(40);
        for (int i = 0; i < 797; i++) {
            String str;
            do {
                str = TestUtil.randomRealisticUnicodeString(random(), 1000);
            } while (str.length() == 0);
            ref.copyChars(str);
            long count = hash.size();
            long key = hash.add(ref.get());
            if (key < 0)
                assertEquals(hash.size(), count);
            else
                assertEquals(hash.size(), count + 1);
            if (i % mod == 0) {
                newHash();
            }
        }
    }
    hash.close();
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder)

Example 97 with BytesRefBuilder

use of org.apache.lucene.util.BytesRefBuilder in project elasticsearch by elastic.

the class CollectionUtilsTests method testSortAndDedupByteRefArray.

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));
    }
    if (randomBoolean()) {
        Collections.shuffle(tmpList, random());
        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()));
    }
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) BytesRefArray(org.apache.lucene.util.BytesRefArray) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) BytesRef(org.apache.lucene.util.BytesRef)

Example 98 with BytesRefBuilder

use of org.apache.lucene.util.BytesRefBuilder in project elasticsearch by elastic.

the class BytesRefHashTests method testAdd.

/**
     * Test method for
     * {@link org.apache.lucene.util.BytesRefHash#add(org.apache.lucene.util.BytesRef)}
     * .
     */
public void testAdd() {
    BytesRefBuilder ref = new BytesRefBuilder();
    BytesRef scratch = new BytesRef();
    int num = scaledRandomIntBetween(2, 20);
    for (int j = 0; j < num; j++) {
        Set<String> strings = new HashSet<>();
        int uniqueCount = 0;
        for (int i = 0; i < 797; i++) {
            String str;
            do {
                str = TestUtil.randomRealisticUnicodeString(random(), 1000);
            } while (str.length() == 0);
            ref.copyChars(str);
            long count = hash.size();
            long key = hash.add(ref.get());
            if (key >= 0) {
                assertTrue(strings.add(str));
                assertEquals(uniqueCount, key);
                assertEquals(hash.size(), count + 1);
                uniqueCount++;
            } else {
                assertFalse(strings.add(str));
                assertTrue((-key) - 1 < count);
                assertEquals(str, hash.get((-key) - 1, scratch).utf8ToString());
                assertEquals(count, hash.size());
            }
        }
        assertAllIn(strings, hash);
        newHash();
    }
    hash.close();
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) BytesRef(org.apache.lucene.util.BytesRef) HashSet(java.util.HashSet)

Example 99 with BytesRefBuilder

use of org.apache.lucene.util.BytesRefBuilder in project lucene-solr by apache.

the class TestLegacyNumericUtils method testIntConversionAndOrdering.

public void testIntConversionAndOrdering() throws Exception {
    // generate a series of encoded ints, each numerical one bigger than the one before
    BytesRefBuilder act = new BytesRefBuilder();
    BytesRefBuilder last = new BytesRefBuilder();
    for (int i = -100000; i < 100000; i++) {
        LegacyNumericUtils.intToPrefixCoded(i, 0, act);
        if (last != null) {
            // test if smaller
            assertTrue("actual bigger than last (BytesRef)", last.get().compareTo(act.get()) < 0);
            assertTrue("actual bigger than last (as String)", last.get().utf8ToString().compareTo(act.get().utf8ToString()) < 0);
        }
        // test is back and forward conversion works
        assertEquals("forward and back conversion should generate same int", i, LegacyNumericUtils.prefixCodedToInt(act.get()));
        // next step
        last.copyBytes(act.get());
    }
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder)

Example 100 with BytesRefBuilder

use of org.apache.lucene.util.BytesRefBuilder in project lucene-solr by apache.

the class TestLegacyNumericUtils method testLongConversionAndOrdering.

public void testLongConversionAndOrdering() throws Exception {
    // generate a series of encoded longs, each numerical one bigger than the one before
    BytesRefBuilder last = new BytesRefBuilder();
    BytesRefBuilder act = new BytesRefBuilder();
    for (long l = -100000L; l < 100000L; l++) {
        LegacyNumericUtils.longToPrefixCoded(l, 0, act);
        if (last != null) {
            // test if smaller
            assertTrue("actual bigger than last (BytesRef)", last.get().compareTo(act.get()) < 0);
            assertTrue("actual bigger than last (as String)", last.get().utf8ToString().compareTo(act.get().utf8ToString()) < 0);
        }
        // test is back and forward conversion works
        assertEquals("forward and back conversion should generate same long", l, LegacyNumericUtils.prefixCodedToLong(act.get()));
        // next step
        last.copyBytes(act);
    }
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder)

Aggregations

BytesRefBuilder (org.apache.lucene.util.BytesRefBuilder)150 BytesRef (org.apache.lucene.util.BytesRef)79 ArrayList (java.util.ArrayList)21 IOException (java.io.IOException)17 Term (org.apache.lucene.index.Term)16 HashSet (java.util.HashSet)15 ChecksumIndexInput (org.apache.lucene.store.ChecksumIndexInput)14 FieldType (org.apache.solr.schema.FieldType)14 IndexInput (org.apache.lucene.store.IndexInput)12 BytesRefIterator (org.apache.lucene.util.BytesRefIterator)10 CharsRefBuilder (org.apache.lucene.util.CharsRefBuilder)10 IntsRef (org.apache.lucene.util.IntsRef)10 SchemaField (org.apache.solr.schema.SchemaField)10 BufferedChecksumIndexInput (org.apache.lucene.store.BufferedChecksumIndexInput)9 ParseException (java.text.ParseException)8 IntsRefBuilder (org.apache.lucene.util.IntsRefBuilder)8 DecimalFormat (java.text.DecimalFormat)7 HashMap (java.util.HashMap)7 Map (java.util.Map)7 Directory (org.apache.lucene.store.Directory)7