Skip to content

Achilles Custom Types

DuyHai DOAN edited this page Nov 13, 2013 · 31 revisions

To support all Cassandra specific but powerful features such as tunable consistency levels or counters, Achilles introduces custom Java types:


#### ConsistencyLevel It is an enum exposing all existing consistency levels in Cassandra:
  • ANY
  • ONE
  • TWO
  • THREE
  • QUORUM
  • EACH_QUORUM
  • LOCAL_QUORUM
  • ALL

Because Achilles supports both CQL & Thrift implementations, it has to define a custom enum for Consistency instead of re-using the enums provided by Hector or Java Driver Core libraries.

See Consistency Level for more details


#### Counter This type represents a **Cassandra** counter column. It exposes the following methods:
	public Long get();

	public Long get(ConsistencyLevel readLevel);

	public void incr();

	public void incr(ConsistencyLevel writeLevel);

	public void incr(Long increment);

	public void incr(Long increment, ConsistencyLevel writeLevel);

	public void decr();

	public void decr(ConsistencyLevel writeLevel);

	public void decr(Long decrement);

	public void decr(Long decrement, ConsistencyLevel writeLevel);

For more details on counters, see Counter type


#### CounterBuilder

The above Counter type can be gotten only from a 'managed' entity. If you want to persist a transient entity having counter fields, you should use the CounterBuilder type provided by Achilles

The builder exposes the following static methods:

    public static Counter incr() 

    public static Counter incr(Long incr) 

    public static Counter decr() 

    public static Counter decr(Long decr) 

The only sensible usage for CounterBuilder is for transient entity persistence. Example:

@Entity
public class UserEntity
{
    @Id
    private Long userId;

    @Column
    private Counter 

    private UserEntity(Long userId,.....,Counter popularity)
    {
        this.userId = userId;
        ...
        this.popularity = popularity;
    }
    ...
}

 // Creating a new user with initial popularity value set to 100
 manager.persist(new UserEntity(10L,....,CounterBuilder.incr(100L));

#### Options

An Options is just a holder object for Cassandra specific TTL, Timestampand and Consistency level parameters

public class Options {

    ConsistencyLevel consistency;

    Integer ttl;

    Long timestamp;

    public Optional<ConsistencyLevel> getConsistencyLevel() {
        return Optional.fromNullable(consistency);
    }

    public Optional<Integer> getTtl() {
        return Optional.fromNullable(ttl);
    }

    public Optional<Long> getTimestamp() {
        return Optional.fromNullable(timestamp);
    }
}

Options are used in conjunction with common Persistence Manager operations persist(), merge(), find(), getReference() and remove().

Normally you cannot instantiate an Options object yourself, you need to use the OptionsBuilder instead, check below.


#### OptionsBuilder

Main builder to create Options.

The exposed methods are:

Options options;
// Consistency, TTL and timstamp
options = OptionsBuilder.withConsistency(QUORUM)
                .ttl(10)
                .timestamp(100L);

// Consistency and TTL only
options = OptionsBuilder.withConsistency(QUORUM)
                .ttl(10);

// Consistency and Timestamp only
options = OptionsBuilder.withConsistency(QUORUM)
                .timestamp(100L);

//TTL, Consistency and Timestamp
options = OptionsBuilder.withTtl(11)
                .consistency(ANY)
                .timestamp(111L);

// TTL and Consistency only
options = OptionsBuilder.withTtl(11)
                .consistency(ANY);

// TTL and Timestamp only
options = OptionsBuilder.withTtl(11)
                .timestamp(111L);

// Timestamp, Consistency and TTL 
options = OptionsBuilder.withTimestamp(122L)
                .consistency(ONE)
                .ttl(12);

// Timestamp and Consistency only
options = OptionsBuilder.withTimestamp(122L)
                .consistency(ONE);

// Timestamp and TTL only
options = OptionsBuilder.withTimestamp(122L)
                .ttl(12);

#### IndexCondition

This class defines an index condition necessary for Indexed Query

Below is the public constructor of an IndexCondition instance.

/**
	 * Shortcut constructor to build an EQUAL index condition
	 * 
	 * @param columnName
	 *            name of indexed column
	 * @param columnValue
	 *            value of indexed column
	 */
	public IndexCondition(String columnName, Object columnValue) {
		this.columnName = columnName;
		this.indexRelation = IndexRelation.EQUAL;
		this.columnValue = columnValue;
	}

As you can notice, the support for secondary index is limited to EQUALITY clause. Inequalities on secondary indices do not provide predictable performance so Achilles does not support it by default.

Home

Clone this wiki locally