<?xml version="1.0" encoding="utf-8"?><?xml-stylesheet title="XSL formatting" type="text/xsl" href="http://blog.pwkf.org/feed/rss2/xslt" ?><rss version="2.0"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wfw="http://wellformedweb.org/CommentAPI/"
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
  <title>Personal Workflow Blog</title>
  <link>http://blog.pwkf.org/</link>
  <atom:link href="http://blog.pwkf.org/feed/rss2" rel="self" type="application/rss+xml"/>
  <description>Some thoughts I encounter during my working day in the J2EE land. Originally a blog about PWKF, an easy-to-use workflow solution, but I have less time to work on PWKF that what I had before as real life kicked in !</description>
  <language>en</language>
  <pubDate>Fri, 05 Mar 2010 07:21:00 +0100</pubDate>
  <copyright>(c) 2006-2009 - Steve Schnepp</copyright>
  <docs>http://blogs.law.harvard.edu/tech/rss</docs>
  <generator>Dotclear</generator>
  
    
  <item>
    <title>Free Exception lunch : Use unchecked exceptions, but still announce which ones you might throw.</title>
    <link>http://blog.pwkf.org/post/2010/02/You-Shall-Declare-The-Unchecked-Exceptions-You-Might-Throw</link>
    <guid isPermaLink="false">urn:md5:03e06c61165fcdfa2d6e0d529dfbeb36</guid>
    <pubDate>Sat, 20 Feb 2010 16:01:00 +0100</pubDate>
    <dc:creator>Steve Schnepp</dc:creator>
        <category>java</category>
            
    <description>    &lt;p&gt;In a previous article I choosed my side : &lt;a href=&quot;http://blog.pwkf.org/post/2009/07/Checked-or-Unchecked-Exceptions-for-Legacy-Code&quot;&gt;Unchecked
Exceptions are much simpler to use&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;But, on the other side of this great division, there is a very valid point :
&lt;strong&gt;You usually declare checked exceptions&lt;/strong&gt;. Sure it's possible to
only declare to throw &lt;code&gt;Exception&lt;/code&gt;, but that would defeat the whole
purpose of using checked exceptions.&lt;/p&gt;
&lt;p&gt;The nicest thing is that you can also have a custom exception hierarchy, but
based on &lt;code&gt;RuntimeException&lt;/code&gt; instead of a plain
&lt;code&gt;Exception&lt;/code&gt;. This way it's like in C++. Everything might be thrown,
and you don't &lt;strong&gt;need&lt;/strong&gt; to handle them.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Declaring them&lt;/strong&gt;, on the other side, is very interesting
because you are &lt;strong&gt;documenting your interface&lt;/strong&gt; for almost
&lt;strong&gt;free&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;So, use unchecked exceptions to free yourself of the checked catch-slavery,
but still declare the custom ones you might throw.&lt;/p&gt;</description>
    
    
    
          <comments>http://blog.pwkf.org/post/2010/02/You-Shall-Declare-The-Unchecked-Exceptions-You-Might-Throw#comment-form</comments>
      <wfw:comment>http://blog.pwkf.org/post/2010/02/You-Shall-Declare-The-Unchecked-Exceptions-You-Might-Throw#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.pwkf.org/feed/atom/comments/436982</wfw:commentRss>
      </item>
    
  <item>
    <title>Immutability of an URL</title>
    <link>http://blog.pwkf.org/post/2010/02/Immutability-of-an-URL</link>
    <guid isPermaLink="false">urn:md5:d309c7c0d59a5f7e7cbcd5c896b3f2cd</guid>
    <pubDate>Sat, 20 Feb 2010 15:18:00 +0100</pubDate>
    <dc:creator>Steve Schnepp</dc:creator>
        <category>general</category>
            
    <description>    &lt;p&gt;In the pure spirit of &lt;a href=&quot;http://www.javapractices.com/topic/TopicAction.do?Id=211&quot; hreflang=&quot;en&quot;&gt;Data
is King&lt;/a&gt; I think that URL should &lt;strong&gt;never&lt;/strong&gt; change. Even the W3C
agrees with their &lt;a href=&quot;http://www.w3.org/Provider/Style/URI.html&quot; hreflang=&quot;en&quot;&gt;Cool URIs don't change&lt;/a&gt; article.&lt;/p&gt;
&lt;p&gt;But we all know that in IT &lt;em&gt;never&lt;/em&gt; is only &lt;em&gt;not in the foreseen
future&lt;/em&gt;. So URL &lt;strong&gt;do&lt;/strong&gt; change, at least after a while, and
usually for technical reasons&lt;sup&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2010/02/#pnote-397657-1&quot; id=&quot;rev-pnote-397657-1&quot; name=&quot;rev-pnote-397657-1&quot;&gt;1&lt;/a&gt;]&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;Since you can update your website to update the URLs, but the inbound link
cannot be easily updated. To handle this need, the HTTP protocol has specified
the 301 response code.&lt;/p&gt;
&lt;p&gt;The solution is that the site should remember all the urls that it generated
and redirects accordingly. This way you'll never loose a potential reader to
the infamous 404 (this page does not exist).&lt;/p&gt;
&lt;p&gt;Some sites even try to approximate the page on a custom 404 page. That's
another reason to have user-friendly urls : to be able to hint your reader to
appropriate pages in case you don't find his initial destination.&lt;/p&gt;
&lt;p&gt;Sadly, this redirect behavior isn't supported by my blog engine
(dotclear)... That's for the &lt;a href=&quot;http://en.wikipedia.org/wiki/Eating_one%27s_own_dog_food&quot; hreflang=&quot;en&quot;&gt;eat
your own dog's food&lt;/a&gt;, but I'm looking forward to do it on my current
blogging platform.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot;&gt;
&lt;h4&gt;Notes&lt;/h4&gt;
&lt;p&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2010/02/#rev-pnote-397657-1&quot; id=&quot;pnote-397657-1&quot; name=&quot;pnote-397657-1&quot;&gt;1&lt;/a&gt;] upgrade to another blog engine...&lt;/p&gt;
&lt;/div&gt;</description>
    
    
    
          <comments>http://blog.pwkf.org/post/2010/02/Immutability-of-an-URL#comment-form</comments>
      <wfw:comment>http://blog.pwkf.org/post/2010/02/Immutability-of-an-URL#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.pwkf.org/feed/atom/comments/397657</wfw:commentRss>
      </item>
    
  <item>
    <title>Avoid the Preprocessor : Use ''Compile-Time Polymorphism'' for Cross-platform Development</title>
    <link>http://blog.pwkf.org/post/2009/12/Avoid-the-Preprocessor-Use-Compile-Time-Polymorphism-for-Cross-platform-Development</link>
    <guid isPermaLink="false">urn:md5:ac68468a97cc5a2146661e506b24a7c2</guid>
    <pubDate>Fri, 11 Dec 2009 23:00:00 +0100</pubDate>
    <dc:creator>Steve Schnepp</dc:creator>
        <category>c++</category>
            
    <description>&lt;p&gt;When writing portable cross-platform code, don't litter your code with
preprocessor macros, use &lt;em&gt;compile-time polymorphism&lt;/em&gt; instead.&lt;/p&gt;
&lt;p&gt;A flexible build system will enable you to use advanced OOP-like
compile-time polymorphism. That way you can hide all the specifics of the
different platform behind an interface firewall. It is the usual way that most
cross-platform toolkits and frameworks (such as QT, GTK or wxWidgets) are
designed.&lt;/p&gt;    &lt;p&gt;We'll take a very simple file I/O subsystem here as an example
(&lt;code&gt;open&lt;/code&gt;, &lt;code&gt;close&lt;/code&gt;). We'll also only take Linux and
Windows, since that's usually the 2 most common platforms people want to
develop for&lt;sup&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/12/#pnote-467153-1&quot; id=&quot;rev-pnote-467153-1&quot; name=&quot;rev-pnote-467153-1&quot;&gt;1&lt;/a&gt;]&lt;/sup&gt;.&lt;/p&gt;
&lt;h2&gt;The usual abstraction with the preprocessor&lt;/h2&gt;
&lt;p&gt;Usually preprocessor &lt;code&gt;#ifdef&lt;/code&gt;s are used to compile specific parts
for the underlying OS.&lt;/p&gt;
&lt;p&gt;The file &lt;code&gt;fileiosys.cpp&lt;/code&gt; looks&lt;sup&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/12/#pnote-467153-2&quot; id=&quot;rev-pnote-467153-2&quot; name=&quot;rev-pnote-467153-2&quot;&gt;2&lt;/a&gt;]&lt;/sup&gt; like the one below
:&lt;/p&gt;
&lt;pre&gt;
fileiosys_filedesc open(char* filename) {
#ifdefine __WIN32__
    return OpenFile(
        filename, 
        GENERIC_READ | GENERIC_WRITE, 
        FILE_SHARE_READ | FILE_SHARE_WRITE, 
        NULL,
    );  
#else // UNIX
    return open(filename, O_READ | O_WRITE, 0666);
#endif
}

int close (fileiosys_filedesc fd) {
#ifdefine __WIN32__
    return CloseHandle(fd) ? 0 : -1;
#else // UNIX
    return close(fd);
#endif
}
&lt;/pre&gt;
&lt;p&gt;The header &lt;code&gt;fileiosys.h&lt;/code&gt; looks then like :&lt;/p&gt;
&lt;pre&gt;
// Define a custom file descriptor
#ifdefine __WIN32__
    #define fileiosys_filedesc HANDLE
#else // UNIX
    #define fileiosys_filedesc int
#endif

fileiosys_filedesc open(char* filename); 
int close(fileiosys_filedesc fd); 
&lt;/pre&gt;
&lt;h2&gt;Avoid the preprocessor Compile-time polymorphism&lt;/h2&gt;
&lt;p&gt;As the previous little example, the code isn't really easy to read. You have
to always think which environnement you are in. All the specifics are
multiplexed in the same file, and the programmer has to always demultiplex it
in real time each time he reads the code.&lt;/p&gt;
&lt;p&gt;The last part of the interface file is quite simple to read since it's
already abstracted away. Now let's completely demultiplex the implementation in
several implementation files.&lt;/p&gt;
&lt;h3&gt;The header file is the common interface&lt;/h3&gt;
&lt;p&gt;The interface is the most important part of the design. It should be
high-level enough to mask the differences between the plateforms you want to
support, but not too high-level, otherwise you'll end up duplicating to much
code.&lt;/p&gt;
&lt;p&gt;So, for our file I/O subsystem, we'll just abstract the usual syscalls
&lt;code&gt;open&lt;/code&gt;, &lt;code&gt;close&lt;/code&gt; in the same way as before.&lt;/p&gt;
&lt;p&gt;The parameters that are passed through the interface are also very
important. You cannot usually leak a platform-specific structure. So here all
the file descriptors are just an opaque handle, represented by a pointer to a
structure defined only as a forward declaration in the header. This pattern,
sometimes called the &lt;a href=&quot;http://en.wikipedia.org/wiki/Opaque_pointer&quot; hreflang=&quot;en&quot;&gt;pimpl idiom&lt;/a&gt;, enables use to really share the representation
while implementing it differently.&lt;/p&gt;
&lt;p&gt;The header file is preserved as &lt;code&gt;fileiosys.h&lt;/code&gt;, whereas the
implementation is in the files &lt;code&gt;linux/fileiosys.cpp&lt;/code&gt; and
&lt;code&gt;win32/fileiosys.cpp&lt;/code&gt;&lt;/p&gt;
&lt;h4&gt;&lt;code&gt;fileiosys.h&lt;/code&gt;&lt;/h4&gt;
&lt;pre&gt;
// Define the forward declaration
struct fis_filedesc;
fis_filedesc* open(char* filename); 
int close(fileiosys_filedesc* fd); 
&lt;/pre&gt;
&lt;h4&gt;&lt;code&gt;win32/fileiosys.cpp&lt;/code&gt;&lt;/h4&gt;
&lt;pre&gt;
struct fis_filedesc {
    HANDLE handle;
};

fis_filedesc* open(char* filename) {
    fis_filedesc* fd = new fis_filedesc();
    fd-&amp;gt;handle = OpenFile(
        filename, 
        GENERIC_READ | GENERIC_WRITE, 
        FILE_SHARE_READ | FILE_SHARE_WRITE, 
        NULL,
    );  

    return fd;
}

int close (fis_filedesc* fd) {
    int retval = CloseHandle(fd-&amp;gt;handle) ? 0 : -1;
    delete(fd);
    return retval;
}
&lt;/pre&gt;
&lt;h4&gt;&lt;code&gt;linux/fileiosys.cpp&lt;/code&gt;&lt;/h4&gt;
&lt;pre&gt;
struct fis_filedesc {
    int file_descriptor;
};

static fis_filedesc[32];

