This repository has been archived by the owner on Dec 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 228
/
GridFileStream.cs
executable file
·388 lines (350 loc) · 15.6 KB
/
GridFileStream.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
using System;
using System.IO;
using MongoDB.Driver;
namespace MongoDB.GridFS
{
/// <summary>
/// Stream for reading and writing to a file in GridFS.
/// </summary>
/// <remarks>
/// When using the stream for random io it is possible to produce chunks in the begining and middle of the
/// file that are not full size followed by other chunks that are full size. This only affects the md5 sum
/// that is calculated on the file on close. Because of this do not rely on the md5 sum of a file when doing
/// random io. Writing to the stream sequentially works fine and will produce a consistent md5.
/// </remarks>
public class GridFileStream : Stream
{
private IMongoCollection files;
private IMongoCollection chunks;
private Document chunk;
private bool chunkDirty;
private long chunkLower = -1;
private long chunkUpper = -1;
private byte[] buffer;
private byte[] blankBuffer;
private int buffPosition;
private int highestBuffPosition;
private long highestPosWritten;
#region Properties
private GridFileInfo gridFileInfo;
public GridFileInfo GridFileInfo {
get { return gridFileInfo; }
set { gridFileInfo = value; }
}
private bool canRead;
public override bool CanRead {
get { return canRead; }
}
private bool canWrite;
public override bool CanWrite {
get { return canRead; }
}
public override bool CanSeek {
get { return true; }
}
public override long Length {
get {
return gridFileInfo.Length;
}
}
private long position;
public override long Position {
get {
return position;
}
set {
this.Seek(value, SeekOrigin.Begin);
}
}
#endregion
public GridFileStream(GridFileInfo gridfileinfo,IMongoCollection files, IMongoCollection chunks, FileAccess access){
switch (access){
case FileAccess.Read:
canRead = true;
break;
case FileAccess.ReadWrite:
canRead = true;
canWrite = true;
break;
case FileAccess.Write:
canWrite = true;
break;
}
this.gridFileInfo = gridfileinfo;
this.files = files;
this.chunks = chunks;
this.buffer = new byte[gridFileInfo.ChunkSize];
this.blankBuffer = new byte[gridFileInfo.ChunkSize];
this.highestPosWritten = this.gridFileInfo.Length;
this.MoveTo(0);
}
/// <summary>
/// Reads data from the stream into the specified array. It will fill the array in starting at offset and
/// adding count bytes returning the number of bytes read from the stream.
/// </summary>
public override int Read(byte[] array, int offset, int count){
int bytesLeftToRead = count;
int bytesRead = 0;
while(bytesLeftToRead > 0 && this.position < this.Length){
int buffAvailable = buffer.Length - buffPosition;
int readCount = 0;
if(buffAvailable > bytesLeftToRead){
readCount = bytesLeftToRead;
}else{
readCount = buffAvailable;
}
if(readCount + position > highestPosWritten){
//adjust readcount so that we don't read past the end of file.
readCount = readCount - (int)(readCount + position - highestPosWritten);
}
Array.Copy(buffer,buffPosition,array,offset,readCount);
buffPosition += readCount;
bytesLeftToRead -= readCount;
bytesRead += readCount;
offset += bytesRead;
MoveTo(position + readCount);
}
return bytesRead;
}
private void ValidateReadState(byte[] array, int offset, int count){
if (array == null){
throw new ArgumentNullException("array", new Exception("array is null"));
}
else if (offset < 0){
throw new ArgumentOutOfRangeException("offset", new Exception("offset is negative"));
}
else if (count < 0){
throw new ArgumentOutOfRangeException("count", new Exception("count is negative"));
}
else if ((array.Length - offset) < count){
throw new MongoGridFSException("Invalid count argument", gridFileInfo.FileName, null);
}
else if (!canRead){
throw new MongoGridFSException("Reading this file is not supported", gridFileInfo.FileName, null);
}
}
/// <summary>
/// Copies from the source array into the grid file.
/// </summary>
/// <param name="array">
/// A <see cref="System.Byte[]"/> The source array to copy from.
/// </param>
/// <param name="offset">
/// A <see cref="System.Int32"/> The offset within the source array.
/// </param>
/// <param name="count">
/// A <see cref="System.Int32"/> The number of bytes from within the source array to copy.
/// </param>
public override void Write(byte[] array, int offset, int count){
ValidateWriteState(array,offset,count);
int bytesLeftToWrite = count;
while(bytesLeftToWrite > 0){
int buffAvailable = buffer.Length - buffPosition;
int writeCount = 0;
if(buffAvailable > bytesLeftToWrite){
writeCount = bytesLeftToWrite;
}else{
writeCount = buffAvailable;
}
Array.Copy(array,offset,buffer,buffPosition,writeCount);
chunkDirty = true;
buffPosition += writeCount;
offset += writeCount;
bytesLeftToWrite -= writeCount;
MoveTo(position + writeCount);
highestPosWritten = Math.Max(highestPosWritten, position);
}
}
private void ValidateWriteState(byte[] array, int offset, int count){
if (array == null){
throw new ArgumentNullException("array", new Exception("array is null"));
}else if (offset < 0){
throw new ArgumentOutOfRangeException("offset", new Exception("offset is negative"));
}else if (count < 0){
throw new ArgumentOutOfRangeException("count",new Exception("count is negative"));
}else if ((array.Length - offset) < count){
throw new MongoGridFSException("Invalid count argument", gridFileInfo.FileName, null);
}else if (!canWrite){
throw new System.NotSupportedException("Stream does not support writing.");
}
}
/// <summary>
/// Flushes any changes to current chunk to the database. It can be called in client code at any time or it
/// will automatically be called on Close() and when the stream position moves off the bounds of the current
/// chunk.
/// </summary>
public override void Flush(){
if(chunkDirty == false) return;
//avoid a copy if possible.
if(highestBuffPosition == buffer.Length){
chunk["data"] = new Binary(buffer);
}else{
byte[] data = new byte[highestBuffPosition];
Array.Copy(buffer,data,highestBuffPosition);
chunk["data"] = new Binary(data);
}
if(chunk.Contains("_id")){
chunks.Update(chunk);
}else{
chunks.Insert(chunk);
}
this.gridFileInfo.Length = highestPosWritten;
}
/// <summary>
/// Seek to any location in the stream. Seeking past the end of the file is allowed. Any writes to that
/// location will cause the file to grow to that size. Any holes that may be created from the seek will
/// be zero filled on close.
/// </summary>
public override long Seek(long offset, SeekOrigin origin){
if ((origin < SeekOrigin.Begin) || (origin > SeekOrigin.End)){
throw new ArgumentException("Invalid Seek Origin");
}
switch (origin){
case SeekOrigin.Begin:
if (offset < 0){
throw new ArgumentException("Attempted seeking before the begining of the stream");
}else{
MoveTo(offset);
}
break;
case SeekOrigin.Current:
MoveTo(position + offset);
break;
case SeekOrigin.End:
if (offset <= 0){
throw new ArgumentException("Attempted seeking after the end of the stream");
}
MoveTo(this.Length - offset);
break;
}
return position;
}
/// <summary>
/// Sets the length of this stream to the given value.
/// </summary>
/// <param name="value">
/// A <see cref="System.Int64"/>
/// </param>
public override void SetLength(long value){
if(value < 0) throw new ArgumentOutOfRangeException("length");
if(this.CanSeek == false || this.CanWrite == false) {
throw new NotSupportedException("The stream does not support both writing and seeking.");
}
if(value < highestPosWritten) {
TruncateAfter(value);
}else{
this.Seek(value, SeekOrigin.Begin);
}
chunkDirty = true;
this.gridFileInfo.Length = value;
highestPosWritten = value;
}
/// <summary>
/// Close the stream and flush any changes to the database.
/// </summary>
public override void Close(){
this.Flush();
this.gridFileInfo.Length = highestPosWritten;
EnsureNoHoles();
string md5 = gridFileInfo.CalcMD5();
gridFileInfo.Md5 = md5;
this.files.Update(gridFileInfo.ToDocument());
base.Close();
}
/// <summary>
/// Moves the current position to the new position. If this causes a new chunk to need to be loaded it will take
/// care of flushing the buffer and loading a new chunk.
/// </summary>
/// <param name="position">
/// A <see cref="System.Int32"/> designating where to go to.
/// </param>
private void MoveTo(long position){
this.position = position;
int chunkSize = this.gridFileInfo.ChunkSize;
bool chunkInRange = (chunk != null && position >= chunkLower && position < chunkUpper);
if(chunkInRange == false){
if(chunk != null && chunkDirty){
highestBuffPosition = Math.Max(highestBuffPosition, buffPosition);
this.Flush();
}
int chunknum = (int)Math.Floor((double)(position / chunkSize));
Array.Copy(blankBuffer,buffer,buffer.Length);
LoadOrCreateChunk(chunknum);
chunkDirty = false;
chunkLower = chunknum * chunkSize;
chunkUpper = chunkLower + chunkSize;
}
buffPosition = (int)(position % chunkSize);
highestBuffPosition = Math.Max(highestBuffPosition, buffPosition);
}
/// <summary>
/// Loads a chunk from the chunks collection if it exists. Otherwise it creates a blank chunk Document.
/// </summary>
/// <param name="num"></param>
private void LoadOrCreateChunk(int num){
Object fid = this.GridFileInfo.Id;
Document spec = new Document().Append("files_id", fid).Append("n",num);
chunk = this.chunks.FindOne(spec);
if(chunk == null) {
chunk = spec;
highestBuffPosition = 0;
}else{
Binary b = (Binary)chunk["data"];
highestBuffPosition = b.Bytes.Length;
Array.Copy(b.Bytes,buffer, highestBuffPosition);
}
}
/// <summary>
/// Deletes all chunks after the specified position and clears out any extra bytes if the position doesn't fall on
/// a chunk boundry.
/// </summary>
private void TruncateAfter(long value){
int chunknum = CalcChunkNum(value);
Document spec = new Document().Append("files_id", this.gridFileInfo.Id)
.Append("n",new Document().Append("$gt",chunknum));
this.chunks.Delete(spec);
this.MoveTo(value );
Array.Copy(blankBuffer,0,buffer,buffPosition, buffer.Length - buffPosition);
highestBuffPosition = buffPosition;
}
private int CalcChunkNum(long position){
int chunkSize = this.gridFileInfo.ChunkSize;
return (int)Math.Floor((double)(position / chunkSize));
}
/// <summary>
/// Makes sure that at least a skelton chunk exists for all numbers. If not the MD5 calculation will fail on a sparse file.
/// </summary>
private void EnsureNoHoles(){
int highChunk = CalcChunkNum(this.GridFileInfo.Length);
Document query = new Document().Append("files_id", this.GridFileInfo.Id)
.Append("n", new Document()
.Append("$lte",highChunk));
Document sort = new Document().Append("n",1);
Document fields = new Document().Append("_id", 1).Append("n",1);
Binary data = new Binary(this.blankBuffer);
int i = 0;
using(ICursor<Document> cur = chunks.Find(new Document().Append("query", query).Append("sort", sort), 0, 0, fields))
{
foreach(Document doc in cur.Documents){
int n = Convert.ToInt32(doc["n"]);
if(i < n){
while(i < n){
chunks.Insert(new Document().Append("files_id", this.gridFileInfo.Id)
.Append("n", i)
.Append("data", data)
);
i++;
}
}else{
i++;
}
}
}
}
protected override void Dispose(bool disposing){
this.canRead = false;
this.canWrite = false;
base.Dispose(disposing);
}
}
}