<?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>CodeWord: Apokalyptik &#187; MySQL</title>
	<atom:link href="http://blog.apokalyptik.com/category/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.apokalyptik.com</link>
	<description>The random things that spew forth from my brain...</description>
	<lastBuildDate>Thu, 04 Mar 2010 09:45:42 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=3.0-alpha</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>php open locking daemon</title>
		<link>http://blog.apokalyptik.com/2009/09/14/php-open-locking-daemon/</link>
		<comments>http://blog.apokalyptik.com/2009/09/14/php-open-locking-daemon/#comments</comments>
		<pubDate>Mon, 14 Sep 2009 09:19:49 +0000</pubDate>
		<dc:creator>apokalyptik</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[Excuses]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Web Stuff]]></category>

		<guid isPermaLink="false">http://blog.apokalyptik.com/?p=447</guid>
		<description><![CDATA[Don&#8217;t you hate that&#8230; When it&#8217;s 2:00am&#8230; and you really should be in bed&#8230; But your mind has hold of a problem, and wont let it go.  I have a project where it would be really handy for a process to be able to lock (arbitrary string identifier) and for another process to be [...]]]></description>
			<content:encoded><![CDATA[<p>Don&#8217;t you hate that&#8230; When it&#8217;s 2:00am&#8230; and you really should be in bed&#8230; But your mind has hold of a problem, and wont let it go.  I have <a href="http://code.svn.wordpress.org/jobs/">a project</a> where it would be really handy for a process to be able to lock (arbitrary string identifier) and for another process to be able to check whether (arbitrary string identifier) is still locked.  And the processes that do the locking can die&#8230; so the lock really needs to expire when they do.  I could use <a href="http://dev.mysql.com/doc/refman/5.0/en/miscellaneous-functions.html">MySQLs get_lock</a> but I&#8217;m already abusing the hell out of that for more distributed things (and since you cannot have more than one mysql named lock at a time per connection, i don&#8217;t think it would work here&#8230;) in the originating processes, and these locks are machine wide, not network wide&#8230;</p>
<p>I don&#8217;t like flock because you have to actually create a file to try and lock it leaving race conditions and the possibility of orphaned files on the file system which just sucks&#8230;  I thought about Memcached but I really need something that can be held open for long periods of inactivity and released if the client dies which precludes the infinite and the timed method of memcached value storage&#8230;</p>
<p>After some searching I found <a href="http://blitiri.com.ar/p/old/">old &#8212; Open Lock Daemon</a> which looked like a super good fit&#8230; Until I dug into the communication protocol&#8230; What a nightmare for wanting to lock a string&#8230; srsly.  So not being able to find anything (and apparently not being able to sleep until I had a satisfactory answer) I decided to write one. In PHP, naturally.  Weighing in at 180 lines I think it&#8217;s a pretty acceptable/workable first pass.  </p>
<p>[ edit: <a href="http://code.svn.wordpress.org/lockd/">code available here</a> ]</p>
<p><span id="more-447"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> lockd <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$sock</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$lockd_port</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2626</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$lockd_addr</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'127.0.0.1'</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$processing</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$stat_connects</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$stat_orphans</span>  <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$stat_commands</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$gs</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$rs</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$is</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$qs</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$answering</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$connections</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$locks</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">function</span> lockd<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$args</span><span style="color: #339933;">=</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$args</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$v</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;lockd_<span style="color: #006699; font-weight: bold;">$i</span>&quot;</span><span style="color: #339933;">;</span>
                        <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span><span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$v</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                <span style="color: #990000;">set_time_limit</span><span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                pcntl_signal<span style="color: #009900;">&#40;</span> SIGTERM<span style="color: #339933;">,</span>  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'sig_handler'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                pcntl_signal<span style="color: #009900;">&#40;</span> SIGHUP<span style="color: #339933;">,</span>   <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'sig_handler'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                pcntl_signal<span style="color: #009900;">&#40;</span> SIGUSR1<span style="color: #339933;">,</span>  <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'sig_handler'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>sock <span style="color: #339933;">=</span> <span style="color: #990000;">socket_create</span><span style="color: #009900;">&#40;</span>AF_INET<span style="color: #339933;">,</span> SOCK_STREAM<span style="color: #339933;">,</span> SOL_TCP<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #339933;">@</span><span style="color: #990000;">socket_set_option</span><span style="color: #009900;">&#40;</span> SOL_SOCKET<span style="color: #339933;">,</span> SO_REUSEADDR<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #339933;">@</span><span style="color: #990000;">socket_set_option</span><span style="color: #009900;">&#40;</span> SOL_SOCKET<span style="color: #339933;">,</span> SO_LINGER<span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">'l_onoff'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'l_linger'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">0</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #990000;">socket_set_nonblock</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>sock <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">socket_bind</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>sock<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>lockd_addr<span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>lockd_port <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
                        <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Could not bind socket...<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">socket_listen</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>sock<span style="color: #339933;">,</span> <span style="color: #cc66cc;">100</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
                        <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Could not listen on socket...<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000088;">$pid</span> <span style="color: #339933;">=</span> pcntl_fork<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$pid</span> <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span>
                        <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Error Forking...<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$pid</span> <span style="color: #009900;">&#41;</span>
                        <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Detaching...<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">echo</span> <span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #990000;">getmypid</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">.</span><span style="color: #990000;">chr</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #990000;">register_tick_function</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span><span style="color: #000088;">$this</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'process'</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000000; font-weight: bold;">declare</span><span style="color: #009900;">&#40;</span>ticks <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>accept_loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">function</span> accept_loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>answering <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">socket_accept</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>sock<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                                <span style="color: #666666; font-style: italic;">// echo &quot;Accepting connection #&quot;.count( $this-&amp;gt;connections ).&quot; $c &quot;.chr(10);</span>
                                <span style="color: #990000;">socket_set_block</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$c</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                <span style="color: #990000;">socket_set_option</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">,</span> SOL_SOCKET<span style="color: #339933;">,</span> SO_KEEPALIVE<span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                <span style="color: #990000;">socket_set_option</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">,</span> SOL_SOCKET<span style="color: #339933;">,</span> SO_RCVLOWAT<span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>connections<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;</span><span style="color: #000088;">$c</span><span style="color: #339933;">;</span>
                        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
                                <span style="color: #990000;">usleep</span><span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">50000</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>accept_loop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">function</span> sig_handler<span style="color: #009900;">&#40;</span> <span style="color: #000088;">$sig</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>answering <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #666666; font-style: italic;">//echo &quot;Got signal: $sig\r\n&quot;;</span>
                <span style="color: #666666; font-style: italic;">//echo &quot;\tKilling the listening socket\r\n&quot;;</span>
                <span style="color: #990000;">socket_close</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>sock <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>connections <span style="color: #b1b100;">as</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$c</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #666666; font-style: italic;">//echo &quot;\tKilling connection $i\r\n&quot;;</span>
                        <span style="color: #990000;">socket_close</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>connections<span style="color: #009900;">&#91;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #666666; font-style: italic;">//echo &quot;Sockets closed. Shutting down...&quot;;</span>
                <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">function</span> process<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #666666; font-style: italic;">// begin function locking</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>processing <span style="color: #009900;">&#41;</span>
                        <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>processing<span style="color: #339933;">++;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>processing <span style="color: #339933;">&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>processing<span style="color: #339933;">--;</span>
                        <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #666666; font-style: italic;">// end function locking</span>
&nbsp;
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>connections <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>processing<span style="color: #339933;">--;</span>
                        <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
&nbsp;
                <span style="color: #666666; font-style: italic;">// process reads</span>
                <span style="color: #000088;">$r</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>connections<span style="color: #339933;">;</span>
                <span style="color: #000088;">$w</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$e</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">//$this-&amp;gt;connections;</span>
&nbsp;
                <span style="color: #000088;">$num_changed_sockets</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #990000;">socket_select</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$r</span><span style="color: #339933;">,</span> <span style="color: #000088;">$w</span><span style="color: #339933;">,</span> <span style="color: #000088;">$e</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$num_changed_sockets</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">false</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>processing<span style="color: #339933;">--;</span>
                        <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$num_changed_sockets</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>                         <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>processing<span style="color: #339933;">--;</span>
                        <span style="color: #b1b100;">return</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$r</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$c</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">socket_get_option</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">,</span> SOL_SOCKET<span style="color: #339933;">,</span> SO_RCVBUF <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
                                <span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
                        <span style="color: #000088;">$n</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_search</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>connections <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #000088;">$d</span> <span style="color: #339933;">=</span> <span style="color: #990000;">socket_read</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4096</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #000088;">$d</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                                <span style="color: #666666; font-style: italic;">// echo &quot;&amp;lt; &amp;lt; #$c EOF\r\n&quot;;                                 socket_close( $this-&amp;gt;connections[$n] );</span>
                                <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>connections<span style="color: #009900;">&#91;</span><span style="color: #000088;">$n</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">array_keys</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>locks<span style="color: #339933;">,</span> <span style="color: #000088;">$c</span> <span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$lock</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                                        <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>stat_orphans<span style="color: #339933;">++;</span>
                                        <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>locks<span style="color: #009900;">&#91;</span><span style="color: #000088;">$lock</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                <span style="color: #009900;">&#125;</span>
                                <span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
&nbsp;
                        <span style="color: #009900;">&#125;</span>
&nbsp;
                        <span style="color: #000088;">$cmd</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$d</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
                        <span style="color: #000088;">$hash</span> <span style="color: #339933;">=</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$d</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$cmd</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                                <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'g'</span><span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">// get a lock</span>
                                        <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>gs<span style="color: #339933;">++;</span>
                                        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>locks<span style="color: #009900;">&#91;</span><span style="color: #000088;">$hash</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                                                <span style="color: #990000;">socket_write</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;0 Cannot Get Lock<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                                <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
                                        <span style="color: #009900;">&#125;</span>
                                        <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>locks<span style="color: #009900;">&#91;</span><span style="color: #000088;">$hash</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$c</span><span style="color: #339933;">;</span>
                                        <span style="color: #990000;">socket_write</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;1 Got Lock<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
                                <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'r'</span><span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">// release lock</span>
                                        <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>rs<span style="color: #339933;">++;</span>
                                        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>locks<span style="color: #009900;">&#91;</span><span style="color: #000088;">$hash</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;</span>amp<span style="color: #339933;">;&amp;</span>amp<span style="color: #339933;">;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>locks<span style="color: #009900;">&#91;</span><span style="color: #000088;">$hash</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> <span style="color: #000088;">$c</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                                                <span style="color: #990000;">unset</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>locks<span style="color: #009900;">&#91;</span><span style="color: #000088;">$hash</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                                <span style="color: #990000;">socket_write</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;1 Released Lock<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                                <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
                                        <span style="color: #009900;">&#125;</span>
                                        <span style="color: #990000;">socket_write</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;0 Cannot Release Lock<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
                                <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'i'</span><span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">// inspect lock</span>
                                        <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>is<span style="color: #339933;">++;</span>
                                        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #990000;">isset</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>locks<span style="color: #009900;">&#91;</span><span style="color: #000088;">$hash</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
                                                <span style="color: #990000;">socket_write</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;1 Locked<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                        <span style="color: #b1b100;">else</span>
                                                <span style="color: #990000;">socket_write</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;0 Not Locked<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
                                <span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'q'</span><span style="color: #339933;">:</span> <span style="color: #666666; font-style: italic;">// get system stats</span>
                                        <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>qs<span style="color: #339933;">++;</span>
                                        <span style="color: #990000;">socket_write</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$c</span><span style="color: #339933;">,</span> <span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
                                                <span style="color: #0000ff;">'conns'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>connections <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                                                <span style="color: #0000ff;">'locks'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>locks <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                                                <span style="color: #0000ff;">'orphans'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>stat_orphans<span style="color: #339933;">,</span>
                                                <span style="color: #0000ff;">'commands'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>stat_commands<span style="color: #339933;">,</span>
                                                <span style="color: #0000ff;">'command_g'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>gs<span style="color: #339933;">,</span>
                                                <span style="color: #0000ff;">'command_r'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>rs<span style="color: #339933;">,</span>
                                                <span style="color: #0000ff;">'command_i'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>is<span style="color: #339933;">,</span>
                                                <span style="color: #0000ff;">'command_q'</span> <span style="color: #339933;">=&amp;</span>gt<span style="color: #339933;">;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&amp;</span>gt<span style="color: #339933;">;</span>qs<span style="color: #339933;">,</span>
                                        <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                                        <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
                        <span style="color: #009900;">&#125;</span>
&nbsp;
                        <span style="color: #666666; font-style: italic;">// echo &quot;$c &amp;lt; &amp;lt; $d&quot;;                  }                 $this-&amp;gt;processing--;</span>
        <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.apokalyptik.com/2009/09/14/php-open-locking-daemon/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Bigger Aint Always Better</title>
		<link>http://blog.apokalyptik.com/2009/06/15/bigger-aint-always-better/</link>
		<comments>http://blog.apokalyptik.com/2009/06/15/bigger-aint-always-better/#comments</comments>
		<pubDate>Mon, 15 Jun 2009 19:44:38 +0000</pubDate>
		<dc:creator>apokalyptik</dc:creator>
				<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://blog.apokalyptik.com/?p=430</guid>
		<description><![CDATA[Recently I was troubleshooting some inefficiencies with the jobs systems locking and fetching queries at work.  Like a good little boy I, originally, came up with one index which satisfied all the queries that I needed to run against this particular critical table.

 &#40;`completed`,`heartbeat`,`priority`,`datacenter`,`worker`&#41; 
-- datetime,datetime,tinyint,varchar(16),varchar(255)

the query looked like this:

SELECT `id`,GET_LOCK&#40;CONCAT&#40;'foo_',`id`&#41;,0&#41; as mylock FROM [...]]]></description>
			<content:encoded><![CDATA[<p>Recently I was troubleshooting some inefficiencies with the jobs systems locking and fetching queries at work.  Like a good little boy I, originally, came up with one index which satisfied all the queries that I needed to run against this particular critical table.</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"> <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">`completed`</span><span style="color: #000033;">,</span><span style="color: #008000;">`heartbeat`</span><span style="color: #000033;">,</span><span style="color: #008000;">`priority`</span><span style="color: #000033;">,</span><span style="color: #008000;">`datacenter`</span><span style="color: #000033;">,</span><span style="color: #008000;">`worker`</span><span style="color: #FF00FF;">&#41;</span> 
<span style="color: #808080; font-style: italic;">-- datetime,datetime,tinyint,varchar(16),varchar(255)</span></pre></div></div>

<p>the query looked like this:</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">`id`</span><span style="color: #000033;">,</span><span style="color: #000099;">GET_LOCK</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'foo<span style="color: #008080; font-weight: bold;">_</span>'</span><span style="color: #000033;">,</span><span style="color: #008000;">`id`</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span><span style="color: #008080;">0</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">as</span> mylock <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #008000;">`foo<span style="color: #008080; font-weight: bold;">_</span>jobs`</span> 
<span style="color: #990099; font-weight: bold;">WHERE</span> <span style="color: #008000;">`completed`</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">'0000-00-00 00:00:00'</span> <span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #008000;">`worker`</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">''</span> <span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #008000;">`dirty`</span> <span style="color: #CC0099;">=</span> <span style="color: #008080;">0</span> 
<span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #000099;">IS_FREE_LOCK</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'foo<span style="color: #008080; font-weight: bold;">_</span>'</span><span style="color: #000033;">,</span><span style="color: #008000;">`id`</span><span style="color: #FF00FF;">&#41;</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099;">=</span> <span style="color: #008080;">1</span> <span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #008000;">`priority`</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">'0'</span> <span style="color: #990099; font-weight: bold;">LIMIT</span> <span style="color: #008080;">1</span></pre></div></div>

<p>This worked really well as long as the number of jobs in the table remained small.. But there was an event horizon of sorts&#8230; where after a certain number of rows (say 10k-25k) the query above took so long to get rows that the system could no longer catch up, but would fall invariably behind&#8230;  Apparently, the problem was that there was too much data in the key.  Because the requirements for these queries had changed slightly (due to other scaling improvements) I was able to simplify and break the indexes into parts which vastly outperform the previous index.</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">KEY</span> <span style="color: #008000;">`get<span style="color: #008080; font-weight: bold;">_</span>a<span style="color: #008080; font-weight: bold;">_</span>job`</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">`priority`</span><span style="color: #000033;">,</span><span style="color: #008000;">`workerpid`</span><span style="color: #FF00FF;">&#41;</span>
<span style="color: #808080; font-style: italic;">-- tinyint, int</span>
<span style="color: #990099; font-weight: bold;">KEY</span> <span style="color: #008000;">`janitor`</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">`completed`</span><span style="color: #000033;">,</span><span style="color: #008000;">`heartbeat`</span><span style="color: #000033;">,</span><span style="color: #008000;">`worker`</span><span style="color: #000033;">,</span><span style="color: #008000;">`dirty`</span><span style="color: #FF00FF;">&#41;</span>
<span style="color: #808080; font-style: italic;">-- datetime,datetime,varchar(255),tinyint</span></pre></div></div>

<p>This works because workerpid is enough to tell us whether a job definition is in process (or was, and has not been cleaned yet.)  and makes the following query run against a super small index&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #008000;">`id`</span><span style="color: #000033;">,</span><span style="color: #000099;">GET_LOCK</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'foo<span style="color: #008080; font-weight: bold;">_</span>'</span><span style="color: #000033;">,</span><span style="color: #008000;">`id`</span><span style="color: #FF00FF;">&#41;</span><span style="color: #000033;">,</span><span style="color: #008080;">0</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">as</span> mylock <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #008000;">`foo<span style="color: #008080; font-weight: bold;">_</span>jobs`</span>
<span style="color: #990099; font-weight: bold;">WHERE</span> <span style="color: #008000;">`priority`</span> <span style="color: #CC0099;">=</span> <span style="color: #008000;">'0'</span> <span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #008000;">`workerpid`</span> <span style="color: #CC0099; font-weight: bold;">IS</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> 
<span style="color: #CC0099; font-weight: bold;">AND</span> <span style="color: #000099;">IS_FREE_LOCK</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">CONCAT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">'foo<span style="color: #008080; font-weight: bold;">_</span>,`id`)) = 1 LIMIT 1</span></pre></div></div>

<p>If you&#8217;re interested in what dark monstrosity could possibly require queries like these (and are a brave PHP developer) I invite you to <a href="http://code.trac.wordpress.org/browser/jobs">check out the jobs system</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.apokalyptik.com/2009/06/15/bigger-aint-always-better/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Just what you need to know to write a CouchDB reduce function</title>
		<link>http://blog.apokalyptik.com/2009/02/18/just-what-you-need-to-know-to-write-a-couchdb-view/</link>
		<comments>http://blog.apokalyptik.com/2009/02/18/just-what-you-need-to-know-to-write-a-couchdb-view/#comments</comments>
		<pubDate>Wed, 18 Feb 2009 22:47:45 +0000</pubDate>
		<dc:creator>apokalyptik</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Random Thoughts]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Web Stuff]]></category>

		<guid isPermaLink="false">http://blog.apokalyptik.com/?p=408</guid>
		<description><![CDATA[Lets say you have the CouchDB classes (located here) all compiled together and included into your test.php script.  Lets also say that you have created a database with the built-in web ui called &#8220;testing&#8221;.  Finally let us say that your test.php has the following code in it, which would add a record to [...]]]></description>
			<content:encoded><![CDATA[<p>Lets say you have <a href="http://wiki.apache.org/couchdb/Getting_started_with_PHP">the CouchDB classes (located here)</a> all compiled together and included into your test.php script.  Lets also say that you have created a database with the built-in web ui called &#8220;testing&#8221;.  Finally let us say that your test.php has the following code in it, which would add a record to the db every time it is run. (i know that the data in the document serves no useful purpose&#8230; but really I just want to figure out this map/reduce thing so that I can make awesome views&#8230; so this suffices sufficiently.)</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">require_once</span> <span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span> <span style="color: #990000;">dirname</span><span style="color: #009900;">&#40;</span> <span style="color: #009900; font-weight: bold;">__FILE__</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/includes/couchdb.php'</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$couchdb</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> CouchDB<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'testing'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'localhost'</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5984</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$key</span> <span style="color: #339933;">=</span> <span style="color: #990000;">microtime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$couchdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">send</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'/'</span><span style="color: #339933;">.</span><span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'put'</span><span style="color: #339933;">,</span>
    <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span>
        <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">&quot;_id&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">&quot;time&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$key</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'md5'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">md5</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'sha1'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">sha1</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'crc'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">crc32</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBody</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>After running the code a bunch of times you would end up with a bunch of documents which look more or less like this:</p>
<p><a href="http://blog.apokalyptik.com/wp-content/uploads/2009/02/picture-1.png"><img width="600" src="http://blog.apokalyptik.com/wp-content/uploads/2009/02/picture-1.png" alt="picture-1" title="picture-1" class="alignnone size-medium wp-image-412" />(click for full size)</a></p>
<p>Now lets say you want to write a view that told you what the first characters of the _id were and how many documents share that first letter.  This is analogous to the following in MySQL</p>

<div class="wp_syntax"><div class="code"><pre class="mysql" style="font-family:monospace;"><span style="color: #990099; font-weight: bold;">SELECT</span> <span style="color: #000099;">LEFT</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">md5</span><span style="color: #000033;">,</span> <span style="color: #008080;">1</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">AS</span> <span style="color: #008000;">`lchar`</span><span style="color: #000033;">,</span> <span style="color: #000099;">count</span><span style="color: #FF00FF;">&#40;</span><span style="color: #000099;">md5</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #990099; font-weight: bold;">FROM</span> <span style="color: #008000;">`md5table`</span> <span style="color: #990099; font-weight: bold;">GROUP BY</span> <span style="color: #008000;">`lchar`</span></pre></div></div>

<p>Your map function is easy, because you dont have any selection criteria, so we process all rows</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>doc<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> emit<span style="color: #009900;">&#40;</span>doc._id<span style="color: #339933;">,</span>doc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span></pre></div></div>

<p>The reduce function is where the actual programming comes in&#8230;  And it seems there aren&#8217;t many well explained examples of exactly how to do this (I just brute forced it by trial and error)</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span> values<span style="color: #339933;">,</span> rereduce<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
    <span style="color: #003366; font-weight: bold;">var</span> output <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> rereduce <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
        <span style="color: #006600; font-style: italic;">// key is null, and values are values returned by previous calls</span>
	<span style="color: #006600; font-style: italic;">//</span>
	<span style="color: #006600; font-style: italic;">// see http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views</span>
	<span style="color: #006600; font-style: italic;">//</span>
	<span style="color: #006600; font-style: italic;">// essentially we are taking the previously reduced view, and the </span>
	<span style="color: #006600; font-style: italic;">// reduced view for new records, and we are reducing those two things</span>
	<span style="color: #006600; font-style: italic;">// together.  Summarizing two summaries, essentially</span>
        <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #000066; font-weight: bold;">in</span> values <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #006600; font-style: italic;">// here we have multiple prebuilt output objects and we're simply combining them</span>
  	    <span style="color: #006600; font-style: italic;">// just like below we have an array with a numeric id and an output object</span>
	    <span style="color: #006600; font-style: italic;">// </span>
	    <span style="color: #006600; font-style: italic;">// retrieve a summary</span>
            <span style="color: #003366; font-weight: bold;">var</span> vals <span style="color: #339933;">=</span> values<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> key <span style="color: #000066; font-weight: bold;">in</span> vals <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #006600; font-style: italic;">// debugging</span>
                <span style="color: #006600; font-style: italic;">// log(key);</span>
		<span style="color: #006600; font-style: italic;">// </span>
		<span style="color: #006600; font-style: italic;">// store in or increment our new output object </span>
                <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> output<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">==</span> undefined <span style="color: #009900;">&#41;</span>
                    output<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> vals<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
                <span style="color: #000066; font-weight: bold;">else</span>
                    output<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> output<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> vals<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #006600; font-style: italic;">// key is an array, which we dont care about, and values are the </span>
	<span style="color: #006600; font-style: italic;">// values returned by the map</span>
	<span style="color: #006600; font-style: italic;">//</span>
	<span style="color: #006600; font-style: italic;">// see http://wiki.apache.org/couchdb/Introduction_to_CouchDB_views</span>
	<span style="color: #006600; font-style: italic;">//</span>
	<span style="color: #006600; font-style: italic;">// we are taking each document and processing that, reducing it down</span>
	<span style="color: #006600; font-style: italic;">// to a summary object (output) for each of the rows passed</span>
        <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #000066; font-weight: bold;">in</span> values <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	    <span style="color: #006600; font-style: italic;">// we have an array, values, with numeric ids and a document objects</span>
	    <span style="color: #006600; font-style: italic;">//</span>
	    <span style="color: #006600; font-style: italic;">// retrieve a document</span>
            <span style="color: #003366; font-weight: bold;">var</span> doc <span style="color: #339933;">=</span> values<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
	    <span style="color: #006600; font-style: italic;">// get what we want from it, the first char of the md5</span>
            <span style="color: #003366; font-weight: bold;">var</span> key <span style="color: #339933;">=</span> doc._id.<span style="color: #660066;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	    <span style="color: #006600; font-style: italic;">// debugging</span>
            <span style="color: #006600; font-style: italic;">// log( key + &quot; :: &quot; + doc._id );</span>
	    <span style="color: #006600; font-style: italic;">//</span>
	    <span style="color: #006600; font-style: italic;">// store or increment the output object</span>
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span> output<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">!==</span> undefined <span style="color: #009900;">&#41;</span>
                output<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> output<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">+</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
            <span style="color: #000066; font-weight: bold;">else</span>
                output<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #006600; font-style: italic;">// done</span>
    <span style="color: #000066; font-weight: bold;">return</span> output<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>and in code, using a temporary view, ( if you used this view all the time you would want to make it permanent&#8230; but this is about how to lay out a reduce function, nothing more ) so request code that looks like this</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$view</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #0000ff;">'map'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'function(doc){ emit(doc._id,doc); }'</span><span style="color: #339933;">,</span>
    <span style="color: #0000ff;">'reduce'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'
        function(key, values, rereduce) { 
            var output = {};
            if ( rereduce ) { 
                // key is null, and values are values returned by previous calls
                for ( var i in values ) {
                    var vals = values[i];
                    for ( var key in vals ) {
                        // log(key);
                        if ( output[key] == undefined )
                            output[key] = vals[key];
                        else
                            output[key] = output[key] + vals[key];
                    }
                }
            } else {
                // key is an array, which we dont care about, and values are the values returneb by the map
                for ( var i in values ) {
                    var doc = values[i];
                    var key = doc._id.substr(0, 1);
                    // log( key + &quot; :: &quot; + doc._id );
                    if ( output[key] !== undefined )
                        output[key] = output[key] + 1;
                    else
                        output[key] = 1;
                }
            }
            return output;
        }
    '</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$couchdb</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">send</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/_temp_view'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'POST'</span><span style="color: #339933;">,</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$view</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getBody</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>would give you output that looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">stdClass Object
<span style="color: #009900;">&#40;</span>
    <span style="color: #009900;">&#91;</span>rows<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">Array</span>
        <span style="color: #009900;">&#40;</span>
            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> stdClass Object
                <span style="color: #009900;">&#40;</span>
                    <span style="color: #009900;">&#91;</span><span style="color: #990000;">key</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> 
                    <span style="color: #009900;">&#91;</span>value<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> stdClass Object
                        <span style="color: #009900;">&#40;</span>
                            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">15</span>
                            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">17</span>
                            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">16</span>
                            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">13</span>
                            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">27</span>
                            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">18</span>
                            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">6</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">26</span>
                            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">7</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">15</span>
                            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">8</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">18</span>
                            <span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">9</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">21</span>
                            <span style="color: #009900;">&#91;</span>a<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">12</span>
                            <span style="color: #009900;">&#91;</span>b<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">23</span>
                            <span style="color: #009900;">&#91;</span>c<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">20</span>
                            <span style="color: #009900;">&#91;</span>d<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">27</span>
                            <span style="color: #009900;">&#91;</span>e<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">28</span>
                            <span style="color: #009900;">&#91;</span>f<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">26</span>
                        <span style="color: #009900;">&#41;</span>
&nbsp;
                <span style="color: #009900;">&#41;</span>
&nbsp;
        <span style="color: #009900;">&#41;</span>
&nbsp;
<span style="color: #009900;">&#41;</span></pre></div></div>

<p>I hope this helps somebody out.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.apokalyptik.com/2009/02/18/just-what-you-need-to-know-to-write-a-couchdb-view/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>a dumbed down version of wpdb for sqlite</title>
		<link>http://blog.apokalyptik.com/2008/11/18/a-dumbed-down-version-of-wpdb-for-sqlite/</link>
		<comments>http://blog.apokalyptik.com/2008/11/18/a-dumbed-down-version-of-wpdb-for-sqlite/#comments</comments>
		<pubDate>Tue, 18 Nov 2008 20:08:47 +0000</pubDate>
		<dc:creator>apokalyptik</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[Random Thoughts]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Web Stuff]]></category>

		<guid isPermaLink="false">http://blog.apokalyptik.com/?p=307</guid>
		<description><![CDATA[I&#8217;ve been working, gradually, on a project using an sqlite3 database (for its convenience) and found myself missing the clean elegance of wpdb&#8230; so I implemented it.  It was actually really easy to do, and I figured I would throw it up here for anyone else wishing to use it.    The [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working, gradually, on a project using an sqlite3 database (for its convenience) and found myself missing the clean elegance of wpdb&#8230; so I implemented it.  It was actually really easy to do, and I figured I would throw it up here for anyone else wishing to use it.    The functionality that I build this around is obtainable here: http://php-sqlite3.sourceforge.net/pmwiki/pmwiki.php (don&#8217;t freak&#8230; its in apt&#8230;)  </p>
<p>With this I can focus on the sql, which is different enough, and not fumble over function names and such&#8230;  $db = new sqlite_wpdb($dbfile, 3); var_dump($db->get_results(&#8220;SELECT * FROM `mytable` LIMIT 5&#8243;));</p>
<p>the code is below&#8230; and hopefully not too mangled&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span> ?php
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> sqlite_wpdb <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$version</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$db</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">var</span> <span style="color: #000088;">$error</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">function</span> sqwpdb<span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$version</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> 
                <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$version</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #339933;">,</span> <span style="color: #000088;">$version</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$function</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;sqlite<span style="color: #006699; font-weight: bold;">{$version}</span>_open&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">function_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$function</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
                        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">file_exists</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
                        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span> <span style="color: #339933;">=</span> <span style="color: #339933;">@</span><span style="color: #000088;">$function</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$file</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
                        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">version</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$version</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fquery</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;sqlite<span style="color: #006699; font-weight: bold;">{$this-&gt;version}</span>_query&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ferror</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;sqlite<span style="color: #006699; font-weight: bold;">{$this-&gt;version}</span>_error&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">farray</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;sqlite<span style="color: #006699; font-weight: bold;">{$this-&gt;version}</span>_fetch_array&quot;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">function</span> escape<span style="color: #009900;">&#40;</span><span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #990000;">str_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;''&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">function</span> query<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">call_user_func</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">fquery</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #339933;">,</span> <span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
                        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">error</span> <span style="color: #339933;">=</span> <span style="color: #990000;">call_user_func</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">ferror</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">db</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">function</span> array_to_object<span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #990000;">is_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
                        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$array</span><span style="color: #339933;">;</span>
&nbsp;
                <span style="color: #000088;">$object</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> stdClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$array</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$idx</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$val</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000088;">$object</span><span style="color: #339933;">-&gt;</span><span style="color: #000088;">$idx</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$val</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #000088;">$object</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">function</span> get_results<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
                        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$rval</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">array_to_object</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">call_user_func</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">farray</span><span style="color: #339933;">,</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000088;">$rval</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #000088;">$rval</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">function</span> get_row<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span> <span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_results</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
                        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$results</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">function</span> get_var<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_val</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">function</span> get_val<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_row</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
                        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">get_object_vars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
                        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">function</span> get_col<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #000088;">$results</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_results</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
                        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$column</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$results</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$row</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">get_object_vars</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #339933;">!</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span>
                                <span style="color: #b1b100;">continue</span><span style="color: #339933;">;</span>
                        <span style="color: #000088;">$column</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array_shift</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                <span style="color: #b1b100;">return</span> <span style="color: #000088;">$column</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.apokalyptik.com/2008/11/18/a-dumbed-down-version-of-wpdb-for-sqlite/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Writing your own shell in php</title>
		<link>http://blog.apokalyptik.com/2008/08/03/writing-your-own-shell-in-php/</link>
		<comments>http://blog.apokalyptik.com/2008/08/03/writing-your-own-shell-in-php/#comments</comments>
		<pubDate>Sun, 03 Aug 2008 20:47:48 +0000</pubDate>
		<dc:creator>apokalyptik</dc:creator>
				<category><![CDATA[API]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[Random Thoughts]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Web Stuff]]></category>

		<guid isPermaLink="false">http://blog.apokalyptik.com/?p=295</guid>
		<description><![CDATA[I&#8217;ve always wanted to write my own simple shell in php.  Call me a glutin for punishment, but it seems like something that a lot of people could use to be able to do&#8230; If your web app had a command line interface for various things&#8230; like looking up stats, or users, or suspending naughty [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve always wanted to write my own simple shell in php.  Call me a glutin for punishment, but it seems like something that a lot of people could use to be able to do&#8230; If your web app had a command line interface for various things&#8230; like looking up stats, or users, or suspending naughty accounts, or whatever&#8230;. wouldnt that be cool and useful?  Talk about geek porn.  Anyways this this morning I got around to tinkering with the idea, and here is what i came up with&#8230; It&#8217;s rough, and empty, but its REALLY easy to extend and plug into any php application.</p>
<pre>apokalyptik:~/phpshell$ ./shell.php

/home/apokalyptik/phpshell &gt; hello

hi there

/home/apokalyptik/phpshell &gt; hello world

hi there world

/home/apokalyptik/phpshell &gt; cd ..

/home/apokalyptik/ &gt; cd phpshell

/home/apokalyptik/phpshell &gt; ls

shell.php

/home/apokalyptik//phpshell &gt; exit

apokalyptik:~/phpshell$ ./shell.php</pre>
<p>See the source here: <a href="http://blog.apokalyptik.com/files/phpshell/shell.phps">shell.phps</a></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.apokalyptik.com/2008/08/03/writing-your-own-shell-in-php/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>so-you-wanna-see-an-image</title>
		<link>http://blog.apokalyptik.com/2007/10/10/so-you-wanna-see-an-image/</link>
		<comments>http://blog.apokalyptik.com/2007/10/10/so-you-wanna-see-an-image/#comments</comments>
		<pubDate>Wed, 10 Oct 2007 20:21:01 +0000</pubDate>
		<dc:creator>apokalyptik</dc:creator>
				<category><![CDATA[Amazon AWS]]></category>
		<category><![CDATA[Blogging]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Web Stuff]]></category>

		<guid isPermaLink="false">http://blog.apokalyptik.com/2007/10/10/so-you-wanna-see-an-image/</guid>
		<description><![CDATA[We&#8217;ve been asked how we manage serving files from Amazons very cool S3 service at WordPress.com&#8230; This is how. (covering a requested image already stored on S3, not the upload -&#62; s3 process)
A request comes into pound for a file. Pound hashes the hostname (via a custom patch which we have not, but may, release) [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://photomatt.net/2007/10/09/s3-news/" target="_blank">We&#8217;ve been asked</a> how we manage serving files from <a href="http://s3.amazonaws.com/" target="_blank">Amazons very cool S3 service</a> at <a href="http://wordpress.com/" target="_blank">WordPress.com</a>&#8230; This is how. (covering a requested image already stored on S3, not the upload -&gt; s3 process)</p>
<p>A request comes into <a href="http://www.apsis.ch/pound/" target="_blank">pound</a> for a file. Pound hashes the hostname (via a custom patch which we have not, but may, release) , to determine which of several backend servers the request should hit. Pound forwards the request to that server. This, of course, means that a given blog always serves from the same backend server. The only exception to the afore-mentioned rule is if that server is, for some reason, unavailable in which case it picks another server to serve that hostname from temporarily.</p>
<p>The request then comes into <a href="http://varnish.projects.linpro.no/" target="_blank">varnishd</a> on the backend servers. The varnishd daemon checks its 300Gb worth of files cache and (for the sake of this example) finds nothing (hey, new images are uploaded all the time!) Varnishd then checks with the web server (running on the same machine, just bound to a different IP/Port#) and that request is handled by a custom script.</p>
<p>So, a http daemon on the same backend server runs the file request. The custom script checks the DB to gather information on the file (specifically which DC’s it is in, size, mod time, and whether its deleted or not) all this info is saved in memcached for 5 minutes. The script increments and checks the “hawtness” (term courtesy of <a href="http://barry.wordpress.com/" target="_blank">Barry</a>) of the file in <a href="http://www.danga.com/memcached/" target="_blank">memcached</a> (if the file has been accessed over a certain # of times it is then deemed “hawt”, and a special header is sent with the response telling varnishd to put the file into its cache. When that happens the request would be served directly by varnishd in the previous paragraph and never hit the httpd or this script again (or at least not until the cache entry expires.)) At this point, assuming the file should exist (deleted = 0 in the files db) we fetch the file from a backend source.</p>
<p>Which backend source depends on where it is available. The order of preference is as follows: Always fetch from Amazon S3 if the file lives there (no matter what, the following preferences only ever occur if, for some reason, s3 = 0 in the files db), and if that fails fetch from the one files server we still have (which has larger slower disks, and is used for archiving purposes and fault tolerance only)</p>
<p>After fetching the file from the back end… the custom script hands the data and programatically generated headers to the http daemon, which hands the data to varnishd, varnishd hands the data to pound, pound hands the data to the requesting client, and the image appears in the web browser.</p>
<p>And there was much rejoicing (yay.)</p>
<p>For the visual people among us who like visuals and stuff… (I like visuals…) here goes…</p>
<p><img src="http://tmp.thoughtcandy.com/images/photo/ada/16677/imagerequest-small.jpg" /></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.apokalyptik.com/2007/10/10/so-you-wanna-see-an-image/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>I have to say</title>
		<link>http://blog.apokalyptik.com/2007/10/06/i-have-to-say/</link>
		<comments>http://blog.apokalyptik.com/2007/10/06/i-have-to-say/#comments</comments>
		<pubDate>Sun, 07 Oct 2007 04:08:21 +0000</pubDate>
		<dc:creator>apokalyptik</dc:creator>
				<category><![CDATA[Blogging]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[MySQL]]></category>

		<guid isPermaLink="false">http://blog.apokalyptik.com/2007/10/06/i-have-to-say/</guid>
		<description><![CDATA[this is amazing: http://jan.kneschke.de/2007/10/7/wormhole-index-reads and I cant wait to try it somewhere!
]]></description>
			<content:encoded><![CDATA[<p>this is amazing: <a href="http://jan.kneschke.de/2007/10/7/wormhole-index-reads" target="_blank">http://jan.kneschke.de/2007/10/7/wormhole-index-reads</a> and I cant wait to try it somewhere!</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.apokalyptik.com/2007/10/06/i-have-to-say/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP CLI Status Indicator</title>
		<link>http://blog.apokalyptik.com/2007/05/21/php-cli-status-indicator/</link>
		<comments>http://blog.apokalyptik.com/2007/05/21/php-cli-status-indicator/#comments</comments>
		<pubDate>Mon, 21 May 2007 21:17:32 +0000</pubDate>
		<dc:creator>apokalyptik</dc:creator>
				<category><![CDATA[CLI]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[Random Thoughts]]></category>
		<category><![CDATA[Software Development]]></category>

		<guid isPermaLink="false">http://blog.apokalyptik.com/2007/05/21/php-cli-status-indicator/</guid>
		<description><![CDATA[Most times when people write command line scripts they just let the output flow down the screen as a status indicator, or just figure &#8220;it&#8217;s done when it&#8217;s done&#8221; But sometimes it would be nice to have a simple clean status indicator, allowing you to monitor progress and gauge time-to-completion.  This is actually very [...]]]></description>
			<content:encoded><![CDATA[<p>Most times when people write command line scripts they just let the output flow down the screen as a status indicator, or just figure &#8220;it&#8217;s done when it&#8217;s done&#8221; But sometimes it would be nice to have a simple clean status indicator, allowing you to monitor progress and gauge time-to-completion.  This is actually very easy to accomplish.  Simply use \r instead of \r\n in your output.  Obviously the example below is very simplified, and this can be applied in a much more sophisticated fashion.  But it works.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>font style<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;color: darkred&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #000088;">$row_count</span> <span style="color: #339933;">=</span> get_total_rows_for_processing<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;&lt;/</span>font<span style="color: #339933;">&gt;</span>
<span style="color: #000088;">$limit</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">10000</span><span style="color: #339933;">;</span>
<span style="color: #339933;">&lt;</span>font style<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;color: darkred&quot;</span><span style="color: #339933;">&gt;</span>echo <span style="color: #0000ff;">&quot;&amp;#92;r&amp;#92;n[  0%]&quot;</span><span style="color: #339933;">;&lt;/</span>font<span style="color: #339933;">&gt;</span>
<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$i</span><span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$row_count</span><span style="color: #339933;">;</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$i</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$limit</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000088;">$query</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;SELECT * FROM table LIMIT <span style="color: #006699; font-weight: bold;">{$limit}</span> OFFSET <span style="color: #006699; font-weight: bold;">{$i}</span>&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// do whatever</span>
  <span style="color: #339933;">&lt;</span>font style<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;color: darkred&quot;</span><span style="color: #339933;">&gt;</span><span style="color: #000088;">$pct</span> <span style="color: #339933;">=</span> <span style="color: #990000;">round</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$i</span><span style="color: #339933;">+</span><span style="color: #000088;">$offset</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">/</span><span style="color: #000088;">$row_count</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">100</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$pct</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">10</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&amp;#92;r[  <span style="color: #006699; font-weight: bold;">$pct</span>%]&quot;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span> <span style="color: #000088;">$pct</span> <span style="color: #339933;">&amp;</span>lt<span style="color: #339933;">;</span> <span style="color: #cc66cc;">100</span> <span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&amp;#92;r[ <span style="color: #006699; font-weight: bold;">$pct</span>%]&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&amp;#92;r[<span style="color: #006699; font-weight: bold;">$pct</span>%]&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>font<span style="color: #339933;">&gt;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;</span>font style<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;color: darkred&quot;</span><span style="color: #339933;">&gt;</span>echo <span style="color: #0000ff;">&quot;&amp;#92;r[100%]&amp;#92;r&amp;#92;n&quot;</span><span style="color: #339933;">;&lt;/</span>font<span style="color: #339933;">&gt;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.apokalyptik.com/2007/05/21/php-cli-status-indicator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Any&#8230; good&#8230; php devs out there looking for some side work?</title>
		<link>http://blog.apokalyptik.com/2007/05/17/any-good-php-devs-out-there-looking-for-some-side-work/</link>
		<comments>http://blog.apokalyptik.com/2007/05/17/any-good-php-devs-out-there-looking-for-some-side-work/#comments</comments>
		<pubDate>Fri, 18 May 2007 03:04:57 +0000</pubDate>
		<dc:creator>apokalyptik</dc:creator>
				<category><![CDATA[Business]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Random Thoughts]]></category>
		<category><![CDATA[Security]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Web Stuff]]></category>

		<guid isPermaLink="false">http://blog.apokalyptik.com/2007/05/17/any-good-php-devs-out-there-looking-for-some-side-work/</guid>
		<description><![CDATA[I know a group of guys looking to do some cool stuff who could use a few good contractors.  Drop me an e-mail with maybe a sample or something cool you did in php and I&#8217;ll pass it on.
apokalyptik apokalyptik com &#8212; Subject: &#8220;PHP Consulting&#8221; (I&#8217;ll likely completely overlook your mail if you use [...]]]></description>
			<content:encoded><![CDATA[<p>I know a group of guys looking to do some cool stuff who could use a few good contractors.  Drop me an e-mail with maybe a sample or something cool you did in php and I&#8217;ll pass it on.</p>
<p>apokalyptik apokalyptik com &#8212; Subject: &#8220;PHP Consulting&#8221; (I&#8217;ll likely completely overlook your mail if you use some subject not starting with that string)</p>
<p>Cheers</p>
<p>DK</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.apokalyptik.com/2007/05/17/any-good-php-devs-out-there-looking-for-some-side-work/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>tags, items, users &#8211; loose the joins &#8211; gain the freedom.</title>
		<link>http://blog.apokalyptik.com/2007/03/20/tags-items-users-loose-the-joins-gain-the-freedom/</link>
		<comments>http://blog.apokalyptik.com/2007/03/20/tags-items-users-loose-the-joins-gain-the-freedom/#comments</comments>
		<pubDate>Tue, 20 Mar 2007 08:08:20 +0000</pubDate>
		<dc:creator>apokalyptik</dc:creator>
				<category><![CDATA[Amazon AWS]]></category>
		<category><![CDATA[Business]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[MySQL]]></category>
		<category><![CDATA[Personal]]></category>
		<category><![CDATA[Random Thoughts]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Web Stuff]]></category>

		<guid isPermaLink="false">http://blog.apokalyptik.com/2007/03/20/tags-items-users-loose-the-joins-gain-the-freedom/</guid>
		<description><![CDATA[Long time readers of this blog will assert that I have no problem presenting an unpopular opinion, and/or sticking my foot in my mouth.  Some times both at once! (&#8220;But wait&#8230; there&#8217;s more!&#8221;) So when N. Shah asks me how he should split his database (a tags table, items table, and users table) I [...]]]></description>
			<content:encoded><![CDATA[<p>Long time readers of this blog will assert that I have no problem presenting an unpopular opinion, and/or sticking my foot in my mouth.  Some times both at once! (&#8220;But wait&#8230; there&#8217;s more!&#8221;) So when N. Shah asks me how he should split his database (a tags table, items table, and users table) I say: The answer is in the question.</p>
<p>You have only one database</p>
<p>Lets drop the pretense folks. Lets come back to the real world. This is the web 2.0 world. Data is growing at a seriously exponential.  And desperate times call for desperate measures.</p>
<p>Joins are nice. They&#8217;re pretty. They&#8217;re convenient. They keep us from having to think very much.  But they do NOT promote using commodity hardware for your databases. They just don&#8217;t. No, really, an in-database join chains you to an in-database solution. You *could* keep upgrading and upgrading&#8230; faster processors&#8230; larger disks&#8230; faster raid&#8230; And then you move to buying SAN&#8217;s and you&#8217;re talking about some serious cash for that cache.  Or&#8230; You think about things differently. You put in a little work up front. And you break the mold. Because one database ties you to one server. And that, my friends, is the problem.</p>
<p>So, N, here&#8217;s my answer: Split your database once, and then your databases once.</p>
<blockquote><p>DB:/users</p>
<p>DB:/items</p>
<p>DB:/tags</p></blockquote>
<p>becomes</p>
<blockquote><p>DBTags:/tags</p>
<p>DBUsers:/users</p>
<p>DBItems:/items</p></blockquote>
<p>And then</p>
<blockquote><p>DBUsers:/users</p></blockquote>
<p>Pretty simple&#8230; users tend to be a small table, and keeping them in one place makes a lot of sense here. HOWEVER. depending on your architecture and uses you could easily split the users as we do the tags (not items) below.</p>
<blockquote><p>DBItems:/</p>
<ul>
<li>items_id_ending_in_0</li>
<li>items_id_ending_in_1</li>
<li>items_id_ending_in_2</li>
<li>items_id_ending_in_3</li>
<li>items_id_ending_in_4</li>
<li>items_id_ending_in_5</li>
<li>items_id_ending_in_6</li>
<li>items_id_ending_in_7</li>
<li>items_id_ending_in_8</li>
<li>items_id_ending_in_9</li>
</ul>
</blockquote>
<p>again, pretty simple. you have your run of the mill integer item id&#8217;s split them by the last digit of your item id, and you can reduce the footprint of any one table to 1/10th of the whole dataset size</p>
<blockquote><p>DBTags:/</p>
<ul>
<li>tags_crc_ending_in_0</li>
<li>tags_crc_ending_in_1</li>
<li>tags_crc_ending_in_2</li>
<li>tags_crc_ending_in_3</li>
<li>tags_crc_ending_in_4</li>
<li>tags_crc_ending_in_5</li>
<li>tags_crc_ending_in_6</li>
<li>tags_crc_ending_in_7</li>
<li>tags_crc_ending_in_8</li>
<li>tags_crc_ending_in_9</li>
</ul>
</blockquote>
<p>Now here is a little bit of voodoo.  You have these tags, and tags are words.  And I like numbers. Numbers make life easy.  So by creating a CRC32 hash of the word, and storing it with the tag {id|tag|crc332} you can quickly reverse the tag to an id, and then go find items with that tag id associated, while still retaining the ability to split the db by powers of 10.</p>
<p>You can still use your join tables items_to_users, and tags_t_items, these tables consisting of ints take up almost _NO_ space whatsoever, and so can go where convenient (if you query items for users more than users for items, then put the join table in the users db) but you cant actually preform in-server full joins any longer. Heck you can even keep two copies of the join data, items_to_tags in the items dbs, and tags_to_items in the items dbs.</p>
<p>So, like many things in life, going cheaper meant going a bit harder. But what did we gain? Well lets assume 10 ec2 instances&#8230;</p>
<blockquote><p>Ec2a</p>
<ul>
<li>users (w)</li>
<li>items 0-1 (w)</li>
<li>tags 0-1 (w)</li>
</ul>
<p>Ec2b</p>
<ul>
<li>items 2-3 (w)</li>
<li>tags 2-3 (w)</li>
</ul>
<p>Ec2c</p>
<ul>
<li>items 4-5 (w)</li>
<li>tags 4-5 (w)</li>
</ul>
<p>Ec2d</p>
<ul>
<li>items 6-7 (w)</li>
<li>tags 6-7 (w)</li>
</ul>
<p>Ec2e</p>
<ul>
<li>items 8-9 (w)</li>
<li>tags 8-9 (w)</li>
</ul>
<p>Ec2f</p>
<ul>
<li>items 0-1 (r)</li>
<li>tags 0-1 (r)</li>
</ul>
<p>Ec2g</p>
<ul>
<li>users (r)</li>
<li>items 2-3 (r)</li>
<li>tags 2-3 (r)</li>
</ul>
<p>Ec2h</p>
<ul>
<li>items 4-5 (r)</li>
<li>tags 4-5 (r)</li>
</ul>
<p>Ec2i</p>
<ul>
<li>items 6-7 (r)</li>
<li>tags 6-7 (r)</li>
</ul>
<p>Ec2j</p>
<ul>
<li>items 8-9 (r)</li>
<li>tags 8-9 (r)</li>
</ul>
</blockquote>
<p>So thats a total of about&#8230; oh&#8230; 1.6 terrabytes of space&#8230; 18gb of RAM,  17Ghz of processor speed, and an inherently load balanced set of database instances. And when you need to grow? split by the last 2 (16TB) digits, 3(160Tb) digits, 4(1,600TB) digits&#8230;</p>
<p>So, now that you&#8217;ve read to the bottom. It&#8217;s 1:00am, way past my bed time. Remember that when designing a database you &#8212; above all &#8212; need to listen to your data. Nobody will come up with a solution that perfectly fits your problem (thats why its called &#8220;your problem&#8221;) but techniques can be applied, and outlooks can be leveraged.</p>
<p>Disclaimer: some or all of this might be wrong, there may be better ways, dont blame me. I&#8217;m sleep-typing <img src='http://blog.apokalyptik.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.apokalyptik.com/2007/03/20/tags-items-users-loose-the-joins-gain-the-freedom/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
