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
20 import java.io.EOFException;
22 import java.io.IOException;
23 import java.nio.ByteBuffer;
24 import java.nio.channels.ClosedChannelException; // javadoc
25 import java.nio.channels.FileChannel;
26 import java.util.concurrent.Future; // javadoc
29 * An {@link FSDirectory} implementation that uses java.nio's FileChannel's
30 * positional read, which allows multiple threads to read from the same file
31 * without synchronizing.
33 * This class only uses FileChannel when reading; writing is achieved with
34 * {@link FSDirectory.FSIndexOutput}.
36 * <b>NOTE</b>: NIOFSDirectory is not recommended on Windows because of a bug in
37 * how FileChannel.read is implemented in Sun's JRE. Inside of the
38 * implementation the position is apparently synchronized. See <a
39 * href="http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6265734">here</a>
43 * <font color="red"><b>NOTE:</b> Accessing this class either directly or
44 * indirectly from a thread while it's interrupted can close the
45 * underlying file descriptor immediately if at the same time the thread is
46 * blocked on IO. The file descriptor will remain closed and subsequent access
47 * to {@link NIOFSDirectory} will throw a {@link ClosedChannelException}. If
48 * your application uses either {@link Thread#interrupt()} or
49 * {@link Future#cancel(boolean)} you should use {@link SimpleFSDirectory} in
50 * favor of {@link NIOFSDirectory}.</font>
53 public class NIOFSDirectory extends FSDirectory {
55 /** Create a new NIOFSDirectory for the named location.
57 * @param path the path of the directory
58 * @param lockFactory the lock factory to use, or null for the default
59 * ({@link NativeFSLockFactory});
62 public NIOFSDirectory(File path, LockFactory lockFactory) throws IOException {
63 super(path, lockFactory);
66 /** Create a new NIOFSDirectory for the named location and {@link NativeFSLockFactory}.
68 * @param path the path of the directory
71 public NIOFSDirectory(File path) throws IOException {
75 /** Creates an IndexInput for the file with the given name. */
77 public IndexInput openInput(String name, int bufferSize) throws IOException {
79 return new NIOFSIndexInput(new File(getDirectory(), name), bufferSize, getReadChunkSize());
82 protected static class NIOFSIndexInput extends SimpleFSDirectory.SimpleFSIndexInput {
84 private ByteBuffer byteBuf; // wraps the buffer for NIO
86 private byte[] otherBuffer;
87 private ByteBuffer otherByteBuf;
89 final FileChannel channel;
91 public NIOFSIndexInput(File path, int bufferSize, int chunkSize) throws IOException {
92 super("NIOFSIndexInput(path=\"" + path + "\")", path, bufferSize, chunkSize);
93 channel = file.getChannel();
97 protected void newBuffer(byte[] newBuffer) {
98 super.newBuffer(newBuffer);
99 byteBuf = ByteBuffer.wrap(newBuffer);
103 public void close() throws IOException {
104 if (!isClone && file.isOpen) {
105 // Close the channel & file
115 protected void readInternal(byte[] b, int offset, int len) throws IOException {
119 // Determine the ByteBuffer we should use
120 if (b == buffer && 0 == offset) {
121 // Use our own pre-wrapped byteBuf:
122 assert byteBuf != null;
128 if (otherBuffer != b) {
129 // Now wrap this other buffer; with compound
130 // file, we are repeatedly called with its
131 // buffer, so we wrap it once and then re-use it
132 // on subsequent calls
134 otherByteBuf = ByteBuffer.wrap(b);
136 otherByteBuf.clear();
137 otherByteBuf.limit(len);
140 // Always wrap when offset != 0
141 bb = ByteBuffer.wrap(b, offset, len);
145 int readOffset = bb.position();
146 int readLength = bb.limit() - readOffset;
147 assert readLength == len;
149 long pos = getFilePointer();
152 while (readLength > 0) {
154 if (readLength > chunkSize) {
155 // LUCENE-1566 - work around JVM Bug by breaking
156 // very large reads into chunks
157 limit = readOffset + chunkSize;
159 limit = readOffset + readLength;
162 int i = channel.read(bb, pos);
164 throw new EOFException("read past EOF (resource: " + this + ")");
170 } catch (OutOfMemoryError e) {
171 // propagate OOM up and add a hint for 32bit VM Users hitting the bug
172 // with a large chunk size in the fast path.
173 final OutOfMemoryError outOfMemoryError = new OutOfMemoryError(
174 "OutOfMemoryError likely caused by the Sun VM Bug described in "
175 + "https://issues.apache.org/jira/browse/LUCENE-1566; try calling FSDirectory.setReadChunkSize "
176 + "with a value smaller than the current chunk size (" + chunkSize + ")");
177 outOfMemoryError.initCause(e);
178 throw outOfMemoryError;
179 } catch (IOException ioe) {
180 IOException newIOE = new IOException(ioe.getMessage() + ": " + this);
181 newIOE.initCause(ioe);