fis_filedesc* open(char* filename) {
    fis_filedesc* fd = new fis_filedesc();
    fd-&amp;gt;file_descriptor = open(filename, O_READ | O_WRITE, 0666);
    return fd;
}

int close (fis_filedesc* fd) {
    int retval = close(fd);
    delete(fd);
    return retval;
}
&lt;/pre&gt;
&lt;h4&gt;The build system : &lt;code&gt;Makefile&lt;/code&gt;&lt;/h4&gt;
&lt;p&gt;The makefile should take into account the different platforms, and only
compile the needed implementation file. All the gluing magic will then be done
at linking time instead of preprocessor time.&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;The interest of have multiple implementation files is obvious. It is
&lt;strong&gt;much&lt;/strong&gt; more straightforward to read and only marginally harder
to write. But since most of the time code is read and not written, the choose
is quite a no-brainer.&lt;/p&gt;
&lt;p&gt;The nicest part is that all this is possible even without the expensive
run-time polymorphism and RTTI, since the choose is done at compile-time.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot;&gt;
&lt;h4&gt;Notes&lt;/h4&gt;
&lt;p&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/12/#rev-pnote-467153-1&quot; id=&quot;pnote-467153-1&quot; name=&quot;pnote-467153-1&quot;&gt;1&lt;/a&gt;] actually when targeting Linux, you usually target all
Unix-like systems since they already have POSIX as a common abstraction&lt;/p&gt;
&lt;p&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/12/#rev-pnote-467153-2&quot; id=&quot;pnote-467153-2&quot; name=&quot;pnote-467153-2&quot;&gt;2&lt;/a&gt;] The code is not real, it has been sweetened&lt;/p&gt;
&lt;/div&gt;</description>
    
    
    
          <comments>http://blog.pwkf.org/post/2009/12/Avoid-the-Preprocessor-Use-Compile-Time-Polymorphism-for-Cross-platform-Development#comment-form</comments>
      <wfw:comment>http://blog.pwkf.org/post/2009/12/Avoid-the-Preprocessor-Use-Compile-Time-Polymorphism-for-Cross-platform-Development#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.pwkf.org/feed/atom/comments/467153</wfw:commentRss>
      </item>
    
  <item>
    <title>Native SSH transport for Munin</title>
    <link>http://blog.pwkf.org/post/2009/11/Native-SSH-transport-for-Munin</link>
    <guid isPermaLink="false">urn:md5:9850ae8f77d67536a24ea8c5e21b723e</guid>
    <pubDate>Thu, 26 Nov 2009 22:00:00 +0100</pubDate>
    <dc:creator>Steve Schnepp</dc:creator>
        <category>munin</category>
        <category>munin</category><category>ssh</category>    
    <description>&lt;p&gt;I &lt;a href=&quot;http://blog.pwkf.org/post/2008/11/04/A-Poor-Man-s-Munin-Node-to-Monitor-Hostile-UNIX-Servers&quot;&gt;blogged&lt;/a&gt;
about the &lt;a href=&quot;http://munin.projects.linpro.no/&quot; hreflang=&quot;en&quot;&gt;munin
monitoring system&lt;/a&gt; a while ago.&lt;/p&gt;
&lt;p&gt;The fact the Munin team did quite a remarkable job in cleaning up the 1.2
code for the 1.4 release enabled me to add a native SSH transport for Munin,
and made be able to get rid of all SSH tunnels.&lt;/p&gt;    &lt;p&gt;Actually the tunnel won't disappear, but they will be launched only when
needed and, most importantly configured in &lt;code&gt;munin.conf&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The &lt;a href=&quot;http://blog.pwkf.org/public/direct_ssh_transport.diff&quot; hreflang=&quot;en&quot;&gt;native ssh for
munin patch&lt;/a&gt; should applies quite cleanly on revision 3101 of the svn
trunk.&lt;/p&gt;
&lt;p&gt;Its use is quite straightforward : in &lt;code&gt;/etc/munin/munin.conf&lt;/code&gt;,
you just migrate address to the new configuration directive
&lt;code&gt;remote_connection_cmd&lt;/code&gt; that take the whole &lt;code&gt;ssh&lt;/code&gt; command
to launch a stdio munin-node such as pmmn.&lt;/p&gt;
&lt;p&gt;If we take the examples from the previous post, it becomes clear it's
&lt;strong&gt;much&lt;/strong&gt; easier to configure.&lt;/p&gt;
&lt;h3&gt;munin.conf snippet - Inetd version&lt;/h3&gt;
&lt;pre&gt;
[server1]
    address localhost
    port 7001
[server2]
    address localhost
    port 7002
&lt;/pre&gt;
&lt;h3&gt;munin.conf snippet - Native SSH transport version&lt;/h3&gt;
&lt;p&gt;Right now, the &lt;code&gt;address&lt;/code&gt; directive is still mandatory, but
ignored when connecting.&lt;/p&gt;
&lt;pre&gt;
[server1]
    address dummy
    remote_connection_cmd /usr/bin/ssh -- supusr@server1 /home/suprusr/pmmn/pmmn.pl
[server2]
    address dummy
    remote_connection_cmd /usr/bin/ssh -- supusr@server2 /home/suprusr/pmmn/pmmn.pl
&lt;/pre&gt;
&lt;h3&gt;Warning&lt;/h3&gt;
&lt;p&gt;Beware that the &lt;code&gt;ssh&lt;/code&gt; process will now be launched by the
&lt;code&gt;munin&lt;/code&gt; user, so you have to update the key-based SSH authentication
accordingly.&lt;/p&gt;</description>
    
    
    
          <comments>http://blog.pwkf.org/post/2009/11/Native-SSH-transport-for-Munin#comment-form</comments>
      <wfw:comment>http://blog.pwkf.org/post/2009/11/Native-SSH-transport-for-Munin#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.pwkf.org/feed/atom/comments/462735</wfw:commentRss>
      </item>
    
  <item>
    <title>Sed is much slower than Perl, or not...</title>
    <link>http://blog.pwkf.org/post/2009/11/Sed-is-much-slower-than-Perl-or-not...</link>
    <guid isPermaLink="false">urn:md5:97ad55cfcb78666f554d7bfbd3f3915e</guid>
    <pubDate>Sat, 14 Nov 2009 12:18:00 +0100</pubDate>
    <dc:creator>Steve Schnepp</dc:creator>
        <category>sysadmin</category>
        <category>locales</category><category>perl</category><category>sql</category>    
    <description>    &lt;p&gt;I wanted to do some text replacement with a &lt;strong&gt;huge&lt;/strong&gt; file
