Package org.chaidb.db.log.logrecord

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

/*
* 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.BTreeSpec;
import org.chaidb.db.index.btree.bufmgr.PageBufferManager;


/**
* free page while BtreePage(or DataPage) add page to free list
* log the new allocated page number
*/
public class BTreeFreeOverflowPageLogRecord extends BTreeLogRecord {
    private static final Logger logger = Logger.getLogger(BTreeFreeOverflowPageLogRecord.class);
    private static PageBufferManager bpm = PageBufferManager.getInstance();
    private short lowBound; //2 bytes
    private short upperBound;//2bytes
    private byte[] headArr;  //lowBound bytes
    private byte[] dataArr;  //pageSize-upperBound size

    private static final int LOWBOUND_SIZE = 2;//2 bytes
    private static final int UPPERBOUND_SIZE = 2;//2 bytes

    /**
     * default Construct
     */
    public BTreeFreeOverflowPageLogRecord() {
        super();
        super.setType(LOG_BTREE_FREE_OVERFLOWPAGE);
    }

    public BTreeFreeOverflowPageLogRecord(int treeId, int newPageNum, int newTxnId, byte[] headArr, byte[] dataArr, short btreeType) {
        super(treeId, newPageNum, newTxnId, btreeType);
        this.lowBound = (short) headArr.length;
        this.upperBound = (short) (BTreeSpec.PAGE_SIZE - dataArr.length);
        this.headArr = headArr;
        this.dataArr = dataArr;

        super.setType(LOG_BTREE_FREE_OVERFLOWPAGE);
    }

    public byte[] getDataArr() {
        return ByteTool.copyByteArray(dataArr, 0, dataArr.length);
    }

    public byte[] getHeadArr() {
        return ByteTool.copyByteArray(headArr, 0, headArr.length);
    }

    public short getUpperBound() {
        return upperBound;
    }

    public short getLowBound() {
        return lowBound;
    }

    /**
     * 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();
        lowBound = ByteTool.bytesToShort(bArr, step, msbFirst);
        step = step + LOWBOUND_SIZE;
        upperBound = ByteTool.bytesToShort(bArr, step, msbFirst);
        step = step + UPPERBOUND_SIZE;
        headArr = ByteTool.copyByteArray(bArr, step, lowBound);
        step = step + lowBound;
        dataArr = ByteTool.copyByteArray(bArr, step, BTreeSpec.PAGE_SIZE - upperBound);

        return true;
    }

    /**
     * do redo function
     *
     * @param page
     * @return boolean true|false
     *         update date:2001-10-12 by marriane,delete freelist recover
     */
    protected boolean doRedo(byte[] page) throws ChaiDBException {
        /* redo allocate a new page function */
        /* free the allocated page function */
        try {
            /* add page to freelist of BufferedPage in memory*/
            PageBufferManager bp = bpm;
            bp.addToFreeList(treeId, super.getPageNum(), new Integer(super.getTxnId()));

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

    /**
     * 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 {
            /* copy old page content to page */
            System.arraycopy(this.headArr, 0, page, 0, lowBound);
            System.arraycopy(this.dataArr, 0, page, upperBound, BTreeSpec.PAGE_SIZE - upperBound);

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

    }

    /**
     * print data and help in debugging log files
     */
    public void print() throws ChaiDBException {
        //logger.log(ServerLog.LOG_DEBUG,"begin: printing the information of BTreeFreeOverflowPageLogRecord object......");
        super.print();
        logger.debug("lowBound:" + lowBound + " upperBound:" + upperBound);
        logger.debug("head array:" + ByteTool.toHexString(1, headArr, 0, headArr.length));
        logger.debug("data array:" + ByteTool.toHexString(1, dataArr, 0, dataArr.length));
        //logger.log(ServerLog.LOG_DEBUG,"end: printing the information of BTreeFreeOverflowPageLogRecord object.");

        return;

    }

    /**
     * converts a log record instance into a byte array.
     * The byte array has the following format:
     * --------------------------------------------------
     * | lowBound | upperBound | headArr | dataArr
     * --------------------------------------------------
     * <p/>
     * lowBound:  2 bytes.
     * upperBound:2 bytes
     * headArr:   lowBound bytes
     * dataArr:   BTreeSpec.PAGE_SIZE-upperBound bytes
     */
    public void toBytes(byte[] byteArray, int start) throws ChaiDBException {
        super.toBytes(byteArray, start);
        int step = start + super.getRecordLength();
        ByteTool.shortToBytes(byteArray, step, lowBound);
        step += LOWBOUND_SIZE;
        ByteTool.shortToBytes(byteArray, step, upperBound);
        step += UPPERBOUND_SIZE;
        System.arraycopy(headArr, 0, byteArray, step, lowBound);
        step += lowBound;
        System.arraycopy(dataArr, 0, byteArray, step, BTreeSpec.PAGE_SIZE - upperBound);
    }

    /**
     * get current log record type total length
     *
     * @return int total lenth
     */
    public int getRecordLength() {
        return super.getRecordLength() + LOWBOUND_SIZE + UPPERBOUND_SIZE + lowBound + (BTreeSpec.PAGE_SIZE - upperBound);
    }
}
TOP

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

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.