Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add missing v1 ops #99

Merged
merged 6 commits into from
Mar 17, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
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
82 changes: 80 additions & 2 deletions src/Cache/CacheOperationTypes/CacheOperationTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Cache_client\_DictionaryGetResponse;
use Cache_client\_DictionaryIncrementResponse;
use Cache_client\_GetResponse;
use Cache_client\_KeysExistResponse;
use Cache_client\_ListFetchResponse;
use Cache_client\_ListLengthResponse;
use Cache_client\_ListPopBackResponse;
Expand Down Expand Up @@ -422,8 +423,7 @@ public function __toString()
}
}

class CacheSetIfNotExistsResponseNotStored extends CacheSetIfNotExistsResponse {
}
class CacheSetIfNotExistsResponseNotStored extends CacheSetIfNotExistsResponse {}

class CacheSetIfNotExistsResponseError extends CacheSetIfNotExistsResponse
{
Expand Down Expand Up @@ -459,6 +459,84 @@ class CacheDeleteResponseError extends CacheDeleteResponse
use ErrorBody;
}

abstract class CacheKeysExistResponse extends ResponseBase
{
public function asSuccess() : CacheKeysExistResponseSuccess|null
{
if ($this->isSuccess()) {
return $this;
}
return null;
}

public function asError() : CacheKeysExistResponseError|null
{
if ($this->isError()) {
return $this;
}
return null;
}
}

class CacheKeysExistResponseSuccess extends CacheKeysExistResponse
{
private array $values = [];

public function __construct(_KeysExistResponse $response) {
parent::__construct();
foreach ($response->getExists()->getIterator() as $value) {
$this->values[] = (bool)$value;
}
}

public function exists() : array {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, up to you: i am imagining that for most of the SDKs we will want to provide a dictionary accessor for this. If we do it as an array like this, it's useful if the caller wants to do a .every or .all check, but otherwise they have to loop over this and their original fields list in parallel to check values and it's kinda janky.

I feel like as an end user it's a much nicer experience if I can get back a dictionary with the fields as keys.

we can always add this later, so definitely not a blocker.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with that, but I was going off the SDK API spec and it explicitly calls for a list of booleans. For that reason, the golang SDK is returning []bool for this too. I totally agree that a dictionary is a better UX choice. I'm happy to change this up to return a dictionary (which because this is PHP will still be called and typed "an array") but we should think about adding existsMap or similar to the gloang SDK as well. And we should obviously correct the spec.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need the list of booleans. but i think adding a map too is nice. you can see how we did it in the .NET SDK if you like. And yes if you wouldn't mind updating the spec that would be great, thank you!!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and adding it to go at some point would be great too but it's an additive change so we can do it whenever IMO

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a "dictionary" accessor in 1a803f9 and updated the spec to reference existsDictionaryString and existsDictionaryBytes accessors. @cprice404 I opted to omit a "Bool" suffix on those accessors but feel free to change that if you prefer the more verbose version. Because PHP doesn't differentiate, I just added the accessor in this SDK as existsDictionary.

return $this->values;
}
}

class CacheKeysExistResponseError extends CacheKeysExistResponse
{
use ErrorBody;
}

abstract class CacheKeyExistsResponse extends ResponseBase
{
public function asSuccess() : CacheKeyExistsResponseSuccess|null
{
if ($this->isSuccess()) {
return $this;
}
return null;
}

public function asError() : CacheKeyExistsResponseError|null
{
if ($this->isError()) {
return $this;
}
return null;
}
}

class CacheKeyExistsResponseSuccess extends CacheKeyExistsResponse
{
private bool $value;

public function __construct(bool $response) {
parent::__construct();
$this->value = $response;
}

public function exists() : bool {
return $this->value;
}
}

class CacheKeyExistsResponseError extends CacheKeyExistsResponse
{
use ErrorBody;
}

