KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > daffodilwoods > daffodildb > server > datasystem > indexsystem > VariableFileNode


1 package com.daffodilwoods.daffodildb.server.datasystem.indexsystem;
2
3 import com.daffodilwoods.daffodildb.utils.byteconverter.CbCUsffWbmvfIboemfs;
4 import com.daffodilwoods.database.resource.DException;
5 import com.daffodilwoods.daffodildb.server.datasystem.btree.BTreeElement;
6 import com.daffodilwoods.daffodildb.utils.byteconverter.CCzufDpowfsufs;
7 import com.daffodilwoods.daffodildb.utils.BufferRange;
8 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.
9
    ClusterCharacteristics;
10 import com.daffodilwoods.daffodildb.utils.FieldUtility;
11 import com.daffodilwoods.daffodildb.server.datasystem.btree.BTreeKey;
12
13 public class VariableFileNode
14     extends FixedFileNode {
15
16   public VariableFileNode(FixedBTreeCluster cluster1,IndexColumnInformation iinf, CbCUsffWbmvfIboemfs handler0) throws DException {
17    super(cluster1, iinf, handler0);
18   }
19
20   protected int getBytesForTable(BufferRange bytes, BTreeElement element) throws DException {
21     boolean[] fixedColumns = indexInformation.getFixedColumns();
22     int[] sizeOfColumns = indexInformation.getSizeOfColumns();
23     int count = fixedColumns.length;
24     int offset = bytes.getOffSet();
25     BufferRange[] array = new BufferRange[count];
26     int varLengthPointer = count;
27     int pointer = startPointer - versionHandler.LENGTH;
28     for (int i = 0; i < count; i++) {
29       int columnsSize = 0;
30       if (fixedColumns[i]) {
31         columnsSize = sizeOfColumns[i];
32       }
33       else {
34           columnsSize = CCzufDpowfsufs.getShortValue(bytes, varLengthPointer);
35         varLengthPointer += versionHandler.LENGTH;
36       }
37         array[i] = bytes.getByte(i) == versionHandler.NULL ? FieldUtility.NULLBUFFERRANGE :
38             new BufferRange(bytes.getFulBytes(), pointer + offset, columnsSize);
39
40       pointer += columnsSize;
41     }
42     if (count == 1)
43       element.setKey(array[0]);
44     else
45       element.setKey(array);
46     return pointer;
47   }
48
49   /**
50    * To store BtreeElement in Cluster makes single dimension byte array consisting column values, tableKey
51    * and child element address
52    *
53    * @param ele which has to store in file node
54    * @return single dimension byte[] having column values, tableKey and child element address
55    */

56
57   protected byte[] getRow(BTreeElement ele) throws DException {
58     FileBTreeElement element = (FileBTreeElement) ele;
59     boolean[] fixedColumns = indexInformation.getFixedColumns();
60     Object JavaDoc tableColumns = element.getKey();
61     int[] sizeOfColumns = indexInformation.getSizeOfColumns();
62     int totalLength = getTotalLength(tableColumns, fixedColumns, sizeOfColumns);
63     byte[] row = new byte[totalLength];
64     System.arraycopy(CCzufDpowfsufs.getBytes( (short) (totalLength)), 0, row, 0,
65                      versionHandler.LENGTH);
66     int pointer = fixedColumns.length == 1 ?
67         putSingleColumnValues( (BufferRange) tableColumns, row, fixedColumns,
68                               sizeOfColumns)
69         :
70         putColumnValues( (BufferRange[]) tableColumns, row, fixedColumns,
71                         sizeOfColumns);
72     Object JavaDoc key = element.getValue();
73       System.arraycopy(handler.getBytes(key), 0, row, pointer, handler.getLength());
74     if (!cluster.isLeafCluster()) {
75       ClusterCharacteristics cc = (ClusterCharacteristics) element.
76           getChildNodeKey();
77       if (cc != null) {
78         int childAddress = cc.getStartAddress();
79         System.arraycopy(CCzufDpowfsufs.getBytes(childAddress), 0, row,
80                          pointer + handler.getLength(), versionHandler.CHILD_LENGTH);
81       }
82     }
83
84     return row;
85   }
86
87   /**
88    * returns total length of byte array to store element in file node.
89    * @param tableColumns bufferRange of column Values on which columns Btree is made
90    * @param fixedColumns whether particular column is of fixed type or not
91    * @param sizeofColumns size of columns
92    * @return total length of byte array to store element in file node.
93    */

94
95   protected int getTotalLength(Object JavaDoc tableColumns, boolean[] fixedColumns,
96                                int[] sizeofColumns) throws DException {
97     int numberOfColumns = fixedColumns.length;
98     int totalLength = startPointer + handler.getLength();
99     if (numberOfColumns == 1)
100       totalLength += ( (BufferRange) tableColumns).getLength();
101     else {
102       BufferRange[] columnValues = (BufferRange[]) tableColumns;
103       for (int i = 0; i < numberOfColumns; i++)
104         totalLength += fixedColumns[i] ? sizeofColumns[i] :
105             columnValues[i].getLength();
106     }
107     if (!cluster.isLeafCluster())
108       totalLength += versionHandler.CHILD_LENGTH;
109     return totalLength;
110   }
111
112   /**
113    * makes row of byte[] having column values, with the help of fixedColumns and size of columns array
114    * makes single dimension byte array from buffer Range of column Values. for all columns of btree puts
115    * null and not null byte in starting depending on whether column values are null or not null, if
116    * columns are of variable size then puts length of column value also.
117    * @param tableColumns bufferrange of column values of btree columns
118    * @param row byte Array in which bytes of columns has to set
119    * @param fixedColumns
120    * @param sizeOfColumns
121    * @return
122    * @throws DException
123    */

124   private int putColumnValues(BufferRange[] tableColumns, byte[] row,
125                               boolean[] fixedColumns, int[] sizeOfColumns) throws
126       DException {
127     int varLengthPointer = versionHandler.LENGTH + tableColumns.length;
128     int pointer = startPointer;
129     for (int i = 0; i < tableColumns.length; i++) {
130       row[i + versionHandler.LENGTH] = tableColumns[i].getNull() ? versionHandler.NULL : versionHandler.NOTNULL;
131       if (fixedColumns[i]) {
132         int s = sizeOfColumns[i];
133         if (tableColumns[i].getNull() == false)
134             System.arraycopy(tableColumns[i].getFulBytes(),
135                              tableColumns[i].getOffSet(), row, pointer, s);
136         pointer += s;
137       }
138       else {
139         if (!tableColumns[i].getNull()) {
140           short len = (short) tableColumns[i].getBytes().length;
141           System.arraycopy(CCzufDpowfsufs.getBytes(len), 0, row,
142                            varLengthPointer, versionHandler.LENGTH);
143             System.arraycopy(tableColumns[i].getBytes(), 0, row, pointer, len);
144           pointer += len;
145         }
146         varLengthPointer += versionHandler.LENGTH;
147       }
148     }
149     return pointer;
150   }
151
152   private int putSingleColumnValues(BufferRange tableColumns, byte[] row,
153                                     boolean[] fixedColumns, int[] sizeOfColumns) throws
154       DException {
155     int varLengthPointer = versionHandler.LENGTH + 1;
156     int pointer = startPointer;
157     row[versionHandler.LENGTH] = tableColumns.getNull() ? versionHandler.NULL : versionHandler.NOTNULL;
158     if (!tableColumns.getNull()) {
159       short len = (short) tableColumns.getLength();
160       System.arraycopy(CCzufDpowfsufs.getBytes(len), 0, row, varLengthPointer,
161                        versionHandler.LENGTH);
162         System.arraycopy(tableColumns.getFulBytes(), tableColumns.getOffSet(),
163                          row, pointer, len);
164       pointer += len;
165     }
166     varLengthPointer += versionHandler.LENGTH;
167     return pointer;
168   }
169
170
171   public void updateBtree(BTreeKey btreekey,Object JavaDoc newKey,Object JavaDoc newValue) throws DException {
172     int position = btreekey.getPosition();
173    BTreeElement element = createElement(false,null,newKey,newValue,null,null);
174     byte[] bytes = getRow(element);
175     cluster.updateVariable(position,bytes);
176     BTreeElement oldElement = (BTreeElement) BTreeElements.get(position);
177     if (oldElement != null) {
178       oldElement.setKey(newKey);
179       oldElement.setValue(newValue);
180     }
181     }
182
183
184 }
185
Popular Tags