Directory overall process is still relatively simple, picture borrowing
Directory directory = fsdirectory.open (file);
/**creates an Fsdirectory instance, trying to pick the "best implementation given" the current environment. * The directory returned uses the {@linkNativefslockfactory}. * * <p>currently This returns {@linkMmapdirectory} for most Solaris * and Windows 64-bit JREs, {@linkNiofsdirectory} for other * Non-windows JREs, and {@linkSimplefsdirectory} for other * JREs on Windows. It is highly recommended so consult the * implementation ' s documentation for your platform before * using this Method. * * <P><B>NOTE</B>: This method may suddenly change which * implementation are returned from release to release, in * the event that higher performance defaults become * possible; If the precise implementation is important to * your application, please instantiate it directly, * instead. For optimal performance should consider using * {@linkMmapdirectory} on the JVMs bit. * * <p>see <a href= "#subclasses" >above</a>*/ Public StaticFsdirectory Open (File path)throwsIOException {returnOpen (Path,NULL);//Use default lock factory}/**Just like {@link#open (File)}, but allows your to * also specify a custom {@linkLockfactory}. */ Public StaticFsdirectory Open (File path, lockfactory lockfactory)throwsIOException {if((Constants.windows | | Constants.sun_os | |constants.linux)&& Constants.jre_is_64bit &&mmapdirectory.unmap_supported) { return Newmmapdirectory (path, lockfactory); } Else if(constants.windows) {return Newsimplefsdirectory (path, lockfactory); } Else { return Newniofsdirectory (path, lockfactory); } }
Taking Niofsdirectory as an example
/** Create a new niofsdirectory for the named Location. * * @param path the path of the directory * @param Lockfactory the lock factory to use , or NULL for the default * ({ @link @throws IOException if there is a Low-level I/O error */ public Niofsdirectory (File path, lockfactory lockfactory) throws IOException { super (path, lockfactory) ; }
/**Create A new fsdirectory for the named location (ctor for subclasses). * @parampath The path of the directory *@paramLockfactory the Lock factory to use, or NULL for the default * ({@linknativefslockfactory}); * @throwsIOException If there is a low-level I/O error*/ protectedFsdirectory (File path, lockfactory lockfactory)throwsIOException {//new ctors use always nativefslockfactory as default: if(Lockfactory = =NULL) {lockfactory=Newnativefslockfactory (); } directory=Getcanonicalpath (path);//Get Path absolute pathif(Directory.Exists () &&!directory.isdirectory ())Throw NewNosuchdirectoryexception ("file" + directory + "' exists but isn't a directory"); Setlockfactory (lockfactory);//Set Lock Factory}
/** * Create A nativefslockfactory instance, with null (unset) * Lock directory. When your pass this factory to a {@link fsdirectory} * Subclass, the lock directory is automatically Set to the * directory itself. Be sure to create one instance for each directory * your create! */ Public nativefslockfactory () { thisnull);//Factory not set path }
@Override Public voidSetlockfactory (Lockfactory lockfactory)throwsIOException {Super. Setlockfactory (lockfactory); //for filesystem based Lockfactory, delete the Lockprefix, if the locks is placed//In index dir. If No index dir is given, set ourselves if(lockfactoryinstanceoffslockfactory) { FinalFslockfactory LF =(fslockfactory) lockfactory; FinalFile dir =Lf.getlockdir ();//Because (File) null, so dir is null//If the lock factory have no Lockdir set, use the This directory as Lockdir if(dir = =NULL) {lf.setlockdir (directory);//Use the directory lf.setlockprefix that we set up (NULL); } Else if(Dir.getcanonicalpath (). Equals (Directory.getcanonicalpath ())) {Lf.setlockprefix (NULL); } } }
The entire open process is in fact setlockdir () in a simple positioning lockfactory, and set lockid
BaseDirectory
@Override Public String toString () { return'@'" lockfactory= " + getlockfactory (); }
Index establishment process of lucene4.7 source Research (1)