⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 indexreader.cs

📁 Lucene.Net 版本源码 测试通过
💻 CS
📖 第 1 页 / 共 3 页
字号:
				}
			}
			finally
			{
				docs.Close();
			}
			return n;
		}
		
		/// <summary>Undeletes all documents currently marked as deleted in this index.</summary>
		public void  UndeleteAll()
		{
			lock (this)
			{
				if (directoryOwner)
					AquireWriteLock();
				hasChanges = true;
				DoUndeleteAll();
			}
		}
		
		/// <summary>Implements actual undeleteAll() in subclass. </summary>
		protected internal abstract void  DoUndeleteAll();
		
		/// <summary> Should internally checkpoint state that will change
		/// during commit so that we can rollback if necessary.
		/// </summary>
		internal virtual void  StartCommit()
		{
			if (directoryOwner)
			{
				rollbackSegmentInfos = (SegmentInfos) segmentInfos.Clone();
			}
			rollbackHasChanges = hasChanges;
		}
		
		/// <summary> Rolls back state to just before the commit (this is
		/// called by commit() if there is some exception while
		/// committing).
		/// </summary>
		internal virtual void  RollbackCommit()
		{
			if (directoryOwner)
			{
				for (int i = 0; i < segmentInfos.Count; i++)
				{
					// Rollback each segmentInfo.  Because the
					// SegmentReader holds a reference to the
					// SegmentInfo we can't [easily] just replace
					// segmentInfos, so we reset it in place instead:
					segmentInfos.Info(i).Reset(rollbackSegmentInfos.Info(i));
				}
				rollbackSegmentInfos = null;
			}
			
			hasChanges = rollbackHasChanges;
		}
		
		/// <summary> Commit changes resulting from delete, undeleteAll, or
		/// setNorm operations
		/// 
		/// If an exception is hit, then either no changes or all
		/// changes will have been committed to the index
		/// (transactional semantics).
		/// 
		/// </summary>
		/// <throws>  IOException </throws>
		protected internal void  Commit()
		{
			lock (this)
			{
				if (hasChanges)
				{
					if (deleter == null)
					{
						// In the MultiReader case, we share this deleter
						// across all SegmentReaders:
						SetDeleter(new IndexFileDeleter(segmentInfos, directory));
					}
					if (directoryOwner)
					{
						
						// Should not be necessary: no prior commit should
						// have left pending files, so just defensive:
						deleter.ClearPendingFiles();
						
						System.String oldInfoFileName = segmentInfos.GetCurrentSegmentFileName();
						System.String nextSegmentsFileName = segmentInfos.GetNextSegmentFileName();
						
						// Checkpoint the state we are about to change, in
						// case we have to roll back:
						StartCommit();
						
						bool success = false;
						try
						{
							DoCommit();
							segmentInfos.Write(directory);
							success = true;
						}
						finally
						{
							
							if (!success)
							{
								
								// Rollback changes that were made to
								// SegmentInfos but failed to get [fully]
								// committed.  This way this reader instance
								// remains consistent (matched to what's
								// actually in the index):
								RollbackCommit();
								
								// Erase any pending files that we were going to delete:
								deleter.ClearPendingFiles();
								
								// Remove possibly partially written next
								// segments file:
								deleter.DeleteFile(nextSegmentsFileName);
								
								// Recompute deletable files & remove them (so
								// partially written .del files, etc, are
								// removed):
								deleter.FindDeletableFiles();
								deleter.DeleteFiles();
							}
						}
						
						// Attempt to delete all files we just obsoleted:
						deleter.DeleteFile(oldInfoFileName);
						deleter.CommitPendingFiles();
						
						if (writeLock != null)
						{
							writeLock.Release(); // release write lock
							writeLock = null;
						}
					}
					else
						DoCommit();
				}
				hasChanges = false;
			}
		}
		
		protected internal virtual void  SetDeleter(IndexFileDeleter deleter)
		{
			this.deleter = deleter;
		}
		protected internal virtual IndexFileDeleter GetDeleter()
		{
			return deleter;
		}
		
		/// <summary>Implements commit. </summary>
		protected internal abstract void  DoCommit();
		
		/// <summary> Closes files associated with this index.
		/// Also saves any new deletions to disk.
		/// No other methods should be called after this has been called.
		/// </summary>
		public void  Close()
		{
			lock (this)
			{
				Commit();
				DoClose();
				if (closeDirectory)
					directory.Close();
			}
		}
		
		/// <summary>Implements close. </summary>
		protected internal abstract void  DoClose();
		
		/// <summary>Release the write lock, if needed. </summary>
		~IndexReader()
		{
			try
			{
				if (writeLock != null)
				{
					writeLock.Release(); // release write lock
					writeLock = null;
				}
			}
			finally
			{
			}
		}
		
		
		/// <summary> Get a list of unique field names that exist in this index and have the specified
		/// field option information.
		/// </summary>
		/// <param name="fldOption">specifies which field option should be available for the returned fields
		/// </param>
		/// <returns> Collection of Strings indicating the names of the fields.
		/// </returns>
		/// <seealso cref="IndexReader.FieldOption">
		/// </seealso>
		public abstract System.Collections.ICollection GetFieldNames(FieldOption fldOption);
		
		/// <summary> Returns <code>true</code> iff the index in the named directory is
		/// currently locked.
		/// </summary>
		/// <param name="directory">the directory to check for a lock
		/// </param>
		/// <throws>  IOException if there is a problem with accessing the index </throws>
		public static bool IsLocked(Directory directory)
		{
			return directory.MakeLock(IndexWriter.WRITE_LOCK_NAME).IsLocked();
		}
		
		/// <summary> Returns <code>true</code> iff the index in the named directory is
		/// currently locked.
		/// </summary>
		/// <param name="directory">the directory to check for a lock
		/// </param>
		/// <throws>  IOException if there is a problem with accessing the index </throws>
		public static bool IsLocked(System.String directory)
		{
			Directory dir = FSDirectory.GetDirectory(directory);
			bool result = IsLocked(dir);
			dir.Close();
			return result;
		}
		
		/// <summary> Forcibly unlocks the index in the named directory.
		/// <P>
		/// Caution: this should only be used by failure recovery code,
		/// when it is known that no other process nor thread is in fact
		/// currently accessing this index.
		/// </summary>
		public static void  Unlock(Directory directory)
		{
			directory.MakeLock(IndexWriter.WRITE_LOCK_NAME).Release();
		}
		
		/// <summary> Prints the filename and size of each file within a given compound file.
		/// Add the -extract flag to extract files to the current working directory.
		/// In order to make the extracted version of the index work, you have to copy
		/// the segments file from the compound index into the directory where the extracted files are stored.
		/// </summary>
		/// <param name="args">Usage: Lucene.Net.Index.IndexReader [-extract] &lt;cfsfile&gt;
		/// </param>
		[STAThread]
		public static void  Main(System.String[] args)
		{
			System.String filename = null;
			bool extract = false;
			
			for (int i = 0; i < args.Length; ++i)
			{
				if (args[i].Equals("-extract"))
				{
					extract = true;
				}
				else if (filename == null)
				{
					filename = args[i];
				}
			}
			
			if (filename == null)
			{
				System.Console.Out.WriteLine("Usage: Lucene.Net.Index.IndexReader [-extract] <cfsfile>");
				return ;
			}
			
			Directory dir = null;
			CompoundFileReader cfr = null;
			
			try
			{
				System.IO.FileInfo file = new System.IO.FileInfo(filename);
				System.String dirname = new System.IO.FileInfo(file.FullName).DirectoryName;
				filename = file.Name;
				dir = FSDirectory.GetDirectory(dirname);
				cfr = new CompoundFileReader(dir, filename);
				
				System.String[] files = cfr.List();
				System.Array.Sort(files); // sort the array of filename so that the output is more readable
				
				for (int i = 0; i < files.Length; ++i)
				{
					long len = cfr.FileLength(files[i]);
					
					if (extract)
					{
						System.Console.Out.WriteLine("extract " + files[i] + " with " + len + " bytes to local directory...");
						IndexInput ii = cfr.OpenInput(files[i]);
						
						System.IO.FileStream f = new System.IO.FileStream(files[i], System.IO.FileMode.Create);
						
						// read and write with a small buffer, which is more effectiv than reading byte by byte
						byte[] buffer = new byte[1024];
						int chunk = buffer.Length;
						while (len > 0)
						{
							int bufLen = (int) System.Math.Min(chunk, len);
							ii.ReadBytes(buffer, 0, bufLen);

							byte[] byteArray = new byte[buffer.Length];
							for (int index=0; index < buffer.Length; index++)
								byteArray[index] = (byte) buffer[index];

							f.Write(byteArray, 0, bufLen);

							len -= bufLen;
						}
						
						f.Close();
						ii.Close();
					}
					else
						System.Console.Out.WriteLine(files[i] + ": " + len + " bytes");
				}
			}
			catch (System.IO.IOException ioe)
			{
				System.Console.Error.WriteLine(ioe.StackTrace);
			}
			finally
			{
				try
				{
					if (dir != null)
						dir.Close();
					if (cfr != null)
						cfr.Close();
				}
				catch (System.IO.IOException ioe)
				{
					System.Console.Error.WriteLine(ioe.StackTrace);
				}
			}
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -