Permalink
Browse files

- Added ICursor and IMongoCollection for better abstraction

  • Loading branch information...
1 parent effd6bf commit f4d8fff9776055183cf550b1b5f5fad7521496b7 @sdether sdether committed Oct 1, 2009
@@ -47,8 +47,8 @@ public class TestAuthentication
[Test]
public void TestAuthenticatedInsert(){
- bool ok = Authenticate();
- Collection tests = db["inserts"];
+ bool ok = Authenticate();
+ IMongoCollection tests = db["inserts"];
if (ok){
tests.Insert(new Document().Append("value", 34));
}
@@ -63,8 +63,8 @@ public class TestAuthentication
db.Logout();
}catch(MongoException){
//We don't care. Just wanted to make sure we weren't logged in
- }
- Collection tests = db["inserts"];
+ }
+ IMongoCollection tests = db["inserts"];
tests.Insert(new Document().Append("value", 84));
Document valA = tests.FindOne(new Document().Append("value", 84));
Assert.AreNotEqual(84, valA["value"]);
@@ -35,7 +35,7 @@ public class TestCollection
Document fields = new Document();
fields["x"] = 1;
- Cursor c = db["tests"]["reads"].Find(query,-1,0,fields);
+ ICursor c = db["tests"]["reads"].Find(query,-1,0,fields);
foreach(Document result in c.Documents){
Assert.IsNotNull(result);
Assert.AreEqual(4, result["x"]);
@@ -48,7 +48,7 @@ public class TestCollection
Document query = new Document();
query["j"] = new Document().Append("$gt",20);
- Cursor c = db["tests"]["reads"].Find(query);
+ ICursor c = db["tests"]["reads"].Find(query);
foreach(Document result in c.Documents){
Assert.IsNotNull(result);
Object j = result["j"];
@@ -67,7 +67,7 @@ public class TestCollection
[Test]
public void TestSimpleInsert(){
- Collection inserts = db["tests"]["inserts"];
+ IMongoCollection inserts = db["tests"]["inserts"];
Document indoc = new Document();
indoc["song"] = "Palmdale";
indoc["artist"] = "Afroman";
@@ -82,7 +82,7 @@ public class TestCollection
[Test]
public void TestReallySimpleInsert(){
- Collection inserts = db["tests"]["inserts"];
+ IMongoCollection inserts = db["tests"]["inserts"];
Document indoc = new Document();
indoc["y"] = 1;
indoc["x"] = 2;
@@ -95,7 +95,7 @@ public class TestCollection
[Test]
public void TestPoundSymbolInsert(){
- Collection inserts = db["tests"]["inserts"];
+ IMongoCollection inserts = db["tests"]["inserts"];
Document indoc = new Document().Append("x","1234£56").Append("y",1);;
inserts.Insert(indoc);
@@ -106,7 +106,7 @@ public class TestCollection
[Test]
public void TestArrayInsert(){
- Collection inserts = db["tests"]["inserts"];
+ IMongoCollection inserts = db["tests"]["inserts"];
Document indoc1 = new Document();
indoc1["song"] = "The Axe";
indoc1["artist"] = "Tinsley Ellis";
@@ -130,7 +130,7 @@ public class TestCollection
[Test]
public void TestDelete(){
- Collection deletes = db["tests"]["deletes"];
+ IMongoCollection deletes = db["tests"]["deletes"];
Document doc = new Document();
doc["y"] = 1;
doc["x"] = 2;
@@ -150,7 +150,7 @@ public class TestCollection
[Test]
public void TestUpdateUpsertNotExisting(){
- Collection updates = db["tests"]["updates"];
+ IMongoCollection updates = db["tests"]["updates"];
Document doc = new Document();
doc["First"] = "Sam";
doc["Last"] = "Corder";
@@ -164,7 +164,7 @@ public class TestCollection
[Test]
public void TestUpdateUpsertExisting(){
- Collection updates = db["tests"]["updates"];
+ IMongoCollection updates = db["tests"]["updates"];
Document doc = new Document();
doc["First"] = "Mtt";
doc["Last"] = "Brewer";
@@ -187,13 +187,13 @@ public class TestCollection
[Test]
public void TestUpdateMany(){
- Collection updates = db["tests"]["updates"];
+ IMongoCollection 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);
+ ICursor results = updates.Find(selector);
bool found = false;
foreach(Document doc in results.Documents){
Assert.AreEqual("Cordr", doc["Last"]);
@@ -217,7 +217,7 @@ public class TestCollection
[Test]
public void TestCount(){
- Collection counts = db["tests"]["counts"];
+ IMongoCollection counts = db["tests"]["counts"];
int top = 100;
for(int i = 0; i < top; i++){
counts.Insert(new Document().Append("Last", "Cordr").Append("First","Sam").Append("cnt", i));
@@ -228,7 +228,7 @@ public class TestCollection
[Test]
public void TestCountWithSpec(){
- Collection counts = db["tests"]["counts_spec"];
+ IMongoCollection counts = db["tests"]["counts_spec"];
counts.Insert(new Document().Append("Last", "Cordr").Append("First","Sam").Append("cnt", 1));
counts.Insert(new Document().Append("Last", "Cordr").Append("First","Sam").Append("cnt", 2));
counts.Insert(new Document().Append("Last", "Corder").Append("First","Sam").Append("cnt", 3));
@@ -241,7 +241,7 @@ public class TestCollection
[Test]
public void TestCountInvalidCollection(){
- Collection counts = db["tests"]["counts_wtf"];
+ IMongoCollection counts = db["tests"]["counts_wtf"];
Assert.AreEqual(0, counts.Count());
}
@@ -24,7 +24,7 @@ public class TestCursor
[Test]
public void TestCanReadSmall()
{
- Cursor c = db["tests"]["smallreads"].FindAll();
+ ICursor c = db["tests"]["smallreads"].FindAll();
Assert.IsNotNull(c,"Cursor shouldn't be null");
int reads = 0;
@@ -36,7 +36,7 @@ public void TestCanReadSmall()
}
[Test]
public void TestCanReadMore(){
- Cursor c = db["tests"]["reads"].FindAll();
+ ICursor c = db["tests"]["reads"].FindAll();
Assert.IsNotNull(c,"Cursor shouldn't be null");
int reads = 0;
@@ -59,7 +59,7 @@ public void TestCanReadSmall()
[Test]
public void TestCanReadAndKillCursor()
{
- Cursor c = db["tests"]["reads"].FindAll();
+ ICursor c = db["tests"]["reads"].FindAll();
Assert.IsNotNull(c,"Cursor shouldn't be null");
foreach(Document doc in c.Documents){
@@ -72,7 +72,7 @@ public void TestCanReadAndKillCursor()
[Test]
public void TestCanLimit()
{
- Cursor c = db["tests"]["reads"].FindAll();
+ ICursor c = db["tests"]["reads"].FindAll();
c.Limit = 5;
Assert.IsNotNull(c,"Cursor shouldn't be null");
@@ -34,7 +34,7 @@ public class TestDatabase
[Test]
public void TestReferenceNonOid(){
Database tests = mongo["tests"];
- Collection refs = tests["refs"];
+ IMongoCollection refs = tests["refs"];
Document doc = new Document().Append("_id",123).Append("msg", "this has a non oid key");
refs.Insert(doc);
@@ -10,8 +10,7 @@ namespace MongoDB.Driver
/// <summary>
/// Description of Collection.
/// </summary>
- public class Collection
- {
+ public class Collection : IMongoCollection {
private Connection connection;
private string name;
@@ -46,28 +45,28 @@ public Collection(string name, Connection conn, string dbName)
}
public Document FindOne(Document spec){
- Cursor cur = this.Find(spec, -1,0,null);
+ ICursor cur = this.Find(spec, -1, 0, null);
foreach(Document doc in cur.Documents){
cur.Dispose();
return doc;
}
//FIXME Decide if this should throw a not found exception instead of returning null.
return null; //this.Find(spec, -1, 0, null)[0];
}
- public Cursor FindAll(){
+ public ICursor FindAll() {
Document spec = new Document();
return this.Find(spec, 0, 0, null);
}
- public Cursor Find(Document spec){
+ public ICursor Find(Document spec) {
return this.Find(spec, 0, 0, null);
}
- public Cursor Find(Document spec, int limit, int skip){
+ public ICursor Find(Document spec, int limit, int skip) {
return this.Find(spec, limit, skip, null);
}
- public Cursor Find(Document spec, int limit, int skip, Document fields){
+ public ICursor Find(Document spec, int limit, int skip, Document fields) {
if(spec == null) spec = new Document();
Cursor cur = new Cursor(connection, this.FullName, spec, limit, skip, fields);
return cur;
@@ -79,7 +78,7 @@ public Collection(string name, Connection conn, string dbName)
public long Count(Document spec){
Database db = new Database(this.connection, this.dbName);
- Collection cmd = db["$cmd"];
+ IMongoCollection cmd = db["$cmd"];
Document ret = cmd.FindOne(new Document().Append("count",this.Name).Append("query",spec));
if(ret.Contains("ok") && (double)ret["ok"] == 1){
double n = (double)ret["n"];
@@ -156,7 +155,7 @@ public Collection(string name, Connection conn, string dbName)
public void UpdateAll(Document doc, Document selector){
//TODO do this server side with generated code.
- Cursor toUpdate = this.Find(selector);
+ ICursor toUpdate = this.Find(selector);
foreach(Document udoc in toUpdate.Documents){
Document updSel = new Document();
updSel["_id"] = udoc["_id"];
@@ -45,7 +45,7 @@ public class CollectionMetaData
indexes.Clear();
- Cursor docs = db["system.indexes"].Find(new Document().Append("ns", this.fullName));
+ ICursor docs = db["system.indexes"].Find(new Document().Append("ns", this.fullName));
foreach(Document doc in docs.Documents){
indexes.Add((string)doc["name"],doc);
}
View
@@ -8,9 +8,7 @@
namespace MongoDB.Driver
{
-
- public class Cursor : IDisposable
- {
+ public class Cursor : ICursor {
private Connection connection;
private long id = -1;
View
@@ -33,23 +33,24 @@ public class Database
}
public List<String> GetCollectionNames(){
- Collection namespaces = this["system.namespaces"];
- Cursor cursor = namespaces.Find(null);
+ IMongoCollection namespaces = this["system.namespaces"];
+ ICursor cursor = namespaces.Find(null);
List<String> names = new List<string>();
foreach (Document doc in cursor.Documents){
names.Add((String)doc["name"]); //Fix Me: Should filter built-ins
}
return names;
}
- public Collection this[ String name ] {
+ public IMongoCollection this[ String name ] {
get{
return this.GetCollection(name);
}
}
- public Collection GetCollection(String name){
- Collection col = new Collection(name, this.connection, this.Name);
+ public IMongoCollection GetCollection(String name)
+ {
+ IMongoCollection col = new Collection(name, this.connection, this.Name);
return col;
}
@@ -60,7 +61,7 @@ public class Database
}
public bool Authenticate(string username, string password){
- Collection cmd = this["$cmd"];
+ IMongoCollection cmd = this["$cmd"];
Document nonceResult = cmd.FindOne(new Document().Append("getnonce", 1.0));
String nonce = (String)nonceResult["nonce"];
if (nonce == null){
@@ -79,7 +80,7 @@ public class Database
}
public void Logout(){
- Collection cmd = this["$cmd"];
+ IMongoCollection cmd = this["$cmd"];
Document logoutResult = cmd.FindOne(new Document().Append("logout", 1.0));
double ok = (double)logoutResult["ok"];
if (ok != 1.0){
@@ -44,7 +44,7 @@ public class DatabaseMetaData
}
public void AddUser(string username, string password){
- Collection users = db["system.users"];
+ IMongoCollection users = db["system.users"];
string pwd = Database.Hash(username + ":mongo:" + password);
Document user = new Document().Append("user", username).Append("pwd", pwd);
Document userExists = users.FindOne(new Document().Append("user",username));
@@ -57,12 +57,12 @@ public class DatabaseMetaData
}
public void RemoveUser(string username){
- Collection users = db["system.users"];
+ IMongoCollection users = db["system.users"];
users.Delete(new Document().Append("user", username));
}
- public Cursor ListUsers(){
- Collection users = db["system.users"];
+ public ICursor ListUsers(){
+ IMongoCollection users = db["system.users"];
return users.FindAll();
}
View
@@ -0,0 +1,17 @@
+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; }
+ }
+}
@@ -0,0 +1,26 @@
+using System;
+using System.Collections.Generic;
+using System.Text;
+
+namespace MongoDB.Driver {
+ public interface IMongoCollection {
+ string Name { get; }
+ string DbName { get; }
+ string FullName { get; }
+ CollectionMetaData MetaData { get; }
+ Document FindOne(Document spec);
+ ICursor FindAll();
+ ICursor Find(Document spec);
+ ICursor Find(Document spec, int limit, int skip);
+ ICursor Find(Document spec, int limit, int skip, Document fields);
+ long Count();
+ long Count(Document spec);
+ void Insert(Document doc);
+ void Insert(IEnumerable<Document> docs);
+ void Delete(Document selector);
+ void Update(Document doc);
+ void Update(Document doc, Document selector);
+ void Update(Document doc, Document selector, int upsert);
+ void UpdateAll(Document doc, Document selector);
+ }
+}
@@ -90,6 +90,8 @@
<Compile Include="DatabaseMetaData.cs" />
<Compile Include="DBRef.cs" />
<Compile Include="Document.cs" />
+ <Compile Include="ICursor.cs" />
+ <Compile Include="IMongoCollection.cs" />
<Compile Include="IO\GetMoreMessage.cs" />
<Compile Include="IO\InsertMessage.cs" />
<Compile Include="IO\KillCursorsMessage.cs" />

0 comments on commit f4d8fff

Please sign in to comment.