Unverified Commit faabcf13 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2664322 by benjifisher, dawehner, daffie, almaudoh, mradcliffe, catch,...

Issue #2664322 by benjifisher, dawehner, daffie, almaudoh, mradcliffe, catch, quietone, alexpott, larowlan: key_value table is only used by a core service but it depends on system install
parent f124d526
Loading
Loading
Loading
Loading
+171 −20
Original line number Diff line number Diff line
@@ -61,11 +61,17 @@ public function __construct($collection, SerializationInterface $serializer, Con
   * {@inheritdoc}
   */
  public function has($key) {
    try {
      return (bool) $this->connection->query('SELECT 1 FROM {' . $this->connection->escapeTable($this->table) . '} WHERE [collection] = :collection AND [name] = :key', [
        ':collection' => $this->collection,
        ':key' => $key,
      ])->fetchField();
    }
    catch (\Exception $e) {
      $this->catchException($e);
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
@@ -92,9 +98,15 @@ public function getMultiple(array $keys) {
   * {@inheritdoc}
   */
  public function getAll() {
    try {
      $result = $this->connection->query('SELECT [name], [value] FROM {' . $this->connection->escapeTable($this->table) . '} WHERE [collection] = :collection', [':collection' => $this->collection]);
    $values = [];
    }
    catch (\Exception $e) {
      $this->catchException($e);
      $result = [];
    }

    $values = [];
    foreach ($result as $item) {
      if ($item) {
        $values[$item->name] = $this->serializer->decode($item->value);
@@ -104,9 +116,16 @@ public function getAll() {
  }

  /**
   * {@inheritdoc}
   * Saves a value for a given key.
   *
   * This will be called by set() within a try block.
   *
   * @param string $key
   *   The key of the data to store.
   * @param mixed $value
   *   The data to store.
   */
  public function set($key, $value) {
  protected function doSet($key, $value) {
    $this->connection->merge($this->table)
      ->keys([
        'name' => $key,
@@ -119,7 +138,35 @@ public function set($key, $value) {
  /**
   * {@inheritdoc}
   */
  public function setIfNotExists($key, $value) {
  public function set($key, $value) {
    try {
      $this->doSet($key, $value);
    }
    catch (\Exception $e) {
      // If there was an exception, try to create the table.
      if ($this->ensureTableExists()) {
        $this->doSet($key, $value);
      }
      else {
        throw $e;
      }
    }
  }

  /**
   * Saves a value for a given key if it does not exist yet.
   *
   * This will be called by setIfNotExists() within a try block.
   *
   * @param string $key
   *   The key of the data to store.
   * @param mixed $value
   *   The data to store.
   *
   * @return bool
   *   TRUE if the data was set, FALSE if it already existed.
   */
  public function doSetIfNotExists($key, $value) {
    $result = $this->connection->merge($this->table)
      ->insertFields([
        'collection' => $this->collection,
@@ -132,16 +179,39 @@ public function setIfNotExists($key, $value) {
    return $result == Merge::STATUS_INSERT;
  }

  /**
   * {@inheritdoc}
   */
  public function setIfNotExists($key, $value) {
    try {
      return $this->doSetIfNotExists($key, $value);
    }
    catch (\Exception $e) {
      // If there was an exception, try to create the table.
      if ($this->ensureTableExists()) {
        return $this->doSetIfNotExists($key, $value);
      }
      else {
        throw $e;
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function rename($key, $new_key) {
    try {
      $this->connection->update($this->table)
        ->fields(['name' => $new_key])
        ->condition('collection', $this->collection)
        ->condition('name', $key)
        ->execute();
    }
    catch (\Exception $e) {
      $this->catchException($e);
    }
  }

  /**
   * {@inheritdoc}
@@ -149,20 +219,101 @@ public function rename($key, $new_key) {
  public function deleteMultiple(array $keys) {
    // Delete in chunks when a large array is passed.
    while ($keys) {
      try {
        $this->connection->delete($this->table)
          ->condition('name', array_splice($keys, 0, 1000), 'IN')
          ->condition('collection', $this->collection)
          ->execute();
      }
      catch (\Exception $e) {
        $this->catchException($e);
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  public function deleteAll() {
    try {
      $this->connection->delete($this->table)
        ->condition('collection', $this->collection)
        ->execute();
    }
    catch (\Exception $e) {
      $this->catchException($e);
    }
  }

  /**
   * Check if the table exists and create it if not.
   *
   * @return bool
   *   TRUE if the table exists, FALSE if it does not exists.
   */
  protected function ensureTableExists() {
    try {
      $database_schema = $this->connection->schema();
      if (!$database_schema->tableExists($this->table)) {
        $database_schema->createTable($this->table, $this->schemaDefinition());
        return TRUE;
      }
    }
    // If the table already exists, then attempting to recreate it will throw an
    // exception. In this case just catch the exception and do nothing.
    catch (SchemaObjectExistsException $e) {
      return TRUE;
    }
    return FALSE;
  }

  /**
   * Act on an exception when the table might not have been created.
   *
   * If the table does not yet exist, that's fine, but if the table exists and
   * yet the query failed, then the exception needs to propagate.
   *
   * @param \Exception $e
   *   The exception.
   *
   * @throws \Exception
   */
  protected function catchException(\Exception $e) {
    if ($this->connection->schema()->tableExists($this->table)) {
      throw $e;
    }
  }

  /**
   * Defines the schema for the key_value table.
  */
  public static function schemaDefinition() {
    return [
      'description' => 'Generic key-value storage table. See the state system for an example.',
      'fields' => [
        'collection' => [
          'description' => 'A named collection of key and value pairs.',
          'type' => 'varchar_ascii',
          'length' => 128,
          'not null' => TRUE,
          'default' => '',
        ],
        'name' => [
          'description' => 'The key of the key-value pair. As KEY is a SQL reserved keyword, name was chosen instead.',
          'type' => 'varchar_ascii',
          'length' => 128,
          'not null' => TRUE,
          'default' => '',
        ],
        'value' => [
          'description' => 'The value.',
          'type' => 'blob',
          'not null' => TRUE,
          'size' => 'big',
        ],
      ],
      'primary key' => ['collection', 'name'],
    ];
  }

}
+145 −23
Original line number Diff line number Diff line
@@ -33,17 +33,24 @@ public function __construct($collection, SerializationInterface $serializer, Con
   * {@inheritdoc}
   */
  public function has($key) {
    try {
      return (bool) $this->connection->query('SELECT 1 FROM {' . $this->connection->escapeTable($this->table) . '} WHERE [collection] = :collection AND [name] = :key AND [expire] > :now', [
        ':collection' => $this->collection,
        ':key' => $key,
        ':now' => REQUEST_TIME,
      ])->fetchField();
    }
    catch (\Exception $e) {
      $this->catchException($e);
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function getMultiple(array $keys) {
    try {
      $values = $this->connection->query(
        'SELECT [name], [value] FROM {' . $this->connection->escapeTable($this->table) . '} WHERE [expire] > :now AND [name] IN ( :keys[] ) AND [collection] = :collection',
        [
@@ -53,11 +60,21 @@ public function getMultiple(array $keys) {
        ])->fetchAllKeyed();
      return array_map([$this->serializer, 'decode'], $values);
    }
    catch (\Exception $e) {
      // @todo: Perhaps if the database is never going to be available,
      // key/value requests should return FALSE in order to allow exception
      // handling to occur but for now, keep it an array, always.
      // https://www.drupal.org/node/2787737
      $this->catchException($e);
    }
    return [];
  }

  /**
   * {@inheritdoc}
   */
  public function getAll() {
    try {
      $values = $this->connection->query(
        'SELECT [name], [value] FROM {' . $this->connection->escapeTable($this->table) . '} WHERE [collection] = :collection AND [expire] > :now',
        [
@@ -66,11 +83,25 @@ public function getAll() {
        ])->fetchAllKeyed();
      return array_map([$this->serializer, 'decode'], $values);
    }
    catch (\Exception $e) {
      $this->catchException($e);
    }
    return [];
  }

  /**
   * {@inheritdoc}
   * Saves a value for a given key with a time to live.
   *
   * This will be called by setWithExpire() within a try block.
   *
   * @param string $key
   *   The key of the data to store.
   * @param mixed $value
   *   The data to store.
   * @param int $expire
   *   The time to live for items, in seconds.
   */
  public function setWithExpire($key, $value, $expire) {
  protected function doSetWithExpire($key, $value, $expire) {
    $this->connection->merge($this->table)
      ->keys([
        'name' => $key,
@@ -86,7 +117,37 @@ public function setWithExpire($key, $value, $expire) {
  /**
   * {@inheritdoc}
   */
  public function setWithExpireIfNotExists($key, $value, $expire) {
  public function setWithExpire($key, $value, $expire) {
    try {
      $this->doSetWithExpire($key, $value, $expire);
    }
    catch (\Exception $e) {
      // If there was an exception, then try to create the table.
      if ($this->ensureTableExists()) {
        $this->doSetWithExpire($key, $value, $expire);
      }
      else {
        throw $e;
      }
    }
  }

  /**
   * Sets a value for a given key with a time to live if it does not yet exist.
   *
   * This will be called by setWithExpireIfNotExists() within a try block.
   *
   * @param string $key
   *   The key of the data to store.
   * @param mixed $value
   *   The data to store.
   * @param int $expire
   *   The time to live for items, in seconds.
   *
   * @return bool
   *   TRUE if the data was set, or FALSE if it already existed.
   */
  protected function doSetWithExpireIfNotExists($key, $value, $expire) {
    if (!$this->has($key)) {
      $this->setWithExpire($key, $value, $expire);
      return TRUE;
@@ -94,6 +155,24 @@ public function setWithExpireIfNotExists($key, $value, $expire) {
    return FALSE;
  }

  /**
   * {@inheritdoc}
   */
  public function setWithExpireIfNotExists($key, $value, $expire) {
    try {
      return $this->doSetWithExpireIfNotExists($key, $value, $expire);
    }
    catch (\Exception $e) {
      // If there was an exception, try to create the table.
      if ($this->ensureTableExists()) {
        return $this->doSetWithExpireIfNotExists($key, $value, $expire);
      }
      else {
        throw $e;
      }
    }
  }

  /**
   * {@inheritdoc}
   */
@@ -110,4 +189,47 @@ public function deleteMultiple(array $keys) {
    parent::deleteMultiple($keys);
  }

  /**
   * Defines the schema for the key_value_expire table.
   */
  public static function schemaDefinition() {
    return [
      'description' => 'Generic key/value storage table with an expiration.',
      'fields' => [
        'collection' => [
          'description' => 'A named collection of key and value pairs.',
          'type' => 'varchar_ascii',
          'length' => 128,
          'not null' => TRUE,
          'default' => '',
        ],
        'name' => [
          // KEY is an SQL reserved word, so use 'name' as the key's field name.
          'description' => 'The key of the key/value pair.',
          'type' => 'varchar_ascii',
          'length' => 128,
          'not null' => TRUE,
          'default' => '',
        ],
        'value' => [
          'description' => 'The value of the key/value pair.',
          'type' => 'blob',
          'not null' => TRUE,
          'size' => 'big',
        ],
        'expire' => [
          'description' => 'The time since Unix epoch in seconds when this item expires. Defaults to the maximum possible time.',
          'type' => 'int',
          'not null' => TRUE,
          'default' => 2147483647,
        ],
      ],
      'primary key' => ['collection', 'name'],
      'indexes' => [
        'all' => ['name', 'collection', 'expire'],
        'expire' => ['expire'],
      ],
    ];
  }

}
+25 −3
Original line number Diff line number Diff line
@@ -58,9 +58,31 @@ public function get($collection) {
   * Deletes expired items.
   */
  public function garbageCollection() {
    try {
      $this->connection->delete('key_value_expire')
        ->condition('expire', REQUEST_TIME, '<')
        ->execute();
    }
    catch (\Exception $e) {
      $this->catchException($e);
    }
  }

  /**
   * Act on an exception when the table might not have been created.
   *
   * If the table does not yet exist, that's fine, but if the table exists and
   * yet the query failed, then the exception needs to propagate.
   *
   * @param \Exception $e
   *   The exception.
   *
   * @throws \Exception
   */
  protected function catchException(\Exception $e) {
    if ($this->connection->schema()->tableExists('key_value_expire')) {
      throw $e;
    }
  }

}
+2 −1
Original line number Diff line number Diff line
@@ -48,7 +48,8 @@ public function get($key, $default = NULL);
   * @return array
   *   An associative array of items successfully returned, indexed by key.
   *
   * @todo What's returned for non-existing keys?
   * @todo Determine the best return value for non-existing keys in
   *   https://www.drupal.org/node/2787737
   */
  public function getMultiple(array $keys);

+1 −1
Original line number Diff line number Diff line
@@ -43,7 +43,7 @@ class WorkspacesContentModerationStateTest extends ContentModerationStateTest {
  protected function setUp(): void {
    parent::setUp();

    $this->installSchema('system', ['key_value_expire', 'sequences']);
    $this->installSchema('system', ['sequences']);

    $this->initializeWorkspacesModule();
    $this->switchToWorkspace('stage');
Loading