Use search engine technology for object persistence

How a seemingly unrelated technology can help solve some typical problems

Page 2 of 2

Real-life applications

As you can see, it is easy to adopt the Lucene search engine library for object persistence. As simple and maybe even naive as this article's example is, you can appreciate the opportunities you may discover by going this route. I have used variations of this approach in several of my projects quite successfully.

You can easily extend the base library to suit your particular needs. For example, consider what may happen if you were to add a permission system to your persistent objects. Let's assume your objects are just like Unix files: they "know" what user and group they belong to, and they also know whether "owner," "group," and "others" have permission to read or write this particular object. You can easily design a BaseProtectedObject simply by storing user and group ownership as well as Boolean permissions in an object's JavaBean properties.

Now the StorageServiceInterface must be changed to accept user and group information for every method. It looks like this:

 public interface ControlledStorageServiceInterface {
   public void store(String actorUserId, String actorGroupId, 
     BaseProtectedObject object) throws AccessControlException, StorageException;
   public BaseProtectedObject retrieve(String actorUserId, String actorGroupId, 
     String objectId) throws AccessControlException, StorageException;
   public List find(String actorUserId, String actorGroupId, String query) throws StorageException;
   public List find(String actorUserId, String actorGroupId, String key, String value)
     throws StorageException;
   public List find(String actorUserId, String actorGroupId, Class clazz) throws StorageException;
   public void delete(String actorUserId, String actorGroupId, String objectId) 
     throws AccessControlException, StorageException;
   public void delete(String actorUserId, String actorGroupId, String key, 
     String value) throws StorageException;
}

We also add AccessControlException to be thrown to indicate when the calling user lacks permissions to execute the desired method call.

The complete, fully-documented implementation of the controlled persistence mechanism is included in my open source MAOS (Meta Attribute Object Store) project hosted on SourceForge.net. You can find all the latest and greatest MAOS code there, all tidied up and ready to go.

Conclusion

In this article, we have implemented a simple persistence mechanism for Java objects using search-engine technology. I hope this example will show you how it is often possible to look outside of the proverbial box and apply seemingly unrelated technology solutions to your problem with a great degree of success.

Mikhail Garber is a Dallas-based independent technology consultant with more than 12 years of experience in enterprise software development. Garber specializes in Java/J2EE, databases, messaging, and open source solutions. His services have been employed by such organizations as Mary Kay Cosmetics, Boeing Defense and Space, Verizon Wireless, the US government, Lockheed Martin, and many others.

Learn more about this topic

| 1 2 Page 2