diff --git a/core/lib/Drupal/Core/KeyValueStore/DatabaseStorageSortedSet.php b/core/lib/Drupal/Core/KeyValueStore/DatabaseStorageSortedSet.php index c01196c..a7ce599 100644 --- a/core/lib/Drupal/Core/KeyValueStore/DatabaseStorageSortedSet.php +++ b/core/lib/Drupal/Core/KeyValueStore/DatabaseStorageSortedSet.php @@ -14,26 +14,36 @@ class DatabaseStorageSortedSet implements KeyValueStoreSortedSetInterface { /** + * The name of the collection holding key and value pairs. + * * @var string */ protected $collection; /** + * The serialization class to use. + * * @var \Drupal\Component\Serialization\SerializationInterface */ protected $serializer; /** + * The database connection to use. + * * @var \Drupal\Core\Database\Connection */ protected $connection; /** + * The name of the SQL table to use, defaults to key_value_sorted. + * * @var string */ protected $table; /** + * Constructs the database key/value implementation for sorted sets. + * * @param string $collection * The name of the collection holding key and value pairs. * @param \Drupal\Component\Serialization\SerializationInterface $serializer @@ -54,8 +64,7 @@ public function __construct($collection, SerializationInterface $serializer, Con * {@inheritdoc} */ public function add($score, $member) { - $this->addMultiple(array(array($score => $member))); - return $this; + return $this->addMultiple([[$score => $member]]); } /** @@ -66,11 +75,11 @@ public function addMultiple(array $pairs) { foreach ($pair as $score => $member) { $encoded_member = $this->serializer->encode($member); $this->connection->merge($this->table) - ->fields(array( + ->fields([ 'collection' => $this->collection, 'name' => $score, 'value' => $encoded_member, - )) + ]) ->condition('collection', $this->collection) ->condition('value', $encoded_member) ->execute(); @@ -84,8 +93,8 @@ public function addMultiple(array $pairs) { */ public function getCount() { return $this->connection->select($this->table, 't') - ->condition('collection', $this->collection) ->countQuery() + ->condition('collection', $this->collection) ->execute() ->fetchField(); } @@ -95,17 +104,17 @@ public function getCount() { */ public function getRange($start, $stop = NULL) { $query = $this->connection->select($this->table, 't') - ->fields('t', array('value')) + ->fields('t', ['value']) + ->orderBy('name', 'ASC') ->condition('collection', $this->collection) ->condition('name', $start, '>='); - if ($stop !== NULL) { + if (is_int($stop)) { $query->condition('name', $stop, '<='); } - $result = $query->orderBy('name', 'ASC')->execute(); - $values = array(); - foreach ($result as $item) { + $values = []; + foreach ($query->execute() as $item) { $values[] = $this->serializer->decode($item->value); } return $values; diff --git a/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreSortedSetInterface.php b/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreSortedSetInterface.php index 98d9181..eed1ee8 100644 --- a/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreSortedSetInterface.php +++ b/core/lib/Drupal/Core/KeyValueStore/KeyValueStoreSortedSetInterface.php @@ -11,7 +11,9 @@ interface KeyValueStoreSortedSetInterface { /** - * @param float $score + * Add a single item to a set. + * + * @param int $score * The key for the item, for example microtime(), which can be used to * generate a sequential value. * @param mixed $member @@ -22,6 +24,8 @@ public function add($score, $member); /** + * Add multiple items to a set. + * * @param array $pairs * An array of key/value pairs to add. * @@ -30,27 +34,35 @@ public function add($score, $member); public function addMultiple(array $pairs); /** - * @return float + * Get the highest key in a set. + * + * @return int * The highest key in the collection. */ public function getMaxScore(); /** - * @return float + * Get the lowest key in in a set. + * + * @return int * The lowest key in the collection. */ public function getMinScore(); /** - * @return integer + * Get the number of items in a collection. + * + * @return int * The number of items in a collection. */ public function getCount(); /** - * @param float $start + * Get multiple items within a range of keys. + * + * @param int $start * The first key in the range. - * @param float $stop + * @param int $stop * The last key in the range. * * @return array diff --git a/core/modules/system/system.install b/core/modules/system/system.install index 5676fee..22df38e 100644 --- a/core/modules/system/system.install +++ b/core/modules/system/system.install @@ -994,7 +994,7 @@ function system_schema() { ); $schema['key_value_sorted'] = array( - 'description' => 'Key-value list storage table.', + 'description' => 'Sorted key-value list storage table.', 'fields' => array( 'collection' => array( 'description' => 'A named collection of key and value pairs.', @@ -1836,7 +1836,7 @@ function system_update_8203() { */ function system_update_8300() { $table = array( - 'description' => 'Key-value list storage table.', + 'description' => 'Sorted key-value list storage table.', 'fields' => array( 'collection' => array( 'description' => 'A named collection of key and value pairs.', @@ -1848,7 +1848,7 @@ function system_update_8300() { // KEY is an SQL reserved word, so use 'name' as the key's field name. 'name' => array( 'description' => 'The index or score key for the value.', - 'type' => 'float', + 'type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'big', diff --git a/core/tests/Drupal/KernelTests/Core/KeyValueStore/DatabaseStorageSortedSetTest.php b/core/tests/Drupal/KernelTests/Core/KeyValueStore/DatabaseStorageSortedSetTest.php index d06d2eb..4d0fd73 100644 --- a/core/tests/Drupal/KernelTests/Core/KeyValueStore/DatabaseStorageSortedSetTest.php +++ b/core/tests/Drupal/KernelTests/Core/KeyValueStore/DatabaseStorageSortedSetTest.php @@ -2,6 +2,7 @@ namespace Drupal\KernelTests\Core\KeyValueStore; +use Drupal\Core\Database\DatabaseExceptionWrapper; use Drupal\KernelTests\KernelTestBase; /** @@ -34,9 +35,12 @@ class DatabaseStorageSortedSetTest extends KernelTestBase { */ protected $store; + /** + * {@inheritdoc} + */ public function setUp() { parent::setUp(); - $this->installSchema('system', array('key_value_sorted')); + $this->installSchema('system', ['key_value_sorted']); $this->collection = $this->randomMachineName(); $this->serializer = \Drupal::service('serialization.phpserialize'); @@ -44,9 +48,15 @@ public function setUp() { $this->store = \Drupal::service('keyvalue.sorted_set')->get($this->collection); } - public function assertPairs($expected_pairs) { + /** + * Helper method to assert key value pairs. + * + * @param $expected_pairs array + * Array of expected key value pairs. + */ + public function assertPairs(array $expected_pairs) { $result = $this->connection->select('key_value_sorted', 't') - ->fields('t', array('name', 'value')) + ->fields('t', ['name', 'value']) ->condition('collection', $this->collection) ->condition('name', array_keys($expected_pairs), 'IN') ->execute() @@ -59,6 +69,14 @@ public function assertPairs($expected_pairs) { } } + /** + * Helper method to assert the number of records. + * + * @param $expected int + * Expected number of records. + * @param null $message string + * The message to display. + */ public function assertRecords($expected, $message = NULL) { $count = $this->connection->select('key_value_sorted', 't') ->fields('t') @@ -69,10 +87,19 @@ public function assertRecords($expected, $message = NULL) { $this->assertEquals($expected, $count, $message ? $message : "There are $expected records."); } + /** + * Helper method to generate a key based on microtime(). + * + * @return integer + * A key based on microtime(). + */ public function newKey() { return (int) (microtime(TRUE) * 1000000); } + /** + * Tests getting and setting of sorted key value sets. + */ public function testCalls() { $key0 = $this->newKey(); $value0 = $this->randomMachineName();