Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added BsonLong and BsonArray types.

  • Loading branch information...
commit ce009037c4ae67fa096641094607b8d6f7f0e593 1 parent d55dbc1
@samus authored
View
4 .gitignore
@@ -11,3 +11,7 @@
test-results/*.xml
MongoDB.Net-Tests/test-results/MongoDB.Driver.Tests.csproj.test-cache
+
+*.suo
+/_UpgradeReport_Files/*
+obj/*
View
1  MongoDB.Net-Tests/.gitignore
@@ -1,3 +1,4 @@
*.dll
*.pdb
*.xml
+obj/*
View
74 MongoDB.Net-Tests/Bson/TestBsonArray.cs
@@ -0,0 +1,74 @@
+/*
+ * User: scorder
+ */
+
+using System;
+using System.IO;
+
+using NUnit.Framework;
+
+namespace MongoDB.Driver.Bson
+{
+ [TestFixture]
+ public class TestBsonArray
+ {
+ [Test]
+ public void TestType(){
+ Assert.AreEqual(BsonDataType.Array, (BsonDataType)new BsonArray().TypeNum);
+ }
+
+ [Test]
+ public void TestOnlyNumericKeys(){
+ bool thrown = false;
+ BsonArray ba = new BsonArray();
+ ba.Add("1","A");
+ Assert.IsNotNull(ba["1"]);
+ try {
+ ba.Add("A","A");
+ } catch (ArgumentOutOfRangeException) {
+ thrown = true;
+ }
+ if(thrown != true) Assert.Fail("Exception should have been thrown");
+
+ thrown = false;
+ try {
+ ba["A"] = new BsonElement("A", new BsonString("A"));
+ } catch (ArgumentOutOfRangeException) {
+ thrown = true;
+ }
+ if(thrown != true) Assert.Fail("Exception should have been thrown");
+ }
+
+ [Test]
+ public void TestKeysCanBeInts(){
+ BsonArray ba = new BsonArray();
+ ba.Add(1,"A");
+ Assert.AreEqual("A", ba[1].Val.ToNative());
+ }
+
+ [Test]
+ public void TestKeyHoles(){
+ BsonArray ba = new BsonArray();
+ ba.Add(1,"A");
+ ba.Add(3,"C");
+ Assert.IsNull(ba[2]);
+ }
+
+ [Test]
+ public void TestKeyOrdering(){
+ BsonArray ba = new BsonArray();
+ ba.Add(1,"A");
+ ba.Add(2,"B");
+ ba.Add(5,"E");
+ ba.Add(3,"C");
+ ba.Add(4,"D");
+
+ int ikey = 1;
+ foreach(string key in ba.Keys){
+ Assert.AreEqual(ikey, int.Parse(key));
+ ikey++;
+ }
+ }
+
+ }
+}
View
68 MongoDB.Net-Tests/Bson/TestBsonDocument.cs
@@ -116,5 +116,73 @@ public class TestBsonDocument
Assert.AreEqual("2C000000075F6964004A75384CFAC16EA58B290350016100000000000000F03F016200000000000000004000",hexdump, "Dump not correct");
}
+ [Test]
+ public void TestArrayElements(){
+ String hexdoc = "82000000075f6964004a78937917220000000061cf0461005d0000" +
+ "00013000000000000000f03f013100000000000000004001320000" +
+ "000000000008400133000000000000001040013400000000000000" +
+ "145001350000000000000018400136000000000000001c40013700" +
+ "00000000000020400002620005000000746573740000";
+
+ byte[] bytes = HexToBytes(hexdoc);
+ MemoryStream buf = new MemoryStream(bytes);
+ BsonReader reader = new BsonReader(buf);
+
+ BsonDocument bdoc = new BsonDocument();
+ bdoc.Read(reader);
+ Assert.AreEqual(BsonDataType.Array, (BsonDataType)bdoc["a"].Val.TypeNum);
+
+ buf = new MemoryStream();
+ BsonWriter writer = new BsonWriter(buf);
+ bdoc.Write(writer);
+
+ String hexdump = BitConverter.ToString(buf.ToArray());
+ hexdump = hexdump.Replace("-","").ToLower();
+
+ Assert.AreEqual(hexdoc, hexdump);
+ }
+
+ [Test]
+ public void TestArraysWithHoles(){
+ String hexdoc =
+ "46000000075F6964004A79BFD517220000000061D304617272617900" +
+ "29000000023000020000006100023100020000006200023200020000" +
+ "0063000234000200000065000000";
+ byte[] bytes = HexToBytes(hexdoc);
+ MemoryStream buf = new MemoryStream(bytes);
+ BsonReader reader = new BsonReader(buf);
+ BsonDocument bdoc = new BsonDocument();
+ bdoc.Read(reader);
+ Assert.AreEqual(BsonDataType.Array, (BsonDataType)bdoc["array"].Val.TypeNum);
+
+ buf = new MemoryStream();
+ BsonWriter writer = new BsonWriter(buf);
+ bdoc.Write(writer);
+
+ String hexdump = BitConverter.ToString(buf.ToArray());
+ hexdump = hexdump.Replace("-","");
+
+ Assert.AreEqual(hexdoc, hexdump);
+ }
+
+ private byte[] HexToBytes(string hex){
+ //TODO externalize somewhere.
+ if(hex.Length % 2 == 1){
+ System.Console.WriteLine("uneven number of hex pairs.");
+ hex = "0" + hex;
+ }
+ int numberChars = hex.Length;
+ byte[] bytes = new byte[numberChars / 2];
+ for (int i = 0; i < numberChars; i += 2){
+ try{
+ bytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
+ }
+ catch{
+ //failed to convert these 2 chars, they may contain illegal charracters
+ bytes[i / 2] = 0;
+ }
+ }
+ return bytes;
+ }
}
}
View
1  MongoDB.Net-Tests/MongoDB.Driver.Tests.csproj
@@ -36,6 +36,7 @@
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="Bson\TestBsonArray.cs" />
<Compile Include="Bson\TestBsonConvert.cs" />
<Compile Include="Bson\TestBsonDate.cs" />
<Compile Include="Bson\TestBsonDocument.cs" />
View
1  MongoDBDriver/.gitignore
@@ -1,3 +1,4 @@
+obj/*
*.xml
*.dll
*.mdb
View
67 MongoDBDriver/Bson/BsonArray.cs
@@ -0,0 +1,67 @@
+/*
+ * User: scorder
+ */
+using System;
+
+namespace MongoDB.Driver.Bson
+{
+ /// <summary>
+ /// Description of BsonArray.
+ /// </summary>
+ public class BsonArray : BsonDocument{
+
+ public BsonArray(){}
+
+ public override byte TypeNum {
+ get {return (byte)BsonDataType.Array;}
+ }
+ public override BsonElement this[ String key ] {
+ get{
+ return (BsonElement)Dictionary[key];
+ }
+ set{
+ int t;
+ if(int.TryParse(key,out t) == false)throw new ArgumentOutOfRangeException(String.Format("Key '{0}' isn't numeric", key));
+ if(orderedKeys.Contains(key) == false){
+ string lastkey = string.Empty;
+ if(orderedKeys.Count > 0) lastkey = orderedKeys[orderedKeys.Count -1];
+ orderedKeys.Add(key);
+ if(lastkey != string.Empty && int.Parse(key) < int.Parse(lastkey)){
+ orderedKeys.Sort();
+ }
+ }
+ Dictionary[key] = value;
+ }
+ }
+
+ public void Add(int key, BsonElement value){
+ this.Add(key.ToString(),value);
+ }
+
+ public void Add(int key, BsonType val){
+ this.Add(new BsonElement(key.ToString(), val));
+ }
+
+ public void Add(int key, Object val){
+ this.Add(new BsonElement(key.ToString(), BsonConvert.From(val)));
+ }
+
+ public BsonDocument Append(int key, BsonElement value){
+ this.Add(key.ToString(),value);
+ return this;
+ }
+ public BsonElement this[ int key ] {
+ get{
+ return (BsonElement)Dictionary[key.ToString()];
+ }
+ set{
+ string skey = key.ToString();
+ if(orderedKeys.Contains(skey) == false){
+ orderedKeys.Add(skey);
+ }
+ Dictionary[skey] = value;
+ }
+ }
+
+ }
+}
View
20 MongoDBDriver/Bson/BsonContainer.cs
@@ -0,0 +1,20 @@
+/*
+ * User: scorder
+ */
+using System;
+using System.Collections.Generic;
+
+namespace MongoDB.Driver.Bson
+{
+ /// <summary>
+ /// Description of BsonContainer.
+ /// </summary>
+ public class BsonContainer<T>
+ {
+ private List<T> orderedKeys = new List<T>();
+// private SortedList<TKey, TValue>
+ public BsonContainer()
+ {
+ }
+ }
+}
View
10 MongoDBDriver/Bson/BsonConvert.cs
@@ -27,6 +27,8 @@ public static class BsonConvert
ret = From((Document)val);
}else if(t == typeof(int)){
ret = From((int)val);
+ }else if(t == typeof(long)){
+ ret = From((long)val);
}else if(t == typeof(bool)){
ret = From((bool)val);
}else if(t == typeof(Oid)){
@@ -56,6 +58,10 @@ public static class BsonConvert
return new BsonInteger(val);
}
+ public static BsonLong From(long val){
+ return new BsonLong();
+ }
+
public static BsonNumber From(double val){
return new BsonNumber(val);
}
@@ -83,9 +89,11 @@ public static class BsonConvert
}else if(type == BsonDataType.Obj){
ret = new BsonDocument();
}else if(type == BsonDataType.Array){
- ret = new BsonDocument();
+ ret = new BsonArray();
}else if(type == BsonDataType.Integer){
ret = new BsonInteger();
+ }else if(type == BsonDataType.Long){
+ ret = new BsonLong();
}else if(type == BsonDataType.Boolean){
ret = new BsonBoolean();
}else if(type == BsonDataType.Oid){
View
60 MongoDBDriver/Bson/BsonDocument.cs
@@ -16,11 +16,11 @@ namespace MongoDB.Driver.Bson
/// </summary>
public class BsonDocument : System.Collections.DictionaryBase, BsonType
{
- private List<String> orderedKeys = new List<String>();
+ protected List<String> orderedKeys = new List<String>();
- public BsonDocument(){
- }
- public BsonElement this[ String key ] {
+ public BsonDocument(){}
+
+ public virtual BsonElement this[ String key ] {
get{
return (BsonElement)Dictionary[key];
}
@@ -34,17 +34,17 @@ public class BsonDocument : System.Collections.DictionaryBase, BsonType
public ICollection Keys {
get {
- return(orderedKeys);
+ return (ICollection)orderedKeys;
}
}
public ICollection Values {
get {
- return( Dictionary.Values );
+ return Dictionary.Values;
}
}
- public void Add( String key, BsonElement value ) {
+ public virtual void Add( String key, BsonElement value ) {
if(orderedKeys.Contains(key)) throw new ArgumentException("Key already exists");
this[key] = value;
}
@@ -55,6 +55,10 @@ public class BsonDocument : System.Collections.DictionaryBase, BsonType
this.Add(new BsonElement(key, val));
}
+ public void Add(String key, Object val){
+ this.Add(new BsonElement(key, BsonConvert.From(val)));
+ }
+
public BsonDocument Append(String key, BsonElement value){
this.Add(key,value);
return this;
@@ -77,7 +81,7 @@ public class BsonDocument : System.Collections.DictionaryBase, BsonType
orderedKeys.Remove(key);
}
- public byte TypeNum {
+ public virtual byte TypeNum {
get {return (byte)BsonDataType.Obj;}
}
@@ -102,35 +106,21 @@ public class BsonDocument : System.Collections.DictionaryBase, BsonType
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());
-// }
+
+ 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));
+ }
return bytesRead;
}
@@ -141,7 +131,7 @@ public class BsonDocument : System.Collections.DictionaryBase, BsonType
doc[key] = be.Val.ToNative();
}
return doc;
- }
+ }
public override string ToString ()
{
View
50 MongoDBDriver/Bson/BsonLong.cs
@@ -0,0 +1,50 @@
+/*
+ * User: scorder
+ */
+using System;
+
+namespace MongoDB.Driver.Bson
+{
+ /// <summary>
+ /// Description of BsonLong.
+ /// </summary>
+ public class BsonLong: BsonType
+ {
+ long val;
+ public long Val {
+ get { return val; }
+ set { val = value; }
+ }
+ public int Size {
+ get {return 8;}
+ }
+
+ public byte TypeNum {
+ get {return (byte)BsonDataType.Long;}
+ }
+
+ public BsonLong(){}
+
+ public BsonLong(long val){
+ this.Val = val;
+ }
+
+ 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("[BsonLong: Val={0}, Size={1}, TypeNum={2}]", Val, Size, TypeNum);
+ }
+
+ public object ToNative(){
+ return this.Val;
+ }
+ }
+}
View
1  MongoDBDriver/Bson/Types.cs
@@ -33,6 +33,7 @@ public enum BsonDataType:sbyte
//should be evaluated.
Integer = 16, //int32
Timestamp = 17, //int64 first 4 are a timestamp, next 4 are an incremented field
+ Long = 18, //int64 64 bit integer
MinKey = -1, //VOID See Splitting Shards
MaxKey = 127 //VOID See Splitting Shards
}
View
5 MongoDBDriver/MongoDB.Driver.csproj
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -46,10 +46,13 @@
<Reference Include="System" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="Bson\BsonArray.cs" />
<Compile Include="Bson\BsonBoolean.cs" />
+ <Compile Include="Bson\BsonContainer.cs" />
<Compile Include="Bson\BsonConvert.cs" />
<Compile Include="Bson\BsonDate.cs" />
<Compile Include="Bson\BsonDocument.cs" />
+ <Compile Include="Bson\BsonLong.cs" />
<Compile Include="Bson\BsonNumber.cs" />
<Compile Include="Bson\BsonInteger.cs" />
<Compile Include="Bson\BsonOid.cs" />

0 comments on commit ce00903

Please sign in to comment.
Something went wrong with that request. Please try again.