Tuesday, April 28, 2009

Is RIFE dead?

I wrote a blog entry in April 2007 about RIFE. It has been 2 years since I wrote that entry. I had mentioned that RIFE was lacking in documention and the learning curve was pretty steeep. That was one of the resons why I decided not to investigate further. Geert tried to calrify some of the questions I had, but that didn't really remove all the doubts I had. I know that Geert has moved on to bigger and better things. The framework seems to be dead now as there hasn't been any new releases since 2007. This goes to show that with too much complexity, it is very difficult to build a community that can sustain the project beyond the original founders. It was a framework built on some very good concepts and I am sad to see it go. I do see some nightly snapshots, but no new releases. Is RIFE really dead?

Saturday, January 17, 2009

KDE free and happy

I wrote in a previous post from October 20008 that I was very unhappy with the direction of KDE. The Kubuntu Intrepid was about to be released with KDE 4 as the default desktop. I got a lot of responses saying that I could still continue using KDE 3.x or I should switch to another distro like OpenSuse. Neither of those were really feasible options as I didn't want to be stuck on KDE 3.x and switching distro was even more a radical change.

When Kubuntu Intrepid released, I downloaded it and gave it a try. I knew immediately that I could no longer use it as my desktop. It was sluggish and not very user friendly. I promptly switched to Ubuntu Intrepid with Gnome desktop. I must admit that Gnome implementation in Ubuntu feels a lot more tightly integrated than KDE was in Kubuntu.

I did have to adjust myself to Gnome initially. As an example, I was very fond of Superkaramba on KDE. Gnome does have comparable desklet and screenlet implememtations. But they just do not seem as seemless as Superkaramba. So, I decided to stick with regular panel applets and I must admit that I somehow like them better than full fledged Superkaramba widgets. This could be true with Gnome in general as everything seems intuitive and simple. Gnome doesn't try to emulate Windoze like KDE. I got the native drivers for the Nvidia card installed and now have Compiz enabled. So, now I have the simplicity of Gnome with added 3D effects. It's not a scientific observation, but the memory management also seems better on Gnome than KDE.

The moral of the story is that I am KDE free and happy.

Sunday, October 19, 2008

Is this the end of KDE for me?

I have been a linux user since early days. In fact for home use I only have Linux and Mac OSX. I have used RedHat, Fedora and Mandrake/Mandriva in the past. Then I discovered Ubuntu few years back and it’s been no looking back. I have used both Ubuntu and Kubuntu extensively. But my destop of choice for home computer has been Kubuntu as I have alwyas felt that KDE has better tooling than GNOME. I like the simplicity of GNOME, but KDE always had more applications.

I love how KDE evloved and matured in 3.x versions. But, then I think the focus shifted to make it look and behave more like Windows (especially Vista with Plasma L&;) for version 4. That’s where my frustration with KDE comes from. I have given it a try a number of times since early beta days and I have been disappointed every single time. My main frustrations have been:

  • It’s horribly slow compared to 3.x versions.

  • It takes more memory to run. In fact it pretty much takes same memory as you would require for Windows XP.

  • The Desktop is ugly. I am pretty happy some simple icons on the desktop. I don’t need fancy effects. There is a value in simplicty and I wish KDE devleopers learnt this from Mac OSX.

  • I have been using Superkaramba for a long time and love it compared to KDE applets. In version 4, these are replaced by widgets. It just seems like a step in wrong direction as it has fewer widgets and they are horrible.

  • Every time I tried to customize the desktop and L&F, it ended up screwing up everything.

  • The main KDE menu is almost unusable. I don’t understand how it is more user friendly

  • Same applies to Dolphin file manager. I have Dolphin on KDE 3.x as well, but again there isn’t anything mind blowing.


The release of Ubuntu 8.10 is almost here and Kubuntu is going to make KDE 4 as the default desktop. I am seriously considering switching to GNOME or Xfce going forward. The purpose of a desktop is make the life easier for a user and that has not been my impression of KDE 4. May be GNOME was right all along in the philosophy of keeping things simple and usable.

Tuesday, January 1, 2008

POX (Plain Old XML) JAX-WS service using CXF and Spring Configuration

