-public class LookUpService extends Task {\r
+public class LookUpService extends Thread {\r
DistributedHashMap dir;\r
DistributedHashMap fs;\r
- GlobalString error[]; // String for incorrect path, etc.\r
+ GlobalString inputfile;\r
+ int mid;\r
\r
- public LookUpService(Queue todoList, DistributedHashMap dir, DistributedHashMap fs) {\r
- this.todoList = todoList;\r
+ public LookUpService(DistributedHashMap dir, DistributedHashMap fs) {\r
this.dir = dir;\r
this.fs = fs;\r
}\r
\r
- public void init() {\r
- makeErrorStatement();\r
- fillHashTable();\r
- fillTodoList();\r
+ public LookUpService(DistributedHashMap dir, DistributedHashMap fs, String filename, int mid) {\r
+ this.dir = dir;\r
+ this.fs = fs;\r
+ this.mid = mid;\r
+ this.inputfile = global new GlobalString(filename + "_" + mid);\r
}\r
\r
- public void makeErrorStatement() {\r
- int num = 4;\r
- int i = 0;\r
-\r
- atomic {\r
- error = global new GlobalString[num];\r
-\r
- error[i++] = global new GlobalString("/hkhang/error/"); // w: no root (hkhang), r: non existed path\r
- error[i++] = global new GlobalString("/home/abc/def/ghi/"); // w: create multiple directories, r: non existed path\r
- error[i++] = global new GlobalString("/home/hkhang/abc"); // w: create directory and file together\r
- error[i++] = global new GlobalString("/home/hkhang/abc/def"); // w: create directory and file together\r
- }\r
+ public void init() {\r
+ fillHashTable();\r
}\r
\r
public void fillHashTable() {\r
DistributedLinkedList list; \r
\r
atomic {\r
- path = global new GlobalString("/home/"); // root is home\r
+ path = global new GlobalString("/home/"); // root is 'home'\r
list = global new DistributedLinkedList();\r
\r
dir.put(path, list);\r
}\r
}\r
\r
- public void fillTodoList() {\r
- GlobalString directory;\r
- GlobalString file;\r
- GlobalString val;\r
- String str;\r
- String str2;\r
- Transaction t;\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
- atomic {\r
- c = 'w';\r
-\r
- directory = global new GlobalString("/home/folder/");\r
- str = new String("/home/folder/");\r
- t = global new Transaction(c, directory);\r
- todoList.push(t);\r
-\r
- for (int i = 0; i < 1000; i++) {\r
- file = global new GlobalString(str+"file_"+i);\r
- str2 = new String(str+"file_"+i);\r
- val = global new GlobalString("This is "+str2);\r
- t = global new Transaction(c, file, val);\r
- todoList.push(t);\r
- }\r
- }\r
-\r
- int rdprob = 93;\r
- int wrtprob = 98;\r
- int dirprob = 90;\r
- int rdwr;\r
- int isdir;\r
- int findex;\r
- Random rand = new Random(0);\r
+ fis = new FileInputStream(file);\r
\r
- atomic {\r
- for (int i = 0; i < 10000; i++) {\r
- rdwr = rand.nextInt(100);\r
- isdir = rand.nextInt(100);\r
- findex = rand.nextInt(1000);\r
- \r
- if (rdwr < rdprob) { // read\r
- c = 'r';\r
- if (isdir < dirprob) { // file\r
- file = global new GlobalString(str+"file_"+findex);\r
- t = global new Transaction(c, file);\r
- }\r
- else { // dir\r
- directory = global new GlobalString(str);\r
- t = global new Transaction(c, directory);\r
- }\r
- }\r
- else if (rdwr >= rdprob && rdwr < wrtprob) { // write\r
- c = 'w';\r
- if (isdir < dirprob) { // file\r
- file = global new GlobalString(str+"file_"+findex);\r
- str2 = new String(str+"file_"+findex);\r
- val = global new GlobalString(str2+" has been modified!!");\r
- t = global new Transaction(c, file, val);\r
- }\r
- else { // dir\r
- directory = global new GlobalString(str+"new_dir_"+findex+"/");\r
- t = global new Transaction(c, directory);\r
- }\r
- }\r
- else { // error\r
- int err = rand.nextInt(4);\r
- file = error[err];\r
- val = global new GlobalString("It is error path!!");\r
- t = global new Transaction(c, file, val);\r
- }\r
- todoList.push(t);\r
+ while ((comm = fis.readLine()) != null) { // 'command' 'path'\r
+ c = comm.charAt(0); // ex) w /home/abc.c \r
+ key = comm.subString(2);\r
+ if (c == 'c') {\r
+// System.out.println(c + " " + key);\r
+ t = new Transaction(c, key);\r
+// t = new Transaction(c, key, val);\r
+ }\r
+ else {\r
+// System.out.println(c + " " + key);\r
+ t = new Transaction(c, key);\r
}\r
+ todoList.add(t);\r
}\r
}\r
- \r
- public void execute() {\r
+\r
+ public void run() {\r
+ Transaction t;\r
+\r
char command;\r
+ String key;\r
+ String val;\r
boolean isDir;\r
+\r
GlobalString gkey;\r
GlobalString gval;\r
+\r
int index;\r
+ String file;\r
+ atomic {\r
+ file = inputfile.toLocalString();\r
+ }\r
\r
- String key;\r
- String val;\r
+ LinkedList todoList = new LinkedList();\r
+ fillTodoList(file, todoList);\r
\r
- atomic {\r
- command = ((Transaction)myWork).getCommand();\r
- gkey = ((Transaction)myWork).getKey();\r
+ while (!todoList.isEmpty()) {\r
+ t = (Transaction)(todoList.removeFirst());\r
\r
- key = gkey.toLocalString();\r
- index = gkey.lastindexOf('/');\r
- if (index+1 == gkey.length()) \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
-\r
- if (command == 'r') { \r
- System.out.println("["+command+"] ["+key+"]");\r
- if (isDir == true) {\r
- atomic {\r
- readDirectory(gkey);\r
- }\r
- }\r
- else {\r
- atomic {\r
- readFile(gkey);\r
- }\r
+ \r
+ atomic {\r
+ gkey = global new GlobalString(key);\r
}\r
- }\r
- else if (command == 'w') { \r
- if (isDir == true) {\r
+\r
+ if (command == 'r') { \r
System.out.println("["+command+"] ["+key+"]");\r
- atomic {\r
- createDirectory(gkey);\r
+ if (isDir == true) {\r
+ atomic {\r
+ readDirectory(gkey);\r
+ }\r
+ }\r
+ else {\r
+ atomic {\r
+ readFile(gkey);\r
+ }\r
}\r
}\r
- else {\r
- atomic {\r
- gval = ((Transaction)myWork).getValue();\r
- val = gval.toLocalString();\r
+ else if (command == 'c') { \r
+ if (isDir == true) {\r
+ System.out.println("["+command+"] ["+key+"]");\r
+ atomic {\r
+ createDirectory(gkey);\r
+ }\r
}\r
- System.out.println("["+command+"] ["+key+"] ["+val+"]");\r
- atomic {\r
- createFile(gkey, gval);\r
+ else {\r
+ val = t.getValue();\r
+ System.out.println("["+command+"] ["+key+"] ["+val+"]");\r
+ atomic {\r
+ gval = global new GlobalString(val);\r
+ createFile(gkey, gval);\r
+ }\r
}\r
}\r
}\r
\r
public static void main(String[] args) {\r
int NUM_THREADS = 3;\r
+ String filename;\r
\r
NUM_THREADS = Integer.parseInt(args[0]);\r
+ filename = args[1];\r
\r
int[] mid = new int[NUM_THREADS];\r
-// mid[0] = (128<<24)|(195<<16)|(180<<8)|21;//dw-2\r
-// mid[0] = (128<<24)|(195<<16)|(180<<8)|24;//dw-5\r
-// mid[1] = (128<<24)|(195<<16)|(180<<8)|26;//dw-7\r
- mid[0] = (128<<24)|(195<<16)|(136<<8)|166;//dc-5\r
- mid[1] = (128<<24)|(195<<16)|(136<<8)|167;//dc-6\r
- mid[2] = (128<<24)|(195<<16)|(136<<8)|168;//dc-7\r
+ mid[0] = (128<<24)|(195<<16)|(180<<8)|21;//dw-2\r
+ mid[1] = (128<<24)|(195<<16)|(180<<8)|24;//dw-5\r
+ mid[2] = (128<<24)|(195<<16)|(180<<8)|26;//dw-7\r
+// mid[0] = (128<<24)|(195<<16)|(136<<8)|165;//dc-4\r
+// mid[1] = (128<<24)|(195<<16)|(136<<8)|166;//dc-5\r
+// mid[2] = (128<<24)|(195<<16)|(136<<8)|167;//dc-6\r
+// mid[3] = (128<<24)|(195<<16)|(136<<8)|168;//dc-7\r
\r
LookUpService[] lus;\r
LookUpService initLus;\r
Transaction[] currentWorkList; // type might be something else\r
\r
atomic {\r
- Queue todoList = global new Queue();\r
- \r
currentWorkList = global new Transaction[NUM_THREADS]; // something else\r
works = global new Work[NUM_THREADS];\r
\r
DistributedHashMap fs = global new DistributedHashMap(500, 500, 0.75f);\r
DistributedHashMap dir = global new DistributedHashMap(500, 500, 0.75f);\r
\r
- initLus = global new LookUpService(todoList, dir, fs);\r
+ initLus = global new LookUpService(dir, fs);\r
initLus.init();\r
\r
lus = global new LookUpService[NUM_THREADS];\r
for(int i = 0; i < NUM_THREADS; i++) {\r
- lus[i] = global new LookUpService(initLus.todoList, initLus.dir, initLus.fs);\r
- works[i] = global new Work(lus[i], NUM_THREADS, i, currentWorkList);\r
+ lus[i] = global new LookUpService(initLus.dir, initLus.fs, filename, i);\r
}\r
}\r
\r
- Work tmp;\r
+ LookUpService tmp;\r
/* Start threads */\r
for(int i = 0; i < NUM_THREADS; i++) {\r
atomic {\r
- tmp = works[i];\r
+ tmp = lus[i];\r
}\r
Thread.myStart(tmp, mid[i]);\r
}\r
/* Join threads */\r
for(int i = 0; i < NUM_THREADS; i++) {\r
atomic {\r
- tmp = works[i];\r
+ tmp = lus[i];\r
}\r
tmp.join();\r
}\r
}\r
\r
public class Transaction { // object for todoList\r
- char command; // 'r'ead, 'w'rite\r
- GlobalString key;\r
- GlobalString val;\r
+ char command; // r: read, w: write\r
+ String key;\r
+ String val;\r
\r
- Transaction (char c, GlobalString key) {\r
+ Transaction (char c, String key) {\r
command = c;\r
\r
- atomic {\r
- this.key = global new GlobalString(key);\r
- }\r
+ this.key = new String(key);\r
+ this.val = new String();\r
}\r
\r
- Transaction (char c, GlobalString key, GlobalString val) {\r
+ Transaction (char c, String key, String val) {\r
command = c;\r
\r
- atomic {\r
- this.key = global new GlobalString(key);\r
- this.val = global new GlobalString(val);\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 GlobalString getKey() {\r
+ public String getKey() {\r
return key;\r
}\r
\r
- public GlobalString getValue() {\r
+ public String getValue() {\r
return val;\r
}\r
}\r