<?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>Ranok&#039;s Ramblings &#187; mnesia</title>
	<atom:link href="http://www.r4n0k.com/tag/mnesia/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.r4n0k.com</link>
	<description>A peek into my life, and the projects I never complete</description>
	<lastBuildDate>Sat, 04 Feb 2012 16:36:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Introduction to Mnesia I</title>
		<link>http://www.r4n0k.com/2009/10/14/introduction-to-mnesia-i/</link>
		<comments>http://www.r4n0k.com/2009/10/14/introduction-to-mnesia-i/#comments</comments>
		<pubDate>Wed, 14 Oct 2009 18:56:31 +0000</pubDate>
		<dc:creator>ranok</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[dbms]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[mnesia]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.r4n0k.com/?p=203</guid>
		<description><![CDATA[Today in my Advanced Concepts in Operating Systems class I led the discussion on the Mnesia paper from PADL&#8217;99, while this paper has numerous typos it does do an excellent job highlighting the features and advantages of Mnesia. For those of you who are unaware, Mnesia is a distributed, fault-tolerant object DBMS written in Erlang. [...]]]></description>
			<content:encoded><![CDATA[<p>Today in my Advanced Concepts in Operating Systems class I led the discussion on the <a href="http://www.erlang.se/publications/mnesia_overview.pdf">Mnesia paper</a> from PADL&#8217;99, while this paper has numerous typos it does do an excellent job highlighting the features and advantages of Mnesia. For those of you who are unaware, Mnesia is a distributed, fault-tolerant object DBMS written in Erlang. One thing about Mnesia that I have found to be lacking is a tutorial written for the lay person from the ground up, this gap I intend to try and fill. This multi-segment tutorial assumes you have knowledge of Erlang, and the basic  concepts of manipulating data with DBMSes, other than that, I hope to provide enough information and code to demystify a fairly complex system. However, I still am on the road to mastery, so if I make any errors, or you have any tips for improvement, I&#8217;d be happy to add them in.</p>
<p>To get started, start up the Erlang shell (erl) with a name, I will use -sname foo in the following examples. Below is a transcript of starting up and creating a disk-based</p>
<div class="hl-container">
<div class="hl-main">
<pre><span class="hl-code">ranok@orion:~/Desktop$ erl -sname foo
Erlang R13B01 (erts-5.7.2) [source] [smp:2:2] [rq:2] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.7.2  (abort with ^G)
(foo@orion)1&gt; mnesia:create_schema([node()]).
ok
(foo@orion)2&gt; mnesia:start().
ok</span></pre>
</div>
</div>
<p>The most important call made here, create_schema, takes a list of nodes to replicate the schema table to on disk. You can add additional disk-based nodes or ram-based copies later (we&#8217;ll get to the details later). After you&#8217;ve created the schema (this will make a folder for all the Mnesia table data), you can start the application with the start function.</p>
<p>Now that we have the database running, we need at least one table to store the data in, we will start with a very simple record to just store simple key/value data. The nice thing about Mnesia, is that the data we store can be pretty much anything, from a simple atom to a function. We will start learning the basics from the mnesia_test module, which I have <a href="http://code.jitunleashed.com/mnesia/mnesia_test.erl">uploaded here</a>.</p>
<p>The first few lines of the module start off like any Erlang code, a module declaration, what functions to export, including the QLC (Query List Comprehensions) include file (you may need to find it for your system) and a record definition:</p>
<div class="hl-container">
<div class="hl-main">
<pre><span class="hl-code">-record(data, {key, value}).</span></pre>
</div>
</div>
<p>Which will define our record for the table also named data. In the function setup_and_start/0, we tie in what we already went over with the create_table function, which in our case looks like</p>
<div class="hl-container">
<div class="hl-main">
<pre><span class="hl-code">mnesia:create_table(data, [{disc_copies, [node()]}, {attributes, record_info(fields, data)}])</span></pre>
</div>
</div>
<p>The <a href="http://erlang.org/doc/man/mnesia.html#create_table-2">create_table</a> function has a number of options, the most basic of which we will deal with at the moment: the name of the new table, where and how the table will be stored and what fields the table has (this code used the record_info() function to pull those out of the data record for us). Now that we have our table, we need a way to get the data in and out of it.</p>
<p>Many databases provide the ability for multiple queries to be joined into one transaction to be executed atomically. Mnesia is no different, but for the most part, all queries are executed through the transaction manager (there is a dirty interface which will be discussed later), this makes working in a distributed environment much easier. The way to perform a transaction in Mnesia is to pass a fun to the mnesia:transaction() function that will atomically run.</p>
<p>The actual function to enter data (both insert and update) is write(Record). We wrap this into the mnesia_test:insert(Key, Val) function displayed below:</p>
<div class="hl-container">
<div class="hl-main">
<pre><span class="hl-code">insert(Key, Val) -&gt;
Record = #data{key = Key, value = Val},
F = fun() -&gt;
mnesia:write(Record)
end,
mnesia:transaction(F).</span></pre>
</div>
</div>
<p>To now retrieve this data back from the database, the read function is now used, the read function takes two arguments: the table name (in this case, data) and the key to retrieve. The mnesia_test retrieve function wraps this nicely for us, and is shown below:</p>
<div class="hl-container">
<div class="hl-main">
<pre><span class="hl-code">retrieve(Key) -&gt;
F = fun() -&gt;
mnesia:read({data, Key})
end,
{atomic, Data} = mnesia:transaction(F),
Data.</span></pre>
</div>
</div>
<p>mnesia:transaction will either return {aborted, Reason} or {atomic, Rows}, where Rows is a list of all the retrieved data. If the key we tried to retrieve could not be found, then it will return an empty list.</p>
<p>Say however, we want to search the table for certain values that are not the index of the table. For that there is the matching functions, the simplest of them is the match_object whose usage can be seen here:</p>
<div class="hl-container">
<div class="hl-main">
<pre><span class="hl-code">search(Val) -&gt;
F = fun() -&gt;
mnesia:match_object(#data{key = '_', value = Val})
end,
{atomic, Data} = mnesia:transaction(F),
Data.</span></pre>
</div>
</div>
<p>As you can see, simply fill in all the values that are known and that you want to search for, and use the &#8216;_&#8217; unmatched value for all the other values. This transaction will return the same forms as the read transaction.</p>
<p>There is another method for filtering through Mnesia tables, which is very similar to the list comprehensions builtin to Erlang which is called QLC. The QLC version of the above function is below:</p>
<div class="hl-container">
<div class="hl-main">
<pre><span class="hl-code">search_qlc(Val) -&gt;
F = fun() -&gt;
qlc:eval(
qlc:q(
[X || X &lt;- mnesia:table(data), X#data.value == Val]
))
end,
{atomic, Data} = mnesia:transaction(F),
Data.</span></pre>
</div>
</div>
<p>What the query here is doing is is returning a list of Xs where every possible X comes from our data table, and X#data.value == Val. This should be very intuitive for those of you who are familiar with list comprehensions. What qlc:q() does is form a query handle (much like a function object) which gets evaluated by qlc:eval() inside of our transaction object. Again, this will return the same values from mnesia:transaction.</p>
<p>Well, that about covers the basics of Mnesia, you now should be able to setup Mnesia on your computer, create a table and insert/retrieve data from it. In the next installment, we will look at distributed Mnesia and the dirty interface, which provides faster queries by bypassing the transaction manager. After that we will put all of what we&#8217;ve learned into creating a system that will take advantage of Mnesia and give a pseudo real-world problem a fitting solution. Please check back soon for the next installment!</p>
<p>Peace and chow,</p>
<p>Ranok</p>
]]></content:encoded>
			<wfw:commentRss>http://www.r4n0k.com/2009/10/14/introduction-to-mnesia-i/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Open Server Platform Version 0.3 Released!</title>
		<link>http://www.r4n0k.com/2009/04/21/osp-version-03-released/</link>
		<comments>http://www.r4n0k.com/2009/04/21/osp-version-03-released/#comments</comments>
		<pubDate>Tue, 21 Apr 2009 23:14:27 +0000</pubDate>
		<dc:creator>ranok</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[mnesia]]></category>
		<category><![CDATA[osp]]></category>
		<category><![CDATA[release]]></category>

		<guid isPermaLink="false">http://www.r4n0k.com/?p=179</guid>
		<description><![CDATA[Today I finally got around to going through and testing the RC for version 0.3 of Open Server Platform. Everything seemed to work as planned, except for a few known issues that will be fixed in the next release, which should be coming down the pipe soon. Improvements: Added replication nodes to increase fail-safe reliability [...]]]></description>
			<content:encoded><![CDATA[<p>Today I finally got around to going through and testing the RC for <a href="http://groups.google.com/group/openserverplatform/browse_thread/thread/101e2a449b87e648">version 0.3</a> of <a href="http://openserverplatform.com/">Open Server Platform</a>. Everything seemed to work as planned, except for a few known issues that will be fixed in the next release, which should be coming down the pipe soon.</p>
<p>Improvements:</p>
<ul>
<li>Added replication nodes to increase fail-safe reliability</li>
<li>Added commands to the administration console to stop and live migrate applications</li>
<li>Can use configuration files to assist in the start up of OSP</li>
<li>Many bug fixes and documentation updates</li>
<li>The HTTPd servlet example now supports large files and the HEAD command</li>
</ul>
<p>So, please check it out and let me know what you think!</p>
<p>Peace and chow,</p>
<p>Ranok</p>
]]></content:encoded>
			<wfw:commentRss>http://www.r4n0k.com/2009/04/21/osp-version-03-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OSP Version 0.2 Released!</title>
		<link>http://www.r4n0k.com/2008/04/07/osp-version-02-released/</link>
		<comments>http://www.r4n0k.com/2008/04/07/osp-version-02-released/#comments</comments>
		<pubDate>Tue, 08 Apr 2008 00:53:28 +0000</pubDate>
		<dc:creator>ranok</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[mnesia]]></category>
		<category><![CDATA[osp]]></category>
		<category><![CDATA[servlet]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.r4n0k.com/?p=39</guid>
		<description><![CDATA[After less than a week OSP has reached another milestone in it&#8217;s development! Version 0.2 was just released with a few big fixes, performance improvments and added functionality. Also the documentation has been improved and made consistant with the code. The most important bug fix was a performance issue that caused a slowdown over time, [...]]]></description>
			<content:encoded><![CDATA[<p>After less than a week OSP has reached another milestone in it&#8217;s development! Version 0.2 was just released with a few big fixes, performance improvments and added functionality. Also the documentation has been improved and made consistant with the code.</p>
<p>The most important bug fix was a performance issue that caused a slowdown over time, this has been fixed and along with it, the performance and scalability has increased! The biggest feature added was support for UDP (and soon SCTP, and SSL) which can be set without changing the code! Imagine writing a server and deploying it using TCP, UDP, SSL and SCTP, just by changing one line in the XML servlet.</p>
<p>Setup should be simple too, just download from <a href="http://code.google.com/p/openserverplatform/">Google Code</a>, extract, make and then compile your servlet. Now on the first node, run ./start-osp.sh and then you can telnet to port 9876 and administer your cluster.</p>
<p>Links:</p>
<ul>
<li><a href="http://www.openserverplatform.com">Open Server Platform home</a></li>
<li><a href="http://groups.google.com/group/openserverplatform">OSP Group</a></li>
<li><a href="http://code.google.com/p/openserverplatform/">OSP Code</a></li>
</ul>
<p>Enjoy,</p>
<p>Ranok</p>
]]></content:encoded>
			<wfw:commentRss>http://www.r4n0k.com/2008/04/07/osp-version-02-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OSP v0.1 Released!</title>
		<link>http://www.r4n0k.com/2008/04/03/osp-v01-released/</link>
		<comments>http://www.r4n0k.com/2008/04/03/osp-v01-released/#comments</comments>
		<pubDate>Thu, 03 Apr 2008 22:20:40 +0000</pubDate>
		<dc:creator>ranok</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[mnesia]]></category>
		<category><![CDATA[osp]]></category>

		<guid isPermaLink="false">http://www.r4n0k.com/?p=38</guid>
		<description><![CDATA[I&#8217;m happy to announce that my OSP version has gotten stable enough to release version 0.1! While still a very temperamental system, it is showing true promise. It&#8217;s gotten to the point where on the first node, you edit a configuration file and run start-osp.sh and on each of the other nodes, just copy join.sh [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;m happy to announce that my OSP version has gotten stable enough to release version 0.1! While still a very temperamental system, it is showing true promise. It&#8217;s gotten to the point where on the first node, you edit a configuration file and run start-osp.sh and on each of the other nodes, just copy join.sh and edit it on each machine. After it&#8217;s run on each, you can telnet into the first machine and start applications which all have shared state across the cluster.</p>
<p>For more information, check out the <a href="http://www.openserverplatform.com/">home page</a>, the <a href="http://code.google.com/p/openserverplatform/">Google Code page</a> and the <a href="http://groups.google.com/group/openserverplatform" target="_self">Google Group</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.r4n0k.com/2008/04/03/osp-v01-released/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Open Server Platform Update</title>
		<link>http://www.r4n0k.com/2008/02/11/open-server-platform-update/</link>
		<comments>http://www.r4n0k.com/2008/02/11/open-server-platform-update/#comments</comments>
		<pubDate>Mon, 11 Feb 2008 05:09:27 +0000</pubDate>
		<dc:creator>ranok</dc:creator>
				<category><![CDATA[Projects]]></category>
		<category><![CDATA[Technical]]></category>
		<category><![CDATA[cosi]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[erlfs]]></category>
		<category><![CDATA[mnesia]]></category>
		<category><![CDATA[osp]]></category>

		<guid isPermaLink="false">http://www.r4n0k.com/2008/02/11/open-server-platform-update/</guid>
		<description><![CDATA[    For those of you who don&#8217;t know, I&#8217;ve been working on a project making a server environment that only requires the server logic to be coded, the rest (high availability, load balancing, etc&#8230;) is all taken care of by my project. However, before I can start on the server itself, I need to make [...]]]></description>
			<content:encoded><![CDATA[<p>    For those of you who don&#8217;t know, I&#8217;ve been working on a project making a server environment that only requires the server logic to be coded, the rest (high availability, load balancing, etc&#8230;) is all taken care of by my project. However, before I can start on the server itself, I need to make a redundant file-system that lets distributed nodes keep synced up. It&#8217;s almost at a prototype stage where I can demo to the world. I put up <a href="http://www.openserverplatform.com">a website</a> and a <a href="http://code.google.com/p/openserverplatform/">Google code project</a> with a SVN repository.</p>
<p>It&#8217;s written entirely in Erlang, and using the Mnesia database for the meta-data storage. It&#8217;s licensed under the MIT license so it&#8217;s business friendly and open-source.</p>
<p>Peace and chow,</p>
<p>Ranok</p>
]]></content:encoded>
			<wfw:commentRss>http://www.r4n0k.com/2008/02/11/open-server-platform-update/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

