package voldemort.store.readonly.io;

import java.io.Closeable;
import java.io.File;
import java.io.FileDescriptor;
import java.io.IOException;

import org.apache.log4j.Logger;

import voldemort.store.readonly.io.jna.mman;

import com.sun.jna.Pointer;

public class MemLock implements Closeable {

    private static final Logger logger = Logger.getLogger(MemLock.class);

    private Pointer pa;
    private long length;
    private File file;
    private FileDescriptor descriptor;

    /**
     * Call mmap a file descriptor, then lock the pages using mlock(). This
     * will then prevent the pages from being swapped out due to VFS cache
     * pressure.
     * 
     * @param descriptor The file we should mmap and mlock
     * @param offset
     * @param length
     * @see #close()
     * @throws IOException
     */
    public MemLock(File file, FileDescriptor descriptor, long offset, long length)
                                                                                  throws IOException {
        if(logger.isDebugEnabled())
            logger.debug("mlocking " + file + " with length " + length);

        this.setFile(file);
        this.setDescriptor(descriptor);
        this.length = length;
	pa = null;

        int fd = voldemort.store.readonly.io.Native.getFd(descriptor);

        pa = mman.mmap(length, mman.PROT_READ, mman.MAP_SHARED, fd, offset);

	/* note, any failure in the mlock() system call is ignored,
	 * because it's normal behavior in an unprivileged process is to
	 * fail due system rlimits.
	 */
	mman.mlock(pa, length);

    }

    /**
     * Release this lock so that the memory can be returned to the OS if it
     * wants to us it.
     */
    @Override
    public void close() throws IOException {

        if(logger.isDebugEnabled())
            logger.debug("munlocking " + file + " with length " + length);

	if(pa != null) {
	    /* note: any failure in the munlock() system call is ignored too */
	    Pointer xpa = pa;
	    pa = null;
	    mman.munlock(xpa, length);
	    mman.munmap(xpa, length);
	}
    }

    public File getFile() {
        return file;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public FileDescriptor getDescriptor() {
        return descriptor;
    }

    public void setDescriptor(FileDescriptor descriptor) {
        this.descriptor = descriptor;
    }

}

@@ -441,47 +441,8 @@
 ile;
-%0A    private FileDescriptor descriptor;
 %0A%0A  
@@ -1093,61 +1093,19 @@
 his.
-setFile(file);%0A        this.setDescriptor(descriptor)
+file = file
 ;%0A  
@@ -2003,24 +2003,24 @@
 getFile() %7B%0A
+
         retu
@@ -2039,258 +2039,6 @@
  %7D%0A%0A
-    public void setFile(File file) %7B%0A        this.file = file;%0A    %7D%0A%0A    public FileDescriptor getDescriptor() %7B%0A        return descriptor;%0A    %7D%0A%0A    public void setDescriptor(FileDescriptor descriptor) %7B%0A        this.descriptor = descriptor;%0A    %7D%0A%0A
 %7D%0A

Remove unused members and methods from MemLock