| 1 package com.daffodilwoods.daffodildb.server.datasystem.persistentsystem; 2 3 import com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs; 4 import com.daffodilwoods.database.resource.DException; 5 import java.io.*; 6 import java.lang.ref.*; 7 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.versioninfo.VersionHandler; 8 9 14 15 public class Cluster implements DatabaseConstants{ 16 17 20 21 ClusterCharacteristics clusterCharacteristics; 22 23 26 27 protected byte[] clusterBytes; 28 29 32 33 protected short actualRecordCount; 34 35 38 39 protected short activeRecordCount; 40 41 44 45 private short[] columnPositions; 46 47 52 53 public boolean readMode; 54 55 59 60 private boolean isDirty; 61 62 65 public DatabaseProperties databaseProperties; 66 67 70 protected VersionHandler versionHandler; 71 74 protected short recordsToBeShifted = 0; 75 76 public Cluster(ClusterCharacteristics clusterCharacteristics1,DatabaseProperties databaseProperties0,VersionHandler versionHandler0) throws DException { 77 clusterCharacteristics = clusterCharacteristics1; 78 databaseProperties = databaseProperties0; 79 versionHandler =versionHandler0; 80 } 81 82 88 89 public final void updateByte(int position,byte value) throws DException{ 90 setDirty(true); 91 clusterBytes[position] = value; 92 } 93 94 100 101 public final void updateBytes(int position,byte[] value) throws DException{ 102 setDirty(true); 103 System.arraycopy(value,0,clusterBytes,position,value.length); 104 } 105 115 public final void updateBytes(byte[] source,int start,int targetPosition,int length) throws DException{ 116 setDirty(true); 117 System.arraycopy(source,start,clusterBytes,targetPosition,length); 118 } 119 120 123 124 public final byte[] getBytes() { 125 return clusterBytes; 126 } 127 133 public final boolean equals(Object object) { 134 return clusterCharacteristics.equals(((Cluster)object).clusterCharacteristics); 135 } 136 137 141 142 public void setBytes(byte[] bytes ) throws DException { 143 clusterBytes = bytes; 144 } 145 146 151 152 protected void setBytesWithInitializeParameters(byte[] bytes ) throws DException { 153 clusterBytes = bytes; 154 initializeParameters(); 155 } 156 157 158 161 162 public ClusterCharacteristics getNextClusterCharacteristics() throws DException{ 163 int add = CCzufDpowfsufs.getIntValue(clusterBytes,databaseProperties.NEXTCLUSTERADDRESS); 164 return add == 0 ? null : new ClusterCharacteristics(add,false); 165 } 166 167 170 171 public ClusterCharacteristics getPreviousClusterCharacteristics() throws DException{ 172 int add = CCzufDpowfsufs.getIntValue(clusterBytes,versionHandler.PREVIOUSCLUSTERADDRESS); 173 return add == 0 ? null : new ClusterCharacteristics(add,false); 174 } 175 176 179 180 void setNextCluster(Cluster nextCluster) throws DException{ 181 byte[] addressBytes = CCzufDpowfsufs.getBytes(nextCluster.clusterCharacteristics.getStartAddress()); 182 updateBytes(databaseProperties.NEXTCLUSTERADDRESS,addressBytes); 183 byte[] previousClusterAddress = CCzufDpowfsufs.getBytes(clusterCharacteristics.getStartAddress()); 184 nextCluster.updateBytes(versionHandler.PREVIOUSCLUSTERADDRESS,previousClusterAddress); 185 } 186 187 public String toString() { 188 return "["+clusterCharacteristics.toString() +"] --- ["+activeRecordCount +"] --- ["+actualRecordCount+"]"; 189 } 190 191 195 196 public int freeSpace() throws DException { 197 return (databaseProperties.CLUSTERSIZE - CCzufDpowfsufs.getShortValue(clusterBytes,0) - versionHandler.NEWADDRESSLENGTH - actualRecordCount * versionHandler.LENGTH - versionHandler.UPDATEBYTES); 198 } 199 200 210 211 short getStartPointerOfRecord(short recordNumber) throws DException{ 212 return columnPositions[--recordNumber] == 0 ? columnPositions[recordNumber] = CCzufDpowfsufs.getShortValue(clusterBytes,databaseProperties.CLUSTERSIZE - versionHandler.NEWADDRESSLENGTH - (recordNumber + 1) * versionHandler.LENGTH) 213 : columnPositions[recordNumber]; 214 } 215 216 217 218 219 220 221 238 242 243 public void setMode(boolean mode) { 244 readMode = mode; 245 } 246 247 250 251 public void rollBack() { 252 clusterBytes = null; 253 } 254 255 258 259 void initializeParameters() throws DException{ 260 if(clusterCharacteristics.getStartAddress() != 0){ 261 try{ 262 actualRecordCount = CCzufDpowfsufs.getShortValue( clusterBytes,versionHandler.LENGTH); 263 activeRecordCount = CCzufDpowfsufs.getShortValue(clusterBytes,2*versionHandler.LENGTH); 264 recordsToBeShifted = activeRecordCount; 265 columnPositions = new short[actualRecordCount]; 266 } 267 catch(Exception ne){ 268 throw new DException("DSE0", new Object [] {ne.getMessage()}); 269 } 270 } 271 } 272 273 278 279 protected void updateClusterInformation(short add) throws DException { 280 if ( add > 0 ) 281 updateBytes(0, CCzufDpowfsufs.getBytes(add) ); 282 updateBytes(versionHandler.LENGTH , CCzufDpowfsufs.getBytes(actualRecordCount)); 283 updateBytes( 2* versionHandler.LENGTH ,CCzufDpowfsufs.getBytes(activeRecordCount) ); 284 } 285 286 289 public short getActualRecordCount(){ 290 return actualRecordCount; 291 } 292 296 public short getActiveRecordCount(){ 297 return activeRecordCount; 298 } 299 300 304 protected int getClusterAddress(){ 305 return clusterCharacteristics.getStartAddress(); 306 } 307 311 public ClusterCharacteristics getClusterCharacteristics(){ 312 return clusterCharacteristics; 313 } 314 315 325 void setLastCluster(ClusterCharacteristics lastClusterCC) throws DException{ 326 byte[] addressBytes = CCzufDpowfsufs.getBytes(lastClusterCC.getStartAddress()); 327 updateBytes(versionHandler.PREVIOUSCLUSTERADDRESS,addressBytes); 328 } 329 330 334 public final void setDirty(boolean flag){ 335 isDirty = flag; 336 } 337 338 339 345 346 public boolean isDirty(){ 347 return isDirty; 348 } 349 350 354 355 void addNewEntry() throws DException { 356 int length = columnPositions.length; 357 short[] temp = new short[length+1]; 358 System.arraycopy(columnPositions,0,temp,0,length); 359 columnPositions = temp; 360 } 361 362 368 369 protected void updateColumnPositions(int index,short value) { 370 columnPositions[index == -1 ? (columnPositions.length - 1) : index] = value; 371 } 372 373 377 protected short[] getColumnPositions() { 378 return columnPositions; 379 } 380 384 public DatabaseProperties getDatabaseProperties() { 385 return databaseProperties; 386 } 387 388 392 public VersionHandler getVersionHandler() { 393 return versionHandler; 394 } 395 } 396 | Popular Tags |