Dashboard > People > Himanshu Gupta > Home
  Himanshu Gupta Log In   View a printable version of the current page.  
  Home
Added by Himanshu Gupta, last edited by Himanshu Gupta on Apr 15, 2008  (view change)
Labels: 

  • Can you cluster Enum Pattern using terracotta?

You can cluster an implementation of Enum pattern (this is done by defining members as final and static). In this case you need to define the enum constants as roots.
If you don't need to do a object comparision of the enum constants, and instead use .equals() method of the same, then you can very well not define the enum constants as roots.

The following example illustrates this point

class EmployeeType implements java.io.Serializable

Unknown macro: { public static final EmployeeType NOT_EMPLOYEE = new EmployeeType("NOT_EMPLOYEE", 0); public static final EmployeeType EMPLOYEE = new EmployeeType("EMPLOYEE", 1); public static final EmployeeType SUPER_USER = new EmployeeType("SUPER_USER", 2); }

EmployeeType can be considered an enum.

If we store the final static member variables in some kind of cache like

public static final Cache CACHE = new Cache();

Now if we do an object comparison on some other node as

boolean flag = EmployeeType.EMPLOYEE == CACHE.get(EmployeeType.EMPLOYEE.toString())

The value of flag variable will be false. This is because their are two seperate copies of EmployeeType.EMPLOYEE on two different terracotta clients, and hence a put of EmployeeType.EMPLOYEE in CACHE on one client, puts his copy in the cache, and when this copy is extracted from the CACHE on a different client and compared, it is different as it is being compared from the local copy of the other client.

This scenario of object comparison to implement Enum pattern (as goes with jdk 1.4 and earlier), can be very well handled if we declare these static final members as terracotta roots. Doing this will make sure that only a single(virtual, i.e. object ids are same) copy of them exist throughout the cluster and hence object comparison will work. Once they are marked as roots in tc-config.xml, the value of flag will be true.

I have attached a sample application which demonstrates this scenario.

  • Can you change enum values in some other nodes across the terracotta cluster (i.e mutate enums)

If we use enum (as defined in jdk 1.5) we cannot change their values in other nodes because they are treated as primitive types in Terracotta. In case of using enums instead of using Enum patterns (as described above), you dont need to do any special marking in your tc-config file. The enum values are unique thoughout the cluster and are positive on object comparison test.

  • You cannot serialize the Enum - then its value is lost on the other node?

You can very well do this, not an issue at all (With terracotta perspective we never serialize the objects).

  • You cannot extend Enum ?

All enums implicitly extend java.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else

Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.5 Build:#811 Jul 25, 2007) - Bug/feature request - Contact Administrators