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 ; 13 import com.daffodilwoods.daffodildb.server.datasystem.persistentsystem. 14 versioninfo.VersionHandler; 15 16 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 BTreeElements; 31 VersionHandler versionHandler; 32 33 43 private boolean isCanBeRemovedFromMap ; 44 45 46 47 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 (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 newKey, Object 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 105 106 public BTreeElement getElement(int position) throws DException { 107 if (position >= cluster.getActiveRecordCount()) 108 throw new DException("DSE2043", new Object [] {new Integer (position), 109 new Integer (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 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 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 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 245 246 public Object getParentNode(_DatabaseUser user) throws DException { 247 return cluster.getParentKey(); 248 } 249 250 254 255 public final int getElementCount() throws DException { 256 return cluster.getActiveRecordCount(); 257 } 258 259 265 public BTreeElement createElement(boolean leaf, _DatabaseUser user, 266 Object key, Object value, 267 BTreeNode leftNode, BTreeNode rightNode) throws 268 DException { 269 return new FileBTreeElement(key, value); 270 } 271 272 276 public short getLevel() { 277 return cluster.getLevel(); 278 } 279 280 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 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 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 getElements() throws DException { 321 return cluster.getBytes(); 322 } 323 324 public Object getNextNode(_DatabaseUser user) throws DException { 325 return cluster.getNextClusterCharacteristics(); 326 } 327 328 public Object 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 getNodeKey() throws DException { 347 return cluster.getClusterCharacteristics(); 348 } 349 350 public void setLeafNode(boolean flag) throws DException { 351 cluster.setLeafNode(flag); 352 } 353 354 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 toString() { 379 try { 380 String str = " 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 key) throws DException { 400 cluster.updateChild(position, key); 401 } 402 414 public Object getKey(int position) throws DException { 415 return getElement(position).getKey(); 416 } 417 418 424 public Object getChildNodeKey(int position) throws DException { 425 if (position >= cluster.getActiveRecordCount()) 426 throw new DException("DSE2043", new Object [] {new Integer (position), 427 new Integer (cluster.getActiveRecordCount())}); 428 return getElement(position).getChildNodeKey(); 429 } 430 431 432 433 439 public Object getValue(int position) throws DException { 440 if (position >= cluster.getActiveRecordCount()) 441 throw new DException("DSE2043", new Object [] {new Integer (position), 442 new Integer (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 |