Package org.chaidb.db.log.logrecord

Source Code of org.chaidb.db.log.logrecord.BTreeNewPageLogRecord

/*
* Copyright (C) 2006  http://www.chaidb.org
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
*
*/

package org.chaidb.db.log.logrecord;

import org.apache.log4j.Logger;
import org.chaidb.db.exception.ChaiDBException;
import org.chaidb.db.exception.ErrorCode;
import org.chaidb.db.helper.ByteTool;
import org.chaidb.db.index.btree.BTree;
import org.chaidb.db.index.btree.BTreeSpec;
import org.chaidb.db.index.btree.bufmgr.PageBufferManager;
import org.chaidb.db.log.LogRecord;


/**
* allocate a new page while BtreePage(or DataPage) newPage()
* log the new allocated page number
*/
public class BTreeNewPageLogRecord extends BTreeLogRecord {
    private static PageBufferManager bpm = PageBufferManager.getInstance();
    private static final Logger logger = Logger.getLogger(BTreeNewPageLogRecord.class);
    private byte pageFlag; //page flags

    static int PAGEFLAG_SIZE = 1; //1 bytes

    private int docId;

    /**
     * default Construct
     */
    public BTreeNewPageLogRecord() {
        super();
        super.setType(LOG_BTREE_NEW_PAGE);
    }

    /**
     * Construct
     *
     * @param docId Only used by DataPage,excluding BTreePage and OverFlowpage
     */
    public BTreeNewPageLogRecord(int treeId, int newPageNum, int newTxnId, int newPageFlag, int docId, short btreeType) {
        super(treeId, newPageNum, newTxnId, btreeType);
        this.pageFlag = (byte) newPageFlag;
        this.docId = docId;

        super.setType(LOG_BTREE_NEW_PAGE);
    }

    /**
     * get page flag
     */
    public byte getPageFlag() {
        return pageFlag;
    }

    public int getDocId() {
        return docId;
    }

    /**
     * converts a byte array into a log record instance
     */
    public boolean read(byte[] bArr, int start) throws ChaiDBException {

        /* construct a new LogRecord instance */
        super.read(bArr, start);

        /* get the values of BTreeMoveLogRecord Object */
        int step = start + super.getRecordLength();
        pageFlag = bArr[step];
        if (isDataPage(pageFlag)) {
            byte tmpByte = bArr[step];
            bArr[step] = 0;
            docId = ByteTool.bytesToInt(bArr, step, LogRecord.msbFirst);
            bArr[step] = tmpByte;
        }

        return true;
    }

    /**
     * redo allocate a new page function
     *
     * @param page
     * @return boolean true|false
     *         update date:2001-10-12 by marriane,delete freelist recover
     */
    protected boolean doRedo(byte[] page) throws ChaiDBException {

        try {
            /* set metadata's lowbound and uppbound data with default value */
            System.arraycopy(ByteTool.shortToBytes((short) BTreeSpec.PAGE_HEADER_SIZE), 0, page, BTreeSpec.OFF_LOWERBOUND, 2);
            System.arraycopy(ByteTool.shortToBytes((short) BTreeSpec.PAGE_SIZE), 0, page, BTreeSpec.OFF_UPPERBOUND, 2);

            /* set metadata's pageNumber */
            System.arraycopy(ByteTool.intToBytes(super.getPageNum()), 0, page, BTreeSpec.OFF_PAGENUMBER, 4);

            /* set prevPage and nextPage in metadata */
            System.arraycopy(ByteTool.intToBytes(BTreeSpec.INVALID_PAGENO), 0, page, BTreeSpec.OFF_PREVPAGE, 4);
            System.arraycopy(ByteTool.intToBytes(BTreeSpec.INVALID_PAGENO), 0, page, BTreeSpec.OFF_NEXTPAGE, 4);

            /* set flags of different page type */
            System.arraycopy(ByteTool.intToBytes(pageFlag), 0, page, BTreeSpec.OFF_FLAGS, 4);

        } catch (Exception e) {
            logger.debug(e);
            throw new ChaiDBException(ErrorCode.LOG_REDO_FAILED, e.toString());
        }
        return true;
    }

    /**
     * do undo function
     *
     * @param page
     * @return boolean true|false
     *         update date:2001-10-12 by marriane,delete freelist recover
     */
    protected boolean doUndo(byte[] page) throws ChaiDBException {
        try {
            /* set metadata's lowbound and uppbound data with default value */
            System.arraycopy(ByteTool.shortToBytes((short) BTreeSpec.PAGE_HEADER_SIZE), 0, page, BTreeSpec.OFF_LOWERBOUND, 2);
            System.arraycopy(ByteTool.shortToBytes((short) BTreeSpec.PAGE_SIZE), 0, page, BTreeSpec.OFF_UPPERBOUND, 2);

            /* add page to freelist of BufferedPage in memory*/
            PageBufferManager bp = bpm;
            bp.addToFreeList(treeId, super.getPageNum(), new Integer(super.getTxnId()));

            if (isDataPage(pageFlag))
                bp.removeLatestDataPage(docId, treeId, super.getPageNum(), new Integer(super.getTxnId()));

            //add 1 on the free page number in metadata page of file if it's not in freelist
        } catch (Exception e) {
            logger.debug(e);
            throw new ChaiDBException(ErrorCode.LOG_UNDO_FAILED, e.toString());
        }
        return true;
    }

    /**
     * print data and help in debugging log files
     */
    public void print() throws ChaiDBException {
        //logger.log(ServerLog.LOG_DEBUG,"begin: printing the information of BTreeNewPageLogRecord object......");
        super.print();
        logger.debug(" flag:" + this.getPageFlag());

        //logger.log(ServerLog.LOG_DEBUG,"end: printing the information of BTreeNewPageLogRecord object.");

    }

    /**
     * converts a log record instance into a byte array.
     * The byte array has the following format:
     * --------------------------------------------------
     * | pageFlag
     * --------------------------------------------------
     * <p/>
     * pageFlag:  1 bytes.
     */
    public void toBytes(byte[] byteArray, int start) throws ChaiDBException {
        super.toBytes(byteArray, start);
        int step = start + super.getRecordLength();
        byteArray[step] = pageFlag;
        if (super.isDataPage(pageFlag)) {
            byte[] docIdArr = ByteTool.intToBytes(docId, LogRecord.msbFirst);
            System.arraycopy(docIdArr, docIdArr.length - BTree.DOCID_SIZE, byteArray, ++step, BTree.DOCID_SIZE);
        }

    }

    /**
     * get current log record type total length
     *
     * @return int total lenth
     */
    public int getRecordLength() {
        return super.getRecordLength() + PAGEFLAG_SIZE + (isDataPage(pageFlag) ? BTree.DOCID_SIZE : 0);
    }

}
TOP

Related Classes of org.chaidb.db.log.logrecord.BTreeNewPageLogRecord

TOP
Copyright © 2018 www.massapi.com. 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.