implement count operation

This commit is contained in:
Albert Shift 2015-03-22 21:45:29 -07:00
parent 06f9d59cfe
commit 25a8cd7eea
6 changed files with 59 additions and 8 deletions

View file

@ -24,7 +24,7 @@ public class CasserEntityCache {
private ConcurrentMap<Class<?>, CasserMappingEntity<?>> cache = new ConcurrentHashMap<Class<?>, CasserMappingEntity<?>>();
public CasserMappingEntity<?> getEntity(Class<?> iface) {
public CasserMappingEntity<?> getOrCreateEntity(Class<?> iface) {
CasserMappingEntity<?> entity = cache.get(iface);

View file

@ -21,6 +21,7 @@ import java.util.Set;
import casser.core.dsl.Getter;
import casser.core.dsl.Setter;
import casser.core.operation.CountOperation;
import casser.core.operation.DeleteOperation;
import casser.core.operation.SelectOperation;
import casser.core.operation.UpdateOperation;
@ -167,6 +168,16 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
p1, p2, p3, p4, p5, p6, p7);
}
public CountOperation count(Object dsl) {
Objects.requireNonNull(dsl, "dsl is empty");
Class<?> iface = MappingUtil.getMappingInterface(dsl);
CasserMappingEntity<?> entity = entityCache.getOrCreateEntity(iface);
return new CountOperation(this, entity);
}
public <V> UpdateOperation update(Setter<V> setter, V v) {
Objects.requireNonNull(setter, "field is empty");
Objects.requireNonNull(v, "value is empty");
@ -181,7 +192,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
Class<?> iface = MappingUtil.getMappingInterface(pojo);
CasserMappingEntity<?> entity = entityCache.getEntity(iface);
CasserMappingEntity<?> entity = entityCache.getOrCreateEntity(iface);
return new UpsertOperation(this, entity, pojo);
}
@ -191,7 +202,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
Class<?> iface = MappingUtil.getMappingInterface(dsl);
CasserMappingEntity<?> entity = entityCache.getEntity(iface);
CasserMappingEntity<?> entity = entityCache.getOrCreateEntity(iface);
return new DeleteOperation(this, entity);
}

View file

@ -115,7 +115,7 @@ public class SessionInitializer extends AbstractSessionOperations {
Class<?> iface = MappingUtil.getMappingInterface(dsl);
CasserMappingEntity<?> entity = entityCache.getEntity(iface);
CasserMappingEntity<?> entity = entityCache.getOrCreateEntity(iface);
if (type == AutoDsl.CREATE || type == AutoDsl.CREATE_DROP) {
createNewTable(entity);

View file

@ -16,19 +16,40 @@
package casser.core.operation;
import casser.core.AbstractSessionOperations;
import casser.core.Filter;
import casser.mapping.CasserMappingEntity;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.querybuilder.BuiltStatement;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Select;
import com.datastax.driver.core.querybuilder.Select.Where;
public class CountOperation extends AbstractObjectOperation<Long, CountOperation> {
public class CountOperation extends AbstractFilterOperation<Long, CountOperation> {
public CountOperation(AbstractSessionOperations sessionOperations) {
private final CasserMappingEntity<?> entity;
public CountOperation(AbstractSessionOperations sessionOperations, CasserMappingEntity<?> entity) {
super(sessionOperations);
this.entity = entity;
}
@Override
public BuiltStatement buildStatement() {
return null;
Select select = QueryBuilder.select().countAll().from(entity.getTableName());
if (filters != null && !filters.isEmpty()) {
Where where = select.where();
for (Filter<?> filter : filters) {
where.and(filter.getClause());
}
}
return select;
}
@Override

View file

@ -49,7 +49,23 @@ public class SelectOperation<E> extends AbstractFilterStreamOperation<E, SelectO
}
public CountOperation count() {
return null;
CasserMappingEntity<?> entity = null;
for (CasserMappingProperty<?> prop : props) {
if (entity == null) {
entity = prop.getEntity();
}
else if (entity != prop.getEntity()) {
throw new CasserMappingException("you can count records only from a single entity " + entity.getMappingInterface() + " or " + prop.getEntity().getMappingInterface());
}
}
if (entity == null) {
throw new CasserMappingException("no entity or table to count data");
}
return new CountOperation(sessionOperations, entity);
}
public <R> SelectOperation<R> map(Function<E, R> fn) {

View file

@ -51,6 +51,9 @@ public class SimpleUserTest extends AbstractEmbeddedCassandraTest {
session.update(user::setName, "albert").set(user::setAge, 35)
.where(user::getId, "==", 123L).sync();
long cnt = session.count(user).where(user::getId, "==", 123L).sync();
Assert.assertEquals(1L, cnt);
String name = session.select(user::getName)
.where(user::getId, "==", 123L)
.sync()