Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 37 additions & 8 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use Etcdserverpb\ResponseOp;
use Etcdserverpb\TxnRequest;
use Etcdserverpb\TxnResponse;
use Generator;
use Grpc\ChannelCredentials;
use Mvccpb\KeyValue;

Expand Down Expand Up @@ -129,7 +130,7 @@ public function put(string $key, $value, bool $prevKv = false, int $leaseID = 0,
$request->setLease($leaseID);

/** @var PutResponse $response */
list($response, $status) = $client->Put($request, $this->getMetaData(), $this->getOptions())->wait();
[$response, $status] = $client->Put($request, $this->getMetaData(), $this->getOptions())->wait();
$this->validateStatus($status);

if ($prevKv) {
Expand All @@ -152,7 +153,7 @@ public function get(string $key)
$request->setKey($key);

/** @var RangeResponse $response */
list($response, $status) = $client->Range($request, $this->getMetaData(), $this->getOptions())->wait();
[$response, $status] = $client->Range($request, $this->getMetaData(), $this->getOptions())->wait();
$this->validateStatus($status);

$field = $response->getKvs();
Expand All @@ -164,6 +165,34 @@ public function get(string $key)
return $field[0]->getValue();
}

/**
* Get range of values by key prefix
*
* @param string $prefix
* @return bool|Generator<KeyValue>
* @throws InvalidResponseStatusCodeException
*/
public function getWithPrefix(string $prefix): Generator
{
$client = $this->getKvClient();

$request = new RangeRequest();
$request->setKey($prefix);
$request->setRangeEnd($prefix."\xff");

/** @var RangeResponse $response */
[$response, $status] = $client->Range($request, $this->getMetaData(), $this->getOptions())->wait();
$this->validateStatus($status);

$field = $response->getKvs();

if (count($field) === 0) {
return false;
}

yield from $field;
Comment thread
texnicii marked this conversation as resolved.
Outdated
}

/**
* Delete a key
*
Expand All @@ -179,7 +208,7 @@ public function delete(string $key)
$request->setKey($key);

/** @var DeleteRangeResponse $response */
list($response, $status) = $client->DeleteRange($request, $this->getMetaData(), $this->getOptions())->wait();
[$response, $status] = $client->DeleteRange($request, $this->getMetaData(), $this->getOptions())->wait();
$this->validateStatus($status);

if ($response->getDeleted() > 0) {
Expand Down Expand Up @@ -242,7 +271,7 @@ public function getLeaseID(int $ttl)
$leaseRequest->setTTL($ttl);

/** @var LeaseGrantResponse $response */
list($response, $status) = $lease->LeaseGrant($leaseRequest, $this->getMetaData())->wait();
[$response, $status] = $lease->LeaseGrant($leaseRequest, $this->getMetaData())->wait();
$this->validateStatus($status);

return (int)$response->getID();
Expand All @@ -260,7 +289,7 @@ public function revokeLeaseID(int $leaseID)
$leaseRequest = new LeaseRevokeRequest();
$leaseRequest->setID($leaseID);

list(, $status) = $lease->LeaseRevoke($leaseRequest, $this->getMetaData())->wait();
[, $status] = $lease->LeaseRevoke($leaseRequest, $this->getMetaData())->wait();
$this->validateStatus($status);
}

Expand Down Expand Up @@ -312,7 +341,7 @@ public function txnRequest(array $requestOperations, ?array $failureOperations,
$request->setFailure($failureOperations);

/** @var TxnResponse $response */
list($response, $status) = $client->Txn($request, $this->getMetaData(), $this->getOptions())->wait();
[$response, $status] = $client->Txn($request, $this->getMetaData(), $this->getOptions())->wait();
$this->validateStatus($status);

return $response;
Expand Down Expand Up @@ -504,7 +533,7 @@ protected function getAuthenticationToken(): string
$request->setPassword($this->password);

/** @var AuthenticateResponse $response */
list($response, $status) = $client->Authenticate($request, [], $this->getOptions())->wait();
[$response, $status] = $client->Authenticate($request, [], $this->getOptions())->wait();
$this->validateStatus($status);

$this->token = $response->getToken();
Expand Down Expand Up @@ -604,4 +633,4 @@ protected function getFailOperation(string $key, bool $returnNewValueOnFail)

return $failOperation;
}
}
}
13 changes: 12 additions & 1 deletion src/ClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use Etcdserverpb\RequestOp;
use Etcdserverpb\TxnResponse;
use Exception;
use Generator;
use Mvccpb\KeyValue;

/**
* Interface ClientInterface
Expand Down Expand Up @@ -44,6 +46,15 @@ public function put(string $key, $value, bool $prevKv = false, int $leaseID = 0,
*/
public function get(string $key);

/**
* Get range of values by key prefix
*
* @param string $prefix
* @return bool|Generator<KeyValue>
* @throws InvalidResponseStatusCodeException
*/
public function getWithPrefix(string $prefix);
Comment thread
texnicii marked this conversation as resolved.
Outdated

/**
* Delete a key
*
Expand Down Expand Up @@ -172,4 +183,4 @@ public function refreshLease(int $leaseID);
* ]
*/
public function getResponses(TxnResponse $txnResponse, ?string $type = null, bool $simpleArray = false): array;
}
}
11 changes: 10 additions & 1 deletion src/FailoverClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ public function get(string $key)
return $this->callClientMethod(__FUNCTION__, false, $key);
}

/**
* @inheritDoc
* @throws NoClientAvailableException
*/
public function getWithPrefix(string $prefix)
{
return $this->callClientMethod(__FUNCTION__, false, $prefix);
}

/**
* @inheritDoc
* @throws NoClientAvailableException
Expand Down Expand Up @@ -321,4 +330,4 @@ protected function callClientMethod(string $name, bool $isLocalCall, ...$argumen
}
}
}
}
}