Skip to content

Commit

Permalink
chore: update get responses (#198)
Browse files Browse the repository at this point in the history
* chore: improve storage client configs and get responses

* chore: renaming integer -> int and double -> float

* chore: update storage example

* chore: fix naming around data types

* chore: remove unneeded value from samples array

* chore: StorageGetFound no longer accepts null grpc response
  • Loading branch information
pgautier404 authored Jul 9, 2024
1 parent 0f41ab1 commit 18a185a
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 115 deletions.
42 changes: 27 additions & 15 deletions examples/storage-example.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
require "vendor/autoload.php";

use Momento\Auth\CredentialProvider;
use Momento\Config\Configurations\StorageLaptop;
use Momento\Config\Configurations\Storage\Laptop;
use Momento\Logging\StderrLoggerFactory;
use Psr\Log\LoggerInterface;
use Momento\Storage\PreviewStorageClient;
use Momento\Storage\StorageOperationTypes\StorageValueType;

$STORE_NAME = uniqid("php-storage-example-");
$VALUES = [
"float" => 123.456,
"str"=> "StringValue",
"int" => 123,
"double" => 123.456,
"fakeout" => "123"
"int" => 123
];

// Setup
$authProvider = CredentialProvider::fromEnvironmentVariable("MOMENTO_AUTH_TOKEN");
$configuration = StorageLaptop::latest(new StderrLoggerFactory());
$configuration = Laptop::latest(new StderrLoggerFactory());
$client = new PreviewStorageClient($configuration, $authProvider);
$logger = $configuration->getLoggerFactory()->getLogger("ex:");

Expand Down Expand Up @@ -72,8 +71,18 @@ function deleteStore(string $storeName, LoggerInterface $logger, PreviewStorageC

// Set
foreach ($VALUES as $key => $value) {
$logger->info("Setting key: '$key' to value: '$value', type = " . get_class($value) . "\n");
$response = $client->set($STORE_NAME, $key, $value);
$logger->info("Setting key: '$key' in '$STORE_NAME' to value: '$value', type = " . gettype($value) . "\n");
if (is_int($value)) {
$response = $client->putInt($STORE_NAME, $key, $value);
} elseif (is_float($value)) {
$response = $client->putFloat($STORE_NAME, $key, $value);
} elseif (is_string($value)) {
$response = $client->putString($STORE_NAME, $key, $value);
} else {
$logger->info("Unsupported value type: " . get_class($value) . "\n");
deleteStore($STORE_NAME, $logger, $client);
exit(1);
}
if ($response->asSuccess()) {
$logger->info("SUCCESS\n");
} elseif ($response->asError()) {
Expand All @@ -87,19 +96,22 @@ function deleteStore(string $storeName, LoggerInterface $logger, PreviewStorageC
foreach ($VALUES as $key => $value) {
$logger->info("Getting value for key: '$key'\n");
$response = $client->get($STORE_NAME, $key);
if ($response->asSuccess()) {
if ($found = $response->asFound()) {
$logger->info("SUCCESS\n");
$valueType = $response->asSuccess()->type();
$valueType = $found->type();
if ($valueType == StorageValueType::STRING) {
print("Got string value: " . $response->asSuccess()->tryGetString() . "\n");
} elseif ($valueType == StorageValueType::INTEGER) {
print("Got integer value: " . $response->asSuccess()->tryGetInteger() . "\n");
} elseif ($valueType == StorageValueType::DOUBLE) {
print("Got double value: " . $response->asSuccess()->tryGetDouble() . "\n");
print("Got string value: " . $found->valueString() . "\n");
} elseif ($valueType == StorageValueType::INT) {
print("Got integer value: " . $found->valueInt() . "\n");
} elseif ($valueType == StorageValueType::FLOAT) {
print("Got double value: " . $found->valueFloat() . "\n");
} elseif ($valueType == StorageValueType::BYTES) {
// This case is not expected in this example as PHP doesn't have a native byte type
print("Got bytes value: " . $response->asSuccess()->tryGetBytes() . "\n");
print("Got bytes value: " . $found->valueBytes() . "\n");
}
// Raw value is also available, and is being pulled directly from the response without
// casting to Found. All other `valueXYZ()` methods can also be called directly on the response.
print("Raw value: " . $response->value() . "\n");
} elseif ($response->asError()) {
$logger->info("Error getting key: " . $response->asError()->message() . "\n");
deleteStore($STORE_NAME, $logger, $client);
Expand Down
10 changes: 10 additions & 0 deletions src/Cache/CacheOperationTypes/CacheOperationTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,16 @@ protected function isNotStored(): bool
return get_class($this) == "{$this->baseType}NotStored";
}

protected function isFound(): bool
{
return get_class($this) == "{$this->baseType}Found";
}

protected function isNotFound(): bool
{
return get_class($this) == "{$this->baseType}NotFound";
}

protected function shortValue(string $value): string
{
if (strlen($value) <= $this->valueSubstringLength) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);

namespace Momento\Config\Configurations;
namespace Momento\Config\Configurations\Storage;

use Momento\Config\StorageConfiguration;
use Momento\Config\Transport\StaticStorageGrpcConfiguration;
Expand All @@ -14,17 +14,17 @@
* in the same region as the Momento service. It has more aggressive timeouts and retry behavior
* than the StorageLaptop config.
*/
class StorageInRegion extends StorageConfiguration
class InRegion extends StorageConfiguration
{
/**
* Provides the latest recommended configuration for an InRegion environment.
* This configuration may change in future releases to take advantage of
* improvements we identify for default configurations.
*
* @param ILoggerFactory|null $loggerFactory
* @return StorageInRegion
* @return InRegion
*/
public static function latest(?ILoggerFactory $loggerFactory = null): StorageInRegion
public static function latest(?ILoggerFactory $loggerFactory = null): InRegion
{
return self::v1($loggerFactory);
}
Expand All @@ -36,9 +36,9 @@ public static function latest(?ILoggerFactory $loggerFactory = null): StorageInR
* TODO: this is marked private now to hide it from customers until we're ready for a v1
*
* @param ILoggerFactory|null $loggerFactory
* @return StorageInRegion
* @return InRegion
*/
private static function v1(?ILoggerFactory $loggerFactory = null): StorageInRegion
private static function v1(?ILoggerFactory $loggerFactory = null): InRegion
{
$loggerFactory = $loggerFactory ?? new NullLoggerFactory();
$grpcConfig = new StaticStorageGrpcConfiguration(1100);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
declare(strict_types=1);

namespace Momento\Config\Configurations;
namespace Momento\Config\Configurations\Storage;

use Momento\Config\StorageConfiguration;
use Momento\Config\Transport\StaticStorageGrpcConfiguration;
Expand All @@ -13,15 +13,15 @@
* StorageLaptop config provides defaults suitable for a medium-to-high-latency dev environment. Permissive timeouts, retries, and
* relaxed latency and throughput targets.
*/
class StorageLaptop extends StorageConfiguration
class Laptop extends StorageConfiguration
{
/**
* Provides the latest recommended configuration for a Laptop environment.
*
* @param ILoggerFactory|null $loggerFactory
* @return StorageLaptop
* @return Laptop
*/
public static function latest(?ILoggerFactory $loggerFactory = null): StorageLaptop
public static function latest(?ILoggerFactory $loggerFactory = null): Laptop
{
return self::v1($loggerFactory);
}
Expand All @@ -33,13 +33,13 @@ public static function latest(?ILoggerFactory $loggerFactory = null): StorageLap
* TODO: this is marked private now to hide it from customers until we're ready for a v1
*
* @param ILoggerFactory|null $loggerFactory
* @return StorageLaptop
* @return Laptop
*/
private static function v1(?ILoggerFactory $loggerFactory = null): StorageLaptop
private static function v1(?ILoggerFactory $loggerFactory = null): Laptop
{
$loggerFactory = $loggerFactory ?? new NullLoggerFactory();
$grpcConfig = new StaticStorageGrpcConfiguration(5000);
$transportStrategy = new StaticStorageTransportStrategy($grpcConfig, $loggerFactory, self::$maxIdleMillis);
return new StorageLaptop($loggerFactory, $transportStrategy);
return new Laptop($loggerFactory, $transportStrategy);
}
}
21 changes: 10 additions & 11 deletions src/Storage/Internal/StorageDataClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,17 @@
use Grpc\UnaryCall;
use Momento\Auth\ICredentialProvider;
use Momento\Cache\CacheOperationTypes\ResponseFuture;
use Momento\Cache\Errors\CacheNotFoundError;
use Momento\Cache\Errors\ItemNotFoundError;
use Momento\Cache\Errors\SdkError;
use Momento\Cache\Errors\StoreNotFoundError;
use Momento\Cache\Errors\UnknownError;
use Momento\Config\IStorageConfiguration;
use Momento\Storage\StorageOperationTypes\StorageDeleteResponse;
use Momento\Storage\StorageOperationTypes\StorageDeleteError;
use Momento\Storage\StorageOperationTypes\StorageDeleteSuccess;
use Momento\Storage\StorageOperationTypes\StorageGetFound;
use Momento\Storage\StorageOperationTypes\StorageGetNotFound;
use Momento\Storage\StorageOperationTypes\StorageGetResponse;
use Momento\Storage\StorageOperationTypes\StorageGetError;
use Momento\Storage\StorageOperationTypes\StorageGetSuccess;
use Momento\Storage\StorageOperationTypes\StoragePutResponse;
use Momento\Storage\StorageOperationTypes\StoragePutError;
use Momento\Storage\StorageOperationTypes\StoragePutSuccess;
Expand Down Expand Up @@ -89,9 +88,9 @@ public function putString(string $storeName, string $key, string $value): Respon
* @param int $value
* @return ResponseFuture<StoragePutResponse>
*/
public function putInteger(string $storeName, string $key, int $value): ResponseFuture
public function putInt(string $storeName, string $key, int $value): ResponseFuture
{
return $this->_put($storeName, $key, $value, StorageValueType::INTEGER);
return $this->_put($storeName, $key, $value, StorageValueType::INT);
}

/**
Expand All @@ -100,9 +99,9 @@ public function putInteger(string $storeName, string $key, int $value): Response
* @param float $value
* @return ResponseFuture<StoragePutResponse>
*/
public function putDouble(string $storeName, string $key, float $value): ResponseFuture
public function putFloat(string $storeName, string $key, float $value): ResponseFuture
{
return $this->_put($storeName, $key, $value, StorageValueType::DOUBLE);
return $this->_put($storeName, $key, $value, StorageValueType::FLOAT);
}

/**
Expand All @@ -129,9 +128,9 @@ private function _put(string $storeName, string $key, $value, string $type): Res
validateStoreName($storeName);
validateNullOrEmpty($key, "Key");
$storeValue = new _StoreValue();
if ($type == StorageValueType::INTEGER) {
if ($type == StorageValueType::INT) {
$storeValue->setIntegerValue($value);
} elseif ($type == StorageValueType::DOUBLE) {
} elseif ($type == StorageValueType::FLOAT) {
$storeValue->setDoubleValue($value);
} elseif ($type == StorageValueType::STRING) {
$storeValue->setStringValue($value);
Expand Down Expand Up @@ -194,13 +193,13 @@ function () use ($call): StorageGetResponse {
try {
$response = $this->processCall($call);
} catch (ItemNotFoundError $e) {
return new StorageGetSuccess();
return new StorageGetNotFound();
} catch (SdkError $e) {
return new StorageGetError($e);
} catch (Exception $e) {
return new StorageGetError(new UnknownError($e->getMessage(), 0, $e));
}
return new StorageGetSuccess($response);
return new StorageGetFound($response);
}
);
}
Expand Down
24 changes: 12 additions & 12 deletions src/Storage/PreviewStorageClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,9 @@ public function putBytes(string $storeName, string $key, string $value): Storage
* we implicitly wait for completion of the request on destruction of the
* response future.
*/
public function putIntegerAsync(string $storeName, string $key, int $value): ResponseFuture
public function putIntAsync(string $storeName, string $key, int $value): ResponseFuture
{
return $this->getNextDataClient()->putInteger($storeName, $key, $value);
return $this->getNextDataClient()->putInt($storeName, $key, $value);
}

/**
Expand All @@ -310,13 +310,13 @@ public function putIntegerAsync(string $storeName, string $key, int $value): Res
* }
* </code>
*/
public function putInteger(string $storeName, string $key, int $value): StoragePutResponse
public function putInt(string $storeName, string $key, int $value): StoragePutResponse
{
return $this->putIntegerAsync($storeName, $key, $value)->wait();
return $this->putIntAsync($storeName, $key, $value)->wait();
}

/**
* Put the double value in the store.
* Put the float value in the store.
*
* @param string $storeName Name of the store in which to put the value.
* @param string $key The key to put.
Expand All @@ -338,13 +338,13 @@ public function putInteger(string $storeName, string $key, int $value): StorageP
* we implicitly wait for completion of the request on destruction of the
* response future.
*/
public function putDoubleAsync(string $storeName, string $key, float $value): ResponseFuture
public function putFloatAsync(string $storeName, string $key, float $value): ResponseFuture
{
return $this->getNextDataClient()->putDouble($storeName, $key, $value);
return $this->getNextDataClient()->putFloat($storeName, $key, $value);
}

/**
* Put the double value in the store.
* Put the float value in the store.
*
* @param string $storeName Name of the store in which to put the value.
* @param string $key The key to put.
Expand All @@ -360,9 +360,9 @@ public function putDoubleAsync(string $storeName, string $key, float $value): Re
* }
* </code>
*/
public function putDouble(string $storeName, string $key, float $value): StoragePutResponse
public function putFloat(string $storeName, string $key, float $value): StoragePutResponse
{
return $this->putDoubleAsync($storeName, $key, $value)->wait();
return $this->putFloatAsync($storeName, $key, $value)->wait();
}

/**
Expand All @@ -385,9 +385,9 @@ public function putDouble(string $storeName, string $key, float $value): Storage
* $type = $hit->type();
* if ($type == StorageValueType::STRING) {
* $value = $hit->tryGetString();
* } elseif ($type == StorageValueType::INTEGER) {
* } elseif ($type == StorageValueType::INT) {
* $value = $hit->tryGetInteger();
* } elseif ($type == StorageValueType::DOUBLE) {
* } elseif ($type == StorageValueType::FLOAT) {
* $value = $hit->tryGetDouble();
* }
* } elseif ($error = $response->asError()) {
Expand Down
Loading

0 comments on commit 18a185a

Please sign in to comment.