Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
83.61% |
51 / 61 |
|
22.22% |
2 / 9 |
CRAP | |
0.00% |
0 / 1 |
common_persistence_AdvKeyValuePersistence | |
83.61% |
51 / 61 |
|
22.22% |
2 / 9 |
51.15 | |
0.00% |
0 / 1 |
hmSet | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
5.58 | |||
hExists | |
66.67% |
2 / 3 |
|
0.00% |
0 / 1 |
3.33 | |||
hSet | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
4.03 | |||
hGet | |
87.50% |
7 / 8 |
|
0.00% |
0 / 1 |
6.07 | |||
hGetAll | |
75.00% |
6 / 8 |
|
0.00% |
0 / 1 |
5.39 | |||
hDel | |
77.78% |
7 / 9 |
|
0.00% |
0 / 1 |
6.40 | |||
keys | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
4 | |||
del | |
90.91% |
10 / 11 |
|
0.00% |
0 / 1 |
9.06 | |||
getMappedKey | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | /** |
4 | * This program is free software; you can redistribute it and/or |
5 | * modify it under the terms of the GNU General Public License |
6 | * as published by the Free Software Foundation; under version 2 |
7 | * of the License (non-upgradable). |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
17 | * |
18 | * Copyright (c) 2013-2017 (original work) Open Assessment Technologies SA (under the project TAO-PRODUCT); |
19 | * |
20 | * @author Patrick Plichart <patrick@taotesting.com> |
21 | * @author Camille Moyon <camille@taotesting.com> |
22 | * @license GPLv2 |
23 | * @package generis |
24 | |
25 | * |
26 | */ |
27 | class common_persistence_AdvKeyValuePersistence extends common_persistence_KeyValuePersistence |
28 | { |
29 | /** |
30 | * Set all $fields of a $key |
31 | * If one of field values is large, a map is created into storage, and reference map is the new value of the field/ |
32 | * |
33 | * @param $key |
34 | * @param $fields |
35 | * @return bool |
36 | */ |
37 | public function hmSet($key, $fields) |
38 | { |
39 | if ($this->hasMaxSize()) { |
40 | foreach ($fields as $field => $value) { |
41 | try { |
42 | if ($this->isLarge($value)) { |
43 | $fields[$field] = $this->setLargeValue($this->getMappedKey($key, $field), $value, 0, false); |
44 | } |
45 | } catch (common_Exception $e) { |
46 | common_Logger::w('Max size value is misconfigured: ' . $e->getMessage()); |
47 | } |
48 | } |
49 | } |
50 | |
51 | return $this->getDriver()->hmSet($key, $fields); |
52 | } |
53 | |
54 | /** |
55 | * Check if a $field exists for a given $key. |
56 | * Mapped $key will be ignored |
57 | * |
58 | * @param $key |
59 | * @param $field |
60 | * @return bool |
61 | */ |
62 | public function hExists($key, $field) |
63 | { |
64 | if ($this->isMappedKey($key) || $this->isMappedKey($field)) { |
65 | return false; |
66 | } |
67 | return (bool) $this->getDriver()->hExists($key, $field); |
68 | } |
69 | |
70 | /** |
71 | * Fill a $key $field with the given value |
72 | * Check if old value is a map to delete all references |
73 | * Check if value is not too large, otherwise create a map |
74 | * |
75 | * @param $key |
76 | * @param $field |
77 | * @param $value |
78 | * @return mixed |
79 | * @throws common_Exception If the the size is misconfigured |
80 | */ |
81 | public function hSet($key, $field, $value) |
82 | { |
83 | if (!$this->hasMaxSize()) { |
84 | return $this->getDriver()->hSet($key, $field, $value); |
85 | } |
86 | |
87 | if ($this->isLarge($value)) { |
88 | $value = $this->setLargeValue($this->getMappedKey($key, $field), $value, 0, false); |
89 | } |
90 | $oldValue = $this->getDriver()->hGet($key, $field); |
91 | if ($this->isSplit($oldValue)) { |
92 | $this->deleteMappedKey($field, $oldValue); |
93 | } |
94 | |
95 | return $this->getDriver()->hSet($key, $field, $value); |
96 | } |
97 | |
98 | /** |
99 | * Get the value of $field under $key |
100 | * Mapped key will be ignored |
101 | * Check if value is a map, in case of yes, join values |
102 | * |
103 | * @param $key |
104 | * @param $field |
105 | * @return bool|mixed|string |
106 | */ |
107 | public function hGet($key, $field) |
108 | { |
109 | if ($this->hasMaxSize()) { |
110 | if ($this->isMappedKey($key) || $this->isMappedKey($field)) { |
111 | return false; |
112 | } |
113 | } |
114 | $value = $this->getDriver()->hGet($key, $field); |
115 | if ($this->hasMaxSize()) { |
116 | if ($this->isSplit($value)) { |
117 | $value = $this->join($this->getMappedKey($key, $field), $value); |
118 | } |
119 | } |
120 | return $value; |
121 | } |
122 | |
123 | /** |
124 | * Get old $field of a $key |
125 | * If one of values is a map, join related values |
126 | * |
127 | * @param $key |
128 | * @return array |
129 | */ |
130 | public function hGetAll($key) |
131 | { |
132 | $fields = $this->getDriver()->hGetAll($key); |
133 | |
134 | if (empty($fields)) { |
135 | return $fields; |
136 | } |
137 | |
138 | if ($this->hasMaxSize()) { |
139 | foreach ($fields as $field => $value) { |
140 | if ($this->isSplit($value)) { |
141 | $fields[$field] = $this->join($this->getMappedKey($key, $field), $value); |
142 | } |
143 | } |
144 | } |
145 | |
146 | return $fields; |
147 | } |
148 | |
149 | /** |
150 | * Deletes $field value. |
151 | * |
152 | * @param string $key |
153 | * @param string $field |
154 | * @return bool |
155 | */ |
156 | public function hDel($key, $field): bool |
157 | { |
158 | |
159 | if (!$this->hasMaxSize()) { |
160 | return $this->getDriver()->hDel($key, $field); |
161 | } |
162 | |
163 | if ($this->isMappedKey($key)) { |
164 | return false; |
165 | } |
166 | |
167 | $success = true; |
168 | $value = $this->getDriver()->hGet($key, $field); |
169 | if ($this->isSplit($value)) { |
170 | $success = $success && $this->deleteMappedKey($key, $value); |
171 | } |
172 | |
173 | return $success && $this->getDriver()->hDel($key, $field); |
174 | } |
175 | |
176 | /** |
177 | * Get a list of existing $keys |
178 | * Mapped will be ignored |
179 | * |
180 | * IMPORTANT: Never use this method for production purposes. |
181 | * Time complexity: O(N) with N being the number of keys in the database, |
182 | * under the assumption that the key names in the database and the given pattern have limited length. |
183 | * |
184 | * @param $pattern |
185 | * @return array |
186 | */ |
187 | public function keys($pattern) |
188 | { |
189 | $keys = $this->getDriver()->keys($pattern); |
190 | |
191 | if ($this->hasMaxSize()) { |
192 | foreach ($keys as $index => $key) { |
193 | if ($this->isMappedKey($key)) { |
194 | unset($keys[$index]); |
195 | } |
196 | } |
197 | } |
198 | return $keys; |
199 | } |
200 | |
201 | /** |
202 | * Delete a key. If key is split, all associated mapped key are deleted too |
203 | * |
204 | * @param $key |
205 | * @return bool |
206 | */ |
207 | public function del($key) |
208 | { |
209 | if ($this->isMappedKey($key)) { |
210 | return false; |
211 | } else { |
212 | $success = true; |
213 | if ($this->hasMaxSize()) { |
214 | $fields = $this->getDriver()->hGetAll($key); |
215 | if (!empty($fields)) { |
216 | foreach ($fields as $subKey => $value) { |
217 | if ($this->isSplit($value)) { |
218 | $success = $success && $this->deleteMappedKey($subKey, $value); |
219 | } |
220 | } |
221 | } |
222 | $success = $success && $this->deleteMappedKey($key); |
223 | } |
224 | |
225 | return $success && $this->getDriver()->del($key); |
226 | } |
227 | } |
228 | |
229 | /** |
230 | * Get a serial to reference mapped $key |
231 | * |
232 | * @param $key |
233 | * @param $field |
234 | * @return string |
235 | */ |
236 | protected function getMappedKey($key, $field) |
237 | { |
238 | return $key . '.' . $field; |
239 | } |
240 | } |