LUCENE-4315: Minor fixes for Fields abstract class, TermVectorsWriter, remove UnmodifiableIterator (added before, without issue).

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1375522 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Uwe Schindler 2012-08-21 13:12:56 +00:00
parent e53aee7739
commit 5cb2b9b9f2
16 changed files with 45 additions and 93 deletions

View File

@ -18,6 +18,7 @@ package org.apache.lucene.codecs;
*/
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.TreeMap;
@ -39,7 +40,6 @@ import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.DoubleBarrelLRUCache;
import org.apache.lucene.util.UnmodifiableIterator;
/** Handles a terms dict, but decouples all details of
* doc/freqs/positions reading to an instance of {@link
@ -185,7 +185,7 @@ public class BlockTermsReader extends FieldsProducer {
@Override
public Iterator<String> iterator() {
return new UnmodifiableIterator<String>(fields.keySet().iterator());
return Collections.unmodifiableSet(fields.keySet()).iterator();
}
@Override

View File

@ -21,6 +21,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Locale;
@ -45,7 +46,6 @@ import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.RamUsageEstimator;
import org.apache.lucene.util.StringHelper;
import org.apache.lucene.util.UnmodifiableIterator;
import org.apache.lucene.util.automaton.CompiledAutomaton;
import org.apache.lucene.util.automaton.RunAutomaton;
import org.apache.lucene.util.automaton.Transition;
@ -200,7 +200,7 @@ public class BlockTreeTermsReader extends FieldsProducer {
@Override
public Iterator<String> iterator() {
return new UnmodifiableIterator<String>(fields.keySet().iterator());
return Collections.unmodifiableSet(fields.keySet()).iterator();
}
@Override

View File

@ -20,6 +20,7 @@ package org.apache.lucene.codecs;
import java.io.Closeable;
import java.io.IOException;
import java.util.Comparator;
import java.util.Iterator;
import org.apache.lucene.index.AtomicReader;
import org.apache.lucene.index.DocsAndPositionsEnum;
@ -187,19 +188,21 @@ public abstract class TermVectorsWriter implements Closeable {
}
/** Safe (but, slowish) default method to write every
* vector field in the document. This default
* implementation requires that the vectors implement
* both Fields.size and
* Terms.size. */
* vector field in the document. */
protected final void addAllDocVectors(Fields vectors, MergeState mergeState) throws IOException {
if (vectors == null) {
startDocument(0);
return;
}
final int numFields = vectors.size();
int numFields = vectors.size();
if (numFields == -1) {
throw new IllegalStateException("vectors.size() must be implemented (it returned -1)");
// count manually! TODO: Maybe enforce that Fields.size() returns something valid?
numFields = 0;
for (final Iterator<String> it = vectors.iterator(); it.hasNext(); ) {
it.next();
numFields++;
}
}
startDocument(numFields);
@ -208,7 +211,9 @@ public abstract class TermVectorsWriter implements Closeable {
TermsEnum termsEnum = null;
DocsAndPositionsEnum docsAndPositionsEnum = null;
int fieldCount = 0;
for(String fieldName : vectors) {
fieldCount++;
final FieldInfo fieldInfo = mergeState.fieldInfos.fieldInfo(fieldName);
assert lastFieldName == null || fieldName.compareTo(lastFieldName) > 0: "lastFieldName=" + lastFieldName + " fieldName=" + fieldName;
@ -225,9 +230,14 @@ public abstract class TermVectorsWriter implements Closeable {
final boolean hasPayloads = terms.hasPayloads();
assert !hasPayloads || hasPositions;
final int numTerms = (int) terms.size();
int numTerms = (int) terms.size();
if (numTerms == -1) {
throw new IllegalStateException("terms.size() must be implemented (it returned -1)");
// count manually. It is stupid, but needed, as Terms.size() is not a mandatory statistics function
numTerms = 0;
termsEnum = terms.iterator(termsEnum);
while(termsEnum.next() != null) {
numTerms++;
}
}
startField(fieldInfo, numTerms, hasPositions, hasOffsets, hasPayloads);
@ -263,6 +273,7 @@ public abstract class TermVectorsWriter implements Closeable {
}
assert termCount == numTerms;
}
assert fieldCount == numFields;
}
/** Return the BytesRef Comparator used to sort terms

View File

@ -207,7 +207,7 @@ public class BloomFilteringPostingsFormat extends PostingsFormat {
}
}
public int size() throws IOException {
public int size() {
return delegateFieldsProducer.size();
}

View File

@ -18,6 +18,7 @@ package org.apache.lucene.codecs.memory;
*/
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
@ -43,7 +44,6 @@ import org.apache.lucene.store.RAMOutputStream;
import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.UnmodifiableIterator;
import org.apache.lucene.util.automaton.CompiledAutomaton;
import org.apache.lucene.util.automaton.RunAutomaton;
import org.apache.lucene.util.automaton.Transition;
@ -131,7 +131,7 @@ public class DirectPostingsFormat extends PostingsFormat {
@Override
public Iterator<String> iterator() {
return new UnmodifiableIterator<String>(fields.keySet().iterator());
return Collections.unmodifiableSet(fields.keySet()).iterator();
}
@Override

View File

@ -18,6 +18,7 @@ package org.apache.lucene.codecs.memory;
*/
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.SortedMap;
@ -48,7 +49,6 @@ import org.apache.lucene.util.ArrayUtil;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.IntsRef;
import org.apache.lucene.util.UnmodifiableIterator;
import org.apache.lucene.util.fst.Builder;
import org.apache.lucene.util.fst.ByteSequenceOutputs;
import org.apache.lucene.util.fst.BytesRefFSTEnum;
@ -863,7 +863,7 @@ public class MemoryPostingsFormat extends PostingsFormat {
return new FieldsProducer() {
@Override
public Iterator<String> iterator() {
return new UnmodifiableIterator<String>(fields.keySet().iterator());
return Collections.unmodifiableSet(fields.keySet()).iterator();
}
@Override

View File

@ -19,6 +19,7 @@ package org.apache.lucene.codecs.perfield;
import java.io.Closeable;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@ -34,7 +35,6 @@ import org.apache.lucene.index.SegmentReadState;
import org.apache.lucene.index.SegmentWriteState;
import org.apache.lucene.index.Terms;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.UnmodifiableIterator;
/**
* Enables per field format support.
@ -199,7 +199,7 @@ public abstract class PerFieldPostingsFormat extends PostingsFormat {
@Override
public Iterator<String> iterator() {
return new UnmodifiableIterator<String>(fields.keySet().iterator());
return Collections.unmodifiableSet(fields.keySet()).iterator();
}
@Override

View File

@ -18,6 +18,7 @@ package org.apache.lucene.codecs.simpletext;
*/
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
@ -43,7 +44,6 @@ import org.apache.lucene.util.IntsRef;
import org.apache.lucene.util.OpenBitSet;
import org.apache.lucene.util.StringHelper;
import org.apache.lucene.util.UnicodeUtil;
import org.apache.lucene.util.UnmodifiableIterator;
import org.apache.lucene.util.fst.Builder;
import org.apache.lucene.util.fst.BytesRefFSTEnum;
import org.apache.lucene.util.fst.FST;
@ -608,7 +608,7 @@ class SimpleTextFieldsReader extends FieldsProducer {
@Override
public Iterator<String> iterator() {
return new UnmodifiableIterator<String>(fields.keySet().iterator());
return Collections.unmodifiableSet(fields.keySet()).iterator();
}
private final Map<String,Terms> termsCache = new HashMap<String,Terms>();

View File

@ -19,6 +19,7 @@ package org.apache.lucene.codecs.simpletext;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.Map;
@ -44,8 +45,6 @@ import org.apache.lucene.util.CharsRef;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.StringHelper;
import org.apache.lucene.util.UnicodeUtil;
import org.apache.lucene.util.UnmodifiableIterator;
import static org.apache.lucene.codecs.simpletext.SimpleTextTermVectorsWriter.*;
/**
@ -241,7 +240,7 @@ public class SimpleTextTermVectorsReader extends TermVectorsReader {
@Override
public Iterator<String> iterator() {
return new UnmodifiableIterator<String>(fields.keySet().iterator());
return Collections.unmodifiableSet(fields.keySet()).iterator();
}
@Override
@ -250,7 +249,7 @@ public class SimpleTextTermVectorsReader extends TermVectorsReader {
}
@Override
public int size() throws IOException {
public int size() {
return fields.size();
}
}

View File

@ -36,7 +36,7 @@ public abstract class Fields implements Iterable<String> {
/** Returns the number of fields or -1 if the number of
* distinct field names is unknown. If &gt;= 0,
* {@link #iterator} will return as many field names. */
public abstract int size() throws IOException;
public abstract int size();
/** Returns the number of terms for all fields, or -1 if this
* measure isn't stored by the codec. Note that, just like

View File

@ -57,7 +57,7 @@ public class FilterAtomicReader extends AtomicReader {
}
@Override
public int size() throws IOException {
public int size() {
return in.size();
}

View File

@ -27,7 +27,6 @@ import java.util.SortedMap;
import java.util.TreeMap;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.UnmodifiableIterator;
/** An {@link AtomicReader} which reads multiple, parallel indexes. Each index
@ -163,7 +162,7 @@ public final class ParallelAtomicReader extends AtomicReader {
@Override
public Iterator<String> iterator() {
return new UnmodifiableIterator<String>(fields.keySet().iterator());
return Collections.unmodifiableSet(fields.keySet()).iterator();
}
@Override

View File

@ -1110,14 +1110,8 @@ class FieldCacheImpl implements FieldCache {
// Try for coarse estimate for number of bits; this
// should be an underestimate most of the time, which
// is fine -- GrowableWriter will reallocate as needed
long numUniqueTerms = 0;
try {
numUniqueTerms = terms.size();
} catch (UnsupportedOperationException uoe) {
numUniqueTerms = -1;
}
if (numUniqueTerms != -1) {
long numUniqueTerms = terms.size();
if (numUniqueTerms != -1L) {
if (numUniqueTerms > termCountHardLimit) {
// app is misusing the API (there is more than
// one term per doc); in this case we make best
@ -1248,13 +1242,8 @@ class FieldCacheImpl implements FieldCache {
// Try for coarse estimate for number of bits; this
// should be an underestimate most of the time, which
// is fine -- GrowableWriter will reallocate as needed
long numUniqueTerms = 0;
try {
numUniqueTerms = terms.size();
} catch (UnsupportedOperationException uoe) {
numUniqueTerms = -1;
}
if (numUniqueTerms != -1) {
long numUniqueTerms = terms.size();
if (numUniqueTerms != -1L) {
if (numUniqueTerms > termCountHardLimit) {
numUniqueTerms = termCountHardLimit;
}

View File

@ -1,46 +0,0 @@
package org.apache.lucene.util;
/*
* 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.
*/
import java.util.Iterator;
/**
* Wraps an iterator to ensure its unmodifiable
*/
public class UnmodifiableIterator<T> implements Iterator<T> {
private final Iterator<T> in;
public UnmodifiableIterator(Iterator<T> in) {
this.in = in;
}
@Override
public boolean hasNext() {
return in.hasNext();
}
@Override
public T next() {
return in.next();
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}

View File

@ -83,7 +83,7 @@ public class AssertingPostingsFormat extends PostingsFormat {
}
@Override
public int size() throws IOException {
public int size() {
return in.size();
}

View File

@ -19,6 +19,7 @@ package org.apache.lucene.codecs.ramonly;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
@ -49,7 +50,6 @@ import org.apache.lucene.store.IndexOutput;
import org.apache.lucene.util.Bits;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.IOUtils;
import org.apache.lucene.util.UnmodifiableIterator;
/** Stores all postings data in RAM, but writes a small
* token (header + single int) to identify which "slot" the
@ -113,7 +113,7 @@ public class RAMOnlyPostingsFormat extends PostingsFormat {
@Override
public Iterator<String> iterator() {
return new UnmodifiableIterator<String>(fieldToTerms.keySet().iterator());
return Collections.unmodifiableSet(fieldToTerms.keySet()).iterator();
}
@Override