1 23 24 package org.objectweb.medor.eval.prefetch.lib; 25 26 import java.util.ArrayList ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 30 import org.objectweb.medor.api.MedorException; 31 import org.objectweb.medor.eval.prefetch.api.PrefetchBuffer; 32 import org.objectweb.medor.eval.prefetch.api.PrefetchBufferFactory; 33 import org.objectweb.medor.eval.prefetch.api.PrefetchCache; 34 import org.objectweb.medor.tuple.api.Tuple; 35 import org.objectweb.medor.tuple.api.TupleCollection; 36 import org.objectweb.util.monolog.api.BasicLevel; 37 import org.objectweb.util.monolog.api.Logger; 38 39 42 public class PrefetchCacheImpl implements PrefetchCache { 43 private HashMap txContexts = new HashMap (); 44 45 Logger logger; 46 47 public PrefetchCacheImpl() { 48 } 49 50 public PrefetchCacheImpl(Logger logger) { 51 this.logger = logger; 52 } 53 54 public Logger getLogger() { 55 return logger; 56 } 57 58 public void setLogger(Logger logger) { 59 this.logger = logger; 60 } 61 62 64 public Tuple getPrefetchTuple(Object index, Object cat, Object ctxt) 65 throws MedorException { 66 if (ctxt == null) { 67 throw new MedorException("Cannot search a prefetch tuple without a transaction context!"); 68 } 69 if (cat == null) { 70 throw new MedorException("Cannot search a prefetch tuple without an object category!"); 71 } 72 HashMap catmap = (HashMap ) txContexts.get(ctxt); 73 if (catmap == null) { 74 return null; 75 } 76 ArrayList pblist = (ArrayList ) catmap.get(cat); 77 if (pblist == null) { 78 return null; 79 } 80 Tuple res = null, tmp; 81 for(int i=0; i<pblist.size(); i++) { 82 PrefetchBuffer pb = (PrefetchBuffer) pblist.get(i); 83 tmp = pb.getTuple(index); 84 if (res == null && tmp != null) { 85 res = tmp; 86 } 87 } 88 if (logger != null && logger.isLoggable(BasicLevel.DEBUG)) { 89 logger.log(BasicLevel.DEBUG, "getPrefetchTupleCollection: " 90 + "\n\tcategory=" + cat 91 + "\n\tcontext=" + ctxt 92 + "\n\tindex=" + index 93 + "\n\t==>" + printTuple(res)); 94 } 95 return res; 96 } 97 98 public TupleCollection getPrefetchTupleCollection(Object index, Object cat, Object ctxt) 99 throws MedorException { 100 if (ctxt == null) { 101 throw new MedorException("Cannot search a prefetch tuple without a transaction context!"); 102 } 103 if (cat == null) { 104 throw new MedorException("Cannot search a prefetch tuple without an object category!"); 105 } 106 HashMap catmap = (HashMap ) txContexts.get(ctxt); 107 if (catmap == null) { 108 return null; 109 } 110 ArrayList pblist = (ArrayList ) catmap.get(cat); 111 if (pblist == null) { 112 return null; 113 } 114 TupleCollection res = null, tmp; 115 for(int i=0; i<pblist.size(); i++) { 116 PrefetchBuffer pb = (PrefetchBuffer) pblist.get(i); 117 tmp = pb.getTupleCollection(index); 118 if (res == null && tmp != null) { 119 res = tmp; 120 } 121 } 122 if (logger != null && logger.isLoggable(BasicLevel.DEBUG)) { 123 logger.log(BasicLevel.DEBUG, "getPrefetchTupleCollection: " 124 + "\n\tcategory=" + cat 125 + "\n\tcontext=" + ctxt 126 + "\n\tindex=" + index 127 + "\n\t==>" + printTuple(res.getTuple())); 128 } 129 return res; 130 } 131 132 public void invalidatePrefetchBuffer(Object ctxt) { 133 HashMap catmap = (HashMap ) txContexts.get(ctxt); 134 if (catmap == null) { 135 if (logger != null && logger.isLoggable(BasicLevel.INFO)) { 136 logger.log(BasicLevel.INFO, "Invalidation of an unregistered context: " + ctxt); 137 } 138 return; 139 } 140 if (logger != null && logger.isLoggable(BasicLevel.INFO)) { 141 logger.log(BasicLevel.INFO, "Invalidation of the context: " + ctxt); 142 } 143 txContexts.remove(ctxt); 144 Iterator it = catmap.values().iterator(); 145 while (it.hasNext()) { 146 ArrayList pblist = (ArrayList ) it.next(); 147 Iterator it2 = pblist.iterator(); 148 while (it2.hasNext()) { 149 try { 150 ((PrefetchBuffer) it2.next()).close(); 151 } catch (MedorException e) { 152 } 153 } 154 } 155 } 156 157 public PrefetchBuffer createPrefetchBuffer(PrefetchBufferFactory pbf, Object cat, Object ctxt, int indexpos, boolean register) 158 throws MedorException { 159 return createPrefetchBuffer(null, cat, ctxt, indexpos, false, register); 160 } 161 162 public PrefetchBuffer createPrefetchBuffer(PrefetchBufferFactory pbf, Object cat, Object ctxt, int indexpos, boolean multithread, boolean register) 163 throws MedorException { 164 PrefetchBuffer res = new PrefetchBufferImpl(indexpos, multithread, logger); 165 if (register) { 166 registerPrefetchBuffer(res, cat, ctxt); 167 } 168 return res; 169 } 170 171 public boolean registerPrefetchBuffer(PrefetchBuffer pb, Object cat, Object ctxt) 172 throws MedorException { 173 if (ctxt == null) { 174 return false; 175 } 176 if (cat == null) { 177 throw new MedorException("Cannot register a prefetch buffer without an object category!"); 178 } 179 HashMap catmap = (HashMap ) txContexts.get(ctxt); 180 if (catmap == null) { 181 synchronized(txContexts) { 182 catmap = (HashMap ) txContexts.get(ctxt); 183 if (catmap == null) { 184 catmap = new HashMap (); 185 txContexts.put(ctxt, catmap); 186 if (logger != null && logger.isLoggable(BasicLevel.DEBUG)) { 187 logger.log(BasicLevel.DEBUG, "create a map for the context"); 188 } 189 190 } 191 } 192 } 193 ArrayList pblist = (ArrayList ) catmap.get(cat); 194 if (pblist == null) { 195 synchronized(catmap) { 196 pblist = (ArrayList ) catmap.get(cat); 197 if (pblist == null) { 198 pblist = new ArrayList (); 199 catmap.put(cat, pblist); 200 if (logger != null && logger.isLoggable(BasicLevel.DEBUG)) { 201 logger.log(BasicLevel.DEBUG, "create a list of PrefetchBuffer"); 202 } 203 } 204 } 205 } 206 pblist.add(pb); 207 if (logger != null && logger.isLoggable(BasicLevel.DEBUG)) { 208 logger.log(BasicLevel.DEBUG, "PrefetchBuffer created and added: " + pb); 209 } 210 return true; 211 } 212 213 public static String printTuple(Tuple t) { 214 if (t == null) { 215 return null; 216 } 217 StringBuffer sb = new StringBuffer (); 218 String sep = "("; 219 int size = t.getSize(); 220 for(int i=1; i<=size; i++) { 221 sb.append(sep); 222 sep = ", "; 223 sb.append(i); 224 sb.append(": "); 225 try { 226 sb.append(t.getObject(i)); 227 } catch (MedorException e) { 228 sb.append("not printable"); 229 } 230 } 231 return sb.toString(); 232 } 233 } 234 | Popular Tags |