From b0b9de684a5b4354361e693dd2ea2e16644869d6 Mon Sep 17 00:00:00 2001 From: Ivan Shumkov Date: Thu, 28 Oct 2010 17:17:12 +0400 Subject: [PATCH] fix #4052 --- .../Command/GetSortedSetLengthByScore.php | 38 +++++++++++++++++++ library/Rediska/Commands.php | 1 + library/Rediska/Key/Hash.php | 12 +++++- library/Rediska/Key/List.php | 7 +++- library/Rediska/Key/Set.php | 7 +++- library/Rediska/Key/SortedSet.php | 23 +++++++++-- .../Command/GetSortedSetLengthByScoreTest.php | 14 +++++++ tests/library/Rediska/Key/SortedSetTest.php | 14 ++++++- 8 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 library/Rediska/Command/GetSortedSetLengthByScore.php create mode 100644 tests/library/Rediska/Command/GetSortedSetLengthByScoreTest.php diff --git a/library/Rediska/Command/GetSortedSetLengthByScore.php b/library/Rediska/Command/GetSortedSetLengthByScore.php new file mode 100644 index 0000000..1c48a06 --- /dev/null +++ b/library/Rediska/Command/GetSortedSetLengthByScore.php @@ -0,0 +1,38 @@ +_rediska->getConnectionByKeyName($key); + + $command = array('ZCOUNT', $this->_rediska->getOption('namespace') . $key, $min, $max); + + return new Rediska_Connection_Exec($connection, $command); + } +} \ No newline at end of file diff --git a/library/Rediska/Commands.php b/library/Rediska/Commands.php index 9b68cd4..8c0b6f2 100644 --- a/library/Rediska/Commands.php +++ b/library/Rediska/Commands.php @@ -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', diff --git a/library/Rediska/Key/Hash.php b/library/Rediska/Key/Hash.php index c673e68..b2bee3f 100644 --- a/library/Rediska/Key/Hash.php +++ b/library/Rediska/Key/Hash.php @@ -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 * @@ -242,7 +252,7 @@ public function toArray() public function count() { - return $this->_getRediskaOn()->getHashLength($this->getName()); + return $this->getLength(); } /* IteratorAggregate implementation */ diff --git a/library/Rediska/Key/List.php b/library/Rediska/Key/List.php index 45ab39b..ab81340 100644 --- a/library/Rediska/Key/List.php +++ b/library/Rediska/Key/List.php @@ -54,7 +54,7 @@ public function prepend($value) * * @return integer */ - public function count() + public function getLength() { return $this->_getRediskaOn()->getListLength($this->getName()); } @@ -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()); diff --git a/library/Rediska/Key/Set.php b/library/Rediska/Key/Set.php index 34300df..9d28600 100644 --- a/library/Rediska/Key/Set.php +++ b/library/Rediska/Key/Set.php @@ -70,7 +70,7 @@ public function move($set, $value) * * @return integer */ - public function count() + public function getLength() { return $this->_getRediskaOn()->getSetLength($this->getName()); } @@ -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()); diff --git a/library/Rediska/Key/SortedSet.php b/library/Rediska/Key/SortedSet.php index 87758c1..b1334cc 100644 --- a/library/Rediska/Key/SortedSet.php +++ b/library/Rediska/Key/SortedSet.php @@ -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 * @@ -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()); diff --git a/tests/library/Rediska/Command/GetSortedSetLengthByScoreTest.php b/tests/library/Rediska/Command/GetSortedSetLengthByScoreTest.php new file mode 100644 index 0000000..43ddba7 --- /dev/null +++ b/tests/library/Rediska/Command/GetSortedSetLengthByScoreTest.php @@ -0,0 +1,14 @@ +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); + } +} \ No newline at end of file diff --git a/tests/library/Rediska/Key/SortedSetTest.php b/tests/library/Rediska/Key/SortedSetTest.php index 6b689bf..cb8a691 100644 --- a/tests/library/Rediska/Key/SortedSetTest.php +++ b/tests/library/Rediska/Key/SortedSetTest.php @@ -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);