Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Shared key flattening #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
93 changes: 93 additions & 0 deletions lib/Doctrine/KeyValueStore/Storage/AbstractStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\KeyValueStore\Storage;

abstract class AbstractStorage implements Storage
{
/**
* {@inheritDoc}
*/
abstract public function supportsPartialUpdates();

/**
* {@inheritDoc}
*/
abstract public function supportsCompositePrimaryKeys();

/**
* {@inheritDoc}
*/
abstract public function requiresCompositePrimaryKeys();

/**
* {@inheritDoc}
*/
abstract public function insert($storageName, $key, array $data);

/**
* {@inheritDoc}
*/
abstract public function update($storageName, $key, array $data);

/**
* {@inheritDoc}
*/
abstract public function delete($storageName, $key);

/**
* {@inheritDoc}
*/
abstract public function find($storageName, $key);

/**
* {@inheritDoc}
*/
abstract public function getName();

/**
* Used to flattening keys.
*
* @param string $storageName
* @param string|int|float|bool|array $key
*
* @return string
*/
protected function flattenKey($storageName, $key)
{
if (is_scalar($key)) {
return $storageName . '-' . $key;
}

if ( ! is_array($key)) {
throw new \InvalidArgumentException('The key should be a string or a flat array.');
}

ksort($key);

$hash = $storageName . '-oid:';

foreach ($key as $property => $value) {
$hash .= $property . '=' . $value . ';';
}

return $hash;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class AzureSdkTableStorage implements Storage, RangeQueryStorage
class AzureSdkTableStorage extends AbstractStorage implements RangeQueryStorage
{
/**
* @var \WindowsAzure\Table\TableRestProxy
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/KeyValueStore/Storage/CassandraStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*
* @uses https://github.com/datastax/php-driver
*/
class CassandraStorage implements Storage
class CassandraStorage extends AbstractStorage
{
/**
* @var \Cassandra\Session
Expand Down
27 changes: 1 addition & 26 deletions lib/Doctrine/KeyValueStore/Storage/CouchDbStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*
* @author Emanuele Minotto <minottoemanuele@gmail.com>
*/
final class CouchDbStorage implements Storage
final class CouchDbStorage extends AbstractStorage
{
/**
* @var CouchDBClient
Expand Down Expand Up @@ -112,29 +112,4 @@ public function getName()
{
return 'couchdb';
}

/**
* @param string $storageName
* @param array|string $key
*
* @return string
*/
private function flattenKey($storageName, $key)
{
$finalKey = $storageName . '-';

if (is_string($key)) {
return $finalKey . $key;
}

if ( ! is_array($key)) {
throw new \InvalidArgumentException('The key should be a string or a flat array.');
}

foreach ($key as $property => $value) {
$finalKey .= sprintf('%s:%s-', $property, $value);
}

return $finalKey;
}
}
2 changes: 1 addition & 1 deletion lib/Doctrine/KeyValueStore/Storage/CouchbaseStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* @author Simon Schick <simonsimcity@gmail.com>
*/
class CouchbaseStorage implements Storage
class CouchbaseStorage extends AbstractStorage
{
/**
* @var \Couchbase
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/KeyValueStore/Storage/DBALStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class DBALStorage implements Storage
class DBALStorage extends AbstractStorage
{
private $conn;
private $table;
Expand Down
16 changes: 1 addition & 15 deletions lib/Doctrine/KeyValueStore/Storage/DoctrineCacheStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class DoctrineCacheStorage implements Storage
class DoctrineCacheStorage extends AbstractStorage
{
/**
* @var Doctrine\Common\Cache\Cache
Expand Down Expand Up @@ -60,20 +60,6 @@ public function requiresCompositePrimaryKeys()
return false;
}

private function flattenKey($storageName, $key)
{
if ( ! $this->supportsCompositeKeys) {
return $storageName . '-' . $key;
}

$hash = $storageName . '-oid:';
ksort($key);
foreach ($key as $property => $value) {
$hash .= $property . '=' . $value . ';';
}
return $hash;
}

public function insert($storageName, $key, array $data)
{
$key = $this->flattenKey($storageName, $key);
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/KeyValueStore/Storage/DynamoDbStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
*
* @author Stan Lemon <stosh1985@gmail.com>
*/
class DynamoDbStorage implements Storage
class DynamoDbStorage extends AbstractStorage
{
/**
* @var \Aws\DynamoDb\DynamoDbClient
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/KeyValueStore/Storage/MongoDbStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
*
* @author Markus Bachmann <markus.bachmann@bachi.biz>
*/
class MongoDbStorage implements Storage
class MongoDbStorage extends AbstractStorage
{
/**
* @var \Mongo
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/KeyValueStore/Storage/RedisStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/**
* @author Marcel Araujo <admin@marcelaraujo.me>
*/
class RedisStorage implements Storage
class RedisStorage extends AbstractStorage
{
/**
* @var \Redis
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/KeyValueStore/Storage/RiakStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
/**
* @author Markus Bachmann <markus.bachmann@bachi.biz>
*/
class RiakStorage implements Storage
class RiakStorage extends AbstractStorage
{
/**
* @var \Riak\Client
Expand Down
2 changes: 1 addition & 1 deletion lib/Doctrine/KeyValueStore/Storage/SimpleDbStorage.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
*
* @author Stan Lemon <stosh1985@gmail.com>
*/
class SimpleDbStorage implements Storage
class SimpleDbStorage extends AbstractStorage
{
/**
* @var \Aws\SimpleDb\SimpleDbClient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*
* @deprecated This class is deprecated and will be removed in 2.0, use the AzureSdkTableStorage instead.
*/
class WindowsAzureTableStorage implements Storage, RangeQueryStorage
class WindowsAzureTableStorage extends AbstractStorage implements RangeQueryStorage
{
const WINDOWS_AZURE_TABLE_BASEURL = 'https://%s.table.core.windows.net';

Expand All @@ -61,7 +61,7 @@ class WindowsAzureTableStorage implements Storage, RangeQueryStorage
</entry>';

const XML_TEMPLATE_TABLE = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<entry xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
<title />
<updated></updated>
<author>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class CassandraTest extends \PHPUnit_Framework_TestCase

protected function setUp()
{
$cluster = Cassandra::cluster()->build();
$cluster = Cassandra::cluster()->build();
$this->session = $cluster->connect();

try {
Expand Down
80 changes: 80 additions & 0 deletions tests/Doctrine/Tests/KeyValueStore/Storage/AbstractStorageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

/*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the MIT license. For more information, see
* <http://www.doctrine-project.org>.
*/

namespace Doctrine\Tests\KeyValueStore\Storage;

use Doctrine\KeyValueStore\Storage\AbstractStorage;
use PHPUnit_Framework_TestCase;
use ReflectionClass;

/**
* @covers \Doctrine\KeyValueStore\Storage\AbstractStorage
*/
class AbstractStorageTest extends PHPUnit_Framework_TestCase
{
/**
* @var AbstractStorage
*/
protected $object;

public function setUp()
{
$this->object = $this->getMockForAbstractClass(AbstractStorage::class);
}

/**
* @dataProvider keysDataProvider
*/
public function testFlattenKey($storageName, $key, $expected)
{
$reflectionClass = new ReflectionClass($this->object);
$method = $reflectionClass->getMethod('flattenKey');
$method->setAccessible(true);

$hash = $method->invokeArgs($this->object, [$storageName, $key]);

$this->assertInternalType('string', $hash);
$this->assertSame($expected, $hash);
}

/**
* @return array
*/
public function keysDataProvider()
{
return [
// key: string
['foo', 'bar', 'foo-bar'],
['foo', 0.0, 'foo-0'],
['foo', 0.05, 'foo-0.05'],
['foo', 1, 'foo-1'],
['foo', 1.0, 'foo-1'],
['foo', 1.05, 'foo-1.05'],
['foo', false, 'foo-'],
['foo', true, 'foo-1'],
// key: array
['foo', ['bar', 'test'], 'foo-oid:0=bar;1=test;'],
['foo', ['bar', 0.0], 'foo-oid:0=bar;1=0;'],
['foo', ['test' => 3, 'bar' => 5], 'foo-oid:bar=5;test=3;'],
['foo', ['test' => 3.1, 'bar' => 5.0], 'foo-oid:bar=5;test=3.1;'],
['foo', ['test' => true, 'bar' => false], 'foo-oid:bar=;test=1;'],
];
}
}