The new and upcoming Apache CXF framework is quickly gaining steam and rightfully so. It's very intuitive, simple to use and functional. Some of it comes from the fact the its roots are in popular XFire and Celtix frameworks. It's still in the incubation stage at Apache but the current releases are pretty stable. The documentation is not complete right now, but it's improving as well.

One of the cases where I found the document and example lacking is the POX (Plain Old XML) service using JAX-WS Provider model and configuring it using Spring. The examples are mainly focussed towards configuring and starting the service from Java code. So, I created this simple EchoService example that shows step by step how to do it.

First step is to create the service Java class that will echo the incoming XML message back to the requester. We will also annotate this class using the JAX-WS annotation to mark it as a web service provider and the message type to be the payload. So, the CXF implementation knows that it should only deliver the message payload to the service class. The CXF binding layer processes any binding level wrappers and headers.



/**
 * EchoService.java Jan 1, 2008
 */
package test.jws.provider;

import javax.xml.transform.dom.DOMSource;
import javax.xml.ws.Provider;
import javax.xml.ws.Service;
import javax.xml.ws.ServiceMode;
import javax.xml.ws.WebServiceProvider;

/**
 * <p>
 * A sample EchoService POX service using the JAX-WS Provider interface.
 * </p>
 * @author Atif Khan
 * @since Jan 1, 2008
 */
@WebServiceProvider()
@ServiceMode(value = Service.Mode.PAYLOAD)
public class EchoService
  implements Provider<DOMSource>
{

  /**
   * <p>
   * Process the incoming DOM request and echo it back.
   * </p>
   * @param request
   *          DOMsource representing the request source
   * @return DOMsource representing the response source
   * @see javax.xml.ws.Provider#invoke(java.lang.Object)
   */
  public DOMSource invoke( DOMSource request )
  {
    System.out.println( "EchoService received the POX request." );

    return request;
  }

}



In the above class we specified that we want to receive the incoming XML message as DOM. You can change it to use any concrete subclass of the Source interface (e.g. SAXSource).

Next step is to create the Spring configuration. For simplicity sake, let's keep the name of this file to applicationContext.xml. Here is an example:


<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:jaxws="http://cxf.apache.org/jaxws"
  xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">

  <import resource="classpath:META-INF/cxf/cxf.xml" />
  <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
  <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
  <import resource="classpath:META-INF/cxf/cxf-extension-xml.xml" />
  <import resource="classpath:META-INF/cxf/cxf-extension-http-binding.xml" />

  <jaxws:endpoint id="echoService" implementor="test.jws.provider.EchoService"
    address="/echo" bindingUri="http://www.w3.org/2004/08/wsdl/http">
    <jaxws:serviceFactory>
      <bean class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">
        <property name="wrapped" value="true" />
      </bean>
    </jaxws:serviceFactory>
  </jaxws:endpoint>
  
</beans>



