/* ***************************************************************************** * File Name: ParNode.java * Purpose: class representing internal node in b+-tree * Authors: Han Don, Jeff Xing, Carol Cheung * Version: 1 * Last Date Updated: Feb. 27, 2001 ****************************************************************************/ /** * ParNode holds information of a parent node. * It provides the services that the * BTreeString requires. */ public class ParNode { /** * The order of the tree. */ public final static int ORDER = 4; /** * An empty key. */ private final static long EMPTY = -1; /** * Pointer array if my children are leaves. */ private LeafNode [] leaves; /** * Pointer array if my children are parent * nodes as well. */ private ParNode [] nextLevel; /** * The keys that I am holding. */ private long [] keys; /** * Class constructor */ public ParNode () { leaves = new LeafNode [ORDER]; nextLevel = new ParNode [ORDER]; keys = new long[ORDER]; for (int i = 0; i < ORDER; i++) { nextLevel[i] = null; leaves[i] = null; keys[i] = EMPTY; } } /** * returns the index of the next available slot for a child */ public int nextAvailable() { for(int i = 0; i < ORDER; i++){ if(keys[i] == EMPTY) return i; } return -1; } /** * Replace my last key with 'key'. */ public void updateLastKey(long key) { int last = nextAvailable(); if (last == -1) keys[ORDER-1] = key; else keys[last-1] = key; } /** * Return my last key. */ public long getLastKey() { return keys[ORDER - 1]; } /** * Return my leaf child at index i. * precondition: 0 <= i < ORDER */ public LeafNode getLeafAt(int i) { return leaves[i]; } /** * Return my ParNode child at index i. * precondition: 0<= i < ORDER */ public ParNode getNextLevelAt(int i) { return nextLevel[i]; } /** * Return my key at index i. * precondition: 0 <= i < ORDER */ public long getKeyAt(int i) { return keys[i]; } /** * Set a new key and point to my ParNode child 'par'. */ public void setParNode(long newKey, ParNode par) { boolean added = false; for(int i = 0; i < ORDER && !added; i++){ if(keys[i] == EMPTY) { keys[i] = newKey; nextLevel[i] = par; added = true; } } } /** * Set a new key and point to my leaf child 'par'. */ public void setLeafNode (long newKey, LeafNode leaf) { boolean added = false; for(int i = 0; i < ORDER && !added; i++){ if(keys[i] == EMPTY) { keys[i] = newKey; leaves[i] = leaf; added = true; } } } }