Commit 8f2aaeab authored by Will Banfield's avatar Will Banfield Committed by Jeremy Mikola

PHPLIB-148: Implement file deletion

parent 76f89eae
......@@ -111,21 +111,30 @@ class Bucket
{
return $this->options['bucketName'];
}
public function find($filter, array $options =[])
public function getReadConcern()
{
//add proper validation for the filter and for the options
return $this->filesCollection->find($filter);
if(isset($this->options['readPreference'])) {
return $this->options['readPreference'];
} else{
return null;
}
}
public function getWriteConcern()
{
if(isset($this->options['writeConcern'])) {
return $this->options['writeConcern'];
} else{
return null;
}
public function chunkInsert($toUpload) {
$this->chunksCollection->insertOne($toUpload);
}
public function fileInsert($toUpload) {
$this->filesCollection->insertOne($toUpload);
public function find($filter, array $options =[])
{
//add proper validation for the filter and for the options
return $this->filesCollection->find($filter);
}
public function ensureIndexes()
private function ensureIndexes()
{
if ($this->ensuredIndexes) {
return;
......@@ -162,11 +171,4 @@ class Bucket
'projection' => ['_id' => 1],
]);
}
public function delete(ObjectId $id)
{
$options = ['writeConcern' => $this->writeConcern];
$this->chunksCollection->deleteMany(['file_id' => $id], $options);
$this->filesCollection->deleteOne(['_id' => $id], $options);
}
}
......@@ -63,5 +63,25 @@ class BucketReadWriter
$gridFsStream = new GridFsDownload($this->bucket, $id);
$gridFsStream->downloadToStream($destination);
}
/**
* Delete a file from the GridFS bucket. If the file collection entry is not found, still attempts to delete orphaned chunks
*
* @param ObjectId $id file id
* @throws GridFSFileNotFoundException
*/
public function delete(\MongoDB\BSON\ObjectId $id)
{
$options =[];
$writeConcern = $this->bucket->getWriteConcern();
if(!is_null($writeConcern)) {
$options['writeConcern'] = $writeConcern;
}
$file = $this->bucket->getFilesCollection()->findOne(['_id' => $id]);
$this->bucket->getChunksCollection()->deleteMany(['files_id' => $id], $options);
if (is_null($file)) {
throw new \MongoDB\Exception\GridFSFileNotFoundException($id, $this->bucket->getDatabaseName(), $this->bucket->getBucketName());
}
$this->bucket->getFilesCollection()->deleteOne(['_id' => $id], $options);
}
}
......@@ -19,7 +19,7 @@ class GridFsDownload extends GridFsStream
private $bufferFresh=true;
private $bufferEmpty=true;
/**
* Constructs a GridFS upload stream
* Constructs a GridFS download stream
*
* Supported options:
*
......@@ -45,7 +45,6 @@ class GridFsDownload extends GridFsStream
{
$this->file = $bucket->getFilesCollection()->findOne(['_id' => $objectId]);
if (is_null($this->file)) {
//MUST RAISE AN ERROR ! (WHICH ONE I DON'T)
throw new \MongoDB\Exception\GridFSFileNotFoundException($objectId, $bucket->getBucketName(), $bucket->getDatabaseName());
}
if ($this->file->length > 0) {
......
......@@ -75,12 +75,12 @@ class SpecificationTests extends FunctionalTestCase
$test['assert']['result'] = $result;
}
if ($testResult == "void") {
$test['assert']['result'] = null;
$fixedAssert['result'] = null;
}
$this->assertEquals($result, $fixedAssert['result']);
}
if (isset($test['assert']['data'])) {
$this->runCommands($fixedAssert, $result);
$this->runCommands($fixedAssert['data'], $result);
$this->collectionsEqual($this->collections['expected.files'],$this->bucket->getFilesCollection());
if(isset($this->collections['expected.chunks'])) {
$this->collectionsEqual($this->collections['expected.chunks'],$this->bucket->getChunksCollection());
......@@ -91,7 +91,7 @@ class SpecificationTests extends FunctionalTestCase
public function provideSpecificationTests()
{
$testPath=getcwd().'/tests/GridFS/Specification/tests/download.json';
$testPath=getcwd().'/tests/GridFS/Specification/tests/delete.json';
$testArgs = [];
foreach(glob($testPath) as $filename) {
......@@ -148,13 +148,12 @@ class SpecificationTests extends FunctionalTestCase
public function runCommands($cmds, $result)
{
$cmds = $this->fixTypes($cmds, true);
foreach($cmds as $cmd) {
foreach($cmds as $cmd){
foreach($cmd as $key => $value) {
if(isset($this->commands[$key])) {
$cmdName = $key;
$collectionName = $value;
if(isset($cmd['documents'])){
foreach($cmd['documents'] as $docIndex => $doc) {
foreach($doc as $docKey => $docVal){
if(is_string($docVal)) {
......@@ -165,12 +164,14 @@ class SpecificationTests extends FunctionalTestCase
}
$cmd['documents'][$docIndex] = $doc;
}
}
$collection = new Collection($this->manager, sprintf("%s.%s", $this->getDatabaseName(), $collectionName));
$this->commands[$key]($collection, $this->fixTypes($cmd, true));
$this->collections[$collectionName] = $collection;
}
}
}
}
public function initializeDatabases($data, $test)
......@@ -186,12 +187,15 @@ class SpecificationTests extends FunctionalTestCase
$filesCollection->insertMany($data['files']);
$expectedFilesCollection = new Collection($this->manager, sprintf("%s.%s", $this->getDatabaseName(), "expected.files"));
$expectedFilesCollection->insertMany($data['files']);
$this->collections['expected.files'] = $expectedFilesCollection;
}
if (isset($data['chunks']) && count($data['chunks']) > 0) {
$chunksCollection = new Collection($this->manager, sprintf("%s.%s", $this->getDatabaseName(), "fs.chunks"));
$chunksCollection->insertMany($data['chunks']);
$expectedChunksCollection = new Collection($this->manager, sprintf("%s.%s", $this->getDatabaseName(), "expected.chunks"));
$expectedChunksCollection->insertMany($data['chunks']);
$this->collections['expected.chunks'] = $expectedChunksCollection;
}
if(isset($test['arrange'])) {
foreach($test['arrange']['data'] as $cmd) {
......@@ -203,9 +207,7 @@ class SpecificationTests extends FunctionalTestCase
}
}
}
}
public function uploadCommand($args)
{
$args = $this->fixTypes($args, false);
......@@ -230,7 +232,8 @@ class SpecificationTests extends FunctionalTestCase
}
function deleteCommand($args)
{
$args = $this->fixTypes($args, false);
$this->bucketReadWriter->delete($args['id']);
}
function download_by_nameCommand($args)
{
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment