Thursday, December 16, 2010

How to create a custom ClockResolution in Hector (0.7.0-x)


Create your own ClockResolution in Hector (as of 0.7.0-22) is easy. We have modified the code a bit to allow Hector clients to define their own implementation if needed. Let's assume we want to create a clock resolution class that generates negative numbers (this is not a useful example. It is only to show how it is possible).
The way to do it is by creating a class that implements me.prettyprint.hector.api.ClockResolution



    // Define my own clock resolution.
    class SequentialClockResolution implements ClockResolution {
        @Override
        public long createClock() {
            return System.currentTimeMillis() * -1;
        }
    }

And now we have to tell Hector to use it.

    CassandraHostConfigurator cassandraHostConfigurator = new CassandraHostConfigurator("localhost");

    cassandraHostConfigurator.setClockResolution(new SequentialClockResolution());

As an additional information, this is the class diagram involved in ClockResolution.