Example usage for org.apache.lucene.util BytesRef hashCode

List of usage examples for org.apache.lucene.util BytesRef hashCode

Introduction

In this page you can find the example usage for org.apache.lucene.util BytesRef hashCode.

Prototype

@Override
public int hashCode() 

Source Link

Document

Calculates the hash code as required by TermsHash during indexing.

Usage

From source file:org.elasticsearch.index.fielddata.plain.SortedSetDVAtomicFieldData.java

License:Apache License

public org.elasticsearch.index.fielddata.BytesValues.WithOrdinals getHashedBytesValues() {
    final SortedSetDocValues values = getValuesNoException(reader, field);
    if (hashes == null) {
        synchronized (this) {
            if (hashes == null) {
                final long valueCount = values.getValueCount();
                final IntArray hashes = BigArrays.newIntArray(1L + valueCount);
                BytesRef scratch = new BytesRef(16);
                hashes.set(0, scratch.hashCode());
                for (long i = 0; i < valueCount; ++i) {
                    values.lookupOrd(i, scratch);
                    hashes.set(1L + i, scratch.hashCode());
                }//from ww w . j  a v  a2s  .  c om
                this.hashes = hashes;
            }
        }
    }
    return new SortedSetHashedValues(reader, field, values, hashes);
}

From source file:org.elasticsearch.search.aggregations.bucket.BytesRefHashTests.java

License:Apache License

public void testDuell() {
    final BytesRef[] values = new BytesRef[randomIntBetween(1, 100000)];
    for (int i = 0; i < values.length; ++i) {
        values[i] = new BytesRef(randomAsciiOfLength(5));
    }//from   w ww  .  jav a 2  s. c  o  m
    final ObjectLongMap<BytesRef> valueToId = new ObjectLongOpenHashMap<BytesRef>();
    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.release();
}

From source file:org.elasticsearch.search.aggregations.bucket.terms.StringTermsAggregator.java

License:Apache License

@Override
public void collect(int doc, long owningBucketOrdinal) throws IOException {
    assert owningBucketOrdinal == 0;
    final int valuesCount = values.setDocument(doc);

    for (int i = 0; i < valuesCount; ++i) {
        final BytesRef bytes = values.nextValue();
        if (includeExclude != null && !includeExclude.accept(bytes)) {
            continue;
        }/*from w ww  .  j a va  2 s.c  om*/
        final int hash = values.currentValueHash();
        assert hash == bytes.hashCode();
        long bucketOrdinal = bucketOrds.add(bytes, hash);
        if (bucketOrdinal < 0) { // already seen
            bucketOrdinal = -1 - bucketOrdinal;
        }
        collectBucket(doc, bucketOrdinal);
    }
}

From source file:org.elasticsearch.search.aggregations.support.FieldDataSourceTests.java

License:Apache License

private static void assertConsistent(BytesValues values) {
    for (int i = 0; i < 10; ++i) {
        final int valueCount = values.setDocument(i);
        for (int j = 0; j < valueCount; ++j) {
            final BytesRef term = values.nextValue();
            assertEquals(term.hashCode(), values.currentValueHash());
            assertTrue(term.bytesEquals(values.copyShared()));
        }//  w w  w  .j  a  v a2  s . co  m
    }
}

From source file:org.elasticsearch.search.facet.terms.strings.TermsStringFacetExecutor.java

License:Apache License

static void loadAllTerms(SearchContext context, IndexFieldData indexFieldData, HashedAggregator aggregator) {

    for (AtomicReaderContext readerContext : context.searcher().getTopReaderContext().leaves()) {
        int maxDoc = readerContext.reader().maxDoc();
        if (indexFieldData instanceof IndexFieldData.WithOrdinals) {
            BytesValues.WithOrdinals values = ((IndexFieldData.WithOrdinals) indexFieldData).load(readerContext)
                    .getBytesValues(false);
            Ordinals.Docs ordinals = values.ordinals();
            // 0 = docs with no value for field, so start from 1 instead
            for (long ord = Ordinals.MIN_ORDINAL; ord < ordinals.getMaxOrd(); ord++) {
                BytesRef value = values.getValueByOrd(ord);
                aggregator.addValue(value, value.hashCode(), values);
            }/*w ww . j av a  2  s . c  o m*/
        } else {
            BytesValues values = indexFieldData.load(readerContext).getBytesValues(true);
            for (int docId = 0; docId < maxDoc; docId++) {
                final int size = values.setDocument(docId);
                for (int i = 0; i < size; i++) {
                    final BytesRef value = values.nextValue();
                    aggregator.addValue(value, values.currentValueHash(), values);
                }
            }
        }
    }
}

From source file:org.elasticsearch.search.slice.TermsSliceQueryTests.java

License:Apache License

