From f9bdb3cfd17f97561be21397713ffadf70adbd74 Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Fri, 11 Jan 2013 16:17:19 +0000 Subject: [PATCH] get slow-wrapper working with dv2.0 git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene4547@1432131 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/lucene/index/MultiDocValues.java | 43 +-------- .../lucene/index/MultiSimpleDocValues.java | 92 ++++++++++++++++++- .../index/SlowCompositeReaderWrapper.java | 8 +- 3 files changed, 96 insertions(+), 47 deletions(-) rename lucene/{test-framework => core}/src/java/org/apache/lucene/index/MultiSimpleDocValues.java (52%) diff --git a/lucene/core/src/java/org/apache/lucene/index/MultiDocValues.java b/lucene/core/src/java/org/apache/lucene/index/MultiDocValues.java index 1a1352272c5..0fb8e09222f 100644 --- a/lucene/core/src/java/org/apache/lucene/index/MultiDocValues.java +++ b/lucene/core/src/java/org/apache/lucene/index/MultiDocValues.java @@ -604,46 +604,5 @@ class MultiDocValues extends DocValues { @Override protected Source loadDirectSource() throws IOException { return new MultiSource(slices, starts, true, type); - } - - public static NumericDocValues simpleNormValues(final IndexReader r, final String field) throws IOException { - final List leaves = r.leaves(); - if (leaves.size() == 1) { - return leaves.get(0).reader().simpleNormValues(field); - } - FieldInfo fi = MultiFields.getMergedFieldInfos(r).fieldInfo(field); - if (fi == null || fi.hasNorms() == false) { - return null; - } - boolean anyReal = false; - for(AtomicReaderContext ctx : leaves) { - NumericDocValues norms = ctx.reader().simpleNormValues(field); - - if (norms != null) { - anyReal = true; - } - } - - // assert anyReal; // nocommit: unsafe until 4.0 is done - - return new NumericDocValues() { - @Override - public long get(int docID) { - int subIndex = ReaderUtil.subIndex(docID, leaves); - NumericDocValues norms; - try { - norms = leaves.get(subIndex).reader().simpleNormValues(field); - } catch (IOException ioe) { - throw new RuntimeException(ioe); - } - if (norms == null) { // WTF? should be EMPTY? - return 0; - } else { - return norms.get(docID - leaves.get(subIndex).docBase); - } - } - }; - } - - + } } diff --git a/lucene/test-framework/src/java/org/apache/lucene/index/MultiSimpleDocValues.java b/lucene/core/src/java/org/apache/lucene/index/MultiSimpleDocValues.java similarity index 52% rename from lucene/test-framework/src/java/org/apache/lucene/index/MultiSimpleDocValues.java rename to lucene/core/src/java/org/apache/lucene/index/MultiSimpleDocValues.java index fc547439734..c3bed867f0e 100644 --- a/lucene/test-framework/src/java/org/apache/lucene/index/MultiSimpleDocValues.java +++ b/lucene/core/src/java/org/apache/lucene/index/MultiSimpleDocValues.java @@ -18,15 +18,58 @@ package org.apache.lucene.index; */ import java.io.IOException; +import java.util.ArrayList; import java.util.List; +import org.apache.lucene.codecs.Codec; +import org.apache.lucene.index.IndexReader.ReaderClosedListener; +import org.apache.lucene.store.Directory; +import org.apache.lucene.store.RAMDirectory; +import org.apache.lucene.util.Bits; import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.IOUtils; +import org.apache.lucene.util.Version; public class MultiSimpleDocValues { // moved to src/java so SlowWrapper can use it... uggggggh public static NumericDocValues simpleNormValues(final IndexReader r, final String field) throws IOException { - return MultiDocValues.simpleNormValues(r, field); + final List leaves = r.leaves(); + if (leaves.size() == 1) { + return leaves.get(0).reader().simpleNormValues(field); + } + FieldInfo fi = MultiFields.getMergedFieldInfos(r).fieldInfo(field); + if (fi == null || fi.hasNorms() == false) { + return null; + } + boolean anyReal = false; + for(AtomicReaderContext ctx : leaves) { + NumericDocValues norms = ctx.reader().simpleNormValues(field); + + if (norms != null) { + anyReal = true; + } + } + + // assert anyReal; // nocommit: unsafe until 4.0 is done + + return new NumericDocValues() { + @Override + public long get(int docID) { + int subIndex = ReaderUtil.subIndex(docID, leaves); + NumericDocValues norms; + try { + norms = leaves.get(subIndex).reader().simpleNormValues(field); + } catch (IOException ioe) { + throw new RuntimeException(ioe); + } + if (norms == null) { // WTF? should be EMPTY? + return 0; + } else { + return norms.get(docID - leaves.get(subIndex).docBase); + } + } + }; } public static NumericDocValues simpleNumericValues(final IndexReader r, final String field) throws IOException { @@ -105,4 +148,51 @@ public class MultiSimpleDocValues { }; } } + + public static SortedDocValues simpleSortedValues(final IndexReader r, final String field) throws IOException { + final List leaves = r.leaves(); + if (leaves.size() == 1) { + return leaves.get(0).reader().getSortedDocValues(field); + } + boolean anyReal = false; + + for(AtomicReaderContext ctx : leaves) { + SortedDocValues values = ctx.reader().getSortedDocValues(field); + + if (values != null) { + anyReal = true; + } + } + + if (!anyReal) { + return null; + } else { + // its called slow-wrapper for a reason right? + final Directory scratch = new RAMDirectory(); + IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_50, null); + config.setCodec(Codec.forName("SimpleText")); + IndexWriter writer = new IndexWriter(scratch, config); + List newLeaves = new ArrayList(); + for (AtomicReaderContext ctx : leaves) { + final AtomicReader a = ctx.reader(); + newLeaves.add(new FilterAtomicReader(a) { + @Override + public Bits getLiveDocs() { + return null; // lie + } + }); + } + writer.addIndexes(newLeaves.toArray(new AtomicReader[0])); + writer.close(); + final IndexReader newR = DirectoryReader.open(scratch); + assert newR.leaves().size() == 1; + r.addReaderClosedListener(new ReaderClosedListener() { + @Override + public void onClose(IndexReader reader) { + IOUtils.closeWhileHandlingException(newR, scratch); + } + }); + return newR.leaves().get(0).reader().getSortedDocValues(field); + } + } } diff --git a/lucene/core/src/java/org/apache/lucene/index/SlowCompositeReaderWrapper.java b/lucene/core/src/java/org/apache/lucene/index/SlowCompositeReaderWrapper.java index b21cbfbefa8..af8070727ba 100644 --- a/lucene/core/src/java/org/apache/lucene/index/SlowCompositeReaderWrapper.java +++ b/lucene/core/src/java/org/apache/lucene/index/SlowCompositeReaderWrapper.java @@ -91,19 +91,19 @@ public final class SlowCompositeReaderWrapper extends AtomicReader { @Override public NumericDocValues getNumericDocValues(String field) throws IOException { ensureOpen(); - return null; // nocommit: UOE + return MultiSimpleDocValues.simpleNumericValues(in, field); } @Override public BinaryDocValues getBinaryDocValues(String field) throws IOException { ensureOpen(); - return null; // nocommit: UOE + return MultiSimpleDocValues.simpleBinaryValues(in, field); } @Override public SortedDocValues getSortedDocValues(String field) throws IOException { ensureOpen(); - return null; // nocommit: UOE + return MultiSimpleDocValues.simpleSortedValues(in, field); } @Override @@ -121,7 +121,7 @@ public final class SlowCompositeReaderWrapper extends AtomicReader { public NumericDocValues simpleNormValues(String field) throws IOException { ensureOpen(); // nocommit hmm - return MultiDocValues.simpleNormValues(in, field); + return MultiSimpleDocValues.simpleNormValues(in, field); } @Override