Skip to content

Commit 5d5ed35

Browse files
author
Will Banfield
committed
Changing over to a decorator pattern
1 parent 78142bc commit 5d5ed35

File tree

5 files changed

+83
-76
lines changed

5 files changed

+83
-76
lines changed

src/GridFS/Bucket.php

Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class Bucket
2222
private $options;
2323
private $filesCollection;
2424
private $chunksCollection;
25+
private $indexChecker;
2526
private $ensuredIndexes = false;
2627
/**
2728
* Constructs a GridFS bucket.
@@ -129,41 +130,25 @@ public function getChunkSizeBytes()
129130
{
130131
return $this->options['chunkSizeBytes'];
131132
}
132-
/**
133-
* Opens a Stream for writing the contents of a file.
134-
*
135-
* @param string $filename file to upload
136-
* @param array $options Stream Options
137-
* @return Stream uploadStream
138-
*/
139-
public function openUploadStream($filename, array $options = [])
133+
public function getDatabaseName()
140134
{
141-
$this->ensureIndexes();
142-
$options = [
143-
'filesCollection' => $this->filesCollection,
144-
'chunksCollection' => $this->chunksCollection,
145-
'chunkSizeBytes' => $this->getChunkSizeBytes()
146-
];
147-
$context = stream_context_create(['gridfs' => $options]);
148-
return fopen(sprintf('gridfs://%s/%s', $this->databaseName, $filename), 'w', false, $context);
135+
return $this->options['chunkSizeBytes'];
149136
}
150-
/**
151-
* Upload a file to this bucket by specifying the source stream file
152-
*
153-
* @param String $filename Filename To Insert
154-
* @param Stream $source Source Stream
155-
* @param array $options Stream Options
156-
* @return ObjectId
157-
*/
158-
159-
public function uploadFromStream($filename, $source, array $options = [])
137+
public function find($filter, array $options =[])
160138
{
161-
$this->ensureIndexes();
162-
$gridFsStream = new GridFsUpload($this->filesCollection, $this->chunksCollection, $this->getChunkSizeBytes(), $filename, $options);
163-
return $gridFsStream->uploadFromStream($source);
139+
//add proper validation for the filter and for the options
140+
return $this->filesCollection->find($filter);
141+
}
142+
143+
public function chunkInsert($toUpload) {
144+
$this->chunksCollection->insertOne($toUpload);
145+
}
146+
147+
public function fileInsert($toUpload) {
148+
$this->filesCollection->insertOne($toUpload);
164149
}
165150

166-
private function ensureIndexes()
151+
public function ensureIndexes()
167152
{
168153
if ($this->ensuredIndexes) {
169154
return;
@@ -175,6 +160,7 @@ private function ensureIndexes()
175160
$this->ensureChunksIndex();
176161
$this->ensuredIndexes = true;
177162
}
163+
178164
private function ensureChunksIndex()
179165
{
180166
foreach ($this->chunksCollection->listIndexes() as $index) {
@@ -184,6 +170,7 @@ private function ensureChunksIndex()
184170
}
185171
$this->chunksCollection->createIndex(['files_id' => 1, 'n' => 1], ['unique' => true]);
186172
}
173+
187174
private function ensureFilesIndex()
188175
{
189176
foreach ($this->filesCollection->listIndexes() as $index) {
@@ -193,18 +180,19 @@ private function ensureFilesIndex()
193180
}
194181
$this->filesCollection->createIndex(['filename' => 1, 'uploadDate' => 1]);
195182
}
183+
196184
private function isFilesCollectionEmpty()
197185
{
198186
return null === $this->filesCollection->findOne([], [
199187
'readPreference' => new ReadPreference(ReadPreference::RP_PRIMARY),
200188
'projection' => ['_id' => 1],
201189
]);
202190
}
191+
203192
public function delete(ObjectId $id)
204193
{
205194
$options = ['writeConcern' => $this->writeConcern];
206195
$this->chunksCollection->deleteMany(['file_id' => $id], $options);
207-
208196
$this->filesCollection->deleteOne(['_id' => $id], $options);
209197
}
210198
}

