Permalink
Browse files

Refactored cursor to fluent interface

  • Loading branch information...
1 parent 36ce284 commit 4ce639729e07d0fcc401fcfca71b0561fef81a5d @samus committed Feb 17, 2010
Showing with 56 additions and 51 deletions.
  1. +1 −2 MongoDB.Net-Tests/TestCursor.cs
  2. +44 −37 MongoDBDriver/Cursor.cs
  3. +11 −12 MongoDBDriver/ICursor.cs
@@ -68,8 +68,7 @@ public void TestCanReadAndKillCursor()
[Test]
public void TestCanLimit(){
- ICursor c = db["tests"]["reads"].FindAll();
- c.Limit = 5;
+ ICursor c = db["tests"]["reads"].FindAll().Limit(5);
Assert.IsNotNull(c,"Cursor shouldn't be null");
int reads = 0;
View
@@ -19,54 +19,56 @@ public class Cursor : ICursor {
private String fullCollectionName;
public string FullCollectionName {
get {return fullCollectionName;}
- set {fullCollectionName = value;}
}
-
- private String collName;
- public string CollName {
- get {return collName;}
- set {collName = value;}
- }
-
+
private Document spec;
- public Document Spec{
- get {return spec;}
- set {spec = value;}
+ public ICursor Spec (Document spec){
+ TryModify();
+ this.spec = spec;
+ return this;
}
private int limit;
- public int Limit{
- get {return limit;}
- set {limit = value;}
+ public ICursor Limit (int limit){
+ TryModify();
+ this.limit = limit;
+ return this;
}
- private int skip;
- public int Skip{
- get {return skip;}
- set {skip = value;}
+ private int skip;
+ public ICursor Skip (int skip){
+ TryModify();
+ this.skip = skip;
+ return this;
}
-
- private Document fields;
- public Document Fields{
- get {return fields;}
- set {fields = value;}
+
+ private Document fields;
+ public ICursor Fields (Document fields){
+ TryModify();
+ this.fields = fields;
+ return this;
}
+
private bool modifiable = true;
public bool Modifiable{
get {return modifiable;}
}
private ReplyMessage reply;
- public Cursor(Connection conn, String fullCollectionName, Document spec, int limit, int skip, Document fields){
+ public Cursor(Connection conn, string fullCollectionName){
this.connection = conn;
- this.FullCollectionName = fullCollectionName;
+ 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;
+ this.spec = spec;
+ this.limit = limit;
+ this.skip = skip;
+ this.fields = fields;
}
public IEnumerable<Document> Documents{
@@ -79,7 +81,7 @@ public class Cursor : ICursor {
Boolean shouldBreak = false;
while(!shouldBreak){
foreach(Document doc in docs){
- if((this.Limit == 0) || (this.Limit != 0 && docsReturned < this.Limit)){
+ if((this.limit == 0) || (this.limit != 0 && docsReturned < this.limit)){
docsReturned++;
yield return doc;
}else{
@@ -103,24 +105,24 @@ public class Cursor : ICursor {
private void RetrieveData(){
QueryMessage query = new QueryMessage();
query.FullCollectionName = this.FullCollectionName;
- query.Query = this.Spec;
- query.NumberToReturn = this.Limit;
- query.NumberToSkip = this.Skip;
- if(this.Fields != null){
- query.ReturnFieldSelector = this.Fields;
+ query.Query = this.spec;
+ query.NumberToReturn = this.limit;
+ query.NumberToSkip = this.skip;
+ if(this.fields != null){
+ query.ReturnFieldSelector = this.fields;
}
try{
this.reply = connection.SendTwoWayMessage(query);
this.id = this.reply.CursorID;
- if(this.Limit < 0)this.Limit = this.Limit * -1;
+ 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);
+ GetMoreMessage gmm = new GetMoreMessage(this.fullCollectionName, this.Id, this.limit);
try{
this.reply = connection.SendTwoWayMessage(gmm);
this.id = this.reply.CursorID;
@@ -141,5 +143,10 @@ public class Cursor : ICursor {
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.");
+ }
}
}
View
@@ -1,17 +1,16 @@
-using System;
+using System;
using System.Collections.Generic;
using System.Text;
namespace MongoDB.Driver {
- public interface ICursor : IDisposable {
- long Id { get; }
- string FullCollectionName { get; set; }
- string CollName { get; set; }
- Document Spec { get; set; }
- int Limit { get; set; }
- int Skip { get; set; }
- Document Fields { get; set; }
- bool Modifiable { get; }
- IEnumerable<Document> Documents { get; }
- }
+ public interface ICursor : IDisposable {
+ long Id { get; }
+ string FullCollectionName { get; }
+ ICursor Spec(Document spec);
+ ICursor Limit(int limit);
+ ICursor Skip(int skip);
+ ICursor Fields (Document fields);
+ bool Modifiable { get; }
+ IEnumerable<Document> Documents { get; }
+ }
}

0 comments on commit 4ce6397

Please sign in to comment.