PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Rediska/Key/SortedSet.php

https://bitbucket.org/ilyabazhenov/speakplace
PHP | 336 lines | 154 code | 43 blank | 139 comment | 17 complexity | 716031a05719f2bd9dc7d65686bca0a8 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 0.5.7
  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. * @param boolean $revert Is revert result
  74. * @return array
  75. */
  76. public function getByScore($min, $max, $withScores = false, $limit = null, $offset = null, $revert = false)
  77. {
  78. return $this->_getRediskaOn()->getFromSortedSetByScore($this->getName(), $min, $max, $withScores, $limit, $offset, $revert);
  79. }
  80. /**
  81. * Remove members from sorted set by score
  82. *
  83. * @param $min Min score
  84. * @param $max Max score
  85. * @return integer
  86. */
  87. public function removeByScore($min, $max)
  88. {
  89. return $this->_getRediskaOn()->DeleteFromSortedSetByScore($this->getName(), $min, $max);
  90. }
  91. /**
  92. * Get member score from Sorted Set
  93. *
  94. * @param mixed $value
  95. * @return numeric
  96. */
  97. public function getScore($value)
  98. {
  99. return $this->_getRediskaOn()->getScoreFromSortedSet($this->getName(), $value);
  100. }
  101. /**
  102. * Increment score of element
  103. *
  104. * @param $value
  105. * @return integer
  106. */
  107. public function incrementScore($value, $score)
  108. {
  109. return $this->_getRediskaOn()->incrementScoreInSortedSet($this->getName(), $value, $score);
  110. }
  111. /**
  112. * Get Sorted set by Rank
  113. *
  114. * @param boolean $withScores Get with scores
  115. * @param integer $start Start index
  116. * @param integer $end End index
  117. * @param boolean $revert Revert elements (not used in sorting)
  118. * @param boolean $responseIterator[optional] If true - command return iterator which read from socket buffer.
  119. * Important: new connection will be created
  120. *
  121. * @return array
  122. */
  123. public function getByRank($withScores = false, $start = 0, $end = -1, $revert = false, $responseIterator = false)
  124. {
  125. return $this->_getRediskaOn()->getSortedSet($this->getName(), $withScores, $start, $end, $revert, $responseIterator);
  126. }
  127. /**
  128. * Remove all elements in the sorted set at key with rank between start and end
  129. *
  130. * @param numeric $start Start position
  131. * @param numeric $end End position
  132. * @return integer
  133. */
  134. public function removeByRank($start, $end)
  135. {
  136. return $this->_getRediskaOn()->deleteFromSortedSetByRank($this->getName(), $start, $end);
  137. }
  138. /**
  139. * Get rank of member
  140. *
  141. * @param integer $value Member value
  142. * @param boolean $revert Revert elements (not used in sorting)
  143. * @return integer
  144. */
  145. public function getRank($value, $revert = false)
  146. {
  147. return $this->_getRediskaOn()->getRankFromSortedSet($this->getName(), $value, $revert);
  148. }
  149. /**
  150. * Store to key union between the sorted sets
  151. *
  152. * @param string|Rediska_Key_SortedSet|array $setOrSets Sorted set key name or object, or array of its
  153. * @param string $storeKeyName Result sorted set key name
  154. * @param string $aggregation Aggregation method: SUM (for default), MIN, MAX.
  155. * @return integer
  156. */
  157. public function union($setOrSets, $storeKeyName, $aggregation = 'sum')
  158. {
  159. $sets = $this->_prepareSetsForComapre($setOrSets);
  160. return $this->_getRediskaOn()->unionSortedSets($sets, $storeKeyName, $aggregation);
  161. }
  162. /**
  163. * Store to key intersection between sorted sets
  164. *
  165. * @param string|Rediska_Key_SortedSet|array $setOrSets Sorted set key name or object, or array of its
  166. * @param string $storeKeyName Result sorted set key name
  167. * @param string $aggregation Aggregation method: SUM (for default), MIN, MAX.
  168. * @return integer
  169. */
  170. public function intersect($setOrSets, $storeKeyName, $aggregation = 'sum')
  171. {
  172. $sets = $this->_prepareSetsForComapre($setOrSets);
  173. return $this->_getRediskaOn()->intersectSortedSets($sets, $storeKeyName, $aggregation);
  174. }
  175. /**
  176. * Get sorted the elements
  177. *
  178. * @param string|array $value Options or SORT query string (http://code.google.com/p/redis/wiki/SortCommand).
  179. * Important notes for SORT query string:
  180. * 1. If you set Rediska namespace option don't forget add it to key names.
  181. * 2. If you use more then one connection to Redis servers, it will choose by key name,
  182. * and key by you pattern's may not present on it.
  183. * @return array
  184. */
  185. public function sort($options = array())
  186. {
  187. return $this->_getRediskaOn()->sort($this->getName(), $options);
  188. }
  189. /**
  190. * Get Sorted set values
  191. *
  192. * @param integer $withScores Return values with scores
  193. * @param integer $start Start index
  194. * @param integer $end End index
  195. * @param boolean $revert Revert elements (not used in sorting)
  196. * @param boolean $responseIterator[optional] If true - command return iterator which read from socket buffer.
  197. * Important: new connection will be created
  198. *
  199. * @return array
  200. */
  201. public function toArray($withScores = false, $start = 0, $end = -1, $revert = false, $responseIterator = false)
  202. {
  203. return $this->getByRank($withScores, $start, $end, $revert, $responseIterator);
  204. }
  205. /**
  206. * Add array to Sorted set
  207. *
  208. * @param array $array
  209. */
  210. public function fromArray(array $array)
  211. {
  212. $pipeline = $this->_getRediskaOn()->pipeline();
  213. foreach($array as $score => $value) {
  214. $pipeline->addToSortedSet($this->getName(), $value, $score);
  215. }
  216. if (!is_null($this->getExpire())) {
  217. $pipeline->expire($this->getName(), $this->getExpire(), $this->isExpireTimestamp());
  218. }
  219. $pipeline->execute();
  220. return true;
  221. }
  222. /**
  223. * Implement intrefaces
  224. */
  225. public function count()
  226. {
  227. return $this->getLength();
  228. }
  229. public function getIterator()
  230. {
  231. return new ArrayObject($this->toArray());
  232. }
  233. public function offsetSet($score, $value)
  234. {
  235. if (is_null($score)) {
  236. throw new Rediska_Key_Exception('Score must be present');
  237. }
  238. $this->add($value, $score);
  239. return $value;
  240. }
  241. public function offsetExists($score)
  242. {
  243. return (boolean)$this->offsetGet($score);
  244. }
  245. public function offsetUnset($score)
  246. {
  247. $value = $this->offsetGet($score);
  248. if (!is_null($value)) {
  249. $this->remove($value);
  250. return true;
  251. } else {
  252. return false;
  253. }
  254. }
  255. public function offsetGet($score)
  256. {
  257. $values = $this->getByScore($score, $score);
  258. if (!empty($values)) {
  259. return $values[0];
  260. }
  261. }
  262. protected function _prepareSetsForComapre($setOrSets)
  263. {
  264. if (!is_array($setOrSets)) {
  265. $sets = array($setOrSets);
  266. } else {
  267. $sets = $setOrSets;
  268. }
  269. // With weights?
  270. $withWeights = false;
  271. foreach($sets as $nameOrIndex => $weightOrName) {
  272. if (is_string($nameOrIndex)) {
  273. $withWeights = true;
  274. break;
  275. }
  276. }
  277. if ($withWeights) {
  278. if (!array_key_exists($this->getName(), $sets)) {
  279. $sets[$this->getName()] = 1;
  280. }
  281. } else {
  282. foreach($sets as &$set) {
  283. if ($set instanceof Rediska_Key_SortedSet) {
  284. $set = $set->getName();
  285. }
  286. }
  287. if (!in_array($this->getName(), $sets)) {
  288. $sets[] = $this->getName();
  289. }
  290. }
  291. return $sets;
  292. }
  293. }