Package org.chaidb.db.log.logrecord

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

/*
* 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.Db;
import org.chaidb.db.exception.ChaiDBException;
import org.chaidb.db.helper.ByteTool;
import org.chaidb.db.log.LogManager;
import org.chaidb.db.log.LogRecord;
import org.chaidb.db.log.Lsn;

/*
* This is the (new) log operation for a child commit.  It is
* logged as a record in the PARENT.  The child field contains
* the transaction ID of the child committing and the c_lsn is
* the last LSN of the child's log trail.
*/

public class TxnChildLogRecord extends LogRecord {

    private static final Logger logger = Logger.getLogger(TxnChildLogRecord.class);

    /*The child field contains the transaction ID of the child committing*/
    private int childTxnId;

    /*the last LSN of the child's log trail*/
    private Lsn lastLsnOfChildTxn;

    static int TXN_CHILD_CHILDTXNID_SIZE = 4; //4 bytes

    /**
     * Default Constructor
     */
    public TxnChildLogRecord() {
        super();
        lastLsnOfChildTxn = new Lsn();
        super.setType(LOG_TXN_CHILD);
    }

    /**
     * Constructor
     *
     * @param newChildTxnId
     * @param newLastLsnOfChildTxn
     */
    public TxnChildLogRecord(int newChildTxnId, Lsn newLastLsnOfChildTxn) {
        super();
        this.childTxnId = newChildTxnId;
        this.lastLsnOfChildTxn = newLastLsnOfChildTxn;
        super.setType(LOG_TXN_CHILD);
    }

    /**
     * Constructor
     *
     * @param newChildTxnId
     * @param newLastLsnOfChildTxn
     * @param newTxnId
     */
    public TxnChildLogRecord(int newChildTxnId, Lsn newLastLsnOfChildTxn, int newTxnId) {
        super();
        this.childTxnId = newChildTxnId;
        this.lastLsnOfChildTxn = newLastLsnOfChildTxn;
        super.setType(LOG_TXN_CHILD);
        super.setTxnId(newTxnId);
    }

    /**
     * get childTxnId of current TxnChildLogRecord Object
     *
     * @return int childTxnId
     */
    public int getChildTxnId() {
        return childTxnId;
    }

    /**
     * get lastLsnOfChildTxn of current TxnChildLogRecord Object
     *
     * @return Lsn lastLsnOfChildTxn
     */
    public Lsn getLastLsnOfChildTxn() {
        if (lastLsnOfChildTxn == null) {
            return null;
        }
        return new Lsn(lastLsnOfChildTxn);
    }

    /**
     * just for compatible
     */
    public String getBTreeFileName() {
        return null;
    }

    /**
     * user interface to add a log record and put it to buffer pool
     */
    public Lsn log() throws ChaiDBException {
        super.log();
        LogManager logMgr = Db.getLogManager();
        Lsn newLsn = logMgr.put(this, LogManager.LOG_CHILD_FLUSH);

        return newLsn;
    }

    /**
     * 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 TxnChildLogRecord Object */
        int step = start + super.getRecordLength();
        childTxnId = ByteTool.bytesToInt(bArr, step, msbFirst);
        step = step + TXN_CHILD_CHILDTXNID_SIZE;
        lastLsnOfChildTxn.read(bArr, step);

        return true;
    }

    /**
     * redo or undo the operation for recovery
     */
    public boolean recover(short flag) throws ChaiDBException {
        return true;
    }

    /**
     * print data and help in debugging log files
     */
    public void print() throws ChaiDBException {

        //logger.debug("begin: printing the information of TxnChildLogRecord object......");
        super.print();
        logger.debug("childTxnId:" + this.getChildTxnId());

        logger.debug("lastLsnOfChildTxn's file id:" + this.getLastLsnOfChildTxn().getFileId());

        logger.debug("lastLsnOfChildTxn's offset:" + this.getLastLsnOfChildTxn().getOffset());

        //logger.debug("end: printing the information of TxnChildLogRecord object.");

    }

    /**
     * converts a log record instance into a byte array.
     * The byte array has the following format:
     * ---------------------------------------------------------------
     * | hdr | type | txnId | prevLsn | childTxnId | lastLsnOfChildTxn |
     * ---------------------------------------------------------------
     * hdr: 8 bytes,the byte array of the header,generated by Hdr.toBytes().
     * type:    1 bytes.
     * txnId:   4 bytes.
     * prevLsn: 8 bytes,the byte array of the header of lsn,generated by Lsn.toBytes().
     * <p/>
     * childTxnId:   4 bytes.
     * lastLsnOfChildTxn:   8 bytes,generated by Lsn.toBytes().
     */
    public void toBytes(byte[] byteArray, int start) throws ChaiDBException {
        super.toBytes(byteArray, start);
        int step = start + super.getRecordLength();

        ByteTool.intToBytes(byteArray, step, childTxnId, msbFirst);
        step += TXN_CHILD_CHILDTXNID_SIZE;

        lastLsnOfChildTxn.toBytes(byteArray, step);
    }

    /**
     * get current log record type total length
     *
     * @return int total lenth
     */
    public int getRecordLength() {
        return super.getRecordLength() + Lsn.getLsnLength() + TXN_CHILD_CHILDTXNID_SIZE;
    }

}
TOP

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

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.