Package org.chaidb.db.index

Source Code of org.chaidb.db.index.LockableBTree

/*
* 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.
*
*/

/**
* User: Administrator
* Date: Nov 11, 2003
* Time: 3:24:30 PM
*/
package org.chaidb.db.index;

import org.apache.log4j.Logger;
import org.chaidb.db.KernelContext;
import org.chaidb.db.exception.ChaiDBException;
import org.chaidb.db.index.btree.BTreeLock;
import org.chaidb.db.index.btree.bufmgr.PageNumber;
import org.chaidb.db.lock.Lock;
import org.chaidb.db.lock.LockManager;

public abstract class LockableBTree {
    private Logger logger = Logger.getLogger(LockableBTree.class);

    protected int id;

    public void acquire(KernelContext kContext, int mode) throws ChaiDBException {
        Lock lock = null;
        try {
            if (mode == IBTreeConst.READ_MODE) {
                lock = BTreeLock.acquire(kContext, LockManager.LOCK_WAITING, new PageNumber(id, 0, 0), LockManager.LOCK_READ);
            } else if (mode == IBTreeConst.WRITE_MODE) {
                lock = BTreeLock.acquire(kContext, LockManager.LOCK_WAITING, new PageNumber(id, 0, 0), LockManager.LOCK_WRITE);
            } else {
                logger.fatal("Open mode is not recognized.");
            }
            kContext.addLock(getBTreeName(), lock);
        } catch (ChaiDBException e) {
            logger.error("Acquire tree lock on tree id: " + id + " failed, "); //Todo: make it  perfect.
            throw e;
        }


    }

    public void release(KernelContext kContext) {
        try {
            Lock lock = kContext.getLock(getBTreeName());
            if (lock != null) {
                if (kContext.getNeedLog()) {
                    if (lock.getMode() == LockManager.LOCK_READ) {
                        BTreeLock.release(kContext, id, lock);

                    } else {
                        lock = BTreeLock.change(kContext, lock, id, LockManager.LOCK_READ);
                    }
                } else {
                    BTreeLock.release(kContext, id, lock);
                }
            }
        } catch (ChaiDBException e) {
            logger.error("Release tree lock on tree id: " + id + " failed, "); //Todo: make it  perfect.
        }

    }

    public abstract String getBTreeName();
}
TOP

Related Classes of org.chaidb.db.index.LockableBTree

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.