📄 indexreader.java
字号:
package org.apache.lucene.index;/** * 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 org.apache.lucene.document.Document;import org.apache.lucene.document.FieldSelector;import org.apache.lucene.search.Similarity;import org.apache.lucene.store.*;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.util.Arrays;import java.util.Collection;/** IndexReader is an abstract class, providing an interface for accessing an index. Search of an index is done entirely through this abstract interface, so that any subclass which implements it is searchable. <p> Concrete subclasses of IndexReader are usually constructed with a call to one of the static <code>open()</code> methods, e.g. {@link #open(String)}. <p> For efficiency, in this API documents are often referred to via <i>document numbers</i>, 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. <p> An IndexReader can be opened on a directory for which an IndexWriter is opened already, but it cannot be used to delete documents from the index then. <p> NOTE: for backwards API compatibility, several methods are not listed as abstract, but have no useful implementations in this base class and instead always throw UnsupportedOperationException. Subclasses are strongly encouraged to override these methods, but in many cases may not need to. </p> @version $Id: IndexReader.java 598462 2007-11-26 23:31:39Z dnaber $*/public abstract class IndexReader { /** * Constants describing field properties, for example used for * {@link IndexReader#getFieldNames(FieldOption)}. */ public static final class FieldOption { private String option; private FieldOption() { } private FieldOption(String option) { this.option = option; } public String toString() { return this.option; } /** All fields */ public static final FieldOption ALL = new FieldOption ("ALL"); /** All indexed fields */ public static final FieldOption INDEXED = new FieldOption ("INDEXED"); /** All fields that store payloads */ public static final FieldOption STORES_PAYLOADS = new FieldOption ("STORES_PAYLOADS"); /** All fields which are not indexed */ public static final FieldOption UNINDEXED = new FieldOption ("UNINDEXED"); /** All fields which are indexed with termvectors enabled */ public static final FieldOption INDEXED_WITH_TERMVECTOR = new FieldOption ("INDEXED_WITH_TERMVECTOR"); /** All fields which are indexed but don't have termvectors enabled */ public static final FieldOption INDEXED_NO_TERMVECTOR = new FieldOption ("INDEXED_NO_TERMVECTOR"); /** All fields with termvectors enabled. Please note that only standard termvector fields are returned */ public static final FieldOption TERMVECTOR = new FieldOption ("TERMVECTOR"); /** All fields with termvectors with position values enabled */ public static final FieldOption TERMVECTOR_WITH_POSITION = new FieldOption ("TERMVECTOR_WITH_POSITION"); /** All fields with termvectors with offset values enabled */ public static final FieldOption TERMVECTOR_WITH_OFFSET = new FieldOption ("TERMVECTOR_WITH_OFFSET"); /** All fields with termvectors with offset values and position values enabled */ public static final FieldOption TERMVECTOR_WITH_POSITION_OFFSET = new FieldOption ("TERMVECTOR_WITH_POSITION_OFFSET"); } private boolean closed; protected boolean hasChanges; private volatile int refCount; // for testing synchronized int getRefCount() { return refCount; } /** * Increments the refCount of this IndexReader instance. RefCounts are used to determine * when a reader can be closed safely, i. e. as soon as no other IndexReader is referencing * it anymore. */ protected synchronized void incRef() { assert refCount > 0; refCount++; } /** * Decreases the refCount of this IndexReader instance. If the refCount drops * to 0, then pending changes are committed to the index and this reader is closed. * * @throws IOException in case an IOException occurs in commit() or doClose() */ protected synchronized void decRef() throws IOException { assert refCount > 0; if (refCount == 1) { commit(); doClose(); } refCount--; } /** * @deprecated will be deleted when IndexReader(Directory) is deleted * @see #directory() */ private Directory directory; /** * Legacy Constructor for backwards compatibility. * * <p> * This Constructor should not be used, it exists for backwards * compatibility only to support legacy subclasses that did not "own" * a specific directory, but needed to specify something to be returned * by the directory() method. Future subclasses should delegate to the * no arg constructor and implement the directory() method as appropriate. * * @param directory Directory to be returned by the directory() method * @see #directory() * @deprecated - use IndexReader() */ protected IndexReader(Directory directory) { this(); this.directory = directory; } protected IndexReader() { refCount = 1; } /** * @throws AlreadyClosedException if this IndexReader is closed */ protected final void ensureOpen() throws AlreadyClosedException { if (refCount <= 0) { throw new AlreadyClosedException("this IndexReader is closed"); } } /** Returns an IndexReader reading the index in an FSDirectory in the named path. * @throws CorruptIndexException if the index is corrupt * @throws IOException if there is a low-level IO error * @param path the path to the index directory */ public static IndexReader open(String path) throws CorruptIndexException, IOException { return open(FSDirectory.getDirectory(path), true, null); } /** Returns an IndexReader reading the index in an FSDirectory in the named * path. * @param path the path to the index directory * @throws CorruptIndexException if the index is corrupt * @throws IOException if there is a low-level IO error */ public static IndexReader open(File path) throws CorruptIndexException, IOException { return open(FSDirectory.getDirectory(path), true, null); } /** Returns an IndexReader reading the index in the given Directory. * @param directory the index directory * @throws CorruptIndexException if the index is corrupt * @throws IOException if there is a low-level IO error */ public static IndexReader open(final Directory directory) throws CorruptIndexException, IOException { return open(directory, false, null); } /** Expert: returns an IndexReader reading the index in the given * Directory, with a custom {@link IndexDeletionPolicy}. * @param directory the index directory * @param deletionPolicy a custom deletion policy (only used * if you use this reader to perform deletes or to set * norms); see {@link IndexWriter} for details. * @throws CorruptIndexException if the index is corrupt * @throws IOException if there is a low-level IO error */ public static IndexReader open(final Directory directory, IndexDeletionPolicy deletionPolicy) throws CorruptIndexException, IOException { return open(directory, false, deletionPolicy); } private static IndexReader open(final Directory directory, final boolean closeDirectory, final IndexDeletionPolicy deletionPolicy) throws CorruptIndexException, IOException { return DirectoryIndexReader.open(directory, closeDirectory, deletionPolicy); } /** * Refreshes an IndexReader if the index has changed since this instance * was (re)opened. * <p> * Opening an IndexReader is an expensive operation. This method can be used * to refresh an existing IndexReader to reduce these costs. This method * tries to only load segments that have changed or were created after the * IndexReader was (re)opened. * <p> * If the index has not changed since this instance was (re)opened, then this * call is a NOOP and returns this instance. Otherwise, a new instance is * returned. The old instance is <b>not</b> closed and remains usable.<br> * <b>Note:</b> The re-opened reader instance and the old instance might share * the same resources. For this reason no index modification operations * (e. g. {@link #deleteDocument(int)}, {@link #setNorm(int, String, byte)}) * should be performed using one of the readers until the old reader instance * is closed. <b>Otherwise, the behavior of the readers is undefined.</b> * <p> * You can determine whether a reader was actually reopened by comparing the * old instance with the instance returned by this method: * <pre> * IndexReader reader = ... * ... * IndexReader new = r.reopen(); * if (new != reader) { * ... // reader was reopened * reader.close(); * } * reader = new; * ... * </pre> * * @throws CorruptIndexException if the index is corrupt * @throws IOException if there is a low-level IO error */ public synchronized IndexReader reopen() throws CorruptIndexException, IOException { throw new UnsupportedOperationException("This reader does not support reopen()."); } /** * Returns the directory associated with this index. The Default * implementation returns the directory specified by subclasses when * delegating to the IndexReader(Directory) constructor, or throws an * UnsupportedOperationException if one was not specified. * @throws UnsupportedOperationException if no directory */ public Directory directory() { ensureOpen(); if (null != directory) { return directory; } else { throw new UnsupportedOperationException("This reader does not support this method."); } } /** * Returns the time the index in the named directory was last modified. * Do not use this to check whether the reader is still up-to-date, use * {@link #isCurrent()} instead. * @throws CorruptIndexException if the index is corrupt * @throws IOException if there is a low-level IO error */ public static long lastModified(String directory) throws CorruptIndexException, IOException { return lastModified(new File(directory)); } /** * Returns the time the index in the named directory was last modified. * Do not use this to check whether the reader is still up-to-date, use * {@link #isCurrent()} instead. * @throws CorruptIndexException if the index is corrupt * @throws IOException if there is a low-level IO error */ public static long lastModified(File fileDirectory) throws CorruptIndexException, IOException { return ((Long) new SegmentInfos.FindSegmentsFile(fileDirectory) { public Object doBody(String segmentFileName) { return new Long(FSDirectory.fileModified(fileDirectory, segmentFileName)); } }.run()).longValue(); } /** * Returns the time the index in the named directory was last modified. * Do not use this to check whether the reader is still up-to-date, use * {@link #isCurrent()} instead. * @throws CorruptIndexException if the index is corrupt * @throws IOException if there is a low-level IO error */ public static long lastModified(final Directory directory2) throws CorruptIndexException, IOException { return ((Long) new SegmentInfos.FindSegmentsFile(directory2) { public Object doBody(String segmentFileName) throws IOException { return new Long(directory2.fileModified(segmentFileName)); } }.run()).longValue(); } /** * Reads version number from segments files. The version number is * initialized with a timestamp and then increased by one for each change of * the index. * * @param directory where the index resides. * @return version number. * @throws CorruptIndexException if the index is corrupt * @throws IOException if there is a low-level IO error */ public static long getCurrentVersion(String directory) throws CorruptIndexException, IOException { return getCurrentVersion(new File(directory)); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -