<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>MindTouch Developer Blog &#187; mono</title>
	<atom:link href="http://blog.developer.mindtouch.com/tag/mono/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.developer.mindtouch.com</link>
	<description>You’ve reached the home of the MindTouch engineering team. Here, MindTouch enginerds muse about random topics. Kick back and enjoy!</description>
	<lastBuildDate>Mon, 05 Mar 2012 20:17:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>10.1.3 is Here!</title>
		<link>http://blog.developer.mindtouch.com/2012/03/05/10-1-3-is-here/</link>
		<comments>http://blog.developer.mindtouch.com/2012/03/05/10-1-3-is-here/#comments</comments>
		<pubDate>Mon, 05 Mar 2012 20:08:02 +0000</pubDate>
		<dc:creator>jamesv</dc:creator>
				<category><![CDATA[Releases]]></category>
		<category><![CDATA[10.1.3]]></category>
		<category><![CDATA[mindtouch]]></category>
		<category><![CDATA[mono]]></category>
		<category><![CDATA[release notes]]></category>
		<category><![CDATA[upgrade]]></category>

		<guid isPermaLink="false">http://blog.developer.mindtouch.com/?p=2103</guid>
		<description><![CDATA[Tweet 10.1.3 is here! &#160; This minor release includes various bug fixes and a few new features. &#160;Please see the list below for some notable fixes:&#160; Improved editor load time Corrected an issue when trying to navigate cursor within search field Corrected an issue causing page view count to incorrectly increment (his addresses future increments, [...]]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton2103" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Fblog.developer.mindtouch.com%2F2012%2F03%2F05%2F10-1-3-is-here%2F&amp;text=10.1.3%20is%20Here%21&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fblog.developer.mindtouch.com%2F2012%2F03%2F05%2F10-1-3-is-here%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://blog.developer.mindtouch.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p><a href="http://blog.developer.mindtouch.com/wp-content/uploads/2012/03/open-box.jpg" rel="" style="" target="" title=""><img alt="" class="size-medium wp-image-2115 alignright" height="136" src="http://blog.developer.mindtouch.com/wp-content/uploads/2012/03/open-box-300x227.jpg" style="" title="open-box" width="180" /></a></p>
<p>10.1.3 is here! &nbsp; This minor release includes various bug fixes and a few new features. &nbsp;Please see the list below for some notable fixes:&nbsp;</p>
<ul style="margin-top: 8px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 25px; list-style-type: disc; list-style-position: outside; list-style-image: initial; ">
<li class="rn-list-item" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 5px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 5px; line-height: 1.6; "><em><span class="rn-sum">Improved editor load time</span></em></li>
<li class="rn-list-item" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 5px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 5px; line-height: 1.6; "><em><span class="rn-sum">Corrected an issue when trying to navigate cursor within search field</span></em></li>
<li class="rn-list-item" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 5px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 5px; line-height: 1.6; "><em><span class="rn-sum">Corrected an issue causing page view count to incorrectly increment (his addresses future increments, though is not retroactive).</span></em></li>
<li class="rn-list-item" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 5px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 5px; line-height: 1.6; "><em><span class="rn-sum">Corrected an issue causing&nbsp;links to fail when referencing&nbsp;multiple tags that have define pages.</span></em></li>
<li class="rn-list-item" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 5px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 5px; line-height: 1.6; "><em><span class="rn-sum">Addressed a rare&nbsp;issue where performing an advanced search would redirect the user to&nbsp;the home page.</span></em></li>
<li class="rn-list-item" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 5px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 5px; line-height: 1.6; "><em><span class="rn-sum">Resolved a cosmetic&nbsp;issue where a user&#39;s configuration within the Control Panel was not displaying group membership.</span></em></li>
</ul>
<p class="rn-list-item" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 5px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 5px; line-height: 1.6; ">&nbsp;</p>
<h2 style="margin-top: 8px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><strong>New Features</strong></h2>
<p style="margin-top: 8px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">In addition, 10.1.3 adds the following new features!</p>
<ul style="margin-top: 8px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 25px; list-style-type: disc; list-style-position: outside; list-style-image: initial; ">
<li class="rn-list-item" style="margin-top: 8px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><em>Support for HTML5 content elements</em></li>
<li class="rn-list-item" style="margin-top: 8px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><em>Pagination for </em>@api/deki/pages</li>
</ul>
<p style="margin-top: 8px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;</p>
<p style="margin-top: 8px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;</p>
<p class="rn-list-item" style="margin-top: 8px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">To upgrade* your installation, please visit our <a href="http://developer.mindtouch.com/en/docs/MindTouch_Administration_Guide/Upgrading_MindTouch" target="_blank">upgrade documentation</a> for further details.&nbsp; In addition, you can download the following packages:</p>
<ul style="margin-top: 8px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 25px; list-style-type: disc; list-style-position: outside; list-style-image: initial; ">
<li class="rn-list-item" style="margin-top: 8px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">prepackaged&nbsp;<a href="http://sourceforge.net/projects/dekiwiki/files/MindTouch%20Deki%20VM/10.1.3%20%28Pipestone%29%20Software%20Appliance/MindTouch_VM_10.1.3.zip/download" target="_blank">Debian VM</a></li>
<li class="rn-list-item" style="margin-top: 8px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><a href="http://sourceforge.net/projects/dekiwiki/files/MindTouch%20Windows%20MSI/MindTouch%2010.1.3/MindTouch_Win2003_10.1.3.zip/download">Windows Server 2003</a>&nbsp; MSI</li>
<li class="rn-list-item" style="margin-top: 8px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><a href="http://sourceforge.net/projects/dekiwiki/files/MindTouch%20Windows%20MSI/MindTouch%2010.1.3/MindTouch_Win2008_10.1.3.zip/download" target="_blank">Windows Server 2008</a>&nbsp;MSI</li>
</ul>
<p style="margin-top: 8px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;</p>
<p style="margin-top: 8px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><span style="font-size:12px;"><strong style="font-style: normal; font-weight: bold; ">*IMPORTANT:</strong><br />
	Within Linux environments, upgrading to MindTouch 10.1+ from any previous version will require a Mono upgrade to Mono 2.10.2. &nbsp;Failure to do so will result in the API crashing; this latest version of MindTouch does require Mono 2.10.2. &nbsp;Please refer to the upgrade documentation referenced above for further details on upgrading your Mono installation within Linux environments.</span></p>
<p style="margin-top: 8px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;</p>
<p style="margin-top: 8px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.developer.mindtouch.com/2012/03/05/10-1-3-is-here/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>10.1 is coming&#8230;</title>
		<link>http://blog.developer.mindtouch.com/2011/06/23/10-1-is-coming/</link>
		<comments>http://blog.developer.mindtouch.com/2011/06/23/10-1-is-coming/#comments</comments>
		<pubDate>Fri, 24 Jun 2011 00:31:43 +0000</pubDate>
		<dc:creator>jamesv</dc:creator>
				<category><![CDATA[Dev]]></category>
		<category><![CDATA[10.1]]></category>
		<category><![CDATA[mindtouch]]></category>
		<category><![CDATA[mono]]></category>

		<guid isPermaLink="false">http://blog.developer.mindtouch.com/?p=2002</guid>
		<description><![CDATA[TweetAre you excited?  We are.  10.1 is coming soon, and we will update you again when it has been released. In the meantime, there is one important note with 10.1. On Linux installations, an upgrade of Mono 2.10.1 is required (note that Mono 2.10.2 is compatible as well). Mono 2.10.1 is backwards compatible with MindTouch [...]]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton2002" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Fblog.developer.mindtouch.com%2F2011%2F06%2F23%2F10-1-is-coming%2F&amp;text=10.1%20is%20coming%26%238230%3B&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fblog.developer.mindtouch.com%2F2011%2F06%2F23%2F10-1-is-coming%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://blog.developer.mindtouch.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p><a href="http://blog.developer.mindtouch.com/wp-content/uploads/2011/06/Screen-shot-2011-06-23-at-5.28.49-PM.png"><img class="size-thumbnail wp-image-2003 alignleft" src="http://blog.developer.mindtouch.com/wp-content/uploads/2011/06/Screen-shot-2011-06-23-at-5.28.49-PM-150x150.png" alt="" width="74" height="74" /></a>Are you excited?  We are.  10.1 is coming soon, and we will update you again when it has been released.</p>
<p>In the meantime, there is one important note with 10.1.  On Linux installations, an upgrade of Mono 2.10.1 is <strong>required</strong> (note that Mono 2.10.2 is compatible as well).  Mono 2.10.1 is backwards compatible with MindTouch 10.x, so in preparation for our upcoming release you can get a head start by upgrading your Mono version.  That way, you&#8217;ll be all ready for the latest and greatest from MindTouch on launch day.</p>
<p>To upgrade Mono on CentOS/RHEL simply follow this <a href="http://developer.mindtouch.com/en/docs/mindtouch_setup/010Installation/060Installing_on_CentOS/Installing%2F%2FUpgrade_Mono_on_CentOS" target="_blank">documentation</a>.</p>
<p>To upgrade Mono on Debian/Ubuntu (use this for your pre-packaged MindTouch VM) simply follow this <a href="http://developer.mindtouch.com/en/docs/mindtouch_setup/010Installation/020Installing_on_Debian/Installing%2F%2FUpgrading_Mono_on_Debian" target="_blank">documentation</a>.</p>
<p>&#8230;stay tuned for more details on MindTouch 10.1.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.developer.mindtouch.com/2011/06/23/10-1-is-coming/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>What&#8217;s asynchrony good for?</title>
		<link>http://blog.developer.mindtouch.com/2011/06/13/whats-asynchrony-good-for/</link>
		<comments>http://blog.developer.mindtouch.com/2011/06/13/whats-asynchrony-good-for/#comments</comments>
		<pubDate>Mon, 13 Jun 2011 14:34:00 +0000</pubDate>
		<dc:creator>arnec</dc:creator>
				<category><![CDATA[API Team (Red)]]></category>
		<category><![CDATA[async]]></category>
		<category><![CDATA[dream]]></category>
		<category><![CDATA[mono]]></category>
		<category><![CDATA[monospace]]></category>

		<guid isPermaLink="false">http://blog.developer.mindtouch.com/?p=1988</guid>
		<description><![CDATA[TweetIn my previous post, &#34;Tasked to get Results&#34;, I covered the use of DReAM&#39;s Result and the TPL&#39;s Task as waithandles for asynchronously completed work. Except that all examples dealt with blocking behavior to await the completion of the other task, which made the async illustration rather academic. Blocking for async work does have its [...]]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton1988" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Fblog.developer.mindtouch.com%2F2011%2F06%2F13%2Fwhats-asynchrony-good-for%2F&amp;text=What%26%238217%3Bs%20asynchrony%20good%20for%3F&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fblog.developer.mindtouch.com%2F2011%2F06%2F13%2Fwhats-asynchrony-good-for%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://blog.developer.mindtouch.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>In my previous post, &quot;<a href="http://blog.developer.mindtouch.com/2011/06/10/tasked-to-get-results/" target="_blank">Tasked to get Results</a>&quot;, I covered the use of <a href="https://github.com/MindTouch/dream" target="_blank">DReAM</a>&#39;s <code>Result</code> and the <a href="http://msdn.microsoft.com/en-us/library/dd460717.aspx" target="_blank">TPL</a>&#39;s <code>Task</code> as waithandles for asynchronously completed work. Except that all examples dealt with blocking behavior to await the completion of the other task, which made the async illustration rather academic.</p>
<p>Blocking for async work does have its applications. The canonical example being a controlling thread firing off a number of workers and waiting for all of them to complete to gather the results. For implementing this example,&nbsp;<code>Task</code> and <code>Result</code> can be thought of as convenient alternatives to <code>WaitHandle.WaitAll()</code> or <code>Thread.Join()</code> or <code>EndInvoke()</code> calls:</p>
<pre class="csharp" name="code">// Summing up Results
var results = new List<Result<int>>();
for(var i = 0; i < 100; i++) {
&nbsp;&nbsp;&nbsp; results.Add(SomeAsyncWork(i));
}
var total = results.Sum(x => x.Wait());

// Summing up Tasks
var tasks = new List<Task<int>>();
for(var i = 0; i < 100; i++) {
&nbsp;&nbsp;&nbsp; tasks.Add(SomeMoreAsyncWork(i));
}
var total = tasks.Sum(x => x.Result);
</pre>
<p>Both rely on implicit blocking behavior to perform the summation. We could just as easily have explicitly waited on all the handles with C#5.0 <a href="http://msdn.microsoft.com/en-us/vstudio/gg316360" target="_blank">AsyncCtp</a>&#39;s <code>TaskEx.WhenAll()</code> or DReAM&#39;s <code>Result.Join()</code>, and then summed the results. One interesting feature to keep in mind about the join constructs in TPL and DReAM is that both return synchronization handles themselves. I&#39;ll explain why this is useful in the next article. Either way though, our main thread was blocked until the workers completed.</p>
<h2>Finding opportunities for asynchrony</h2>
<p>The parallel worker example is a great way to show the power of <code>Task</code> and <code>Result</code> for managing many workers, but in itself is not asynchronous. Asynchrony implies that we do not block waiting for work to complete, whereas the above example explicilty blocks the main thread. Asynchronous behavior may be combined with parallelism to manage resources better, but asynchronous behavior does not mean that we are performing the work in parallel. Quite the opposite, the primary goal of asynchrony is to suspend the caller until the asynchronous operation has completed. This means that most asynchronous workflows are inherently serial even if the steps executed may happen on different threads.</p>
<p>To properly show the benefits of asynchrony let&#39;s use something that we deal with every day and is inherently asynchronous: <strong>I/O &#8211;</strong> Whenver we are reading from or writing to a file, making a database call or calling a webservice we are making an I/O request and waiting for the response. <strong>We don&#39;t think of I/O as asynchronous because most I/O APIs have exposed these operations as synchronous method calls</strong>, i.e. they block the thread while waiting for the out-of-context operation to complete:</p>
<pre class="csharp" name="code">var command = new SqlCommand(
&nbsp;&nbsp;&nbsp; &quot;SELECT CategoryID, CategoryName FROM Categories;&quot;,
&nbsp;&nbsp;&nbsp; connection);

// Blocking!
connection.Open();

//Blocking!
var reader = command.ExecuteReader();

// Blocking!
while(reader.Read()) {
&nbsp;&nbsp;&nbsp; // read row
}
reader.Close();
</pre>
<p>Most of time, we don&#39;t even interact with a DB at this low level, but that doesn&#39;t change the fact that in order to get data back from the database, we have to:</p>
<ol>
<li>Open a socket to the DB and <strong>wait </strong>for the connection to be established</li>
<li>Send our SQL and <strong>wait </strong>to for a cursor to become readable</li>
<li>Iterate over the cursor and <strong>wait </strong>for data to be sent</li>
</ol>
<p>Each operation sends data over the wire and waits on a response. That waiting time is blocking the current thread.</p>
<h2>Why so slow, I/O?</h2>
<p>Most I/O operations are quite fast, and much of our optimization work relates to reducing the times of database queries, file reads and web service calls to the single digit milliseconds. However, compared to in-process memory access, which is measured in microseconds, even the best optimized I/O operation is orders of magnitude slower. Every time we treat I/O as synchronous we rely on thread scheduling to utilize the CPU time not utilized by the blocking thread, which in turn costs us in thread context switching and memory footprint.</p>
<p>But we&#39;ve been doing this for ages and it hasn&#39;t been a problem, right? <strong>What&#39;s changed?</strong></p>
<p>For one thing, it has actually been a problem and for the most part, we&#39;ve just been throwing larger and larger machines at our server clusters to make up for the resource wastes committed. But all the optimization of I/O aside, in the real world we will always encounter the occasional slow DB query, file read/write blocked by a lock and web service request over a congested network.</p>
<p>For another, on top of those real world concerns, we&#39;ve received a wake-up call in the form of evented I/O: Suddenly <a href="http://nodejs.org/" target="_blank">node.js</a> and python&#39;s <a href="http://www.tornadoweb.org/" target="_blank">tornado web server</a> have shown us that a single server could handle thousands of simultaneous connections, which would make traditional servers fall over and die. This incredible capacity is not due to some magic optimization or that python and javascript have secretly gotten faster than other languages. What they are doing is deal with I/O as non-blocking. It is easy to keep thousand&#39;s of sockets open at the same time when each isn&#39;t tied to a process or thread. When node.js <a href="http://amix.dk/blog/post/19490#Plurk-Instant-conversations-using-comet" target="_blank">boasts 100k+ connections at once</a>, <strong>it still is only processing one request at a time</strong>, but every time a I/O operation is required, it schedules a callback for completion rather than blocking until the I/O has completed. That this one at a time model ends up with more requests/second shows how much overhead we incur when we rely on thread management. But there&#39;s nothing about node.js performance that couldn&#39;t be replicated in C# (and <a href="http://jacksonh.tumblr.com/post/1159500924/manos-de-mono-the-manifesto" target="_blank">Manos de Mono</a> is doing exactly that), as long as we break our addiction to blocking.</p>
<h2>&#8230;and after you are done with that, could you do this?</h2>
<p>Using an asynchronous approach provides a way to mitigate this in-process vs. I/O operation speed differential and stop blocking our threads. What we want is to be notified when the asynchronous operation has completed. While most I/O APIs provide some type of asynchronous calling convention (ADO.NET only provides it on the <code>Execute*</code> members), <code>Task</code> and <code>Result</code> provide a unifying interface for all operations and a way to handle the continuation of work in a non-blocking form in a much simpler way:</p>
<pre class="csharp" name="code">// Result
connection.Open();
Async.From(command.BeginExecuteReader,
           command.EndExecuteReader,
           null,
           new Result<SqlDataReader>())
&nbsp;&nbsp;&nbsp; .WhenDone(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result => {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var reader = result.Value;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(reader.Read()) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // read row
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; reader.Close();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; );

// Task
connection.Open();
Task.Factory.FromAsync<SqlDataReader>(command.BeginExecuteReader,
                                      command.EndExecuteReader,null)
&nbsp;&nbsp;&nbsp; .ContinueWith(
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; task => {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; var reader = task.Result;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; while(reader.Read()) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // read row
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; reader.Close();
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }
&nbsp;&nbsp;&nbsp; );
</pre>
<p>The above introduces the continuation constructs <code>Result.WhenDone</code> and <code>Task.ContinueWith</code>, which allow us to chain operations to execute in the context of the asynchronous method&#39;s callback. Both <code>Result</code> and&nbsp;<code>Task</code><code> </code>provide a way to convert the standard <code>Begin*/End*</code> pattern from any API into <code>Result</code>/<code>Task</code>, which we can then attach a continuation to. I should note that in the C#5.0 <a href="http://msdn.microsoft.com/en-us/vstudio/gg316360" target="_blank">AsyncCtp</a> Microsoft has added Extension Methods for virtually all Microsoft async patterns to further simplify invocation.</p>
<p><a href="http://blog.developer.mindtouch.com/2011/06/21/exit-screen-right/">Next time</a>, we&#39;ll take a look at a more complex workflow using the <a href="http://en.wikipedia.org/wiki/Continuation-passing_style" target="_blank">continuation passing style</a> possible with <code>Result.WhenDone</code> and <code>Task.ContinueWith</code> and what complications it may introduce.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.developer.mindtouch.com/2011/06/13/whats-asynchrony-good-for/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>MindTouch Coroutines and C#5.0 async</title>
		<link>http://blog.developer.mindtouch.com/2010/11/01/mindtouch-coroutines-and-c5-0-async/</link>
		<comments>http://blog.developer.mindtouch.com/2010/11/01/mindtouch-coroutines-and-c5-0-async/#comments</comments>
		<pubDate>Mon, 01 Nov 2010 15:30:30 +0000</pubDate>
		<dc:creator>arnec</dc:creator>
				<category><![CDATA[API Team (Red)]]></category>
		<category><![CDATA[Dev]]></category>
		<category><![CDATA[geek]]></category>
		<category><![CDATA[asynchronous]]></category>
		<category><![CDATA[coroutine]]></category>
		<category><![CDATA[dream]]></category>
		<category><![CDATA[mono]]></category>

		<guid isPermaLink="false">http://blog.developer.mindtouch.com/?p=1653</guid>
		<description><![CDATA[Tweet Photo by Shereen M. This week, at PDC 2010, Anders Hejlsberg introduced what is to become C# 5.0 and it&#8217;s major new feature: Making Async easy. The main facility for achieving this goal is the language keyword pair or async and await. Together they allow synchronous methods to be called in an asynchronous way [...]]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton1653" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Fblog.developer.mindtouch.com%2F2010%2F11%2F01%2Fmindtouch-coroutines-and-c5-0-async%2F&amp;text=MindTouch%20Coroutines%20and%20C%235.0%20async&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fblog.developer.mindtouch.com%2F2010%2F11%2F01%2Fmindtouch-coroutines-and-c5-0-async%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://blog.developer.mindtouch.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><div style="float: right; padding: 6px; text-align: center; font-size: 11px;"><img src="http://farm3.static.flickr.com/2165/2424579233_4bf0838fb7_m.jpg" alt="" /><br />
<a href="http://www.flickr.com/photos/shereen84/e">Photo by Shereen M.</a></div>
<p>This week, at PDC 2010, <a href="http://bit.ly/bBGOfV" target="_blank">Anders Hejlsberg introduced what is to become C# 5.0</a> and it&#8217;s major new feature: Making Async easy. The main facility for achieving this goal is the language keyword pair or <code>async</code> and <code>await</code>. Together they allow synchronous methods to be called in an asynchronous way without having to jump through all sorts of hoops.</p>
<p>This is very exciting stuff for us here at MindTouch, since we&#8217;ve built almost exactly that facility with our Asynchronous Method Pattern and Coroutine infrastructure in <a href="http://developer.mindtouch.com/dream" target="_blank">DReAM</a>. But the one thing that has always bothered us what the additional manual code and sometimes unintuitive syntax required.</p>
<h2>The Result and Task based asynchronous patterns</h2>
<p>Steve Bjorg introduced <code>Result&lt;T&gt;</code> with DReAM on top of the .NET 2.0 and mono 1.1.16 back in 2006. You can find a quick overview of it <a href="ht<code>tp://</code>www.mindtouch.com/blog/2007/10/17/dream-asynchronicity-results/" target="_blank">here</a>. I've also <a href="http://blog.developer.mindtouch.com/2009/11/05/mindtouch-monospace-going-concurrent-keeping-your-sanity/" target="_blank">covered it extensively in my monoconf talk</a>. It's is similar in purpose to the <code>Task</code> class in the <a href="http://msdn.microsoft.com/en-us/library/dd460717.aspx" target="_blank">Task Parallel Library </a>introduced by Microsoft in .NET 4.0. Both of them are a kind of <a href="http://en.wikipedia.org/wiki/Futures_and_promises" target="_blank">future</a>, a handle for a value that may or may not have been produced yet. Both provide asynchrony patterns for easily calling a method that promises to produce a result eventually.</p>
<p>The MindTouch  Asynchronous Method Pattern (AMP) takes this form:</p>
<pre class="csharp" name="code">Result&lt;T&gt; MethodName(...input paramaters..., Result&lt;T&gt; result)</pre>
<p>where the trailing <code>Result&lt;T&gt;</code> is used to set up conditions on the return value, such as Timeout.</p>
<p>The Task-based Asynchronous Pattern (TAP) looks like this:</p>
<pre class="csharp" name="code">Task&lt;T&gt; MethodNameAsync(...input paramaters...)</pre>
<p>Both are easy enough to call in blocking form, but that defeats their purpose. Both could also be set up with continuation handles, which get executed once a value <code>T</code> is available, but that takes the return code out of the current flow. And as completions call other asynchronous methods, your code just wanders off the right-hand side of the screen. This is common problem with continuations and is both a aesthetic and readability problem, in that your logically linear (<em>if non-blocking</em>) execution flow becomes increasingly difficult to follow.</p>
<h2>Simplifying async flow with MindTouch Coroutines</h2>
<p>What would be really nice is being able to call an asynchronous method and write the code that deals with the result on the next line, while simultaneously having your code suspend itself, freeing up your thread while the asynchronous code completes.</p>
<p>One way to approach this is with coroutines, i.e. a method that can exit and resume multiple times, rather having only one entry point. A coroutine can yield control to the async method and resume after it completes. In .NET 2.0 the <code>yield </code>operator was added to C# for the iterator pattern. <strong>If you turn the iterator pattern upside down, you end up with a method that yields not iteration results but continuations </strong>to some engine executing the control flow, i.e. MindTouch Coroutines:</p>
<pre class="csharp" name="code">Result&lt;XDoc&gt; r;
yield return r = Download(uri, new Result&lt;XDoc&gt;);
var doc = r.Value;</pre>
<p>This provided us with the ability to write methods in synchronous style that could call to potentially long running asynchronous processes but continue their linear flow without ever blocking the thread. For a more detailed look at how coroutines are implemented check <a href="http://www.mindtouch.com/blog/2008/10/21/dream-asynchronicity-coroutines/" target="_blank">this article</a>. However, the cost of achieving this came with some syntactic artifacts.</p>
<p>As illustrated above, we can't declare and assign the result type T in the yield return, hence the pre-declaration of <code>Result&lt;XDoc&gt; r</code> and the trailing <code>var doc = r.Value</code>. <em>Three lines instead of one</em>. In C# 3.0, using Lambdas, we reduced this to two lines:</p>
<pre class="csharp" name="code">XDoc doc;
yield return doc = DownloadAndProcess(uri, new Result&lt;XDoc&gt;).Set(v =&gt; doc = v);</pre>
<p>Another artifact is that a coroutine has to return an enumerator. We use  <code>IYield</code>, which <code>Result</code> implements and coroutines require the return type <code>IEnumerator&lt;IYield&gt;</code> and by convention use a <code>Result&lt;T&gt;</code> as their last argument. That means <strong>coroutines can yield AMPs but are not AMPs themselves</strong>. As I stated above, a coroutine is an iterator turned upside down, yielding control to some executing engine. This means that every AMP that uses a coroutine can be written using this convention:</p>
<pre class="csharp" name="code">Result&lt;XDoc&gt; DownloadAndProcess(XUri uri, Result&lt;XDoc&gt; result) {
  return Coroutine.Invoke(DownloadAndProcess_Coroutine, uri, result);
}
IEnumerator&lt;IYield&gt; DownloadAndProcess_Coroutine(XUri uri, Result&lt;XDoc&gt; result) {
  XDoc doc;
  yield return Plug.New(uri).Get(new Result&lt;DreamMessage&gt;)
    .Set(v =&gt; doc = v.ToDocument());
  // do some more async work
  result.Return(doc);
}</pre>
<p><em><strong>Note:</strong> </em>I snuck in <code>Plug</code> here, which is our fluent interface wrapper around <code>HttpWebRequest</code> and implements the AMP, internally using the async interface of <code>HttpWebRequest</code>, i.e. we can yield a <code>Plug.Get</code> and it will resume execution once the request completes.</p>
<h2>C# 5.0 async/wait continuation flow</h2>
<p>With C# vNext, Microsoft introduces two new language keywords that basically let you create the same flow we provide via the AMP and Coroutines, but using <code>Task</code> and leaving the syntactic cost to the compiler instead. This means that <strong>the TAP can be used without having to define a custom method with a different signature to execute the async work flow</strong>:</p>
<pre class="csharp" name="code">async Task&lt;XDoc&gt; DownloadAndProcess(XUri uri) {
  var doc = await Plug.New(uri).Get();
  // do some more async work
  return doc;
}</pre>
<p>The <code>async</code> keyword marks the method as one that the compiler needs to rewrite a sequence of continuations. The <code>await</code> keyword tells the compiler that a TAP method is being called and to squirrel the rest of the method body into a continuation that is executed once the <code>Task&lt;XDoc&gt;</code> has a result. Here the compiler takes on the work of letting you use a single method signature to call either as a regular method or as source of a continuation and rewrites a TAP call to yield execution flow and resume after completion. Using <code>async/await</code> certainly reduces ceremony and makes it all more readable. We wish we could have had that luxury when we embarked on this pattern 4 years ago. <em><strong>Note:</strong></em> The above code pre-supposes a version of <code>Plug</code> that implements the TAP instead of the AMP.</p>
<h2>yield vs. await vs. something else</h2>
<p>Shortly after Anders' PDC talk Eric Lippert posed the question whether <code>await</code> is the best name for this new facility in the post <a href="http://blogs.msdn.com/b/ericlippert/archive/2010/10/29/asynchronous-programming-in-c-5-0-part-two-whence-await.aspx" target="_blank">Asynchronous Programming in C# 5.0 part two: Whence await?</a> on his blog. The comment thread is a good read of arguments for and against various keywords. Personally being partial to <code>yield</code>, I read Jon Skeet's case against it in his post <a href="http://msmvps.com/blogs/jon_skeet/archive/2010/10/30/c-5-async-and-choosing-terminology-why-i-m-against-quot-yield-quot.aspx" target="_blank">C# 5 async and choosing terminology: why I'm against "yield"</a> with interest. His point is that <em>"[w]hen the action completes very quickly and synchronously, it isn't yielding at all." </em> My personal feeling is that whether or not the call returns very quickly is an implementation detail invisible to the caller. The caller is <em>yielding control</em> to the callee, who makes the decision whether to return immediately or suspend the caller until completion.</p>
<p>However <code>await</code> does seem like the least appropriate of the options, since we're really not waiting at all. Syntax like <code>continue after</code>, <code>continue with</code> and <code>continue when</code> certainly seem more appropriate and descriptive of the action taking place. While <code>yield return</code> perfectly describes to me what happens, maybe that is a sign the terminology reveals the implementation detail rather than the intent. <strong>The caller doesn't care about the yielding of control. The caller just wants the result from the call and get on with its business.</strong> So from a clarity of code perspective, I have to side with the <code>continue (after|with|when)</code> camp.</p>
<h2>When can I use async/await?</h2>
<p>The CTP is available now. Of course, there is no release date, taking a gaming industry "when it's done!" stance. My bet is that <code>async/await</code> will be usable in the next mono release (<em>2.10? 3.0?</em>) before C# 5.0 ships. Any way you look at it, though, it's going to be a while before you can rely on it existing on a platform you didn't personally configure.</p>
<p>In the meantime, DReAM coroutines are battle tested, powering all REST services in the MindTouch product and any other product using DReAM, and you can be sure that as C#5 approaches we will be tracking its progress closely to evaluate using the TAP in DReAM. We certainly would love to take advantage of the simplified syntax, while staying compatible with our existing syntax.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.developer.mindtouch.com/2010/11/01/mindtouch-coroutines-and-c5-0-async/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Generate .NET documentation with MindTouch 2010</title>
		<link>http://blog.developer.mindtouch.com/2010/10/05/generate-net-documentation-with-mindtouch-2010/</link>
		<comments>http://blog.developer.mindtouch.com/2010/10/05/generate-net-documentation-with-mindtouch-2010/#comments</comments>
		<pubDate>Tue, 05 Oct 2010 18:55:33 +0000</pubDate>
		<dc:creator>arnec</dc:creator>
				<category><![CDATA[API Team (Red)]]></category>
		<category><![CDATA[Documentation]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[dream]]></category>
		<category><![CDATA[mono]]></category>

		<guid isPermaLink="false">http://blog.developer.mindtouch.com/?p=1448</guid>
		<description><![CDATA[TweetIf you write .NET code consumed by others, you are undoubtedly familiar with the inline XML documentation markup available. It&#8217;s a fairly simple embedded documentation syntax for which Visual Studio even provides some Intellisense and compile time checking. It keeps documentation and signature meta-data that can be derived from the code separate to avoid drift [...]]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton1448" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Fblog.developer.mindtouch.com%2F2010%2F10%2F05%2Fgenerate-net-documentation-with-mindtouch-2010%2F&amp;text=Generate%20.NET%20documentation%20with%20MindTouch%202010&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fblog.developer.mindtouch.com%2F2010%2F10%2F05%2Fgenerate-net-documentation-with-mindtouch-2010%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://blog.developer.mindtouch.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>If you write .NET code consumed by others, you are undoubtedly familiar with the inline XML documentation markup available. It&#8217;s a fairly simple embedded documentation syntax for which Visual Studio even provides some Intellisense and compile time checking. It keeps documentation and signature meta-data that can be derived from the code separate to avoid drift between code and documentation as much as possible.</p>
<p>Most developers initially start using it to provide Intellisense support for their code. But sooner or later most will want to publish the documentation as a reference document for their APIs. This would seem like the ideal use case of the mark-up format, but once you starting looking at the available tools, most developers find them lacking. Originally, NDoc provided the equivalent documentation generation that javadoc provides to developers on the Java side. Unfortunately NDoc was discontinued before .NET 2.0. In its place there exist Microsoft&#8217;s Sandcastle and mono&#8217;s monodoc.  And now <strong>mindtouch.doc</strong> has been added to that list.</p>
<p>The goal of <strong>mindtouch.doc</strong> was to generate hierarchical reference documentation that could easily be imported into an MindTouch install. Unlike the standalone documentation generated by other tools, documentation imported into MindTouch can be easily extended by the extensive authoring and curation capabilities of MindTouch, such as <a href="http://www.mindtouch.com/blog/2010/09/30/idf-dita-pita/" target="_blank">IDF</a>. Instead of just statically generated reference documentation, documentation in MindTouch can be tagged, linked, mashed-up with all your other documentation, tutorials, etc., including the ability to involve your community in the further growth of your documentation.</p>
<h2>Authoring</h2>
<p>We created <strong>mindtouch.doc</strong> to generate documentation for the DReAM Framework to be hosted on <a href="http://developer.mindtouch.com" target="_blank">developer.mindtouch.com</a>. The first task was to document all public classes in the framework using the .NET Xml comment docs:</p>
<p><a href="http://blog.developer.mindtouch.com/wp-content/uploads/2010/10/itransactionalqueue-doc.gif"><img class="aligncenter size-full wp-image-1553" title="itransactionalqueue-doc" src="http://blog.developer.mindtouch.com/wp-content/uploads/2010/10/itransactionalqueue-doc.gif" alt="" width="560" height="181" /></a></p>
<p>With this in place, Visual Studio generates an XML document containing the meta-data from those comments. Using <strong>mindtouch.doc.exe</strong>, the code Assemblies along with their Documentation XML were converted into MindTouch import archives and imported into the site to produce documentation like this:</p>
<p><a href="http://blog.developer.mindtouch.com/wp-content/uploads/2010/10/itransactionalqueue.gif"><img class="aligncenter size-full wp-image-1554" title="itransactionalqueue" src="http://blog.developer.mindtouch.com/wp-content/uploads/2010/10/itransactionalqueue.gif" alt="" width="560" height="379" /></a></p>
<p>You can take a look at the <a href="http://developer.mindtouch.com/en/ref/dream" target="_blank">MindTouch DReAM reference documentation</a> to explore the entire generated documentation.</p>
<p>In addition to generating individual pages for each class, separate pages with Constructor, Property, Method and Event details are generates as child pages, along with Table of Content pages by Namespace and Assembly.</p>
<p><strong>mindtouch.doc</strong> can generate documentation for an unlimited number of Assemblies at once. When the code to be documented is analyzed any references to any any class or member mentioned in the same or another assembly generates links between the relevant pages.</p>
<p>Since all content is imported as pages into MindTouch, you also get full revision history of past documentation imports (if the content has changed), so that documentation of newly added classes just requires a fresh import on top of the existing documentation.</p>
<h2>Importing .NET documentation</h2>
<p>If you want to try out the process, but don&#8217;t have documented code handy, you can import the DReAM documentation into your own MindTouch instance with the following command:</p>
<pre>mindtouch.doc.exe -h mydomain.com -I ref/Dream mindtouch.dream.dll mindtouch.core.dll</pre>
<p>This will prompt you for your user and password and import the documentation in <em>ref/Dream</em>.</p>
<p>The generated documentation has an external dependency on a template called DocToc. The purpose of this template is to allow simple Table of content customization using DekiScript and to make it easy to include custom css for documentation pages. The generated documentation HTML uses minimal markup along with classes to provide the greatest customization flexibility. The version of DocToc we use on developer.mindtouch.com simply looks like this:</p>
<pre>&lt;table style="background-color:#F9F9F9; border:1px solid #AAAAAA; font-size:95%; padding:5px;"&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;
        &lt;div align="center"&gt;&lt;strong&gt;Table of Contents&lt;/strong&gt;&lt;/div&gt;
        {{ page.toc }}
      &lt;/td&gt;
    &lt;/tr&gt;
 &lt;/tbody&gt;
&lt;/table&gt;</pre>
<h2>Shipping your documentation</h2>
<p><strong>mindtouch.doc</strong> allows to directly import the generated documentation or to save the generated output as a MindTouch archive (.mtarc) which you can provide to customers to import into their own MindTouch installs. This is a regular MindTouch archive, the same that you would use to package up other documentation, scripts and templates authored in MindTouch. It can be imported via the Desktop Adapter or <strong>mindtouch.import.exe</strong>.</p>
<h2>Documentation generation tool details</h2>
<p>Running <strong>mindtouch.doc.exe</strong> without any arguments produces the following help output:</p>
<pre>MindTouch Documentation, Copyright (c) 2010 MindTouch Inc.

USAGE: mindtouch.doc.exe [options] [assembly1 .. assemblyN]

Options:
 (Options must be prefixed by a '-' or '/')

 General:
 ?|usage                  - display this message
 h|host &lt;host&gt;            - MindTouch host (assumes standard API location)
 u|uri &lt;api-uri&gt;          - specify the full uri to the API (instead of using &lt;host&gt;)
 I|importreltopath &lt;path&gt; - relative uri path for import
 importrelto &lt;id&gt;         - relative page id for import (alternative to importreltopath)
 o|output                 - output path for generated documentation (will skip import)
 R|retries                - Maximum number of retries on import/export item failures (default: 3)

 Authentication:
 (if no authentication is provided, the program will prompt for user and password interactively)
 A|authtoken &lt;token&gt;      - authtoken to use for user authentication
 U|user &lt;username&gt;        - username for authentication (requires password option
 P|password &lt;password&gt;    - password for username authentcation</pre>
<p>The official documentation can be found on <a href="http://developer.mindtouch.com/en/docs/MindTouch/Tools/mindtouch.doc" target="_blank">developer.mindtouch.com</a>.</p>
<p><strong>mindtouch.doc</strong> offers an easy way to take your documented .NET APIs and publish them with the power of MindTouch, no separate runtime or site required.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.developer.mindtouch.com/2010/10/05/generate-net-documentation-with-mindtouch-2010/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Read() vs. Peek(): when -1 != -1</title>
		<link>http://blog.developer.mindtouch.com/2010/03/25/read-vs-peek-when-1-1/</link>
		<comments>http://blog.developer.mindtouch.com/2010/03/25/read-vs-peek-when-1-1/#comments</comments>
		<pubDate>Fri, 26 Mar 2010 00:23:36 +0000</pubDate>
		<dc:creator>arnec</dc:creator>
				<category><![CDATA[API Team (Red)]]></category>
		<category><![CDATA[Dev]]></category>
		<category><![CDATA[geek]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[mono]]></category>

		<guid isPermaLink="false">http://blog.developer.mindtouch.com/?p=1330</guid>
		<description><![CDATA[Tweet Photo Credit I just got through an elaborate debug session trying to find a sneaky parsing problem with incoming Http POST requests that happened: intermittently (every 500-2000 iterations), and even in intermittent form was only reproducible (outside of the client production install) on a VM running on Corey&#8217;s laptop Copying the VM to my [...]]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton1330" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Fblog.developer.mindtouch.com%2F2010%2F03%2F25%2Fread-vs-peek-when-1-1%2F&amp;text=Read%28%29%20vs.%20Peek%28%29%3A%20when%20-1%20%21%3D%20-1&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fblog.developer.mindtouch.com%2F2010%2F03%2F25%2Fread-vs-peek-when-1-1%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://blog.developer.mindtouch.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><div style="float: right; padding: 6px; text-align: center; font-size: 11px;"><img src="http://farm3.static.flickr.com/2536/3929208853_f7accb802d_m.jpg" alt="" /><br />
<a href="http://www.flickr.com/photos/rudiriet/3929208853/in/photostream/">Photo Credit</a></div>
<p>I just got through an elaborate debug session trying to find a sneaky parsing problem with incoming Http POST requests that happened:</p>
<ul>
<li>intermittently (every 500-2000 iterations), and</li>
<li>even in intermittent form was only reproducible (outside of the client production install) on a VM running on Corey&#8217;s laptop</li>
</ul>
<p>Copying the VM to my machine or another VMware server would stop it from occuring at all, no matter how many iterations. At least this ruled out a bad binary, runtime version and environmental config issues.</p>
<p>I narrowed the problem down to occasional premature end of stream issues, i.e. less bytes were read than sent. The <strong>fun thing with debugging Streams is that by the time you find out a problem occurred, the debugger is useless</strong>. So I wrote a Stream wrapper that recorded each bytes as it came in.</p>
<p>This identified our <strong>TrimmingTextReader</strong>&#8216;s <strong>Read</strong>() implementation as sometimes returning -1 before the end of the stream. The <strong>TrimmingTextReader</strong> is a wrapper around a normal <strong>TextReader</strong> that trims leading and trailing whitespace as it reads the stream without having to allocate the full string first.</p>
<p>Double-checking MSDN, i confirmed that <a href="http://msdn.microsoft.com/en-us/library/0w3csw16.aspx" target="_blank">TextReader.Read()</a> should return &#8220;<em>[t]he next character from the input stream, or -1 if no more characters are available</em>&#8220;. Our implemenation of Read() had an optimization of using Peak() to check the next byte for whitespace and read it into our whitespace buffer, otherwise return the next character from the whitespace buffer instead:</p>
<pre>// accumulate whitespace characters until we determine
// if we have reached the end of the reader or not
for( ch = _original.Peek();
     (ch &gt;= 0) &amp;&amp; char.IsWhiteSpace((char)ch);
     ch = _original.Peek()
) {
  _buffer.Append((char)_original.Read());
}</pre>
<p>The documentation for <a href="http://msdn.microsoft.com/en-us/library/system.io.textreader.peek.aspx" target="_blank">Peak()</a> states that it returns &#8220;<em>[a]n integer representing the next character to be read, or -1 if no more characters are available</em>&#8220;. Essentially the same wording, except further down there was this caveat: &#8220;<strong>The </strong><span><span class="selflink"><strong>Peek</strong></span></span><strong> method returns an integer value in order to determine whether the end of the file, or another error has occurred.</strong>&#8221; Get that? <em>End of file, or another error!</em> But if i just did Read() it happily returned the next character, so what was this <em>error</em> that Peak() seemed to be detecting?</p>
<p>StreamReader reads bytes from the underlying stream in chunks:</p>
<pre>var read = int Read(int[] buffer, int offset, int count)</pre>
<p>In the failed request scenario, the Stream.Read call had returned less <em>read</em> bytes than requested in <em>count</em>, a perfectly valid response. Only if <em>read</em> is 0 has the end of stream been reached. But <strong>Peek() does not trigger another read from the Stream, while Read() will</strong>. This means that, Peek() will report -1 if the next byte it looks at isn&#8217;t in current buffer, since it&#8217;s at an index larger than <em>read</em>.</p>
<p>So, aside from being ill-documented behavior of two seemingly analogous calls, this behavior is also incredibly useless: You can keep calling Peek() over and over and keep getting -1 and never know whether it&#8217;s really the end of the stream or just some error. Only a Read() will tell the difference between the two scenarios. Of course, this is simply a lesson in &#8220;<em>don&#8217;t assume anything</em>&#8220;, but I hope that my debugging session will help someone avoid the tedious path of discovery to this bit of knowledge.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.developer.mindtouch.com/2010/03/25/read-vs-peek-when-1-1/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>MindTouch @ Monospace: Going Concurrent &amp; Keeping your Sanity</title>
		<link>http://blog.developer.mindtouch.com/2009/11/05/mindtouch-monospace-going-concurrent-keeping-your-sanity/</link>
		<comments>http://blog.developer.mindtouch.com/2009/11/05/mindtouch-monospace-going-concurrent-keeping-your-sanity/#comments</comments>
		<pubDate>Thu, 05 Nov 2009 21:50:36 +0000</pubDate>
		<dc:creator>arnec</dc:creator>
				<category><![CDATA[API Team (Red)]]></category>
		<category><![CDATA[Dev]]></category>
		<category><![CDATA[geek]]></category>
		<category><![CDATA[async]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[coroutine]]></category>
		<category><![CDATA[dream]]></category>
		<category><![CDATA[mono]]></category>
		<category><![CDATA[monospace]]></category>
		<category><![CDATA[result]]></category>
		<category><![CDATA[screencast]]></category>
		<category><![CDATA[threadpool]]></category>

		<guid isPermaLink="false">http://blog.developer.mindtouch.com/?p=1181</guid>
		<description><![CDATA[TweetLast week, on October 28th, Steve and I had the pleasure of presenting at the Monospace conference. I picked a topic close to the API Team&#8217;s heart, Concurrency, and specifically covered our use of the asynchronous method pattern and our coroutine framework. We&#8217;ve covered a lot of the motivation and reasoning for this a number [...]]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton1181" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Fblog.developer.mindtouch.com%2F2009%2F11%2F05%2Fmindtouch-monospace-going-concurrent-keeping-your-sanity%2F&amp;text=MindTouch%20%40%20Monospace%3A%20Going%20Concurrent%20%26%23038%3B%20Keeping%20your%20Sanity&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fblog.developer.mindtouch.com%2F2009%2F11%2F05%2Fmindtouch-monospace-going-concurrent-keeping-your-sanity%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://blog.developer.mindtouch.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>Last week, on October 28th, Steve and I had the pleasure of presenting at the <a title="Monospace" href="http://monospace.us/" target="_blank">Monospace conference</a>. I picked a topic close to the API Team&#8217;s heart, Concurrency, and specifically covered our use of the asynchronous method pattern and our coroutine framework. We&#8217;ve covered a lot of the motivation and reasoning for this a number of times on the <a href="http://blog.developer.mindtouch.com/category/podcast/concurrent-podcast/" target="_blank">Concurrent Podcast</a>. I captured the talk as a screencast  because i think it&#8217;s a useful primer on concurrency in MindTouch 2009 and our Dream framework and its especially useful as a hands-on companion for <a href="http://blog.developer.mindtouch.com/2009/09/23/concurrent-podcast-episode-3-coroutines/" target="_blank">Concurrent Podcast 3: Coroutines</a>:</p>
<p><object width="600" height="440" data="http://www.kaltura.com/index.php/kwidget/cache_st/1257547258/wid/_54512/uiconf_id/48410/entry_id/ewf8uom820" type="application/x-shockwave-flash"><param name="id" value="kaltura_player" /><param name="name" value="kaltura_player" /><param name="allowfullscreen" value="true" /><param name="allowScriptAccess" value="always" /><param name="allowNetworking" value="all" /><param name="allowFullScreen" value="true" /><param name="bgcolor" value="#000000" /><param name="src" value="http://www.kaltura.com/index.php/kwidget/cache_st/1257547258/wid/_54512/uiconf_id/48410/entry_id/ewf8uom820" /></object></p>
<p>I&#8217;d like to thank <a href="http://blog.scottbellware.com/" target="_blank">Scott Bellware</a> for putting on a great conference, as well as everyone from the Mono team and the Mono/.NET community who attended. Monospace was incredibly insightful and I met a lot of very smart people there. Let&#8217;s hope this becomes a regular conference.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.developer.mindtouch.com/2009/11/05/mindtouch-monospace-going-concurrent-keeping-your-sanity/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Concurrent Podcast (Episode 3): Coroutines</title>
		<link>http://blog.developer.mindtouch.com/2009/09/23/concurrent-podcast-episode-3-coroutines/</link>
		<comments>http://blog.developer.mindtouch.com/2009/09/23/concurrent-podcast-episode-3-coroutines/#comments</comments>
		<pubDate>Wed, 23 Sep 2009 19:11:27 +0000</pubDate>
		<dc:creator>arnec</dc:creator>
				<category><![CDATA[Concurrent Podcast]]></category>
		<category><![CDATA[geek]]></category>
		<category><![CDATA[Podcast]]></category>
		<category><![CDATA[asynchronous]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[concurrency]]></category>
		<category><![CDATA[coroutine]]></category>
		<category><![CDATA[dream]]></category>
		<category><![CDATA[mono]]></category>

		<guid isPermaLink="false">http://blog.developer.mindtouch.com/?p=1145</guid>
		<description><![CDATA[Tweet Photo by ToniVC Concurrent Podcast 03: Coroutines One topic we&#8217;ve mentioned in both previous episodes is Coroutines. We use them extensively at MindTouch and the asynchronous programming model provided by the Dream framework fundamentally relies on them. Since we&#8217;re likely to keep bringing them up in the future, we thought it best to cover [...]]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton1145" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Fblog.developer.mindtouch.com%2F2009%2F09%2F23%2Fconcurrent-podcast-episode-3-coroutines%2F&amp;text=Concurrent%20Podcast%20%28Episode%203%29%3A%20Coroutines&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fblog.developer.mindtouch.com%2F2009%2F09%2F23%2Fconcurrent-podcast-episode-3-coroutines%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://blog.developer.mindtouch.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><div style="padding: 4px 8px; font-size: 11px; text-align: center; float: right;"><a href="http://www.flickr.com/photos/tonivc/382150181/"><img class="alignright" title="Gear (II)" src="http://farm1.static.flickr.com/160/382150181_63c36ccd92_m.jpg" alt="" /></a></p>
<p>Photo by <a href="http://www.flickr.com/photos/tonivc/382150181/">ToniVC</a></div>
<p><a href="http://s3.amazonaws.com/mindtouch/podcasts/concurrent03-20090922.mp3">Concurrent Podcast 03: Coroutines</a></p>
<p>One topic we&#8217;ve mentioned in both previous episodes is <a href="http://en.wikipedia.org/wiki/Coroutine" target="_blank">Coroutines</a>. We use them extensively at MindTouch and the asynchronous programming model provided by the Dream framework fundamentally relies on them. Since we&#8217;re likely to keep bringing them up in the future, we thought it best to cover them sooner rather than later.</p>
<p>For some background reading on Coroutines in C# check out my previous article <a href="http://www.mindtouch.com/blog/2008/10/21/dream-asynchronicity-coroutines/" target="_blank">corporate blog</a> or the same content as a tutorial <a href="http://developer.mindtouch.com/Dream/Tutorials/Using_Coroutines_for_Asynchronous_programming" target="_blank">on the developer site</a> (better code formatting).</p>
<p>As usual, you can find <a href="http://developer.mindtouch.com/Community/Concurrent_Podcast" target="_blank">future topic listed here</a>, and we always welcome suggestions on what should take precedence or what other topics to cover. If you want to subscribe to just the podcasts, you can find the feed <a href="http://blog.developer.mindtouch.com/category/podcast/concurrent-podcast/feed">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.developer.mindtouch.com/2009/09/23/concurrent-podcast-episode-3-coroutines/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
<enclosure url="http://s3.amazonaws.com/mindtouch/podcasts/concurrent03-20090922.mp3" length="12298993" type="audio/mpeg" />
		</item>
		<item>
		<title>Producer/Consumer: IBlockingQueue vs. ElasticThreadPool</title>
		<link>http://blog.developer.mindtouch.com/2009/09/10/producerconsumer-iblockingqueue-vs-elasticthreadpool/</link>
		<comments>http://blog.developer.mindtouch.com/2009/09/10/producerconsumer-iblockingqueue-vs-elasticthreadpool/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 22:49:42 +0000</pubDate>
		<dc:creator>arnec</dc:creator>
				<category><![CDATA[Dev]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[dream]]></category>
		<category><![CDATA[mono]]></category>

		<guid isPermaLink="false">http://blog.developer.mindtouch.com/?p=758</guid>
		<description><![CDATA[TweetSince we don&#8217;t have a new Concurrent Podcast ready, I thought I&#8217;d go into a little detail about the change away from IBlockingQueue I mentioned in Episode 1 of the Concurrent Podcast. A little history When I first wrote the dispatcher for the PubSubService in Dream, I was hoping to dispatch the work against the [...]]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton758" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Fblog.developer.mindtouch.com%2F2009%2F09%2F10%2Fproducerconsumer-iblockingqueue-vs-elasticthreadpool%2F&amp;text=Producer%2FConsumer%3A%20IBlockingQueue%20vs.%20ElasticThreadPool&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fblog.developer.mindtouch.com%2F2009%2F09%2F10%2Fproducerconsumer-iblockingqueue-vs-elasticthreadpool%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://blog.developer.mindtouch.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p>Since we don&#8217;t have a new <a href="http://blog.developer.mindtouch.com/category/podcast/concurrent-podcast/" target="_blank">Concurrent Podcast</a> ready, I thought I&#8217;d go into a little detail about the change away from <strong>IBlockingQueue</strong> I mentioned in <a href="http://blog.developer.mindtouch.com/category/podcast/" target="_blank">Episode 1 of the Concurrent Podcast</a>.</p>
<p><strong>A little history</strong></p>
<p>When I first wrote the dispatcher for the PubSubService in <a href="http://developer.mindtouch.com/Dream" target="_blank"><em>Dream</em></a>, I was hoping to dispatch the work against the .NET Threadpool. Early testing showed that the standard threadpool was near useless for dispatching lots of small work items quickly. Load tests seemed to indicate that there was a lock contention scheduling new work that caused short lived workers to execute in a near sequential manner. I.e. a new worker wouldn&#8217;t get dispatched until the the previous one was done.</p>
<p>Another problem with using the standard threadpool for dispatch was that i couldn&#8217;t easily limit how many workers would be used in isolation from other users of the threadpool, which would lead to worker starvation.</p>
<p><strong>Blocking on work</strong></p>
<p>Rather than fight with the threadpool, I opted for a classic producer/consumer pattern in which all work was queued while dedicated worker threads would pull from the queue and dispatch the items. Since .NET queues are not threadsafe, I implemented a blocking queue:</p>
<pre>    public interface IBlockingQueue&lt;T&gt; : IEnumerable&lt;T&gt; {
        bool TryDequeue(TimeSpan timeout, out T item);
        T Dequeue();
        void Enqueue(T data);
        void Close();
        bool IsClosed { get; }
        int Count { get; }
    }</pre>
<p>I also made it <strong>IEnumerable</strong>, so that the worker thread could simply run the dispatch as a foreach loop. This way any thread can enqueue an item to process, while <em>n</em> dedicated worker threads can process the work like this:</p>
<pre>   foreach(var data in queue) {
      // process data
   }</pre>
<p>The thread automatically exits when someone closes the queue, dropping each processor out of its foreach loop.</p>
<p>This pattern works great if you have a steady flow of work and can determine the optimum number of worker threads to spawn. Unfortunately, many scenarios revolve around inconsistent producers of work, leading to alternately workers sitting idle and the queue backing up. Since each thread has a significant memory footprint having them sit idle isn&#8217;t desirable, and neither is work backing up because  you didn&#8217;t spawn  enough workers.</p>
<p>PubSub is one of these scenarios where the flow of events is generally feast or famine. In addition, we use a number of chained pubsub services, leading to a number of dedicated threads sitting around. It was clear that further use of this pattern was only going to exasperate the problem.</p>
<p><strong>Enter the ElasticThreadPool</strong></p>
<p>With the existing .NET Threadpool being a known problem and the Work Stealing Threadpool of <a href="http://blogs.msdn.com/pfxteam/" target="_blank">ParallelFX</a> not arriving until C# 4.0, we had a need for our own threadpool to more efficiently dispatch asynchronous work in Dream. Steve set out to build a lock-free work stealing threadpool and after a couple of iterations arrived at the work dispatch abstraction, <strong>IDispatchQueue</strong>:</p>
<pre>    public interface IDispatchQueue {
        void QueueWorkItem(VoidHandler callback);
        bool TryQueueWorkItem(VoidHandler callback);
    }</pre>
<p>one of the implementors of which is <strong>ElasticThreadPool(int</strong> <em>minReservedThreads</em>, <strong>int</strong> <em>maxParallelThreads</em><strong>)</strong>, which takes the number of reserved (as in dedicated) and maximum threads to use for dispatch. An <strong>ElasticThreadPool(</strong><em>n,n</em><strong>)</strong> would function identically to an <strong>IBlockingQueue&lt;VoidHandler&gt;</strong> with <em>n</em> dedicated threads spawned to pull items from it. Where  the ElasticThreadPool gets interesting is when <em>maxParallelThreads</em> is larger than <em>minReservedThreads</em> &#8212; now the threadpool will get a thread from a global dispatch threadpool to grow or shrink the number of workers according to the amount of work to dispatch.</p>
<p>One of the drawbacks of the built-in threadpool is that it uses a single queue that all workers feed off, creating lock contention for acquiring work. Rather than using raw threads as the workers, the ElasticThreadPool uses a wrapper called <strong>DispatchThread</strong>, each of which has its own work queue, so that work can be queued on a worker directly and pulled without contention. In addition ElasticThreadPool employs a stealing algorithm, so that an idle worker can steal queued work from another worker. This is done via a lock-free double-ended queue, so that stealing does not re-introduce lock contention in work acquisition. The combination of these techniques means that the ElasticThreadPool is very efficient in dispatching work and dynamically scaling the number of workers required.</p>
<p><strong>Dispatching work is an implementation detail<br />
</strong></p>
<p>With <strong>IDispatchQueue</strong> <em><a href="http://developer.mindtouch.com/Dream" target="_blank"><em>Dream</em></a></em> has abstracted the implementation of how processing of work items happens, so rolling your own consumer with something like<strong> IBlockingQueue</strong> is no longer required. Should the scenario of fixed dedicated threads with a single blocking queue still appears to be more advantageous, the dispatch interface makes it easy to create an implementation to use instead of the <strong>ElasticThreadPool</strong>. And while the best fit for most work queuing scenarios is generally the  ElasticThreadPool, <a href="http://developer.mindtouch.com/Dream" target="_blank"><em>Dream</em></a> already includes several alternative <strong>IDispatchQueue</strong> implementations, including one that wraps the standard ThreadPool.</p>
<p>One of the goals of the Dream framework is to make parallel programming simpler. For this purpose it provides such helpers as <strong>Result&lt;T&gt;</strong> for coordination, guidance on asynchronous message dispatch, our coroutine framework, etc. We use the <strong>ElasticThreadPool</strong>, as the underlying threading model for all our own operations and believe that it is an efficient and simple tool for handling producer/consumer scenarios. Let us know if you find it useful or have suggestions for improvement.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.developer.mindtouch.com/2009/09/10/producerconsumer-iblockingqueue-vs-elasticthreadpool/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Schema-less Storage in Dream: IDocStore</title>
		<link>http://blog.developer.mindtouch.com/2009/08/26/schema-less-storage-in-dream-idocstore/</link>
		<comments>http://blog.developer.mindtouch.com/2009/08/26/schema-less-storage-in-dream-idocstore/#comments</comments>
		<pubDate>Thu, 27 Aug 2009 05:45:54 +0000</pubDate>
		<dc:creator>arnec</dc:creator>
				<category><![CDATA[Dev]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[dream]]></category>
		<category><![CDATA[mono]]></category>

		<guid isPermaLink="false">http://blog.developer.mindtouch.com/?p=761</guid>
		<description><![CDATA[TweetNote: The interface IDocStore and its implementations are preview code. We&#8217;ve only started playing around with it for production use and will likely introduce changes before we ship any features of MindTouch depending on it. Your feedback is welcome and appreciated. As part of Dream 1.7 (which is used by MindTouch 9.08), we&#8217;ve introduced a [...]]]></description>
			<content:encoded><![CDATA[<div id="tweetbutton761" class="tw_button" style=""><a href="http://twitter.com/share?url=http%3A%2F%2Fblog.developer.mindtouch.com%2F2009%2F08%2F26%2Fschema-less-storage-in-dream-idocstore%2F&amp;text=Schema-less%20Storage%20in%20Dream%3A%20IDocStore&amp;related=&amp;lang=en&amp;count=horizontal&amp;counturl=http%3A%2F%2Fblog.developer.mindtouch.com%2F2009%2F08%2F26%2Fschema-less-storage-in-dream-idocstore%2F" class="twitter-share-button"  style="width:55px;height:22px;background:transparent url('http://blog.developer.mindtouch.com/wp-content/plugins/wp-tweet-button/tweetn.png') no-repeat  0 0;text-align:left;text-indent:-9999px;display:block;">Tweet</a></div><p><strong>Note:</strong> <em>The interface <strong>IDocStore</strong> and its implementations are preview code. We&#8217;ve only started playing around with it for production use and will likely introduce changes before we ship any features of MindTouch depending on it. Your feedback is welcome and appreciated.<br />
</em></p>
<p>As part of Dream 1.7 (which is used by MindTouch 9.08), we&#8217;ve introduced a schema-less Xml storage inspired by Bret Taylor&#8217;s blog post &#8220;<a href="http://bret.appspot.com/entry/how-friendfeed-uses-mysql">How FriendFeed uses MySQL to store schema-less data</a>&#8221; about a schema-less storage system for JSON blobs used by FriendFeed. Since MindTouch is very much Xml centric and we like the richness of XPath, we decided to adapt the idea to using Xml as the document format.</p>
<h3>Why &#8220;schema-less&#8221; and what does that mean?</h3>
<p>In a traditional database,  data is split into columns stored in rows in a table. For data more complex than a flat set, other tables are used and referenced from the the first table. That means that to add a new field to your record, you have to create a new column, an operation that can have significant performance implications for that table. In addition, if you choose optimize query performance, you may choose to index that new column, again affecting the the table&#8217;s performance during the creation of the index.</p>
<p>But what if you just serialized your record and stuffed that blob into a single column? I mean aside from having just given your DBA an ulcer&#8230; Well, you would have the data stored, but in terms of a relational database, you&#8217;ve just removed all benefits typically bestowed on a row. How are you going to get this data back out? Pulling each record out and examining it for a match is not a realistic option. Ok, you create an id for the object, and now you have a key/value store on top of an RDBMS&#8230; Still lost the ability to query on any other part of your record.</p>
<p>Chances are that you generally only need to retrieve records based on some other field in that record. Adding more columns and populating each for fields to be queried lands you right back where you started. How about creating a self-contained &#8220;index&#8221; by creating key/value pair tables, one for each field, with values in the rows being taken from your record and linking back to the main storage table. Since each of these index tables is a table of its own index, creation and maintenance no longer affects the main table. You can add an index and populate it without affecting the use of any other key or the data itself.</p>
<h3>Introducing IDocStore</h3>
<p>This is the storage model that <strong>IDocStore</strong> follows. The <em>blob</em> record format used is xml, allowing you to store simple fields, or complex hierarchies. Fields to index are defined by using XPath expressions, which makes it possible to not only index single values in the Xml document, but even lists of values. For example, given the following xml:</p>
<pre>&lt;user id="123" &gt;
  &lt;name&gt;bob&lt;/name&gt;
  &lt;groups xmlns:g="http://foo/groups"&gt;
    &lt;group&gt;admin&lt;/group&gt;
    &lt;group&gt;contributor&lt;/group&gt;
  &lt;/groups&gt;
&lt;/user&gt;</pre>
<p>You could define an index xpath of<strong> g:groups/g:group</strong> and be able to find <em>bob</em> with a query for either <em>admin</em> or <em>contributor</em>.</p>
<p>The interface of <strong>IDocStore</strong> is simply this:</p>
<pre>public interface IDocStore {
  bool Put(XDoc doc, bool force);
  void Delete(string docId);
  XDoc Get(string docId);
  IList&lt;XDoc&gt; Get(string keyName, string keyValue);
}</pre>
<p>We don&#8217;t differentiate between <em><strong>Create</strong></em> and <em><strong>Update</strong></em>, simply using <strong>Put</strong>. We also limit <strong>Get</strong> to querying on either the primary document id returning at most a single document, or an xpath index, which may return more than one document.</p>
<p>In it&#8217;s current incarnation, <strong>IDocStore</strong> requires the <strong>docId</strong> already exist in in the document at a configured XPath (default is <strong>@id</strong>). Lacking a natural key, a Guid could always be used to define the id. Another consideration is to add auto-key generation.</p>
<p>Dream contains two implementations of the <strong>IDocStore</strong>, <strong>MysqlDocStore</strong> and <strong>SqliteDocStore</strong>.</p>
<h3>Sqlite Implementation</h3>
<p>Starting with Dream 1.7.0 (used by MindTouch 9.08), every service is provided a DataCatalog backed by its own sqlite database (although this can be swapped out for any other database provider via configuration). This can be used as the backend for the SqliteDocStore.</p>
<p>The sqlite implementation, <strong>SqliteDocStore,</strong> is the simpler of the two. It does not yet include <em>optimistic locking</em>, meaning that <strong>Put</strong> will force an overwrite of existing data, regardless what <em>force </em>is set to. Also, since the DB file that backs the database cannot be shared by multiple processes, an instance of SqliteDocStore assumes that is the authoritative owner of the storage path, and therefore does index definition and maintenance at instantiation time:</p>
<pre>IDocStore store = new SqliteDocStore(_catalog, new XDoc("config")
  .Elem("name", "user")
  .Elem("id-xpath","@id")
  .Start("namespaces")
    .Start("namespace").Attr("prefix","g").Attr("urn","http://foo/groups").End()
  .End()
  .Start("indicies")
    .Start("index").Attr("name","group").Attr("xpath","g:groups/g:group").End()
  .End());</pre>
<p>This initialization will either use or create a new store named <strong>user</strong> in the specified <strong>_catalog</strong>. It will then perform indicies, adding/removing indicies so that only the index is<strong> group</strong>.</p>
<h3>Mysql Implementation</h3>
<p>The mysql implementation, <strong>MysqlDocStore,</strong>, by virtue of using a remote resource, removes index maintenance from the <strong>IDocStore</strong> implementation, and adds <strong>MysqlDocStoreManager</strong> for index manipulation, including updating an index. For this reason, an instance <strong>MysqlDocStore</strong> requires an instance of <strong>MysqlDocStoreManager</strong> (really, an instance of an <strong>IMysqlDocStoreIndexer</strong>) so that it can update the indicies when the documents change.</p>
<p><strong>MysqlDocStore</strong> also implements optimistic locking, meaning that the store will attach a revision attribute to any document returned and use that revision to halt an update should the revision in the database be newer (or <em>force</em> is set to true on <strong>Put</strong>).</p>
<h3>Bringing IDocStore to production use</h3>
<p>Although an RDBMS is used as the underlying storage, the schema-less store is not ACID compliant. A request based on an index may not result in all the documents that match that index, since the index is maintained asynchronously from the update action. However, the result set it pruned to at least never return a false positive. This is analogous to authoring a page in MindTouch and waiting for the search index to pick up the changes. For most document storage usages, this should be fine.</p>
<p>Schema-less storage is useful when the data to be stored may have unknown or frequently changing structure and simple access patterns, i.e. primary or single key queries, are sufficient. Since MindTouch services often deal with this type of storage and complex DekiScript templates could also benefit from arbitrary storage, we&#8217;re experimenting with this as backing store for future extensions, and maybe even expose the storage as a service via an extension so that DekiScript could read from it and JEM could write to it.</p>
<p>If you have use cases for this storage facility, we&#8217;d love to hear from you as we test out and mature the design for production.  The code is available for preview purposes now in the <em>trunk </em>and <em>1.7</em> branches on our source control server, and is part of the binary distribution going out with <em>mindtouch.dream.dll</em> in MindTouch 9.08</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.developer.mindtouch.com/2009/08/26/schema-less-storage-in-dream-idocstore/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

