Update of /cvsroot/freenet/freenet/src/freenet/support
In directory sc8-pr-cvs1:/tmp/cvs-serv10578/support

Modified Files:
        RandomAccessFilePool.java 
Added Files:
        FakeRandomAccessFilePool.java RealRandomAccessFilePool.java 
Log Message:
Cleanup descriptions shown by --config
setExpert on three options someone added without descriptions.
Fix an NPE in the old RandomAccessFilePool
Reimplement RandomAccessFilePool to support OSs that don't limit FDs

--- NEW FILE: FakeRandomAccessFilePool.java ---
package freenet.support;

import java.io.*;
import freenet.Core;

public class FakeRandomAccessFilePool implements RandomAccessFilePool {
    volatile int totalOpenFiles;

    public int totalOpenFiles() {
        return totalOpenFiles;
    }

    public int maxOpenFiles() {
        return 0;
    }

    public PooledRandomAccessFile open(File f, String mode) throws IOException {
        return new MyPooledRandomAccessFile(f, mode);
    }

    class MyPooledRandomAccessFile extends RandomAccessFile implements 
PooledRandomAccessFile {

        public MyPooledRandomAccessFile(File f, String mode) throws IOException {
            super(f,mode);
            synchronized(FakeRandomAccessFilePool.this) {
                totalOpenFiles++;
            }
        }

        /**
         * Not in RandomAccessFile API, but because of synchronization
         * issues, it is better to put it here */
        public synchronized void sync() throws IOException {
            super.getFD().sync();
        }

        public void closeRAF() {}

        public synchronized void close() throws IOException {
            super.close();
            synchronized(FakeRandomAccessFilePool.this) {
                totalOpenFiles--;
            }
        }
    }
}

--- NEW FILE: RealRandomAccessFilePool.java ---
package freenet.support;

import java.io.*;
import freenet.Core;

/**
 * A pool of PooledRandomAccessFiles. These are similar to 
 * java.io.RandomAccessFiles, but we have a maximum number of file
 * descriptors. If there are more RAFs than that allocated, we keep only
 * the MRU fds open.
 *
 * Synchronized.
 */
public class RealRandomAccessFilePool implements RandomAccessFilePool {
    int maxOpenFiles;
    volatile int totalOpenFiles;
    LRUQueue queue;

    public int totalOpenFiles() {
        return totalOpenFiles;
    }

    public int maxOpenFiles() {
        return maxOpenFiles;
    }

