Merge "Added some lock()/unlock() support for SQLite using lock file emulation"
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Thu, 13 Feb 2014 00:32:32 +0000 (00:32 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Thu, 13 Feb 2014 00:32:32 +0000 (00:32 +0000)
includes/db/DatabaseSqlite.php

index fa827d1..2e23d73 100644 (file)
@@ -41,8 +41,11 @@ class DatabaseSqlite extends DatabaseBase {
        /** @var PDO */
        protected $mConn;
 
+       /** @var FSLockManager (hopefully on the same server as the DB) */
+       protected $lockMgr;
+
        function __construct( $p = null ) {
-               global $wgSharedDB;
+               global $wgSharedDB, $wgSQLiteDataDir;
 
                if ( !is_array( $p ) ) { // legacy calling pattern
                        wfDeprecated( __METHOD__ . " method called without parameter array.", "1.22" );
@@ -67,6 +70,8 @@ class DatabaseSqlite extends DatabaseBase {
                                }
                        }
                }
+
+               $this->lockMgr = new FSLockManager( array( 'lockDirectory' => "$wgSQLiteDataDir/locks" ) );
        }
 
        /**
@@ -866,6 +871,22 @@ class DatabaseSqlite extends DatabaseBase {
                return $s;
        }
 
+       public function lock( $lockName, $method, $timeout = 5 ) {
+               global $wgSQLiteDataDir;
+
+               if ( !is_dir( "$wgSQLiteDataDir/locks" ) ) { // create dir as needed
+                       if ( !is_writable( $wgSQLiteDataDir ) || !mkdir( "$wgSQLiteDataDir/locks" ) ) {
+                               throw new DBError( "Cannot create directory \"$wgSQLiteDataDir/locks\"." );
+                       }
+               }
+
+               return $this->lockMgr->lock( array( $lockName ), LockManager::LOCK_EX, $timeout )->isOK();
+       }
+
+       public function unlock( $lockName, $method ) {
+               return $this->lockMgr->unlock( array( $lockName ), LockManager::LOCK_EX )->isOK();
+       }
+
        /**
         * Build a concatenation list to feed into a SQL query
         *