(think ~18GiB), filled with &lt;strong&gt;huge&lt;/strong&gt; lines (think ~2MiB per
ligne)&lt;sup&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/11/#pnote-459477-1&quot; id=&quot;rev-pnote-459477-1&quot; name=&quot;rev-pnote-459477-1&quot;&gt;1&lt;/a&gt;]&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;I naïvely piped it through &lt;code&gt;sed&lt;/code&gt; and I was quite shocked that it
was CPU bound, and not I/O bound. The average rate was about 5 MiB/s (measured
with &lt;a href=&quot;http://www.ivarch.com/programs/pv.shtml&quot; hreflang=&quot;en&quot;&gt;pv&lt;/a&gt;,
and the CPU was at almost 100%.The text file was gzipped on the filesystem, but
with a 1/100 ratio, so the gzip process just took less than 2% CPU. I replaced
then the &lt;code&gt;sed -e&lt;/code&gt; with the Perl one-liner &lt;code&gt;perl -lnpe&lt;/code&gt;,
and .... &lt;em&gt;tadaa&lt;/em&gt;, it was flying at a rate of 50MiB/s !&lt;/p&gt;
&lt;p&gt;While I'm a big fan of Perl, and know its effectiveness to handle text
streams, I'm was still astonished : being 10x faster than sed was
something.&lt;/p&gt;
&lt;p&gt;But in the good old saying &lt;q&gt;Too good to be true means suspect&lt;/q&gt;, I
remembered something about the character encoding of the regular expression.
Since the system is entirely configured in UTF8, I suspected the
&lt;em&gt;infamous&lt;/em&gt; UTF8 overhead over plain ASCII.&lt;/p&gt;
&lt;p&gt;I was right : a little &lt;code&gt;LANG=C&lt;/code&gt; in front of the sed command line
restored the rate to 50MiB/s.&lt;/p&gt;
&lt;p&gt;So, &lt;strong&gt;beware&lt;/strong&gt; of the &lt;strong&gt;performance impact&lt;/strong&gt; of
&lt;strong&gt;UTF8&lt;/strong&gt; strings, and try to avoid it if you can.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot;&gt;
&lt;h4&gt;Notes&lt;/h4&gt;
&lt;p&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/11/#rev-pnote-459477-1&quot; id=&quot;pnote-459477-1&quot; name=&quot;pnote-459477-1&quot;&gt;1&lt;/a&gt;] For the record, it was a MySQL dump&lt;/p&gt;
&lt;/div&gt;</description>
    
    
    
          <comments>http://blog.pwkf.org/post/2009/11/Sed-is-much-slower-than-Perl-or-not...#comment-form</comments>
      <wfw:comment>http://blog.pwkf.org/post/2009/11/Sed-is-much-slower-than-Perl-or-not...#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.pwkf.org/feed/atom/comments/459477</wfw:commentRss>
      </item>
    
  <item>
    <title>Quickly replicate the clock between remote hosts with SSH</title>
    <link>http://blog.pwkf.org/post/2009/09/Code-snippet-to-quickly-replicate-the-clock-from/to-a-remote-host</link>
    <guid isPermaLink="false">urn:md5:11e72e879c3d3f63b031ab72bb65db9f</guid>
    <pubDate>Fri, 11 Sep 2009 08:00:00 +0200</pubDate>
    <dc:creator>Steve Schnepp</dc:creator>
        <category>sysadmin</category>
        <category>ssh</category>    
    <description>    &lt;p&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Network_Time_Protocol&quot; hreflang=&quot;en&quot;&gt;NTP&lt;/a&gt; is very handy for server clock synchronisation, but it can be
cumbersome to deploy.&lt;/p&gt;
&lt;p&gt;Sometimes you just need to do a one-shot clock synchronisation, so you use
the standard &lt;code&gt;date&lt;/code&gt; command. But there isn't a flag to easily copy a
setting to another.&lt;/p&gt;
&lt;h2&gt;From a remote host&lt;/h2&gt;
&lt;p&gt;Quite easy :&lt;/p&gt;
&lt;pre&gt;
# date `ssh remoteuser@remotehost date +%m%d%H%M%Y.%S`
&lt;/pre&gt;
&lt;h2&gt;To a remote host&lt;/h2&gt;
&lt;p&gt;It's also very easy&lt;sup&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/09/Code-snippet-to-quickly-replicate-the-clock-from/#pnote-440747-1&quot; id=&quot;rev-pnote-440747-1&quot; name=&quot;rev-pnote-440747-1&quot;&gt;1&lt;/a&gt;]&lt;/sup&gt; :&lt;/p&gt;
&lt;pre&gt;
# ssh root@remotehost date `date +%m%d%H%M%Y.%S`
&lt;/pre&gt;
&lt;div class=&quot;footnotes&quot;&gt;
&lt;h4&gt;Notes&lt;/h4&gt;
&lt;p&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/09/Code-snippet-to-quickly-replicate-the-clock-from/#rev-pnote-440747-1&quot; id=&quot;pnote-440747-1&quot; name=&quot;pnote-440747-1&quot;&gt;1&lt;/a&gt;] Yes, I &lt;strong&gt;do&lt;/strong&gt; know that logging remotely
as root is a security pitfall...&lt;/p&gt;
&lt;/div&gt;</description>
    
    
    
          <comments>http://blog.pwkf.org/post/2009/09/Code-snippet-to-quickly-replicate-the-clock-from/to-a-remote-host#comment-form</comments>
      <wfw:comment>http://blog.pwkf.org/post/2009/09/Code-snippet-to-quickly-replicate-the-clock-from/to-a-remote-host#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.pwkf.org/feed/atom/comments/440747</wfw:commentRss>
      </item>
    
  <item>
    <title>Databases: Efficient Case-insensitive searches with Function-based Indexing</title>
    <link>http://blog.pwkf.org/post/2009/09/Databases%3A-Efficient-Case-insensitive-searches-with-Function-based-Indexing</link>
    <guid isPermaLink="false">urn:md5:0672e1835437418b84886618aa2e1440</guid>
    <pubDate>Tue, 08 Sep 2009 22:00:00 +0200</pubDate>
    <dc:creator>Steve Schnepp</dc:creator>
        <category>database</category>
            
    <description>&lt;p&gt;Doing a &lt;strong&gt;case insensitive search&lt;/strong&gt; is a very &lt;strong&gt;common
task&lt;/strong&gt;, but is quite &lt;strong&gt;hard to optimize correctly&lt;/strong&gt;. But
since it's done via a &lt;code&gt;UPPER(MY_COLUMN) = UPPER('MY_DATA')&lt;/code&gt;, it
doesn't use the index that could be on &lt;code&gt;MY_COLUMN&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Different RDMS means different approaches.&lt;/p&gt;    &lt;h2&gt;Special case of case-insensitive search&lt;/h2&gt;
&lt;p&gt;In Oracle10g, you might use the new case-insensitive search with a
&lt;code&gt;&lt;a href=&quot;http://www.dba-oracle.com/t_oracle10g_release_2_case_insensitive_searches.htm&quot; hreflang=&quot;en&quot;&gt;NLS_SORT=BINARY_CI&lt;/a&gt;&lt;/code&gt; command.&lt;/p&gt;
&lt;h3&gt;Pro&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Designed for this purpose, so it's very straightforward to use&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Con&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Limited to case-insensitive searching by design&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Native functional indexes&lt;/h2&gt;
&lt;p&gt;Some databases provides native functional indexes.&lt;/p&gt;
&lt;p&gt;On these databases optimization is done simply by creating an index on
&lt;code&gt;UPPER(MY_COLUMN)&lt;/code&gt; and letting the query optimizer
transparently&lt;sup&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/09/#pnote-426279-1&quot; id=&quot;rev-pnote-426279-1&quot; name=&quot;rev-pnote-426279-1&quot;&gt;1&lt;/a&gt;]&lt;/sup&gt; use the newly created index.&lt;/p&gt;
&lt;p&gt;It usually work by applies a function to the data just before handing it to
the index, so the function output doesn't exist in the database.&lt;/p&gt;
&lt;h3&gt;Pro&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Very&lt;/strong&gt; easy to use : it just feels right (you can naïvely
create an index on the WHERE clause)&lt;/li&gt;
&lt;li&gt;Doesn't take any extra space in the database (only the index).&lt;/li&gt;
&lt;li&gt;Generic, can be used for something else than just case-insensitive
searches.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Con&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Since the data isn't stored in the database, a call to the function has to
be made when&lt;/li&gt;
&lt;li&gt;Functions have to be from the immutable category in the &lt;a href=&quot;http://www.postgresql.org/docs/8.3/static/xfunc-volatility.html&quot; hreflang=&quot;en&quot;&gt;function volatility categories&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Generated columns&lt;/h2&gt;
&lt;p&gt;DB2 provides something called &lt;a href=&quot;http://publib.boulder.ibm.com/infocenter/db2luw/v8/topic/com.ibm.db2.udb.doc/ad/c0007004.htm&quot; hreflang=&quot;en&quot;&gt;generated columns&lt;/a&gt;. It's almost the same than the native
indexes, except that the functional column is explicit.&lt;/p&gt;
&lt;h3&gt;Pro&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Quite easy to use, since the column is updated and used transparently.&lt;/li&gt;
&lt;li&gt;Generic, can be used for something else than just case-insensitive
searches. Just make sure the optimizer uses the extra column. You might have to
rewrite the request a little.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Con&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Requires extra space in the table.&lt;/li&gt;
&lt;li&gt;Removing a column can be cumbersome (in DB2 you have to recreate the whole
table for example), whereas removing a simple index is much easier.&lt;/li&gt;
&lt;li&gt;The extra column is returned when doing a &lt;code&gt;SELECT * FROM
...&lt;/code&gt;&lt;sup&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/09/#pnote-426279-2&quot; id=&quot;rev-pnote-426279-2&quot; name=&quot;rev-pnote-426279-2&quot;&gt;2&lt;/a&gt;]&lt;/sup&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Trigger-based &lt;em&gt;Generated columns&lt;/em&gt; Emulation&lt;/h2&gt;
&lt;p&gt;If nothing else is provided, you always have the option to emulate. The
solution will be trigger-based since it's one of the few perfect match for
them.&lt;/p&gt;
&lt;p&gt;So, the base idea is derived from the &lt;em&gt;Generated columns&lt;/em&gt; : have a
special extra column that represents the output of the function. An index will
be created on this column and used via a &lt;em&gt;manual&lt;/em&gt; update of the involved
requests (Adding an extra &lt;code&gt;WHERE&lt;/code&gt; clause should be more than enough,
this way you might even benefit from a partial match).&lt;/p&gt;
&lt;h3&gt;Pro&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Universal. Useful if portability is paramount.&lt;/li&gt;
&lt;li&gt;Very simple : there is no need to understand advanced database
features.&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;Con&lt;/h3&gt;
&lt;ul&gt;
&lt;li&gt;Only a poor man's solution : everything is manual&lt;/li&gt;
&lt;li&gt;The same than &lt;em&gt;Generated columns&lt;/em&gt; since it's the same idea, just
manually implemented.&lt;/li&gt;
&lt;/ul&gt;
&lt;div class=&quot;footnotes&quot;&gt;
&lt;h4&gt;Notes&lt;/h4&gt;
&lt;p&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/09/#rev-pnote-426279-1&quot; id=&quot;pnote-426279-1&quot; name=&quot;pnote-426279-1&quot;&gt;1&lt;/a&gt;] You might have to update some kind of statistics for
the new index&lt;/p&gt;
&lt;p&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/09/#rev-pnote-426279-2&quot; id=&quot;pnote-426279-2&quot; name=&quot;pnote-426279-2&quot;&gt;2&lt;/a&gt;] But you don't do that anyway, do you ?&lt;/p&gt;
&lt;/div&gt;</description>
    
    
    
          <comments>http://blog.pwkf.org/post/2009/09/Databases%3A-Efficient-Case-insensitive-searches-with-Function-based-Indexing#comment-form</comments>
      <wfw:comment>http://blog.pwkf.org/post/2009/09/Databases%3A-Efficient-Case-insensitive-searches-with-Function-based-Indexing#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.pwkf.org/feed/atom/comments/426279</wfw:commentRss>
      </item>
    
  <item>
    <title>Overloading a method is hard : a common pitfall</title>
    <link>http://blog.pwkf.org/post/2009/09/Overloading-method-is-hard-%3A-a-common-pitfall</link>
    <guid isPermaLink="false">urn:md5:b89efe0436b185be77c2bd6856338b51</guid>
    <pubDate>Mon, 07 Sep 2009 22:00:00 +0200</pubDate>
    <dc:creator>Steve Schnepp</dc:creator>
        <category>java</category>
            
    <description>    &lt;p&gt;As I said in my &lt;a href=&quot;http://blog.pwkf.org/post/2009/06/Equality-in-Java-is-a-Hot-Topic-but-a-Hazardous-one&quot;&gt;equality
article&lt;/a&gt;, overloading in Java&lt;sup&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/09/#pnote-439371-1&quot; id=&quot;rev-pnote-439371-1&quot; name=&quot;rev-pnote-439371-1&quot;&gt;1&lt;/a&gt;]&lt;/sup&gt; is resolved by the
&lt;strong&gt;static&lt;/strong&gt; type of the argument, not the &lt;strong&gt;run-time&lt;/strong&gt;
type.&lt;/p&gt;
&lt;p&gt;It's a &lt;strong&gt;generic&lt;/strong&gt; problem of most compiled OO languages since
usually &lt;strong&gt;overloading resolution happens at compile-time and not at
runtime&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Now, &lt;em&gt;that&lt;/em&gt; militates for the well known idiom :&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Never overload a method with one that has the same number of parameters.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Actually, it should be enough to overload a method with one that accept
parameters that are not inheritance-related : &lt;code&gt;String&lt;/code&gt; and
&lt;code&gt;Number&lt;/code&gt; would be OK, but &lt;code&gt;MyClass&lt;/code&gt; and
&lt;code&gt;Object&lt;/code&gt; would not.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot;&gt;
&lt;h4&gt;Notes&lt;/h4&gt;
&lt;p&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/09/#rev-pnote-439371-1&quot; id=&quot;pnote-439371-1&quot; name=&quot;pnote-439371-1&quot;&gt;1&lt;/a&gt;] It's not really a &lt;em&gt;Java&lt;/em&gt;-ism, it's the same in
other languages, such as C++ .&lt;/p&gt;
&lt;/div&gt;</description>
    
    
    
          <comments>http://blog.pwkf.org/post/2009/09/Overloading-method-is-hard-%3A-a-common-pitfall#comment-form</comments>
      <wfw:comment>http://blog.pwkf.org/post/2009/09/Overloading-method-is-hard-%3A-a-common-pitfall#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.pwkf.org/feed/atom/comments/439371</wfw:commentRss>
      </item>
    
  <item>
    <title>A Simple Dns Server for a SOHO Network</title>
    <link>http://blog.pwkf.org/post/2009/08/A-Simple-Dns-Server-for-a-SOHO-Network</link>
    <guid isPermaLink="false">urn:md5:980ba57dcf712852978b5c8496cfeca0</guid>
    <pubDate>Mon, 10 Aug 2009 20:00:00 +0200</pubDate>
    <dc:creator>Steve Schnepp</dc:creator>
        <category>sysadmin</category>
        <category>sql</category>    
    <description>    &lt;p&gt;I'm in search of a very simple DNS Server for a small network. It should be
:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;recursive &amp;amp; caching (can be used as a proxy)&lt;/li&gt;
&lt;li&gt;very simple administration (parsing /etc/hosts would be perfect, raw DNS
zones like BIND would be a little bit overkill)&lt;/li&gt;
&lt;li&gt;quite lightweight (aka no dependency on an SQL engine like MySQL, such as
MyDNS)&lt;/li&gt;
&lt;li&gt;Seamless integration to Windows lookups (nmblookup) via proxying functions
(DNS to/from NMB)&lt;/li&gt;
&lt;/ul&gt;</description>
    
    
    
          <comments>http://blog.pwkf.org/post/2009/08/A-Simple-Dns-Server-for-a-SOHO-Network#comment-form</comments>
      <wfw:comment>http://blog.pwkf.org/post/2009/08/A-Simple-Dns-Server-for-a-SOHO-Network#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.pwkf.org/feed/atom/comments/340907</wfw:commentRss>
      </item>
    
  <item>
    <title>Databases: Better Defer Constraints than Avoid Them</title>
    <link>http://blog.pwkf.org/post/2009/07/Databases%3A-Better-Defer-Constraints-than-Avoid-Them</link>
    <guid isPermaLink="false">urn:md5:23cfdca534043c8742e5371ebd5a5ede</guid>
    <pubDate>Fri, 31 Jul 2009 08:00:00 +0200</pubDate>
    <dc:creator>Steve Schnepp</dc:creator>
        <category>database</category>
            
    <description>&lt;p&gt;Constraints are considered a &lt;q&gt;Good Thing&lt;/q&gt;. They enable to rely more
heavily on the validity of the database. It is quite important to note that
validity is in terms of &lt;strong&gt;modeling&lt;/strong&gt; and not in terms of
&lt;strong&gt;business&lt;/strong&gt; &lt;sup&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/07/#pnote-415135-1&quot; id=&quot;rev-pnote-415135-1&quot; name=&quot;rev-pnote-415135-1&quot;&gt;1&lt;/a&gt;]&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;The biggest complains we can have about constraints is that it is sometimes
quite annoying to do some updates while consistently validating the
constraints. You have to care about the order of the operations you do.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot;&gt;
&lt;h4&gt;Notes&lt;/h4&gt;
&lt;p&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/07/#rev-pnote-415135-1&quot; id=&quot;pnote-415135-1&quot; name=&quot;pnote-415135-1&quot;&gt;1&lt;/a&gt;] I'll write an article about this later&lt;/p&gt;
&lt;/div&gt;    &lt;h2&gt;&lt;em&gt;ACID&lt;/em&gt;ity of transactions as rescue...&lt;/h2&gt;
&lt;p&gt;Let's examine what all of the &lt;code&gt;A&lt;/code&gt;, the &lt;code&gt;C&lt;/code&gt;, the
&lt;code&gt;I&lt;/code&gt; and the &lt;code&gt;D&lt;/code&gt; have for implications on our current
subject.&lt;/p&gt;
&lt;h3&gt;Transactions are &lt;strong&gt;atomic&lt;/strong&gt; (A)&lt;/h3&gt;
&lt;p&gt;The database should not really care about validating the constraints all the
time Only the beginning and end state is really important. &lt;strong&gt;Inside a
transaction, the data may be inconsistent&lt;/strong&gt; : &lt;q&gt;Dust hasn't yet
settled&lt;/q&gt;.&lt;/p&gt;
&lt;p&gt;Let's study an example. You have an unique index on an ordering.&lt;/p&gt;
&lt;pre&gt;
TABLE ITEMS (
    ITEM_ID SERIAL,
    GROUP_ID INTEGER,
    ORDER INTEGER,
    LABEL VARCHAR
)

ADD UNIQUE INDEX ON ITEMS(GROUP_ID, ORDER)
&lt;/pre&gt;
&lt;p&gt;Here, if you want to swap 2 items, you have to use an unused temporary
value, otherwise the check won't be valid &lt;strong&gt;at all times&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;Transactions are &lt;strong&gt;consistent&lt;/strong&gt; (C)&lt;/h3&gt;
&lt;p&gt;The check &lt;strong&gt;needs&lt;/strong&gt; to be done at least when the transaction
ends. No special need here except that you &lt;strong&gt;need&lt;/strong&gt; to re-enable
before ending the transaction.&lt;/p&gt;
&lt;h3&gt;Transactions are &lt;strong&gt;isolated&lt;/strong&gt; (I)&lt;/h3&gt;
&lt;p&gt;We can also easily imagine that the check only &lt;strong&gt;needs&lt;/strong&gt; to be
done when the transaction ends, just before the commit. Normally no one should
be able to see the changing data meanwhile it's not completed.&lt;/p&gt;
&lt;p&gt;One &lt;strong&gt;very important&lt;/strong&gt; thing to know is that if you are using a
&lt;strong&gt;READ UNCOMMITTED isolation&lt;/strong&gt; for other transactions &lt;strong&gt;you
will see inconsistent data&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Anyway, if you are doing that, you know what you are doing and are obviously
taking special care about it, don't you ?&lt;/p&gt;
&lt;h3&gt;Transactions are &lt;strong&gt;durable&lt;/strong&gt; (D)&lt;/h3&gt;
&lt;p&gt;This final property has nothing to do with our current issue. Good.&lt;/p&gt;
&lt;h2&gt;... But sometimes early warning is quite nice&lt;/h2&gt;
&lt;p&gt;When interacting with external systems, that don't participate in our
transaction, extra care should be taken in order to cope with the exceptional
case of a constraint failure. Distributed transactions is a very complex
subject, and usually it's not &lt;del&gt;supported&lt;/del&gt; done&lt;sup&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/07/#pnote-415135-1&quot; id=&quot;rev-pnote-415135-1&quot; name=&quot;rev-pnote-415135-1&quot;&gt;1&lt;/a&gt;]&lt;/sup&gt;.&lt;/p&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;With the principle of &lt;em&gt;least surprise&lt;/em&gt; in mind, we can easily
understand why &lt;code&gt;deferred&lt;/code&gt; is not the default behavior, but it makes
a very nice addition to our toolbox.&lt;/p&gt;
&lt;p&gt;So now there isn't any good reason anymore not to use (and abuse)
constraints in your databases.&lt;/p&gt;
&lt;p&gt;Remember, &lt;strong&gt;your data is you most precious asset&lt;/strong&gt;, protect it
at all cost from evil misbehaved bug-ridden software&lt;sup&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/07/#pnote-415135-2&quot; id=&quot;rev-pnote-415135-2&quot; name=&quot;rev-pnote-415135-2&quot;&gt;2&lt;/a&gt;]&lt;/sup&gt; !&lt;/p&gt;
&lt;p&gt;Wow... Just in time before the end of the month... I would have failed
&lt;a href=&quot;http://blog.pwkf.org/post/2009/04/Should-The-URL-Include-a-Date-or-Not&quot;&gt;my motto on
URLs&lt;/a&gt; ;-)&lt;/p&gt;
&lt;div class=&quot;footnotes&quot;&gt;
&lt;h4&gt;Notes&lt;/h4&gt;
&lt;p&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/07/#rev-pnote-415135-1&quot; id=&quot;pnote-415135-1&quot; name=&quot;pnote-415135-1&quot;&gt;1&lt;/a&gt;] Do you always implement a double-phase commit on every
SOAP/REST/XML-HTTP/POST interface that you expose ?&lt;/p&gt;
&lt;p&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/07/#rev-pnote-415135-2&quot; id=&quot;pnote-415135-2&quot; name=&quot;pnote-415135-2&quot;&gt;2&lt;/a&gt;] Yeah, I have chosen my side in the coming war...&lt;/p&gt;
&lt;/div&gt;</description>
    
    
    
          <comments>http://blog.pwkf.org/post/2009/07/Databases%3A-Better-Defer-Constraints-than-Avoid-Them#comment-form</comments>
      <wfw:comment>http://blog.pwkf.org/post/2009/07/Databases%3A-Better-Defer-Constraints-than-Avoid-Them#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.pwkf.org/feed/atom/comments/415135</wfw:commentRss>
      </item>
    
  <item>
    <title>Checked or Unchecked Exceptions for Legacy Code ?</title>
    <link>http://blog.pwkf.org/post/2009/07/Checked-or-Unchecked-Exceptions-for-Legacy-Code</link>
    <guid isPermaLink="false">urn:md5:4a50674f72aab4eef3e236336183a3c3</guid>
    <pubDate>Wed, 15 Jul 2009 20:55:00 +0200</pubDate>
    <dc:creator>Steve Schnepp</dc:creator>
        <category>java</category>
            
    <description>&lt;p&gt;This question can almost trigger a &lt;strong&gt;religious war&lt;/strong&gt;. On
