Author: toad
Date: 2006-08-30 12:44:52 +0000 (Wed, 30 Aug 2006)
New Revision: 10299

Modified:
   trunk/freenet/src/freenet/store/BerkeleyDBFreenetStore.java
Log:
Automatically truncate if we have a big block of holes at the end of the store 
file.

Modified: trunk/freenet/src/freenet/store/BerkeleyDBFreenetStore.java
===================================================================
--- trunk/freenet/src/freenet/store/BerkeleyDBFreenetStore.java 2006-08-30 
12:32:46 UTC (rev 10298)
+++ trunk/freenet/src/freenet/store/BerkeleyDBFreenetStore.java 2006-08-30 
12:44:52 UTC (rev 10299)
@@ -220,8 +220,14 @@
                                                t.printStackTrace();
                                        }
                                        throw new DatabaseException("Keys in 
database: "+chkBlocksInStore+" but keys in file: "+chkBlocksFromFile);
-                               } else
-                                       checkForHoles(chkBlocksFromFile);
+                               } else {
+                                       long len = 
checkForHoles(chkBlocksFromFile);
+                                       if(len < chkBlocksFromFile) {
+                                               System.err.println("Truncating 
to "+len+" as no non-holes after that point");
+                                               chkStore.setLength(len * 
(dataBlockSize + headerBlockSize));
+                                               chkBlocksInStore = len;
+                                       }
+                               }
                        }

                        chkBlocksInStore = Math.max(chkBlocksInStore, 
chkBlocksFromFile);
@@ -243,9 +249,10 @@
                }
        }

-       private void checkForHoles(long blocksInFile) throws DatabaseException {
+       private long checkForHoles(long blocksInFile) throws DatabaseException {
                System.err.println("Checking for holes in database...");
                long holes = 0;
+               long maxPresent = 0;
                for(long i=0;i<blocksInFile;i++) {
                        Long blockNo = new Long(i);
                        DatabaseEntry blockNumEntry = new DatabaseEntry();
@@ -258,11 +265,13 @@
                        if(success.equals(OperationStatus.NOTFOUND)) {
                                addFreeBlock(i, true, "hole found");
                                holes++;
-                       }
+                       } else
+                               maxPresent = i;
                        if(i % 1024 == 0)
                        System.err.println("Checked "+i+" blocks, found 
"+holes+" holes");
                }
                System.err.println("Checked database, found "+holes+" holes");
+               return maxPresent+1;
        }

        private void maybeShrink(boolean dontCheck, boolean offline) throws 
DatabaseException, IOException {


Reply via email to