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 — ファイルをデータベースに格納する
説明
public mixed MongoGridFS::storeFile
( string
$filename
[, array $extra = array()
[, array $options = array()
]] )パラメータ
-
filename -
ファイルの名前。
-
extra -
保存するファイルに追加したいメタデータ。
-
options -
格納時のオプション。
-
"safe"
格納に成功したかどうかを調べます。
-
返り値
保存されたオブジェクトの _id を返します。
エラー / 例外
"safe" オプションが設定されていれば、追加に失敗した場合に MongoCursorException をスローします。
mike at eastghost dot com
16-Feb-2012 10:50
mike at eastghost dot com
16-Feb-2012 10:31
A little confusing, but the "string $filename" (i.e., the first parameter) is the actual data that gets stored into the GridFS.
