1 23 24 package org.objectweb.medor.eval.prefetch.lib; 25 26 import org.objectweb.medor.eval.prefetch.api.PrefetchBuffer; 27 import org.objectweb.medor.eval.prefetch.api.PrefetchBufferHolder; 28 import org.objectweb.medor.tuple.api.Tuple; 29 import org.objectweb.medor.tuple.api.TupleCollection; 30 import org.objectweb.medor.api.MedorException; 31 import org.objectweb.util.monolog.api.Logger; 32 import org.objectweb.util.monolog.api.BasicLevel; 33 34 import java.util.HashMap ; 35 import java.util.Map ; 36 37 40 public class PrefetchBufferImpl implements PrefetchBuffer { 41 private int indexPos; 42 private TupleCollection tupleCollection = null; 43 private Map tupleMap = null; 44 private Logger logger; 45 private boolean multithread; 46 private boolean closed; 47 48 PrefetchBufferImpl(int indexpos, boolean multithread, Logger logger) { 49 this.indexPos = indexpos; 50 this.logger = logger; 51 this.multithread = multithread; 52 this.closed = false; 53 } 54 55 57 public void addPrefetchTuple() throws MedorException { 58 TupleCollection localTC = tupleCollection; 59 if (localTC == null) { 60 return; 61 } 62 try { 63 int row = localTC.getRow(); 64 if (row <1) { 65 return; 66 } 67 if (tupleMap == null) { 68 tupleMap = new HashMap (); 69 } 70 Tuple pt = localTC.getTuple(); 71 Object index = pt.getObject(indexPos); 72 if (logger != null && logger.isLoggable(BasicLevel.DEBUG)) { 73 logger.log(BasicLevel.DEBUG, "Add Tuple in PreftechBuffer:" 74 + "\n\tindex=" + index 75 + "\n\trow=" + row); 76 } 77 tupleMap.put(index, new Integer (row)); 78 } catch (MedorException e) { 79 if (logger != null) { 80 logger.log(BasicLevel.WARN, 81 "Error during the adding of a prefetched tuple: ", e); 82 } 83 } 84 } 85 86 public void setTupleCollection(TupleCollection tc) throws MedorException { 87 if (tupleCollection != null) { 88 throw new MedorException("A tuple collection has already been assigned to this PrefetchBuffer!!"); 89 } 90 tupleCollection = tc; 91 } 92 93 public Tuple getTuple(Object index) throws MedorException { 94 Map m = tupleMap; 95 if (m == null) { 96 return null; 97 } 98 Integer i = (Integer ) m.get(index); 99 if (i == null) { 100 return null; 101 } 102 m.remove(index); 103 int row = i.intValue(); 104 int oldrow = -1; 105 Tuple res = null; 106 TupleCollection localTC = tupleCollection; 108 if (localTC == null) 109 return null; 110 try { 111 oldrow = localTC.getRow(); 112 res = localTC.getTuple(row); 113 } catch(MedorException e) { 114 res = null; 117 } finally { 118 if (multithread) { 119 if (tupleCollection != null && oldrow != row) { 122 try { 124 localTC.row(oldrow); 125 } catch (MedorException e) { 126 } 128 } 129 } 130 } 131 if (logger != null && logger.isLoggable(BasicLevel.DEBUG)) { 132 logger.log(BasicLevel.DEBUG, "Tuple found :" 133 + "\n\trow=" + row + "\toldrow=" + oldrow 134 + "\n\tindex=" + index 135 + "\n\ttuple=" + PrefetchCacheImpl.printTuple(res)); 136 } 137 return res; 138 } 139 140 public TupleCollection getTupleCollection(Object index) throws MedorException { 141 Map m = tupleMap; 142 if (m == null) { 143 return null; 144 } 145 Integer i = (Integer ) m.get(index); 146 if (i == null) { 147 return null; 148 } 149 m.remove(index); 150 int row = i.intValue(); 151 int oldrow = -1; 152 TupleCollection res = null; 153 TupleCollection localTC = tupleCollection; 155 if (localTC == null) 156 return null; 157 try { 158 oldrow = localTC.getRow(); 159 localTC.getTuple(row); 160 res = localTC; 161 } catch(MedorException e) { 162 res = null; 165 } finally { 166 if (multithread) { 167 if (tupleCollection != null && oldrow != row) { 170 try { 172 localTC.row(oldrow); 173 } catch (MedorException e) { 174 } 176 } 177 } 178 } 179 if (logger != null && logger.isLoggable(BasicLevel.DEBUG)) { 180 logger.log(BasicLevel.DEBUG, "Tuple found :" 181 + "\n\trow=" + row + "\toldrow=" + oldrow 182 + "\n\tindex=" + index 183 + "\n\ttuple=" + PrefetchCacheImpl.printTuple(res.getTuple())); 184 } 185 return res; 186 } 187 188 public synchronized void close() throws MedorException { 189 closed = true; 190 if (tupleCollection != null) { 191 if (logger != null && logger.isLoggable(BasicLevel.DEBUG)) { 192 logger.log(BasicLevel.DEBUG, "Closing PrefetchBuffer: " + this); 193 } 194 try { 195 ((PrefetchBufferHolder)tupleCollection) 197 .invalidatePrefetchBuffer(); 198 } finally { 199 tupleCollection = null; 200 } 201 } 202 } 203 204 public boolean isClosed() { 205 return closed; 206 } 207 } 208 | Popular Tags |