1 package org.apache.lucene.store;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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
21 import java.io.IOException;
22 import org.apache.lucene.store.Directory; // javadoc
23 import org.apache.lucene.store.NativeFSLockFactory; // javadoc
26 * Native {@link Directory} implementation for Microsoft Windows.
30 * <li>Compile the source code to create WindowsDirectory.dll:
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
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.
40 * @lucene.experimental
42 public class WindowsDirectory extends FSDirectory {
43 private static final int DEFAULT_BUFFERSIZE = 4096; /* default pgsize on ia32/amd64 */
46 System.loadLibrary("WindowsDirectory");
49 /** Create a new WindowsDirectory for the named location.
51 * @param path the path of the directory
52 * @param lockFactory the lock factory to use, or null for the default
53 * ({@link NativeFSLockFactory});
56 public WindowsDirectory(File path, LockFactory lockFactory) throws IOException {
57 super(path, lockFactory);
60 /** Create a new WindowsDirectory for the named location and {@link NativeFSLockFactory}.
62 * @param path the path of the directory
65 public WindowsDirectory(File path) throws IOException {
70 public IndexInput openInput(String name, int bufferSize) throws IOException {
72 return new WindowsIndexInput(new File(getDirectory(), name), Math.max(bufferSize, DEFAULT_BUFFERSIZE));
75 protected static class WindowsIndexInput extends BufferedIndexInput {
76 private final long fd;
77 private final long length;
81 public WindowsIndexInput(File file, int bufferSize) throws IOException {
83 fd = WindowsDirectory.open(file.getPath());
84 length = WindowsDirectory.length(fd);
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");
95 protected void seekInternal(long pos) throws IOException {
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);
108 public long length() {
113 public Object clone() {
114 WindowsIndexInput clone = (WindowsIndexInput)super.clone();
115 clone.isClone = true;
120 /** Opens a handle to a file. */
121 private static native long open(String filename) throws IOException;
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;
126 /** Closes a handle to a file */
127 private static native void close(long fd) throws IOException;
129 /** Returns the length of a file */
130 private static native long length(long fd) throws IOException;