Skip to content

Commit

Permalink
Merge branch 'master' of git://github.com/samus/mongodb-csharp
Browse files Browse the repository at this point in the history
Conflicts:
	.gitignore

merging upstream
  • Loading branch information
Sedward committed Mar 10, 2010
2 parents b49f1d9 + 6c4dbe3 commit 8f2276d
Show file tree
Hide file tree
Showing 46 changed files with 2,303 additions and 1,038 deletions.
25 changes: 4 additions & 21 deletions .gitignore
Expand Up @@ -15,28 +15,11 @@
*.orig
*.cache

test-results/*.xml
MongoDB.Net-Tests/bin/*
MongoDB.Net-Tests/test-results/MongoDB.Driver.Tests.csproj.test-cache
MongoDB.Linq.Tests/test-results/MongoDB.Linq.Tests.csproj.test-cache
*/bin/*
*/obj/*
*/test-results/*
/test-results/

*.suo
/_UpgradeReport_Files/*
obj/*
MongoDB.Linq/obj/*
MongoDB.Linq.Tests/bin/*
MongoDB.Linq.Tests/obj/*
MongoDB.Linq.Tests/test-results/*

MongoDB.Driver.Benchmark/bin/*
MongoDB.Driver.Benchmark/ProfilingSessions/*
MongoDB.Driver.Benchmark/obj/*


/MongoDB.GridFS/bin/*
/MongoDB.GridFS/obj/*
/MongoDB.GridFS.Tests/bin/*
/MongoDB.GridFS.Tests/obj/*
/MongoDB.GridFS.Tests/test-results/*

/redist/*.zip
41 changes: 39 additions & 2 deletions MongoDB.GridFS.Tests/GridFileInfoTest.cs
Expand Up @@ -73,8 +73,18 @@ public class GridFileInfoTest
gfi.MoveTo(filename2);
Assert.IsFalse(gf.Exists(filename), "File should have been moved.");
Assert.IsTrue(gf.Exists(filename2), "File wasn't");
Assert.AreEqual(filename2, gfi.FileName, "Filename wasn't set in GridFileInfo");
}


[Test]
public void TestFileExists(){
string filename = "gfi-exists.txt";
GridFileInfo gfi = new GridFileInfo(db["tests"], "gfexists", filename);
Assert.IsFalse(gfi.Exists);
GridFileStream gfs = gfi.Create();
Assert.IsTrue(gfi.Exists);
}

[Test]
public void TestOpenNonExistentFails(){
string filename = "gfi-opennothere.txt";
Expand Down Expand Up @@ -109,7 +119,32 @@ public class GridFileInfoTest
}
Assert.IsTrue(thrown, "NotSupportedException not thrown");
}


[Test]
public void TestUpdateInfo(){
string filename = "gfi-meta.txt";
string fs = "gfinfo";

Object id;
GridFileInfo gfi = new GridFileInfo(db["tests"],fs, filename);
using(GridFileStream gfs = gfi.Create(FileMode.CreateNew)){
id = gfs.GridFileInfo.Id;
gfi.ContentType = "text/sam";
Assert.AreEqual(gfi.ContentType, gfs.GridFileInfo.ContentType, "gridfileinfos don't point to the same object");
TextWriter tw = new StreamWriter(gfs);
tw.WriteLine("test");
tw.Close();
}
gfi.Aliases = new String[]{"file1"};
GridFileInfo gfi2 = new GridFileInfo(db["tests"],fs, filename);
Assert.IsTrue(gfi2.Exists, "Couldn't find " + filename);
Assert.AreEqual("text/sam", gfi2.ContentType);
Assert.AreNotEqual(gfi2.Aliases, gfi.Aliases);
gfi.UpdateInfo();
gfi2.Refresh();
Assert.AreEqual(gfi2.Aliases, gfi.Aliases);
}

[TestFixtureSetUp]
public void Init(){
db.Connect();
Expand All @@ -127,6 +162,8 @@ public class GridFileInfoTest
DropGridFileSystem("gfdelete");
DropGridFileSystem("gfmove");
DropGridFileSystem("gfopen");
DropGridFileSystem("gfexists");
DropGridFileSystem("gfinfo");
}

protected void DropGridFileSystem(string filesystem){
Expand Down
22 changes: 21 additions & 1 deletion MongoDB.GridFS.Tests/GridFileStreamTest.cs
Expand Up @@ -260,7 +260,27 @@ public class GridFileStreamTest
Assert.AreEqual(newsize, gfs.GridFileInfo.Length);
}


[Test]
public void TestReadLengthIsSameAsWriteLength(){
string filename = "readwritelength.txt";
GridFileStream gfs = fs.Create(filename);
int length = 0;
for(int i = 1; i <= 50; i++){
gfs.Write(BitConverter.GetBytes(i), 0, 4);
length += 4;
}
gfs.Close();
Assert.AreEqual(length, gfs.GridFileInfo.Length, "File length written is not the same as in gridfileinfo");

gfs = fs.OpenRead(filename);
byte[] buffer = new byte[16];
int read = 0;
int readLength = read;
while((read = gfs.Read(buffer,0,buffer.Length)) > 0){
readLength += read;
}
Assert.AreEqual(length, readLength, "Too much read back.");
}

#region File API compatibility

Expand Down
19 changes: 17 additions & 2 deletions MongoDB.GridFS/GridFile.cs
Expand Up @@ -118,27 +118,42 @@ public class GridFile{


#region Delete

/// <summary>
/// Permanently removes a file from the database.
/// </summary>
public void Delete(Object id){
files.Delete(new Document().Append("_id",id));
chunks.Delete(new Document().Append("files_id",id));
}

/// <summary>
/// Permanently removes a file from the database.
/// </summary>
public void Delete(String filename){
files.Delete(new Document().Append("filename",filename));
}

/// <summary>
/// Permanently removes all files from the database that match the query.
/// </summary>
public void Delete(Document query ){
foreach(Document doc in ListFiles(query).Documents){
Delete((Oid)doc["_id"]);
}
}
#endregion

#region Exists
#region Exists
/// <summary>
/// Gets a value indicating whether the file exists.
/// </summary>
public Boolean Exists(string name){
return this.files.FindOne(new Document().Append("filename",name)) != null;
}

/// <summary>
/// Gets a value indicating whether the file exists.
/// </summary>
public Boolean Exists(Object id){
return this.files.FindOne(new Document().Append("_id",id)) != null;
}
Expand Down
47 changes: 46 additions & 1 deletion MongoDB.GridFS/GridFileInfo.cs
Expand Up @@ -91,13 +91,15 @@ public string FileName
SetFileDataDefaults(filename);
if(gridFile.Exists(filename)) this.LoadFileData();
}

private void SetFileDataDefaults(string filename){
this.FileName = filename;
this.ChunkSize = DEFAULT_CHUNKSIZE;
this.ContentType = DEFAULT_CONTENT_TYPE;
this.UploadDate = DateTime.UtcNow;
this.Length = 0;
}

#region Create
/// <summary>
/// Creates the file named FileName and returns the GridFileStream
Expand Down Expand Up @@ -131,6 +133,7 @@ public string FileName
}
#endregion

#region Open
/// <summary>
/// Creates a read-only GridFileStream to an existing file.
/// </summary>
Expand Down Expand Up @@ -175,7 +178,11 @@ public string FileName
}
throw new NotImplementedException("Mode not implemented.");
}
#endregion

/// <summary>
/// Permanently removes a file from the database.
/// </summary>
public void Delete(){
if(this.Id != null){
this.gridFile.Delete(this.Id);
Expand All @@ -184,10 +191,26 @@ public string FileName
}
}

/// <summary>
/// Renames a file.
/// </summary>
public void MoveTo(String newFileName){
this.gridFile.Move(this.FileName, newFileName);
this.FileName = newFileName;
}

/// <summary>
/// Gets a value indicating whether the file exists.
/// </summary>
public Boolean Exists{
get{
return this.gridFile.Exists(this.FileName);
}
}

/// <summary>
/// Deletes all data in a file and sets the length to 0.
/// </summary>
public void Truncate(){
if(filedata.Contains("_id") == false) return;
this.gridFile.Chunks.Delete(new Document().Append("files_id", filedata["_id"]));
Expand All @@ -199,7 +222,29 @@ public string FileName
Document doc = this.db.SendCommand(new Document().Append("filemd5", this.Id).Append("root",this.bucket));
return (String)doc["md5"];
}


/// <summary>
/// Updates the aliases, contentType, metadata and uploadDate in the database.
/// </summary>
/// <remarks> To rename a file use the MoveTo method.
/// </remarks>
public void UpdateInfo(){
Document info = new Document(){{"uploadDate", this.UploadDate},
{"aliases", this.Aliases},
{"metadata", this.Metadata},
{"contentType", this.ContentType}};
this.gridFile.Files.Update(new Document(){{"$set",info}}, new Document(){{"_id", this.Id}});
}

/// <summary>
/// Reloads the file information from the database.
/// </summary>
/// <remarks>The data in the database will not reflect any changes done through an open stream until it is closed.
/// </remarks>
public void Refresh(){
LoadFileData();
}

private void LoadFileData(){
Document doc = this.gridFile.Files.FindOne(new Document().Append("filename",this.FileName));
if(doc != null){
Expand Down
4 changes: 4 additions & 0 deletions MongoDB.GridFS/GridFileStream.cs
Expand Up @@ -108,6 +108,10 @@ public class GridFileStream : Stream
}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;
Expand Down

0 comments on commit 8f2276d

Please sign in to comment.