1 package com.daffodilwoods.daffodildb.server.datasystem.persistentsystem; 2 3 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.versioninfo.*; 4 import com.daffodilwoods.daffodildb.utils.*; 5 import com.daffodilwoods.daffodildb.utils.byteconverter.*; 6 import com.daffodilwoods.database.resource.*; 7 import com.daffodilwoods.daffodildb.server.datasystem.btree.*; 8 9 16 public class PartialFixedRecordCluster extends FixedRecordCluster{ 17 18 22 int MINRECORDLENGTH; 23 24 public PartialFixedRecordCluster(TableProperties tp,DatabaseProperties databaseProperties0,VersionHandler versionHandler0) throws DException { 25 super(tp,databaseProperties0,versionHandler0); 26 MINRECORDLENGTH = versionHandler.ACTIVE_DELETE + versionHandler.FULL_PARTIAL + versionHandler.LENGTH + 7; 27 } 28 29 39 40 public int[] insert(byte[] bytes,int startColumnIndex, boolean isUpdate) throws DException { 41 42 int insertType = insertType(bytes, startColumnIndex); 43 return insertType == NONE ? new int[]{FAILED,0} : partialInsertRecord(bytes,getNextInsertableAddress(),startColumnIndex,isUpdate); 44 } 45 46 55 private int insertType(byte[] bytes,int startPosition) throws DException { 56 int recordSize = bytes.length + versionHandler.ACTIVE_DELETE + versionHandler.FULL_PARTIAL + versionHandler.LENGTH ; 57 return startPosition > 0 ? PARTIALY : recordSize <= freeSpace() ? FULLY 58 : recordSize > databaseProperties.CLUSTERSIZE && freeSpace() > MINRECORDLENGTH 59 ? PARTIALY : NONE; 60 } 61 62 73 74 private int[] partialInsertRecord(byte[] bytes,short insertAdd,int startPosition, boolean isUpdate) throws DException { 75 short position = insertAdd; 76 cluster.updateByte(position++,startPosition == 0 ? versionHandler.ACTIVE : versionHandler.DELETE); 77 short recordSizePointer = (short)(databaseProperties.CLUSTERSIZE - 78 versionHandler.NEWADDRESSLENGTH - cluster.actualRecordCount * versionHandler.LENGTH - versionHandler.LENGTH); 79 int range = freeSpace() - cluster.actualRecordCount * versionHandler.LENGTH - versionHandler.LENGTH - 1 - 1 -1 ; 80 int bytesTowrite = bytes.length - startPosition; 81 int possibleLength = range > bytesTowrite ? bytesTowrite : range; 82 cluster.updateByte(position++,range > bytesTowrite ? versionHandler.COMPLETE : versionHandler.PARTIALLY); 83 cluster.updateBytes(bytes,startPosition,position,possibleLength); 84 if(startPosition == 0) 85 cluster.activeRecordCount++; 86 cluster.actualRecordCount++; 87 cluster.updateClusterInformation((short)(position+possibleLength)); 88 cluster.updateBytes(recordSizePointer,CCzufDpowfsufs.getBytes( (short)(possibleLength))); 89 int writtenSize = possibleLength + startPosition; 90 return writtenSize == bytes.length ? new int[]{SUCCESSFUL,0} : new int[]{PARTIAL,writtenSize}; 91 } 92 93 109 public BufferRange retrieveBufferRange(short recordNumber ) throws DException { 110 byte[] clusterBytes = cluster.getBytes(); 111 int pointer = moveToRecordNumber(clusterBytes,recordNumber); 112 if(clusterBytes[pointer++] == versionHandler.DELETE ) 113 throw new DException("DSE2003",new Object [] {new Integer (recordNumber)}); 114 byte isComplete = clusterBytes[pointer++]; 115 if( isComplete == versionHandler.PARTIALLY){ 116 int recordSizePointer = databaseProperties.CLUSTERSIZE - 117 2 * versionHandler.LENGTH - recordNumber * versionHandler.LENGTH ; 118 short sad = CCzufDpowfsufs.getShortValue(clusterBytes,recordSizePointer) ; 119 byte[] dd = new byte[sad]; 120 System.arraycopy(clusterBytes,pointer,dd,0,sad); 121 BufferRange bf = new BufferRange(dd,0,sad); 122 throw new DException("DSE2006",new Object []{bf}); 123 } 124 return new BufferRange(true); 125 } 126 127 128 136 137 public Object [] partialRetrieve(short recordNumber ) throws DException { 138 byte[] clusterBytes = cluster.getBytes(); 139 int pointer = moveToRecordNumber(clusterBytes,recordNumber); 140 pointer++; 141 byte isComplete = clusterBytes[pointer++]; 142 int recordSizePointer = databaseProperties.CLUSTERSIZE - 143 2 * versionHandler.LENGTH - recordNumber * versionHandler.LENGTH ; 144 short sad = CCzufDpowfsufs.getShortValue(clusterBytes,recordSizePointer); 145 byte[] dd = new byte[sad]; 146 System.arraycopy(clusterBytes,pointer,dd,0,sad); 147 return new Object [] {dd,new byte[] {isComplete}}; 148 149 } 150 151 152 161 162 public ClusterStatus delete(short recordNumber, boolean checkKeyValidity) throws DException{ 163 164 if(recordNumber > cluster.actualRecordCount ) 165 throw new DException("DSE2007", new Object []{cluster.toString() }); 166 byte [] clusterBytes = cluster.getBytes(); 167 short skip = moveToRecordNumber(clusterBytes,recordNumber); 168 boolean curre = clusterBytes[skip+1] == versionHandler.PARTIALLY; 169 if( curre) 170 throw new DException("DSE2006",new Object []{}); 171 cluster.updateByte(skip,versionHandler.DELETE); 172 cluster.activeRecordCount--; 173 cluster.updateBytes(2*versionHandler.LENGTH,CCzufDpowfsufs.getBytes(cluster.activeRecordCount)); 174 return new ClusterStatus(cluster,cluster.activeRecordCount,cluster.actualRecordCount,clusterBytes[versionHandler.CLUSTER_STARTPOINTER+1] != versionHandler.FULL,clusterBytes[moveToRecordNumber(clusterBytes,cluster.actualRecordCount)+1] != versionHandler.FULL,curre); 175 } 176 177 185 186 187 public void update(short recordNumber,byte[] newBytes) throws DException { 188 throw new DException("DSE2006",new Object []{}); 189 190 } 191 192 197 198 199 200 206 public short getRecordCount() throws DException { 207 return cluster.actualRecordCount; 208 } 209 210 211 218 public int freeSpace() throws DException { 219 return databaseProperties.CLUSTERSIZE - CCzufDpowfsufs.getShortValue(cluster.getBytes(),0) - versionHandler.NEWADDRESSLENGTH; 220 } 221 222 226 public Cluster getCluster(){ 227 return cluster; 228 } 229 230 236 public void setCluster(Cluster cluster0) throws DException{ 237 cluster = cluster0; 238 } 239 240 241 247 protected short getNextInsertableAddress() throws DException { 248 return CCzufDpowfsufs.getShortValue(cluster.getBytes(),0); 249 } 250 251 261 protected short moveToRecordNumber(byte[] clusterBytes,short recordNumber) throws DException { 262 short skip = versionHandler.CLUSTER_STARTPOINTER ; for(int i = 1 , rec = recordNumber ; i < rec ; i++){ 264 int recordSizePointer = databaseProperties.CLUSTERSIZE - 265 2 * versionHandler.LENGTH - i * versionHandler.LENGTH ; 266 skip += (short)(CCzufDpowfsufs.getShortValue(clusterBytes,recordSizePointer) + 2); 267 } 268 return skip; 269 } 270 271 278 279 public void checkValidity(short recordNumber)throws DException{ 280 byte[] clusterBytes = cluster.getBytes(); 281 short pointer = moveToRecordNumber(clusterBytes,recordNumber); 282 if(clusterBytes[pointer++] == versionHandler.DELETE) 283 throw StaticExceptions.RECORD_DELETED_EXCEPTION; 284 } 285 286 291 292 public int getLength0fPartialRecord(short recordNumber) throws DException{ 293 294 throw new java.lang.UnsupportedOperationException ("Method getLength0fPartialRecord() not yet implemented."); 295 } 296 297 302 303 public boolean isComplete(short recordNumber)throws DException { 304 305 throw new java.lang.UnsupportedOperationException ("Method isComplete() not yet implemented."); 306 } 307 308 320 public ClusterStatus partialDelete(short recordNumber) throws DException{ 321 byte [] clusterBytes = cluster.getBytes(); 322 short skip = moveToRecordNumber(clusterBytes,recordNumber); 323 boolean curre = clusterBytes[skip+1] == versionHandler.COMPLETE; 324 if(clusterBytes[skip] == versionHandler.ACTIVE){ 325 cluster.updateByte(skip,versionHandler.DELETE); 326 cluster.activeRecordCount--; 327 cluster.updateBytes(2*versionHandler.LENGTH,CCzufDpowfsufs.getBytes(cluster.activeRecordCount)); 328 } 329 return new ClusterStatus(cluster,cluster.activeRecordCount,cluster.actualRecordCount,clusterBytes[versionHandler.CLUSTER_STARTPOINTER+1] != versionHandler.FULL,clusterBytes[moveToRecordNumber(clusterBytes,cluster.actualRecordCount)+1] != versionHandler.FULL,curre); 330 } 331 332 public String toString() { 333 return "Fixed RecordCluster " ; } 335 336 341 public int getRange() throws DException { 342 return databaseProperties.CLUSTERSIZE - CCzufDpowfsufs.getShortValue(cluster.getBytes(),0) - versionHandler.LENGTH * cluster.actualRecordCount - 2*versionHandler.LENGTH; 343 } 344 354 public int partialUpdate(short recordNumber, int startPosition, byte[] newBytes) throws DException { 355 byte [] bytes = cluster.getBytes(); 356 short pointer = (short)(moveToRecordNumber(bytes,recordNumber) + 2); 357 int recordSizePointer = databaseProperties.CLUSTERSIZE - 358 2*versionHandler.LENGTH - recordNumber * versionHandler.LENGTH ; 359 short sad = CCzufDpowfsufs.getShortValue(bytes,recordSizePointer) ; 360 cluster.updateBytes(newBytes,startPosition,pointer,sad); 361 return sad + startPosition; 362 } 363 364 365 366 } 367 | Popular Tags |