abstract class CacheListFetchResponse extends ResponseBase
{
public function asHit(): CacheListFetchResponseHit|null
Expand Down
2 changes: 0 additions & 2 deletions src/Cache/Errors/Errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

namespace Momento\Cache\Errors;

use Grpc\Status;

abstract class MomentoErrorCode
{
/// Invalid argument passed to Momento client
Expand Down
2 changes: 1 addition & 1 deletion src/Cache/Interceptors/AgentInterceptor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
class AgentInterceptor extends Interceptor
{

private const AGENT = "php:0.3.0";
private const AGENT = "php:0.5.0";
private bool $isFirstRequest = true;

public function interceptUnaryUnary($method, $argument, $deserialize, $continuation, array $metadata = [], array $options = [])
Expand Down
15 changes: 13 additions & 2 deletions src/Cache/SimpleCacheClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace Momento\Cache;

use Cache_client\_KeysExistResponse;
use Momento\Auth\ICredentialProvider;
use Momento\Cache\CacheOperationTypes\CacheDeleteResponse;
use Momento\Cache\CacheOperationTypes\CacheDictionaryDeleteResponse;
use Momento\Cache\CacheOperationTypes\CacheDictionaryFetchResponse;
use Momento\Cache\CacheOperationTypes\CacheDictionaryGetFieldResponse;
use Momento\Cache\CacheOperationTypes\CacheDictionaryGetFieldsResponse;
Expand All @@ -15,6 +15,8 @@
use Momento\Cache\CacheOperationTypes\CacheDictionarySetFieldResponse;
use Momento\Cache\CacheOperationTypes\CacheDictionarySetFieldsResponse;
use Momento\Cache\CacheOperationTypes\CacheGetResponse;
use Momento\Cache\CacheOperationTypes\CacheKeyExistsResponse;
use Momento\Cache\CacheOperationTypes\CacheKeysExistResponse;
use Momento\Cache\CacheOperationTypes\CacheListFetchResponse;
use Momento\Cache\CacheOperationTypes\CacheListLengthResponse;
use Momento\Cache\CacheOperationTypes\CacheListPopBackResponse;
Expand All @@ -23,7 +25,6 @@
use Momento\Cache\CacheOperationTypes\CacheListPushFrontResponse;
use Momento\Cache\CacheOperationTypes\CacheListRemoveValueResponse;
use Momento\Cache\CacheOperationTypes\CacheSetAddElementResponse;
use Momento\Cache\CacheOperationTypes\CacheSetDeleteResponse;
use Momento\Cache\CacheOperationTypes\CacheSetFetchResponse;
use Momento\Cache\CacheOperationTypes\CacheSetIfNotExistsResponse;
use Momento\Cache\CacheOperationTypes\CacheSetRemoveElementResponse;
Expand Down Expand Up @@ -106,6 +107,16 @@ public function delete(string $cacheName, string $key): CacheDeleteResponse
return $this->dataClient->delete($cacheName, $key);
}

public function keysExist(string $cacheName, array $keys): CacheKeysExistResponse
{
return $this->dataClient->keysExist($cacheName, $keys);
}

public function keyExists(string $cacheName, string $key): CacheKeyExistsResponse
{
return $this->dataClient->keyExists($cacheName, $key);
}

public function listFetch(string $cacheName, string $listName): CacheListFetchResponse
{
return $this->dataClient->listFetch($cacheName, $listName);
Expand Down
51 changes: 36 additions & 15 deletions src/Cache/_ScsDataClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,23 @@

use Cache_client\_DeleteRequest;
use Cache_client\_DictionaryDeleteRequest;
use Cache_client\_DictionaryDeleteRequest\All;
use Cache_client\_DictionaryFetchRequest;
use Cache_client\_DictionaryFieldValuePair;
use Cache_client\_DictionaryGetRequest;
use Cache_client\_DictionaryIncrementRequest;
use Cache_client\_DictionarySetRequest;
use Cache_client\_GetRequest;
use Cache_client\_ListEraseRequest;
use Cache_client\_KeysExistRequest;
use Cache_client\_KeysExistResponse;
use Cache_client\_ListFetchRequest;
use Cache_client\_ListLengthRequest;
use Cache_client\_ListPopBackRequest;
use Cache_client\_ListPopFrontRequest;
use Cache_client\_ListPushBackRequest;
use Cache_client\_ListPushFrontRequest;
use Cache_client\_ListRange;
use Cache_client\_ListRemoveRequest;
use Cache_client\_SetDifferenceRequest;
use Cache_client\_SetDifferenceRequest\_Subtrahend;
use Cache_client\_SetDifferenceRequest\_Subtrahend\_Identity;
use Cache_client\_SetDifferenceRequest\_Subtrahend\_Set;
use Cache_client\_SetFetchRequest;
use Cache_client\_SetIfNotExistsRequest;
Expand All @@ -36,9 +34,6 @@
use Momento\Cache\CacheOperationTypes\CacheDeleteResponse;
use Momento\Cache\CacheOperationTypes\CacheDeleteResponseError;
use Momento\Cache\CacheOperationTypes\CacheDeleteResponseSuccess;
use Momento\Cache\CacheOperationTypes\CacheDictionaryDeleteResponse;
use Momento\Cache\CacheOperationTypes\CacheDictionaryDeleteResponseError;
use Momento\Cache\CacheOperationTypes\CacheDictionaryDeleteResponseSuccess;
use Momento\Cache\CacheOperationTypes\CacheDictionaryFetchResponse;
use Momento\Cache\CacheOperationTypes\CacheDictionaryFetchResponseError;
use Momento\Cache\CacheOperationTypes\CacheDictionaryFetchResponseHit;
Expand Down Expand Up @@ -70,9 +65,12 @@
use Momento\Cache\CacheOperationTypes\CacheGetResponseError;
use Momento\Cache\CacheOperationTypes\CacheGetResponseHit;
use Momento\Cache\CacheOperationTypes\CacheGetResponseMiss;
use Momento\Cache\CacheOperationTypes\CacheListEraseResponse;
use Momento\Cache\CacheOperationTypes\CacheListEraseResponseError;
use Momento\Cache\CacheOperationTypes\CacheListEraseResponseSuccess;
use Momento\Cache\CacheOperationTypes\CacheKeyExistsResponse;
use Momento\Cache\CacheOperationTypes\CacheKeyExistsResponseError;
use Momento\Cache\CacheOperationTypes\CacheKeyExistsResponseSuccess;
use Momento\Cache\CacheOperationTypes\CacheKeysExistResponse;
use Momento\Cache\CacheOperationTypes\CacheKeysExistResponseError;
use Momento\Cache\CacheOperationTypes\CacheKeysExistResponseSuccess;
use Momento\Cache\CacheOperationTypes\CacheListFetchResponse;
use Momento\Cache\CacheOperationTypes\CacheListFetchResponseError;
use Momento\Cache\CacheOperationTypes\CacheListFetchResponseHit;
Expand Down Expand Up @@ -100,9 +98,6 @@
use Momento\Cache\CacheOperationTypes\CacheSetAddElementResponse;
use Momento\Cache\CacheOperationTypes\CacheSetAddElementResponseError;
use Momento\Cache\CacheOperationTypes\CacheSetAddElementResponseSuccess;
use Momento\Cache\CacheOperationTypes\CacheSetDeleteResponse;
use Momento\Cache\CacheOperationTypes\CacheSetDeleteResponseError;
use Momento\Cache\CacheOperationTypes\CacheSetDeleteResponseSuccess;
use Momento\Cache\CacheOperationTypes\CacheSetFetchResponse;
use Momento\Cache\CacheOperationTypes\CacheSetFetchResponseError;
use Momento\Cache\CacheOperationTypes\CacheSetFetchResponseHit;
Expand All @@ -122,7 +117,6 @@
use Momento\Cache\Errors\UnknownError;
use Momento\Config\IConfiguration;
use Momento\Requests\CollectionTtl;
use Momento\Requests\CollectionTtlFactory;
use Momento\Utilities\_ErrorConverter;
use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
Expand All @@ -135,11 +129,11 @@
use function Momento\Utilities\validateItems;
use function Momento\Utilities\validateListName;
use function Momento\Utilities\validateOperationTimeout;
use function Momento\Utilities\validateRange;
use function Momento\Utilities\validateSetName;
use function Momento\Utilities\validateTruncateSize;
use function Momento\Utilities\validateTtl;
use function Momento\Utilities\validateValueName;
use function PHPUnit\Framework\isInstanceOf;

class _ScsDataClient implements LoggerAwareInterface
{
Expand Down Expand Up @@ -288,6 +282,33 @@ public function delete(string $cacheName, string $key): CacheDeleteResponse
return new CacheDeleteResponseSuccess();
}

public function keysExist(string $cacheName, array $keys) : CacheKeysExistResponse
{
try {
validateCacheName($cacheName);
$keysExistRequest = new _KeysExistRequest();
$keysExistRequest->setCacheKeys($keys);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to validate $keys before passing them like this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great callout, I'll add that, thanks!

$call = $this->grpcManager->client->KeysExist(
$keysExistRequest, ["cache"=> [$cacheName]], ["timeout" => $this->timeout]
);
$response = $this->processCall($call);
} catch (SdkError $e) {
return new CacheKeysExistResponseError($e);
} catch (Exception $e) {
return new CacheKeysExistResponseError(new UnknownError($e->getMessage()));
}
return new CacheKeysExistResponseSuccess($response);
}

public function keyExists(string $cacheName, string $key) : CacheKeyExistsResponse
{
$response = $this->keysExist($cacheName, [$key]);
if ($response instanceof CacheKeysExistResponseError) {
return new CacheKeyExistsResponseError($response->innerException());
}
return new CacheKeyExistsResponseSuccess($response->asSuccess()->exists()[0]);
}

public function listFetch(string $cacheName, string $listName): CacheListFetchResponse
{
try {
Expand Down
23 changes: 23 additions & 0 deletions src/Config/Configurations/InRegion.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
declare(strict_types=1);

namespace Momento\Config\Configurations;

use Momento\Config\Configuration;
use Momento\Config\Transport\StaticGrpcConfiguration;
use Momento\Config\Transport\StaticTransportStrategy;
use Momento\Logging\ILoggerFactory;
use Momento\Logging\NullLoggerFactory;

class InRegion extends Configuration
{

public static function latest(?ILoggerFactory $loggerFactory = null): Laptop
{
$loggerFactory = $loggerFactory ?? new NullLoggerFactory();
$grpcConfig = new StaticGrpcConfiguration(1100);
$transportStrategy = new StaticTransportStrategy(null, $grpcConfig, $loggerFactory);
return new Laptop($loggerFactory, $transportStrategy);
}

}
4 changes: 1 addition & 3 deletions src/Config/Transport/ITransportStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

namespace Momento\Config\Transport;

use Momento\Config\Transport\IGrpcConfiguration;
use Momento\Logging\ILoggerFactory;
use Monolog\Logger;

interface ITransportStrategy
{
Expand All @@ -18,4 +16,4 @@ public function withLoggerFactory(ILoggerFactory $loggerFactory);
public function withGrpcConfig(IGrpcConfiguration $grpcConfig);

public function withClientTimeout(int $clientTimeout);
}
}
2 changes: 0 additions & 2 deletions src/Config/Transport/StaticTransportStrategy.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

namespace Momento\Config\Transport;

use Momento\Config\Transport\IGrpcConfiguration;
use Momento\Logging\ILoggerFactory;
use Monolog\Logger;
use Psr\Log\LoggerInterface;

class StaticTransportStrategy implements ITransportStrategy
Expand Down
Loading