Skip to content

Commit 4d5b089

Browse files
committed
ensuring indices on grid fs
1 parent 7efa8c5 commit 4d5b089

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

src/Flow/Mongo/MongoFile.php

+14
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ public function saveChunk()
9494
$this->config->getGridFs()->chunks->findAndModify($chunkQuery, $chunk, [], ['upsert' => true]);
9595
unlink($file['tmp_name']);
9696

97+
$this->ensureIndices();
98+
9799
return true;
98100
} catch (\Exception $e) {
99101
try {
@@ -154,6 +156,18 @@ public function deleteChunks()
154156
// nothing to do, as chunks are directly part of the final file
155157
}
156158

159+
public function ensureIndices()
160+
{
161+
$gridFs = $this->config->getGridFs();
162+
$indexKeys = ['files_id' => 1, 'n' => 1];
163+
$indexOptions = ['unique' => true, 'background' => true];
164+
if(method_exists($gridFs, 'createIndex')) { // only available for PECL mongo >= 1.5.0
165+
$gridFs->createIndex($indexKeys, $indexOptions);
166+
} else {
167+
$gridFs->ensureIndex($indexKeys, $indexOptions);
168+
}
169+
}
170+
157171
/**
158172
* @return array
159173
*/

0 commit comments

Comments
 (0)