Permalink
Browse files

Implemented batch processing.

  • Loading branch information...
object committed Aug 27, 2012
1 parent ae3f667 commit 0e5570b49f9b9f2ec91ccad82c91602f67f56e9b
View
@@ -21,3 +21,5 @@ NDependOut
*.dotCover
packages/
*_mm_cache.bin
+*.user
+
@@ -1,4 +1,4 @@
-<?xml version="1.0" encoding="utf-8"?>
+<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -86,6 +86,7 @@
<Compile Include="SpecialTest.cs" />
<Compile Include="TestBase.cs" />
<Compile Include="TestService.cs" />
+ <Compile Include="TransactionTest.cs" />
<Compile Include="UpdateTest.cs" />
</ItemGroup>
<ItemGroup>
@@ -1,6 +1,7 @@
using System;
using System.Net;
using System.Text;
+using Simple.NExtLib;
namespace Simple.Data.OData
{
@@ -15,62 +16,56 @@ public BatchRequestBuilder(string urlBase)
{
}
- public void BeginBatch(string command, string method, string content = null)
+ public void BeginBatch()
{
- var uri = CreateRequestUrl(command);
+ var uri = CreateRequestUrl("$batch");
this.Request = (HttpWebRequest)WebRequest.Create(uri);
this.Request.Method = RestVerbs.POST;
_batchId = Guid.NewGuid().ToString();
- this.Request.ContentType = "multipart/mixed; boundary=" + _batchId;
+ this.Request.ContentType = string.Format("multipart/mixed; boundary=batch_{0}", _batchId);
+
_contentBuilder = new StringBuilder();
+ _contentBuilder.AppendLine();
+ _contentBuilder.AppendLine(string.Format("--batch_{0}", _batchId));
+
+ _changesetId = Guid.NewGuid().ToString();
+ _contentBuilder.AppendLine(string.Format("Content-Type: multipart/mixed; boundary=changeset_{0}", _changesetId));
+ _contentBuilder.AppendLine();
}
public void EndBatch()
{
- _contentBuilder.AppendLine(string.Format("--batch({0})--", _batchId));
- AddContent(this.Request, _contentBuilder.ToString());
+ _contentBuilder.AppendLine(string.Format("--changeset_{0}--", _changesetId));
+ _contentBuilder.AppendLine(string.Format("--batch_{0}--", _batchId));
+ var content = this._contentBuilder.ToString();
+ this.Request.ContentLength = content.Length;
+ this.Request.SetContent(content);
+ _contentBuilder.Clear();
+ }
+
+ public void CancelBatch()
+ {
+ _contentBuilder.Clear();
}
public override void AddTableCommand(string command, string method, string content = null)
{
- _contentBuilder.AppendLine(string.Format("--batch_{0}", _batchId));
- if (method == RestVerbs.GET)
- {
- if (_changesetId != null)
- {
- _contentBuilder.AppendLine(string.Format("--changeset_{0}--", _changesetId));
- _contentBuilder.AppendLine();
- }
- _changesetId = null;
- }
- else
- {
- if (_changesetId == null)
- {
- _changesetId = Guid.NewGuid().ToString();
- _contentBuilder.AppendLine(string.Format("Content-Type: multipart/mixed; boundary=changeset_{0}", _changesetId));
- _contentBuilder.AppendLine(string.Format("Content-Length: {0}", 0));
- _contentBuilder.AppendLine();
- _contentBuilder.AppendLine(string.Format("--changeset_{0}", _changesetId));
- }
- }
+ _contentBuilder.AppendLine(string.Format("--changeset_{0}", _changesetId));
_contentBuilder.AppendLine("Content-Type: application/http");
_contentBuilder.AppendLine("Content-Transfer-Encoding:binary");
_contentBuilder.AppendLine();
- _contentBuilder.AppendLine(string.Format("{0} {1} HTTP/{2}", method, command, "1.1"));
- _contentBuilder.AppendLine(this.Host);
+
+ _contentBuilder.AppendLine(string.Format("{0} {1} HTTP/{2}", method, CreateRequestUrl(command), "1.1"));
if (content != null)
{
_contentBuilder.AppendLine(string.Format("Content-Type: application/atom+xml;type=entry"));
_contentBuilder.AppendLine(string.Format("Content-Length: {0}", (content ?? string.Empty).Length));
+ _contentBuilder.AppendLine();
_contentBuilder.Append(content);
}
- }
- protected override void AddContent(WebRequest request, string content)
- {
- _contentBuilder.AppendLine(content);
+ _contentBuilder.AppendLine();
}
}
}
@@ -0,0 +1,35 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+
+namespace Simple.Data.OData
+{
+ public class BatchRequestRunner : RequestRunner
+ {
+ public BatchRequestRunner(RequestBuilder requestBuilder)
+ : base(requestBuilder)
+ {
+ }
+
+ public override IEnumerable<IDictionary<string, object>> FindEntries(bool scalarResult, bool setTotalCount, out int totalCount)
+ {
+ totalCount = 0;
+ return null;
+ }
+
+ public override IDictionary<string, object> InsertEntry(bool resultRequired)
+ {
+ return null;
+ }
+
+ public override int UpdateEntry()
+ {
+ return 0;
+ }
+
+ public override int DeleteEntry()
+ {
+ return 0;
+ }
+ }
+}
@@ -11,17 +11,6 @@ public CommandRequestBuilder(string urlBase)
}
public override void AddTableCommand(string command, string method, string content = null)
- {
- this.Request = CreateTableRequest(command, method, content);
- }
-
- protected override void AddContent(WebRequest request, string content)
- {
- request.ContentType = "application/atom+xml";
- request.SetContent(content);
- }
-
- private HttpWebRequest CreateTableRequest(string command, string method, string content = null)
{
var uri = CreateRequestUrl(command);
var request = (HttpWebRequest)WebRequest.Create(uri);
@@ -36,10 +25,11 @@ private HttpWebRequest CreateTableRequest(string command, string method, string
if (content != null)
{
- AddContent(request, content);
+ request.ContentType = "application/atom+xml";
+ request.SetContent(content);
}
- return request;
+ this.Request = request;
}
}
}
@@ -1,8 +1,68 @@
-using Simple.Data.OData;
+using System.Collections.Generic;
+using System.Linq;
+using System.Net;
namespace Simple.Data.OData
{
public class CommandRequestRunner : RequestRunner
{
+ public CommandRequestRunner(RequestBuilder requestBuilder)
+ : base(requestBuilder)
+ {
+ }
+
+ public override IEnumerable<IDictionary<string, object>> FindEntries(bool scalarResult, bool setTotalCount, out int totalCount)
+ {
+ using (var response = TryRequest(_requestBuilder.Request))
+ {
+ totalCount = 0;
+ IEnumerable<IDictionary<string, object>> result = null;
+ if (response.StatusCode != HttpStatusCode.OK)
+ {
+ result = Enumerable.Empty<IDictionary<string, object>>();
+ }
+ else
+ {
+ var stream = response.GetResponseStream();
+ if (setTotalCount)
+ result = DataServicesHelper.GetData(stream, out totalCount);
+ else
+ result = DataServicesHelper.GetData(response.GetResponseStream(), scalarResult);
+ }
+
+ return result;
+ }
+ }
+
+ public override IDictionary<string, object> InsertEntry(bool resultRequired)
+ {
+ var text = Request(_requestBuilder.Request);
+ if (resultRequired)
+ {
+ return DataServicesHelper.GetData(text).First();
+ }
+ else
+ {
+ return null;
+ }
+ }
+
+ public override int UpdateEntry()
+ {
+ using (var response = TryRequest(_requestBuilder.Request))
+ {
+ // TODO
+ return response.StatusCode == HttpStatusCode.OK ? 1 : 0;
+ }
+ }
+
+ public override int DeleteEntry()
+ {
+ using (var response = TryRequest(_requestBuilder.Request))
+ {
+ // TODO: check response code
+ return response.StatusCode == HttpStatusCode.OK ? 1 : 0;
+ }
+ }
}
}
Oops, something went wrong.

0 comments on commit 0e5570b

Please sign in to comment.