src/GridFS/BucketReadWriter.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
namespace MongoDB\GridFS;
3+
4+
class BucketReadWriter
5+
{
6+
private $bucket;
7+
8+
public function __construct(Bucket $bucket)
9+
{
10+
$this->bucket = $bucket;
11+
}
12+
/**
13+
* Opens a Stream for writing the contents of a file.
14+
*
15+
* @param string $filename file to upload
16+
* @param array $options Stream Options
17+
* @return Stream uploadStream
18+
*/
19+
public function openUploadStream($filename, array $options = [])
20+
{
21+
$options = [
22+
'bucket' => $this->bucket,
23+
];
24+
$context = stream_context_create(['gridfs' => $options]);
25+
return fopen(sprintf('gridfs://%s/%s', $this->bucket->getDatabaseName(), $filename), 'w', false, $context);
26+
}
27+
/**
28+
* Upload a file to this bucket by specifying the source stream file
29+
*
30+
* @param String $filename Filename To Insert
31+
* @param Stream $source Source Stream
32+
* @param array $options Stream Options
33+
* @return ObjectId
34+
*/
35+
public function uploadFromStream($filename, $source, array $options = [])
36+
{
37+
$gridFsStream = new GridFsUpload($this->bucket, $filename, $options);
38+
return $gridFsStream->uploadFromStream($source);
39+
}
40+
}