Internet &lt;strong&gt;no fixed consensus&lt;/strong&gt; on the matter seems to
&lt;strong&gt;exists&lt;/strong&gt;. But most of the articles there have usually
&lt;strong&gt;something in common&lt;/strong&gt; : they are always mostly
&lt;strong&gt;applicable&lt;/strong&gt; when you start a &lt;strong&gt;new
application&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;Is the situation the same when you have a certain amount of &lt;strong&gt;existing
code to maintain&lt;/strong&gt; ?&lt;/p&gt;    &lt;h2&gt;Checked Exceptions are breaking encapsulation&lt;/h2&gt;
&lt;p&gt;In theory it's quite nice since the called code can communicate with it's
caller when something unexpected happened.&lt;/p&gt;
&lt;p&gt;The key word here is &lt;em&gt;&lt;ins&gt;unexpected&lt;/ins&gt;&lt;/em&gt;. If you have to
explicitly know the exceptions that could occur, it's not really unexpected.
And if it's not unexpected, using exception handling just add an
&lt;em&gt;out-of-band&lt;/em&gt; data path. It's on par with transporting data in a private
class field when calling a member instead of using its arguments. This leads to
breaking encapsulation as Alan Griffiths wrote in &lt;a href=&quot;http://www.octopull.demon.co.uk/java/ExceptionalJava.html&quot; hreflang=&quot;en&quot;&gt;Exceptional Java&lt;/a&gt;. This vision is also shared by Bruce Eckel in his
article entitled &lt;a href=&quot;http://www.mindview.net/Etc/Discussions/CheckedExceptions&quot; hreflang=&quot;en&quot;&gt;Does
Java need Checked Exceptions&lt;/a&gt;?.&lt;/p&gt;
&lt;h2&gt;Checked Exceptions are quite painful to use&lt;/h2&gt;
&lt;h3&gt;Local Exception Handling is hard to manage&lt;/h3&gt;
&lt;p&gt;I personally find checked exceptions quite painful to use. By definition,
you have to catch every exception that is thrown by the underlaying code.&lt;/p&gt;
&lt;p&gt;And if the underlaying code doesn't know what to do with the exception,
chances are, that you don't know either, so you just pass the exception to the
caller. And so on...&lt;/p&gt;
&lt;h3&gt;Too much code to change&lt;/h3&gt;
&lt;p&gt;Therefore exceptions are usually caught at the top level with a generic
catch-all structure that logs the error, since no layer could sensibly do
something clever with the exception.&lt;/p&gt;
&lt;p&gt;Then you just have to change all the signature of the whole stack, just to
be able to catch them at the top. Using unchecked exception lets you have this
for free, and conveys the meaning that nothing is caught until the top.&lt;/p&gt;
&lt;h2&gt;Unchecked Exceptions might be dangerous...&lt;/h2&gt;
&lt;p&gt;Obviously, unchecked means not checked. So you might fail to catch them at
the top level and then the whole application crashes. Checked exceptions are a
safeguard against that. Just like strong static typing is.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;You trade compile-time safety&lt;/strong&gt; (checked) &lt;strong&gt;for
development-time speed and ease&lt;/strong&gt; (unchecked).&lt;/p&gt;
&lt;h2&gt;... but are not really.&lt;/h2&gt;
&lt;p&gt;On the other hand, if you have a good design, you don't have much different
top levels, and then the risk is somewhat limited.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Moreover, since you &lt;em&gt;always&lt;/em&gt; have to take unchecked one into
account, why don't use them ?&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;By the way, in C++, the exceptions are unchecked by default.&lt;/p&gt;</description>
    
    
    
          <comments>http://blog.pwkf.org/post/2009/07/Checked-or-Unchecked-Exceptions-for-Legacy-Code#comment-form</comments>
      <wfw:comment>http://blog.pwkf.org/post/2009/07/Checked-or-Unchecked-Exceptions-for-Legacy-Code#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.pwkf.org/feed/atom/comments/395563</wfw:commentRss>
      </item>
    
  <item>
    <title>Databases: Efficient Denormalization with Views</title>
    <link>http://blog.pwkf.org/post/2009/05/Efficient-Denormalization-with-Views</link>
    <guid isPermaLink="false">urn:md5:dddd0fc150be07492e5d6229f6e07480</guid>
    <pubDate>Tue, 02 Jun 2009 08:00:00 +0200</pubDate>
    <dc:creator>Steve Schnepp</dc:creator>
        <category>database</category>
            
    <description>&lt;p&gt;Everyone is unanimous that &lt;strong&gt;database normalization&lt;/strong&gt; is
considered a &lt;strong&gt;Good Thing&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;But it usually comes with a cost : &lt;strong&gt;writing queries can be very
tedious&lt;/strong&gt; since you always have to join many tables together to be able
to retrieve useful human data from all those reference tables.&lt;/p&gt;    &lt;p&gt;On the other side, to &lt;strong&gt;denormalize&lt;/strong&gt; is sometimes seen as a
way to :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;optimize development&lt;/strong&gt; : you do not need to write (and
debug) complex queries since all the data is nicely located in the same
table&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;optimize performance&lt;/strong&gt; : the data has a better locality (no
need to fetch or compute data from elsewhere). You can even pre-compute
&lt;a href=&quot;http://database-programmer.blogspot.com/2008/11/keeping-denormalized-values-correct.html&quot; hreflang=&quot;en&quot;&gt;order totals&lt;/a&gt;&lt;sup&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/05/#pnote-402145-1&quot; id=&quot;rev-pnote-402145-1&quot; name=&quot;rev-pnote-402145-1&quot;&gt;1&lt;/a&gt;]&lt;/sup&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Denormalize correctly is quite difficult since &lt;strong&gt;one change&lt;/strong&gt;
snowballs in &lt;strong&gt;multiple updates&lt;/strong&gt; to keep the redundant data
coherent. It is therefore usually done on the &lt;strong&gt;application side&lt;/strong&gt;
with ready-to-use ORM frameworks. But managing it at the application level
comes with an ugly cost : it has to be &lt;strong&gt;reimplemented over and
over&lt;/strong&gt; each time the application switches technology, version or when a
new application is connecting to the database.&lt;/p&gt;
&lt;p&gt;In almost every programming language, code reuse is encouraged. Here, at the
database level, we can also apply the same principles, and use views to
transparently present a denormalized API (read &lt;em&gt;tables&lt;/em&gt;) on a more
normalized schema.&lt;/p&gt;
&lt;p&gt;The main points are :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Views are the main code reuse vector at the database level. Current
database usually correctly optimize (by rewrite and recombination) the simple
queries so that performance is on par with a hand crafted query that doesn't
use the views&lt;/li&gt;
&lt;li&gt;Sometime even faster since the data is nicely organized, so there is much
less data to transfer, and I/O bandwidth is a usual suspect.&lt;/li&gt;
&lt;li&gt;The application part doesn't even need to know that normalization happens
under-hood since &lt;a href=&quot;http://www.ibm.com/developerworks/db2/library/techarticle/0210rielau/0210rielau.html&quot; hreflang=&quot;en&quot;&gt;updates to views&lt;/a&gt; are possible in many modern RDMs with the
&lt;a href=&quot;http://publib.boulder.ibm.com/infocenter/ids9help/topic/com.ibm.sqlt.doc/sqltmst333.htm&quot; hreflang=&quot;en&quot;&gt;@@INSTEAD OF@@ trigger&lt;/a&gt; (or something &lt;a href=&quot;http://www.postgresql.org/docs/current/static/rules-update.html&quot; hreflang=&quot;en&quot;&gt;equivalent&lt;/a&gt;).&lt;/li&gt;
&lt;li&gt;Theses updates can be created with &lt;a href=&quot;http://en.wikipedia.org/wiki/Materialized_view&quot; hreflang=&quot;en&quot;&gt;Materialized
views&lt;/a&gt; are a step even further on the denormalizing road, since it provides
the common benefits of denormalize without the implementation caveats. We can
even hand-craft these &lt;em&gt;Materialized views&lt;/em&gt; directly in an aggregation
table in order to have the space benefits of normalisation and the performance
benefits of denormalization.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;So, nothing stops you from normalizing at will, and denormalizing when
needed. Where to put the cursor is yours to decide, but moving it afterwards is
finally easier that what is commonly admited.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot;&gt;
&lt;h4&gt;Notes&lt;/h4&gt;
&lt;p&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/05/#rev-pnote-402145-1&quot; id=&quot;pnote-402145-1&quot; name=&quot;pnote-402145-1&quot;&gt;1&lt;/a&gt;] That article also explains why &lt;strong&gt;denormalization
maintenance must stay at the database level&lt;/strong&gt; with a very interesting
metaphor&lt;/p&gt;
&lt;/div&gt;</description>
    
    
    
          <comments>http://blog.pwkf.org/post/2009/05/Efficient-Denormalization-with-Views#comment-form</comments>
      <wfw:comment>http://blog.pwkf.org/post/2009/05/Efficient-Denormalization-with-Views#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.pwkf.org/feed/atom/comments/402145</wfw:commentRss>
      </item>
    
  <item>
    <title>Equality in Java is a Hot Topic, but a Hazardous one.</title>
    <link>http://blog.pwkf.org/post/2009/06/Equality-in-Java-is-a-Hot-Topic-but-a-Hazardous-one</link>
    <guid isPermaLink="false">urn:md5:9b16cf0509ff3d384294314679d05f68</guid>
    <pubDate>Mon, 01 Jun 2009 17:38:00 +0200</pubDate>
    <dc:creator>Steve Schnepp</dc:creator>
        <category>java</category>
            
    <description>    &lt;p&gt;It seems that comparing two objects isn't as a simple task to do as it seems
