Can't seem to figure out how to use GridFS

17 views
Skip to first unread message

Christian

unread,
Sep 21, 2010, 5:31:12 AM9/21/10
to NoRM mongodb
Am I missing something?

I'm trying to save a file to mongodb using GridFS. I'm using the code
from the GridFileCollectionTests on
http://github.com/atheken/NoRM/blob/master/NoRM.Tests/GridFS/GridFileCollectionTests.cs

The file document is created but the data is not written it just
returns immediately and fetching the file again the Content property
is empty.

Again, am I missing something? Any examples of how to use GridFS with
NoRM would appreciated.

--
Christian

Christian

unread,
Sep 21, 2010, 6:36:42 AM9/21/10
to NoRM mongodb
Ok, I guess NoRM doesn't support saving files using GridFS yet? Or am
I wrong? I took a look at the GridFile.Content property's set block
and it seems to run trough the provided byte array and create several
FileChunk objects in the do-while loop, but it doesn't put that
FileChunk anywhere as far as I can see...

/// <summary>
/// The content of this file.
/// </summary>
/// <returns></returns>
[MongoIgnore]
public IEnumerable<byte> Content
{
get
{
return this.CachedChunks.SelectMany(y => y.BinaryData);
}
set
{
var hasher = MD5.Create();
this.CachedChunks.Clear();
int takeCount = 0;
int cursor = 0;
int chunkNumber = 0;
do
{
var binary = value.Skip(cursor)
.Take(this.ChunkSize).ToArray();

takeCount = binary.Length;
cursor += takeCount;
if (takeCount > 0)
{
----> var c = new FileChunk();
----> c.ChunkNumber = chunkNumber;
----> c.FileID = this.Id;
----> c.BinaryData = binary;
chunkNumber++;
}
} while (takeCount > 0);
this.Length = cursor;
var hash = hasher.ComputeHash(this.CachedChunks.SelectMany(y
=> y.BinaryData).ToArray());
this.MD5Checksum = BitConverter.ToString(hash).Replace("-",
"");
}
}

What's up with that?

I might just be a duce, but it seems that something is missing?

--
Christian

--
Christian

Christoph Menge

unread,
Sep 21, 2010, 10:12:51 AM9/21/10
to NoRM mongodb
Hi,

correct, there's a line missing:
CachedChunks.Add(c);

In the unstable branch, this is fixed and to my experience, works
quite well.

Hope that helps,
Chris

Christian

unread,
Sep 22, 2010, 3:05:38 AM9/22/10
to NoRM mongodb
Hi Christoph, and others :)

I tried the unstable source and it seems to do the trick, however when
trying to send files that are larger than 16-17 MB it fails with an
IOException and a message that the connection was forcible closed by
the remote host.

This is the stacktrace

ved System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32
offset, Int32 size)
ved Norm.Connection.Write(Byte[] bytes, Int32 start, Int32 size)
ved Norm.Protocol.Messages.QueryMessage`2.Execute()
ved Norm.MongoQueryExecutor`3.<GetEnumerator>d__0.MoveNext()
ved System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1
source)
ved Norm.Collections.MongoCollection`1.FindOne[U](U template)
ved Norm.MongoDatabase.LastError()
ved Norm.Collections.MongoCollection`1.Insert(IEnumerable`1
documentsToInsert)
ved Norm.GridFS.GridFileCollection.Save(GridFile file)
ved EC2.MongoDB.Benchmarks.Program.Main(String[] args) i P:
\Projekter\Privat\EC2.MongoDB.Benchmarks\EC2.MongoDB.Benchmarks
\Program.cs:linje 43
ved System.AppDomain._nExecuteAssembly(Assembly assembly, String[]
args)
ved System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
ved
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
ved System.Threading.ThreadHelper.ThreadStart_Context(Object state)
ved System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
ved System.Threading.ThreadHelper.ThreadStart()

It might point out that the MongoDB service is hosted on an Amazon EC2
micro instance in eastern USA and I'm located in Denmark, so there is
a huge blob of water between my program an the server, but other than
~100 ms pings to the server it performs very well handling 20000+
inserts a second using batch inserts.

Any idea why I get this error? the mongodb-csharp driver does not seem
to have this problem. I'm not seeing anything in the mongodb logs
other than connection opened and closed messages. no errors.

--
Christian

Christian

unread,
Sep 22, 2010, 5:01:32 AM9/22/10
to NoRM mongodb
Hi again

I have now tried the same code on an server located about 3km away and
on localhost with the same result..

--
Christian
Reply all
Reply to author
Forward
0 new messages