From: <fi...@so...> - 2004-06-08 00:48:57
|
hi these are the changes I made in InterfaceManager to track managed instances and make sure of unique identity. it is a useful spec of jdo Comparing: C:\Program Files\xorm-beta6\src\org\xorm\InterfaceManager.java To: C:\Progetti\xorm-beta6\src\org\xorm\InterfaceManager.java ==== ==== 27 <! import java.util.HashMap;//sj777 used to track managed instances 64 <! import java.lang.ref.SoftReference; //sj777 used to track managed instances 65 <! import java.lang.ref.Reference;//sj777 used to track managed instances 94 <! //sj777 added class 95 <! //sj777 this class is used to mantain a soft reference to 96 <! //sj777 all InterfaceInvocationHandler(IIH) managed by this 97 <! //sj777 InterfaceManager 98 <! //sj777 I think it is cleaner than efficient but 99 <! //sj777 for the moment it is not bad 100 <! //sj777 objects must be retrieved by using the same syntax 101 <! //sj777 that was used using the TransactionImpl.getMethod 102 <! //sj777 so the keys are the mapped class of the object 103 <! //sj777 and the primarykey of the IIH 104 <! //sj777 the objects are referenced through a map of maps 105 <! //sj777 there are some simple optimizations we can do like 106 <! //sj777 1. add a buffer that olds the last used mapped class key 107 <! //sj777 and its associated class, this would be very cheap 108 <! //sj777 to implement and can prove useful expecially inside queries 109 <! //sj777 2. add a method to retrieve more than one object... 110 <! //sj777 of the same class if it sort the list and 111 <! //sj777 it can make more efficient check and request 112 <! //sj777 to load the data from datastore by primarykey 113 <! //sj777 ?? should I move this to a stand alone class? 114 <! //sj777 ?? is this much different for your row factory wide cache? 115 <! //sj777 ?? with your implementation of factory cache can 2 instances 116 <! //sj777 of IIH with the same jdo identinty, and so perhaps the same row 117 <! //sj777 interfere ? 118 <! */ 119 <! 120 <! 121 <! private class DoubleKeyWeakMap{//sj777 122 <! //sj777 TODO can we know a value to init Map size from InterfaceManagerFactory,//sj777 123 <! //sj777 in ex: how many mappings... i'll see//sj777 124 <! Map primaryMap;//sj777 125 <! 126 <! public DoubleKeyWeakMap(){//sj777 127 <! primaryMap=new HashMap();//sj777 128 <! 129 <! } 130 <! public DoubleKeyWeakMap(Map _primaryMap){//sj777 131 <! primaryMap =_primaryMap; 132 <! 133 <! } 134 <! //sj777 addedmethod 135 <! public synchronized Object get(Object key1,Object key2){ 136 <! 137 <! Map wm = (HashMap)primaryMap.get(key1); 138 <! if (wm!=null){ 139 <! Reference r =(Reference )wm.get(key2); 140 <! if (r!=null) return r.get(); 141 <! } 142 <! return null; 143 <! }; 144 <! public synchronized Object getProxy(Object key1,Object key2){ 145 <! Map wm = (HashMap)primaryMap.get(key1); 146 <! if (wm!=null) synchronized(wm){ 147 <! boolean myflag =wm.containsKey(key2); 148 <! 149 <! Iterator i =wm.keySet().iterator(); 150 <! while (i.hasNext()){ 151 <! Object ks=i.next(); 152 <! 153 <! } 154 <! Reference r =(Reference )wm.get(key2); 155 <! if (r!=null) { 156 <! Object o=r.get(); 157 <! if(o!=null&&o instanceof InterfaceInvocationHandler) 158 <! return ((InterfaceInvocationHandler)o).getProxy(); 159 <! }}return null; 160 <! }; 161 <! 162 <! public synchronized void remove(Object key1,Object key2){ 163 <! Map wm = (HashMap)primaryMap.get(key1); 164 <! if (wm!=null) wm.remove(key2); 165 <! 166 <! 167 <! }; 168 <! //sj777 169 <! public synchronized void put(Object key1,Object key2,Object value){ 170 <! Map wm = (Map)primaryMap.get(key1); 171 <! if (wm==null) { 172 <! wm= new HashMap(); 173 <! primaryMap.put(key1,wm); 174 <! } 175 <! wm.put(key2,new SoftReference(value)); 176 <! } 177 <! } 178 <! 179 <! private DoubleKeyWeakMap managedInstances; 180 <! 181 <! /** 194 <! managedInstances=new InterfaceManager.DoubleKeyWeakMap();//sj777 start to take track of managed instances 291 <! //TransactionImpl txn = (TransactionImpl) currentTransaction();//sj777 !> TransactionImpl txn = (TransactionImpl) currentTransaction(); 293 <! //Object proxy = txn.get(classMapping.getMappedClass(), primaryKey);//sj777 old line 294 <! Object proxy =managedInstances.getProxy(classMapping.getMappedClass(),primaryKey); 295 <! !> Object proxy = txn.get(classMapping.getMappedClass(), primaryKey); 305 <! //proxy = rowToProxy(classMapping, row);//sj777 old 306 <! //sj777 InterfaceInvocationHandler will automatically add itself to this interfacemanager, 307 <! //sj777 todo but not to the transaction if there is an active one 308 <! //sj777 the problem is that 309 <! InterfaceInvocationHandler handler = new InterfaceInvocationHandler(factory, classMapping, row,this);//sj777 310 <! proxy = handler.newProxy();//sj777 I hope that the state of the object can be hollow... jdo spec seem complex !> proxy = rowToProxy(classMapping, row); 316 <! * sj777 no more use for this... problems? 485 <! * sj777 : and add its to the instances managed by this InterfaceManager 486 <! * sj777: and gives an identity to it and do love it forever too 490 <! //if (!currentTransaction().isActive()) {//sj777 old 491 <! if (currentTransaction()==null||!currentTransaction().isActive()) {//sj777 new !> if (!currentTransaction().isActive()) { 496 <! //sj777 todo jdo specs says that there are 2 types of 497 <! //sj777 persitent new objects : 498 <! //sj777 1)persistent new 499 <! //sj777 2)persistent new provisionally... i think this is the right 500 <! //sj777 place to mark that distinction, any suggestion? 501 <! 508 <! if (mgr == this) return; // already managed//sj777 ?? are you sure !> if (mgr == this) return; // already managed 614 <! if (!txn.contains(object)) {//sj777 TODO if it's not transactional.. this line is wrong !> if (!txn.contains(object)) { 618 <! ///ObjectState handler = getObjectState(object);//sj777 old 619 <! InterfaceInvocationHandler handler =//sj777 620 <! InterfaceInvocationHandler.getHandler(object);//sj777 !> ObjectState handler = getObjectState(object); 622 <! stopManaging(handler);//sj777 setStatus i'd like that the handler itself could ask 623 <! //sj777 to be not managed but it actually the status of 624 <! //sj777 an handler is modified directly by other classes 625 <! //sj777 i think we must refactor 626 <! 1103 <! } 1104 <! 1105 <! //sj777 1106 <! void stopManaging(InterfaceInvocationHandler handler){ 1107 <! managedInstances.remove(handler.getClassMapping().getMappedClass(),handler.g etObjectId()); 1108 <! //sj777 todo make sure that the handler clears its reference to the interfacemanager 1109 <! } 1110 <! //sj777 todo doc 1111 <! void manage(InterfaceInvocationHandler obj){ 1112 <! managedInstances.put(obj.getClassMapping().getMappedClass(),obj.getObjectId( ),obj); 1113 <! //sj777 todo make sure that the handler sets its reference to the interfacemanager |