<?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>Alkampfer&#039;s Place&#187; Programming</title>
	<atom:link href="http://www.codewrecks.com/blog/index.php/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.codewrecks.com/blog</link>
	<description>Wrecks of code floating in the sea of Internet By Ricci Gian Maria</description>
	<lastBuildDate>Wed, 08 Feb 2012 17:49:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2</generator>
		<item>
		<title>Rename a property in RavenDb</title>
		<link>http://www.codewrecks.com/blog/index.php/2012/02/08/rename-a-property-in-ravendb/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2012/02/08/rename-a-property-in-ravendb/#comments</comments>
		<pubDate>Wed, 08 Feb 2012 17:24:00 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[RavenDB]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2012/02/08/rename-a-property-in-ravendb/</guid>
		<description><![CDATA[Previous posts on the NoSql and Raven Series NoSql and a life without Schema NoSql and a life without schema continued Other posts by Mauro on RavenDb Subject. RavenDb: Start your engines RavenDb: First Contact In previous articles I showed how simple is to store objects inside a NO SQL database because the data storage [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2012%2F02%2F08%2Frename-a-property-in-ravendb%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2012%2F02%2F08%2Frename-a-property-in-ravendb%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=RavenDB&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>Previous posts on the <a title="NoSql" href="http://www.codewrecks.com/blog/index.php/tag/nosql/">NoSql</a> and Raven Series</p>
<ul>
<li><a href="http://www.codewrecks.com/blog/index.php/2012/02/04/nosql-and-a-life-without-schema/">NoSql and a life without Schema</a></li>
<li><a href="http://www.codewrecks.com/blog/index.php/2012/02/06/nosql-and-a-life-without-schema-continued/">NoSql and a life without schema continued</a></li>
</ul>
<p>Other posts by Mauro on <a target="_blank" title="RavenDb" href="http://ravendb.net/">RavenDb</a> Subject.</p>
<ul>
<li><a target="_blank" href="http://mauroservienti.blogspot.com/2012/01/ravendb-start-your-engines.html">RavenDb: Start your engines</a></li>
<li><a target="_blank" href="http://mauroservienti.blogspot.com/2012/02/ravendb-first-contact.html">RavenDb: First Contact</a></li>
</ul>
<p>In previous articles I showed how simple is to store objects inside a NO SQL database because the data storage has no schema and does not require you to specify the format of your data. I decided to use <a target="_blank" title="RavenDb" href="http://ravendb.net/">RavenDb</a> as a <a title="NoSql" href="http://www.codewrecks.com/blog/index.php/tag/nosql/">NoSql</a> storage to show some basic concepts of NoSql and I showed also how simple is to add a new property to a document, because <a title="RavenDb" href="http://www.codewrecks.com/blog/index.php/category/programming/ravendb/">RavenDb</a> takes care of everything, just save and load objects and the new property is just there.</p>
<p>Now I want to deal with a different kind of problem:<em> what happen when you rename a property of a document that has already some instances saved in database</em>?</p>
<p>This is the typical situation where having No Schema does not solves the problem automatically. Suppose you change the Player document <em>renaming <strong>Description</strong> property to <strong>Background</strong></em>, if you load an old document from the database, since the Description property does not exists anymore, it is ignored and when the object is saved again, it will be deleted. Clearly this is unacceptable because it lead to data loss, so there is some need of manual intervention to handle the scenario of property rename.</p>
<p>There are multiple approaches to solve this problem, but the simplest one is verifying if your NoSql storage supports the concepts of Bulk-Updating documents, a feature that is really well supported by <a title="RavenDb" href="http://www.codewrecks.com/blog/index.php/category/programming/ravendb/">RavenDb</a>.</p>
<p><a title="RavenDb" href="http://www.codewrecks.com/blog/index.php/tag/ravendb/">RavenDb</a> natively support the concept of&#160; <strong>Document Patch</strong>, a technique used to update a single document without the need to load the entire object or replacing its entire content. Basically a Patch is a dedicated command that transform a stored document directly in the store; along the many different type of patches supported by <a title="RavenDb" href="http://www.codewrecks.com/blog/index.php/tag/ravendb/">RavenDb</a> you can use the <strong><em>rename</em> <em>patch</em></strong> specifically designed to change the name of a property. The code is really simple, just reference the assembly <em>Raven.Abstractions </em>and write this code.</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> store.DatabaseCommands.Patch(<span style="color: rgb(0, 96, 128);">&quot;Players/1&quot;</span>,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span>       <span style="color: rgb(0, 0, 255);">new</span>[] { <span style="color: rgb(0, 0, 255);">new</span> PatchRequest() {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span>         Type = PatchCommandType.Rename,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>         Name = <span style="color: rgb(0, 96, 128);">&quot;Description&quot;</span>,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span>         Value = <span style="color: rgb(0, 0, 255);">new</span> RavenJValue(<span style="color: rgb(0, 96, 128);">&quot;Background&quot;</span>),</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum7">   7:</span> ;</pre>
<p><!--CRLF--></div>
</div>
<p>The syntax is really simple, the IDocumentStore has a property called <strong><em>DatabaseCommands</em></strong> used to access all commands supported by RavenDb engine; with this property you can call the <em><strong>Patch()</strong></em> method specifying the Id of the object you want to patch and an array of PatchRequest (you can ask to apply a sequence of patches, in this example I only want to rename a property). To request a Patch operation you need to specify the type of <em>Patch </em>you want to execute, in this example <strong><em>PatchCommandType.Rename</em></strong>, then you need to fill the appropriate properties of PatchRequest object, required by the type of the path you are issuing. To rename a property you need to specify the <strong>Name</strong> of the property in the <strong>Name</strong> field and the new name of the property in the <strong>Value</strong> parameter, passed as RavenJValue element.</p>
<p>This technique is useful but it does not solve our original problem, because it <strong><em>patches a single <a target="_blank" title="entity" href="http://en.wikipedia.org/wiki/Domain-driven_design#Building_blocks_of_DDD">entity</a></em></strong>, while our need is to rename the property <em>Description of all saved documents of type Player</em>, so we need to use a different type of operation called: <a target="_blank" href="http://ravendb.net/docs/client-api/set-based-operations">set-based-operation</a>. A set-based-operation is an operation that operates on multiple documents at once, in its most basic form it request an <em>index </em>to identify the list of documents to modify and a list of Patch operation<em>. </em>The whole concept of indexes is a really fundamental concept in RavenDb and in all NoSql database, but for this specific need just think to index as a <em>way to create a query that identify documents in database</em>. To define an index by code you must create a specific class for the index, but I can easily create an index with RavenStudio. In <strong>Figure 1</strong> I created an index to identify all the documents of type Players.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2012/02/image4.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2012/02/image_thumb4.png" width="676" height="333" /></a></p>
<p><strong>Figure 1: </strong><em>Define an index in RavenStudio</em></p>
<p>To define a simple index you just need to give it a name and write the text of the index. The index starts with <strong><em>from doc in docs</em></strong>, this part is used to query all documents of the database, then I specify a restriction with the keyword <strong><em>where </em></strong>followed by the condition. </p>
<p>In this example the condition is doc[”@metadata”][“Raven-Entity-Name”] == “Players”, and uses <a target="_blank" href="http://ravendb.net/docs/client-api/advanced/document-metadata">RavenDb Metadata</a>, a series of internal properties that RavenDb attach to each saved document. One of this metadata is called <strong><em>Raven-<a target="_blank" title="Entity" href="http://en.wikipedia.org/wiki/Domain-driven_design#Building_blocks_of_DDD">Entity</a>-Name</em></strong> and for .NET object is the name of the class followed by an s to pluralize the name. In my example, since the .NET class saved is called “Player” the Raven-Entity-Name is equal to “Players”. The last part of the Index Map is the select clause, where I simply select property Name of the document, but it is not important for our example, because I’m only interested in creating an index to <em>identify all documents that corresponds to Player entity.</em></p>
<p>You can execute the index from RavenDb Studio just to verify that it works as intended and it selects all the entities of desired type. When the index is ok you are able to issue a Set-Based-Patch operation:</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> store.DatabaseCommands.UpdateByIndex(<span style="color: rgb(0, 96, 128);">&quot;PlayersToBeUpdated&quot;</span>,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span>       <span style="color: rgb(0, 0, 255);">new</span> IndexQuery(),</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span>       <span style="color: rgb(0, 0, 255);">new</span> [] {<span style="color: rgb(0, 0, 255);">new</span> PatchRequest() {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>           Type = PatchCommandType.Rename,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span>           Name = <span style="color: rgb(0, 96, 128);">&quot;Description&quot;</span>,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span>           Value = <span style="color: rgb(0, 0, 255);">new</span> RavenJValue(<span style="color: rgb(0, 96, 128);">&quot;Background&quot;</span>),</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum7">   7:</span>       }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum8">   8:</span> });</pre>
<p><!--CRLF--></div>
</div>
<p>As you can see the syntax is really similar to the previous example, the main difference is that you should not use the <strong><em>Patch()</em></strong>&#160; database command but <em><strong>UpdateByIndex()</strong></em> that requires: </p>
<ul>
<li>the name of the index to use </li>
<li>an index query (to specify index parameters, if any)</li>
<li>the list of Patches request. </li>
</ul>
<p>The above command basically means:</p>
<p><em>Apply the list of Patches to all object identified by the index named “PlayersToBeUpdated”</em></p>
<p>This simple call updates all Player objects saved in database, renaming the property, as you can verify from RavenDb Studio.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2012/02/image5.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2012/02/image_thumb5.png" width="481" height="246" /></a></p>
<p><strong>Figure 2: </strong><em>Description property was renamed to Background</em></p>
<p>As you can see, even if NoSql databases does not require schema, if you refactor your documents changing the name of existing properties, you need to update all saved data to reflect your changes.</p>
<p>In the next series of posts I’ll deal with RavenDb HTTP API to show how to rename a property directly with HTTP requests, without the need to use C# code. This is useful if you want easily to generate a bat script that updates documents using only HTTP Requests.</p>
<p>Gian Maria</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2012/02/08/rename-a-property-in-ravendb/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Check progress of DBCC CHECKDB</title>
		<link>http://www.codewrecks.com/blog/index.php/2012/02/07/check-progress-of-dbcc-checkdb/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2012/02/07/check-progress-of-dbcc-checkdb/#comments</comments>
		<pubDate>Tue, 07 Feb 2012 10:12:52 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Sql Server]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2012/02/07/check-progress-of-dbcc-checkdb/</guid>
		<description><![CDATA[If you issue a DBCC CHECKDB on a big database to verify for consistency errors, it will take a long time to complete, but the Management Studio windows usually does not give you any hint about how long does it take, or a percentage progress. Luckily enough sql server has a Dynamic Management View that [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2012%2F02%2F07%2Fcheck-progress-of-dbcc-checkdb%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2012%2F02%2F07%2Fcheck-progress-of-dbcc-checkdb%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=Sql+Server&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>If you issue a <a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms176064.aspx">DBCC CHECKDB</a> on a big database to verify for consistency errors, it will take a long time to complete, but the Management Studio windows usually does not give you any hint about how long does it take, or a percentage progress. Luckily enough <a title="sql server" href="http://www.codewrecks.com/blog/index.php/category/programming/sql-server/">sql server</a> has a Dynamic Management View that can solve your problem.</p>
<p>This is the SQL code to visualize progress of the operation</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> <span style="color: rgb(0, 0, 255);">SELECT</span>  session_id ,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span>         request_id ,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span>         percent_complete ,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>         estimated_completion_time ,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span>         DATEADD(ms,estimated_completion_time,GETDATE()) <span style="color: rgb(0, 0, 255);">AS</span> EstimatedEndTime, </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span>         start_time ,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum7">   7:</span>         status ,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum8">   8:</span>         command </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum9">   9:</span> <span style="color: rgb(0, 0, 255);">FROM</span> sys.dm_exec_requests</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum10">  10:</span> <span style="color: rgb(0, 0, 255);">WHERE</span> database_id = 16</pre>
<p><!--CRLF--></div>
</div>
<p>In my example I filtered the results only for the database and used the Id of the database that you can obtain with the <a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms186274.aspx">DB_ID</a> function.</p>
<p>An example of what you got with this query is represented in the following picture.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2012/02/image6.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2012/02/image_thumb6.png" width="881" height="127" /></a></p>
<p>As you can see you can easily visualize percentage of completion, estimated end time and the command that is running.</p>
<p>Gian Maria.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2012/02/07/check-progress-of-dbcc-checkdb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NoSql and a life without Schema continued</title>
		<link>http://www.codewrecks.com/blog/index.php/2012/02/06/nosql-and-a-life-without-schema-continued/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2012/02/06/nosql-and-a-life-without-schema-continued/#comments</comments>
		<pubDate>Mon, 06 Feb 2012 08:06:52 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[RavenDB]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2012/02/06/nosql-and-a-life-without-schema-continued/</guid>
		<description><![CDATA[In the first part I showed how simple is to store object inside a NoSql database like RavenDb, today I want to point out how cool is having no schema when it is time to add properties to your documents. Suppose that your Player entity changed and you add a new property called Description and [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2012%2F02%2F06%2Fnosql-and-a-life-without-schema-continued%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2012%2F02%2F06%2Fnosql-and-a-life-without-schema-continued%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=RavenDB&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In the first part I showed how simple is to store object inside a <a target="_blank" title="NoSql" href="http://www.codewrecks.com/blog/index.php/tag/nosql/">NoSql</a> database like <a title="RavenDb" href="http://ravendb.net/">RavenDb</a>, today I want to point out how cool is having no schema when it is time to add properties to your documents.</p>
<p>Suppose that your Player <a target="_blank" title="entity" href="http://en.wikipedia.org/wiki/Domain-driven_design#Building_blocks_of_DDD">entity</a> changed and you add a new property called Description and you already saved some Players on <a target="_blank" title="RavenDb" href="http://ravendb.net/">RavenDb</a>. The question is: what happens when you try to save a new Player now that the class has another property? What happens when you load an old <a target="_blank" title="entity" href="http://en.wikipedia.org/wiki/Domain-driven_design#Building_blocks_of_DDD">entity</a>, that was saved when that property did not exist?</p>
<p>Since we have no schema we have absolutely no problem, just save another object to Raven and you can find that it got saved without even a warning.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2012/02/image1.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2012/02/image_thumb1.png" width="482" height="170" /></a></p>
<p><strong>Figure 1: </strong><em>New object is saved without any problem even if it is different from the old one</em></p>
<p>If you double click a Document (Raven call each object saved to the db a <em>Document</em>) you can simply look at its content and the new property called Description got saved without any problem.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2012/02/image2.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2012/02/image_thumb2.png" width="408" height="244" /></a></p>
<p><strong>Figure 2: </strong><em>The object contains the new Description property</em></p>
<p>Probably you had already recognized JSON format to store the object, but the question is, what happened to the old object saved before you added the Description property ? The answer is: it is still there, clearly without the Description property.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2012/02/image3.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2012/02/image_thumb3.png" width="419" height="235" /></a></p>
<p><strong>Figure 3: </strong><em>The old object does not contain the Description property, but it can coexist with the new one without any problem </em></p>
<p>Now if you load all object from the database, you can find that for this instance Description property is simply null, because it did not exists in the database, but no error or warning occurred. If you simply load all Player entities and finally issue a SaveChanges, as shown in the following snippet: </p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> <span style="color: rgb(0, 0, 255);">using</span> (var session = store.OpenSession())</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span>     <span style="color: rgb(0, 0, 255);">foreach</span> (var player <span style="color: rgb(0, 0, 255);">in</span> session.Query&lt;Player&gt;().ToList())</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span>         Console.WriteLine(player.Name + <span style="color: rgb(0, 96, 128);">&quot; &quot;</span> + player.RegistrationDate + <span style="color: rgb(0, 96, 128);">&quot; &quot;</span> + player.Description);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span>     }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum7">   7:</span>     session.SaveChanges();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum8">   8:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>you will end with an automatic update of object and now if you look in the database you can see in figure xxx now the player entity has a Description property equal to null</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2012/02/image16.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2012/02/image16_thumb.png" width="399" height="233" /></a></p>
<p><strong>Figure 4: </strong><em>If you ask to SaveChanges, the old player object with id:1 is updated in Raven, now the Description property is present.</em></p>
<p>The same happens for removed properties, they simply are ignored during the load process, and removed during the save.</p>
<p>This really trivial example shows how simple is to deal to add or remove a property from documents when your data storage is a NO SQL one and you have no need to define a schema.</p>
<p>Gian Maria.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2012/02/06/nosql-and-a-life-without-schema-continued/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>NoSql and a life without Schema</title>
		<link>http://www.codewrecks.com/blog/index.php/2012/02/04/nosql-and-a-life-without-schema/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2012/02/04/nosql-and-a-life-without-schema/#comments</comments>
		<pubDate>Sat, 04 Feb 2012 07:24:15 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[RavenDB]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2012/02/04/nosql-and-a-life-without-schema/</guid>
		<description><![CDATA[NoSql is not a replacemente for SQL databases, but it is a valid alternative for a lot of situations where standard SQL is not the best approach to store your data. Since we were taught that whenever you need to store data on a “data store” and you need to query that data for retrieval, [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2012%2F02%2F04%2Fnosql-and-a-life-without-schema%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2012%2F02%2F04%2Fnosql-and-a-life-without-schema%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=RavenDB&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><a title="NoSql" href="http://www.codewrecks.com/blog/index.php/tag/nosql/">NoSql</a> is not a replacemente for SQL databases, but it is a valid alternative for a lot of situations where standard SQL is not the best approach to store your data. Since we were taught that whenever you need to store data on a “data store” and you need to query that data for retrieval, SQL is the best solution, you have only to decide what Sql Engine to use and the game is done.</p>
<p>In 2012 this sentence has proven wrong, what I mean is that is not possible to assume anymore that SQL is the “only way to go” to store data; but you should be aware that other alternative exists and it is called NO SQL. Under this term we have various storage engine that are not based on SQL and in .NET we have an exceptional product called <a target="_blank" title="RavenDB" href="http://ravendb.net/">RavenDB</a> (you can find a really good introduction to <a target="_blank" title="RavenDb" href="http://ravendb.net/">RavenDb</a> in <a target="_blank" href="http://mauroservienti.blogspot.com/2012/01/ravendb-start-your-engines.html">Mauro’s Blog</a>).</p>
<p>The first big difference with standard Sql is being Schemaless. One of the most annoying restriction of <a target="_blank" title="Sql Server" href="http://www.codewrecks.com/blog/index.php/category/programming/sql-server/">Sql Server</a> is the need to specify exactly the format of the data you want to store inside your storage. This is needed for a lot of good reason, but there are situation when you really does not care about it, especially if your software is heavily based on <a title="OOP" href="http://en.wikipedia.org/wiki/Object-oriented_programming">OOP</a> concepts. Suppose you have this object</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> <span style="color: rgb(0, 0, 255);">class</span> Player</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span> {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span>     <span style="color: rgb(0, 0, 255);">public</span> String Name { <span style="color: rgb(0, 0, 255);">get</span>; <span style="color: rgb(0, 0, 255);">set</span>; }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span>     <span style="color: rgb(0, 0, 255);">public</span> DateTime RegistrationDate { <span style="color: rgb(0, 0, 255);">get</span>; <span style="color: rgb(0, 0, 255);">set</span>; }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span>&#160; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum7">   7:</span>     <span style="color: rgb(0, 0, 255);">public</span> Int32 Age { <span style="color: rgb(0, 0, 255);">get</span>; <span style="color: rgb(0, 0, 255);">set</span>; }</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum8">   8:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>For a moment do not care about the fact that this object is not well encapsulated (it has public getter and setter) but focus only on the need to <em>“store” this object somewhere</em>. If you use a standard Sql storage, first of all you need to create a table, then define columns, decide maximum length for the Name column and finally decide an ORM to use or build a dedicate data layer and finally you can save the object.</p>
<p>If you work with raven, this is the only code you need</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> var store = <span style="color: rgb(0, 0, 255);">new</span> DocumentStore { Url = <span style="color: rgb(0, 96, 128);">&quot;http://localhost:8080&quot;</span> }; </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span> store.Initialize();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span> <span style="color: rgb(0, 0, 255);">using</span> (var session = store.OpenSession())</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span> {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum5">   5:</span>     var player = <span style="color: rgb(0, 0, 255);">new</span> Player</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum6">   6:</span>     {</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum7">   7:</span>         Age = 30,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum8">   8:</span>         RegistrationDate = DateTime.Now,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum9">   9:</span>         Name = <span style="color: rgb(0, 96, 128);">&quot;<a title="Alkampfer" href="http://www.codewrecks.com">Alkampfer</a>&quot;</span>,</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum10">  10:</span>     };</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum11">  11:</span>     session.Store(player);</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum12">  12:</span>     session.SaveChanges();</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum13">  13:</span> }</pre>
<p><!--CRLF--></div>
</div>
<p>I simply created a DocumentStore based on a local server, opened a session and saved an object, I did not defined anything on the server, I did not need to have an ORM, the server simply takes the object and save it, period!</p>
<p>I liked very much this approach because </p>
<p><em>I needed to save an object to a data storage and everything I need is just a two function all, Store to tell the storage the object I want to save and SaveChanges that actually do the save.</em></p>
<p>What I got with this simple snippet of code? Just browse with a standard browser to the address of the server and you should see the content of the database.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2012/02/image.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2012/02/image_thumb.png" width="592" height="273" /></a></p>
<p><strong>Figure 1: </strong><em>Content of the database after insertion of a simple object</em></p>
<p>From <strong>Figure 1 </strong>you can see content of the raven database, it contains a player and the little 1 beside the object is the Id that Raven uses internally to uniquely identify that object. The other object called <em>Sys Doc Hilo/players</em> takes care of id generation for Players object with an Hilo algorithm.</p>
<p>That’s all folks, no need to define schema, no need to have special Id property or any other requirement to make the object compatible with the store, just call Store method on whatever .NET object and your object is inside the database, Period!.</p>
<p>This is only a scratch of the many functionalities of <a target="_blank" title="RavenDb" href="http://www.codewrecks.com/blog/index.php/category/programming/ravendb/">RavenDb</a> <img src='http://www.codewrecks.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> , more to come in my blog and in <a href="http://mauroservienti.blogspot.com/">Mauro’s one</a>.</p>
<p>Gian Maria.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2012/02/04/nosql-and-a-life-without-schema/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>When it is time to tweak SQL Server queries</title>
		<link>http://www.codewrecks.com/blog/index.php/2012/01/31/when-it-is-time-to-tweak-sql-server-queries/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2012/01/31/when-it-is-time-to-tweak-sql-server-queries/#comments</comments>
		<pubDate>Tue, 31 Jan 2012 18:35:45 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Sql Server]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2012/01/31/when-it-is-time-to-tweak-sql-server-queries/</guid>
		<description><![CDATA[I’ve a stored procedure with a query that runs on a quite big database, it was slow (more than one minute to run) and was optimized using a temp table. The result is that execution time dropped to ~2 secs, and since this was acceptable the optimization stopped. After a couple of months, the query [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2012%2F01%2F31%2Fwhen-it-is-time-to-tweak-sql-server-queries%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2012%2F01%2F31%2Fwhen-it-is-time-to-tweak-sql-server-queries%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=Sql+Server&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I’ve a stored procedure with a query that runs on a quite big database, it was slow (more than one minute to run) and was optimized using a temp table. The result is that execution time dropped to ~2 secs, and since this was acceptable the optimization stopped.</p>
<p>After a couple of months, the query become really slow again, it got executed in ~30 secs and I started to investigate why.</p>
<p>A quick tour with SSMS and STATISTICS I/O did not reveal some <a target="_blank" title="news" href="http://www.codewrecks.com/blog/index.php/category/news/">news</a>, but the execution plan have a strange sort operation that takes 90% of the time of the overall query and this is really strange, but I did not find any reason why it should slow the query so much. To have a better picture of what is happening I decided to fire the Activity Monitor to check if the query stops for any lock in table, but I found that the task that is executing the query goes into heavy parallelism (you see a lot of row in the activity monitor with the same id), but each subtask is waiting a lot with a&#160; <em><strong>CXPACKET wait type</strong></em> and everything seems stuck. <a href="http://www.mssqltips.com/sqlservertip/2027/a-closer-look-at-cxpacket-wait-type-in-sql-server/">CXPACKET wait time happens when the execution of the query is parallelized</a> so I decided to disable parallelism of the query with <a target="_blank" href="http://msdn.microsoft.com/en-us/library/ms181007.aspx"><strong>OPTION (MAXDOP 1</strong></a><strong>) </strong>to verify if the situation change<strong>. </strong>The result is that the query now executed in ~2 secs, like it did two months before, so I decided to leave it with parallelism disabled and it start to run just fine.</p>
<p>The conclusion is,: when database is big, and query are complex, it is not so simple to understand why a query is slow, sometimes you need to tweak how <a title="SQL Server" href="http://www.codewrecks.com/blog/index.php/category/programming/sql-server/">SQL Server</a> issue the query with Query Hint and you should use all the <a title="tools" href="http://www.codewrecks.com/blog/index.php/tag/tools/">tools</a> you have to understand what is really happening <img src='http://www.codewrecks.com/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> .</p>
<p>Gian Maria.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2012/01/31/when-it-is-time-to-tweak-sql-server-queries/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hardcore fix error in Sql Server database</title>
		<link>http://www.codewrecks.com/blog/index.php/2012/01/31/hardcore-fix-error-in-sql-server-database/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2012/01/31/hardcore-fix-error-in-sql-server-database/#comments</comments>
		<pubDate>Tue, 31 Jan 2012 09:20:00 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Sql Server]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2012/01/31/hardcore-fix-error-in-sql-server-database/</guid>
		<description><![CDATA[In a production Sql Server database we had some issue with the hardware, the result is that one very big database started to gave us errors on DBCC CHECKDB, the error is the following one. Msg 8929, Level 16, State 1, Line 1 Object ID xxxxxx, index ID 1, partition ID xxxxxx, alloc unit ID [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2012%2F01%2F31%2Fhardcore-fix-error-in-sql-server-database%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2012%2F01%2F31%2Fhardcore-fix-error-in-sql-server-database%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=Sql+Server&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>In a production <a title="Sql Server" href="http://www.codewrecks.com/blog/index.php/category/programming/sql-server/">Sql Server</a> database we had some issue with the <a title="hardware" href="http://www.codewrecks.com/blog/index.php/tag/hardware/">hardware</a>, the result is that one very big database started to gave us errors on DBCC CHECKDB, the error is the following one.</p>
<blockquote><p>Msg 8929, Level 16, State 1, Line 1      <br />Object ID xxxxxx, index ID 1, partition ID xxxxxx, alloc unit ID xxxxx (type In-row data): Errors found in off-row data with ID xxxxxxx owned by data record identified by RID = (3:34252:4)</p>
</blockquote>
<p>This happens in one of the most central and important table of the database, that contains millions of record, and only 20 rows gave us error, so I’d like to verify witch rows had the error, because in that table there are lots of records in logical status “deleted” and the corresponding physical row can be removed from the database without much pain and reinserted with the very same data to avoid dataloss.</p>
<p>So my question is… how can I identify the row given the RID (Row Identifier?) First of all from Object ID you can easily find the name of the table with <strong>select Object_Name(&lt;Object_ID&gt;) </strong>but what about the RID. After a little search I find the sys.fn_physLocFormatter function and I created this query</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> <span style="color: rgb(0, 0, 255);">SELECT</span> sys.fn_PhysLocFormatter (%%physloc%%) <span style="color: rgb(0, 0, 255);">AS</span> RID,  </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span>         *</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum3">   3:</span> <span style="color: rgb(0, 0, 255);">into</span> debug.RIDTABLE </pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum4">   4:</span> <span style="color: rgb(0, 0, 255);">FROM</span> TableName</pre>
<p><!--CRLF--></div>
</div>
<p>I selected into a table in Debug schema the RID of the Row as well as all the other columns of the table with error, now I take the output of the DBCC CHECKDB, selected all the RID of rows with errors and I could execute the query</p>
<div style="margin: 20px 0px 10px; padding: 4px; border: 1px solid silver; width: 97.5%; text-align: left; line-height: 12pt; overflow: auto; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; cursor: text; direction: ltr; max-height: 200px; background-color: rgb(244, 244, 244);" id="codeSnippetWrapper">
<div style="padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);" id="codeSnippet">
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: white;"><span style="color: rgb(96, 96, 96);" id="lnum1">   1:</span> <span style="color: rgb(0, 0, 255);">select</span> * <span style="color: rgb(0, 0, 255);">from</span> debug.RIDTABLE</pre>
<p><!--CRLF--></p>
<pre style="margin: 0em; padding: 0px; width: 100%; text-align: left; color: black; line-height: 12pt; overflow: visible; font-family: &quot;Courier New&quot;, courier, monospace; font-size: 8pt; direction: ltr; background-color: rgb(244, 244, 244);"><span style="color: rgb(96, 96, 96);" id="lnum2">   2:</span> <span style="color: rgb(0, 0, 255);">where</span> RID <span style="color: rgb(0, 0, 255);">IN</span> (<span style="color: rgb(0, 96, 128);">'(3:34252:4)'</span>, <span style="color: rgb(0, 96, 128);">'(x:xxxxx:x)'</span>..... </pre>
<p><!--CRLF--></div>
</div>
<p>To extract from the RIDTAble all the row with errors, then I can simply delete them from the original table, and insert them again from the RIDTABLE. Checking the data inside RIDTABLE it seems that the data is correct, except for a NVARCHAR(MAX) field that contains long string of text. </p>
<p>After the process DBCC CHECKDB did not give any other error, so I was quite happy to solve this problem without the need to put the database in single-user-mode, but the most important aspect is that I was able to identify the rows with error, to understand the impact of errors in the software. It turned out that only a couple of lines contained really important data, the others are in Deleted status, so we did not lost anything so important.</p>
<p>Gian Maria.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2012/01/31/hardcore-fix-error-in-sql-server-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Traffic light, say goodbye to public properties</title>
		<link>http://www.codewrecks.com/blog/index.php/2012/01/30/traffic-light-say-goodbye-to-public-properties/</link>
		<comments>http://www.codewrecks.com/blog/index.php/2012/01/30/traffic-light-say-goodbye-to-public-properties/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 10:20:00 +0000</pubDate>
		<dc:creator>alkampfer</dc:creator>
				<category><![CDATA[Domain Driven Design]]></category>
		<category><![CDATA[DDD]]></category>

		<guid isPermaLink="false">http://www.codewrecks.com/blog/index.php/2012/01/30/traffic-light-say-goodbye-to-public-properties/</guid>
		<description><![CDATA[I’ve more to say about the Traffic Light example I explained in a previous post, if you look at that post, you can notice that the Domain is composed by properties that have only getter and not setters. This needs is explained in this post, where I explain why you should protect the status of [...]]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a target="_blank" href="http://api.tweetmeme.com/share?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2012%2F01%2F30%2Ftraffic-light-say-goodbye-to-public-properties%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Fwww.codewrecks.com%2Fblog%2Findex.php%2F2012%2F01%2F30%2Ftraffic-light-say-goodbye-to-public-properties%2F&amp;source=alkampfer&amp;style=normal&amp;hashtags=DDD&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p>I’ve more to say about the Traffic Light example I explained in a <a href="http://www.codewrecks.com/blog/index.php/2011/11/07/traffic-light-experiment/">previous post</a>, if you look at that post, you can notice that the Domain is composed by properties that have only getter and not setters. This needs is explained <a href="http://www.codewrecks.com/blog/index.php/2011/10/20/protect-the-status-of-your-entities/">in this post</a>, where I explain why you should <a title="protect the status of your entities" href="http://www.codewrecks.com/blog/index.php/2011/10/20/protect-the-status-of-your-entities/">protect the status of your entities</a> from external direct manipulation.</p>
<p>If I wish to use my simple Traffic-Light example in a class to teach <a target="_blank" title="OOP" href="http://en.wikipedia.org/wiki/Object-oriented_programming">OOP</a> principle, it is not good enough, because it breaks the concept of <em><a target="_blank" href="http://en.wikipedia.org/wiki/Encapsulation_(object-oriented_programming)">Encapsulation</a></em>. As stated in Wikipedia </p>
<blockquote><p>encapsulation means that the internal representation of an <a target="_blank" href="http://en.wikipedia.org/wiki/Object_(computer_science)">object</a> is generally <strong>hidden from view outside of the object&#8217;s definition</strong>. Typically, only the object&#8217;s own methods can directly <strong>inspect</strong> or manipulate its fields</p>
</blockquote>
<p>Having no setters prevent status from external modification, but the getters permits to the outside world to view inside the object. If you think that getters are not an antipattern and that you can always use them in your domain classes without problem, probably you are still thinking in procedural code, or at least not fully Object Oriented. </p>
<blockquote><p>If you permit to the outside world to read one property of an object, you risk that the outside world will use that value to implement logic that should be contained in the object, so you are breaking encapsulation.</p>
</blockquote>
<p>Whenever you ask yourself “I need to search in code all part of the software that access this property because I’m going to modify it”, you are implicitly looking for <em>logic that is actually using that property but it is outside the object</em>. The result is that you have piece of Business Logic that</p>
<ul>
<li>Read properties of several objects</li>
<li>implement logic based on these properties.</li>
</ul>
<p>Then all these objects are bounded together by that external logic and you loose encapsulation and you are not fully <a target="_blank" title="OOP" href="http://en.wikipedia.org/wiki/Object-oriented_programming">OOP</a>. Based on this considerations I took my traffic light sample and decide to try to remove all getters and thanks to DOMAIN EVENTS it was really simple, more than I tought. Here is my new domain.</p>
<p><a href="http://www.codewrecks.com/blog/wp-content/uploads/2012/01/image13.png"><img style="border: 0px currentcolor; display: inline; background-image: none;" title="image" border="0" alt="image" src="http://www.codewrecks.com/blog/wp-content/uploads/2012/01/image_thumb13.png" width="763" height="222" /></a></p>
<p>Even if this is a trivial domain, I appreciate a lot that I ended having a couple of DOMAIN EVENTS composed only by readonly properties (events are in the past and are immutable) and a couple of Domain Classes that contains only methods. Removing all properties actually does not required me to modify any <a title="test" href="http://www.codewrecks.com/blog/index.php/2007/09/03/test/">test</a>, and actually I found that each time I found some code that use a getter, that code looked really better after the access to the getter is removed. </p>
<p>If you really can structure your core domain with object that contains only methods, you have a really modular domain where each class is really an autonomous piece of software that does some business in the domain without the need of a direct relation with other entities except communicating with Domain Events.</p>
<p>I know that this is a super simple trivial example (a couple of traffic light is not even close to a real business case), but the result of this stupid and little experiment convinced me that it is the right road.</p>
<p>Gian Maria.</p>]]></content:encoded>
			<wfw:commentRss>http://www.codewrecks.com/blog/index.php/2012/01/30/traffic-light-say-goodbye-to-public-properties/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

