KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jalisto > se > impl > readonly > OidTableReadOnlyImpl


1 /*
2  * Jalisto - JAva LIght STOrage
3  * Copyright (C) 2000-2005 Xcalia http://www.xcalia.com
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * Xcalia
20  * 71, rue Desnouettes
21  * 75014 Paris - France
22  * http://www.xcalia.com
23  */

24 package org.objectweb.jalisto.se.impl.readonly;
25
26 import org.objectweb.jalisto.se.api.JalistoProperties;
27 import org.objectweb.jalisto.se.api.internal.OidTable;
28 import org.objectweb.jalisto.se.api.internal.InternalPhysicalFileAccess;
29 import org.objectweb.jalisto.se.exception.JalistoException;
30 import org.objectweb.jalisto.se.impl.*;
31 import org.objectweb.jalisto.se.impl.server.IdentityProvider;
32 import org.objectweb.jalisto.se.impl.InFileAddress;
33 import org.objectweb.jalisto.se.impl.server.PhysicalOid;
34 import org.objectweb.jalisto.se.impl.server.oid.OidInfo;
35 import org.objectweb.jalisto.se.impl.server.page.OidPage;
36 import org.objectweb.jalisto.se.impl.trace.Trace;
37 import org.objectweb.jalisto.se.JalistoFactory;
38
39 import java.util.*;
40
41 public class OidTableReadOnlyImpl implements OidTable {
42
43     private OidTableReadOnlyImpl(InternalPhysicalFileAccess physicalAccess, JalistoProperties properties) {
44         tracer = JalistoFactory.getInternalFactory().getTracer(properties);
45         atSize = properties.getOidTableSize();
46         tracer.println(Trace.OIDTABLE, "OidTableReadOnlyImpl : create new instance with size = {0}",
47                        new Integer JavaDoc(atSize));
48
49         floidTable = JalistoFactory.getInternalFactory().getCache(properties, atSize, "atCache");
50         this.physicalAccess = physicalAccess;
51     }
52
53     public LogicalOid allocateNewFloid(short clid) {
54         throw new UnsupportedOperationException JavaDoc();
55     }
56
57     public void insertFpoid(Object JavaDoc sessionId, LogicalOid floid, PhysicalOid fpoid) {
58         throw new UnsupportedOperationException JavaDoc();
59     }
60
61     public void updatePoid(Object JavaDoc sessionId, LogicalOid floid, PhysicalOid newFpoid) {
62         throw new UnsupportedOperationException JavaDoc();
63     }
64
65     public void removeFloid(Object JavaDoc sessionId, LogicalOid floid) {
66         throw new UnsupportedOperationException JavaDoc();
67     }
68
69     public void markAsCreated(Object JavaDoc sessionId, LogicalOid floid) {
70         throw new UnsupportedOperationException JavaDoc();
71     }
72
73
74     public Collection getFloidsFromClid(Object JavaDoc sessionId, Object JavaDoc clid, boolean fullyTransactionnal) {
75         tracer.println(Trace.OIDTABLE, "OidTableReadOnlyImpl : getFloidsFromClid({0})", clid);
76         Collection workingExtent = new ArrayList();
77         Iterator oidIfaList = physicalAccess.getKeysStartingWith(IFA_PREFIXE + clid, true).iterator();
78         while (oidIfaList.hasNext()) {
79             InFileAddress ifa = new InFileAddress((String JavaDoc) oidIfaList.next());
80             ifa.setFileIndex(JalistoProperties.OID_INDEX);
81             OidPage oidPage = (OidPage) physicalAccess.readFileObjectAt(ifa);
82             Object JavaDoc[] infos = oidPage.getAllDatas();
83             for (int j = 0; j < infos.length; j++) {
84                 OidInfo info = (OidInfo) infos[j];
85                 if (info != null) {
86                     if (info.getFpoid().hasClidEqualsWith(clid)) {
87                         workingExtent.add(info.getFloid());
88                     }
89                 }
90             }
91         }
92         return workingExtent;
93     }
94
95     public Collection getFloidsFromClid(Object JavaDoc sessionId, Object JavaDoc clid, ExtentImpl extent, int number) {
96         tracer.println(Trace.OIDTABLE, "OidTableReadOnlyImpl : getFloidsFromClid({0})", clid);
97         Collection workingExtent = new ArrayList();
98         boolean continueSearching = true;
99         String JavaDoc startingAddress = extent.getStartingAddress();
100         LogicalOid lastFloid = extent.getLastFloid();
101         String JavaDoc oidPageAddress = null;
102         Iterator oidIfaList = physicalAccess.getKeysStartingWith(IFA_PREFIXE + clid, true).iterator();
103         while (oidIfaList.hasNext() && continueSearching) {
104             oidPageAddress = (String JavaDoc) oidIfaList.next();
105             if (oidPageAddress.compareTo(startingAddress) >= 0) {
106                 InFileAddress ifa = new InFileAddress(oidPageAddress);
107                 ifa.setFileIndex(JalistoProperties.OID_INDEX);
108                 OidPage oidPage = (OidPage) physicalAccess.readFileObjectAt(ifa);
109                 Object JavaDoc[] infos = oidPage.getAllDatas();
110                 for (int j = 0; (j < infos.length) && continueSearching; j++) {
111                     OidInfo info = (OidInfo) infos[j];
112                     if (info != null) {
113                         LogicalOid floid = info.getFloid();
114                         if (info.getFpoid().hasClidEqualsWith(clid)) {
115                             if ((lastFloid == null) || (floid.compareTo(lastFloid) > 0)) {
116                                 workingExtent.add(floid);
117                             }
118                         }
119                     }
120                     if (workingExtent.size() >= number) {
121                         continueSearching = false;
122                     }
123                 }
124             }
125         }
126         extent.setStartingAddress(oidPageAddress);
127         return workingExtent;
128     }
129
130     public synchronized PhysicalOid getFpoid(Object JavaDoc sessionId, LogicalOid floid) {
131         tracer.println(Trace.OIDTABLE, "OidTableReadOnlyImpl : getFpoid({0})", floid);
132         if (!floidTable.containsKey(floid)) {
133             if (!loadOidPage(floid)) {
134                 return null;
135             }
136         }
137         OidInfo oidInfo = (OidInfo) floidTable.get(floid);
138         if (oidInfo != null) {
139             return oidInfo.getFpoid();
140         } else {
141             return null;
142         }
143     }
144
145     public boolean containsFloid(Object JavaDoc sessionId, LogicalOid floid) {
146         tracer.println(Trace.OIDTABLE, "OidTableReadOnlyImpl : containsFloid({0})", floid);
147         if (!floidTable.containsKey(floid)) {
148             return (!loadOidPage(floid));
149         }
150         OidInfo oidInfo = (OidInfo) floidTable.get(floid);
151         if (oidInfo != null) {
152             return (!oidInfo.getActionCaller().isDelete());
153         } else {
154             return false;
155         }
156     }
157
158     public PhysicalOid getDeletedFpoid(Object JavaDoc sessionId, LogicalOid floid) {
159         throw new UnsupportedOperationException JavaDoc();
160     }
161
162     public void setUpdate(Object JavaDoc sessionId, LogicalOid floid, short value) {
163         throw new UnsupportedOperationException JavaDoc("should not be used in this implementation");
164     }
165
166     public short getUpdate(Object JavaDoc sessionId, LogicalOid floid, boolean transactionnal) {
167         throw new UnsupportedOperationException JavaDoc("should not be used in this implementation");
168     }
169
170     public synchronized void checkStateFromBase(Object JavaDoc sessionId, JalistoProperties properties) {
171         tracer.println(Trace.OIDTABLE, "OidTableReadOnlyImpl : getStateFromBase()");
172         floidTable = JalistoFactory.getInternalFactory().getCache(properties, atSize, "atCache");
173         readAtTable();
174     }
175
176     public Map getOidTable() {
177         return floidTable;
178     }
179
180
181     public String JavaDoc toString() {
182         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
183         sb.append("OidTableReadOnlyImpl : \n");
184         sb.append("\tfloid list : ").append(floidTable).append("\n");
185         return sb.toString();
186     }
187
188
189     /**
190      * ********************************* protected **********************************************
191      */

192
193     private synchronized boolean loadOidPage(LogicalOid floid) {
194         tracer.println(Trace.OIDTABLE, "OidTableReadOnlyImpl : loadOidPage({0})", floid);
195 // int index = floid.getFloidAsInt() % oidPageSize;
196
// int pageNumber = floid.getFloidAsInt() / oidPageSize;
197
// InFileAddress atIfa = new InFileAddress(IFA_PREFIXE + String.valueOf(pageNumber));
198
// atIfa.setIndex(index);
199
// atIfa.setFileIndex(JalistoProperties.OID_INDEX);
200
// try {
201
// // could raise exception if oidPage don't exist
202
// OidPage oidPage = (OidPage) physicalAccess.readFileObjectAt(atIfa);
203
// Object[] infos = oidPage.getAllDatas();
204
// for (int j = 0; j < infos.length; j++) {
205
// OidInfo info = (OidInfo) infos[j];
206
// if (info != null) {
207
// floidTable.put(info.getFloid(), info);
208
// }
209
// }
210
// return true;
211
// } catch (Exception e) {
212
// trace.println(Trace.OIDTABLE, "OidTableImplPage : loadOidPage : oidPage doesn't exist");
213
// }
214
return false;
215     }
216
217     private synchronized void readAtTable() {
218         tracer.println(Trace.OIDTABLE, "OidTableReadOnlyImpl : readAtTable()");
219         int dataRead = 0;
220         Iterator oidIfaList = physicalAccess.getKeysStartingWith(IFA_PREFIXE, true).iterator();
221         while (oidIfaList.hasNext()) {
222             InFileAddress ifa = new InFileAddress((String JavaDoc) oidIfaList.next());
223             ifa.setFileIndex(JalistoProperties.OID_INDEX);
224             OidPage oidPage = (OidPage) physicalAccess.readFileObjectAt(ifa);
225             Object JavaDoc[] infos = oidPage.getAllDatas();
226             for (int i = 0; i < infos.length; i++) {
227                 OidInfo info = (OidInfo) infos[i];
228                 if (info != null) {
229                     floidTable.put(info.getFloid(), info);
230                     dataRead++;
231                     if (dataRead == atSize) {
232                         return;
233                     }
234                 }
235             }
236         }
237     }
238
239     public void begin(Object JavaDoc sessionId) {
240     }
241
242     public void commit(Object JavaDoc sessionId) {
243     }
244
245     public void rollback(Object JavaDoc sessionId) {
246     }
247
248     public void close(Object JavaDoc sessionId) {
249     }
250
251     public void open(Object JavaDoc sessionId) {
252     }
253
254     public void deleteOidInfoInBase(LogicalOid floid) {
255     }
256
257     public void insertOidInfoInBase(OidInfo info) {
258     }
259
260     public void updateOidInfoInBase(OidInfo info) {
261     }
262
263     public IdentityProvider getIdentityProvider() {
264         return null;
265     }
266
267
268     private InternalPhysicalFileAccess physicalAccess;
269
270     private Map floidTable;
271
272     private int atSize;
273
274     private Trace tracer;
275
276
277     /**
278      * ************************************ STATIC *********************************************
279      */

280
281     public static OidTable getAnOidTable(InternalPhysicalFileAccess physicalAccess, JalistoProperties properties) {
282         OidTableReadOnlyImpl oidTable;
283         try {
284             oidTable = new OidTableReadOnlyImpl(physicalAccess, properties);
285             oidTable.readAtTable();
286         } catch (Exception JavaDoc e) {
287             throw new JalistoException("cannot create a base in read only mode", e);
288         }
289         return oidTable;
290     }
291
292     private static final String JavaDoc IFA_PREFIXE = "id";
293 }
294
Popular Tags