Package org.chaidb.db.index.btree.bufmgr

Examples of org.chaidb.db.index.btree.bufmgr.PageNumber


    public DuplicatedKeyIterator lookup(Key key, KernelContext kContext) throws ChaiDBException {
        kContext.checkLock(getBTreeName());
        HyperBTreeIterator it = null;
        it = HyperBTreeIterator.createIterator(this, kContext);
        try {
            PageNumber root = getTopRoot();

            BTreePage rootPage = new BTreePage(id, root, btreeSpec, buffer);
            if (rootPage.getPage() == null) {
                return it;
            }
View Full Code Here


        kContext.checkLock(getBTreeName());
        ArrayList values = new ArrayList();

        try {
            PageNumber root = getTopRoot();
            rangeLookupKeys(minKey, maxKey, includeMinKey, includeMaxKey, root, values, kContext);
        } finally {
            doFinalJob();
        }
        return values;
View Full Code Here

            nextNodePos = nodePos;
        }

        final PageBufferManager buffer = btree.getBuffer();
        final BTreeSpec btreeSpec = btree.getBTreeSpec();
        PageNumber dupPageNumber = new PageNumber(nodePos.getPageNo());
        try {
            DataPage curDataPage = new DataPage(btree.getBtreeId(), dupPageNumber, btreeSpec, buffer);
            DataNode curDataNode = new DataNode(curDataPage, nodePos.getOffsetInPage());
            curDataNode.setFlags(BTreeSpec.DATA_NODE_DUP_NEXT);
            byte[] oldData = ByteTool.copyByteArray(curDataNode.getPage().getPage(), curDataNode.getNodeOffset(), (int) curDataNode.getNodeSpace());
View Full Code Here

        int nPageNo = bulkNextNodePos.getPageNo();

        int id = btree.getBtreeId();
        final PageBufferManager buffer = btree.getBuffer();
        final BTreeSpec btreeSpec = btree.getBTreeSpec();
        PageNumber dupPageNumber = new PageNumber(nPageNo);

        try {
            DataPage dupDataPage = new DataPage(id, dupPageNumber, btreeSpec, buffer);
            int pageCount = 0;
            while (pageCount < page && !finished) {
                NodeInfo node = new NodeInfo();
                node.nodePosition = bulkNextNodePos;
                int ret = getNode(dupDataPage, node);
                bulkNextNodePos = node.nodeNextPosition;
                if ((ret & DUP_FINISHED) == DUP_FINISHED) {
                    finished = true;
                    if ((ret & DUP_NEXT) == DUP_NEXT) {
                        break;
                    }
                }
                if ((ret & DUP_DATA) == DUP_DATA) {
                    cache.add(node);
                } else if ((ret & DUP_NEXT) == DUP_NEXT) {
                    if (bulkNextNodePos.getPageNo() == nPageNo) {
                        continue;
                    } else {
                        if (cache.size() > 0) {
                            pageCount++;
                        }
                        if (pageCount < page) {
                            nPageNo = bulkNextNodePos.getPageNo();
                            buffer.releasePage(id, dupPageNumber, false);
                            dupPageNumber = new PageNumber(nPageNo);
                            dupDataPage = new DataPage(id, dupPageNumber, btreeSpec, buffer);
                        }
                    }
                } else {
                    logger.error("Failed to get next dup node.");
View Full Code Here

     * Default Constructor
     */
    public BTreePage(BTreeSpec btreeSpec, PageBufferManager buffer) {
        this.btreeSpec = btreeSpec;
        this.buffer = buffer;
        this.pageNumber = new PageNumber(-1, -1, -1);
        this.prevPage = new PageNumber(-1, -1, -1);
        this.nextPage = new PageNumber(-1, -1, -1);
        keyType = -1;
    }
View Full Code Here

    public BTreePage(int id, PageNumber pageNumber, BTreeSpec btreeSpec, PageBufferManager buffer) throws ChaiDBException {
        this(btreeSpec, buffer);
        pageNumber.setTreeId(id);
        nextPage.setTreeId(id);
        prevPage.setTreeId(id);
        this.pageNumber = new PageNumber(pageNumber);
        this.page = buffer.getPage(id, this.pageNumber);
        if (this.page != null) {
            // get variables from page
            if (Debug.DEBUG_CHECKPAGENUMBER) {
                if (!Debug.checkPageNumber(pageNumber, this.page)) {
View Full Code Here

        newPage.setPageNumber(freePageInfo.getPageNumber());

        //the following code may generate log records.
        newPage.setLogInfo(txnId, false);
        newPage.setInternal();
        newPage.setNextPage(new PageNumber(BTreeSpec.INVALID_PAGENO));
        newPage.setPrevPage(new PageNumber(BTreeSpec.INVALID_PAGENO));
        // ### not write to page yet!
        newPage.setLowerBound((short) BTreeSpec.PAGE_HEADER_SIZE);
        newPage.setUpperBound((short) newPage.btreeSpec.getPageSize());
        newPage.setLogInfo(txnId, needLog);
View Full Code Here

            System.arraycopy(page, (BTreeSpec.PAGE_HEADER_SIZE + nextIndex * 2), page, (BTreeSpec.PAGE_HEADER_SIZE + (nextIndex + 1) * 2), (currNode - nextIndex) * 2);
        }
        System.arraycopy(ByteTool.shortToBytes(upperBound), 0, page, (BTreeSpec.PAGE_HEADER_SIZE + nextIndex * 2), 2);
        //logger.debug(" internalpage:" + pageNumber +" lb:" + lowerBound + " ub:" + upperBound);
        /*Modified by ben zhang at aug, 12, 2002 */
        PageNumber pageNum = new PageNumber(ByteTool.bytesToInt(data, 0, btreeSpec.isMsbFirst()));
        pageNum.setTreeId(btreeSpec.btree.getBtreeId());
        BTreeNode internalNode = BTreeNode.createNewBTreeNode(this, upperBound, key.toBytes(), pageNum);

        // size in byte of the node needed
        if (nodeSize <= btreeSpec.getInternalNodeSize()) {
            internalNode.setFlags((byte) 0);
View Full Code Here

        final BTreeSpec btreeSpec = btree.getBTreeSpec();
        dupNextCountForTest = dupNextCountForNextNode;
        dupNextCountForNextNode = 0;

        int curPageNo = nodePos.getPageNo();
        PageNumber dupPageNumber = new PageNumber(curPageNo);
        try {
            DataPage dupDataPage = new DataPage(id, dupPageNumber, btreeSpec, buffer);
            NodeInfo node = new NodeInfo();
            node.nodePosition = nodePos;
            while (true) {
                int ret = getNode(dupDataPage, node);
                if ((ret & DUP_FINISHED) == DUP_FINISHED) {
                    finished = true;
                    if ((ret & DUP_NEXT) == DUP_NEXT) {
                        break;
                    }
                }
                if ((ret & DUP_DATA) == DUP_DATA) {
                    return node;
                } else if ((ret & DUP_NEXT) == DUP_NEXT) {
                    node.nodePosition = node.nodeNextPosition;
                    if (node.nodeNextPosition.getPageNo() != curPageNo) {
                        curPageNo = node.nodeNextPosition.getPageNo();
                        buffer.releasePage(id, dupPageNumber, false);
                        dupPageNumber = new PageNumber(curPageNo);
                        dupDataPage = new DataPage(id, dupPageNumber, btreeSpec, buffer);
                    }
                } else {
                    logger.error("Failed to get next dup node.");
                    break;
View Full Code Here

        // Get the latest data page within which we store data node
        boolean pageFixed = false;

        //add by stanley
        boolean hasPageForOverflow = false;
        PageNumber PgnForOverflow = null;
        DataPage DataPageForOverflow = null;
        Lock lockforOverflow = null;

        PageNumber latestDataPageNumber = buffer.getLatestDataPage(docid, pageNumber.getTreeId(), needLog ? new Integer(txnId) : null);

        Lock lock = null;
        boolean isValidPage;
        while (latestDataPageNumber != null && latestDataPageNumber.getPageNumber() > 0) {

            lock = BTreeLock.acquire(kContext, LockManager.LOCK_WAITING, latestDataPageNumber, LockManager.LOCK_WRITE);

            //Usually the page we get should not stay at freelist. But there is one exception:
            //With txn, each txn get LDP from GLDP list. Thus assuming
            //(1) txn1 get a LDP (pageNumber) from GLDP list and remove it from the list and
            //paused due to OS scheduler
            //(2) At this moment, txn2 deletes all nodes in this page
            //, finds that it is not a LDP by isLatestDataPage() (checking a page in its own LDP
            //list and GLDP list), then frees this page.
            //(3) Later txn1 resumes getting the LDP page with the pagenumber it got at (1), then
            // finds it has been freed. The following exception occurs!!!!!!!!
            //Hereby, when we get a LDP, we must catch this exception and get a new LDP. See codes

            try {
                isValidPage = true;
                dataPage = new DataPage(btreeSpec.btree.getBtreeId(), latestDataPageNumber, btreeSpec, buffer);
            } catch (ChaiDBException e) {
                if (e.getErrorCode() != ErrorCode.BTREE_USE_FREEPAGE) {
                    logger.error(e);
                    throw e;
                }
                isValidPage = false;
            }
            /* we abide by the following algorithm to get a data page
             * 1.if  the latest data page is big enough to hold all the new datanode then use it. else go to 2
             * 2.if a new data page is big enough to hold all the new datanode then use it. else go to 3
             * 3.if the latest data page is big enough to hold BTreeSpec.DATA_NODE_HEADER_SIZE + 4 then use it. else go to 2
             * 4.we new a data page and use it.
            */
            if (isValidPage && !dataPage.isOverflow()) {

                if (dataPage.getFreeSpace() >= newNodeSize) {

                    if (!(btreeSpec.btree.getType() == IDBIndex.ID2NODE_BTREE)) {
                        pageFixed = true;
                        break;
                    } else { // if this tree is special, this data page must
                        // belong to this doc if there are still data in
                        // this datapage
                        int docID = ((NodeId) key).getDocId();
                        if (dataPage.getCurrNodeNumbers() == 0) {
                            dataPage.setDocID(docID);
                            pageFixed = true;
                            break;
                        } else {
                            if (dataPage.isOfDoc(docID)) {
                                pageFixed = true;
                                break;
                            } else {
                                // release the data page

                                buffer.releasePage(latestDataPageNumber
                                        .getTreeId(), latestDataPageNumber, false);
                                // because we did NOT update this page, so we
                                // release it at once
                                BTreeLock.release(kContext, pageNumber
                                        .getTreeId(), lock);

                                latestDataPageNumber = buffer
                                        .getANewLatestDataPage(docid, pageNumber.getTreeId(), needLog ? new Integer(txnId) : null);
                            }
                        }
                    }

                } else if (!hasPageForOverflow && dataPage.getFreeSpace() >= BTreeSpec.DATA_NODE_HEADER_SIZE + 4) {

                    if (!(btreeSpec.btree.getType() == IDBIndex.ID2NODE_BTREE)) {
                        hasPageForOverflow = true;
                        PgnForOverflow = latestDataPageNumber;
                        DataPageForOverflow = dataPage;
                        lockforOverflow = lock;
                    } else { // if this tree is special, this data page must
                        // belong to this doc if there are still data in
                        // this datapage
                        int docID = ((NodeId) key).getDocId();
                        if (dataPage.getCurrNodeNumbers() == 0) {
                            dataPage.setDocID(docID);
                            hasPageForOverflow = true;
                            PgnForOverflow = latestDataPageNumber;
                            DataPageForOverflow = dataPage;
                            lockforOverflow = lock;
                        } else {
                            if (dataPage.isOfDoc(docID)) {
                                hasPageForOverflow = true;
                                PgnForOverflow = latestDataPageNumber;
                                DataPageForOverflow = dataPage;
                                lockforOverflow = lock;
                            } else {
                                // release the data page

                                buffer.releasePage(latestDataPageNumber
                                        .getTreeId(), latestDataPageNumber, false);
                                // because we did NOT update this page, so we
                                // release it at once
                                BTreeLock.release(kContext, pageNumber
                                        .getTreeId(), lock);
                            }
                        }
                    }
                    latestDataPageNumber = buffer.getANewLatestDataPage(docid, pageNumber.getTreeId(), needLog ? new Integer(txnId) : null);
                } else {
                    //The datapage is not suitable then release it
                    buffer.releasePage(latestDataPageNumber.getTreeId(), latestDataPageNumber, false);
                    //because we did NOT update this page, so we release it at once
                    BTreeLock.release(kContext, pageNumber.getTreeId(), lock);

                    latestDataPageNumber = buffer.getANewLatestDataPage(docid, pageNumber.getTreeId(), needLog ? new Integer(txnId) : null);
                }

            } else {
                //The datapage is not suitable then release it
                buffer.releasePage(latestDataPageNumber.getTreeId(), latestDataPageNumber, false);
                //because we did NOT update this page, so we release it at once
                BTreeLock.release(kContext, pageNumber.getTreeId(), lock);

                latestDataPageNumber = buffer.getANewLatestDataPage(docid, pageNumber.getTreeId(), needLog ? new Integer(txnId) : null);
            }
        }
        // size in byte of the node needed
        if (!pageFixed) {
            if (!hasPageForOverflow) {
                dataPage = DataPage.newPage(btreeSpec, buffer, false, kContext, docid);
                if (btreeSpec.btree.getType() == IDBIndex.ID2NODE_BTREE) dataPage.setDocID(docid);
            } else {
                if (newNodeSize <= BTreeSpec.PAGE_SIZE - BTreeSpec.PAGE_HEADER_SIZE) {
                    dataPage = DataPage.newPage(btreeSpec, buffer, false, kContext, docid);
                    if (btreeSpec.btree.getType() == IDBIndex.ID2NODE_BTREE) dataPage.setDocID(docid);

                    buffer.releasePage(PgnForOverflow.getTreeId(), PgnForOverflow, false);
                    // because we did NOT update this page, so we release it
                    // at once
                    BTreeLock.release(kContext, pageNumber.getTreeId(), lockforOverflow);

                } else {
                    dataPage = DataPageForOverflow;
                    latestDataPageNumber = PgnForOverflow;
                    lock = lockforOverflow;
                }
            }
            // lock=returnLock[0];
        } else {
            if (hasPageForOverflow) {
                buffer.releasePage(PgnForOverflow.getTreeId(), PgnForOverflow, false);
                //because we did NOT update this page, so we release it at once
                BTreeLock.release(kContext, pageNumber.getTreeId(), lockforOverflow);
            }
        }

        /*Modified by ben zhang at Aug, 12, 2002 */
        int nodeSize = BTreeSpec.NODE_HEADER_SIZE + key.size();

        /* begin : added by marriane 2001-12-28 for bind all insert
         * node log records
         */
        if (needLog) {
            bindAllInsertNodeLogRecords(dataPage, data, nodeSize, keyExist, key, txnId, nextIndex, currNode);
        }
        /* end : added by marriane 2001-12-28 for bind all insert
         * node log records
         */

        setUpperBound((short) (upperBound - btreeSpec.getLeafNodeSize()));
        setLowerBound((short) (lowerBound + 2));

        if (nextIndex < currNode) {
            System.arraycopy(page, (BTreeSpec.PAGE_HEADER_SIZE + nextIndex * 2), page, (BTreeSpec.PAGE_HEADER_SIZE + (nextIndex + 1) * 2), (currNode - nextIndex) * 2);
        }
        System.arraycopy(ByteTool.shortToBytes(upperBound), 0, page, (BTreeSpec.PAGE_HEADER_SIZE + nextIndex * 2), 2);
        // insert data node in data page
        /* Modified by ben zhang at Aug, 12, 2002 Pending issue
         * whether key is necessary
         */
        // @@@ Modified by Kurt

        byte[] newPageOff = null;
        short newOffset = 0;
        if (btreeSpec.btree.getType() == IDBIndex.HYPER_BTREE) {
            newPageOff = insertDupNode(dataPage, key, data, mode, kContext, -1);
        } else {
            newOffset = dataPage.insertNode(key.toBytes(), data, mode, kContext, -1, (byte) 0);
        }
//
//                short dataNodeOff=dataPage.insertNode(key,data,mode,kContext,-1);
        // @@@ Modified by Kurt

        // unfix the page
        buffer.releasePage(dataPage.pageNumber.getTreeId(), dataPage.pageNumber, true);

        // create a new BTree leaf node
        /*Modified by ben zhang at aug, 12, 2002 */
        leafNode = BTreeNode.createNewBTreeNode(this, upperBound, key.toBytes(), dataPage.getPageNumber());
        if (nodeSize <= btreeSpec.getLeafNodeSize()) {
            // if the page has enough space to hold the new node
            leafNode.setFlags((byte) 0);
        } else {
            // The leaf page doesn't have enough space to hold data
            leafNode.setFlags((byte) 3);
        }
        short dataNodeOff = 0;
        // @@@ Modified by Kurt
        if (btreeSpec.btree.getType() == IDBIndex.HYPER_BTREE) {
            if (newPageOff.length >= 4) {
                dataNodeOff = (short) ByteTool.bytesToInt(newPageOff, 0, btreeSpec.isMsbFirst());
                leafNode.setDataNodeOffset(dataNodeOff);
            }
            if (newPageOff.length == 8) {
                int newPageNum = ByteTool.bytesToInt(newPageOff, 4, btreeSpec.isMsbFirst());
                leafNode.setPageNumber(newPageNum);
            }
        } else {
            leafNode.setDataNodeOffset(newOffset);
        }
        // @@@ Modified by Kurt

        // We only need to store key in the leaf page now
        /*Modified by ben zhang at aug, 12, 2002 */
        leafNode.setInternalNode(key.toBytes(), kContext);
        //reput it to LDPL if this datapage is allocated this time
        if (!pageFixed) {
            if (dataPage.isOverflow()) {
                String details = Debug.getDebugInfo() + " want to put overflowpage[" + dataPage.pageNumber + "] to LDPL.";
                throw new ChaiDBException(ErrorCode.BTREE_DEBUG, details);
            }
            buffer.putLatestDataPage(docid, dataPage.pageNumber, needLog ? new Integer(txnId) : null);
        } else {
            /* if the following statement is not within "else",
             * the below line must be uncommented
             */
            if (needLog) BTreeLock.change(kContext, lock, latestDataPageNumber.getTreeId(), LockManager.LOCK_READ);
            else BTreeLock.release(kContext, pageNumber.getTreeId(), lock);

        }

    }
View Full Code Here

TOP

Related Classes of org.chaidb.db.index.btree.bufmgr.PageNumber

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.