package ncku.hpds.hadoop.fedhdfs;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.HashMap;

public class LogicalVolumeManager {

	private static HashMap<String, HashMap<String, String>> LogicalMappingTable = new HashMap<String, HashMap<String, String>>();

	// private static HashMap<String, String> innerMap;

	public void addElementsToLTable(String logicalName, String hostName,
			String path) {

		if (LogicalMappingTable.containsKey(logicalName)) {

			while (LogicalMappingTable.get(logicalName).containsKey(hostName)) {
				LogicalMappingTable.get(logicalName).remove(hostName);
				break;
			}
			LogicalMappingTable.get(logicalName).put(hostName, path);

		} else {

			LogicalMappingTable.put(logicalName, new HashMap<String, String>());

			while (LogicalMappingTable.get(logicalName).containsKey(hostName)) {
				LogicalMappingTable.get(logicalName).remove(hostName);
				break;
			}
			LogicalMappingTable.get(logicalName).put(hostName, path);
		}

		/*
		 * if (innerMap == null){ innerMap = new HashMap<String, String>();
		 * LogicalMappingTable.put(logicalName, innerMap); }
		 * 
		 * LogicalMappingTable.get(logicalName).put("c02",
		 * "/user/hpds/Directory1");
		 * 
		 * innerMap.put(hostName, path); innerMap.put("c34",
		 * "/user/hpds/Folder2/file.txt"); innerMap.put("c33",
		 * "/user/hpds/Folder1"); innerMap.put("c02", "/user/hpds/Directory1");
		 * innerMap.put("c09", "/user/hpds/Folder/Directory1");
		 * innerMap.put("c13", "/user/hpds/Folder2/Directory2");
		 * innerMap.put("c19", "/user/hpds/Folder3/Directory4");
		 */

	}

	public void showHashTable() {
		System.out.println(LogicalMappingTable.entrySet());
	}

	public void hashTableDownload() {
		try {
			// ----------------------------declare for
			// writing--------------------------
			// We don't need "DataOutputStream"
			File fout = new File("FedFSImage/LHashTable.txt");
			FileOutputStream fos = new FileOutputStream(fout);
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
			// ----------------------------write into
			// file.txt---------------------------
			for (String key : LogicalMappingTable.keySet()) {
				bw.write(key + " --> " + LogicalMappingTable.get(key) + "\n");
			}
			// bw.write(PhysicalMappingTable.entrySet().toString());
			bw.flush();
		} catch (FileNotFoundException ex) {
			ex.printStackTrace();
		} catch (IOException ex) {
			ex.printStackTrace();
		} catch (Exception e) {// Catch exception if any
			System.err.println("Error: " + e.getMessage());
		}

		System.out.println("\n[INFO] Logical Table download successful .");
	}

}
@@ -239,16 +239,66 @@
 HashMap;
+%0Aimport java.util.Set;%0Aimport java.util.Map.Entry;
 %0A%0Apublic
@@ -460,62 +460,8 @@
 );%0A%0A
-%09// private static HashMap%3CString, String%3E innerMap;%0A%0A
 %09pub
@@ -1112,601 +1112,980 @@
 %7D%0A%0A%09
-%09/*%0A%09%09 * if (innerMap == null)%7B innerMap = new HashMap%3CString, String%3E();%0A%09%09 * LogicalMappingTable.put(logicalName, innerMap); %7D%0A%09%09 * %0A%09%09 * LogicalMappingTable.get(logicalName).put(%22c02%22,%0A%09%09 * %22/user/hpds/Directory1%22);%0A%09%09 * %0A%09%09 * innerMap.put(
+%7D%0A%0A%09public void checkAlogicalFile(String logicalFile) %7B%0A%09%09System.out.println(LogicalMappingTable.containsKey(logicalFile));%0A%09%7D%0A%0A%09public boolean DcheckAlogicalFile(String logicalFile) %7B%0A%09%09return LogicalMappingTable.containsKey(logicalFile);%0A%09%7D%0A%0A%09public void checkAfedcluster(String logicalFile, String hostname) %7B%0A%09%09System.out.println(LogicalMappingTable.get(logicalFile).containsKey(%0A%09%09%09%09hostname));%0A%09%7D%0A%0A%09public boolean DcheckAfedcluster(String logicalFile, String 
 host
-Name, path); innerMap.put(%22c34%22,%0A%09%09 * %22/user/hpds/Folder2/file.txt%22); innerMap.put(%22c33%22,%0A%09%09 * %22/user/hpds/Folder1%22); innerMap.put(%22c02%22, %22/user/hpds/Directory1%22);%0A%09%09 * innerMap.put(%22c09%22, %22/user/hpds/Folder/Directory1%22);%0A%09%09 * innerMap.put(%22c13%22, %22/user/hpds/Folder2/Directory2%22);%0A%09%09 * innerMap.put(%22c19%22, %22/user/hpds/Folder3/Directory4%22);%0A%09%09 */%0A
+name) %7B%0A%09%09return LogicalMappingTable.get(logicalFile).containsKey(hostname);%0A%09%7D%0A%0A%09public boolean checkAfedPhysicalPath(String logicalFile, String physicalPath) %7B%0A%09%09return LogicalMappingTable.get(logicalFile).containsValue(physicalPath);%0A%09%7D%0A%0A%09public void showAfedPhysicalPath(String logicalFile, String hostname) %7B%0A%09%09System.out.println(LogicalMappingTable.get(logicalFile).get(hostname));%0A%09%7D%0A%0A%09public Set%3CEntry%3CString, HashMap%3CString, String%3E%3E%3E DShowHashTable() %7B%0A%09%09return LogicalMappingTable.entrySet();
 %0A%09%7D%0A

add some methods.