src/GridFS/GridFsStream.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,18 @@
1010
*/
1111
class GridFsStream
1212
{
13-
protected $chunkSizeBytes;
14-
protected $filesCollection;
15-
protected $chunksCollection;
13+
protected $bucket;
1614
protected $n;
1715
protected $buffer;
1816
protected $file;
1917
/**
2018
* Constructs a GridFsStream
2119
*
22-
* @param \MongoDB\Collection $filesCollection GridFS files Collection
23-
* @param \MongoDB\Collection $chunksCollection GridFS chunks Collection
24-
* @param int32 $chunkSizeBytes Size of chunks
20+
* @param \MongoDB\GridFS\Bucket $bucket GridFS Bucket
2521
*/
26-
public function __construct(\MongoDB\Collection $filesCollection,\MongoDB\Collection $chunksCollection, $chunkSizeBytes)
22+
public function __construct(Bucket $bucket)
2723
{
28-
$this->chunkSizeBytes = $chunkSizeBytes;
29-
$this->filesCollection = $filesCollection;
30-
$this->chunksCollection = $chunksCollection;
24+
$this->bucket = $bucket;
3125
$this->n = 0;
3226
$this->buffer = fopen('php://temp', 'w+');
3327
}

src/GridFS/GridFsUpload.php

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class GridFsUpload extends GridFsStream
1515
{
1616
private $ctx;
1717
private $bufferLength;
18+
private $indexChecker;
1819
/**
1920
* Constructs a GridFS upload stream
2021
*
@@ -40,19 +41,18 @@ class GridFsUpload extends GridFsStream
4041
* @throws InvalidArgumentException
4142
*/
4243
public function __construct(
43-
\MongoDB\Collection $filesCollection,
44-
\MongoDB\Collection $chunksCollection,
45-
$chunkSizeBytes,
44+
Bucket $bucket,
4645
$filename,
4746
array $options=[]
4847
)
4948
{
5049
$this->bufferLength = 0;
5150
$this->ctx = hash_init('md5');
51+
5252
$uploadDate = time();
5353
$objectId = new \MongoDB\BSON\ObjectId();
5454
$main_file = [
55-
"chunkSize" => $chunkSizeBytes,
55+
"chunkSize" => $bucket->getChunkSizeBytes(),
5656
"filename" => $filename,
5757
"uploadDate" => $uploadDate,
5858
"_id" => $objectId
@@ -82,7 +82,7 @@ public function __construct(
8282
}
8383
}
8484
$this->file = array_merge($main_file, $fileOptions);
85-
parent::__construct($filesCollection, $chunksCollection, $chunkSizeBytes);
85+
parent::__construct($bucket);
8686
}
8787
/**
8888
* Reads data from a stream into GridFS
@@ -92,6 +92,8 @@ public function __construct(
9292
*/
9393
public function uploadFromStream($source)
9494
{
95+
$this->bucket->ensureIndexes();
96+
9597
if (!is_resource($source) || get_resource_type($source) != "stream") {
9698
throw new UnexpectedTypeException('stream', $source);
9799
} else{
@@ -100,7 +102,7 @@ public function uploadFromStream($source)
100102
// throw new InvalidArgumentException("stream not readable");
101103
//issue being that php's is_readable reports native streams as not readable like php://temp
102104
}
103-
while ($data = fread($source, $this->chunkSizeBytes)) {
105+
while ($data = fread($source, $this->bucket->getChunkSizeBytes())) {
104106
$this->insertChunk($data);
105107
}
106108
return $this->fileCollectionInsert();
@@ -113,37 +115,21 @@ public function uploadFromStream($source)
113115
*/
114116
public function insertChunks($toWrite)
115117
{
116-
$readBytes = 0;
118+
$this->bucket->ensureIndexes();
117119

120+
$readBytes = 0;
118121
while($readBytes != strlen($toWrite)) {
119-
$addToBuffer = substr($toWrite, $readBytes, $this->chunkSizeBytes - $this->bufferLength);
122+
$addToBuffer = substr($toWrite, $readBytes, $this->bucket->getChunkSizeBytes() - $this->bufferLength);
120123
fwrite($this->buffer, $addToBuffer);
121124
$readBytes += strlen($addToBuffer);
122125
$this->bufferLength += strlen($addToBuffer);
123-
if($this->bufferLength == $this->chunkSizeBytes) {
126+
if($this->bufferLength == $this->bucket->getChunkSizeBytes()) {
124127
rewind($this->buffer);
125-
$this->insertChunk(fread($this->buffer, $this->chunkSizeBytes));
128+
$this->insertChunk(fread($this->buffer, $this->bucket->getChunkSizeBytes()));
126129
ftruncate($this->buffer,0);
127130
$this->bufferLength = 0;
128131
}
129132
}
130-
/*
131-
rewind($this->buffer);
132-
$cached = fread($this->buffer, $this->chunkSizeBytes);
133-
$toWrite = $cached . $toWrite;
134-
$bytes = str_split($toWrite, $this->chunkSizeBytes);
135-
136-
fclose($this->buffer);
137-
$this->buffer = fopen("php://temp", "w+");
138-
139-
for ($i = 0; $i < count($bytes); $i++) {
140-
if ($i == count($bytes) - 1 && strlen($bytes[$i]) < $this->chunkSizeBytes) {
141-
fwrite($this->buffer, $bytes[$i]);
142-
return strlen($toWrite);
143-
}
144-
$this->insertChunk($bytes[$i]);
145-
}
146-
*/
147133
return $readBytes;
148134
}
149135
/**
@@ -153,11 +139,11 @@ public function insertChunks($toWrite)
153139
public function close()
154140
{
155141
rewind($this->buffer);
156-
$cached = fread($this->buffer, $this->chunkSizeBytes);
142+
$cached = fread($this->buffer, $this->bucket->getChunkSizeBytes());
157143

158144
if(strlen($cached) > 0) {
159145
$toUpload = ["files_id" => $this->file["_id"], "n" => $this->n, "data" => $cached];
160-
$this->chunksCollection->insertOne($toUpload);
146+
$this->bucket->chunkInsert($toUpload);
161147
$this->n++;
162148
}
163149

@@ -169,15 +155,15 @@ private function insertChunk($data)
169155
{
170156
$toUpload = ["files_id" => $this->file['_id'], "n" => $this->n, "data" => $data];
171157
hash_update($this->ctx, $data);
172-
$this->chunksCollection->insertOne($toUpload);
158+
$this->bucket->chunkInsert($toUpload);
173159
$this->n++;
174160
}
175161

176162
private function fileCollectionInsert()
177163
{
178164
$md5 = hash_final($this->ctx);
179165
$this->file = array_merge($this->file, ['length' => $this->n, 'md5' => $md5]);
180-
$this->filesCollection->insertOne($this->file);
166+
$this->bucket->fileInsert($this->file);
181167
return $this->file['_id'];
182168
}
183169
}

src/GridFS/StreamWrapper.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class StreamWrapper
2121
private $protocol = 'gridfs';
2222
private $mode;
2323
private $gridFsStream;
24+
private $bucket;
2425

2526
/**
2627
* Register the GridFS stream wrapper.
@@ -90,9 +91,7 @@ public function stream_open($path, $mode, $options, &$openedPath)
9091
{
9192
$this->initProtocol($path);
9293
$context = stream_context_get_options($this->context);
93-
$this->chunksCollection =$context['gridfs']['chunksCollection'];
94-
$this->filesCollection = $context['gridfs']['filesCollection'];
95-
$this->chunkSizeBytes = $context['gridfs']['chunkSizeBytes'];
94+
$this->bucket =$context['gridfs']['bucket'];
9695
$this->mode = $mode;
9796
switch ($this->mode) {
9897
case 'w' : return $this ->openWriteStream();
@@ -102,7 +101,7 @@ public function stream_open($path, $mode, $options, &$openedPath)
102101
}
103102

104103
public function openWriteStream() {
105-
$this->gridFsStream = new GridFsUpload($this->filesCollection, $this->chunksCollection, $this->chunkSizeBytes, $this->identifier, []);
104+
$this->gridFsStream = new GridFsUpload($this->bucket, $this->identifier, []);
106105
return true;
107106
}
108107

0 commit comments

Comments
 (0)