Skip to content

Commit 2407bc4

Browse files
authored
PHPLIB-659 Support options for time-series collections (#829)
* PHPLIB-659 Support options for time-series collections * Update wording about 5.0 support
1 parent 4ee3fb3 commit 2407bc4

File tree

5 files changed

+323
-2
lines changed

5 files changed

+323
-2
lines changed

docs/includes/apiargs-MongoDBDatabase-method-createCollection-option.yaml

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,21 @@ pre: |
3535
</reference/bson-type-comparison-order/#collation>` for the collection.
3636
---
3737
arg_name: option
38+
name: expireAfterSeconds
39+
type: integer
40+
description: |
41+
Allows specifying a TTL after which documents will be removed from a
42+
time-series collection.
43+
44+
This option is available in MongoDB 5.0+ and will result in an exception at
45+
execution time if specified for an older server version.
46+
47+
.. versionadded:: 1.9
48+
interface: phpmethod
49+
operation: ~
50+
optional: true
51+
---
52+
arg_name: option
3853
name: flags
3954
type: integer
4055
description: |
@@ -133,6 +148,20 @@ interface: phpmethod
133148
operation: ~
134149
optional: true
135150
---
151+
arg_name: option
152+
name: timeseries
153+
type: array|object
154+
description: |
155+
Allows users to specify options for time-series collections.
156+
157+
This option is available in MongoDB 5.0+ and will result in an exception at
158+
execution time if specified for an older server version.
159+
160+
.. versionadded:: 1.9
161+
interface: phpmethod
162+
operation: ~
163+
optional: true
164+
---
136165
source:
137166
file: apiargs-MongoDBDatabase-common-option.yaml
138167
ref: typeMap

src/Operation/CreateCollection.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ class CreateCollection implements Executable
8282
* This is not supported for server versions < 3.4 and will result in an
8383
* exception at execution time if used.
8484
*
85+
* * expireAfterSeconds: The TTL for documents in time series collections.
86+
*
87+
* This is not supported for servers versions < 5.0.
88+
*
8589
* * flags (integer): Options for the MMAPv1 storage engine only. Must be a
8690
* bitwise combination CreateCollection::USE_POWER_OF_2_SIZES and
8791
* CreateCollection::NO_PADDING. The default is
@@ -104,6 +108,10 @@ class CreateCollection implements Executable
104108
*
105109
* * storageEngine (document): Storage engine options.
106110
*
111+
* * timeseries (document): Options for time series collections.
112+
*
113+
* This is not supported for servers versions < 5.0.
114+
*
107115
* * typeMap (array): Type map for BSON deserialization. This will only be
108116
* used for the returned command result document.
109117
*
@@ -139,6 +147,10 @@ public function __construct($databaseName, $collectionName, array $options = [])
139147
throw InvalidArgumentException::invalidType('"collation" option', $options['collation'], 'array or object');
140148
}
141149

150+
if (isset($options['expireAfterSeconds']) && ! is_integer($options['expireAfterSeconds'])) {
151+
throw InvalidArgumentException::invalidType('"expireAfterSeconds" option', $options['expireAfterSeconds'], 'integer');
152+
}
153+
142154
if (isset($options['flags']) && ! is_integer($options['flags'])) {
143155
throw InvalidArgumentException::invalidType('"flags" option', $options['flags'], 'integer');
144156
}
@@ -167,6 +179,10 @@ public function __construct($databaseName, $collectionName, array $options = [])
167179
throw InvalidArgumentException::invalidType('"storageEngine" option', $options['storageEngine'], 'array or object');
168180
}
169181

182+
if (isset($options['timeseries']) && ! is_array($options['timeseries']) && ! is_object($options['timeseries'])) {
183+
throw InvalidArgumentException::invalidType('"timeseries" option', $options['timeseries'], ['array', 'object']);
184+
}
185+
170186
if (isset($options['typeMap']) && ! is_array($options['typeMap'])) {
171187
throw InvalidArgumentException::invalidType('"typeMap" option', $options['typeMap'], 'array');
172188
}
@@ -237,13 +253,13 @@ private function createCommand()
237253
{
238254
$cmd = ['create' => $this->collectionName];
239255

240-
foreach (['autoIndexId', 'capped', 'flags', 'max', 'maxTimeMS', 'size', 'validationAction', 'validationLevel'] as $option) {
256+
foreach (['autoIndexId', 'capped', 'expireAfterSeconds', 'flags', 'max', 'maxTimeMS', 'size', 'validationAction', 'validationLevel'] as $option) {
241257
if (isset($this->options[$option])) {
242258
$cmd[$option] = $this->options[$option];
243259
}
244260
}
245261

246-
foreach (['collation', 'indexOptionDefaults', 'storageEngine', 'validator'] as $option) {
262+
foreach (['collation', 'indexOptionDefaults', 'storageEngine', 'timeseries', 'validator'] as $option) {
247263
if (isset($this->options[$option])) {
248264
$cmd[$option] = (object) $this->options[$option];
249265
}

tests/Operation/CreateCollectionTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public function provideInvalidConstructorOptions()
3232
$options[][] = ['collation' => $value];
3333
}
3434

35+
foreach ($this->getInvalidIntegerValues() as $value) {
36+
$options[][] = ['expireAfterSeconds' => $value];
37+
}
38+
3539
foreach ($this->getInvalidIntegerValues() as $value) {
3640
$options[][] = ['flags' => $value];
3741
}
@@ -60,6 +64,10 @@ public function provideInvalidConstructorOptions()
6064
$options[][] = ['storageEngine' => $value];
6165
}
6266

67+
foreach ($this->getInvalidDocumentValues() as $value) {
68+
$options[][] = ['timeseries' => $value];
69+
}
70+
6371
foreach ($this->getInvalidArrayValues() as $value) {
6472
$options[][] = ['typeMap' => $value];
6573
}

tests/UnifiedSpecTests/UnifiedSpecTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,19 @@ public function provideChangeStreamsTests()
141141
return $this->provideTests(__DIR__ . '/change-streams/*.json');
142142
}
143143

144+
/**
145+
* @dataProvider provideCollectionManagementTests
146+
*/
147+
public function testCollectionManagement(UnifiedTestCase $test)
148+
{
149+
self::$runner->run($test);
150+
}
151+
152+
public function provideCollectionManagementTests()
153+
{
154+
return $this->provideTests(__DIR__ . '/collection-management/*.json');
155+
}
156+
144157
/**
145158
* @dataProvider provideCrudTests
146159
*/

0 commit comments

Comments
 (0)