package org.apache.lucene.index;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import org.apache.lucene.codecs.DocValuesProducer;
import org.apache.lucene.codecs.FieldsProducer;
import org.apache.lucene.codecs.NormsProducer;
import org.apache.lucene.codecs.PointsReader;
import org.apache.lucene.codecs.StoredFieldsReader;
import org.apache.lucene.codecs.TermVectorsReader;
import org.apache.lucene.util.Accountable;
import org.apache.lucene.util.Accountables;
import org.apache.lucene.util.FutureObjects;
public abstract class CodecReader extends LeafReader implements Accountable {
protected CodecReader() {}
public abstract StoredFieldsReader getFieldsReader();
public abstract TermVectorsReader getTermVectorsReader();
public abstract NormsProducer getNormsReader();
public abstract DocValuesProducer getDocValuesReader();
public abstract FieldsProducer getPostingsReader();
public abstract PointsReader getPointsReader();
@Override
public final void document(int docID, StoredFieldVisitor visitor) throws IOException {
checkBounds(docID);
getFieldsReader().visitDocument(docID, visitor);
}
@Override
public final Fields getTermVectors(int docID) throws IOException {
TermVectorsReader termVectorsReader = getTermVectorsReader();
if (termVectorsReader == null) {
return null;
}
checkBounds(docID);
return termVectorsReader.get(docID);
}
private void checkBounds(int docID) {
FutureObjects.checkIndex(docID, maxDoc());
}
@Override
public final Terms terms(String field) throws IOException {
return getPostingsReader().terms(field);
}
private FieldInfo getDVField(String field, DocValuesType type) {
FieldInfo fi = getFieldInfos().fieldInfo(field);
if (fi == null) {
return null;
}
if (fi.getDocValuesType() == DocValuesType.NONE) {
return null;
}
if (fi.getDocValuesType() != type) {
return null;
}
return fi;
}
@Override
public final NumericDocValues getNumericDocValues(String field) throws IOException {
ensureOpen();
FieldInfo fi = getDVField(field, DocValuesType.NUMERIC);
if (fi == null) {
return null;
}
return getDocValuesReader().getNumeric(fi);
}
@Override
public final BinaryDocValues getBinaryDocValues(String field) throws IOException {
ensureOpen();
FieldInfo fi = getDVField(field, DocValuesType.BINARY);
if (fi == null) {
return null;
}
return getDocValuesReader().getBinary(fi);
}
@Override
public final SortedDocValues getSortedDocValues(String field) throws IOException {
ensureOpen();
FieldInfo fi = getDVField(field, DocValuesType.SORTED);
if (fi == null) {
return null;
}
return getDocValuesReader().getSorted(fi);
}
@Override
public final SortedNumericDocValues getSortedNumericDocValues(String field) throws IOException {
ensureOpen();
FieldInfo fi = getDVField(field, DocValuesType.SORTED_NUMERIC);
if (fi == null) {
return null;
}
return getDocValuesReader().getSortedNumeric(fi);
}
@Override
public final SortedSetDocValues getSortedSetDocValues(String field) throws IOException {
ensureOpen();
FieldInfo fi = getDVField(field, DocValuesType.SORTED_SET);
if (fi == null) {
return null;
}
return getDocValuesReader().getSortedSet(fi);
}
@Override
public final NumericDocValues getNormValues(String field) throws IOException {
ensureOpen();
FieldInfo fi = getFieldInfos().fieldInfo(field);
if (fi == null || fi.hasNorms() == false) {
return null;
}
return getNormsReader().getNorms(fi);
}
@Override
public final PointValues getPointValues(String field) throws IOException {
ensureOpen();
FieldInfo fi = getFieldInfos().fieldInfo(field);
if (fi == null || fi.getPointDataDimensionCount() == 0) {
return null;
}
return getPointsReader().getValues(field);
}
@Override
protected void doClose() throws IOException {
}
@Override
public long ramBytesUsed() {
ensureOpen();
long ramBytesUsed = getPostingsReader().ramBytesUsed();
if (getNormsReader() != null) {
ramBytesUsed += getNormsReader().ramBytesUsed();
}
if (getDocValuesReader() != null) {
ramBytesUsed += getDocValuesReader().ramBytesUsed();
}
if (getFieldsReader() != null) {
ramBytesUsed += getFieldsReader().ramBytesUsed();
}
if (getTermVectorsReader() != null) {
ramBytesUsed += getTermVectorsReader().ramBytesUsed();
}
if (getPointsReader() != null) {
ramBytesUsed += getPointsReader().ramBytesUsed();
}
return ramBytesUsed;
}
@Override
public Collection<Accountable> getChildResources() {
ensureOpen();
final List<Accountable> resources = new ArrayList<>(6);
resources.add(Accountables.namedAccountable("postings", getPostingsReader()));
if (getNormsReader() != null) {
resources.add(Accountables.namedAccountable("norms", getNormsReader()));
}
if (getDocValuesReader() != null) {
resources.add(Accountables.namedAccountable("docvalues", getDocValuesReader()));
}
if (getFieldsReader() != null) {
resources.add(Accountables.namedAccountable("stored fields", getFieldsReader()));
}
if (getTermVectorsReader() != null) {
resources.add(Accountables.namedAccountable("term vectors", getTermVectorsReader()));
}
if (getPointsReader() != null) {
resources.add(Accountables.namedAccountable("points", getPointsReader()));
}
return Collections.unmodifiableList(resources);
}
@Override
public void checkIntegrity() throws IOException {
ensureOpen();
getPostingsReader().checkIntegrity();
if (getNormsReader() != null) {
getNormsReader().checkIntegrity();
}
if (getDocValuesReader() != null) {
getDocValuesReader().checkIntegrity();
}
if (getFieldsReader() != null) {
getFieldsReader().checkIntegrity();
}
if (getTermVectorsReader() != null) {
getTermVectorsReader().checkIntegrity();
}
if (getPointsReader() != null) {
getPointsReader().checkIntegrity();
}
}
}