at first.&lt;/p&gt;
&lt;p&gt;My concept for &lt;a href=&quot;http://blog.pwkf.org/post/2009/05/Compare-Efficiently-in-Java-%3A-Embrace-Smart-Comparison&quot;&gt;smart
comparison&lt;/a&gt; still holds, but is completed perfectly with the &lt;a href=&quot;http://www.artima.com/lejava/articles/equality.html&quot; hreflang=&quot;en&quot;&gt;How to
Write an Equality Method in Java&lt;/a&gt; article posted on Artima soon after
mine&lt;sup&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/06/#pnote-406940-1&quot; id=&quot;rev-pnote-406940-1&quot; name=&quot;rev-pnote-406940-1&quot;&gt;1&lt;/a&gt;]&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;2 main points that you have to be careful to (Others are also detailed in
this article) :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;hasCode()&lt;/code&gt; has to be redefined usually since otherwise the
&lt;a href=&quot;http://www.geocities.com/technofundo/tech/java/equalhash.html&quot; hreflang=&quot;en&quot;&gt;equality contract&lt;/a&gt; on &lt;code&gt;Object.hascode()&lt;/code&gt; would be
broken.&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;equals()&lt;/code&gt; has to take an Object as parameter since
overloading in Java is resolved by the compile-time type of the argument, not
the run-time type.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I just got bitten by the second one, but not much (yet) since I mostly rely
on compile-time overloading. My purpose here was only to compare objects either
to constants or to simple variables. But in the generalization I would
certainly have overlooked this and be bitten much more deeply.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot;&gt;
&lt;h4&gt;Notes&lt;/h4&gt;
&lt;p&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/06/#rev-pnote-406940-1&quot; id=&quot;pnote-406940-1&quot; name=&quot;pnote-406940-1&quot;&gt;1&lt;/a&gt;] But as much as I wished it to be, it is not related
:-)&lt;/p&gt;
&lt;/div&gt;</description>
    
    
    
          <comments>http://blog.pwkf.org/post/2009/06/Equality-in-Java-is-a-Hot-Topic-but-a-Hazardous-one#comment-form</comments>
      <wfw:comment>http://blog.pwkf.org/post/2009/06/Equality-in-Java-is-a-Hot-Topic-but-a-Hazardous-one#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.pwkf.org/feed/atom/comments/406940</wfw:commentRss>
      </item>
    
  <item>
    <title>Synthetic Style for Blog Posts : Presentation Style Blogging</title>
    <link>http://blog.pwkf.org/post/2009/05/Synthetic-Style-for-Blog-Posts-%3A-Presentation-Style-Blogging</link>
    <guid isPermaLink="false">urn:md5:60e0d1888b4066bfbb5a1e5c0b324e28</guid>
    <pubDate>Fri, 29 May 2009 08:00:00 +0200</pubDate>
    <dc:creator>Steve Schnepp</dc:creator>
        <category>general</category>
            
    <description>    &lt;h2&gt;The good&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;With SMS, IM and now Twitter becoming more and more predominant :
&lt;strong&gt;ideas might be given as bullet points&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;A presentation is much more dense in meaning than a big blob of text, Less
time is required to read the post and &lt;strong&gt;be inspired&lt;/strong&gt; by the
content&lt;/li&gt;
&lt;li&gt;Blogging is much less time consuming. As Jeff Artwood said &lt;a href=&quot;http://www.codinghorror.com/blog/archives/001160.html&quot; hreflang=&quot;en&quot;&gt;Quantity
Always Trumps Quality&lt;/a&gt;, it is &lt;strong&gt;the commitment&lt;/strong&gt; that
&lt;strong&gt;is important&lt;/strong&gt; (hence the schedule).&lt;/li&gt;
&lt;li&gt;You might divide a long article in several short and related posts.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;The bad&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Blog&lt;/strong&gt; articles are usually a &lt;strong&gt;medium-depth
analysis&lt;/strong&gt; of a problem whereas presentation are usually a written
support of a more detailed oral presentation.&lt;/li&gt;
&lt;li&gt;Presentation sentences are usually hard to understand since they are just
headlines without the underlying context.&lt;/li&gt;
&lt;li&gt;You can always do a &lt;em&gt;fast-reading&lt;/em&gt; version by putting the
&lt;strong&gt;important sentences in bold&lt;/strong&gt; for all those hasty readers.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;The ugly&lt;/h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Longer don't always mean more interesting&lt;/strong&gt;. &lt;a href=&quot;http://en.wikipedia.org/wiki/Voltaire&quot; hreflang=&quot;en&quot;&gt;Voltaire&lt;/a&gt; said
&lt;q&gt;Perfection is attained by slow degrees; it requires the hand of time.&lt;/q&gt;
and &lt;a href=&quot;http://en.wikipedia.org/wiki/Antoine_de_Saint-Exupery&quot; hreflang=&quot;en&quot;&gt;Antoine de Saint-Exupery&lt;/a&gt; completed &lt;q&gt;Perfection is achieved, not when
there is nothing more to add, but when there is nothing left to take
away.&lt;/q&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Conclusion&lt;/h2&gt;
&lt;p&gt;As concluded by &lt;a href=&quot;http://en.wikipedia.org/wiki/Blaise_Pascal&quot; hreflang=&quot;en&quot;&gt;Pascal&lt;/a&gt;, &lt;q&gt;I made this letter longer than usual because I
lack the time to make it shorter&lt;/q&gt;, a &lt;strong&gt;good and terse&lt;/strong&gt; article
is therefore much &lt;strong&gt;more difficult&lt;/strong&gt; to achieve. Therefore
&lt;em&gt;Presentation Style Blogging&lt;/em&gt; may be a false good idea.&lt;/p&gt;</description>
    
    
    
          <comments>http://blog.pwkf.org/post/2009/05/Synthetic-Style-for-Blog-Posts-%3A-Presentation-Style-Blogging#comment-form</comments>
      <wfw:comment>http://blog.pwkf.org/post/2009/05/Synthetic-Style-for-Blog-Posts-%3A-Presentation-Style-Blogging#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.pwkf.org/feed/atom/comments/406133</wfw:commentRss>
      </item>
    
  <item>
    <title>Compare Efficiently in Java : Embrace Smart Comparison</title>
    <link>http://blog.pwkf.org/post/2009/05/Compare-Efficiently-in-Java-%3A-Embrace-Smart-Comparison</link>
    <guid isPermaLink="false">urn:md5:280c11dc4be06c3f64b772c0dd1ebefb</guid>
    <pubDate>Tue, 26 May 2009 08:00:00 +0200</pubDate>
    <dc:creator>Steve Schnepp</dc:creator>
        <category>java</category>
            
    <description>&lt;p&gt;In Java, comparing things can be quite tricky. Comparing them with a simple
&lt;code&gt;==&lt;/code&gt; only compares the references, and for example 2 strings could
be the same without being the same &lt;strong&gt;object&lt;/strong&gt;. You have to compare
them with the &lt;code&gt;equals()&lt;/code&gt; method. Now the infamous
NullPointerException (NPE) shows its ugly head when the first object to compare
is &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;    &lt;p&gt;Bill the Lizard shows us &lt;a href=&quot;http://www.billthelizard.com/2008/08/avoid-npe-when-comparing-strings.html&quot; hreflang=&quot;en&quot;&gt;a nice trick to avoid this NPE&lt;/a&gt; when used with string literals
: call the method on the string literal. It's possible because it's also an
object, and cannot be &lt;code&gt;null&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;But, as much as this trick code seems sweet, but it just feels like a red
herring on the long run, First it only works for comparing strings and even
only when comparing a string with a string literal.&lt;/p&gt;
&lt;h2&gt;To Equality ...&lt;/h2&gt;
&lt;p&gt;It is what he said at the end that is much more interesting :&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;The lesson to be learned here is that if you have to resort to &amp;quot;clever&amp;quot;
tricks to get something done, with just a little bit of lateral thinking you
can probably find a cleaner, simpler way.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;Let's take this lesson even further.&lt;/p&gt;
&lt;p&gt;I had a Perl &amp;amp; C++ background before coming to the Java world, and I am
sometimes puzzled that Java makes things quite unnecessary complicated for the
programmer from time to time.&lt;/p&gt;
&lt;p&gt;My little suggestion : create a quite simple helper static function :&lt;/p&gt;
&lt;pre&gt;
static boolean isEqual(String a, String b) {
   if (a == b) return true;

   // a &amp;amp; b cannot be null at the same time
   if (a == null || b == null) return false;

   // Now none can be null
   return a.equals(b);
}
&lt;/pre&gt;
&lt;p&gt;Now, the NPE is avoided for any kind of string, with any order. We could
also have a &lt;em&gt;smarter&lt;/em&gt; equality comparison function by first converting
to a string, and comparing the resulting string instead of comparing the
original numbers.&lt;/p&gt;
&lt;pre&gt;
static boolean isEqualSmart(Object a, Object b) {
   if (a == b) return true;

   // a &amp;amp; b cannot be null at the same time
   if (a == null || b == null) return false;

   // Now none can be null
   return a.toString().equals(b.toString());
}
&lt;/pre&gt;
&lt;p&gt;With the wonders of Java's function overloading, you can even write
specialized functions that convert even more &lt;em&gt;smartly&lt;/em&gt; their arguments.
And then be able to compare a string with a number (even a primitive type).&lt;/p&gt;
&lt;h2&gt;... and Beyond&lt;/h2&gt;
&lt;p&gt;We began about testing equality, but for comparison such as
&lt;code&gt;&amp;lt;=&lt;/code&gt; or &lt;code&gt;&amp;gt;&lt;/code&gt;, you have to decode the output of the
quite ugly &lt;code&gt;compareTo()&lt;/code&gt; method. The helper static function trick
comes also handy here.&lt;/p&gt;
&lt;p&gt;First, we design a complete API that enable alphanumeric, and numeric
comparison : the so-called &lt;strong&gt;smart comparison&lt;/strong&gt;. The API should
take any Object, or even primitive types, and convert it at will to enable
hybrid comparison. This will greatly enhance the comparisons abilities of
Java.&lt;/p&gt;
&lt;pre&gt;
class OpAlpha {

   // Alphanumeric comparison
   public static boolean lt(Object a, Object b) { 
      /* convert to string and compare */ 
   }
   public static boolean gt(Object a, Object b) { 
      /* convert to string and compare */ 
   }

   // ... rest of the implementation ...
}


class OpNum {

   // numeric comparison
   public static boolean lt(Object a, Object b) { 
      /* convert to numeric and compare */ 
   }

   // ... rest of the implementation ...
}

class Op {
   // smart comparison, depends on the 2nd argument type
   public static boolean lt(Object a, int b) {
      /* convert to int and compare */
   }
   public static boolean lt(Object a, String b) {
      /* convert to string and compare */
   }
   public static boolean lt(Object a, Number b) {
      /* convert to Number and compare */
   }

