📄 indexreader.cs
字号:
/*
* 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.
*/
using System;
using Document = Lucene.Net.Documents.Document;
using FieldSelector = Lucene.Net.Documents.FieldSelector;
using Similarity = Lucene.Net.Search.Similarity;
using Directory = Lucene.Net.Store.Directory;
using FSDirectory = Lucene.Net.Store.FSDirectory;
using IndexInput = Lucene.Net.Store.IndexInput;
using Lock = Lucene.Net.Store.Lock;
namespace Lucene.Net.Index
{
/// <summary>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.
/// </summary>
/// <author> Doug Cutting
/// </author>
/// <version> $Id: IndexReader.java 497612 2007-01-18 22:47:03Z mikemccand $
/// </version>
public abstract class IndexReader
{
private class AnonymousClassFindSegmentsFile : SegmentInfos.FindSegmentsFile
{
private void InitBlock(bool closeDirectory)
{
this.closeDirectory = closeDirectory;
}
private bool closeDirectory;
internal AnonymousClassFindSegmentsFile(bool closeDirectory, Lucene.Net.Store.Directory Param1) : base(Param1)
{
InitBlock(closeDirectory);
}
public override System.Object DoBody(System.String segmentFileName)
{
SegmentInfos infos = new SegmentInfos();
infos.Read(directory, segmentFileName);
if (infos.Count == 1)
{
// index is optimized
return SegmentReader.Get(infos, infos.Info(0), closeDirectory);
}
else
{
// To reduce the chance of hitting FileNotFound
// (and having to retry), we open segments in
// reverse because IndexWriter merges & deletes
// the newest segments first.
IndexReader[] readers = new IndexReader[infos.Count];
for (int i = infos.Count - 1; i >= 0; i--)
{
try
{
readers[i] = SegmentReader.Get(infos.Info(i));
}
catch (System.IO.IOException e)
{
// Close all readers we had opened:
for (i++; i < infos.Count; i++)
{
readers[i].Close();
}
throw e;
}
}
return new MultiReader(directory, infos, closeDirectory, readers);
}
}
}
private class AnonymousClassFindSegmentsFile1 : SegmentInfos.FindSegmentsFile
{
internal AnonymousClassFindSegmentsFile1(System.IO.FileInfo Param1):base(Param1)
{
}
public override System.Object DoBody(System.String segmentFileName)
{
return (long) FSDirectory.FileModified(fileDirectory, segmentFileName);
}
}
private class AnonymousClassFindSegmentsFile2 : SegmentInfos.FindSegmentsFile
{
private void InitBlock(Lucene.Net.Store.Directory directory2)
{
this.directory2 = directory2;
}
private Lucene.Net.Store.Directory directory2;
internal AnonymousClassFindSegmentsFile2(Lucene.Net.Store.Directory directory2, Lucene.Net.Store.Directory Param1):base(Param1)
{
InitBlock(directory2);
}
public override System.Object DoBody(System.String segmentFileName)
{
return (long) directory2.FileModified(segmentFileName);
}
}
public sealed class FieldOption
{
private System.String option;
internal FieldOption()
{
}
internal FieldOption(System.String option)
{
this.option = option;
}
public override System.String ToString()
{
return this.option;
}
// all fields
public static readonly FieldOption ALL = new FieldOption("ALL");
// all indexed fields
public static readonly FieldOption INDEXED = new FieldOption("INDEXED");
// all fields which are not indexed
public static readonly FieldOption UNINDEXED = new FieldOption("UNINDEXED");
// all fields which are indexed with termvectors enables
public static readonly FieldOption INDEXED_WITH_TERMVECTOR = new FieldOption("INDEXED_WITH_TERMVECTOR");
// all fields which are indexed but don't have termvectors enabled
public static readonly FieldOption INDEXED_NO_TERMVECTOR = new FieldOption("INDEXED_NO_TERMVECTOR");
// all fields where termvectors are enabled. Please note that only standard termvector fields are returned
public static readonly FieldOption TERMVECTOR = new FieldOption("TERMVECTOR");
// all field with termvectors wiht positions enabled
public static readonly FieldOption TERMVECTOR_WITH_POSITION = new FieldOption("TERMVECTOR_WITH_POSITION");
// all fields where termvectors with offset position are set
public static readonly FieldOption TERMVECTOR_WITH_OFFSET = new FieldOption("TERMVECTOR_WITH_OFFSET");
// all fields where termvectors with offset and position values set
public static readonly FieldOption TERMVECTOR_WITH_POSITION_OFFSET = new FieldOption("TERMVECTOR_WITH_POSITION_OFFSET");
}
/// <summary> Constructor used if IndexReader is not owner of its directory.
/// This is used for IndexReaders that are used within other IndexReaders that take care or locking directories.
///
/// </summary>
/// <param name="directory">Directory where IndexReader files reside.
/// </param>
protected internal IndexReader(Directory directory)
{
this.directory = directory;
}
/// <summary> Constructor used if IndexReader is owner of its directory.
/// If IndexReader is owner of its directory, it locks its directory in case of write operations.
///
/// </summary>
/// <param name="directory">Directory where IndexReader files reside.
/// </param>
/// <param name="segmentInfos">Used for write-l
/// </param>
/// <param name="">closeDirectory
/// </param>
internal IndexReader(Directory directory, SegmentInfos segmentInfos, bool closeDirectory)
{
Init(directory, segmentInfos, closeDirectory, true);
}
internal virtual void Init(Directory directory, SegmentInfos segmentInfos, bool closeDirectory, bool directoryOwner)
{
this.directory = directory;
this.segmentInfos = segmentInfos;
this.directoryOwner = directoryOwner;
this.closeDirectory = closeDirectory;
}
private Directory directory;
private bool directoryOwner;
private bool closeDirectory;
protected internal IndexFileDeleter deleter;
private SegmentInfos segmentInfos;
private Lock writeLock;
private bool stale;
private bool hasChanges;
/// <summary>Used by commit() to record pre-commit state in case
/// rollback is necessary
/// </summary>
private bool rollbackHasChanges;
private SegmentInfos rollbackSegmentInfos;
/// <summary>Returns an IndexReader reading the index in an FSDirectory in the named
/// path.
/// </summary>
public static IndexReader Open(System.String path)
{
return Open(FSDirectory.GetDirectory(path), true);
}
/// <summary>Returns an IndexReader reading the index in an FSDirectory in the named
/// path.
/// </summary>
public static IndexReader Open(System.IO.FileInfo path)
{
return Open(FSDirectory.GetDirectory(path), true);
}
/// <summary>Returns an IndexReader reading the index in the given Directory. </summary>
public static IndexReader Open(Directory directory)
{
return Open(directory, false);
}
private static IndexReader Open(Directory directory, bool closeDirectory)
{
return (IndexReader) new AnonymousClassFindSegmentsFile(closeDirectory, directory).run();
}
/// <summary>Returns the directory this index resides in. </summary>
public virtual Directory Directory()
{
return directory;
}
/// <summary> 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.
/// </summary>
public static long LastModified(System.String directory)
{
return LastModified(new System.IO.FileInfo(directory));
}
/// <summary> 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.
/// </summary>
public static long LastModified(System.IO.FileInfo fileDirectory)
{
return (long) ((System.Int64) new AnonymousClassFindSegmentsFile1(fileDirectory).run());
}
/// <summary> 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.
/// </summary>
public static long LastModified(Directory directory2)
{
return (long) ((System.Int64) new AnonymousClassFindSegmentsFile2(directory2, directory2).run());
}
/// <summary> 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.
///
/// </summary>
/// <param name="directory">where the index resides.
/// </param>
/// <returns> version number.
/// </returns>
/// <throws> IOException if segments file cannot be read </throws>
public static long GetCurrentVersion(System.String directory)
{
return GetCurrentVersion(new System.IO.FileInfo(directory));
}
/// <summary> 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.
///
/// </summary>
/// <param name="directory">where the index resides.
/// </param>
/// <returns> version number.
/// </returns>
/// <throws> IOException if segments file cannot be read </throws>
public static long GetCurrentVersion(System.IO.FileInfo directory)
{
Directory dir = FSDirectory.GetDirectory(directory);
long version = GetCurrentVersion(dir);
dir.Close();
return version;
}
/// <summary> 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.
///
/// </summary>
/// <param name="directory">where the index resides.
/// </param>
/// <returns> version number.
/// </returns>
/// <throws> IOException if segments file cannot be read. </throws>
public static long GetCurrentVersion(Directory directory)
{
return SegmentInfos.ReadCurrentVersion(directory);
}
/// <summary> Version number when this IndexReader was opened.</summary>
public virtual long GetVersion()
{
return segmentInfos.GetVersion();
}
/// <summary> Check whether this IndexReader still works on a current version of the index.
/// If this is not the case you will need to re-open the IndexReader to
/// make sure you see the latest changes made to the index.
///
/// </summary>
/// <throws> IOException </throws>
public virtual bool IsCurrent()
{
return SegmentInfos.ReadCurrentVersion(directory) == segmentInfos.GetVersion();
}
/// <summary> Checks is the index is optimized (if it has a single segment and no deletions)</summary>
/// <returns> <code>true</code> if the index is optimized; <code>false</code> otherwise
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -