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

<channel>
	<title>Eigenjoy &#187; code</title>
	<atom:link href="http://eigenjoy.com/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://eigenjoy.com</link>
	<description>a programming blog</description>
	<lastBuildDate>Wed, 14 Dec 2011 18:22:45 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Binary Search Revisited</title>
		<link>http://eigenjoy.com/2011/09/09/binary-search-revisited/</link>
		<comments>http://eigenjoy.com/2011/09/09/binary-search-revisited/#comments</comments>
		<pubDate>Fri, 09 Sep 2011 17:11:08 +0000</pubDate>
		<dc:creator>Matt Pulver</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://www.xcombinator.com/?p=506</guid>
		<description><![CDATA[As ubiquitous as it is, the standard binary search algorithm can still be further optimized by using bit operations to iterate through its sorted list, in place of arithmetic. Admittedly, this is primarily an academic discussion, since the code improvement does not decrease the logarithmic complexity of the standard algorithm.  Nevertheless, a well-developed programming [...]]]></description>
			<content:encoded><![CDATA[<p>As ubiquitous as it is, the standard binary search algorithm can still be further optimized by using bit operations to iterate through its sorted list, in place of arithmetic. Admittedly, this is primarily an academic discussion, since the code improvement does not decrease the logarithmic complexity of the standard algorithm.  Nevertheless, a well-developed programming intuition should by default implement (or at a minimum consider) a solution similar to the one presented here, prior to &#8220;the obvious&#8221; standard arithmetic solution.</p>
<p>Here&#8217;s an example. We want to find the largest index i such that haystack[i] <= needle. Needle = 15, and haystack is a sorted list of the first 8 prime numbers, indexed by binary numbers:</p>
<p><code>000</code> &nbsp; &nbsp; 2<br />
<code>001</code> &nbsp; &nbsp; 3<br />
<code>010</code> &nbsp; &nbsp; 5<br />
<code>011</code> &nbsp; &nbsp; 7<br />
<code>100</code> &nbsp; &nbsp; 11<br />
<code>101</code> &nbsp; &nbsp; 13<br />
<code>110</code> &nbsp; &nbsp; 17<br />
<code>111</code> &nbsp; &nbsp; 19</p>
<p>Note first that the haystack index requires no more than 3 bits.  Therefore we start with the highest order bit set: b=<code>100</code>. (<code>This</code> <code>font</code> denotes binary numbers here.) Is haystack[<code>100</code>] <= 15?  Yes, 11<=15. Observe this means that the first bit of the index we are looking for is <code>1</code>. We look at the next bit. Is haystack[<code>110</code>] <= 15? No, 17 <= 15 is false. This means the 2nd bit is <code>0</code>. Finally, is haystack[<code>101</code>] <= 15? Yes, 13 <= 15. Therefore the index we are looking for is <code>101</code>.</p>
<p>In essence, the main loop to find the index i is simply:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> <span style="color: #008080;">;</span> b <span style="color: #008080;">;</span> b <span style="color: #000080;">&gt;&gt;=</span> <span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> haystack<span style="color: #008000;">&#91;</span>i<span style="color: #000040;">|</span>b<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;=</span> needle <span style="color: #008000;">&#41;</span> i <span style="color: #000040;">|</span><span style="color: #000080;">=</span> b<span style="color: #008080;">;</span></pre></div></div>

<p>where b is the value with 1 bit set, that began with b=<code>100</code> and i=<code>0</code>.</p>
<p><em>It is the natural structure of the bits within the index variable that automatically tracks both the upper and lower bounds of the search window for each iteration.</em> Compare this with the less efficient and more verbose arithmetic performed in the main loops of standard binary search algorithms.</p>
<p>The full C++ code for the improved binary search algorithm fbsearch() is:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Binary search revisited.</span>
&nbsp;
<span style="color: #666666;">// Define only one of these:</span>
<span style="color: #666666;">// #define SETBIT_FAST</span>
<span style="color: #666666;">// #define SETBIT_FASTER</span>
<span style="color: #339900;">#define SETBIT_FASTEST</span>
&nbsp;
<span style="color: #339900;">#ifdef SETBIT_FAST</span>
<span style="color: #339900;">#include &lt;math.h&gt;</span>
<span style="color: #339900;">#endif</span>
&nbsp;
<span style="color: #666666;">// Return 1 &lt;&lt; log_2(list_size-1), or 0 if list_size == 1.</span>
<span style="color: #666666;">// This sets the initial value of b in fbsearch().</span>
<span style="color: #0000ff;">inline</span> <span style="color: #0000ff;">unsigned</span> init_bit<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">unsigned</span> list_size <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
<span style="color: #339900;">#ifdef SETBIT_FAST</span>
    <span style="color: #0000ff;">return</span> list_size <span style="color: #000080;">==</span> <span style="color: #0000dd;">1</span> <span style="color: #008080;">?</span> <span style="color: #0000dd;">0</span> <span style="color: #008080;">:</span>
        <span style="color: #0000dd;">1</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000ff;">int</span><span style="color: #008000;">&#40;</span> <span style="color: #0000dd;">log</span><span style="color: #008000;">&#40;</span>list_size<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">/</span> M_LN2 <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #339900;">#endif</span>
<span style="color: #339900;">#ifdef SETBIT_FASTER</span>
    <span style="color: #0000ff;">return</span> list_size <span style="color: #000080;">==</span> <span style="color: #0000dd;">1</span> <span style="color: #008080;">?</span> <span style="color: #0000dd;">0</span> <span style="color: #008080;">:</span>
        <span style="color: #0000dd;">1</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #008000;">&#40;</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">unsigned</span><span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #0000dd;">3</span> <span style="color: #008000;">&#41;</span> <span style="color: #000040;">-</span> <span style="color: #0000dd;">1</span>
             <span style="color: #000040;">-</span> __builtin_clz<span style="color: #008000;">&#40;</span>list_size<span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #339900;">#endif</span>
<span style="color: #339900;">#ifdef SETBIT_FASTEST</span>
    <span style="color: #0000ff;">unsigned</span> b<span style="color: #008080;">;</span>
    __asm__ <span style="color: #008000;">&#40;</span> <span style="color: #FF0000;">&quot;decl %%eax;&quot;</span>
              <span style="color: #FF0000;">&quot;je DONE;&quot;</span>
              <span style="color: #FF0000;">&quot;bsrl %%eax, %%ecx;&quot;</span> <span style="color: #666666;">// BSR - Bit Scan Reverse (386+)</span>
              <span style="color: #FF0000;">&quot;movl $1, %%eax;&quot;</span>
              <span style="color: #FF0000;">&quot;shll %%cl, %%eax;&quot;</span>
              <span style="color: #FF0000;">&quot;DONE:&quot;</span> <span style="color: #008080;">:</span> <span style="color: #FF0000;">&quot;=a&quot;</span> <span style="color: #008000;">&#40;</span>b<span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> <span style="color: #FF0000;">&quot;a&quot;</span> <span style="color: #008000;">&#40;</span>list_size<span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">return</span> b<span style="color: #008080;">;</span>
<span style="color: #339900;">#endif</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// Return the greatest unsigned i where haystack[i] &lt;= needle.</span>
<span style="color: #666666;">// If i does not exist (haystack is empty, or needle &lt; haystack[0])</span>
<span style="color: #666666;">// then return unsigned(-1). T can be any type for which the binary</span>
<span style="color: #666666;">// operator &lt;= is defined.</span>
<span style="color: #0000ff;">template</span> <span style="color: #000080;">&lt;</span><span style="color: #0000ff;">typename</span> T<span style="color: #000080;">&gt;</span>
<span style="color: #0000ff;">unsigned</span> fbsearch<span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">const</span> T haystack<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span>, <span style="color: #0000ff;">unsigned</span> haystack_size,
                   <span style="color: #0000ff;">const</span> T<span style="color: #000040;">&amp;</span> needle <span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> haystack_size <span style="color: #000080;">==</span> <span style="color: #0000dd;">0</span> <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">return</span> <span style="color: #0000ff;">unsigned</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">unsigned</span> i <span style="color: #000080;">=</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> <span style="color: #0000ff;">unsigned</span> b <span style="color: #000080;">=</span> init_bit<span style="color: #008000;">&#40;</span>haystack_size<span style="color: #008000;">&#41;</span> <span style="color: #008080;">;</span> b <span style="color: #008080;">;</span> b <span style="color: #000080;">&gt;&gt;=</span> <span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000ff;">unsigned</span> j <span style="color: #000080;">=</span> i <span style="color: #000040;">|</span> b<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> haystack_size <span style="color: #000080;">&lt;=</span> j <span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">continue</span><span style="color: #008080;">;</span>
        <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> haystack<span style="color: #008000;">&#91;</span>j<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;=</span> needle <span style="color: #008000;">&#41;</span> i <span style="color: #000080;">=</span> j<span style="color: #008080;">;</span>
        <span style="color: #0000ff;">else</span>
        <span style="color: #008000;">&#123;</span>
            <span style="color: #0000ff;">for</span><span style="color: #008000;">&#40;</span> b <span style="color: #000080;">&gt;&gt;=</span> <span style="color: #0000dd;">1</span> <span style="color: #008080;">;</span> b <span style="color: #008080;">;</span> b <span style="color: #000080;">&gt;&gt;=</span> <span style="color: #0000dd;">1</span> <span style="color: #008000;">&#41;</span>
                <span style="color: #0000ff;">if</span><span style="color: #008000;">&#40;</span> haystack<span style="color: #008000;">&#91;</span>i<span style="color: #000040;">|</span>b<span style="color: #008000;">&#93;</span> <span style="color: #000080;">&lt;=</span> needle <span style="color: #008000;">&#41;</span> i <span style="color: #000040;">|</span><span style="color: #000080;">=</span> b<span style="color: #008080;">;</span>
            <span style="color: #0000ff;">break</span><span style="color: #008080;">;</span>
        <span style="color: #008000;">&#125;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">return</span> i <span style="color: #000040;">||</span> <span style="color: #000040;">*</span>haystack <span style="color: #000080;">&lt;=</span> needle <span style="color: #008080;">?</span> i <span style="color: #008080;">:</span> <span style="color: #0000ff;">unsigned</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">-</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// Example Usage</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> sorted_list<span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> <span style="color: #000080;">=</span> <span style="color: #008000;">&#123;</span> <span style="color: #0000dd;">2</span>, <span style="color: #0000dd;">3</span>, <span style="color: #0000dd;">5</span>, <span style="color: #0000dd;">7</span>, <span style="color: #0000dd;">11</span>, <span style="color: #0000dd;">13</span>, <span style="color: #0000dd;">17</span>, <span style="color: #0000dd;">19</span>, <span style="color: #0000dd;">23</span> <span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">unsigned</span> list_size <span style="color: #000080;">=</span> <span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span>sorted_list<span style="color: #008000;">&#41;</span><span style="color: #000040;">/</span><span style="color: #0000dd;">sizeof</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> needle <span style="color: #000080;">=</span> <span style="color: #0000dd;">15</span><span style="color: #008080;">;</span>
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;fbsearch(sorted_list,&quot;</span><span style="color: #000080;">&lt;&lt;</span>list_size<span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">','</span><span style="color: #000080;">&lt;&lt;</span>needle<span style="color: #000080;">&lt;&lt;</span><span style="color: #FF0000;">&quot;) = &quot;</span>
         <span style="color: #000080;">&lt;&lt;</span> fbsearch<span style="color: #008000;">&#40;</span>sorted_list,list_size,needle<span style="color: #008000;">&#41;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
    <span style="color: #666666;">// fbsearch(sorted_list,9,15) = 5</span>
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Ancillary notes:</p>
<ul>
<li>The function init_bit() sets the initial value of b to having a single bit set in the highest position for indexing the array list. The three SETBIT_FAST, SETBIT_FASTER, and SETBIT_FASTEST code blocks within it are simply 3 different ways of calculating the initial value of b, whose speed ranks are the reverse of their cross-platform compatibility. The first method should compile everywhere, and simply calculates it from a base-2 logarithm. The second method uses the faster GNU __builtin_clz method (thanks Chao Xu for the suggestion) that counts the highest order bit from the left. The third and fastest method uses the Intel assembly instruction BSR that counts it from the right.</li>
<li>The null return value of unsigned(-1) (=4294967295 for 32-bit unsigned) is the unique value that will never otherwise be returned. Therefore it can safely be checked for by the calling function without risk of misinterpretation.</li>
<li>The one <code lang="cpp">else</code> block can safely be removed without changing the overall functionality. It exists to simply save having to check that the array index is in bounds on every iteration, since it is at a point in logic where that condition is guaranteed.</li>
</ul>
<p>Share:</p>
<p>	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Feigenjoy.com%2F2011%2F09%2F09%2Fbinary-search-revisited%2F&amp;title=Binary%20Search%20Revisited&amp;notes=As%20ubiquitous%20as%20it%20is%2C%20the%20standard%20binary%20search%20algorithm%20can%20still%20be%20further%20optimized%20by%20using%20bit%20operations%20to%20iterate%20through%20its%20sorted%20list%2C%20in%20place%20of%20arithmetic.%20Admittedly%2C%20this%20is%20primarily%20an%20academic%20discussion%2C%20since%20the%20code%20impro" title="del.icio.us"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Feigenjoy.com%2F2011%2F09%2F09%2Fbinary-search-revisited%2F&amp;title=Binary%20Search%20Revisited" title="Reddit"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Feigenjoy.com%2F2011%2F09%2F09%2Fbinary-search-revisited%2F" title="Technorati"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://twitter.com/home?status=Binary%20Search%20Revisited%20-%20http%3A%2F%2Feigenjoy.com%2F2011%2F09%2F09%2Fbinary-search-revisited%2F" title="Twitter"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Feigenjoy.com%2F2011%2F09%2F09%2Fbinary-search-revisited%2F&amp;t=Binary%20Search%20Revisited" title="Facebook"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Feigenjoy.com%2F2011%2F09%2F09%2Fbinary-search-revisited%2F&amp;title=Binary%20Search%20Revisited&amp;annotation=As%20ubiquitous%20as%20it%20is%2C%20the%20standard%20binary%20search%20algorithm%20can%20still%20be%20further%20optimized%20by%20using%20bit%20operations%20to%20iterate%20through%20its%20sorted%20list%2C%20in%20place%20of%20arithmetic.%20Admittedly%2C%20this%20is%20primarily%20an%20academic%20discussion%2C%20since%20the%20code%20impro" title="Google Bookmarks"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Feigenjoy.com%2F2011%2F09%2F09%2Fbinary-search-revisited%2F&amp;t=Binary%20Search%20Revisited" title="HackerNews"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.printfriendly.com/print?url=http%3A%2F%2Feigenjoy.com%2F2011%2F09%2F09%2Fbinary-search-revisited%2F&amp;partner=sociable" title="PDF"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/pdf.png" title="PDF" alt="PDF" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://eigenjoy.com/feed/" title="RSS"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a></p>
<p><br/><br/></p>
]]></content:encoded>
			<wfw:commentRss>http://eigenjoy.com/2011/09/09/binary-search-revisited/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>activeuuid &#8211; binary uuid primary keys in Rails 3.1 on MySQL</title>
		<link>http://eigenjoy.com/2011/08/29/activeuuid-binary-uuid-primary-keys-in-rails-3-1-on-mysql/</link>
		<comments>http://eigenjoy.com/2011/08/29/activeuuid-binary-uuid-primary-keys-in-rails-3-1-on-mysql/#comments</comments>
		<pubDate>Mon, 29 Aug 2011 14:42:38 +0000</pubDate>
		<dc:creator>Nate Murray</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[rails]]></category>

		<guid isPermaLink="false">http://www.xcombinator.com/?p=494</guid>
		<description><![CDATA[I&#8217;ve been debating a lot recently if I want to use NoSQL (Cassandra) for my next project or if I should just use MySQL. I plan to write a longer post on this soon, but for now I want to share with you a little gem I wrote.
I plan to use NoSQL in the not-too-distance [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been debating a lot recently if I want to use NoSQL (Cassandra) for my next project or if I should just use MySQL. I plan to write a longer post on this soon, but for now I want to share with you a little gem I wrote.</p>
<p>I plan to use NoSQL in the not-too-distance future, but not today. When I port the data over I don&#8217;t want to have to map &#8220;old ids&#8221; to &#8220;new ids&#8221;. By using a <tt>uuid</tt> from day one I can easily maintain referential integrity when I migrate to a new datastore.</p>
<p>I looked up how this is typically done in Rails using MySQL and all I could find was examples that store the UUID as strings (e.g. <a href="https://gist.github.com/937739">here</a>). The UUID is natively just 16 bytes so storing this in a <tt>VARCHAR(36)</tt> seemed like a poor solution. Ideally we would store the UUID as binary and let the ORM deal with translating it to and from a UUID object.</p>
<p><tt>activeuuid</tt> solves this problem. <tt>activeuuid</tt> is a Rails plugin that will let you use <tt>UUIDTools::UUID</tt> objects as primary keys for your ActiveRecords and it stores them in MySQL as <tt>binary(16)</tt>.</p>
<p>Here&#8217;s how to use it:</p>
<h2>Create a Migration</h2>
<p><tt>activeuuid</tt> adds the <tt>uuid</tt> type to your migrations. Example:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> CreateEmails <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Migration</span>
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">up</span>
    create_table <span style="color:#ff3333; font-weight:bold;">:emails</span>, <span style="color:#ff3333; font-weight:bold;">:id</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">false</span>  <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>t<span style="color:#006600; font-weight:bold;">|</span>
      t.<span style="color:#9900CC;">uuid</span> <span style="color:#ff3333; font-weight:bold;">:id</span>, <span style="color:#ff3333; font-weight:bold;">:unique</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#0000FF; font-weight:bold;">true</span>
      t.<span style="color:#9900CC;">uuid</span> <span style="color:#ff3333; font-weight:bold;">:sender_id</span>  <span style="color:#008000; font-style:italic;"># belongs_to :sender</span>
&nbsp;
      t.<span style="color:#CC0066; font-weight:bold;">string</span> <span style="color:#ff3333; font-weight:bold;">:subject</span>
      t.<span style="color:#9900CC;">text</span> <span style="color:#ff3333; font-weight:bold;">:body</span>
&nbsp;
      t.<span style="color:#9900CC;">timestamp</span> <span style="color:#ff3333; font-weight:bold;">:sent_at</span>
      t.<span style="color:#9900CC;">timestamps</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    add_index <span style="color:#ff3333; font-weight:bold;">:emails</span>, <span style="color:#ff3333; font-weight:bold;">:id</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> <span style="color:#0000FF; font-weight:bold;">self</span>.<span style="color:#9900CC;">down</span>
    drop_table <span style="color:#ff3333; font-weight:bold;">:emails</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h2>include ActiveUUID::UUID in your model</h2>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> Email <span style="color:#006600; font-weight:bold;">&lt;</span> <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>
  <span style="color:#9966CC; font-weight:bold;">include</span> <span style="color:#6666ff; font-weight:bold;">ActiveUUID::UUID</span>
  belongs_to <span style="color:#ff3333; font-weight:bold;">:sender</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h2>use it</h2>
<p>Here are some example specs:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'spec_helper'</span>
&nbsp;
describe Email <span style="color:#9966CC; font-weight:bold;">do</span>
&nbsp;
  context <span style="color:#996600;">&quot;when using uuid's as keys&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
    before<span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:each</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      Email.<span style="color:#9900CC;">delete_all</span>
      <span style="color:#0066ff; font-weight:bold;">@guid</span> = <span style="color:#996600;">&quot;1dd74dd0-d116-11e0-99c7-5ac5d975667e&quot;</span>
      <span style="color:#0066ff; font-weight:bold;">@e</span> = Email.<span style="color:#9900CC;">new</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#ff3333; font-weight:bold;">:subject</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;hello&quot;</span>, <span style="color:#ff3333; font-weight:bold;">:body</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> <span style="color:#996600;">&quot;world&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006600; font-weight:bold;">|</span>e<span style="color:#006600; font-weight:bold;">|</span> e.<span style="color:#9900CC;">id</span> = <span style="color:#6666ff; font-weight:bold;">UUIDTools::UUID</span>.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span>@guid<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
      <span style="color:#0066ff; font-weight:bold;">@e</span>.<span style="color:#9900CC;">save</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    it <span style="color:#996600;">&quot;the id guid should be equal to the uuid&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      <span style="color:#0066ff; font-weight:bold;">@e</span>.<span style="color:#9900CC;">id</span>.<span style="color:#9900CC;">to_s</span>.<span style="color:#9900CC;">should</span> eql<span style="color:#006600; font-weight:bold;">&#40;</span>@guid<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
    it <span style="color:#996600;">&quot;should be able to find an email by the uuid&quot;</span> <span style="color:#9966CC; font-weight:bold;">do</span>
      f = Email.<span style="color:#9900CC;">find</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#6666ff; font-weight:bold;">UUIDTools::UUID</span>.<span style="color:#9900CC;">parse</span><span style="color:#006600; font-weight:bold;">&#40;</span>@guid<span style="color:#006600; font-weight:bold;">&#41;</span><span style="color:#006600; font-weight:bold;">&#41;</span>
      f.<span style="color:#9900CC;">id</span>.<span style="color:#9900CC;">to_s</span>.<span style="color:#9900CC;">should</span> eql<span style="color:#006600; font-weight:bold;">&#40;</span>@guid<span style="color:#006600; font-weight:bold;">&#41;</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">end</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<h2>Benefits</h2>
<p>There are a number of benefits to using UUIDs as primary keys.:</p>
<ul>
<li>no id conflict during multi-master write</li>
<li>no locking due to auto-increment</li>
<li>with time-based UUIDs you can store a timestamp within your UUID</li>
<li>you can create natural keys (based on the SHA of model attributes)</li>
</ul>
<h2>Future work</h2>
<ul>
<li>more transparent support for natural and composite keys</li>
<li>support for MySQLs `INSERT &#8230; ON DUPLICATE KEY UPDATE` syntax</li>
<li>support a primary column name other than `id`</li>
<li> work on other databases (Postgres, etc)</li>
<li>tests</li>
</ul>
<h2>Installation</h2>
<p>Add this to your <tt>Gemfile</tt></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">    gem <span style="color:#996600;">&quot;activeuuid&quot;</span></pre></div></div>

<p>Or get the code here: <a href="https://github.com/jashmenn/activeuuid">https://github.com/jashmenn/activeuuid</a></p>
<h2>References</h2>
<ul>
<li><a href="http://bret.appspot.com/entry/how-friendfeed-uses-mysql">http://bret.appspot.com/entry/how-friendfeed-uses-mysql</a></li>
<li><a href="http://kekoav.com/blog/36-computers/58-uuids-as-primary-keys-in-mysql.html ">http://kekoav.com/blog/36-computers/58-uuids-as-primary-keys-in-mysql.html</a></li>
<li><a href="https://gist.github.com/937739">https://gist.github.com/937739</a></li>
<li><a href="http://www.codinghorror.com/blog/2007/03/primary-keys-ids-versus-guids.html">http://www.codinghorror.com/blog/2007/03/primary-keys-ids-versus-guids.html</a></li>
<li><a href="http://krow.livejournal.com/497839.html">http://krow.livejournal.com/497839.html</a></li>
<li><a href="https://github.com/jamesgolick/friendly">https://github.com/jamesgolick/friendly</a></li>
</ul>
<h2>Dependencies</h2>
<p>Note that this depends on Rails 3.1 because it uses the custom column serialization added by Aaron Patterson. (See: <a href="https://github.com/rails/rails/commit/ebe485fd8ec80a1a9b86516bc6f74bc5bbba3476">https://github.com/rails/rails/commit/ebe485fd8ec80a1a9b86516bc6f74bc5bbba3476</a>)</p>
<p>Share:</p>
<p>	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Feigenjoy.com%2F2011%2F08%2F29%2Factiveuuid-binary-uuid-primary-keys-in-rails-3-1-on-mysql%2F&amp;title=activeuuid%20-%20binary%20uuid%20primary%20keys%20in%20Rails%203.1%20on%20MySQL&amp;notes=I%27ve%20been%20debating%20a%20lot%20recently%20if%20I%20want%20to%20use%20NoSQL%20%28Cassandra%29%20for%20my%20next%20project%20or%20if%20I%20should%20just%20use%20MySQL.%20I%20plan%20to%20write%20a%20longer%20post%20on%20this%20soon%2C%20but%20for%20now%20I%20want%20to%20share%20with%20you%20a%20little%20gem%20I%20wrote.%0D%0A%0D%0AI%20plan%20to%20use%20NoSQL%20in%20t" title="del.icio.us"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Feigenjoy.com%2F2011%2F08%2F29%2Factiveuuid-binary-uuid-primary-keys-in-rails-3-1-on-mysql%2F&amp;title=activeuuid%20-%20binary%20uuid%20primary%20keys%20in%20Rails%203.1%20on%20MySQL" title="Reddit"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Feigenjoy.com%2F2011%2F08%2F29%2Factiveuuid-binary-uuid-primary-keys-in-rails-3-1-on-mysql%2F" title="Technorati"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://twitter.com/home?status=activeuuid%20-%20binary%20uuid%20primary%20keys%20in%20Rails%203.1%20on%20MySQL%20-%20http%3A%2F%2Feigenjoy.com%2F2011%2F08%2F29%2Factiveuuid-binary-uuid-primary-keys-in-rails-3-1-on-mysql%2F" title="Twitter"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Feigenjoy.com%2F2011%2F08%2F29%2Factiveuuid-binary-uuid-primary-keys-in-rails-3-1-on-mysql%2F&amp;t=activeuuid%20-%20binary%20uuid%20primary%20keys%20in%20Rails%203.1%20on%20MySQL" title="Facebook"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Feigenjoy.com%2F2011%2F08%2F29%2Factiveuuid-binary-uuid-primary-keys-in-rails-3-1-on-mysql%2F&amp;title=activeuuid%20-%20binary%20uuid%20primary%20keys%20in%20Rails%203.1%20on%20MySQL&amp;annotation=I%27ve%20been%20debating%20a%20lot%20recently%20if%20I%20want%20to%20use%20NoSQL%20%28Cassandra%29%20for%20my%20next%20project%20or%20if%20I%20should%20just%20use%20MySQL.%20I%20plan%20to%20write%20a%20longer%20post%20on%20this%20soon%2C%20but%20for%20now%20I%20want%20to%20share%20with%20you%20a%20little%20gem%20I%20wrote.%0D%0A%0D%0AI%20plan%20to%20use%20NoSQL%20in%20t" title="Google Bookmarks"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Feigenjoy.com%2F2011%2F08%2F29%2Factiveuuid-binary-uuid-primary-keys-in-rails-3-1-on-mysql%2F&amp;t=activeuuid%20-%20binary%20uuid%20primary%20keys%20in%20Rails%203.1%20on%20MySQL" title="HackerNews"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.printfriendly.com/print?url=http%3A%2F%2Feigenjoy.com%2F2011%2F08%2F29%2Factiveuuid-binary-uuid-primary-keys-in-rails-3-1-on-mysql%2F&amp;partner=sociable" title="PDF"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/pdf.png" title="PDF" alt="PDF" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://eigenjoy.com/feed/" title="RSS"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a></p>
<p><br/><br/></p>
]]></content:encoded>
			<wfw:commentRss>http://eigenjoy.com/2011/08/29/activeuuid-binary-uuid-primary-keys-in-rails-3-1-on-mysql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>git-style-binaries screencast</title>
		<link>http://eigenjoy.com/2009/06/10/git-style-binaries-screencast/</link>
		<comments>http://eigenjoy.com/2009/06/10/git-style-binaries-screencast/#comments</comments>
		<pubDate>Wed, 10 Jun 2009 18:16:44 +0000</pubDate>
		<dc:creator>Nate Murray</dc:creator>
				<category><![CDATA[code]]></category>
		<category><![CDATA[gems]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.xcombinator.com/2009/06/10/git-style-binaries-screencast/</guid>
		<description><![CDATA[Just released: git-style-binaries ruby gem. Checkout the README on github.
Checkout the screencast.

Share:
	
	
	
	
	
	
	
	
	

]]></description>
			<content:encoded><![CDATA[<p>Just released: git-style-binaries ruby gem. Checkout <a href="http://github.com/jashmenn/git-style-binaries">the README on github</a>.</p>
<p>Checkout <a href="http://www.xcombinator.com/movies/git-style-binaries.mov">the screencast</a>.</p>
<p><a href='http://www.xcombinator.com/movies/git-style-binaries.mov' title='gsb-screencast'><img src='http://www.xcombinator.com/wp-content/uploads/2009/06/gsb-screencast1.png' alt='gsb-screencast1.png' /></a></p>
<p>Share:</p>
<p>	<a rel="nofollow"  href="http://delicious.com/post?url=http%3A%2F%2Feigenjoy.com%2F2009%2F06%2F10%2Fgit-style-binaries-screencast%2F&amp;title=git-style-binaries%20screencast&amp;notes=Just%20released%3A%20git-style-binaries%20ruby%20gem.%20Checkout%20the%20README%20on%20github.%0D%0A%0D%0ACheckout%20the%20screencast.%0D%0A%0D%0A%0D%0A%0D%0A" title="del.icio.us"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/delicious.png" title="del.icio.us" alt="del.icio.us" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://reddit.com/submit?url=http%3A%2F%2Feigenjoy.com%2F2009%2F06%2F10%2Fgit-style-binaries-screencast%2F&amp;title=git-style-binaries%20screencast" title="Reddit"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/reddit.png" title="Reddit" alt="Reddit" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://technorati.com/faves?add=http%3A%2F%2Feigenjoy.com%2F2009%2F06%2F10%2Fgit-style-binaries-screencast%2F" title="Technorati"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/technorati.png" title="Technorati" alt="Technorati" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://twitter.com/home?status=git-style-binaries%20screencast%20-%20http%3A%2F%2Feigenjoy.com%2F2009%2F06%2F10%2Fgit-style-binaries-screencast%2F" title="Twitter"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/twitter.png" title="Twitter" alt="Twitter" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.facebook.com/share.php?u=http%3A%2F%2Feigenjoy.com%2F2009%2F06%2F10%2Fgit-style-binaries-screencast%2F&amp;t=git-style-binaries%20screencast" title="Facebook"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/facebook.png" title="Facebook" alt="Facebook" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.google.com/bookmarks/mark?op=edit&amp;bkmk=http%3A%2F%2Feigenjoy.com%2F2009%2F06%2F10%2Fgit-style-binaries-screencast%2F&amp;title=git-style-binaries%20screencast&amp;annotation=Just%20released%3A%20git-style-binaries%20ruby%20gem.%20Checkout%20the%20README%20on%20github.%0D%0A%0D%0ACheckout%20the%20screencast.%0D%0A%0D%0A%0D%0A%0D%0A" title="Google Bookmarks"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/googlebookmark.png" title="Google Bookmarks" alt="Google Bookmarks" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://news.ycombinator.com/submitlink?u=http%3A%2F%2Feigenjoy.com%2F2009%2F06%2F10%2Fgit-style-binaries-screencast%2F&amp;t=git-style-binaries%20screencast" title="HackerNews"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/hackernews.png" title="HackerNews" alt="HackerNews" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://www.printfriendly.com/print?url=http%3A%2F%2Feigenjoy.com%2F2009%2F06%2F10%2Fgit-style-binaries-screencast%2F&amp;partner=sociable" title="PDF"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/pdf.png" title="PDF" alt="PDF" class="sociable-hovers" /></a><br />
	<a rel="nofollow"  href="http://eigenjoy.com/feed/" title="RSS"><img src="http://eigenjoy.com/wp-content/plugins/sociable/images/rss.png" title="RSS" alt="RSS" class="sociable-hovers" /></a></p>
<p><br/><br/></p>
]]></content:encoded>
			<wfw:commentRss>http://eigenjoy.com/2009/06/10/git-style-binaries-screencast/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://www.xcombinator.com/movies/git-style-binaries.mov" length="135240332" type="video/quicktime" />
		</item>
	</channel>
</rss>

