diff --git a/core/lib/Drupal/Core/KeyValueStore/DatabaseStorageSortedSet.php b/core/lib/Drupal/Core/KeyValueStore/DatabaseStorageSortedSet.php index 0a312c1..0a08d42 100644 --- a/core/lib/Drupal/Core/KeyValueStore/DatabaseStorageSortedSet.php +++ b/core/lib/Drupal/Core/KeyValueStore/DatabaseStorageSortedSet.php @@ -2,7 +2,37 @@ namespace Drupal\Core\KeyValueStore; -class DatabaseStorageSortedSet extends DatabaseStorageSortedBase implements KeyValueStoreSortedSetInterface { +use Drupal\Component\Serialization\SerializationInterface; +use Drupal\Core\Database\Driver\mysql\Connection; + +class DatabaseStorageSortedSet implements KeyValueStoreSortedSetInterface { + + /** + * @var string + */ + protected $collection; + + /** + * @var \Drupal\Component\Serialization\SerializationInterface + */ + protected $serializer; + + /** + * @var \Drupal\Core\Database\Connection + */ + protected $connection; + + /** + * @var string + */ + protected $table; + + public function __construct($collection, SerializationInterface $serializer, Connection $connection, $table = 'key_value_sorted') { + $this->collection = $collection; + $this->serializer = $serializer; + $this->connection = $connection; + $this->table = $table; + } /** * {@inheritdoc} @@ -51,6 +81,27 @@ public function getCount() { ->fetchField(); } + /** + * {@inheritdoc} + */ + public function getRange($start, $stop = NULL) { + $query = $this->connection->select($this->table, 't') + ->fields('t', array('value')) + ->condition('collection', $this->collection) + ->condition('name', $start, '>='); + + if ($stop !== NULL) { + $query->condition('name', $stop, '<='); + } + $result = $query->orderBy('name', 'ASC')->execute(); + + $values = array(); + foreach ($result as $item) { + $values[] = $this->serializer->decode($item->value); + } + return $values; + } + public function getMaxScore() { $query = $this->connection->select($this->table); $query->condition('collection', $this->collection, '='); diff --git a/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreSortedSetInterface.php b/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreSortedSetInterface.php index aa559b4..c7fba78 100644 --- a/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreSortedSetInterface.php +++ b/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreSortedSetInterface.php @@ -2,7 +2,7 @@ namespace Drupal\Core\KeyValueStore; -interface KeyValueStoreSortedSetInterface extends KeyValueStoreSortedInterface { +interface KeyValueStoreSortedSetInterface { /** * @param float $score @@ -31,4 +31,17 @@ public function getMaxScore(); */ public function getMinScore(); + /** + * @return integer + */ + public function getCount(); + + /** + * @param float $start + * @param float $stop + * + * @return array + */ + public function getRange($start, $stop = NULL); + } diff --git a/core/tests/Drupal/KernelTests/Core/KeyValueStore/DatabaseStorageSortedSetTest.php b/core/tests/Drupal/KernelTests/Core/KeyValueStore/DatabaseStorageSortedSetTest.php index da4ed47..1b42fb1 100644 --- a/core/tests/Drupal/KernelTests/Core/KeyValueStore/DatabaseStorageSortedSetTest.php +++ b/core/tests/Drupal/KernelTests/Core/KeyValueStore/DatabaseStorageSortedSetTest.php @@ -2,12 +2,32 @@ namespace Drupal\KernelTests\Core\KeyValueStore; +use Drupal\KernelTests\KernelTestBase; + /** * Tests the sorted set key-value database storage. * * @group KeyValueStore */ -class DatabaseStorageSortedSetTest extends DatabaseStorageSortedTestBase { +class DatabaseStorageSortedSetTest extends KernelTestBase { + + + static public $modules = array('system', 'serialization'); + + /** + * @var string + */ + protected $collection; + + /** + * @var \Drupal\Component\Serialization\SerializationInterface + */ + protected $serializer; + + /** + * @var \Drupal\Core\Database\Connection + */ + protected $connection; /** * @var \Drupal\Core\KeyValueStore\KeyValueStoreSortedSetInterface @@ -16,9 +36,43 @@ class DatabaseStorageSortedSetTest extends DatabaseStorageSortedTestBase { public function setUp() { parent::setUp(); + $this->installSchema('system', array('key_value_sorted')); + + $this->collection = $this->randomMachineName(); + $this->serializer = \Drupal::service('serialization.phpserialize'); + $this->connection = \Drupal::service('database'); $this->store = \Drupal::service('keyvalue.sorted_set')->get($this->collection); } + public function assertPairs($expected_pairs) { + $result = $this->connection->select('key_value_sorted', 't') + ->fields('t', array('name', 'value')) + ->condition('collection', $this->collection) + ->condition('name', array_keys($expected_pairs), 'IN') + ->execute() + ->fetchAllAssoc('name'); + + $expected_count = count($expected_pairs); + $this->assertIdentical(count($result), $expected_count, "Query affected $expected_count records."); + foreach ($expected_pairs as $key => $value) { + $this->assertIdentical($this->serializer->decode($result[$key]->value), $value, "Key $key have value $value"); + } + } + + public function assertRecords($expected, $message = NULL) { + $count = $this->connection->select('key_value_sorted', 't') + ->fields('t') + ->condition('collection', $this->collection) + ->countQuery() + ->execute() + ->fetchField(); + $this->assertEqual($count, $expected, $message ? $message : "There are $expected records."); + } + + public function newKey() { + return (int) (microtime(TRUE) * 1000000); + } + public function testCalls() { $key0 = $this->newKey(); $value0 = $this->randomMachineName();