Merge "[FileBackend] Added getScopedLocksForOps() function."
[lhc/web/wiklou.git] / includes / filerepo / backend / FileBackend.php
index 59ca04d..42ddcbf 100644 (file)
@@ -8,6 +8,23 @@
  */
 
 /**
+ * Base class for all file backends.
+ *
+ * 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.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
  * @file
  * @ingroup FileBackend
  * @author Aaron Schulz
@@ -43,6 +60,9 @@ abstract class FileBackend {
        protected $name; // string; unique backend name
        protected $wikiId; // string; unique wiki name
        protected $readOnly; // string; read-only explanation message
+       protected $parallelize; // string; when to do operations in parallel
+       protected $concurrency; // integer; how many operations can be done in parallel
+
        /** @var LockManager */
        protected $lockManager;
        /** @var FileJournal */
@@ -63,8 +83,12 @@ abstract class FileBackend {
         *                     Journals simply log changes to files stored in the backend.
         *     'readOnly'    : Write operations are disallowed if this is a non-empty string.
         *                     It should be an explanation for the backend being read-only.
+        *     'parallelize' : When to do file operations in parallel (when possible).
+        *                     Allowed values are "implicit", "explicit" and "off".
+        *     'concurrency' : How many file operations can be done in parallel.
         *
         * @param $config Array
+        * @throws MWException
         */
        public function __construct( array $config ) {
                $this->name = $config['name'];
@@ -83,6 +107,12 @@ abstract class FileBackend {
                $this->readOnly = isset( $config['readOnly'] )
                        ? (string)$config['readOnly']
                        : '';
+               $this->parallelize = isset( $config['parallelize'] )
+                       ? (string)$config['parallelize']
+                       : 'off';
+               $this->concurrency = isset( $config['concurrency'] )
+                       ? (int)$config['concurrency']
+                       : 50;
        }
 
        /**
@@ -187,6 +217,7 @@ abstract class FileBackend {
         *                         This has no effect unless the 'force' flag is set.
         * 'nonJournaled'        : Don't log this operation batch in the file journal.
         *                         This limits the ability of recovery scripts.
+        * 'parallelize'         : Try to do operations in parallel when possible.
         *
         * Remarks on locking:
         * File system paths given to operations should refer to files that are
@@ -212,6 +243,16 @@ abstract class FileBackend {
                        unset( $opts['nonLocking'] );
                        unset( $opts['allowStale'] );
                }
+               $opts['concurrency'] = 1; // off
+               if ( $this->parallelize === 'implicit' ) {
+                       if ( !isset( $opts['parallelize'] ) || $opts['parallelize'] ) {
+                               $opts['concurrency'] = $this->concurrency;
+                       }
+               } elseif ( $this->parallelize === 'explicit' ) {
+                       if ( !empty( $opts['parallelize'] ) ) {
+                               $opts['concurrency'] = $this->concurrency;
+                       }
+               }
                return $this->doOperationsInternal( $ops, $opts );
        }
 
@@ -246,8 +287,7 @@ abstract class FileBackend {
         * @return Status
         */
        final public function create( array $params, array $opts = array() ) {
-               $params['op'] = 'create';
-               return $this->doOperation( $params, $opts );
+               return $this->doOperation( array( 'op' => 'create' ) + $params, $opts );
        }
 
        /**
@@ -261,8 +301,7 @@ abstract class FileBackend {
         * @return Status
         */
        final public function store( array $params, array $opts = array() ) {
-               $params['op'] = 'store';
-               return $this->doOperation( $params, $opts );
+               return $this->doOperation( array( 'op' => 'store' ) + $params, $opts );
        }
 
        /**
@@ -276,8 +315,7 @@ abstract class FileBackend {
         * @return Status
         */
        final public function copy( array $params, array $opts = array() ) {
-               $params['op'] = 'copy';
-               return $this->doOperation( $params, $opts );
+               return $this->doOperation( array( 'op' => 'copy' ) + $params, $opts );
        }
 
        /**
@@ -291,8 +329,7 @@ abstract class FileBackend {
         * @return Status
         */
        final public function move( array $params, array $opts = array() ) {
-               $params['op'] = 'move';
-               return $this->doOperation( $params, $opts );
+               return $this->doOperation( array( 'op' => 'move' ) + $params, $opts );
        }
 
        /**
@@ -306,10 +343,80 @@ abstract class FileBackend {
         * @return Status
         */
        final public function delete( array $params, array $opts = array() ) {
-               $params['op'] = 'delete';
-               return $this->doOperation( $params, $opts );
+               return $this->doOperation( array( 'op' => 'delete' ) + $params, $opts );
        }
 
+       /**
+        * Perform a set of independent file operations on some files.
+        *
+        * This does no locking, nor journaling, and possibly no stat calls.
+        * Any destination files that already exist will be overwritten.
+        * This should *only* be used on non-original files, like cache files.
+        *
+        * Supported operations and their parameters:
+        * a) Create a new file in storage with the contents of a string
+        *     array(
+        *         'op'                  => 'create',
+        *         'dst'                 => <storage path>,
+        *         'content'             => <string of new file contents>
+        *     )
+        * b) Copy a file system file into storage
+        *     array(
+        *         'op'                  => 'store',
+        *         'src'                 => <file system path>,
+        *         'dst'                 => <storage path>
+        *     )
+        * c) Copy a file within storage
+        *     array(
+        *         'op'                  => 'copy',
+        *         'src'                 => <storage path>,
+        *         'dst'                 => <storage path>
+        *     )
+        * d) Move a file within storage
+        *     array(
+        *         'op'                  => 'move',
+        *         'src'                 => <storage path>,
+        *         'dst'                 => <storage path>
+        *     )
+        * e) Delete a file within storage
+        *     array(
+        *         'op'                  => 'delete',
+        *         'src'                 => <storage path>,
+        *         'ignoreMissingSource' => <boolean>
+        *     )
+        * f) Do nothing (no-op)
+        *     array(
+        *         'op'                  => 'null',
+        *     )
+        *
+        * Boolean flags for operations (operation-specific):
+        * 'ignoreMissingSource' : The operation will simply succeed and do
+        *                         nothing if the source file does not exist.
+        *
+        * Return value:
+        * This returns a Status, which contains all warnings and fatals that occured
+        * during the operation. The 'failCount', 'successCount', and 'success' members
+        * will reflect each operation attempted for the given files. The status will be
+        * considered "OK" as long as no fatal errors occured.
+        *
+        * @param $ops Array Set of operations to execute
+        * @return Status
+        */
+       final public function doQuickOperations( array $ops ) {
+               if ( $this->isReadOnly() ) {
+                       return Status::newFatal( 'backend-fail-readonly', $this->name, $this->readOnly );
+               }
+               foreach ( $ops as &$op ) {
+                       $op['overwrite'] = true; // avoids RTTs in key/value stores
+               }
+               return $this->doQuickOperationsInternal( $ops );
+       }
+
+       /**
+        * @see FileBackend::doQuickOperations()
+        */
+       abstract protected function doQuickOperationsInternal( array $ops );
+
        /**
         * Concatenate a list of storage files into a single file system file.
         * The target path should refer to a file that is already locked or
@@ -557,6 +664,7 @@ abstract class FileBackend {
         * $params include:
         *     dir : storage directory
         *
+        * @param $params array
         * @return bool|null Returns null on failure
         * @since 1.20
         */
@@ -576,6 +684,7 @@ abstract class FileBackend {
         *     dir     : storage directory
         *     topOnly : only return direct child dirs of the directory
         *
+        * @param $params array
         * @return Traversable|Array|null Returns null on failure
         * @since 1.20
         */
@@ -590,6 +699,7 @@ abstract class FileBackend {
         * $params include:
         *     dir : storage directory
         *
+        * @param $params array
         * @return Traversable|Array|null Returns null on failure
         * @since 1.20
         */
@@ -611,6 +721,7 @@ abstract class FileBackend {
         *     dir     : storage directory
         *     topOnly : only return direct child files of the directory (@since 1.20)
         *
+        * @param $params array
         * @return Traversable|Array|null Returns null on failure
         */
        abstract public function getFileList( array $params );
@@ -624,6 +735,7 @@ abstract class FileBackend {
         * $params include:
         *     dir : storage directory
         *
+        * @param $params array
         * @return Traversable|Array|null Returns null on failure
         * @since 1.20
         */
@@ -711,6 +823,15 @@ abstract class FileBackend {
                return "mwstore://{$this->name}";
        }
 
+       /**
+        * Get the file journal object for this backend
+        *
+        * @return FileJournal
+        */
+       final public function getJournal() {
+               return $this->fileJournal;
+       }
+
        /**
         * Check if a given path is a "mwstore://" path.
         * This does not do any further validation or any existence checks.