KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.ArrayList JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Iterator JavaDoc;
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 /**
40  * @author P. Dechamboux
41  */

42 public class PrefetchCacheImpl implements PrefetchCache {
43     private HashMap JavaDoc txContexts = new HashMap JavaDoc();
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     // IMPLEMENTATION OF METHODS FROM THE PrefetchCache INTERFACE
63

64     public Tuple getPrefetchTuple(Object JavaDoc index, Object JavaDoc cat, Object JavaDoc 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 JavaDoc catmap = (HashMap JavaDoc) txContexts.get(ctxt);
73         if (catmap == null) {
74             return null;
75         }
76         ArrayList JavaDoc pblist = (ArrayList JavaDoc) 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 JavaDoc index, Object JavaDoc cat, Object JavaDoc 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 JavaDoc catmap = (HashMap JavaDoc) txContexts.get(ctxt);
107         if (catmap == null) {
108             return null;
109         }
110         ArrayList JavaDoc pblist = (ArrayList JavaDoc) 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 JavaDoc ctxt) {
133         HashMap JavaDoc catmap = (HashMap JavaDoc) 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 JavaDoc it = catmap.values().iterator();
145         while (it.hasNext()) {
146             ArrayList JavaDoc pblist = (ArrayList JavaDoc) it.next();
147             Iterator JavaDoc 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 JavaDoc cat, Object JavaDoc 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 JavaDoc cat, Object JavaDoc 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 JavaDoc cat, Object JavaDoc 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 JavaDoc catmap = (HashMap JavaDoc) txContexts.get(ctxt);
180         if (catmap == null) {
181             synchronized(txContexts) {
182                 catmap = (HashMap JavaDoc) txContexts.get(ctxt);
183                 if (catmap == null) {
184                     catmap = new HashMap JavaDoc();
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 JavaDoc pblist = (ArrayList JavaDoc) catmap.get(cat);
194         if (pblist == null) {
195             synchronized(catmap) {
196                 pblist = (ArrayList JavaDoc) catmap.get(cat);
197                 if (pblist == null) {
198                     pblist = new ArrayList JavaDoc();
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 JavaDoc printTuple(Tuple t) {
214         if (t == null) {
215             return null;
216         }
217         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
218         String JavaDoc 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