    public RealRandomAccessFilePool(int maxFDsOpen) {
        this.maxOpenFiles = maxFDsOpen;
        queue = new LRUQueue();
        if(maxFDsOpen < 2) throw new IllegalArgumentException("Unreasonable maximum 
number of open files "+maxFDsOpen);
    }

    public PooledRandomAccessFile open(File f, String mode) throws IOException {
        return new MyPooledRandomAccessFile(f, mode);
    }

    class MyPooledRandomAccessFile implements PooledRandomAccessFile {
        File filename;
        String mode;
        RandomAccessFile raf;
        boolean closed = false;
        long position = 0; // Not guaranteed to be up to date unless raf closed

        public MyPooledRandomAccessFile(File f, String mode) throws IOException {
            this.filename = f;
            this.mode = mode;
            reopen();
        }

        synchronized void reopen() throws IOException {
            if(raf == null) {
                if(closed) 
                    throw new IOException("Already closed or failed to save position");
                boolean firstIteration = true;
                while(totalOpenFiles >= maxOpenFiles) {
                    PooledRandomAccessFile fClose = null;
                    synchronized(RealRandomAccessFilePool.this) {
                        if(firstIteration) {
                            totalOpenFiles++; // the one we are about to open
                            queue.push(this);
                            firstIteration = false;
                        }
                        // Do it here to try to avoid livelock

                        if(totalOpenFiles > maxOpenFiles)
                            fClose = (PooledRandomAccessFile)(queue.pop());
                    }
                    // Close LRU
                    // Since this is MRU we shouldn't have lock contention
                    if(fClose == this)
                        throw new IllegalStateException("aaaaargh! Popped self!");
                    if(fClose != null) fClose.closeRAF();
                }
                raf = new RandomAccessFile(filename, mode);
                if(position != 0) raf.seek(position);
            }
        }
    
        public long length() throws IOException {
            synchronized(this) {
                if(raf != null) return raf.length();
            }
            return filename.length();
        }
    
        public synchronized void seek(long pos) throws IOException {
            reopen();
            raf.seek(pos);
            position = pos;
        }
    
        /**
         * Not in RandomAccessFile API, but because of synchronization
         * issues, it is better to put it here */
        public synchronized void sync() throws IOException {
            // No need to reopen
            if(raf != null) raf.getFD().sync();
        }
    
        public synchronized int read() throws IOException {
            reopen();
            return raf.read();
        }
    
        public synchronized int read(byte[] buf, int off, int len) 
            throws IOException {
            reopen();
            return raf.read(buf, off, len);
        }
    
        public synchronized void write(int b) throws IOException {
            reopen();
            raf.write(b);
        }
    
        public synchronized void write(byte[] b, int offset, int len) 
            throws IOException {
            reopen();
            raf.write(b, offset, len);
        }
    
        public synchronized long getFilePointer() throws IOException {
            if(raf == null) return position;
                else return raf.getFilePointer();
        }
    
        public synchronized void closeRAF() {
            if(raf == null) return;
            synchronized(RealRandomAccessFilePool.this) {
                if(totalOpenFiles < maxOpenFiles) return;
            }
            try {
                 position = raf.getFilePointer();
            } catch (IOException e) {
                closed = true;
                position = 0;
            } catch (Throwable t) {
                Core.logger.log(this, "Caught "+t+" saving file pointer for "+
                        raf, Logger.ERROR);
            }
            try {
                raf.close();
            } catch (IOException e) {
            } catch (Throwable t) {
                Core.logger.log(this, "Caught "+t+" closing "+raf, 
                        Logger.ERROR);
            // assume it is closed...
            }
            synchronized(RealRandomAccessFilePool.this) {
                totalOpenFiles--;
            }
            raf = null;
        }
    
        public synchronized void close() throws IOException {
            closed = true;
            if (raf != null) {
                raf.close();
                raf = null;
                synchronized(RealRandomAccessFilePool.this) {
                    totalOpenFiles--;
                }
            }
        }
    }
}

Index: RandomAccessFilePool.java
===================================================================
RCS file: /cvsroot/freenet/freenet/src/freenet/support/RandomAccessFilePool.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -w -r1.4 -r1.5
--- RandomAccessFilePool.java   17 Oct 2003 01:43:29 -0000      1.4
+++ RandomAccessFilePool.java   10 Nov 2003 09:48:39 -0000      1.5
@@ -1,174 +1,9 @@
 package freenet.support;
 
