--- /dev/null
+/*\r
+Usage :\r
+ ./FileSystem.bin <num thread> <datafile prefix>\r
+*/\r
+\r
+\r
+\r
+public class FileSystem {\r
+ HashMap dir; // Directory \r
+ HashMap fs; // File system\r
+ LinkedList dir_list;\r
+ String inputfile;\r
+ int mid;\r
+ \r
+ public FileSystem(HashMap dir, HashMap fs, LinkedList dir_list, String filename) {\r
+ this.dir = dir;\r
+ this.fs = fs;\r
+ this.dir_list = dir_list;\r
+ this.inputfile = new String("../data/"+filename + "0");\r
+ }\r
+ \r
+ public void init() {\r
+ fillHashTable();\r
+ }\r
+ \r
+ public void fillHashTable() {\r
+ String path;\r
+ LinkedList list; \r
+\r
+ path = new String("/home/"); // root is 'home'\r
+ list = new LinkedList();\r
+\r
+ dir.put(path, list);\r
+ dir_list.add(path);\r
+ }\r
+ \r
+ public static void fillTodoList(String file, LinkedList todoList) {\r
+ FileInputStream fis;\r
+ String comm;\r
+ char c;\r
+ String key;\r
+ String val;\r
+ Transaction t;\r
+\r
+ fis = new FileInputStream(file);\r
+\r
+ while ((comm = fis.readLine()) != null) { // 'command' 'path'\r
+ c = comm.charAt(0); // ex) w /home/abc.c \r
+ key = comm.subString(2);\r
+ t = new Transaction(c, key);\r
+ todoList.add(t);\r
+ }\r
+ }\r
+\r
+ public void execute() {\r
+ Transaction t;\r
+\r
+ char command;\r
+ String key;\r
+ String val;\r
+ boolean isDir;\r
+\r
+ int index;\r
+\r
+ LinkedList todoList = new LinkedList();\r
+ fillTodoList(inputfile, todoList);\r
+\r
+ while (!todoList.isEmpty()) {\r
+ t = (Transaction)(todoList.removeFirst());\r
+\r
+ command = t.getCommand();\r
+ key = t.getKey();\r
+\r
+ index = key.lastindexOf('/');\r
+ if (index+1 == key.length()) \r
+ isDir = true;\r
+ else \r
+ isDir = false;\r
+ \r
+ if (command == 'r') {\r
+ System.out.println("["+command+"] ["+key+"]");\r
+ if (isDir == true) {\r
+ readDirectory(key);\r
+ }\r
+ else {\r
+ readFile(key);\r
+ }\r
+ }\r
+ else if (command == 'c') {\r
+ System.out.println("["+command+"] ["+key+"]");\r
+ if (isDir == true) {\r
+ createDirectory(key);\r
+ }\r
+ else {\r
+ val = t.getValue();\r
+ val = new String(val);\r
+ createFile(key, val);\r
+ }\r
+ }\r
+ }\r
+\r
+ output();\r
+\r
+// RecoveryStat.printRecoveryStat();\r
+ }\r
+\r
+ public void output() { \r
+ Iterator iter;\r
+ String str;\r
+\r
+ iter = dir_list.iterator();\r
+\r
+ while (iter.hasNext()) {\r
+ str = (String)(iter.next());\r
+ System.printString(str + "\n");\r
+ }\r
+ }\r
+\r
+ public void readFile(String key) {\r
+ String val;\r
+\r
+ val = (String)(fs.get(key));\r
+ if (val != null) {\r
+// System.out.println("<"+val+">");\r
+ }\r
+ else {\r
+ System.out.println("No such file or directory");\r
+ }\r
+ }\r
+\r
+ public void readDirectory(String key) {\r
+ LinkedList list;\r
+ Iterator iter;\r
+ String val;\r
+\r
+ list = (LinkedList)(dir.get(key));\r
+\r
+ if (list != null) {\r
+ iter = list.iterator();\r
+ while (iter.hasNext() == true) {\r
+ val = (String)(iter.next());\r
+// System.out.print("["+val+"] ");\r
+ }\r
+// System.out.println("");\r
+ }\r
+ else {\r
+ System.out.println("No such file or directory");\r
+ }\r
+ }\r
+\r
+ public void createFile(String key, String val) {\r
+ String path;\r
+ String target;\r
+ int index;\r
+ LinkedList list;\r
+\r
+ index = key.lastindexOf('/');\r
+ path = key.subString(0, index+1);\r
+ target = key.subString(index+1);\r
+\r
+ if (dir.containsKey(path)) {\r
+ list = (LinkedList)(dir.get(path));\r
+ list.push(target);\r
+ dir.put(path, list);\r
+ fs.put(key, val);\r
+ }\r
+ else {\r
+ System.out.println("Cannot create file");\r
+ }\r
+ }\r
+\r
+ public void createDirectory(String key) {\r
+ int index;\r
+ String path;\r
+ String target;\r
+ LinkedList list;\r
+\r
+ index = key.lastindexOf('/', key.length()-2);\r
+\r
+ if (index != -1) {\r
+ path = key.subString(0, index+1);\r
+ target = key.subString(index+1);\r
+\r
+ if (dir.containsKey(path)) {\r
+ list = (LinkedList)(dir.get(path));\r
+ list.push(target);\r
+ dir.put(path, list);\r
+\r
+ list = new LinkedList();\r
+ dir.put(key, list);\r
+ dir_list.add(key);\r
+ }\r
+ else {\r
+ System.out.println("Cannot create directory");\r
+ }\r
+ }\r
+ }\r
+ \r
+ public Object read(HashMap mydhmap, String key) {\r
+ Object obj = mydhmap.get(key); \r
+ \r
+ return obj;\r
+ }\r
+ \r
+ public static void main(String[] args) {\r
+ String filename;\r
+\r
+ if (args.length == 1) {\r
+ filename = args[0];\r
+ }\r
+ else {\r
+ System.out.println("./FileSystem.bin <data>");\r
+ System.exit(0);\r
+ }\r
+ \r
+ FileSystem file;\r
+\r
+ HashMap fs = new HashMap(500, 0.75f); // file system\r
+ HashMap dir = new HashMap(500, 0.75f); // directory\r
+ LinkedList dir_list = new LinkedList();\r
+ \r
+ file = new FileSystem(dir, fs, dir_list, filename);\r
+ file.init();\r
+\r
+ file.execute();\r
+ \r
+ System.printString("Finished\n");\r
+ }\r
+}\r
+\r
+public class Transaction { // object for todoList\r
+ char command; // r: read, w: write\r
+ String key;\r
+ String val;\r
+ \r
+ Transaction (char c, String key) {\r
+ command = c;\r
+ \r
+ this.key = new String(key);\r
+ this.val = new String();\r
+ }\r
+ \r
+ Transaction (char c, String key, String val) {\r
+ command = c;\r
+ \r
+ this.key = new String(key);\r
+ this.val = new String(val);\r
+ }\r
+ \r
+ public char getCommand() {\r
+ return command;\r
+ }\r
+ \r
+ public String getKey() {\r
+ return key;\r
+ }\r
+ \r
+ public String getValue() {\r
+ return val;\r
+ }\r
+}\r