Skip to content

Commit 29da9dd

Browse files
authored
Merge pull request #5 from 007hacky007/get-responses
Added getResponses method
2 parents a817a56 + 019a501 commit 29da9dd

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,18 @@ $client->put("key", "value");
4141
$client->get("key");
4242
$client->delete("key");
4343
$client->putIf("key", "newValue", "valueToCompareWith");
44-
$client->deleteIf("key", "3");
44+
$client->deleteIf("key", "valueToCompareWith");
45+
46+
// complex transaction example
47+
$leaseId = $client->getLeaseID(10);
48+
$putOp = $client->getPutOperation('key', 'someValueToPutOnSuccess', $leaseId);
49+
$getOp = $client->getGetOperation('key');
50+
// following compare checks for key existence
51+
$compare = $client->getCompare('key', '0', \Etcdserverpb\Compare\CompareResult::EQUAL, \Etcdserverpb\Compare\CompareTarget::MOD);
52+
// execute Put operation and return the key we stored, just return the key value if it already exists
53+
$txnResponse = $client->txnRequest([$putOp, $getOp], [$getOp], [$compare]);
54+
$result = $client->getResponses($txnResponse, 'response_range', true);
55+
// $result[0] contains "someValueToPutOnSuccess"
4556
```
4657

4758
#### Sharded client

src/Client.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Etcdserverpb\TxnResponse;
3030
use Exception;
3131
use Grpc\ChannelCredentials;
32+
use Mvccpb\KeyValue;
3233

3334
/**
3435
* Class Client
@@ -316,6 +317,51 @@ public function txnRequest(array $requestOperations, ?array $failureOperations,
316317
return $response;
317318
}
318319

320+
/**
321+
* Transform TxnResponse into more friendly array
322+
*
323+
* @param TxnResponse $txnResponse return value of txnRequest method
324+
* @param string|null $type returns only chosen type if defined,
325+
* can be one of those: response_range, response_put, response_delete_range, response_txn
326+
* @param bool $simpleArray return just simple array containing values,
327+
* example: ['value1', 'value2']
328+
* @return array example: [
329+
* [
330+
* 'type' => 'response_range',
331+
* 'values' => [
332+
* 'key' => 'key',
333+
* 'value' => '2',
334+
* 'version' => 3
335+
* ]
336+
* ]
337+
* ]
338+
*/
339+
public function getResponses(TxnResponse $txnResponse, ?string $type = null, bool $simpleArray = false): array
340+
{
341+
$v = $r = [];
342+
/** @var ResponseOp $response */
343+
foreach ($txnResponse->getResponses() as $response) {
344+
if ($type !== null && $type !== $response->getResponse())
345+
continue;
346+
$values = [];
347+
if ($response->getResponseRange() !== null) {
348+
/** @var KeyValue $field */
349+
foreach ($response->getResponseRange()->getKvs() as $field) {
350+
$v[] = $field->getValue();
351+
$values[] = [
352+
'key' => $field->getKey(),
353+
'value' => $field->getValue(),
354+
'version' => $field->getVersion()
355+
];
356+
}
357+
358+
}
359+
$r[] = ['type' => $response->getResponse(), 'values' => $values];
360+
}
361+
362+
return ($simpleArray) ? $v : $r;
363+
}
364+
319365
/**
320366
* Creates RequestOp of Get operation for requestIf method
321367
*

src/ClientInterface.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,4 +151,25 @@ public function revokeLeaseID(int $leaseID);
151151
* @throws Exception
152152
*/
153153
public function refreshLease(int $leaseID);
154+
155+
/**
156+
* Transform TxnResponse into more friendly array
157+
*
158+
* @param TxnResponse $txnResponse return value of txnRequest method
159+
* @param string|null $type returns only chosen type if defined,
160+
* can be one of those: response_range, response_put, response_delete_range, response_txn
161+
* @param bool $simpleArray return just simple array containing values,
162+
* example: ['value1', 'value2']
163+
* @return array example: [
164+
* [
165+
* 'type' => 'response_range',
166+
* 'values' => [
167+
* 'key' => 'key',
168+
* 'value' => '2',
169+
* 'version' => 3
170+
* ]
171+
* ]
172+
* ]
173+
*/
174+
public function getResponses(TxnResponse $txnResponse, ?string $type = null, bool $simpleArray = false): array;
154175
}

src/ShardedClient.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,4 +209,28 @@ public function refreshLease(int $leaseID)
209209
{
210210
return $this->getRandomClient()->refreshLease($leaseID);
211211
}
212+
213+
/**
214+
* Transform TxnResponse into more friendly array
215+
*
216+
* @param TxnResponse $txnResponse return value of txnRequest method
217+
* @param string|null $type returns only chosen type if defined,
218+
* can be one of those: response_range, response_put, response_delete_range, response_txn
219+
* @param bool $simpleArray return just simple array containing values,
220+
* example: ['value1', 'value2']
221+
* @return array example: [
222+
* [
223+
* 'type' => 'response_range',
224+
* 'values' => [
225+
* 'key' => 'key',
226+
* 'value' => '2',
227+
* 'version' => 3
228+
* ]
229+
* ]
230+
* ]
231+
*/
232+
public function getResponses(TxnResponse $txnResponse, ?string $type = null, bool $simpleArray = false): array
233+
{
234+
return $this->getRandomClient()->getResponses($txnResponse, $type, $simpleArray);
235+
}
212236
}

0 commit comments

Comments
 (0)