 import java.io.*;
-import freenet.Core;
 
-/**
- * A pool of PooledRandomAccessFiles. These are similar to 
- * java.io.RandomAccessFiles, but we have a maximum number of file
- * descriptors. If there are more RAFs than that allocated, we keep only
- * the MRU fds open.
- *
- * Synchronized.
- */
-public class RandomAccessFilePool {
-    int maxOpenFiles;
-    volatile int totalOpenFiles;
-    LRUQueue queue;
-    
-    public int totalOpenFiles() {
-       return totalOpenFiles;
-    }
-    
-    public int maxOpenFiles() {
-       return maxOpenFiles;
-    }
-    
-    public RandomAccessFilePool(int maxFDsOpen) {
-       this.maxOpenFiles = maxFDsOpen;
-       queue = new LRUQueue();
-       if(maxFDsOpen < 2) throw new IllegalArgumentException("Unreasonable maximum 
number of open files "+maxFDsOpen);
-    }
-    
-    public PooledRandomAccessFile open(File f, String mode) 
-       throws IOException {
-       return new MyPooledRandomAccessFile(f, mode);
-    }
-    
-    class MyPooledRandomAccessFile implements PooledRandomAccessFile {
-       File filename;
-       String mode;
-       RandomAccessFile raf;
-       boolean closed = false;
-       long position = 0; // Not guaranteed to be up to date unless raf closed
-       
-       public MyPooledRandomAccessFile(File f, String mode) throws IOException {
-           this.filename = f;
-           this.mode = mode;
-           reopen();
-       }
-       
-       synchronized void reopen() throws IOException {
-           if(raf == null) {
-               if(closed) 
-                   throw new IOException("Already closed or failed to save position");
-               boolean firstIteration = true;
-               while(totalOpenFiles >= maxOpenFiles) {
-                   PooledRandomAccessFile fClose = null;
-                   synchronized(RandomAccessFilePool.this) {
-                       if(firstIteration) {
-                           totalOpenFiles++; // the one we are about to open
-                           queue.push(this);
-                           firstIteration = false;
-                       }
-                       // Do it here to try to avoid livelock
-                       
-                       if(totalOpenFiles > maxOpenFiles) {
-                           fClose = (PooledRandomAccessFile)(queue.pop());
-                       }
-                   }
-                   // Close LRU
-                   // Since this is MRU we shouldn't have lock contention
-                   if(fClose == this)
-                       throw new IllegalStateException("aaaaargh! Popped self!");
-                   if(fClose != null) fClose.closeRAF();
-               }
-               raf = new RandomAccessFile(filename, mode);
-               if(position != 0) {
-                   raf.seek(position);
-               }
-           }
-       }
-       
-       public long length() throws IOException {
-           synchronized(this) {
-               if(raf != null)
-                   return raf.length();
-           }
-           return filename.length();
-       }
-       
-       public synchronized void seek(long pos) throws IOException {
-           reopen();
-           raf.seek(pos);
-           position = pos;
-       }
-       
-       /**
-        * Not in RandomAccessFile API, but because of synchronization
-        * issues, it is better to put it here */
-       public synchronized void sync() throws IOException {
-           // No need to reopen
-           if(raf != null) {
-               raf.getFD().sync();
-           }
-       }
-       
-       public synchronized int read() throws IOException {
-           reopen();
-           return raf.read();
-       }
-       
-       public synchronized int read(byte[] buf, int off, int len) 
-           throws IOException {
-           reopen();
-           return raf.read(buf, off, len);
-       }
-       
-       public synchronized void write(int b) throws IOException {
-           reopen();
-           raf.write(b);
-       }
-       
-       public synchronized void write(byte[] b, int offset, int len) 
-           throws IOException {
-           reopen();
-           raf.write(b, offset, len);
-       }
-       
-       public synchronized long getFilePointer() throws IOException {
-           if(raf == null)
-               return position;
-           else
-               return raf.getFilePointer();
-       }
-       
-       public synchronized void closeRAF() {
-           if(raf == null) return;
-           synchronized(RandomAccessFilePool.this) {
-               if(totalOpenFiles < maxOpenFiles)
-                   return;
-           }
-           try {
-               position = raf.getFilePointer();
-           } catch (IOException e) {
-               closed = true;
-               position = 0;
-           } catch (Throwable t) {
-               Core.logger.log(this, "Caught "+t+" saving file pointer for "+
-                               raf, Logger.ERROR);
-           }
-           try {
-               raf.close();
-           } catch (IOException e) {
-           } catch (Throwable t) {
-               Core.logger.log(this, "Caught "+t+" closing "+raf, 
-                               Logger.ERROR);
-               // assume it is closed...
-           }
-           synchronized(RandomAccessFilePool.this) {
-               totalOpenFiles--;
-           }
-           raf = null;
-       }
-       
-       public synchronized void close() throws IOException {
-           closed = true;
-           raf.close();
-           raf = null;
-           synchronized(RandomAccessFilePool.this) {
-               totalOpenFiles--;
-           }
-       }
-    }
+public interface RandomAccessFilePool {
+    public int totalOpenFiles();
+    public int maxOpenFiles();
+    public PooledRandomAccessFile open(File f, String mode) throws IOException;
 }

_______________________________________________
cvs mailing list
[EMAIL PROTECTED]
http://dodo.freenetproject.org/cgi-bin/mailman/listinfo/cvs

Reply via email to