Shay Banon home

 

Resource (RSEM) Creation Refactoring

In Compass, currently, the way to create a Resource (usually when using RSEM) is using the CompassSesssion API. Here is a quick code example of how it is done:


Resource resource = session.createResource(“alias”);
resource.addProperty(“id”, 1);
session.create(resource);

This is a bit confusing, especially when it comes to resource creation. Some people confuse the createResource method (which instantiates a resource) with the create method which creates it in the search engine index.

This is why, in version 2.0 (its committed on trunk), this has been abstracted to a ResourceFactory, which is accessible through the Compass instance. Here is a revised example that makes use of it:


Resource resource = compass.getResourceFactory().createResource(“alias”);
resource.addProperty(“id”, 1);

// open session, begin transaction
session.create(resource);
// commit transaction, close session

This breaks backward compatibility at the expense of greater clarity. It is fairly simple to keep the old behavior as well, but it really cleans Compass up not supporting the old method. What do you say? If it really messes your application, just comment or send me an email.

© 2003-2010 Shay Banon
Fork me on GitHub