?? fsdirectory.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 IndexFileNameFilter = Lucene.Net.Index.IndexFileNameFilter;
// Used only for WRITE_LOCK_NAME in deprecated create=true case:
using IndexWriter = Lucene.Net.Index.IndexWriter;
namespace Lucene.Net.Store
{
/// <summary> Straightforward implementation of {@link Directory} as a directory of files.
/// Locking implementation is by default the {@link SimpleFSLockFactory}, but
/// can be changed either by passing in a {@link LockFactory} instance to
/// <code>getDirectory</code>, or specifying the LockFactory class by setting
/// <code>Lucene.Net.Store.FSDirectoryLockFactoryClass</code> Java system
/// property, or by calling {@link #setLockFactory} after creating
/// the Directory.
/// <p>Directories are cached, so that, for a given canonical
/// path, the same FSDirectory instance will always be
/// returned by <code>getDirectory</code>. This permits
/// synchronization on directories.</p>
///
/// </summary>
/// <seealso cref="Directory">
/// </seealso>
/// <author> Doug Cutting
/// </author>
public class FSDirectory : Directory
{
/// <summary>This cache of directories ensures that there is a unique Directory
/// instance per path, so that synchronization on the Directory can be used to
/// synchronize access between readers and writers. We use
/// refcounts to ensure when the last use of an FSDirectory
/// instance for a given canonical path is closed, we remove the
/// instance from the cache. See LUCENE-776
/// for some relevant discussion.
/// </summary>
private static readonly System.Collections.Hashtable DIRECTORIES = System.Collections.Hashtable.Synchronized(new System.Collections.Hashtable());
private static bool disableLocks = false;
// TODO: should this move up to the Directory base class? Also: should we
// make a per-instance (in addition to the static "default") version?
/// <summary> Set whether Lucene's use of lock files is disabled. By default,
/// lock files are enabled. They should only be disabled if the index
/// is on a read-only medium like a CD-ROM.
/// </summary>
public static void SetDisableLocks(bool doDisableLocks)
{
FSDirectory.disableLocks = doDisableLocks;
}
/// <summary> Returns whether Lucene's use of lock files is disabled.</summary>
/// <returns> true if locks are disabled, false if locks are enabled.
/// </returns>
public static bool GetDisableLocks()
{
return FSDirectory.disableLocks;
}
/// <summary> Directory specified by <code>Lucene.Net.lockDir</code>
/// or <code>java.io.tmpdir</code> system property.
/// </summary>
/// <deprecated> As of 2.1, <code>LOCK_DIR</code> is unused
/// because the write.lock is now stored by default in the
/// index directory. If you really want to store locks
/// elsewhere you can create your own {@link
/// SimpleFSLockFactory} (or {@link NativeFSLockFactory},
/// etc.) passing in your preferred lock directory. Then,
/// pass this <code>LockFactory</code> instance to one of
/// the <code>getDirectory</code> methods that take a
/// <code>lockFactory</code> (for example, {@link #GetDirectory(String, LockFactory)}).
/// </deprecated>
public static readonly System.String LOCK_DIR = SupportClass.AppSettings.Get("Lucene.Net.lockDir", System.IO.Path.GetTempPath());
/// <summary>The default class which implements filesystem-based directories. </summary>
private static System.Type IMPL;
private static System.Security.Cryptography.MD5 DIGESTER;
/// <summary>A buffer optionally used in renameTo method </summary>
private byte[] buffer = null;
/// <summary>Returns the directory instance for the named location.</summary>
/// <param name="path">the path to the directory.
/// </param>
/// <returns> the FSDirectory for the named file.
/// </returns>
public static FSDirectory GetDirectory(System.String path)
{
return GetDirectory(new System.IO.FileInfo(path), null);
}
/// <summary>Returns the directory instance for the named location.</summary>
/// <param name="path">the path to the directory.
/// </param>
/// <param name="lockFactory">instance of {@link LockFactory} providing the
/// locking implementation.
/// </param>
/// <returns> the FSDirectory for the named file.
/// </returns>
public static FSDirectory GetDirectory(System.String path, LockFactory lockFactory)
{
return GetDirectory(new System.IO.FileInfo(path), lockFactory);
}
/// <summary>Returns the directory instance for the named location.</summary>
/// <param name="file">the path to the directory.
/// </param>
/// <returns> the FSDirectory for the named file.
/// </returns>
public static FSDirectory GetDirectory(System.IO.FileInfo file)
{
return GetDirectory(file, null);
}
/// <summary>Returns the directory instance for the named location.</summary>
/// <param name="file">the path to the directory.
/// </param>
/// <param name="lockFactory">instance of {@link LockFactory} providing the
/// locking implementation.
/// </param>
/// <returns> the FSDirectory for the named file.
/// </returns>
public static FSDirectory GetDirectory(System.IO.FileInfo file, LockFactory lockFactory)
{
file = new System.IO.FileInfo(file.FullName);
bool tmpBool;
if (System.IO.File.Exists(file.FullName))
tmpBool = true;
else
tmpBool = System.IO.Directory.Exists(file.FullName);
if (tmpBool && !System.IO.Directory.Exists(file.FullName))
throw new System.IO.IOException(file + " not a directory");
bool tmpBool2;
if (System.IO.File.Exists(file.FullName))
tmpBool2 = true;
else
tmpBool2 = System.IO.Directory.Exists(file.FullName);
if (!tmpBool2)
{
try
{
System.IO.Directory.CreateDirectory(file.FullName);
}
catch
{
throw new System.IO.IOException("Cannot create directory: " + file);
}
}
FSDirectory dir;
lock (DIRECTORIES.SyncRoot)
{
dir = (FSDirectory) DIRECTORIES[file];
if (dir == null)
{
try
{
dir = (FSDirectory) System.Activator.CreateInstance(IMPL);
}
catch (System.Exception e)
{
throw new System.SystemException("cannot load FSDirectory class: " + e.ToString(), e);
}
dir.Init(file, lockFactory);
DIRECTORIES[file] = dir;
}
else
{
// Catch the case where a Directory is pulled from the cache, but has a
// different LockFactory instance.
if (lockFactory != null && lockFactory != dir.GetLockFactory())
{
throw new System.IO.IOException("Directory was previously created with a different LockFactory instance; please pass null as the lockFactory instance and use setLockFactory to change it");
}
}
}
lock (dir)
{
dir.refCount++;
}
return dir;
}
/// <summary>Returns the directory instance for the named location.
///
/// </summary>
/// <deprecated> Use IndexWriter's create flag, instead, to
/// create a new index.
///
/// </deprecated>
/// <param name="path">the path to the directory.
/// </param>
/// <param name="create">if true, create, or erase any existing contents.
/// </param>
/// <returns> the FSDirectory for the named file.
/// </returns>
public static FSDirectory GetDirectory(System.String path, bool create)
{
return GetDirectory(new System.IO.FileInfo(path), create);
}
/// <summary>Returns the directory instance for the named location.
///
/// </summary>
/// <deprecated> Use IndexWriter's create flag, instead, to
/// create a new index.
///
/// </deprecated>
/// <param name="file">the path to the directory.
/// </param>
/// <param name="create">if true, create, or erase any existing contents.
/// </param>
/// <returns> the FSDirectory for the named file.
/// </returns>
public static FSDirectory GetDirectory(System.IO.FileInfo file, bool create)
{
FSDirectory dir = GetDirectory(file, null);
// This is now deprecated (creation should only be done
// by IndexWriter):
if (create)
{
dir.Create();
}
return dir;
}
private void Create()
{
bool tmpBool;
if (System.IO.File.Exists(directory.FullName))
tmpBool = true;
else
tmpBool = System.IO.Directory.Exists(directory.FullName);
if (tmpBool)
{
System.String[] files = System.IO.Directory.GetFileSystemEntries(directory.FullName); // directory.list(IndexFileNameFilter.GetFilter()); // clear old files // {{Aroush-2.1}} we don't want all files in the directory but a filtered list
if (files == null)
throw new System.IO.IOException("Cannot read directory " + directory.FullName);
for (int i = 0; i < files.Length; i++)
{
System.IO.FileInfo file = new System.IO.FileInfo(System.IO.Path.Combine(directory.FullName, files[i]));
bool tmpBool2;
if (System.IO.File.Exists(file.FullName))
{
System.IO.File.Delete(file.FullName);
tmpBool2 = true;
}
else if (System.IO.Directory.Exists(file.FullName))
{
System.IO.Directory.Delete(file.FullName);
tmpBool2 = true;
}
else
tmpBool2 = false;
if (!tmpBool2)
throw new System.IO.IOException("Cannot delete " + file);
}
}
lockFactory.ClearLock(IndexWriter.WRITE_LOCK_NAME);
}
private System.IO.FileInfo directory = null;
private int refCount;
public FSDirectory()
{
}
// permit subclassing
private void Init(System.IO.FileInfo path, LockFactory lockFactory)
{
// Set up lockFactory with cascaded defaults: if an instance was passed in,
// use that; else if locks are disabled, use NoLockFactory; else if the
// system property Lucene.Net.Store.FSDirectoryLockFactoryClass is set,
// instantiate that; else, use SimpleFSLockFactory:
directory = path;
bool doClearLockID = false;
if (lockFactory == null)
{
if (disableLocks)
{
// Locks are disabled:
lockFactory = NoLockFactory.GetNoLockFactory();
}
else
{
System.String lockClassName = SupportClass.AppSettings.Get("Lucene.Net.Store.FSDirectoryLockFactoryClass", "");
if (lockClassName != null && !lockClassName.Equals(""))
{
System.Type c;
try
{
c = System.Type.GetType(lockClassName);
}
catch (System.Exception)
{
throw new System.IO.IOException("unable to find LockClass " + lockClassName);
}
try
{
lockFactory = (LockFactory) System.Activator.CreateInstance(c);
}
catch (System.UnauthorizedAccessException e)
{
throw new System.IO.IOException("IllegalAccessException when instantiating LockClass " + lockClassName);
}
catch (System.InvalidCastException)
{
throw new System.IO.IOException("unable to cast LockClass " + lockClassName + " instance to a LockFactory");
}
catch (System.Exception)
{
throw new System.IO.IOException("InstantiationException when instantiating LockClass " + lockClassName);
}
}
else
{
// Our default lock is SimpleFSLockFactory;
// default lockDir is our index directory:
lockFactory = new SimpleFSLockFactory(path);
doClearLockID = true;
}
}
}
SetLockFactory(lockFactory);
if (doClearLockID)
{
// Clear the prefix because write.lock will be
// stored in our directory:
lockFactory.SetLockPrefix(null);
}
}
/// <summary>Returns an array of strings, one for each Lucene index file in the directory. </summary>
public override System.String[] List()
{
System.String[] files = System.IO.Directory.GetFileSystemEntries(directory.FullName); // IndexFileNameFilter.GetFilter()); // {{Aroush-2.1}} we want to limit the files to the list
for (int i = 0; i < files.Length; i++)
{
System.IO.FileInfo fi = new System.IO.FileInfo(files[i]);
files[i] = fi.Name;
}
return files;
}
/// <summary>Returns true iff a file with the given name exists. </summary>
public override bool FileExists(System.String name)
{
System.IO.FileInfo file = new System.IO.FileInfo(System.IO.Path.Combine(directory.FullName, name));
bool tmpBool;
if (System.IO.File.Exists(file.FullName))
tmpBool = true;
else
tmpBool = System.IO.Directory.Exists(file.FullName);
return tmpBool;
}
/// <summary>Returns the time the named file was last modified. </summary>
public override long FileModified(System.String name)
{
System.IO.FileInfo file = new System.IO.FileInfo(System.IO.Path.Combine(directory.FullName, name));
return (file.LastWriteTime.Ticks);
}
/// <summary>Returns the time the named file was last modified. </summary>
public static long FileModified(System.IO.FileInfo directory, System.String name)
{
System.IO.FileInfo file = new System.IO.FileInfo(System.IO.Path.Combine(directory.FullName, name));
return (file.LastWriteTime.Ticks);
}
/// <summary>Set the modified time of an existing file to now. </summary>
public override void TouchFile(System.String name)
{
System.IO.FileInfo file = new System.IO.FileInfo(System.IO.Path.Combine(directory.FullName, name));
file.LastWriteTime = System.DateTime.Now;
}
/// <summary>Returns the length in bytes of a file in the directory. </summary>
public override long FileLength(System.String name)
{
System.IO.FileInfo file = new System.IO.FileInfo(System.IO.Path.Combine(directory.FullName, name));
return file.Exists ? file.Length : 0;
}
/// <summary>Removes an existing file in the directory. </summary>
public override void DeleteFile(System.String name)
{
System.IO.FileInfo file = new System.IO.FileInfo(System.IO.Path.Combine(directory.FullName, name));
bool tmpBool;
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -