Package org.chaidb.db.index.btree

Source Code of org.chaidb.db.index.btree.BTreeLock

/*
* 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.index.btree;

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

public class BTreeLock {
    private static final Logger logger = Logger.getLogger(BTreeLock.class);

    private static LockManager lm = Db.getLockManager();
    private static final boolean DEBUG = false;
    public static final int MAX_RETRYS = 0;
    public static final int ISOLATION_LEVEL = Config.getConfig("isolation.level", 0);
    public static final int ISOLATION_LEVEL_COMMIT_READ = 1;
    private static final String LOG_DIR = Config.getConfig("log.dir", "");
    private static final boolean _debug = false;

    /**
     * Acquire a lock for a object, which here is a page
     *
     * @return Lock the lock we get
     */
    public static Lock acquire(KernelContext kc, int flag, PageNumber p, int lockType) throws ChaiDBException {
        int locker = kc.getLocker();
        Lock lock = null;


        lock = lm.get(locker, flag, lm.OBJECT_BTREE, p.uniqueID(), lockType);

        if (_debug) {
            logger.debug("[AllLocks]" + "lock:" + lock.getLockID());

        }
        return lock;
    }

    /**
     * Change a lock's mode, such as upgrade or downgrade
     * In fact this change occurs on the lock specified by @param lock.
     *
     * @return lock with new mode, equal @param lock except of mode.
     */
    public static Lock change(KernelContext kc, Lock lock, int treeid, int lockType) throws ChaiDBException {
        if (lock == null) return null;
        if (_debug) {
            logger.debug("[AllLocks]" + "put btreeID:" + treeid + " Lock:" + lock.getLockID());
        }
        //if already got lock has the same type as the coming one, just reuturn the old one.
        if (lock.getMode() == lockType) return lock;

        /* modified by marriane 2002-9-20, committed read,
            needn't downgrade lock,directly return */
        if (ISOLATION_LEVEL == ISOLATION_LEVEL_COMMIT_READ) {
            return lock;
        }
        //here we reacquire a new lock and release the old one.
        Lock newlock = lm.get(kc.getLocker(), LockManager.LOCK_WAITING, lm.OBJECT_BTREE, lock.getLockID(), lockType);
        lm.put(lock);
        return newlock;
    }

    public static void release(KernelContext kc, int treeid, Lock lock) throws ChaiDBException {
        if (_debug) {
            logger.debug("[AllLocks]" + "put btreeID:" + treeid + "   Lock:" + lock.getLockID());
        }
        lm.put(lock);

    }
}
TOP

Related Classes of org.chaidb.db.index.btree.BTreeLock

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.