Skip to content

Commit 20f52cf

Browse files
Merge pull request #111 from basho/2.1.x
Preflist support. Thanks @lukebakken!
2 parents f71fcba + ddd444d commit 20f52cf

File tree

4 files changed

+170
-3
lines changed

4 files changed

+170
-3
lines changed

src/Riak/Api/Http.php

+6-3
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ protected function buildPath()
132132
case 'Basho\Riak\Command\DataType\Set\Store':
133133
case 'Basho\Riak\Command\DataType\Map\Fetch':
134134
case 'Basho\Riak\Command\DataType\Map\Store':
135-
$this->path = sprintf('/types/%s/buckets/%s/datatypes/%s', $bucket->getType(), $bucket->getName(),
135+
$this->path = sprintf('/types/%s/buckets/%s/datatypes/%s', $bucket->getType(), $bucket->getName(),
136136
$key);
137137
break;
138138
case 'Basho\Riak\Command\Search\Index\Fetch':
@@ -159,6 +159,9 @@ protected function buildPath()
159159
case 'Basho\Riak\Command\Stats':
160160
$this->path = '/stats';
161161
break;
162+
case 'Basho\Riak\Command\Object\FetchPreflist':
163+
$this->path = sprintf('/types/%s/buckets/%s/keys/%s/preflist', $bucket->getType(), $bucket->getName(), $key);
164+
break;
162165
default:
163166
$this->path = '';
164167
}
@@ -294,7 +297,7 @@ protected function prepareRequestData()
294297
protected function prepareRequestUrl()
295298
{
296299
$protocol = $this->node->useTls() ? 'https' : 'http';
297-
$url = sprintf('%s://%s%s?%s', $protocol, $this->node->getUri(), $this->path, $this->query);
300+
$url = sprintf('%s://%s%s%s', $protocol, $this->node->getUri(), $this->path, $this->query);
298301

299302
// set the built request URL on the connection
300303
$this->options[CURLOPT_URL] = $url;
@@ -311,7 +314,7 @@ protected function prepareRequestParameters()
311314
{
312315
if ($this->command->hasParameters()) {
313316
// build query using RFC 3986 (spaces become %20 instead of '+')
314-
$this->query = http_build_query($this->command->getParameters(), '', '&', PHP_QUERY_RFC3986);
317+
$this->query = '?' . http_build_query($this->command->getParameters(), '', '&', PHP_QUERY_RFC3986);
315318
}
316319

317320
return $this;
+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
Copyright 2014 Basho Technologies, Inc.
5+
6+
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
7+
distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
specific language governing permissions and limitations under the License.
16+
*/
17+
18+
namespace Basho\Riak\Command\Builder;
19+
20+
use Basho\Riak\Command;
21+
22+
/**
23+
* Used to fetch map objects from Riak
24+
*
25+
* <code>
26+
* $command = (new Command\Builder\FetchPreflist($riak))
27+
* ->buildLocation($order_id, 'online_orders', 'sales')
28+
* ->build();
29+
*
30+
* $response = $command->execute($command);
31+
*
32+
* $map = $response->getMap();
33+
* </code>
34+
*
35+
* @author Christopher Mancini <cmancini at basho d0t com>
36+
*/
37+
class FetchPreflist extends Command\Builder implements Command\BuilderInterface
38+
{
39+
use LocationTrait;
40+
41+
/**
42+
* {@inheritdoc}
43+
*
44+
* @return Command\Object\FetchPreflist;
45+
*/
46+
public function build()
47+
{
48+
$this->validate();
49+
50+
return new Command\Object\FetchPreflist($this);
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*/
56+
public function validate()
57+
{
58+
$this->required('Location');
59+
}
60+
}
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
/*
4+
Copyright 2014 Basho Technologies, Inc.
5+
6+
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
7+
distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
specific language governing permissions and limitations under the License.
16+
*/
17+
18+
namespace Basho\Riak\Command\Object;
19+
20+
use Basho\Riak\Command;
21+
use Basho\Riak\CommandInterface;
22+
23+
/**
24+
* Fetches the Preflist for a Riak Kv Object
25+
*
26+
* @author Christopher Mancini <cmancini at basho d0t com>
27+
*/
28+
class FetchPreflist extends Command\Object implements CommandInterface
29+
{
30+
public function __construct(Command\Builder\FetchPreflist $builder)
31+
{
32+
parent::__construct($builder);
33+
34+
$this->bucket = $builder->getBucket();
35+
$this->location = $builder->getLocation();
36+
}
37+
38+
public function setResponse($statusCode, $responseHeaders = [], $responseBody = '')
39+
{
40+
$this->response = new Response($statusCode, $responseHeaders, $responseBody);
41+
42+
return $this;
43+
}
44+
}

tests/functional/PreflistTest.php

+60
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
Copyright 2015 Basho Technologies, Inc.
5+
6+
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file
7+
distributed with this work for additional information regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
9+
with the License. You may obtain a copy of the License at
10+
11+
http://www.apache.org/licenses/LICENSE-2.0
12+
13+
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an
14+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
specific language governing permissions and limitations under the License.
16+
*/
17+
18+
namespace Basho\Tests;
19+
20+
use Basho\Riak\Command;
21+
22+
/**
23+
* Class PreflistTest
24+
*
25+
* Functional tests related to Riak Preference lists
26+
*
27+
* @author Christopher Mancini <cmancini at basho d0t com>
28+
*/
29+
class PreflistTest extends TestCase
30+
{
31+
/**
32+
* @dataProvider getLocalNodeConnection
33+
*
34+
* @param $riak \Basho\Riak
35+
*/
36+
public function testFetch($riak)
37+
{
38+
// build a store object comand, get the location of the newly minted object
39+
$location = (new Command\Builder\StoreObject($riak))
40+
->buildObject('some_data')
41+
->buildBucket('users')
42+
->build()
43+
->execute()
44+
->getLocation();
45+
46+
// build a fetch command
47+
$command = (new Command\Builder\FetchPreflist($riak))
48+
->atLocation($location)
49+
->build();
50+
51+
$response = $command->execute();
52+
53+
$this->assertEquals('200', $response->getStatusCode(), $response->getBody());
54+
$this->assertJson($response->getBody());
55+
$this->assertNotEmpty($response->getObject()->getData()->preflist);
56+
$this->assertObjectHasAttribute("partition", $response->getObject()->getData()->preflist[0]);
57+
$this->assertObjectHasAttribute("node", $response->getObject()->getData()->preflist[0]);
58+
$this->assertObjectHasAttribute("primary", $response->getObject()->getData()->preflist[0]);
59+
}
60+
}

0 commit comments

Comments
 (0)