Skip to content

Commit

Permalink
fix #4052
Browse files Browse the repository at this point in the history
  • Loading branch information
shumkov committed Oct 28, 2010
1 parent c5399f2 commit b0b9de6
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 8 deletions.
38 changes: 38 additions & 0 deletions library/Rediska/Command/GetSortedSetLengthByScore.php
@@ -0,0 +1,38 @@
<?php

/**
* Get count of members from sorted set by min and max score
*
* @author Ivan Shumkov
* @package Rediska
* @subpackage Commands
* @version @package_version@
* @link http://rediska.geometria-lab.net
* @license http://www.opensource.org/licenses/bsd-license.php
*/
class Rediska_Command_GetSortedSetLengthByScore extends Rediska_Command_Abstract
{
/**
* Supported version
*
* @var string
*/
protected $_version = '1.1';

/**
* Create command
*
* @param string $key Key name
* @param number $min Min score
* @param number $max Max score
* @return Rediska_Connection_Exec
*/
public function create($key, $min, $max)
{
$connection = $this->_rediska->getConnectionByKeyName($key);

$command = array('ZCOUNT', $this->_rediska->getOption('namespace') . $key, $min, $max);

return new Rediska_Connection_Exec($connection, $command);
}
}
1 change: 1 addition & 0 deletions library/Rediska/Commands.php
Expand Up @@ -77,6 +77,7 @@ class Rediska_Commands
'getsortedset' => 'Rediska_Command_GetSortedSet',
'getfromsortedsetbyscore' => 'Rediska_Command_GetFromSortedSetByScore',
'getsortedsetlength' => 'Rediska_Command_GetSortedSetLength',
'getsortedsetlengthbyscore' => 'Rediska_Command_GetSortedSetLengthByScore',
'incrementscoreinsortedset' => 'Rediska_Command_IncrementScoreInSortedSet',
'deletefromsortedsetbyscore' => 'Rediska_Command_DeleteFromSortedSetByScore',
'deletefromsortedsetbyrank' => 'Rediska_Command_DeleteFromSortedSetByRank',
Expand Down
12 changes: 11 additions & 1 deletion library/Rediska/Key/Hash.php
Expand Up @@ -228,6 +228,16 @@ public function getValues()
return $this->_getRediskaOn()->getHashValues($this->getName());
}

/**
* Get hash length
*
* @return intger
*/
public function getLength()
{
return $this->_getRediskaOn()->getHashLength($this->getName());
}

/**
* Get hash as array
*
Expand All @@ -242,7 +252,7 @@ public function toArray()

public function count()
{
return $this->_getRediskaOn()->getHashLength($this->getName());
return $this->getLength();
}

/* IteratorAggregate implementation */
Expand Down
7 changes: 6 additions & 1 deletion library/Rediska/Key/List.php
Expand Up @@ -54,7 +54,7 @@ public function prepend($value)
*
* @return integer
*/
public function count()
public function getLength()
{
return $this->_getRediskaOn()->getListLength($this->getName());
}
Expand Down Expand Up @@ -248,6 +248,11 @@ public function fromArray(array $array)
* Implement intrefaces
*/

public function count()
{
return $this->getLength();
}

public function getIterator()
{
return new ArrayObject($this->toArray());
Expand Down
7 changes: 6 additions & 1 deletion library/Rediska/Key/Set.php
Expand Up @@ -70,7 +70,7 @@ public function move($set, $value)
*
* @return integer
*/
public function count()
public function getLength()
{
return $this->_getRediskaOn()->getSetLength($this->getName());
}
Expand Down Expand Up @@ -180,6 +180,11 @@ public function fromArray(array $array)
* Implement intrefaces
*/

public function count()
{
return $this->getLength();
}

public function getIterator()
{
return new ArrayObject($this->toArray());
Expand Down
23 changes: 20 additions & 3 deletions library/Rediska/Key/SortedSet.php
Expand Up @@ -49,17 +49,29 @@ public function remove($value)

return $result;
}

/**
* Get Sorted set length
*
*
* @return integer
*/
public function count()
public function getLength()
{
return $this->_getRediskaOn()->getSortedSetLength($this->getName());
}

/**
* Get count of members from sorted set by min and max score
*
* @param integer $min Min score
* @param integer $max Max score
* @return integer
*/
public function getLengthByScore($min, $max)
{
return $this->_getRediskaOn()->getSortedSetLengthByScore($this->getName(), $min, $max);
}

/**
* Get Sorted set by score
*
Expand Down Expand Up @@ -218,6 +230,11 @@ public function fromArray(array $array)
* Implement intrefaces
*/

public function count()
{
return $this->getLength();
}

public function getIterator()
{
return new ArrayObject($this->toArray());
Expand Down
14 changes: 14 additions & 0 deletions tests/library/Rediska/Command/GetSortedSetLengthByScoreTest.php
@@ -0,0 +1,14 @@
<?php

class Rediska_Command_GetSortedSetLengthByScoreTest extends Rediska_TestCase
{
public function testGetSortedSetLengthByScore()
{
$this->rediska->addToSortedSet('test', 1, 1);
$this->rediska->addToSortedSet('test', 2, 2);
$this->rediska->addToSortedSet('test', 3, 3);

$reply = $this->rediska->getSortedSetLengthByScore('test', 2, 3);
$this->assertEquals(2, $reply);
}
}
14 changes: 12 additions & 2 deletions tests/library/Rediska/Key/SortedSetTest.php
Expand Up @@ -47,15 +47,25 @@ public function testRemove()
$this->assertTrue(empty($values));
}

public function testCount()
public function testGetLength()
{
$this->rediska->addToSortedSet('test', 123, 1);
$this->rediska->addToSortedSet('test', 456, 2);

$this->assertEquals(2, $this->set->count());
$this->assertEquals(2, $this->set->getLength());
$this->assertEquals(2, count($this->set));
}

public function testGetLengthByScore()
{
$this->rediska->addToSortedSet('test', 1, 1);
$this->rediska->addToSortedSet('test', 2, 2);
$this->rediska->addToSortedSet('test', 3, 3);

$reply = $this->set->getLengthByScore(2, 3);
$this->assertEquals(2, $reply);
}

public function testGetByScore()
{
$this->rediska->addToSortedSet('test', 123, 1);
Expand Down

0 comments on commit b0b9de6

Please sign in to comment.