Skip to content
This repository was archived by the owner on Feb 5, 2024. It is now read-only.

Commit e32cd91

Browse files
Merge pull request #94 from /issues/91
RiakStorage upgrade
2 parents fe7dcc5 + 8f07e47 commit e32cd91

6 files changed

Lines changed: 176 additions & 190 deletions

File tree

.travis.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,23 @@ php:
1111
- 7.2
1212
- 7.3
1313

14+
addons:
15+
apt:
16+
sources:
17+
-
18+
key_url: 'https://packagecloud.io/gpg.key'
19+
sourceline: 'deb https://packagecloud.io/basho/riak/ubuntu/ trusty main'
20+
-
21+
key_url: 'https://packagecloud.io/gpg.key'
22+
sourceline: 'deb-src https://packagecloud.io/basho/riak/ubuntu/ trusty main'
23+
update: true
24+
25+
cache:
26+
apt: true
27+
1428
before_install:
29+
- sudo apt-get install -y --allow-unauthenticated riak
30+
- sudo service riak start
1531
- pecl install --force mongodb
1632
- if [[ ${TRAVIS_PHP_VERSION:0:1} != "7" ]]; then sh ./tests/travis.sh; fi
1733
- composer self-update

composer.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99
"doctrine/couchdb": "^1.0.0-beta4",
1010
"phpunit/phpunit": "^4.8|^5.0",
1111
"aws/aws-sdk-php": "^3.8",
12-
"riak/riak-client": "dev-master",
12+
"php-riak/riak-client": "^1.0@alpha",
1313
"mongodb/mongodb": "^1.4"
1414
},
1515
"suggest": {
1616
"aws/aws-sdk-php": "to use the DynamoDB storage",
1717
"doctrine/couchdb": "to use the CouchDB storage",
18-
"ext-couchbase": "to use the Couchbase storage",
19-
"riak/riak-client": "to use the Riak storage"
18+
"ext-couchbase": "to use the Couchbase storage"
2019
},
2120
"description": "Simple Key-Value Store Abstraction Layer that maps to PHP objects, allowing for many backends.",
2221
"license": "MIT",

docs/reference/configuration.rst

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,15 +242,17 @@ instance.
242242
Riak
243243
----
244244

245-
Riak support is provided through the library `riak/riak-client <https://github.com/nacmartin/riak-client>`_ :
245+
Riak support is provided through the library `php-riak/riak-client <https://github.com/php-riak/riak-client>`_ :
246246

247247
.. code-block:: php
248248
249249
<?php
250250
251251
use Doctrine\KeyValueStore\Storage\RiakStorage;
252-
use Riak\Client;
252+
use Riak\Client\RiakClientBuilder;
253253
254-
$conn = new Riak(/* connection parameters */);
254+
$conn = (new RiakClientBuilder())
255+
->withNodeUri(/* connection DNS */)
256+
->build();
255257
256258
$storage = new RiakStorage($conn);

lib/Doctrine/KeyValueStore/Storage/RiakStorage.php

Lines changed: 51 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,26 @@
2121
namespace Doctrine\KeyValueStore\Storage;
2222

2323
use Doctrine\KeyValueStore\NotFoundException;
24-
use Riak\Client;
24+
use Riak\Client\Command\Kv\DeleteValue;
25+
use Riak\Client\Command\Kv\FetchValue;
26+
use Riak\Client\Command\Kv\StoreValue;
27+
use Riak\Client\Core\Query\RiakLocation;
28+
use Riak\Client\Core\Query\RiakNamespace;
29+
use Riak\Client\Core\Query\RiakObject;
30+
use Riak\Client\RiakClient;
31+
use Riak\Client\RiakException;
2532

2633
/**
2734
* @author Markus Bachmann <markus.bachmann@bachi.biz>
2835
*/
2936
class RiakStorage implements Storage
3037
{
3138
/**
32-
* @var \Riak\Client
39+
* @var RiakClient
3340
*/
34-
protected $client;
41+
private $client;
3542

36-
/**
37-
* Constructor
38-
*
39-
* @param \Riak\Client $riak
40-
* @param string $bucketName
41-
*/
42-
public function __construct(Client $riak)
43+
public function __construct(RiakClient $riak)
4344
{
4445
$this->client = $riak;
4546
}
@@ -68,62 +69,79 @@ public function requiresCompositePrimaryKeys()
6869
return false;
6970
}
7071

72+
private function store($storageName, $key, array $data)
73+
{
74+
$location = $this->getRiakLocation($storageName, $key);
75+
76+
$riakObject = new RiakObject();
77+
$riakObject->setContentType('application/json');
78+
$riakObject->setValue(json_encode($data));
79+
80+
$store = StoreValue::builder($location, $riakObject)->build();
81+
82+
$this->client->execute($store);
83+
}
84+
7185
/**
7286
* {@inheritDoc}
7387
*/
7488
public function insert($storageName, $key, array $data)
7589
{
76-
$bucket = $this->client->bucket($storageName);
77-
$object = $bucket->newObject($key, $data);
78-
$object->store();
90+
$this->store($storageName, $key, $data);
7991
}
8092

8193
/**
8294
* {@inheritDoc}
8395
*/
8496
public function update($storageName, $key, array $data)
8597
{
86-
$bucket = $this->client->bucket($storageName);
87-
/** @var $object \Riak\Object */
88-
$object = $bucket->get($key);
89-
90-
$object->setData($data);
91-
$object->store();
98+
$this->store($storageName, $key, $data);
9299
}
93100

94101
/**
95102
* {@inheritDoc}
96103
*/
97104
public function delete($storageName, $key)
98105
{
99-
$bucket = $this->client->bucket($storageName);
106+
$location = $this->getRiakLocation($storageName, $key);
100107

101-
/** @var $object \Riak\Object */
102-
$object = $bucket->get($key);
108+
$delete = DeleteValue::builder($location)->build();
103109

104-
if (! $object->exists()) {
105-
// object does not exist, do nothing
106-
return;
110+
try {
111+
$this->client->execute($delete);
112+
} catch (RiakException $exception) {
113+
// deletion can fail silent
107114
}
108-
109-
$object->delete();
110115
}
111116

112117
/**
113118
* {@inheritDoc}
114119
*/
115120
public function find($storageName, $key)
116121
{
117-
$bucket = $this->client->bucket($storageName);
122+
$location = $this->getRiakLocation($storageName, $key);
118123

119-
/** @var $object \Riak\Object */
120-
$object = $bucket->get($key);
124+
// fetch object
125+
$fetch = FetchValue::builder($location)->build();
121126

122-
if (! $object->exists()) {
123-
throw new NotFoundException;
127+
try {
128+
$result = $this->client->execute($fetch);
129+
} catch (RiakException $exception) {
130+
throw new NotFoundException();
124131
}
125132

126-
return $object->getData();
133+
$json = (string) $result
134+
->getValue()
135+
->getValue();
136+
137+
return json_decode($json, true);
138+
}
139+
140+
private function getRiakLocation($storageName, $key)
141+
{
142+
$namespace = new RiakNamespace('default', $storageName);
143+
144+
return new RiakLocation($namespace, $key);
127145
}
128146

129147
/**

phpunit.xml.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@
1616
<var name="DOCTRINE_KEYVALUE_AZURE_AUTHSCHEMA" value="sharedlite" />
1717
<var name="DOCTRINE_KEYVALUE_AZURE_NAME" value="" />
1818
<var name="DOCTRINE_KEYVALUE_AZURE_KEY" value="" />
19+
<env name="RIAK_DNS" value="" />
1920
</php>
2021
</phpunit>

0 commit comments

Comments
 (0)