You are here

class SqlImport in MongoDB 8.2

Service providing the import of the SQL-based KV storage to MongoDB.

Hierarchy

  • class \Drupal\mongodb_storage\Install\SqlImport

Expanded class hierarchy of SqlImport

3 files declare their use of SqlImport
MongoDbStorageCommands.php in modules/mongodb_storage/src/Commands/MongoDbStorageCommands.php
SqlImportCommand.php in modules/mongodb_storage/src/Command/SqlImportCommand.php
SqlImportTest.php in modules/mongodb_storage/tests/src/Kernel/SqlImportTest.php
1 string reference to 'SqlImport'
mongodb_storage.services.yml in modules/mongodb_storage/mongodb_storage.services.yml
modules/mongodb_storage/mongodb_storage.services.yml
1 service uses SqlImport
mongodb.storage.sql_import in modules/mongodb_storage/mongodb_storage.services.yml
\Drupal\mongodb_storage\Install\SqlImport

File

modules/mongodb_storage/src/Install/SqlImport.php, line 18

Namespace

Drupal\mongodb_storage\Install
View source
class SqlImport {
  const KVP_TABLE = 'key_value';
  const KVE_TABLE = 'key_value_expire';

  /**
   * The database service.
   *
   * @var \Drupal\mongodb_storage\Commands\Connection
   */
  protected $database;

  /**
   * The expirable database KV factory.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueDatabaseExpirableFactory
   */
  protected $expirableDbFactory;

  /**
   * The expirable MongoDB KV factory.
   *
   * @var \Drupal\mongodb_storage\KeyValueExpirableFactory
   */
  protected $expirableMoFactory;

  /**
   * The database KV factory.
   *
   * @var \Drupal\Core\KeyValueStore\KeyValueDatabaseFactory
   */
  protected $persistentDbFactory;

  /**
   * The MongoDB KV factory.
   *
   * @var \Drupal\mongodb_storage\KeyValueFactory
   */
  protected $persistentMoFactory;

  /**
   * The datetime.time service.
   *
   * @var \Drupal\Component\Datetime\TimeInterface
   */
  protected $time;

  /**
   * MongoDbStorageCommands constructor.
   *
   * Note that this constructor type-hints on classes instead of interfaces,
   * because this is a migration command relying on actual implementations
   * details, not on the high-level KV interfaces.
   *
   * @param \Drupal\Core\Database\Connection $database
   *   The database service.
   * @param \Drupal\Core\KeyValueStore\KeyValueDatabaseFactory $persistentDbFactory
   *   The database KV factory.
   * @param \Drupal\Core\KeyValueStore\KeyValueDatabaseExpirableFactory $expirableDbFactory
   *   The expirable database KV factory.
   * @param \Drupal\mongodb_storage\KeyValueFactory $persistentMoFactory
   *   The MongoDB KV factory.
   * @param \Drupal\mongodb_storage\KeyValueExpirableFactory $expirableMoFactory
   *   The expirable MongoDB KV factory.
   * @param \Drupal\Component\Datetime\TimeInterface $time
   *   The datetime.time service.
   */
  public function __construct(Connection $database, KeyValueDatabaseFactory $persistentDbFactory, KeyValueDatabaseExpirableFactory $expirableDbFactory, KeyValueFactory $persistentMoFactory, KeyValueExpirableFactory $expirableMoFactory, TimeInterface $time) {
    $this->database = $database;
    $this->persistentDbFactory = $persistentDbFactory;
    $this->expirableDbFactory = $expirableDbFactory;
    $this->persistentMoFactory = $persistentMoFactory;
    $this->expirableMoFactory = $expirableMoFactory;
    $this->time = $time;
  }

  /**
   * List the collections in a database KV table.
   *
   * @param string $tableName
   *   The name of the KV table.
   *
   * @return \Drupal\Core\Database\StatementInterface
   *   A cursor to the individual collection names.
   */
  protected function getCollections(string $tableName) : StatementInterface {
    $cursor = $this->database
      ->select($tableName, 's')
      ->distinct()
      ->fields('s', [
      'collection',
    ])
      ->execute();
    return $cursor;
  }

  /**
   * Import a database persistent KV store.
   *
   * @param \Drupal\Core\Database\StatementInterface $cursor
   *   A cursor enumerating collections in a database KV store.
   */
  protected function importPersistent(StatementInterface $cursor) {
    foreach ($cursor as $row) {
      $collection = $row->collection;

      /** @var \Drupal\Core\KeyValueStore\KeyValueStoreInterface $dbStore */
      $dbStore = $this->persistentDbFactory
        ->get($collection);

      /** @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $mgStore */
      $mgStore = $this->persistentMoFactory
        ->get($collection);
      $mgStore
        ->deleteAll();
      foreach ($dbStore
        ->getAll() as $key => $value) {
        $mgStore
          ->set($key, $value);
      }
    }
  }

  /**
   * Import an expirable database KV store.
   *
   * This function needs to access the table-level information for the expirable
   * database KV store because the KeyValueExpirableStore[Interface] does not
   * provide access to the "expire" information.
   *
   * @param \Drupal\Core\Database\StatementInterface $cursor
   *   A cursor enumerating collections in a database KV store.
   * @param string $tableName
   *   The name of the database collection table.
   */
  protected function importExpirable(StatementInterface $cursor, string $tableName) {
    $columns = [
      'name',
      'value',
      'expire',
    ];
    foreach ($cursor as $row) {
      $collection = $row->collection;
      $valueCursor = $this->database
        ->select($tableName, 'kve')
        ->fields('kve', $columns)
        ->condition('kve.collection', $collection)
        ->execute();

      /** @var \Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface $mgStore */
      $mgStore = $this->expirableMoFactory
        ->get($collection, FALSE);
      $mgStore
        ->deleteAll();
      foreach ($valueCursor as $valueRow) {
        $key = $valueRow->name;
        $value = unserialize($valueRow->value);
        $now = $this->time
          ->getCurrentTime();
        $expire = $valueRow->expire;
        $mgStore
          ->setWithExpire($key, $value, $expire - $now);
      }
    }
  }

  /**
   * The command implementation for most-ikv: import the DB KV to MongoDB.
   */
  public function import() {
    $cursor = $this
      ->getCollections(static::KVP_TABLE);
    echo static::KVP_TABLE . PHP_EOL;
    $this
      ->importPersistent($cursor);
    $cursor = $this
      ->getCollections(static::KVE_TABLE);
    echo static::KVE_TABLE . PHP_EOL;
    $this
      ->importExpirable($cursor, static::KVE_TABLE);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
SqlImport::$database protected property The database service.
SqlImport::$expirableDbFactory protected property The expirable database KV factory.
SqlImport::$expirableMoFactory protected property The expirable MongoDB KV factory.
SqlImport::$persistentDbFactory protected property The database KV factory.
SqlImport::$persistentMoFactory protected property The MongoDB KV factory.
SqlImport::$time protected property The datetime.time service.
SqlImport::getCollections protected function List the collections in a database KV table.
SqlImport::import public function The command implementation for most-ikv: import the DB KV to MongoDB.
SqlImport::importExpirable protected function Import an expirable database KV store.
SqlImport::importPersistent protected function Import a database persistent KV store.
SqlImport::KVE_TABLE constant
SqlImport::KVP_TABLE constant
SqlImport::__construct public function MongoDbStorageCommands constructor.