Pass
$options['safe'] = true;
to make Mongo slow down and do inserts sequentially instead of in parallel.
Use case:
I had 274 PDF documents stored on ext3 disk (avg. size was 5MB, none over 20MB)
A PHP loop loaded each PDF, then did a $GridFS->storeFile( $data, $meta );
This caused server to spike from load average of 0.2 to over 20.0
Apparently what was happening is the loop slammed Mongo (then memory, disk) with 274 convert/stores to do all at once.
Adding
$GridFS->storeFile( $data, $meta, array( 'safe' => true ) );
made the loop slow down and wait for each insert to finish and be confirmed before going on to next PDF.
Moral: Safe option can be used to maintain sanity and is not always just for data validity.
MongoGridFS::storeFile
(PECL mongo >=0.9.0)
MongoGridFS::storeFile — Stocke un fichier dans la base de données
Description
public mixed MongoGridFS::storeFile
( string
$filename
[, array $extra = array()
[, array $options = array()
]] )Liste de paramètres
-
filename -
Le nom du fichier.
-
extra -
Les autres métadonnées du fichier.
-
options -
Options pour le stockage.
-
"safe"
Vérifie si le stockage a réussi.
-
Valeurs de retour
Retourne l'identifiant de l'objet sauvegardé.
Erreurs / Exceptions
Lance une exception MongoCursorException si l'option "safe" est définie et que l'insertion échoue.
mike at eastghost dot com
16-Feb-2012 02:50
mike at eastghost dot com
16-Feb-2012 02:31
A little confusing, but the "string $filename" (i.e., the first parameter) is the actual data that gets stored into the GridFS.
