Search in sources :

Example 1 with LongArray

use of org.opensearch.common.util.LongArray in project OpenSearch by opensearch-project.

the class SignificanceLookup method bytesLookup.

/**
 * Get the background frequency of a {@link BytesRef} term.
 */
BackgroundFrequencyForBytes bytesLookup(BigArrays bigArrays, CardinalityUpperBound cardinality) {
    if (cardinality == CardinalityUpperBound.ONE) {
        return new BackgroundFrequencyForBytes() {

            @Override
            public long freq(BytesRef term) throws IOException {
                return getBackgroundFrequency(term);
            }

            @Override
            public void close() {
            }
        };
    }
    return new BackgroundFrequencyForBytes() {

        private final BytesRefHash termToPosition = new BytesRefHash(1, bigArrays);

        private LongArray positionToFreq = bigArrays.newLongArray(1, false);

        @Override
        public long freq(BytesRef term) throws IOException {
            long position = termToPosition.add(term);
            if (position < 0) {
                return positionToFreq.get(-1 - position);
            }
            long freq = getBackgroundFrequency(term);
            positionToFreq = bigArrays.grow(positionToFreq, position + 1);
            positionToFreq.set(position, freq);
            return freq;
        }

        @Override
        public void close() {
            Releasables.close(termToPosition, positionToFreq);
        }
    };
}
Also used : LongArray(org.opensearch.common.util.LongArray) BytesRefHash(org.opensearch.common.util.BytesRefHash) BytesRef(org.apache.lucene.util.BytesRef)

Example 2 with LongArray

use of org.opensearch.common.util.LongArray in project OpenSearch by opensearch-project.

the class SignificanceLookup method longLookup.

/**
 * Get the background frequency of a {@code long} term.
 */
BackgroundFrequencyForLong longLookup(BigArrays bigArrays, CardinalityUpperBound cardinality) {
    if (cardinality == CardinalityUpperBound.ONE) {
        return new BackgroundFrequencyForLong() {

            @Override
            public long freq(long term) throws IOException {
                return getBackgroundFrequency(term);
            }

            @Override
            public void close() {
            }
        };
    }
    return new BackgroundFrequencyForLong() {

        private final LongHash termToPosition = new LongHash(1, bigArrays);

        private LongArray positionToFreq = bigArrays.newLongArray(1, false);

        @Override
        public long freq(long term) throws IOException {
            long position = termToPosition.add(term);
            if (position < 0) {
                return positionToFreq.get(-1 - position);
            }
            long freq = getBackgroundFrequency(term);
            positionToFreq = bigArrays.grow(positionToFreq, position + 1);
            positionToFreq.set(position, freq);
            return freq;
        }

        @Override
        public void close() {
            Releasables.close(termToPosition, positionToFreq);
        }
    };
}
Also used : LongArray(org.opensearch.common.util.LongArray) LongHash(org.opensearch.common.util.LongHash)

Aggregations

LongArray (org.opensearch.common.util.LongArray)2 BytesRef (org.apache.lucene.util.BytesRef)1 BytesRefHash (org.opensearch.common.util.BytesRefHash)1 LongHash (org.opensearch.common.util.LongHash)1