/library/Rediska/Key/SortedSet.php

https://github.com/netweaver/Rediska · PHP · 335 lines · 154 code · 43 blank · 138 comment · 17 complexity · 18131e0fdbd8beb43aa855a381c401c9 MD5 · raw file

  1. <?php
  2. // Require Rediska
  3. require_once dirname(__FILE__) . '/../../Rediska.php';
  4. /**
  5. * Rediska Sorted set key
  6. *
  7. * @author Ivan Shumkov
  8. * @package Rediska
  9. * @subpackage Key objects
  10. * @version @package_version@
  11. * @link http://rediska.geometria-lab.net
  12. * @license http://www.opensource.org/licenses/bsd-license.php
  13. */
  14. class Rediska_Key_SortedSet extends Rediska_Key_Abstract implements IteratorAggregate, ArrayAccess, Countable
  15. {
  16. /**
  17. * Add the specified member to the Sorted set
  18. *
  19. * @param mixed $value Value
  20. * @param numeric $score Score
  21. * @return boolean
  22. */
  23. public function add($value, $score)
  24. {
  25. $result = $this->_getRediskaOn()->addToSortedSet($this->getName(), $value, $score);
  26. if (!is_null($this->getExpire()) && $result) {
  27. $this->expire($this->getExpire(), $this->isExpireTimestamp());
  28. }
  29. return $result;
  30. }
  31. /**
  32. * Remove the specified member from the Sorted set
  33. *
  34. * @param mixed $value Value
  35. * @return boolean
  36. */
  37. public function remove($value)
  38. {
  39. $result = $this->_getRediskaOn()->deleteFromSortedSet($this->getName(), $value);
  40. if (!is_null($this->getExpire()) && $result) {
  41. $this->expire($this->getExpire(), $this->isExpireTimestamp());
  42. }
  43. return $result;
  44. }
  45. /**
  46. * Get Sorted set length
  47. *
  48. * @return integer
  49. */
  50. public function getLength()
  51. {
  52. return $this->_getRediskaOn()->getSortedSetLength($this->getName());
  53. }
  54. /**
  55. * Get count of members from sorted set by min and max score
  56. *
  57. * @param integer $min Min score
  58. * @param integer $max Max score
  59. * @return integer
  60. */
  61. public function getLengthByScore($min, $max)
  62. {
  63. return $this->_getRediskaOn()->getSortedSetLengthByScore($this->getName(), $min, $max);
  64. }
  65. /**
  66. * Get Sorted set by score
  67. *
  68. * @param number $min Min score
  69. * @param number $max Max score
  70. * @param boolean $withScores Get with scores
  71. * @param integer $limit Limit
  72. * @param integer $offset Offset
  73. * @return array
  74. */
  75. public function getByScore($min, $max, $withScores = false, $limit = null, $offset = null)
  76. {
  77. return $this->_getRediskaOn()->getFromSortedSetByScore($this->getName(), $min, $max, $withScores, $limit, $offset);
  78. }
  79. /**
  80. * Remove members from sorted set by score
  81. *
  82. * @param $min Min score
  83. * @param $max Max score
  84. * @return integer
  85. */
  86. public function removeByScore($min, $max)
  87. {
  88. return $this->_getRediskaOn()->DeleteFromSortedSetByScore($this->getName(), $min, $max);
  89. }
  90. /**
  91. * Get member score from Sorted Set
  92. *
  93. * @param mixed $value
  94. * @return numeric
  95. */
  96. public function getScore($value)
  97. {
  98. return $this->_getRediskaOn()->getScoreFromSortedSet($this->getName(), $value);
  99. }
  100. /**
  101. * Increment score of element
  102. *
  103. * @param $value
  104. * @return integer
  105. */
  106. public function incrementScore($value, $score)
  107. {
  108. return $this->_getRediskaOn()->incrementScoreInSortedSet($this->getName(), $value, $score);
  109. }
  110. /**
  111. * Get Sorted set by Rank
  112. *
  113. * @param boolean $withScores Get with scores
  114. * @param integer $start Start index
  115. * @param integer $end End index
  116. * @param boolean $revert Revert elements (not used in sorting)
  117. * @param boolean $responseIterator[optional] If true - command return iterator which read from socket buffer.
  118. * Important: new connection will be created
  119. *
  120. * @return array
  121. */
  122. public function getByRank($withScores = false, $start = 0, $end = -1, $revert = false, $responseIterator = false)
  123. {
  124. return $this->_getRediskaOn()->getSortedSet($this->getName(), $withScores, $start, $end, $revert, $responseIterator);
  125. }
  126. /**
  127. * Remove all elements in the sorted set at key with rank between start and end
  128. *
  129. * @param numeric $start Start position
  130. * @param numeric $end End position
  131. * @return integer
  132. */
  133. public function removeByRank($start, $end)
  134. {
  135. return $this->_getRediskaOn()->deleteFromSortedSetByRank($this->getName(), $start, $end);
  136. }
  137. /**
  138. * Get rank of member
  139. *
  140. * @param integer $value Member value
  141. * @param boolean $revert Revert elements (not used in sorting)
  142. * @return integer
  143. */
  144. public function getRank($value, $revert = false)
  145. {
  146. return $this->_getRediskaOn()->getRankFromSortedSet($this->getName(), $value, $revert);
  147. }
  148. /**
  149. * Store to key union between the sorted sets
  150. *
  151. * @param string|Rediska_Key_SortedSet|array $setOrSets Sorted set key name or object, or array of its
  152. * @param string $storeKeyName Result sorted set key name
  153. * @param string $aggregation Aggregation method: SUM (for default), MIN, MAX.
  154. * @return integer
  155. */
  156. public function union($setOrSets, $storeKeyName, $aggregation = 'sum')
  157. {
  158. $sets = $this->_prepareSetsForComapre($setOrSets);
  159. return $this->_getRediskaOn()->unionSortedSets($sets, $storeKeyName, $aggregation);
  160. }
  161. /**
  162. * Store to key intersection between sorted sets
  163. *
  164. * @param string|Rediska_Key_SortedSet|array $setOrSets Sorted set key name or object, or array of its
  165. * @param string $storeKeyName Result sorted set key name
  166. * @param string $aggregation Aggregation method: SUM (for default), MIN, MAX.
  167. * @return integer
  168. */
  169. public function intersect($setOrSets, $storeKeyName, $aggregation = 'sum')
  170. {
  171. $sets = $this->_prepareSetsForComapre($setOrSets);
  172. return $this->_getRediskaOn()->intersectSortedSets($sets, $storeKeyName, $aggregation);
  173. }
  174. /**
  175. * Get sorted the elements
  176. *
  177. * @param string|array $value Options or SORT query string (http://code.google.com/p/redis/wiki/SortCommand).
  178. * Important notes for SORT query string:
  179. * 1. If you set Rediska namespace option don't forget add it to key names.
  180. * 2. If you use more then one connection to Redis servers, it will choose by key name,
  181. * and key by you pattern's may not present on it.
  182. * @return array
  183. */
  184. public function sort($options = array())
  185. {
  186. return $this->_getRediskaOn()->sort($this->getName(), $options);
  187. }
  188. /**
  189. * Get Sorted set values
  190. *
  191. * @param integer $withScores Return values with scores
  192. * @param integer $start Start index
  193. * @param integer $end End index
  194. * @param boolean $revert Revert elements (not used in sorting)
  195. * @param boolean $responseIterator[optional] If true - command return iterator which read from socket buffer.
  196. * Important: new connection will be created
  197. *
  198. * @return array
  199. */
  200. public function toArray($withScores = false, $start = 0, $end = -1, $revert = false, $responseIterator = false)
  201. {
  202. return $this->getByRank($withScores, $start, $end, $revert, $responseIterator);
  203. }
  204. /**
  205. * Add array to Sorted set
  206. *
  207. * @param array $array
  208. */
  209. public function fromArray(array $array)
  210. {
  211. $pipeline = $this->_getRediskaOn()->pipeline();
  212. foreach($array as $score => $value) {
  213. $pipeline->addToSortedSet($this->getName(), $value, $score);
  214. }
  215. if (!is_null($this->getExpire())) {
  216. $pipeline->expire($this->getName(), $this->getExpire(), $this->isExpireTimestamp());
  217. }
  218. $pipeline->execute();
  219. return true;
  220. }
  221. /**
  222. * Implement intrefaces
  223. */
  224. public function count()
  225. {
  226. return $this->getLength();
  227. }
  228. public function getIterator()
  229. {
  230. return new ArrayObject($this->toArray());
  231. }
  232. public function offsetSet($score, $value)
  233. {
  234. if (is_null($score)) {
  235. throw new Rediska_Key_Exception('Score must be present');
  236. }
  237. $this->add($value, $score);
  238. return $value;
  239. }
  240. public function offsetExists($score)
  241. {
  242. return (boolean)$this->offsetGet($score);
  243. }
  244. public function offsetUnset($score)
  245. {
  246. $value = $this->offsetGet($score);
  247. if (!is_null($value)) {
  248. $this->remove($value);
  249. return true;
  250. } else {
  251. return false;
  252. }
  253. }
  254. public function offsetGet($score)
  255. {
  256. $values = $this->getByScore($score, $score);
  257. if (!empty($values)) {
  258. return $values[0];
  259. }
  260. }
  261. protected function _prepareSetsForComapre($setOrSets)
  262. {
  263. if (!is_array($setOrSets)) {
  264. $sets = array($setOrSets);
  265. } else {
  266. $sets = $setOrSets;
  267. }
  268. // With weights?
  269. $withWeights = false;
  270. foreach($sets as $nameOrIndex => $weightOrName) {
  271. if (is_string($nameOrIndex)) {
  272. $withWeights = true;
  273. break;
  274. }
  275. }
  276. if ($withWeights) {
  277. if (!array_key_exists($this->getName(), $sets)) {
  278. $sets[$this->getName()] = 1;
  279. }
  280. } else {
  281. foreach($sets as &$set) {
  282. if ($set instanceof Rediska_Key_SortedSet) {
  283. $set = $set->getName();
  284. }
  285. }
  286. if (!in_array($this->getName(), $sets)) {
  287. $sets[] = $this->getName();
  288. }
  289. }
  290. return $sets;
  291. }
  292. }