<?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>Ståle&#039;s weblog &#187; JDP</title>
	<atom:link href="http://blog.staale.org/tag/jdp/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.staale.org</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Mon, 18 Oct 2010 07:43:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Using traits to configure Berkeley JDP</title>
		<link>http://blog.staale.org/2009/03/using-traits-to-configure-berkely-jdp.html</link>
		<comments>http://blog.staale.org/2009/03/using-traits-to-configure-berkely-jdp.html#comments</comments>
		<pubDate>Fri, 27 Mar 2009 16:13:27 +0000</pubDate>
		<dc:creator>Staale</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[BerkeleyDB]]></category>
		<category><![CDATA[JDP]]></category>

		<guid isPermaLink="false">http://blog.staale.org/?p=70</guid>
		<description><![CDATA[I have been using BerkeleyDBs JDP on and off a bit, just experementing with it to see how it works. BerkeleyDB is basically a B-Tree with transaction support. You store key/value pairs, both being byte arrays. JDP &#8211; Java Direct Persistence &#8211; is a layer that is built on top off this using annotations. It [...]]]></description>
			<content:encoded><![CDATA[<p>I have been using BerkeleyDBs JDP on and off a bit, just experementing with it to see how it works. BerkeleyDB is basically a B-Tree with transaction support. You store key/value pairs, both being byte arrays. JDP &#8211; Java Direct Persistence &#8211; is a layer that is built on top off this using annotations. It provides you with JPA like architecture for storing objects. Except there is no underlying SQL, just pure key lookups. This means you can&#8217;t write complex queries to select data, you will have to use code instead. But imho. this also means the complexity off what you are doing becomes more apparent.</p>
<p>I have fiddled around a bit with this, and I wanted to create a clean way off configuring environments. When you are using BerkeleyDB, you need to set up an Environment first:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Environment</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">File</span> envHome, EnvironmentConfig configuration<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>The EnvironmentConfig in return is set up using a setters:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">EnvironmentConfig envConfig <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EnvironmentConfig<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
envConfig.<span style="color: #006633;">setTransactional</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
envConfig.<span style="color: #006633;">setAllowCreate</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
envConfig.<span style="color: #006633;">setCacheSize</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>However, for Scala I wanted to use chaining to set up the environment config. The base implementation does not return this in the setters, so you can&#8217;t chain the statements. My first idea was to use implicit to convert the EnvironmentConfig to a richer version, that simply returned itself, so you could chain it.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> RichEnvConfig<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">val</span> config<span style="color: #000080;">:</span> EnvironmentConfig<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> transactional<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>config.<span style="color: #000000;">setTransactional</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">true</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span> <span style="color: #0000ff; font-weight: bold;">this</span><span style="color: #F78811;">&#125;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> allowCreate<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>config.<span style="color: #000000;">setAllowCreate</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">true</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span> <span style="color: #0000ff; font-weight: bold;">this</span><span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>However I didn&#8217;t get it to work properly. I also thought about using operators to chain together traits. But then it occured to me that using traits themselves might make things clearer. So I created the following class:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">object</span> EnvConfig <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">implicit</span> det unwrap<span style="color: #F78811;">&#40;</span>envCfg<span style="color: #000080;">:</span> EnvConfig<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> envCfg.<span style="color: #000000;">config</span>
<span style="color: #F78811;">&#125;</span>
<span style="color: #0000ff; font-weight: bold;">class</span> EnvConfig <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> config <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> EnvironmentConfig<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">trait</span> Transactional <span style="color: #F78811;">&#123;</span> config.<span style="color: #000000;">setTransactional</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">true</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span>
    <span style="color: #0000ff; font-weight: bold;">trait</span> AllowCreate <span style="color: #F78811;">&#123;</span> config.<span style="color: #000000;">setTransactional</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">true</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>Now I could more easily create EnvironmentConfigs and use them in code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">val</span> config <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> EnvConfig <span style="color: #0000ff; font-weight: bold;">with</span> Transactional <span style="color: #0000ff; font-weight: bold;">with</span> AllowCreate
<span style="color: #0000ff; font-weight: bold;">val</span> env <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Environment<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">new</span> File<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;.&quot;</span><span style="color: #F78811;">&#41;</span>, config<span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>I do however want to use the loan pattern with the Environment, as the Environment requires you to call close on it after use. So I thought instead off having a class for EnvironmentConfig, I could use have one for Environment itself, with an apply method. The Environment wouldn&#8217;t be created until it was required in apply, and using traits you could configure the Environment. Pretty happy with myself with the final solution for the Environment part:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> Env<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">val</span> path<span style="color: #000080;">:</span>File<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">trait</span> Transactional <span style="color: #0000ff; font-weight: bold;">extends</span> Env <span style="color: #F78811;">&#123;</span> config.<span style="color: #000000;">setTransactional</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">true</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span>
    <span style="color: #0000ff; font-weight: bold;">trait</span> AllowCreate <span style="color: #0000ff; font-weight: bold;">extends</span> Env <span style="color: #F78811;">&#123;</span> config.<span style="color: #000000;">setAllowCreate</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">true</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">val</span> config <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> EnvironmentConfig<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> apply<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>f<span style="color: #000080;">:</span><span style="color: #F78811;">&#40;</span>Environment<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> T<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
        <span style="color: #0000ff; font-weight: bold;">val</span> env <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Environment<span style="color: #F78811;">&#40;</span>path, config<span style="color: #F78811;">&#41;</span>
        <span style="color: #0000ff; font-weight: bold;">try</span> <span style="color: #F78811;">&#123;</span> f<span style="color: #F78811;">&#40;</span>env<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span> <span style="color: #0000ff; font-weight: bold;">finally</span> <span style="color: #F78811;">&#123;</span> env.<span style="color: #000000;">close</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span>
&nbsp;
    <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">object</span> EnvTest <span style="color: #0000ff; font-weight: bold;">extends</span> Application <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> env <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Env<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">new</span> File<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;.&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">with</span> Transactional <span style="color: #0000ff; font-weight: bold;">with</span> AllowCreate
    env <span style="color: #F78811;">&#123;</span> e <span style="color: #000080;">=&gt;</span>
        println<span style="color: #F78811;">&#40;</span>e<span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>Some handy links to BerkeleyDB Java Edition:</p>
<ul>
<li><a href="http://www.oracle.com/database/berkeley-db/je/index.html">BerkeleyDB Java Edition</a></li>
<li><a href="http://www.oracle.com/technology/documentation/berkeley-db/je/GettingStartedGuide/index.html">Getting started guide</a></li>
<li><a href="http://www.oracle.com/technology/documentation/berkeley-db/je/java/index.html">BerkeleyDB JavaDoc</a></li>
</ul>
<div id="e_clipper" style="border: 0pt none; margin: 10px; padding: 0pt; position: absolute; right: 0px; z-index: 100000; top: 0px;"></div>
<div id="evernote_clip_form" style="display: none;">
<form action="http://www.evernote.com/clip.action" accept-charset="UTF-8" enctype="application/x-www-form-urlencoded" method="post">
<input name="url" type="text" />
<input name="format" type="text" /><textarea name="body"></textarea></p>
<input name="title" type="text" />
<input name="quicknote" type="text" /></form>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.staale.org/2009/03/using-traits-to-configure-berkely-jdp.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

