Skip to content

Commit

Permalink
Merge pull request #7 from HilarioKP/add-rename-has
Browse files Browse the repository at this point in the history
feat: adding rename and has to KeyValueInterface
  • Loading branch information
byjg committed Mar 13, 2024
2 parents 610b1a4 + 3719620 commit 60b9e95
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions .run/Start Backends for Test.run.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<configuration default="false" name="Start Backends for Test" type="docker-deploy" factoryName="docker-compose.yml" server-name="Docker">
<deployment type="docker-compose.yml">
<settings>
<option name="envFilePath" value="" />
<option name="removeVolumesOnComposeDown" value="true" />
<option name="sourceFilePath" value="docker-compose.yml" />
</settings>
Expand Down
10 changes: 10 additions & 0 deletions docs/AwsDynamoDbKeyValue.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,16 @@ $iterator = $dynamodb->getIterator($options);
print_r($iterator->toArray());
```

### Check if a key exists

```php
<?php
$dynamodb = \ByJG\AnyDataset\NoSql\Factory::getInstance('dynamodb://....');
if ($dynamodb->has(1201)) {
echo "exist!";
}
```

## Further reading

- [https://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-dynamodb.html](https://docs.aws.amazon.com/aws-sdk-php/v2/guide/service-dynamodb.html)
Expand Down
17 changes: 17 additions & 0 deletions docs/AwsS3KeyValue.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,22 @@ while (strlen($data) <= $size) {
}
```

## Rename a key

```php
<?php
$s3 = \ByJG\AnyDataset\NoSql\Factory::getInstance('s3://....');
$s3->rename("object_name", "new_object_name");
```

## Check if a key exists

```php
<?php
$s3 = \ByJG\AnyDataset\NoSql\Factory::getInstance('s3://....');
if ($s3->has("object_name")) {
echo "exist!";
}
```
----
[Open source ByJG](http://opensource.byjg.com)
16 changes: 16 additions & 0 deletions src/AwsDynamoDbDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Aws\DynamoDb\DynamoDbClient;
use Aws\Result;
use ByJG\AnyDataset\Core\Exception\NotImplementedException;
use ByJG\AnyDataset\Core\GenericIterator;
use ByJG\AnyDataset\Lists\ArrayDataset;
use ByJG\Serializer\SerializerObject;
Expand Down Expand Up @@ -115,6 +116,10 @@ protected function extractRecord($awsResult) {
$raw = $awsResult["Item"];
}

if (empty($raw)) {
return null;
}

array_walk($raw, function($val, $key) use (&$result) {
$value = null;
if (isset($val["N"])) {
Expand Down Expand Up @@ -233,4 +238,15 @@ public static function schema()
{
return ["dynamo", "dynamodb"];
}

public function rename($oldKey, $newKey)
{
throw new NotImplementedException("DynamoDB cannot rename");
}

public function has($key, $options = [])
{
$value = $this->get($key, $options);
return !empty($value);
}
}
17 changes: 17 additions & 0 deletions src/AwsS3Driver.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,4 +214,21 @@ public static function schema()
{
return "s3";
}

public function rename($oldKey, $newKey)
{
$data = [
'Bucket' => $this->bucket,
'Key' => $newKey,
'CopySource' => "{$this->bucket}/{$oldKey}",
];

$this->s3Client->copyObject($data);
$this->remove($oldKey);
}

public function has($key, $options = [])
{
return $this->s3Client->doesObjectExistV2($this->bucket, $key, false, $options);
}
}
10 changes: 10 additions & 0 deletions src/CloudflareKV.php
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,14 @@ public static function schema()
{
return "kv";
}

public function rename($oldKey, $newKey)
{
// TODO: Implement rename() method.
}

public function has($key, $options = [])
{
// TODO: Implement has() method.
}
}
4 changes: 4 additions & 0 deletions src/KeyValueInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ interface KeyValueInterface
*/
public function getIterator($options = []);

public function has($key, $options = []);

public function get($key, $options = []);

public function put($key, $value, $options = []);
Expand All @@ -35,4 +37,6 @@ public function removeBatch($keys, $options = []);

public function getDbConnection();

public function rename($oldKey, $newKey);

}
4 changes: 4 additions & 0 deletions tests/AwsDynamoDbDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,12 @@ public function testDynamoDbOperations()
$this->assertEquals(0, $iterator->count());

// Add an element
$this->assertFalse($this->object->has(1, $this->options));
$this->assertFalse($this->object->has(2, $this->options));
$this->object->put(1, ["Name" => "John", "SurName" => "Doe"], $this->options);
$this->object->put(2, ["Name" => "Jane", "SurName" => "Smith"], $this->options);
$this->assertTrue($this->object->has(1, $this->options));
$this->assertTrue($this->object->has(2, $this->options));

// Check new elements
$iterator = $this->object->getIterator($this->queryOptions);
Expand Down
59 changes: 59 additions & 0 deletions tests/AwsS3DriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,63 @@ public function testGetChunk()
$this->assertEquals(str_repeat("1", 256), $part2);
$this->assertEquals(str_repeat("2", 250), $part3);
}

public function testRename()
{
if (empty($this->object)) {
$this->markTestIncomplete("In order to test S3 you must define S3_CONNECTION");
}

// Get current bucket
$iterator = $this->object->getIterator();
$currentCount = $iterator->count();

// Add an element
$this->object->put("KEY", "value");

// Check new elements
$iterator = $this->object->getIterator();
$this->assertEquals($currentCount + 1, $iterator->count());

// Get elements
$elem1 = $this->object->get("KEY");
$this->assertEquals("value", $elem1);
$this->assertFalse($this->object->has("NEW_KEY"));

// Rename elements
$this->object->rename("KEY", "NEW_KEY");
$elem1 = $this->object->get("NEW_KEY");
$this->assertEquals("value", $elem1);
$this->assertFalse($this->object->has("KEY"));

// Remove elements
$this->object->remove("NEW_KEY");

// Check new elements
$iterator = $this->object->getIterator();
$this->assertEquals($currentCount, $iterator->count());

}

public function testHas()
{
if (empty($this->object)) {
$this->markTestIncomplete("In order to test S3 you must define S3_CONNECTION");
}

// Assert key not exists
$this->assertFalse($this->object->has("KEY"));

// Add an element
$this->object->put("KEY", "value");

// Assert key exists
$this->assertTrue($this->object->has("KEY"));

// Remove an element
$this->object->remove("KEY");

// Assert key not exists
$this->assertFalse($this->object->has("KEY"));
}
}

0 comments on commit 60b9e95

Please sign in to comment.