<?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>Toru Maesaka &#187; algorithm</title>
	<atom:link href="http://torum.net/tag/algorithm/feed/" rel="self" type="application/rss+xml" />
	<link>http://torum.net</link>
	<description>Hackaholic and a Web Addict based in Tokyo</description>
	<lastBuildDate>Fri, 27 Aug 2010 15:13:38 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>TC Concurrency Model and BlitzDB Part 1</title>
		<link>http://torum.net/2009/11/tc-concurrency-model-and-blitzdb-1/</link>
		<comments>http://torum.net/2009/11/tc-concurrency-model-and-blitzdb-1/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 18:18:32 +0000</pubDate>
		<dc:creator>Toru Maesaka</dc:creator>
				<category><![CDATA[drizzle]]></category>
		<category><![CDATA[knowledge]]></category>
		<category><![CDATA[algorithm]]></category>
		<category><![CDATA[blitzdb]]></category>
		<category><![CDATA[btree]]></category>
		<category><![CDATA[hashtable]]></category>

		<guid isPermaLink="false">http://torum.net/?p=2304</guid>
		<description><![CDATA[Recently I started rewriting BlitzDB because I&#8217;ve come to realize the mistakes I&#8217;ve made from getting a better understanding of the Drizzle Storage API and Tokyo Cabinet internals. Admittedly a rewrite is an exaggeration because I&#8217;ll be reusing most of the components but more in a C++ way. One decision I decided to make is [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I started rewriting BlitzDB because I&#8217;ve come to realize the mistakes I&#8217;ve made from getting a better understanding of the <a href="https://launchpad.net/drizzle">Drizzle</a> Storage API and <a href="http://1978th.net/tokyocabinet/">Tokyo Cabinet</a> internals. Admittedly a rewrite is an exaggeration because I&#8217;ll be reusing most of the components but more in a C++ way.</p>
<p>One decision I decided to make is that BlitzDB will only support a BTREE index via <a href="http://1978th.net/tokyocabinet/spex-en.html#tcbdbapi">TC&#8217;s B+Tree API</a> in it&#8217;s first release. Ignoring BlitzDB for now, several people I&#8217;ve talked to about key/value data structures often ask why I love <a href="http://en.wikipedia.org/wiki/B%2Btree">B+Tree</a> so much when it&#8217;s faster to work with a <a href="http://en.wikipedia.org/wiki/Hash_table">hash table</a>. Please don&#8217;t take it wrong, O(1) operations are beautiful and I love hash tables but stereotyping key/value structures to it is not. Everything has it&#8217;s ups and downs and hash table/map is not an exception. In this blog entry, I will describe why B+Tree is good for index scanning.</p>
<h3>Why a B+Tree Index</h3>
<p>A search algorithm of O(1) like hashing is clearly faster than O(log n) unless there&#8217;s something fishy about the implementation or the dataset is too small for the time complexity to matter. However, this is only true for looking up and fetching the value. For those that are only interested in fetching a particular value, that&#8217;s probably the best you can ask for. However things are different if you look into things beyond lookups like fetching or scanning through a range of keys.</p>
<p>To do this with a typical hash table, either your data structure must be able to provide a list of stored keys OR your application must do some housekeeping and save a list of relevant keys elsewhere for future use. Your application would then need to compute the subset of keys that you&#8217;re interested in and fetch them with a loop. Algorithmically speaking, each fetch operation is O(1) but what&#8217;s expensive here is that you end up doing a lot of random access. This is obviously going to kill your performance, especially when you need to chew through a heavy workload (though this _could_ change when SSD becomes standard).</p>
<p>B+Tree on the other hand is fantastic for this use-case. The actual data are stored at the leaf node and they are usually logically linked so that you don&#8217;t need to re-traverse the tree to get the next greater key (if you run out of relevant pages in the node, you move on to the neighbor leaf node). The pages are aligned on disk, which means sequential access. Another bonus is that most of the time, you can keep the entire internal nodes on memory which is small and inexpensive but effective for searching.</p>
<p>Solution to this? well, mine is to implement a combination play of the two data structures and take advantage of the different characteristics. In BlitzDB, the actual rows are stored in TC&#8217;s Hash Database and the index will store keys to the row. So, a clustered index.</p>
<p>What I&#8217;ve mentioned so far is all theoretical without providing any benchmark results but all I&#8217;m trying to say is, <strong>it&#8217;s all about access patterns and use-cases</strong>. My current interest is in index scan and therefore the decision. However, if there is enough people that asks me for a HASH index, I can write that functionality relatively easily later on :)</p>
<h3>Next Stop</h3>
<p>I would love to keep writing but it is currently past 3am in Japan and I&#8217;m dozing out here. Apologies for not covering Tokyo Cabinet&#8217;s in-depth concurrency model but I will cover it in my next post of the series and how this impacts BlitzDB&#8217;s design.</p>
]]></content:encoded>
			<wfw:commentRss>http://torum.net/2009/11/tc-concurrency-model-and-blitzdb-1/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