   // ... rest of the implementation ...
}
&lt;/pre&gt;
&lt;p&gt;The usage is like this :&lt;/p&gt;
&lt;pre&gt;
String a = &amp;quot;124&amp;quot;;
assertTrue( Op.eq(a, 124) );
assertFalse( Op.lt(a, 1) );
assertTrue( OpAlpha.lt(a, 1) );
&lt;/pre&gt;
&lt;p&gt;We can see that all the tedious part of handling the conversions is done
under the hood. Choosing the comparison to is just a matter of choosing the
right class. A little more work could even be done to take the &lt;code&gt;Op&lt;/code&gt;
classes to implement &lt;code&gt;Comparator&lt;/code&gt; in a complete OO way in addition
of the static helpers.&lt;/p&gt;
&lt;h2&gt;Update (01/06/2009)&lt;/h2&gt;
&lt;p&gt;It seems that &lt;a href=&quot;http://blog.pwkf.org/post/2009/06/Equality-in-Java-is-a-Hot-Topic-but-a-Hazardous-one&quot;&gt;equality in
Java is quite hazardous&lt;/a&gt;, and that you have to be extra careful not to set a
comparison-land-mine off by accident.&lt;/p&gt;</description>
    
    
    
          <comments>http://blog.pwkf.org/post/2009/05/Compare-Efficiently-in-Java-%3A-Embrace-Smart-Comparison#comment-form</comments>
      <wfw:comment>http://blog.pwkf.org/post/2009/05/Compare-Efficiently-in-Java-%3A-Embrace-Smart-Comparison#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.pwkf.org/feed/atom/comments/403789</wfw:commentRss>
      </item>
    
  <item>
    <title>Surrogate Keys : Globally Unique, Application Unique or Type Unique ?</title>
    <link>http://blog.pwkf.org/post/2009/05/Surrogate-Keys-%3A-Globally-Unique-Application-Unique-or-Type-Unique</link>
    <guid isPermaLink="false">urn:md5:d96265934254808d9b64da403c212d83</guid>
    <pubDate>Tue, 19 May 2009 08:00:00 +0200</pubDate>
    <dc:creator>Steve Schnepp</dc:creator>
        <category>database</category>
        <category>database</category><category>sql</category>    
    <description>&lt;p&gt;When you just decided to use Surrogate Keys, another problem arises : which
value should I use ? It seems to be a very simple problem since the key now is
completely in isolation in your application and is not related to any data. The
choice is quite bonding, since a change means that every value has to be
changed, and you did &lt;ins&gt;not&lt;/ins&gt; let this key leak outside the database did
you ?&lt;/p&gt;
&lt;p&gt;The keypoint here is &lt;strong&gt;isolation&lt;/strong&gt;. Many differents kinds of
isolation are available, and soon you will be able to make a informed
decision.&lt;/p&gt;    &lt;h2&gt;Type unique : One sequence per table&lt;/h2&gt;
&lt;p&gt;The most common idiom out there. Just define a surrogate key per table,
usually done via an &lt;code&gt;auto_increment&lt;/code&gt; field, &lt;code&gt;serial&lt;/code&gt;
field or a &lt;code&gt;sequence&lt;/code&gt; per table.&lt;/p&gt;
&lt;p&gt;It is the most natural way of thinking, since the ID is usually seen as a
monotically incrementing counter. Therefore many applications use even the
evil&lt;sup&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/05/#pnote-401164-1&quot; id=&quot;rev-pnote-401164-1&quot; name=&quot;rev-pnote-401164-1&quot;&gt;1&lt;/a&gt;]&lt;/sup&gt; &lt;code&gt;SELECT MAX(ID)+1&lt;/code&gt; construct,
because they specifically require that there should be no missed rows and that
the ID have to be strictly in order.&lt;/p&gt;
&lt;p&gt;The usual implementation of this is to have a kind of thread-local function
that hands unique ID allocated in group and never trying to reuse an already
issued/allocated ID. If you reserve 50 ID at once you directly level the cost
of locking the ressource to only 1 in 50 requests, and not trying to reuse them
enables you to just have to lock the end of the spectrum and not the whole
spectrum. That's what the &lt;code&gt;CACHE&lt;/code&gt; keyword is about in &lt;a href=&quot;http://www.techonthenet.com/oracle/sequences.php&quot; hreflang=&quot;en&quot;&gt;Oracle&lt;/a&gt; and
&lt;a href=&quot;http://www.postgresql.org/docs/8.3/static/sql-createsequence.html&quot; hreflang=&quot;en&quot;&gt;PostgreSQL&lt;/a&gt;. Others RDBMS usually also have a similar
option&lt;/p&gt;
&lt;p&gt;But this optimisation leaves us with a big side-effect : the ID that are
generated are not strictly in order anymore, and furthermore there can be huge
gaps in them. Since one of the main purpose of one ID per table is now gone,
why not directly bite the bullet and use the same ID generation on the the
whole application ?&lt;/p&gt;
&lt;h2&gt;Application unique : One sequence to rule them all&lt;/h2&gt;
&lt;p&gt;This is something I first experienced with &lt;a href=&quot;http://subversion.tigris.org/&quot; hreflang=&quot;en&quot;&gt;Subversion&lt;/a&gt;, coming from a CVS
background : the revision number is repository-wide and not file-wide anymore.
This has some mental drawbacks at first, since you still know that a file of
version 5324 is more recent than version 3200, but not &lt;em&gt;how much&lt;/em&gt; more
since the commits could have been done in another place. The difference is more
visible with 1.4 versus 1.5.&lt;/p&gt;
&lt;p&gt;But as I was more used to it, that thinking was obviously a red herring. It
did divert us that a file could have been completely rewritten in just one
revision, and that one signle line could have be flipped back and forth in
several revisions,&lt;/p&gt;
&lt;p&gt;So the number, and the gap between them, as the basis of the amount of
modifications has its value decreased. What could be quite interesting though
is that the revision number is globally incrementing&lt;sup&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/05/#pnote-401164-2&quot; id=&quot;rev-pnote-401164-2&quot; name=&quot;rev-pnote-401164-2&quot;&gt;2&lt;/a&gt;]&lt;/sup&gt;. That enables us to know that a file in
revision 1000 was commited before another one with revision 2000.&lt;/p&gt;
&lt;p&gt;Back in our database, if we just define &lt;strong&gt;one&lt;/strong&gt; sequence for
all the ID in the database we have 2 advantages :&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;One ID represents only one row. Debugging is easier and when you see an ID
&lt;em&gt;in the wild&lt;/em&gt;, you can think of which row it represents.&lt;/li&gt;
&lt;li&gt;You can establish an almost exact history of sequencing the creations of
the rows.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The inevitable locking performance issue that arises often is made painless
through bigger block allocations.&lt;/p&gt;
&lt;p&gt;One of the lesser known issue is that, usually when using an ID per table,
after a while you begin to &lt;em&gt;know&lt;/em&gt; which ID belongs to wich table since
all the tables are filled at different rates : 50083 is an order ID, whereas
13650239 looks more like a message ID.&lt;/p&gt;
&lt;h2&gt;Globally unique :&lt;/h2&gt;
&lt;p&gt;If you push this logic a little bit further, you can even imagine that all
the ID produced could be unique. Then you will have the benefits of unicity,
but even on the whole IT level. It makes interapplication debugging easier,
since the leaked keys can be nicely tracked down to their origin. How to
generate such an unique ID is tricky question, since the applications usually
are coded in heterogeneous environemments and technologies.&lt;/p&gt;
&lt;h3&gt;A central ID service&lt;/h3&gt;
&lt;p&gt;The most obvious way is to have a central ID service, the same as the unique
sequence in the application. It has to be accessed in a distributed way, and
the implementation can be quite cumbersome thoughout all the different
technologies that exists.&lt;/p&gt;
&lt;p&gt;This service has also to have the highest &lt;a href=&quot;http://en.wikipedia.org/wiki/Service_level_agreement&quot; hreflang=&quot;en&quot;&gt;SLA&lt;/a&gt; as
the applications that depends on it. It becomes then quickly a &lt;a href=&quot;http://en.wikipedia.org/wiki/Single_point_of_failure&quot; hreflang=&quot;en&quot;&gt;SPOF&lt;/a&gt;
if not designed carefully.&lt;/p&gt;
&lt;h3&gt;GUID&lt;/h3&gt;
&lt;p&gt;Since &lt;em&gt;online&lt;/em&gt; ID generation has some serious drawbacks, the easiest
&lt;em&gt;offline&lt;/em&gt; way is to generate &lt;a href=&quot;http://en.wikipedia.org/wiki/Globally_Unique_Identifier&quot; hreflang=&quot;en&quot;&gt;GUID&lt;/a&gt;s, there are many hooks in each technology/application to be a
quite educated move.&lt;/p&gt;
&lt;p&gt;It also has drawbacks, but more in the semantic fields. A GUID is quite
semantically opaque since looking at it does not tell you anything about
where/when the ID was generated. You have to do a full key scan of all the
applications to find its origin.&lt;/p&gt;
&lt;h3&gt;Almost globally unique : Application ID + Application Unique&lt;/h3&gt;
&lt;p&gt;Earlier, we just found out the application-unique ID. If you pair it with an
unique application ID, you just managed to have a globally unique ID. In the
generation of this ID, you just prefix it with the ID of the application, and
tada.... you have your globally unique ID.&lt;/p&gt;
&lt;p&gt;The net effect is that if you see this ID in the wild, you know where it was
created. If you have an increasing one, you can even guess almost
&lt;em&gt;when&lt;/em&gt;.&lt;/p&gt;
&lt;h2&gt;Parallels between applications and networks&lt;/h2&gt;
&lt;p&gt;The more I think about it, the more I can draw parallels between rows in
applications and adresses in networks, but I'll write more on that later in
another article.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot;&gt;
&lt;h4&gt;Notes&lt;/h4&gt;
&lt;p&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/05/#rev-pnote-401164-1&quot; id=&quot;pnote-401164-1&quot; name=&quot;pnote-401164-1&quot;&gt;1&lt;/a&gt;] Evil because it's usually not thread safe and quite
horridly inefficient&lt;/p&gt;
&lt;p&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/05/#rev-pnote-401164-2&quot; id=&quot;pnote-401164-2&quot; name=&quot;pnote-401164-2&quot;&gt;2&lt;/a&gt;] Actually in subversion it has a **strict** incrementing
policy&lt;/p&gt;
&lt;/div&gt;</description>
    
    
    
          <comments>http://blog.pwkf.org/post/2009/05/Surrogate-Keys-%3A-Globally-Unique-Application-Unique-or-Type-Unique#comment-form</comments>
      <wfw:comment>http://blog.pwkf.org/post/2009/05/Surrogate-Keys-%3A-Globally-Unique-Application-Unique-or-Type-Unique#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.pwkf.org/feed/atom/comments/401164</wfw:commentRss>
      </item>
    
  <item>
    <title>Are Excerpts a Good Thing ?</title>
    <link>http://blog.pwkf.org/post/2009/05/Are-Excerpts-a-Good-Thing</link>
    <guid isPermaLink="false">urn:md5:3b3d3b1b6cfb8e4a4c520a8e89d3b51f</guid>
    <pubDate>Mon, 18 May 2009 12:04:00 +0200</pubDate>
    <dc:creator>Steve Schnepp</dc:creator>
        <category>general</category>
            
    <description>    &lt;p&gt;One thing I'm really wondering is : &lt;q&gt;Should I use excerpts&lt;/q&gt; ?&lt;/p&gt;
&lt;p&gt;On the plus side :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The list is easier to read &amp;amp; shorter to parse. You can then just click
on the link that are interesting to you&lt;/li&gt;
&lt;li&gt;I can manage to see which posts are mostly read (useful ?) since the
comments aren't a good indicator&lt;sup&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/05/#pnote-403185-1&quot; id=&quot;rev-pnote-403185-1&quot; name=&quot;rev-pnote-403185-1&quot;&gt;1&lt;/a&gt;]&lt;/sup&gt;&lt;/li&gt;
&lt;li&gt;Mostly all my readers comes from Google. Having a little list and only then
content on a dedicated page seems to help being nicely referenced on more
specifics, hence ususally more relevant topics.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;On the down side :&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The RSS flow and the front page list are truncated. You are one click
further to the whole story.&lt;/li&gt;
&lt;li&gt;It seems quite arrogant to force the reader to come to your site.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I just edited my articles to have an excerpt when it is quite long.&lt;/p&gt;
&lt;p&gt;What do you think ?&lt;/p&gt;
&lt;div class=&quot;footnotes&quot;&gt;
&lt;h4&gt;Notes&lt;/h4&gt;
&lt;p&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/05/#rev-pnote-403185-1&quot; id=&quot;pnote-403185-1&quot; name=&quot;pnote-403185-1&quot;&gt;1&lt;/a&gt;] It is &lt;strong&gt;very&lt;/strong&gt; far from being a very
famous blog with hordes of readers :-p&lt;/p&gt;
&lt;/div&gt;</description>
    
    
    
          <comments>http://blog.pwkf.org/post/2009/05/Are-Excerpts-a-Good-Thing#comment-form</comments>
      <wfw:comment>http://blog.pwkf.org/post/2009/05/Are-Excerpts-a-Good-Thing#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.pwkf.org/feed/atom/comments/403185</wfw:commentRss>
      </item>
    
  <item>
    <title>Bringing C++ Const to Java</title>
    <link>http://blog.pwkf.org/post/2009/05/Bringing-C-Const-to-Java</link>
    <guid isPermaLink="false">urn:md5:af70fc2fb014c6e82ae871a3d3f18a07</guid>
    <pubDate>Wed, 06 May 2009 11:15:00 +0200</pubDate>
    <dc:creator>Steve Schnepp</dc:creator>
        <category>java</category>
            
    <description>&lt;p&gt;&lt;em&gt;Constness&lt;/em&gt; is another C++ idiom, like RAII that I talked about
