objectcache: normalize BagOStuff method overriding pattern for *Multi() methods
authorAaron Schulz <aschulz@wikimedia.org>
Thu, 11 Jul 2019 11:32:35 +0000 (04:32 -0700)
committerAaron Schulz <aschulz@wikimedia.org>
Thu, 11 Jul 2019 11:32:35 +0000 (04:32 -0700)
Change-Id: I1bebb60307b1a166461cb5f9a55a79194cc0e363

includes/libs/objectcache/BagOStuff.php
includes/libs/objectcache/CachedBagOStuff.php
includes/libs/objectcache/EmptyBagOStuff.php
includes/libs/objectcache/MemcachedPeclBagOStuff.php
includes/libs/objectcache/MemcachedPhpBagOStuff.php
includes/libs/objectcache/MultiWriteBagOStuff.php
includes/libs/objectcache/RedisBagOStuff.php
includes/libs/objectcache/ReplicatedBagOStuff.php
includes/libs/objectcache/WinCacheBagOStuff.php
includes/objectcache/SqlBagOStuff.php

index d13626a..c47f6ee 100644 (file)
@@ -407,7 +407,7 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar
         * @param int $flags Bitfield of BagOStuff::WRITE_* constants
         * @return bool Success
         */
-       protected function mergeViaCas( $key, $callback, $exptime = 0, $attempts = 10, $flags = 0 ) {
+       final protected function mergeViaCas( $key, callable $callback, $exptime, $attempts, $flags ) {
                do {
                        $casToken = null; // passed by reference
                        // Get the old value and CAS token from cache
@@ -665,19 +665,18 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar
        /**
         * Delete all objects expiring before a certain date.
         * @param string|int $timestamp The reference date in MW or TS_UNIX format
-        * @param callable|null $progressCallback Optional, a function which will be called
+        * @param callable|null $progress Optional, a function which will be called
         *     regularly during long-running operations with the percentage progress
         *     as the first parameter. [optional]
         * @param int $limit Maximum number of keys to delete [default: INF]
         *
-        * @return bool Success, false if unimplemented
+        * @return bool Success; false if unimplemented
         */
        public function deleteObjectsExpiringBefore(
                $timestamp,
-               callable $progressCallback = null,
+               callable $progress = null,
                $limit = INF
        ) {
-               // stub
                return false;
        }
 
@@ -726,11 +725,10 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar
         * @return bool Success
         * @since 1.24
         */
-       final public function setMulti( array $data, $exptime = 0, $flags = 0 ) {
+       public function setMulti( array $data, $exptime = 0, $flags = 0 ) {
                if ( ( $flags & self::WRITE_ALLOW_SEGMENTS ) === self::WRITE_ALLOW_SEGMENTS ) {
                        throw new InvalidArgumentException( __METHOD__ . ' got WRITE_ALLOW_SEGMENTS' );
                }
-
                return $this->doSetMulti( $data, $exptime, $flags );
        }
 
@@ -745,7 +743,6 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar
                foreach ( $data as $key => $value ) {
                        $res = $this->doSet( $key, $value, $exptime, $flags ) && $res;
                }
-
                return $res;
        }
 
@@ -759,11 +756,10 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar
         * @return bool Success
         * @since 1.33
         */
-       final public function deleteMulti( array $keys, $flags = 0 ) {
+       public function deleteMulti( array $keys, $flags = 0 ) {
                if ( ( $flags & self::WRITE_ALLOW_SEGMENTS ) === self::WRITE_ALLOW_SEGMENTS ) {
                        throw new InvalidArgumentException( __METHOD__ . ' got WRITE_ALLOW_SEGMENTS' );
                }
-
                return $this->doDeleteMulti( $keys, $flags );
        }
 
@@ -777,7 +773,6 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar
                foreach ( $keys as $key ) {
                        $res = $this->doDelete( $key, $flags ) && $res;
                }
-
                return $res;
        }
 
@@ -853,7 +848,7 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar
         * @param mixed $mainValue
         * @return string|null|bool The combined string, false if missing, null on error
         */
-       protected function resolveSegments( $key, $mainValue ) {
+       final protected function resolveSegments( $key, $mainValue ) {
                if ( SerializedValueContainer::isUnified( $mainValue ) ) {
                        return $this->unserialize( $mainValue->{SerializedValueContainer::UNIFIED_DATA} );
                }
@@ -929,7 +924,7 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar
         * @param callable $workCallback
         * @since 1.28
         */
-       public function addBusyCallback( callable $workCallback ) {
+       final public function addBusyCallback( callable $workCallback ) {
                $this->busyCallbacks[] = $workCallback;
        }
 
@@ -938,9 +933,7 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar
         */
        protected function debug( $text ) {
                if ( $this->debugMode ) {
-                       $this->logger->debug( "{class} debug: $text", [
-                               'class' => static::class,
-                       ] );
+                       $this->logger->debug( "{class} debug: $text", [ 'class' => static::class ] );
                }
        }
 
@@ -948,7 +941,7 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar
         * @param int $exptime
         * @return bool
         */
-       protected function expiryIsRelative( $exptime ) {
+       final protected function expiryIsRelative( $exptime ) {
                return ( $exptime != 0 && $exptime < ( 10 * self::TTL_YEAR ) );
        }
 
@@ -964,9 +957,7 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar
         * @param int $exptime Absolute TTL or 0 for indefinite
         * @return int
         */
-       protected function convertToExpiry( $exptime ) {
-               $exptime = (int)$exptime; // sanity
-
+       final protected function convertToExpiry( $exptime ) {
                return $this->expiryIsRelative( $exptime )
                        ? (int)$this->getCurrentTime() + $exptime
                        : $exptime;
@@ -979,16 +970,10 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar
         * @param int $exptime
         * @return int
         */
-       protected function convertToRelative( $exptime ) {
-               if ( $exptime >= ( 10 * self::TTL_YEAR ) ) {
-                       $exptime -= (int)$this->getCurrentTime();
-                       if ( $exptime <= 0 ) {
-                               $exptime = 1;
-                       }
-                       return $exptime;
-               } else {
-                       return $exptime;
-               }
+       final protected function convertToRelative( $exptime ) {
+               return $this->expiryIsRelative( $exptime )
+                       ? (int)$exptime
+                       : max( $exptime - (int)$this->getCurrentTime(), 1 );
        }
 
        /**
@@ -997,7 +982,7 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar
         * @param mixed $value
         * @return bool
         */
-       protected function isInteger( $value ) {
+       final protected function isInteger( $value ) {
                if ( is_int( $value ) ) {
                        return true;
                } elseif ( !is_string( $value ) ) {
@@ -1080,7 +1065,7 @@ abstract class BagOStuff implements IExpiringStore, IStoreKeyEncoder, LoggerAwar
         * @param BagOStuff[] $bags
         * @return int[] Resulting flag map (class ATTR_* constant => class QOS_* constant)
         */
-       protected function mergeFlagMaps( array $bags ) {
+       final protected function mergeFlagMaps( array $bags ) {
                $map = [];
                foreach ( $bags as $bag ) {
                        foreach ( $bag->attrMap as $attr => $rank ) {
index 50ee1e3..ea434e0 100644 (file)
@@ -83,16 +83,12 @@ class CachedBagOStuff extends BagOStuff {
 
        public function deleteObjectsExpiringBefore(
                $timestamp,
-               callable $progressCallback = null,
+               callable $progress = null,
                $limit = INF
        ) {
-               $this->procCache->deleteObjectsExpiringBefore( $timestamp, $progressCallback, $limit );
+               $this->procCache->deleteObjectsExpiringBefore( $timestamp, $progress, $limit );
 
-               return $this->backend->deleteObjectsExpiringBefore(
-                       $timestamp,
-                       $progressCallback,
-                       $limit
-               );
+               return $this->backend->deleteObjectsExpiringBefore( $timestamp, $progress, $limit );
        }
 
        // These just call the backend (tested elsewhere)
index 575bc58..6dc1363 100644 (file)
@@ -33,7 +33,7 @@ class EmptyBagOStuff extends BagOStuff {
                return false;
        }
 
-       protected function doSet( $key, $value, $exp = 0, $flags = 0 ) {
+       protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
                return true;
        }
 
index f6721ce..221bc82 100644 (file)
@@ -261,7 +261,7 @@ class MemcachedPeclBagOStuff extends MemcachedBagOStuff {
                return $result;
        }
 
-       public function doGetMulti( array $keys, $flags = 0 ) {
+       protected function doGetMulti( array $keys, $flags = 0 ) {
                $this->debug( 'getMulti(' . implode( ', ', $keys ) . ')' );
                foreach ( $keys as $key ) {
                        $this->validateKeyEncoding( $key );
@@ -270,7 +270,7 @@ class MemcachedPeclBagOStuff extends MemcachedBagOStuff {
                return $this->checkResult( false, $result );
        }
 
-       public function doSetMulti( array $data, $exptime = 0, $flags = 0 ) {
+       protected function doSetMulti( array $data, $exptime = 0, $flags = 0 ) {
                $this->debug( 'setMulti(' . implode( ', ', array_keys( $data ) ) . ')' );
                foreach ( array_keys( $data ) as $key ) {
                        $this->validateKeyEncoding( $key );
@@ -279,7 +279,7 @@ class MemcachedPeclBagOStuff extends MemcachedBagOStuff {
                return $this->checkResult( false, $result );
        }
 
-       public function doDeleteMulti( array $keys, $flags = 0 ) {
+       protected function doDeleteMulti( array $keys, $flags = 0 ) {
                $this->debug( 'deleteMulti(' . implode( ', ', $keys ) . ')' );
                foreach ( $keys as $key ) {
                        $this->validateKeyEncoding( $key );
index 5d82fee..f8b91bc 100644 (file)
@@ -111,7 +111,7 @@ class MemcachedPhpBagOStuff extends MemcachedBagOStuff {
                );
        }
 
-       public function doGetMulti( array $keys, $flags = 0 ) {
+       protected function doGetMulti( array $keys, $flags = 0 ) {
                foreach ( $keys as $key ) {
                        $this->validateKeyEncoding( $key );
                }
index 2df8f0c..8e791ba 100644 (file)
@@ -210,12 +210,12 @@ class MultiWriteBagOStuff extends BagOStuff {
 
        public function deleteObjectsExpiringBefore(
                $timestamp,
-               callable $progressCallback = null,
+               callable $progress = null,
                $limit = INF
        ) {
                $ret = false;
                foreach ( $this->caches as $cache ) {
-                       if ( $cache->deleteObjectsExpiringBefore( $timestamp, $progressCallback, $limit ) ) {
+                       if ( $cache->deleteObjectsExpiringBefore( $timestamp, $progress, $limit ) ) {
                                $ret = true;
                        }
                }
@@ -236,7 +236,7 @@ class MultiWriteBagOStuff extends BagOStuff {
                return $res;
        }
 
-       public function doSetMulti( array $data, $exptime = 0, $flags = 0 ) {
+       public function setMulti( array $data, $exptime = 0, $flags = 0 ) {
                return $this->doWrite(
                        $this->cacheIndexes,
                        $this->usesAsyncWritesGivenFlags( $flags ),
@@ -245,7 +245,16 @@ class MultiWriteBagOStuff extends BagOStuff {
                );
        }
 
-       public function doDeleteMulti( array $data, $flags = 0 ) {
+       public function deleteMulti( array $data, $flags = 0 ) {
+               return $this->doWrite(
+                       $this->cacheIndexes,
+                       $this->usesAsyncWritesGivenFlags( $flags ),
+                       __FUNCTION__,
+                       func_get_args()
+               );
+       }
+
+       public function changeTTLMulti( array $keys, $exptime, $flags = 0 ) {
                return $this->doWrite(
                        $this->cacheIndexes,
                        $this->usesAsyncWritesGivenFlags( $flags ),
@@ -370,11 +379,19 @@ class MultiWriteBagOStuff extends BagOStuff {
                throw new LogicException( __METHOD__ . ': proxy class does not need this method.' );
        }
 
+       protected function doSetMulti( array $keys, $exptime = 0, $flags = 0 ) {
+               throw new LogicException( __METHOD__ . ': proxy class does not need this method.' );
+       }
+
+       protected function doDeleteMulti( array $keys, $flags = 0 ) {
+               throw new LogicException( __METHOD__ . ': proxy class does not need this method.' );
+       }
+
        protected function serialize( $value ) {
                throw new LogicException( __METHOD__ . ': proxy class does not need this method.' );
        }
 
-       protected function unserialize( $value ) {
+       protected function unserialize( $blob ) {
                throw new LogicException( __METHOD__ . ': proxy class does not need this method.' );
        }
 }
index f67b887..dd859ad 100644 (file)
@@ -106,15 +106,15 @@ class RedisBagOStuff extends BagOStuff {
                return $result;
        }
 
-       protected function doSet( $key, $value, $expiry = 0, $flags = 0 ) {
+       protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
                list( $server, $conn ) = $this->getConnection( $key );
                if ( !$conn ) {
                        return false;
                }
-               $expiry = $this->convertToRelative( $expiry );
+               $ttl = $this->convertToRelative( $exptime );
                try {
-                       if ( $expiry ) {
-                               $result = $conn->setex( $key, $expiry, $this->serialize( $value ) );
+                       if ( $ttl ) {
+                               $result = $conn->setex( $key, $ttl, $this->serialize( $value ) );
                        } else {
                                // No expiry, that is very different from zero expiry in Redis
                                $result = $conn->set( $key, $this->serialize( $value ) );
@@ -146,7 +146,7 @@ class RedisBagOStuff extends BagOStuff {
                return $result;
        }
 
-       public function doGetMulti( array $keys, $flags = 0 ) {
+       protected function doGetMulti( array $keys, $flags = 0 ) {
                $batches = [];
                $conns = [];
                foreach ( $keys as $key ) {
@@ -185,7 +185,7 @@ class RedisBagOStuff extends BagOStuff {
                return $result;
        }
 
-       public function doSetMulti( array $data, $expiry = 0, $flags = 0 ) {
+       protected function doSetMulti( array $data, $expiry = 0, $flags = 0 ) {
                $batches = [];
                $conns = [];
                foreach ( $data as $key => $value ) {
@@ -229,7 +229,7 @@ class RedisBagOStuff extends BagOStuff {
                return $result;
        }
 
-       public function doDeleteMulti( array $keys, $flags = 0 ) {
+       protected function doDeleteMulti( array $keys, $flags = 0 ) {
                $batches = [];
                $conns = [];
                foreach ( $keys as $key ) {
index 8c2b3f6..295ec30 100644 (file)
@@ -110,14 +110,10 @@ class ReplicatedBagOStuff extends BagOStuff {
 
        public function deleteObjectsExpiringBefore(
                $timestamp,
-               callable $progressCallback = null,
+               callable $progress = null,
                $limit = INF
        ) {
-               return $this->writeStore->deleteObjectsExpiringBefore(
-                       $timestamp,
-                       $progressCallback,
-                       $limit
-               );
+               return $this->writeStore->deleteObjectsExpiringBefore( $timestamp, $progress, $limit );
        }
 
        public function getMulti( array $keys, $flags = 0 ) {
@@ -126,14 +122,18 @@ class ReplicatedBagOStuff extends BagOStuff {
                        : $this->readStore->getMulti( $keys, $flags );
        }
 
-       public function doSetMulti( array $data, $exptime = 0, $flags = 0 ) {
+       public function setMulti( array $data, $exptime = 0, $flags = 0 ) {
                return $this->writeStore->setMulti( $data, $exptime, $flags );
        }
 
-       public function doDeleteMulti( array $keys, $flags = 0 ) {
+       public function deleteMulti( array $keys, $flags = 0 ) {
                return $this->writeStore->deleteMulti( $keys, $flags );
        }
 
+       public function changeTTLMulti( array $keys, $exptime, $flags = 0 ) {
+               return $this->writeStore->changeTTLMulti( $keys, $exptime, $flags );
+       }
+
        public function incr( $key, $value = 1 ) {
                return $this->writeStore->incr( $key, $value );
        }
@@ -189,6 +189,14 @@ class ReplicatedBagOStuff extends BagOStuff {
                throw new LogicException( __METHOD__ . ': proxy class does not need this method.' );
        }
 
+       protected function doSetMulti( array $keys, $exptime = 0, $flags = 0 ) {
+               throw new LogicException( __METHOD__ . ': proxy class does not need this method.' );
+       }
+
+       protected function doDeleteMulti( array $keys, $flags = 0 ) {
+               throw new LogicException( __METHOD__ . ': proxy class does not need this method.' );
+       }
+
        protected function serialize( $value ) {
                throw new LogicException( __METHOD__ . ': proxy class does not need this method.' );
        }
index 9d7e143..d75b344 100644 (file)
@@ -67,8 +67,8 @@ class WinCacheBagOStuff extends BagOStuff {
                return $success;
        }
 
-       protected function doSet( $key, $value, $expire = 0, $flags = 0 ) {
-               $result = wincache_ucache_set( $key, $this->serialize( $value ), $expire );
+       protected function doSet( $key, $value, $exptime = 0, $flags = 0 ) {
+               $result = wincache_ucache_set( $key, $this->serialize( $value ), $exptime );
 
                // false positive, wincache_ucache_set returns an empty array
                // in some circumstances.
index 7fa0bfa..a8c23d6 100644 (file)
@@ -340,7 +340,7 @@ class SqlBagOStuff extends BagOStuff {
                return $values;
        }
 
-       public function doSetMulti( array $data, $exptime = 0, $flags = 0 ) {
+       protected function doSetMulti( array $data, $exptime = 0, $flags = 0 ) {
                return $this->modifyMulti( $data, $exptime, $flags, self::$OP_SET );
        }
 
@@ -509,7 +509,7 @@ class SqlBagOStuff extends BagOStuff {
                return (bool)$db->affectedRows();
        }
 
-       public function doDeleteMulti( array $keys, $flags = 0 ) {
+       protected function doDeleteMulti( array $keys, $flags = 0 ) {
                return $this->modifyMulti(
                        array_fill_keys( $keys, null ),
                        0,
@@ -565,7 +565,7 @@ class SqlBagOStuff extends BagOStuff {
                return $ok;
        }
 
-       public function changeTTLMulti( array $keys, $exptime, $flags = 0 ) {
+       protected function doChangeTTLMulti( array $keys, $exptime, $flags = 0 ) {
                return $this->modifyMulti(
                        array_fill_keys( $keys, null ),
                        $exptime,
@@ -634,7 +634,7 @@ class SqlBagOStuff extends BagOStuff {
 
        public function deleteObjectsExpiringBefore(
                $timestamp,
-               callable $progressCallback = null,
+               callable $progress = null,
                $limit = INF
        ) {
                /** @noinspection PhpUnusedLocalVariableInspection */
@@ -653,7 +653,7 @@ class SqlBagOStuff extends BagOStuff {
                                $this->deleteServerObjectsExpiringBefore(
                                        $db,
                                        $timestamp,
-                                       $progressCallback,
+                                       $progress,
                                        $limit,
                                        $numServersDone,
                                        $keysDeletedCount