Ayende @ Rahienhttp://ayende.com/blog/Ayende @ RahienCopyright (C) Ayende Rahien 2004 - 2015 (c) 201560Inside RavenDB 3.0–Chapter 9 is out<p>Chapter 9 of the <a href="https://github.com/ayende/book">Inside RavenDB 3.0 book</a> is now available. This chapter goes over Full Text Search, and how to make the best use of that in RavenDB. It covers how Full Text Search works, Suggestions, Facets, and more. </p> <p>You can download it using <a href="https://github.com/ayende/book/releases/tag/v0.3.5">the following link</a>. We now have PDF, Kindle and ePub versions available.</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/Inside-RavenDB-3.0Chapter-9-is-out_E29E/SmallCover_2.jpg"><img title="SmallCover" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="SmallCover" src="http://ayende.com/blog/Images/Windows-Live-Writer/Inside-RavenDB-3.0Chapter-9-is-out_E29E/SmallCover_thumb.jpg" width="306" height="434"></a></p>http://ayende.com/blog/170145/inside-ravendb-3-0-chapter-9-is-out?Key=a124e912-407d-4649-b7d9-f6f33d90ce0chttp://ayende.com/blog/170145/inside-ravendb-3-0-chapter-9-is-out?Key=a124e912-407d-4649-b7d9-f6f33d90ce0cTue, 10 Feb 2015 10:00:00 GMTAzure DocumentDB<p>
On Friday, Microsoft came up with Azure DocumentDB. You might say that I have a small interest in such things, so I headed over there to see what I can learn about this project.</p>
<p>
Aside from being somewhat annoyed with the name, this seems to be a very different animal from RavenDB, and something that was built to serve a different niche. One of the things that we put first with RavenDB is ease of use, development and deployment for business applications. The ADB design appears to be built around a different goal, around very big datasets.</p>
<p>
Nitpicker corner: Yes, I know this is a preview, and I know that they are going to be changes. And I repeat, I have <em>no</em> knowledge about this project beyond the documentation and several hours of playing with it.</p>
<p>
That said, I do have a fair bit of experience in this area. So I feel that I can speak with confidence about the topic.</p>
<p>
ADB is supposed to be an highly scalable system that store documents. So far, so good, I can certainly understand that need. But it has made drastically different design choices, some of which I feel very strongly about. I&#39;ll try to explore the issues that I have issues with, and contrast that with what you can do with RavenDB.</p>
<p>
This post has two parts, the first talks about conceptual issues. The second talk about the currently published limits, and their implications for general use for ADB.</p>
<blockquote>
<p>
<strong>TLDR;</strong></p>
<ul>
<li>
No sorting option, or a good paging story</li>
<li>
SQL Injection, without any other alternative</li>
<li>
Hard to deploy and to keep current with your codebase</li>
<li>
Poor development story &amp; no testing story</li>
<li>
Poor client API</li>
<li>
Lots of table scans</li>
<li>
Limited queries and few optimization options</li>
<li>
Single document transactions (from the client)</li>
<li>
No cross collection transactions&nbsp;at all</li>
<li>
Very small document sizes allowed</li>
</ul>
<p>
Also see the &ldquo;What is this for?&rdquo; section below.</p>
<p>
For a document database platform that doesn&rsquo;t have any of those issues, and run in Azure, see <a href="http://azure.microsoft.com/en-us/gallery/store/ravenhq/database/">RavenHQ on Azure</a>.</p>
</blockquote>
<p>
<strong>Transactions &ndash; </strong>ADB say that it has transactions, and for a very limited meaning of the word, I believe it means it. Transactions in ADB means a single document only can be saved with a guarantee it will either be saved or not. That is great, in the sense that at least you won&rsquo;t have data corruption, but that isn&rsquo;t really something that mean much. Even MongoDB can satisfy <em>that</em> bar.</p>
<p>
Oh, sure, you can get actual transactions if you write JS code that run as a &ldquo;stored procedure&rdquo; inside ADB. This means that you can send data to the server and have your JS Stored Procedure make multiple operations in a single transaction. Which is just slightly better (although see my comments on those stored procedures later), but that is still limited to only operations inside the same collections.</p>
<p>
A trivial example for transactions in a document database would be to add a new comment, and update the comment count. You cannot do that in ADB. Not in a single transaction. I don&rsquo;t know about you, but most of the interesting use cases happen when you are working with multiple document types. Sure, you can put all your documents inside the same collection, but have fun trying to work with that in the long term.</p>
<p>
<em>In contrast</em>, RavenDB fully support actual transactions that can span multiple documents (even on different collections, which I would never believe would be an accomplishment). RavenDB can even support DTC and transactions that spans multiple interactions with the server. You know, the kind of transactions you actually want to use. For more, see the documentation on <a href="http://ravendb.net/docs/2.5/client-api/advanced/transaction-support">RavenDB transactions</a>.</p>
<p>
<strong>Management &ndash; </strong>it honestly feels like someone missed all the points that made people want to ditch SQL in the first place. ADB has the concepts of triggers, user defined functions (more on <em>that </em>travesty later, when I discuss queries) and stored procedures. You can define them in JS, and you create something that looks like this:</p>
<blockquote>
<p>
<a href="http://ayende.com/blog/Images/Windows-Live-Writer/Azure-DocumentDB_F173/image_4.png"><img alt="image" border="0" height="253" src="http://ayende.com/blog/Images/Windows-Live-Writer/Azure-DocumentDB_F173/image_thumb_1.png" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" title="image" width="686" /></a></p>
</blockquote>
<p>
Let me count the ways that this is going to cause problems for you.</p>
<ul>
<li>
Business logic in the database, because we haven&rsquo;t learned anything about that in the past.</li>
<li>
Code that you <em>cannot </em>run or test independently. Just debugging something like that is going to be hard.</li>
<li>
No way to actually manage deployment or make sure that this code is in sync with the rest of your codebase.</li>
<li>
Didn&rsquo;t we already learn that triggers are a source for a lot of pain? Are they really necessary for you to do things?</li>
</ul>
<p>
Yes, you have a DB that is schema less, but those kind of things are actually <em>important</em>. They define what you can do with the database, and not having a good way to move those around, and most importantly, not having a way to tie them to the source control system you are using is going to be a giant PITA.</p>
<p>
Sorry, that isn&rsquo;t actually something that you <em>can</em> delay doing for later. You need a good development story, and as I see it, the entire development story all around here is just going to be hard. You would have to manually schlep things around between development and production. And that isn&rsquo;t just about the SP or UDFs. There are a <em>lot</em> of settings that you&rsquo;re going to have to deal with. For example, the configuration per collection, which you&rsquo;ll want to make sure is the same (otherwise you get some very subtle and hard to understand bugs).</p>
<p>
For that matter, there doesn&rsquo;t seem to <em>be</em> a development story. You are probably expected to just run another ADB instance on Azure and use that. This means a <em>lot</em> of latency in development, and that also means that you can&rsquo;t have separate databases per developer, which is a standard practice. This means having to write a lot of code just to manage those things, and you are right back again at the good old days of &ldquo;who didn&rsquo;t update the schema script&rdquo; and failed deployments.</p>
<p>
<em>In contrast,</em> RavenDB make is very easy to handle your indexes &amp; transformers in your code and deploying them as a single step. That also means that they are versioned in the same place as your code, so you don&rsquo;t have to worry about moving between dev &amp; prod. We spent a <em>lot</em> of time thinking and working around this specific area, because this is a common pain point in relational databases, and we weren&rsquo;t willing to accept that being the case in our database. For more information, please see the documentation about <a href="http://ravendb.net/docs/2.5/client-api/querying/static-indexes/defining-static-index">index management in RavenDB</a>.</p>
<p>
<strong>Indexing </strong>&ndash; there are several things here that bother me. By default, everything is indexed, and in the same transaction. This is a <em>great</em> decision, for a <strong>demo</strong> system. But in a real world system, the overhead of indexing everything is prohibitive, especially in a high write system. So ADB is allowing to specify the paths that you will include or exclude from indexing, as well as whatever indexing should be within the same transaction or lazy.</p>
<p>
The problem with that is that those are per collection settings and there doesn&rsquo;t appear to be any way to modify them after the fact. So you start running your system in production, realize that the cost of indexing is high, so you need to change the indexing strategy for a collection. The only way to do that is to create a new collection, with a new indexing strategy, move all the data there, then delete the old one. For even more fun, consider the case where you have a production and development environments. In production, you have a different indexing strategy then in development (where the &lsquo;index everything&rsquo; mode is still on). That means that when you push things to production, your system will fail silently, because you won&rsquo;t be indexing the fields you though were indexed.</p>
<p>
This need re-iteration, the way this currently work, you start running with the default indexing option, which is expensive. As long as you don&rsquo;t have any performance requirements (for example, during development), that is just fine. But when you actually have a lot of data there, or have a lot of writes, that is when you&rsquo;ll figure out that those things need to be changed. At that point, you are are pretty much screwed, because you need to pull all the data out, create a new collection with the new indexing options, and write it all back. That is a horrible experience, especially because you&rsquo;ll likely need to do that under pressure with users breathing down your necks and management complaining about the performance.</p>
<p>
For that matter, indexing in general scares me. Again, I don&rsquo;t actually have any knowledge of the internal operations, but there are a lot of stuff there that just doesn&rsquo;t make sense. It looks like the precision of the indexes used are up to 3 characters (by default) per value. I&rsquo;m guessing that this is done to reduce the amount of space used by the indexing, at least that is what the docs says. The problem is that when you do that, you do a lookup by the first 3 characters, then you have to do a flat search over all the other values. That is <em>going</em> to be causing problems.</p>
<p>
It is also indicated that you cannot do any range searches except on numeric values. Which has interesting implications if you want to do searches on something like a date range, or time spans, an incredibly common operation.</p>
<p>
<em>In contrast, </em>RavenDB indexes are always using the full value, so you are getting an O(logN) search behavior, and not a fallback to O(N) behavior. Range searches are possible on any value, numeric, date time, time span, string, etc. For more information, see the RavenDB documentation about <a href="http://ravendb.net/docs/2.5/client-api/querying/static-indexes/searching">searching with RavenDB</a>.</p>
<p>
<strong>Queries &ndash; </strong>Speaking of problems. Let me talk for a moment on ADB SQL. It looks nice on the surface, it is certainly would be familiar to most people. It is also contain a lot of hidden traps.</p>
<p>
For example, the docs talk about being able to do joins, but you are only actually able to do &ldquo;joins&rdquo; into the sub documents, not into other collections, or even documents in the same collection. A query such as:</p>
<blockquote>
<p>
&nbsp;</p>
<div id="codeSnippetWrapper">
<pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">
<span style="color: #0000ff">SELECT</span> c.Name <span style="color: #0000ff">as</span> CustomerName, o.Total, o.<span style="color: #0000ff">Date</span>
<span style="color: #0000ff">FROM</span> Orders o
<span style="color: #0000ff">JOIN</span> Customers c <span style="color: #0000ff">ON</span> c.Id = o.CustomerId</pre>
</div>
</blockquote>
<p>
Can&rsquo;t be executed on ADB. So the whole notion of &ldquo;joins&rdquo; is actually limited to what you can do in a single document and the sub documents it contains. That make it very limited.</p>
<p>
The options for filtering (where clause) is also interesting. Mostly because of the wide range they allow. It is very easy to create queries that cannot be computed using indexes. That means that your query is now running table scans. Lots &amp; lots of table scans. Sure, you don&rsquo;t have tables, but O(N) is still O(N), and when N is large, as it is apparently the expected case here, you are going to be pretty much dead in the water.</p>
<p>
Another thing that I can&rsquo;t wrap my head around is the queries shown. There is <strong>no way to pass parameters to the query</strong>. None.&nbsp; This appears to be the case because 30+ years of working with SQL has shown that there is absolutely no issue with putting user&rsquo;s input directly into the query. And since complex queries <em>require</em> you to use the raw ADB SQL, you are pretty much have guaranteed that you&rsquo;ll have SQL Injection attacks.</p>
<p>
Sure, you might no get caught by Little Bobby Tables (you can&rsquo;t modify data via SQL), but you are still exposed and can leak important data. This query works just fine, and will return all products:</p>
<blockquote>
<div id="codeSnippetWrapper">
<pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">
<span style="color: #0000ff">SELECT</span> * <span style="color: #0000ff">FROM</span> Products p <span style="color: #0000ff">WHERE</span> p.Name = &quot;testing&quot; <span style="color: #0000ff">OR</span> 1 = 1 -- &quot;</pre>
</div>
</blockquote>
<p>
I&rsquo;ll assume that you understand how I got there. This is a brand new database engine, but ADB is bringing very old issues back into the future. Not only that, we don&rsquo;t have anyway <em>around </em>that. I guess you are going to have to write your on parameter scrubbing code, and make sure to use it everywhere.</p>
<p>
In general, queries are limited. Severely limited, actually. Take a look at the following query:</p>
<blockquote>
<div id="codeSnippetWrapper">
<pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">
<span style="color: #0000ff">SELECT</span> * <span style="color: #0000ff">FROM</span> Products p
<span style="color: #0000ff">WHERE</span> p.Type = &quot;Beer&quot;
<span style="color: #0000ff">AND</span> p.Maker = &quot;Guinness&quot;
<span style="color: #0000ff">AND</span> p.Discontinued = <span style="color: #0000ff">false</span>
<span style="color: #0000ff">AND</span> p.Price &gt; 10 <span style="color: #0000ff">AND</span> p.Price &lt; 100</pre>
</div>
</blockquote>
<p>
You can&rsquo;t run it in ADB. It is too complex to run. Note that this is about as trivial a query as you can get, in pretty much any reasonable business system.</p>
<p>
Continuing on with the problems for business apps theme, there doesn&rsquo;t appear to any good way to do things like paging. When you issue a query, you can specify the number of items to take and you can repeat a query by passing a continuation. But that doesn&rsquo;t really help when you need to actually page with the user. So you show the data to the user, then want to go to the next page&hellip; you have to pass the continuation token all the way around, and hope that it will remain valid for the duration. For that matter, the current client API does paging at the server level, but it will fetch <em>all </em>the results for a query, even if it take it hours to do so.</p>
<p>
There is no way to actually get the total number of items that match the query. So you can&rsquo;t show the user something like: &ldquo;You have 250 new emails&rdquo;, nor can you show them &ldquo;Page 1 &hellip; 50&rdquo;.</p>
<p>
Another troubling omission is the total lack of anything that would allow you to actually query your documents in a particular order. If I want to get the latest orders in descending order (or in fact, in any well defined order), I am out of luck. There is no way of doing that. This is a <strong>huge</strong> deal, because this isn&rsquo;t just something that you can try papering over. This is a core functionality that you need in pretty much any application. And it is just not there. There is some indication that this is being worked on, but I&rsquo;m surprised that this isn&rsquo;t here already. Distributed sorting is a non trivial problem, of course, so I&rsquo;ll reserve further judgment until I see what they have.</p>
<p>
ADB&rsquo;s queries are highly limited, so I expect a workaround for that is going to be to push functionality into the UDF. Note that UDF don&rsquo;t have access to any context, so it can&rsquo;t load additional documents. What it <em>can</em> do it utterly destroy any chance you&rsquo;ll ever have for optimizing a query. The moment that a UDF is involved, you don&rsquo;t really have a choice about how to execute a query, you pretty much have to go to a table scan. Maybe filtering some stuff based on the other filters in the query, but in many cases, that means that you&rsquo;ll have to run your UDF over millions of records. Because UDFs are permitted to perform non pure operations (like the current time), you can&rsquo;t even cache its values, or do anything smart around that. You&rsquo;ll always have to execute the UDF, regardless of the amount of data you have to go through. I don&rsquo;t expect that to perform very well.</p>
<p>
<em>In contrast,</em> RavenDB was explicitly designed to give you both flexibility and performance in queries. There are <strong>no</strong> table scans in RavenDB, and complex queries are expected, encouraged and are handled properly. Queries across multiple documents (and in other collections) are possible, and quite easy to do. Common operations, like paging or sorting are part of the core functionality, and are both very easy to use and come with no additional costs. Complex things like full text search, spatial queries, facets and many more are right there for you to use.&nbsp; For more information, see the RavenDB documentation about <a href="http://ravendb.net/docs/2.5/client-api/querying">querying in RavenDB</a>, <a href="http://ravendb.net/docs/2.5/client-api/querying/static-indexes/spatial-search">spatial searches in RavenDB</a> and how <a href="http://ravendb.net/docs/2.5/appendixes/lucene-indexes-usage">RavenDB actually index the data to allow complex operations</a>.</p>
<p>
<strong>Data types &ndash; </strong>ADB data types are the ones defined in the JSON spec. In particular, it doesn&rsquo;t have native support for date times. The ADB documentation suggest that you&rsquo;ll do custom serialization to handle that. Rendering things like asking: &ldquo;Give me all the orders for this customer for 2014&rdquo; very hard, leaving aside the issues of querying for orders in a particular month, which is not possible either, since you can only do range searches on numeric data. Dates, in particular, are a very complex topic, and not actually handling this in the database is going to open you up for a lot of issues down the road. And dates are <em>kinda</em> important type to have.</p>
<p>
<em>In contrast</em>, RavenDB handles complex (including user defined) types in a well defined manner. And has full support for dates, operations on dates, etc. It seems silly to mention, to be fair, because it seems so basic to have that. For more information, you can read the documentation about <a href="http://ravendb.net/kb/61/working-with-date-and-time-in-ravendb">dates in RavenDB</a>.</p>
<p>
<strong>Aggregation &ndash; </strong>this one is simple, you don&rsquo;t have any. That means that you cannot get the total number of unread emails, or the total sum of orders per customer, or the maximum order per this month . This whole functionality just isn&rsquo;t there.</p>
<p>
<em>In contrast</em>, RavenDB has explicit support for counting the number of results for a query as well as map/reduce indexes. Those give you powerful aggregation framework, which execute the work in the background. When you query, you get the pre-computed results <em>very</em> quickly, without having to do any work at query time. For more information, you can read about Map<a href="http://ravendb.net/docs/2.5/http-api/indexes/map-reduce-indexes">/Reduce in RavenDB</a> and <a href="http://ravendb.net/docs/2.5/client-api/querying/dynamic-aggregation">dynamic aggregation queries</a>.</p>
<p>
<strong>Set operations &ndash; </strong>another easy one, it is just not there. You can do some operations in a stored procedure, but you have <a href="http://azure.microsoft.com/en-us/documentation/articles/documentdb-limits/">5 seconds to run</a>, and that is it. If you need to do something like: Split FullName to FirstName and LastName, get ready to write a lot of code, and wait for a long time for this to complete. For that matter, something as simple as &ldquo;delete all inactive users&rdquo; is very hard to do as well.</p>
<p>
<em>In contrast</em>, RavenDB has explicit support for set based updates and deletes. You can specify a query that match a set of results that would either be deleted or patched using a JS script. For more operations, read the documentations about <a href="http://ravendb.net/docs/2.5/client-api/set-based-operations">Set Based Operations</a>.</p>
<p>
<strong>Client API </strong>&ndash; this is still a preview, so that is somewhat unfair, but the client API is very primitive. Basically, it is a very thin wrapper around the REST API, and it does a poor job at that. The REST API support paging, but the C# client API does not, for example. There is no concept of unit of work, change tracking, client side behavior or anything at all that would actually make this work nicely. There is also an interesting design decision to go async for all operations <em>except</em> queries.</p>
<p>
With queries, you actually issue an async REST call, but you are going to be waiting on that query synchronously. This is probably because of the IQueryable interface and its assumption that the query is sync. But that is a <em>very</em> bad thing to do in terms of mixing sync and async work. It is easy to get into problems such as deadlocks, self lock and just plain weirdness.</p>
<p>
<em>In contrast</em>, RavenDB has a carefully designed client APIs (for .NET, JVM, etc), which fully expose the power of RavenDB. They have been designed to be intuitive, easy to use and guide you into the pit of success, RavenDB also have separate sync and async API, including fully async queries. For more information, read the documentation about <a href="http://ravendb.net/docs/2.5/client-api">the client API</a>.</p>
<p>
<strong>Self links &ndash; </strong>when issuing any operation whatsoever to the database, you have to use something call the object link, or self link. For example, in my test database, the Products collection link is: dbs/frETAA==/colls/frETANSmEAA=/</p>
<p>
You have to use links like that whenever you make any operation what so ever. For fun, those are going to be unique per database, so creating a Products collection in another database would result in a different collection link. That means that I can&rsquo;t just store them in configuration. So you&rsquo;ll probably have to read them from the database every time you need to use them (maybe with some caching?). This is just silly. This is making it very hard to look at what is going on and see what the system is doing (for example, by watching what is going on in Fiddler).</p>
<p>
<em>In contrast</em>, RavenDB applies human readable names whenever possible. For more information, see the documentation about the efforts to make sure that everything in RavenDB in human readable and easily debuggable. One such place is the <a href="http://ravendb.net/docs/2.5/theory/document-key-generation">id generation strategy</a>.</p>
<p>
<strong>Development and testing </strong>&ndash; in this day and age, people are connected to the internet through most of their day to day life. That doesn&rsquo;t mean that they are <em>always</em> connected, or that you can actually rely on the network, or that the latency is acceptable. There is no current development story for ADB. No way to run your own database and develop while you are offline (on the train or at 30,000 feet in the air). That means that every call to ADB has to go over the internet, and that means, in turn, that there is no local development story at all. It means a lot more waiting from the point of view of the developer (also see next point), it means that there is just no testing story.</p>
<p>
If you want to run code to test your ADB usage, you have to setup (and pay) a whole new ADB instance, have to make sure that it is setup exactly the same way as your production instance, and run it against that. It means that test not only have to go outside your process, but across the internet to a remote server. This pretty much kills the notion of fast tests.</p>
<p>
<em>In contrast,</em> RavenDB has an excellent development and testing story. You don&rsquo;t pay for development or CI instances, and you can run tests against RavenDB using an in memory mode embedded inside your process. This has been heavily optimize to allow fast running tests. We are developers, and we care to make other developers&rsquo; life <em>easy</em>. It shows. For more information, see the documentation about <a href="http://ravendb.net/docs/2.5/samples/raven-tests/createraventests">unit testing RavenDB</a>.</p>
<p>
<strong>Joins are for your code &ndash; </strong>because ADB doesn&rsquo;t actually support joins beyond the document scope, or any other option like that, it means that if you want to do something trivial, like show a customer a list of their orders, you are actually going to have to do the join in your own code, not in the database. In fact, let us take a silly scenario, let us say that we want to show a list of new employees as well as their managers, so we can have a chat with them about how they are settling in.</p>
<p>
If we were using SQL, we would be using something like this:</p>
<blockquote>
<div id="codeSnippetWrapper">
<pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">
<span style="color: #0000ff">SELECT</span> emp.Id <span style="color: #0000ff">as</span> EmpId, emp.Name <span style="color: #0000ff">as</span> EmpName, mngr.Id <span style="color: #0000ff">as</span> ManagerId, mngr.Name <span style="color: #0000ff">as</span> ManagerName
<span style="color: #0000ff">FROM</span> Employees emp
<span style="color: #0000ff">JOIN</span> Managers mngr <span style="color: #0000ff">where</span> emp.ManagerId = mngr.Id
<span style="color: #0000ff">WHERE</span> emp.JoinedAt &gt; <span style="color: #006080">&#39;2014-06-01&#39;</span></pre>
</div>
</blockquote>
<p>
That is pretty easy, right? How do you do something like that in ADB? Well, you start with the first query:</p>
<blockquote>
<div id="codeSnippetWrapper">
<pre id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; font-family: 'Courier New', courier, monospace; width: 100%; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; border-left-style: none; line-height: 12pt; padding-right: 0px; background-color: #f4f4f4">
<span style="color: #0000ff">SELECT</span> emp.Id <span style="color: #0000ff">as</span> EmpId, emp.Name <span style="color: #0000ff">as</span> EmpName, emp.ManagerId <span style="color: #0000ff">as</span> ManagerId
<span style="color: #0000ff">FROM</span> Employees emp
<span style="color: #0000ff">WHERE</span> emp.JoinedAt &gt; <span style="color: #006080">&#39;2014-06-01&#39;</span></pre>
</div>
</blockquote>
<p>
And then, for each of the returned managers&rsquo; ids, we have to issue a <em>separate query</em> (ADB doesn&rsquo;t have support for IN). This pattern of usage is called SELECT N+1, and it is a very well known anti pattern, even leaving aside the fact that you have to manually do the join in your own code, with all that this implies. This sort of operations will effectively kill the performance of any application, because you are very chatty with the database.</p>
<p>
<em>In contrast, </em>RavenDB contains several ways to load related items. From including a related document to projecting it via a transformer, you can very easily and efficiently get all the data you need, in a single query to RavenDB. In fact, RavenDB applies a Safe By Default approach and limit the number of times you can call the server (configurable) to prevent just this case. We&rsquo;ll error if you go over the budget of remote calls you are allowed to make. This gives you an early chance to catch performance problems. For more information, see the documentation about <a href="http://ravendb.net/docs/2.5/faq/optimizing-referenced-documents-load">includes</a>, <a href="http://ravendb.net/docs/2.5/client-api/querying/results-transformation/result-transformers">transformers</a> and&nbsp; the <a href="http://ravendb.net/docs/2.5/intro/safe-by-default">Safe By Default</a> approach practiced by RavenDB.</p>
<p>
<strong>Limits - r</strong>eading the <a href="http://azure.microsoft.com/en-us/documentation/articles/documentdb-limits/">limits for ADB</a> makes for some head scratching. Yes, I know that we are talking about the preview mode only. I&rsquo;m aware that you can ask to increase those limits. Nevertheless, those limits likely reflect real trade offs made in the system. So increasing those limits for a particular use case means that you&rsquo;ll have to pay the price for that elsewhere.</p>
<p>
For example, let us take this blog post as an example. It is over 22KB in size. But I can&rsquo;t store this blog post in ADB. That is because documents are limited to 16KB in size. This is utterly ridiculous. I just checked a few of our databases, an common size for documents is 4 &ndash; 8 KB, this is true. But larger documents appear all the time. Even if you exclude blog posts as BLOB of text, we have order documents that have with&nbsp; multiple order lines that are easily past that size. In our users, we see every document size possible, from hundreds of KB to several MB.</p>
<p>
I reached out to Codealike, one of our customers, who were also featured in one of <a href="http://www.microsoft.com/casestudies/Microsoft-Azure/Codealike/ISV-Embeds-Open-Source-Tools-in-Cloud-Application-Delivers-New-Product-in-Six-Weeks/710000000857">Azure&rsquo;s case studies</a>, to hear from them what their situation was. Out of 1.6 million documents in one of their databases, about 90% are in the 500Kb range.</p>
<p>
I&rsquo;m assuming that a large part of this limitation is the fact that by default, everything is indexed. You can&rsquo;t index everything <em>and</em> have large documents <em>and</em> have reasonable performance. So this limit was introduced. I&rsquo;m also assuming that there are other issues here (to be able to fit into pages? low level technical stuff?). Regardless, this is just utterly ridiculous low limit. The problem is that even raising this limit by x5 or x10, that is still not enough. And I&rsquo;m assuming that they didn&rsquo;t chose this limit out of thin air, that there is a technical reason for it.</p>
<p>
Other issues is the number of stored procedure and UDF that you have available. You get 5 of each, and that is it. So you don&rsquo;t get to actually express anything complex there. <a href="http://azure.microsoft.com/en-us/documentation/articles/documentdb-limits/">You also get to use only a single UDF per query, and to use a maximum of 3 AND / OR clauses in a query</a>. I&rsquo;m assuming that the reasoning here is that the more clauses you have, the more complex it is to run the query, especially in a distributed environment. So they put a hard limit on that.</p>
<p>
Those limits together, along with not supporting sorting basically render ADB into an interesting curiosity, but not a real contender for a generally applicable database.</p>
<p>
<strong>What is this for?</strong></p>
<p>
After going over the documentation, there is one thing that I couldn&rsquo;t find. What is the primary use case for ADB?&nbsp;</p>
<p>
It looks more like a solution in search of a problem than the other way around. It appears that this is used by several MS systems to store 100s of TB of data, and process millions of queries. Sheer data size isn&rsquo;t really interesting, we have customers that have multiple TB data. And millions of queries per day isn&rsquo;t really something to brag about (10 million queries per day translate to about 115 queries per second, or about 20 &ndash; 30 queries per second per node).</p>
<p>
What interests me is what sort of data do you put there? The small size limitation make it pretty much unsuitable for storing actual complex documents. You have to always be aware of the size you are storing, and that put a serious crimp in how you can work with this. The limited queries and the inability to sort also lead me to believe that this is a very purpose built tool.</p>
<p>
OneNote&rsquo;s server side is apparently one such use case, but from the look of things, I would expect that this is the other way around. That ADB is actually the backend of OneNote that Microsoft has decided to make public (like Dynamo&rsquo;s in Amazon&rsquo;s case).</p>
<p>
Some of those limitations are probably going to be alleviated by using additional Microsoft tools. So the new Search Server (presumably that one has complex searching &amp; sorting available) would allow you to do some proper queries, and HDInsight might be used for doing aggregation.</p>
<p>
You aren&rsquo;t going to be able to get the &ldquo;show me the count of unread emails for this user&rdquo; from Hadoop, not when the data is constantly changing. And using a secondary search server will introduce high latencies for the indexing. That is leaving aside the additional operational complexity of having to manage multiple systems (and the communication between them) just to get things done.</p>
<p>
Here are a few things that would be hard to build in ADB, as it stands today:</p>
<ul>
<li>
This blog &ndash; the posts are too big, can&rsquo;t sort posts by date, can&rsquo;t do &ldquo;complex&rdquo; queries (tag &amp; date &amp; published &amp; not deleted)</li>
<li>
Logging &ndash; I actually thought that this would be a great use case, but we actually need to show logs by date. As well as be able to search using multiple fields (more than 3) or do contains queries.</li>
<li>
Orders system &ndash;&nbsp; important orders with a lot of line items will be rejected because of the size limitation.</li>
</ul>
<p>
In fact, I don&rsquo;t know what would work there. What kind of data are you putting there? This isn&rsquo;t good for bulk data work, because the ingest rate is really small (~500 writes / second? The debug version RavenDB does 2,500 writes per sec that on my dev laptop without even using the bulk insert API) and there isn&rsquo;t a good way to work with large amount of data at once. It isn&rsquo;t good for business applications, for the reasons outlined above.</p>
<p>
I guess that if you patched this and the search server and Hadoop together you would get something that might be able to serve. But I think that the complexity involved is going to be very high, and I just don&rsquo;t see where this would be a great solution.</p>
<p>
In short, what is the problem that this is trying to solve? What application would be a perfect fit for this?</p>
<p>
With RavenDB, the answer is simple, it is a general purpose database focused on OTLP applications. Until you have an answer, you can use RavenDB on Azure today using <a href="http://azure.microsoft.com/en-us/gallery/store/ravenhq/database/">RavenHQ on Azure</a>.</p>
http://ayende.com/blog/168034/azure-documentdb?Key=44daf9cd-0030-4304-a22a-9ed988d1d23ehttp://ayende.com/blog/168034/azure-documentdb?Key=44daf9cd-0030-4304-a22a-9ed988d1d23eMon, 25 Aug 2014 09:00:00 GMTOn site Architecture & RavenDB consulting availabilities: Malmo & New York City<p>I’m going to have availability for on site consulting in Malmo, Sweden&nbsp; (17 Sep) and in New York City, NY (end of Sep – beginning of Oct).</p> <p>If you want me to come by and discuss what you are doing (architecture, nhibernate or ravendb), please drop me a line. </p> <p>I’m especially interested in people who need to do “strange” things with data and data access. We are building a set of tailored database solutions for customers now, and we have seem customers show x750 improvement in performance when we gave them a database that was designed to fit their exact needs, instead of having to contort their application and their database to a seven dimensional loop just to try to store and read what they needed.</p>http://ayende.com/blog/167745/on-site-architecture-ravendb-consulting-availabilities-malmo-new-york-city?Key=abf51c6a-3a74-498d-960a-1f03fb8a6b46http://ayende.com/blog/167745/on-site-architecture-ravendb-consulting-availabilities-malmo-new-york-city?Key=abf51c6a-3a74-498d-960a-1f03fb8a6b46Mon, 11 Aug 2014 09:00:00 GMTThe fallacy of distributed transactions<p>This can be a very short post, just: See <a href="http://en.wikipedia.org/wiki/CAP_theorem">CAP</a>. Unfortunately, we have a lot of people who actually have experience in using distributed transactions, and have a good reason to believe that they work. The answer is that yes, they do, as long as you don’t run into some of the interesting edge cases.</p> <p>By the way, that is not a new observation, see <a href="http://en.wikipedia.org/wiki/Two_Generals'_Problem">The Two Generals</a>.</p> <p>Allow me to demonstrate. Assume that we have a distributed system with the following actors:</p> <blockquote> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/The-fallacy-of-distributed-transactions_F67A/image_2.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/The-fallacy-of-distributed-transactions_F67A/image_thumb.png" width="855" height="259"></a></p></blockquote> <p>This is a fairly typical setup. You have a worker that pull messages from a queue and read/write to a database based on those messages. To coordinate between them, it uses a transaction coordinator such as MSDTC.</p> <p>Transaction coordinators use a two phase commit (or sometimes a three phase commit protocols) to ensure that either all the data would be committed, or none of it would be.</p> <p>The general logics goes like this:</p> <ul> <li>For each participant in the transaction, send a prepare message. </li> <ul> <li>If the participant answered “prepared”, it is <em>guaranteeing</em> that the transaction can be committed.</li></ul> <li>If any of the participants failed on prepare, abort the whole transaction.</li> <li>If all of the participants successfully prepared, send the commit message to all of them.</li></ul> <p>The actual details are a bit more involved, obviously, but that is pretty much it.</p> <p>Now, let us take a look at an interesting scenario. Worker #1 is pulling (in a distributed transaction) a message from the queue, and based on that message, it modify the database. Then it tells the transaction coordinator that it can commit the transaction. At this point, the TC is sending the prepare message to the database and the queue. Both reply that they have successfully prepared the transaction to be committed. The TC sends a commit message to the queue, completing the transaction from its point of view, however, at this point, it is unable to communicate with the database.</p> <p>What happens now?</p> <p>Before I answer that, let us look at another such scenario. The TC needs to commit a transaction, it sends a prepare message to the database, and receive a successful reply. However, before it manages to send a prepare message to the queue, it becomes unavailable.</p> <p>Note that from the point of view of the database, the situation looks exactly the same. It got (and successfully replied) to a Prepare message, then it didn’t hear back from the transaction coordinator afterward.</p> <p>Now, that is where it gets interesting. In an abstract world, we can just wait with the pending transaction until the connection with the coordinator is resumed, and we can actually get a “commit / abort” notification.</p> <p>But we aren’t in abstract world. When we have such a scenario, we are actually locking records in the database (because they are in the process of being modified). What happens when another client comes to us and want to modify the same record?</p> <p>For example, it is quite common for to host the business logic, queue and transaction coordinator on the same worker instance, while the database is on a separate machine. That means that in the image above, if Worker #1 isn’t available, we recover by directing all the users to the 2nd worker. However, at that point, we have a transaction that was prepared, but not committed. </p> <p>When the user continue to make requests to our system, the 2nd worker, which has its own queue and transaction coordinator is going to try and access the user’s record. The same user whose record are currently locked because of the ongoing transaction.</p> <p>If we just let it hang in this manner, we have essentially created a situation where the user’s data become utterly unavailable (at least for writes). In order to resolve that, transactions comes with a timeout. So after the timeout has expired, we can roll back that transaction. Of course, that leads to a <em>very</em> interesting situation.</p> <p>Let us go back to the first scenario we explored. In this scenario, the queue got both Prepare &amp; Commit messages, while the database got just a Prepare message. The timeout has expired, and the database has rolled back the transaction.&nbsp; In other words, as far as the queue is concerned, the transaction committed, and the message is gone. As far as the database is concerned, that transaction was rolled back, and never happened. </p> <p>Of course, the chance that something like that can happen in one of <em>your</em> systems? Probably <a href="http://blogs.msdn.com/b/larryosterman/archive/2004/03/30/104165.aspx">one in a million</a>.</p>http://ayende.com/blog/167362/the-fallacy-of-distributed-transactions?Key=96667b63-5c34-4cf1-9244-7f8e79b8b657http://ayende.com/blog/167362/the-fallacy-of-distributed-transactions?Key=96667b63-5c34-4cf1-9244-7f8e79b8b657Thu, 17 Jul 2014 09:00:00 GMTEarly lock release, transactions and errors<p>There has been quite a lot of research on the notion of <a href="http://infoscience.epfl.ch/record/152158/files/ELR-single-column.pdf">early lock release</a> as a way to improve database concurrency. For a while, Voron actually supported that, mostly because I thought that this is a good idea. Lately, however, I decided that it is anything but for modern databases.</p> <p>In short, this is how transactions behave:</p> <table cellspacing="0" cellpadding="2" width="1014" border="0"> <tbody> <tr> <td valign="top" width="413"><strong>Standard transaction</strong></td> <td valign="top" width="599"><strong>Early Lock Release Transaction</strong></td></tr> <tr> <td valign="top" width="413"> <ul> <li>Take locks</li> <li>Perform work</li> <li>Commit transaction in memory</li> <li>Flush transaction to log file</li> <li>Release locks</li> <li>Notify user that the transaction successfully completed</li></ul></td> <td valign="top" width="599"> <ul> <li>Take locks <li>Perform work <li>Commit transaction in memory <li>Flush transaction to log file async <li>Release locks <li>Notify user that the transaction successfully completed when the log flush competes</li></ul></td></tr></tbody></table> <p>As you note, the major difference is when we release the locks. In the case of ELR, we release the locks immediately when start flushing the transaction state to log. The idea is that we don’t need to wait for potentially length I/O to allow the next transaction to start running. However, we don’t report the transaction as completed before we flushed the transaction.</p> <p>There is a tiny complication in there, the <em>next</em> transaction cannot start doing the async flushing to log before our transaction is also over, but that is fine and expected, anyway.</p> <p><em>However</em>, what about error conditions? What happens if we’ve started to flush the transaction to disk in an async manner, then we got an error. Well, the obvious thing to do here (and as called out in the paper) is to abort the transaction, and then abort any transaction that has started after the failed transaction released its locks.</p> <p>This is usually okay, because in general, that is no big deal at all. Aside from something like out of disk space errors (which can be resolved by pre-allocating the data), there aren’t really any non catastrophic disk errors. So usually if the disk give you a hard time, it pretty much time to give up and go home.</p> <p>However, with the use of cloud computing, it is actually pretty common (however much it is a bad idea) to have a “networked disk”. This means that it can certainly be the case that a I/O request you made to the disk can get lost, delayed and just appear to fail (but actually go through). It is the last scenario in particular that worries me. If you actually wrote to the log, but you think that you didn’t, what is your state now?</p> <p>And while I can handle that in a case where I can fail the transaction and rollback all the previous state, it is <em>much</em> harder to do that if I’ve already committed the transaction in memory, since we might need to do a memory only rollback, and that isn’t something that we are actually setup to do.</p> <p>In short, we’ll be rolling back early lock release in Voron, it isn’t worth the complexity involved, especially since we already have better ways to handle concurrency.</p>http://ayende.com/blog/165090/early-lock-release-transactions-and-errors?Key=3f8d8f4c-350e-44dd-86f9-1fe506365da4http://ayende.com/blog/165090/early-lock-release-transactions-and-errors?Key=3f8d8f4c-350e-44dd-86f9-1fe506365da4Fri, 10 Jan 2014 10:00:00 GMTReview: Getting started with LevelDB<p><img style="float: right; display: inline" alt="Getting Started with LevelDB" align="right" src="http://dgdsbygo8mp3h.cloudfront.net/sites/default/files/imagecache/productview_larger/1015OS.jpg"></p> <p>I was asked to review <a href="http://www.packtpub.com/getting-started-with-leveldb/book">the book</a> (and received a free electronic copy). </p> <p>As someone that is very into storage engines, I was quite excited about this. After going over the leveldb codebase, I would finally get to read a real book about how it works.</p> <p>I was disappointed, badly.</p> <p>This book isn’t really about leveldb. It contains pretty much no background, explanation, history or anything much at all about how leveldb <em>works</em>. Instead, it is pretty much a guide of how to use LevelDB to write iOS application. There is a lot of chapters dealing with Objective-C, NSString and variants, how to do binding, how to handle <em>drag and drop</em>. </p> <p>However, things that I would expect. Such as explanations of how it works, what does it do, alternative use cases, etc are very rare, if there at all. Only chapter 10 is really worth reading, and even so, I got the feeling that it only made sense to me because I already knew quite a lot leveldb already. I can’t imagine actually starting from scratch and actually being able to understand leveldb from this book. </p> <p>If you are working on iOS apps / OS X, I guess that this might be a good choice, but only if you want to know about actually implementing leveldb. You’ll need to do your actual leveldb learning elsewhere.</p> <p>The book does contain some interesting tidbits. Chapter 10 is talking about tuning and key policies, and it did have some interesting things to talk about, but it also contain wrong information* (and if I could spot it, with my relatively little experience with leveldb, I’m pretty sure that there are other things there too that are wrong).</p> <p>* The book said that is it better to write keys in order, to reduce I/O. But leveldb writes to a skip list in memory, then flush that entire thing in sorted fashion to disk. Your writes have to be bigger than the buffer size of that to actually matter, and that still won’t help you much.</p> <p>In short, feel free to skip this book, unless you are very focused on writing leveldb apps on iOS. In which case it might be a worth it, but I don’t think so. You are better off reading the docs or any of the tutorials. </p>http://ayende.com/blog/164705/review-getting-started-with-leveldb?Key=fdc9b119-e267-473e-9a7f-23f56e45bea6http://ayende.com/blog/164705/review-getting-started-with-leveldb?Key=fdc9b119-e267-473e-9a7f-23f56e45bea6Tue, 03 Dec 2013 10:00:00 GMTre: Why You Should Never Use MongoDB<p>I was pointed at this <a href="http://www.sarahmei.com/blog/2013/11/11/why-you-should-never-use-mongodb/">blog post</a>, and I thought that I would comment, from a RavenDB perspective.</p> <p>TL;DR summary:</p> <blockquote> <p>If you don’t know what how to tie your shoes, don’t run.</p></blockquote> <p>The actual details in the posts are fascinating, I’ve never heard about this Diaspora project. But to be perfectly honest, the problems that they run into has nothing to do with MongoDB or its features. They have a lot to do with a fundamental lack of understanding on how to model using a document database.</p> <p>In particular, I actually winced in sympathetic pain when the author explained how they modeled a TV show.</p> <p>They store the <em>entire </em>thing as a single document:</p> <blockquote> <p><img src="http://www.sarahmei.com/blog/wp-content/uploads/2013/11/Screen-Shot-2013-11-09-at-7.09.27-PM.png" width="640" height="295"></p></blockquote> <p>Hell, the author even talks about General Hospital, a show that has 50+ sessions and 12,000 episodes <em>in the blog post</em>. And at no point did they stop to think that this might not be such a good idea?</p> <p>A much better model would be something like this:</p> <ul> <li>Actors</li> <ul> <li>[episode ids]</li></ul> <li>TV shows</li> <ul> <li>[seasons ids]</li></ul> <li>Seasons</li> <ul> <li>[review ids]</li> <li>[episode ids]</li></ul> <li>Episodes</li> <ul> <li>[actor ids]</li></ul></ul> <p>Now, I am not settled in my own mind if it would be better to have a single season document per season, containing all episodes, reviews, etc. Or if it would be better to have separate episode documents.</p> <p>What I <em>do</em> know is that having a single document that large is something that I want to avoid. And yes, this is a somewhat relational model. That is because what you are looking at is a list of independent aggregates that have different reasons to change.</p> <p>Later on in the post the author talks about the problem when they wanted to show “all episodes by actor”, and they had to move to a relational database to do that. But that is like saying that if you stored all your actors information as plain names (without any ids), you would have hard time to handle them in a relational database. </p> <p>Well, duh!</p> <p>Now, as for the Disapora project issues.&nbsp; They appear to have been doing some really silly things there. In particular, let us store store the all information multiple times:</p> <blockquote> <p><img src="http://www.sarahmei.com/blog/wp-content/uploads/2013/11/Screen-Shot-2013-11-09-at-8.55.46-PM.png" width="240" height="183"></p></blockquote> <p>You can see for example all the user information being duplicated all over the place.&nbsp; Now, this is a distributed social network, but that doesn’t call for it to be <em>stupid</em> about it.&nbsp; They should have used references instead of copying the data.</p> <p>Now, to be fair, there <em>are</em> very good reasons why you’ll want to duplicate the data, when you want a point in time view of it. For example, if I commented on a post, I probably want my name in that post to remain frozen. It would certainly make things much easier all around. But if changing my email now requires that we’ll run some sort of a huge update operation on the entire database… well, it ain’t the db fault. You are doing it wrong.</p> <p>Now, when you store references to other documents, you have a few options. If you are using RavenDB, you have Include support, so you can get the associated documents easily enough. If you are using mongo, you have an additional step in that you have to call $in(the ids), but that is about it. </p> <p>I am sorry, but this is blaming the dancer blaming the floor.</p>http://ayende.com/blog/164483/re-why-you-should-never-use-mongodb?Key=863c3b71-1c86-4a72-8b8d-53605ed90257http://ayende.com/blog/164483/re-why-you-should-never-use-mongodb?Key=863c3b71-1c86-4a72-8b8d-53605ed90257Tue, 12 Nov 2013 10:00:00 GMTre: How memory mapped files, filesystems and cloud storage works<p>Kelly has <a href="http://kellabyte.com/2013/08/19/how-memory-mapped-files-filesystems-and-cloud-storage-works/">an interesting post about memory mapped files and the cloud</a>. This is in response to a comment on <a href="http://ayende.com/blog/163458/hello-voron">my post</a> where I stated that we don’t reserve space up front in Voron because we&nbsp; support cloud providers that charge per storage.</p> <p>From Kelly’s post, I assume she thinks about running it herself on her own cloud instances, and that is what here pricing indicates. Indeed, if you want to get a 100GB cloud disk from pretty much anywhere, you’ll pay for the full 100GB disk from day 1. But that isn’t the scenario that I actually had in mind.</p> <p>I was thinking about the cloud <em>providers</em>. Imagine that you want to go to <a href="http://www.ravenhq.com">RavenHQ</a>, and get a db there. You sigh up for a 2 GB plan, and all if great. Except that on the very first write, we allocate a fixed 10 GB, and you start paying overage charges. This isn’t what you pay when you run on your own hardware. This is what you would have to deal with as a cloud DBaaS provider, and as a consumer of such a service.</p> <p>That aside, let me deal a bit with the issues of memory mapped files &amp; sparse files. I created 6 sparse files, each of them 128GB in size in my E drive. </p> <p>As you can see, this is a 300GB disk, but I just “allocated” 640GB of space in it. </p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/re-How-memory-mapped-files-filesystems-a_AA95/image_2.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/re-How-memory-mapped-files-filesystems-a_AA95/image_thumb.png" width="724" height="445"></a></p> <p>This also shows that there has been no reservation of space on the disk. In fact, it is entirely possible to create files that are entirely too big for the volume they are located on.</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/re-How-memory-mapped-files-filesystems-a_AA95/image_4.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/re-How-memory-mapped-files-filesystems-a_AA95/image_thumb_1.png" width="728" height="348"></a></p> <p>I did a lot of testing with mmap files &amp; sparseness, and I came to the conclusion that you can’t trust it. You especially can’t trust it in a cloud scenario.</p> <p>But why? Well, imagine the scenario where you need to use a new page, and the FS needs to allocate one for you. At this point, it need to find an available page. That might fail, let us imagine that this fails because of no free space, because that is easiest.</p> <p>What happens then? Well, you aren’t access things via an API, so there isn’t an error code it can return, or an exception to be thrown.</p> <p>In Windows, it will use Standard Exception Handler to throw the error. In Linux, that will be probably generate a SIVXXX error. Now, to make things interesting, this may not actually happen when you are writing to the newly reserved page, it may be deferred by the OS to a later point in time (or if you call msync / FlushViewOfFile).&nbsp; At any rate, that means that at some point the OS is going to wake up and realize that it promised something it can’t deliver, and in that point (which, again, may be later than the point you actually wrote to that page) you are going to find yourself in a very interesting situation. I’ve actually tested that scenario, and it isn’t a good one form the point of view of reliability. You really don’t want to get there, because then all bets are off with regards to what happens to the data you wrote. And you can’t even do graceful error handling at that point, because you might be <em>past</em> the point.</p> <p>Considering the fact that disk full is one of those things that you really need to be aware about, you can’t really trust this intersection of features.</p>http://ayende.com/blog/163489/re-how-memory-mapped-files-filesystems-and-cloud-storage-works?Key=37764b36-0e74-41a3-84c2-12b3dcf6ca9ahttp://ayende.com/blog/163489/re-how-memory-mapped-files-filesystems-and-cloud-storage-works?Key=37764b36-0e74-41a3-84c2-12b3dcf6ca9aWed, 21 Aug 2013 07:03:00 GMTB+Trees and why I love them, Part III–in page additions, deletions and updates<p>This is more of less how a page is represented in memory.</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/BTrees-and-why-I-love-them-Part-III--_11F04/image_6.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/BTrees-and-why-I-love-them-Part-III--_11F04/image_thumb_2.png" width="967" height="347"></a></p> <p>There are a few things to note. The entries immediately following the page header points to the actual data at the end of the page. Why is that? Well, remember that we are working with a fixed size page. And we need to add data to the page in a sorted fashion, and do this cheaply. In order to do that, we always add the actual data at the end of the file, <em>before </em>any other data. When the page is empty, we add the value in the end. And the next value is immediately before the previous value, etc.</p> <p>However, as you can see in the image, the fact that we add values in a particular order doesn’t mean that they are <em>sorted</em> in that order. In order to get good sorting, we keep a list of the entries offsets in the beginning of the page, just after the header. This is a sorted list, which gives us the ability to easy add / move an item in the page without actually moving the page, just by changing the offset position it is located on. If we would add another entry to this page, with the key C, the actual data would go on top of entry# 3. But the offset recording this entry would go between B &amp; A at the head of the page. </p> <p>In other words, the actual data of the page grows upward, and the offsets pointing to the locations of entries in the page grows downward. A page is full when you can’t fit the next data item and its offset in the space between the data &amp; the offsets.</p> <p>So that is addition. How about <em>deletion</em>. When we delete, we need to recover the space, but that is actually very easy to handle. Let us say that we want to delete the A entry. That means that we need to do the following. Move all the higher entries data downward to cover the space taken by the entry, and then update the offsets to reflect that. In the end, we are left with:</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/BTrees-and-why-I-love-them-Part-III--_11F04/image_8.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/BTrees-and-why-I-love-them-Part-III--_11F04/image_thumb_3.png" width="959" height="325"></a></p> <p>Updates work as a pair of delete/add operations, with some important edge cases. What happen if we have a page that is nearly full, and we want to update an entry that is bigger than it can currently hold? We have to check if the value is also too big if the previous value is removed. If not, we can fit it into the currently page. Otherwise, we would have to do a page split to accommodate it.</p> <p>Surprisingly enough, the code that is required to handle that is quite pretty, although the actual mechanism probably require a whiteboard to explain. And a lot of hand waving.</p>http://ayende.com/blog/163009/b-trees-and-why-i-love-them-part-iii-in-page-additions-deletions-and-updates?Key=627e3328-dd36-4bcd-8010-92cdf300483dhttp://ayende.com/blog/163009/b-trees-and-why-i-love-them-part-iii-in-page-additions-deletions-and-updates?Key=627e3328-dd36-4bcd-8010-92cdf300483dTue, 20 Aug 2013 09:00:00 GMTB+Trees and why I love them, Part II – splitting hairs (and pages)<p>In the previous post, I gave a brief introduction about B+Trees. Now I want to talk about a crucial part of handling them. Let us start with the basic, we have the following tree, which consist of a single page:</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/58c768bee0f3_1174E/image_4.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/58c768bee0f3_1174E/image_thumb_1.png" width="185" height="480"></a></p> <p>As you can imagine, attempting to add a single item to this page isn’t going to happen (there isn’t enough space), so we are going to have to something about it. That something is call page splitting. The easiest way to do that is to simply cut things in half, like so:</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/58c768bee0f3_1174E/image_6.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/58c768bee0f3_1174E/image_thumb_2.png" width="538" height="480"></a></p> <p>We have split the page, and now we are left with two pages that we can start writing to. Everyone is happy. Almost… in this case, you can see that we are doing sequential inserts. So page #0 is never going to have any additional data written to it. That means that we are wasting half this page! </p> <p> What I have done is to add just a tiny bit of smarts into the mix. Instead of doing a 50/50 split, we can detect if this is a sequential insert that is causing the split. If it is, we are going to split the data in a way that put more of it in the original page, like so:</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/58c768bee0f3_1174E/image_8.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/58c768bee0f3_1174E/image_thumb_3.png" width="627" height="480"></a></p> <p>We leave 85% of the data in the original page because that give some room to play with if one of the entries change there. This gives us a better page utilization in the common case of sequential inserts. But what about non sequential inserts? Well, if we detect that the page is being split because of a non sequential insert, we are going to do a 50/50 split, expecting that there would be additional non sequential inserts along the way.</p> <p>I am not sure how useful that would be, but it seems like something that could be a nice optimization for a common case, and it requires nothing else from the user, we can do it all on our own.</p>http://ayende.com/blog/162946/b-trees-and-why-i-love-them-part-ii-splitting-hairs-and-pages?Key=4eb25f18-5cc3-4402-8d0d-99887a8b5507http://ayende.com/blog/162946/b-trees-and-why-i-love-them-part-ii-splitting-hairs-and-pages?Key=4eb25f18-5cc3-4402-8d0d-99887a8b5507Mon, 19 Aug 2013 09:00:00 GMTB+Trees and why I love them, part I<p>One of the things that I enjoy about learning new things is the way it changes the way I look at the stuff that I already knows. Reading the LMDB codebase, and implementing a persistent B+Tree has given me a new depth of understanding about how relational databases (and Esent, too), work.</p> <p>I am going to go back to level zero and assume that you have never heard about this. You probably know what a binary search tree is. And you know how they work. Here is such a tree:</p> <p><img src="http://encrypt3d.files.wordpress.com/2010/09/nodes-in-binary-search-tree.png"></p> <p>Most tree algorithms spend a lot of time ensuring that the trees are balanced, because their most important property, O(log N) search performance, require that.&nbsp; Binary search trees seems ideally suitable for building databases ,since they allow speedy lookups and backward &amp; forward iteration through the data. Unfortunately, they suffer from a pretty crippling limitation. They have absolutely horrible performance when used in persistent medium. Let us assume that I have a file that contains the following information (Key, Left offset, Right offset). I could search through that file by just walking the links. That, however, would result quite terrible performance. Every time that we move between nodes, we have to do a seek, and that would kill any benefits we will gain from using binary search tree.</p> <p>Instead, we have a data structure that is aimed at reducing those costs. B+Tree. On the face of it, B+Tree is a truly stupidly simple idea. Instead of having just 2 items in the tree, left &amp; right, let us have more.</p> <p>Now, since B+Trees are usually used in persistent format, we start out by defining pages. That is because if we want to do random I/O, we have to be able to write in pages to the file. Therefor, we usually talk about B+Trees in terms of pages. Here is the above tree, rendered as a B+Tree:</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/BTrees-and-why-I-love-them_10CE6/image_2.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/BTrees-and-why-I-love-them_10CE6/image_thumb.png" width="219" height="480"></a></p> <p>Since the data is small, it all fits into a single page. That means that you can read the entire page into memory, and then do a binary search on the data page cheaply. The page above uses a page size of 4,096, so it can contain about 200 entries (of the size we just put in, which are very small). What happens when we have more than that? We <strong>Split</strong> the page. The resulting data now looks like this (256 entries):</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/BTrees-and-why-I-love-them_10CE6/image_4.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/BTrees-and-why-I-love-them_10CE6/image_thumb_1.png" width="573" height="480"></a></p> <p>Now, if we want to do a search for key 152, we start by reading the root node, we can see that the page containing values greater than 102 is page 1, so we will go there. We read that, and do a binary search on the data to find our value. So that was two seeks to get the data. Instead of eight. And usually, the high level pages are already cached, so we would only need a single seek.&nbsp; </p> <p>Let us see what happens when we put even more data in (512):</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/BTrees-and-why-I-love-them_10CE6/image_6.png"><img title="image" style="border-left-width: 0px; border-right-width: 0px; background-image: none; border-bottom-width: 0px; padding-top: 0px; padding-left: 0px; margin: 0px; display: inline; padding-right: 0px; border-top-width: 0px" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/BTrees-and-why-I-love-them_10CE6/image_thumb_2.png" width="640" height="365"></a></p> <p>As you can see, we doubled the amount of information we store, but we remain at the same height. That is quite important, in order to get good performance from&nbsp; the disk. The usual fan out is about a 100, depending on your page size and the keys you selected.</p> <p>I played with the settings a a bit so you can get a good idea about what this look like when you have some more depth, I wrote the following code:</p> <div id="codeSnippetWrapper"> <div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt; 32; i++)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> ms.Position = 0;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j = 0; j &lt; 32; j++)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060"> 5:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum6" style="color: #606060"> 6:</span> env.Root.Add(tx, <span style="color: #0000ff">string</span>.Format(<span style="color: #006080">"{0,5}"</span>, i+j), ms);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum7" style="color: #606060"> 7:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum8" style="color: #606060"> 8:</span> }</pre><!--CRLF--></div></div>
<p>Which resulted in the following tree:</p>
<p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/BTrees-and-why-I-love-them_10CE6/output_2.png"><img title="output" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="output" src="http://ayende.com/blog/Images/Windows-Live-Writer/BTrees-and-why-I-love-them_10CE6/output_thumb.png" width="640" height="454"></a></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>So far, so good, but this is actually the best case scenario. This is what happens when we are inserting data in a sequential manner. Let us mix things up a bit, okay? </p>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum1" style="color: #606060"> 1:</span> <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> i = 0; i &lt; 16; i++)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum2" style="color: #606060"> 2:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum3" style="color: #606060"> 3:</span> ms.Position = 0;</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum4" style="color: #606060"> 4:</span> <span style="color: #0000ff">for</span> (<span style="color: #0000ff">int</span> j = 0; j &lt; 4; j++)</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum5" style="color: #606060"> 5:</span> {</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum6" style="color: #606060"> 6:</span> env.Root.Add(tx, <span style="color: #0000ff">string</span>.Format(<span style="color: #006080">"{1,5}-{0,5}"</span>, i, j), ms);</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white"><span id="lnum7" style="color: #606060"> 7:</span> }</pre><!--CRLF--><pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4"><span id="lnum8" style="color: #606060"> 8:</span> }</pre><!--CRLF--></div></div>
<p>This code will generate values in effectively a random fashion, and you can see the impact it has on the tree:</p>
<p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/BTrees-and-why-I-love-them_10CE6/output_4.png"><img title="output" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="output" src="http://ayende.com/blog/Images/Windows-Live-Writer/BTrees-and-why-I-love-them_10CE6/output_thumb_1.png" width="403" height="480"></a></p>
<p>What happens is that we have had to insert data in a lot of places, which resulted in a lot of page splits, which increase the depth of the tree. Deeper trees means more operations required to actually get to the data on the leaf nodes. And yes, if you have a flash back to “always use sequential ids” section in RDMBS class, this is the reason why. </p>
<p>Now, I think that this is enough for now, I’ll do some more talking about the subject in my next post.</p>http://ayende.com/blog/162945/b-trees-and-why-i-love-them-part-i?Key=3e425555-5570-4ec4-8f1a-659d6dafdebehttp://ayende.com/blog/162945/b-trees-and-why-i-love-them-part-i?Key=3e425555-5570-4ec4-8f1a-659d6dafdebeThu, 15 Aug 2013 09:00:00 GMTVoron’s implementation: Managed memory mapped database–getting to C memory model<p>
In my previous post I started to talk about the architecture of Naver. But this is actually still too early to tell, since before anything else, I want to create the low level interface for working with pages. And even before that, I had to decide how to actually represent a page in C#. In LMDB, this is easy because you can just access the memory using a pointer, and work with memory in this fashion is really natural in C. But in C#, that is much harder. It took me some trial and error, but I realize that I was trying to write C code in C#, and that isn&rsquo;t going to work. Instead, I have to write native C#, which is similar, but different. In C, I can just pass around a pointer, and start doing evil things to it at will. In C#, there are a lot of road blocks along the way that prevent you from doing that.</p>
<p>
So I ended up with this:</p>
<blockquote>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4">
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white">
<span id="lnum1" style="color: #606060"> 1:</span> [StructLayout(LayoutKind.Explicit, Pack = 1)]</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4">
<span id="lnum2" style="color: #606060"> 2:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">struct</span> PageHeader</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white">
<span id="lnum3" style="color: #606060"> 3:</span> {</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4">
<span id="lnum4" style="color: #606060"> 4:</span> [FieldOffset(0)]</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white">
<span id="lnum5" style="color: #606060"> 5:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> PageNumber;</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4">
<span id="lnum6" style="color: #606060"> 6:</span> [FieldOffset(4)]</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white">
<span id="lnum7" style="color: #606060"> 7:</span> <span style="color: #0000ff">public</span> PageFlags Flags;</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4">
<span id="lnum8" style="color: #606060"> 8:</span>&nbsp; </pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white">
<span id="lnum9" style="color: #606060"> 9:</span> [FieldOffset(5)]</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4">
<span id="lnum10" style="color: #606060"> 10:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">ushort</span> Lower;</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white">
<span id="lnum11" style="color: #606060"> 11:</span> [FieldOffset(7)]</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4">
<span id="lnum12" style="color: #606060"> 12:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">ushort</span> Upper;</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white">
<span id="lnum13" style="color: #606060"> 13:</span>&nbsp; </pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4">
<span id="lnum14" style="color: #606060"> 14:</span> [FieldOffset(5)]</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white">
<span id="lnum15" style="color: #606060"> 15:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> NumberOfPages;</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4">
<span id="lnum16" style="color: #606060"> 16:</span> }</pre>
<!--CRLF--></div>
</div>
</blockquote>
<p>
This is the memory layout that I want. But, there is not way in C# to say, allocate this struct at this location. Luckily, I found a workaround.</p>
<blockquote>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4">
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white">
<span id="lnum1" style="color: #606060"> 1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">unsafe</span> <span style="color: #0000ff">class</span> Page</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4">
<span id="lnum2" style="color: #606060"> 2:</span> {</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white">
<span id="lnum3" style="color: #606060"> 3:</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> <span style="color: #0000ff">byte</span>* _base;</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4">
<span id="lnum4" style="color: #606060"> 4:</span> <span style="color: #0000ff">private</span> <span style="color: #0000ff">readonly</span> PageHeader* _header;</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white">
<span id="lnum5" style="color: #606060"> 5:</span>&nbsp; </pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4">
<span id="lnum6" style="color: #606060"> 6:</span> <span style="color: #0000ff">public</span> Page(<span style="color: #0000ff">byte</span>* b)</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white">
<span id="lnum7" style="color: #606060"> 7:</span> {</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4">
<span id="lnum8" style="color: #606060"> 8:</span> _base = b;</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white">
<span id="lnum9" style="color: #606060"> 9:</span> _header = (PageHeader*)b;</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4">
<span id="lnum10" style="color: #606060"> 10:</span> }</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white">
<span id="lnum11" style="color: #606060"> 11:</span>&nbsp; </pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4">
<span id="lnum12" style="color: #606060"> 12:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">int</span> PageNumber { get { <span style="color: #0000ff">return</span> _header-&gt;PageNumber; } set { _header-&gt;PageNumber = <span style="color: #0000ff">value</span>; } }</pre>
<!--CRLF--></div>
</div>
</blockquote>
<p>
What I can do, I can have a Page class that manage this for me. This means that I just give the class a pointer, and it can treat this either as a PageHeader* or as a byte array. This means that I also get to do cool tricks like having an array backed by memory directly in the code:</p>
<blockquote>
<div id="codeSnippetWrapper">
<div id="codeSnippet" style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4">
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white">
<span id="lnum1" style="color: #606060"> 1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">ushort</span>* KeysOffsets</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4">
<span id="lnum2" style="color: #606060"> 2:</span> {</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: white">
<span id="lnum3" style="color: #606060"> 3:</span> get { <span style="color: #0000ff">return</span> (<span style="color: #0000ff">ushort</span>*)(_base + Constants.PageHeaderSize); }</pre>
<!--CRLF-->
<pre style="border-top-style: none; overflow: visible; font-size: 8pt; border-left-style: none; font-family: 'Courier New', courier, monospace; border-bottom-style: none; color: black; padding-bottom: 0px; direction: ltr; text-align: left; padding-top: 0px; border-right-style: none; padding-left: 0px; margin: 0em; line-height: 12pt; padding-right: 0px; width: 100%; background-color: #f4f4f4">
<span id="lnum4" style="color: #606060"> 4:</span> }</pre>
<!--CRLF--></div>
</div>
</blockquote>
<p>
The Page class have a lot of methods relating to managing the page itself, and it should abstract away all the pesky details of actually working with memory directly.</p>
<p>
So, this isn&rsquo;t a really informative post, I fear. But it did take me a few hours to come up with the approach that I wanted. I kept trying to find ways to embed addresses inside the PageHeader, until I realized that I can just hold that externally. Note that all the important state about the Page is actually stored in memory outside the Page class, so that is shared globally, but you can have two Page instances that point to the same place. And that is where I decided to put <em>local</em> state. Things like where we are currently positioned in the page, for example.</p>
http://ayende.com/blog/162916/vorons-implementation-managed-memory-mapped-database-getting-to-c-memory-model?Key=6217c8d7-17c4-4bd7-a0d2-f6ad5f713942http://ayende.com/blog/162916/vorons-implementation-managed-memory-mapped-database-getting-to-c-memory-model?Key=6217c8d7-17c4-4bd7-a0d2-f6ad5f713942Wed, 14 Aug 2013 09:00:00 GMTThe joy of schema less development<p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/The-joy-of-schemaless-development_CD0D/image_2.png"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; float: right; padding-top: 0px; padding-left: 0px; margin: 0px 0px 0px 20px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" align="right" src="http://ayende.com/blog/Images/Windows-Live-Writer/The-joy-of-schemaless-development_CD0D/image_thumb.png" width="200" height="120"></a>As the author of a schema less database, I find myself in the strange position of the barefoot shoemaker. I need to explain a bit. Our current storage engines, Esent and Munin (which was designed mostly to be <em>like</em> Esent) have rigid schemas. There are tables, and indexes, etc. This means that features that touch the storage layer tend to be much more complex. They require migrations, adding new features that require storage means that we have to create new storage tables, or modify indexes, or any number of a bunch of stuff that we <em>developed RavenDB so our users wouldn’t have to</em>.</p> <p>I have been working with our managed implementation of LevelDB quite a lot lately. In order to do more than merely write tests for this, I tried to create a feature complete feature, an aggregation engine. The code is not production worthy (yet!), but what struck me quite hard was the fact that except for the fact that the storage layer is full of bugs (well, that is why I was writing stuff on top of it, to expose it), I had a blast actually working with it.</p> <p>I could make modifications &amp; changes with hardly any friction, and it was a real pleasure to start working with things in this fashion. </p>http://ayende.com/blog/162657/the-joy-of-schema-less-development?Key=c8d39ce8-b9f1-4e36-b50b-7257993a1e14http://ayende.com/blog/162657/the-joy-of-schema-less-development?Key=c8d39ce8-b9f1-4e36-b50b-7257993a1e14Thu, 27 Jun 2013 09:00:00 GMTListOfParams and other horrible things that you shouldn’t bring to RavenDB<p>In the mailing list, we got asked about an issue with code that looked like this:</p> <blockquote> <div id="codeSnippetWrapper"> <div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1"> 1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">abstract</span> <span style="color: #0000ff">class</span> Parameter</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2"> 2:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3"> 3:</span> <span style="color: #0000ff">public</span> String Name { get; set; }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4"> 4:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5"> 5:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6"> 6:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> IntArrayParameter : Parameter</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7"> 7:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8"> 8:</span> <span style="color: #0000ff">public</span> Int32[,] Value { get; set; }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9"> 9:</span> }</pre><!--CRLF--></div></div></blockquote>
<p>I fixed the bug, but that was a strange thing to do, I thought. Happily, the person asking that question was actually taking part of a RavenDB course and I could sit with him and understand the whole question.</p>
<p>It appears that in their system, they have a lot of things like that:</p>
<ul>
<li>IntParameter</li>
<li>StringParameter</li>
<li>BoolParameter</li>
<li>LongParameter</li></ul>
<p>And along with that, they also have a coordinating class:</p>
<blockquote>
<div id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1"> 1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> ListOfParams</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2"> 2:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3"> 3:</span> <span style="color: #0000ff">public</span> List&lt;Param&gt; Values {get;set;}</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4"> 4:</span> }</pre><!--CRLF--></div></div></blockquote>
<p>The question was, could they keep using the same approach using RavenDB? They were quite anxious about this, since they had a need for the capabilities of this in their software.</p>
<p>This is why I <em><a href="http://ayende.com/blog/157537/i-hate-hello-world-questions">hate Hello World questions</a></em>. I could answer just the question that was asked, and that was it. But the problem is quite different.</p>
<p>You might have recognized it by now, what they have here is <a href="http://en.wikipedia.org/wiki/Entity%E2%80%93attribute%E2%80%93value_model">Entity Attribute Value system</a>. A well known anti pattern for the relational database world and one of the few ways to actually get a dynamic schema in that world.</p>
<p>In RavenDB, you don’t <em>need</em> all of those things. You can just get things done. Here is the code that we wrote to replace the above monstrosity:</p>
<blockquote>
<div id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet"><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum1"> 1:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">class</span> Item : DynamicObject</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum2"> 2:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum3"> 3:</span> <span style="color: #0000ff">private</span> Dictionary&lt;<span style="color: #0000ff">string</span>, <span style="color: #0000ff">object</span>&gt; vals = <span style="color: #0000ff">new</span> Dictionary&lt;<span style="color: #0000ff">string</span>, <span style="color: #0000ff">object</span>&gt;();</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum4"> 4:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum5"> 5:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">string</span> StaticlyDefinedProp { get; set; }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum6"> 6:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum7"> 7:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">bool</span> TryGetMember(GetMemberBinder binder, <span style="color: #0000ff">out</span> <span style="color: #0000ff">object</span> result)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum8"> 8:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum9"> 9:</span> <span style="color: #0000ff">return</span> vals.TryGetValue(binder.Name, <span style="color: #0000ff">out</span> result);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum10"> 10:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum11"> 11:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum12"> 12:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">bool</span> TrySetMember(SetMemberBinder binder, <span style="color: #0000ff">object</span> <span style="color: #0000ff">value</span>)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum13"> 13:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum14"> 14:</span> <span style="color: #0000ff">if</span>(binder.Name == <span style="color: #006080">"Id"</span>)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum15"> 15:</span> <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum16"> 16:</span> vals[binder.Name] = <span style="color: #0000ff">value</span>;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum17"> 17:</span> <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum18"> 18:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum19"> 19:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum20"> 20:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">bool</span> TrySetIndex(SetIndexBinder binder, <span style="color: #0000ff">object</span>[] indexes, <span style="color: #0000ff">object</span> <span style="color: #0000ff">value</span>)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum21"> 21:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum22"> 22:</span> var key = (<span style="color: #0000ff">string</span>) indexes[0];</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum23"> 23:</span> <span style="color: #0000ff">if</span>(key == <span style="color: #006080">"Id"</span>)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum24"> 24:</span> <span style="color: #0000ff">return</span> <span style="color: #0000ff">false</span>;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum25"> 25:</span> vals[key] = <span style="color: #0000ff">value</span>;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum26"> 26:</span> <span style="color: #0000ff">return</span> <span style="color: #0000ff">true</span>;</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum27"> 27:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum28"> 28:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum29"> 29:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> <span style="color: #0000ff">bool</span> TryGetIndex(GetIndexBinder binder, <span style="color: #0000ff">object</span>[] indexes, <span style="color: #0000ff">out</span> <span style="color: #0000ff">object</span> result)</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum30"> 30:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum31"> 31:</span> <span style="color: #0000ff">return</span> vals.TryGetValue((<span style="color: #0000ff">string</span>) indexes[0], <span style="color: #0000ff">out</span> result);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum32"> 32:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum33"> 33:</span>&nbsp; </pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum34"> 34:</span> <span style="color: #0000ff">public</span> <span style="color: #0000ff">override</span> IEnumerable&lt;<span style="color: #0000ff">string</span>&gt; GetDynamicMemberNames()</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum35"> 35:</span> {</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum36"> 36:</span> <span style="color: #0000ff">return</span> GetType().GetProperties().Select(x =&gt; x.Name).Concat(vals.Keys);</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum37"> 37:</span> }</pre><!--CRLF--><pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: 'Courier New', courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #606060" id="lnum38"> 38:</span> }</pre><!--CRLF--></div></div></blockquote>
<p>Not only will this class handle the dynamics quite well, it also serializes&nbsp; to idiomatic JSON, which means that querying that is about as easy as you can ask.</p>
<p>The EAV schema was created because RDBMS aren’t suitable for dynamic work, and like many other things from the RDMBS world, this problem just doesn’t exists for us in RavenDB.</p>http://ayende.com/blog/159041/listofparams-and-other-horrible-things-that-you-shouldnt-bring-to-ravendb?Key=a2fb7f40-f2ca-40de-b071-b434fe38ee7fhttp://ayende.com/blog/159041/listofparams-and-other-horrible-things-that-you-shouldnt-bring-to-ravendb?Key=a2fb7f40-f2ca-40de-b071-b434fe38ee7fTue, 09 Oct 2012 10:00:00 GMTNorthwind Starter Kit Review: It is all about the services<p>This is a review of the <a href="http://nsk.codeplex.com/">Northwind Starter Kit project</a>, this review revision <a href="http://nsk.codeplex.com/SourceControl/changeset/changes/94815">94815</a> from Dec 18 2011. <p>Okay, enough about the data access parts. Let us see take a look at a few of the other things that are going on in the application. In particular, this is supposed to be an application with… <blockquote> <p>Domain logic is implemented by means of a Domain Model, onto a layer of services adds application logic. The model is persisted by a DAL designed around the principles of the "Repository" patterns, which has been implemented in a LINQ-friendly way.</p></blockquote> <p>Let us try to figure this out one at a time, okay? <p>The only method in the domain model that have even a hint of domain logic is the CalculateTotalIncome method. Yes, you got it right, that is <em>a </em>method, as in singular. And that method should be replaced with a query, it has no business being on the domain model. <p>So let us move to the services, okay? Here are the service definitions in the entire project: <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/0ae6875d1b24_71A7/image_6.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/0ae6875d1b24_71A7/image_thumb_2.png" width="885" height="690"></a></p> <p>Look at the methods carefully. Do you see any <em>action</em> at all? You don’t, the entire thing is just about <em>queries</em>. </p> <p>And queries should be <em>simple</em>, not abstracted away and made very hard to figure out.</p> <p>The rule of the thumb is that you try hard to not abstract queries, it is operations that you try to abstract. Operations is where you usually actually find the business logic.</p>http://ayende.com/blog/153061/northwind-starter-kit-review-it-is-all-about-the-services?Key=7f5a17e7-68be-468a-8660-a83386aa31a4http://ayende.com/blog/153061/northwind-starter-kit-review-it-is-all-about-the-services?Key=7f5a17e7-68be-468a-8660-a83386aa31a4Mon, 23 Jan 2012 10:00:00 GMTNorthwind Starter Kit Review: From start to finishing–tracing a request<p>This is a review of the <a href="http://nsk.codeplex.com/">Northwind Starter Kit project</a>, this review revision <a href="http://nsk.codeplex.com/SourceControl/changeset/changes/94815">94815</a> from Dec 18 2011.</p> <p>One of the things that I repeatedly call out is the forwarding type of architecture, a simple operation that is hidden away by a large number of abstractions that serves no real purpose. </p> <p>Instead of a controller, let us look at a web service, just to make things slightly different. We have the following:</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/Northwind-Starter-Kit-Review-From-start-_72A0/image_8.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/Northwind-Starter-Kit-Review-From-start-_72A0/image_thumb_3.png" width="650" height="497"></a></p> <p>Okay, let us dig deeper:</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/Northwind-Starter-Kit-Review-From-start-_72A0/image_12.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/Northwind-Starter-Kit-Review-From-start-_72A0/image_thumb_5.png" width="451" height="121"></a></p> <p>I <em>really </em>like the fact that this repository actually <em>have</em> have FindById method, which this service promptly ignores in favor of using the IQueryable&lt;Customer&gt; implementation. If you want to know how that is implemented, just look (using the EF Code First repository implementations, the others are fairly similar):</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/Northwind-Starter-Kit-Review-From-start-_72A0/image_14.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/Northwind-Starter-Kit-Review-From-start-_72A0/image_thumb_6.png" width="501" height="376"></a></p> <p>&nbsp;</p> <p>All in all, the entire thing only serves to make things harder to understand and maintain. </p> <p>Does anyone really think that this abstraction adds anything? What is the <strong><em>point</em></strong>?!</p>http://ayende.com/blog/153060/northwind-starter-kit-review-from-start-to-finishing-tracing-a-request?Key=ed569a76-c10c-41a2-a602-27bb9bb06d04http://ayende.com/blog/153060/northwind-starter-kit-review-from-start-to-finishing-tracing-a-request?Key=ed569a76-c10c-41a2-a602-27bb9bb06d04Fri, 20 Jan 2012 10:00:00 GMTNorthwind Starter Kit Review: Refactoring to an actual read model<p>This is a review of the <a href="http://nsk.codeplex.com">Northwind Starter Kit project</a>, this review revision <a href="http://nsk.codeplex.com/SourceControl/changeset/changes/94815">94815</a> from Dec 18 2011.</p> <p>In my previous post, I talked about how the CatalogController.Product(id) action was completely ridiculous in the level of abstraction that it used to do its work and promised to show how to do the same work on an actual read model in a much simpler fashion. Here is the code.</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/Northwind-Starter-Kit-Review_628/image_2.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/Northwind-Starter-Kit-Review_628/image_thumb.png" width="849" height="538"></a></p> <p><img style="display: inline; float: right" align="right" src="http://psalmsinpurgatory.files.wordpress.com/2011/07/pitchfork-mob1.jpg"></p> <p>There are several things that you might note about this code:</p> <ul> <li>It is all inline, so it is possible to analyze, optimize and figure out what the hell is going on easily.</li> <li>It doesn’t got to the database to load data that it already has.</li> <li>The code actually does something meaningful.</li> <li>It only do one thing, and it does this elegantly.</li></ul> <p>This is actually using a read model. By, you know, <em>reading</em> from it, instead of abstracting it.</p> <p>But there is a problem here, I hear you shout (already reaching for the pitchfork, at least let me finish this post).</p> <p>Previously, we have hidden the logic of discontinued products and available products behind the following abstraction:</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/Northwind-Starter-Kit-Review_628/image_4.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/Northwind-Starter-Kit-Review_628/image_thumb_1.png" width="507" height="96"></a></p> <p>Now we are embedding this inside the query itself, what happens if this logic changes? We would now need to go everywhere we used this logic and update it.</p> <p>Well, yes. Except that there are two mitigating factors.</p> <ul> <li>This nice abstraction is <strong>never used elsewhere</strong>.</li> <li>It is easy to create our own abstraction.</li></ul> <p>Here is an example on how to do this without adding additional layers of abstractions.</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/Northwind-Starter-Kit-Review_628/image_6.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/Northwind-Starter-Kit-Review_628/image_thumb_2.png" width="672" height="197"></a></p> <p>Which means that our action method now looks like this:</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/Northwind-Starter-Kit-Review_628/image_8.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/Northwind-Starter-Kit-Review_628/image_thumb_3.png" width="680" height="541"></a></p> <p>Simple, easy, performant, maintainable.</p> <p>And it doesn’t make my head hurt or cause me to stay up until 4 AM feeling like <a href="http://xkcd.com/386/">an XKCD item</a>.</p>http://ayende.com/blog/153153/northwind-starter-kit-review-refactoring-to-an-actual-read-model?Key=4cc1a59d-3842-4154-a6c7-c4f85525e4b2http://ayende.com/blog/153153/northwind-starter-kit-review-refactoring-to-an-actual-read-model?Key=4cc1a59d-3842-4154-a6c7-c4f85525e4b2Mon, 16 Jan 2012 10:00:00 GMTNorthwind Starter Kit Review: Data Access review thoughts<p>This is a review of the <a href="http://nsk.codeplex.com">Northwind Starter Kit project</a>, this review revision <a href="http://nsk.codeplex.com/SourceControl/changeset/changes/94815">94815</a> from Dec 18 2011.</p> <p>In my last few posts, I have gone over the data access strategy used in NSK. I haven’t been impressed. In fact, I am somewhere between horrified, shocked and amused in the same way you feel when you see a clown slipping on a banana peel.&nbsp; Why do I say that? Let us trace a single call from the front end all the way to the back.</p> <p>The case in point, CatalogController.Product(id) action. This is something that should just display the product on the screen, so it should be fairly simple, right? Here is how it works when drawn as UML:</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/6a03ddd05a08_46EE/image_11.png"><img style="background-image: none; border-bottom: 0px; border-left: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top: 0px; border-right: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/6a03ddd05a08_46EE/image_thumb.png" width="1025" height="826"></a></p> <p>To simplify things, I decided to skip any method calls on the same objects (there are more than a few).</p> <p>Let me show you how this looks like in actual code:</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/6a03ddd05a08_46EE/image_6.png"><img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/6a03ddd05a08_46EE/image_thumb_2.png" width="647" height="124"></a></p> <p>Digging deeper, we get:</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/6a03ddd05a08_46EE/image_8.png"><img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/6a03ddd05a08_46EE/image_thumb_3.png" width="826" height="488"></a></p> <p>We will deal with the first method call to CatalogServices now, which looks like:</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/6a03ddd05a08_46EE/image_10.png"><img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/6a03ddd05a08_46EE/image_thumb_4.png" width="670" height="68"></a></p> <p>I’ll skip going deeper, because this is just a layer of needless abstraction on top of Entity Framework and that is quite enough already.</p> <p>Now let us deal with the second call to CatalogServices, which is actually more interesting:</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/6a03ddd05a08_46EE/image_14.png"><img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/6a03ddd05a08_46EE/image_thumb_6.png" width="831" height="205"></a></p> <p>Note the marked line? This is generating a query. This is interesting, because we have <em>already loaded the product. </em>There is no way of optimizing that, of course, because the architecture doesn’t let you.</p> <p>Now, you need all of this <em>just to show a single product on the screen</em>. I mean, <strong>seriously.</strong></p> <p>You might have noticed some references to things like Read Model in the code. Which I find highly ironic. Read Models are about making the read side of things <em>simple</em>, not drowning the code in abstraction on top of abstraction on top of abstraction.</p> <p>In my next post, I’ll show a better way to handle this scenario. A way that is actually simpler and make use an of actual read model and not infinite levels of indirection.</p>http://ayende.com/blog/153058/northwind-starter-kit-review-data-access-review-thoughts?Key=852c1fa5-de55-4460-b9a8-b4fbc2e2843ahttp://ayende.com/blog/153058/northwind-starter-kit-review-data-access-review-thoughts?Key=852c1fa5-de55-4460-b9a8-b4fbc2e2843aFri, 13 Jan 2012 10:00:00 GMTNorthwind Starter Kit Review: Data Access and the essence of needless work, Part I<p>This is a review of the <a href="http://nsk.codeplex.com">Northwind Starter Kit project</a>, this review revision <a href="http://nsk.codeplex.com/SourceControl/changeset/changes/94815">94815</a> from Dec 18 2011.</p> <blockquote> <p><strong>Update: </strong>Andrea, the project’s leader has posted a <a href="http://blogs.ugidotnet.org/mrbrightside/archive/2012/01/10/no-nsk-is-not-about-northwind.aspx">reply to this series of posts</a>.</p></blockquote> <p>I like to start reviewing applications from their database interactions. That it usually low level enough to tell me what is actually going on, and it is critical to the app, so a lot of thought usually goes there.</p> <p>In good applications, I have hard time finding the data access code, because it isn’t there. It is in the OR/M or the server client API (in the case of RavenDB). In some applications, if they work against legacy databases or without the benefit of OR/M or against a strange data source (such as a remote web service target) may need an explicit data layer, but most don’t.</p> <p>NSK actually have 5 projects dedicated solely to data access. I find this.. scary.</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/Northwind-Starter-Kit-Review_3B93/image_8.png"><img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/Northwind-Starter-Kit-Review_3B93/image_thumb_3.png" width="351" height="144"></a></p> <p>Okay, let me start outlying things in simple terms. You <strong>don’t </strong>want to do things with regards to data access the way NSK does them.</p> <p>Let us explore all the ways it is broken. First, in terms of actual ROI. There is absolutely no reason to have multiple implementations with different OR/Ms. There is really not a shred of reason to do so. The OR/M is already doing the work of handling the abstraction from the database layer, and the only thing that you get is an inconsistent API, inability to actually important features and a whole lot <em>more work</em> that doesn’t' get you anything.</p> <p>Second, there are the abstractions used:</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/Northwind-Starter-Kit-Review_3B93/image_10.png"><img style="background-image: none; border-right-width: 0px; margin: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/Northwind-Starter-Kit-Review_3B93/image_thumb_4.png" width="1095" height="360"></a></p> <p>I don’t like repositories, because they abstract important details about the way you work with the database. But let us give this the benefit of the doubt and look at the implementation. There is only one implementation of IRepository, which uses NHibernate.</p> <p><a href="http://ayende.com/blog/Images/Windows-Live-Writer/Northwind-Starter-Kit-Review_3B93/image_12.png"><img style="background-image: none; border-right-width: 0px; padding-left: 0px; padding-right: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px; padding-top: 0px" title="image" border="0" alt="image" src="http://ayende.com/blog/Images/Windows-Live-Writer/Northwind-Starter-Kit-Review_3B93/image_thumb_5.png" width="613" height="401"></a></p> <p>As you can see, this is pretty basic stuff. You can also see that there are several methods that aren’t implemented. That is because they make no sense to a data. The reason they are there is because IRepository&lt;T&gt; inherits from ICollection&lt;T&gt;. And the reason for <em>that</em> is likely because of <a href="http://martinfowler.com/eaaCatalog/repository.html">this</a>:</p> <blockquote> <p><i>Mediates between the domain and data mapping layers using a collection-like interface for accessing domain objects.</i></p></blockquote> <p>The fact that this is totally the wrong abstraction to use doesn’t enter to the design, it seems.</p> <p>Note that we also violate the contract of <a href="http://msdn.microsoft.com/en-us/library/bye7h94w.aspx">ICollection&lt;T&gt;.Remove</a>:</p> <blockquote> <p>true if item was successfully removed from the <a href="http://msdn.microsoft.com/en-us/library/92t2ye13.aspx">ICollection&lt;T&gt;</a>; otherwise, false. This method also returns false if item is not found in the original <a href="http://msdn.microsoft.com/en-us/library/92t2ye13.aspx">ICollection&lt;T&gt;</a>.</p></blockquote> <p>There are other reasons to dislike this sort of thing, but I’ll touch on that on my next post.</p>http://ayende.com/blog/153028/northwind-starter-kit-review-data-access-and-the-essence-of-needless-work-part-i?Key=a3c79694-45fd-4efb-a2ee-8b2b4a3f11c6http://ayende.com/blog/153028/northwind-starter-kit-review-data-access-and-the-essence-of-needless-work-part-i?Key=a3c79694-45fd-4efb-a2ee-8b2b4a3f11c6Tue, 10 Jan 2012 10:00:00 GMTIs OR/M an anti pattern?<p><a href="http://seldo.com/weblog/2011/08/11/orm_is_an_antipattern">This article</a> thinks so, and I was asked to comment on that. I have to say that I agree with a lot in this article. It starts by laying out what an anti pattern <em>is</em>:</p> <blockquote> <ol> <li>It initially appears to be beneficial, but in the long term has more bad consequences than good ones <li>An alternative solution exists that is proven and repeatable</li></ol></blockquote> <p>And then goes on to list some of the problems with OR/M:</p> <ul> <li>Inadequate abstraction - The most obvious problem with ORM as an abstraction is that it does not adequately abstract away the implementation details. The documentation of all the major ORM libraries is rife with references to SQL concepts. <li>Incorrect abstraction – …if your data is not relational, then you are adding a huge and unnecessary overhead by using SQL in the first place and then compounding the problem by adding a further abstraction layer on top of that.<br>On the the other hand, if your data is relational, then your object mapping will eventually break down. SQL is about <a href="http://en.wikipedia.org/wiki/Relational_algebra">relational algebra</a>: the output of SQL is not an object but an answer to a question. <li>Death by a thousand queries – …when you are fetching a thousand records at a time, fetching 30 columns when you only need 3 becomes a pernicious source of inefficiency. Many ORM layers are also notably bad at deducing joins, and will fall back to dozens of individual queries for related objects.</li></ul> <p>If the article was about pointing out the problems in OR/M I would have no issues in endorsing it unreservedly. Many of the problems it points out are real. They can be mitigated quite nicely by someone who knows what they are doing, but that is beside the point.</p> <p>I think that I am in a pretty unique position to answer this question. I have over 7 years of being heavily involved in the NHibernate project, and I have been living &amp; breathing OR/M for all of that time. I have also created RavenDB, a NoSQL database, that gives me a good perspective about what it means to work with a non relational store.</p> <p>And like most criticisms of OR/M that I have heard over the years, this article does only half the job. It tells you what is good &amp; bad (most bad) in OR/M, but it fails to point out something quite important. </p> <p>To misquote Churchill, Object Relational Mapping is the worst form of accessing a relational database, except all of the other options when used for OLTP. </p> <p>When I see people railing against the problems in OR/M, they usually point out <em>quite correctly</em> problems that are truly painful. But they never seem to remember all of the <em>other</em> problems that OR/M usually shields you from. </p> <p>One alternative is to move away from Relational Databases. RavenDB and the RavenDB Client API has been specifically designed by us to overcome a lot of the limitations and pitfalls inherit to OR/M. We have been able to take advantage of all of our experience in the area and create what I consider to be a truly awesome experience. </p> <p>But if you <em>can’t </em>move away from Relational Databases, what are the alternative? Ad hoc SQL or Stored Procedures? You want to call <em>that</em> better?</p> <p>A better alternative might be something like <a href="https://github.com/robconery/massive">Massive</a>, which is a <em>very</em> thin layer over SQL. But that suffers from a whole host of other issues (no unit of work means aliasing issues, no support for eager load means better chance for SELECT N+1, no easy way to handle migrations, etc). There is a <em>reason</em> why OR/M have reached where they have. There are a <em>lot</em> of design decisions that simply cannot be made any other way without unacceptable tradeoffs.</p> <p>From my perspective, that means that if you are using Relational Databases for OLTP, you are most likely best served with an OR/M. Now, if you want to move away from Relational Databases for OLTP, I would be quite happy to agree with you that this is the right move to make.</p>http://ayende.com/blog/105473/is-or-m-an-anti-pattern?Key=4a92c926-48fe-4bf8-8373-ebc2dd290e3ehttp://ayende.com/blog/105473/is-or-m-an-anti-pattern?Key=4a92c926-48fe-4bf8-8373-ebc2dd290e3eTue, 27 Sep 2011 11:00:00 GMT