Skip to content

Achilles Custom Types

DuyHai DOAN edited this page Jan 26, 2014 · 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
  • LOCAL_SERIAL
  • SERIAL

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, Timestamp and 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(), update(), find(), getProxy() 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.


#### TypedMap

The native query API used to return a Map<String,Object> as result. It is not very user-friendly because it forces you do manual type casting. Example:

	
	Map<String,Object> columns = manager.nativeQuery("SELECT * FROM users WHERE userId = 10").getFirst();
	
	String name = (String)columns.get("name");
	Long age = (Long)columns.get("age");

TypedMap is just an extension of Map<String,Object> offering two extras methods for convenience:

	@SuppressWarnings("unchecked")
	public <T> T getTyped(String key) {
		T value = null;
		if (super.containsKey(key) && super.get(key) != null) {
			value = (T) super.get(key);
			return value;
		}
		return value;
	}

	public <T> T getTypedOr(String key, T defaultValue) {
		if (super.containsKey(key)) {
			return getTyped(key);
		} else {
			return defaultValue;
		}
	}

getTyped() will do the casting for you. The target type is passed at runtime while calling the method. getTypedOr() lets you provide a fall-back value.

Example of usage:

	
	TypedMap columns = manager.nativeQuery("SELECT * FROM users WHERE userId = 10").first();
	
	// Explicit type (String) is passed to method invocation
	String name = columns.<String>getTyped("name");
	
	// No need to provide explicit type. The compiler will infer type in this case
	Long age = columns.get("age");

#### Interceptor

This is an interface defining the contract for a lifecycle interceptor. For more details, please check Interceptors

public interface Interceptor<T> 
{
	public void onEvent(T entity);

	public List<Event> events();
}

Home

Clone this wiki locally