KickJava   Java API By Example, From Geeks To Geeks.

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


1 package com.daffodilwoods.daffodildb.server.datasystem.indexsystem;
2
3 import com.daffodilwoods.daffodildb.server.datasystem.btree.*;
4 import com.daffodilwoods.daffodildb.utils.BufferRange;
5 import com.daffodilwoods.database.resource.DException;
6 import com.daffodilwoods.daffodildb.server.datasystem.interfaces.*;
7 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.
8
    ClusterCharacteristics;
9 import com.daffodilwoods.daffodildb.utils.byteconverter.*;
10 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.
11
    DatabaseProperties;
12 import java.util.ArrayList JavaDoc;
13 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem.
14
    versioninfo.VersionHandler;
15
16 /**
17  * FileNode class makes row of bytes for insertion of element in fileBTree node and sets column values ,
18  * tablekey,child address in element in retrieval and maintains next and previous cluster address.
19  * <p>Title: FileNode
20  */

21
22                                                                                                                                                             public class FixedFileNode
23     implements _Node {
24
25   public FixedBTreeCluster cluster;
26   protected IndexColumnInformation indexInformation;
27   protected int startPointer;
28   protected CbCUsffWbmvfIboemfs handler;
29   DatabaseProperties databaseProperties;
30   ArrayList JavaDoc BTreeElements;
31   VersionHandler versionHandler;
32
33   /*
34  Thid flag is taken while resolving a bug 12510.
35  So that node can be removed from map during insert operation of a btree
36  while loading child nodes of a non leaf node in shift right half
37  which is spliting so during loading of nodes any node can be removed
38  from map which is used in insert method.
39  So this flag is seted false for those nodes which are currently used
40  in insert method and reseted to true later while those can be
41  removed from map.
42  */

43 private boolean isCanBeRemovedFromMap ;
44
45
46
47   /**
48    * initializes cluster for insertion of bytes of element and indexinformation to get column indexes,
49    * column sizes, whether particular column is variable or not and row size.
50    * @param cluster1 to store
51    * @param iinf
52    * @throws DException
53    */

54
55   public FixedFileNode(FixedBTreeCluster cluster1, IndexColumnInformation iinf,
56                        CbCUsffWbmvfIboemfs handler0) throws DException {
57     cluster = cluster1;
58     databaseProperties = cluster.getDatabaseProperties();
59     versionHandler = cluster.getVersionHandler();
60     indexInformation = iinf;
61     startPointer = versionHandler.LENGTH +
62         indexInformation.getFixedColumns().length +
63         (indexInformation.numberOfVariableColumns() * versionHandler.LENGTH);
64     handler = handler0;
65     int length = cluster.getActiveRecordCount();
66     BTreeElements = new ArrayList JavaDoc(length);
67     for (int i = 0; i < length; i++) {
68       BTreeElements.add(i, null);
69     }
70     isCanBeRemovedFromMap = true;
71   }
72
73   public void insert(int position, BTreeElement element, boolean flag) throws
74       DException {
75     cluster.insert(position, getRow(element));
76     BTreeElements.add(position, element);
77   }
78
79   public void updateBtree(BTreeKey btreekey, Object JavaDoc newKey, Object JavaDoc newValue) throws
80       DException {
81     int position = btreekey.getPosition();
82     BTreeElement element = btreekey.getNode().getElement(position, null);
83     element.setKey(newKey);
84     element.setValue(newValue);
85     byte[] bytes = getRow(element);
86     cluster.updateFixed(position, bytes);
87     BTreeElement oldElement = (BTreeElement) BTreeElements.get(position);
88     if (oldElement != null) {
89       oldElement.setKey(newKey);
90       oldElement.setValue(newValue);
91     }
92   }
93
94   public boolean isLeafNode() {
95     return cluster.isLeafCluster();
96   }
97
98   /**
99    * Returns element at given position , sets buffer range of column values and tableKey and if nonleaf
100    * element then its child address
101    * @param position at which element is required
102    * @return BtreeElement at given position
103    * @throws DException position is greater then total number of elemnts in the current node
104    */

105
106   public BTreeElement getElement(int position) throws DException {
107     if (position >= cluster.getActiveRecordCount())
108       throw new DException("DSE2043", new Object JavaDoc[] {new Integer JavaDoc(position),
109                            new Integer JavaDoc(cluster.getActiveRecordCount())});
110     BTreeElement be = (BTreeElement) BTreeElements.get(position);
111     if (be != null) {
112       return be;
113     }
114     BufferRange bytes = cluster.getRowBuffer(position);
115     if (position == 0) {
116       FileBTreeElement element = new FileBTreeElement();
117       int add = CCzufDpowfsufs.getIntValue(bytes, 0);
118       ClusterCharacteristics cc = add == 0 ? null :
119           new ClusterCharacteristics(add, true);
120       element.setChildNodeKey(cc);
121       BTreeElements.set(position, element);
122       return element;
123     }
124     FileBTreeElement element = new FileBTreeElement();
125     int pointer = getBytesForTable(bytes, element);
126     byte[] value = new byte[handler.getLength()];
127     System.arraycopy(bytes.getFulBytes(), bytes.getOffSet() + pointer,
128                      value, 0, handler.getLength());
129     element.setValue(handler.getObject(value));
130     if (!cluster.isLeafCluster()) {
131       pointer += handler.getLength();
132       int addres = CCzufDpowfsufs.getIntValue(bytes.getFulBytes(),
133                                              bytes.getOffSet() + pointer);
134       ClusterCharacteristics childKey = addres == 0 ? null :
135           new ClusterCharacteristics(addres, true);
136       element.setChildNodeKey(childKey);
137     }
138     BTreeElements.set(position, element);
139     return element;
140   }
141
142   public void delete(int position) throws DException {
143
144     cluster.delete(position);
145      BTreeElements.remove(position);
146
147   }
148
149   protected byte[] getRow(BTreeElement element) throws DException {
150     int columnCount = indexInformation.getFixedColumns().length;
151     Object JavaDoc tableColumns = element.getKey();
152     int[] sizeOfColumns = indexInformation.getSizeOfColumns();
153     int totalLength = getTotalLength(tableColumns, columnCount, sizeOfColumns);
154     byte[] row = new byte[totalLength];
155     byte[] bb = CCzufDpowfsufs.getBytes( (short) (totalLength));
156     System.arraycopy(bb, 0, row,
157                      0, versionHandler.LENGTH);
158
159     int pointer = 0;
160     pointer = columnCount == 1 ?
161         putSingleColumnValues( (BufferRange) tableColumns, row, sizeOfColumns)
162         : putColumnValues( (BufferRange[]) tableColumns, row, sizeOfColumns);
163     Object JavaDoc key = element.getValue();
164     System.arraycopy(handler.getBytes(key), 0, row, pointer, handler.getLength());
165     if (!cluster.isLeafCluster()) {
166       ClusterCharacteristics cc = (ClusterCharacteristics) element.
167           getChildNodeKey();
168       if (cc != null) {
169         int childAddress = cc.getStartAddress();
170         System.arraycopy(CCzufDpowfsufs.getBytes(childAddress), 0, row,
171                          pointer + handler.getLength(),
172                          versionHandler.CHILD_LENGTH);
173       }
174     }
175     return row;
176   }
177
178   protected int getTotalLength(Object JavaDoc tableColumns, int numberOfColumns,
179                                int[] sizeofColumns) throws DException {
180     int totalLength = startPointer + handler.getLength()+indexInformation.getRecordSize();
181     if (!cluster.isLeafCluster())
182       totalLength += versionHandler.CHILD_LENGTH;
183     return totalLength;
184   }
185
186   private int putSingleColumnValues(BufferRange tableColumns, byte[] row,
187                                     int[] sizeOfColumns) throws DException {
188     int pointer = startPointer;
189     row[versionHandler.LENGTH] = tableColumns.getNull() ? versionHandler.NULL :
190         versionHandler.NOTNULL;
191     int s = sizeOfColumns[0];
192     if (tableColumns.getNull() == false)
193       System.arraycopy(tableColumns.getFulBytes(), tableColumns.getOffSet(),
194                        row, pointer, s);
195     pointer += s;
196     return pointer;
197   }
198
199   private int putColumnValues(BufferRange[] tableColumns, byte[] row,
200                               int[] sizeOfColumns) throws DException {
201     int pointer = startPointer;
202     for (int i = 0,s; i < tableColumns.length; i++) {
203       row[i + versionHandler.LENGTH] = tableColumns[i].getNull() ?
204           versionHandler.NULL : versionHandler.NOTNULL;
205       s = sizeOfColumns[i];
206
207       if (tableColumns[i].getNull() == false)
208         System.arraycopy(tableColumns[i].getFulBytes(),
209                          tableColumns[i].getOffSet(), row, pointer, s);
210       pointer += s;
211     }
212     return pointer;
213   }
214
215   protected int getBytesForTable(BufferRange bytes, BTreeElement element) throws
216       DException {
217     int[] sizeOfColumns = indexInformation.getSizeOfColumns();
218     int count = sizeOfColumns.length;
219     int offset = bytes.getOffSet();
220     BufferRange[] array = new BufferRange[count];
221     int pointer = count;
222     for (int i = 0,columnSize; i < count; i++) {
223       columnSize = sizeOfColumns[i];
224       array[i] = bytes.getByte(i) == versionHandler.NOTNULL ?
225           new BufferRange(bytes.getFulBytes(), pointer + offset, columnSize)
226           : new BufferRange(true);
227       pointer += columnSize;
228
229     }
230     if (count == 1)
231       element.setKey(array[0]);
232     else
233       element.setKey(array);
234     return pointer;
235   }
236
237
238   public void updateNode(int position, boolean flag) throws DException {
239   }
240
241   /**
242    * Returns parent node of current node
243    * @return parent node of current node
244    */

245
246   public Object JavaDoc getParentNode(_DatabaseUser user) throws DException {
247     return cluster.getParentKey();
248   }
249
250   /**
251    * Returns total number of elements in this node also includes dummy element
252    * @return total number of elements in this node
253    */

254
255   public final int getElementCount() throws DException {
256     return cluster.getActiveRecordCount();
257   }
258
259   /**
260    * Creates new Element having key and value
261    * @param key column values of btree
262    * @param value Memory Table Key
263    * @return new element having key and value
264    */

265   public BTreeElement createElement(boolean leaf, _DatabaseUser user,
266                                     Object JavaDoc key, Object JavaDoc value,
267                                     BTreeNode leftNode, BTreeNode rightNode) throws
268       DException {
269     return new FileBTreeElement(key, value);
270   }
271
272   /**
273    * returns level of node in Btree
274    * @return level of node in Btree
275    */

276   public short getLevel() {
277     return cluster.getLevel();
278   }
279
280   /**
281    * Sets level of node in Btree
282    * @param level level of node in Btree
283    */

284
285   public void setLevel(short level) throws DException {
286     cluster.setLevel(level);
287   }
288
289   public void updateElementCount(boolean increase) throws DException {
290   }
291
292   /**
293    * It returns Split point of node , It is used in Breaking of nodes , it means element after split
294    * count should be shifted in new node.
295    * @return split point of node
296    */

297
298   public int getSplitPoint() throws DException {
299     int cnt = cluster.getActiveRecordCount();
300     cnt = cnt > 10 ? ( (cnt - 1) * 4) / 5 : (cnt - 1) / 2;
301     return cnt;
302   }
303
304
305   public void insertRange(Object JavaDoc elemnts, int startPosition, int endPosition) throws
306       DException {
307     cluster.insertRange( (byte[]) elemnts, startPosition, endPosition);
308     for (int i = 1; i <= endPosition - startPosition + 1; i++) {
309       BTreeElements.add(i, null);
310     }
311   }
312
313   public void deleteRange(int startPosition, int endPosition) throws DException {
314     cluster.deleteRange(startPosition, endPosition);
315     for (int i = BTreeElements.size()-1; i > endPosition; i--) {
316         BTreeElements.remove(i);
317     }
318   }
319
320   public Object JavaDoc getElements() throws DException {
321     return cluster.getBytes();
322   }
323
324   public Object JavaDoc getNextNode(_DatabaseUser user) throws DException {
325     return cluster.getNextClusterCharacteristics();
326   }
327
328   public Object JavaDoc getPreviousNode(_DatabaseUser user) throws DException {
329     return cluster.getPreviousClusterCharacteristics();
330   }
331
332   public void setNextNode(BTreeNode node) throws DException {
333     int nextNodeAddress = ( (ClusterCharacteristics) node.getNodeKey()).
334         getStartAddress();
335     cluster.updateBytes(databaseProperties.NEXTCLUSTERADDRESS,
336                         CCzufDpowfsufs.getBytes(nextNodeAddress));
337   }
338
339   public void setPreviousNode(BTreeNode node) throws DException {
340     int previousNodeAddress = ( (ClusterCharacteristics) node.getNodeKey()).
341         getStartAddress();
342     cluster.updateBytes(versionHandler.PREVIOUSCLUSTERADDRESS,
343                         CCzufDpowfsufs.getBytes(previousNodeAddress));
344   }
345
346   public Object JavaDoc getNodeKey() throws DException {
347     return cluster.getClusterCharacteristics();
348   }
349
350   public void setLeafNode(boolean flag) throws DException {
351     cluster.setLeafNode(flag);
352   }
353
354   /**
355    * makes bytes for dummy Element if element has child node address then sets child address bytes in
356    * bytes of dummy element .
357    * @param element dummy element which has to insert
358    */

359
360   public void insertDummyElement(BTreeElement element) throws DException {
361     BTreeNode node = element.getChild();
362     byte[] dummyElmentBytes = new byte[3 * versionHandler.LENGTH];
363     System.arraycopy(CCzufDpowfsufs.getBytes( (short) dummyElmentBytes.length),
364                      0, dummyElmentBytes, 0, versionHandler.LENGTH);
365     if (node == null) {
366       cluster.insert(0, dummyElmentBytes);
367     }
368     else {
369       ClusterCharacteristics cc = (ClusterCharacteristics) node.getNodeKey();
370       System.arraycopy(CCzufDpowfsufs.getBytes(cc.getStartAddress()), 0,
371                        dummyElmentBytes, versionHandler.LENGTH,
372                        versionHandler.NEWADDRESSLENGTH);
373       cluster.insert(0, dummyElmentBytes);
374     }
375     BTreeElements.add(0, null);
376   }
377
378   public String JavaDoc toString() {
379     try {
380       String JavaDoc str = /*"NODE ADDRESS : "+getNodeKey()+*/ " ISLEAF NODE :: " +
381           isLeafNode();
382       int count = getElementCount();
383       for (int i = 0; i < count; i++) {
384         BTreeElement element = getElement(i);
385         str += "[" + element + "]";
386       }
387       return str;
388     }
389     catch (DException ex) {
390     }
391     return null;
392   }
393
394   public void setParentNode(BTreeNode node) throws DException {
395     if (node != null)
396       cluster.setParentNode( (ClusterCharacteristics) node.getNodeKey());
397   }
398
399   public void updateChild(int position, Object JavaDoc key) throws DException {
400     cluster.updateChild(position, key);
401   }
402 /*
403   public static void main(String[] rgas) {
404     boolean flag = true;
405     new java.util.HashMap(flag ? 1 : 2);
406   }
407
408   /**
409    * Returns column values of index columns at given position
410    * @param position at which column values of index columns are required
411    * @return column values of index columns at given position
412    * @throws DException If position is greater than total elements in node
413    */

414   public Object JavaDoc getKey(int position) throws DException {
415     return getElement(position).getKey();
416   }
417
418   /**
419    * Returns child node address of element at given position . if no child exists then returns null
420    * @param position at which child node address is required
421    * @return child node address of element at given position
422    * @throws DException if position is greater than total number of elements in this node
423    */

424   public Object JavaDoc getChildNodeKey(int position) throws DException {
425     if (position >= cluster.getActiveRecordCount())
426       throw new DException("DSE2043", new Object JavaDoc[] {new Integer JavaDoc(position),
427                            new Integer JavaDoc(cluster.getActiveRecordCount())});
428     return getElement(position).getChildNodeKey();
429   }
430
431
432
433   /**
434    * returns tableKey of element at given position
435    * @param position at which tableKey of element is required
436    * @return tableKey of element at given position
437    * @throws DException
438    */

439   public Object JavaDoc getValue(int position) throws DException {
440     if (position >= cluster.getActiveRecordCount())
441       throw new DException("DSE2043", new Object JavaDoc[] {new Integer JavaDoc(position),
442                            new Integer JavaDoc(cluster.getActiveRecordCount())});
443     if (position == 0)
444       return null;
445     return getElement(position).getValue();
446   }
447
448   public int getFreespace() throws DException {
449     return cluster.freeSpace();
450   }
451
452   public FixedBTreeCluster getCluster() {
453     return cluster;
454   }
455
456   public void clearReferences() {
457     for (int i = 0; i < BTreeElements.size(); i++) {
458       FileBTreeElement element = (FileBTreeElement) BTreeElements.set(i, null);
459       if (element != null)
460         element.clearChild();
461     }
462   }
463
464   public void clearCache(){
465     for (int i = BTreeElements.size()-1; i >= 0; i--) {
466       BTreeElements.remove(i);
467     }
468   }
469   public void setIsNodeCanBeRemovedFromMap(boolean isNodeCanBeRemovedFromMap){
470   isCanBeRemovedFromMap = isNodeCanBeRemovedFromMap;
471 }
472
473 public boolean isNodeCanBeRemovedFromMap(){
474   return isCanBeRemovedFromMap;
475 }
476
477
478 }
479
Popular Tags