1 24 package org.objectweb.jalisto.se.impl.server; 25 26 import org.objectweb.jalisto.se.api.internal.InternalPhysicalFileAccess; 27 import org.objectweb.jalisto.se.api.JalistoProperties; 28 import org.objectweb.jalisto.se.impl.server.page.SystemPage; 29 import org.objectweb.jalisto.se.impl.InFileAddress; 30 31 import java.util.Collection ; 32 import java.util.Hashtable ; 33 import java.util.Map ; 34 35 public class IfaIndex { 36 37 private IfaIndex(InternalPhysicalFileAccess physicalAccess) { 38 this.physicalAccess = physicalAccess; 39 pidIndex = new Hashtable (); 40 systemPageIndex = 0; 41 instancePageIndex = 0; 42 nodePageIndex = 0; 43 leafPageIndex = 0; 44 } 45 46 public Collection getAllPid() { 47 return pidIndex.keySet(); 48 } 49 50 public Collection getAllPidIfa() { 51 return pidIndex.values(); 52 } 53 54 public void setCurrentAvalaibleSystemPage(InFileAddress currentAvalaibleSystemPage) { 55 this.currentAvalaibleSystemPage = currentAvalaibleSystemPage.getClone(); 56 updateStateInBase(true); 57 } 58 59 public InFileAddress getCurrentAvalaibleSystemPage() { 60 return currentAvalaibleSystemPage; 61 } 62 63 public String getNewInstancePageAddress() { 64 return "ip" + (instancePageIndex++); 65 } 66 67 public String getNewSystemPageAddress() { 68 return "sp" + (systemPageIndex++); 69 } 70 71 public String getNewNodePageAddress() { 72 return "no" + (nodePageIndex++); 73 } 74 75 public String getNewLeafPageAddress() { 76 return "le" + (leafPageIndex++); 77 } 78 79 public InFileAddress getPidIfa(Object pid) { 80 return (InFileAddress) pidIndex.get(pid); 81 } 82 83 public void putPidIfa(Object pid, InFileAddress ifa) { 84 pidIndex.put(pid, ifa); 85 physicalAccess.writeObjectInBase(OID_IFA_PID_INDEX_IFA, new Hashtable (pidIndex), true); 86 } 87 88 public String toString() { 89 StringBuffer sb = new StringBuffer (); 90 sb.append("FileIndexImplBasic2 : \n"); 91 sb.append("\tpid index : ").append(pidIndex).append("\n"); 92 sb.append("\tsystemPageIndex : ").append(systemPageIndex).append("\n"); 93 sb.append("\tinstancePageIndex : ").append(instancePageIndex).append("\n"); 94 sb.append("\tcurrentAvalaibleSystemPage : ").append(currentAvalaibleSystemPage).append("\n"); 95 return sb.toString(); 96 } 97 98 private void init(short systemPageSize) { 99 currentAvalaibleSystemPage = new InFileAddress(getNewSystemPageAddress()); 100 SystemPage firstSystemPage = new SystemPage(systemPageSize); 101 firstSystemPage.setIfa(currentAvalaibleSystemPage); 102 physicalAccess.insertFileObject(firstSystemPage); 103 updateStateInBase(false); 104 physicalAccess.writeObjectInBase(OID_IFA_PID_INDEX_IFA, new Hashtable (pidIndex), false); 105 } 106 107 public void getStateFromBase() { 108 pidIndex = (Map ) physicalAccess.readFileObjectAt(OID_IFA_PID_INDEX_IFA).getData(); 109 Object [] datas = (Object []) physicalAccess.readFileObjectAt(OID_IFAINDEX_IFA).getData(); 110 currentAvalaibleSystemPage = (InFileAddress) datas[0]; 111 systemPageIndex = ((Integer ) datas[1]).intValue(); 112 instancePageIndex = ((Integer ) datas[2]).intValue(); 113 nodePageIndex = ((Integer ) datas[3]).intValue(); 114 leafPageIndex = ((Integer ) datas[4]).intValue(); 115 } 116 117 public void updateStateInBase(boolean isUpdate) { 118 Object [] datas = new Object [5]; 119 datas[0] = currentAvalaibleSystemPage; 120 datas[1] = new Integer (systemPageIndex); 121 datas[2] = new Integer (instancePageIndex); 122 datas[3] = new Integer (nodePageIndex); 123 datas[4] = new Integer (leafPageIndex); 124 physicalAccess.writeObjectInBase(OID_IFAINDEX_IFA, datas, isUpdate); 125 } 126 127 130 131 public static IfaIndex getAnIfaIndex(InternalPhysicalFileAccess physicalAccess, JalistoProperties properties) { 132 IfaIndex ifaIndex = new IfaIndex(physicalAccess); 133 try { 134 ifaIndex.getStateFromBase(); 135 } catch (Exception e) { 136 ifaIndex.init(properties.getSystemPageSize()); 137 } 138 return ifaIndex; 139 } 140 141 142 private InternalPhysicalFileAccess physicalAccess; 143 private InFileAddress currentAvalaibleSystemPage; 144 145 private Map pidIndex; 147 private int systemPageIndex; 148 private int instancePageIndex; 149 private int nodePageIndex; 150 private int leafPageIndex; 151 152 153 private static final InFileAddress OID_IFAINDEX_IFA = new InFileAddress("IFAIst"); 154 private static final InFileAddress OID_IFA_PID_INDEX_IFA = new InFileAddress("pids"); 155 } 156 | Popular Tags |