From cffd3f5d66095408fe4fdbd938ea7dbe5202cb7d Mon Sep 17 00:00:00 2001 From: Jan Krpes Date: Wed, 29 Apr 2020 17:55:49 +0200 Subject: [PATCH 01/13] Custom compare support for the If methods --- README.md | 4 +-- src/Client.php | 79 +++++++++++++++++++++++++++++------------ src/ClientInterface.php | 16 ++++++--- src/ShardedClient.php | 9 ++--- 4 files changed, 75 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 5ecc7a2..7d57e71 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,8 @@ $client = new Aternos\Etcd\Client("localhost:2379", "username", "password"); $client->put("key", "value"); $client->get("key"); $client->delete("key"); -$client->putIf("key", "newValue", "expectedPreviousValue"); -$client->deleteIf("key", "expectedPreviousValue"); +$client->putIf("key", "newValue", "valueToCompareWith", "=", Etcdserverpb\Compare\CompareTarget::VALUE); +$client->deleteIf("key", "3", ">", Etcdserverpb\Compare\CompareTarget::VERSION); ``` #### Sharded client diff --git a/src/Client.php b/src/Client.php index 148be9a..1a85943 100644 --- a/src/Client.php +++ b/src/Client.php @@ -172,13 +172,16 @@ public function delete(string $key) * Put $value if $key value matches $previousValue otherwise $returnNewValueOnFail * * @param string $key - * @param mixed $value The new value to set - * @param mixed $previousValue The previous value to compare against + * @param string $value The new value to set + * @param string $compareValue The previous value to compare against + * @param string $compareOp can be '=', '!=', '>', '<' + * @param int $compareTarget check constants in the CompareTarget class for available values * @param bool $returnNewValueOnFail * @return bool|string * @throws InvalidResponseStatusCodeException + * @throws \Exception */ - public function putIf(string $key, $value, $previousValue, bool $returnNewValueOnFail = false) + public function putIf(string $key, string $value, string $compareValue = '0', string $compareOp = '=', int $compareTarget = CompareTarget::VALUE, bool $returnNewValueOnFail = false) { $request = new PutRequest(); $request->setKey($key); @@ -187,19 +190,24 @@ public function putIf(string $key, $value, $previousValue, bool $returnNewValueO $operation = new RequestOp(); $operation->setRequestPut($request); - return $this->requestIf($key, $previousValue, $operation, $returnNewValueOnFail); + $compare = $this->getCompare($key, $compareValue, $compareOp, $compareTarget); + + return $this->requestIf($key, $operation, $compare, $returnNewValueOnFail); } /** * Delete if $key value matches $previous value otherwise $returnNewValueOnFail * * @param string $key - * @param $previousValue + * @param string $compareValue The previous value to compare against + * @param string $compareOp can be '=', '!=', '>', '<' + * @param int $compareTarget check constants in the CompareTarget class for available values * @param bool $returnNewValueOnFail * @return bool|string * @throws InvalidResponseStatusCodeException + * @throws \Exception */ - public function deleteIf(string $key, $previousValue, bool $returnNewValueOnFail = false) + public function deleteIf(string $key, string $compareValue = '0', string $compareOp = '=', int $compareTarget = CompareTarget::VALUE, bool $returnNewValueOnFail = false) { $request = new DeleteRangeRequest(); $request->setKey($key); @@ -207,36 +215,25 @@ public function deleteIf(string $key, $previousValue, bool $returnNewValueOnFail $operation = new RequestOp(); $operation->setRequestDeleteRange($request); - return $this->requestIf($key, $previousValue, $operation, $returnNewValueOnFail); + $compare = $this->getCompare($key, $compareValue, $compareOp, $compareTarget); + + return $this->requestIf($key, $operation, $compare, $returnNewValueOnFail); } /** * Execute $requestOperation if $key value matches $previous otherwise $returnNewValueOnFail * * @param string $key - * @param $previousValue * @param RequestOp $requestOperation + * @param Compare $compare * @param bool $returnNewValueOnFail * @return bool|string * @throws InvalidResponseStatusCodeException */ - protected function requestIf(string $key, $previousValue, RequestOp $requestOperation, bool $returnNewValueOnFail = false) + protected function requestIf(string $key, RequestOp $requestOperation, Compare $compare, bool $returnNewValueOnFail = false) { $client = $this->getKvClient(); - $compare = new Compare(); - $compare->setKey($key); - - if ($previousValue === false) { - $compare->setValue("0"); - $compare->setResult(CompareResult::EQUAL); - $compare->setTarget(CompareTarget::VERSION); - } else { - $compare->setValue($previousValue); - $compare->setResult(CompareResult::EQUAL); - $compare->setTarget(CompareTarget::VALUE); - } - $request = new TxnRequest(); $request->setCompare([$compare]); $request->setSuccess([$requestOperation]); @@ -304,6 +301,44 @@ protected function getAuthClient(): AuthClient return $this->authClient; } + /** + * Get an instance of Compare + * + * @param string $key + * @param string $value + * @param string $result resultOp, can be '=', '!=', '>', '<' + * @param int $target check constants in the CompareTarget class for available values + * @return Compare + * @throws \Exception + */ + protected function getCompare(string $key, string $value, string $result, int $target): Compare + { + switch ($result) { + case '=': + $cmpResult = CompareResult::EQUAL; + break; + case '!=': + $cmpResult = CompareResult::NOT_EQUAL; + break; + case '>': + $cmpResult = CompareResult::GREATER; + break; + case '<': + $cmpResult = CompareResult::LESS; + break; + default: + throw new \Exception('Unknown result op'); + } + + $compare = new Compare(); + $compare->setKey($key); + $compare->setValue($value); + $compare->setTarget($target); + $compare->setResult($cmpResult); + + return $compare; + } + /** * Get an authentication token * diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 4ab5340..e601062 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -3,6 +3,7 @@ namespace Aternos\Etcd; use Aternos\Etcd\Exception\Status\InvalidResponseStatusCodeException; +use Etcdserverpb\Compare\CompareTarget; /** * Interface ClientInterface @@ -53,22 +54,27 @@ public function delete(string $key); * Put $value if $key value matches $previousValue otherwise $returnNewValueOnFail * * @param string $key - * @param mixed $value The new value to set - * @param mixed $previousValue The previous value to compare against + * @param string $value The new value to set + * @param string $compareValue The previous value to compare against + * @param string $compareOp can be '=', '!=', '>', '<' + * @param int $compareTarget check constants in the CompareTarget class for available values * @param bool $returnNewValueOnFail * @return bool|string * @throws InvalidResponseStatusCodeException */ - public function putIf(string $key, $value, $previousValue, bool $returnNewValueOnFail = false); + public function putIf(string $key, string $value, string $compareValue = '0', string $compareOp = '=', int $compareTarget = CompareTarget::VALUE, bool $returnNewValueOnFail = false); /** * Delete if $key value matches $previous value otherwise $returnNewValueOnFail * * @param string $key - * @param $previousValue + * @param string $compareValue The previous value to compare against + * @param string $compareOp can be '=', '!=', '>', '<' + * @param int $compareTarget check constants in the CompareTarget class for available values * @param bool $returnNewValueOnFail * @return bool|string * @throws InvalidResponseStatusCodeException + * @throws \Exception */ - public function deleteIf(string $key, $previousValue, bool $returnNewValueOnFail = false); + public function deleteIf(string $key, string $compareValue = '0', string $compareOp = '=', int $compareTarget = CompareTarget::VALUE, bool $returnNewValueOnFail = false); } \ No newline at end of file diff --git a/src/ShardedClient.php b/src/ShardedClient.php index 6f6096c..a8e0267 100644 --- a/src/ShardedClient.php +++ b/src/ShardedClient.php @@ -3,6 +3,7 @@ namespace Aternos\Etcd; use Aternos\Etcd\Exception\InvalidClientException; +use Etcdserverpb\Compare\CompareTarget; use Flexihash\Flexihash; /** @@ -112,17 +113,17 @@ public function delete(string $key) * @inheritDoc * @throws \Flexihash\Exception */ - public function putIf(string $key, $value, $previousValue, bool $returnNewValueOnFail = false) + public function putIf(string $key, string $value, string $compareValue = '0', string $compareOp = '=', int $compareTarget = CompareTarget::VALUE, bool $returnNewValueOnFail = false) { - return $this->getClientFromKey($key)->putIf($key, $value, $previousValue, $returnNewValueOnFail); + return $this->getClientFromKey($key)->putIf($key, $value, $compareValue, $compareOp, $compareTarget, $returnNewValueOnFail); } /** * @inheritDoc * @throws \Flexihash\Exception */ - public function deleteIf(string $key, $previousValue, bool $returnNewValueOnFail = false) + public function deleteIf(string $key, string $compareValue = '0', string $compareOp = '=', int $compareTarget = CompareTarget::VALUE, bool $returnNewValueOnFail = false) { - return $this->getClientFromKey($key)->deleteIf($key, $previousValue, $returnNewValueOnFail); + return $this->getClientFromKey($key)->deleteIf($key, $compareValue, $compareOp, $compareTarget, $returnNewValueOnFail); } } \ No newline at end of file From a9f3103603760f3853e70914099dfa66dc424cd2 Mon Sep 17 00:00:00 2001 From: Jan Krpes Date: Wed, 29 Apr 2020 23:31:23 +0200 Subject: [PATCH 02/13] Changed $returnNewValueOnFail param position --- src/Client.php | 8 ++++---- src/ClientInterface.php | 8 ++++---- src/ShardedClient.php | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Client.php b/src/Client.php index 1a85943..051a19f 100644 --- a/src/Client.php +++ b/src/Client.php @@ -174,14 +174,14 @@ public function delete(string $key) * @param string $key * @param string $value The new value to set * @param string $compareValue The previous value to compare against + * @param bool $returnNewValueOnFail * @param string $compareOp can be '=', '!=', '>', '<' * @param int $compareTarget check constants in the CompareTarget class for available values - * @param bool $returnNewValueOnFail * @return bool|string * @throws InvalidResponseStatusCodeException * @throws \Exception */ - public function putIf(string $key, string $value, string $compareValue = '0', string $compareOp = '=', int $compareTarget = CompareTarget::VALUE, bool $returnNewValueOnFail = false) + public function putIf(string $key, string $value, string $compareValue = '0', bool $returnNewValueOnFail = false, string $compareOp = '=', int $compareTarget = CompareTarget::VALUE) { $request = new PutRequest(); $request->setKey($key); @@ -200,14 +200,14 @@ public function putIf(string $key, string $value, string $compareValue = '0', st * * @param string $key * @param string $compareValue The previous value to compare against + * @param bool $returnNewValueOnFail * @param string $compareOp can be '=', '!=', '>', '<' * @param int $compareTarget check constants in the CompareTarget class for available values - * @param bool $returnNewValueOnFail * @return bool|string * @throws InvalidResponseStatusCodeException * @throws \Exception */ - public function deleteIf(string $key, string $compareValue = '0', string $compareOp = '=', int $compareTarget = CompareTarget::VALUE, bool $returnNewValueOnFail = false) + public function deleteIf(string $key, string $compareValue = '0', bool $returnNewValueOnFail = false, string $compareOp = '=', int $compareTarget = CompareTarget::VALUE) { $request = new DeleteRangeRequest(); $request->setKey($key); diff --git a/src/ClientInterface.php b/src/ClientInterface.php index e601062..2cc2674 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -56,25 +56,25 @@ public function delete(string $key); * @param string $key * @param string $value The new value to set * @param string $compareValue The previous value to compare against + * @param bool $returnNewValueOnFail * @param string $compareOp can be '=', '!=', '>', '<' * @param int $compareTarget check constants in the CompareTarget class for available values - * @param bool $returnNewValueOnFail * @return bool|string * @throws InvalidResponseStatusCodeException */ - public function putIf(string $key, string $value, string $compareValue = '0', string $compareOp = '=', int $compareTarget = CompareTarget::VALUE, bool $returnNewValueOnFail = false); + public function putIf(string $key, string $value, string $compareValue = '0', bool $returnNewValueOnFail = false, string $compareOp = '=', int $compareTarget = CompareTarget::VALUE); /** * Delete if $key value matches $previous value otherwise $returnNewValueOnFail * * @param string $key * @param string $compareValue The previous value to compare against + * @param bool $returnNewValueOnFail * @param string $compareOp can be '=', '!=', '>', '<' * @param int $compareTarget check constants in the CompareTarget class for available values - * @param bool $returnNewValueOnFail * @return bool|string * @throws InvalidResponseStatusCodeException * @throws \Exception */ - public function deleteIf(string $key, string $compareValue = '0', string $compareOp = '=', int $compareTarget = CompareTarget::VALUE, bool $returnNewValueOnFail = false); + public function deleteIf(string $key, string $compareValue = '0', bool $returnNewValueOnFail = false, string $compareOp = '=', int $compareTarget = CompareTarget::VALUE); } \ No newline at end of file diff --git a/src/ShardedClient.php b/src/ShardedClient.php index a8e0267..e5684ea 100644 --- a/src/ShardedClient.php +++ b/src/ShardedClient.php @@ -113,17 +113,17 @@ public function delete(string $key) * @inheritDoc * @throws \Flexihash\Exception */ - public function putIf(string $key, string $value, string $compareValue = '0', string $compareOp = '=', int $compareTarget = CompareTarget::VALUE, bool $returnNewValueOnFail = false) + public function putIf(string $key, string $value, string $compareValue = '0', bool $returnNewValueOnFail = false, string $compareOp = '=', int $compareTarget = CompareTarget::VALUE) { - return $this->getClientFromKey($key)->putIf($key, $value, $compareValue, $compareOp, $compareTarget, $returnNewValueOnFail); + return $this->getClientFromKey($key)->putIf($key, $value, $compareValue, $returnNewValueOnFail, $compareOp, $compareTarget); } /** * @inheritDoc * @throws \Flexihash\Exception */ - public function deleteIf(string $key, string $compareValue = '0', string $compareOp = '=', int $compareTarget = CompareTarget::VALUE, bool $returnNewValueOnFail = false) + public function deleteIf(string $key, string $compareValue = '0', bool $returnNewValueOnFail = false, string $compareOp = '=', int $compareTarget = CompareTarget::VALUE) { - return $this->getClientFromKey($key)->deleteIf($key, $compareValue, $compareOp, $compareTarget, $returnNewValueOnFail); + return $this->getClientFromKey($key)->deleteIf($key, $compareValue, $returnNewValueOnFail, $compareOp, $compareTarget); } } \ No newline at end of file From 17f94430c07d440b4201ed5a4a4505c5d7ecd24b Mon Sep 17 00:00:00 2001 From: Jan Krpes Date: Wed, 29 Apr 2020 23:47:54 +0200 Subject: [PATCH 03/13] compareOp now use const from CompareResult class --- src/Client.php | 32 +++++++------------------------- src/ClientInterface.php | 9 +++++---- src/ShardedClient.php | 5 +++-- 3 files changed, 15 insertions(+), 31 deletions(-) diff --git a/src/Client.php b/src/Client.php index 051a19f..16d7998 100644 --- a/src/Client.php +++ b/src/Client.php @@ -175,13 +175,13 @@ public function delete(string $key) * @param string $value The new value to set * @param string $compareValue The previous value to compare against * @param bool $returnNewValueOnFail - * @param string $compareOp can be '=', '!=', '>', '<' + * @param int $compareOp see CompareResult class for available constants * @param int $compareTarget check constants in the CompareTarget class for available values * @return bool|string * @throws InvalidResponseStatusCodeException * @throws \Exception */ - public function putIf(string $key, string $value, string $compareValue = '0', bool $returnNewValueOnFail = false, string $compareOp = '=', int $compareTarget = CompareTarget::VALUE) + public function putIf(string $key, string $value, string $compareValue = '0', bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE) { $request = new PutRequest(); $request->setKey($key); @@ -201,13 +201,13 @@ public function putIf(string $key, string $value, string $compareValue = '0', bo * @param string $key * @param string $compareValue The previous value to compare against * @param bool $returnNewValueOnFail - * @param string $compareOp can be '=', '!=', '>', '<' + * @param int $compareOp see CompareResult class for available constants * @param int $compareTarget check constants in the CompareTarget class for available values * @return bool|string * @throws InvalidResponseStatusCodeException * @throws \Exception */ - public function deleteIf(string $key, string $compareValue = '0', bool $returnNewValueOnFail = false, string $compareOp = '=', int $compareTarget = CompareTarget::VALUE) + public function deleteIf(string $key, string $compareValue = '0', bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE) { $request = new DeleteRangeRequest(); $request->setKey($key); @@ -306,35 +306,17 @@ protected function getAuthClient(): AuthClient * * @param string $key * @param string $value - * @param string $result resultOp, can be '=', '!=', '>', '<' + * @param int $result see CompareResult class for available constants * @param int $target check constants in the CompareTarget class for available values * @return Compare - * @throws \Exception */ - protected function getCompare(string $key, string $value, string $result, int $target): Compare + protected function getCompare(string $key, string $value, int $result, int $target): Compare { - switch ($result) { - case '=': - $cmpResult = CompareResult::EQUAL; - break; - case '!=': - $cmpResult = CompareResult::NOT_EQUAL; - break; - case '>': - $cmpResult = CompareResult::GREATER; - break; - case '<': - $cmpResult = CompareResult::LESS; - break; - default: - throw new \Exception('Unknown result op'); - } - $compare = new Compare(); $compare->setKey($key); $compare->setValue($value); $compare->setTarget($target); - $compare->setResult($cmpResult); + $compare->setResult($result); return $compare; } diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 2cc2674..48c345f 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -3,6 +3,7 @@ namespace Aternos\Etcd; use Aternos\Etcd\Exception\Status\InvalidResponseStatusCodeException; +use Etcdserverpb\Compare\CompareResult; use Etcdserverpb\Compare\CompareTarget; /** @@ -57,12 +58,12 @@ public function delete(string $key); * @param string $value The new value to set * @param string $compareValue The previous value to compare against * @param bool $returnNewValueOnFail - * @param string $compareOp can be '=', '!=', '>', '<' + * @param int $compareOp see CompareResult class for available constants * @param int $compareTarget check constants in the CompareTarget class for available values * @return bool|string * @throws InvalidResponseStatusCodeException */ - public function putIf(string $key, string $value, string $compareValue = '0', bool $returnNewValueOnFail = false, string $compareOp = '=', int $compareTarget = CompareTarget::VALUE); + public function putIf(string $key, string $value, string $compareValue = '0', bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE); /** * Delete if $key value matches $previous value otherwise $returnNewValueOnFail @@ -70,11 +71,11 @@ public function putIf(string $key, string $value, string $compareValue = '0', bo * @param string $key * @param string $compareValue The previous value to compare against * @param bool $returnNewValueOnFail - * @param string $compareOp can be '=', '!=', '>', '<' + * @param int $compareOp see CompareResult class for available constants * @param int $compareTarget check constants in the CompareTarget class for available values * @return bool|string * @throws InvalidResponseStatusCodeException * @throws \Exception */ - public function deleteIf(string $key, string $compareValue = '0', bool $returnNewValueOnFail = false, string $compareOp = '=', int $compareTarget = CompareTarget::VALUE); + public function deleteIf(string $key, string $compareValue = '0', bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE); } \ No newline at end of file diff --git a/src/ShardedClient.php b/src/ShardedClient.php index e5684ea..f2ecb34 100644 --- a/src/ShardedClient.php +++ b/src/ShardedClient.php @@ -3,6 +3,7 @@ namespace Aternos\Etcd; use Aternos\Etcd\Exception\InvalidClientException; +use Etcdserverpb\Compare\CompareResult; use Etcdserverpb\Compare\CompareTarget; use Flexihash\Flexihash; @@ -113,7 +114,7 @@ public function delete(string $key) * @inheritDoc * @throws \Flexihash\Exception */ - public function putIf(string $key, string $value, string $compareValue = '0', bool $returnNewValueOnFail = false, string $compareOp = '=', int $compareTarget = CompareTarget::VALUE) + public function putIf(string $key, string $value, string $compareValue = '0', bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE) { return $this->getClientFromKey($key)->putIf($key, $value, $compareValue, $returnNewValueOnFail, $compareOp, $compareTarget); } @@ -122,7 +123,7 @@ public function putIf(string $key, string $value, string $compareValue = '0', bo * @inheritDoc * @throws \Flexihash\Exception */ - public function deleteIf(string $key, string $compareValue = '0', bool $returnNewValueOnFail = false, string $compareOp = '=', int $compareTarget = CompareTarget::VALUE) + public function deleteIf(string $key, string $compareValue = '0', bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE) { return $this->getClientFromKey($key)->deleteIf($key, $compareValue, $returnNewValueOnFail, $compareOp, $compareTarget); } From 2fb794984228a48bb4d7107c7fc084a0f6d0ba8b Mon Sep 17 00:00:00 2001 From: Jan Krpes Date: Wed, 29 Apr 2020 23:48:13 +0200 Subject: [PATCH 04/13] Fixed README --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 7d57e71..be858ce 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,8 @@ $client = new Aternos\Etcd\Client("localhost:2379", "username", "password"); $client->put("key", "value"); $client->get("key"); $client->delete("key"); -$client->putIf("key", "newValue", "valueToCompareWith", "=", Etcdserverpb\Compare\CompareTarget::VALUE); -$client->deleteIf("key", "3", ">", Etcdserverpb\Compare\CompareTarget::VERSION); +$client->putIf("key", "newValue", "valueToCompareWith", false, \Etcdserverpb\Compare\CompareResult::EQUAL, Etcdserverpb\Compare\CompareTarget::VALUE); +$client->deleteIf("key", "3", false, \Etcdserverpb\Compare\CompareResult::GREATER, Etcdserverpb\Compare\CompareTarget::VERSION); ``` #### Sharded client From 8998aa4f92c22672e0e59c1435dc86ed18548159 Mon Sep 17 00:00:00 2001 From: Jan Krpes Date: Thu, 30 Apr 2020 11:35:36 +0200 Subject: [PATCH 05/13] Removed default value from the compareValue parameter --- src/Client.php | 4 ++-- src/ClientInterface.php | 4 ++-- src/ShardedClient.php | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Client.php b/src/Client.php index 16d7998..df17cc4 100644 --- a/src/Client.php +++ b/src/Client.php @@ -181,7 +181,7 @@ public function delete(string $key) * @throws InvalidResponseStatusCodeException * @throws \Exception */ - public function putIf(string $key, string $value, string $compareValue = '0', bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE) + public function putIf(string $key, string $value, string $compareValue, bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE) { $request = new PutRequest(); $request->setKey($key); @@ -207,7 +207,7 @@ public function putIf(string $key, string $value, string $compareValue = '0', bo * @throws InvalidResponseStatusCodeException * @throws \Exception */ - public function deleteIf(string $key, string $compareValue = '0', bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE) + public function deleteIf(string $key, string $compareValue, bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE) { $request = new DeleteRangeRequest(); $request->setKey($key); diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 48c345f..f43a4af 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -63,7 +63,7 @@ public function delete(string $key); * @return bool|string * @throws InvalidResponseStatusCodeException */ - public function putIf(string $key, string $value, string $compareValue = '0', bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE); + public function putIf(string $key, string $value, string $compareValue, bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE); /** * Delete if $key value matches $previous value otherwise $returnNewValueOnFail @@ -77,5 +77,5 @@ public function putIf(string $key, string $value, string $compareValue = '0', bo * @throws InvalidResponseStatusCodeException * @throws \Exception */ - public function deleteIf(string $key, string $compareValue = '0', bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE); + public function deleteIf(string $key, string $compareValue, bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE); } \ No newline at end of file diff --git a/src/ShardedClient.php b/src/ShardedClient.php index f2ecb34..03747b4 100644 --- a/src/ShardedClient.php +++ b/src/ShardedClient.php @@ -114,7 +114,7 @@ public function delete(string $key) * @inheritDoc * @throws \Flexihash\Exception */ - public function putIf(string $key, string $value, string $compareValue = '0', bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE) + public function putIf(string $key, string $value, string $compareValue, bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE) { return $this->getClientFromKey($key)->putIf($key, $value, $compareValue, $returnNewValueOnFail, $compareOp, $compareTarget); } @@ -123,7 +123,7 @@ public function putIf(string $key, string $value, string $compareValue = '0', bo * @inheritDoc * @throws \Flexihash\Exception */ - public function deleteIf(string $key, string $compareValue = '0', bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE) + public function deleteIf(string $key, string $compareValue, bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE) { return $this->getClientFromKey($key)->deleteIf($key, $compareValue, $returnNewValueOnFail, $compareOp, $compareTarget); } From 67041ee99f8bd7a3bb912c7a06fdb8b1e112cab5 Mon Sep 17 00:00:00 2001 From: Jan Krpes Date: Thu, 30 Apr 2020 17:17:00 +0200 Subject: [PATCH 06/13] Rollback to original putIf and deleteIf functionality --- README.md | 4 ++-- src/Client.php | 31 +++++++++++++++++++++---------- src/ClientInterface.php | 12 ++++-------- src/ShardedClient.php | 8 ++++---- 4 files changed, 31 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index be858ce..3cf8243 100644 --- a/README.md +++ b/README.md @@ -40,8 +40,8 @@ $client = new Aternos\Etcd\Client("localhost:2379", "username", "password"); $client->put("key", "value"); $client->get("key"); $client->delete("key"); -$client->putIf("key", "newValue", "valueToCompareWith", false, \Etcdserverpb\Compare\CompareResult::EQUAL, Etcdserverpb\Compare\CompareTarget::VALUE); -$client->deleteIf("key", "3", false, \Etcdserverpb\Compare\CompareResult::GREATER, Etcdserverpb\Compare\CompareTarget::VERSION); +$client->putIf("key", "newValue", "valueToCompareWith"); +$client->deleteIf("key", "3"); ``` #### Sharded client diff --git a/src/Client.php b/src/Client.php index 1a75367..1e05bae 100644 --- a/src/Client.php +++ b/src/Client.php @@ -180,15 +180,13 @@ public function delete(string $key) * * @param string $key * @param string $value The new value to set - * @param string $compareValue The previous value to compare against + * @param bool|string $compareValue The previous value to compare against * @param bool $returnNewValueOnFail - * @param int $compareOp see CompareResult class for available constants - * @param int $compareTarget check constants in the CompareTarget class for available values * @return bool|string * @throws InvalidResponseStatusCodeException * @throws \Exception */ - public function putIf(string $key, string $value, string $compareValue, bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE) + public function putIf(string $key, string $value, $compareValue, bool $returnNewValueOnFail = false) { $request = new PutRequest(); $request->setKey($key); @@ -197,7 +195,7 @@ public function putIf(string $key, string $value, string $compareValue, bool $re $operation = new RequestOp(); $operation->setRequestPut($request); - $compare = $this->getCompare($key, $compareValue, $compareOp, $compareTarget); + $compare = $this->getCompareForIf($key, $compareValue); return $this->requestIf($key, $operation, $compare, $returnNewValueOnFail); } @@ -206,15 +204,13 @@ public function putIf(string $key, string $value, string $compareValue, bool $re * Delete if $key value matches $previous value otherwise $returnNewValueOnFail * * @param string $key - * @param string $compareValue The previous value to compare against + * @param bool|string $compareValue The previous value to compare against * @param bool $returnNewValueOnFail - * @param int $compareOp see CompareResult class for available constants - * @param int $compareTarget check constants in the CompareTarget class for available values * @return bool|string * @throws InvalidResponseStatusCodeException * @throws \Exception */ - public function deleteIf(string $key, string $compareValue, bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE) + public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail = false) { $request = new DeleteRangeRequest(); $request->setKey($key); @@ -222,7 +218,7 @@ public function deleteIf(string $key, string $compareValue, bool $returnNewValue $operation = new RequestOp(); $operation->setRequestDeleteRange($request); - $compare = $this->getCompare($key, $compareValue, $compareOp, $compareTarget); + $compare = $this->getCompareForIf($key, $compareValue); return $this->requestIf($key, $operation, $compare, $returnNewValueOnFail); } @@ -390,4 +386,19 @@ protected function validateStatus($status) throw ResponseStatusCodeExceptionFactory::getExceptionByCode($status->code, $status->details); } } + + /** + * @param string $key + * @param string $compareValue + * @return Compare + */ + protected function getCompareForIf(string $key, string $compareValue): Compare + { + if ($compareValue === false) { + $compare = $this->getCompare($key, '0', CompareResult::EQUAL, CompareTarget::VERSION); + } else { + $compare = $this->getCompare($key, $compareValue, CompareResult::EQUAL, CompareTarget::VALUE); + } + return $compare; + } } \ No newline at end of file diff --git a/src/ClientInterface.php b/src/ClientInterface.php index f43a4af..f609a86 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -56,26 +56,22 @@ public function delete(string $key); * * @param string $key * @param string $value The new value to set - * @param string $compareValue The previous value to compare against + * @param bool|string $compareValue The previous value to compare against * @param bool $returnNewValueOnFail - * @param int $compareOp see CompareResult class for available constants - * @param int $compareTarget check constants in the CompareTarget class for available values * @return bool|string * @throws InvalidResponseStatusCodeException */ - public function putIf(string $key, string $value, string $compareValue, bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE); + public function putIf(string $key, string $value, $compareValue, bool $returnNewValueOnFail = false); /** * Delete if $key value matches $previous value otherwise $returnNewValueOnFail * * @param string $key - * @param string $compareValue The previous value to compare against + * @param bool|string $compareValue The previous value to compare against * @param bool $returnNewValueOnFail - * @param int $compareOp see CompareResult class for available constants - * @param int $compareTarget check constants in the CompareTarget class for available values * @return bool|string * @throws InvalidResponseStatusCodeException * @throws \Exception */ - public function deleteIf(string $key, string $compareValue, bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE); + public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail = false); } \ No newline at end of file diff --git a/src/ShardedClient.php b/src/ShardedClient.php index 03747b4..5a4c41c 100644 --- a/src/ShardedClient.php +++ b/src/ShardedClient.php @@ -114,17 +114,17 @@ public function delete(string $key) * @inheritDoc * @throws \Flexihash\Exception */ - public function putIf(string $key, string $value, string $compareValue, bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE) + public function putIf(string $key, string $value, $compareValue, bool $returnNewValueOnFail = false) { - return $this->getClientFromKey($key)->putIf($key, $value, $compareValue, $returnNewValueOnFail, $compareOp, $compareTarget); + return $this->getClientFromKey($key)->putIf($key, $value, $compareValue, $returnNewValueOnFail); } /** * @inheritDoc * @throws \Flexihash\Exception */ - public function deleteIf(string $key, string $compareValue, bool $returnNewValueOnFail = false, int $compareOp = CompareResult::EQUAL, int $compareTarget = CompareTarget::VALUE) + public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail = false) { - return $this->getClientFromKey($key)->deleteIf($key, $compareValue, $returnNewValueOnFail, $compareOp, $compareTarget); + return $this->getClientFromKey($key)->deleteIf($key, $compareValue, $returnNewValueOnFail); } } \ No newline at end of file From 5a9e5a26dc80ef9448c55677c0abaf7bf011ae18 Mon Sep 17 00:00:00 2001 From: Jan Krpes Date: Thu, 30 Apr 2020 17:19:49 +0200 Subject: [PATCH 07/13] requestIf method is now public --- src/Client.php | 2 +- src/ClientInterface.php | 16 ++++++++++++++-- src/ShardedClient.php | 13 +++++++++++-- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/src/Client.php b/src/Client.php index 1e05bae..5e003dd 100644 --- a/src/Client.php +++ b/src/Client.php @@ -233,7 +233,7 @@ public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail * @return bool|string * @throws InvalidResponseStatusCodeException */ - protected function requestIf(string $key, RequestOp $requestOperation, Compare $compare, bool $returnNewValueOnFail = false) + public function requestIf(string $key, RequestOp $requestOperation, Compare $compare, bool $returnNewValueOnFail = false) { $client = $this->getKvClient(); diff --git a/src/ClientInterface.php b/src/ClientInterface.php index f609a86..de7cdb7 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -3,8 +3,8 @@ namespace Aternos\Etcd; use Aternos\Etcd\Exception\Status\InvalidResponseStatusCodeException; -use Etcdserverpb\Compare\CompareResult; -use Etcdserverpb\Compare\CompareTarget; +use Etcdserverpb\Compare; +use Etcdserverpb\RequestOp; /** * Interface ClientInterface @@ -74,4 +74,16 @@ public function putIf(string $key, string $value, $compareValue, bool $returnNew * @throws \Exception */ public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail = false); + + /** + * Execute $requestOperation if $key value matches $previous otherwise $returnNewValueOnFail + * + * @param string $key + * @param RequestOp $requestOperation + * @param Compare $compare + * @param bool $returnNewValueOnFail + * @return bool|string + * @throws InvalidResponseStatusCodeException + */ + public function requestIf(string $key, RequestOp $requestOperation, Compare $compare, bool $returnNewValueOnFail = false); } \ No newline at end of file diff --git a/src/ShardedClient.php b/src/ShardedClient.php index 5a4c41c..c032118 100644 --- a/src/ShardedClient.php +++ b/src/ShardedClient.php @@ -3,8 +3,8 @@ namespace Aternos\Etcd; use Aternos\Etcd\Exception\InvalidClientException; -use Etcdserverpb\Compare\CompareResult; -use Etcdserverpb\Compare\CompareTarget; +use Etcdserverpb\Compare; +use Etcdserverpb\RequestOp; use Flexihash\Flexihash; /** @@ -127,4 +127,13 @@ public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail { return $this->getClientFromKey($key)->deleteIf($key, $compareValue, $returnNewValueOnFail); } + + /** + * @inheritDoc + * @throws \Flexihash\Exception + */ + public function requestIf(string $key, RequestOp $requestOperation, Compare $compare, bool $returnNewValueOnFail = false) + { + return $this->getClientFromKey($key)->requestIf($key, $requestOperation, $compare, $returnNewValueOnFail); + } } \ No newline at end of file From cb408743d33dcd525a3dab06a5b9f07f60fad912 Mon Sep 17 00:00:00 2001 From: Jan Krpes Date: Thu, 30 Apr 2020 17:40:38 +0200 Subject: [PATCH 08/13] getCompare method is now public --- src/Client.php | 39 ++++++++++++++++++++------------------- src/ClientInterface.php | 11 +++++++++++ src/ShardedClient.php | 9 +++++++++ 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/Client.php b/src/Client.php index 5e003dd..f9e8b4f 100644 --- a/src/Client.php +++ b/src/Client.php @@ -272,6 +272,26 @@ public function requestIf(string $key, RequestOp $requestOperation, Compare $com } } + /** + * Get an instance of Compare + * + * @param string $key + * @param string $value + * @param int $result see CompareResult class for available constants + * @param int $target check constants in the CompareTarget class for available values + * @return Compare + */ + public function getCompare(string $key, string $value, int $result, int $target): Compare + { + $compare = new Compare(); + $compare->setKey($key); + $compare->setValue($value); + $compare->setTarget($target); + $compare->setResult($result); + + return $compare; + } + /** * Get an instance of KVClient * @@ -304,25 +324,6 @@ protected function getAuthClient(): AuthClient return $this->authClient; } - /** - * Get an instance of Compare - * - * @param string $key - * @param string $value - * @param int $result see CompareResult class for available constants - * @param int $target check constants in the CompareTarget class for available values - * @return Compare - */ - protected function getCompare(string $key, string $value, int $result, int $target): Compare - { - $compare = new Compare(); - $compare->setKey($key); - $compare->setValue($value); - $compare->setTarget($target); - $compare->setResult($result); - - return $compare; - } /** * Get an authentication token diff --git a/src/ClientInterface.php b/src/ClientInterface.php index de7cdb7..9ec3d83 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -86,4 +86,15 @@ public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail * @throws InvalidResponseStatusCodeException */ public function requestIf(string $key, RequestOp $requestOperation, Compare $compare, bool $returnNewValueOnFail = false); + + /** + * Get an instance of Compare + * + * @param string $key + * @param string $value + * @param int $result see CompareResult class for available constants + * @param int $target check constants in the CompareTarget class for available values + * @return Compare + */ + public function getCompare(string $key, string $value, int $result, int $target): Compare; } \ No newline at end of file diff --git a/src/ShardedClient.php b/src/ShardedClient.php index c032118..94abf90 100644 --- a/src/ShardedClient.php +++ b/src/ShardedClient.php @@ -136,4 +136,13 @@ public function requestIf(string $key, RequestOp $requestOperation, Compare $com { return $this->getClientFromKey($key)->requestIf($key, $requestOperation, $compare, $returnNewValueOnFail); } + + /** + * @inheritDoc + * @throws \Flexihash\Exception + */ + public function getCompare(string $key, string $value, int $result, int $target): Compare + { + return $this->getClientFromKey($key)->getCompare($key, $value, $result, $target); + } } \ No newline at end of file From 55077ec7b2b0d878f8b75c2a8dd3e88d1235f1e5 Mon Sep 17 00:00:00 2001 From: Jan Krpes Date: Thu, 30 Apr 2020 17:49:28 +0200 Subject: [PATCH 09/13] Added public get, put and delete RequestOp methods --- src/Client.php | 68 +++++++++++++++++++++++++++++++++-------- src/ClientInterface.php | 25 +++++++++++++++ src/ShardedClient.php | 27 ++++++++++++++++ 3 files changed, 107 insertions(+), 13 deletions(-) diff --git a/src/Client.php b/src/Client.php index f9e8b4f..1434517 100644 --- a/src/Client.php +++ b/src/Client.php @@ -188,13 +188,7 @@ public function delete(string $key) */ public function putIf(string $key, string $value, $compareValue, bool $returnNewValueOnFail = false) { - $request = new PutRequest(); - $request->setKey($key); - $request->setValue($value); - - $operation = new RequestOp(); - $operation->setRequestPut($request); - + $operation = $this->getPutOperation($key, $value); $compare = $this->getCompareForIf($key, $compareValue); return $this->requestIf($key, $operation, $compare, $returnNewValueOnFail); @@ -212,12 +206,7 @@ public function putIf(string $key, string $value, $compareValue, bool $returnNew */ public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail = false) { - $request = new DeleteRangeRequest(); - $request->setKey($key); - - $operation = new RequestOp(); - $operation->setRequestDeleteRange($request); - + $operation = $this->getDeleteOperation($key); $compare = $this->getCompareForIf($key, $compareValue); return $this->requestIf($key, $operation, $compare, $returnNewValueOnFail); @@ -272,6 +261,59 @@ public function requestIf(string $key, RequestOp $requestOperation, Compare $com } } + /** + * Creates RequestOp of Get operation for requestIf method + * + * @param string $key + * @return RequestOp + */ + public function getGetOperation(string $key): RequestOp + { + $request = new RangeRequest(); + $request->setKey($key); + + $operation = new RequestOp(); + $operation->setRequestRange($request); + + return $operation; + } + + /** + * Creates RequestOp of Put operation for requestIf method + * + * @param string $key + * @param string $value + * @return RequestOp + */ + public function getPutOperation(string $key, string $value): RequestOp + { + $request = new PutRequest(); + $request->setKey($key); + $request->setValue($value); + + $operation = new RequestOp(); + $operation->setRequestPut($request); + + return $operation; + } + + /** + * Creates RequestOp of Delete operation for requestIf method + * + * @param string $key + * @return RequestOp + */ + public function getDeleteOperation(string $key): RequestOp + { + $request = new DeleteRangeRequest(); + $request->setKey($key); + + $operation = new RequestOp(); + $operation->setRequestDeleteRange($request); + + return $operation; + } + /** * Get an instance of Compare * diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 9ec3d83..18bd5f6 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -97,4 +97,29 @@ public function requestIf(string $key, RequestOp $requestOperation, Compare $com * @return Compare */ public function getCompare(string $key, string $value, int $result, int $target): Compare; + + /** + * Creates RequestOp of Get operation for requestIf method + * + * @param string $key + * @return RequestOp + */ + public function getGetOperation(string $key): RequestOp; + + /** + * Creates RequestOp of Put operation for requestIf method + * + * @param string $key + * @param string $value + * @return RequestOp + */ + public function getPutOperation(string $key, string $value): RequestOp; + + /** + * Creates RequestOp of Delete operation for requestIf method + * + * @param string $key + * @return RequestOp + */ + public function getDeleteOperation(string $key): RequestOp; } \ No newline at end of file diff --git a/src/ShardedClient.php b/src/ShardedClient.php index 94abf90..7b1f45e 100644 --- a/src/ShardedClient.php +++ b/src/ShardedClient.php @@ -145,4 +145,31 @@ public function getCompare(string $key, string $value, int $result, int $target) { return $this->getClientFromKey($key)->getCompare($key, $value, $result, $target); } + + /** + * @inheritDoc + * @throws \Flexihash\Exception + */ + public function getGetOperation(string $key): RequestOp + { + return $this->getClientFromKey($key)->getGetOperation($key); + } + + /** + * @inheritDoc + * @throws \Flexihash\Exception + */ + public function getPutOperation(string $key, string $value): RequestOp + { + return $this->getClientFromKey($key)->getPutOperation($key, $value); + } + + /** + * @inheritDoc + * @throws \Flexihash\Exception + */ + public function getDeleteOperation(string $key): RequestOp + { + return $this->getClientFromKey($key)->getDeleteOperation($key); + } } \ No newline at end of file From 7e14dbf2aa346886bf24b7f7237e9b7432933a8b Mon Sep 17 00:00:00 2001 From: Jan Krpes Date: Thu, 30 Apr 2020 17:57:07 +0200 Subject: [PATCH 10/13] requestIf now accepts array of RequestOp and Compare objects Previously it accepted only single Compare object and single RequestOp object which limited usability of etcd transactions. putIf and deleteIf methods were changed accordingly, so the backwards compatibility with these methods is retained. --- src/Client.php | 14 +++++++------- src/ClientInterface.php | 6 +++--- src/ShardedClient.php | 4 ++-- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Client.php b/src/Client.php index 1434517..61cd72a 100644 --- a/src/Client.php +++ b/src/Client.php @@ -191,7 +191,7 @@ public function putIf(string $key, string $value, $compareValue, bool $returnNew $operation = $this->getPutOperation($key, $value); $compare = $this->getCompareForIf($key, $compareValue); - return $this->requestIf($key, $operation, $compare, $returnNewValueOnFail); + return $this->requestIf($key, [$operation], [$compare], $returnNewValueOnFail); } /** @@ -209,26 +209,26 @@ public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail $operation = $this->getDeleteOperation($key); $compare = $this->getCompareForIf($key, $compareValue); - return $this->requestIf($key, $operation, $compare, $returnNewValueOnFail); + return $this->requestIf($key, [$operation], [$compare], $returnNewValueOnFail); } /** * Execute $requestOperation if $key value matches $previous otherwise $returnNewValueOnFail * * @param string $key - * @param RequestOp $requestOperation - * @param Compare $compare + * @param array $requestOperations array of RequestOp objects + * @param array $compare array of Compare objects * @param bool $returnNewValueOnFail * @return bool|string * @throws InvalidResponseStatusCodeException */ - public function requestIf(string $key, RequestOp $requestOperation, Compare $compare, bool $returnNewValueOnFail = false) + public function requestIf(string $key, array $requestOperations, array $compare, bool $returnNewValueOnFail = false) { $client = $this->getKvClient(); $request = new TxnRequest(); - $request->setCompare([$compare]); - $request->setSuccess([$requestOperation]); + $request->setCompare($compare); + $request->setSuccess($requestOperations); if ($returnNewValueOnFail) { $getRequest = new RangeRequest(); diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 18bd5f6..5232c16 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -79,13 +79,13 @@ public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail * Execute $requestOperation if $key value matches $previous otherwise $returnNewValueOnFail * * @param string $key - * @param RequestOp $requestOperation - * @param Compare $compare + * @param array $requestOperations array of RequestOp objects + * @param array $compare array of Compare objects * @param bool $returnNewValueOnFail * @return bool|string * @throws InvalidResponseStatusCodeException */ - public function requestIf(string $key, RequestOp $requestOperation, Compare $compare, bool $returnNewValueOnFail = false); + public function requestIf(string $key, array $requestOperations, array $compare, bool $returnNewValueOnFail = false); /** * Get an instance of Compare diff --git a/src/ShardedClient.php b/src/ShardedClient.php index 7b1f45e..9dbfc0b 100644 --- a/src/ShardedClient.php +++ b/src/ShardedClient.php @@ -132,9 +132,9 @@ public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail * @inheritDoc * @throws \Flexihash\Exception */ - public function requestIf(string $key, RequestOp $requestOperation, Compare $compare, bool $returnNewValueOnFail = false) + public function requestIf(string $key, array $requestOperations, array $compare, bool $returnNewValueOnFail = false) { - return $this->getClientFromKey($key)->requestIf($key, $requestOperation, $compare, $returnNewValueOnFail); + return $this->getClientFromKey($key)->requestIf($key, $requestOperations, $compare, $returnNewValueOnFail); } /** From 339cabb50d25a5b627b3e58014fc67e4dcc53d5c Mon Sep 17 00:00:00 2001 From: Jan Krpes Date: Thu, 30 Apr 2020 18:56:46 +0200 Subject: [PATCH 11/13] requestIf is now more universal Added support for operations to execute in case the 'if' condition does not succeed. requestIf now also returns TxnResponse directly which makes it more universal. --- src/Client.php | 82 ++++++++++++++++++++++++++--------------- src/ClientInterface.php | 8 ++-- src/ShardedClient.php | 5 ++- 3 files changed, 60 insertions(+), 35 deletions(-) diff --git a/src/Client.php b/src/Client.php index 61cd72a..c486b5a 100644 --- a/src/Client.php +++ b/src/Client.php @@ -190,8 +190,10 @@ public function putIf(string $key, string $value, $compareValue, bool $returnNew { $operation = $this->getPutOperation($key, $value); $compare = $this->getCompareForIf($key, $compareValue); + $failOperation = $this->getFailOperation($key, $returnNewValueOnFail); - return $this->requestIf($key, [$operation], [$compare], $returnNewValueOnFail); + $response = $this->requestIf($key, [$operation], $failOperation, [$compare]); + return $this->getIfResponse($returnNewValueOnFail, $response); } /** @@ -208,57 +210,38 @@ public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail { $operation = $this->getDeleteOperation($key); $compare = $this->getCompareForIf($key, $compareValue); + $failOperation = $this->getFailOperation($key, $returnNewValueOnFail); - return $this->requestIf($key, [$operation], [$compare], $returnNewValueOnFail); + $response = $this->requestIf($key, [$operation], $failOperation, [$compare]); + return $this->getIfResponse($returnNewValueOnFail, $response); } /** * Execute $requestOperation if $key value matches $previous otherwise $returnNewValueOnFail * * @param string $key - * @param array $requestOperations array of RequestOp objects + * @param array $requestOperations operations to perform on success, array of RequestOp objects + * @param array|null $failureOperations operations to perform on failure, array of RequestOp objects * @param array $compare array of Compare objects * @param bool $returnNewValueOnFail - * @return bool|string + * @return TxnResponse * @throws InvalidResponseStatusCodeException */ - public function requestIf(string $key, array $requestOperations, array $compare, bool $returnNewValueOnFail = false) + public function requestIf(string $key, array $requestOperations, ?array $failureOperations, array $compare): TxnResponse { $client = $this->getKvClient(); $request = new TxnRequest(); $request->setCompare($compare); $request->setSuccess($requestOperations); - - if ($returnNewValueOnFail) { - $getRequest = new RangeRequest(); - $getRequest->setKey($key); - - $getOperation = new RequestOp(); - $getOperation->setRequestRange($getRequest); - $request->setFailure([$getOperation]); - } + if($failureOperations !== null) + $request->setFailure($failureOperations); /** @var TxnResponse $response */ list($response, $status) = $client->Txn($request, $this->getMetaData(), $this->getOptions())->wait(); $this->validateStatus($status); - if ($returnNewValueOnFail && !$response->getSucceeded()) { - /** @var ResponseOp $responseOp */ - $responseOp = $response->getResponses()[0]; - - $getResponse = $responseOp->getResponseRange(); - - $field = $getResponse->getKvs(); - - if (count($field) === 0) { - return false; - } - - return $field[0]->getValue(); - } else { - return $response->getSucceeded(); - } + return $response; } /** @@ -444,4 +427,43 @@ protected function getCompareForIf(string $key, string $compareValue): Compare } return $compare; } + + /** + * @param bool $returnNewValueOnFail + * @param TxnResponse $response + * @return bool + */ + protected function getIfResponse(bool $returnNewValueOnFail, TxnResponse $response): bool + { + if ($returnNewValueOnFail && !$response->getSucceeded()) { + /** @var ResponseOp $responseOp */ + $responseOp = $response->getResponses()[0]; + + $getResponse = $responseOp->getResponseRange(); + + $field = $getResponse->getKvs(); + + if (count($field) === 0) { + return false; + } + + return $field[0]->getValue(); + } else { + return $response->getSucceeded(); + } + } + + /** + * @param string $key + * @param bool $returnNewValueOnFail + * @return array|null + */ + protected function getFailOperation(string $key, bool $returnNewValueOnFail) + { + $failOperation = null; + if ($returnNewValueOnFail) + $failOperation = [$this->getGetOperation($key)]; + + return $failOperation; + } } \ No newline at end of file diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 5232c16..26618e1 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -5,6 +5,7 @@ use Aternos\Etcd\Exception\Status\InvalidResponseStatusCodeException; use Etcdserverpb\Compare; use Etcdserverpb\RequestOp; +use Etcdserverpb\TxnResponse; /** * Interface ClientInterface @@ -79,13 +80,14 @@ public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail * Execute $requestOperation if $key value matches $previous otherwise $returnNewValueOnFail * * @param string $key - * @param array $requestOperations array of RequestOp objects + * @param array $requestOperations operations to perform on success, array of RequestOp objects + * @param array|null $failureOperations operations to perform on failure, array of RequestOp objects * @param array $compare array of Compare objects * @param bool $returnNewValueOnFail - * @return bool|string + * @return TxnResponse * @throws InvalidResponseStatusCodeException */ - public function requestIf(string $key, array $requestOperations, array $compare, bool $returnNewValueOnFail = false); + public function requestIf(string $key, array $requestOperations, ?array $failureOperations, array $compare): TxnResponse; /** * Get an instance of Compare diff --git a/src/ShardedClient.php b/src/ShardedClient.php index 9dbfc0b..5f80bac 100644 --- a/src/ShardedClient.php +++ b/src/ShardedClient.php @@ -5,6 +5,7 @@ use Aternos\Etcd\Exception\InvalidClientException; use Etcdserverpb\Compare; use Etcdserverpb\RequestOp; +use Etcdserverpb\TxnResponse; use Flexihash\Flexihash; /** @@ -132,9 +133,9 @@ public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail * @inheritDoc * @throws \Flexihash\Exception */ - public function requestIf(string $key, array $requestOperations, array $compare, bool $returnNewValueOnFail = false) + public function requestIf(string $key, array $requestOperations, ?array $failureOperations, array $compare): TxnResponse { - return $this->getClientFromKey($key)->requestIf($key, $requestOperations, $compare, $returnNewValueOnFail); + return $this->getClientFromKey($key)->requestIf($key, $requestOperations, $failureOperations, $compare); } /** From f5ed4b01dcf53c892fe6fe339e7c3af669e5f6bf Mon Sep 17 00:00:00 2001 From: Jan Krpes Date: Fri, 1 May 2020 10:43:55 +0200 Subject: [PATCH 12/13] Added support for the leaseId in the getPutOperation method --- src/Client.php | 5 ++++- src/ClientInterface.php | 3 ++- src/ShardedClient.php | 4 ++-- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Client.php b/src/Client.php index c486b5a..5137403 100644 --- a/src/Client.php +++ b/src/Client.php @@ -266,13 +266,16 @@ public function getGetOperation(string $key): RequestOp * * @param string $key * @param string $value + * @param int $leaseId * @return RequestOp */ - public function getPutOperation(string $key, string $value): RequestOp + public function getPutOperation(string $key, string $value, int $leaseId = 0): RequestOp { $request = new PutRequest(); $request->setKey($key); $request->setValue($value); + if($leaseId !== 0) + $request->setLease($leaseId); $operation = new RequestOp(); $operation->setRequestPut($request); diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 26618e1..9393b93 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -113,9 +113,10 @@ public function getGetOperation(string $key): RequestOp; * * @param string $key * @param string $value + * @param int $leaseId * @return RequestOp */ - public function getPutOperation(string $key, string $value): RequestOp; + public function getPutOperation(string $key, string $value, int $leaseId = 0): RequestOp; /** * Creates RequestOp of Delete operation for requestIf method diff --git a/src/ShardedClient.php b/src/ShardedClient.php index 5f80bac..799767c 100644 --- a/src/ShardedClient.php +++ b/src/ShardedClient.php @@ -160,9 +160,9 @@ public function getGetOperation(string $key): RequestOp * @inheritDoc * @throws \Flexihash\Exception */ - public function getPutOperation(string $key, string $value): RequestOp + public function getPutOperation(string $key, string $value, int $leaseId = 0): RequestOp { - return $this->getClientFromKey($key)->getPutOperation($key, $value); + return $this->getClientFromKey($key)->getPutOperation($key, $value, $leaseId); } /** From 9ab9e1cc4bb756ec1bd956e1b275e5a63dc15f07 Mon Sep 17 00:00:00 2001 From: Jan Krpes Date: Fri, 1 May 2020 11:17:51 +0200 Subject: [PATCH 13/13] Renamed requestIf to txnRequest --- src/Client.php | 7 +++---- src/ClientInterface.php | 3 +-- src/ShardedClient.php | 4 ++-- 3 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/Client.php b/src/Client.php index 5137403..d157d9d 100644 --- a/src/Client.php +++ b/src/Client.php @@ -192,7 +192,7 @@ public function putIf(string $key, string $value, $compareValue, bool $returnNew $compare = $this->getCompareForIf($key, $compareValue); $failOperation = $this->getFailOperation($key, $returnNewValueOnFail); - $response = $this->requestIf($key, [$operation], $failOperation, [$compare]); + $response = $this->txnRequest($key, [$operation], $failOperation, [$compare]); return $this->getIfResponse($returnNewValueOnFail, $response); } @@ -212,7 +212,7 @@ public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail $compare = $this->getCompareForIf($key, $compareValue); $failOperation = $this->getFailOperation($key, $returnNewValueOnFail); - $response = $this->requestIf($key, [$operation], $failOperation, [$compare]); + $response = $this->txnRequest($key, [$operation], $failOperation, [$compare]); return $this->getIfResponse($returnNewValueOnFail, $response); } @@ -223,11 +223,10 @@ public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail * @param array $requestOperations operations to perform on success, array of RequestOp objects * @param array|null $failureOperations operations to perform on failure, array of RequestOp objects * @param array $compare array of Compare objects - * @param bool $returnNewValueOnFail * @return TxnResponse * @throws InvalidResponseStatusCodeException */ - public function requestIf(string $key, array $requestOperations, ?array $failureOperations, array $compare): TxnResponse + public function txnRequest(string $key, array $requestOperations, ?array $failureOperations, array $compare): TxnResponse { $client = $this->getKvClient(); diff --git a/src/ClientInterface.php b/src/ClientInterface.php index 9393b93..63ced30 100644 --- a/src/ClientInterface.php +++ b/src/ClientInterface.php @@ -83,11 +83,10 @@ public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail * @param array $requestOperations operations to perform on success, array of RequestOp objects * @param array|null $failureOperations operations to perform on failure, array of RequestOp objects * @param array $compare array of Compare objects - * @param bool $returnNewValueOnFail * @return TxnResponse * @throws InvalidResponseStatusCodeException */ - public function requestIf(string $key, array $requestOperations, ?array $failureOperations, array $compare): TxnResponse; + public function txnRequest(string $key, array $requestOperations, ?array $failureOperations, array $compare): TxnResponse; /** * Get an instance of Compare diff --git a/src/ShardedClient.php b/src/ShardedClient.php index 799767c..7bfb3ec 100644 --- a/src/ShardedClient.php +++ b/src/ShardedClient.php @@ -133,9 +133,9 @@ public function deleteIf(string $key, $compareValue, bool $returnNewValueOnFail * @inheritDoc * @throws \Flexihash\Exception */ - public function requestIf(string $key, array $requestOperations, ?array $failureOperations, array $compare): TxnResponse + public function txnRequest(string $key, array $requestOperations, ?array $failureOperations, array $compare): TxnResponse { - return $this->getClientFromKey($key)->requestIf($key, $requestOperations, $failureOperations, $compare); + return $this->getClientFromKey($key)->txnRequest($key, $requestOperations, $failureOperations, $compare); } /**