From e2a4b862603fa874a0d0b112e756f3fab7440156 Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Thu, 19 Jan 2012 18:10:51 +0000 Subject: [PATCH] LUCENE-3661: bitvector->mutablebits in indexwriter git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/lucene3661@1233507 13f79535-47bb-0310-9956-ffa450edef68 --- .../lucene/codecs/lucene40/BitVector.java | 2 +- .../org/apache/lucene/index/IndexWriter.java | 29 ++++++++++--------- .../org/apache/lucene/index/MergePolicy.java | 4 +-- .../apache/lucene/index/SegmentReader.java | 7 +++-- .../org/apache/lucene/util/MutableBits.java | 7 ++++- 5 files changed, 28 insertions(+), 21 deletions(-) diff --git a/lucene/src/java/org/apache/lucene/codecs/lucene40/BitVector.java b/lucene/src/java/org/apache/lucene/codecs/lucene40/BitVector.java index 7b4aff06a0e..e26422500f9 100644 --- a/lucene/src/java/org/apache/lucene/codecs/lucene40/BitVector.java +++ b/lucene/src/java/org/apache/lucene/codecs/lucene40/BitVector.java @@ -68,7 +68,7 @@ public final class BitVector implements Cloneable, MutableBits { } @Override - public Object clone() { + public BitVector clone() { byte[] copyBits = new byte[bits.length]; System.arraycopy(bits, 0, copyBits, 0, bits.length); BitVector clone = new BitVector(copyBits, size); diff --git a/lucene/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/src/java/org/apache/lucene/index/IndexWriter.java index 43a686f9b38..36e9a123bd5 100644 --- a/lucene/src/java/org/apache/lucene/index/IndexWriter.java +++ b/lucene/src/java/org/apache/lucene/index/IndexWriter.java @@ -34,7 +34,6 @@ import java.util.regex.Pattern; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.codecs.Codec; -import org.apache.lucene.codecs.lucene40.BitVector; import org.apache.lucene.index.DocumentsWriterPerThread.FlushedSegment; import org.apache.lucene.index.FieldInfos.FieldNumberBiMap; import org.apache.lucene.index.IndexWriterConfig.OpenMode; @@ -52,6 +51,7 @@ import org.apache.lucene.store.MergeInfo; import org.apache.lucene.util.Constants; import org.apache.lucene.util.IOUtils; import org.apache.lucene.util.InfoStream; +import org.apache.lucene.util.MutableBits; import org.apache.lucene.util.ThreadInterruptedException; import org.apache.lucene.util.TwoPhaseCommit; @@ -416,7 +416,7 @@ public class IndexWriter implements Closeable, TwoPhaseCommit { // docs, and it's copy-on-write (cloned whenever we need // to change it but it's been shared to an external NRT // reader). - public BitVector liveDocs; + public MutableBits liveDocs; // How many further deletions we've done against // liveDocs vs when we loaded it or last wrote it: @@ -486,7 +486,8 @@ public class IndexWriter implements Closeable, TwoPhaseCommit { if (reader == null) { reader = new SegmentReader(info, config.getReaderTermsIndexDivisor(), context); if (liveDocs == null) { - liveDocs = (BitVector) reader.getLiveDocs(); + // nocommit: nuke cast + liveDocs = (MutableBits) reader.getLiveDocs(); } //System.out.println("ADD seg=" + rld.info + " isMerge=" + isMerge + " " + readerMap.size() + " in pool"); } @@ -513,7 +514,7 @@ public class IndexWriter implements Closeable, TwoPhaseCommit { } else { mergeReader = new SegmentReader(info, -1, context); if (liveDocs == null) { - liveDocs = (BitVector) mergeReader.getLiveDocs(); + liveDocs = (MutableBits) mergeReader.getLiveDocs(); } } } @@ -567,7 +568,7 @@ public class IndexWriter implements Closeable, TwoPhaseCommit { } } - public synchronized void initWritableLiveDocs() { + public synchronized void initWritableLiveDocs() throws IOException { assert Thread.holdsLock(IndexWriter.this); //System.out.println("initWritableLivedocs seg=" + info + " liveDocs=" + liveDocs + " shared=" + shared); if (shared) { @@ -577,10 +578,9 @@ public class IndexWriter implements Closeable, TwoPhaseCommit { // change it: if (liveDocs == null) { //System.out.println("create BV seg=" + info); - liveDocs = new BitVector(info.docCount); - liveDocs.setAll(); + liveDocs = info.getCodec().liveDocsFormat().newLiveDocs(info.docCount); } else { - liveDocs = (BitVector) liveDocs.clone(); + liveDocs = liveDocs.clone(); } shared = false; } else { @@ -588,7 +588,8 @@ public class IndexWriter implements Closeable, TwoPhaseCommit { } } - public synchronized BitVector getReadOnlyLiveDocs() { + // nocommit: if this is read-only live docs, why doesn't it return Bits?! + public synchronized MutableBits getReadOnlyLiveDocs() { //System.out.println("getROLiveDocs seg=" + info); assert Thread.holdsLock(IndexWriter.this); shared = true; @@ -618,7 +619,7 @@ public class IndexWriter implements Closeable, TwoPhaseCommit { final String delFileName = info.getDelFileName(); boolean success = false; try { - liveDocs.write(dir, delFileName, IOContext.DEFAULT); + info.getCodec().liveDocsFormat().writeLiveDocs(liveDocs, dir, info, IOContext.DEFAULT); success = true; } finally { if (!success) { @@ -3035,8 +3036,8 @@ public class IndexWriter implements Closeable, TwoPhaseCommit { SegmentInfo info = sourceSegments.get(i); minGen = Math.min(info.getBufferedDeletesGen(), minGen); final int docCount = info.docCount; - final BitVector prevLiveDocs = merge.readerLiveDocs.get(i); - final BitVector currentLiveDocs; + final MutableBits prevLiveDocs = merge.readerLiveDocs.get(i); + final MutableBits currentLiveDocs; ReadersAndLiveDocs rld = readerPool.get(info, false); // We enrolled in mergeInit: assert rld != null; @@ -3576,7 +3577,7 @@ public class IndexWriter implements Closeable, TwoPhaseCommit { } merge.readers = new ArrayList(); - merge.readerLiveDocs = new ArrayList(); + merge.readerLiveDocs = new ArrayList(); // This is try/finally to make sure merger's readers are // closed: @@ -3595,7 +3596,7 @@ public class IndexWriter implements Closeable, TwoPhaseCommit { assert reader != null; // Carefully pull the most recent live docs: - final BitVector liveDocs; + final MutableBits liveDocs; synchronized(this) { // Must sync to ensure BufferedDeletesStream // cannot change liveDocs/pendingDeleteCount while diff --git a/lucene/src/java/org/apache/lucene/index/MergePolicy.java b/lucene/src/java/org/apache/lucene/index/MergePolicy.java index 58d7f05ea79..40b56d56449 100644 --- a/lucene/src/java/org/apache/lucene/index/MergePolicy.java +++ b/lucene/src/java/org/apache/lucene/index/MergePolicy.java @@ -22,9 +22,9 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -import org.apache.lucene.codecs.lucene40.BitVector; import org.apache.lucene.store.Directory; import org.apache.lucene.store.MergeInfo; +import org.apache.lucene.util.MutableBits; import org.apache.lucene.util.SetOnce.AlreadySetException; import org.apache.lucene.util.SetOnce; @@ -74,7 +74,7 @@ public abstract class MergePolicy implements java.io.Closeable { int maxNumSegments = -1; // used by IndexWriter public long estimatedMergeBytes; // used by IndexWriter List readers; // used by IndexWriter - List readerLiveDocs; // used by IndexWriter + List readerLiveDocs; // used by IndexWriter public final List segments; public final int totalDocCount; boolean aborted; diff --git a/lucene/src/java/org/apache/lucene/index/SegmentReader.java b/lucene/src/java/org/apache/lucene/index/SegmentReader.java index 0962e56c1f2..e58280f69d3 100644 --- a/lucene/src/java/org/apache/lucene/index/SegmentReader.java +++ b/lucene/src/java/org/apache/lucene/index/SegmentReader.java @@ -23,10 +23,11 @@ import org.apache.lucene.store.Directory; import org.apache.lucene.codecs.PerDocProducer; import org.apache.lucene.codecs.StoredFieldsReader; import org.apache.lucene.codecs.TermVectorsReader; -import org.apache.lucene.codecs.lucene40.BitVector; +import org.apache.lucene.codecs.lucene40.BitVector; // nocommit: move asserts/checks to codec import org.apache.lucene.search.FieldCache; // javadocs import org.apache.lucene.store.IOContext; import org.apache.lucene.util.Bits; +import org.apache.lucene.util.MutableBits; /** * @lucene.experimental @@ -92,7 +93,7 @@ public final class SegmentReader extends IndexReader { assert si.hasDeletions(); // ... but load our own deleted docs: - liveDocs = new BitVector(si.dir, si.getDelFileName(), context); + liveDocs = si.getCodec().liveDocsFormat().readLiveDocs(si.dir, si, context); numDocs = si.docCount - si.getDelCount(); assert checkLiveCounts(false); @@ -105,7 +106,7 @@ public final class SegmentReader extends IndexReader { // SegmentReader and using the provided in-memory // liveDocs. Used by IndexWriter to provide a new NRT // reader: - SegmentReader(SegmentReader parent, BitVector liveDocs, int numDocs) throws IOException { + SegmentReader(SegmentReader parent, MutableBits liveDocs, int numDocs) throws IOException { this.si = parent.si; parent.core.incRef(); this.core = parent.core; diff --git a/lucene/src/java/org/apache/lucene/util/MutableBits.java b/lucene/src/java/org/apache/lucene/util/MutableBits.java index 6dd3efad21a..5a4c253af44 100644 --- a/lucene/src/java/org/apache/lucene/util/MutableBits.java +++ b/lucene/src/java/org/apache/lucene/util/MutableBits.java @@ -17,8 +17,13 @@ package org.apache.lucene.util; * limitations under the License. */ -public interface MutableBits extends Bits { +public interface MutableBits extends Bits,Cloneable { public void clear(int bit); // nocommit: remove this from this interface somehow? (used by DWPT infostream at least) public int count(); + + // nocommit: are these truly necessary? + public boolean getAndSet(int bit); + public boolean getAndClear(int bit); + public MutableBits clone(); }