refactoring and update readme document

This commit is contained in:
Albert Shift 2015-03-27 13:12:36 -07:00
parent 647cc9b4ea
commit 9afca8a957
6 changed files with 46 additions and 51 deletions

View file

@ -23,20 +23,15 @@ Entity definition:
public interface Timeline {
@PartitionKey
UUID getUserId();
void setUserId(UUID uid);
UUID userId();
@ClusteringColumn
@DataTypeName(Name.TIMEUUID)
Date getTimestamp();
void setTimestamp(Date ts);
Date timestamp();
@Column
String getText();
String text();
void setText(String text);
}
```
@ -48,17 +43,17 @@ CasserSession session = Casser.init(getSession()).showCql().add(Timeline.class).
Select information:
```
session.select(timeline::getUserId, timeline::getTimestamp, timeline::getText)
.where(timeline::getUserId, "==", userId)
.orderBy(timeline::getTimestamp, "desc").limit(5).sync()
session.select(timeline::userId, timeline::timestamp, timeline::text)
.where(timeline::userId, Operator.EQ, userId)
.orderBy(timeline::timestamp, "desc").limit(5).sync()
.forEach(System.out::println);
```
Insert information:
```
Timeline post = Casser.pojo(Timeline.class);
post.setUserId(userId);
post.setTimestamp(new Date(postTime+1000L*i));
post.setText("hello");
TimelineImpl post = new TimelineImpl();
post.userId=userId;
post.timestamp=new Date(postTime+1000L*i);
post.text="hello";
session.upsert(post).sync();
```

View file

@ -28,18 +28,18 @@ import com.datastax.driver.core.querybuilder.QueryBuilder;
public final class Filter<V> {
private final CasserMappingProperty property;
private final FilterOperator operation;
private final Operator operation;
private final V value;
private final V[] values;
private Filter(CasserMappingProperty prop, FilterOperator op, V value) {
private Filter(CasserMappingProperty prop, Operator op, V value) {
this.property = prop;
this.operation = op;
this.value = value;
this.values = null;
}
private Filter(CasserMappingProperty prop, FilterOperator op, V[] values) {
private Filter(CasserMappingProperty prop, Operator op, V[] values) {
this.property = prop;
this.operation = op;
this.value = null;
@ -50,7 +50,7 @@ public final class Filter<V> {
return property;
}
public FilterOperator getOperation() {
public Operator getOperation() {
return operation;
}
@ -62,7 +62,7 @@ public final class Filter<V> {
switch(operation) {
case EQUAL:
case EQ:
return QueryBuilder.eq(property.getColumnName(),
valuePreparer.prepareColumnValue(value, property));
@ -73,16 +73,16 @@ public final class Filter<V> {
}
return QueryBuilder.in(property.getColumnName(), preparedValues);
case LESSER:
case LT:
return QueryBuilder.lt(property.getColumnName(), valuePreparer.prepareColumnValue(value, property));
case LESSER_OR_EQUAL:
case LTE:
return QueryBuilder.lte(property.getColumnName(), valuePreparer.prepareColumnValue(value, property));
case GREATER:
case GT:
return QueryBuilder.gt(property.getColumnName(), valuePreparer.prepareColumnValue(value, property));
case GREATER_OR_EQUAL:
case GTE:
return QueryBuilder.gte(property.getColumnName(), valuePreparer.prepareColumnValue(value, property));
default:
@ -92,7 +92,7 @@ public final class Filter<V> {
}
public static <V> Filter<V> equal(Getter<V> getter, V val) {
return create(getter, FilterOperator.EQUAL, val);
return create(getter, Operator.EQ, val);
}
public static <V> Filter<V> in(Getter<V> getter, V[] vals) {
@ -109,29 +109,29 @@ public final class Filter<V> {
CasserMappingProperty prop = MappingUtil.resolveMappingProperty(getter);
return new Filter<V>(prop, FilterOperator.IN, vals);
return new Filter<V>(prop, Operator.IN, vals);
}
public static <V> Filter<V> greater(Getter<V> getter, V val) {
return create(getter, FilterOperator.GREATER, val);
return create(getter, Operator.GT, val);
}
public static <V> Filter<V> less(Getter<V> getter, V val) {
return create(getter, FilterOperator.LESSER, val);
return create(getter, Operator.LT, val);
}
public static <V> Filter<V> greaterOrEqual(Getter<V> getter, V val) {
return create(getter, FilterOperator.GREATER_OR_EQUAL, val);
return create(getter, Operator.GTE, val);
}
public static <V> Filter<V> lessOrEqual(Getter<V> getter, V val) {
return create(getter, FilterOperator.LESSER_OR_EQUAL, val);
return create(getter, Operator.LTE, val);
}
public static <V> Filter<V> create(Getter<V> getter, String operator, V val) {
Objects.requireNonNull(operator, "empty operator");
FilterOperator fo = FilterOperator.findByOperator(operator);
Operator fo = Operator.findByOperator(operator);
if (fo == null) {
throw new CasserMappingException("invalid operator " + operator);
@ -140,12 +140,12 @@ public final class Filter<V> {
return create(getter, fo, val);
}
public static <V> Filter<V> create(Getter<V> getter, FilterOperator op, V val) {
public static <V> Filter<V> create(Getter<V> getter, Operator op, V val) {
Objects.requireNonNull(getter, "empty getter");
Objects.requireNonNull(op, "empty op");
Objects.requireNonNull(val, "empty value");
if (op == FilterOperator.IN) {
if (op == Operator.IN) {
throw new IllegalArgumentException("invalid usage of the 'in' operator, use Filter.in() static method");
}

View file

@ -18,31 +18,31 @@ package casser.core;
import java.util.HashMap;
import java.util.Map;
public enum FilterOperator {
public enum Operator {
EQUAL("=="),
EQ("=="),
IN("in"),
GREATER(">"),
GT(">"),
LESSER("<"),
LT("<"),
GREATER_OR_EQUAL(">="),
GTE(">="),
LESSER_OR_EQUAL("<=");
LTE("<=");
private final String name;
private final static Map<String, FilterOperator> indexByName = new HashMap<String, FilterOperator>();
private final static Map<String, Operator> indexByName = new HashMap<String, Operator>();
static {
for (FilterOperator fo : FilterOperator.values()) {
for (Operator fo : Operator.values()) {
indexByName.put(fo.getName(), fo);
}
}
private FilterOperator(String name) {
private Operator(String name) {
this.name = name;
}
@ -50,7 +50,7 @@ public enum FilterOperator {
return name;
}
public static FilterOperator findByOperator(String name) {
public static Operator findByOperator(String name) {
return indexByName.get(name);
}

View file

@ -20,7 +20,7 @@ import java.util.List;
import casser.core.AbstractSessionOperations;
import casser.core.Filter;
import casser.core.FilterOperator;
import casser.core.Operator;
import casser.core.Getter;
public abstract class AbstractFilterOperation<E, O extends AbstractFilterOperation<E, O>> extends AbstractEntityOperation<E, O> {
@ -38,7 +38,7 @@ public abstract class AbstractFilterOperation<E, O extends AbstractFilterOperati
return (O) this;
}
public <V> O where(Getter<V> getter, FilterOperator operator, V val) {
public <V> O where(Getter<V> getter, Operator operator, V val) {
addFilter(Filter.create(getter, operator, val));
@ -59,7 +59,7 @@ public abstract class AbstractFilterOperation<E, O extends AbstractFilterOperati
return (O) this;
}
public <V> O and(Getter<V> getter, FilterOperator operator, V val) {
public <V> O and(Getter<V> getter, Operator operator, V val) {
addFilter(Filter.create(getter, operator, val));

View file

@ -20,7 +20,7 @@ import java.util.List;
import casser.core.AbstractSessionOperations;
import casser.core.Filter;
import casser.core.FilterOperator;
import casser.core.Operator;
import casser.core.Getter;
public abstract class AbstractFilterStreamOperation<E, O extends AbstractFilterStreamOperation<E, O>> extends AbstractStreamOperation<E, O> {
@ -38,7 +38,7 @@ public abstract class AbstractFilterStreamOperation<E, O extends AbstractFilterS
return (O) this;
}
public <V> O where(Getter<V> getter, FilterOperator operator, V val) {
public <V> O where(Getter<V> getter, Operator operator, V val) {
addFilter(Filter.create(getter, operator, val));
@ -59,7 +59,7 @@ public abstract class AbstractFilterStreamOperation<E, O extends AbstractFilterS
return (O) this;
}
public <V> O and(Getter<V> getter, FilterOperator operator, V val) {
public <V> O and(Getter<V> getter, Operator operator, V val) {
addFilter(Filter.create(getter, operator, val));

View file

@ -22,7 +22,7 @@ import org.junit.Test;
import casser.core.Casser;
import casser.core.CasserSession;
import casser.core.FilterOperator;
import casser.core.Operator;
import casser.test.integration.build.AbstractEmbeddedCassandraTest;
public class SimpleUserTest extends AbstractEmbeddedCassandraTest {
@ -73,7 +73,7 @@ public class SimpleUserTest extends AbstractEmbeddedCassandraTest {
session.update(user::name, "albert").set(user::age, 35)
.where(user::id, "==", 123L).sync();
long cnt = session.count(user).where(user::id, FilterOperator.EQUAL, 123L).sync();
long cnt = session.count(user).where(user::id, Operator.EQ, 123L).sync();
Assert.assertEquals(1L, cnt);
String name = session.select(user::name)