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 ...@@ -111,21 +111,30 @@ class Bucket
{ {
return $this->options['bucketName']; return $this->options['bucketName'];
} }
public function find($filter, array $options =[]) public function getReadConcern()
{ {
//add proper validation for the filter and for the options if(isset($this->options['readPreference'])) {
return $this->filesCollection->find($filter); return $this->options['readPreference'];
} else{
return null;
}
} }
public function getWriteConcern()
public function chunkInsert($toUpload) { {
$this->chunksCollection->insertOne($toUpload); if(isset($this->options['writeConcern'])) {
return $this->options['writeConcern'];
} else{
return null;
}
} }
public function fileInsert($toUpload) { public function find($filter, array $options =[])
$this->filesCollection->insertOne($toUpload); {
//add proper validation for the filter and for the options
return $this->filesCollection->find($filter);
} }
public function ensureIndexes() private function ensureIndexes()
{ {
if ($this->ensuredIndexes) { if ($this->ensuredIndexes) {
return; return;
...@@ -162,11 +171,4 @@ class Bucket ...@@ -162,11 +171,4 @@ class Bucket
'projection' => ['_id' => 1], '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 ...@@ -63,5 +63,25 @@ class BucketReadWriter
$gridFsStream = new GridFsDownload($this->bucket, $id); $gridFsStream = new GridFsDownload($this->bucket, $id);
$gridFsStream->downloadToStream($destination); $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 ...@@ -19,7 +19,7 @@ class GridFsDownload extends GridFsStream
private $bufferFresh=true; private $bufferFresh=true;
private $bufferEmpty=true; private $bufferEmpty=true;
/** /**
* Constructs a GridFS upload stream * Constructs a GridFS download stream
* *
* Supported options: * Supported options:
* *
...@@ -45,7 +45,6 @@ class GridFsDownload extends GridFsStream ...@@ -45,7 +45,6 @@ class GridFsDownload extends GridFsStream
{ {
$this->file = $bucket->getFilesCollection()->findOne(['_id' => $objectId]); $this->file = $bucket->getFilesCollection()->findOne(['_id' => $objectId]);
if (is_null($this->file)) { if (is_null($this->file)) {
//MUST RAISE AN ERROR ! (WHICH ONE I DON'T)
throw new \MongoDB\Exception\GridFSFileNotFoundException($objectId, $bucket->getBucketName(), $bucket->getDatabaseName()); throw new \MongoDB\Exception\GridFSFileNotFoundException($objectId, $bucket->getBucketName(), $bucket->getDatabaseName());
} }
if ($this->file->length > 0) { if ($this->file->length > 0) {
......
...@@ -75,12 +75,12 @@ class SpecificationTests extends FunctionalTestCase ...@@ -75,12 +75,12 @@ class SpecificationTests extends FunctionalTestCase
$test['assert']['result'] = $result; $test['assert']['result'] = $result;
} }
if ($testResult == "void") { if ($testResult == "void") {
$test['assert']['result'] = null; $fixedAssert['result'] = null;
} }
$this->assertEquals($result, $fixedAssert['result']); $this->assertEquals($result, $fixedAssert['result']);
} }
if (isset($test['assert']['data'])) { if (isset($test['assert']['data'])) {
$this->runCommands($fixedAssert, $result); $this->runCommands($fixedAssert['data'], $result);
$this->collectionsEqual($this->collections['expected.files'],$this->bucket->getFilesCollection()); $this->collectionsEqual($this->collections['expected.files'],$this->bucket->getFilesCollection());
if(isset($this->collections['expected.chunks'])) { if(isset($this->collections['expected.chunks'])) {
$this->collectionsEqual($this->collections['expected.chunks'],$this->bucket->getChunksCollection()); $this->collectionsEqual($this->collections['expected.chunks'],$this->bucket->getChunksCollection());
...@@ -91,7 +91,7 @@ class SpecificationTests extends FunctionalTestCase ...@@ -91,7 +91,7 @@ class SpecificationTests extends FunctionalTestCase
public function provideSpecificationTests() public function provideSpecificationTests()
{ {
$testPath=getcwd().'/tests/GridFS/Specification/tests/download.json'; $testPath=getcwd().'/tests/GridFS/Specification/tests/delete.json';
$testArgs = []; $testArgs = [];
foreach(glob($testPath) as $filename) { foreach(glob($testPath) as $filename) {
...@@ -148,22 +148,22 @@ class SpecificationTests extends FunctionalTestCase ...@@ -148,22 +148,22 @@ class SpecificationTests extends FunctionalTestCase
public function runCommands($cmds, $result) public function runCommands($cmds, $result)
{ {
$cmds = $this->fixTypes($cmds, true); foreach($cmds as $cmd){
foreach($cmds as $cmd) {
foreach($cmd as $key => $value) { foreach($cmd as $key => $value) {
if(isset($this->commands[$key])) { if(isset($this->commands[$key])) {
$cmdName = $key; $cmdName = $key;
$collectionName = $value; $collectionName = $value;
if(isset($cmd['documents'])){
foreach($cmd['documents'] as $docIndex => $doc) { foreach($cmd['documents'] as $docIndex => $doc) {
foreach($doc as $docKey => $docVal){ foreach($doc as $docKey => $docVal){
if(is_string($docVal)) { if(is_string($docVal)) {
if($docVal == '*result') { if($docVal == '*result') {
$doc[$docKey] = $result; $doc[$docKey] = $result;
}
} }
} }
$cmd['documents'][$docIndex] = $doc;
} }
$cmd['documents'][$docIndex] = $doc;
} }
$collection = new Collection($this->manager, sprintf("%s.%s", $this->getDatabaseName(), $collectionName)); $collection = new Collection($this->manager, sprintf("%s.%s", $this->getDatabaseName(), $collectionName));
$this->commands[$key]($collection, $this->fixTypes($cmd, true)); $this->commands[$key]($collection, $this->fixTypes($cmd, true));
...@@ -171,6 +171,7 @@ class SpecificationTests extends FunctionalTestCase ...@@ -171,6 +171,7 @@ class SpecificationTests extends FunctionalTestCase
} }
} }
} }
} }
public function initializeDatabases($data, $test) public function initializeDatabases($data, $test)
...@@ -186,12 +187,15 @@ class SpecificationTests extends FunctionalTestCase ...@@ -186,12 +187,15 @@ class SpecificationTests extends FunctionalTestCase
$filesCollection->insertMany($data['files']); $filesCollection->insertMany($data['files']);
$expectedFilesCollection = new Collection($this->manager, sprintf("%s.%s", $this->getDatabaseName(), "expected.files")); $expectedFilesCollection = new Collection($this->manager, sprintf("%s.%s", $this->getDatabaseName(), "expected.files"));
$expectedFilesCollection->insertMany($data['files']); $expectedFilesCollection->insertMany($data['files']);
$this->collections['expected.files'] = $expectedFilesCollection;
} }
if (isset($data['chunks']) && count($data['chunks']) > 0) { if (isset($data['chunks']) && count($data['chunks']) > 0) {
$chunksCollection = new Collection($this->manager, sprintf("%s.%s", $this->getDatabaseName(), "fs.chunks")); $chunksCollection = new Collection($this->manager, sprintf("%s.%s", $this->getDatabaseName(), "fs.chunks"));
$chunksCollection->insertMany($data['chunks']); $chunksCollection->insertMany($data['chunks']);
$expectedChunksCollection = new Collection($this->manager, sprintf("%s.%s", $this->getDatabaseName(), "expected.chunks")); $expectedChunksCollection = new Collection($this->manager, sprintf("%s.%s", $this->getDatabaseName(), "expected.chunks"));
$expectedChunksCollection->insertMany($data['chunks']); $expectedChunksCollection->insertMany($data['chunks']);
$this->collections['expected.chunks'] = $expectedChunksCollection;
} }
if(isset($test['arrange'])) { if(isset($test['arrange'])) {
foreach($test['arrange']['data'] as $cmd) { foreach($test['arrange']['data'] as $cmd) {
...@@ -203,9 +207,7 @@ class SpecificationTests extends FunctionalTestCase ...@@ -203,9 +207,7 @@ class SpecificationTests extends FunctionalTestCase
} }
} }
} }
} }
public function uploadCommand($args) public function uploadCommand($args)
{ {
$args = $this->fixTypes($args, false); $args = $this->fixTypes($args, false);
...@@ -230,7 +232,8 @@ class SpecificationTests extends FunctionalTestCase ...@@ -230,7 +232,8 @@ class SpecificationTests extends FunctionalTestCase
} }
function deleteCommand($args) function deleteCommand($args)
{ {
$args = $this->fixTypes($args, false);
$this->bucketReadWriter->delete($args['id']);
} }
function download_by_nameCommand($args) 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