Skip to content
Browse files

Initial release on GitHub

  • Loading branch information...
0 parents commit d55dbc196f9f9c12c9b5949e8d627bb7b32d0f71 @samus committed Aug 2, 2009
Showing with 50,041 additions and 0 deletions.
  1. +13 −0 .gitignore
  2. +203 −0 LICENSE
  3. +3 −0 MongoDB.Net-Tests/.gitignore
  4. +46 −0 MongoDB.Net-Tests/Bson/TestBsonConvert.cs
  5. +48 −0 MongoDB.Net-Tests/Bson/TestBsonDate.cs
  6. +120 −0 MongoDB.Net-Tests/Bson/TestBsonDocument.cs
  7. +20 −0 MongoDB.Net-Tests/Bson/TestBsonElement.cs
  8. +91 −0 MongoDB.Net-Tests/Bson/TestBsonReader.cs
  9. +36 −0 MongoDB.Net-Tests/Bson/TestBsonString.cs
  10. +89 −0 MongoDB.Net-Tests/Bson/TestBsonTypes.cs
  11. +33 −0 MongoDB.Net-Tests/IO/TestMsgMessage.cs
  12. +38 −0 MongoDB.Net-Tests/IO/TestQueryMessage.cs
  13. +69 −0 MongoDB.Net-Tests/MongoDB.Driver.Tests.csproj
  14. +46,217 −0 MongoDB.Net-Tests/PartCover/Coverage.Xml
  15. +42 −0 MongoDB.Net-Tests/TestB.cs
  16. +207 −0 MongoDB.Net-Tests/TestCollection.cs
  17. +49 −0 MongoDB.Net-Tests/TestConnection.cs
  18. +93 −0 MongoDB.Net-Tests/TestCursor.cs
  19. +105 −0 MongoDB.Net-Tests/TestDocument.cs
  20. +35 −0 MongoDB.Net-Tests/TestMongo.cs
  21. +99 −0 MongoDB.Net-Tests/test-data/tests.reads.txt
  22. +4 −0 MongoDB.Net-Tests/test-data/tests.smallreads.txt
  23. +40 −0 MongoDBDriver.sln
  24. +3 −0 MongoDBDriver/.gitignore
  25. +36 −0 MongoDBDriver/AssemblyInfo.cs
  26. +52 −0 MongoDBDriver/Bson/BsonBoolean.cs
  27. +101 −0 MongoDBDriver/Bson/BsonConvert.cs
  28. +63 −0 MongoDBDriver/Bson/BsonDate.cs
  29. +151 −0 MongoDBDriver/Bson/BsonDocument.cs
  30. +55 −0 MongoDBDriver/Bson/BsonElement.cs
  31. +51 −0 MongoDBDriver/Bson/BsonInteger.cs
  32. +53 −0 MongoDBDriver/Bson/BsonNumber.cs
  33. +69 −0 MongoDBDriver/Bson/BsonOid.cs
  34. +79 −0 MongoDBDriver/Bson/BsonReader.cs
  35. +60 −0 MongoDBDriver/Bson/BsonString.cs
  36. +81 −0 MongoDBDriver/Bson/BsonWriter.cs
  37. +47 −0 MongoDBDriver/Bson/Types.cs
  38. +135 −0 MongoDBDriver/Collection.cs
  39. +86 −0 MongoDBDriver/Connection.cs
  40. +135 −0 MongoDBDriver/Cursor.cs
  41. +73 −0 MongoDBDriver/Database.cs
  42. +82 −0 MongoDBDriver/Document.cs
  43. +48 −0 MongoDBDriver/IO/DeleteMessage.cs
  44. +64 −0 MongoDBDriver/IO/GetMoreMessage.cs
  45. +47 −0 MongoDBDriver/IO/InsertMessage.cs
  46. +47 −0 MongoDBDriver/IO/KillCursorsMessage.cs
  47. +23 −0 MongoDBDriver/IO/Message.cs
  48. +62 −0 MongoDBDriver/IO/MessageHeader.cs
  49. +33 −0 MongoDBDriver/IO/MsgMessage.cs
  50. +99 −0 MongoDBDriver/IO/QueryMessage.cs
  51. +88 −0 MongoDBDriver/IO/ReplyMessage.cs
  52. +42 −0 MongoDBDriver/IO/RequestMessage.cs
  53. +60 −0 MongoDBDriver/IO/UpdateMessage.cs
  54. +70 −0 MongoDBDriver/Mongo.cs
  55. +85 −0 MongoDBDriver/MongoDB.Driver.csproj
  56. BIN MongoDBDriver/MongoDB.Driver.pidb
  57. +38 −0 MongoDBDriver/MongoDBDriver.csproj
  58. +25 −0 MongoDBDriver/Oid.cs
  59. +2 −0 MongoDBDriver/bin/Debug/.gitignore
  60. +15 −0 MongoDBDriverTranslation/MongoDBDriverTranslation.mdproj
  61. +12 −0 MongoDBDriverTranslation/en.po
  62. +12 −0 MongoDBDriverTranslation/messages.po
  63. +21 −0 Packages.mdproj
  64. +36 −0 README.rst
