diff --git a/CHANGES.txt b/CHANGES.txt index 28377037047..1037598c56b 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -4,7 +4,8 @@ $Id$ 1.3 RC2 - 1. + 1. Added getFieldNames(boolean) to IndexReader, SegmentReader, and + SegmentsReader. (Julien Nioche via otis) 1.3 RC1 diff --git a/src/java/org/apache/lucene/index/IndexReader.java b/src/java/org/apache/lucene/index/IndexReader.java index eb2eb47fa5d..48ef356fe7f 100644 --- a/src/java/org/apache/lucene/index/IndexReader.java +++ b/src/java/org/apache/lucene/index/IndexReader.java @@ -3,8 +3,8 @@ package org.apache.lucene.index; /* ==================================================================== * The Apache Software License, Version 1.1 * - * Copyright (c) 2001 The Apache Software Foundation. All rights - * reserved. + * Copyright (c) 2001, 2002, 2003 The Apache Software Foundation. + * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -75,8 +75,8 @@ import org.apache.lucene.document.Field; // for javadoc document numbers, non-negative integers which each name a unique 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 - 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 { protected IndexReader(Directory directory) { this.directory = directory; @@ -209,7 +209,8 @@ public abstract class IndexReader { Term    =>    <docNum, freq>*

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 { TermDocs termDocs = termDocs(); termDocs.seek(term); @@ -233,7 +234,8 @@ public abstract class IndexReader {

This positional information faciliates phrase and proximity searching.

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 { TermPositions termPositions = termPositions(); termPositions.seek(term); @@ -248,7 +250,8 @@ public abstract class IndexReader { Attempts to read its field with the {@link #document} method will result in an error. The presence of this document may still be 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 { if (writeLock == null) { Lock writeLock = directory.makeLock("write.lock"); @@ -258,13 +261,15 @@ public abstract class IndexReader { } doDelete(docNum); } + abstract void doDelete(int docNum) throws IOException; /** Deletes all documents containing term. 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 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 { TermDocs docs = termDocs(term); 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 - * this IndexReader. - * @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() throws IOException; + /** + * Returns a list of all unique field names that exist in the index pointed to by + * this IndexReader. + * @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() 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 true if only indexed fields should be returned; + * false 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 true 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 */ 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 */ 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. */ public static void unlock(Directory directory) throws IOException { - directory.deleteFile("write.lock"); - directory.deleteFile("commit.lock"); + directory.deleteFile("write.lock"); + directory.deleteFile("commit.lock"); } } diff --git a/src/java/org/apache/lucene/index/SegmentReader.java b/src/java/org/apache/lucene/index/SegmentReader.java index bb912951cf0..67f86d5c00b 100644 --- a/src/java/org/apache/lucene/index/SegmentReader.java +++ b/src/java/org/apache/lucene/index/SegmentReader.java @@ -223,6 +223,33 @@ final class SegmentReader extends IndexReader { 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 { Norm norm = (Norm)norms.get(field); 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; - } } diff --git a/src/java/org/apache/lucene/index/SegmentsReader.java b/src/java/org/apache/lucene/index/SegmentsReader.java index 673340cae44..35385411b5e 100644 --- a/src/java/org/apache/lucene/index/SegmentsReader.java +++ b/src/java/org/apache/lucene/index/SegmentsReader.java @@ -3,8 +3,8 @@ package org.apache.lucene.index; /* ==================================================================== * The Apache Software License, Version 1.1 * - * Copyright (c) 2001 The Apache Software Foundation. All rights - * reserved. + * Copyright (c) 2001, 2002, 2003 The Apache Software Foundation. + * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions @@ -179,21 +179,37 @@ final class SegmentsReader extends IndexReader readers[i].close(); } - // javadoc inherited - public Collection getFieldNames() 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(); - // iterate through the field names and add them to the set - for (Iterator iterator = names.iterator(); iterator.hasNext();) { - String s = (String) iterator.next(); - fieldSet.add(s); - } + /** + * @see IndexReader#getFieldNames() + */ + public Collection getFieldNames() 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(); + // iterate through the field names and add them to the set + 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 {