The key things to notice in the above XML are:

  • Address of the service (/echo).
  • Binding URI (http://www.w3.org/2004/08/wsdl/http). This is really important. This is what tells CXF that this service is going to be simple XML over HTTP.

  • Declaration of service factor where we indicate that the messages are going to be of wrapped nature
All that's remaining is to declare the Spring context listener in your web.xml for the web application that this service is going to be a part of. Here is an example of the web.xml:

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
         version="2.4">

  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <servlet>
    <display-name>CXF Servlet</display-name>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>CXFServlet</servlet-name>
    <url-pattern>/*</url-pattern>
  </servlet-mapping>
</web-app>



As you can see that there is nothing special here at all. It's all standard Spring configuration using the context loader listener. Also, we are declaring the CXF servlet here and mapping it to /* to make all the requests to this web application go through the CXF servlet.

Now all you have to do is to package this web application and deploy it in your favorite application server. You should be able to access the Echo Service we wrote at http://localhost:8080/AppName/echo (you will have to adjust the port and application name in the URL). Now HTTP post a XML to this URL and you should get the request echoed back to you.

Maven POM
I think it's more useful to also specify the Maven POM that contains all the dependencies as well to build the run this example. Please keep in mind that some of the dependencies here may not be required anymore as I was playing around with a lot of CXF functionalities.

 < ?xml version="1.0" encoding="UTF-8"?>
 <project>
   <modelversion>4.0.0</modelversion>
   <groupid>CXFTest</groupid>
   <artifactid>CXFTest</artifactid>
   <packaging>war</packaging>
   <version>0.0.1</version>
   <repositories>
     <repository>
       <id>apache.incubating.releases</id>
       <name>
         Apache Incubating Release Distribution Repository  
       </name>
       <url>
         http://people.apache.org/repo/m2-incubating-repository  
       </url>
     </repository>
   </repositories>
   <properties>
     <spring .version>2.0.7</spring>
     <cxf .version>2.0.3-incubator</cxf>
   </properties>
   <dependencies>
     <!--
     Spring is directly included to override the version 2.0.4 cxf 
     brings in its own dependencies.  This is not strictly necessary but
     just being shown to let you know how this is done.
     -->
     <dependency>
       <groupid>org.springframework</groupid>
       <artifactid>spring-core</artifactid>
       <version>${spring.version}</version>
     </dependency>
     <dependency>
       <groupid>org.springframework</groupid>
       <artifactid>spring-beans</artifactid>
       <version>${spring.version}</version>
     </dependency>
     <dependency>
       <groupid>org.springframework</groupid>
       <artifactid>spring-context</artifactid>
       <version>${spring.version}</version>
     </dependency>
     <dependency>
       <groupid>org.springframework</groupid>
       <artifactid>spring-web</artifactid>
       <version>${spring.version}</version>
     </dependency>
     <dependency>
       <groupid>log4j</groupid>
       <artifactid>log4j</artifactid>
       <version>1.2.14</version>
     </dependency>
     <!-- Depending on your requirements you may need more or less modules from cxf -->
     <dependency>
       <groupid>org.apache.cxf</groupid>
       <artifactid>cxf-rt-core</artifactid>
       <version>${cxf.version}</version>
     </dependency>
     <dependency>
       <groupid>org.apache.cxf</groupid>
       <artifactid>cxf-rt-frontend-simple</artifactid>
       <version>${cxf.version}</version>
     </dependency>
     <dependency>
       <groupid>org.apache.cxf</groupid>
       <artifactid>cxf-rt-frontend-jaxws</artifactid>
       <version>${cxf.version}</version>
     </dependency>
     <dependency>
       <groupid>org.apache.cxf</groupid>
       <artifactid>cxf-rt-databinding-aegis</artifactid>
       <version>${cxf.version}</version>
     </dependency>
     <dependency>
       <groupid>org.apache.cxf</groupid>
       <artifactid>cxf-rt-transports-local</artifactid>
       <version>${cxf.version}</version>
     </dependency>
     <dependency>
       <groupid>org.apache.cxf</groupid>
       <artifactid>cxf-rt-transports-http</artifactid>
       <version>${cxf.version}</version>
     </dependency>
     <dependency>
       <groupid>org.apache.cxf</groupid>
       <artifactid>cxf-rt-transports-http-jetty</artifactid>
       <version>${cxf.version}</version>
     </dependency>
     <dependency>
       <groupid>org.apache.cxf</groupid>
       <artifactid>cxf-rt-transports-jms</artifactid>
       <version>${cxf.version}</version>
     </dependency>
     <dependency>
       <groupid>org.apache.cxf</groupid>
       <artifactid>cxf-rt-management</artifactid>
       <version>${cxf.version}</version>
     </dependency>
     <dependency>
       <groupid>org.apache.cxf</groupid>
       <artifactid>cxf-common-utilities</artifactid>
       <version>${cxf.version}</version>
     </dependency>
     <dependency>
       <groupid>org.mortbay.jetty</groupid>
       <artifactid>jetty</artifactid>
       <version>6.1.5</version>
     </dependency>
     <dependency>
       <groupid>junit</groupid>
       <artifactid>junit</artifactid>
       <version>3.8.2</version>
       <scope>test</scope>
     </dependency>
     <dependency>
       <groupid>org.apache.cxf</groupid>
       <artifactid>cxf-rt-bindings-http</artifactid>
       <version>${cxf.version}</version>
     </dependency>
   </dependencies>
   <build>
     <plugins>
       <plugin>
         <artifactid>maven-compiler-plugin</artifactid>
         <configuration>
           <source>1.6</source>
           <target>1.6</target>
         </configuration>
       </plugin>
     </plugins>
   </build>
 </project>

Here is a list of all the jar files that ended up being in the war file from the build using above Maven POM. Again, some of the jars here may not be needed.


  1. XmlSchema-1.3.2.jar                               jaxb-impl-2.0.5.jar    
  2. aopalliance-1.0.jar                               jaxb-xjc-2.0.jar    
  3. avalon-framework-4.1.3.jar                        jaxen-1.1.jar    
  4. commons-lang-2.1.jar                              jaxws-api-2.0.jar    
  5. commons-logging-1.1.jar                           jdom-1.0.jar    
  6. cxf-api-2.0.3-incubator.jar                       jetty-6.1.5.jar    
  7. cxf-common-schemas-2.0.3-incubator.jar            jetty-util-6.1.5.jar    
  8. cxf-common-utilities-2.0.3-incubator.jar          jra-1.0-alpha-4.jar    
  9. cxf-rt-bindings-http-2.0.3-incubator.jar          log4j-1.2.14.jar    
  10. cxf-rt-bindings-soap-2.0.3-incubator.jar          logkit-1.0.1.jar    
  11. cxf-rt-bindings-xml-2.0.3-incubator.jar           neethi-2.0.2.jar    
  12. cxf-rt-core-2.0.3-incubator.jar                   saaj-api-1.3.jar    
  13. cxf-rt-databinding-aegis-2.0.3-incubator.jar      saaj-impl-1.3.jar    
  14. cxf-rt-databinding-jaxb-2.0.3-incubator.jar       servlet-api-2.3.jar    
  15. cxf-rt-frontend-jaxws-2.0.3-incubator.jar         servlet-api-2.5-6.1.5.jar    
  16. cxf-rt-frontend-simple-2.0.3-incubator.jar        slf4j-api-1.3.1.jar    
  17. cxf-rt-management-2.0.3-incubator.jar             slf4j-jdk14-1.3.1.jar    
  18. cxf-rt-transports-http-2.0.3-incubator.jar        spring-beans-2.0.7.jar    
  19. cxf-rt-transports-http-jetty-2.0.3-incubator.jar  spring-context-2.0.7.jar    
  20. cxf-rt-transports-jms-2.0.3-incubator.jar         spring-core-2.0.7.jar    
  21. cxf-rt-transports-local-2.0.3-incubator.jar       spring-web-2.0.7.jar    
  22. cxf-tools-common-2.0.3-incubator.jar              stax-api-1.0.1.jar    
  23. geronimo-activation_1.1_spec-1.0-M1.jar           stax-utils-20060502.jar    
  24. geronimo-annotation_1.0_spec-1.1.jar              velocity-1.4.jar    
  25. geronimo-javamail_1.4_spec-1.0-M1.jar             velocity-dep-1.4.jar    
  26. geronimo-jms_1.1_spec-1.1.jar                     wsdl4j-1.6.1.jar    
  27. geronimo-servlet_2.5_spec-1.1-M1.jar              wstx-asl-3.2.1.jar    
  28. geronimo-ws-metadata_2.0_spec-1.1.1.jar           xml-apis-1.3.02.jar    
  29. jaxb-api-2.0.jar                                  xml-resolver-1.2.jar

Monday, April 16, 2007

Multi-valued Reversible enum in Java

Sometime back I proposed a reversible enum pattern in my post. One thing missing from that implementation was the ability to successfully lookup the enum constants in cases where enum constants can have multiple values. To accomplish this, I utilized another feature introduced in Java 5, namely varargs or variable argument support. This way while building the key for the map, we utilize all the values for a constant and use the varargs in the reverse() method of the enum.

Let's look at the interface MultiValueReversibleEnum that denotes an enum as reversible and that an enum should implement.

/**
 * <p>
 * This interface defines the method that the {@link Enum} implementations
 * should implement if they want to have the reversible lookup functionality and
 * define multiple values for each enum constant. i.e. allow the lookup using
 * the codes for the {@link Enum} constants.
 * </p>
 * @param <E>
 *          The value of Enum constant
 * @param <V>
 *          The Enum constant to return from lookup
 *          </p>
 * @author Atif Khan
 * @since Mar 26, 2007
 */
public interface MultiValueReversibleEnum<E, V>
{
  /**
   * <p>
   * Return the values/codes of the enum constant. It should return the
   * values/codes in the same order as defined in the enum.
   * </p>
   * @return value
   */
  public E[] getAllCodes();

  /**
   * <p>
   * Get the {@link Enum} constant by looking up the code in the reverse enum
   * map.
   * </p>
   * @param code
   * @return V - The enum constant
   */
  public V reverse( E ... code );

}



The things to notice in this interface are:

  • getAllCode() method - It returns an array of all the possible values for an enum constant

  • reverse( E ... code ) method - You can see the usage of varargs here. You basically give all the possible values for the enum constant you want to lookup in the right order




Next we need to look at the map MultiValueReverseEnumMap that will store the enum constants and their value mappings.

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * A utility class that provides a reverse map of the {@link Enum} that is keyed
 * by the values of the {@link Enum} constant. <br/> This map implementation
 * should be used with the enums implementing the
 * {@link MultiValueReversibleEnum} interface.
 * </p>
 * @author Atif Khan
 * @param <K>
 *          The class type of the values of the enum constant
 * @param <V>
 *          The Enum for which the map is being created
 * @since Mar 26, 2007
 */
public class MultiValueReverseEnumMap<K, V extends MultiValueReversibleEnum<K, V>>
{
  private final Map<String, V> mReverseMap = new HashMap<String, V>();

  /**
   * <p>
   * Create a new instance of ReverseEnumMap.
   * </p>
   * @param valueType
   */
  public MultiValueReverseEnumMap( final Class<V> valueType )
  {
    for( final V v : valueType.getEnumConstants() ) {
      mReverseMap.put( buildKey( v.getAllCodes() ), v );
    }
  }

  /**
   * <p>
   * Perform the reverse lookup for the given enum values and return the enum
   * constant.
   * </p>
   * @param enumValues
   * @return enum constant
   */
  public V get( final K ... enumValues )
  {
    return mReverseMap.get( buildKey( enumValues ) );
  }

  /**
   * <p>
   * Build the key for the entry in the map using the array of values supplied.
   * </p>
   * @param values
   * @return key for the entry
   */
  private String buildKey( final K[] values )
  {
    final StringBuilder valueBuff = new StringBuilder( values.length * 5 );
    for( final K value : values ) {
      valueBuff.append( value.hashCode() );
    }

    return valueBuff.toString();
  }
}


As you can see, the getAllCodes() defined in the interface is used to build the key for the map. Also, the get( final K ... enumValues ) method that looks up the value in map uses varargs.



Now it's time to glue everything together and see how to use the interface and map to build a multi-valued reversible enum. We will write a test class MultiValuedEnumTest.

import com.ihg.dec.framework.commons.utils.enums.MultiValueReverseEnumMap;
import com.ihg.dec.framework.commons.utils.enums.MultiValueReversibleEnum;

/**
 * <p>
 * A test class for multi-values enum that implements the
 * <code>MultiValueReversibleEnum</code> interface and uses the
 * <code>MultiValueReverseEnumMap</code>.
 * </p>
 * @author Atif Khan
 * @since Apr 16, 2007
 */
public class MultiValuedEnumTest
{
  enum TestEnum
    implements MultiValueReversibleEnum<Integer, TestEnum>
  {

    ONE(1, 11),
    TWO(2, 22);

    private int mCode1;
    private int mCode2;

    private static final MultiValueReverseEnumMap<Integer, TestEnum> mMap = new MultiValueReverseEnumMap<Integer, TestEnum>(
      TestEnum.class );

    /**
     * Create a new instance of TestEnum. <p/>
     * @param code1
     * @param code2
     */
    private TestEnum( final int code1, final int code2 )
    {
      mCode1 = code1;
      mCode2 = code2;
    }

    /**
     * Get the value of <code>code1</code>. <p/>
     * @return Returns the code1.
     */
    public int getCode1()
    {
      return mCode1;
    }

    /**
     * Get the value of <code>code2</code>. <p/>
     * @return Returns the code2.
     */
    public int getCode2()
    {
      return mCode2;
    }

    /**
     * Get an instance of TestEnum
     * @return TestEnum constant
     */
    public static TestEnum getInstance()
    {
      return TestEnum.ONE;
    }

    /**
     * @see com.ihg.dec.framework.commons.utils.enums.MultiValueReversibleEnum#getAllCodes()
     */
    public Integer[] getAllCodes()
    {
      return new Integer[] {
        getCode1(), getCode2()
      };
    }

    /**
     * @see com.ihg.dec.framework.commons.utils.enums.MultiValueReversibleEnum#reverse(E[])
     */
    public TestEnum reverse( final Integer ... code )
    {
      return mMap.get( code );
    }
  }

  /**
   * <p>
   * The main method to execute this class.
   * </p>
   * @param args
   */
  public static void main( final String[] args )
  {
    System.out.println("Reverse for [1, 11]: " + TestMultiValueEnum.getInstance().reverse( 1, 11 ) );
    System.out.println("Reverse for [2, 22]: " +  TestMultiValueEnum.getInstance().reverse( 2, 22 ) );
  }

}


If you run this class, you should see following getting printed:
Reverse for [1, 11]: ONE
Reverse for [2, 22]: TWO

It's all very simple and straightforward. If you have any suggestions or feedback to improve this, please leave me a comment.

Tuesday, April 10, 2007

Impression of RIFE Web Framework

I have been spending time on and off looking at various web frameworks available for a little more than a year. A lot of this has to do with keeping track of what's going on in the web framework arena and keep in sync with the technologies. Another focus of mine is to see how the new (at least for me) frameworks affect the productivity of a developer.

I first looked at RIFE last year and I have been very impressed by the concepts.The developers of RIFE, Geert Bevin and others have been very helpful when I had some questions. I was even able to get answers on the IRC channel for RIFE. It was not until recently that I tried to write an application using RIFE that I really got a feel of it.

Here are some of the good and bad that I encountered:

Pros

  • A complete framework. It includes basic web framework, ORM, Content Management, Validations, Templates etc.

  • Good support from developers

  • A nice quick start application to get you running quickly

  • Good support for constraints for adding validations

  • Continuations - ability to pause, rewind, jump through transaction steps



Cons

  • Templates are really weird to work with. The syntax for the templates really did me. It is not really designer friendly like Tapestry or Wicket

  • The concept of flowlink and datalink is just too verbose and not clear

  • Using annotation didn't do me any good as well for defining the flow and data links. There isn't enough documentation for annotations

  • The only way for form validations I could find was to define the a MetaData class equivalent to the domain object representing the form and define the validations in there. This just seems like way too much of work for simple validations.

  • I could not figure out how to customize the error messages for form validations



In the end, I feel that it's the lack of documentation that prevented me from really exploring it. I know Geert is a very sharp guy and if he ever gets to read my blog, I welcome his comments. I think the learning curve is too much for RIFE and lack of documentation adds to it and I am not sure if the return is proportional to the effort compared to other frameworks.

Tuesday, March 27, 2007

A simple file based Ruby Database - KirbyBase

Have you ever been in a situation where you wanted to write a simple application for internal use that should be portable and completely self contained (including the database)? I am pretty sure you have. I had to do this recently for one of the simple reporting applications and I didn't really want to create a dependency on external database like MySQL or PostgreSQL. This application needed only 4 tables and the data in these table is never going to be large enough to warrant an extensive database system. I thought about putting the data in flat files and write the code to access it. But, then I found this really neat gem called KirbyBase that does exactly the same and little more.

KirbyBase has some neat features that mirror the traditional RDBMS while still sticking to it's simple roots:

  • Pure Ruby code

  • Data is kept in simple delimited files, allowing the modification in any text editor

  • It's not an in-memory DB. The data is actually stored on the disk

  • Allows various data types like String, Integer, Float, Boolean, Time, Date, DateTime, Memo, Blob, and YAML

  • Allows creating indexes on fields

  • Query results can be sorted

  • Allows to define one-many links between tables similar to joins in RDBMS

  • The columns can be defined with a formula, so the value will be calculated during insertion

  • Automatic lookup fields can be defined to lookup the related values from another table





To give a simple example on how all this works, let's consider a scenario of Department and Employees. The simple requirements can be laid out as each employee has personal information and belongs to a department. Also, each department has a name and identifier and can contain multiple employees. Following is how the code going to look using KirbyBase. Please keep in mind that I have tried to write a reusable DAO, hence the code may look a little verbose. You can very well short circuit the whole thing and write very concise code.


#!/usr/bin/ruby -w

require 'rubygems'
require 'kirbybase'

class DAO
  attr_accessor(:employee_tbl, :department_tbl)

  #Initialize the KirbyBase engine
  def initialize
    db = KirbyBase.new(:local, nil, nil, '.')

    #Get/Create the Department table
    begin
      self.department_tbl = db.get_table(:department)
    rescue
      self.department_tbl = db.create_table(:department, :dept_name, {:DataType=>:String, :Key=>true, :Index=>1}, :employee, {:DataType=>:ResultSet, :Link_many=>[:recno, :employee, :dept_id]}) do |t|
        t.record_class = Department
      end
    end

    #Get/Create the Employee table
    begin
      self.employee_tbl = db.get_table(:employee)
    rescue
      self.employee_tbl = db.create_table(:employee, :dept_id, {:DataType=>:Integer, :Key=>true, :Index=>1}, :name, {:DataType=>:String, :Key=>true, :Index=>1}) do |t|
        t.record_class = Employee
      end
    end
  end

  #Get the department by name or if it does not exist, insert it 
  def get_department(name)
    dept = self.department_tbl.select_by_dept_name_index { |r| r.dept_name == name  }.first
    unless dept
      dept = Department.new do |x|
        x.dept_name = name
      end
      self.department_tbl.insert(dept)
      dept = self.department_tbl.select_by_dept_name_index { |r| r.dept_name == name  }.first
    end
    return dept
  end

  #Insert a new employee in table
  def insert_employee(dept, employee)
    employee_rs = self.employee_tbl.select_by_dept_id_name_index { |r| r.dept_id == dept.recno and r.name == employee.name }.first

    if employee_rs
     self.employee_tbl.update(employee) { |r| r.dept_id == dept.recno and r.name == employee.name }
    else
      employee_obj = Employee.new do |x|
        x.dept_id = dept.recno
        x.name = employee.name
      end
      self.employee_tbl.insert(employee_obj)
    end
    
  end

  #Get all employees for a department
  def get_all_employees(dept)
    employee_rs = self.employee_tbl.select_by_dept_id_name_index { |r| r.dept_id == dept.recno }.sort(:name)
  end
  
end

# The Department class
class Department
  attr_accessor(:recno, :dept_name, :employee)

  def Department.kb_create(recno, dept_name, employee)
    Department.new do |x|
      x.recno = recno
      x.dept_name = dept_name
      x.employee = employee
    end
  end

  def initialize(&block)
    instance_eval(&block)
  end
end

# The Employee class
class Employee
  attr_accessor(:recno, :dept_id, :name)

  def Employee.kb_create(recno, dept_id, name)
    Employee.new do |x|
      x.recno = recno
      x.dept_id = dept_id
      x.name = name
    end
  end

  def initialize(&block)
    instance_eval(&block)
  end
end

#Test everything now
dao = DAO.new
dept = dao.get_department("IT")

emp1 = Employee.new { |x| 
  x.name = "John"
}
dao.insert_employee(dept, emp1)

emp2 = Employee.new { |x|
  x.name = "Doe"
}
dao.insert_employee(dept, emp2)

#Get all empoyees in IT department and print
#Let's use the get_all_employees function first
puts "Employee in #{dept.dept_name} department"
dao.get_all_employees(dept).each { |x|
  puts x.name
}

#Alternatively we could use the dept.employee to get all employees
#Refresh the dept object
dept = dao.get_department("IT")
puts "Employee in #{dept.dept_name} department"
dept.employee.each { |x|
  puts x.name
}



The output after execution will look like this:

Employee in IT department
Doe
John

Employee in IT department
John
Doe


This should also generate 2 files in your current directory, department.tbl and employee.tbl. Let's look at content of these files now.

department.tbl

000001|000000|Department|recno:Integer|dept_name:String:Key->true:Index->1|employee:ResultSet:Link_many->recno=employee.dept_id
1|IT|kb_nil


employee.tbl

000002|000000|Employee|recno:Integer|dept_id:Integer:Key->true:Index->1|name:String:Key->true:Index->1
1|1|John
2|1|Doe


That's all there to it. It is very powerful and easy to work with library.