Permalink
Browse files

tie to DB

  • Loading branch information...
1 parent 0272700 commit 01f44dafa5dc22b8968e34d0043150ab6fe88335 Alex Sinm committed May 18, 2012
Showing with 200 additions and 103 deletions.
  1. +46 −8 NCouch.Test/TestHighLevel.cs
  2. +30 −8 NCouch/Changes.cs
  3. +25 −75 NCouch/DB.cs
  4. +95 −8 NCouch/Document.cs
  5. +2 −2 NCouch/Row.cs
  6. +2 −2 NCouch/View.cs
@@ -6,6 +6,7 @@
using System.Text;
using System.Reflection;
using Employ.Model;
+using System.Threading;
namespace NCouch.Test
{
@@ -62,7 +63,7 @@ public void CRUD()
Assert.IsNotNull(old_rev);
e1_1.FirstName = new_name;
- DB1.Update(e1_1);
+ e1_1.Data.Update();
Assert.IsNotNull(e1_1.Data.Rev);
Assert.AreNotEqual(old_rev, e1_1.Data.Rev);
@@ -71,13 +72,14 @@ public void CRUD()
Assert.AreEqual(e1_1.FirstName, e1_2.FirstName);
e1_1.FirstName = String.Empty;
- DB1.Update(e1_1);
+ e1_1.Data.Update();
+
Assert.AreNotEqual(e1_1.FirstName, e1_2.FirstName);
- DB1.Refresh(e1_2);
+ e1_2.Data.Refresh();
Assert.AreEqual(e1_1.FirstName, e1_2.FirstName);
- DB1.Delete(e1_1);
- DB1.Refresh(e1_2);
+ e1_2.Data.Delete();
+ e1_2.Data.Refresh();
Assert.IsNull(e1_2.Id);
}
@@ -96,7 +98,8 @@ public void Bulk()
{
List<Employee> conflicts;
Assert.IsTrue(bulk_insert(out conflicts));
- Assert.IsTrue(DB1.Delete(DB1.Read("employee-1")));
+ var emp = DB1.Read("employee-1");
+ Assert.IsTrue(emp.Delete());
Assert.IsFalse(bulk_insert(out conflicts));
Assert.AreEqual(conflicts.Count, 11);
}
@@ -142,7 +145,7 @@ public void Attachments()
Assert.AreEqual((int)re.Response.Status, 409);
}
DB1.SaveAttachment(a);
- DB1.Refresh(emp);
+ emp.Data.Refresh();
a = emp.Data.GetAttachment("picture");
a.Data = attachment;
DB1.SaveAttachment(a);
@@ -163,9 +166,44 @@ public void Updates()
DB1.Create(e);
DB1.Create(SampleData.readDocument("_design"));
DB1.Update("_design/design/_update/in_place", e.Id, new {param="foo", value="bar"});
- DB1.Refresh(e);
+ e.Data.Refresh();
Assert.AreEqual((string)e.Data["foo"], "bar");
}
+
+ [Test]
+ public void Changes()
+ {
+ var e = SampleData.readEmployee(listen_id);
+ DB1.Create(e);
+ listen_count = 0;
+ var info = DB1.GetInfo();
+ DB1.Changes(new Feed{feed = FeedMode.longpoll, since = info.update_seq}, listen);
+ e.LastName += "x";
+ e.Data.Update();
+ e.LastName += "x";
+ e.Data.Update();
+ e.LastName += "x";
+ e.Data.Update();
+ for(int i=0; i<200;i++)
+ Thread.Sleep(10);
+ Assert.AreEqual(listen_count, 3);
+ }
+
+ int listen_count = 0;
+ int listen_max_count = 3;
+ string listen_id = "employee-1";
+
+ bool listen(ChangeLog log, Exception ex)
+ {
+ if (ex != null)
+ return false;
+ foreach(Change change in log.results)
+ {
+ if (change.id == listen_id)
+ listen_count++;
+ }
+ return listen_count < listen_max_count;
+ }
}
}
View
@@ -4,7 +4,7 @@
namespace NCouch
{
- public delegate bool ChangesDelegate(DB db, ChangeLog log, Exception ex);
+ public delegate bool ChangesDelegate(ChangeLog log, Exception ex);
public enum FeedMode {normal, longpoll}
@@ -16,18 +16,36 @@ public class Feed
public bool include_docs = false;
public string filter = null;
public int? limit = null;
+
+ public Dictionary<string, object> ToDictionary()
+ {
+ var result = new Dictionary<string, object>();
+ result["feed"] = feed.ToString();
+ if (timeout.HasValue && feed != FeedMode.normal)
+ result["timeout"] = timeout <= 0 ? 60000 : timeout;
+ if (since.HasValue)
+ result["since"] = since < 0 ? 0 : since;
+ if (include_docs)
+ result["include_docs"] = "true";
+ if (!String.IsNullOrEmpty(filter))
+ result["filter"] = filter;
+ if (limit.HasValue)
+ result["limit"] = limit < 0 ? 0 : limit;
+ return result;
+ }
}
public class ChangeLog : Dictionary<string, object>
{
- public ChangeLog() : base() {}
+ public readonly DB DB;
- public ChangeLog(Dictionary<string, object> dict) : base(dict)
+ public ChangeLog(Dictionary<string, object> dict, DB db) : base(dict)
{
- object[] r = dict["results"] as object[];
+ DB = db;
+ var r = dict["results"] as object[];
foreach(Dictionary<string, object> o in r)
{
- results.Add(new Change(o));
+ results.Add(new Change(o, DB));
}
}
@@ -64,7 +82,7 @@ public string rev
{
get
{
- IDictionary d = ((object[])this["changes"])[0] as IDictionary;
+ var d = ((object[])this["changes"])[0] as IDictionary;
return d.Contains("rev") ? (string)d["rev"] : String.Empty;
}
}
@@ -81,12 +99,16 @@ public IData doc
{
get
{
- return ContainsKey("doc") ? Document.FromHash(this["doc"] as Dictionary<string, object>) : null;
+ return ContainsKey("doc") ?
+ Document.FromHash(this["doc"] as Dictionary<string, object>, DB) : null;
}
}
- public Change(Dictionary<string, object> dict) : base(dict)
+ DB DB;
+
+ public Change(Dictionary<string, object> dict, DB db) : base(dict)
{
+ DB = db;
}
}
}
View
@@ -66,42 +66,25 @@ public Request Prepare(string verb, string path)
//TODO: replication
//TODO: copy
//TODO: locals?
-
- public string Update(string update_handler, string object_id, object args)
+
+ public string Update(string update_handler)
{
- bool has_id = !String.IsNullOrEmpty(object_id);
- Request request = Prepare(has_id? "PUT" : "POST", update_handler + (has_id? "/" + EscapePath(object_id) : ""));
- request.JsonQuery = false;
- request.SetQueryObject(args);
- return request.Send().Text;
+ return Update (update_handler, null, null);
}
- public string Update(string update_handler, IData doc)
+ public string Update(string update_handler, object args)
{
- Request request = Prepare("PUT", update_handler + "/" + EscapePath(doc.Data.Id));
- request.SetObject(doc.Data);
- return request.Send().Text;
+ return Update (update_handler, null, args);
}
-
- public void Refresh(IData doc)
+
+ public string Update(string update_handler, string object_id, object args)
{
- string path = EscapePath(doc.Data.Id);
- Document new_data = Read(path);
- if (new_data == null)
- {
- doc.Data.Clear();
- }
- else
- {
- var document = doc as Document;
- if (document != null) {
- document.Clear();
- foreach(KeyValuePair<string, object> kvp in new_data) {
- document[kvp.Key] = kvp.Value;
- }
- } else
- doc.Data = new_data;
- }
+ var has_id = !String.IsNullOrEmpty(object_id);
+ var request = Prepare(has_id? "PUT" : "POST", update_handler + (has_id? "/" + EscapePath(object_id) : ""));
+ request.JsonQuery = false;
+ if (args != null)
+ request.SetQueryObject(args);
+ return request.Send().Text;
}
public byte[] ReadAttachment(Attachment attachment)
@@ -172,16 +155,17 @@ public void Create(IData doc)
doc.Data.Rev = (string)report["rev"];
if (doc.Data.Id == null)
doc.Data.Id = (string)report["id"];
+ doc.Data.DB = this;
}
public Document Read(string id)
{
- return Read<Document>(id, false);
+ return Read(id, false);
}
- public Document Read(string id, bool include_attachments)
+ public Document Read(string id, bool attachments)
{
- return Read<Document>(id);
+ return Read<Document>(id, attachments);
}
public T Read<T>(string id) where T : class, IData, new()
@@ -197,40 +181,17 @@ public T Read<T>(string id, bool attachments) where T : class, IData, new()
if (attachments)
request.Query["attachments"] = true;
var response = request.Send().GetObject() as Dictionary<string, object>;
- return Document.FromHash<T>(response);
+ var doc = Document.FromHash<T>(response, this);
+ doc.Data.DB = this;
+ return doc;
}
catch(ResponseException re)
{
if (re.Response.Status == HttpStatusCode.NotFound)
- return default(T);
+ return null;
else
throw;
}
- }
-
- public void Update(IData doc)
- {
- Request request = Prepare("PUT", EscapePath(doc.Data.Id));
- request.SetObject(doc.Data);
- doc.Data.Rev = request.Send().Parse("rev") as string;
- }
-
- public bool Delete(IData doc)
- {
- Request request = Prepare("DELETE", EscapePath(doc.Data.Id));
- request.SetQueryObject(new {rev=doc.Data.Rev});
- try
- {
- doc.Data.Rev = request.Send().Parse("rev") as string;
- return true;
- }
- catch(ResponseException re)
- {
- if (re.Response.Status != HttpStatusCode.NotFound)
- throw;
- return false;
- }
-
}
#endregion
@@ -256,8 +217,9 @@ public void Changes(Feed feed, ChangesDelegate del)
catch(Exception ex)
{
e = ex;
+ log = null;
}
- } while( del(this, e == null ? log : new ChangeLog(), e) );
+ } while( del(log, e) );
}
catch(ThreadAbortException) {}
}).Start();
@@ -267,20 +229,8 @@ public ChangeLog Changes(Feed feed)
{
Request request = Prepare("GET", "_changes");
request.JsonQuery = false;
- request.Query["feed"] = feed.feed.ToString();
- if (feed.timeout.HasValue && feed.feed != FeedMode.normal)
- request.Query["timeout"] = feed.timeout <= 0 ? 60000 : feed.timeout;
- if (feed.since.HasValue)
- request.Query["since"] = feed.since < 0 ? 0 : feed.since;
- if (feed.include_docs)
- request.Query["include_docs"] = "true";
- if (!String.IsNullOrEmpty(feed.filter))
- request.Query["filter"] = feed.filter;
- if (feed.limit.HasValue)
- request.Query["limit"] = feed.limit < 0 ? 0 : feed.limit;
-
- return new ChangeLog(
- request.Send().GetObject() as Dictionary<string, object>);
+ request.Query = feed.ToDictionary();
+ return new ChangeLog(request.Send().GetObject() as Dictionary<string, object>, this);
}
public DBInfo GetInfo()
Oops, something went wrong.

0 comments on commit 01f44da

Please sign in to comment.