1 Commits

Author SHA1 Message Date
Emanuele Minotto
b5d8dde073 shared key flattening 2015-12-16 00:03:09 +01:00
15 changed files with 187 additions and 53 deletions

View File

@@ -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;
}
}

View File

@@ -33,7 +33,7 @@ use WindowsAzure\Table\TableRestProxy;
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class AzureSdkTableStorage implements Storage, RangeQueryStorage
class AzureSdkTableStorage extends AbstractStorage implements RangeQueryStorage
{
/**
* @var \WindowsAzure\Table\TableRestProxy

View File

@@ -31,7 +31,7 @@ use Doctrine\KeyValueStore\NotFoundException;
*
* @uses https://github.com/datastax/php-driver
*/
class CassandraStorage implements Storage
class CassandraStorage extends AbstractStorage
{
/**
* @var \Cassandra\Session

View File

@@ -30,7 +30,7 @@ use Doctrine\CouchDB\CouchDBClient;
*
* @author Emanuele Minotto <minottoemanuele@gmail.com>
*/
final class CouchDbStorage implements Storage
final class CouchDbStorage extends AbstractStorage
{
/**
* @var CouchDBClient
@@ -112,29 +112,4 @@ final class CouchDbStorage implements Storage
{
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;
}
}

View File

@@ -25,7 +25,7 @@ use Doctrine\KeyValueStore\NotFoundException;
/**
* @author Simon Schick <simonsimcity@gmail.com>
*/
class CouchbaseStorage implements Storage
class CouchbaseStorage extends AbstractStorage
{
/**
* @var \Couchbase

View File

@@ -30,7 +30,7 @@ use Doctrine\KeyValueStore\NotFoundException;
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class DBALStorage implements Storage
class DBALStorage extends AbstractStorage
{
private $conn;
private $table;

View File

@@ -30,7 +30,7 @@ use Doctrine\Common\Cache\Cache;
*
* @author Benjamin Eberlei <kontakt@beberlei.de>
*/
class DoctrineCacheStorage implements Storage
class DoctrineCacheStorage extends AbstractStorage
{
/**
* @var Doctrine\Common\Cache\Cache
@@ -60,20 +60,6 @@ class DoctrineCacheStorage implements Storage
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);

View File

@@ -30,7 +30,7 @@ use Doctrine\KeyValueStore\NotFoundException;
*
* @author Stan Lemon <stosh1985@gmail.com>
*/
class DynamoDbStorage implements Storage
class DynamoDbStorage extends AbstractStorage
{
/**
* @var \Aws\DynamoDb\DynamoDbClient

View File

@@ -27,7 +27,7 @@ use Doctrine\KeyValueStore\NotFoundException;
*
* @author Markus Bachmann <markus.bachmann@bachi.biz>
*/
class MongoDbStorage implements Storage
class MongoDbStorage extends AbstractStorage
{
/**
* @var \Mongo

View File

@@ -25,7 +25,7 @@ use Doctrine\KeyValueStore\NotFoundException;
/**
* @author Marcel Araujo <admin@marcelaraujo.me>
*/
class RedisStorage implements Storage
class RedisStorage extends AbstractStorage
{
/**
* @var \Redis

View File

@@ -26,7 +26,7 @@ use Riak\Client;
/**
* @author Markus Bachmann <markus.bachmann@bachi.biz>
*/
class RiakStorage implements Storage
class RiakStorage extends AbstractStorage
{
/**
* @var \Riak\Client

View File

@@ -31,7 +31,7 @@ use Doctrine\KeyValueStore\NotFoundException;
*
* @author Stan Lemon <stosh1985@gmail.com>
*/
class SimpleDbStorage implements Storage
class SimpleDbStorage extends AbstractStorage
{
/**
* @var \Aws\SimpleDb\SimpleDbClient

View File

@@ -36,7 +36,7 @@ use Doctrine\KeyValueStore\Storage\WindowsAzureTable\HttpStorageException;
*
* @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';
@@ -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>

View File

@@ -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 {

View File

@@ -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;'],
];
}
}