try {
// fix the current page
page = buffer.getPage(pageNumber.getTreeId(), pageNumber);
if (page == null)
throw new ChaiDBException(ErrorCode.BTREE_INVALID_BTREEPAGE, "Page is null: " + pageNumber.toHexString() + " of " + btreeSpec.getBtreeName());
if (Debug.DEBUG_CHECKPAGENUMBER) {
if (!Debug.checkPageNumber(pageNumber, this.page)) {
logger.fatal(new Throwable("there is hole in GetPage!"));
this.buffer.dump();
Db.getLogManager().flush();
Debug.dumpPageToLog(page);
if (Debug.DEBUG_PAGEINFO_TRACE) Debug.pageHistory(this.pageNumber);
System.exit(-1);
}
}
setKeyType((byte) key.getKeyType());
//====>>> 1. calculate the place to insert the node
// sort insert
KeyIndex keyIndex = sortKey(key);
boolean keyExist = keyIndex.keyExist;
int nextIndex = keyIndex.keyIndex;
int currNode = getCurrNodeNumbers();
// check mode to replace existing key's value or just return
if (keyExist && mode == IDBIndex.STORE_INSERT) {
// untouch the node, may throw exception later &&&
// unfix this page
buffer.releasePage(pageNumber.getTreeId(), pageNumber, false);
// details -ranjeet
String details = "The key value is " + new String(key.toBytes()) + ".";
/* Modified by ben at aug, 12, 2002. pending issue */
throw new ChaiDBException(ErrorCode.ENTRY_ALREADY_EXIST, details);
}
/* Modified by ben zhang at aug, 12, 2002 */
int newNodeSize = BTreeSpec.DATA_NODE_HEADER_SIZE + data.length;
if (isLeaf()) {
//=====>>> If is a leaf page
//=====>>> 2. insert / replace key/data pair
// BTreeNode leafNode;
DataPage dataPage = null;
if (keyExist) {
insertNodeWhenKeyExistedAndIsLeaf(nextIndex, kContext, key, data, mode);
} else { //Key not exist
insertNodeWhenKeyNotExistedAndIsLeaf(key, needLog, txnId, kContext, dataPage, newNodeSize, data, keyExist, nextIndex, currNode, mode);
}
} else if (isBTree()) {
//=====>>> If is an internal page
if (data.length != 4) {
throw new ChaiDBException(ErrorCode.BTREE_INT_PAGE_NUMBER);
}
//=====>>> 3a. if node already exists, simply change the pageNumber and return
if (keyExist) {
insertNodeWhenKeyExistedAndIsBTree(nextIndex, needLog, data, txnId);