Skip to content

Commit

Permalink
Merge 775efe8 into 9af07d0
Browse files Browse the repository at this point in the history
  • Loading branch information
fgm committed Jul 25, 2021
2 parents 9af07d0 + 775efe8 commit 1a96eee
Show file tree
Hide file tree
Showing 18 changed files with 342 additions and 48 deletions.
18 changes: 18 additions & 0 deletions core.phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@
<!-- Example SIMPLETEST_DB value: mysql://username:password@localhost/databasename#table_prefix -->
<env name="SIMPLETEST_DB" value=""/>
<!-- Example BROWSERTEST_OUTPUT_DIRECTORY value: /path/to/webroot/sites/simpletest/browser_output -->
<!-- To have browsertest output use an alternative base URL. For example if
SIMPLETEST_BASE_URL is an internal DDEV URL, you can set this to the
external DDev URL so you can follow the links directly.
-->
<env name="BROWSERTEST_OUTPUT_DIRECTORY" value=""/>
<!-- To have browsertest output use an alternative base URL. For example if
SIMPLETEST_BASE_URL is an internal DDEV URL, you can set this to the
Expand Down Expand Up @@ -74,4 +78,18 @@
</exclude>
</whitelist>
</filter>
<!-- Filter for coverage reports. -->
<coverage>
<include>
<directory>../modules/contrib/mongodb</directory>
</include>
<exclude>
<file>../modules/contrib/mongodb/example.settings.local.php</file>
<!-- By definition test classes have no tests. -->
<directory suffix="Test.php">../modules/contrib/mongodb</directory>
<directory suffix="TestBase.php">../modules/contrib/mongodb</directory>
<!-- There is a remaining legacy test for reference in watchdog for now -->
<directory suffix=".test">../modules/contrib/mongodb</directory>
</exclude>
</coverage>
</phpunit>
5 changes: 2 additions & 3 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ by Drupal core for the SQL database drivers.
| Module | In a word | Information |
|---------------------|-----------|----------------------------------------------|
| [mongodb] | driver | Client and Database services, [tests] base |
| [mongodb_storage] | key-value | Key-value store, with server-side expiration |
| [mongodb_storage] | key-value,<br/> queue | Key-value store, with server-side expiration <br/>Default queue implementation |
| [mongodb_watchdog] | logger | PSR-3 compliant logger with a built-in UI |
| &nbsp; | | |

Expand All @@ -35,11 +35,10 @@ by Drupal core for the SQL database drivers.

### Planned

Modules expected to be ported to 8.x-2.x in some release after 2.0 include:
Modules expected to be ported to 8.x-2.x in some release after 2.1 include:

| Module | In a word | Information |
|---------------------|-----------|--------------------------------------------|
| mongodb_queue | queue | Queue API implementation |
| mongodb_cache | cache | Cache storage, with server-side expiration |
| mongodb_lock | lock | Lock plugin |
| mongodb_path | path | Path plugin |
Expand Down
8 changes: 6 additions & 2 deletions docs/install.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ guide assumes that :

