KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > datasystem > persistentsystem > ColumnBytesTable


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 JavaDoc;
12 import com.daffodilwoods.daffodildb.utils.FieldUtility;
13 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.versioninfo.VersionHandler;
14
15 /**
16  *
17    It is used to convert columnBytes into rowBytes While writting and to convert row bytes into column
18    bytes while retrieving it.
19
20    */

21 public class ColumnBytesTable implements _TableList{
22
23   /**
24    * table to perform write and read operations
25    */

26
27   private _TableList table;
28
29   /**
30    * to get column count and column information of table
31    */

32
33   private TableProperties tableProperties;
34
35   /**
36    * Table Charactersitics of This Table
37    */

38   private _TableCharacteristics tableCharacteristics;
39
40   /**
41    * code of table to identify it
42    */

43
44   private int CODE;
45
46   /**
47    * Name Of Table
48    */

49
50   /**
51    * For Optimization
52    * length of header of record for variable type table
53    */

54
55   private int length;
56
57   /**
58    * For Optimization
59    * length of header of record for fixed type table
60    */

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   /**
77    * Converts column bytes into rowBytes and inserts rowbytes in table
78    *
79    * @param values 2d array of bytes which has to be inserted
80    *
81    * @returns tablekey at which record is inserted
82    */

83
84   public Object JavaDoc insert(_DatabaseUser user, _RecordCluster recordCluster,Object JavaDoc values)throws DException {
85     return table.insert(user,recordCluster,getRowBytes((BufferRange[])values));
86   }
87
88   /**
89    * Converts column bytes into rowBytes and updates rowbytes in table at given key
90    *
91    * @param values 2d array of bytes which has to update
92    * @return tablekey at which record is updated
93    */

94
95   public Object JavaDoc update(_DatabaseUser user ,_RecordCluster recordCluster,Object JavaDoc key, Object JavaDoc values) throws DException {
96     return table.update(user,recordCluster, key,getRowBytes((BufferRange[])values));
97   }
98
99   /**
100    * deletes Record from given key
101    */

102
103   public Object JavaDoc delete(_DatabaseUser user, _RecordCluster recordCluster, Object JavaDoc key)throws DException {
104     return table.delete(user,recordCluster,key);
105   }
106
107   /**
108    * It converts columnbytes(2D array) into rowbytes(1D array of bytes)
109    * in case of Fixed Table calls another method getRowBytesOfFixedTable
110    * else in case of variable table checks for various columns as they are fixed
111    * or variable sets bytes in the rowBytes array accordingly
112    * set bytes NULL/NOTNULL for each column in front of the actual record and returns the comlete array of bytes
113    *
114    * @param bytes 2D array of bytes to be inserted as a record
115    * @return byte[] containing bytes corresponding to a row or record
116    */

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   /**
151    * It converts columnbytes(2D array) into rowbytes(1D array of bytes) for the fixed table
152    * set bytes NULL/NOTNULL for each column in front of the actual record and returns the complete array of bytes
153    *
154    * @param bytes 2D array of bytes to be inserted as a record
155    * @return byte[] containing bytes corresponding to a row or record
156    */

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   /**
176    * Gets column values from the Persistent table in the form of rowbytes and converts
177    * it into column bytes before returning it to the upper class
178    *
179    * If the table is fixed table then it calls the method getFieldsForTableWithFixedCloumns
180    * for coverting row bytes of the fixed table into column bytes
181    *
182    * elseif table is variable table then it calls the method getFieldsForTableWithVariableCloumns
183    * for coverting row bytes of the variable table into column bytes
184    *
185    * if the satrt address in the table key is set to 0 then throws exception to the upper class
186    * @param key table key of the record for which column values are to be retrieved
187
188    * @return Array of BufferRange containing column values
189    */

190
191
192   public Object JavaDoc 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   /**
199    * Converts row bytes into column bytes for fixed table.It removes the NULL/NOTNULL
200    * bytes appended in the row bytes and fetches the exact bytes of the column
201    * in the form of bufferRange
202    * if the column is NULL then it retuns NULLBUFFERRANGE for that particular column
203    * and then the complete array of the BufferRange is returned
204    *
205    * @param bufferRange bufferRange containing the rowbytes
206    *
207    * @return an array of BufferRange with values for each column present in the table
208    */

209
210   private Object JavaDoc getFieldsForTableWithFixedCloumns(BufferRange bufferRange) throws DException{
211     int count = tableCharacteristics.getColumnCount();
212     int rowRangeOffset = bufferRange.getOffSet();
213     int pointer = count;
214     int columnSize ;
215     Object JavaDoc[] 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   /**
226    * Converts row bytes into column bytes for Variable table.It removes the NULL/NOTNULL
227    * bytes appended in the row bytes and fetches the exact bytes of the column
228    * in the form of bufferRange
229    *
230    * ColumnSize in case of a fixed column is taken from the TableProperties class
231    * else it is read from the the bytes appended after the NULL/NOTNULL bytes for
232    * the size of the variable column and for that a pointer varLengthPointer is maintained
233    * one more pointer is maintained to read the actual bytes of the column present in the BufferRange
234    *
235    * if the column is NULL then it retuns NULLBUFFERRANGE for that particular column
236    * and then the complete array of the BufferRange is returned
237    *
238    * @param bufferRange bufferRange containing the rowbytes
239    *
240    * @return an array of BufferRange with values for each column present in the table
241    */

242
243   private Object JavaDoc 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 JavaDoc[] 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   /**
265    * Get column values for a particular column.It first of all calls getColumnValues()
266    * method to get an array of bufferRange for all the columns but then returns a BufferRange
267    * for a particular column index
268    *
269    * @param key table key of the record for which column values are to be retrieved
270    * @param column for which vaule is to be retrieved in a particular record
271    *
272    * @return BufferRange of the particular column value
273    */

274
275   public Object JavaDoc getColumnValues(TableKey key,_RecordCluster recordCluster,int column) throws DException{
276     return ((Object JavaDoc[])getColumnValues(key,recordCluster))[column];
277   }
278
279   /**
280    * Get column values for some columns whose indexes are given in the int[] .It first of all calls getColumnValues()
281    * method to get an array of bufferRange for all the columns and then returns a reuired
282    * array with values of those columns only for which value is required
283    *
284    * @param key table key of the record for which column values are to be retrieved
285    * @param columns int[] containing indexes of the columns for which values is to be fetched
286    *
287    * @return BufferRange[] for the column values required
288    */

289
290   public Object JavaDoc getColumnValues(TableKey key,_RecordCluster recordCluster,int[] columns) throws DException{
291     Object JavaDoc[] buffers = (Object JavaDoc[])getColumnValues(key,recordCluster);
292     Object JavaDoc[] 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   /**
299    * returns table having specified code
300    * @param num code of table which is required
301    * @return table having specified code
302    */

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 JavaDoc 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