public void TestCastsToDocument() { OidGenerator ogen = new OidGenerator(); DBRef dref = new DBRef("tests.dbrefs", ogen.Generate()); Document doc = (Document)dref; Assert.AreEqual(dref.CollectionName, doc[DBRef.RefName]); }
public void TestGenerate() { OidGenerator ogen = new OidGenerator(); Oid oid = ogen.Generate(); String hex = BitConverter.ToString(oid.ToByteArray()).Replace("-",""); Assert.IsTrue(hex.EndsWith("000001"), "Increment didn't start with 1."); oid = ogen.Generate(); hex = BitConverter.ToString(oid.ToByteArray()).Replace("-",""); Assert.IsTrue(hex.EndsWith("000002"), "Next increment should have been 2"); DateTime created = oid.Created; DateTime now = DateTime.UtcNow; Console.Out.WriteLine(oid.Created); Assert.AreEqual(now.Year, created.Year); Assert.AreEqual(now.Month, created.Month); }
public void TestGenerate() { OidGenerator ogen = new OidGenerator(); Oid oid = ogen.Generate(); String hex = BitConverter.ToString(oid.Value).Replace("-", ""); Assert.IsTrue(hex.EndsWith("000001"), "Increment didn't start with 1."); oid = ogen.Generate(); hex = BitConverter.ToString(oid.Value).Replace("-", ""); Assert.IsTrue(hex.EndsWith("000002"), "Next increment should have been 2"); DateTime created = oid.Created; DateTime now = DateTime.UtcNow; Console.Out.WriteLine(oid.Created); Assert.AreEqual(now.Year, created.Year); Assert.AreEqual(now.Month, created.Month); }
public void TestInsertOfArray() { OidGenerator ogen = new OidGenerator(); IMongoCollection inserts = db["tests"]["inserts"]; Document album = new Document(); album["_id"] = ogen.Generate(); album["artist"] = "Popa Chubby"; album["title"] = "Deliveries After Dark"; album["songs"] = new[] { new Document().Append("title", "Let The Music Set You Free").Append("length", "5:15").Append("_id", ogen.Generate()), new Document().Append("title", "Sally Likes to Run").Append("length", "4:06").Append("_id", ogen.Generate()), new Document().Append("title", "Deliveries After Dark").Append("length", "4:17").Append("_id", ogen.Generate()), new Document().Append("title", "Theme From The Godfather").Append("length", "3:06").Append("_id", ogen.Generate()), new Document().Append("title", "Grown Man Crying Blues").Append("length", "8:09").Append("_id", ogen.Generate()), }; inserts.Insert(album); Document result = inserts.FindOne(new Document().Append("songs.title", "Deliveries After Dark")); Assert.IsNotNull(result); Assert.AreEqual(album.ToString(), result.ToString()); }
public void TestInsertOfArray() { OidGenerator ogen = new OidGenerator(); IMongoCollection inserts = DB["inserts"]; Document album = new Document(); album["_id"] = ogen.Generate(); album["artist"] = "Popa Chubby"; album["title"] = "Deliveries After Dark"; album["songs"] = new[] { new Document().Append("title", "Let The Music Set You Free").Append("length", "5:15").Append("_id", ogen.Generate()), new Document().Append("title", "Sally Likes to Run").Append("length", "4:06").Append("_id", ogen.Generate()), new Document().Append("title", "Deliveries After Dark").Append("length", "4:17").Append("_id", ogen.Generate()), new Document().Append("title", "Theme From The Godfather").Append("length", "3:06").Append("_id", ogen.Generate()), new Document().Append("title", "Grown Man Crying Blues").Append("length", "8:09").Append("_id", ogen.Generate()), }; inserts.Insert(album); Document result = inserts.FindOne(new Document().Append("songs.title","Deliveries After Dark")); Assert.IsNotNull(result); Assert.AreEqual(album.ToString(), result.ToString()); }
private void ChunkFile(byte[] data, string mongoname, bool overwrite, string contentType) { if (string.IsNullOrEmpty(contentType)) { contentType = "file/undef"; if (mongoname.LastIndexOf('.') > 0) { contentType = "file/" + mongoname.Substring(mongoname.LastIndexOf('.') + 1); } } _chunksDocument.Clear(); System.Security.Cryptography.MD5 sscMD5 = System.Security.Cryptography.MD5.Create(); Document file = null; bool closeConnection = false; try { if (this.mongo.Connection.State != ConnectionState.Opened) { this.mongo.Connection.Open(); closeConnection = true; } file = files.FindOne(this.FileDocument); if (file != null) { if (overwrite) { file["length"] = data.Length; file["contentType"] = contentType; file["uploadDate"] = DateTime.Now; file["chunkSize"] = this.ChunkSize; byte[] mHash = sscMD5.ComputeHash(data); file["md5"] = Convert.ToBase64String(mHash); files.Update(file); Document eraser = new Document(); eraser.Add("files_id", file["_id"]); foreach (Document erased in chunks.Find(eraser).Documents) { chunks.Delete(erased); } if (data.Length <= this.ChunkSize) { Chunk chunk = new Chunk(); chunk.FileID = this.Oid; chunk.Number = 0; chunk.Data = data; _chunksDocument.Add(chunk); } else { int chucksNumbers = data.Length / this.ChunkSize + (data.Length % this.ChunkSize > 0 ? 1 : 0); for (int i = 0; i < chucksNumbers; i++) { byte[] buffer = new byte[i < chucksNumbers - 1 ? this.ChunkSize : data.Length % this.ChunkSize]; Array.Copy(data, i * this.ChunkSize, buffer, 0, buffer.Length); Chunk chunk = new Chunk(); chunk.FileID = this.Oid; chunk.Number = i; chunk.Data = buffer; _chunksDocument.Add(chunk); } } } else { throw new Exception("Il file '" + file["filename"] + " esiste nella collection '" + this.files.FullName + "'"); } } else { file = new Document(); OidGenerator oidg = new OidGenerator(); file.Add("_id", oidg.Generate()); file.Add("contentType", contentType); file.Add("length", data.Length); file.Add("chunkSize", DEFAULT_CHUNKSIZE); file.Add("uploadDate", DateTime.Now); byte[] mHash = sscMD5.ComputeHash(data); file.Add("md5", Convert.ToBase64String(mHash)); files.Insert(file); if (data.Length <= this.ChunkSize) { Chunk chunk = new Chunk(); chunk.FileID = this.Oid; chunk.Number = 0; chunk.Data = data; _chunksDocument.Add(chunk); } else { int chucksNumbers = data.Length / this.ChunkSize + (data.Length % this.ChunkSize > 0 ? 1 : 0); for (int i = 0; i < chucksNumbers; i++) { byte[] buffer = new byte[i < chucksNumbers - 1 ? this.ChunkSize : data.Length % this.ChunkSize]; Array.Copy(data, i * this.ChunkSize, buffer, 0, buffer.Length); Chunk chunk = new Chunk(); chunk.FileID = this.Oid; chunk.Number = i; chunk.Data = buffer; _chunksDocument.Add(chunk); } } } this.CreateIndex(); foreach (Chunk c in _chunksDocument) { chunks.Insert(c.Document); } this.FileDocument = file; } catch (Exception) { throw; } finally { if (closeConnection) { this.mongo.Connection.Close(); } } }
public void TestGenerate() { OidGenerator ogen = new OidGenerator(); Oid oid = ogen.Generate(); Console.WriteLine(oid.ToString()); }