Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: samus/mongodb-csharp
base: master
...
head fork: samus/mongodb-csharp
compare: generics
Checking mergeability… Don’t worry, you can still create the pull request.
  • 3 commits
  • 6 files changed
  • 1 commit comment
  • 2 contributors
View
29 MongoDB.Net-Tests/TestCursor.cs
@@ -1,4 +1,6 @@
using System;
+using System.Collections.Generic;
+
using NUnit.Framework;
using MongoDB.Driver;
@@ -138,5 +140,32 @@ public void TestCanReadAndKillCursor()
Assert.IsTrue(exp.Contains("n"));
Assert.IsTrue(exp.Contains("nscanned"));
}
+
+ [Test]
+ public void TestSearchWithNonDocument(){
+ IMongoCollection reads = DB["reads"];
+ Dictionary<string, object> fake = new Dictionary<string, object>(){{"j", 5}};
+ using(ICursor cur = reads.FindAll().Spec(fake)){
+ try{
+ foreach(Document d in cur.Documents){
+ d["returned"] = 1; //just do nothing with it.
+ }
+ }catch(Exception e){
+ Assert.Fail("Cursor couldn't execute. " + e.Message);
+ }
+ }
+ }
+
}
+
+// internal class FakeDoc : IEnumerable<KeyValuePair<string, object>>{
+//
+// System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator (){
+// return this.GetEnumerator();
+// }
+//
+// public IEnumerator<KeyValuePair<string, object>> GetEnumerator (){
+//
+// }
+// }
}
View
8 MongoDB.Net-Tests/TestDocument.cs
@@ -1,10 +1,6 @@
-/*
- * User: scorder
- * Date: 7/8/2009
- */
-
using System;
using System.Collections;
+using System.Collections.Generic;
using NUnit.Framework;
@@ -152,7 +148,7 @@ public void TestValuesAdded()
Document d2 = new Document().Append("k1", new Document().Append("k2", new Document().Append("k3", "bar")));
AreNotEqual(d1, d2);
}
-
+
private void AreEqual(Document d1, Document d2) {
if (!d1.Equals(d2)) {
Assert.Fail(string.Format("Documents don't match\r\nExpected: {0}\r\nActual: {1}", d1, d2));
View
268 MongoDBDriver/Cursor.cs
@@ -6,214 +6,228 @@
namespace MongoDB.Driver
{
- public class Cursor : ICursor {
+ public class Cursor : ICursor
+ {
private Connection connection;
-
+ private ReplyMessage reply;
+
+ #region "Properties"
private long id = -1;
- public long Id{
- get {return id;}
- }
-
+ public long Id {
+ get { return id; }
+ }
+
private String fullCollectionName;
public string FullCollectionName {
- get {return fullCollectionName;}
+ get { return fullCollectionName; }
}
- private Document spec;
- public ICursor Spec (Document spec){
- TryModify();
+ private bool modifiable = true;
+ public bool Modifiable {
+ get { return modifiable; }
+ }
+ #endregion
+
+ public Cursor (Connection conn, string fullCollectionName){
+ this.connection = conn;
+ this.fullCollectionName = fullCollectionName;
+ }
+
+ public Cursor (Connection conn, String fullCollectionName, Document spec, int limit, int skip, Document fields) : this(conn, fullCollectionName){
+ if (spec == null)
+ spec = new Document ();
this.spec = spec;
+ this.limit = limit;
+ this.skip = skip;
+ this.fields = fields;
+ }
+
+
+ #region "Fluency"
+ private Document spec;
+ public ICursor Spec (IEnumerable<KeyValuePair<String, Object>> spec){
+ TryModify ();
+ this.spec = EnsureIsDocument(spec);
return this;
}
-
+
private int limit;
public ICursor Limit (int limit){
- TryModify();
+ TryModify ();
this.limit = limit;
return this;
}
-
- private int skip;
+
+ private int skip;
public ICursor Skip (int skip){
- TryModify();
+ TryModify ();
this.skip = skip;
return this;
}
-
- private Document fields;
- public ICursor Fields (Document fields){
- TryModify();
- this.fields = fields;
+
+ private Document fields;
+ public ICursor Fields (IEnumerable<KeyValuePair<String, Object>> fields){
+ TryModify ();
+ this.fields = EnsureIsDocument(fields);
return this;
}
-
+
private QueryOptions options;
- public ICursor Options(QueryOptions options){
- TryModify();
+ public ICursor Options (QueryOptions options){
+ TryModify ();
this.options = options;
return this;
}
-
+
#region "Spec Options"
- private Document specOpts = new Document();
-
- public ICursor Sort(string field){
- return this.Sort(field, IndexOrder.Ascending);
+ private Document specOpts = new Document ();
+
+ public ICursor Sort (string field){
+ return this.Sort (field, IndexOrder.Ascending);
}
-
- public ICursor Sort(string field, IndexOrder order){
- return this.Sort(new Document().Append(field, order));
+
+ public ICursor Sort (string field, IndexOrder order){
+ return this.Sort (new Document ().Append (field, order));
}
-
- public ICursor Sort(Document fields){
- TryModify();
- AddOrRemoveSpecOpt("$orderby", fields);
+
+ public ICursor Sort (IEnumerable<KeyValuePair<String, Object>> fields){
+ TryModify ();
+ AddOrRemoveSpecOpt ("$orderby", fields);
return this;
}
-
- public ICursor Hint(Document index){
- TryModify();
- AddOrRemoveSpecOpt("$hint", index);
+
+ public ICursor Hint (IEnumerable<KeyValuePair<String, Object>> index){
+ TryModify ();
+ AddOrRemoveSpecOpt ("$hint", index);
return this;
}
- public ICursor Snapshot(Document index){
- TryModify();
- AddOrRemoveSpecOpt("$snapshot", index);
+ public ICursor Snapshot (IEnumerable<KeyValuePair<String, Object>> index){
+ TryModify ();
+ AddOrRemoveSpecOpt ("$snapshot", index);
return this;
}
-
- public Document Explain(){
- TryModify();
+
+ public Document Explain (){
+ TryModify ();
specOpts["$explain"] = true;
IEnumerable<Document> docs = this.Documents;
- using((IDisposable)docs){
- foreach(Document doc in docs){
+ using ((IDisposable)docs) {
+ foreach (Document doc in docs) {
return doc;
}
}
- throw new InvalidOperationException("Explain failed.");
+ throw new InvalidOperationException ("Explain failed.");
}
#endregion
-
- private bool modifiable = true;
- public bool Modifiable{
- get {return modifiable;}
- }
-
- private ReplyMessage reply;
-
- public Cursor(Connection conn, string fullCollectionName){
- this.connection = conn;
- this.fullCollectionName = fullCollectionName;
- }
-
- public Cursor(Connection conn, String fullCollectionName, Document spec, int limit, int skip, Document fields):
- this(conn,fullCollectionName){
- if(spec == null)spec = new Document();
- this.spec = spec;
- this.limit = limit;
- this.skip = skip;
- this.fields = fields;
- }
-
- public IEnumerable<Document> Documents{
- get{
- if(this.reply == null){
- RetrieveData();
+ #endregion
+
+ public IEnumerable<Document> Documents {
+ get {
+ if (this.reply == null) {
+ RetrieveData ();
}
int docsReturned = 0;
Document[] docs = this.reply.Documents;
Boolean shouldBreak = false;
- while(!shouldBreak){
- foreach(Document doc in docs){
- if((this.limit == 0) || (this.limit != 0 && docsReturned < this.limit)){
+ while (!shouldBreak) {
+ foreach (Document doc in docs) {
+ if ((this.limit == 0) || (this.limit != 0 && docsReturned < this.limit)) {
docsReturned++;
yield return doc;
- }else{
+ } else {
shouldBreak = true;
yield break;
}
}
- if(this.Id != 0 && shouldBreak == false){
- RetrieveMoreData();
+ if (this.Id != 0 && shouldBreak == false) {
+ RetrieveMoreData ();
docs = this.reply.Documents;
- if(docs == null){
- shouldBreak = true;
+ if (docs == null) {
+ shouldBreak = true;
}
- }else{
+ } else {
shouldBreak = true;
}
}
- }
+ }
}
-
- private void RetrieveData(){
- QueryMessage query = new QueryMessage();
+
+ private void RetrieveData (){
+ QueryMessage query = new QueryMessage ();
query.FullCollectionName = this.FullCollectionName;
- query.Query = BuildSpec();
+ query.Query = BuildSpec ();
query.NumberToReturn = this.limit;
query.NumberToSkip = this.skip;
query.Options = options;
- if(this.fields != null){
- query.ReturnFieldSelector = this.fields;
+ if (this.fields != null) {
+ query.ReturnFieldSelector = (Document)this.fields;
}
- try{
- this.reply = connection.SendTwoWayMessage(query);
+ try {
+ this.reply = connection.SendTwoWayMessage (query);
this.id = this.reply.CursorID;
- if(this.limit < 0)this.limit = this.limit * -1;
- }catch(IOException ioe){
- throw new MongoCommException("Could not read data, communication failure", this.connection,ioe);
+ if (this.limit < 0)
+ this.limit = this.limit * -1;
+ } catch (IOException ioe) {
+ throw new MongoCommException ("Could not read data, communication failure", this.connection, ioe);
}
-
+
}
-
- private void RetrieveMoreData(){
- GetMoreMessage gmm = new GetMoreMessage(this.fullCollectionName, this.Id, this.limit);
- try{
- this.reply = connection.SendTwoWayMessage(gmm);
+
+ private void RetrieveMoreData (){
+ GetMoreMessage gmm = new GetMoreMessage (this.fullCollectionName, this.Id, this.limit);
+ try {
+ this.reply = connection.SendTwoWayMessage (gmm);
this.id = this.reply.CursorID;
- }catch(IOException ioe){
+ } catch (IOException ioe) {
this.id = 0;
- throw new MongoCommException("Could not read data, communication failure", this.connection,ioe);
+ throw new MongoCommException ("Could not read data, communication failure", this.connection, ioe);
}
}
-
-
- public void Dispose(){
- if(this.Id == 0) return; //All server side resources disposed of.
- KillCursorsMessage kcm = new KillCursorsMessage(this.Id);
- try{
+
+
+ public void Dispose (){
+ if (this.Id == 0)
+ return;
+ //All server side resources disposed of.
+ KillCursorsMessage kcm = new KillCursorsMessage (this.Id);
+ try {
this.id = 0;
- connection.SendMessage(kcm);
- }catch(IOException ioe){
- throw new MongoCommException("Could not read data, communication failure", this.connection,ioe);
+ connection.SendMessage (kcm);
+ } catch (IOException ioe) {
+ throw new MongoCommException ("Could not read data, communication failure", this.connection, ioe);
}
}
-
- private void TryModify(){
- if(this.modifiable) return;
- throw new InvalidOperationException("Cannot modify a cursor that has already returned documents.");
+
+ private void TryModify (){
+ if (this.modifiable)
+ return;
+ throw new InvalidOperationException ("Cannot modify a cursor that has already returned documents.");
}
- private void AddOrRemoveSpecOpt(string key, Document doc){
- if(doc == null){
- specOpts.Remove(key);
- }else{
- specOpts[key] = doc;
+ private void AddOrRemoveSpecOpt (string key, IEnumerable<KeyValuePair<String, Object>> doc){
+ if (doc == null) {
+ specOpts.Remove (key);
+ } else {
+ specOpts[key] = EnsureIsDocument(doc);
}
}
-
- private Document BuildSpec(){
- if(this.specOpts.Count == 0) return this.spec;
- Document doc = new Document();
- this.specOpts.CopyTo(doc);
+
+ private Document BuildSpec (){
+ if (this.specOpts.Count == 0)
+ return this.spec;
+ Document doc = new Document ();
+ this.specOpts.CopyTo (doc);
doc["$query"] = this.spec;
return doc;
}
-
+
+ private Document EnsureIsDocument(IEnumerable<KeyValuePair<string,object>> doc){
+ if(doc is Document) return (Document)doc;
+ return new Document(doc);
+ }
}
}
View
154 MongoDBDriver/Document.cs
@@ -1,135 +1,155 @@
using System;
using System.Collections;
-using System.Collections.Generic;
-
-namespace MongoDB.Driver {
+using System.Collections.Generic;
+
+namespace MongoDB.Driver
+{
/// <summary>
- /// Description of Document.
+ /// A Document is the base container for an entry inside of a <see cref="Collection/>.
/// </summary>
- public class Document : DictionaryBase {
- private List<String> orderedKeys = new List<String>();
+ public class Document : IEnumerable<KeyValuePair<String, Object>>
+ {
+ private List<String> orderedKeys = new List<String> ();
+ private Dictionary<String, Object> dictionary = new Dictionary<String, Object>();
+
+ #region "Properties"
public Object this[String key] {
- get {
- return Dictionary[key];
- }
+ get { return dictionary[key]; }
set {
- if (orderedKeys.Contains(key) == false) {
- orderedKeys.Add(key);
+ if (orderedKeys.Contains (key) == false) {
+ orderedKeys.Add (key);
}
- Dictionary[key] = value;
+ dictionary[key] = value;
}
}
public ICollection Keys {
- get {
- return (orderedKeys);
- }
+ get { return (orderedKeys); }
}
public ICollection Values {
- get {
- return (Dictionary.Values);
- }
+ get { return (dictionary.Values); }
}
- public void Add(String key, Object value) {
- Dictionary.Add(key, value);
+ public int Count {
+ get { return orderedKeys.Count;}
+ }
+
+ #endregion
+
+ #region "Constructors"
+ public Document(){}
+
+ public Document(string key, object value){
+ this.Add(key, value);
+ }
+
+ public Document(IEnumerable<KeyValuePair<string,object>> kvps){
+ foreach(KeyValuePair<string,object> kvp in kvps){
+ this.Add(kvp.Key, kvp.Value);
+ }
+ }
+ #endregion
+
+ public void Add (String key, Object value){
+ dictionary.Add (key, value);
//Relies on ArgumentException from above if key already exists.
- orderedKeys.Add(key);
+ orderedKeys.Add (key);
}
- public Document Append(String key, Object value) {
- this.Add(key, value);
+ public Document Append (String key, Object value){
+ this.Add (key, value);
return this;
}
-
+
/// <summary>
/// Adds an item to the Document at the specified position
/// </summary>
- public void Insert(String key, Object value, int Position){
- Dictionary.Add(key, value);
+ public void Insert (String key, Object value, int Position){
+ dictionary.Add (key, value);
//Relies on ArgumentException from above if key already exists.
- orderedKeys.Insert(Position,key);
+ orderedKeys.Insert (Position, key);
}
- public Document Prepend(String key, Object value) {
- this.Insert(key, value,0);
+ public Document Prepend (String key, Object value){
+ this.Insert (key, value, 0);
return this;
}
-
- public Document Update(Document from) {
- if (from == null) return this;
- foreach (String key in from.Keys) {
- this[key] = from[key];
+
+ public Document Update (Document @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 bool Contains (String key){
+ return (orderedKeys.Contains (key));
}
- public void Remove(String key) {
- Dictionary.Remove(key);
- orderedKeys.Remove(key);
+ public void Remove (String key){
+ dictionary.Remove (key);
+ orderedKeys.Remove (key);
}
- public new void Clear(){
- Dictionary.Clear();
- orderedKeys.Clear();
+ public new void Clear (){
+ dictionary.Clear ();
+ orderedKeys.Clear ();
}
/// <summary>
/// TODO Fix any accidental reordering issues.
/// </summary>
/// <param name="dest"></param>
- public void CopyTo(Document dest) {
+ public void CopyTo (Document dest){
foreach (String key in orderedKeys) {
- if(dest.Contains(key))
- dest.Remove(key);
+ if (dest.Contains (key))
+ dest.Remove (key);
dest[key] = this[key];
}
}
-
- public override bool Equals(object obj) {
+
+ public override bool Equals (object obj){
if (obj is Document) {
- return Equals(obj as Document);
+ return Equals (obj as Document);
}
- return base.Equals(obj);
+ return base.Equals (obj);
}
- public bool Equals(Document obj) {
+ public bool Equals (Document obj){
if (obj == null)
return false;
if (orderedKeys.Count != obj.orderedKeys.Count)
return false;
- return this.GetHashCode() == obj.GetHashCode();
+ return this.GetHashCode () == obj.GetHashCode ();
}
- public override int GetHashCode() {
-
+ public override int GetHashCode (){
+
int hash = 27;
foreach (var key in orderedKeys) {
- var valueHashCode = GetValueHashCode(this[key]);
+ var valueHashCode = GetValueHashCode (this[key]);
unchecked {
- hash = (13 * hash) + key.GetHashCode();
+ hash = (13 * hash) + key.GetHashCode ();
hash = (13 * hash) + valueHashCode;
}
}
return hash;
}
- private int GetValueHashCode(object value) {
+ private int GetValueHashCode (object value){
if (value == null) {
return 0;
}
- return (value is Array) ? GetArrayHashcode((Array)value) : value.GetHashCode();
+ return (value is Array) ? GetArrayHashcode ((Array)value) : value.GetHashCode ();
}
- private int GetArrayHashcode(Array array) {
+ private int GetArrayHashcode (Array array){
var hash = 0;
foreach (var value in array) {
- var valueHashCode = GetValueHashCode(value);
+ var valueHashCode = GetValueHashCode (value);
unchecked {
hash = (13 * hash) + valueHashCode;
}
@@ -137,8 +157,20 @@ public class Document : DictionaryBase {
return hash;
}
- public override string ToString() {
- return JsonFormatter.Serialize(this);
+ public override string ToString (){
+ return JsonFormatter.Serialize (this);
}
+
+
+ public IEnumerator<KeyValuePair<string, object>> GetEnumerator (){
+ foreach(KeyValuePair<string,object> kvp in dictionary){
+ yield return kvp;
+ }
+ }
+
+ IEnumerator IEnumerable.GetEnumerator (){
+ return GetEnumerator();
+ }
+
}
}
View
10 MongoDBDriver/ICursor.cs
@@ -5,15 +5,15 @@ namespace MongoDB.Driver {
public interface ICursor : IDisposable {
long Id { get; }
string FullCollectionName { get; }
- ICursor Spec(Document spec);
+ ICursor Spec(IEnumerable<KeyValuePair<String, Object>> spec);
ICursor Limit(int limit);
ICursor Skip(int skip);
- ICursor Fields (Document fields);
+ ICursor Fields (IEnumerable<KeyValuePair<String, Object>> fields);
ICursor Sort(string field);
ICursor Sort(string field, IndexOrder order);
- ICursor Sort(Document fields);
- ICursor Hint(Document index);
- ICursor Snapshot(Document index);
+ ICursor Sort(IEnumerable<KeyValuePair<String, Object>> fields);
+ ICursor Hint(IEnumerable<KeyValuePair<String, Object>> index);
+ ICursor Snapshot(IEnumerable<KeyValuePair<String, Object>> index);
Document Explain();
bool Modifiable { get; }
IEnumerable<Document> Documents { get; }
View
1  MongoDBDriver/IMongoCollection.cs
@@ -13,6 +13,7 @@ public interface IMongoCollection
ICursor FindAll ();
ICursor Find (String @where);
ICursor Find (Document spec);
+ //ICursor Find (Document spec, Document fields);
ICursor Find (Document spec, int limit, int skip);
ICursor Find (Document spec, int limit, int skip, Document fields);
MapReduce MapReduce ();

Showing you all comments on commits in this comparison.

@craiggwilson

I like this a lot. What are you thoughts about it? Has someone requested this?

Something went wrong with that request. Please try again.