Removed synchronization from term lookup.

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@150361 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Doug Cutting 2004-06-07 16:55:52 +00:00
parent cdd9601158
commit 27821a7f9a
2 changed files with 39 additions and 23 deletions

View File

@ -21,6 +21,10 @@ $Id$
UTF-8 and changed the build encoding to UTF-8, to make changed files UTF-8 and changed the build encoding to UTF-8, to make changed files
compile. (Otis Gospodnetic) compile. (Otis Gospodnetic)
5. Removed synchronization from term lookup under IndexReader methods
termFreq(), termDocs() or termPositions() to improve
multi-threaded performance. (cutting)
1.4 RC3 1.4 RC3

View File

@ -29,7 +29,8 @@ final class TermInfosReader {
private String segment; private String segment;
private FieldInfos fieldInfos; private FieldInfos fieldInfos;
private SegmentTermEnum enumerator; private ThreadLocal enumerators = new ThreadLocal();
private SegmentTermEnum origEnum;
private long size; private long size;
TermInfosReader(Directory dir, String seg, FieldInfos fis) TermInfosReader(Directory dir, String seg, FieldInfos fis)
@ -38,19 +39,19 @@ final class TermInfosReader {
segment = seg; segment = seg;
fieldInfos = fis; fieldInfos = fis;
enumerator = new SegmentTermEnum(directory.openFile(segment + ".tis"), origEnum = new SegmentTermEnum(directory.openFile(segment + ".tis"),
fieldInfos, false); fieldInfos, false);
size = enumerator.size; size = origEnum.size;
readIndex(); readIndex();
} }
public int getSkipInterval() { public int getSkipInterval() {
return enumerator.skipInterval; return origEnum.skipInterval;
} }
final void close() throws IOException { final void close() throws IOException {
if (enumerator != null) if (origEnum != null)
enumerator.close(); origEnum.close();
} }
/** Returns the number of term/value pairs in the set. */ /** Returns the number of term/value pairs in the set. */
@ -58,6 +59,15 @@ final class TermInfosReader {
return size; return size;
} }
private SegmentTermEnum getEnum() {
SegmentTermEnum enum = (SegmentTermEnum)enumerators.get();
if (enum == null) {
enum = terms();
enumerators.set(enum);
}
return enum;
}
Term[] indexTerms = null; Term[] indexTerms = null;
TermInfo[] indexInfos; TermInfo[] indexInfos;
long[] indexPointers; long[] indexPointers;
@ -102,16 +112,17 @@ final class TermInfosReader {
} }
private final void seekEnum(int indexOffset) throws IOException { private final void seekEnum(int indexOffset) throws IOException {
enumerator.seek(indexPointers[indexOffset], getEnum().seek(indexPointers[indexOffset],
(indexOffset * enumerator.indexInterval) - 1, (indexOffset * getEnum().indexInterval) - 1,
indexTerms[indexOffset], indexInfos[indexOffset]); indexTerms[indexOffset], indexInfos[indexOffset]);
} }
/** Returns the TermInfo for a Term in the set, or null. */ /** Returns the TermInfo for a Term in the set, or null. */
final synchronized TermInfo get(Term term) throws IOException { TermInfo get(Term term) throws IOException {
if (size == 0) return null; if (size == 0) return null;
// optimize sequential access: first try scanning cached enumerator w/o seeking // optimize sequential access: first try scanning cached enum w/o seeking
SegmentTermEnum enumerator = getEnum();
if (enumerator.term() != null // term is at or past current if (enumerator.term() != null // term is at or past current
&& ((enumerator.prev != null && term.compareTo(enumerator.prev) > 0) && ((enumerator.prev != null && term.compareTo(enumerator.prev) > 0)
|| term.compareTo(enumerator.term()) >= 0)) { || term.compareTo(enumerator.term()) >= 0)) {
@ -128,6 +139,7 @@ final class TermInfosReader {
/** Scans within block for matching term. */ /** Scans within block for matching term. */
private final TermInfo scanEnum(Term term) throws IOException { private final TermInfo scanEnum(Term term) throws IOException {
SegmentTermEnum enumerator = getEnum();
while (term.compareTo(enumerator.term()) > 0 && enumerator.next()) {} while (term.compareTo(enumerator.term()) > 0 && enumerator.next()) {}
if (enumerator.term() != null && term.compareTo(enumerator.term()) == 0) if (enumerator.term() != null && term.compareTo(enumerator.term()) == 0)
return enumerator.termInfo(); return enumerator.termInfo();
@ -136,10 +148,12 @@ final class TermInfosReader {
} }
/** Returns the nth term in the set. */ /** Returns the nth term in the set. */
final synchronized Term get(int position) throws IOException { final Term get(int position) throws IOException {
if (size == 0) return null; if (size == 0) return null;
if (enumerator != null && enumerator.term() != null && position >= enumerator.position && SegmentTermEnum enumerator = getEnum();
if (enumerator != null && enumerator.term() != null &&
position >= enumerator.position &&
position < (enumerator.position + enumerator.indexInterval)) position < (enumerator.position + enumerator.indexInterval))
return scanEnum(position); // can avoid seek return scanEnum(position); // can avoid seek
@ -148,6 +162,7 @@ final class TermInfosReader {
} }
private final Term scanEnum(int position) throws IOException { private final Term scanEnum(int position) throws IOException {
SegmentTermEnum enumerator = getEnum();
while(enumerator.position < position) while(enumerator.position < position)
if (!enumerator.next()) if (!enumerator.next())
return null; return null;
@ -156,12 +171,13 @@ final class TermInfosReader {
} }
/** Returns the position of a Term in the set or -1. */ /** Returns the position of a Term in the set or -1. */
final synchronized long getPosition(Term term) throws IOException { final long getPosition(Term term) throws IOException {
if (size == 0) return -1; if (size == 0) return -1;
int indexOffset = getIndexOffset(term); int indexOffset = getIndexOffset(term);
seekEnum(indexOffset); seekEnum(indexOffset);
SegmentTermEnum enumerator = getEnum();
while(term.compareTo(enumerator.term()) > 0 && enumerator.next()) {} while(term.compareTo(enumerator.term()) > 0 && enumerator.next()) {}
if (term.compareTo(enumerator.term()) == 0) if (term.compareTo(enumerator.term()) == 0)
@ -171,17 +187,13 @@ final class TermInfosReader {
} }
/** Returns an enumeration of all the Terms and TermInfos in the set. */ /** Returns an enumeration of all the Terms and TermInfos in the set. */
final synchronized SegmentTermEnum terms() throws IOException { public SegmentTermEnum terms() {
if (enumerator.position != -1) // if not at start return (SegmentTermEnum)origEnum.clone();
seekEnum(0); // reset to start
return (SegmentTermEnum)enumerator.clone();
} }
/** Returns an enumeration of terms starting at or after the named term. */ /** Returns an enumeration of terms starting at or after the named term. */
final synchronized SegmentTermEnum terms(Term term) throws IOException { public SegmentTermEnum terms(Term term) throws IOException {
get(term); // seek enumerator to term get(term);
return (SegmentTermEnum)enumerator.clone(); return (SegmentTermEnum)getEnum().clone();
} }
} }