make some progress fixing up lucene41 dv

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene4547@1418367 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Robert Muir 2012-12-07 15:55:26 +00:00
parent 916ce9b36a
commit 52f62fe470
3 changed files with 51 additions and 63 deletions

View File

@ -25,6 +25,7 @@ import org.apache.lucene.codecs.SortedDocValuesConsumer;
import org.apache.lucene.index.FieldInfo; import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.IndexFileNames; import org.apache.lucene.index.IndexFileNames;
import org.apache.lucene.index.SegmentInfo; import org.apache.lucene.index.SegmentInfo;
import org.apache.lucene.index.SegmentWriteState;
import org.apache.lucene.store.CompoundFileDirectory; import org.apache.lucene.store.CompoundFileDirectory;
import org.apache.lucene.store.Directory; import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IOContext; import org.apache.lucene.store.IOContext;
@ -50,19 +51,27 @@ public class Lucene41DocValuesConsumer extends SimpleDVConsumer {
private final Directory dir; private final Directory dir;
private Directory cfs; private Directory cfs;
private final IOContext context; private final IOContext context;
private final String segmentSuffix;
Lucene41DocValuesConsumer(Directory dir, SegmentInfo si, IOContext context) Lucene41DocValuesConsumer(SegmentWriteState state) throws IOException {
throws IOException { this.dir = state.directory;
this.dir = dir; this.info = state.segmentInfo;
this.info = si; this.context = state.context;
this.context = context; this.segmentSuffix = state.segmentSuffix;
} }
private synchronized Directory getDirectory() throws IOException { private synchronized Directory getDirectory() throws IOException {
if (cfs == null) { if (cfs == null) {
cfs = new CompoundFileDirectory(dir, IndexFileNames.segmentFileName(info.name, DV_SEGMENT_SUFFIX, final String suffix;
IndexFileNames.COMPOUND_FILE_EXTENSION), context, true); if (segmentSuffix.length() == 0) {
suffix = Lucene41DocValuesConsumer.DV_SEGMENT_SUFFIX;
} else {
suffix = segmentSuffix + "_" + Lucene41DocValuesConsumer.DV_SEGMENT_SUFFIX;
}
String fileName = IndexFileNames.segmentFileName(info.name,
suffix,
IndexFileNames.COMPOUND_FILE_EXTENSION);
cfs = new CompoundFileDirectory(dir, fileName, context, true);
} }
return cfs; return cfs;
} }
@ -113,6 +122,7 @@ public class Lucene41DocValuesConsumer extends SimpleDVConsumer {
} }
} }
// nocommit: bogus to put segmentName in here. think about copySegmentAsIs!!!!!!
static String getDocValuesFileName(SegmentInfo info, FieldInfo field, String extension) { static String getDocValuesFileName(SegmentInfo info, FieldInfo field, String extension) {
return IndexFileNames.segmentFileName(info.name + "_" return IndexFileNames.segmentFileName(info.name + "_"
+ field.number, DV_SEGMENT_SUFFIX, extension); + field.number, DV_SEGMENT_SUFFIX, extension);

View File

@ -34,13 +34,13 @@ public class Lucene41DocValuesFormat extends SimpleDocValuesFormat {
@Override @Override
public SimpleDVConsumer fieldsConsumer(SegmentWriteState state) public SimpleDVConsumer fieldsConsumer(SegmentWriteState state)
throws IOException { throws IOException {
return new Lucene41DocValuesConsumer(state.directory, state.segmentInfo, state.context); return new Lucene41DocValuesConsumer(state);
} }
@Override @Override
public SimpleDVProducer fieldsProducer(SegmentReadState state) public SimpleDVProducer fieldsProducer(SegmentReadState state)
throws IOException { throws IOException {
return new Lucene41DocValuesProducer(state.directory, state.segmentInfo, state.fieldInfos, state.context); return new Lucene41DocValuesProducer(state);
} }
} }

View File

@ -18,22 +18,17 @@ package org.apache.lucene.codecs.lucene41.values;
*/ */
import java.io.Closeable; import java.io.Closeable;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.lucene.codecs.SimpleDVProducer; import org.apache.lucene.codecs.SimpleDVProducer;
import org.apache.lucene.index.BinaryDocValues; import org.apache.lucene.index.BinaryDocValues;
import org.apache.lucene.index.DocValues; import org.apache.lucene.index.DocValues;
import org.apache.lucene.index.FieldInfo; import org.apache.lucene.index.FieldInfo;
import org.apache.lucene.index.FieldInfos;
import org.apache.lucene.index.IndexFileNames; import org.apache.lucene.index.IndexFileNames;
import org.apache.lucene.index.NumericDocValues; import org.apache.lucene.index.NumericDocValues;
import org.apache.lucene.index.SegmentInfo; import org.apache.lucene.index.SegmentInfo;
import org.apache.lucene.index.SegmentReadState;
import org.apache.lucene.index.SortedDocValues; import org.apache.lucene.index.SortedDocValues;
import org.apache.lucene.store.CompoundFileDirectory; import org.apache.lucene.store.CompoundFileDirectory;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.IOContext; import org.apache.lucene.store.IOContext;
import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.IOUtils;
@ -41,77 +36,60 @@ import org.apache.lucene.util.IOUtils;
public class Lucene41DocValuesProducer extends SimpleDVProducer { public class Lucene41DocValuesProducer extends SimpleDVProducer {
private final CompoundFileDirectory cfs; private final CompoundFileDirectory cfs;
// nocommit: remove this
private final SegmentInfo info; private final SegmentInfo info;
private final Map<String,DocValuesFactory<NumericDocValues>> numeric = new HashMap<String,DocValuesFactory<NumericDocValues>>(); private final IOContext context;
private final Map<String,DocValuesFactory<BinaryDocValues>> binary = new HashMap<String,DocValuesFactory<BinaryDocValues>>();
private final Map<String,DocValuesFactory<SortedDocValues>> sorted = new HashMap<String,DocValuesFactory<SortedDocValues>>();
public Lucene41DocValuesProducer(Directory dir, SegmentInfo segmentInfo, public Lucene41DocValuesProducer(SegmentReadState state) throws IOException {
FieldInfos fieldInfos, IOContext context) throws IOException { final String suffix;
this.cfs = new CompoundFileDirectory(dir, IndexFileNames.segmentFileName( if (state.segmentSuffix.length() == 0) {
segmentInfo.name, Lucene41DocValuesConsumer.DV_SEGMENT_SUFFIX, suffix = Lucene41DocValuesConsumer.DV_SEGMENT_SUFFIX;
IndexFileNames.COMPOUND_FILE_EXTENSION), context, false);
this.info = segmentInfo;
for (FieldInfo fieldInfo : fieldInfos) {
if (fieldInfo.hasDocValues()) {
if (DocValues.isNumber(fieldInfo.getDocValuesType())
|| DocValues.isFloat(fieldInfo.getDocValuesType())) {
numeric.put(fieldInfo.name, new Lucene41NumericDocValues.Factory(
this.cfs, this.info, fieldInfo, context));
} else if (DocValues.isBytes(fieldInfo.getDocValuesType())) {
binary.put(fieldInfo.name, new Lucene41BinaryDocValues.Factory(
this.cfs, this.info, fieldInfo, context));
} else { } else {
assert DocValues.isSortedBytes(fieldInfo.getDocValuesType()); suffix = state.segmentSuffix + "_" + Lucene41DocValuesConsumer.DV_SEGMENT_SUFFIX;
sorted.put(fieldInfo.name, new Lucene41SortedDocValues.Factory(
this.cfs, this.info, fieldInfo, context));
}
}
} }
String cfsFileName = IndexFileNames.segmentFileName(state.segmentInfo.name, suffix,
IndexFileNames.COMPOUND_FILE_EXTENSION);
this.cfs = new CompoundFileDirectory(state.directory, cfsFileName, state.context, false);
this.info = state.segmentInfo;
this.context = state.context;
} }
@Override @Override
public void close() throws IOException { public void close() throws IOException {
try {
List<Closeable> closeables = new ArrayList<Closeable>(numeric.values());
closeables.addAll(binary.values());
closeables.addAll(sorted.values());
IOUtils.close(closeables);
} finally {
IOUtils.close(cfs); IOUtils.close(cfs);
} }
}
@Override @Override
public SimpleDVProducer clone() { public SimpleDVProducer clone() {
// nocommit todo return this; // nocommit ? actually safe since we open new each time from cfs?
return null;
} }
@Override @Override
public NumericDocValues getNumeric(FieldInfo field) throws IOException { public NumericDocValues getNumeric(FieldInfo field) throws IOException {
return valueOrNull(numeric, field); if (DocValues.isNumber(field.getDocValuesType()) || DocValues.isFloat(field.getDocValuesType())) {
return new Lucene41NumericDocValues.Factory(this.cfs, this.info, field, context).getDirect();
} else {
return null;
}
} }
@Override @Override
public BinaryDocValues getBinary(FieldInfo field) throws IOException { public BinaryDocValues getBinary(FieldInfo field) throws IOException {
return valueOrNull(binary, field); if (DocValues.isBytes(field.getDocValuesType()) || DocValues.isSortedBytes(field.getDocValuesType())) {
return new Lucene41BinaryDocValues.Factory(this.cfs, this.info, field, context).getDirect();
} else {
return null;
}
} }
@Override @Override
public SortedDocValues getSorted(FieldInfo field) throws IOException { public SortedDocValues getSorted(FieldInfo field) throws IOException {
return valueOrNull(sorted, field); if (DocValues.isSortedBytes(field.getDocValuesType())) {
} return new Lucene41SortedDocValues.Factory(this.cfs, this.info, field, context).getDirect();
} else {
private static <T> T valueOrNull(Map<String,DocValuesFactory<T>> map,
FieldInfo field) throws IOException {
final DocValuesFactory<T> docValuesFactory = map.get(field.name);
if (docValuesFactory != null) {
return docValuesFactory.getDirect();
}
return null; return null;
} }
}
public static abstract class DocValuesFactory<T> implements Closeable { public static abstract class DocValuesFactory<T> implements Closeable {