Rename interfaces and classes in the Predis\Cluster namespace.

A few methods were renamed accordingly for consistency
This commit is contained in:
Daniele Alessandri
2013-11-16 16:02:34 +01:00
parent c26b6763f9
commit e6f1788e27
19 changed files with 93 additions and 95 deletions
+4 -4
View File
@@ -13,13 +13,13 @@ require 'SharedConfigurations.php';
// Developers can customize the distribution strategy used by the client
// to distribute keys among a cluster of servers simply by creating a class
// that implements Predis\Distribution\DistributionStrategyInterface.
// that implements Predis\Distribution\DistributorInterface.
use Predis\Connection\PredisCluster;
use Predis\Cluster\Distribution\DistributionStrategyInterface;
use Predis\Cluster\Distributor\DistributorInterface;
use Predis\Cluster\Hash\HashGeneratorInterface;
class NaiveDistributionStrategy implements DistributionStrategyInterface, HashGeneratorInterface
class NaiveDistributor implements DistributorInterface, HashGeneratorInterface
{
private $nodes;
private $nodesCount;
@@ -67,7 +67,7 @@ class NaiveDistributionStrategy implements DistributionStrategyInterface, HashGe
$options = array(
'cluster' => function () {
$distributor = new NaiveDistributionStrategy();
$distributor = new NaiveDistributor();
$cluster = new PredisCluster($distributor);
return $cluster;
@@ -9,17 +9,17 @@
* file that was distributed with this source code.
*/
namespace Predis\Cluster\Distribution;
namespace Predis\Cluster\Distributor;
use Predis\Cluster\Hash\HashGeneratorInterface;
/**
* A distributor implements the logic to automatically distribute
* keys among several nodes for client-side sharding.
* Distributors implement the logic to automatically distribute keys among
* several nodes for client-side sharding.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
interface DistributionStrategyInterface
interface DistributorInterface
{
/**
* Adds a node to the distributor with an optional weight.
@@ -9,13 +9,15 @@
* file that was distributed with this source code.
*/
namespace Predis\Cluster\Distribution;
namespace Predis\Cluster\Distributor;
use Exception;
/**
* Exception class that identifies empty rings.
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class EmptyRingException extends \Exception
class EmptyRingException extends Exception
{
}
@@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
namespace Predis\Cluster\Distribution;
namespace Predis\Cluster\Distributor;
use Predis\Cluster\Hash\HashGeneratorInterface;
@@ -21,7 +21,7 @@ use Predis\Cluster\Hash\HashGeneratorInterface;
* @author Daniele Alessandri <suppakilla@gmail.com>
* @author Lorenzo Castelli <lcastelli@gmail.com>
*/
class HashRing implements DistributionStrategyInterface, HashGeneratorInterface
class HashRing implements DistributorInterface, HashGeneratorInterface
{
const DEFAULT_REPLICAS = 128;
const DEFAULT_WEIGHT = 100;
@@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
namespace Predis\Cluster\Distribution;
namespace Predis\Cluster\Distributor;
/**
* This class implements an hashring-based distributor that uses the same
@@ -19,7 +19,7 @@ namespace Predis\Cluster\Distribution;
* @author Daniele Alessandri <suppakilla@gmail.com>
* @author Lorenzo Castelli <lcastelli@gmail.com>
*/
class KetamaPureRing extends HashRing
class KetamaRing extends HashRing
{
const DEFAULT_REPLICAS = 160;
@@ -21,7 +21,7 @@ use Predis\Command\ScriptedCommand;
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class PredisClusterHashStrategy implements CommandHashStrategyInterface
class PredisStrategy implements StrategyInterface
{
private $commands;
private $hashGenerator;
@@ -11,7 +11,6 @@
namespace Predis\Cluster;
use Predis\Cluster\Hash\CRC16HashGenerator;
use Predis\Command\CommandInterface;
use Predis\Command\ScriptedCommand;
@@ -21,7 +20,7 @@ use Predis\Command\ScriptedCommand;
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class RedisClusterHashStrategy implements CommandHashStrategyInterface
class RedisStrategy implements StrategyInterface
{
private $commands;
private $hashGenerator;
@@ -32,7 +31,7 @@ class RedisClusterHashStrategy implements CommandHashStrategyInterface
public function __construct()
{
$this->commands = $this->getDefaultCommands();
$this->hashGenerator = new CRC16HashGenerator();
$this->hashGenerator = new Hash\CRC16HashGenerator();
}
/**
@@ -21,7 +21,7 @@ use Predis\Command\CommandInterface;
*
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
interface CommandHashStrategyInterface
interface StrategyInterface
{
/**
* Returns the hash for the given command using the specified algorithm, or null
+11 -11
View File
@@ -11,11 +11,11 @@
namespace Predis\Connection;
use Predis\Cluster\CommandHashStrategyInterface;
use Countable;
use IteratorAggregate;
use Predis\NotSupportedException;
use Predis\Cluster\PredisClusterHashStrategy;
use Predis\Cluster\Distribution\DistributionStrategyInterface;
use Predis\Cluster\Distribution\HashRing;
use Predis\Cluster;
use Predis\Cluster\Distributor;
use Predis\Command\CommandInterface;
/**
@@ -25,21 +25,21 @@ use Predis\Command\CommandInterface;
* @author Daniele Alessandri <suppakilla@gmail.com>
* @todo Add the ability to remove connections from pool.
*/
class PredisCluster implements ClusterConnectionInterface, \IteratorAggregate, \Countable
class PredisCluster implements ClusterConnectionInterface, IteratorAggregate, Countable
{
private $pool;
private $strategy;
private $distributor;
/**
* @param DistributionStrategyInterface $distributor Distribution strategy used by the cluster.
* @param Distributor\DistributorInterface $distributor Distribution strategy used by the cluster.
*/
public function __construct(DistributionStrategyInterface $distributor = null)
public function __construct(Distributor\DistributorInterface $distributor = null)
{
$distributor = $distributor ?: new HashRing();
$distributor = $distributor ?: new Distributor\HashRing();
$this->pool = array();
$this->strategy = new PredisClusterHashStrategy($distributor->getHashGenerator());
$this->strategy = new Cluster\PredisStrategy($distributor->getHashGenerator());
$this->distributor = $distributor;
}
@@ -166,9 +166,9 @@ class PredisCluster implements ClusterConnectionInterface, \IteratorAggregate, \
* Returns the underlying command hash strategy used to hash
* commands by their keys.
*
* @return CommandHashStrategyInterface
* @return Cluster\StrategyInterface
*/
public function getCommandHashStrategy()
public function getClusterStrategy()
{
return $this->strategy;
}
+5 -7
View File
@@ -12,9 +12,8 @@
namespace Predis\Connection;
use Predis\ClientException;
use Predis\Cluster\CommandHashStrategyInterface;
use Predis\NotSupportedException;
use Predis\Cluster\RedisClusterHashStrategy;
use Predis\Cluster;
use Predis\Command\CommandInterface;
use Predis\Response;
@@ -39,7 +38,7 @@ class RedisCluster implements ClusterConnectionInterface, \IteratorAggregate, \C
{
$this->pool = array();
$this->slots = array();
$this->strategy = new RedisClusterHashStrategy();
$this->strategy = new Cluster\RedisStrategy();
$this->connections = $connections ?: new ConnectionFactory();
}
@@ -304,12 +303,11 @@ class RedisCluster implements ClusterConnectionInterface, \IteratorAggregate, \C
}
/**
* Returns the underlying command hash strategy used to hash
* commands by their keys.
* Returns the underlying hash strategy used to hash commands by their keys.
*
* @return CommandHashStrategyInterface
* @return Cluster\StrategyInterface
*/
public function getCommandHashStrategy()
public function getClusterStrategy()
{
return $this->strategy;
}
@@ -9,30 +9,30 @@
* file that was distributed with this source code.
*/
namespace Predis\Cluster\Distribution;
namespace Predis\Cluster\Distributor;
use PHPUnit_Framework_TestCase as StandardTestCase;
/**
*
*/
abstract class DistributionStrategyTestCase extends StandardTestCase
abstract class DistributorTestCase extends StandardTestCase
{
/**
* Returns a new instance of the tested distributor.
*
* @return Predis\Cluster\Distribution\DistributionStrategyInterface
* @return Predis\Cluster\Distributor\DistributorInterface
*/
protected abstract function getDistributorInstance();
/**
* Returns a list of nodes from the hashring.
*
* @param DistributionStrategyInterface $ring Hashring instance.
* @param DistributorInterface $ring Hashring instance.
* @param int $iterations Number of nodes to fetch.
* @return array Nodes from the hashring.
*/
protected function getNodes(DistributionStrategyInterface $ring, $iterations = 10)
protected function getNodes(DistributorInterface $ring, $iterations = 10)
{
$nodes = array();
@@ -49,7 +49,7 @@ abstract class DistributionStrategyTestCase extends StandardTestCase
*/
public function testEmptyRingThrowsException()
{
$this->setExpectedException('Predis\Cluster\Distribution\EmptyRingException');
$this->setExpectedException('Predis\Cluster\Distributor\EmptyRingException');
$ring = $this->getDistributorInstance();
$ring->get('nodekey');
@@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
namespace Predis\Cluster\Distribution;
namespace Predis\Cluster\Distributor;
use PHPUnit_Framework_TestCase as StandardTestCase;
@@ -24,7 +24,7 @@ class EmptyRingExceptionTest extends StandardTestCase
public function testExceptionMessage()
{
$message = 'Empty Ring';
$this->setExpectedException('Predis\Cluster\Distribution\EmptyRingException', $message);
$this->setExpectedException('Predis\Cluster\Distributor\EmptyRingException', $message);
throw new EmptyRingException($message);
}
@@ -9,12 +9,12 @@
* file that was distributed with this source code.
*/
namespace Predis\Cluster\Distribution;
namespace Predis\Cluster\Distributor;
/**
* @todo To be improved.
*/
class HashRingTest extends DistributionStrategyTestCase
class HashRingTest extends DistributorTestCase
{
/**
* {@inheritdoc}
@@ -131,7 +131,7 @@ class HashRingTest extends DistributionStrategyTestCase
}
/**
* @todo This tests should be moved in Predis\Cluster\Distribution\DistributionStrategyTestCase
* @todo This tests should be moved in Predis\Cluster\Distributor\DistributorTestCase
* @group disconnected
*/
public function testCallbackToGetNodeHash()
@@ -9,19 +9,19 @@
* file that was distributed with this source code.
*/
namespace Predis\Cluster\Distribution;
namespace Predis\Cluster\Distributor;
/**
* @todo To be improved.
*/
class KetamaPureRingTest extends DistributionStrategyTestCase
class KetamaRingTest extends DistributorTestCase
{
/**
* {@inheritdoc}
*/
public function getDistributorInstance()
{
return new KetamaPureRing();
return new KetamaRing();
}
/**
@@ -132,7 +132,7 @@ class KetamaPureRingTest extends DistributionStrategyTestCase
}
/**
* @todo This tests should be moved in Predis\Cluster\Distribution\DistributionStrategyTestCase
* @todo This tests should be moved in Predis\Cluster\Distributor\DistributorTestCase
* @group disconnected
*/
public function testCallbackToGetNodeHash()
@@ -145,7 +145,7 @@ class KetamaPureRingTest extends DistributionStrategyTestCase
->with($node)
->will($this->returnValue($node));
$ring = new KetamaPureRing($callable);
$ring = new KetamaRing($callable);
$ring->add($node);
$this->getNodes($ring);
@@ -13,13 +13,12 @@ namespace Predis\Cluster;
use PHPUnit_Framework_TestCase as StandardTestCase;
use Predis\Cluster\Distribution\HashRing;
use Predis\Profile\ServerProfile;
/**
*
*/
class PredisClusterHashStrategyTest extends StandardTestCase
class PredisStrategyTest extends StandardTestCase
{
/**
* @group disconnected
@@ -29,7 +28,7 @@ class PredisClusterHashStrategyTest extends StandardTestCase
// NOTE: 32 and 64 bits PHP runtimes can produce different hash values.
$expected = PHP_INT_SIZE == 4 ? -1938594527 : 2356372769;
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$this->assertSame($expected, $strategy->getKeyHash('{foo}'));
$this->assertSame($expected, $strategy->getKeyHash('{foo}:bar'));
@@ -45,7 +44,7 @@ class PredisClusterHashStrategyTest extends StandardTestCase
*/
public function testSupportedCommands()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$this->assertSame($this->getExpectedCommands(), $strategy->getSupportedCommands());
}
@@ -55,7 +54,7 @@ class PredisClusterHashStrategyTest extends StandardTestCase
*/
public function testReturnsNullOnUnsupportedCommand()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$command = ServerProfile::getDevelopment()->createCommand('ping');
$this->assertNull($strategy->getHash($command));
@@ -66,7 +65,7 @@ class PredisClusterHashStrategyTest extends StandardTestCase
*/
public function testFirstKeyCommands()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$profile = ServerProfile::getDevelopment();
$arguments = array('key');
@@ -81,7 +80,7 @@ class PredisClusterHashStrategyTest extends StandardTestCase
*/
public function testAllKeysCommands()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$profile = ServerProfile::getDevelopment();
$arguments = array('{key}:1', '{key}:2', '{key}:3', '{key}:4');
@@ -96,7 +95,7 @@ class PredisClusterHashStrategyTest extends StandardTestCase
*/
public function testInterleavedKeysCommands()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$profile = ServerProfile::getDevelopment();
$arguments = array('{key}:1', 'value1', '{key}:2', 'value2');
@@ -111,7 +110,7 @@ class PredisClusterHashStrategyTest extends StandardTestCase
*/
public function testKeysForBlockingListCommands()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$profile = ServerProfile::getDevelopment();
$arguments = array('{key}:1', '{key}:2', 10);
@@ -126,7 +125,7 @@ class PredisClusterHashStrategyTest extends StandardTestCase
*/
public function testKeysForZsetAggregationCommands()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$profile = ServerProfile::getDevelopment();
$arguments = array('{key}:destination', 2, '{key}:1', '{key}:1', array('aggregate' => 'SUM'));
@@ -141,7 +140,7 @@ class PredisClusterHashStrategyTest extends StandardTestCase
*/
public function testKeysForBitOpCommand()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$profile = ServerProfile::getDevelopment();
$arguments = array('AND', '{key}:destination', '{key}:src:1', '{key}:src:2');
@@ -156,7 +155,7 @@ class PredisClusterHashStrategyTest extends StandardTestCase
*/
public function testKeysForScriptCommand()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$profile = ServerProfile::getDevelopment();
$arguments = array('%SCRIPT%', 2, '{key}:1', '{key}:2', 'value1', 'value2');
@@ -171,7 +170,7 @@ class PredisClusterHashStrategyTest extends StandardTestCase
*/
public function testKeysForScriptedCommand()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$arguments = array('{key}:1', '{key}:2', 'value1', 'value2');
$command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript', 'getKeysCount'));
@@ -191,7 +190,7 @@ class PredisClusterHashStrategyTest extends StandardTestCase
*/
public function testUnsettingCommandHandler()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$profile = ServerProfile::getDevelopment();
$strategy->setCommandHandler('set');
@@ -209,7 +208,7 @@ class PredisClusterHashStrategyTest extends StandardTestCase
*/
public function testSettingCustomCommandHandler()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$profile = ServerProfile::getDevelopment();
$callable = $this->getMock('stdClass', array('__invoke'));
@@ -229,15 +228,15 @@ class PredisClusterHashStrategyTest extends StandardTestCase
// ******************************************************************** //
/**
* Creates the default hash strategy object.
* Creates the default cluster strategy object.
*
* @return CommandHashStrategyInterface
* @return StrategyInterface
*/
protected function getHashStrategy()
protected function getClusterStrategy()
{
$distributor = new HashRing();
$distributor = new Distributor\HashRing();
$hashGenerator = $distributor->getHashGenerator();
$strategy = new PredisClusterHashStrategy($hashGenerator);
$strategy = new PredisStrategy($hashGenerator);
return $strategy;
}
@@ -18,14 +18,14 @@ use Predis\Profile\ServerProfile;
/**
*
*/
class RedisClusterHashStrategyTest extends StandardTestCase
class RedisStrategyTest extends StandardTestCase
{
/**
* @group disconnected
*/
public function testDoesNotSupportKeyTags()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$this->assertSame(35910, $strategy->getKeyHash('{foo}'));
$this->assertSame(60032, $strategy->getKeyHash('{foo}:bar'));
@@ -38,7 +38,7 @@ class RedisClusterHashStrategyTest extends StandardTestCase
*/
public function testSupportedCommands()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$this->assertSame($this->getExpectedCommands(), $strategy->getSupportedCommands());
}
@@ -48,7 +48,7 @@ class RedisClusterHashStrategyTest extends StandardTestCase
*/
public function testReturnsNullOnUnsupportedCommand()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$command = ServerProfile::getDevelopment()->createCommand('ping');
$this->assertNull($strategy->getHash($command));
@@ -59,7 +59,7 @@ class RedisClusterHashStrategyTest extends StandardTestCase
*/
public function testFirstKeyCommands()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$profile = ServerProfile::getDevelopment();
$arguments = array('key');
@@ -74,7 +74,7 @@ class RedisClusterHashStrategyTest extends StandardTestCase
*/
public function testAllKeysCommandsWithOneKey()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$profile = ServerProfile::getDevelopment();
$arguments = array('key');
@@ -89,7 +89,7 @@ class RedisClusterHashStrategyTest extends StandardTestCase
*/
public function testAllKeysCommandsWithMoreKeys()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$profile = ServerProfile::getDevelopment();
$arguments = array('key1', 'key2');
@@ -104,7 +104,7 @@ class RedisClusterHashStrategyTest extends StandardTestCase
*/
public function testInterleavedKeysCommandsWithOneKey()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$profile = ServerProfile::getDevelopment();
$arguments = array('key:1', 'value1');
@@ -119,7 +119,7 @@ class RedisClusterHashStrategyTest extends StandardTestCase
*/
public function testInterleavedKeysCommandsWithMoreKeys()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$profile = ServerProfile::getDevelopment();
$arguments = array('key:1', 'value1', 'key:2', 'value2');
@@ -134,7 +134,7 @@ class RedisClusterHashStrategyTest extends StandardTestCase
*/
public function testKeysForBlockingListCommandsWithOneKey()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$profile = ServerProfile::getDevelopment();
$arguments = array('key:1', 10);
@@ -149,7 +149,7 @@ class RedisClusterHashStrategyTest extends StandardTestCase
*/
public function testKeysForBlockingListCommandsWithMoreKeys()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$profile = ServerProfile::getDevelopment();
$arguments = array('key:1', 'key:2', 10);
@@ -164,7 +164,7 @@ class RedisClusterHashStrategyTest extends StandardTestCase
*/
public function testKeysForScriptCommand()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$profile = ServerProfile::getDevelopment();
$arguments = array('%SCRIPT%', 1, 'key:1', 'value1');
@@ -179,7 +179,7 @@ class RedisClusterHashStrategyTest extends StandardTestCase
*/
public function testKeysForScriptedCommand()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$arguments = array('key:1', 'value1');
$command = $this->getMock('Predis\Command\ScriptedCommand', array('getScript', 'getKeysCount'));
@@ -199,7 +199,7 @@ class RedisClusterHashStrategyTest extends StandardTestCase
*/
public function testUnsettingCommandHandler()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$profile = ServerProfile::getDevelopment();
$strategy->setCommandHandler('set');
@@ -217,7 +217,7 @@ class RedisClusterHashStrategyTest extends StandardTestCase
*/
public function testSettingCustomCommandHandler()
{
$strategy = $this->getHashStrategy();
$strategy = $this->getClusterStrategy();
$profile = ServerProfile::getDevelopment();
$callable = $this->getMock('stdClass', array('__invoke'));
@@ -237,13 +237,13 @@ class RedisClusterHashStrategyTest extends StandardTestCase
// ******************************************************************** //
/**
* Creates the default hash strategy object.
* Creates the default cluster strategy object.
*
* @return CommandHashStrategyInterface
* @return StrategyInterface
*/
protected function getHashStrategy()
protected function getClusterStrategy()
{
$strategy = new RedisClusterHashStrategy();
$strategy = new RedisStrategy();
return $strategy;
}
@@ -26,7 +26,7 @@ class PredisClusterTest extends StandardTestCase
public function testExposesCommandHashStrategy()
{
$cluster = new PredisCluster();
$this->assertInstanceOf('Predis\Cluster\PredisClusterHashStrategy', $cluster->getCommandHashStrategy());
$this->assertInstanceOf('Predis\Cluster\PredisStrategy', $cluster->getClusterStrategy());
}
/**
+1 -1
View File
@@ -27,7 +27,7 @@ class RedisClusterTest extends StandardTestCase
public function testExposesCommandHashStrategy()
{
$cluster = new RedisCluster();
$this->assertInstanceOf('Predis\Cluster\RedisClusterHashStrategy', $cluster->getCommandHashStrategy());
$this->assertInstanceOf('Predis\Cluster\RedisStrategy', $cluster->getClusterStrategy());
}
/**
+1 -1
View File
@@ -15,4 +15,4 @@ require __DIR__.'/PHPUnit/ArrayHasSameValuesConstraint.php';
require __DIR__.'/PHPUnit/CommandTestCase.php';
require __DIR__.'/PHPUnit/ConnectionTestCase.php';
require __DIR__.'/PHPUnit/ServerVersionTestCase.php';
require __DIR__.'/PHPUnit/DistributionStrategyTestCase.php';
require __DIR__.'/PHPUnit/DistributorTestCase.php';