Skip to content

Commit 8a79808

Browse files
committed
Optimize rename using a single updateMany command
1 parent b61d817 commit 8a79808

File tree

3 files changed

+20
-16
lines changed

3 files changed

+20
-16
lines changed

src/GridFS/CollectionWrapper.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,17 @@ public function insertFile($file): void
253253
$this->filesCollection->insertOne($file);
254254
}
255255

256+
/**
257+
* Updates the filename field in the file document for all the files with a given filename.
258+
*/
259+
public function updateFilenameForFilename(string $filename, string $newFilename): UpdateResult
260+
{
261+
return $this->filesCollection->updateMany(
262+
['filename' => $filename],
263+
['$set' => ['filename' => $newFilename]],
264+
);
265+
}
266+
256267
/**
257268
* Updates the filename field in the file document for a given ID.
258269
*

src/GridFS/ReadableStream.php

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,17 @@
1919

2020
use Iterator;
2121
use MongoDB\BSON\Binary;
22-
use MongoDB\BSON\Document;
2322
use MongoDB\Driver\CursorInterface;
2423
use MongoDB\Exception\InvalidArgumentException;
2524
use MongoDB\GridFS\Exception\CorruptFileException;
25+
use MongoDB\GridFS\Exception\LogicException;
2626

2727
use function assert;
2828
use function ceil;
2929
use function floor;
3030
use function is_integer;
3131
use function is_object;
32+
use function is_string;
3233
use function property_exists;
3334
use function sprintf;
3435
use function strlen;
@@ -179,24 +180,16 @@ public function readBytes(int $length): string
179180

180181
/**
181182
* Rename all revisions of the file.
182-
*
183-
* @return int The number of revisions renamed
184183
*/
185-
public function rename(string $newFilename): int
184+
public function rename(string $newFilename): bool
186185
{
187-
/** @var Document[] $revisions */
188-
$revisions = $this->collectionWrapper->findFiles(
189-
['filename' => $this->file->filename],
190-
['typeMap' => ['root' => 'bson'], 'projection' => ['_id' => 1]],
191-
);
192-
$count = 0;
193-
194-
foreach ($revisions as $revision) {
195-
$this->collectionWrapper->updateFilenameForId($revision->get('_id'), $newFilename);
196-
$count++;
186+
if (! isset($this->file->filename) || ! is_string($this->file->filename)) {
187+
throw new LogicException('Cannot rename file without a filename');
197188
}
198189

199-
return $count;
190+
$result = $this->collectionWrapper->updateFilenameForFilename($this->file->filename, $newFilename);
191+
192+
return $result->isAcknowledged();
200193
}
201194

202195
/**

src/GridFS/StreamWrapper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public function rename(string $fromPath, string $toPath): bool
114114
$newName = explode('/', $toPath, 4)[3] ?? '';
115115
assert($this->stream instanceof ReadableStream);
116116

117-
return $this->stream->rename($newName) > 0;
117+
return $this->stream->rename($newName);
118118
}
119119

120120
/**

0 commit comments

Comments
 (0)