1 8 package com.jofti.btree; 9 10 11 12 26 class IndexNode extends Node { 27 28 private IndexNodeEntry parentKey = null; 29 30 protected Object [] keyList = new Object [BTree.getMaxNodeSize()+1]; 31 32 33 34 private Comparable rightValue = null; 35 37 private NodeLink nextNode = null; 38 39 private boolean deleted =false; 40 41 44 public boolean isDeleted() 45 { 46 return deleted; 47 } 48 49 52 public void setDeleted(boolean deleted) 53 { 54 this.deleted = deleted; 55 } 56 57 58 protected IndexNode() { 59 } 60 61 62 63 71 public Node getContainingNode(Comparable entry) { 72 73 if (entryNumber ==0) 74 { 75 return null; 76 } 77 78 79 81 return indexedBinaryRetrieve(keyList,entry); 82 83 } 84 85 protected Node indexedBinaryRetrieve(Object [] list1, Object obj) 86 { 87 int i = 0; 88 IndexNodeEntry entry =null; 89 int size =entryNumber; 90 for(int j = size- 1; i <= j;) 91 { 92 int k = i + j >> 1; 93 entry = (IndexNodeEntry)list1[k]; 94 int l = entry.value.compareTo(obj); 95 if(l < 0){ 96 i = k + 1; 97 } else if(l > 0){ 98 j = k - 1; 99 }else{ 100 return entry.getChildNode(); 101 } 102 } 103 104 i= -(i + 1); 105 106 if (-i == entryNumber){ 107 return entry.getChildNode(); 108 } else{ 109 return ((IndexNodeEntry)list1[-i-1]).getChildNode(); 110 } 111 112 } 113 114 115 116 protected int indexedBinaryLocate(Object [] list1, Object obj) 117 { 118 119 120 int low = 0; 121 int high = entryNumber-1; 122 123 IndexNodeEntry entry =null; 124 while (low <= high) { 125 int mid = (low + high) >> 1; 126 127 entry = (IndexNodeEntry)list1[mid]; 128 int cmp = entry.compareTo(obj); 129 130 if (cmp < 0) 131 low = mid + 1; 132 else if (cmp > 0) 133 high = mid - 1; 134 else 135 return mid; } 137 return -(low + 1); 139 } 140 141 142 143 144 147 public Object [] insertEntry(NodeEntry key) { 148 149 resetNextNode(); 151 152 IndexNodeEntry entry = (IndexNodeEntry) key; 153 154 if (entryNumber == 0) { 156 keyList[0]=entry; 157 entry.setContainingNode(this); 158 entryNumber++; 159 rightValue=entry.getValue(); 160 return keyList; 161 162 } else { 163 for (int i=0;i<entryNumber;i++) { 166 IndexNodeEntry listEntry = (IndexNodeEntry)keyList[i]; 167 169 170 int comparison = listEntry.getValue().compareTo(entry.getValue()); 172 if ( comparison>0) { 173 174 System.arraycopy(keyList, i, keyList, i + 1, 175 entryNumber - i); 176 keyList[i] = entry; 177 178 entry.setContainingNode(this); 179 entryNumber++; 180 return keyList; 181 } else if (comparison ==0 ){ 184 listEntry.updateValue(); 186 if (i == entryNumber -1){ 187 keyList[entryNumber]=entry; 188 rightValue=entry.getValue(); 189 190 }else{ 191 int inner =i+1; 192 System.arraycopy(keyList, inner, keyList, inner+1, 193 entryNumber - inner); 194 keyList[inner] = entry; 195 } 196 entry.setContainingNode(this); 197 entryNumber++; 198 return keyList; 199 } 200 } 201 202 keyList[entryNumber]=entry; 203 rightValue=entry.getValue(); 204 entry.setContainingNode(this); 205 entryNumber++; 206 return keyList; 207 } 208 209 210 211 } 212 213 214 private void resetNextNode(){ 215 if (nextNode != null && nextNode.getNode() != null 216 && nextNode.getNode().isDeleted()) 217 { 218 nextNode = nextNode.getNode().getLinkNode(); 219 } 220 } 221 222 229 public boolean updateEntry(NodeEntry key) { 230 231 resetNextNode(); 232 IndexNodeEntry entry = (IndexNodeEntry) key; 233 234 if (entryNumber == 0) { 235 236 return false; 237 238 } else { 239 int length = entryNumber; 240 for (int i=0;i<length;i++) { 241 IndexNodeEntry listEntry = (IndexNodeEntry)keyList[i]; 242 if (listEntry.getValue().equals(entry.getValue()) ) { 243 listEntry.updateValue(); 245 if (i == length -1){ 246 rightValue=listEntry.getValue(); 247 } 248 return true; 249 } 250 } 251 return false; 252 } 253 254 255 256 } 257 258 259 260 265 public void setKeyList(Object [] temp){ 266 267 268 269 for(int i=0;i<entryNumber;i++){ 270 IndexNodeEntry entry = (IndexNodeEntry) temp[i]; 271 entry.setContainingNode(this); 272 keyList[i]=entry; 273 } 274 275 } 276 277 278 281 public boolean isEmpty() { 282 if (entryNumber == 0) { 283 return true; 284 } 285 return false; 286 } 287 288 289 290 293 public Comparable getRightValue() { 294 return rightValue; 295 } 296 297 298 299 IndexNodeEntry getParentKey() { 300 return parentKey; 301 } 302 303 304 public String toString() { 305 StringBuffer buf = new StringBuffer (); 306 buf.append(" IndexNode{"); 307 308 buf.append(" rightValue:" + getRightValue() +"}"); 309 return buf.toString(); 310 } 311 312 public boolean isUnderFull() { 313 if (entryNumber < BTree.getMaxNodeSize() ) { 314 return true; 315 } 316 return false; 317 } 318 319 324 void setParentKey(IndexNodeEntry parentKey) { 325 this.parentKey = parentKey; 326 } 327 328 329 330 333 public boolean deleteEntry(NodeEntry entry) { 334 335 resetNextNode(); 336 337 int location = indexedBinaryLocate(keyList,entry); 338 339 if (location >=0) { 340 342 int numMoved = entryNumber - location - 1; 343 if (numMoved > 0) 344 System.arraycopy(keyList, location+1, keyList, location, 345 numMoved); 346 keyList[--entryNumber] = null; 348 349 350 if (entryNumber ==0){ 351 rightValue = BTree.MIN_RIGHT_VALUE; 352 }else{ 353 ((IndexNodeEntry)keyList[entryNumber-1]).updateValue(); 354 setRightValue(((IndexNodeEntry)keyList[entryNumber-1]).getValue()); 355 } 356 return true; 357 } 358 return false; 359 360 } 361 362 365 public Node splitNode(Object [] entries) 366 { 367 369 Object [] entriesList = split(entries,entryNumber); 370 371 372 373 Node newNode = NodeFactory.getInstance().createIndexNode(); 374 375 Comparable currentRight = rightValue; 376 377 EntrySplitWrapper newEntries = ((EntrySplitWrapper)entriesList[1]); 379 380 newNode.entryNumber = newEntries.size; 381 newNode.setEntries( (Object []) newEntries.entries ); 382 383 384 newNode.setRightValue(currentRight); 385 newNode.setLinkNode(getLinkNode()); 386 387 389 EntrySplitWrapper replacements = (EntrySplitWrapper)entriesList[0]; 390 391 392 keyList = (Object [])replacements.entries; 393 entryNumber = replacements.size; 394 setRightValue(((NodeEntry) keyList[entryNumber - 1]).getValue()); 395 396 397 398 399 return newNode; 400 401 } 402 403 406 public int getEntryNumber() { 407 return entryNumber; 408 } 409 410 411 412 415 public Object [] getEntries() { 416 417 return keyList; 418 } 419 420 421 422 425 public void setRightValue(Comparable value) 426 { 427 this.rightValue = value; 428 429 } 430 431 434 435 436 439 public void setEntries(Object [] entries) 440 { 441 setKeyList(entries); 442 443 } 444 445 448 public NodeLink getLinkNode() 449 { 450 451 return nextNode; 452 } 453 454 457 public void setLinkNode(NodeLink node) 458 { 459 this.nextNode = node; 460 461 } 462 463 472 public boolean contains(Comparable value) 473 { 474 if (entryNumber ==0 || value ==null) 475 { 476 return false; 477 } 478 479 if (value instanceof MinComparableValue){ 480 return true; 481 } 482 return rightValue.compareTo(value) >=0; 483 } 484 485 486 } 487 | Popular Tags |