public void testSearch() throws Exception {
    final int numDocs = randomIntBetween(100, 200);
    final Directory dir = newDirectory();
    final RandomIndexWriter w = new RandomIndexWriter(random(), dir, new KeywordAnalyzer());
    int max = randomIntBetween(2, 10);
    int[] sliceCounters = new int[max];
    Set<String> keys = new HashSet<>();
    for (int i = 0; i < numDocs; ++i) {
        Document doc = new Document();
        String uuid = UUIDs.base64UUID();
        BytesRef br = new BytesRef(uuid);
        int id = Math.floorMod(br.hashCode(), max);
        sliceCounters[id]++;/*from   w  ww . ja v a 2 s .co m*/
        doc.add(new StringField("uuid", uuid, Field.Store.YES));
        w.addDocument(doc);
        keys.add(uuid);
    }
    final IndexReader reader = w.getReader();
    final IndexSearcher searcher = newSearcher(reader);

    for (int id = 0; id < max; id++) {
        TermsSliceQuery query1 = new TermsSliceQuery("uuid", id, max);
        assertThat(searcher.count(query1), equalTo(sliceCounters[id]));
        searcher.search(query1, new Collector() {
            @Override
            public LeafCollector getLeafCollector(LeafReaderContext context) throws IOException {
                return new LeafCollector() {
                    @Override
                    public void setScorer(Scorer scorer) throws IOException {
                    }

                    @Override
                    public void collect(int doc) throws IOException {
                        Document d = context.reader().document(doc, Collections.singleton("uuid"));
                        String uuid = d.get("uuid");
                        assertThat(keys.contains(uuid), equalTo(true));
                        keys.remove(uuid);
                    }
                };
            }

            @Override
            public boolean needsScores() {
                return false;
            }
        });
    }
    assertThat(keys.size(), equalTo(0));
    w.close();
    reader.close();
    dir.close();
}

From source file:org.elasticsearch.test.unit.index.fielddata.DuellFieldDataTest.java

License:Apache License

private static void duellFieldDataBytes(Random random, AtomicReaderContext context, IndexFieldData left,
        IndexFieldData right, Preprocessor pre) throws Exception {
    AtomicFieldData leftData = random.nextBoolean() ? left.load(context) : left.loadDirect(context);
    AtomicFieldData rightData = random.nextBoolean() ? right.load(context) : right.loadDirect(context);
    assertThat(leftData.isMultiValued(), equalTo(rightData.isMultiValued()));
    assertThat(leftData.getNumDocs(), equalTo(rightData.getNumDocs()));

    int numDocs = leftData.getNumDocs();
    BytesValues leftBytesValues = random.nextBoolean() ? leftData.getBytesValues()
            : leftData.getHashedBytesValues();
    BytesValues rightBytesValues = random.nextBoolean() ? rightData.getBytesValues()
            : rightData.getHashedBytesValues();
    for (int i = 0; i < numDocs; i++) {
        assertThat(leftBytesValues.hasValue(i), equalTo(rightBytesValues.hasValue(i)));
        if (leftBytesValues.hasValue(i)) {
            assertThat(pre.toString(leftBytesValues.getValue(i)),
                    equalTo(pre.toString(rightBytesValues.getValue(i))));

        } else {//from www  .j a v  a  2 s .  c o m
            assertThat(leftBytesValues.getValue(i), nullValue());
            assertThat(rightBytesValues.getValue(i), nullValue());
        }

        boolean hasValue = leftBytesValues.hasValue(i);
        Iter leftIter = leftBytesValues.getIter(i);
        Iter rightIter = rightBytesValues.getIter(i);
        assertThat(leftIter.hasNext(), equalTo(rightIter.hasNext()));
        assertThat(leftIter.hasNext(), equalTo(hasValue));

        while (leftIter.hasNext()) {
            assertThat(hasValue, equalTo(true));
            assertThat(leftIter.hasNext(), equalTo(rightIter.hasNext()));
            BytesRef rightBytes = rightIter.next();
            BytesRef leftBytes = leftIter.next();

            assertThat(pre.toString(leftBytes), equalTo(pre.toString(rightBytes)));
            if (rightBytes.equals(leftBytes)) {
                assertThat(leftIter.hash(), equalTo(rightIter.hash()));// call twice
                assertThat(leftIter.hash(), equalTo(rightIter.hash()));
                assertThat(leftIter.hash(), equalTo(rightBytes.hashCode()));
                assertThat(rightIter.hash(), equalTo(leftBytes.hashCode()));
            }
        }
        assertThat(leftIter.hasNext(), equalTo(rightIter.hasNext()));
    }
}

From source file:org.fastcatsearch.ir.io.MappedFileBaseByteHashSet.java

License:Apache License

public boolean add(BytesRef key) {
    try {/*from w ww .  j  a  v a 2s. co m*/
        int hashValue = (Math.abs(key.hashCode()) % (bucketSize - 1)) + 1; //0? .

        int prev = 0;
        int id = readBucket(hashValue);

        logger.trace("------------------");
        logger.trace("key[{}] hash[{}] bucket[{}]", key, hashValue, id);
        while (id > 0) {
            if (isTheSame(key, id)) {
                //??  false.
                logger.trace("Dup entry!");
                return false;
            }

            prev = id;
            id = readNextIndex(id);
        }
        int idx = newIndex();
        logger.trace("Index[{}] {}", idx, key);

        int offset = newKeyPos(idx);
        buf.position(offset);
        logger.trace("write {} at {}", key, offset);
        for (int i = 0; i < key.length(); i++) {
            buf.put(key.get(i));

        }

        if (prev > 0) {
            writeNextIndex(prev, idx);
            logger.trace("writeNext prev[{}], id[{}]", prev, idx);
        } else {
            writeBucket(hashValue, idx);
            logger.trace("writeBucket hash[{}], id[{}]", hashValue, idx);
        }
        return true;
    } catch (Exception e) {
        logger.error("", e);
        return false;
    }
}