<?php
/*
 * $RCSfile: DatabaseLockSystem.class,v $
 *
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2005 Bharat Mediratta
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */
/**
 * @version $Revision: 1.5 $ $Date: 2005/08/23 03:49:02 $
 * @package GalleryCore
 * @author Bharat Mediratta <bharat@menalto.com>
 */

GalleryCoreApi::relativeRequireOnce('modules/core/classes/GalleryLockSystem.class');

/**
 * Database backed locking.  This is less efficient than filesystem based
 * locking, but is more reliable and portable.
 *
 * @package GalleryCore
 * @subpackage Classes
 * @abstract
 */
class DatabaseLockSystem extends GalleryLockSystem {

    /**
     * Information about all the locks we currently hold
     */
    var $_locks;

    /**
     * Constructor
     */
    function DatabaseLockSystem() {
	$this->_locks = array();
    }

    /**
     * @see GalleryLockSystem::acquireReadLock()
     */
    function acquireReadLock($ids, $timeout=10) {
	global $gallery;
	$storage =& $gallery->getStorage();

	list ($ret, list($lockId, $lock)) = $storage->acquireReadLock($ids, $timeout);
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

	$this->_locks[$lockId] = $lock;
	return array(GalleryStatus::success(), $lockId);
    }

    /**
     * @see GalleryLockSystem::isReadLocked()
     */
    function isReadLocked($id) {
	foreach ($this->_locks as $lockId => $lock) {
	    if (in_array($id, $lock['ids'])) {
		return true;
	    }
	}

	return false;
    }

    /**
     * @see GalleryLockSystem::acquireWriteLock()
     */
    function acquireWriteLock($ids, $timeout=10) {
	global $gallery;
	$storage =& $gallery->getStorage();

	list ($ret, list($lockId, $lock)) = $storage->acquireWriteLock($ids, $timeout);
	if ($ret->isError()) {
	    return array($ret->wrap(__FILE__, __LINE__), null);
	}

	$this->_locks[$lockId] = $lock;
	return array(GalleryStatus::success(), $lockId);
    }

    /**
     * @see GalleryLockSystem::isWriteLocked()
     */
    function isWriteLocked($id) {
	foreach ($this->_locks as $lockId => $lock) {
	    if ($lock['type'] == LOCK_WRITE && in_array($id, $lock['ids'])) {
		return true;
	    }
	}

	return false;
    }

    /**
     * @see GalleryLockSystem::releaseLocks()
     */
    function releaseLocks($lockIds) {
	if (!is_array($lockIds)) {
	    $lockIds = array($lockIds);
	}

	if (!empty($lockIds)) {
	    global $gallery;
	    $storage =& $gallery->getStorage();

	    $ret = $storage->releaseLocks($lockIds);
	    if ($ret->isError()) {
		return $ret->wrap(__FILE__, __LINE__);
	    }

	    foreach ($lockIds as $lockId) {
		unset($this->_locks[$lockId]);
	    }
	}

	return GalleryStatus::success();
    }

    /**
     * @see GalleryLockSystem::refreshAllLocks()
     */
    function releaseAllLocks() {
	$ret = $this->releaseLocks(array_keys($this->_locks));
	if ($ret->isError()) {
	    return $ret->wrap(__FILE__, __LINE__);
	}
	return GalleryStatus::success();
    }

    /**
     * @see GalleryLockSystem::refreshLocks()
     */
    function refreshLocks($freshUntil) {
	global $gallery;
	$storage =& $gallery->getStorage();

	if (!empty($this->_locks)) {
	    $ret = $storage->refreshLocks(array_keys($this->_locks), $freshUntil);
	    if ($ret->isError()) {
		return $ret->wrap(__FILE__, __LINE__);
	    }
	}

	return GalleryStatus::success();
    }

    /**
     * Return the ids of all the locks we hold
     *
     * @return array lock ids
     */
    function getLockIds() {
	return array_keys($this->_locks);
    }
}
?>
