You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by ka...@apache.org on 2010/09/26 10:40:10 UTC

svn commit: r1001389 - /directory/apacheds/trunk/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/SingleFileLdifPartition.java

Author: kayyagari
Date: Sun Sep 26 08:40:09 2010
New Revision: 1001389

URL: http://svn.apache.org/viewvc?rev=1001389&view=rev
Log:
o added code to cleanup temporary file resources
o removed some code added during testing

Modified:
    directory/apacheds/trunk/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/SingleFileLdifPartition.java

Modified: directory/apacheds/trunk/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/SingleFileLdifPartition.java
URL: http://svn.apache.org/viewvc/directory/apacheds/trunk/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/SingleFileLdifPartition.java?rev=1001389&r1=1001388&r2=1001389&view=diff
==============================================================================
--- directory/apacheds/trunk/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/SingleFileLdifPartition.java (original)
+++ directory/apacheds/trunk/ldif-partition/src/main/java/org/apache/directory/server/core/partition/ldif/SingleFileLdifPartition.java Sun Sep 26 08:40:09 2010
@@ -25,13 +25,11 @@ import java.io.File;
 import java.io.IOException;
 import java.io.RandomAccessFile;
 import java.nio.channels.FileChannel;
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Comparator;
 import java.util.HashMap;
 import java.util.HashSet;
 import java.util.Iterator;
-import java.util.List;
 import java.util.Map;
 import java.util.Set;
 import java.util.TreeSet;
@@ -86,7 +84,7 @@ public class SingleFileLdifPartition ext
     private Object lock = new Object();
 
     /** the list of temporary buffers */
-    private List<File> tempFileList = new ArrayList<File>();
+    private Map<File,RandomAccessFile> tempBufMap = new HashMap<File,RandomAccessFile>();
 
     private Comparator<EntryOffset> comparator = new Comparator<EntryOffset>()
     {
@@ -415,13 +413,12 @@ public class SingleFileLdifPartition ext
 
                 long count = ( ldifFile.length() - endMovOffset.getEnd() );
 
-                File tmpFile = File.createTempFile( "mov", ".ldif" );
-
-                RandomAccessFile tmpMovFile = new RandomAccessFile( tmpFile.getAbsolutePath(), "rws" );
+                RandomAccessFile tmpMovFile = createTempFile();
 
                 FileChannel tempBuf = tmpMovFile.getChannel();
                 FileChannel mainChannel = ldifFile.getChannel();
                 tempBuf.truncate( 0 );
+                
                 if ( count > 0 )
                 {
                     mainChannel.transferTo( endMovOffset.getEnd(), count, tempBuf );
@@ -449,7 +446,6 @@ public class SingleFileLdifPartition ext
                     ldifFile.setLength( movedTreeOffsetArray[0].getStart() );
                 }
 
-                EntryOffset tmpMovFileOffset = null;
                 for ( EntryOffset o : movedTreeOffsets )
                 {
                     Long childId = o.getId();
@@ -461,7 +457,6 @@ public class SingleFileLdifPartition ext
 
                     entryOffset = new EntryOffset( childId, pos, tmpMovFile.getFilePointer() );
                     offsetMap.put( childId, entryOffset );
-                    tmpMovFileOffset = entryOffset;
                 }
 
                 EntryOffset parentOffset = offsetMap.get( parentId );
@@ -479,9 +474,6 @@ public class SingleFileLdifPartition ext
                 // copy back to main file
                 tempBuf.transferTo( 0, tempBuf.size(), mainChannel );
                 tempBuf.truncate( 0 );
-                tempBuf.close();
-                tmpMovFile.close();
-                tmpFile.delete();
 
                 // change offset of the copied entries
                 Set<Long> idset = new HashSet<Long>();
@@ -506,13 +498,15 @@ public class SingleFileLdifPartition ext
                     parentOffset = o;
                     idset.add( o.getId() );
                 }
-
-                deleteTempFiles();
             }
             catch ( Exception e )
             {
                 throw new LdapException( e );
             }
+            finally
+            {
+                deleteTempFiles();
+            }
         }
     }
 
@@ -560,15 +554,16 @@ public class SingleFileLdifPartition ext
 
                     tmpBufChannel.transferTo( 0, tmpBufChannel.size(), mainChannel );
                     tmpBufChannel.truncate( 0 );
-                    tmpBufChannel.close();
-                    tempBufFile.close();
-                    deleteTempFiles();
                 }
             }
             catch ( IOException e )
             {
                 throw new LdapException( e );
             }
+            finally
+            {
+                deleteTempFiles();
+            }
         }
     }
 
@@ -757,14 +752,15 @@ public class SingleFileLdifPartition ext
 
                 tmpBufChannel.transferTo( 0, tmpBufChannel.size(), mainChannel );
                 tmpBufChannel.truncate( 0 );
-                tmpBufChannel.close();
-                tempBufFile.close();
-                deleteTempFiles();
             }
             catch ( IOException e )
             {
                 throw new LdapException( e );
             }
+            finally
+            {
+                deleteTempFiles();
+            }
         }
     }
 
@@ -837,17 +833,16 @@ public class SingleFileLdifPartition ext
 
                     tmpBufChannel.transferTo( 0, tmpBufChannel.size(), mainChannel );
                     tmpBufChannel.truncate( 0 );
-                    tmpBufChannel.close();
-                    tempBufFile.close();
-                    deleteTempFiles();
                 }
             }
             catch ( IOException e )
             {
-                e.printStackTrace();
-                System.exit(0);
                 throw new LdapException( e );
             }
+            finally
+            {
+                deleteTempFiles();
+            }
         }
     }
 
@@ -1031,6 +1026,7 @@ public class SingleFileLdifPartition ext
             RandomAccessFile tempBufFile = new RandomAccessFile( tmpFile.getAbsolutePath(), "rws" );
             tempBufFile.setLength( 0 );
 
+            tempBufMap.put( tmpFile, tempBufFile );
             return tempBufFile;
         }
     }
@@ -1043,10 +1039,20 @@ public class SingleFileLdifPartition ext
     {
         synchronized ( lock )
         {
-            for ( File f : tempFileList )
+            for ( java.util.Map.Entry<File,RandomAccessFile> f : tempBufMap.entrySet() )
             {
-                f.delete();
+                try
+                {
+                    f.getValue().close();
+                    f.getKey().delete();
+                }
+                catch( IOException e )
+                {
+                    LOG.warn( "failed to close the random accessfile {}", f.getKey() );
+                }
             }
+            
+            tempBufMap.clear();
         }
     }
 }