org.apache.solr.search.facet.UniqueSlotAcc Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of solr-core Show documentation
Show all versions of solr-core Show documentation
Apache Solr (module: core)
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.search.facet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.FixedBitSet;
import org.apache.solr.common.util.Hash;
import org.apache.solr.common.util.SimpleOrderedMap;
import org.apache.solr.schema.SchemaField;
import org.apache.solr.util.hll.HLL;
abstract class UniqueSlotAcc extends SlotAcc {
HLLAgg.HLLFactory factory;
SchemaField field;
FixedBitSet[] arr;
int[] counts; // populated with the cardinality once
int nTerms;
public UniqueSlotAcc(
FacetContext fcontext, SchemaField field, int numSlots, HLLAgg.HLLFactory factory)
throws IOException {
super(fcontext);
this.factory = factory;
arr = new FixedBitSet[numSlots];
this.field = field;
}
@Override
public void reset() throws IOException {
counts = null;
for (FixedBitSet bits : arr) {
if (bits == null) continue;
bits.clear(0, bits.length());
}
}
@Override
public Object getValue(int slot) throws IOException {
if (fcontext.isShard()) {
return getShardValue(slot);
}
return getNonShardValue(slot);
}
/** Returns the current slot value as long This is used to get non-sharded value */
public long getNonShardValue(int slot) {
long res;
if (counts != null) { // will only be pre-populated if this was used for sorting.
res = counts[slot];
} else {
FixedBitSet bs = arr[slot];
res = bs == null ? 0 : bs.cardinality();
}
return res;
}
private Object getShardHLL(int slot) throws IOException {
FixedBitSet ords = arr[slot];
if (ords == null) return HLLAgg.NO_VALUES;
HLL hll = factory.getHLL();
long maxOrd = ords.length();
Hash.LongPair hashResult = new Hash.LongPair();
for (int ord = -1; ++ord < maxOrd; ) {
ord = ords.nextSetBit(ord);
if (ord == DocIdSetIterator.NO_MORE_DOCS) break;
BytesRef val = lookupOrd(ord);
// way to avoid recomputing hash across slots? Prob not worth space
Hash.murmurhash3_x64_128(val.bytes, val.offset, val.length, 0, hashResult);
// idea: if the set is small enough, just send the hashes? We can add at the top
// level or even just do a hash table at the top level.
hll.addRaw(hashResult.val1);
}
SimpleOrderedMap