Permalink
Browse files

Refactored ICursor to be generic.

  • Loading branch information...
1 parent b11772f commit 932b61472b7eda564231523f60cb4794c288327d @lanwin lanwin committed Mar 12, 2010
@@ -254,7 +254,7 @@ public static void Main (string[] args)
static void DoFind(Database db, string col, Document spec){
for(int i = 0; i < perTrial; i++){
- ICursor cur = db[col].Find(spec);
+ ICursor<Document> cur = db[col].Find(spec);
foreach(Document d in cur.Documents){
}
}
@@ -33,11 +33,12 @@ public class GridFile{
this.name = bucket;
}
- public ICursor ListFiles(){
+ public ICursor<Document> ListFiles(){
return this.ListFiles(new Document());
}
-
- public ICursor ListFiles(Document query){
+
+ public ICursor<Document> ListFiles(Document query)
+ {
return this.files.Find(new Document().Append("query",query)
.Append("orderby", new Document()
.Append("filename", 1)));
@@ -358,7 +358,8 @@ public class GridFileStream : Stream
Binary data = new Binary(this.blankBuffer);
int i = 0;
- using (ICursor cur = chunks.Find(new Document().Append("query",query).Append("sort",sort),0,0,fields)){
+ using(ICursor<Document> cur = chunks.Find(new Document().Append("query", query).Append("sort", sort), 0, 0, fields))
+ {
foreach(Document doc in cur.Documents){
int n = Convert.ToInt32(doc["n"]);
if(i < n){
@@ -12,13 +12,13 @@ public class TestMongoDocumentQuerySyntax {
private IMongoQuery queryable;
private Mock<IMongoCollection> collectionMock;
- private Mock<ICursor> cursorMock;
+ private Mock<ICursor<Document>> cursorMock;
[SetUp]
public void Setup() {
Debug.WriteLine("initializing queryable");
collectionMock = new Mock<IMongoCollection>();
- cursorMock = new Mock<ICursor>();
+ cursorMock = new Mock<ICursor<Document>>();
collectionMock.Setup(c => c.Find(It.IsAny<Document>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<Document>())).Returns(cursorMock.Object);
queryable = new MongoQuery(new MongoQueryProvider(collectionMock.Object));
}
@@ -12,13 +12,13 @@ public class TestQueryParsing {
private IMongoQuery queryable;
private Mock<IMongoCollection> collectionMock;
- private Mock<ICursor> cursorMock;
+ private Mock<ICursor<Document>> cursorMock;
[SetUp]
public void Setup() {
Debug.WriteLine("initializing queryable");
collectionMock = new Mock<IMongoCollection>();
- cursorMock = new Mock<ICursor>();
+ cursorMock = new Mock<ICursor<Document>>();
collectionMock.Setup(c => c.Find(It.IsAny<Document>(), It.IsAny<int>(), It.IsAny<int>(), It.IsAny<Document>())).Returns(cursorMock.Object);
queryable = new MongoQuery(new MongoQueryProvider(collectionMock.Object));
}
@@ -62,7 +62,7 @@ public class TestCollection : MongoTestBase
Document fields = new Document();
fields["x"] = 1;
- ICursor c = DB["finds"].Find(query,-1,0,fields);
+ ICursor<Document> c = DB["finds"].Find(query, -1, 0, fields);
foreach(Document result in c.Documents){
Assert.IsNotNull(result);
Assert.AreEqual(4, result["x"]);
@@ -75,7 +75,7 @@ public class TestCollection : MongoTestBase
Document query = new Document();
query["j"] = new Document().Append("$gt",20);
- ICursor c = DB["finds"].Find(query);
+ ICursor<Document> c = DB["finds"].Find(query);
foreach(Document result in c.Documents){
Assert.IsNotNull(result);
Object j = result["j"];
@@ -86,7 +86,7 @@ public class TestCollection : MongoTestBase
[Test]
public void TestManualWhere(){
Document query = new Document().Append("$where", new Code("this.j % 2 == 0"));
- ICursor c = DB["finds"].Find(query);
+ ICursor<Document> c = DB["finds"].Find(query);
foreach(Document result in c.Documents){
Assert.IsNotNull(result);
Object j = result["j"];
@@ -108,7 +108,8 @@ public class TestCollection : MongoTestBase
Assert.AreEqual(4, CountDocs(col.Find(funcDoc)), "Function where didn't return 4 docs");
}
- private int CountDocs(ICursor cur){
+ private int CountDocs(ICursor<Document> cur)
+ {
int cnt = 0;
foreach(Document doc in cur.Documents){
cnt++;
@@ -117,7 +118,7 @@ public class TestCollection : MongoTestBase
}
[Test]
public void TestWhere(){
- ICursor c = DB["finds"].Find("this.j % 2 == 0");
+ ICursor<Document> c = DB["finds"].Find("this.j % 2 == 0");
foreach(Document result in c.Documents){
Assert.IsNotNull(result);
Object j = result["j"];
@@ -287,7 +288,7 @@ public class TestCollection : MongoTestBase
updates.Insert(new Document().Append("Last", "Cordr").Append("First","Sam3"));
Document selector = new Document().Append("Last", "Cordr");
- ICursor results = updates.Find(selector);
+ ICursor<Document> results = updates.Find(selector);
bool found = false;
foreach(Document doc in results.Documents){
Assert.AreEqual("Cordr", doc["Last"]);
@@ -169,8 +169,9 @@ public class Reader{
public void DoReads(){
for(int x = 0; x < this.Iterations; x++){
- try{
- using(ICursor c = this.Collection.FindAll()){
+ try{
+ using(ICursor<Document> c = this.Collection.FindAll())
+ {
//Just read one and do nothing with the Document.
foreach(Document d in c.Documents){
d["works"] = true;
@@ -34,7 +34,7 @@ public class TestCursor : MongoTestBase
[Test]
public void TestCanReadSmall()
{
- ICursor c = DB["smallreads"].FindAll();
+ ICursor<Document> c = DB["smallreads"].FindAll();
Assert.IsNotNull(c,"Cursor shouldn't be null");
int reads = 0;
@@ -47,7 +47,7 @@ public void TestCanReadSmall()
[Test]
public void TestCanReadMore(){
- ICursor c = DB["reads"].FindAll();
+ ICursor<Document> c = DB["reads"].FindAll();
Assert.IsNotNull(c,"Cursor shouldn't be null");
int reads = 0;
@@ -70,7 +70,7 @@ public void TestCanReadSmall()
[Test]
public void TestCanReadAndKillCursor()
{
- ICursor c = DB["reads"].FindAll();
+ ICursor<Document> c = DB["reads"].FindAll();
Assert.IsNotNull(c,"Cursor shouldn't be null");
foreach(Document doc in c.Documents){
@@ -81,8 +81,8 @@ public void TestCanReadAndKillCursor()
}
[Test]
- public void TestCanLimit(){
- ICursor c = DB["reads"].FindAll().Limit(5);
+ public void TestCanLimit(){
+ ICursor<Document> c = DB["reads"].FindAll().Limit(5);
Assert.IsNotNull(c,"Cursor shouldn't be null");
int reads = 0;
@@ -43,9 +43,9 @@ public class CollectionMetaData
if (gotIndexes)
return indexes;
- indexes.Clear ();
-
- ICursor docs = db["system.indexes"].Find (new Document ().Append ("ns", this.fullName));
+ indexes.Clear ();
+
+ var docs = db["system.indexes"].Find(new Document().Append("ns", this.fullName));
foreach (Document doc in docs.Documents) {
indexes.Add ((string)doc["name"], doc);
}
@@ -61,6 +61,15 @@ public Connection(IConnectionFactory factory)
/// <value>The connection string.</value>
public string ConnectionString {
get { return _factory.ConnectionString; }
+ }
+
+ /// <summary>
+ /// Sends the two way message.
+ /// </summary>
+ /// <param name="msg">The MSG.</param>
+ /// <returns></returns>
+ public ReplyMessage<Document> SendTwoWayMessage(RequestMessageBase msg){
+ return SendTwoWayMessage<Document>(msg);
}
/// <summary>
@@ -69,12 +78,12 @@ public Connection(IConnectionFactory factory)
/// <param name="msg"></param>
/// <returns></returns>
/// <exception cref="IOException">A reconnect will be issued but it is up to the caller to handle the error.</exception>
- public ReplyMessage<Document> SendTwoWayMessage (RequestMessageBase msg){
- if (this.State != ConnectionState.Opened) {
+ public ReplyMessage<T> SendTwoWayMessage<T>(RequestMessageBase msg) where T:class {
+ if (State != ConnectionState.Opened) {
throw new MongoCommException ("Operation cannot be performed on a closed connection.", this);
}
try {
- var reply = new ReplyMessage<Document> ();
+ var reply = new ReplyMessage<T> ();
lock (_connection) {
msg.Write (_connection.GetStream ());
reply.Read (_connection.GetStream ());
@@ -6,7 +6,7 @@
namespace MongoDB.Driver
{
- public class Cursor<T> : ICursor
+ public class Cursor<T> : ICursor<T>
where T : class
{
private bool _modifiable = true;
@@ -15,7 +15,7 @@ public class Cursor<T> : ICursor
private Document _fields;
private int _limit;
private QueryOptions _options;
- private ReplyMessage<Document> _reply;
+ private ReplyMessage<T> _reply;
private int _skip;
private Document _spec;
@@ -27,8 +27,7 @@ public class Cursor<T> : ICursor
}
public Cursor(Connection conn, String fullCollectionName, Document spec, int limit, int skip, Document fields)
- :
- this(conn, fullCollectionName){
+ :this(conn, fullCollectionName){
//Todo: should be internal
if(spec == null)
spec = new Document();
@@ -55,7 +54,7 @@ public Cursor(Connection conn, String fullCollectionName, Document spec, int lim
/// </summary>
/// <param name = "spec">The spec.</param>
/// <returns></returns>
- public ICursor Spec(Document spec){
+ public ICursor<T> Spec(Document spec){
TryModify();
_spec = spec;
return this;
@@ -66,7 +65,8 @@ public Cursor(Connection conn, String fullCollectionName, Document spec, int lim
/// </summary>
/// <param name="limit">The limit.</param>
/// <returns></returns>
- public ICursor Limit(int limit){
+ public ICursor<T> Limit(int limit)
+ {
TryModify();
_limit = limit;
return this;
@@ -77,7 +77,8 @@ public Cursor(Connection conn, String fullCollectionName, Document spec, int lim
/// </summary>
/// <param name="skip">The skip.</param>
/// <returns></returns>
- public ICursor Skip(int skip){
+ public ICursor<T> Skip(int skip)
+ {
TryModify();
_skip = skip;
return this;
@@ -88,7 +89,8 @@ public Cursor(Connection conn, String fullCollectionName, Document spec, int lim
/// </summary>
/// <param name="fields">The fields.</param>
/// <returns></returns>
- public ICursor Fields(Document fields){
+ public ICursor<T> Fields(Document fields)
+ {
TryModify();
_fields = fields;
return this;
@@ -99,7 +101,8 @@ public Cursor(Connection conn, String fullCollectionName, Document spec, int lim
/// </summary>
/// <param name="field">The field.</param>
/// <returns></returns>
- public ICursor Sort(string field){
+ public ICursor<T> Sort(string field)
+ {
return Sort(field, IndexOrder.Ascending);
}
@@ -109,7 +112,8 @@ public Cursor(Connection conn, String fullCollectionName, Document spec, int lim
/// <param name="field">The field.</param>
/// <param name="order">The order.</param>
/// <returns></returns>
- public ICursor Sort(string field, IndexOrder order){
+ public ICursor<T> Sort(string field, IndexOrder order)
+ {
return Sort(new Document().Append(field, order));
}
@@ -118,7 +122,8 @@ public Cursor(Connection conn, String fullCollectionName, Document spec, int lim
/// </summary>
/// <param name="fields">The fields.</param>
/// <returns></returns>
- public ICursor Sort(Document fields){
+ public ICursor<T> Sort(Document fields)
+ {
TryModify();
AddOrRemoveSpecOpt("$orderby", fields);
return this;
@@ -129,7 +134,8 @@ public Cursor(Connection conn, String fullCollectionName, Document spec, int lim
/// </summary>
/// <param name="index">The index.</param>
/// <returns></returns>
- public ICursor Hint(Document index){
+ public ICursor<T> Hint(Document index)
+ {
TryModify();
AddOrRemoveSpecOpt("$hint", index);
return this;
@@ -140,7 +146,8 @@ public Cursor(Connection conn, String fullCollectionName, Document spec, int lim
/// </summary>
/// <param name="index">The index.</param>
/// <returns></returns>
- public ICursor Snapshot(Document index){
+ public ICursor<T> Snapshot(Document index)
+ {
TryModify();
AddOrRemoveSpecOpt("$snapshot", index);
return this;
@@ -150,7 +157,7 @@ public Cursor(Connection conn, String fullCollectionName, Document spec, int lim
/// Explains this instance.
/// </summary>
/// <returns></returns>
- public Document Explain(){
+ public T Explain(){
TryModify();
_specOpts["$explain"] = true;
@@ -167,7 +174,7 @@ public Cursor(Connection conn, String fullCollectionName, Document spec, int lim
/// </summary>
/// <param name="options">The options.</param>
/// <returns></returns>
- public ICursor Options(QueryOptions options)
+ public ICursor<T> Options(QueryOptions options)
{
TryModify();
_options = options;
@@ -186,7 +193,7 @@ public ICursor Options(QueryOptions options)
/// Gets the documents.
/// </summary>
/// <value>The documents.</value>
- public IEnumerable<Document> Documents{
+ public IEnumerable<T> Documents{
get{
if(_reply == null)
RetrieveData();
@@ -235,7 +242,7 @@ public ICursor Options(QueryOptions options)
/// Retrieves the data.
/// </summary>
private void RetrieveData(){
- var query = new QueryMessage<Document>{
+ var query = new QueryMessage<T>{
FullCollectionName = FullCollectionName,
Query = BuildSpec(),
NumberToReturn = _limit,
@@ -246,7 +253,7 @@ public ICursor Options(QueryOptions options)
if(_fields != null)
query.ReturnFieldSelector = _fields;
try{
- _reply = _connection.SendTwoWayMessage(query);
+ _reply = _connection.SendTwoWayMessage<T>(query);
Id = _reply.CursorId;
if(_limit < 0)
_limit = _limit*-1;
@@ -264,7 +271,7 @@ public ICursor Options(QueryOptions options)
var getMoreMessage = new GetMoreMessage(FullCollectionName, Id, _limit);
try{
- _reply = _connection.SendTwoWayMessage(getMoreMessage);
+ _reply = _connection.SendTwoWayMessage<T>(getMoreMessage);
Id = _reply.CursorId;
}
catch(IOException ioe){
@@ -37,7 +37,7 @@ public class Database
public List<String> GetCollectionNames(){
IMongoCollection namespaces = this["system.namespaces"];
- ICursor cursor = namespaces.Find(new Document());
+ ICursor<Document> cursor = namespaces.Find(new Document());
List<String> names = new List<string>();
foreach (Document doc in cursor.Documents){
names.Add((String)doc["name"]); //Fix Me: Should filter built-ins
Oops, something went wrong.

0 comments on commit 932b614

Please sign in to comment.