+++ /dev/null
-public class DistributedHashMap {
- DistributedHashEntry[] table;
- float loadFactor;
- int secondcapacity;
-
- public DistributedHashMap(int initialCapacity, int secondcapacity, float loadFactor) {
- init(initialCapacity, secondcapacity, loadFactor);
- }
-
- private void init(int initialCapacity, int secondcapacity, float loadFactor) {
- table = global new DistributedHashEntry[initialCapacity];
- this.loadFactor=loadFactor;
- this.secondcapacity=secondcapacity;
- }
-
- private static int hash1(int hashcode, int length) {
- int value=hashcode%length;
- if (value<0)
- return -value;
- else
- return value;
- }
-
- private static int hash2(int hashcode, int length1, int length2) {
- int value=(hashcode*31)%length2;
- if (value<0)
- return -value;
- else
- return value;
- }
-
- void resize(int index) {
- DHashEntry[] oldtable=table[index].array;
- int newCapacity=oldtable.length*2+1;
- DHashEntry [] newtable=global new DHashEntry[newCapacity];
- table[index].array=newtable;
-
- for(int i=0; i<oldtable.length; i++) {
- DHashEntry e=oldtable[i];
- while(e!=null) {
- DHashEntry next=e.next;
- int bin=hash2(e.hashval, table.length, newCapacity);
- e.next=newtable[bin];
- newtable[bin]=e;
- e=next;
- }
- }
- }
-
- Object remove(Object key) {
- int hashcode=key.hashCode();
- int index1=hash1(hashcode, table.length);
- DistributedHashEntry dhe=table[index1];
- if (dhe==null)
- return null;
- int index2=hash2(hashcode, table.length, dhe.array.length);
- DHashEntry ptr=dhe.array[index2];
-
- if (ptr!=null) {
- if (ptr.hashval==hashcode&&ptr.key.equals(key)) {
- dhe.array[index2]=ptr.next;
- dhe.count--;
- return ptr.value;
- }
- while(ptr.next!=null) {
- if (ptr.hashval==hashcode&&ptr.next.key.equals(key)) {
- Object oldvalue=ptr.value;
- ptr.next=ptr.next.next;
- dhe.count--;
- return oldvalue;
- }
- ptr=ptr.next;
- }
- }
- return null;
- }
-
- Object get(Object key) {
- int hashcode=key.hashCode();
- int index1=hash1(hashcode, table.length);
-
- DistributedHashEntry dhe=table[index1];
- if (dhe==null)
- return null;
-
- int index2=hash2(hashcode, table.length, dhe.array.length);
-
- DHashEntry ptr=dhe.array[index2];
-
- while(ptr!=null) {
- if (ptr.hashval==hashcode
- &&ptr.key.equals(key)) {
- return ptr.value;
- }
- ptr=ptr.next;
- }
- return null;
- }
-
- boolean containsKey(Object key) {
- int hashcode=key.hashCode();
- int index1=hash1(hashcode, table.length);
- DistributedHashEntry dhe=table[index1];
- if (dhe==null)
- return false;
- int index2=hash2(hashcode, table.length, dhe.array.length);
- DHashEntry ptr=dhe.array[index2];
-
- while(ptr!=null) {
- if (ptr.hashval==hashcode
- &&ptr.key.equals(key)) {
- return true;
- }
- ptr=ptr.next;
- }
- return false;
- }
-
- Object put(Object key, Object value) {
- int hashcode=key.hashCode();
- int index1=hash1(hashcode, table.length);
- DistributedHashEntry dhe=table[index1];
- if (dhe==null) {
- dhe=global new DistributedHashEntry(secondcapacity);
- table[index1]=dhe;
- }
- int index2=hash2(hashcode, table.length, dhe.array.length);
- DHashEntry ptr=dhe.array[index2];
-
- while(ptr!=null) {
- if (ptr.hashval==hashcode&&ptr.key.equals(key)) {
- Object oldvalue=ptr.value;
- ptr.value=value;
- return oldvalue;
- }
- ptr=ptr.next;
- }
-
- DHashEntry he=global new DHashEntry();
- he.value=value;
- he.key=key;
- he.hashval=hashcode;
- he.next=dhe.array[index2];
- dhe.array[index2]=he;
-
- dhe.count++;
- if (dhe.count>(loadFactor*dhe.array.length)) {
- //Resize the table
- resize(index1);
- }
- return null;
- }
-}
-
-
-class DistributedHashEntry {
- public DistributedHashEntry(int capacity) {
- array=global new DHashEntry[capacity];
- }
- int count;
- DHashEntry[] array;
-}
-
-
-class DHashEntry {
- public DHashEntry() {
- }
- int hashval;
- Object key;
- Object value;
- DHashEntry next;
-}
-public class LookUpService extends Thread {
- DistributedHashMap mydhmap;
- /**
- * The thread id involved
- **/
- private int threadid;
- /**
- * The total number of threads
- **/
- private int numthreads;
-
- /**
- * The total number of transactions
- **/
- private int numtrans;
-
- /**
- * The total number of objects created
- **/
- private int nobjs;
-
- /**
- * The probability of initiating a look up
- * the read probability % between 0-99
- **/
- private int rdprob;
-
- /**
- * The number of look up operations
- **/
- private int nLookUp;
-
- public LookUpService() {
- }
-
- public LookUpService(DistributedHashMap dmap, int threadid, int numthreads, int nobjs, int numtrans, int rdprob, int nLookUp) {
- mydhmap = dmap;
- this.threadid = threadid;
- this.numthreads = numthreads;
- this.nobjs = nobjs;
- this.numtrans = numtrans;
- this.rdprob = rdprob;
- this.nLookUp = nLookUp;
- }
-
- public void run() {
- int ntrans;
- atomic {
- ntrans = numtrans;
- }
-
- // Do read/writes
- Random rand = new Random(0);
-
- for (int i = 0; i < ntrans; i++) {
- atomic {
- for(int j = 0; j < nLookUp; j++) {
- int rdwr = rand.nextInt(100);
- int rwkey = rand.nextInt(nobjs);
- Integer key = global new Integer(rwkey);
- if (rdwr < rdprob) {
- Object o3 = mydhmap.get(key); //Read
- } else {
- Integer val = global new Integer(j);
- mydhmap.put(key, val); //Modify
- }
- }
- }
- }
- }
-
- public static void main(String[] args) {
- LookUpService ls = new LookUpService();
- LookUpService.parseCmdLine(args,ls);
-
- int nthreads = ls.numthreads;
- int[] mid = new int[8];
- mid[0] = (128<<24)|(195<<16)|(136<<8)|162;//dc-1
- mid[1] = (128<<24)|(195<<16)|(136<<8)|163;//dc-2
- mid[2] = (128<<24)|(195<<16)|(136<<8)|164;//dc-3
- mid[3] = (128<<24)|(195<<16)|(136<<8)|165;//dc-4
- mid[4] = (128<<24)|(195<<16)|(136<<8)|166;//dc-5
- mid[5] = (128<<24)|(195<<16)|(136<<8)|167;//dc-6
- mid[6] = (128<<24)|(195<<16)|(136<<8)|168;//dc-7
- mid[7] = (128<<24)|(195<<16)|(136<<8)|169;//dc-8
-
- LookUpService[] lus;
- DistributedHashMap dhmap;
-
- atomic {
- dhmap = global new DistributedHashMap(100, 100, 0.75f);
- //Add to the hash map
- for(int i = 0; i < ls.nobjs; i++) {
- Integer key = global new Integer(i);
- Integer val = global new Integer(i*i);
- Object o1 = key;
- Object o2 = val;
- dhmap.put(o1, o2);
- }
- lus = global new LookUpService[nthreads];
- for(int i = 0; i<nthreads; i++) {
- lus[i] = global new LookUpService(dhmap, i, ls.numthreads, ls.nobjs, ls.numtrans, ls.rdprob, ls.nLookUp);
- }
- }
-
- LookUpService tmp;
- /* Start threads */
- for(int i = 0; i<nthreads; i++) {
- atomic {
- tmp = lus[i];
- }
- tmp.start(mid[i]);
- }
-
- /* Join threads */
- for(int i = 0; i<nthreads; i++) {
- atomic {
- tmp = lus[i];
- }
- tmp.join();
- }
-
- System.printString("Finished\n");
- }
-
- /**
- * Parse the command line options.
- **/
- public static void parseCmdLine(String args[], LookUpService lus) {
- int i = 0;
- String arg;
- while(i < args.length && args[i].startsWith("-")) {
- arg = args[i++];
- //check options
- if(arg.equals("-N")) {
- if(i < args.length) {
- lus.numthreads = new Integer(args[i++]).intValue();
- }
- } else if(arg.equals("-nEntry")) {
- if(i < args.length) {
- lus.nobjs = new Integer(args[i++]).intValue();
- }
- } else if (arg.equals("-nTrans")) {
- if(i < args.length) {
- lus.numtrans = new Integer(args[i++]).intValue();
- }
- } else if(arg.equals("-probRead")) {
- if(i < args.length) {
- lus.rdprob = new Integer(args[i++]).intValue();
- }
- } else if(arg.equals("-nLookUp")) {
- if(i < args.length) {
- lus.nLookUp = new Integer(args[i++]).intValue();
- }
- } else if(arg.equals("-h")) {
- lus.usage();
- }
- }
-
- if(lus.nobjs == 0 || lus.numtrans == 0)
- lus.usage();
- }
-
- /**
- * The usage routine which describes the program options.
- **/
- public void usage() {
- System.printString("usage: ./LookUpServiceN.bin master -N <threads> -nEntry <objects in hashmap> -nTrans <number of transactions> -probRead <read probability> -nLookUp <number of lookups>\n");
- System.printString(" -N the number of threads\n");
- System.printString(" -nEntry the number of objects to be inserted into distributed hashmap\n");
- System.printString(" -nTrans the number of transactions to run\n");
- System.printString(" -probRead the probability of read given a transaction\n");
- System.printString(" -nLookUp the number of lookups per transaction\n");
- System.printString(" -h help with usage\n");
- }
-}
+public class LookUpService extends Task {\r
+ DistributedHashMap dir;\r
+ DistributedHashMap fs;\r
+ \r
+ public LookUpService(Queue todoList, DistributedHashMap dir, DistributedHashMap fs) {\r
+ this.todoList = todoList;\r
+ this.dir = dir;\r
+ this.fs = fs;\r
+ }\r
+ \r
+ public void init() {\r
+ fillHashTable();\r
+ fillTodoList();\r
+ }\r
+ \r
+ public void fillHashTable() {\r
+ GlobalString path;\r
+ DistributedLinkedList list; \r
+\r
+ atomic {\r
+ path = global new GlobalString("/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
+ char c;\r
+\r
+ atomic {\r
+ c = 'w';\r
+ for (int i = 0; i < 100; i++) {\r
+ directory = global new GlobalString("/home/folder_"+i+"/");\r
+ str = new String("/home/folder_"+i+"/");\r
+ t = global new Transaction(c, directory);\r
+ todoList.push(t);\r
+ \r
+ for (int j = 0; j < 100; j++) {\r
+ file = global new GlobalString(str+"file_"+j);\r
+ str2 = new String(str+"file_"+j);\r
+ val = global new GlobalString("This is "+str2);\r
+ t = global new Transaction(c, file, val);\r
+ todoList.push(t);\r
+ }\r
+ }\r
+ \r
+ c = 'r';\r
+ directory = global new GlobalString("/home/");\r
+ t = global new Transaction(c, directory);\r
+ todoList.push(t);\r
+\r
+ directory = global new GlobalString("/home/folder_28/");\r
+ t = global new Transaction(c, directory);\r
+ todoList.push(t);\r
+\r
+ file = global new GlobalString("/home/folder_98/file_87");\r
+ t = global new Transaction(c, file);\r
+ todoList.push(t);\r
+ }\r
+ }\r
+ \r
+ public void execute() {\r
+ char command;\r
+ boolean isDir;\r
+ GlobalString gkey;\r
+ GlobalString gval;\r
+ int index;\r
+\r
+ String key;\r
+ String val;\r
+\r
+ atomic {\r
+ command = ((Transaction)myWork).getCommand();\r
+ gkey = ((Transaction)myWork).getKey();\r
+\r
+ key = gkey.toLocalString();\r
+ index = gkey.lastindexOf('/');\r
+ if (index+1 == gkey.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
+ }\r
+ else if (command == 'w') { \r
+ if (isDir == true) {\r
+ System.out.println("["+command+"] ["+key+"]");\r
+ atomic {\r
+ createDirectory(gkey);\r
+ }\r
+ }\r
+ else {\r
+ atomic {\r
+ gval = ((Transaction)myWork).getValue();\r
+ val = gval.toLocalString();\r
+ }\r
+ System.out.println("["+command+"] ["+key+"] ["+val+"]");\r
+ atomic {\r
+ createFile(gkey, gval);\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ public void readFile(GlobalString gkey) {\r
+ GlobalString gval;\r
+\r
+ gval = (GlobalString)(fs.get(gkey));\r
+ if (gval != null) {\r
+ System.out.println("<"+gval.toLocalString()+">");\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
+ }\r
+ System.out.println("");\r
+ }\r
+ else {\r
+ System.out.println("No such file or directory");\r
+ }\r
+ }\r
+\r
+ public void createFile(GlobalString gkey, GlobalString gval) {\r
+ GlobalString path;\r
+ GlobalString target;\r
+ int index;\r
+ DistributedLinkedList list;\r
+\r
+ index = gkey.lastindexOf('/');\r
+ path = gkey.subString(0, index+1);\r
+ target = gkey.subString(index+1);\r
+\r
+ if (dir.containsKey(path)) {\r
+ list = (DistributedLinkedList)(dir.get(path));\r
+ list.push(target);\r
+ dir.put(path, list);\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 path;\r
+ GlobalString target;\r
+ DistributedLinkedList list;\r
+\r
+ index = gkey.lastindexOf('/', gkey.length()-2);\r
+\r
+ if (index != -1) {\r
+ path = gkey.subString(0, index+1);\r
+ target = gkey.subString(index+1);\r
+\r
+ if (dir.containsKey(path)) {\r
+ list = (DistributedLinkedList)(dir.get(path));\r
+ list.push(target);\r
+ dir.put(path, list);\r
+\r
+ list = global new DistributedLinkedList();\r
+ dir.put(gkey, list);\r
+ }\r
+ else {\r
+ System.out.println("Cannot create directory");\r
+ }\r
+ }\r
+ }\r
+ \r
+ public void createFile(GlobalString gkey) {\r
+ }\r
+\r
+ public Object read(DistributedHashMap mydhmap, GlobalString key) {\r
+ Object obj = mydhmap.get(key); \r
+ \r
+ return obj;\r
+ }\r
+ \r
+ public static void main(String[] args) {\r
+ int NUM_THREADS = 3;\r
+\r
+ NUM_THREADS = Integer.parseInt(args[0]);\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
+ \r
+ LookUpService[] lus;\r
+ LookUpService initLus;\r
+\r
+ Work[] works;\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.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
+ }\r
+ }\r
+\r
+ Work tmp;\r
+ /* Start threads */\r
+ for(int i = 0; i < NUM_THREADS; i++) {\r
+ atomic {\r
+ tmp = works[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 = works[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'ead, 'w'rite\r
+ GlobalString key;\r
+ GlobalString val;\r
+ \r
+ Transaction (char c, GlobalString key) {\r
+ command = c;\r
+ \r
+ atomic {\r
+ this.key = global new GlobalString(key);\r
+ }\r
+ }\r
+ \r
+ Transaction (char c, GlobalString key, GlobalString val) {\r
+ command = c;\r
+ \r
+ atomic {\r
+ this.key = global new GlobalString(key);\r
+ this.val = global new GlobalString(val);\r
+ }\r
+ }\r
+ \r
+ public char getCommand() {\r
+ return command;\r
+ }\r
+ \r
+ public GlobalString getKey() {\r
+ return key;\r
+ }\r
+ \r
+ public GlobalString getValue() {\r
+ return val;\r
+ }\r
+}\r