Merge commit 'catalyst/MOODLE_19_STABLE' into mdl19-linuxchix
[moodle-linuxchix.git] / search / Zend / Search / Lucene / LockManager.php
blob4730b51a5fef9c67a998a51c64ece6344f48afe1
1 <?php
2 /**
3 * Zend Framework
5 * LICENSE
7 * This source file is subject to the new BSD license that is bundled
8 * with this package in the file LICENSE.txt.
9 * It is also available through the world-wide-web at this URL:
10 * http://framework.zend.com/license/new-bsd
11 * If you did not receive a copy of the license and are unable to
12 * obtain it through the world-wide-web, please send an email
13 * to license@zend.com so we can send you a copy immediately.
15 * @category Zend
16 * @package Zend_Search_Lucene
17 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
18 * @license http://framework.zend.com/license/new-bsd New BSD License
22 /** Zend_Search_Lucene_Exception */
23 require_once 'Zend/Search/Lucene/Exception.php';
25 /** Zend_Search_Lucene_Storage_Directory */
26 require_once 'Zend/Search/Lucene/Storage/Directory.php';
28 /** Zend_Search_Lucene_Storage_File */
29 require_once 'Zend/Search/Lucene/Storage/File.php';
33 /**
34 * This is an utility class which provides index locks processing functionality
36 * @category Zend
37 * @package Zend_Search_Lucene
38 * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
39 * @license http://framework.zend.com/license/new-bsd New BSD License
41 class Zend_Search_Lucene_LockManager
43 const WRITE_LOCK_FILE = 'write.lock.file';
44 const READ_LOCK_FILE = 'read.lock.file';
45 const OPTIMIZATION_LOCK_FILE = 'optimization.lock.file';
47 /**
48 * Obtain exclusive write lock on the index
50 * @param Zend_Search_Lucene_Storage_Directory $lockDirectory
51 * @return Zend_Search_Lucene_Storage_File
52 * @throws Zend_Search_Lucene_Exception
54 public static function obtainWriteLock(Zend_Search_Lucene_Storage_Directory $lockDirectory)
56 $lock = $lockDirectory->createFile(self::WRITE_LOCK_FILE);
57 if (!$lock->lock(LOCK_EX)) {
58 throw new Zend_Search_Lucene_Exception('Can\'t obtain exclusive index lock');
60 return $lock;
63 /**
64 * Release exclusive write lock
66 * @param Zend_Search_Lucene_Storage_Directory $lockDirectory
68 public static function releaseWriteLock(Zend_Search_Lucene_Storage_Directory $lockDirectory)
70 $lock = $lockDirectory->getFileObject(self::WRITE_LOCK_FILE);
71 $lock->unlock();
74 /**
75 * Obtain shared read lock on the index
77 * It doesn't block other read or update processes, but prevent index from the premature cleaning-up
79 * @param Zend_Search_Lucene_Storage_Directory $defaultLockDirectory
80 * @return Zend_Search_Lucene_Storage_File
81 * @throws Zend_Search_Lucene_Exception
83 public static function obtainReadLock(Zend_Search_Lucene_Storage_Directory $lockDirectory)
85 $lock = $lockDirectory->createFile(self::READ_LOCK_FILE);
86 if (!$lock->lock(LOCK_SH)) {
87 throw new Zend_Search_Lucene_Exception('Can\'t obtain shared reading index lock');
89 return $lock;
92 /**
93 * Release shared read lock
95 * @param Zend_Search_Lucene_Storage_Directory $lockDirectory
97 public static function releaseReadLock(Zend_Search_Lucene_Storage_Directory $lockDirectory)
99 $lock = $lockDirectory->getFileObject(self::READ_LOCK_FILE);
100 $lock->unlock();
104 * Escalate Read lock to exclusive level
106 * @param Zend_Search_Lucene_Storage_Directory $lockDirectory
107 * @return boolean
109 public static function escalateReadLock(Zend_Search_Lucene_Storage_Directory $lockDirectory)
111 $lock = $lockDirectory->getFileObject(self::READ_LOCK_FILE);
113 // Try to escalate read lock
114 if (!$lock->lock(LOCK_EX, true)) {
115 // Restore lock state
116 $lock->lock(LOCK_SH);
117 return false;
119 return true;
123 * De-escalate Read lock to shared level
125 * @param Zend_Search_Lucene_Storage_Directory $lockDirectory
127 public static function deEscalateReadLock(Zend_Search_Lucene_Storage_Directory $lockDirectory)
129 $lock = $lockDirectory->getFileObject(self::READ_LOCK_FILE);
130 $lock->lock(LOCK_SH);
134 * Obtain exclusive optimization lock on the index
136 * Returns lock object on success and false otherwise (doesn't block execution)
138 * @param Zend_Search_Lucene_Storage_Directory $lockDirectory
139 * @return mixed
141 public static function obtainOptimizationLock(Zend_Search_Lucene_Storage_Directory $lockDirectory)
143 $lock = $lockDirectory->createFile(self::OPTIMIZATION_LOCK_FILE);
144 if (!$lock->lock(LOCK_EX, true)) {
145 return false;
147 return $lock;
151 * Release exclusive optimization lock
153 * @param Zend_Search_Lucene_Storage_Directory $lockDirectory
155 public static function releaseOptimizationLock(Zend_Search_Lucene_Storage_Directory $lockDirectory)
157 $lock = $lockDirectory->getFileObject(self::OPTIMIZATION_LOCK_FILE);
158 $lock->unlock();