| 1 package com.daffodilwoods.daffodildb.server.datasystem.persistentsystem; 2 3 import java.util.*; 4 5 import com.daffodilwoods.daffodildb.utils.comparator.*; 6 import com.daffodilwoods.database.general.*; 7 import com.daffodilwoods.database.resource.*; 8 9 12 public class UserLock{ 13 14 17 private Object lock = null; 18 private long time ; 19 private Map lockMap; 20 Vector waiters = new Vector(); 21 22 UserLock() { 23 lockMap = Collections.synchronizedMap(new TreeMap(new CRvbmjgjfeDpnqbsbups())); 24 } 25 26 32 33 synchronized final boolean getLock(DatabaseUser user) throws DException { 34 while(true){ 35 if((lock == null || user.equals(lock)) && (lockMap.size() == 0)){ 36 lock = user; 37 time = System.currentTimeMillis(); 38 39 return true; 40 } 41 try { 42 wait(20); 43 } 44 catch (InterruptedException ex) { 45 } 46 if((System.currentTimeMillis() - time) > 3000){ 47 } 48 } 49 } 50 51 final boolean getLock(DatabaseUser user,ArrayList userTableNames) throws DException { 52 while(true){ 53 synchronized (this){ 54 if( lock == null && checkLock(userTableNames) == null ){ 55 for (int i = 0; i < userTableNames.size(); i++){ 56 QualifiedIdentifier tableName = (QualifiedIdentifier)userTableNames.get(i); 57 user.addTable(tableName); 58 Object obj = lockMap.put(tableName,user); 59 time = System.currentTimeMillis(); 60 61 } 62 return true; 63 } 64 } 65 66 wait(user,userTableNames); 67 } 68 } 69 70 74 75 final synchronized void unLock(DatabaseUser user) throws DException{ 76 ArrayList tableNames = user.getTableNames(); 77 if(user.equals(lock)){ 78 lock = null; 79 } 80 for (int i = 0; i < tableNames.size(); i++) { 81 lockMap.remove(tableNames.get(i)); 82 83 } 84 { 85 notify(tableNames); 86 } 87 } 88 89 private QualifiedIdentifier checkLock(ArrayList tableNames){ 90 for (int i = 0,len = tableNames.size() ; i < len; i++) { 91 if((DatabaseUser)lockMap.get(tableNames.get(i)) != null){ 92 return (QualifiedIdentifier)tableNames.get(i); 93 } 94 } 95 return null; 96 } 97 98 private void wait(DatabaseUser user,ArrayList userTableNames) { 99 Wait w = new Wait(); 100 w.user = user; 101 w.list = userTableNames; 102 waiters.add(w); 103 time = System.currentTimeMillis(); 104 do{ 105 w.doWait(); 106 if(System.currentTimeMillis() - time > 3000){ 107 } 108 }while(checkLock(w.list) != null); 109 waiters.remove(w); 110 } 111 112 private void notify(ArrayList userTableNames) { 113 for(int i = 0; i < waiters.size() ; i++) { 114 try { 115 Wait w = (Wait)waiters.get(i); 116 if(checkLock(w.list) == null){ 117 w.signal(); 118 return; 119 } 120 } 121 catch (ArrayIndexOutOfBoundsException ex) { 122 } 123 } 124 } 125 126 private class Wait{ 127 DatabaseUser user; 128 ArrayList list; 129 130 public synchronized void doWait() { 131 try { 132 this.wait(3000); 133 } 134 catch (InterruptedException ex) { 135 } 136 } 137 138 public synchronized void signal() { 139 this.notify(); 140 } 141 } 142 } 143 | Popular Tags |