Package org.chaidb.db.log.logrecord

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

/*
* 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.log.Lsn;

/**
* This is the checkpoint record.  It contains the lsn that the checkpoint
* guarantees and a pointer to the last checkpoint so we can walk backwards
* by checkpoint.
* <p/>
* last_ckp:
* The previous checkpoint.
* <p/>
* timestamp:
* timestamps while doing fuzzy checkpoint
* <p/>
* smallestLsn:
* Smallest first Lsn of all active TXN
*/
public class TxnFuzzyCkpLogRecord extends TxnCkpLogRecord {

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

    /* if no active txn while doing fuzzy checkpoint,smallestLsn is equals to
       current checkpoint log lsn,if has active txns,it's euqals to smallest
       first_lsn of all active txns. */
    private Lsn smallestLsn;


    /**
     * Default Constructor
     */
    public TxnFuzzyCkpLogRecord() {
        super();
        smallestLsn = new Lsn();
        super.setType(LOG_TXN_FUZZY_CHECKPOINT);
    }

    /**
     * Constructor
     *
     * @param newLastCkpLsn
     * @param newTimeStamp
     */
    public TxnFuzzyCkpLogRecord(Lsn newLastCkpLsn, long newTimeStamp, byte flag, Lsn newSmallestLsn) {
        super(newLastCkpLsn, newTimeStamp, flag);
        this.smallestLsn = newSmallestLsn;//it's null while no active txns
        super.setType(LOG_TXN_FUZZY_CHECKPOINT);
    }

    /**
     * Constructor
     *
     * @param newLastCkpLsn
     * @param newTimeStamp
     * @param newTxnId
     */
    public TxnFuzzyCkpLogRecord(Lsn newLastCkpLsn, long newTimeStamp, int newTxnId, byte flag, Lsn newSmallestLsn) {
        super(newLastCkpLsn, newTimeStamp, newTxnId, flag);
        this.smallestLsn = newSmallestLsn;//it's null while no active txns
        super.setType(LOG_TXN_FUZZY_CHECKPOINT);
    }


    /**
     * get smallestLsn of current TxnFuzzyCkpLogRecord Object
     *
     * @return Lsn smallestLsn
     */
    public Lsn getSmallestLsn() {
        if (smallestLsn == null) {
            return null;
        }
        return new Lsn(smallestLsn);
    }

    /**
     * set smallestLsn of current TxnFuzzyCkpLogRecord Object
     * while no active txns,smallestLsn is equals to current checkpoint log Lsn,
     * it will be set while putting to log buffer in LogManagerImpl.
     *
     * @param smallestLsn
     */
    public void setSmallestLsn(Lsn smallestLsn) {
        this.smallestLsn = smallestLsn;
    }


    /**
     * 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 TxnFuzzyCkpLogRecord Object */
        int step = start + super.getRecordLength();
        smallestLsn.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 TxnFuzzyCkpLogRecord object......");
        super.print();

        logger.debug("smallestLsn's file id:" + this.getSmallestLsn().getFileId());

        logger.debug("smallestLsn's offset:" + Integer.toHexString(this.getSmallestLsn().getOffset()));

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

    }

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

        int step = start + super.getRecordLength();
        smallestLsn.toBytes(byteArray, step);
    }

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

}
TOP

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

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.