add --shared
[pylucene.git] / lucene-java-3.4.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.File;
21 import java.io.IOException;
22 import org.apache.lucene.store.Directory; // javadoc
23 import org.apache.lucene.store.NativeFSLockFactory; // javadoc
24
25 /**
26  * Native {@link Directory} implementation for Microsoft Windows.
27  * <p>
28  * Steps:
29  * <ol> 
30  *   <li>Compile the source code to create WindowsDirectory.dll:
31  *       <blockquote>
32  * c:\mingw\bin\g++ -Wall -D_JNI_IMPLEMENTATION_ -Wl,--kill-at 
33  * -I"%JAVA_HOME%\include" -I"%JAVA_HOME%\include\win32" -static-libgcc 
34  * -static-libstdc++ -shared WindowsDirectory.cpp -o WindowsDirectory.dll
35  *       </blockquote> 
36  *       For 64-bit JREs, use mingw64, with the -m64 option. 
37  *   <li>Put WindowsDirectory.dll into some directory in your windows PATH
38  *   <li>Open indexes with WindowsDirectory and use it.
39  * </p>
40  * @lucene.experimental
41  */
42 public class WindowsDirectory extends FSDirectory {
43   private static final int DEFAULT_BUFFERSIZE = 4096; /* default pgsize on ia32/amd64 */
44   
45   static {
46     System.loadLibrary("WindowsDirectory");
47   }
48   
49   /** Create a new WindowsDirectory for the named location.
50    * 
51    * @param path the path of the directory
52    * @param lockFactory the lock factory to use, or null for the default
53    * ({@link NativeFSLockFactory});
54    * @throws IOException
55    */
56   public WindowsDirectory(File path, LockFactory lockFactory) throws IOException {
57     super(path, lockFactory);
58   }
59
60   /** Create a new WindowsDirectory for the named location and {@link NativeFSLockFactory}.
61    *
62    * @param path the path of the directory
63    * @throws IOException
64    */
65   public WindowsDirectory(File path) throws IOException {
66     super(path, null);
67   }
68
69   @Override
70   public IndexInput openInput(String name, int bufferSize) throws IOException {
71     ensureOpen();
72     return new WindowsIndexInput(new File(getDirectory(), name), Math.max(bufferSize, DEFAULT_BUFFERSIZE));
73   }
74   
75   protected static class WindowsIndexInput extends BufferedIndexInput {
76     private final long fd;
77     private final long length;
78     boolean isClone;
79     boolean isOpen;
80     
81     public WindowsIndexInput(File file, int bufferSize) throws IOException {
82       super(bufferSize);
83       fd = WindowsDirectory.open(file.getPath());
84       length = WindowsDirectory.length(fd);
85       isOpen = true;
86     }
87     
88     @Override
89     protected void readInternal(byte[] b, int offset, int length) throws IOException {
90       if (WindowsDirectory.read(fd, b, offset, length, getFilePointer()) != length)
91         throw new IOException("Read past EOF");
92     }
93
94     @Override
95     protected void seekInternal(long pos) throws IOException {
96     }
97
98     @Override
99     public synchronized void close() throws IOException {
100       // NOTE: we synchronize and track "isOpen" because Lucene sometimes closes IIs twice!
101       if (!isClone && isOpen) {
102         WindowsDirectory.close(fd);
103         isOpen = false;
104       }
105     }
106
107     @Override
108     public long length() {
109       return length;
110     }
111     
112     @Override
113     public Object clone() {
114       WindowsIndexInput clone = (WindowsIndexInput)super.clone();
115       clone.isClone = true;
116       return clone;
117     }
118   }
119   
120   /** Opens a handle to a file. */
121   private static native long open(String filename) throws IOException;
122   
123   /** Reads data from a file at pos into bytes */
124   private static native int read(long fd, byte bytes[], int offset, int length, long pos) throws IOException;
125   
126   /** Closes a handle to a file */
127   private static native void close(long fd) throws IOException;
128   
129   /** Returns the length of a file */
130   private static native long length(long fd) throws IOException;
131 }