pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / misc / src / java / org / apache / lucene / store / WindowsDirectory.java
1 package org.apache.lucene.store;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements. See the NOTICE file distributed with this
6  * work for additional information regarding copyright ownership. The ASF
7  * licenses this file to You under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  * http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16  * License for the specific language governing permissions and limitations under
17  * the License.
18  */
19
20 import java.io.EOFException;
21 import java.io.File;
22 import java.io.IOException;
23
24 import org.apache.lucene.store.Directory; // javadoc
25 import org.apache.lucene.store.NativeFSLockFactory; // javadoc
26
27 /**
28  * Native {@link Directory} implementation for Microsoft Windows.
29  * <p>
30  * Steps:
31  * <ol> 
32  *   <li>Compile the source code to create WindowsDirectory.dll:
33  *       <blockquote>
34  * c:\mingw\bin\g++ -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at 
35  * -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -static-libgcc 
36  * -static-libstdc++ -shared WindowsDirectory.cpp -o WindowsDirectory.dll
37  *       </blockquote> 
38  *       For 64-bit JREs, use mingw64, with the -m64 option. 
39  *   <li>Put WindowsDirectory.dll into some directory in your windows PATH
40  *   <li>Open indexes with WindowsDirectory and use it.
41  * </p>
42  * @lucene.experimental
43  */
44 public class WindowsDirectory extends FSDirectory {
45   private static final int DEFAULT_BUFFERSIZE = 4096; /* default pgsize on ia32/amd64 */
46   
47   static {
48     System.loadLibrary("WindowsDirectory");
49   }
50   
51   /** Create a new WindowsDirectory for the named location.
52    * 
53    * @param path the path of the directory
54    * @param lockFactory the lock factory to use, or null for the default
55    * ({@link NativeFSLockFactory});
56    * @throws IOException
57    */
58   public WindowsDirectory(File path, LockFactory lockFactory) throws IOException {
59     super(path, lockFactory);
60   }
61
62   /** Create a new WindowsDirectory for the named location and {@link NativeFSLockFactory}.
63    *
64    * @param path the path of the directory
65    * @throws IOException
66    */
67   public WindowsDirectory(File path) throws IOException {
68     super(path, null);
69   }
70
71   @Override
72   public IndexInput openInput(String name, int bufferSize) throws IOException {
73     ensureOpen();
74     return new WindowsIndexInput(new File(getDirectory(), name), Math.max(bufferSize, DEFAULT_BUFFERSIZE));
75   }
76   
77   protected static class WindowsIndexInput extends BufferedIndexInput {
78     private final long fd;
79     private final long length;
80     boolean isClone;
81     boolean isOpen;
82     
83     public WindowsIndexInput(File file, int bufferSize) throws IOException {
84       super("WindowsIndexInput(path=\"" + file.getPath() + "\")", bufferSize);
85       fd = WindowsDirectory.open(file.getPath());
86       length = WindowsDirectory.length(fd);
87       isOpen = true;
88     }
89     
90     @Override
91     protected void readInternal(byte[] b, int offset, int length) throws IOException {
92       int bytesRead;
93       try {
94         bytesRead = WindowsDirectory.read(fd, b, offset, length, getFilePointer());
95       } catch (IOException ioe) {
96         IOException newIOE = new IOException(ioe.getMessage() + ": " + this);
97         newIOE.initCause(ioe);
98         throw newIOE;
99       }
100
101       if (bytesRead != length) {
102         throw new EOFException("Read past EOF (resource: " + this + ")");
103       }
104     }
105
106     @Override
107     protected void seekInternal(long pos) throws IOException {
108     }
109
110     @Override
111     public synchronized void close() throws IOException {
112       // NOTE: we synchronize and track "isOpen" because Lucene sometimes closes IIs twice!
113       if (!isClone && isOpen) {
114         WindowsDirectory.close(fd);
115         isOpen = false;
116       }
117     }
118
119     @Override
120     public long length() {
121       return length;
122     }
123     
124     @Override
125     public Object clone() {
126       WindowsIndexInput clone = (WindowsIndexInput)super.clone();
127       clone.isClone = true;
128       return clone;
129     }
130   }
131   
132   /** Opens a handle to a file. */
133   private static native long open(String filename) throws IOException;
134   
135   /** Reads data from a file at pos into bytes */
136   private static native int read(long fd, byte bytes[], int offset, int length, long pos) throws IOException;
137   
138   /** Closes a handle to a file */
139   private static native void close(long fd) throws IOException;
140   
141   /** Returns the length of a file */
142   private static native long length(long fd) throws IOException;
143 }