Search in sources :

Example 1 with ObjectLongCursor

use of com.carrotsearch.hppc.cursors.ObjectLongCursor in project elasticsearch by elastic.

the class BytesRefHashTests method testDuell.

public void testDuell() {
    final int len = randomIntBetween(1, 100000);
    final BytesRef[] values = new BytesRef[len];
    for (int i = 0; i < values.length; ++i) {
        values[i] = new BytesRef(randomAsciiOfLength(5));
    }
    final ObjectLongMap<BytesRef> valueToId = new ObjectLongHashMap<>();
    final BytesRef[] idToValue = new BytesRef[values.length];
    final int iters = randomInt(1000000);
    for (int i = 0; i < iters; ++i) {
        final BytesRef value = randomFrom(values);
        if (valueToId.containsKey(value)) {
            assertEquals(-1 - valueToId.get(value), hash.add(value, value.hashCode()));
        } else {
            assertEquals(valueToId.size(), hash.add(value, value.hashCode()));
            idToValue[valueToId.size()] = value;
            valueToId.put(value, valueToId.size());
        }
    }
    assertEquals(valueToId.size(), hash.size());
    for (Iterator<ObjectLongCursor<BytesRef>> iterator = valueToId.iterator(); iterator.hasNext(); ) {
        final ObjectLongCursor<BytesRef> next = iterator.next();
        assertEquals(next.value, hash.find(next.key, next.key.hashCode()));
    }
    for (long i = 0; i < hash.capacity(); ++i) {
        final long id = hash.id(i);
        BytesRef spare = new BytesRef();
        if (id >= 0) {
            hash.get(id, spare);
            assertEquals(idToValue[(int) id], spare);
        }
    }
    hash.close();
}
Also used : ObjectLongHashMap(com.carrotsearch.hppc.ObjectLongHashMap) ObjectLongCursor(com.carrotsearch.hppc.cursors.ObjectLongCursor) BytesRef(org.apache.lucene.util.BytesRef)

Aggregations

ObjectLongHashMap (com.carrotsearch.hppc.ObjectLongHashMap)1 ObjectLongCursor (com.carrotsearch.hppc.cursors.ObjectLongCursor)1 BytesRef (org.apache.lucene.util.BytesRef)1