blob: 0fbed6c32fb5831b1feb1332fba7ceb4015d01e0 [file] [log] [blame]
/*
* 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.stanbol.enhancer.engines.lucenefstlinking.impl;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.index.ReaderUtil;
import org.apache.lucene.queries.function.FunctionValues;
import org.apache.lucene.queries.function.ValueSource;
import org.apache.lucene.search.IndexSearcher;
import org.apache.solr.common.SolrException;
/** See LUCENE-4541 or {@link org.apache.solr.response.transform.ValueSourceAugmenter}. */
public class ValueSourceAccessor {
// implement FunctionValues ?
private final List<AtomicReaderContext> readerContexts;
private final FunctionValues[] docValuesArr;
private final ValueSource valueSource;
private final Map fContext;
private int localId;
private FunctionValues values;
public ValueSourceAccessor(IndexSearcher searcher, ValueSource valueSource) {
readerContexts = searcher.getIndexReader().leaves();
this.valueSource = valueSource;
docValuesArr = new FunctionValues[readerContexts.size()];
fContext = ValueSource.newContext(searcher);
}
private void setState(int docid) {
int idx = ReaderUtil.subIndex(docid, readerContexts);
AtomicReaderContext rcontext = readerContexts.get(idx);
values = docValuesArr[idx];
if (values == null) {
try {
docValuesArr[idx] = values = valueSource.getValues(fContext, rcontext);
} catch (IOException e) {
throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, e);
}
}
localId = docid - rcontext.docBase;
}
public Object objectVal(int docid) {
setState(docid);
return values.objectVal(localId);
}
}