<?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>Random Musings of an Insane Mind &#187; berkeleydb</title>
	<atom:link href="http://cd34.com/blog/tag/berkeleydb/feed/" rel="self" type="application/rss+xml" />
	<link>http://cd34.com/blog</link>
	<description>This is my blog, there are many others like it but this one is mine.</description>
	<lastBuildDate>Tue, 29 Jun 2010 04:22:00 +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>From File_DB to BerkeleyDB</title>
		<link>http://cd34.com/blog/programming/from-file_db-to-berkeleydb/</link>
		<comments>http://cd34.com/blog/programming/from-file_db-to-berkeleydb/#comments</comments>
		<pubDate>Fri, 03 Apr 2009 02:44:02 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[berkeleydb]]></category>
		<category><![CDATA[file_db]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://cd34.com/blog/?p=609</guid>
		<description><![CDATA[File_DB lacked decent file locking and concurrence.  I wasn&#8217;t really willing to move to MySQL which would have solved the problem, but, added a few minor inconveniences along the way.  I needed to store a few thousand bytes for a number of seconds.  While File_DB was wrapped with file locking and assisted by my own [...]]]></description>
			<content:encoded><![CDATA[<p>File_DB lacked decent file locking and concurrence.  I wasn&#8217;t really willing to move to MySQL which would have solved the problem, but, added a few minor inconveniences along the way.  I needed to store a few thousand bytes for a number of seconds.  While File_DB was wrapped with file locking and assisted by my own lock routine, it lacked truly concurrent access which I felt was leading to some of the issues we were seeing.</p>
<p>However, after a relatively painless conversion from File_DB to BerkeleyDB, it did not solve the problem completely.  The error I was addressing is now much harder to get to occur in normal use, but, I am able to reproduce it with a small test script.</p>
<p>The documentation for the perl methods to access BerkeleyDB are a bit sparse for setting up CDB, but, after digging through the documentation, and a few examples on the net, I ended up with some code that did indeed work consistently.</p>
<p>Since CDB isn&#8217;t documented very well, I ended up with the following script to test file locking and ensure things worked as expected.</p>
<pre>
#!/usr/bin/perl

use Data::Dumper;
use BerkeleyDB;

my %hash;
my $filename = "filt.db";
unlink $filename;

my $env = new BerkeleyDB::Env
   -Flags => DB_INIT_CDB|DB_INIT_MPOOL|DB_CREATE;

my $db = tie %hash, 'BerkeleyDB::Hash',
  -Filename   => $filename,
  -Flags        => DB_CREATE,
  -Env        => $env
or die "Cannot open $filename: $!\n" ;

my $lock = $db->cds_lock();

$hash{"abc"} = "def" ;
my $a = $hash{"ABC"} ;
# ...
sleep(10);

print Dumper(%hash);
$lock->cds_unlock();
undef $db ;
untie %hash ;
</pre>
<p>Path issues caused most of the issues as did previous tests not actually clearing out the _db* and filt.db file.  One test got CDB working, I modified a few things and didn&#8217;t realize I had actually broken CDB creation because the other files were still present.  Once I moved the script to another location, it failed to work.  A few quick modifications and I was back in business.</p>
<p>Perhaps this will save someone a few minutes of time debugging BerkeleyDB and Perl.</p>
<p>&#8212;&#8211;</p>
<p>Due to a logic error in the way I handled deletions to work around the fact that BerkeleyDB doesn&#8217;t allow you to delete a single record when you have a duplicate key, my code didn&#8217;t work properly in production.  After diagnosing that and fixing it with a little bit of code, 125 successive tests resulted in 100% completion.  I&#8217;ve pushed it to a few machines and will monitor it, but, I do believe that BerkeleyDB fixed the issues I was having with File_DB.</p>
]]></content:encoded>
			<wfw:commentRss>http://cd34.com/blog/programming/from-file_db-to-berkeleydb/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Embedded indexing versus Client/Server</title>
		<link>http://cd34.com/blog/programming/embedded-indexing-versus-clientserver/</link>
		<comments>http://cd34.com/blog/programming/embedded-indexing-versus-clientserver/#comments</comments>
		<pubDate>Sat, 28 Mar 2009 06:27:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[berkeleydb]]></category>
		<category><![CDATA[file_db]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[sqlite3]]></category>

		<guid isPermaLink="false">http://cd34.com/blog/?p=604</guid>
		<description><![CDATA[For a particular application, I require temporary persistent storage of some data.  That data consists of a key value and a payload.  That key value can be a dupe, which is what causes the problem. File_DB in perl handles duplicates and I can delete a key/value pair without too much difficulty.  However, file locking is [...]]]></description>
			<content:encoded><![CDATA[<p>For a particular application, I require temporary persistent storage of some data.  That data consists of a key value and a payload.  That key value can be a dupe, which is what causes the problem.</p>
<p>File_DB in perl handles duplicates and I can delete a key/value pair without too much difficulty.  However, file locking is not handled very well with File_DB which created concurrency issues with the threaded daemon.</p>
<p>Sqlite3 had no problem with duplicates, and could be compiled with the delete from/limit clause to easily handle duplicate keys.  Rather than recompile the packaged Sqlite3 in Debian, I made a slight modification to the code on my side so that I could do further testing.  Due to a few issues with threading and a potential issue with storing binary data and retrieving it in perl, I needed to reevaluate.</p>
<p>BerkeleyDB solves a few problems.  It supports concurrency, it supports proper file locking, but, a minor limitation is that duplicate keys are not handled well when you want to delete a key.  It&#8217;ll require a rewrite of some functionality to use BerkeleyDB, but, I believe that solution will provide the least potential for failures.</p>
<p>I could have use MySQL which I am very comfortable with, but, the storage of the data really only needs to be there for a few minutes in most cases, and the amount of data stored is 10-20K at most.  With MySQL&#8217;s client timeout, I couldn&#8217;t really guarantee everything would work every time without writing in considerable error checking.  While MySQL would handle everything perfectly, it was overkill for the task at hand.</p>
<p>I&#8217;m rewriting the File_DB methods to use BerkeleyDB and modifying the saved data slightly to work around the key delete issue.</p>
<p>It should work and should raise the reliability of this process from 99.2% to 99.9% which will be a considerable improvement.</p>
]]></content:encoded>
			<wfw:commentRss>http://cd34.com/blog/programming/embedded-indexing-versus-clientserver/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multithreaded madness</title>
		<link>http://cd34.com/blog/programming/multithreaded-madness/</link>
		<comments>http://cd34.com/blog/programming/multithreaded-madness/#comments</comments>
		<pubDate>Mon, 23 Mar 2009 06:09:28 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[berkeleydb]]></category>
		<category><![CDATA[file_db]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[sqlite3]]></category>

		<guid isPermaLink="false">http://cd34.com/blog/?p=602</guid>
		<description><![CDATA[An application I wrote long ago that used File_DB for short-term persistent multithreaded storage had a few issues with concurrency.  I debated rewriting the script to use BerkeleyDB which included proper file locking, but, decided to use Sqlite3 instead as it was closer to SQL and would eliminate a bit of code. The transition was [...]]]></description>
			<content:encoded><![CDATA[<p>An application I wrote long ago that used File_DB for short-term persistent multithreaded storage had a few issues with concurrency.  I debated rewriting the script to use BerkeleyDB which included proper file locking, but, decided to use Sqlite3 instead as it was closer to SQL and would eliminate a bit of code.</p>
<p>The transition was relatively easy.  Writing self-test functions worked well and a few bugs were dealt with along the way.  Most of the issues were getting used to Sqlite3&#8242;s quirks, but, all in all the code worked fine.  Multiple tests with multiple terminal windows resulted in everything working as expected including locking tables, concurrency issues and removing a logic error on the prior application.</p>
<p>First startup of the application resulted in a rather strange result which didn&#8217;t make a lot of sense.  I chalked that up to something I had done during testing, deleted the sqlite3 database file and restarted the application.</p>
<p>Success.</p>
<p>The application started, set it self as a daemon and detached from the terminal.  I sent a task to the daemon, and bam.  It seemed to work, it complained of a length error in the unpack which meant there was some data that didn&#8217;t get retrieved correctly from the database.  A second task was sent and the error received was even stranger.  Trying to connect to sqlite3 through the command line resulted in:</p>
<blockquote><p>sqlite&gt; select * from tasks;<br />
SQL error: database disk image is malformed</p></blockquote>
<p>Ok, something broke badly.</p>
<p>I checked and doublechecked my code with perl running in strict mode and could find nothing that would cause this.  It seems that the packaged version of sqlite3 in debian&#8217;s packaged perl is not compiled with threading enabled.</p>
<p>Oops.</p>
<p>I missed that when I was digging through the library configs and will have to build that package for testing.  I did want to avoid using the BerkeleyDB library and move to Sqlite3, but, I think in the interest of finishing this project a little sooner, I will rewrite the code and adjust the locking and use Sqlite3 in the future.</p>
<p>Sqlite3 works very well with SQLAlchemy and TurboGears, but, in this case, it didn&#8217;t quite solve the problem that I needed solved.</p>
]]></content:encoded>
			<wfw:commentRss>http://cd34.com/blog/programming/multithreaded-madness/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