&lt;a href=&quot;http://blog.pwkf.org/post/2008/07/27/RAII-in-Java-to-clean-your-code&quot;&gt;earlier&lt;/a&gt;. We can
then write code that are side effects free : when a function is called with a
&lt;code&gt;const&lt;/code&gt; argument, we are assured that this argument will not be
modified &lt;em&gt;under the hood&lt;/em&gt; without us knowing&lt;sup&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/05/#pnote-397655-1&quot; id=&quot;rev-pnote-397655-1&quot; name=&quot;rev-pnote-397655-1&quot;&gt;1&lt;/a&gt;]&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;In Java, the commonly accepted way is to use the &lt;code&gt;final&lt;/code&gt; keyword.
But it has a major drawback : the object cannot be &lt;em&gt;redefined&lt;/em&gt;, but can
be &lt;em&gt;modified&lt;/em&gt; by calling mutable members. You have to convert it to an
immutable type. This is a simple task, but radically different ways exists.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot;&gt;
&lt;h4&gt;Notes&lt;/h4&gt;
&lt;p&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/05/#rev-pnote-397655-1&quot; id=&quot;pnote-397655-1&quot; name=&quot;pnote-397655-1&quot;&gt;1&lt;/a&gt;] Of course, C++ being what it is, there always will be
ways. But let's say it's not as tempting to do as otherwise.&lt;/p&gt;
&lt;/div&gt;    &lt;p&gt;In Java, the commonly accepted way is to use the &lt;code&gt;final&lt;/code&gt; keyword.
But it has a major drawback : the object cannot be &lt;em&gt;redefined&lt;/em&gt;, but can
be &lt;em&gt;modified&lt;/em&gt; by calling mutable members. You have to convert it to an
immutable type. This is a simple task, but radically different ways exists.&lt;/p&gt;
&lt;h2&gt;Achieve Immutable in Java&lt;/h2&gt;
&lt;p&gt;We'll see some differents methods with a common example. Let's start with a
very simple &lt;code&gt;Point&lt;/code&gt; class :&lt;/p&gt;
&lt;pre&gt;
public class Point {
    private int x;
    private int y;
    void setX(int i) { this.x = i; }
    void setY(int i) { this.y = i; }
    int getX() { return this.x; }
    int getY() { return this.y; }
}
&lt;/pre&gt;
&lt;h3&gt;The &lt;em&gt;Java Collection way&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;It is the easiest way. It is designed to be a drop-in, like in the Java
Collection API. You just create a read-only class that derives your read-write
class just like the &lt;code&gt;PointReadOnly&lt;/code&gt; that mimics the r/o List. It
overrides all the methods either to delegate or to forbid calling by throwing a
run-time exception.&lt;/p&gt;
&lt;pre&gt;
public class PointReadOnly extends Point {
    private final Point inner;
    public PointReadOnly(Point p) { this.inner = p }; 

    void setX(int i) { 
        throw new UnsupportedOperationException(&amp;quot;PointReadOnly is read-only&amp;quot;); 
    }
    void setY(int i) {
        throw new UnsupportedOperationException(&amp;quot;PointReadOnly is read-only&amp;quot;);
    }

    int getX() { return inner.getX(); }
    int getY() { return inner.getY(); }
}
&lt;/pre&gt;
&lt;h4&gt;Advantages&lt;/h4&gt;
&lt;p&gt;You can use it &lt;strong&gt;right-now&lt;/strong&gt; : Your code can slowly evolve to
progressively generate and integrate this new object without really knowing
it's here. The misuses, that are changing the object, will be catched in the
testing phase when exceptions are being thrown. The use of a good logging
system in the setters is a nice addition.&lt;/p&gt;
&lt;h4&gt;Disadvantages&lt;/h4&gt;
&lt;p&gt;Any pedantic compiler should warn you that the setter parameter isn't read.
I tried to be quite clever and find ways to ignore it. Just rewrite it as :&lt;/p&gt;
&lt;pre&gt;
    void setX(int i) { 
        if (i == 0) { /* Do nothing but keep the compiler happy */ }
        throw new UnsupportedOperationException(&amp;quot;PointReadOnly is read-only&amp;quot;); 
    }
&lt;/pre&gt;
&lt;p&gt;The performance penalty is minimal since any compiler should optimize the
if-case away and even if not, &lt;strong&gt;this function should not be called
anyway&lt;/strong&gt;.&lt;/p&gt;
&lt;p&gt;But &lt;a href=&quot;http://stackoverflow.com/questions/786740/how-to-avoid-the-unused-param-warning-when-overriding-a-method-in-java-1-4/786794#786794&quot; hreflang=&quot;en&quot;&gt;as a user on StackOverflow pointed&lt;/a&gt;, that warning
&lt;em&gt;definitively&lt;/em&gt; indicates a code smell : &lt;strong&gt;this hierarchy blatantly
violates Liskov's principle of substitution&lt;/strong&gt; since a class receiving an
instance of a &lt;code&gt;Point&lt;/code&gt; expects &lt;code&gt;setX()&lt;/code&gt; to work, and may
not handle this exception correctly. Obviously a read-writeable class is
readable, but a readable class is not necessarly a read-writeable class. This
may introduce subtle bugs that would only be catched at runtime, and therefore
testing, as simple as it may be, has to be quite thorough in order not to be
bitten at a inconvenient time.&lt;/p&gt;
&lt;p&gt;A good solution that respects Liskov's principle is to redesign the
hierarchy, we'll talk about it just later. But right now, don't despair, you
can have almost the best of the both world : simple, yet &lt;em&gt;almost&lt;/em&gt;
respecting Liskov's principle.&lt;/p&gt;
&lt;h4&gt;Circus time : ride two horses at once&lt;/h4&gt;
&lt;p&gt;Actually what violates Liskov's principle is that the setters don't do the
job they are entitled to do. Throwing an exception is a sure way to
&lt;strong&gt;ensure&lt;/strong&gt; that the object won't be modified, but that is usually
overkill&lt;sup&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/05/#pnote-397655-1&quot; id=&quot;rev-pnote-397655-1&quot; name=&quot;rev-pnote-397655-1&quot;&gt;1&lt;/a&gt;]&lt;/sup&gt;. If you just log the erroneous call, but
still do what it should do, you have the warnings, but not the nasty
consequences. Just take care to log the callstack also, in order to be able to
fix it much more easily.&lt;/p&gt;
&lt;p&gt;The new method becomes then :&lt;/p&gt;
&lt;pre&gt;
void setX(int i) { 
    Log.warn(new UnsupportedOperationException(&amp;quot;PointReadOnly is read-only&amp;quot;)); 
    return inner.setX(i);
}
&lt;/pre&gt;
&lt;p&gt;A notable side effect is that your class isn't really read-only anymore
though, just complain-only.&lt;/p&gt;
&lt;h3&gt;The &lt;em&gt;Object-Oriented way&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;A much better solution is to create a read-only super class that the
read-write one would inherit. The read-only class only implements getters (or
any non-changing method). The read-write complement them with the setters.&lt;/p&gt;
&lt;p&gt;It would lead to code like :&lt;/p&gt;
&lt;pre&gt;
public class PointReadOnly {
    private final Point inner;
    public PointReadOnly(Point p) { this.inner = p }; 

    int getX() { return inner.getX(); }
    int getY() { return inner.getY(); }
}

public class Point extends PointReadOnly {
    private int x;
    private int y;
    void setX(int i) { this.x = i; }
    void setY(int i) { this.y = i; }
    int getX() { return this.x; }
    int getY() { return this.y; }
}
&lt;/pre&gt;
&lt;h4&gt;Advantages&lt;/h4&gt;
&lt;p&gt;Now &lt;strong&gt;Liskov's principle is not violated anymore&lt;/strong&gt; and code
that expects a read-only class can seamlessly have a r/o or a r/w class.&lt;/p&gt;
&lt;p&gt;The changing will also spread naturally all throughout the codebase, just
like the C++ &lt;code&gt;const&lt;/code&gt; modifier and failures to comply would be caught
at &lt;strong&gt;compile time&lt;/strong&gt; and not at runtime.&lt;/p&gt;
&lt;p&gt;The r/o is a nice wrapper for the read-only class, and a r/w can even be
used in place of the r/o place.&lt;/p&gt;
&lt;h4&gt;Disadvantages&lt;/h4&gt;
&lt;p&gt;It doesn't &lt;em&gt;feel&lt;/em&gt; right :The r/o class is just a placeholder for a
r/w class.&lt;/p&gt;
&lt;h4&gt;Comments&lt;/h4&gt;
&lt;p&gt;An interface-base inheritance scheme can even be better. &lt;code&gt;Point&lt;/code&gt;
as the r/o interface, &lt;code&gt;PointMutable&lt;/code&gt; as the r/w and
&lt;code&gt;PointBase&lt;/code&gt; as the base implentation class. This way you can avoid
the wrapper class. You also get the C++ &lt;code&gt;const_cast&amp;lt;&amp;gt;()&lt;/code&gt; for
free (by just downcasting as &lt;code&gt;PointMutable&lt;/code&gt;).&lt;/p&gt;
&lt;pre&gt;
interface Point { 
        int getX(); 
        int getY(); 
}
interface PointMutable extends Point { 
        void setX(int i); 
        void setY(int i);
}
class PointBase implements Point { 
    private int x;
    private int y;
    void setX(int i) { this.x = i; }
    void setY(int i) { this.y = i; }
    int getX() { return this.x; }
    int getY() { return this.y; }
}
&lt;/pre&gt;
&lt;p&gt;I realize this does not answer your question about getting rid of the
warnings. But warnings can either be suppressed, ignored, or addressed. An
unused parameter is often a bad smell that indicates your method might not be
doing what it's expected to do. Methods should only get essential parameters.
If the parameter is not used, the parameter is not essential, and therefore
something needs to be changed.&lt;/p&gt;
&lt;h3&gt;The &lt;em&gt;&lt;code&gt;String&lt;/code&gt; way&lt;/em&gt;&lt;/h3&gt;
&lt;p&gt;Another way is just to avoid the read-only and mutable API all together and
go the functional way. Since it's a completely different paradigm, it doesn't
exactly fits the bill here but it's a quite important way of &lt;a href=&quot;http://en.wikipedia.org/wiki/Gordian_Knot&quot; hreflang=&quot;en&quot;&gt;cutting the gordian
knot&lt;/a&gt;. Its concepts are rooted in both functional programming and RAII. It
is quite practical only if you have an efficient garbage collection system,
since it has the tendency to create lots of temporary objets that are mostly
created, used and forgot.&lt;/p&gt;
&lt;p&gt;Do do thing, you have to redesign the whole class, in order to be able to
defined it only once, and when you call a modifying method, you end up with an
other object, also immutable. The &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Integer&lt;/code&gt; and
&lt;code&gt;Class&lt;/code&gt; class make extensive usage of this.&lt;/p&gt;
&lt;p&gt;So, our class will become :&lt;/p&gt;
&lt;pre&gt;
class final Point {
    private final int x;
    private final int y;
    Point(int x, int y) { this.x = x; this.y = y; }
    int getX() { return this.x; }
    int getY() { return this.y; }

