- Added method getFieldNames(boolean).

Submitted by:	Julien Nioche
Reviewed by:	Otis


git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@149981 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Otis Gospodnetic 2003-05-01 01:09:15 +00:00
parent f980ec23e5
commit 49db32469f
4 changed files with 95 additions and 46 deletions

View File

@ -4,7 +4,8 @@ $Id$
1.3 RC2 1.3 RC2
1. 1. Added getFieldNames(boolean) to IndexReader, SegmentReader, and
SegmentsReader. (Julien Nioche via otis)
1.3 RC1 1.3 RC1

View File

@ -3,8 +3,8 @@ package org.apache.lucene.index;
/* ==================================================================== /* ====================================================================
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
* *
* Copyright (c) 2001 The Apache Software Foundation. All rights * Copyright (c) 2001, 2002, 2003 The Apache Software Foundation.
* reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -75,8 +75,8 @@ import org.apache.lucene.document.Field; // for javadoc
<i>document numbers</i>, non-negative integers which each name a unique <i>document numbers</i>, non-negative integers which each name a unique
document in the index. These document numbers are ephemeral--they may change document in the index. These document numbers are ephemeral--they may change
as documents are added to and deleted from an index. Clients should thus not as documents are added to and deleted from an index. Clients should thus not
rely on a given document having the same number between sessions. */ rely on a given document having the same number between sessions.
*/
public abstract class IndexReader { public abstract class IndexReader {
protected IndexReader(Directory directory) { protected IndexReader(Directory directory) {
this.directory = directory; this.directory = directory;
@ -209,7 +209,8 @@ public abstract class IndexReader {
Term &nbsp;&nbsp; =&gt; &nbsp;&nbsp; &lt;docNum, freq&gt;<sup>*</sup> Term &nbsp;&nbsp; =&gt; &nbsp;&nbsp; &lt;docNum, freq&gt;<sup>*</sup>
</ul> </ul>
<p>The enumeration is ordered by document number. Each document number <p>The enumeration is ordered by document number. Each document number
is greater than all that precede it in the enumeration. */ is greater than all that precede it in the enumeration.
*/
public TermDocs termDocs(Term term) throws IOException { public TermDocs termDocs(Term term) throws IOException {
TermDocs termDocs = termDocs(); TermDocs termDocs = termDocs();
termDocs.seek(term); termDocs.seek(term);
@ -233,7 +234,8 @@ public abstract class IndexReader {
</ul> </ul>
<p> This positional information faciliates phrase and proximity searching. <p> This positional information faciliates phrase and proximity searching.
<p>The enumeration is ordered by document number. Each document number is <p>The enumeration is ordered by document number. Each document number is
greater than all that precede it in the enumeration. */ greater than all that precede it in the enumeration.
*/
public TermPositions termPositions(Term term) throws IOException { public TermPositions termPositions(Term term) throws IOException {
TermPositions termPositions = termPositions(); TermPositions termPositions = termPositions();
termPositions.seek(term); termPositions.seek(term);
@ -248,7 +250,8 @@ public abstract class IndexReader {
Attempts to read its field with the {@link #document} Attempts to read its field with the {@link #document}
method will result in an error. The presence of this document may still be method will result in an error. The presence of this document may still be
reflected in the {@link #docFreq} statistic, though reflected in the {@link #docFreq} statistic, though
this will be corrected eventually as the index is further modified. */ this will be corrected eventually as the index is further modified.
*/
public final synchronized void delete(int docNum) throws IOException { public final synchronized void delete(int docNum) throws IOException {
if (writeLock == null) { if (writeLock == null) {
Lock writeLock = directory.makeLock("write.lock"); Lock writeLock = directory.makeLock("write.lock");
@ -258,13 +261,15 @@ public abstract class IndexReader {
} }
doDelete(docNum); doDelete(docNum);
} }
abstract void doDelete(int docNum) throws IOException; abstract void doDelete(int docNum) throws IOException;
/** Deletes all documents containing <code>term</code>. /** Deletes all documents containing <code>term</code>.
This is useful if one uses a document field to hold a unique ID string for This is useful if one uses a document field to hold a unique ID string for
the document. Then to delete such a document, one merely constructs a the document. Then to delete such a document, one merely constructs a
term with the appropriate field and the unique ID string as its text and term with the appropriate field and the unique ID string as its text and
passes it to this method. Returns the number of documents deleted. */ passes it to this method. Returns the number of documents deleted.
*/
public final int delete(Term term) throws IOException { public final int delete(Term term) throws IOException {
TermDocs docs = termDocs(term); TermDocs docs = termDocs(term);
if ( docs == null ) return 0; if ( docs == null ) return 0;
@ -304,13 +309,24 @@ public abstract class IndexReader {
} }
} }
/** /**
* Return a list of all unique field names which exist in the index pointed to by * Returns a list of all unique field names that exist in the index pointed to by
* this IndexReader. * this IndexReader.
* @return Collection of Strings indicating the names of the fields * @return Collection of Strings indicating the names of the fields
* @throws IOException if there is a problem with accessing the index * @throws IOException if there is a problem with accessing the index
*/ */
public abstract Collection getFieldNames() throws IOException; public abstract Collection getFieldNames() throws IOException;
/**
* Returns a list of all unique field names that exist in the index pointed to by
* this IndexReader. The boolean argument specifies whether the fields returned
* are indexed or not.
* @param indexed <code>true</code> if only indexed fields should be returned;
* <code>false</code> if only unindexed fields should be returned.
* @return Collection of Strings indicating the names of the fields
* @throws IOException if there is a problem with accessing the index
*/
public abstract Collection getFieldNames(boolean indexed) throws IOException;
/** /**
* Returns <code>true</code> iff the index in the named directory is * Returns <code>true</code> iff the index in the named directory is
@ -319,7 +335,7 @@ public abstract class IndexReader {
* @throws IOException if there is a problem with accessing the index * @throws IOException if there is a problem with accessing the index
*/ */
public static boolean isLocked(Directory directory) throws IOException { public static boolean isLocked(Directory directory) throws IOException {
return directory.fileExists("write.lock"); return directory.fileExists("write.lock");
} }
/** /**
@ -329,7 +345,7 @@ public abstract class IndexReader {
* @throws IOException if there is a problem with accessing the index * @throws IOException if there is a problem with accessing the index
*/ */
public static boolean isLocked(String directory) throws IOException { public static boolean isLocked(String directory) throws IOException {
return (new File(directory, "write.lock")).exists(); return (new File(directory, "write.lock")).exists();
} }
/** /**
@ -340,7 +356,7 @@ public abstract class IndexReader {
* currently accessing this index. * currently accessing this index.
*/ */
public static void unlock(Directory directory) throws IOException { public static void unlock(Directory directory) throws IOException {
directory.deleteFile("write.lock"); directory.deleteFile("write.lock");
directory.deleteFile("commit.lock"); directory.deleteFile("commit.lock");
} }
} }

View File

@ -223,6 +223,33 @@ final class SegmentReader extends IndexReader {
return fieldsReader.size(); return fieldsReader.size();
} }
/**
* @see IndexReader#getFieldNames()
*/
public Collection getFieldNames() throws IOException {
// maintain a unique set of field names
Set fieldSet = new HashSet();
for (int i = 0; i < fieldInfos.size(); i++) {
FieldInfo fi = fieldInfos.fieldInfo(i);
fieldSet.add(fi.name);
}
return fieldSet;
}
/**
* @see IndexReader#getFieldNames(boolean)
*/
public Collection getFieldNames(boolean indexed) throws IOException {
// maintain a unique set of field names
Set fieldSet = new HashSet();
for (int i = 0; i < fieldInfos.size(); i++) {
FieldInfo fi = fieldInfos.fieldInfo(i);
if (fi.isIndexed == indexed)
fieldSet.add(fi.name);
}
return fieldSet;
}
public final byte[] norms(String field) throws IOException { public final byte[] norms(String field) throws IOException {
Norm norm = (Norm)norms.get(field); Norm norm = (Norm)norms.get(field);
if (norm == null) if (norm == null)
@ -273,15 +300,4 @@ final class SegmentReader extends IndexReader {
} }
} }
} }
// javadoc inherited
public Collection getFieldNames() throws IOException {
// maintain a unique set of field names
Set fieldSet = new HashSet();
for (int i = 0; i < fieldInfos.size(); i++) {
FieldInfo fi = fieldInfos.fieldInfo(i);
fieldSet.add(fi.name);
}
return fieldSet;
}
} }

View File

@ -3,8 +3,8 @@ package org.apache.lucene.index;
/* ==================================================================== /* ====================================================================
* The Apache Software License, Version 1.1 * The Apache Software License, Version 1.1
* *
* Copyright (c) 2001 The Apache Software Foundation. All rights * Copyright (c) 2001, 2002, 2003 The Apache Software Foundation.
* reserved. * All rights reserved.
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions * modification, are permitted provided that the following conditions
@ -179,21 +179,37 @@ final class SegmentsReader extends IndexReader
readers[i].close(); readers[i].close();
} }
// javadoc inherited /**
public Collection getFieldNames() throws IOException { * @see IndexReader#getFieldNames()
// maintain a unique set of field names */
Set fieldSet = new HashSet(); public Collection getFieldNames() throws IOException {
for (int i = 0; i < readers.length; i++) { // maintain a unique set of field names
SegmentReader reader = readers[i]; Set fieldSet = new HashSet();
Collection names = reader.getFieldNames(); for (int i = 0; i < readers.length; i++) {
// iterate through the field names and add them to the set SegmentReader reader = readers[i];
for (Iterator iterator = names.iterator(); iterator.hasNext();) { Collection names = reader.getFieldNames();
String s = (String) iterator.next(); // iterate through the field names and add them to the set
fieldSet.add(s); for (Iterator iterator = names.iterator(); iterator.hasNext();) {
} String s = (String) iterator.next();
fieldSet.add(s);
} }
return fieldSet;
} }
return fieldSet;
}
/**
* @see IndexReader#getFieldNames(boolean)
*/
public Collection getFieldNames(boolean indexed) throws IOException {
// maintain a unique set of field names
Set fieldSet = new HashSet();
for (int i = 0; i < readers.length; i++) {
SegmentReader reader = readers[i];
Collection names = reader.getFieldNames(indexed);
fieldSet.addAll(names);
}
return fieldSet;
}
} }
class SegmentsTermEnum extends TermEnum { class SegmentsTermEnum extends TermEnum {