KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > medor > eval > prefetch > lib > PrefetchBufferImpl


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2003 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, A. Lefebvre, S. Chassande-Barrioz
22  */

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 JavaDoc;
35 import java.util.Map JavaDoc;
36
37 /**
38  * @author P. Dechamboux
39  */

40 public class PrefetchBufferImpl implements PrefetchBuffer {
41     private int indexPos;
42     private TupleCollection tupleCollection = null;
43     private Map JavaDoc 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     // IMPLEMENTATION OF METHODS FROM THE PrefetchBuffer INTERFACE
56

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 JavaDoc();
69             }
70             Tuple pt = localTC.getTuple();
71             Object JavaDoc 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 JavaDoc(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 JavaDoc index) throws MedorException {
94         Map JavaDoc m = tupleMap;
95         if (m == null) {
96             return null;
97         }
98         Integer JavaDoc i = (Integer JavaDoc) 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         //local copy to avoid the synchronization
107
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             //The tupleCollection has been closed or it is not possible to go
115
// to the row specified
116
res = null;
117         } finally {
118             if (multithread) {
119                 // try to restore old position into tupleCollection if the
120
// tupleCollection is alive
121
if (tupleCollection != null && oldrow != row) {
122 //TODO : Clone the res variable
123
try {
124                         localTC.row(oldrow);
125                     } catch (MedorException e) {
126                         // ARRGH: cannot do anything more!!
127
}
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 JavaDoc index) throws MedorException {
141         Map JavaDoc m = tupleMap;
142         if (m == null) {
143             return null;
144         }
145         Integer JavaDoc i = (Integer JavaDoc) 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         //local copy to avoid the synchronization
154
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             //The tupleCollection has been closed or it is not possible to go
163
// to the row specified
164
res = null;
165         } finally {
166             if (multithread) {
167                 // try to restore old position into tupleCollection if the
168
// tupleCollection is alive
169
if (tupleCollection != null && oldrow != row) {
170 //TODO : Clone the res variable
171
try {
172                         localTC.row(oldrow);
173                     } catch (MedorException e) {
174                         // ARRGH: cannot do anything more!!
175
}
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                 //don't close the TupleCollection yet, but invalidate it.
196
((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