Skip to content

Commit cae12ee

Browse files
committed
Delete all file revisions by bulk
1 parent c697e66 commit cae12ee

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

psalm-baseline.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,11 @@
8585
<code>$context</code>
8686
</MixedArgumentTypeCoercion>
8787
</file>
88+
<file src="src/GridFS/CollectionWrapper.php">
89+
<MixedAssignment>
90+
<code>$ids[]</code>
91+
</MixedAssignment>
92+
</file>
8893
<file src="src/GridFS/ReadableStream.php">
8994
<MixedArgument>
9095
<code><![CDATA[$currentChunk->n]]></code>
@@ -100,6 +105,11 @@
100105
<code>$context</code>
101106
</MixedAssignment>
102107
</file>
108+
<file src="src/GridFS/WritableStream.php">
109+
<MixedArgument>
110+
<code><![CDATA[$this->file['filename']]]></code>
111+
</MixedArgument>
112+
</file>
103113
<file src="src/Model/BSONArray.php">
104114
<MixedAssignment>
105115
<code>$this[$key]</code>

src/GridFS/CollectionWrapper.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,27 @@ public function deleteFileAndChunksById($id): void
9191
$this->chunksCollection->deleteMany(['files_id' => $id]);
9292
}
9393

94+
public function deleteFileAndChunksByFilename(string $filename): int
95+
{
96+
/** @var iterable<array{_id: mixed}> $files */
97+
$files = $this->findFiles(['filename' => $filename], [
98+
'codec' => null,
99+
'typeMap' => ['root' => 'array'],
100+
'projection' => ['_id' => 1],
101+
]);
102+
103+
/** @var list<mixed> $ids */
104+
$ids = [];
105+
foreach ($files as $file) {
106+
$ids[] = $file['_id'];
107+
}
108+
109+
$count = $this->filesCollection->deleteMany(['_id' => ['$in' => $ids]])->getDeletedCount();
110+
$this->chunksCollection->deleteMany(['files_id' => ['$in' => $ids]]);
111+
112+
return $count ?? 0;
113+
}
114+
94115
/**
95116
* Drops the GridFS files and chunks collections.
96117
*/

src/GridFS/WritableStream.php

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
use HashContext;
2121
use MongoDB\BSON\Binary;
22-
use MongoDB\BSON\Document;
2322
use MongoDB\BSON\ObjectId;
2423
use MongoDB\BSON\UTCDateTime;
2524
use MongoDB\Driver\Exception\RuntimeException as DriverRuntimeException;
@@ -181,24 +180,12 @@ public function close(): void
181180
*/
182181
public function delete(): int
183182
{
184-
/** @var Document[] $revisions */
185-
$revisions = $this->collectionWrapper->findFiles(['filename' => $this->file['filename']], [
186-
'codec' => null,
187-
'typeMap' => ['root' => 'bson'],
188-
'projection' => ['_id' => 1],
189-
]);
190-
191-
$count = 0;
192-
193-
foreach ($revisions as $file) {
194-
$this->collectionWrapper->findFileById($file->get('_id'));
195-
$this->collectionWrapper->deleteFileAndChunksById($file->get('_id'));
196-
$count++;
183+
try {
184+
return $this->collectionWrapper->deleteFileAndChunksByFilename($this->file['filename']);
185+
} finally {
186+
// Prevent further operations on this stream
187+
$this->abort();
197188
}
198-
199-
$this->abort();
200-
201-
return $count;
202189
}
203190

204191
/**

0 commit comments

Comments
 (0)