1 24 package org.objectweb.jalisto.se.impl.server.oid; 25 26 import org.objectweb.jalisto.se.JalistoFactory; 27 import org.objectweb.jalisto.se.api.JalistoProperties; 28 import org.objectweb.jalisto.se.api.cache.JalistoCache; 29 import org.objectweb.jalisto.se.api.internal.*; 30 import org.objectweb.jalisto.se.exception.IdentityException; 31 import org.objectweb.jalisto.se.exception.JalistoException; 32 import org.objectweb.jalisto.se.impl.ExtentImpl; 33 import org.objectweb.jalisto.se.impl.InFileAddress; 34 import org.objectweb.jalisto.se.impl.LogicalOid; 35 import org.objectweb.jalisto.se.impl.server.IdentityProvider; 36 import org.objectweb.jalisto.se.impl.server.PhysicalOid; 37 import org.objectweb.jalisto.se.impl.server.page.OidPage; 38 import org.objectweb.jalisto.se.impl.trace.Trace; 39 40 import java.util.*; 41 42 public abstract class OidTableImpl implements OidTable, OidTableCallback { 43 44 protected OidTableImpl(InternalPhysicalFileAccess physicalAccess, JalistoProperties properties) { 45 trace = JalistoFactory.getInternalFactory().getTracer(properties); 46 oidTableSize = properties.getOidTableSize(); 47 trace.println(Trace.OIDTABLE, "OidTableImpl : create new instance with size = {0}", 48 new Integer (oidTableSize)); 49 trace.println(Trace.DEBUG, "OidTableImpl : create new instance with size = {0}", 50 new Integer (oidTableSize)); 51 52 floidTable = JalistoFactory.getInternalFactory().getCache(properties, oidTableSize, "OidTableImpl"); 53 this.physicalAccess = physicalAccess; 54 this.oidProvider = JalistoFactory.getInternalFactory().getIdentityProvider(properties); 55 this.oidIfaBuffer = new InFileAddress(""); 56 this.oidIfaBuffer.setFileIndex(JalistoProperties.OID_INDEX); 57 } 58 59 60 public int getOidTableSize() { 61 return oidTableSize; 62 } 63 64 public void setOidTableSize(int oidTableSize) { 65 this.oidTableSize = oidTableSize; 66 try { 67 ((JalistoCache) floidTable).setMaxSize(oidTableSize); 68 } catch (ClassCastException cce) { 69 } 70 } 71 72 public int getTransactionalSize(Object sessionId) { 73 return getTransactionnalOids(sessionId).size() + floidTable.size(); 74 } 75 76 77 public void setUpdate(Object sessionId, LogicalOid floid, short value) { 78 throw new UnsupportedOperationException ("should not be used in this implementation"); 79 } 80 81 public short getUpdate(Object sessionId, LogicalOid floid, boolean transactionnal) { 82 throw new UnsupportedOperationException ("should not be used in this implementation"); 83 } 84 85 protected abstract Map getTransactionnalOids(Object sessionId); 86 87 88 public LogicalOid allocateNewFloid(short clid) { 89 return oidProvider.allocateNewFloid(clid); 90 } 91 92 public void insertFpoid(Object sessionId, LogicalOid floid, PhysicalOid fpoid) { 93 trace.println(Trace.OIDTABLE, "OidTableImpl : insertFpoid({0}, {1})", floid, fpoid); 94 OidInfo oidInfo = new OidInfo(floid, fpoid); 95 oidInfo.setActionCaller(OidActionCaller.INSERT_CALLER); 96 getTransactionnalOids(sessionId).put(floid, oidInfo); 97 } 98 99 public void updatePoid(Object sessionId, LogicalOid floid, PhysicalOid newFpoid) { 100 trace.println(Trace.OIDTABLE, "OidTableImpl : updatePoid({0}, {1})", floid, newFpoid); 101 102 OidInfo info = (OidInfo) getTransactionnalOids(sessionId).get(floid); 103 if (info == null) { 104 info = (OidInfo) floidTable.get(floid); 105 if (info == null) { 106 if (loadOidPage(sessionId, floid)) { 107 info = (OidInfo) floidTable.get(floid); 108 } else { 109 throw new IdentityException("the given oid " + floid + " doesn't exist in this Jalisto datastore"); 110 } 111 } 112 info = info.getClone(); 113 getTransactionnalOids(sessionId).put(floid, info); 114 } else { 115 if (info.getActionCaller().isDelete()) { 116 throw new IdentityException("the given oid " + floid + " has been deleted from this Jalisto datastore"); 117 } 118 } 119 info.setActionCaller(OidActionCaller.UPDATE_CALLER); 120 info.setFpoid(newFpoid); 121 } 122 123 public void removeFloid(Object sessionId, LogicalOid floid) { 124 trace.println(Trace.OIDTABLE, "OidTableImpl : removeFloid({0})", floid); 125 OidInfo info = (OidInfo) getTransactionnalOids(sessionId).get(floid); 126 if (info != null) { 127 if (info.getActionCaller().isInsert()) { 128 getTransactionnalOids(sessionId).remove(floid); 129 return; 130 } 131 } else { 132 info = (OidInfo) floidTable.get(floid); 133 if (info == null) { 134 if (loadOidPage(sessionId, floid)) { 135 info = (OidInfo) floidTable.get(floid); 136 } else { 137 throw new IdentityException("the given oid " + floid + " doesn't exist in this Jalisto datastore"); 138 } 139 } 140 info = info.getClone(); 141 getTransactionnalOids(sessionId).put(floid, info); 142 } 143 info.setActionCaller(OidActionCaller.DELETE_CALLER); 144 } 145 146 public Collection getFloidsFromClid(Object sessionId, Object clid, boolean fullyTransactionnal) { 147 trace.println(Trace.OIDTABLE, "OidTableImpl : getFloidsFromClid({0})", clid); 148 Collection workingExtent = new ArrayList(); 149 Iterator oidIfaList = physicalAccess.getKeysStartingWith(IFA_PREFIXE + clid, true).iterator(); 150 while (oidIfaList.hasNext()) { 151 InFileAddress ifa = new InFileAddress((String ) oidIfaList.next()); 152 ifa.setFileIndex(JalistoProperties.OID_INDEX); 153 OidPage oidPage = (OidPage) physicalAccess.readFileObjectAt(ifa); 154 Object [] infos = oidPage.getAllDatas(); 155 for (short i = 0; i < infos.length; i++) { 156 OidInfo info = (OidInfo) infos[i]; 157 if (info != null) { 158 if ((info.isCreated() || fullyTransactionnal) && 159 (info.getFpoid().hasClidEqualsWith(clid))) { 160 workingExtent.add(info.getFloid()); 161 } 162 } 163 } 164 } 165 short clidShort = ((Short ) clid).shortValue(); 166 Iterator infosTx = getTransactionnalOids(sessionId).values().iterator(); 167 while (infosTx.hasNext()) { 168 OidInfo info = (OidInfo) infosTx.next(); 169 if (info.getFloid().getClid() == clidShort) { 170 if (info.getActionCaller().isDelete()) { 171 workingExtent.remove(info.getFloid()); 172 } else if ((info.isCreated() || fullyTransactionnal) && 173 (info.getActionCaller().isInsert())){ 174 workingExtent.add(info.getFloid()); 175 } 176 } 177 } 178 179 return workingExtent; 180 } 181 182 public Collection getFloidsFromClid(Object sessionId, Object clid, ExtentImpl extent, int number) { 183 TreeSet newOids = new TreeSet(); 184 TreeSet removedOids = new TreeSet(); 185 short clidShort = ((Short ) clid).shortValue(); 186 Iterator infosTx = getTransactionnalOids(sessionId).values().iterator(); 187 while (infosTx.hasNext()) { 188 OidInfo info = (OidInfo) infosTx.next(); 189 if (info.getFloid().getClid() == clidShort) { 190 if (info.getActionCaller().isDelete()) { 191 removedOids.add(info.getFloid()); 192 } else if (info.getActionCaller().isInsert()) { 193 newOids.add(info.getFloid()); 194 } 195 } 196 } 197 198 LogicalOid lastFloid = extent.getLastFloid(); 199 if (lastFloid != null) { 200 newOids.tailSet(lastFloid); 201 newOids.remove(lastFloid); 202 } 203 204 TreeSet workingExtent = new TreeSet(); 205 String startingAddress = extent.getStartingAddress(); 206 207 Iterator oidIfaList = physicalAccess.getKeysStartingWith(IFA_PREFIXE + clid, true).iterator(); 208 boolean continueSearching = true; 209 String currentOidPageAddress = ""; 210 int currentIndex; 211 while (oidIfaList.hasNext() && continueSearching) { 212 currentOidPageAddress = (String ) oidIfaList.next(); 213 if (currentOidPageAddress.compareTo(startingAddress) >= 0) { 214 InFileAddress ifa = new InFileAddress(currentOidPageAddress); 215 ifa.setFileIndex(JalistoProperties.OID_INDEX); 216 OidPage oidPage = (OidPage) physicalAccess.readFileObjectAt(ifa); 217 Object [] infos = oidPage.getAllDatas(); 218 for (currentIndex = 0; currentIndex < infos.length; currentIndex++) { 219 OidInfo info = (OidInfo) infos[currentIndex]; 220 if (info != null) { 221 LogicalOid floid = info.getFloid(); 222 if ((lastFloid == null) || (floid.compareTo(lastFloid) > 0)) { 223 try { 224 LogicalOid firstNewOid = (LogicalOid) newOids.first(); 225 while ((firstNewOid.compareTo(floid) < 0) && continueSearching) { 226 workingExtent.add(firstNewOid); 227 if (workingExtent.size() >= number) { 228 continueSearching = false; 229 } 230 newOids.remove(firstNewOid); 231 firstNewOid = (LogicalOid) newOids.first(); 232 } 233 } catch (NoSuchElementException nsee) { 234 } 235 if ((!removedOids.contains(floid)) && continueSearching) { 236 workingExtent.add(floid); 237 if (workingExtent.size() >= number) { 238 continueSearching = false; 239 } 240 } 241 } 242 } 243 } 244 } 245 } 246 247 extent.setStartingAddress(currentOidPageAddress); 248 return workingExtent; 249 } 250 251 public PhysicalOid getFpoid(Object sessionId, LogicalOid floid) { 252 trace.println(Trace.OIDTABLE, "OidTableImpl : getFpoid({0})", floid); 253 OidInfo info = (OidInfo) getTransactionnalOids(sessionId).get(floid); 254 if (info != null) { 255 if (info.getActionCaller().isDelete()) { 256 throw new IdentityException("the given oid " + floid + " has been deleted from this Jalisto datastore"); 257 } 258 } else { 259 info = (OidInfo) floidTable.get(floid); 260 if (info == null) { 261 if (loadOidPage(sessionId, floid)) { 262 info = (OidInfo) floidTable.get(floid); 263 if (info == null) { 264 throw new IdentityException("null info"); 265 } 266 } else { 267 throw new IdentityException("the given oid " + floid + " doesn't exist in this Jalisto datastore"); 268 } 269 } 270 } 271 return info.getFpoid(); 272 } 273 274 public void markAsCreated(Object sessionId, LogicalOid floid) { 275 trace.println(Trace.OIDTABLE, "OidTableImpl : markAsCreated({0})", floid); 276 OidInfo info = (OidInfo) getTransactionnalOids(sessionId).get(floid); 277 if (info == null) { 278 info = (OidInfo) floidTable.get(floid); 279 if (info == null) { 280 if (loadOidPage(sessionId, floid)) { 281 info = (OidInfo) floidTable.get(floid); 282 if (info == null) { 283 throw new IdentityException("null info"); 284 } 285 } else { 286 throw new IdentityException("the given oid " + floid + " doesn't exist in this Jalisto datastore"); 287 } 288 } 289 } 290 info.markAsCreated(); 291 } 292 293 public boolean containsFloid(Object sessionId, LogicalOid floid) { 294 trace.println(Trace.OIDTABLE, "OidTableImpl : containsFloid({0})", floid); 295 OidInfo info = (OidInfo) getTransactionnalOids(sessionId).get(floid); 296 if (info != null) { 297 return (!info.getActionCaller().isDelete()); 298 } else { 299 info = (OidInfo) floidTable.get(floid); 300 if (info == null) { 301 return loadOidPage(sessionId, floid); 302 } 303 return true; 304 } 305 } 306 307 public PhysicalOid getDeletedFpoid(Object sessionId, LogicalOid floid) { 308 trace.println(Trace.OIDTABLE, "OidTableImpl : getDeletedFpoid({0})", floid); 309 OidInfo info = (OidInfo) getTransactionnalOids(sessionId).get(floid); 310 if ((info != null) && info.getActionCaller().isDelete()) { 311 return info.getFpoid(); 312 } 313 throw new IdentityException("the given oid " + floid + " has not been deleted from this Jalisto datastore"); 314 } 315 316 public void begin(Object sessionId) { 317 } 318 319 public void commit(Object sessionId) { 320 oidProvider.commit(); 321 322 Map txOids = getTransactionnalOids(sessionId); 323 Iterator oids = txOids.keySet().iterator(); 324 while (oids.hasNext()) { 325 LogicalOid floid = (LogicalOid) oids.next(); 326 OidInfo info = (OidInfo) txOids.get(floid); 327 info.getActionCaller().callCommitAction(this, floid, info, floidTable); 328 } 329 txOids.clear(); 330 } 331 332 public void rollback(Object sessionId) { 333 getTransactionnalOids(sessionId).clear(); 334 } 335 336 public void close(Object sessionId) { 337 } 338 339 public void open(Object sessionId) { 340 } 341 342 public void insertOidInfoInBase(OidInfo info) { 343 setOidIfa(info.getFloid()); 344 OidPage oidPage = getOidPage(); 345 346 Object old = oidPage.getDataAt(oidIfaBuffer); 347 if (old != null) { 348 throw new IdentityException("the given oid " + info.getFloid() + " already exist in this Jalisto datastore"); 349 } 350 oidPage.setDataAt(oidIfaBuffer, info); 351 physicalAccess.updateFileObject(oidPage); 352 } 353 354 public void updateOidInfoInBase(OidInfo info) { 355 setOidIfa(info.getFloid()); 356 OidPage oidPage = getOidPage(); 357 358 oidPage.setDataAt(oidIfaBuffer, info); 359 physicalAccess.updateFileObject(oidPage); 360 } 361 362 public void deleteOidInfoInBase(LogicalOid floid) { 363 setOidIfa(floid); 364 OidPage oidPage = getOidPage(); 365 366 oidPage.setDataAt(oidIfaBuffer, null); 367 if (oidPage.getUsedSpace() == 0) { 368 physicalAccess.deleteFileObject(oidIfaBuffer.getClone()); 369 } else { 370 physicalAccess.updateFileObject(oidPage); 371 } 372 } 373 374 377 378 public IdentityProvider getIdentityProvider() { 379 return oidProvider; 380 } 381 382 private void setOidIfa(LogicalOid floid) { 383 long instanceNumber = floid.getInstanceNumber(); 384 oidIfaBuffer.setAddress(IFA_PREFIXE + floid.getClid() + "-" + (instanceNumber / oidPageSize)); 385 oidIfaBuffer.setIndex((int) (instanceNumber % oidPageSize)); 386 } 387 388 private OidPage getOidPage() { 389 try { 390 return (OidPage) physicalAccess.readFileObjectAt(oidIfaBuffer); 392 } catch (JalistoException fde) { 393 if (fde.getMessage().startsWith("could not read object at ifa(" + IFA_PREFIXE)) { 394 return createOidPage(); 396 } else { 397 throw fde; 398 } 399 } 400 } 401 402 private OidPage createOidPage() { 403 OidPage oidPage = new OidPage(oidPageSize); 404 oidPage.setIfa(oidIfaBuffer); 405 physicalAccess.insertFileObject(oidPage); 406 return oidPage; 407 } 408 409 protected boolean loadOidPage(Object sessionId, LogicalOid floid) { 410 int index = (int) (floid.getInstanceNumber() % oidPageSize); 411 setOidIfa(floid); 412 try { 413 OidPage oidPage = (OidPage) physicalAccess.readFileObjectAt(oidIfaBuffer); 415 Object [] infos = oidPage.getAllDatas(); 416 Map txOids = getTransactionnalOids(sessionId); 417 for (int j = 0; j < infos.length; j++) { 418 OidInfo info = (OidInfo) infos[j]; 419 if (info != null) { 420 LogicalOid readedFloid = info.getFloid(); 421 if (!txOids.containsKey(readedFloid)) { 422 floidTable.put(readedFloid, info); 423 } 424 } 425 } 426 return (infos[index] != null); 427 } catch (Exception e) { trace.println(Trace.OIDTABLE, "OidTableImpl : loadOidPage : oidPage doesn't exist"); 429 } 430 return false; 431 } 432 433 protected void readAtTable() { 434 trace.println(Trace.OIDTABLE, "OidTableImpl : readAtTable()"); 435 int dataRead = 0; 436 Iterator oidIfaList = physicalAccess.getKeysStartingWith(IFA_PREFIXE, true).iterator(); 437 while (oidIfaList.hasNext()) { 438 InFileAddress ifa = new InFileAddress((String ) oidIfaList.next()); 439 ifa.setFileIndex(JalistoProperties.OID_INDEX); 440 OidPage oidPage = (OidPage) physicalAccess.readFileObjectAt(ifa); 441 Object [] infos = oidPage.getAllDatas(); 442 for (int i = 0; i < infos.length; i++) { 443 OidInfo info = (OidInfo) infos[i]; 444 if (info != null) { 445 floidTable.put(info.getFloid(), info); 446 dataRead++; 447 } 448 if (dataRead == oidTableSize) { return; 450 } 451 } 452 } 453 } 454 455 456 protected InternalPhysicalFileAccess physicalAccess; 457 protected IdentityProvider oidProvider; 458 protected Map floidTable; 459 protected int oidPageSize; 460 protected int oidTableSize; 461 protected Trace trace; 462 463 private InFileAddress oidIfaBuffer; 464 465 private static final String IFA_PREFIXE = "id"; 466 } 467 | Popular Tags |