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