    void setX(int i) { return new Point(i, this.y); }
    void setY(int i) { return new Point(this.x, i); }
}
&lt;/pre&gt;
&lt;p&gt;As you can see, every object is not modifiable, if a method recieves the
object as an argument, it cannot modify it, since it would have a new object.
The properties are final, and the class has usually to be made final in order
not to be able to mutate the class by inheritance.&lt;/p&gt;
&lt;p&gt;This enables programming without side-effect and therefore is quite
interesting for multi-thread programming.&lt;/p&gt;
&lt;h2&gt;Conclusion &amp;amp; thoughts&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;UnsupportedOperationException&lt;/code&gt; is a very practical idiom,
that even the Java Collection API is based on it. It may not be very nice in
theory, but in practice it has its usages. It should not be abused thought, and
the &lt;em&gt;OO&lt;/em&gt;-way should be prefered when possible, usually when you are in
control of all the code base.&lt;/p&gt;
&lt;p&gt;The logging approch doesn't really solve the problem since it's only
advisory, but can fit the bill when the code base is huge and you want to be
able to understand what is going wrong.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;OO&lt;/em&gt;-way is a nice compromise between the abstract beauty of the
String-way and the quite hugly kludge of the &lt;em&gt;Collection&lt;/em&gt;-way,
especially when used with the interfaces scheme : it spreads out as the
&lt;code&gt;const&lt;/code&gt; param, and force the coder to think if a modifying is really
needed.&lt;/p&gt;
&lt;p&gt;The &lt;em&gt;String&lt;/em&gt;-way is very tempting, as a true create-and-forget way.
If you could even enforce to only have one instance of each representation
you'll have a free &lt;code&gt;==&lt;/code&gt; (almost) overloaded operator&lt;sup&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/05/#pnote-397655-2&quot; id=&quot;rev-pnote-397655-2&quot; name=&quot;rev-pnote-397655-2&quot;&gt;2&lt;/a&gt;]&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;So we have at least 3 ways to learn and adapt idioms from C++ in Java. The
major issue is that it's not as itegrated as &lt;code&gt;const&lt;/code&gt; : every object
needs custom code except maybe the &lt;em&gt;String&lt;/em&gt;-way.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot;&gt;
&lt;h4&gt;Notes&lt;/h4&gt;
&lt;p&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/05/#rev-pnote-397655-1&quot; id=&quot;pnote-397655-1&quot; name=&quot;pnote-397655-1&quot;&gt;1&lt;/a&gt;] It all depends on the indent, specific policies on
specific usages.&lt;/p&gt;
&lt;p&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/05/#rev-pnote-397655-2&quot; id=&quot;pnote-397655-2&quot; name=&quot;pnote-397655-2&quot;&gt;2&lt;/a&gt;] Just be aware of classloader issue with
&lt;code&gt;static&lt;/code&gt;. Hmmm that will be covered in another article.&lt;/p&gt;
&lt;/div&gt;</description>
    
    
    
          <comments>http://blog.pwkf.org/post/2009/05/Bringing-C-Const-to-Java#comment-form</comments>
      <wfw:comment>http://blog.pwkf.org/post/2009/05/Bringing-C-Const-to-Java#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.pwkf.org/feed/atom/comments/397655</wfw:commentRss>
      </item>
    
  <item>
    <title>Databases: Partial Indexing</title>
    <link>http://blog.pwkf.org/post/2009/04/Databases%3A-Partial-Indexing</link>
    <guid isPermaLink="false">urn:md5:fe367e501403ec6757c2f38071091a79</guid>
    <pubDate>Fri, 17 Apr 2009 17:05:00 +0200</pubDate>
    <dc:creator>Steve Schnepp</dc:creator>
        <category>database</category>
        <category>database</category><category>sql</category>    
    <description>&lt;p&gt;In huge tables, usually only a fraction of the table is used on a regular
basis. The rest is useful either for historical purpose, or is just not being
treated right now.&lt;/p&gt;    &lt;p&gt;Let's take the table that contains orders that we created in &lt;a href=&quot;http://blog.pwkf.org/post/2009/03/Databases%3A-Meta-Data-ctime-mtime&quot;&gt;my previous post&lt;/a&gt; to show
you some examples.&lt;/p&gt;
&lt;p&gt;Orders have usually a state, Let's create it &lt;sup&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/04/#pnote-391276-1&quot; id=&quot;rev-pnote-391276-1&quot; name=&quot;rev-pnote-391276-1&quot;&gt;1&lt;/a&gt;]&lt;/sup&gt; :&lt;/p&gt;
&lt;pre&gt;
ALTER TABLE ORDERS ADD COLUMN STATE
&lt;/pre&gt;
&lt;p&gt;Now usually a application makes use of the state of the order by querying
it, even quite often. The main problem is that this query will do a full scan
of the table unless the state field is indexed. Here comes the other issue :
indexes take very much space since they have to be ordered and cannot be
trimmed (except throw an expensive DROP/CREATE or REINDEX).&lt;/p&gt;
&lt;p&gt;The space used is adequate if the index has many different values, but
usually a state has only a finite set of values, and usually some are very
transient and other quite long. Compare for example the state CANCELLED or
SHIPPED that are mostly final to the very transient state
INVOICE_TO_BE_PRINTED.&lt;/p&gt;
&lt;p&gt;The number of orders that are in the state INVOICE_TO_BE_PRINTED are quite a
few (especially compared to the final states). However these temporary states
are the mostly queried (usually by batches, pollers or even web pages that
present the workload to the users for &lt;em&gt;manual polling&lt;/em&gt;).&lt;/p&gt;
&lt;p&gt;Another overlooked usage is to have an &lt;em&gt;ALMOST&lt;/em&gt;-UNIQUE index. I mean
an unique index, but only on selected entries. In our ORDERS table, we can add
a field that represent the PRINTING_PID of the process that is responsible for
printing the invoice. It is only unique for the orders that are in the
INVOICE_TO_BE_PRINTED state. A normal index isn't much of a help here since
either you have to clear it afterwards, but you loose information, or you
cannot enforce any uniqueness.&lt;/p&gt;
&lt;p&gt;So there comes the PARTIAL INDEX. It is native in some
databases&lt;sup&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/04/#pnote-391276-2&quot; id=&quot;rev-pnote-391276-2&quot; name=&quot;rev-pnote-391276-2&quot;&gt;2&lt;/a&gt;]&lt;/sup&gt; but in most it isn't. Triggers comes then to
the rescue. Some are not really fond of triggers since many things can happen
undercover, but my moto is to use trigger in order to, and only to, maintain
business-agnostic extra datas (usually to cope automatically with
denormalization). Triggers shall not &lt;strong&gt;change&lt;/strong&gt; the data, merely
copy and/or move it around.&lt;/p&gt;
&lt;p&gt;The new &lt;em&gt;almost-unique&lt;/em&gt; column is also created :&lt;/p&gt;
&lt;pre&gt;
ALTER TABLE ORDERS ADD COLUMN PRINTING_ID
&lt;/pre&gt;
&lt;h2&gt;Implementations&lt;/h2&gt;
&lt;h3&gt;Reducing the index size and the overhead&lt;/h3&gt;
&lt;p&gt;So, a new table is created to contain the partial index :&lt;/p&gt;
&lt;pre&gt;
TABLE PI_ORDERS_STATE ( 
   INT ORDERS_ID FOREIGN KEY ON ORDERS(ORDERS_ID),
   STATE, 
   INDEX ON (STATE)
)
&lt;/pre&gt;
&lt;p&gt;And the insertion/deletion for the table is done via triggers. I do it a
little rapidly since the real creation is a little more tedious than the
pseudo-code here :&lt;/p&gt;
&lt;pre&gt;
CREATE TRIGGER ON ORDERS WHERE INSERT, UPDATE
  WHEN o.STATE not in (PENDING, CANCELLED, SHIPPED) 
    INSERT INTO PI_ORDERS_STATE(o.ORDERS_ID, o.STATE) IF ORDERS_ID NOT PRESENT
  ELSE
    DELETE FROM PI_ORDERS_STATE 
      WHERE ORDERS_ID = o.ORDERS_ID
&lt;/pre&gt;
&lt;p&gt;One should rewrite queries to take advantage of this partial index since
when it's integrated in the database the optimizer just does it himself. A
query like this&lt;/p&gt;
&lt;p&gt;Old :&lt;/p&gt;
&lt;pre&gt;
SELECT * FROM ORDERS WHERE ORDERS.STATE = INVOICE_TO_BE_PRINTED
&lt;/pre&gt;
&lt;p&gt;New :&lt;/p&gt;
&lt;pre&gt;
SELECT o.* FROM ORDERS o
INNER JOIN PI_ORDERS_STATE p ON p.ORDERS_ID = o.ORDERS_ID p.STATE = INVOICE_TO_BE_PRINTED
&lt;/pre&gt;
&lt;p&gt;The query optimizer should now be able to use the small index in
PI_ORDERS_STATE, and retrieve only the relevant orders via their primary key in
ORDERS.&lt;/p&gt;
&lt;h3&gt;Almost-unique colums&lt;/h3&gt;
&lt;p&gt;To handle the almost-unique column, it's basically the same idea : a new
table, new triggers :&lt;/p&gt;
&lt;pre&gt;
TABLE PI_ORDERS_PRINTING_PID ( 
   INT ORDERS_ID FOREIGN KEY ON ORDERS(ORDERS_ID),
   PRINTING_PID, 
   UNIQUE INDEX ON (PRINTING_PID)
)
&lt;/pre&gt;
&lt;pre&gt;
CREATE TRIGGER ON ORDERS WHERE INSERT, UPDATE
  WHEN o.STATE = INVOICE_TO_BE_PRINTED
    INSERT INTO PI_ORDERS_PRINTING_PID(o.ORDERS_ID, o.PRINTING_PID) IF ORDERS_ID NOT PRESENT
  ELSE
    DELETE FROM PI_ORDERS_PRINTING_PID
      WHERE ORDERS_ID = o.ORDERS_ID
&lt;/pre&gt;
&lt;p&gt;No need to rewrite any query here.&lt;/p&gt;
&lt;div class=&quot;footnotes&quot;&gt;
&lt;h4&gt;Notes&lt;/h4&gt;
&lt;p&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/04/#rev-pnote-391276-1&quot; id=&quot;pnote-391276-1&quot; name=&quot;pnote-391276-1&quot;&gt;1&lt;/a&gt;] Notice that here I don't create all the meta-data
stuff. That is left as an exercise to the reader - Yeah, I
&lt;strong&gt;always&lt;/strong&gt; wanted to say that...&lt;/p&gt;
&lt;p&gt;[&lt;a href=&quot;http://blog.pwkf.org/post/2009/04/#rev-pnote-391276-2&quot; id=&quot;pnote-391276-2&quot; name=&quot;pnote-391276-2&quot;&gt;2&lt;/a&gt;] MS-SQL has it but calls it Filtered Index. PostgreSQL
calls it PARTIAL INDEX. )&lt;/p&gt;
&lt;/div&gt;</description>
    
    
    
          <comments>http://blog.pwkf.org/post/2009/04/Databases%3A-Partial-Indexing#comment-form</comments>
      <wfw:comment>http://blog.pwkf.org/post/2009/04/Databases%3A-Partial-Indexing#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.pwkf.org/feed/atom/comments/391276</wfw:commentRss>
      </item>
    
  <item>
    <title>Email Ping to Comments Reply on Blogs</title>
    <link>http://blog.pwkf.org/post/2009/04/Email-Ping-to-Comments-Reply-on-Blogs</link>
    <guid isPermaLink="false">urn:md5:17585b5c7e824093b7a2ab870cf4a33d</guid>
    <pubDate>Fri, 10 Apr 2009 12:46:00 +0200</pubDate>
    <dc:creator>Steve Schnepp</dc:creator>
        <category>general</category>
            
    <description>    &lt;p&gt;As a blog writer I can receive the comments written on my blog via
email.&lt;/p&gt;
&lt;p&gt;I'm wondering why this service isn't implemented for replies to my comments
on foreign blogs. I don't really want to remember all the blogs I left a
comment on in order to poll them to see if there is a reply to my comment.&lt;/p&gt;
&lt;p&gt;Actually, a quick hack would be to subscribe to the RSS feed of this
particular entry and let the RSS feeder handle all the remembrance and polling
for us, but that seems so &lt;em&gt;inneffective&lt;/em&gt;. Just imagine all the wasted
traffic induced by polling if this habit becomes mainstreams.&lt;/p&gt;
&lt;p&gt;I thought about turning it the other way around, since it seems that I'm
forced to leave an email adress on each comment I leave, why don't the blogging
engines just use this adress to send a notification (maybe opt-in) if there is
a activity ?&lt;/p&gt;
&lt;p&gt;I wanted to activate this feature on my blog and it seems non-existant. I
therefore searched around quickly only to discover that noone seems to be
having this particular need.&lt;/p&gt;
&lt;p&gt;Strange...&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Edit(15/04/2009)&lt;/strong&gt;: I just found out that WordPress seems to
support this functionality via a plugin : &lt;a href=&quot;http://txfx.net/code/wordpress/subscribe-to-comments/&quot; hreflang=&quot;en&quot;&gt;Subscribe
to Comments&lt;/a&gt;&lt;/p&gt;</description>
    
    
    
          <comments>http://blog.pwkf.org/post/2009/04/Email-Ping-to-Comments-Reply-on-Blogs#comment-form</comments>
      <wfw:comment>http://blog.pwkf.org/post/2009/04/Email-Ping-to-Comments-Reply-on-Blogs#comment-form</wfw:comment>
      <wfw:commentRss>http://blog.pwkf.org/feed/atom/comments/391836</wfw:commentRss>
      </item>
    
</channel>
</rss>