PageRenderTime 69ms CodeModel.GetById 33ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Rediska/Key/Set.php

https://bitbucket.org/ilyabazhenov/speakplace
PHP | 252 lines | 120 code | 37 blank | 95 comment | 11 complexity | da86691e9e15860cae11c9aa44ac48af MD5 | raw file
  1. <?php
  2. // Require Rediska
  3. require_once dirname(__FILE__) . '/../../Rediska.php';
  4. /**
  5. * Rediska 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_Set extends Rediska_Key_Abstract implements IteratorAggregate, ArrayAccess, Countable
  15. {
  16. /**
  17. * Add the specified member to the Set
  18. *
  19. * @param mixed $value Value
  20. * @return boolean
  21. */
  22. public function add($value)
  23. {
  24. $result = $this->_getRediskaOn()->addToSet($this->getName(), $value);
  25. if (!is_null($this->getExpire()) && $result) {
  26. $this->expire($this->getExpire(), $this->isExpireTimestamp());
  27. }
  28. return $result;
  29. }
  30. /**
  31. * Remove the specified member from the Set
  32. *
  33. * @param mixed $value Value
  34. * @return boolean
  35. */
  36. public function remove($value)
  37. {
  38. $result = $this->_getRediskaOn()->deleteFromSet($this->getName(), $value);
  39. if (!is_null($this->getExpire()) && $result) {
  40. $this->expire($this->getExpire(), $this->isExpireTimestamp());
  41. }
  42. return $result;
  43. }
  44. /**
  45. * Move the specified member from one Set to another atomically
  46. *
  47. * @param string|Rediska_Key_Set $set Set key name or object
  48. * @param mixed $value Value
  49. * @return boolean
  50. */
  51. public function move($set, $value)
  52. {
  53. if ($set instanceof Rediska_Key_Set) {
  54. $set = $set->getName();
  55. }
  56. return $this->_getRediskaOn()->moveToSet($this->getName(), $set, $value);
  57. }
  58. /**
  59. * Get Set length
  60. *
  61. * @return integer
  62. */
  63. public function getLength()
  64. {
  65. return $this->_getRediskaOn()->getSetLength($this->getName());
  66. }
  67. /**
  68. * Test if the specified value is a member of the Set
  69. *
  70. * @param mixed $value Value
  71. * @return boolean
  72. */
  73. public function exists($value)
  74. {
  75. return $this->_getRediskaOn()->existsInSet($this->getName(), $value);
  76. }
  77. /**
  78. * Return the intersection between the Sets
  79. *
  80. * @param string|Rediska_Key_Set|array $setOrSets Set key name or object, or array of its
  81. * @param string|null $storeKeyName Store intersection to set with key name
  82. * @return array|boolean
  83. */
  84. public function intersect($setOrSets, $storeKeyName = null)
  85. {
  86. $sets = $this->_prepareSetsForCompare($setOrSets);
  87. return $this->_getRediskaOn()->intersectSets($sets, $storeKeyName);
  88. }
  89. /**
  90. * Return the union between the Sets
  91. *
  92. * @param string|Rediska_Key_Set|array $setOrSets Set key name or object, or array of its
  93. * @param string|null $storeKeyName Store union to set with key name
  94. * @return array|boolean
  95. */
  96. public function union($setOrSets, $storeKeyName = null)
  97. {
  98. $sets = $this->_prepareSetsForCompare($setOrSets);
  99. return $this->_getRediskaOn()->unionSets($sets, $storeKeyName);
  100. }
  101. /**
  102. * Return the difference between the Sets
  103. *
  104. * @param string|array $setOrSets Set key name or object, or array of its
  105. * @param string|null $storeKeyName Store union to set with key name
  106. * @return array|boolean
  107. */
  108. public function diff($setOrSets, $storeKeyName = null)
  109. {
  110. $sets = $this->_prepareSetsForCompare($setOrSets);
  111. return $this->_getRediskaOn()->diffSets($sets, $storeKeyName);
  112. }
  113. /**
  114. * Get Set values
  115. *
  116. * @param boolean $responseIterator[optional] If true - command return iterator which read from socket buffer.
  117. * Important: new connection will be created
  118. * @return array
  119. */
  120. public function getValues($responseIterator = false)
  121. {
  122. return $this->_getRediskaOn()->getSet($this->getName(), $responseIterator);
  123. }
  124. /**
  125. * Get sorted the elements
  126. *
  127. * @param string|array $value Options or SORT query string (http://code.google.com/p/redis/wiki/SortCommand).
  128. * Important notes for SORT query string:
  129. * 1. If you set Rediska namespace option don't forget add it to key names.
  130. * 2. If you use more then one connection to Redis servers, it will choose by key name,
  131. * and key by you pattern's may not present on it.
  132. * @return array
  133. */
  134. public function sort($options = array())
  135. {
  136. return $this->_getRediskaOn()->sort($this->getName(), $options);
  137. }
  138. /**
  139. * Get Set values
  140. *
  141. * @param boolean $responseIterator[optional] If true - command return iterator which read from socket buffer.
  142. * Important: new connection will be created
  143. * @return array
  144. */
  145. public function toArray($responseIterator = false)
  146. {
  147. return $this->getValues($responseIterator);
  148. }
  149. /**
  150. * Add array to Set
  151. *
  152. * @param array $array
  153. */
  154. public function fromArray(array $array)
  155. {
  156. // TODO: Use pipelines
  157. $pipeline = $this->_getRediskaOn()->pipeline();
  158. foreach($array as $item) {
  159. $pipeline->addToSet($this->getName(), $item);
  160. }
  161. if (!is_null($this->getExpire())) {
  162. $pipeline->expire($this->getName(), $this->getExpire(), $this->isExpireTimestamp());
  163. }
  164. $pipeline->execute();
  165. return true;
  166. }
  167. /**
  168. * Implement intrefaces
  169. */
  170. public function count()
  171. {
  172. return $this->getLength();
  173. }
  174. public function getIterator()
  175. {
  176. return new ArrayObject($this->toArray());
  177. }
  178. public function offsetSet($offset, $value)
  179. {
  180. if (!is_null($offset)) {
  181. throw new Rediska_Key_Exception('Offset is not allowed in sets');
  182. }
  183. $this->add($value);
  184. return $value;
  185. }
  186. public function offsetExists($value)
  187. {
  188. throw new Rediska_Key_Exception('Offset is not allowed in sets');
  189. }
  190. public function offsetUnset($value)
  191. {
  192. throw new Rediska_Key_Exception('Offset is not allowed in sets');
  193. }
  194. public function offsetGet($value)
  195. {
  196. throw new Rediska_Key_Exception('Offset is not allowed in sets');
  197. }
  198. protected function _prepareSetsForCompare($setOrSets)
  199. {
  200. if (!is_array($setOrSets)) {
  201. $sets = array($setOrSets);
  202. } else {
  203. $sets = $setOrSets;
  204. }
  205. foreach($sets as &$set) {
  206. if ($set instanceof Rediska_Key_Set) {
  207. $set = $set->getName();
  208. }
  209. }
  210. if (!in_array($this->getName(), $sets)) {
  211. array_unshift($sets, $this->getName());
  212. }
  213. return $sets;
  214. }
  215. }