📄 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> <b>NOTE</b>: 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> <p> <b>NOTE</b>: as of 2.4, it's possible to open a read-only IndexReader using one of the static open methods that accepts the boolean readOnly parameter. Such a reader has better concurrency as it's not necessary to synchronize on the isDeleted method. Currently the default for readOnly is false, meaning if not specified you will get a read/write IndexReader. But in 3.0 this default will change to true, meaning you must explicitly specify false if you want to make changes with the resulting IndexReader. </p> @version $Id: IndexReader.java 695510 2008-09-15 15:33:15Z otis $*/public abstract class IndexReader { // NOTE: in 3.0 this will change to true final static boolean READ_ONLY_DEFAULT = false; /** * 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 that omit tf */ public static final FieldOption OMIT_TF = new FieldOption ("OMIT_TF"); /** 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; } /** * Expert: increments the refCount of this IndexReader * instance. RefCounts are used to determine when a * reader can be closed safely, i.e. as soon as there are * no more references. Be sure to always call a * corresponding {@link #decRef}, in a finally clause; * otherwise the reader may never be closed. Note that * {@link #close} simply calls decRef(), which means that * the IndexReader will not really be closed until {@link * #decRef} has been called for all outstanding * references. * * @see #decRef */ public synchronized void incRef() { assert refCount > 0; ensureOpen(); refCount++; } /** * Expert: decreases the refCount of this IndexReader * instance. If the refCount drops to 0, then pending * changes (if any) are committed to the index and this * reader is closed. * * @throws IOException in case an IOException occurs in commit() or doClose() * * @see #incRef */ public synchronized void decRef() throws IOException { assert refCount > 0; ensureOpen(); 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 a read/write IndexReader reading the index in an FSDirectory in the named path. <b>NOTE</b>: starting in 3.0 this will return a readOnly IndexReader. * @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, null, READ_ONLY_DEFAULT); } /** Returns a read/write IndexReader reading the index in an FSDirectory in the named * path. <b>NOTE</b>: starting in 3.0 this will return a readOnly IndexReader. * @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, null, READ_ONLY_DEFAULT); } /** Returns a read/write IndexReader reading the index in * the given Directory. <b>NOTE</b>: starting in 3.0 this * will return a readOnly IndexReader. * @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, null, READ_ONLY_DEFAULT); } /** Returns a read/write or read only IndexReader reading the index in the given Directory. * @param directory the index directory * @param readOnly true if no changes (deletions, norms) will be made with this IndexReader * @throws CorruptIndexException if the index is corrupt * @throws IOException if there is a low-level IO error */ public static IndexReader open(final Directory directory, boolean readOnly) throws CorruptIndexException, IOException { return open(directory, false, null, null, readOnly); } /** Expert: returns a read/write IndexReader reading the index in the given * {@link IndexCommit}. <b>NOTE</b>: starting in 3.0 this * will return a readOnly IndexReader. * @param commit the commit point to open * @throws CorruptIndexException if the index is corrupt * @throws IOException if there is a low-level IO error */ public static IndexReader open(final IndexCommit commit) throws CorruptIndexException, IOException { return open(commit.getDirectory(), false, null, commit, READ_ONLY_DEFAULT); } /** Expert: returns a read/write IndexReader reading the index in the given * Directory, with a custom {@link IndexDeletionPolicy}. * <b>NOTE</b>: starting in 3.0 this will return a * readOnly IndexReader. * @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, null, READ_ONLY_DEFAULT); } /** Expert: returns a read/write or read only IndexReader reading the index in the given * Directory, with a custom {@link IndexDeletionPolicy}. * <b>NOTE</b>: starting in 3.0 this will return a * readOnly IndexReader. * @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. * @param readOnly true if no changes (deletions, norms) will be made with this IndexReader * @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, boolean readOnly) throws CorruptIndexException, IOException { return open(directory, false, deletionPolicy, null, readOnly); } /** Expert: returns a read/write IndexReader reading the index in the given * Directory, using a specific commit and with a custom * {@link IndexDeletionPolicy}. <b>NOTE</b>: starting in * 3.0 this will return a readOnly IndexReader. * @param commit the specific {@link IndexCommit} to open; * see {@link IndexReader#listCommits} to list all commits * in a 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 IndexCommit commit, IndexDeletionPolicy deletionPolicy) throws CorruptIndexException, IOException { return open(commit.getDirectory(), false, deletionPolicy, commit, READ_ONLY_DEFAULT); } /** Expert: returns a read/write or read only IndexReader reading the index in the given * Directory, using a specific commit and with a custom {@link IndexDeletionPolicy}. * @param commit the specific {@link IndexCommit} to open; * see {@link IndexReader#listCommits} to list all commits * in a 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. * @param readOnly true if no changes (deletions, norms) will be made with this IndexReader * @throws CorruptIndexException if the index is corrupt * @throws IOException if there is a low-level IO error */ public static IndexReader open(final IndexCommit commit, IndexDeletionPolicy deletionPolicy, boolean readOnly) throws CorruptIndexException, IOException { return open(commit.getDirectory(), false, deletionPolicy, commit, readOnly); } private static IndexReader open(final Directory directory, final boolean closeDirectory, final IndexDeletionPolicy deletionPolicy, final IndexCommit commit, final boolean readOnly) throws CorruptIndexException, IOException { return DirectoryIndexReader.open(directory, closeDirectory, deletionPolicy, commit, readOnly); } /** * 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 {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -