[freenet-cvs] r19735 - branches/saltedhashstore/freenet/src/freenet/store

j16sdiz at freenetproject.org j16sdiz at freenetproject.org
Sun May 4 13:12:37 UTC 2008


Author: j16sdiz
Date: 2008-05-04 13:12:36 +0000 (Sun, 04 May 2008)
New Revision: 19735

Modified:
   branches/saltedhashstore/freenet/src/freenet/store/SaltedHashFreenetStore.java
Log:
shutdown hook


Modified: branches/saltedhashstore/freenet/src/freenet/store/SaltedHashFreenetStore.java
===================================================================
--- branches/saltedhashstore/freenet/src/freenet/store/SaltedHashFreenetStore.java	2008-05-04 13:12:16 UTC (rev 19734)
+++ branches/saltedhashstore/freenet/src/freenet/store/SaltedHashFreenetStore.java	2008-05-04 13:12:36 UTC (rev 19735)
@@ -19,6 +19,7 @@
 import freenet.crypt.Digest;
 import freenet.crypt.SHA1;
 import freenet.keys.KeyVerifyException;
+import freenet.node.SemiOrderedShutdownHook;
 import freenet.support.HexUtil;
 import freenet.support.Logger;
 
@@ -42,12 +43,12 @@
 	private long storeSize;
 
 	public static SaltedHashFreenetStore construct(File baseDir, String name, StoreCallback callback, Random random,
-	        long maxKeys) throws IOException {
-		return new SaltedHashFreenetStore(baseDir, name, callback, random, maxKeys);
+	        long maxKeys, SemiOrderedShutdownHook shutdownHook) throws IOException {
+		return new SaltedHashFreenetStore(baseDir, name, callback, random, maxKeys, shutdownHook);
 	}
 
-	SaltedHashFreenetStore(File baseDir, String name, StoreCallback callback, Random random, long maxKeys)
-	        throws IOException {
+	SaltedHashFreenetStore(File baseDir, String name, StoreCallback callback, Random random, long maxKeys,
+	        SemiOrderedShutdownHook shutdownHook) throws IOException {
 		logMINOR = Logger.shouldLog(Logger.MINOR, this);
 		logDEBUG = Logger.shouldLog(Logger.DEBUG, this);
 
@@ -76,6 +77,7 @@
 		openStoreFiles(baseDir, name);
 
 		callback.setStore(this);
+		shutdownHook.addEarlyJob(new Thread(new ShutdownDB()));
 	}
 
 	public StorableBlock fetch(byte[] routingKey, byte[] fullKey, boolean dontPromote) throws IOException {
@@ -420,7 +422,7 @@
 			try {
 				storeFC[i].force(true);
 			} catch (Exception e) {
-				// TODO log this
+				Logger.normal(this, "error flushing store file", e);
 			}
 		}
 	}
@@ -520,6 +522,17 @@
 		return (bf.getLong(0x30) & ENTRY_FLAG_OCCUPIED) == 0;
 	}
 
+	private void flushAndClose() {
+		for (int i = 0; i < FILE_SPLIT; i++) {
+			try {
+				storeFC[i].force(true);
+				storeFC[i].close();
+			} catch (Exception e) {
+				Logger.error(this, "error flusing store", e);
+			}
+		}
+	}
+
 	// ------------- Configuration
 	/**
 	 * Configuration File
@@ -682,6 +695,20 @@
 		}
 	}
 
+	public class ShutdownDB implements Runnable {
+		public void run() {
+			shutdown = true;
+			lockGlobal(10 * 1000); // 10 seconds
+			flushAndClose();
+
+			try {
+				writeConfigFile();
+			} catch (IOException e) {
+				Logger.error(this, "error writing store config", e);
+			}
+		}
+	}
+
 	// ------------- Hashing
 	/**
 	 * <tt>0x10</tt> bytes of salt for better digestion, not too salty.




More information about the cvs mailing list