1 package com.daffodilwoods.daffodildb.server.datasystem.persistentsystem; 2 3 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*; 4 import com.daffodilwoods.daffodildb.server.datadictionarysystem.SystemTables; 5 import com.daffodilwoods.database.utility.P; 6 import com.daffodilwoods.daffodildb.utils.byteconverter.*; 7 import com.daffodilwoods.database.general.*; 8 import com.daffodilwoods.database.resource.*; 9 import com.daffodilwoods.daffodildb.server.sql99.utils._Reference; 10 import com.daffodilwoods.daffodildb.utils.BufferRange; 11 import java.io.InputStream ; 12 import com.daffodilwoods.daffodildb.utils.FieldUtility; 13 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.versioninfo.VersionHandler; 14 15 21 public class ColumnBytesTable implements _TableList{ 22 23 26 27 private _TableList table; 28 29 32 33 private TableProperties tableProperties; 34 35 38 private _TableCharacteristics tableCharacteristics; 39 40 43 44 private int CODE; 45 46 49 50 54 55 private int length; 56 57 61 62 private int totalLengthForFixed; 63 64 65 private VersionHandler versionHandler; 66 67 public ColumnBytesTable(QualifiedIdentifier tableName0,int CODE1,TableProperties tableProperties0,_TableCharacteristics tableCharacteristics0,VersionHandler versionHandler0) { 68 CODE = CODE1; 69 tableProperties = tableProperties0; 70 tableCharacteristics = tableCharacteristics0; 71 length = tableProperties.columnCount + tableProperties.variableColumns*versionHandler0.LENGTH; 72 totalLengthForFixed = length + tableProperties.recordSize; 73 versionHandler = versionHandler0; 74 } 75 76 83 84 public Object insert(_DatabaseUser user, _RecordCluster recordCluster,Object values)throws DException { 85 return table.insert(user,recordCluster,getRowBytes((BufferRange[])values)); 86 } 87 88 94 95 public Object update(_DatabaseUser user ,_RecordCluster recordCluster,Object key, Object values) throws DException { 96 return table.update(user,recordCluster, key,getRowBytes((BufferRange[])values)); 97 } 98 99 102 103 public Object delete(_DatabaseUser user, _RecordCluster recordCluster, Object key)throws DException { 104 return table.delete(user,recordCluster,key); 105 } 106 107 117 118 private byte[] getRowBytes(BufferRange[] bytes){ 119 if(tableProperties.fixedTable) 120 return getRowBytesOfFixedTable(bytes); 121 int totalLength = 0; 122 for(int i = 0 ; i < bytes.length ; i++){ 123 totalLength += tableProperties.fixed[i] ? tableProperties.size[i] : bytes[i].getNull() ? 0 : bytes[i].getLength(); 124 } 125 byte[] recordBytes = new byte[totalLength+length]; 126 for(int i = 0,pointer = length,varLenPointer = tableProperties.columnCount; i < bytes.length;i++){ 127 recordBytes[i] = bytes[i].getNull() ? versionHandler.NULL : versionHandler.NOTNULL; 128 if(tableProperties.fixed[i]){ 129 if(bytes[i].getNull() == false ) 130 System.arraycopy(bytes[i].getFulBytes(),bytes[i].getOffSet(),recordBytes,pointer,tableProperties.size[i]); 131 pointer += tableProperties.size[i]; 132 } 133 else{ 134 if(!bytes[i].getNull()){ 135 short len = (short)bytes[i].getLength(); 136 System.arraycopy(CCzufDpowfsufs.getBytes(len),0,recordBytes,varLenPointer,versionHandler.LENGTH); 137 System.arraycopy(bytes[i].getFulBytes(), bytes[i].getOffSet(),recordBytes, pointer, len); 138 pointer += len; 139 } 140 else{ 141 System.arraycopy(CCzufDpowfsufs.getBytes((short)0),0,recordBytes,varLenPointer,versionHandler.LENGTH); 142 } 143 varLenPointer += versionHandler.LENGTH; 144 } 145 } 146 147 return recordBytes; 148 } 149 150 157 158 private byte[] getRowBytesOfFixedTable(BufferRange[] bytes){ 159 int pointer = length; 160 byte[] recordBytes = new byte[totalLengthForFixed]; 161 for(int i = 0; i < bytes.length;i++){ 162 int s = tableProperties.size[i]; 163 if(bytes[i].getNull()){ 164 recordBytes[i] = versionHandler.NULL ; 165 } 166 else{ 167 recordBytes[i] = versionHandler.NOTNULL; 168 System.arraycopy(bytes[i].getFulBytes(),bytes[i].getOffSet(),recordBytes,pointer,s); 169 } 170 pointer += s; 171 } 172 return recordBytes; 173 } 174 175 190 191 192 public Object getColumnValues(TableKey key,_RecordCluster recordCluster) throws DException{ 193 return (tableProperties.fixedTable) ? 194 getFieldsForTableWithFixedCloumns((BufferRange)table.getColumnValues(key,recordCluster)) : 195 getFieldsForTableWithVariableCloumns((BufferRange)(table.getColumnValues(key,recordCluster))); 196 } 197 198 209 210 private Object getFieldsForTableWithFixedCloumns(BufferRange bufferRange) throws DException{ 211 int count = tableCharacteristics.getColumnCount(); 212 int rowRangeOffset = bufferRange.getOffSet(); 213 int pointer = count; 214 int columnSize ; 215 Object [] buffers = new BufferRange[count]; 216 for(int i = 0 ; i < count ; i++){ 217 columnSize = tableProperties.size[i]; 218 buffers[i] = bufferRange.getByte(i) == versionHandler.NULL ? FieldUtility.NULLBUFFERRANGE 219 : new BufferRange(bufferRange.getFulBytes(),pointer + rowRangeOffset,columnSize); 220 pointer += columnSize; 221 } 222 return buffers; 223 } 224 225 242 243 private Object getFieldsForTableWithVariableCloumns(BufferRange range) throws DException{ 244 int rowBufferOffSet = range.getOffSet(); 245 int count = tableCharacteristics.getColumnCount(); 246 int varLengthPointer = count ; 247 int pointer = length ; 248 int columnsSize; 249 Object [] buffers = new BufferRange[count]; 250 for(int i = 0 ; i < count ; i++){ 251 columnsSize = tableProperties.fixed[i] ? tableProperties.size[i] 252 : range.getByte(i) == versionHandler.NULL ? 0 : 253 CCzufDpowfsufs.getShortValue(range.getFulBytes(), rowBufferOffSet + varLengthPointer); 254 buffers[i] = range.getByte(i) == versionHandler.NULL ? 255 FieldUtility.NULLBUFFERRANGE 256 : 257 new BufferRange(range.getFulBytes(), pointer + rowBufferOffSet, columnsSize); 258 varLengthPointer += tableProperties.fixed[i]? 0: versionHandler.LENGTH ; 259 pointer += columnsSize; 260 } 261 return buffers; 262 } 263 264 274 275 public Object getColumnValues(TableKey key,_RecordCluster recordCluster,int column) throws DException{ 276 return ((Object [])getColumnValues(key,recordCluster))[column]; 277 } 278 279 289 290 public Object getColumnValues(TableKey key,_RecordCluster recordCluster,int[] columns) throws DException{ 291 Object [] buffers = (Object [])getColumnValues(key,recordCluster); 292 Object [] requiredArray = new BufferRange[columns.length]; 293 for(int i =0; i < columns.length ; i++) 294 requiredArray[i] = buffers[columns[i]]; 295 return requiredArray; 296 } 297 298 303 304 public _TableList getTable(int num) { 305 return CODE == num ? table : table.getTable(num); 306 } 307 308 public void setTable(_TableList table0){ 309 table = table0; 310 } 311 312 public int getColumnCount() throws DException{ 313 return tableProperties.columnCount; 314 } 315 316 public void checkValidity(_RecordCluster recordCluster, Object key) throws DException{ 317 table.checkValidity(recordCluster,key); 318 } 319 320 public _TableList getTable() { 321 return table; 322 } 323 324 public TableProperties getTableProperties() throws DException{ 325 return tableProperties; 326 } 327 328 public _TableCharacteristics getTableCharacteristics() throws DException { 329 return tableCharacteristics; 330 } 331 332 public void rollBack() throws DException { 333 table.rollBack(); 334 } 335 336 } 337 | Popular Tags |