--- /dev/null
+/*\r
+ System.out.println(key);\r
+Usage :\r
+ ./FileSystem.bin <num thread> <datafile prefix>\r
+*/\r
+\r
+public class FileSystem extends Thread {\r
+ DistributedHashMap dir; // Directory \r
+ DistributedHashMap fs; // File \r
+ DistributedLinkedList dir_list;\r
+ GlobalString inputfile;\r
+ int mid;\r
+ int threadid;\r
+\r
+ public FileSystem(DistributedHashMap dir, DistributedHashMap fs, DistributedLinkedList dir_list) {\r
+ this.dir = dir;\r
+ this.fs = fs;\r
+ this.dir_list = dir_list;\r
+ }\r
+\r
+ public FileSystem(DistributedHashMap dir, DistributedHashMap fs, DistributedLinkedList dir_list, String filename, int mid,int threadid) {\r
+ this.dir = dir;\r
+ this.fs = fs;\r
+ this.dir_list = dir_list;\r
+ this.mid = mid;\r
+ this.threadid = threadid;\r
+ this.inputfile = global new GlobalString("../data/"+filename + mid);\r
+ }\r
+\r
+\r
+ public void setInputFileName(String filename, int mid) {\r
+ this.mid = mid;\r
+ this.inputfile = global new GlobalString("../data/"+filename + mid);\r
+ }\r
+\r
+ public void init() {\r
+ fillHashTable();\r
+ }\r
+\r
+ public void fillHashTable() {\r
+ GlobalString path;\r
+ DistributedLinkedList list; \r
+\r
+ atomic {\r
+ path = global new GlobalString("/tmp/"); // root is 'tmp'\r
+ list = global new DistributedLinkedList();\r
+ dir.put(path, list);\r
+ dir_list.add(path);\r
+ }\r
+ }\r
+\r
+ public static void fillTodoList(String file, LinkedList todoList) {\r
+ FileInputStream fis;\r
+ String comm;\r
+ char c;\r
+ String key;\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 run() {\r
+ Transaction t;\r
+ String file;\r
+ atomic {\r
+ file = inputfile.toLocalString();\r
+ }\r
+ System.out.println("file= " + file);\r
+ LinkedList todoList = new LinkedList();\r
+ fillTodoList(file, todoList);\r
+ long st = System.currentTimeMillis();\r
+ long fi;\r
+ long tot1;\r
+\r
+ if(todoList.isEmpty())\r
+ System.out.println("todoList is Empty\n");\r
+\r
+ while (!todoList.isEmpty()) {\r
+ int count = 5;\r
+ atomic {\r
+ while(count>0 && !todoList.isEmpty()) { //commit 5 transactions\r
+ t = (Transaction)(todoList.removeFirst());\r
+ if(t==null) {\r
+ count--;\r
+ continue;\r
+ }\r
+ char command = t.getCommand();\r
+ String key = t.getKey();\r
+ GlobalString gkey = global new GlobalString(key);\r
+\r
+ int index = key.lastindexOf('/');\r
+ boolean isDir;\r
+ if (index+1 == key.length()) \r
+ isDir = true;\r
+ else \r
+ isDir = false;\r
+\r
+ long st1 = 0L;\r
+ long fi1 = 0L;\r
+ if (command == 'r') {\r
+ st1 = System.currentTimeMillis();\r
+ //System.out.println("["+command+"] ["+key+"]");\r
+ if (isDir == true) {\r
+ readDirectory(gkey);\r
+ }\r
+ else {\r
+ readFile(gkey);\r
+ }\r
+ fi1 = System.currentTimeMillis();\r
+ }\r
+ tot1 += fi1 - st1;\r
+ if (command == 'c') {\r
+ //System.out.println("["+command+"] ["+key+"]");\r
+ if (isDir == true) {\r
+ createDirectory(gkey);\r
+ }\r
+ else {\r
+ String val = t.getValue();\r
+ GlobalString gval = global new GlobalString(val);\r
+ createFile(gkey, gval);\r
+ }\r
+ }\r
+ count--;\r
+ }//end of inside loop\r
+ }//end of atomic\r
+ }\r
+ fi = System.currentTimeMillis();\r
+ RecoveryStat.printRecoveryStat();\r
+\r
+ System.out.println("\n\n\n I'm done - Time Elapse : "+ ((double)(fi-st)/1000) + "\n\n\n");\r
+ System.out.println("\n Reading - Time Elapse : "+ ((double)tot1/1000) + "\n");\r
+ while(true) {\r
+ sleep(100000);\r
+ }\r
+ }\r
+\r
+ public void readFile(GlobalString gkey) {\r
+ GlobalString gval=null;\r
+ String val=null;\r
+\r
+ gval = (GlobalString)(fs.get(gkey));\r
+ if(gval!=null) {\r
+ val = gval.toLocalString();\r
+ //Add some useless extra work for now\r
+ //to increase read time\r
+ int hashVal = gval.hashCode();\r
+ int a=0;\r
+ for(int t=0; t<hashVal; t++) {\r
+ for(int z=0; z<val.hashCode(); z++) {\r
+ a = a + t + z;\r
+ }\r
+ }\r
+ System.out.println("a= " + a);\r
+ }\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(GlobalString gkey) {\r
+ DistributedLinkedList list;\r
+ Iterator iter;\r
+ GlobalString gval;\r
+\r
+ list = (DistributedLinkedList)(dir.get(gkey));\r
+\r
+ if (list != null) {\r
+ iter = list.iterator();\r
+ while (iter.hasNext() == true) {\r
+ gval = (GlobalString)(iter.next());\r
+ //System.out.print("["+gval.toLocalString()+"] ");\r
+ //Add some useless extra work for now\r
+ int hashVal = gval.hashCode();\r
+ int a=0;\r
+ for(int t=0; t<hashVal; t++) {\r
+ a = a + t;\r
+ }\r
+ System.out.println("a= " + a);\r
+ }\r
+ }\r
+ else {\r
+ //System.out.println("No such file or directory");\r
+ }\r
+ }\r
+\r
+ public void createFile(GlobalString gkey, GlobalString gval) {\r
+ String path;\r
+ String target;\r
+ GlobalString gpath;\r
+ GlobalString gtarget;\r
+ int index;\r
+ DistributedLinkedList list;\r
+\r
+ index = gkey.lastindexOf('/');\r
+ gpath = gkey.subString(0, index+1);\r
+ gtarget = gkey.subString(index+1);\r
+ FileOutputStream fos = new FileOutputStream(gpath.toLocalString()+gtarget.toLocalString());\r
+ fos.FileOutputStream(gpath.toLocalString()+gtarget.toLocalString());\r
+ for(int i=0; i<10; i++) {\r
+ byte[] b = new byte[1];\r
+ b[0] = (byte) i;\r
+ fos.write(i);\r
+ fos.flush();\r
+ }\r
+ fos.close();\r
+\r
+ if(dir==null)\r
+ System.out.println("dir is null");\r
+\r
+ if (dir.containsKey(gpath)) {\r
+ fs.put(gkey, gval);\r
+ }\r
+ else {\r
+ System.out.println("Cannot create file");\r
+ }\r
+ }\r
+\r
+ public void createDirectory(GlobalString gkey) {\r
+ int index;\r
+ GlobalString gpath;\r
+ GlobalString gtarget;\r
+ DistributedLinkedList list;\r
+\r
+ index = gkey.lastindexOf('/', gkey.length()-2);\r
+ //System.out.println("index= " + index + " gkey= " + gkey.toLocalString());\r
+\r
+ if (index != -1) {\r
+ gpath = gkey.subString(0, index+1);\r
+ gtarget = gkey.subString(index+1);\r
+ if (dir.containsKey(gpath)) {\r
+ list = global new DistributedLinkedList();\r
+ dir.put(gkey, list);\r
+ }\r
+ else {\r
+ //System.out.println("Cannot create directory- HERE1");\r
+ }\r
+ }\r
+ else {\r
+ //System.out.println("Cannot create directory-- HERE2");\r
+ }\r
+ }\r
+\r
+ public static void main(String[] args) {\r
+ int NUM_THREADS = 3;\r
+ String filename = new String();\r
+\r
+ if (args.length == 2) {\r
+ NUM_THREADS = Integer.parseInt(args[0]);\r
+ filename = args[1];\r
+ System.out.println("filename= " + filename);\r
+ }\r
+ else {\r
+ System.out.println("./FileSystem.bin master <num_thread> <data>");\r
+ System.exit(0);\r
+ }\r
+\r
+ int[] mid = new int[8];\r
+ mid[0] = (128<<24)|(195<<16)|(136<<8)|162;//dc-1\r
+ mid[1] = (128<<24)|(195<<16)|(136<<8)|163;//dc-2\r
+ mid[2] = (128<<24)|(195<<16)|(136<<8)|164;//dc-3\r
+ mid[3] = (128<<24)|(195<<16)|(136<<8)|165;//dc-4\r
+ mid[4] = (128<<24)|(195<<16)|(136<<8)|166;//dc-5\r
+ mid[5] = (128<<24)|(195<<16)|(136<<8)|167;//dc-6\r
+ mid[6] = (128<<24)|(195<<16)|(136<<8)|168;//dc-7\r
+ mid[7] = (128<<24)|(195<<16)|(136<<8)|169;//dc-8\r
+\r
+ FileSystem[] lus;\r
+ FileSystem initLus;\r
+ atomic {\r
+ DistributedHashMap fs = global new DistributedHashMap(500, 0.75f);\r
+ DistributedHashMap dir = global new DistributedHashMap(500, 0.75f);\r
+ DistributedLinkedList dir_list = global new DistributedLinkedList();\r
+ initLus = global new FileSystem(dir, fs, dir_list);\r
+ initLus.init();\r
+\r
+ String filename1 = "../data/creates.txt";\r
+ FileInputStream fis = new FileInputStream(filename1);\r
+ String comm;\r
+ //Create and populate the distributed hash map\r
+ boolean isDir;\r
+ while((comm = fis.readLine()) != null) {\r
+ System.out.println("comm= " + comm);\r
+ char command = comm.charAt(0);\r
+ String key = comm.subString(2);\r
+ GlobalString gkey = global new GlobalString(key);\r
+ int index = key.lastindexOf('/');\r
+ if (index+1 == key.length()) \r
+ isDir = true;\r
+ else \r
+ isDir = false;\r
+ if(command == 'c') {\r
+ if(isDir == true) {\r
+ initLus.createDirectory(gkey);\r
+ } else {\r
+ String val = new String();\r
+ GlobalString gval = global new GlobalString(val);\r
+ initLus.createFile(gkey, gval);\r
+ }\r
+ }\r
+ }\r
+ fis.close();\r
+\r
+ lus = global new FileSystem[NUM_THREADS];\r
+ for(int i = 0; i < NUM_THREADS; i++) {\r
+ lus[i] = global new FileSystem(initLus.dir, initLus.fs, initLus.dir_list, filename, i,mid[i]);\r
+ }\r
+ }\r
+\r
+ FileSystem tmp;\r
+ /* Start threads */\r
+ for(int i = 0; i < NUM_THREADS; i++) {\r
+ atomic {\r
+ tmp = lus[i];\r
+ }\r
+ Thread.myStart(tmp, mid[i]);\r
+ }\r
+\r
+ /* Join threads */\r
+ for(int i = 0; i < NUM_THREADS; i++) {\r
+ atomic {\r
+ tmp = lus[i];\r
+ }\r
+ tmp.join();\r
+ }\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