* A [MongoDB][download] 4.0 to 4.2.x server instance has already been installed,
configured and is available for connection from the Drupal instance.
MongoDB 5.x, AWS DocumentDB and Azure CosmosDB might work but are not tested.
Be sure to [report any issue][report] you could have with either.
* The site will be running [Drupal][drupal] 8.9.x or 9.x.y, with [Drush][drush]
10.x.
* The [mongodb][mongodb] (not [mongo][mongo]) PHP extension version 1.7 or
later is installed and configured.
* PHP is version 7.3.x to 8.0.x. PHP 8.0.x should work but is not tested: be sure
to [report any issue][report] you could have with it.
* PHP is version 7.4.x to 8.0.x, to support typed properties.
* We recommend [using Composer](#installing-using-composer) for installing this
module.

Expand Down Expand Up @@ -81,6 +82,7 @@ $settings['mongodb'] = [
'default' => ['default', 'drupal'],
'keyvalue' => ['default', 'keyvalue'],
'logger' => ['default', 'logger'],
'queue' => ['default', 'queue'],
],
];
```
Expand All @@ -91,6 +93,8 @@ $settings['mongodb'] = [
same `default` MongoDB server, but in a separate `keyvalue` database.
* The `logger` database alias will store logger collections on the same
`default` MongoDB server, but in a separate `logger` database.
* The `queue` database alias will store queue collections on the same
`default` MongoDB server, but in a separate `queue` database.

The module contains an example default implementation of these settings, which
you can copy or include, in `mongodb/example.settings.local.php`.
Expand Down
2 changes: 1 addition & 1 deletion docs/modules/mongodb.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ database. What it actually provides:
invoking `parent::setUp()`, and need to drop that database during their
own `tearDown()`.
* They can use `$this->getTestDatabaseName($postfix)` method to build a
per-test database name that will not collide with the default
per-test database name that will not collide with the default
database.
* There are examples for this in the `mongodb_watchdoig` tests.

Expand Down
39 changes: 29 additions & 10 deletions docs/modules/mongodb_storage.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
# Key-value: `mongodb_storage`

The `mongodb_storage` module implements the [Key-Value storage][keyvallink]
for Drupal in MongoDB.
The `mongodb_storage` module implements two Drupal APIs in MongoDB:

## Configuration
- the [Key-Value storage][keyvallink]
- the [Queue API][queueapilink]

[keyvallink]: https://en.wikipedia.org/wiki/Key-value_database
[queueapilink]: https://api.drupal.org/api/drupal/core%21core.api.php/group/queue/9.2.x

[settings]: ../../install#settings-configuration
[default_queue]: https://api.drupal.org/api/drupal/core%21lib%21Drupal.php/function/Drupal%3A%3Aqueue/9.2.x

## Key-Value (Expirable) storage
### Configuration

To use the MongoDB Key-Value (Expirable) storage:

* ensure there is a `keyvalue` database alias as in
[settings configuration](../../install#settings-configuration).
[settings configuration](settings).
* declare MongoDB as the default Key-Value storage implementation by editing
the existing parameter declarations in the `sites/default/services.yml` file:

Expand All @@ -22,19 +31,29 @@ To use the MongoDB Key-Value (Expirable) storage:

* enable the module, e.g. using `drush en mongodb_storage`.
* import the existing Key-Value contents from the database, using the Drush
or Console `mongodb:storage:import_keyvalue` command: `drush most-ikv`.
`mongodb:storage:import_keyvalue` command: `drush most-ikv`.
It will output the names of the imported stores, for your information:

key_value
key_value_expire
* rebuild the container to take these changes into account using `drush cr`.

[keyvallink]: https://en.wikipedia.org/wiki/Key-value_database
* rebuild the container to take these changes into account using `drush cr`.

## Command
### Import of SQL Key-Value content

The module provides one single command to import the content of the default SQL
storage for Key-Value into MongoDB. The command is available for Drush and
storage for Key-Value into MongoDB. The command is available for Drush and
Drupal Console indifferently, and is described in the previous paragraph as part
of the configuration steps.


## Queue service

This module provides a default MongoDB Queue API implementation.

* Enable the module, e.g. using `drush en mongodb_storage`.
* Define a `queue` database alias, as described in [settings configuration][settings]
* Declare it as the [default Queue API implementation][default_queue],
by adding this line to the `settings.php` file

$settings['queue_default'] = 'queue.mongodb';
1 change: 1 addition & 0 deletions example.settings.local.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
'default' => ['default', 'drupal'],
'keyvalue' => ['default', 'keyvalue'],
'logger' => ['default', 'logger'],
'queue' => ['default', 'queue'],
],
];

Expand Down
15 changes: 12 additions & 3 deletions modules/mongodb_storage/mongodb_storage.services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ services:
arguments:
- '@mongodb.database_factory'
- '@datetime.time'
class: 'Drupal\mongodb_storage\KeyValueExpirableFactory'
class: 'Drupal\mongodb_storage\KeyValue\KeyValueExpirableFactory'
keyvalue.mongodb:
arguments: ['@mongodb.database_factory']
class: 'Drupal\mongodb_storage\KeyValueFactory'
arguments: [ '@mongodb.database_factory' ]
class: 'Drupal\mongodb_storage\KeyValue\KeyValueFactory'

mongodb.storage.queue:
class: 'MongoDB\Database'
factory: [ '@mongodb.database_factory', 'get' ]
arguments: [ 'queue' ]

mongodb.storage.sql_import:
class: \Drupal\mongodb_storage\Install\SqlImport
Expand All @@ -19,3 +24,7 @@ services:
- '@keyvalue.mongodb'
- '@keyvalue.expirable.mongodb'
- '@datetime.time'

queue.mongodb:
class: Drupal\mongodb_storage\Queue\QueueMongodbFactory
arguments: [ '@mongodb.storage.queue', '@settings' ]
4 changes: 2 additions & 2 deletions modules/mongodb_storage/src/Install/SqlImport.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
use Drupal\Core\Database\StatementInterface;
use Drupal\Core\KeyValueStore\KeyValueDatabaseExpirableFactory;
use Drupal\Core\KeyValueStore\KeyValueDatabaseFactory;
use Drupal\mongodb_storage\KeyValueExpirableFactory;
use Drupal\mongodb_storage\KeyValueFactory;
use Drupal\mongodb_storage\KeyValue\KeyValueExpirableFactory;
use Drupal\mongodb_storage\KeyValue\KeyValueFactory;

/**
* Service providing the import of the SQL-based KV storage to MongoDB.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Drupal\mongodb_storage;
namespace Drupal\mongodb_storage\KeyValue;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\KeyValueStore\KeyValueExpirableFactoryInterface;
Expand All @@ -12,6 +12,7 @@
* Class KeyValueFactory builds KeyValue stores as MongoDB collections.
*/
class KeyValueExpirableFactory extends KeyValueFactory implements KeyValueExpirableFactoryInterface {

const COLLECTION_PREFIX = 'kve_';

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Drupal\mongodb_storage;
namespace Drupal\mongodb_storage\KeyValue;

use Drupal\Core\KeyValueStore\KeyValueFactoryInterface;
use Drupal\mongodb\DatabaseFactory;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Drupal\mongodb_storage;
namespace Drupal\mongodb_storage\KeyValue;

use Drupal\Core\KeyValueStore\KeyValueStoreInterface;
use Drupal\Core\KeyValueStore\StorageBase;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace Drupal\mongodb_storage;
namespace Drupal\mongodb_storage\KeyValue;

use Drupal\Component\Datetime\TimeInterface;
use Drupal\Core\KeyValueStore\KeyValueStoreExpirableInterface;
Expand Down Expand Up @@ -72,7 +72,7 @@ public function ensureIndexes() {
* @return \MongoDB\BSON\UTCDateTime
* Its ready-to-insert counterpart.
*/
protected function getBsonExpire(int $expire) : UTCDateTime {
protected function getBsonExpire(int $expire): UTCDateTime {
return new UTCDateTime(1000 * ($this->time->getCurrentTime() + $expire));
}

Expand Down Expand Up @@ -117,10 +117,11 @@ public function setWithExpire($key, $value, $expire) {
$selector = [
'_id' => $this->stringifyKey($key),
];
$replacement = $selector + [
'expire' => $this->getBsonExpire($expire),
'value' => serialize($value),
];
$replacement = $selector +
[
'expire' => $this->getBsonExpire($expire),
'value' => serialize($value),
];
$options = [
'upsert' => TRUE,
];
Expand All @@ -145,16 +146,18 @@ public function setWithExpireIfNotExists($key, $value, $expire) {
$selector = [
'_id' => $this->stringifyKey($key),
];
$replacement = $selector + [
'expire' => $this->getBsonExpire($expire),
'value' => serialize($value),
];
$replacement = $selector +
[
'expire' => $this->getBsonExpire($expire),
'value' => serialize($value),
];
$options = [
'upsert' => FALSE,
];

$updateResult = $this->mongoDbCollection->replaceOne($selector, $replacement, $options);
$result = $updateResult->getModifiedCount() ? TRUE : FALSE;
$updateResult = $this->mongoDbCollection->replaceOne($selector,
$replacement, $options);
$result = (bool) $updateResult->getModifiedCount();
return $result;
}

Expand Down

0 comments on commit 1a96eee

Please sign in to comment.