lucene-core-2.9.4-dev

  private byte[] copyBuffer;
  
  public void copyBytes(IndexInput input, long numBytes)
    throws IOException
  {
    assert (numBytes >= 0L) : ("numBytes=" + numBytes);
    long left = numBytes;
    if (copyBuffer == null) {
      copyBuffer = new byte[COPY_BUFFER_SIZE];
    }
    while (left > 0L)
    {
      int toCopy;
      int toCopy;
      if (left > COPY_BUFFER_SIZE) {
        toCopy = COPY_BUFFER_SIZE;
      } else {
        toCopy = (int)left;
      }
      input.readBytes(copyBuffer, 0, toCopy);
      writeBytes(copyBuffer, 0, toCopy);
      left -= toCopy;
    }
  }
  
  public abstract void flush()
    throws IOException;
  
  public abstract void close()
    throws IOException;
  
  public abstract long getFilePointer();
  
  public abstract void seek(long paramLong)
    throws IOException;
  
  public abstract long length()
    throws IOException;
  
  public void setLength(long length)
    throws IOException
  {}
  
  public void writeStringStringMap(Map map)
    throws IOException
  {
    if (map == null)
    {
      writeInt(0);
    }
    else
    {
      writeInt(map.size());
      Iterator it = map.entrySet().iterator();
      while (it.hasNext())
      {
        Map.Entry entry = (Map.Entry)it.next();
        writeString((String)entry.getKey());
        writeString((String)entry.getValue());
      }
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.lucene.store.IndexOutput
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.store;

import java.io.IOException;

public abstract class Lock$With
{
  private Lock lock;
  private long lockWaitTimeout;
  
  public Lock$With(Lock lock, long lockWaitTimeout)
  {
    this.lock = lock;
    this.lockWaitTimeout = lockWaitTimeout;
  }
  
  protected abstract Object doBody()
    throws IOException;
  
  public Object run()
    throws LockObtainFailedException, IOException
  {
    boolean locked = false;
    try
    {
      locked = lock.obtain(lockWaitTimeout);
      return doBody();
    }
    finally
    {
      if (locked) {
        lock.release();
      }
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.lucene.store.Lock.With
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.store;

import java.io.IOException;

public abstract class Lock
{
  public static long LOCK_POLL_INTERVAL = 1000L;
  public static final long LOCK_OBTAIN_WAIT_FOREVER = -1L;
  protected Throwable failureReason;
  
  public abstract boolean obtain()
    throws IOException;
  
  public boolean obtain(long lockWaitTimeout)
    throws LockObtainFailedException, IOException
  {
    failureReason = null;
    boolean locked = obtain();
    if ((lockWaitTimeout < 0L) && (lockWaitTimeout != -1L)) {
      throw new IllegalArgumentException("lockWaitTimeout should be LOCK_OBTAIN_WAIT_FOREVER or a non-negative number (got " + lockWaitTimeout + ")");
    }
    long maxSleepCount = lockWaitTimeout / LOCK_POLL_INTERVAL;
    long sleepCount = 0L;
    while (!locked)
    {
      if ((lockWaitTimeout != -1L) && (sleepCount++ >= maxSleepCount))
      {
        String reason = "Lock obtain timed out: " + toString();
        if (failureReason != null) {
          reason = reason + ": " + failureReason;
        }
        LockObtainFailedException e = new LockObtainFailedException(reason);
        if (failureReason != null) {
          e.initCause(failureReason);
        }
        throw e;
      }
      try
      {
        Thread.sleep(LOCK_POLL_INTERVAL);
      }
      catch (InterruptedException e)
      {
        Thread.currentThread().interrupt();
        throw new IOException(e.toString());
      }
      locked = obtain();
    }
    return locked;
  }
  
  public abstract void release()
    throws IOException;
  
  public abstract boolean isLocked();
  
  public static abstract class With
  {
    private Lock lock;
    private long lockWaitTimeout;
    
    public With(Lock lock, long lockWaitTimeout)
    {
      this.lock = lock;
      this.lockWaitTimeout = lockWaitTimeout;
    }
    
    protected abstract Object doBody()
      throws IOException;
    
    public Object run()
      throws LockObtainFailedException, IOException
    {
      boolean locked = false;
      try
      {
        locked = lock.obtain(lockWaitTimeout);
        return doBody();
      }
      finally
      {
        if (locked) {
          lock.release();
        }
      }
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.lucene.store.Lock
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.store;

import java.io.IOException;

public abstract class LockFactory
{
  protected String lockPrefix = null;
  
  public void setLockPrefix(String lockPrefix)
  {
    this.lockPrefix = lockPrefix;
  }
  
  public String getLockPrefix()
  {
    return lockPrefix;
  }
  
  public abstract Lock makeLock(String paramString);
  
  public abstract void clearLock(String paramString)
    throws IOException;
}

/* Location:
 * Qualified Name:     org.apache.lucene.store.LockFactory
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.store;

import java.io.IOException;

public class LockObtainFailedException
  extends IOException
{
  public LockObtainFailedException(String message)
  {
    super(message);
  }
}

/* Location:
 * Qualified Name:     org.apache.lucene.store.LockObtainFailedException
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.store;

import java.io.IOException;

public class LockReleaseFailedException
  extends IOException
{
  public LockReleaseFailedException(String message)
  {
    super(message);
  }
}

/* Location:
 * Qualified Name:     org.apache.lucene.store.LockReleaseFailedException
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.store;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;

public class LockStressTest
{
  public static void main(String[] args)
    throws Exception
  {
    if (args.length != 6)
    {
      System.out.println("\nUsage: java org.apache.lucene.store.LockStressTest myID verifierHostOrIP verifierPort lockFactoryClassName lockDirName sleepTime\n\n  myID = int from 0 .. 255 (should be unique for test process)\n  verifierHostOrIP = host name or IP address where LockVerifyServer is running\n  verifierPort = port that LockVerifyServer is listening on\n  lockFactoryClassName = primary LockFactory class that we will use\n  lockDirName = path to the lock directory (only set for Simple/NativeFSLockFactory\n  sleepTimeMS = milliseconds to pause betweeen each lock obtain/release\n\nYou should run multiple instances of this process, each with its own\nunique ID, and each pointing to the same lock directory, to verify\nthat locking is working correctly.\n\nMake sure you are first running LockVerifyServer.\n\n");
      
      System.exit(1);
    }
    int myID = Integer.parseInt(args[0]);
    if ((myID < 0) || (myID > 255))
    {
      System.out.println("myID must be a unique int 0..255");
      System.exit(1);
    }
    String verifierHost = args[1];
    int verifierPort = Integer.parseInt(args[2]);
    String lockFactoryClassName = args[3];
    String lockDirName = args[4];
    int sleepTimeMS = Integer.parseInt(args[5]);
    Class c;
    try
    {
      c = Class.forName(lockFactoryClassName);
    }
    catch (ClassNotFoundException e)
    {
      throw new IOException("unable to find LockClass " + lockFactoryClassName);
    }
    LockFactory lockFactory;
    try
    {
      lockFactory = (LockFactory)c.newInstance();
    }
    catch (IllegalAccessException e)
    {
      throw new IOException("IllegalAccessException when instantiating LockClass " + lockFactoryClassName);
    }
    catch (InstantiationException e)
    {
      throw new IOException("InstantiationException when instantiating LockClass " + lockFactoryClassName);
    }
    catch (ClassCastException e)
    {
      throw new IOException("unable to cast LockClass " + lockFactoryClassName + " instance to a LockFactory");
    }
    File lockDir = new File(lockDirName);
    if ((lockFactory instanceof NativeFSLockFactory)) {
      ((NativeFSLockFactory)lockFactory).setLockDir(lockDir);
    } else if ((lockFactory instanceof SimpleFSLockFactory)) {
      ((SimpleFSLockFactory)lockFactory).setLockDir(lockDir);
    }
    lockFactory.setLockPrefix("test");
    
    LockFactory verifyLF = new VerifyingLockFactory((byte)myID, lockFactory, verifierHost, verifierPort);
    
    Lock l = verifyLF.makeLock("test.lock");
    for (;;)
    {
      boolean obtained = false;
      try
      {
        obtained = l.obtain(10L);
      }
      catch (LockObtainFailedException e)
      {
        System.out.print("x");
      }
      if (obtained)
      {
        System.out.print("l");
        l.release();
      }
      Thread.sleep(sleepTimeMS);
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.lucene.store.LockStressTest
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.store;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;

public class LockVerifyServer
{
  private static String getTime(long startTime)
  {
    return "[" + (System.currentTimeMillis() - startTime) / 1000L + "s] ";
  }
  
  public static void main(String[] args)
    throws IOException
  {
    if (args.length != 1)
    {
      System.out.println("\nUsage: java org.apache.lucene.store.LockVerifyServer port\n");
      System.exit(1);
    }
    int port = Integer.parseInt(args[0]);
    
    ServerSocket s = new ServerSocket(port);
    s.setReuseAddress(true);
    System.out.println("\nReady on port " + port + "...");
    
    int lockedID = 0;
    long startTime = System.currentTimeMillis();
    for (;;)
    {
      Socket cs = s.accept();
      OutputStream out = cs.getOutputStream();
      InputStream in = cs.getInputStream();
      
      int id = in.read();
      int command = in.read();
      
      boolean err = false;
      if (command == 1)
      {
        if (lockedID != 0)
        {
          err = true;
          System.out.println(getTime(startTime) + " ERROR: id " + id + " got lock, but " + lockedID + " already holds the lock");
        }
        lockedID = id;
      }
      else if (command == 0)
      {
        if (lockedID != id)
        {
          err = true;
          System.out.println(getTime(startTime) + " ERROR: id " + id + " released the lock, but " + lockedID + " is the one holding the lock");
        }
        lockedID = 0;
      }
      else
      {
        throw new RuntimeException("unrecognized command " + command);
      }
      System.out.print(".");
      if (err) {
        out.write(1);
      } else {
        out.write(0);
      }
      out.close();
      in.close();
      cs.close();
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.lucene.store.LockVerifyServer
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.store;

import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.security.PrivilegedExceptionAction;

class MMapDirectory$1
  implements PrivilegedExceptionAction
{
  private final MMapDirectory this$0;
  
  MMapDirectory$1(MMapDirectory paramMMapDirectory, ByteBuffer paramByteBuffer) {}
  
  public Object run()
    throws Exception
  {
    Method getCleanerMethod = val$buffer.getClass().getMethod("cleaner", MMapDirectory.NO_PARAM_TYPES);
    
    getCleanerMethod.setAccessible(true);
    Object cleaner = getCleanerMethod.invoke(val$buffer, MMapDirectory.NO_PARAMS);
    if (cleaner != null) {
      cleaner.getClass().getMethod("clean", MMapDirectory.NO_PARAM_TYPES).invoke(cleaner, MMapDirectory.NO_PARAMS);
    }
    return null;
  }
}

/* Location:
 * Qualified Name:     org.apache.lucene.store.MMapDirectory.1
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.store;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;

class MMapDirectory$MMapIndexInput
  extends IndexInput
{
  private ByteBuffer buffer;
  private final long length;
  
  MMapDirectory$MMapIndexInput(MMapDirectory x0, RandomAccessFile x1, MMapDirectory.1 x2)
    throws IOException
  {
    this(x0, x1);
  }
  
  private boolean isClone = false;
  private final MMapDirectory this$0;
  
  private MMapDirectory$MMapIndexInput(MMapDirectory paramMMapDirectory, RandomAccessFile raf)
    throws IOException
  {
    length = raf.length();
    buffer = raf.getChannel().map(FileChannel.MapMode.READ_ONLY, 0L, length);
  }
  
  public byte readByte()
    throws IOException
  {
    try
    {
      return buffer.get();
    }
    catch (BufferUnderflowException e)
    {
      throw new IOException("read past EOF");
    }
  }
  
  public void readBytes(byte[] b, int offset, int len)
    throws IOException
  {
    try
    {
      buffer.get(b, offset, len);
    }
    catch (BufferUnderflowException e)
    {
      throw new IOException("read past EOF");
    }
  }
  
  public long getFilePointer()
  {
    return buffer.position();
  }
  
  public void seek(long pos)
    throws IOException
  {
    buffer.position((int)pos);
  }
  
  public long length()
  {
    return length;
  }
  
  public Object clone()
  {
    if (buffer == null) {
      throw new AlreadyClosedException("MMapIndexInput already closed");
    }
    MMapIndexInput clone = (MMapIndexInput)super.clone();
    isClone = true;
    buffer = buffer.duplicate();
    return clone;
  }
  
  public void close()
    throws IOException
  {
    try
    {
      if ((isClone) || (buffer == null)) {
        return;
      }
      this$0.cleanMapping(buffer);
    }
    finally
    {
      buffer = null;
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.lucene.store.MMapDirectory.MMapIndexInput
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.store;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;

class MMapDirectory$MultiMMapIndexInput
  extends IndexInput
{
  private ByteBuffer[] buffers;
  private int[] bufSizes;
  private final long length;
  private int curBufIndex;
  private final int maxBufSize;
  private ByteBuffer curBuf;
  private int curAvail;
  private boolean isClone = false;
  private final MMapDirectory this$0;
  
  public MMapDirectory$MultiMMapIndexInput(MMapDirectory arg1, RandomAccessFile raf, int maxBufSize)
    throws IOException
  {
    length = raf.length();
    this.maxBufSize = maxBufSize;
    if (maxBufSize <= 0) {
      throw new IllegalArgumentException("Non positive maxBufSize: " + maxBufSize);
    }
    if (length / maxBufSize > 2147483647L) {
      throw new IllegalArgumentException("RandomAccessFile too big for maximum buffer size: " + raf.toString());
    }
    int nrBuffers = (int)(length / maxBufSize);
    if (nrBuffers * maxBufSize <= length) {
      nrBuffers++;
    }
    buffers = new ByteBuffer[nrBuffers];
    bufSizes = new int[nrBuffers];
    
    long bufferStart = 0L;
    FileChannel rafc = raf.getChannel();
    for (int bufNr = 0; bufNr < nrBuffers; bufNr++)
    {
      int bufSize = length > bufferStart + maxBufSize ? maxBufSize : (int)(length - bufferStart);
      
      buffers[bufNr] = rafc.map(FileChannel.MapMode.READ_ONLY, bufferStart, bufSize);
      bufSizes[bufNr] = bufSize;
      bufferStart += bufSize;
    }
    seek(0L);
  }
  
  public byte readByte()
    throws IOException
  {
    if (curAvail == 0)
    {
      curBufIndex += 1;
      if (curBufIndex >= buffers.length) {
        throw new IOException("read past EOF");
      }
      curBuf = buffers[curBufIndex];
      curBuf.position(0);
      curAvail = bufSizes[curBufIndex];
    }
    curAvail -= 1;
    return curBuf.get();
  }
  
  public void readBytes(byte[] b, int offset, int len)
    throws IOException
  {
    while (len > curAvail)
    {
      curBuf.get(b, offset, curAvail);
      len -= curAvail;
      offset += curAvail;
      curBufIndex += 1;
      if (curBufIndex >= buffers.length) {
        throw new IOException("read past EOF");
      }
      curBuf = buffers[curBufIndex];
      curBuf.position(0);
      curAvail = bufSizes[curBufIndex];
    }
    curBuf.get(b, offset, len);
    curAvail -= len;
  }
  
  public long getFilePointer()
  {
    return curBufIndex * maxBufSize + curBuf.position();
  }
  
  public void seek(long pos)
    throws IOException
  {
    curBufIndex = ((int)(pos / maxBufSize));
    curBuf = buffers[curBufIndex];
    int bufOffset = (int)(pos - curBufIndex * maxBufSize);
    curBuf.position(bufOffset);
    curAvail = (bufSizes[curBufIndex] - bufOffset);
  }
  
  public long length()
  {
    return length;
  }
  
  public Object clone()
  {
    if (buffers == null) {
      throw new AlreadyClosedException("MultiMMapIndexInput already closed");
    }
    MultiMMapIndexInput clone = (MultiMMapIndexInput)super.clone();
    isClone = true;
    buffers = new ByteBuffer[buffers.length];
    for (int bufNr = 0; bufNr < buffers.length; bufNr++) {
      buffers[bufNr] = buffers[bufNr].duplicate();
    }
    try
    {
      clone.seek(getFilePointer());
    }
    catch (IOException ioe)
    {
      RuntimeException newException = new RuntimeException(ioe);
      newException.initCause(ioe);
      throw newException;
    }
    return clone;
  }
  
  public void close()
    throws IOException
  {
    try
    {
      if ((isClone) || (buffers == null)) {
        return;
      }
      for (int bufNr = 0; bufNr < buffers.length; bufNr++) {
        try
        {
          this$0.cleanMapping(buffers[bufNr]);
        }
        finally
        {
          buffers[bufNr] = null;
        }
      }
    }
    finally
    {
      buffers = null;
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.lucene.store.MMapDirectory.MultiMMapIndexInput
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.store;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.lang.reflect.Method;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
import java.security.AccessController;
import java.security.PrivilegedActionException;
import java.security.PrivilegedExceptionAction;
import org.apache.lucene.util.Constants;

public class MMapDirectory
  extends FSDirectory
{
  public MMapDirectory(File path, LockFactory lockFactory)
    throws IOException
  {
    super(path, lockFactory);
  }
  
  public MMapDirectory(File path)
    throws IOException
  {
    super(path, null);
  }
  
  static final Class[] NO_PARAM_TYPES = new Class[0];
  static final Object[] NO_PARAMS = new Object[0];
  private boolean useUnmapHack = false;
  private int maxBBuf = Constants.JRE_IS_64BIT ? Integer.MAX_VALUE : 268435456;
  public static final boolean UNMAP_SUPPORTED;
  
  static
  {
    boolean v;
    try
    {
      Class.forName("sun.misc.Cleaner");
      Class.forName("java.nio.DirectByteBuffer").getMethod("cleaner", NO_PARAM_TYPES);
      
      v = true;
    }
    catch (Exception e)
    {
      v = false;
    }
    UNMAP_SUPPORTED = v;
  }
  
  public void setUseUnmap(boolean useUnmapHack)
  {
    if ((useUnmapHack) && (!UNMAP_SUPPORTED)) {
      throw new IllegalArgumentException("Unmap hack not supported on this platform!");
    }
    this.useUnmapHack = useUnmapHack;
  }
  
  public boolean getUseUnmap()
  {
    return useUnmapHack;
  }
  
  final void cleanMapping(final ByteBuffer buffer)
    throws IOException
  {
    if (useUnmapHack) {
      try
      {
        AccessController.doPrivileged(new PrivilegedExceptionAction()
        {
          public Object run()
            throws Exception
          {
            Method getCleanerMethod = buffer.getClass().getMethod("cleaner", MMapDirectory.NO_PARAM_TYPES);
            
            getCleanerMethod.setAccessible(true);
            Object cleaner = getCleanerMethod.invoke(buffer, MMapDirectory.NO_PARAMS);
            if (cleaner != null) {
              cleaner.getClass().getMethod("clean", MMapDirectory.NO_PARAM_TYPES).invoke(cleaner, MMapDirectory.NO_PARAMS);
            }
            return null;
          }
        });
      }
      catch (PrivilegedActionException e)
      {
        IOException ioe = new IOException("unable to unmap the mapped buffer");
        ioe.initCause(e.getCause());
        throw ioe;
      }
    }
  }
  
  public void setMaxChunkSize(int maxBBuf)
  {
    if (maxBBuf <= 0) {
      throw new IllegalArgumentException("Maximum chunk size for mmap must be >0");
    }
    this.maxBBuf = maxBBuf;
  }
  
  public int getMaxChunkSize()
  {
    return maxBBuf;
  }
  
  private class MMapIndexInput
    extends IndexInput
  {
    private ByteBuffer buffer;
    private final long length;
    
    MMapIndexInput(RandomAccessFile x1, MMapDirectory.1 x2)
      throws IOException
    {
      this(x1);
    }
    
    private boolean isClone = false;
    
    private MMapIndexInput(RandomAccessFile raf)
      throws IOException
    {
      length = raf.length();
      buffer = raf.getChannel().map(FileChannel.MapMode.READ_ONLY, 0L, length);
    }
    
    public byte readByte()
      throws IOException
    {
      try
      {
        return buffer.get();
      }
      catch (BufferUnderflowException e)
      {
        throw new IOException("read past EOF");
      }
    }
    
    public void readBytes(byte[] b, int offset, int len)
      throws IOException
    {
      try
      {
        buffer.get(b, offset, len);
      }
      catch (BufferUnderflowException e)
      {
        throw new IOException("read past EOF");
      }
    }
    
    public long getFilePointer()
    {
      return buffer.position();
    }
    
    public void seek(long pos)
      throws IOException
    {
      buffer.position((int)pos);
    }
    
    public long length()
    {
      return length;
    }
    
    public Object clone()
    {
      if (buffer == null) {
        throw new AlreadyClosedException("MMapIndexInput already closed");
      }
      MMapIndexInput clone = (MMapIndexInput)super.clone();
      isClone = true;
      buffer = buffer.duplicate();
      return clone;
    }
    
    public void close()
      throws IOException
    {
      try
      {
        if ((isClone) || (buffer == null)) {
          return;
        }
        cleanMapping(buffer);
      }
      finally
      {
        buffer = null;
      }
    }
  }
  
  private class MultiMMapIndexInput
    extends IndexInput
  {
    private ByteBuffer[] buffers;
    private int[] bufSizes;
    private final long length;
    private int curBufIndex;
    private final int maxBufSize;
    private ByteBuffer curBuf;
    private int curAvail;
    private boolean isClone = false;
    
    public MultiMMapIndexInput(RandomAccessFile raf, int maxBufSize)
      throws IOException
    {
      length = raf.length();
      this.maxBufSize = maxBufSize;
      if (maxBufSize <= 0) {
        throw new IllegalArgumentException("Non positive maxBufSize: " + maxBufSize);
      }
      if (length / maxBufSize > 2147483647L) {
        throw new IllegalArgumentException("RandomAccessFile too big for maximum buffer size: " + raf.toString());
      }
      int nrBuffers = (int)(length / maxBufSize);
      if (nrBuffers * maxBufSize <= length) {
        nrBuffers++;
      }
      buffers = new ByteBuffer[nrBuffers];
      bufSizes = new int[nrBuffers];
      
      long bufferStart = 0L;
      FileChannel rafc = raf.getChannel();
      for (int bufNr = 0; bufNr < nrBuffers; bufNr++)
      {
        int bufSize = length > bufferStart + maxBufSize ? maxBufSize : (int)(length - bufferStart);
        
        buffers[bufNr] = rafc.map(FileChannel.MapMode.READ_ONLY, bufferStart, bufSize);
        bufSizes[bufNr] = bufSize;
        bufferStart += bufSize;
      }
      seek(0L);
    }
    
    public byte readByte()
      throws IOException
    {
      if (curAvail == 0)
      {
        curBufIndex += 1;
        if (curBufIndex >= buffers.length) {
          throw new IOException("read past EOF");
        }
        curBuf = buffers[curBufIndex];
        curBuf.position(0);
        curAvail = bufSizes[curBufIndex];
      }
      curAvail -= 1;
      return curBuf.get();
    }
    
    public void readBytes(byte[] b, int offset, int len)
      throws IOException
    {
      while (len > curAvail)
      {
        curBuf.get(b, offset, curAvail);
        len -= curAvail;
        offset += curAvail;
        curBufIndex += 1;
        if (curBufIndex >= buffers.length) {
          throw new IOException("read past EOF");
        }
        curBuf = buffers[curBufIndex];
        curBuf.position(0);
        curAvail = bufSizes[curBufIndex];
      }
      curBuf.get(b, offset, len);
      curAvail -= len;
    }
    
    public long getFilePointer()
    {
      return curBufIndex * maxBufSize + curBuf.position();
    }
    
    public void seek(long pos)
      throws IOException
    {
      curBufIndex = ((int)(pos / maxBufSize));
      curBuf = buffers[curBufIndex];
      int bufOffset = (int)(pos - curBufIndex * maxBufSize);
      curBuf.position(bufOffset);
      curAvail = (bufSizes[curBufIndex] - bufOffset);
    }
    
    public long length()
    {
      return length;
    }
    
    public Object clone()
    {
      if (buffers == null) {
        throw new AlreadyClosedException("MultiMMapIndexInput already closed");
      }
      MultiMMapIndexInput clone = (MultiMMapIndexInput)super.clone();
      isClone = true;
      buffers = new ByteBuffer[buffers.length];
      for (int bufNr = 0; bufNr < buffers.length; bufNr++) {
        buffers[bufNr] = buffers[bufNr].duplicate();
      }
      try
      {
        clone.seek(getFilePointer());
      }
      catch (IOException ioe)
      {
        RuntimeException newException = new RuntimeException(ioe);
        newException.initCause(ioe);
        throw newException;
      }
      return clone;
    }
    
    public void close()
      throws IOException
    {
      try
      {
        if ((isClone) || (buffers == null)) {
          return;
        }
        for (int bufNr = 0; bufNr < buffers.length; bufNr++) {
          try
          {
            cleanMapping(buffers[bufNr]);
          }
          finally
          {
            buffers[bufNr] = null;
          }
        }
      }
      finally
      {
        buffers = null;
      }
    }
  }
  
  public IndexInput openInput(String name, int bufferSize)
    throws IOException
  {
    ensureOpen();
    File f = new File(getFile(), name);
    RandomAccessFile raf = new RandomAccessFile(f, "r");
    try
    {
      return raf.length() <= maxBBuf ? new MMapIndexInput(raf, null) : new MultiMMapIndexInput(raf, maxBBuf);
    }
    finally
    {
      raf.close();
    }
  }
  
  public IndexOutput createOutput(String name)
    throws IOException
  {
    initOutput(name);
    return new SimpleFSDirectory.SimpleFSIndexOutput(new File(directory, name));
  }
  
  /**
   * @deprecated
   */
  MMapDirectory() {}
}

/* Location:
 * Qualified Name:     org.apache.lucene.store.MMapDirectory
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.store;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFSDirectory$NIOFSIndexInput
  extends SimpleFSDirectory.SimpleFSIndexInput
{
  private ByteBuffer byteBuf;
  private byte[] otherBuffer;
  private ByteBuffer otherByteBuf;
  final FileChannel channel;
  
  /**
   * @deprecated
   */
  public NIOFSDirectory$NIOFSIndexInput(File path, int bufferSize)
    throws IOException
  {
    this(path, bufferSize, FSDirectory.DEFAULT_READ_CHUNK_SIZE);
  }
  
  public NIOFSDirectory$NIOFSIndexInput(File path, int bufferSize, int chunkSize)
    throws IOException
  {
    super(path, bufferSize, chunkSize);
    channel = file.getChannel();
  }
  
  protected void newBuffer(byte[] newBuffer)
  {
    super.newBuffer(newBuffer);
    byteBuf = ByteBuffer.wrap(newBuffer);
  }
  
  public void close()
    throws IOException
  {
    if ((!isClone) && (file.isOpen)) {
      try
      {
        channel.close();
      }
      finally
      {
        file.close();
      }
    }
  }
  
  protected void readInternal(byte[] b, int offset, int len)
    throws IOException
  {
    ByteBuffer bb;
    ByteBuffer bb;
    if ((b == buffer) && (0 == offset))
    {
      assert (byteBuf != null);
      byteBuf.clear();
      byteBuf.limit(len);
      bb = byteBuf;
    }
    else
    {
      ByteBuffer bb;
      if (offset == 0)
      {
        if (otherBuffer != b)
        {
          otherBuffer = b;
          otherByteBuf = ByteBuffer.wrap(b);
        }
        else
        {
          otherByteBuf.clear();
        }
        otherByteBuf.limit(len);
        bb = otherByteBuf;
      }
      else
      {
        bb = ByteBuffer.wrap(b, offset, len);
      }
    }
    int readOffset = bb.position();
    int readLength = bb.limit() - readOffset;
    assert (readLength == len);
    
    long pos = getFilePointer();
    try
    {
      while (readLength > 0)
      {
        int limit;
        int limit;
        if (readLength > chunkSize) {
          limit = readOffset + chunkSize;
        } else {
          limit = readOffset + readLength;
        }
        bb.limit(limit);
        int i = channel.read(bb, pos);
        if (i == -1) {
          throw new IOException("read past EOF");
        }
        pos += i;
        readOffset += i;
        readLength -= i;
      }
    }
    catch (OutOfMemoryError e)
    {
      OutOfMemoryError outOfMemoryError = new OutOfMemoryError("OutOfMemoryError likely caused by the Sun VM Bug described in https://issues.apache.org/jira/browse/LUCENE-1566; try calling FSDirectory.setReadChunkSize with a a value smaller than the current chunk size (" + chunkSize + ")");
      
      outOfMemoryError.initCause(e);
      throw outOfMemoryError;
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.lucene.store.NIOFSDirectory.NIOFSIndexInput
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.store;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class NIOFSDirectory
  extends FSDirectory
{
  public NIOFSDirectory(File path, LockFactory lockFactory)
    throws IOException
  {
    super(path, lockFactory);
  }
  
  public NIOFSDirectory(File path)
    throws IOException
  {
    super(path, null);
  }
  
  /**
   * @deprecated
   */
  NIOFSDirectory() {}
  
  public IndexInput openInput(String name, int bufferSize)
    throws IOException
  {
    ensureOpen();
    return new NIOFSIndexInput(new File(getFile(), name), bufferSize, getReadChunkSize());
  }
  
  public IndexOutput createOutput(String name)
    throws IOException
  {
    initOutput(name);
    return new SimpleFSDirectory.SimpleFSIndexOutput(new File(directory, name));
  }
  
  protected static class NIOFSIndexInput
    extends SimpleFSDirectory.SimpleFSIndexInput
  {
    private ByteBuffer byteBuf;
    private byte[] otherBuffer;
    private ByteBuffer otherByteBuf;
    final FileChannel channel;
    
    /**
     * @deprecated
     */
    public NIOFSIndexInput(File path, int bufferSize)
      throws IOException
    {
      this(path, bufferSize, FSDirectory.DEFAULT_READ_CHUNK_SIZE);
    }
    
    public NIOFSIndexInput(File path, int bufferSize, int chunkSize)
      throws IOException
    {
      super(bufferSize, chunkSize);
      channel = file.getChannel();
    }
    
    protected void newBuffer(byte[] newBuffer)
    {
      super.newBuffer(newBuffer);
      byteBuf = ByteBuffer.wrap(newBuffer);
    }
    
    public void close()
      throws IOException
    {
      if ((!isClone) && (file.isOpen)) {
        try
        {
          channel.close();
        }
        finally
        {
          file.close();
        }
      }
    }
    
    protected void readInternal(byte[] b, int offset, int len)
      throws IOException
    {
      ByteBuffer bb;
      ByteBuffer bb;
      if ((b == buffer) && (0 == offset))
      {
        assert (byteBuf != null);
        byteBuf.clear();
        byteBuf.limit(len);
        bb = byteBuf;
      }
      else
      {
        ByteBuffer bb;
        if (offset == 0)
        {
          if (otherBuffer != b)
          {
            otherBuffer = b;
            otherByteBuf = ByteBuffer.wrap(b);
          }
          else
          {
            otherByteBuf.clear();
          }
          otherByteBuf.limit(len);
          bb = otherByteBuf;
        }
        else
        {
          bb = ByteBuffer.wrap(b, offset, len);
        }
      }
      int readOffset = bb.position();
      int readLength = bb.limit() - readOffset;
      assert (readLength == len);
      
      long pos = getFilePointer();
      try
      {
        while (readLength > 0)
        {
          int limit;
          int limit;
          if (readLength > chunkSize) {
            limit = readOffset + chunkSize;
          } else {
            limit = readOffset + readLength;
          }
          bb.limit(limit);
          int i = channel.read(bb, pos);
          if (i == -1) {
            throw new IOException("read past EOF");
          }
          pos += i;
          readOffset += i;
          readLength -= i;
        }
      }
      catch (OutOfMemoryError e)
      {
        OutOfMemoryError outOfMemoryError = new OutOfMemoryError("OutOfMemoryError likely caused by the Sun VM Bug described in https://issues.apache.org/jira/browse/LUCENE-1566; try calling FSDirectory.setReadChunkSize with a a value smaller than the current chunk size (" + chunkSize + ")");
        
        outOfMemoryError.initCause(e);
        throw outOfMemoryError;
      }
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.lucene.store.NIOFSDirectory
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.store;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.nio.channels.FileLock;
import java.util.HashSet;

class NativeFSLock
  extends Lock
{
  private RandomAccessFile f;
  private FileChannel channel;
  private FileLock lock;
  private File path;
  private File lockDir;
  private static HashSet LOCK_HELD = new HashSet();
  
  public NativeFSLock(File lockDir, String lockFileName)
  {
    this.lockDir = lockDir;
    path = new File(lockDir, lockFileName);
  }
  
  private synchronized boolean lockExists()
  {
    return lock != null;
  }
  
  public synchronized boolean obtain()
    throws IOException
  {
    if (lockExists()) {
      return false;
    }
    if (!lockDir.exists())
    {
      if (!lockDir.mkdirs()) {
        throw new IOException("Cannot create directory: " + lockDir.getAbsolutePath());
      }
    }
    else if (!lockDir.isDirectory()) {
      throw new IOException("Found regular file where directory expected: " + lockDir.getAbsolutePath());
    }
    String canonicalPath = path.getCanonicalPath();
    
    boolean markedHeld = false;
    try
    {
      synchronized (LOCK_HELD)
      {
        if (LOCK_HELD.contains(canonicalPath))
        {
          boolean bool1 = false;jsr 224;return bool1;
        }
        LOCK_HELD.add(canonicalPath);
        markedHeld = true;
      }
      try
      {
        channel = f.getChannel();
        try
        {
          lock = channel.tryLock();
        }
        catch (IOException e)
        {
          failureReason = e;
        }
        finally
        {
          if (lock != null) {}
        }
        ret;
      }
      finally
      {
        if (channel != null) {}
      }
      ret;
    }
    finally
    {
      if ((markedHeld) && (!lockExists())) {
        synchronized (LOCK_HELD)
        {
          if (LOCK_HELD.contains(canonicalPath)) {
            LOCK_HELD.remove(canonicalPath);
          }
        }
      }
    }
    return lockExists();
  }
  
  public synchronized void release()
    throws IOException
  {
    if (lockExists())
    {
      try
      {
        lock.release();
      }
      finally
      {
        lock = null;
      }
      ret;ret;
      
      path.delete();
    }
    else
    {
      boolean obtained = false;
      try
      {
        if (!(obtained = obtain())) {
          throw new LockReleaseFailedException("Cannot forcefully unlock a NativeFSLock which is held by another indexer component: " + path);
        }
      }
      finally
      {
        if (obtained) {
          release();
        }
      }
    }
  }
  
  public synchronized boolean isLocked()
  {
    if (lockExists()) {
      return true;
    }
    if (!path.exists()) {
      return false;
    }
    try
    {
      boolean obtained = obtain();
      if (obtained) {
        release();
      }
      return !obtained;
    }
    catch (IOException ioe) {}
    return false;
  }
  
  public String toString()
  {
    return "NativeFSLock@" + path;
  }
}

/* Location:
 * Qualified Name:     org.apache.lucene.store.NativeFSLock
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.store;

import java.io.File;
import java.io.IOException;

public class NativeFSLockFactory
  extends FSLockFactory
{
  public NativeFSLockFactory()
    throws IOException
  {
    this((File)null);
  }
  
  public NativeFSLockFactory(String lockDirName)
    throws IOException
  {
    this(new File(lockDirName));
  }
  
  public NativeFSLockFactory(File lockDir)
    throws IOException
  {
    setLockDir(lockDir);
  }
  
  public synchronized Lock makeLock(String lockName)
  {
    if (lockPrefix != null) {
      lockName = lockPrefix + "-" + lockName;
    }
    return new NativeFSLock(lockDir, lockName);
  }
  
  public void clearLock(String lockName)
    throws IOException
  {
    if (lockDir.exists())
    {
      if (lockPrefix != null) {
        lockName = lockPrefix + "-" + lockName;
      }
      File lockFile = new File(lockDir, lockName);
      if ((lockFile.exists()) && (!lockFile.delete())) {
        throw new IOException("Cannot delete " + lockFile);
      }
    }
  }
}

/* Location:
 * Qualified Name:     org.apache.lucene.store.NativeFSLockFactory
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.store;

import java.io.IOException;

class NoLock
  extends Lock
{
  public boolean obtain()
    throws IOException
  {
    return true;
  }
  
  public void release() {}
  
  public boolean isLocked()
  {
    return false;
  }
  
  public String toString()
  {
    return "NoLock";
  }
}

/* Location:
 * Qualified Name:     org.apache.lucene.store.NoLock
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.store;

public class NoLockFactory
  extends LockFactory
{
  private static NoLock singletonLock = new NoLock();
  private static NoLockFactory singleton = new NoLockFactory();
  
  public static NoLockFactory getNoLockFactory()
  {
    return singleton;
  }
  
  public Lock makeLock(String lockName)
  {
    return singletonLock;
  }
  
  public void clearLock(String lockName) {}
}

/* Location:
 * Qualified Name:     org.apache.lucene.store.NoLockFactory
 * Java Class Version: 1.4 (48.0)
 * JD-Core Version:    0.7.1
 */
package org.apache.lucene.store;

import java.io.FileNotFoundException;

public class NoSuchDirectoryExceptio
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

Further reading...

For more information on Java 1.5 Tiger, you may find Java 1.5 Tiger, A developer's Notebook by D. Flanagan and B. McLaughlin from O'Reilly of interest.

New!JAR listings


Copyright 2006-2017. Infinite Loop Ltd