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 17 public class FixedRecordCluster implements _RecordCluster{ 18 static final int NONE = -1; 19 static final int FULLY = 0; 20 static final int PARTIALY = 1; 21 22 25 protected TableProperties tableProperties; 26 27 30 31 protected Cluster cluster; 32 33 34 37 38 private int moveLength; 39 40 43 44 protected DatabaseProperties databaseProperties; 45 46 49 protected VersionHandler versionHandler; 50 51 public FixedRecordCluster(TableProperties tp,DatabaseProperties databaseProperties0,VersionHandler versionHandler0) throws DException { 52 tableProperties = tp; 53 databaseProperties = databaseProperties0; 54 versionHandler = versionHandler0; 55 moveLength = tableProperties.recordSize + tableProperties.columnCount + versionHandler.ACTIVE_DELETE + versionHandler.FULL_PARTIAL; 56 57 } 58 59 73 74 public int[] insert(byte[] bytes,int startColumnIndex, boolean isUpdate) throws DException { 75 int insertType = insertType(bytes, startColumnIndex); 76 return insertType == NONE ? new int[]{FAILED,0} : insertType == FULLY ? insertRecord(bytes,getNextInsertableAddress(),isUpdate) 77 : partialInsertRecord(bytes,getNextInsertableAddress(),startColumnIndex,isUpdate); 78 } 79 80 89 90 private int insertType(byte[] bytes,int startPosition) throws DException { 91 int recordSize = bytes.length + 2; return recordSize <= freeSpace() ? FULLY : NONE; 93 } 94 95 96 108 109 private int[] insertRecord(byte[] bytes,short insertAdd, boolean isUpdate) throws DException { 110 short currentPointer = insertAdd; 111 cluster.updateByte(currentPointer++,versionHandler.ACTIVE); 112 cluster.updateByte(currentPointer++,versionHandler.COMPLETE); 113 cluster.updateBytes(currentPointer,bytes); 114 cluster.activeRecordCount++; 115 cluster.actualRecordCount++; 116 currentPointer += bytes.length; 117 cluster.updateClusterInformation(currentPointer); 118 return new int[]{SUCCESSFUL,0}; 119 } 120 121 129 130 131 public ClusterStatus delete(short recordNumber, boolean checkKeyValidity) throws DException{ 132 if(recordNumber > cluster.actualRecordCount ) 133 throw new DException("DSE2007", new Object []{cluster.toString() }); 134 byte [] clusterBytes = cluster.getBytes(); 135 short skip = moveToRecordNumber(clusterBytes,recordNumber); 136 boolean curre = clusterBytes[skip+1] != versionHandler.FULL; 137 cluster.updateByte(skip,versionHandler.DELETE); 138 cluster.activeRecordCount--; 139 cluster.updateBytes(2*versionHandler.LENGTH,CCzufDpowfsufs.getBytes(cluster.activeRecordCount)); 140 return new ClusterStatus(cluster,cluster.activeRecordCount,cluster.actualRecordCount,false,false,curre); 141 } 142 143 151 152 153 public void update(short recordNumber,byte[] newBytes) throws DException { 154 short currentPointer = (short)(moveToRecordNumber(cluster.getBytes(),recordNumber)+2); 155 cluster.updateBytes(currentPointer,newBytes); 156 } 157 158 161 162 163 166 167 168 173 public short getRecordCount() throws DException { 174 return cluster.actualRecordCount; 175 } 176 177 178 183 public int freeSpace() throws DException { 184 short spaceUsed = CCzufDpowfsufs.getShortValue(cluster.getBytes(),0); 185 return databaseProperties.CLUSTERSIZE - spaceUsed - versionHandler.NEWADDRESSLENGTH; 186 } 187 191 public Cluster getCluster(){ 192 return cluster; 193 } 194 199 public void setCluster(Cluster cluster0) throws DException{ 200 cluster = cluster0; 201 } 202 206 public short getActiveRecordCount(){ 207 return cluster.activeRecordCount; 208 } 209 214 protected short getNextInsertableAddress() throws DException { 215 return CCzufDpowfsufs.getShortValue(cluster.getBytes(),0); 216 } 217 218 224 225 protected short moveToRecordNumber(byte[] clusterBytes,short recordNumber) throws DException { 226 short skip = versionHandler.CLUSTER_STARTPOINTER; skip += moveLength * (recordNumber -1); 228 return skip ; 229 } 230 231 237 238 public void checkValidity(short recordNumber)throws DException{ 239 byte[] clusterBytes = cluster.getBytes(); 240 short pointer = moveToRecordNumber(clusterBytes,recordNumber); 241 if(clusterBytes[pointer++] == versionHandler.DELETE) 242 throw StaticExceptions.RECORD_DELETED_EXCEPTION ; 243 } 244 245 250 251 public boolean isComplete(short recordNumber)throws DException { 252 253 throw new java.lang.UnsupportedOperationException ("Method isComplete() not yet implemented."); 254 } 255 256 257 public String toString() { 258 return "Fixed RecordCluster " ; } 260 265 public int getRange() throws DException { 266 return databaseProperties.CLUSTERSIZE - CCzufDpowfsufs.getShortValue(cluster.getBytes(),0) - versionHandler.NEWADDRESSLENGTH; 267 } 268 269 276 277 public BufferRange retrieveBufferRange(short recordNumber) throws DException{ 278 byte[] clusterBytes = cluster.getBytes(); 279 short pointer = moveToRecordNumber(clusterBytes,recordNumber); 280 if(clusterBytes[pointer++] == versionHandler.DELETE ){ 281 throw StaticExceptions.RECORD_DELETED_EXCEPTION ; 282 } 283 pointer+=versionHandler.FULL_PARTIAL ; 284 int sad = tableProperties.recordSize + tableProperties.columnCount; 285 byte[] recordBytes = new byte[sad]; 286 System.arraycopy(clusterBytes,pointer,recordBytes,0,sad); 287 BufferRange range = new BufferRange(recordBytes,0,sad); 288 return range; 289 290 } 291 292 295 296 public int partialUpdate(short recordNumber, int startPosition, byte[] newBytes) throws DException { 297 298 throw new java.lang.UnsupportedOperationException ("Method partialUpdate() not yet implemented."); 299 } 300 301 public ClusterStatus partialDelete(short recordNumber) throws DException{ 302 303 throw new java.lang.UnsupportedOperationException ("Method partialDelete() not yet implemented."); 304 } 305 306 private int[] partialInsertRecord(byte[] bytes,short insertAdd,int startPosition, boolean isUpdate) throws DException { 307 308 throw new java.lang.UnsupportedOperationException ("Method partialInsertRecord() not yet implemented."); 309 } 310 311 312 public Object [] partialRetrieve(short recordNumber ) throws DException { 313 314 throw new java.lang.UnsupportedOperationException ("Method partialRetrieve() not yet implemented."); 315 } 316 317 public int getLength0fPartialRecord(short recordNumber) throws DException{ 318 319 throw new java.lang.UnsupportedOperationException ("Method getLength0fPartialRecord() not yet implemented."); 320 } 321 } 322 | Popular Tags |