Chris@0
|
1 <?php
|
Chris@0
|
2 /*
|
Chris@0
|
3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
Chris@0
|
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
Chris@0
|
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
Chris@0
|
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
Chris@0
|
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
Chris@0
|
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
Chris@0
|
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
Chris@0
|
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
Chris@0
|
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
Chris@0
|
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
Chris@0
|
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
Chris@0
|
14 *
|
Chris@0
|
15 * This software consists of voluntary contributions made by many individuals
|
Chris@0
|
16 * and is licensed under the MIT license. For more information, see
|
Chris@0
|
17 * <http://www.doctrine-project.org>.
|
Chris@0
|
18 */
|
Chris@0
|
19
|
Chris@0
|
20 namespace Doctrine\Common\Cache;
|
Chris@0
|
21
|
Chris@0
|
22 use Riak\Bucket;
|
Chris@0
|
23 use Riak\Connection;
|
Chris@0
|
24 use Riak\Input;
|
Chris@0
|
25 use Riak\Exception;
|
Chris@0
|
26 use Riak\Object;
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * Riak cache provider.
|
Chris@0
|
30 *
|
Chris@0
|
31 * @link www.doctrine-project.org
|
Chris@0
|
32 * @since 1.1
|
Chris@0
|
33 * @author Guilherme Blanco <guilhermeblanco@hotmail.com>
|
Chris@0
|
34 */
|
Chris@0
|
35 class RiakCache extends CacheProvider
|
Chris@0
|
36 {
|
Chris@0
|
37 const EXPIRES_HEADER = 'X-Riak-Meta-Expires';
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * @var \Riak\Bucket
|
Chris@0
|
41 */
|
Chris@0
|
42 private $bucket;
|
Chris@0
|
43
|
Chris@0
|
44 /**
|
Chris@0
|
45 * Sets the riak bucket instance to use.
|
Chris@0
|
46 *
|
Chris@0
|
47 * @param \Riak\Bucket $bucket
|
Chris@0
|
48 */
|
Chris@0
|
49 public function __construct(Bucket $bucket)
|
Chris@0
|
50 {
|
Chris@0
|
51 $this->bucket = $bucket;
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * {@inheritdoc}
|
Chris@0
|
56 */
|
Chris@0
|
57 protected function doFetch($id)
|
Chris@0
|
58 {
|
Chris@0
|
59 try {
|
Chris@0
|
60 $response = $this->bucket->get($id);
|
Chris@0
|
61
|
Chris@0
|
62 // No objects found
|
Chris@0
|
63 if ( ! $response->hasObject()) {
|
Chris@0
|
64 return false;
|
Chris@0
|
65 }
|
Chris@0
|
66
|
Chris@0
|
67 // Check for attempted siblings
|
Chris@0
|
68 $object = ($response->hasSiblings())
|
Chris@0
|
69 ? $this->resolveConflict($id, $response->getVClock(), $response->getObjectList())
|
Chris@0
|
70 : $response->getFirstObject();
|
Chris@0
|
71
|
Chris@0
|
72 // Check for expired object
|
Chris@0
|
73 if ($this->isExpired($object)) {
|
Chris@0
|
74 $this->bucket->delete($object);
|
Chris@0
|
75
|
Chris@0
|
76 return false;
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 return unserialize($object->getContent());
|
Chris@0
|
80 } catch (Exception\RiakException $e) {
|
Chris@0
|
81 // Covers:
|
Chris@0
|
82 // - Riak\ConnectionException
|
Chris@0
|
83 // - Riak\CommunicationException
|
Chris@0
|
84 // - Riak\UnexpectedResponseException
|
Chris@0
|
85 // - Riak\NotFoundException
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 return false;
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 /**
|
Chris@0
|
92 * {@inheritdoc}
|
Chris@0
|
93 */
|
Chris@0
|
94 protected function doContains($id)
|
Chris@0
|
95 {
|
Chris@0
|
96 try {
|
Chris@0
|
97 // We only need the HEAD, not the entire object
|
Chris@0
|
98 $input = new Input\GetInput();
|
Chris@0
|
99
|
Chris@0
|
100 $input->setReturnHead(true);
|
Chris@0
|
101
|
Chris@0
|
102 $response = $this->bucket->get($id, $input);
|
Chris@0
|
103
|
Chris@0
|
104 // No objects found
|
Chris@0
|
105 if ( ! $response->hasObject()) {
|
Chris@0
|
106 return false;
|
Chris@0
|
107 }
|
Chris@0
|
108
|
Chris@0
|
109 $object = $response->getFirstObject();
|
Chris@0
|
110
|
Chris@0
|
111 // Check for expired object
|
Chris@0
|
112 if ($this->isExpired($object)) {
|
Chris@0
|
113 $this->bucket->delete($object);
|
Chris@0
|
114
|
Chris@0
|
115 return false;
|
Chris@0
|
116 }
|
Chris@0
|
117
|
Chris@0
|
118 return true;
|
Chris@0
|
119 } catch (Exception\RiakException $e) {
|
Chris@0
|
120 // Do nothing
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 return false;
|
Chris@0
|
124 }
|
Chris@0
|
125
|
Chris@0
|
126 /**
|
Chris@0
|
127 * {@inheritdoc}
|
Chris@0
|
128 */
|
Chris@0
|
129 protected function doSave($id, $data, $lifeTime = 0)
|
Chris@0
|
130 {
|
Chris@0
|
131 try {
|
Chris@0
|
132 $object = new Object($id);
|
Chris@0
|
133
|
Chris@0
|
134 $object->setContent(serialize($data));
|
Chris@0
|
135
|
Chris@0
|
136 if ($lifeTime > 0) {
|
Chris@0
|
137 $object->addMetadata(self::EXPIRES_HEADER, (string) (time() + $lifeTime));
|
Chris@0
|
138 }
|
Chris@0
|
139
|
Chris@0
|
140 $this->bucket->put($object);
|
Chris@0
|
141
|
Chris@0
|
142 return true;
|
Chris@0
|
143 } catch (Exception\RiakException $e) {
|
Chris@0
|
144 // Do nothing
|
Chris@0
|
145 }
|
Chris@0
|
146
|
Chris@0
|
147 return false;
|
Chris@0
|
148 }
|
Chris@0
|
149
|
Chris@0
|
150 /**
|
Chris@0
|
151 * {@inheritdoc}
|
Chris@0
|
152 */
|
Chris@0
|
153 protected function doDelete($id)
|
Chris@0
|
154 {
|
Chris@0
|
155 try {
|
Chris@0
|
156 $this->bucket->delete($id);
|
Chris@0
|
157
|
Chris@0
|
158 return true;
|
Chris@0
|
159 } catch (Exception\BadArgumentsException $e) {
|
Chris@0
|
160 // Key did not exist on cluster already
|
Chris@0
|
161 } catch (Exception\RiakException $e) {
|
Chris@0
|
162 // Covers:
|
Chris@0
|
163 // - Riak\Exception\ConnectionException
|
Chris@0
|
164 // - Riak\Exception\CommunicationException
|
Chris@0
|
165 // - Riak\Exception\UnexpectedResponseException
|
Chris@0
|
166 }
|
Chris@0
|
167
|
Chris@0
|
168 return false;
|
Chris@0
|
169 }
|
Chris@0
|
170
|
Chris@0
|
171 /**
|
Chris@0
|
172 * {@inheritdoc}
|
Chris@0
|
173 */
|
Chris@0
|
174 protected function doFlush()
|
Chris@0
|
175 {
|
Chris@0
|
176 try {
|
Chris@0
|
177 $keyList = $this->bucket->getKeyList();
|
Chris@0
|
178
|
Chris@0
|
179 foreach ($keyList as $key) {
|
Chris@0
|
180 $this->bucket->delete($key);
|
Chris@0
|
181 }
|
Chris@0
|
182
|
Chris@0
|
183 return true;
|
Chris@0
|
184 } catch (Exception\RiakException $e) {
|
Chris@0
|
185 // Do nothing
|
Chris@0
|
186 }
|
Chris@0
|
187
|
Chris@0
|
188 return false;
|
Chris@0
|
189 }
|
Chris@0
|
190
|
Chris@0
|
191 /**
|
Chris@0
|
192 * {@inheritdoc}
|
Chris@0
|
193 */
|
Chris@0
|
194 protected function doGetStats()
|
Chris@0
|
195 {
|
Chris@0
|
196 // Only exposed through HTTP stats API, not Protocol Buffers API
|
Chris@0
|
197 return null;
|
Chris@0
|
198 }
|
Chris@0
|
199
|
Chris@0
|
200 /**
|
Chris@0
|
201 * Check if a given Riak Object have expired.
|
Chris@0
|
202 *
|
Chris@0
|
203 * @param \Riak\Object $object
|
Chris@0
|
204 *
|
Chris@0
|
205 * @return bool
|
Chris@0
|
206 */
|
Chris@0
|
207 private function isExpired(Object $object)
|
Chris@0
|
208 {
|
Chris@0
|
209 $metadataMap = $object->getMetadataMap();
|
Chris@0
|
210
|
Chris@0
|
211 return isset($metadataMap[self::EXPIRES_HEADER])
|
Chris@0
|
212 && $metadataMap[self::EXPIRES_HEADER] < time();
|
Chris@0
|
213 }
|
Chris@0
|
214
|
Chris@0
|
215 /**
|
Chris@0
|
216 * On-read conflict resolution. Applied approach here is last write wins.
|
Chris@0
|
217 * Specific needs may override this method to apply alternate conflict resolutions.
|
Chris@0
|
218 *
|
Chris@0
|
219 * {@internal Riak does not attempt to resolve a write conflict, and store
|
Chris@0
|
220 * it as sibling of conflicted one. By following this approach, it is up to
|
Chris@0
|
221 * the next read to resolve the conflict. When this happens, your fetched
|
Chris@0
|
222 * object will have a list of siblings (read as a list of objects).
|
Chris@0
|
223 * In our specific case, we do not care about the intermediate ones since
|
Chris@0
|
224 * they are all the same read from storage, and we do apply a last sibling
|
Chris@0
|
225 * (last write) wins logic.
|
Chris@0
|
226 * If by any means our resolution generates another conflict, it'll up to
|
Chris@0
|
227 * next read to properly solve it.}
|
Chris@0
|
228 *
|
Chris@0
|
229 * @param string $id
|
Chris@0
|
230 * @param string $vClock
|
Chris@0
|
231 * @param array $objectList
|
Chris@0
|
232 *
|
Chris@0
|
233 * @return \Riak\Object
|
Chris@0
|
234 */
|
Chris@0
|
235 protected function resolveConflict($id, $vClock, array $objectList)
|
Chris@0
|
236 {
|
Chris@0
|
237 // Our approach here is last-write wins
|
Chris@0
|
238 $winner = $objectList[count($objectList)];
|
Chris@0
|
239
|
Chris@0
|
240 $putInput = new Input\PutInput();
|
Chris@0
|
241 $putInput->setVClock($vClock);
|
Chris@0
|
242
|
Chris@0
|
243 $mergedObject = new Object($id);
|
Chris@0
|
244 $mergedObject->setContent($winner->getContent());
|
Chris@0
|
245
|
Chris@0
|
246 $this->bucket->put($mergedObject, $putInput);
|
Chris@0
|
247
|
Chris@0
|
248 return $mergedObject;
|
Chris@0
|
249 }
|
Chris@0
|
250 }
|