Package org.chaidb.db.index

Examples of org.chaidb.db.index.Key


            Enumeration keys = keys(kContext);
            Iterator it = null;
            Object tmpId = null;
            while (keys.hasMoreElements()) {
                Key key = (Key) keys.nextElement();
                it = lookup(key, kContext);
                while (it.hasNext()) {
                    tmpId = it.next();
                    (newBTree).store(key, tmpId, IDBIndex.STORE_REPLACE, kContext);
                }
View Full Code Here


        try {
            this.acquire(kContext, LockManager.LOCK_WRITE);
            int ret = 0;
            Enumeration enuKeys = keys(kContext);
            while (enuKeys.hasMoreElements()) {
                Key key = (Key) enuKeys.nextElement();
                ret += delete(key, docId, kContext);
            }
            return ret;
        } finally {
            this.release(kContext);
View Full Code Here

        kContext.checkLock(getBTreeName());
        ArrayList keys = rangeLookupKeys(minKey, maxKey, includeMinKey, includeMaxKey, kContext);
        try {
            ArrayList newList = new ArrayList(keys.size());
            DuplicatedKeyIterator it = null;
            Key key;
            for (int i = 0; i < keys.size(); i++) {
                key = (Key) keys.get(i);
                it = lookup(key, kContext);
                newList.add(it);
            }
View Full Code Here

        int last = currNode - 1;
        int currIn = 0;
        while (last > first) {
            currIn = (first + last) / 2;
            /* Modified by ben zhang at Aug, 12, 2002 */
            Key tmpKey = getNode(currIn).getKey();
            cmp = tmpKey.compareTo(key);
            if (cmp == 0) return new KeyIndex(true, currIn);
            else if (cmp > 0) // a[currIn] > key
                last = currIn - 1;
            else { // a[currIn] < key
                /*Modified by ben zhang at Aug, 12, 2002 */
                tmpKey = getNode(currIn + 1).getKey();
                int tmp = tmpKey.compareTo(key);
                if (tmp > 0) return new KeyIndex(false, currIn + 1);
                else if (tmp == 0) return new KeyIndex(true, currIn + 1);
                else // a[currIn+1] < key
                    first = currIn + 1;
            }
        }
        /*Modified by ben zhang at Aug, 12, 2002*/
        Key tmpKey1 = getNode(first).getKey();
        cmp = tmpKey1.compareTo(key);
        if (cmp > 0) return new KeyIndex(false, first);
        else if (cmp == 0) return new KeyIndex(true, first);
        else return new KeyIndex(false, first + 1);
    }
View Full Code Here

            //!!!!
            //for sibling, we add it in the locklist so that we
            //can release outside.
            //locks.add(lock);

            Key newKey = null;
            //====>>> 2a. if this is leaf page
            if (isLeaf()) {
                newKey = insertWhenIsLeaf(newPage, txnId, needLog, keyIndex, key, currNode, data, mode, kContext);

                //=====>>> 2b. if this is internal page
View Full Code Here

        buffer.releasePage(btreeSpec.btree.getBtreeId(), parent.pageNumber, true);
        return newRoot;
    }

    private Key insertWhenIsBTree(BTreePage newPage, int txnId, boolean needLog, KeyIndex keyIndex, Key key, int currNode, byte[] data, short mode, KernelContext kContext) throws ChaiDBException {
        Key newKey;
        PageNumber tmpPgNo;
        newPage.setLogInfo(txnId, needLog);
        newPage.setInternal();
        int indexOfInsert = keyIndex.keyIndex;
        // index of V', Tn/2T of keys in (key1, key2, keyn-1, key) >= V'
        int indexOfVictim = order / 2;
        // m is the index of the lowest key that >= V'
        int m = (indexOfInsert < indexOfVictim) ? (indexOfVictim - 1) : indexOfVictim;
        // V'
        /* Modified by ben zhang at Aug, 12, 2002 */
        Key tmpKey = getNode(m).getKey();
        newKey = (indexOfInsert == indexOfVictim) ? key : tmpKey;
        if (Debug.DEBUG_BTREEPAGE)
            logger.debug("m= " + m + " V'= " + new String(newKey.toBytes())); //Modified by ben zhang at Aug, 12, 2002 . Pending issue
        // insert datam, keym+1,...keyn-1 to new page
        // copy to new page
View Full Code Here

        }
        return newKey;
    }

    private Key insertWhenIsLeaf(BTreePage newPage, int txnId, boolean needLog, KeyIndex keyIndex, Key key, int currNode, byte[] data, short mode, KernelContext kContext) throws ChaiDBException {
        Key newKey;
        newPage.setLogInfo(txnId, needLog);
        newPage.setLeaf();
        int indexOfInsert = keyIndex.keyIndex;
        // index of V', Tn/2T of keys in (key1, key2,...keyn-1, key) < V'
        int indexOfVictim = order / 2 + 1;
        // m is the index of the lowest key that >= V'
        int m = (indexOfInsert < indexOfVictim) ? (indexOfVictim - 1) : indexOfVictim;

        /************* The below is recorded in BTreeSplitLogRecord ******/
        /***************** Add by leon,2001-9-27 14:56 ********************/
        if (needLog) {
            int pgno = pageNumber.getPageNumber();
            int newpgno = newPage.getPageNumber().getPageNumber();

            BTreeSplitLogRecord lr = new BTreeSplitLogRecord(pageNumber.getTreeId(), pgno, txnId, (short) m, (short) m, newpgno, page, (short) btreeSpec.getNodeSize(), btreeSpec.btree.getType());
            lr.log();
        }
        /******************************************************************/

        // V'
        /*Modified by Ben Zhang at Aug, 12, 2002*/
        newKey = (indexOfInsert == indexOfVictim) ? key : getNode(m).getKey();
        // move keym,...keyn-1 to new page
        // copy to new page
        for (int j = m; j < currNode; j++) {
            newPage.upperBound -= btreeSpec.getLeafNodeSize();
            System.arraycopy(page, getNodeOffset(j), newPage.page, newPage.upperBound, btreeSpec.getLeafNodeSize());
            System.arraycopy(ByteTool.shortToBytes(newPage.upperBound), 0, newPage.page, newPage.lowerBound, 2);
            newPage.lowerBound += 2;
        }
        newPage.setLowerBound(newPage.lowerBound);
        newPage.setUpperBound(newPage.upperBound);
        if (Debug.DEBUG_BTREEPAGE)
            logger.debug("m= " + m + " V'= " + new String(newKey.toBytes())); //Modified by ben at Aug, 12, 2002 Pending
        // remove from the current page
        if (m < order) {
            byte[] tmp = new byte[m * btreeSpec.getLeafNodeSize()];
            byte[] ptr = new byte[m * 2];
            int k = 0;
View Full Code Here

            // Binary search within page nodes
            while (nodeIndexLast > nodeIndexFirst) {
                nodeIndex = (nodeIndexFirst + nodeIndexLast) / 2;
                BTreeNode node = getNode(nodeIndex);
                /*Modified by ben zhang at aug, 12, 2002 */
                Key tmpKey = node.getKey();
                int cmp = tmpKey.compareTo(key);
                if (cmp > 0) {
                    nodeIndexLast = nodeIndex - 1;
                } else if (cmp < 0) {
                    nodeIndexFirst = nodeIndex;
                    if (nodeIndexLast - nodeIndexFirst == 1) {
                        BTreeNode node2 = getNode(nodeIndexLast);
                        /*Modified by ben zhang at aug, 12, 2002 */
                        tmpKey = node2.getKey();
                        int cmpResult = tmpKey.compareTo(key);
                        if (cmpResult > 0) {
                            //lock it with shared lock.

                            //update the lowest predecessor must be locked
                            BTreePage nPage = lockAPage(kContext, node.getPageNumber(), oper);

                            // unfix current page
                            buffer.releasePage(pageNumber.getTreeId(), pageNumber, false);
                            returnPage = nPage.getLeaf(key, kContext, oper);
                            buffer.releasePage(nPage.pageNumber.getTreeId(), nPage.pageNumber, false);
                            return returnPage;
                        } else {
                            //lock it with shared lock.

                            BTreePage page = lockAPage(kContext, node2.getPageNumber(), oper);
                            // unfix current page
                            buffer.releasePage(pageNumber.getTreeId(), pageNumber, false);
                            returnPage = page.getLeaf(key, kContext, oper);
                            buffer.releasePage(page.pageNumber.getTreeId(), page.pageNumber, false);
                            return returnPage;
                        }
                    }
                } else { // Exact match
                    //lock it with shared lock.
                    BTreePage page = lockAPage(kContext, node.getPageNumber(), oper);
                    // unfix current page
                    buffer.releasePage(pageNumber.getTreeId(), pageNumber, false);
                    returnPage = page.getLeaf(key, kContext, oper);
                    buffer.releasePage(page.pageNumber.getTreeId(), page.pageNumber, false);
                    return returnPage;
                }
            } // end of binary search

            if (nodeIndexFirst - nodeIndexLast == 1) {
                // go to leftmost node
                if (nextPage.getPageNumber() > 0) {
                    //lock it with shared lock.

                    BTreePage leftPage = lockAPage(kContext, nextPage, oper);
                    // unfix current page
                    buffer.releasePage(pageNumber.getTreeId(), pageNumber, false);
                    returnPage = leftPage.getLeaf(key, kContext, oper);
                    buffer.releasePage(leftPage.pageNumber.getTreeId(), leftPage.pageNumber, false);
                    return returnPage;
                } else {
                    logger.error("Page " + pageNumber.toHexString() + " " + buffer.getBTreeName(pageNumber.getTreeId()) + " missing leftmost page " + Debug.getDebugInfo());
                    //Debug.pageHistory(Integer.toHexString(pageNumber.getPageNumber()));
                    Debug.flushPages();
                    if (Debug.DEBUG_THREAD) Debug.flushThreads();
                    // details -ranjeet
                    String details = "The page number is " + pageNumber.toHexString() + ".";
                    throw new ChaiDBException(ErrorCode.BTREE_LEFTMOST_PAGE_MISSING, details);
                }
            }
            // now binary search has identified a single node
            BTreeNode node = getNode(nodeIndexFirst);
            /* Modified by ben zhang at aug, 12, 2002 */
            Key tmpKey1 = node.getKey();
            int cmp = key.compareTo(tmpKey1);
            if (cmp < 0) {
                // go to leftmost pointer
                if (nextPage.getPageNumber() > 0) {
                    //lock it with shared lock.
View Full Code Here

            int nodeIndexLast = getCurrNodeNumbers() - 1;
            while (nodeIndexLast > nodeIndexFirst) {
                int nodeIndex = (nodeIndexFirst + nodeIndexLast) / 2;
                BTreeNode node = getNode(nodeIndex);
                /* Modified by ben zhang at aug, 12, 2002 */
                Key tmpKey = node.getKey();
                int cmp = tmpKey.compareTo(key);
                if (cmp > 0) {
                    nodeIndexLast = nodeIndex - 1;
                } else if (cmp < 0) {
                    nodeIndexFirst = nodeIndex;
                    if (isLeaf()) nodeIndexFirst++;
                    else if (nodeIndexLast - nodeIndexFirst == 1) {
                        BTreeNode node2 = getNode(nodeIndexLast);
                        /*Modified by ben zhang at aug, 12, 2002 */
                        tmpKey = node2.getKey();
                        int cmpResult = tmpKey.compareTo(key);
                        if (cmpResult > 0) {
                            BTreePage page = this.lockAPage(kContext, node.getPageNumber(), SEARCH);
                            // unfix the current page
                            buffer.releasePage(pageNumber.getTreeId(), pageNumber, false);

                            returnNode = page.search(key, kContext);
                            // unfix the current page
                            buffer.releasePage(btreeSpec.btree.getBtreeId(), page.pageNumber, false);
                            return returnNode;
                        } else {
                            BTreePage page = this.lockAPage(kContext, node2.getPageNumber(), SEARCH);
                            // unfix the current page
                            buffer.releasePage(pageNumber.getTreeId(), pageNumber, false);

                            returnNode = page.search(key, kContext);
                            // unfix the current page
                            buffer.releasePage(btreeSpec.btree.getBtreeId(), page.pageNumber, false);
                            return returnNode;
                        }
                    }
                } else { // Exact match
                    if (isLeaf()) {
                        return node;
                    }
                    BTreePage page = this.lockAPage(kContext, node.getPageNumber(), SEARCH);
                    // unfix the current page
                    buffer.releasePage(pageNumber.getTreeId(), pageNumber, false);

                    returnNode = page.search(key, kContext);
                    // unfix the new allocated page
                    buffer.releasePage(btreeSpec.btree.getBtreeId(), page.pageNumber, false);
                    return returnNode;
                }
            } // end of binary search

            if (!isLeaf() && nodeIndexFirst - nodeIndexLast == 1) {
                // go to leftmost node
                if (nextPage.getPageNumber() > 0) {
                    BTreePage page = this.lockAPage(kContext, nextPage, SEARCH);
                    // unfix current page
                    buffer.releasePage(pageNumber.getTreeId(), pageNumber, false);

                    returnNode = page.search(key, kContext);
                    // unfix the new allocated page
                    buffer.releasePage(btreeSpec.btree.getBtreeId(), page.pageNumber, false);
                    return returnNode;
                } else {
                    logger.error("Page " + pageNumber.toHexString() + " " + buffer.getBTreeName(pageNumber.getTreeId()) + " missing leftmost page " + Debug.getDebugInfo());
                    //Debug.pageHistory(Integer.toHexString(pageNumber.getPageNumber()));
                    Debug.flushPages();
                    if (Debug.DEBUG_THREAD) Debug.flushThreads();
                    // details -ranjeet
                    String details = "The page number is " + pageNumber.toHexString() + ".";
                    throw new ChaiDBException(ErrorCode.BTREE_LEFTMOST_PAGE_MISSING, details);
                }
            }

            // now binary search has identified a single node
            // node.setTreeId(btreeSpec.btree.getBtreeId());
            // Leaf node? need an exact match to avoid failure
            if (isLeaf()) {
                BTreeNode node = getNode(nodeIndexFirst);
                //buffer.releasePage(pageNumber.getTreeId() ,pageNumber, false);
                /*Modified by ben zhang at aug, 12, 2002 */
                if (node != null && node.getKey().compareTo(key) == 0) return node;
                else {
                    //release it for outside this method, this page can't be found any longer.
                    buffer.releasePage(pageNumber.getTreeId(), pageNumber, false);
                    return null;
                }
            } else {
                BTreeNode node = getNode(nodeIndexFirst);
                /*Modified by ben zhang at aug, 12, 2002 */
                Key tmpKey = node.getKey();
                int cmp = key.compareTo(tmpKey);
                if (cmp < 0) {
                    // go to leftmost pointer
                    if (nextPage.getPageNumber() > 0) {
                        BTreePage page = this.lockAPage(kContext, nextPage, SEARCH);
View Full Code Here

     */
    private boolean getNodeIdList(Key minKey, Key maxKey, boolean includeMinKey, boolean includeMaxKey, KernelContext kContext, ArrayList valueList) throws ChaiDBException {

        BTreeNode btreeNode;
        int nodeNumOnPage = getCurrNodeNumbers();
        Key key;

        int i = 0;
        for (; i < nodeNumOnPage; i++) {
            btreeNode = getNode(i);
            /*Modified by ben zhang at Aug, 12, 2002. */
            key = btreeNode.getKey();
            if (minKey == null || (includeMinKey ? key.compareTo(minKey) >= 0 : key.compareTo(minKey) > 0)) {
                if (maxKey == null || (includeMaxKey ? key.compareTo(maxKey) <= 0 : key.compareTo(maxKey) < 0))
                    valueList.add(btreeNode.getData(kContext));
                else break;
            }
        }
        return i == nodeNumOnPage;
View Full Code Here

TOP

Related Classes of org.chaidb.db.index.Key

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.