13 .gitignore
@@ -0,0 +1,13 @@
+
+*~
+*.dll
+*.*~
+*.mdb
+*.pdb
+*.pidb
+
+*.userprefs
+*.usertasks
+
+test-results/*.xml
+MongoDB.Net-Tests/test-results/MongoDB.Driver.Tests.csproj.test-cache
203 LICENSE
@@ -0,0 +1,203 @@
+ Apache License
+ Version 2.0, January 2004
+ http://www.apache.org/licenses/
+
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
+
+ 1. Definitions.
+
+ "License" shall mean the terms and conditions for use, reproduction,
+ and distribution as defined by Sections 1 through 9 of this document.
+
+ "Licensor" shall mean the copyright owner or entity authorized by
+ the copyright owner that is granting the License.
+
+ "Legal Entity" shall mean the union of the acting entity and all
+ other entities that control, are controlled by, or are under common
+ control with that entity. For the purposes of this definition,
+ "control" means (i) the power, direct or indirect, to cause the
+ direction or management of such entity, whether by contract or
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
+ outstanding shares, or (iii) beneficial ownership of such entity.
+
+ "You" (or "Your") shall mean an individual or Legal Entity
+ exercising permissions granted by this License.
+
+ "Source" form shall mean the preferred form for making modifications,
+ including but not limited to software source code, documentation
+ source, and configuration files.
+
+ "Object" form shall mean any form resulting from mechanical
+ transformation or translation of a Source form, including but
+ not limited to compiled object code, generated documentation,
+ and conversions to other media types.
+
+ "Work" shall mean the work of authorship, whether in Source or
+ Object form, made available under the License, as indicated by a
+ copyright notice that is included in or attached to the work
+ (an example is provided in the Appendix below).
+
+ "Derivative Works" shall mean any work, whether in Source or Object
+ form, that is based on (or derived from) the Work and for which the
+ editorial revisions, annotations, elaborations, or other modifications
+ represent, as a whole, an original work of authorship. For the purposes
+ of this License, Derivative Works shall not include works that remain
+ separable from, or merely link (or bind by name) to the interfaces of,
+ the Work and Derivative Works thereof.
+
+ "Contribution" shall mean any work of authorship, including
+ the original version of the Work and any modifications or additions
+ to that Work or Derivative Works thereof, that is intentionally
+ submitted to Licensor for inclusion in the Work by the copyright owner
+ or by an individual or Legal Entity authorized to submit on behalf of
+ the copyright owner. For the purposes of this definition, "submitted"
+ means any form of electronic, verbal, or written communication sent
+ to the Licensor or its representatives, including but not limited to
+ communication on electronic mailing lists, source code control systems,
+ and issue tracking systems that are managed by, or on behalf of, the
+ Licensor for the purpose of discussing and improving the Work, but
+ excluding communication that is conspicuously marked or otherwise
+ designated in writing by the copyright owner as "Not a Contribution."
+
+ "Contributor" shall mean Licensor and any individual or Legal Entity
+ on behalf of whom a Contribution has been received by Licensor and
+ subsequently incorporated within the Work.
+
+ 2. Grant of Copyright License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ copyright license to reproduce, prepare Derivative Works of,
+ publicly display, publicly perform, sublicense, and distribute the
+ Work and such Derivative Works in Source or Object form.
+
+ 3. Grant of Patent License. Subject to the terms and conditions of
+ this License, each Contributor hereby grants to You a perpetual,
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
+ (except as stated in this section) patent license to make, have made,
+ use, offer to sell, sell, import, and otherwise transfer the Work,
+ where such license applies only to those patent claims licensable
+ by such Contributor that are necessarily infringed by their
+ Contribution(s) alone or by combination of their Contribution(s)
+ with the Work to which such Contribution(s) was submitted. If You
+ institute patent litigation against any entity (including a
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
+ or a Contribution incorporated within the Work constitutes direct
+ or contributory patent infringement, then any patent licenses
+ granted to You under this License for that Work shall terminate
+ as of the date such litigation is filed.
+
+ 4. Redistribution. You may reproduce and distribute copies of the
+ Work or Derivative Works thereof in any medium, with or without
+ modifications, and in Source or Object form, provided that You
+ meet the following conditions:
+
+ (a) You must give any other recipients of the Work or
+ Derivative Works a copy of this License; and
+
+ (b) You must cause any modified files to carry prominent notices
+ stating that You changed the files; and
+
+ (c) You must retain, in the Source form of any Derivative Works
+ that You distribute, all copyright, patent, trademark, and
+ attribution notices from the Source form of the Work,
+ excluding those notices that do not pertain to any part of
+ the Derivative Works; and
+
+ (d) If the Work includes a "NOTICE" text file as part of its
+ distribution, then any Derivative Works that You distribute must
+ include a readable copy of the attribution notices contained
+ within such NOTICE file, excluding those notices that do not
+ pertain to any part of the Derivative Works, in at least one
+ of the following places: within a NOTICE text file distributed
+ as part of the Derivative Works; within the Source form or
+ documentation, if provided along with the Derivative Works; or,
+ within a display generated by the Derivative Works, if and
+ wherever such third-party notices normally appear. The contents
+ of the NOTICE file are for informational purposes only and
+ do not modify the License. You may add Your own attribution
+ notices within Derivative Works that You distribute, alongside
+ or as an addendum to the NOTICE text from the Work, provided
+ that such additional attribution notices cannot be construed
+ as modifying the License.
+
+ You may add Your own copyright statement to Your modifications and
+ may provide additional or different license terms and conditions
+ for use, reproduction, or distribution of Your modifications, or
+ for any such Derivative Works as a whole, provided Your use,
+ reproduction, and distribution of the Work otherwise complies with
+ the conditions stated in this License.
+
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
+ any Contribution intentionally submitted for inclusion in the Work
+ by You to the Licensor shall be under the terms and conditions of
+ this License, without any additional terms or conditions.
+ Notwithstanding the above, nothing herein shall supersede or modify
+ the terms of any separate license agreement you may have executed
+ with Licensor regarding such Contributions.
+
+ 6. Trademarks. This License does not grant permission to use the trade
+ names, trademarks, service marks, or product names of the Licensor,
+ except as required for reasonable and customary use in describing the
+ origin of the Work and reproducing the content of the NOTICE file.
+
+ 7. Disclaimer of Warranty. Unless required by applicable law or
+ agreed to in writing, Licensor provides the Work (and each
+ Contributor provides its Contributions) on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
+ implied, including, without limitation, any warranties or conditions
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
+ PARTICULAR PURPOSE. You are solely responsible for determining the
+ appropriateness of using or redistributing the Work and assume any
+ risks associated with Your exercise of permissions under this License.
+
+ 8. Limitation of Liability. In no event and under no legal theory,
+ whether in tort (including negligence), contract, or otherwise,
+ unless required by applicable law (such as deliberate and grossly
+ negligent acts) or agreed to in writing, shall any Contributor be
+ liable to You for damages, including any direct, indirect, special,
+ incidental, or consequential damages of any character arising as a
+ result of this License or out of the use or inability to use the
+ Work (including but not limited to damages for loss of goodwill,
+ work stoppage, computer failure or malfunction, or any and all
+ other commercial damages or losses), even if such Contributor
+ has been advised of the possibility of such damages.
+
+ 9. Accepting Warranty or Additional Liability. While redistributing
+ the Work or Derivative Works thereof, You may choose to offer,
+ and charge a fee for, acceptance of support, warranty, indemnity,
+ or other liability obligations and/or rights consistent with this
+ License. However, in accepting such obligations, You may act only
+ on Your own behalf and on Your sole responsibility, not on behalf
+ of any other Contributor, and only if You agree to indemnify,
+ defend, and hold each Contributor harmless for any liability
+ incurred by, or claims asserted against, such Contributor by reason
+ of your accepting any such warranty or additional liability.
+
+ END OF TERMS AND CONDITIONS
+
+ APPENDIX: How to apply the Apache License to your work.
+
+ To apply the Apache License to your work, attach the following
+ boilerplate notice, with the fields enclosed by brackets "[]"
+ replaced with your own identifying information. (Don't include
+ the brackets!) The text should be enclosed in the appropriate
+ comment syntax for the file format. We also recommend that a
+ file or class name and description of purpose be included on the
+ same "printed page" as the copyright notice for easier
+ identification within third-party archives.
+
+ Copyright [yyyy] [name of copyright owner]
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+
+
3 MongoDB.Net-Tests/.gitignore
@@ -0,0 +1,3 @@
+*.dll
+*.pdb
+*.xml
46 MongoDB.Net-Tests/Bson/TestBsonConvert.cs
@@ -0,0 +1,46 @@
+/*
+ * User: scorder
+ * Date: 7/15/2009
+ */
+
+using System;
+using NUnit.Framework;
+
+using MongoDB.Driver;
+
+namespace MongoDB.Driver.Bson
+{
+ [TestFixture]
+ public class TestBsonConvert
+ {
+ [Test]
+ public void TestFromDocument(){
+ Document doc = new Document();
+ doc.Add("string", "test");
+ doc.Add("int", 1);
+ BsonDocument bdoc = BsonConvert.From(doc);
+ Assert.AreEqual(2,bdoc.Count);
+
+ /*doc.Add("date", DateTime.MinValue);
+ Assert.Throws<ArgumentOutOfRangeException>(
+ delegate {BsonConvert.From(doc);});*/ //Not in nUnit 2.4.
+
+
+ }
+ [Test]
+ public void TestFromInt(){
+ BsonType t = BsonConvert.From(1);
+ Assert.AreEqual(typeof(BsonInteger),t.GetType());
+ Assert.AreEqual(1, ((BsonInteger)t).Val);
+ }
+
+ [Test]
+ public void TestCreate(){
+ BsonString bs = (BsonString)BsonConvert.Create(BsonDataType.String);
+ Assert.IsNotNull(bs);
+
+ BsonDocument bd = (BsonDocument)BsonConvert.Create(BsonDataType.Obj);
+ Assert.IsNotNull(bd);
+ }
+ }
+}
48 MongoDB.Net-Tests/Bson/TestBsonDate.cs
@@ -0,0 +1,48 @@
+/*
+ * User: scorder
+ */
+
+using System;
+using NUnit.Framework;
+
+namespace MongoDB.Driver.Bson
+{
+ [TestFixture]
+ public class TestBsonDate
+ {
+ [Test]
+ public void TestPosixEpochConvertsTo1_1_1970(){
+ long epoch = 0;
+ BsonDate bepoch = new BsonDate(epoch);
+ Assert.AreEqual(0, bepoch.Val);
+ }
+ [Test]
+ public void TestNet1_1_1970IsZero(){
+ DateTime nepoch = new DateTime(1970,1,1,0,0,0,DateTimeKind.Utc);
+ BsonDate bd = new BsonDate(nepoch);
+ Assert.AreEqual(0, bd.Val);
+ }
+
+ [Test]
+ public void TestConversionOfNow(){
+ BsonDate bd1 = new BsonDate(DateTime.Now);
+ BsonDate bd2 = new BsonDate(bd1.Val);
+ Assert.AreEqual(bd1.Val, bd2.Val, "Dates weren't the same value.");
+
+ }
+ [Test]
+ public void TestToNative(){
+ DateTime now = DateTime.Now.ToUniversalTime();
+ BsonDate bd = new BsonDate(now);
+ DateTime bnow = (DateTime)bd.ToNative();
+ Assert.AreEqual(now.Date,bnow.Date, "Native conversion of date failed.");
+
+ //.Net uses fractional milliseconds so there is a precision loss.
+ //Just test the hour, minute, second, and milliseconds
+ Assert.AreEqual(now.Hour, bnow.Hour, "Time differed");
+ Assert.AreEqual(now.Minute, bnow.Minute, "Time differed");
+ Assert.AreEqual(now.Second, bnow.Second, "Time differed");
+ Assert.AreEqual(now.Millisecond, bnow.Millisecond, "Time differed");
+ }
+ }
+}
120 MongoDB.Net-Tests/Bson/TestBsonDocument.cs
@@ -0,0 +1,120 @@
+/*
+ * User: scorder
+ * Date: 7/15/2009
+ */
+using System;
+using System.IO;
+
+using NUnit.Framework;
+
+namespace MongoDB.Driver.Bson
+{
+ /// <summary>
+ /// Description of TestBsonDocument.
+ /// </summary>
+ [TestFixture()]
+ public class TestBsonDocument
+ {
+ [Test()]
+ public void TestAdds(){
+ BsonDocument doc = new BsonDocument();
+ for(int i = 1; i < 6; i++){
+ BsonElement be = new BsonElement(Convert.ToString(i),new BsonInteger(i));
+ doc.Add(be.Name, be);
+ }
+ Assert.AreEqual(5,doc.Count,"Not all elements there");
+
+ doc.Add(new BsonElement("6", new BsonInteger(6)));
+ Assert.AreEqual(6,doc.Count,"Not all elements there");
+
+ doc.Add("7", new BsonInteger(7));
+ Assert.AreEqual(7,doc.Count,"Not all elements there");
+
+ }
+
+ [Test]
+ public void TestSize(){
+ BsonDocument doc = new BsonDocument();
+ Assert.AreEqual(5, doc.Size);
+ doc.Add("test", new BsonString("test"));
+ Assert.AreEqual(20, doc.Size);
+ for(int i = 1; i < 6; i++){
+ doc.Add(Convert.ToString(i),new BsonInteger(i));
+ }
+ Assert.AreEqual(55, doc.Size);
+
+ BsonDocument sub = new BsonDocument();
+ sub.Add("test", new BsonString("sub test"));
+ Assert.AreEqual(24,sub.Size);
+ doc.Add("sub",sub);
+ Assert.AreEqual(84,doc.Size);
+ }
+
+ [Test]
+ public void TestFormatting(){
+ BsonDocument doc = new BsonDocument();
+ MemoryStream buf = new MemoryStream();
+ BsonWriter writer = new BsonWriter(buf);
+
+
+ doc.Add("test", new BsonString("test"));
+ doc.Write(writer);
+ writer.Flush();
+
+ Byte[] output = buf.ToArray();
+ String hexdump = BitConverter.ToString(output);
+ hexdump = hexdump.Replace("-","");
+ Assert.AreEqual(20,output[0],"Size didn't take into count null terminator");
+ Assert.AreEqual("1400000002746573740005000000746573740000",hexdump, "Dump not correct");
+ }
+
+ [Test]
+ public void TestElements(){
+ BsonDocument bdoc = new BsonDocument();
+ MemoryStream buf = new MemoryStream();
+ BsonWriter writer = new BsonWriter(buf);
+
+ Oid oid = new Oid("4a753ad8fac16ea58b290351");
+
+ bdoc.Append("_id", new BsonElement("_id",new BsonOid(oid)))
+ .Append("a", new BsonElement("a",new BsonNumber(1)))
+ .Append("b", new BsonElement("b",new BsonString("test")));
+ bdoc.Write(writer);
+
+
+ writer.Flush();
+
+ Byte[] output = buf.ToArray();
+ String hexdump = BitConverter.ToString(output);
+ hexdump = hexdump.Replace("-","");
+ //0 1 2 3 4 5 6 7 8 9
+ //0123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
+ string expected = "2D000000075F6964004A753AD8FAC16EA58B290351016100000000000000F03F02620005000000746573740000";
+ Assert.AreEqual(expected,hexdump, "Dump not correct");
+ }
+
+ [Test]
+ public void TestNumberElements(){
+ BsonDocument bdoc = new BsonDocument();
+ MemoryStream buf = new MemoryStream();
+ BsonWriter writer = new BsonWriter(buf);
+
+ Oid oid = new Oid("4a75384cfac16ea58b290350");
+
+ bdoc.Append("_id", new BsonElement("_id",new BsonOid(oid)))
+ .Append("a", new BsonElement("a",new BsonNumber(1)))
+ .Append("b", new BsonElement("b",new BsonNumber(2)));
+ bdoc.Write(writer);
+
+
+ writer.Flush();
+
+ Byte[] output = buf.ToArray();
+ String hexdump = BitConverter.ToString(output);
+ hexdump = hexdump.Replace("-","");
+
+ Assert.AreEqual("2C000000075F6964004A75384CFAC16EA58B290350016100000000000000F03F016200000000000000004000",hexdump, "Dump not correct");
+ }
+
+ }
+}
20 MongoDB.Net-Tests/Bson/TestBsonElement.cs
@@ -0,0 +1,20 @@
+/*
+ * User: scorder
+ * Date: 7/21/2009
+ */
+
+using System;
+using NUnit.Framework;
+
+namespace MongoDB.Driver.Bson
+{
+ [TestFixture]
+ public class TestBsonElement
+ {
+ [Test]
+ public void TestMethod()
+ {
+ // TODO: Add your test.
+ }
+ }
+}
91 MongoDB.Net-Tests/Bson/TestBsonReader.cs
@@ -0,0 +1,91 @@
+/*
+ * User: scorder
+ * Date: 7/17/2009
+ */
+using System;
+using System.IO;
+
+using NUnit.Framework;
+
+namespace MongoDB.Driver.Bson
+{
+
+ [TestFixture]
+ public class TestBsonReaderWriter
+ {
+ MemoryStream mem;
+ BsonReader reader;
+ BsonWriter writer;
+
+
+ //Not a true unit test but it will work for being lazy right now.
+ [Test]
+ public void TestReadString(){
+ InitStreams();
+ writer.Write("test");
+ FlushAndGotoBegin();
+
+ Assert.AreEqual("test", reader.ReadString(5));
+
+ long start = mem.Position;
+ String[] values = {"one", "two", "three"};
+ foreach(string val in values){
+ writer.Write(val);
+ }
+ writer.Flush();
+ mem.Seek(start, SeekOrigin.Begin);
+
+ foreach(string val in values){
+ Assert.AreEqual(val,reader.ReadString(val.Length + 1));
+ }
+ }
+
+ [Test]
+ public void TestReadStringNoLength(){
+ InitStreams();
+ writer.Write("test");
+ FlushAndGotoBegin();
+
+ Assert.AreEqual("test", reader.ReadString());
+ }
+
+ [Test]
+ public void TestBoolean(){
+ InitStreams();
+ writer.Write(true);
+ FlushAndGotoBegin();
+
+ Assert.IsTrue(reader.ReadBoolean());
+ }
+
+ [Test]
+ public void TestInteger(){
+ InitStreams();
+ writer.Write(1);
+ FlushAndGotoBegin();
+
+ Assert.AreEqual(1,reader.ReadInt32());
+ }
+
+ [Test]
+ public void TestByte(){
+ byte v = (byte)1;
+ InitStreams();
+ writer.Write(v);
+ FlushAndGotoBegin();
+
+ Assert.AreEqual(v,reader.ReadByte());
+ }
+
+ protected void InitStreams(){
+ mem = new MemoryStream();
+ reader = new BsonReader(mem);
+ writer = new BsonWriter(mem);
+ }
+
+ protected void FlushAndGotoBegin(){
+ writer.Flush();
+ mem.Seek(0,SeekOrigin.Begin);
+ }
+ }
+}
36 MongoDB.Net-Tests/Bson/TestBsonString.cs
@@ -0,0 +1,36 @@
+/*
+ * User: scorder
+ * Date: 7/15/2009
+ */
+
+using System;
+using System.IO;
+
+using NUnit.Framework;
+
+namespace MongoDB.Driver.Bson
+{
+ [TestFixture]
+ public class TestBsonString
+ {
+ [Test]
+ public void TestFormatting(){
+ BsonString str = new BsonString("test");
+ MemoryStream buf = new MemoryStream();
+ BsonWriter writer = new BsonWriter(buf);
+ str.Write(writer);
+ writer.Flush();
+ Byte[] output = buf.ToArray();
+ String hexdump = BitConverter.ToString(output);
+ hexdump = hexdump.Replace("-","");
+ Assert.AreEqual(5,output[0],"Size didn't take into count null terminator");
+ Assert.AreEqual("050000007465737400",hexdump, "Dump not correct");
+ }
+
+ [Test]
+ public void TestSize(){
+ BsonString str = new BsonString("test");
+ Assert.AreEqual(9,str.Size);
+ }
+ }
+}
89 MongoDB.Net-Tests/Bson/TestBsonTypes.cs
@@ -0,0 +1,89 @@
+/*
+ * User: scorder
+ * Date: 7/20/2009
+ */
+
+using System;
+using System.IO;
+
+using NUnit.Framework;
+
+namespace MongoDB.Driver.Bson
+{
+ [TestFixture]
+ public class TestBsonTypes
+ {
+ MemoryStream mem;
+ BsonReader reader;
+ BsonWriter writer;
+
+ [Test]
+ public void TestBsonInteger(){
+ InitStreams();
+ BsonInteger w = new BsonInteger(5);
+ w.Write(writer);
+
+ FlushAndGotoBegin();
+
+ BsonInteger r = new BsonInteger();
+ r.Read(reader);
+
+ Assert.AreEqual(w.Val, r.Val);
+ }
+
+ [Test]
+ public void TestBsonBoolean(){
+ InitStreams();
+ BsonBoolean w = new BsonBoolean(true);
+ w.Write(writer);
+
+ FlushAndGotoBegin();
+
+ BsonBoolean r = new BsonBoolean();
+ r.Read(reader);
+
+ Assert.AreEqual(w.Val, r.Val);
+ }
+
+ [Test]
+ public void TestBsonString(){
+ InitStreams();
+ BsonString w = new BsonString("test");
+ w.Write(writer);
+
+ FlushAndGotoBegin();
+
+ BsonString r = new BsonString();
+ r.Read(reader);
+
+ Assert.AreEqual(w.Val, r.Val);
+ }
+
+
+// [Test]
+// public void TestBsonInteger(){
+// InitStreams();
+// BsonInteger w = new BsonInteger(5);
+// w.Write(writer);
+//
+// FlushAndGotoBegin();
+//
+// BsonInteger r = new BsonInteger();
+// r.Read(reader);
+//
+// Assert.AreEqual(w.Val, r.Val);
+// }
+
+
+ protected void InitStreams(){
+ mem = new MemoryStream();
+ reader = new BsonReader(mem);
+ writer = new BsonWriter(mem);
+ }
+
+ protected void FlushAndGotoBegin(){
+ writer.Flush();
+ mem.Seek(0,SeekOrigin.Begin);
+ }
+ }
+}
33 MongoDB.Net-Tests/IO/TestMsgMessage.cs
@@ -0,0 +1,33 @@
+
+using System;
+using System.IO;
+
+using NUnit.Framework;
+
+namespace MongoDB.Driver.IO
+{
+ [TestFixture()]
+ public class TestMsgMessage
+ {
+
+ [Test()]
+ public void TestAllBytesWritten(){
+ MsgMessage msg = new MsgMessage();
+ msg.Header.RequestId = 1;
+ msg.Header.ResponseTo = 11;
+ Assert.AreEqual(16,msg.Header.MessageLength);
+
+ msg.Message = "A";
+ MemoryStream buffer = new MemoryStream(18);
+ msg.Write(buffer);
+
+ Byte[] output = buffer.ToArray();
+ String hexdump = BitConverter.ToString(output);
+ //Console.WriteLine("Dump: " + hexdump);
+
+ Assert.IsTrue(output.Length > 0);
+ Assert.AreEqual(output.Length, msg.Header.MessageLength);
+ Assert.AreEqual("12-00-00-00-01-00-00-00-0B-00-00-00-E8-03-00-00-41-00", hexdump);
+ }
+ }
+}
38 MongoDB.Net-Tests/IO/TestQueryMessage.cs
@@ -0,0 +1,38 @@
+/*
+ * User: scorder
+ * Date: 7/15/2009
+ */
+
+using System;
+using System.IO;
+
+using NUnit.Framework;
+
+using MongoDB.Driver.Bson;
+
+namespace MongoDB.Driver.IO
+{
+ [TestFixture]
+ public class TestQueryMessage
+ {
+ [Test]
+ public void TestAllBytesWritten()
+ {
+ BsonDocument query = new BsonDocument();
+ query.Add("col1", BsonConvert.From(1));
+
+ QueryMessage msg = new QueryMessage(query,"TestDB.TestCol");
+ MemoryStream buffer = new MemoryStream();
+ msg.Write(buffer);
+
+ Byte[] output = buffer.ToArray();
+ String hexdump = BitConverter.ToString(output);
+ //Console.WriteLine("Dump: " + hexdump);
+
+ Assert.IsTrue(output.Length > 0);
+ Assert.AreEqual("3A-00-00-00-00-00-00-00-00-00-00-00-D4-07-00-00-00-00-00-00-54-65-73-74-44-42-2E-54-65-73-74-43-6F-6C-00-00-00-00-00-00-00-00-00-0F-00-00-00-10-63-6F-6C-31-00-01-00-00-00-00", hexdump);
+
+
+ }
+ }
+}
69 MongoDB.Net-Tests/MongoDB.Driver.Tests.csproj
@@ -0,0 +1,69 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+ <PropertyGroup>
+ <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+ <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+ <ProductVersion>8.0.50727</ProductVersion>
+ <SchemaVersion>2.0</SchemaVersion>
+ <ProjectGuid>{C8BC95AB-25C6-4133-BC9F-8B6BB782CA02}</ProjectGuid>
+ <OutputType>Library</OutputType>
+ <AssemblyName>MongoDB.Driver.Tests</AssemblyName>
+ <SourceAnalysisOverrideSettingsFile>C:\Documents and Settings\scorder\Application Data\ICSharpCode/SharpDevelop3.0\Settings.SourceAnalysis</SourceAnalysisOverrideSettingsFile>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+ <DebugSymbols>true</DebugSymbols>
+ <DebugType>full</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Debug</OutputPath>
+ <DefineConstants>DEBUG</DefineConstants>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+ <DebugType>none</DebugType>
+ <Optimize>false</Optimize>
+ <OutputPath>bin\Release</OutputPath>
+ <ErrorReport>prompt</ErrorReport>
+ <WarningLevel>4</WarningLevel>
+ </PropertyGroup>
+ <ItemGroup>
+ <Reference Include="nunit.core">
+ <Package>nunit-2.4</Package>
+ </Reference>
+ <Reference Include="nunit.framework">
+ <Package>nunit-2.4</Package>
+ </Reference>
+ <Reference Include="System" />
+ </ItemGroup>
+ <ItemGroup>
+ <Compile Include="Bson\TestBsonConvert.cs" />
+ <Compile Include="Bson\TestBsonDate.cs" />
+ <Compile Include="Bson\TestBsonDocument.cs" />
+ <Compile Include="Bson\TestBsonElement.cs" />
+ <Compile Include="Bson\TestBsonReader.cs" />
+ <Compile Include="Bson\TestBsonString.cs" />
+ <Compile Include="Bson\TestBsonTypes.cs" />
+ <Compile Include="IO\TestQueryMessage.cs" />
+ <Compile Include="TestCollection.cs" />
+ <Compile Include="TestCursor.cs" />
+ <Compile Include="TestDocument.cs" />
+ <Compile Include="TestMongo.cs" />
+ <Compile Include="TestConnection.cs" />
+ <Compile Include="IO\TestMsgMessage.cs" />
+ <Compile Include="TestB.cs" />
+ <None Include="test-data\tests.reads.txt" />
+ <None Include="test-data\tests.smallreads.txt" />
+ </ItemGroup>
+ <ItemGroup>
+ <ProjectReference Include="..\MongoDBDriver\MongoDB.Driver.csproj">
+ <Project>{B125BBA6-BFFD-44FA-9254-9B1754CD8AF3}</Project>
+ <Name>MongoDB.Driver</Name>
+ </ProjectReference>
+ <Folder Include="Bson" />
+ <Folder Include="test-data" />
+ </ItemGroup>
+ <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
+ <ItemGroup>
+ <Folder Include="IO\" />
+ </ItemGroup>
+</Project>
46,217 MongoDB.Net-Tests/PartCover/Coverage.Xml
46,217 additions, 0 deletions not shown because the diff is too large. Please use a local Git client to view these changes.
42 MongoDB.Net-Tests/TestB.cs
@@ -0,0 +1,42 @@
+
+using System;
+using System.IO;
+using System.Net;
+using System.Net.Sockets;
+
+
+using NUnit.Framework;
+
+namespace MongoDB.Driver
+{
+
+
+ [TestFixture()]
+ public class TestB
+ {
+
+ [Test()]
+ public void TestCase(){
+ TcpClient client = new TcpClient();
+ client.Connect("localhost", 27017);
+ BufferedStream buff = new BufferedStream(client.GetStream());
+ BinaryWriter writer = new BinaryWriter(buff);
+
+ System.Text.UTF8Encoding encoding=new System.Text.UTF8Encoding();
+ Byte[] msg = encoding.GetBytes("Hello MongoDB!");
+
+
+ writer.Write(16 + msg.Length + 1);
+ writer.Write(1);
+ writer.Write(1);
+ writer.Write(1000);
+ writer.Write(msg);
+ writer.Write((byte)0);
+
+ writer.Flush();
+ writer.Close();
+ client.Close();
+
+ }
+ }
+}
207 MongoDB.Net-Tests/TestCollection.cs
@@ -0,0 +1,207 @@
+/*
+ * User: scorder
+ */
+
+using System;
+using NUnit.Framework;
+using MongoDB.Driver.Bson;
+
+namespace MongoDB.Driver
+{
+ [TestFixture]
+ public class TestCollection
+ {
+ Mongo db = new Mongo();
+
+ [Test]
+ public void TestFindOne(){
+ Document query = new Document();
+ query["j"] = 10;
+ Document result = db["tests"]["reads"].FindOne(query);
+ Assert.IsNotNull(result);
+ Assert.AreEqual(4, result["x"]);
+ Assert.AreEqual(10, result["j"]);
+
+ }
+
+ [Test]
+ public void TestFindOneNotThere(){
+ Document query = new Document();
+ query["not_there"] = 10;
+ Document result = db["tests"]["reads"].FindOne(query);
+ Assert.IsNull(result);
+ }
+
+ [Test]
+ public void TestFindAttributeLimit(){
+ Document query = new Document();
+ query["j"] = 10;
+ Document fields = new Document();
+ fields["x"] = 1;
+
+ Cursor c = db["tests"]["reads"].Find(query,-1,0,fields);
+ foreach(Document result in c.Documents){
+ Assert.IsNotNull(result);
+ Assert.AreEqual(4, result["x"]);
+ Assert.IsNull(result["j"]);
+ }
+ }
+
+ [Test]
+ public void TestSimpleInsert(){
+ Collection inserts = db["tests"]["inserts"];
+ Document indoc = new Document();
+ indoc["song"] = "Palmdale";
+ indoc["artist"] = "Afroman";
+ indoc["year"] = 1999;
+
+ inserts.Insert(indoc);
+
+ Document result = inserts.FindOne(new Document().Append("song","Palmdale"));
+ Assert.IsNotNull(result);
+ Assert.AreEqual(1999,result["year"]);
+ }
+
+ [Test]
+ public void TestReallySimpleInsert(){
+ Collection inserts = db["tests"]["inserts"];
+ Document indoc = new Document();
+ indoc["y"] = 1;
+ indoc["x"] = 2;
+ inserts.Insert(indoc);
+
+ Document result = inserts.FindOne(new Document().Append("x",2));
+ Assert.IsNotNull(result);
+ Assert.AreEqual(1,result["y"]);
+ }
+
+ [Test]
+ public void TestArrayInsert(){
+ Collection inserts = db["tests"]["inserts"];
+ Document indoc1 = new Document();
+ indoc1["song"] = "The Axe";
+ indoc1["artist"] = "Tinsley Ellis";
+ indoc1["year"] = 2006;
+
+ Document indoc2 = new Document();
+ indoc2["song"] = "The Axe2";
+ indoc2["artist"] = "Tinsley Ellis2";
+ indoc2["year"] = 2008;
+
+ inserts.Insert(new Document[]{indoc1,indoc2});
+
+ Document result = inserts.FindOne(new Document().Append("song","The Axe"));
+ Assert.IsNotNull(result);
+ Assert.AreEqual(2006,result["year"]);
+
+ result = inserts.FindOne(new Document().Append("song","The Axe2"));
+ Assert.IsNotNull(result);
+ Assert.AreEqual(2008,result["year"]);
+ }
+
+ [Test]
+ public void TestDelete(){
+ Collection deletes = db["tests"]["deletes"];
+ Document doc = new Document();
+ doc["y"] = 1;
+ doc["x"] = 2;
+ deletes.Insert(doc);
+
+ Document selector = new Document().Append("x",2);
+
+ Document result = deletes.FindOne(selector);
+ Assert.IsNotNull(result);
+ Assert.AreEqual(1,result["y"]);
+
+ deletes.Delete(selector);
+ result = deletes.FindOne(selector);
+ Assert.IsNull(result,"Shouldn't have been able to find a document that was deleted");
+
+ }
+
+ [Test]
+ public void TestUpdateUpsertNotExisting(){
+ Collection updates = db["tests"]["updates"];
+ Document doc = new Document();
+ doc["First"] = "Sam";
+ doc["Last"] = "Corder";
+
+ updates.Update(doc);
+ Document selector = new Document().Append("Last", "Corder");
+ Document result = updates.FindOne(selector);
+ Assert.IsNotNull(result);
+ Assert.AreEqual("Sam", result["First"]);
+ }
+
+ [Test]
+ public void TestUpdateUpsertExisting(){
+ Collection updates = db["tests"]["updates"];
+ Document doc = new Document();
+ doc["First"] = "Mtt";
+ doc["Last"] = "Brewer";
+
+ updates.Insert(doc);
+ Document selector = new Document().Append("Last", "Brewer");
+ doc = updates.FindOne(selector);
+ Assert.IsNotNull(doc);
+ Assert.AreEqual("Mtt", doc["First"]);
+ Assert.IsNotNull(doc["_id"]);
+
+ doc["First"] = "Matt";
+ updates.Update(doc);
+
+ Document result = updates.FindOne(selector);
+ Assert.IsNotNull(result);
+ Assert.AreEqual("Matt", result["First"]);
+
+ }
+
+ [Test]
+ public void TestUpdateMany(){
+ Collection updates = db["tests"]["updates"];
+
+ updates.Insert(new Document().Append("Last", "Cordr").Append("First","Sam"));
+ updates.Insert(new Document().Append("Last", "Cordr").Append("First","Sam2"));
+
+ Document selector = new Document().Append("Last", "Cordr");
+ Cursor results = updates.Find(selector);
+ bool found = false;
+ foreach(Document doc in results.Documents){
+ Assert.AreEqual("Cordr", doc["Last"]);
+ found = true;
+ }
+ Assert.IsTrue(found,"Should have found docs inserted for TestMany");
+
+ Document updateData = new Document().Append("Last", "Corder2");
+ updates.UpdateAll(updateData, selector);
+
+ selector["Last"] = "Corder2";
+ results = updates.Find(selector);
+ found = false;
+ foreach(Document doc in results.Documents){
+ Assert.AreEqual("Corder2", doc["Last"]);
+ Assert.IsNotNull(doc["First"],"First name should not disappear");
+ found = true;
+ }
+ Assert.IsTrue(found,"Should have found docs updated for TestMany");
+ }
+
+ [TestFixtureSetUp]
+ public void Init(){
+ db.Connect();
+ cleanDB();
+ }
+
+ [TestFixtureTearDown]
+ public void Dispose(){
+ //cleanDB();
+ db.Disconnect();
+ }
+
+ protected void cleanDB(){
+ db["tests"]["$cmd"].FindOne(new Document().Append("drop","inserts"));
+
+ db["tests"]["$cmd"].FindOne(new Document().Append("drop","updates"));
+ }
+ }
+}
49 MongoDB.Net-Tests/TestConnection.cs
@@ -0,0 +1,49 @@
+
+using System;
+using NUnit.Framework;
+
+using MongoDB.Driver;
+using MongoDB.Driver.IO;
+using MongoDB.Driver.Bson;
+
+namespace MongoDB.Driver
+{
+
+
+ [TestFixture()]
+ public class TestConnection
+ {
+
+// [Test()]
+// public void TestSendMsgMessage(){
+// Connection conn = new Connection("10.141.153.2");
+// //Connection conn = new Connection();
+// conn.Open();
+// conn.SendMsgMessage("Hello MongoDB!");
+// conn.SendMsgMessage("Hello MongoDB2!");
+// conn.SendMsgMessage("Hello MongoDB3!");
+// conn.SendMsgMessage("Hello MongoDB4!");
+// conn.SendMsgMessage("Hello MongoDB5!");
+// conn.SendMsgMessage("Hello MongoDB6!");
+// conn.SendMsgMessage("Hello MongoDB7!");
+// conn.SendMsgMessage("Hello MongoDB8!");
+// conn.Close();
+// }
+
+ [Test]
+ public void TestSendQueryMessage(){
+ //Connection conn = new Connection("10.141.153.2");
+ Connection conn = new Connection();
+ conn.Open();
+
+ BsonDocument qdoc = new BsonDocument();
+ qdoc.Add("listDatabases", new BsonNumber(1.0));
+ //QueryMessage qmsg = new QueryMessage(qdoc,"system.namespaces");
+ QueryMessage qmsg = new QueryMessage(qdoc,"admin.$cmd");
+ qmsg.NumberToReturn = -1;
+ conn.SendTwoWayMessage(qmsg);
+
+ conn.Close();
+ }
+ }
+}
93 MongoDB.Net-Tests/TestCursor.cs
@@ -0,0 +1,93 @@
+/*
+ * User: scorder
+ */
+
+using System;
+using NUnit.Framework;
+
+using MongoDB.Driver;
+using MongoDB.Driver.IO;
+using MongoDB.Driver.Bson;
+
+namespace MongoDB.Driver
+{
+ [TestFixture]
+ public class TestCursor
+ {
+ Mongo db = new Mongo();
+
+ [TestFixtureSetUp]
+ public void Setup(){
+ db.Connect();
+ }
+
+ [Test]
+ public void TestCanReadSmall()
+ {
+ Cursor c = db["tests"]["smallreads"].FindAll();
+
+ Assert.IsNotNull(c,"Cursor shouldn't be null");
+ int reads = 0;
+ foreach(Document doc in c.Documents){
+ reads++;
+ }
+ Assert.IsTrue(reads > 0, "No documents were returned.");
+ Assert.AreEqual(4, reads, "More than 4 documents in the small reads dataset");
+ }
+ [Test]
+ public void TestCanReadMore(){
+ Cursor c = db["tests"]["reads"].FindAll();
+
+ Assert.IsNotNull(c,"Cursor shouldn't be null");
+ int reads = 0;
+ int idchanges = 0;
+ long id = 0;
+ foreach(Document doc in c.Documents){
+ reads++;
+ if(c.Id != id){
+ idchanges++;
+ id = c.Id;
+ }
+ }
+ Assert.IsTrue(reads > 0, "No documents were returned.");
+ System.Console.Out.Write(String.Format("{0} records read", reads));
+ Assert.IsTrue(idchanges > 0,String.Format("ReadMore message never sent. {0} changes seen", idchanges));
+
+ }
+ [Test]
+ public void TestCanReadAndKillCursor()
+ {
+ Cursor c = db["tests"]["reads"].FindAll();
+
+ Assert.IsNotNull(c,"Cursor shouldn't be null");
+ foreach(Document doc in c.Documents){
+ break;
+ }
+ c.Dispose();
+ Assert.AreEqual(0,c.Id);
+ }
+
+ [Test]
+ public void TestCanLimit()
+ {
+ Cursor c = db["tests"]["reads"].FindAll();
+ c.Limit = 5;
+
+ Assert.IsNotNull(c,"Cursor shouldn't be null");
+ int reads = 0;
+ int idchanges = 0;
+ long id = 0;
+ foreach(Document doc in c.Documents){
+ reads++;
+ }
+ Assert.IsTrue(reads > 0, "No documents were returned.");
+ Assert.AreEqual(5, reads);
+
+ }
+// [Test]
+// public void TestMethod()
+// {
+// Assert.Fail("Write Test");
+// }
+ }
+}
105 MongoDB.Net-Tests/TestDocument.cs
@@ -0,0 +1,105 @@
+/*
+ * User: scorder
+ * Date: 7/8/2009
+ */
+
+using System;
+using System.Collections;
+
+using NUnit.Framework;
+
+using MongoDB.Driver;
+
+namespace MongoDB.Driver
+{
+ [TestFixture]
+ public class TestDocument
+ {
+ [Test]
+ public void TestValuesAdded()
+ {
+ Document d = new Document();
+ d["test"] = 1;
+ Assert.AreEqual(1, d["test"]);
+ }
+
+ [Test]
+ public void TestKeyOrderIsPreserved(){
+ Document d = new Document();
+ d["one"] = 1;
+ d.Add("two", 2);
+ d["three"] = 3;
+ int cnt = 1;
+ foreach(String key in d.Keys){
+ Assert.AreEqual(cnt, d[key]);
+ cnt++;
+ }
+ }
+ [Test]
+ public void TestRemove(){
+ Document d = new Document();
+ d["one"] = 1;
+ d.Remove("one");
+ Assert.IsFalse(d.Contains("one"));
+ }
+
+ [Test]
+ public void TestKeyOrderPreservedOnRemove(){
+ Document d = new Document();
+ d["one"] = 1;
+ d["onepointfive"] = 1.5;
+ d.Add("two", 2);
+ d.Add("two.5", 2.5);
+ d.Remove("two.5");
+ d["three"] = 3;
+ d.Remove("onepointfive");
+ int cnt = 1;
+ foreach(String key in d.Keys){
+ Assert.AreEqual(cnt, d[key]);
+ cnt++;
+ }
+ }
+
+ [Test]
+ public void TestValues(){
+ Document d = new Document();
+ d["one"] = 1;
+ d.Add("two", 2);
+ d["three"] = 3;
+ ICollection vals = d.Values;
+ Assert.AreEqual(3, vals.Count);
+
+ }
+
+ [Test]
+ public void TestCopyToCopiesAndPreservesKeyOrderToEmptyDoc(){
+ Document d = new Document();
+ Document dest = new Document();
+ d["one"] = 1;
+ d.Add("two", 2);
+ d["three"] = 3;
+ d.CopyTo(dest);
+ int cnt = 1;
+ foreach(String key in dest.Keys){
+ Assert.AreEqual(cnt, d[key]);
+ cnt++;
+ }
+ }
+
+ [Test]
+ public void TestCopyToCopiesAndPreservesKeyOrderToExistingDoc(){
+ Document d = new Document();
+ Document dest = new Document();
+ dest["two"] = 200;
+ d["one"] = 1;
+ d.Add("two", 2);
+ d["three"] = 3;
+ d.CopyTo(dest);
+ int cnt = 1;
+ foreach(String key in dest.Keys){
+ Assert.AreEqual(cnt, d[key], "Order wasn't reset on CopyTo");
+ cnt++;
+ }
+ }
+ }
+}
35 MongoDB.Net-Tests/TestMongo.cs
@@ -0,0 +1,35 @@
+
+using System;
+
+using NUnit.Framework;
+
+using MongoDB.Driver;
+
+namespace MongoDB.Driver
+{
+
+
+ [TestFixture()]
+ public class TestMongo
+ {
+
+ [Test()]
+ public void TestDefaults()
+ {
+ Mongo m = new Mongo();
+ Assert.AreEqual("localhost", m.Host);
+ Assert.AreEqual(27017, m.Port);
+ }
+
+ [Test()]
+ public void TestExplicitConnection(){
+ Mongo m = new Mongo();
+ Assert.IsTrue(m.Connect());
+ }
+
+ [Test()]
+ public void TestThatConnectMustBeCalled(){
+ Assert.Fail("Write Test");
+ }
+ }
+}
99 MongoDB.Net-Tests/test-data/tests.reads.txt
@@ -0,0 +1,99 @@
+{ "_id" : "4a6a1f53587600000000288a", "x" : 4, "j" : 1 }
+{ "_id" : "4a6a1f53587600000000288b", "x" : 4, "j" : 2 }
+{ "_id" : "4a6a1f53587600000000288c", "x" : 4, "j" : 3 }
+{ "_id" : "4a6a1f53587600000000288d", "x" : 4, "j" : 4 }
+{ "_id" : "4a6a1f53587600000000288e", "x" : 4, "j" : 5 }
+{ "_id" : "4a6a1f53587600000000288f", "x" : 4, "j" : 6 }
+{ "_id" : "4a6a1f535876000000002890", "x" : 4, "j" : 7 }
+{ "_id" : "4a6a1f535876000000002891", "x" : 4, "j" : 8 }
+{ "_id" : "4a6a1f535876000000002892", "x" : 4, "j" : 9 }
+{ "_id" : "4a6a1f535876000000002893", "x" : 4, "j" : 10 }
+{ "_id" : "4a6a1f535876000000002894", "x" : 4, "j" : 11 }
+{ "_id" : "4a6a1f535876000000002895", "x" : 4, "j" : 12 }
+{ "_id" : "4a6a1f535876000000002896", "x" : 4, "j" : 13 }
+{ "_id" : "4a6a1f535876000000002897", "x" : 4, "j" : 14 }
+{ "_id" : "4a6a1f535876000000002898", "x" : 4, "j" : 15 }
+{ "_id" : "4a6a1f535876000000002899", "x" : 4, "j" : 16 }
+{ "_id" : "4a6a1f53587600000000289a", "x" : 4, "j" : 17 }
+{ "_id" : "4a6a1f53587600000000289b", "x" : 4, "j" : 18 }
+{ "_id" : "4a6a1f53587600000000289c", "x" : 4, "j" : 19 }
+{ "_id" : "4a6a1f53587600000000289d", "x" : 4, "j" : 20 }
+{ "_id" : "4a6a1f53587600000000289e", "x" : 4, "j" : 21 }
+{ "_id" : "4a6a1f53587600000000289f", "x" : 4, "j" : 22 }
+{ "_id" : "4a6a1f5358760000000028a0", "x" : 4, "j" : 23 }
+{ "_id" : "4a6a1f5358760000000028a1", "x" : 4, "j" : 24 }
+{ "_id" : "4a6a1f5358760000000028a2", "x" : 4, "j" : 25 }
+{ "_id" : "4a6a1f5358760000000028a3", "x" : 4, "j" : 26 }
+{ "_id" : "4a6a1f5358760000000028a4", "x" : 4, "j" : 27 }
+{ "_id" : "4a6a1f5358760000000028a5", "x" : 4, "j" : 28 }
+{ "_id" : "4a6a1f5358760000000028a6", "x" : 4, "j" : 29 }
+{ "_id" : "4a6a1f5358760000000028a7", "x" : 4, "j" : 30 }
+{ "_id" : "4a6a1f5358760000000028a8", "x" : 4, "j" : 31 }
+{ "_id" : "4a6a1f5358760000000028a9", "x" : 4, "j" : 32 }
+{ "_id" : "4a6a1f5358760000000028aa", "x" : 4, "j" : 33 }
+{ "_id" : "4a6a1f5358760000000028ab", "x" : 4, "j" : 34 }
+{ "_id" : "4a6a1f5358760000000028ac", "x" : 4, "j" : 35 }
+{ "_id" : "4a6a1f5358760000000028ad", "x" : 4, "j" : 36 }
+{ "_id" : "4a6a1f5358760000000028ae", "x" : 4, "j" : 37 }
+{ "_id" : "4a6a1f5358760000000028af", "x" : 4, "j" : 38 }
+{ "_id" : "4a6a1f5358760000000028b0", "x" : 4, "j" : 39 }
+{ "_id" : "4a6a1f5358760000000028b1", "x" : 4, "j" : 40 }
+{ "_id" : "4a6a1f5358760000000028b2", "x" : 4, "j" : 41 }
+{ "_id" : "4a6a1f5358760000000028b3", "x" : 4, "j" : 42 }
+{ "_id" : "4a6a1f5358760000000028b4", "x" : 4, "j" : 43 }
+{ "_id" : "4a6a1f5358760000000028b5", "x" : 4, "j" : 44 }
+{ "_id" : "4a6a1f5358760000000028b6", "x" : 4, "j" : 45 }
+{ "_id" : "4a6a1f5358760000000028b7", "x" : 4, "j" : 46 }
+{ "_id" : "4a6a1f5358760000000028b8", "x" : 4, "j" : 47 }
+{ "_id" : "4a6a1f5358760000000028b9", "x" : 4, "j" : 48 }
+{ "_id" : "4a6a1f5358760000000028ba", "x" : 4, "j" : 49 }
+{ "_id" : "4a6a1f5358760000000028bb", "x" : 4, "j" : 50 }
+{ "_id" : "4a6a1f5358760000000028bc", "x" : 4, "j" : 51 }
+{ "_id" : "4a6a1f5358760000000028bd", "x" : 4, "j" : 52 }
+{ "_id" : "4a6a1f5358760000000028be", "x" : 4, "j" : 53 }
+{ "_id" : "4a6a1f5358760000000028bf", "x" : 4, "j" : 54 }
+{ "_id" : "4a6a1f5358760000000028c0", "x" : 4, "j" : 55 }
+{ "_id" : "4a6a1f5358760000000028c1", "x" : 4, "j" : 56 }
+{ "_id" : "4a6a1f5358760000000028c2", "x" : 4, "j" : 57 }
+{ "_id" : "4a6a1f5358760000000028c3", "x" : 4, "j" : 58 }
+{ "_id" : "4a6a1f5358760000000028c4", "x" : 4, "j" : 59 }
+{ "_id" : "4a6a1f5358760000000028c5", "x" : 4, "j" : 60 }
+{ "_id" : "4a6a1f5358760000000028c6", "x" : 4, "j" : 61 }
+{ "_id" : "4a6a1f5358760000000028c7", "x" : 4, "j" : 62 }
+{ "_id" : "4a6a1f5358760000000028c8", "x" : 4, "j" : 63 }
+{ "_id" : "4a6a1f5358760000000028c9", "x" : 4, "j" : 64 }
+{ "_id" : "4a6a1f5358760000000028ca", "x" : 4, "j" : 65 }
+{ "_id" : "4a6a1f5358760000000028cb", "x" : 4, "j" : 66 }
+{ "_id" : "4a6a1f5358760000000028cc", "x" : 4, "j" : 67 }
+{ "_id" : "4a6a1f5358760000000028cd", "x" : 4, "j" : 68 }
+{ "_id" : "4a6a1f5358760000000028ce", "x" : 4, "j" : 69 }
+{ "_id" : "4a6a1f5358760000000028cf", "x" : 4, "j" : 70 }
+{ "_id" : "4a6a1f5358760000000028d0", "x" : 4, "j" : 71 }
+{ "_id" : "4a6a1f5358760000000028d1", "x" : 4, "j" : 72 }
+{ "_id" : "4a6a1f5358760000000028d2", "x" : 4, "j" : 73 }
+{ "_id" : "4a6a1f5358760000000028d3", "x" : 4, "j" : 74 }
+{ "_id" : "4a6a1f5358760000000028d4", "x" : 4, "j" : 75 }
+{ "_id" : "4a6a1f5358760000000028d5", "x" : 4, "j" : 76 }
+{ "_id" : "4a6a1f5358760000000028d6", "x" : 4, "j" : 77 }
+{ "_id" : "4a6a1f5358760000000028d7", "x" : 4, "j" : 78 }
+{ "_id" : "4a6a1f5358760000000028d8", "x" : 4, "j" : 79 }
+{ "_id" : "4a6a1f5358760000000028d9", "x" : 4, "j" : 80 }
+{ "_id" : "4a6a1f5358760000000028da", "x" : 4, "j" : 81 }
+{ "_id" : "4a6a1f5358760000000028db", "x" : 4, "j" : 82 }
+{ "_id" : "4a6a1f5358760000000028dc", "x" : 4, "j" : 83 }
+{ "_id" : "4a6a1f5358760000000028dd", "x" : 4, "j" : 84 }
+{ "_id" : "4a6a1f5358760000000028de", "x" : 4, "j" : 85 }
+{ "_id" : "4a6a1f5358760000000028df", "x" : 4, "j" : 86 }
+{ "_id" : "4a6a1f5358760000000028e0", "x" : 4, "j" : 87 }
+{ "_id" : "4a6a1f5358760000000028e1", "x" : 4, "j" : 88 }
+{ "_id" : "4a6a1f5358760000000028e2", "x" : 4, "j" : 89 }
+{ "_id" : "4a6a1f5358760000000028e3", "x" : 4, "j" : 90 }
+{ "_id" : "4a6a1f5358760000000028e4", "x" : 4, "j" : 91 }
+{ "_id" : "4a6a1f5358760000000028e5", "x" : 4, "j" : 92 }
+{ "_id" : "4a6a1f5358760000000028e6", "x" : 4, "j" : 93 }
+{ "_id" : "4a6a1f5358760000000028e7", "x" : 4, "j" : 94 }
+{ "_id" : "4a6a1f5358760000000028e8", "x" : 4, "j" : 95 }
+{ "_id" : "4a6a1f5358760000000028e9", "x" : 4, "j" : 96 }
+{ "_id" : "4a6a1f5358760000000028ea", "x" : 4, "j" : 97 }
+{ "_id" : "4a6a1f5358760000000028eb", "x" : 4, "j" : 98 }
+{ "_id" : "4a6a1f5358760000000028ec", "x" : 4, "j" : 99 }
4 MongoDB.Net-Tests/test-data/tests.smallreads.txt
@@ -0,0 +1,4 @@
+{ "_id" : "4a6a1f7f58760000000028ed", "x" : 4, "j" : 1 }
+{ "_id" : "4a6a1f7f58760000000028ee", "x" : 4, "j" : 2 }
+{ "_id" : "4a6a1f7f58760000000028ef", "x" : 4, "j" : 3 }
+{ "_id" : "4a6a1f7f58760000000028f0", "x" : 4, "j" : 4 }
40 MongoDBDriver.sln
@@ -0,0 +1,40 @@
+
+Microsoft Visual Studio Solution File, Format Version 9.00
+# Visual Studio 2005
+# SharpDevelop 3.1.0.4077
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongoDB.Driver", "MongoDBDriver\MongoDB.Driver.csproj", "{B125BBA6-BFFD-44FA-9254-9B1754CD8AF3}"
+EndProject
+Project("{9344bdbb-3e7f-41fc-a0dd-8665d75ee146}") = "Packages", "Packages.mdproj", "{502F3381-58AA-461B-B9D8-12578A588C61}"
+EndProject
+Project("{9344bdbb-3e7f-41fc-a0dd-8665d75ee146}") = "MongoDBDriverTranslation", "MongoDBDriverTranslation\MongoDBDriverTranslation.mdproj", "{DCBE47DD-59A6-4212-AA4A-142838088B69}"
+EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MongoDB.Driver.Tests", "MongoDB.Net-Tests\MongoDB.Driver.Tests.csproj", "{C8BC95AB-25C6-4133-BC9F-8B6BB782CA02}"
+EndProject
+Global
+ GlobalSection(SolutionConfigurationPlatforms) = preSolution
+ Debug|Any CPU = Debug|Any CPU
+ Release|Any CPU = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(ProjectConfigurationPlatforms) = postSolution
+ {502F3381-58AA-461B-B9D8-12578A588C61}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {502F3381-58AA-461B-B9D8-12578A588C61}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {502F3381-58AA-461B-B9D8-12578A588C61}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {502F3381-58AA-461B-B9D8-12578A588C61}.Release|Any CPU.Build.0 = Release|Any CPU
+ {B125BBA6-BFFD-44FA-9254-9B1754CD8AF3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {B125BBA6-BFFD-44FA-9254-9B1754CD8AF3}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {B125BBA6-BFFD-44FA-9254-9B1754CD8AF3}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {B125BBA6-BFFD-44FA-9254-9B1754CD8AF3}.Release|Any CPU.Build.0 = Release|Any CPU
+ {C8BC95AB-25C6-4133-BC9F-8B6BB782CA02}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {C8BC95AB-25C6-4133-BC9F-8B6BB782CA02}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {C8BC95AB-25C6-4133-BC9F-8B6BB782CA02}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {C8BC95AB-25C6-4133-BC9F-8B6BB782CA02}.Release|Any CPU.Build.0 = Release|Any CPU
+ {DCBE47DD-59A6-4212-AA4A-142838088B69}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+ {DCBE47DD-59A6-4212-AA4A-142838088B69}.Debug|Any CPU.Build.0 = Debug|Any CPU
+ {DCBE47DD-59A6-4212-AA4A-142838088B69}.Release|Any CPU.ActiveCfg = Release|Any CPU
+ {DCBE47DD-59A6-4212-AA4A-142838088B69}.Release|Any CPU.Build.0 = Release|Any CPU
+ EndGlobalSection
+ GlobalSection(MonoDevelopProperties) = preSolution
+ version = 0.1
+ StartupItem = MongoDBDriver\MongoDB.Driver.csproj
+ EndGlobalSection
+EndGlobal
3 MongoDBDriver/.gitignore
@@ -0,0 +1,3 @@
+*.xml
+*.dll
+*.mdb
36 MongoDBDriver/AssemblyInfo.cs
@@ -0,0 +1,36 @@
+using System;
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Security.Permissions;
+
+
+// Information about this assembly is defined by the following attributes.
+// Change them to the values specific to your project.
+
+[assembly: AssemblyTitle("MongoDBDriver")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("")]
+[assembly: AssemblyCopyright("")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
+// The form "{Major}.{Minor}.*" will automatically update the build and revision,
+// and "{Major}.{Minor}.{Build}.*" will update just the revision.
+
+[assembly: AssemblyVersion("1.0.*")]
+
+// The following attributes are used to specify the signing key for the assembly,
+// if desired. See the Mono documentation for more information about signing.
+
+[assembly: AssemblyDelaySign(false)]
+[assembly: AssemblyKeyFile("")]
+
+[assembly: System.Runtime.InteropServices.ComVisible(false)]
+[assembly: CLSCompliantAttribute(true)]
+
+[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Bson")]
+[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Bson")]
+[assembly: SecurityPermission(SecurityAction.RequestMinimum, Execution = true)]
52 MongoDBDriver/Bson/BsonBoolean.cs
@@ -0,0 +1,52 @@
+/*
+ * User: scorder
+ * Date: 7/15/2009
+ */
+using System;
+
+namespace MongoDB.Driver.Bson
+{
+ /// <summary>
+ /// Description of BsonBoolean.
+ /// </summary>
+ public class BsonBoolean : BsonType
+ {
+ bool val;
+ public bool Val {
+ get { return val; }
+ set { val = value; }
+ }
+
+ public BsonBoolean(){}
+
+ public BsonBoolean(bool val){
+ this.Val = val;
+ }
+
+ public byte TypeNum {
+ get {return (byte)BsonDataType.Boolean;}
+ }
+
+ public int Size{
+ get {return 1;}
+ }
+
+ public void Write(BsonWriter writer){
+ writer.Write(this.Val);
+ }
+
+ public int Read(BsonReader reader){
+ this.val = reader.ReadBoolean();
+ return 1;
+ }
+
+ public override string ToString ()
+ {
+ return string.Format("[BsonBoolean: Val={0}, TypeNum={1}, Size={2}]", Val, TypeNum, Size);
+ }
+
+
+ public object ToNative(){
+ return this.Val;
+ }
+ }
101 MongoDBDriver/Bson/BsonConvert.cs
@@ -0,0 +1,101 @@
+/*
+ * User: scorder
+ * Date: 7/15/2009
+ */
+using System;
+
+using MongoDB.Driver;
+
+namespace MongoDB.Driver.Bson
+{
+ /// <summary>
+ /// Description of BsonConvert.
+ /// </summary>
+ public static class BsonConvert
+ {
+
+ public static BsonType From(Object val){
+ Type t = val.GetType();
+ BsonType ret = null;
+ if(t == typeof(Double)){
+ ret = From((double)val);
+ }else if(t == typeof(Single)){
+ ret = From((double)val);
+ }else if(t == typeof(String)){
+ ret = From((String)val);
+ }else if(t == typeof(Document)){
+ ret = From((Document)val);
+ }else if(t == typeof(int)){
+ ret = From((int)val);
+ }else if(t == typeof(bool)){
+ ret = From((bool)val);
+ }else if(t == typeof(Oid)){
+ ret = From((Oid)val);
+ }else if(t == typeof(DateTime)){
+ ret = From((DateTime)val);
+ }else{
+ throw new ArgumentOutOfRangeException("Type not recognized");
+ }
+
+ return ret;
+ }
+
+ public static BsonDocument From(Document doc){
+ BsonDocument bdoc = new BsonDocument();
+ foreach(String key in doc.Keys){
+ bdoc.Add(key, From(doc[key]));
+ }
+ return bdoc;
+ }
+
+ public static BsonOid From(Oid val){
+ return new BsonOid(val);
+ }
+
+ public static BsonInteger From(int val){
+ return new BsonInteger(val);
+ }
+
+ public static BsonNumber From(double val){
+ return new BsonNumber(val);
+ }
+
+ public static BsonString From(string val){
+ return new BsonString(val);
+ }
+
+ public static BsonBoolean From(bool val){
+ return new BsonBoolean(val);
+ }
+
+ public static BsonDate From(DateTime val){
+ return new BsonDate(val);
+ }
+
+ public static BsonType Create(BsonDataType type){
+ BsonType ret = null;
+ if(type == BsonDataType.Number){
+ ret = new BsonNumber();
+ }else if(type == BsonDataType.Number){
+ throw new NotImplementedException();
+ }else if(type == BsonDataType.String){
+ ret = new BsonString();
+ }else if(type == BsonDataType.Obj){
+ ret = new BsonDocument();
+ }else if(type == BsonDataType.Array){
+ ret = new BsonDocument();
+ }else if(type == BsonDataType.Integer){
+ ret = new BsonInteger();
+ }else if(type == BsonDataType.Boolean){
+ ret = new BsonBoolean();
+ }else if(type == BsonDataType.Oid){
+ ret = new BsonOid();
+ }else if(type == BsonDataType.Date){
+ ret = new BsonDate();
+ }else{
+ throw new ArgumentOutOfRangeException("Type: " + type + " not recognized");
+ }
+ return ret;
+ }
+ }
+}
63 MongoDBDriver/Bson/BsonDate.cs
@@ -0,0 +1,63 @@
+/*
+ * User: scorder
+ */
+using System;
+
+namespace MongoDB.Driver.Bson
+{
+ /// <summary>
+ /// Description of BsonDate.
+ /// </summary>
+ public class BsonDate:BsonType
+ {
+ private static DateTime epoch = new DateTime(1970,1,1,0,0,0,DateTimeKind.Utc);
+
+ private long val;
+
+ public long Val {
+ get {return val;}
+ set {val = value;}
+ }
+
+ public byte TypeNum {
+ get{
+ return (byte)BsonDataType.Date;
+ }
+ }
+
+ public int Size {
+ get {
+ return sizeof(long);
+ }
+ }
+
+ public BsonDate(){}
+
+ public BsonDate(long val){
+ this.Val = val;
+ }
+
+ public BsonDate(DateTime val){
+ TimeSpan diff = val.ToUniversalTime() - epoch;
+ double time = Math.Floor(diff.TotalMilliseconds);
+ this.Val = (long)time;
+ }
+
+ public void Write(BsonWriter writer){
+ writer.Write(this.Val);
+ }
+
+ public int Read(BsonReader reader){
+ this.Val = reader.ReadInt64();
+ return this.Size;
+ }
+
+ public override string ToString (){
+ return string.Format("[BsonDate: Val={0}, TypeNum={1}, Size={2}]", Val, TypeNum, Size);
+ }
+
+ public object ToNative(){
+ return epoch.AddMilliseconds(this.Val);
+ }
+ }
+}
151 MongoDBDriver/Bson/BsonDocument.cs
@@ -0,0 +1,151 @@
+/*
+ * User: scorder
+ * Date: 7/7/2009
+ */
+using System;
+using System.Collections;
+using System.Collections.Generic;
+using System.Text;
+
+using MongoDB.Driver;
+
+namespace MongoDB.Driver.Bson
+{
+ /// <summary>
+ /// Description of BSONDocument.
+ /// </summary>
+ public class BsonDocument : System.Collections.DictionaryBase, BsonType
+ {
+ private List<String> orderedKeys = new List<String>();
+
+ public BsonDocument(){
+ }
+ public BsonElement this[ String key ] {
+ get{
+ return (BsonElement)Dictionary[key];
+ }
+ set{
+ if(orderedKeys.Contains(key) == false){
+ orderedKeys.Add(key);
+ }
+ Dictionary[key] = value;
+ }
+ }
+
+ public ICollection Keys {
+ get {
+ return(orderedKeys);
+ }
+ }
+
+ public ICollection Values {
+ get {
+ return( Dictionary.Values );
+ }
+ }
+
+ public void Add( String key, BsonElement value ) {
+ if(orderedKeys.Contains(key)) throw new ArgumentException("Key already exists");
+ this[key] = value;
+ }
+ public void Add(BsonElement value){
+ this.Add(value.Name, value);
+ }
+ public void Add(String key, BsonType val){
+ this.Add(new BsonElement(key, val));
+ }
+
+ public BsonDocument Append(String key, BsonElement value){
+ this.Add(key,value);
+ return this;
+ }
+
+ public BsonDocument Update(BsonDocument from){
+ if(from == null) return this;
+ foreach(String key in from.Keys){
+ this[key] = from[key];
+ }
+ return this;
+ }
+
+ public bool Contains( String key ) {
+ return( orderedKeys.Contains( key ) );
+ }
+
+ public void Remove( String key ) {
+ Dictionary.Remove( key );
+ orderedKeys.Remove(key);
+ }
+
+ public byte TypeNum {
+ get {return (byte)BsonDataType.Obj;}
+ }
+
+ public int Size {
+ get {
+ int size = 4;
+ foreach(String key in this.Keys){
+ BsonElement be = this[key];
+ size += be.Size;
+ }
+ size += 1; //Object terminator
+ return size;
+ }
+ }
+
+ public void Write(BsonWriter writer){
+ writer.Write(this.Size);
+ foreach(String key in this.Keys){
+ BsonElement be = this[key];
+ be.Write(writer);
+ }
+ writer.Write((byte)0);
+ }
+
+// public void Read(BsonReader reader){
+// int size = reader.ReadInt32();
+// foreach(String key in this.Keys){
+// BsonElement be = this[key];
+// be.Read(reader);
+// }
+// byte eoo = reader.ReadByte();
+// size++;
+// if(size != this.Size) throw new System.IO.InvalidDataException("Document is not the same size as the stream reported");
+// }
+
+ public int Read(BsonReader reader){
+ int size = reader.ReadInt32();
+ int bytesRead = 4;
+// try{
+ while(bytesRead + 1 < size){
+ BsonElement be = new BsonElement();
+ bytesRead += be.Read(reader);
+ this.Add(be);
+ }
+ byte eoo = reader.ReadByte();
+ bytesRead++;
+ if(eoo != (byte)0) throw new System.IO.InvalidDataException("Document not null terminated");
+ if(size != bytesRead) {
+ throw new System.IO.InvalidDataException(string.Format("Should have read {0} bytes from stream but only read {1}]", size, bytesRead));
+ }
+// }catch(ArgumentOutOfRangeException aor){
+// throw new System.IO.InvalidDataException("Document out of sync with stream bytesRead: " + bytesRead + " size: " + size + " Doc: " + this.ToString());
+// }
+ return bytesRead;
+ }
+
+ public object ToNative(){
+ Document doc = new Document();
+ foreach(String key in this.Keys){
+ BsonElement be = this[key];
+ doc[key] = be.Val.ToNative();
+ }
+ return doc;
+ }
+
+ public override string ToString ()
+ {
+ return string.Format("[BsonDocument: TypeNum={0}, Size={1}]", TypeNum, Size);
+ }
+ }
+}
55 MongoDBDriver/Bson/BsonElement.cs
@@ -0,0 +1,55 @@
+
+using System;
+
+namespace MongoDB.Driver.Bson
+{
+
+
+ public class BsonElement
+ {
+ private string name;
+ public string Name {
+ get {return name;}
+ set {name = value;}
+ }
+
+ private BsonType val;
+ public BsonType Val {
+ get {return val;}
+ set {val = value;}
+ }
+
+ public BsonElement()
+ {}
+
+ public BsonElement(String name, BsonType val){
+ this.Name = name;
+ this.Val = val;
+ }
+
+ public int Size{
+ get{
+ Int32 ret = 1; //TypeNum
+ ret += this.Name.Length + 1; //Name
+ ret += this.Val.Size; //Object data
+ return ret;
+ }
+ }
+
+ public void Write(BsonWriter writer){
+ writer.Write(this.Val.TypeNum);
+ writer.Write(this.Name);
+ this.Val.Write(writer);
+ }
+
+ public int Read(BsonReader reader){
+ sbyte typeNum = (sbyte)reader.ReadByte();
+ int bytesRead;
+ this.Name = reader.ReadString();
+ this.Val = BsonConvert.Create((BsonDataType)typeNum);
+ bytesRead = this.Val.Read(reader);
+ bytesRead += (1 + this.Name.Length + 1); //type byte & name + term
+ return bytesRead;
+ }
+ }
+}
51 MongoDBDriver/Bson/BsonInteger.cs
@@ -0,0 +1,51 @@
+/*
+ * User: scorder
+ * Date: 7/15/2009
+ */
+using System;
+
+namespace MongoDB.Driver.Bson
+{
+ /// <summary>
+ /// Description of BsonInteger.
+ /// </summary>
+ public class BsonInteger : BsonType
+ {
+ int val;
+ public int Val {
+ get { return val; }
+ set { val = value; }
+ }
+ public int Size {
+ get {return 4;}
+ }
+
+ public byte TypeNum {
+ get {return (byte)BsonDataType.Integer;}
+ }
+
+ public BsonInteger(){}
+
+ public BsonInteger(int val){
+ this.Val = val;
+ }
+
+ public void Write(BsonWriter writer){
+ writer.Write(this.Val);
+ }
+
+ public int Read(BsonReader reader){
+ this.val = reader.ReadInt32();
+ return this.Size;
+ }
+
+ public override string ToString ()
+ {
+ return string.Format("[BsonInteger: Val={0}, Size={1}, TypeNum={2}]", Val, Size, TypeNum);
+ }
+
+ public object ToNative(){
+ return this.Val;
+ }
+ }
+}
53 MongoDBDriver/Bson/BsonNumber.cs
@@ -0,0 +1,53 @@
+/*
+ * User: scorder
+ * Date: 7/22/2009
+ */
+using System;
+
+namespace MongoDB.Driver.Bson
+{
+ /// <summary>
+ /// Description of BsonDouble.
+ /// </summary>
+ public class BsonNumber : BsonType
+ {
+ double val;
+ public double Val {
+ get { return val; }
+ set { val = value; }
+ }
+ public int Size {
+ get {return sizeof(double);}
+ }
+
+ public byte TypeNum {
+ get {return (byte)BsonDataType.Number;}
+ }
+
+ public BsonNumber(){}
+
+ public BsonNumber(double val){
+ this.Val = val;
+ }
+
+ public void Write(BsonWriter writer){
+ writer.Write(this.Val);
+ }
+
+ public int Read(BsonReader reader){
+ this.val = reader.ReadDouble();
+ return this.Size;
+ }
+
+ public override string ToString ()
+ {
+ return string.Format("[BsonNumber: Val={0}, Size={1}, TypeNum={2}]", Val, Size, TypeNum);
+ }
+
+ public object ToNative(){
+ return this.Val;
+ }
+
+ }
+}
+
69 MongoDBDriver/Bson/BsonOid.cs
@@ -0,0 +1,69 @@
+/*
+ * User: scorder
+ */
+using System;
+using System.IO;
+using System.Text;
+
+using MongoDB.Driver;
+
+namespace MongoDB.Driver.Bson
+{
+ /// <summary>
+ /// Description of BsonOid.
+ /// </summary>
+ public class BsonOid:BsonType{
+ private byte[] val;
+
+ public byte[] Val {
+ get {return val;}
+ set {val = value;}
+ }
+
+ public BsonOid(){}
+
+ public BsonOid(Oid oid){
+ //have to do some conversion here.
+ string oidstr = oid.Value;
+ if(oidstr.Length % 2 == 1){
+ oidstr = "0" + oidstr;
+ }
+ int numberChars = oidstr.Length;
+
+ byte[] bytes = new byte[numberChars / 2];
+ for (int i = 0; i < numberChars; i += 2){
+ try{
+ bytes[i / 2] = Convert.ToByte(oidstr.Substring(i, 2), 16);
+ }
+ catch{
+ //failed to convert these 2 chars, they may contain illegal charracters
+ bytes[i / 2] = 0;
+ }
+ }
+ this.Val = bytes;
+
+ }
+
+ public int Size {
+ get {return val.Length;}
+ }
+
+ public byte TypeNum {
+ get {return (byte)BsonDataType.Oid;}
+ }
+
+ public int Read(BsonReader reader){
+ this.Val = reader.ReadBytes(12);
+ return this.Size;
+ }
+
+ public void Write(BsonWriter writer){
+ writer.Write(this.val);
+ }
+
+ public object ToNative(){
+ string val = BitConverter.ToString(this.Val).Replace("-","");
+ return new Oid(val);
+ }
+ }
+}
79 MongoDBDriver/Bson/BsonReader.cs
@@ -0,0 +1,79 @@
+
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Text;
+
+namespace MongoDB.Driver.Bson
+{
+
+
+ public class BsonReader
+ {
+ private Stream stream;
+ private BinaryReader reader;
+ private UTF8Encoding encoding = new UTF8Encoding();
+
+
+ public BsonReader(Stream stream){
+ this.stream = stream;
+ reader = new BinaryReader(this.stream);
+ }
+
+ /// <summary>
+ /// Reads a string from the stream. The length should include the null terminator in the size.
+ /// </summary>
+ /// <param name="length">How much to read including null terminator</param>
+ /// <returns></returns>
+ public String ReadString(int length){
+ byte[] buff = reader.ReadBytes(length -1);
+ reader.ReadByte(); //ignore the terminator.
+ string ret = encoding.GetString(buff);
+ return ret;
+ }
+
+ /// <summary>
+ /// Reads from the stream one byte at a time until an ascii null (0) is found and builds a string from the bytes.
+ /// Use ReadString(length) when the length is known ahead of time as it is more efficient.
+ /// </summary>
+ /// <returns></returns>
+ public String ReadString(){
+ List<byte> buff = new List<byte>();
+ byte b = reader.ReadByte();
+ while(b != 0){
+ buff.Add(b);
+ b = reader.ReadByte();
+ }
+ string ret = encoding.GetString(buff.ToArray());
+ return ret;
+ }
+
+ public bool ReadBoolean(){
+ byte val = reader.ReadByte();
+ if(val == 0){
+ return false;
+ }else{
+ return true;
+ }
+ }
+
+ public byte ReadByte(){
+ return reader.ReadByte();
+ }
+ public byte[] ReadBytes(int len){
+ return reader.ReadBytes(len);
+ }
+
+ public Int32 ReadInt32(){
+ return reader.ReadInt32();
+ }
+
+ public Int64 ReadInt64(){
+ return reader.ReadInt64();
+ }
+
+ public double ReadDouble(){
+ return reader.ReadDouble();
+ }
+ }
+}
60 MongoDBDriver/Bson/BsonString.cs
@@ -0,0 +1,60 @@
+
+using System;
+
+namespace MongoDB.Driver.Bson
+{
+ public class BsonString:BsonType
+ {
+ private String val;
+
+ public string Val {
+ get {return val;}
+ set {val = value;}
+ }
+
+ public BsonString(){}
+
+ public BsonString(String str){
+ this.Val = str;
+ }
+
+ public byte TypeNum {
+ get{
+ return (byte)BsonDataType.String;
+ }
+ }
+
+ public int Size {
+ get {
+ int ret = 0;
+ ret = 4; //size bytes
+ if(this.val != null) {
+ ret += this.val.Length;
+ }else{
+ ret += 0;
+ }
+ ret += 1; //terminator
+ return ret;
+ }
+ }
+
+ public void Write(BsonWriter writer){
+ writer.Write(this.Val.Length + 1);
+ writer.Write(this.Val);
+ }
+
+ public int Read(BsonReader reader){
+ int len = reader.ReadInt32();
+ this.Val = reader.ReadString(len);
+ return 4 + len;
+ }
+
+ public override string ToString (){
+ return string.Format("[BsonString: Val={0}, TypeNum={1}, Size={2}]", Val, TypeNum, Size);
+ }
+
+ public object ToNative(){
+ return this.Val;
+ }
+ }
+}
81 MongoDBDriver/Bson/BsonWriter.cs
@@ -0,0 +1,81 @@
+
+using System;
+using System.IO;
+using System.Text;
+
+namespace MongoDB.Driver.Bson
+{
+
+ /// <summary>
+ /// Writes primitives to a stream. Use Documents and Elements to write more complex items.
+ /// </summary>
+ public class BsonWriter : IDisposable
+ {
+ private Stream stream;
+ private BinaryWriter writer;
+ private UTF8Encoding encoding = new UTF8Encoding();
+ private int bytesWritten = 0;
+
+ public BsonWriter(Stream stream){
+ this.stream = stream;
+ writer = new BinaryWriter(this.stream);
+ }
+
+ /// <summary>
+ /// Writes a CString to the stream followed by a null terminator
+ /// </summary>
+ /// <param name="str">
+ /// A <see cref="System.String"/>
+ /// </param>
+ public void Write(String str){
+ Byte[] buf = new byte[encoding.GetByteCount(str) + 1];
+ buf[buf.Length - 1] = (byte)0;
+ encoding.GetBytes(str,0,str.Length,buf,0);
+ writer.Write(buf);
+ bytesWritten += buf.Length;
+ }
+
+ public void Write(Boolean val){
+ if(val){
+ this.Write((Byte)1);
+ }else{
+ this.Write((Byte)0);
+ }
+ bytesWritten += 1;
+ }
+
+ public void Write(Byte val){
+ writer.Write(val);
+ bytesWritten += 1;
+ }
+
+ public void Write(Byte[] val){
+ writer.Write(val);
+ bytesWritten += val.Length;
+ }
+
+ public void Write(Int32 val){
+ writer.Write(val);
+ bytesWritten += 4;
+ }
+
+ public void Write(Int64 val){
+ writer.Write(val);
+ bytesWritten += 8;
+ }
+