<?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>Brain Lint &#187; Programming</title>
	<atom:link href="http://www.monkeyatlarge.com/archives/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.monkeyatlarge.com</link>
	<description>Random musings on life, technology and other miscellany.</description>
	<lastBuildDate>Wed, 07 Apr 2010 01:50:30 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Plotting Game by Game Winning Percentages</title>
		<link>http://www.monkeyatlarge.com/archives/2010/04/06/plotting-game-by-game-winning-percentages/</link>
		<comments>http://www.monkeyatlarge.com/archives/2010/04/06/plotting-game-by-game-winning-percentages/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 01:49:53 +0000</pubDate>
		<dc:creator>James Kebinger</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[visualization]]></category>

		<guid isPermaLink="false">http://www.monkeyatlarge.com/?p=353</guid>
		<description><![CDATA[Another baseball season is upon us, and fans are quick to project the results of their favorite team from the first few games. I wondered if many teams tend to arrive at a winning percentage near their whole-season results, and then oscillate around a little, versus having early results that differ substantially from the final [...]]]></description>
			<content:encoded><![CDATA[<p>Another baseball season is upon us, and fans are quick to project the results of their favorite team from the first few games. I wondered if many teams tend to arrive at a winning percentage near their whole-season results, and then oscillate around a little, versus having early results that differ substantially from the final winning percentage.</p>
<p>I created an interactive plot to look at the results for the 2009 season, team by team.</p>
<p>Take Boston. Seen below, Boston started slow, but pretty quickly arrived at their ultimate winning level.<br />
<img src="http://www.monkeyatlarge.com/blog/wp-content/uploads/2010/04/bos-chart3.png" alt="" title="bos-chart" width="499" height="248" class="aligncenter size-full wp-image-359" /></p>
<p>On the other hand, the Yankees started even slower, and in fact didn&#8217;t reach their ultimate winning level until very late in the season.<br />
<img src="http://www.monkeyatlarge.com/blog/wp-content/uploads/2010/04/nyy-chart.png.png" alt="" title="nyy-chart.png" width="498" height="249" class="aligncenter size-full wp-image-357" /></p>
<p>See the results for the other teams <a href="http://www.monkeyatlarge.com/projects/baseball-game-winning-pct/">on the visualization page</a>.</p>
<p>The visualization was created using Javascript and the <a href="http://raphaeljs.com/">Raphaël JS </a> library.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.monkeyatlarge.com/archives/2010/04/06/plotting-game-by-game-winning-percentages/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Multiple Phrase Search in PostgreSQL</title>
		<link>http://www.monkeyatlarge.com/archives/2010/01/17/multiple-phrase-search-in-postgresql/</link>
		<comments>http://www.monkeyatlarge.com/archives/2010/01/17/multiple-phrase-search-in-postgresql/#comments</comments>
		<pubDate>Mon, 18 Jan 2010 01:34:27 +0000</pubDate>
		<dc:creator>James Kebinger</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.monkeyatlarge.com/?p=341</guid>
		<description><![CDATA[Tsearch, the full text search engine in PostgreSql, is great at rapidly searching for keywords (and combinations of keywords) in large bodies of text. It does not, however, excel at matching multi-word phrases. There are some techniques to work around that to let your application leverage tsearch to find phrases. 
Before I go on, I&#8217;ll [...]]]></description>
			<content:encoded><![CDATA[<p>Tsearch, the full text search engine in PostgreSql, is great at rapidly searching for keywords (and combinations of keywords) in large bodies of text. It does not, however, excel at matching multi-word phrases. There are some techniques to work around that to let your application leverage tsearch to find phrases. </p>
<p>Before I go on, I&#8217;ll credit Paul Sephton&#8217;s <a href="http://linuxgazette.net/164/sephton.html">Understanding Full Text Search</a> for opening my eyes to some of the possibilities to enable phrase search on top of tsearch&#8217;s existing capabilities.</p>
<p>Tsearch operates on tsvectors and tsqueries. Tsvectors are a bag of words like structure &#8211; a list of the unique words appearing in a piece of text, along with their positions in the text. Searches are performed constructing a tsquery, which is boolean expression combining words with AND(&#038;), OR(|), and NOT(!) operators, then comparing the tsquery against candidate tsvectors with the @@ operator.</p>
<pre class="brush: sql; light: true;">
select * from articles where to_tsvector('english',articles.body) @@ 'meatball &amp; sub';
</pre>
<p>will match articles where the the body contains the word meatball and the word sub. If there&#8217;s an index on to_tsvector(&#8216;english&#8217;,articles.body), this query is a very efficient index lookup.</p>
<h3>
Single Phrase Search</h3>
<p>Now how do we match articles with the phrase &#8220;meatball sub&#8221;, anywhere in the article&#8217;s body? Doing the naive query</p>
<pre class="brush: sql; light: true;">
select * from articles where body like '%meatball sub%'
</pre>
<p>will work, but it will be slow because the leading wildcard kills any chance of using an index on that column. What we can do to make this go fast is the following:</p>
<pre class="brush: sql; light: true;">
select * from articles where to_tsvector('english',articles.body) @@ 'meatball &amp; sub' AND body like '%meatball sub%'
</pre>
<p>This will use the full text index to find the set of articles where the body has both words, then that (presumably) smaller set of articles can be scanned for the words together.</p>
<h3>Multi Phrase Search</h3>
<p>It&#8217;s simple to extend the above query to match two phrases:</p>
<pre class="brush: sql; light: true;">
select * from articles where to_tsvector('english',articles.body) @@ 'meatball &amp; sub &amp; ham &amp; sandwich' AND body like '%meatball sub%' AND body like '%ham sandwich%';
</pre>
<p>That query can be tightened up using postgres&#8217;s support for arrays:</p>
<pre class="brush: sql; light: true;">
select * from articles where to_tsvector('english',articles.body) @@ 'meatball &amp; sub &amp; ham &amp; sandwich' AND body like ALL('{&quot;%meatball sub%&quot;,&quot;%ham sandwich%&quot;}')
</pre>
<p>Stepping back a bit, let&#8217;s define create a table called &#8220;concepts&#8221; to allow users of an application to store searches on lists of phrases, and let&#8217;s also allow the user to specify that all phrases must match, or just one of them.</p>
<pre class="brush: sql; light: true;">
CREATE TABLE concepts
(
   id serial,
   match_all boolean,
   phrases character varying[],
   query tsquery
)
</pre>
<p>Now we can specify and execute that previous search this way:</p>
<pre class="brush: sql; light: true;">
insert into concepts(match_all,phrases,query) VALUES(TRUE,'{&quot;%meatball sub%&quot;,&quot;%ham sandwich%&quot;}','meatball &amp; sub &amp; ham &amp; sandwich');
select articles.*, join concepts on (concepts.query @@ to_tsvector(body)) AND ((match_all AND body like ALL(phrases)) OR (not match_all AND body like ANY(phrases)));
</pre>
<p>Where this approach really shines compared with an external text search tools is aggregate queries like counting up matching articles by date. </p>
<pre class="brush: sql; light: true;">
select count(distinct articles.id), articles.date from articles join concepts on (concepts.query @@ to_tsvector(body)) AND ((match_all AND body like ALL(phrases)) OR (not match_all AND body like ANY(phrases)))
group by articles.date
</pre>
<p>The logic to combine lists of phrases into the appropriate query based on the desire to match any or all of the phrases is easy to write at the application layer.  It&#8217;s desirable not to have to include the wildcards into the phrase array, and it&#8217;s easy to write a function to do that at runtime.</p>
<pre class="brush: sql; light: true;">
CREATE OR REPLACE FUNCTION wildcard_wrapper(list varchar[]) RETURNS varchar[] AS $$
      DECLARE
       return_val varchar[];
      BEGIN
        for idx in 1 .. array_upper(list, 1)
        loop
          return_val[idx] := '%' || list[idx] || '%';
        end loop;
        return return_val;
      END;
      $$ LANGUAGE plpgsql;
</pre>
<p>With that function good to go we can make that long query just a little longer:</p>
<pre class="brush: sql; light: true;">
select count(distinct articles.id), articles.date from articles join concepts on (concepts.query @@ to_tsvector(body)) AND ((match_all AND body like ALL(wildcard_wrapper(phrases))) OR (not match_all AND body like ANY(wildcard_wrapper(phrases))))
group by articles.date
</pre>
<p>It&#8217;s straightforward to collapse most, if not all of the sql on clause into a plpgsql function call without adversely affecting the query plan &#8211; it&#8217;s important that the tsvector index be involved in the query for adequate performance.</p>
<h3>Further Work</h3>
<p>This approach works well for lists of phrases. To support boolean logic on phrases, one approach might be to compile the request down to a tsquery as above, along with a regular expression to winnow down the matches to those containing the phrases.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.monkeyatlarge.com/archives/2010/01/17/multiple-phrase-search-in-postgresql/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Deferring Index costs for table to table copies in PostgreSQL</title>
		<link>http://www.monkeyatlarge.com/archives/2009/04/14/deferring-index-costs-for-table-to-table-copies-in-postgresql/</link>
		<comments>http://www.monkeyatlarge.com/archives/2009/04/14/deferring-index-costs-for-table-to-table-copies-in-postgresql/#comments</comments>
		<pubDate>Tue, 14 Apr 2009 14:27:55 +0000</pubDate>
		<dc:creator>James Kebinger</dc:creator>
				<category><![CDATA[Ruby]]></category>
		<category><![CDATA[SQL]]></category>

		<guid isPermaLink="false">http://www.monkeyatlarge.com/?p=318</guid>
		<description><![CDATA[When bulk copying data to a table, it is much faster if the destination table is index and constraint free, because it is cheaper to build an index once than maintain it over many inserts. For postgres, the pg_restore and SQL COPY commands can do this, but they both require that data be copied from [...]]]></description>
			<content:encoded><![CDATA[<p>When bulk copying data to a table, it is much faster if the destination table is index and constraint free, because it is cheaper to build an index once than maintain it over many inserts. For postgres, the pg_restore and SQL COPY commands can do this, but they both require that data be copied from the filesystem rather than directly from another table.</p>
<p>For table to table copying (and transformations) the situation isn&#8217;t as straight-forward. Recently I was working on a problem where we needed to perform some poor-man&#8217;s <a href="http://en.wikipedia.org/wiki/Extract,_transform,_load">ETL</a>, copying and transforming data between tables in different schemas. Since some of the destination tables were heavily indexed(including a full text index) the task took quite a while. In talking with a colleague about the problem, we came up with the idea of dropping the indexes and constraints prior to the data load, and restoring them afterwards. </p>
<p>First stop: how to get the DDL for indices on a table in postgres? Poking around the postgres catalogs, I managed to find a function pg_get_indexdef that would return the DDL for an index. Combining that with a query I found in a forum somewhere and altered, I came up with this query to get the names and DDL of all the indices on a table. (this one excludes the primary key index)</p>
<p><script src="http://gist.github.com/94854.js"></script></p>
<p>With that and the query to do the same for constraints its straightforward to build a helper function that will get the DDL for all indices and constraints, drop them, yield to evaluate a block and then restore the indices and constraints. The method is below: </p>
<p><script src="http://gist.github.com/95196.js"></script></p>
<p>Use of the function would look like the snippet below. This solution would also allow for arbitrarily complex transformations in Ruby as well as pure SQL.</p>
<p><script src="http://gist.github.com/94867.js"></script></p>
<p>For my task loading and transforming data into about 20 tables, doing this reduced the execution time by two-thirds. Of course, your mileage may vary depending how heavily indexed your destination tables are.</p>
<p>Here&#8217;s the whole module:</p>
<p><script src="http://gist.github.com/94853.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://www.monkeyatlarge.com/archives/2009/04/14/deferring-index-costs-for-table-to-table-copies-in-postgresql/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PNG Thumbnails for PDF files. Take two</title>
		<link>http://www.monkeyatlarge.com/archives/2008/10/07/png-thumbnails-for-pdf-files-take-two/</link>
		<comments>http://www.monkeyatlarge.com/archives/2008/10/07/png-thumbnails-for-pdf-files-take-two/#comments</comments>
		<pubDate>Wed, 08 Oct 2008 02:16:20 +0000</pubDate>
		<dc:creator>James Kebinger</dc:creator>
				<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.monkeyatlarge.com/?p=305</guid>
		<description><![CDATA[Updating my previous post, I finished up the work of extending attachment_fu to optionally create PNG thumbnails of updated PDF files. Check out the fork on github
]]></description>
			<content:encoded><![CDATA[<p>Updating my <a href="http://www.monkeyatlarge.com/archives/2008/09/16/creating-thumbnails-of-pdfs-with-attachment_fu/">previous post</a>, I finished up the work of extending attachment_fu to optionally create PNG thumbnails of updated PDF files. Check out the <a href="http://github.com/jkebinger/attachment_fu/tree/master">fork on github</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.monkeyatlarge.com/archives/2008/10/07/png-thumbnails-for-pdf-files-take-two/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Creating thumbnails of PDFs with attachment_fu</title>
		<link>http://www.monkeyatlarge.com/archives/2008/09/16/creating-thumbnails-of-pdfs-with-attachment_fu/</link>
		<comments>http://www.monkeyatlarge.com/archives/2008/09/16/creating-thumbnails-of-pdfs-with-attachment_fu/#comments</comments>
		<pubDate>Tue, 16 Sep 2008 20:56:33 +0000</pubDate>
		<dc:creator>James Kebinger</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[attachment_fu]]></category>
		<category><![CDATA[pdf]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[thumbnails]]></category>

		<guid isPermaLink="false">http://www.monkeyatlarge.com/?p=301</guid>
		<description><![CDATA[
We needed to create some thumbnails from uploading PDF files for a new site feature &#8211; We&#8217;re using attachment_fu which doesn&#8217;t support that (yet?), but we&#8217;re using RMagick as our processor and it understands PDF files.

 I came up with the hack below (warning, first draft, only briefly tested) which works without having to modify [...]]]></description>
			<content:encoded><![CDATA[<p>
We needed to create some thumbnails from uploading PDF files for a new site feature &#8211; We&#8217;re using <a href="http://github.com/technoweenie/attachment_fu/tree/master">attachment_fu </a>which doesn&#8217;t support that (yet?), but we&#8217;re using <a href="http://rmagick.rubyforge.org/">RMagick</a> as our processor and it understands PDF files.
</p>
<p> I came up with the hack below (warning, first draft, only briefly tested) which works without having to modify the attachment_fu plugin itself. One day I&#8217;ll loop back and figure out a cleaner way to do this and see which of attachment_fu&#8217;s other image processors can even support pdfs.
</p>
<p>
There are three methods to override to make a go of this:</p>
<ol>
<li>self.image? :  consider pdf files as an image so thumbnail process will happen</li>
<li>thumbnail_name_for : change the extension of the saved thumbnail filename to png</li>
<li>resize_image: override to change format via block passed to to_blob</li>
</ol>
<p>Apologies for the crappy source formatting, I have to install a plugin to do that well one of these days</p>
<p><code><br />
###Hacks to allow creation of png thumbnails for pdf uploads - depends on RMagic being the configured processor<br />
## likely very fragile</p>
<p>def self.image?(content_type)<br />
(content_types +  ['application/pdf']).include?(content_type)<br />
end</p>
<p>alias_method <img src='http://www.monkeyatlarge.com/blog/wp-includes/images/smilies/icon_surprised.gif' alt=':o' class='wp-smiley' /> riginal_thumbnail_name_for, :thumbnail_name_for<br />
def thumbnail_name_for(thumbnail=nil)<br />
return original_thumbnail_name_for(thumbnail) unless (content_type == 'application/pdf' &amp;&amp; !thumbnail.blank?)<br />
basename = filename.gsub /\.\w+$/ do |s|<br />
ext = s; ''<br />
end<br />
"#{basename}_#{thumbnail}.png"<br />
end<br />
#copied from rmagick_processor with change in last few lines<br />
def resize_image(img, size)<br />
size = size.first if size.is_a?(Array) &amp;&amp; size.length == 1 &amp;&amp; !size.first.is_a?(Fixnum)<br />
if size.is_a?(Fixnum) || (size.is_a?(Array) &amp;&amp; size.first.is_a?(Fixnum))<br />
size = [size, size] if size.is_a?(Fixnum)<br />
img.thumbnail!(*size)<br />
else<br />
img.change_geometry(size.to_s) { |cols, rows, image| image.resize!(cols&lt;1 ? 1 : cols, rows&lt;1 ? 1 : rows) }<br />
end<br />
img.strip! unless attachment_options[:keep_profile]<br />
if content_type == 'application/pdf' # here force the output format to PNG if its a pdf<br />
self.temp_path = write_to_temp_file(img.to_blob {self.format = 'PNG'})<br />
else<br />
self.temp_path = write_to_temp_file(img.to_blob)<br />
end<br />
end</p>
<p></code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.monkeyatlarge.com/archives/2008/09/16/creating-thumbnails-of-pdfs-with-attachment_fu/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Livepipe UI controls for JS</title>
		<link>http://www.monkeyatlarge.com/archives/2008/08/21/livepipe-ui-controls-for-js/</link>
		<comments>http://www.monkeyatlarge.com/archives/2008/08/21/livepipe-ui-controls-for-js/#comments</comments>
		<pubDate>Fri, 22 Aug 2008 01:43:02 +0000</pubDate>
		<dc:creator>James Kebinger</dc:creator>
				<category><![CDATA[Javascript]]></category>

		<guid isPermaLink="false">http://www.monkeyatlarge.com/?p=294</guid>
		<description><![CDATA[I&#8217;ve been pretty pleased so far with the popup window control from LivePipe. It plays nice with Prototype and is easy to style with regular CSS. We had considered using Prototype Window but I was put off that all their default styles looks like operating system windows and restyling their windows required a table and [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been pretty pleased so far with the <a href="http://livepipe.net/control/window">popup window control</a> from <a href="http://livepipe.net/">LivePipe</a>. It plays nice with Prototype and is easy to style with regular CSS. We had considered using <a href="http://prototype-window.xilinus.com/">Prototype Window</a> but I was put off that all their default styles looks like operating system windows and restyling their windows required a table and 9 images.</p>
<p>I&#8217;d recommend anyone looking for a popup window solution at least consider Livepipe. There are downsides however, chiefly that the project is pretty immature &#8211; technically I suppose this is an alpha release since Beta One is being worked on, so the community remains small. While there are some folks already submitting patches, progress on merging the patches is alarmingly slow, as one can see from their <a href="http://livepipe.lighthouseapp.com/projects/11811-livepipe-ui/overview">lighthouse page</a>.</p>
<p>If you&#8217;re doing RESTful stuff in Rails however, you will need the contents of <a href="http://livepipe.lighthouseapp.com/projects/11811/tickets/10-control-window-s-default-request-method#ticket-10-1">ticket #10</a> which modifies the popup window to accept an option to use different HTTP verbs.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.monkeyatlarge.com/archives/2008/08/21/livepipe-ui-controls-for-js/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scriptaculous docs</title>
		<link>http://www.monkeyatlarge.com/archives/2008/08/21/scriptaculous-docs/</link>
		<comments>http://www.monkeyatlarge.com/archives/2008/08/21/scriptaculous-docs/#comments</comments>
		<pubDate>Fri, 22 Aug 2008 01:29:27 +0000</pubDate>
		<dc:creator>James Kebinger</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.monkeyatlarge.com/?p=290</guid>
		<description><![CDATA[For all the complaining I often do about the poor documentation of the scriptaculous project, I finally did something to help that today, creating (very thin) documentation for their new (if released in January is new) Effect.Tween function here on their github wiki.
I was creating a method to scroll the viewport so that the contents [...]]]></description>
			<content:encoded><![CDATA[<p>For all the complaining I often do about the poor documentation of the <a href="http://script.aculo.us/">scriptaculous</a> project, I finally did something to help that today, creating (very thin) documentation for their new (if released in January is new) Effect.Tween function <a href="http://github.com/madrobby/scriptaculous/wikis/effecttween">here</a> on their github wiki.</p>
<p>I was creating a method to scroll the viewport so that the contents of an AJAX-loaded div would be fully visible on the screen &#8211; the (still undocumented) Effect.ScrollTo doesn&#8217;t quite do it because it doesn&#8217;t consider the height of the element it scrolls to, but in doing so I stumbled over Tween in the code. Once the math to figure out how much scrolling is needed, its easy to use Effect.Tween to smoothly scroll the window by repeatedly calling window.scrollBy();</p>
<p>This certainly isn&#8217;t rocket science, but here&#8217;s an outline of how to do it (this code only deals with downward vertical scrolling):</p>
<pre style="padding-left: 30px;">var elementHeight = element.getHeight();
var screenHeight = document.viewport.getHeight();
var elementScreenPos =element.viewportOffset()[1];
var amountToScroll = elementHeight - (screenHeight - elementScreenPos);
if (amountToScroll &gt; 0){
var scrollPos = document.viewport.getScrollOffsets().top;
new Effect.Tween(null,scrollPos,scrollPos+amountToScroll,{},function(n) { window.scrollTo(0,n);});
}
}</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.monkeyatlarge.com/archives/2008/08/21/scriptaculous-docs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The short circuit article</title>
		<link>http://www.monkeyatlarge.com/archives/2008/04/10/the-short-circuit-article/</link>
		<comments>http://www.monkeyatlarge.com/archives/2008/04/10/the-short-circuit-article/#comments</comments>
		<pubDate>Thu, 10 Apr 2008 20:56:27 +0000</pubDate>
		<dc:creator>James Kebinger</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.monkeyatlarge.com/?p=255</guid>
		<description><![CDATA[About three and a half years ago I co-wrote an article named &#8220;Increase stability and responsiveness by short-circuiting code&#8221; for IBM&#8217;s developer works site, and for some reason in the past few days it has repeatedly asked for attention (&#8220;hi this is 2004, your article is on the line, and its woefully dated&#8221;). First, the [...]]]></description>
			<content:encoded><![CDATA[<p>About three and a half years ago I co-wrote an <a href="http://www.ibm.com/developerworks/web/library/wa-shortcir/">article named &#8220;Increase stability and responsiveness by short-circuiting code&#8221;</a> for IBM&#8217;s developer works site, and for some reason in the past few days it has repeatedly asked for attention (&#8220;hi this is 2004, your article is on the line, and its woefully dated&#8221;). First, the one page abstract we submitted fell out of a book on my bookshelf, then I was asked about it at at least one interview. Sadly, that was one of the top results for my name in google for a while and people still find it.<br />
I figure its about time to revisit, and disavow, the implementation in the article, if that isn&#8217;t already obvious to anyone.</p>
<p>The idea was to provide a way to time-box operations that could take an unknown amount of time. In this way for example, a web page that must be displayed faster than a certain time can be guaranteed to run in that time, if it can do without the results of operations that take too long to execute.<br />
One obvious flaw is that the code creates LOTS of new threads for a short period of time. It should have used a thread pool to reduce that churn.<br />
The best reason not to use that code is that Java 1.5 introduced a whole set of Concurrency utilities. <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/ExecutorService.html">ExecutorService</a> and <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/Future.html">Future</a>. There are lots of examples about, so you can check them out.</p>
<p>The high level view is that you package your functionality in a Runnable or Callable (depending if you need to return a result), submit it to an instance of ExecutorService to run. It will return a Future object which can be queried to get the result. One can call get on the Future class, which will return right away if the task is done execuiting, or block until the sooner of a specified timeout or the task completing. Even better, one can submit multiple tasks at once with invokeAll(..) and that will return when all tasks are complete or the timeout has expired.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.monkeyatlarge.com/archives/2008/04/10/the-short-circuit-article/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Interviews on Trivia</title>
		<link>http://www.monkeyatlarge.com/archives/2008/04/07/interviews-on-trivia/</link>
		<comments>http://www.monkeyatlarge.com/archives/2008/04/07/interviews-on-trivia/#comments</comments>
		<pubDate>Mon, 07 Apr 2008 16:11:16 +0000</pubDate>
		<dc:creator>James Kebinger</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Random]]></category>

		<guid isPermaLink="false">http://www.monkeyatlarge.com/?p=253</guid>
		<description><![CDATA[I&#8217;ve started interviewing again now that I should be finished with my Master&#8217;s degree in a month or so. I&#8217;m reminded again of the wide range of interview styles people use. My least favorite is the trivia test. This seems to happen more often with Java-related job interviews than Ruby-related ones.
I may never understand why [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve started interviewing again now that I should be finished with my Master&#8217;s degree in a month or so. I&#8217;m reminded again of the wide range of interview styles people use. My least favorite is the trivia test. This seems to happen more often with Java-related job interviews than Ruby-related ones.</p>
<p>I may never understand why any employer would value memorizing the Java API over being able to reference the docs and know where to find things.</p>
<p>I had such an interview just last week. Here are some of those questions</p>
<ul>
<li>How do you execute a PL-SQL stored procedure from JDBC?</li>
<li>How do you import classes into the classpath of a JSP page? (apparently &#8216;no one in their right mind does that anymore&#8217; isn&#8217;t a good answer to this one)</li>
</ul>
<p>Who memorizes that stuff?</p>
<p>My favorite question of all was this : what are the two conditions under which a finally block is not called. I got one of them, (System.exit()) but the interviewer wouldn&#8217;t even tell me the other one (&#8220;You won&#8217;t learn that way&#8221;). I googled it later to find the answer not well defined. One of the ways I saw mentioned was the thread &#8220;dying&#8221; but Thread.stop() is severely deprecated so that shouldn&#8217;t ever happen. The other answer I saw floating around doesn&#8217;t really fit &#8211; when a exception is thrown from the finally block it doesn&#8217;t complete, but the finally block is still called.</p>
<p>I was talking about this with Frank and he came up with another way: infinite loop in the try block. I then thought of calling PowerSystem.getMainPower().setPosition(OFF).</p>
<p>Now I can&#8217;t wait to get that question again!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.monkeyatlarge.com/archives/2008/04/07/interviews-on-trivia/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Ruby operator precedence (the ors and ands of it)</title>
		<link>http://www.monkeyatlarge.com/archives/2008/03/14/ruby-operator-precedence-the-ors-and-ands-of-it/</link>
		<comments>http://www.monkeyatlarge.com/archives/2008/03/14/ruby-operator-precedence-the-ors-and-ands-of-it/#comments</comments>
		<pubDate>Fri, 14 Mar 2008 18:22:10 +0000</pubDate>
		<dc:creator>James Kebinger</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.monkeyatlarge.com/archives/2008/03/14/ruby-operator-precedence-the-ors-and-ands-of-it/</guid>
		<description><![CDATA[I found out (by introducing a bug into the application I&#8217;ve been working on) that &#8220;or&#8221; and &#8220;&#124;&#124;&#8221; do not have equal precedence in Ruby. 
More importantly, the assignment operator &#8220;=&#8221; has higher precedence than &#8220;or&#8221; so that means that while the expression

>> foo = nil &#124;&#124; 2
=> 2
>> foo
=> 2

results in foo being assigned [...]]]></description>
			<content:encoded><![CDATA[<p>I found out (by introducing a bug into the application I&#8217;ve been working on) that &#8220;or&#8221; and &#8220;||&#8221; do not have equal precedence in Ruby. </p>
<p>More importantly, the assignment operator &#8220;=&#8221; has higher precedence than &#8220;or&#8221; so that means that while the expression</p>
<p><code><br />
>> foo = nil || 2<br />
=> 2<br />
>> foo<br />
=> 2<br />
</code></p>
<p>results in foo being assigned the value 2 as you might expect, the following expression leaves foo assigned the value nil.</p>
<p><code><br />
>> foo = nil or 2<br />
=> 2<br />
>> foo<br />
=> nil<br />
</code></p>
<p>This is well covered ground online (see <a href="http://blog.jayfields.com/2007/08/ruby-operator-precedence-of-and-which.html">this post</a>) but I was surprised that this oddity didn&#8217;t warrant an explicit mention in the operator precedence section of the Pickaxe book.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.monkeyatlarge.com/archives/2008/03/14/ruby-operator-precedence-the-ors-and-ands-of-it/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
