row column value provider optimization

This commit is contained in:
Albert Shift 2015-03-26 20:04:43 -07:00
parent 0543370b64
commit 1812823e52
13 changed files with 109 additions and 76 deletions

View file

@ -20,8 +20,8 @@ import java.util.concurrent.Executor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import casser.mapping.CasserMappingRepository;
import casser.mapping.ColumnValuePreparer;
import casser.mapping.ColumnValueProvider;
import casser.support.CasserException;
import com.datastax.driver.core.RegularStatement;
@ -43,7 +43,7 @@ public abstract class AbstractSessionOperations {
abstract public Executor getExecutor();
abstract public CasserMappingRepository getRepository();
abstract public ColumnValueProvider getValueProvider();
abstract public ColumnValuePreparer getValuePreparer();

View file

@ -37,7 +37,9 @@ import casser.mapping.CasserMappingEntity;
import casser.mapping.CasserMappingProperty;
import casser.mapping.CasserMappingRepository;
import casser.mapping.ColumnValuePreparer;
import casser.mapping.ColumnValueProvider;
import casser.mapping.MappingUtil;
import casser.mapping.RowColumnValueProvider;
import casser.mapping.StatementColumnValuePreparer;
import com.datastax.driver.core.CloseFuture;
@ -52,6 +54,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
private final Executor executor;
private final boolean dropSchemaOnClose;
private final RowColumnValueProvider valueProvider;
private final StatementColumnValuePreparer valuePreparer;
CasserSession(Session session,
@ -67,6 +70,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
this.executor = executor;
this.dropSchemaOnClose = dropSchemaOnClose;
this.valueProvider = new RowColumnValueProvider(this.mappingRepository);
this.valuePreparer = new StatementColumnValuePreparer(this.mappingRepository);
}
@ -101,8 +105,8 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
}
@Override
public CasserMappingRepository getRepository() {
return mappingRepository;
public ColumnValueProvider getValueProvider() {
return valueProvider;
}
@Override
@ -114,7 +118,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
Objects.requireNonNull(getter1, "field 1 is empty");
CasserMappingProperty p1 = MappingUtil.resolveMappingProperty(getter1);
return new SelectOperation<Tuple1<V1>>(this, new Tuple1.Mapper<V1>(p1), p1);
return new SelectOperation<Tuple1<V1>>(this, new Tuple1.Mapper<V1>(getValueProvider(), p1), p1);
}
public <V1, V2> SelectOperation<Tuple2<V1, V2>> select(Getter<V1> getter1, Getter<V2> getter2) {
@ -123,7 +127,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
CasserMappingProperty p1 = MappingUtil.resolveMappingProperty(getter1);
CasserMappingProperty p2 = MappingUtil.resolveMappingProperty(getter2);
return new SelectOperation<Tuple2<V1, V2>>(this, new Tuple2.Mapper<V1, V2>(p1, p2), p1, p2);
return new SelectOperation<Tuple2<V1, V2>>(this, new Tuple2.Mapper<V1, V2>(getValueProvider(), p1, p2), p1, p2);
}
public <V1, V2, V3> SelectOperation<Tuple3<V1, V2, V3>> select(Getter<V1> getter1, Getter<V2> getter2, Getter<V3> getter3) {
@ -134,7 +138,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
CasserMappingProperty p1 = MappingUtil.resolveMappingProperty(getter1);
CasserMappingProperty p2 = MappingUtil.resolveMappingProperty(getter2);
CasserMappingProperty p3 = MappingUtil.resolveMappingProperty(getter3);
return new SelectOperation<Tuple3<V1, V2, V3>>(this, new Tuple3.Mapper<V1, V2, V3>(p1, p2, p3), p1, p2, p3);
return new SelectOperation<Tuple3<V1, V2, V3>>(this, new Tuple3.Mapper<V1, V2, V3>(getValueProvider(), p1, p2, p3), p1, p2, p3);
}
public <V1, V2, V3, V4> SelectOperation<Tuple4<V1, V2, V3, V4>> select(
@ -148,7 +152,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
CasserMappingProperty p2 = MappingUtil.resolveMappingProperty(getter2);
CasserMappingProperty p3 = MappingUtil.resolveMappingProperty(getter3);
CasserMappingProperty p4 = MappingUtil.resolveMappingProperty(getter4);
return new SelectOperation<Tuple4<V1, V2, V3, V4>>(this, new Tuple4.Mapper<V1, V2, V3, V4>(p1, p2, p3, p4), p1, p2, p3, p4);
return new SelectOperation<Tuple4<V1, V2, V3, V4>>(this, new Tuple4.Mapper<V1, V2, V3, V4>(getValueProvider(), p1, p2, p3, p4), p1, p2, p3, p4);
}
public <V1, V2, V3, V4, V5> SelectOperation<Tuple5<V1, V2, V3, V4, V5>> select(
@ -165,7 +169,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
CasserMappingProperty p4 = MappingUtil.resolveMappingProperty(getter4);
CasserMappingProperty p5 = MappingUtil.resolveMappingProperty(getter5);
return new SelectOperation<Tuple5<V1, V2, V3, V4, V5>>(this,
new Tuple5.Mapper<V1, V2, V3, V4, V5>(p1, p2, p3, p4, p5),
new Tuple5.Mapper<V1, V2, V3, V4, V5>(getValueProvider(), p1, p2, p3, p4, p5),
p1, p2, p3, p4, p5);
}
@ -186,7 +190,7 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
CasserMappingProperty p5 = MappingUtil.resolveMappingProperty(getter5);
CasserMappingProperty p6 = MappingUtil.resolveMappingProperty(getter6);
return new SelectOperation<Tuple6<V1, V2, V3, V4, V5, V6>>(this,
new Tuple6.Mapper<V1, V2, V3, V4, V5, V6>(p1, p2, p3, p4, p5, p6),
new Tuple6.Mapper<V1, V2, V3, V4, V5, V6>(getValueProvider(), p1, p2, p3, p4, p5, p6),
p1, p2, p3, p4, p5, p6);
}
@ -210,7 +214,8 @@ public class CasserSession extends AbstractSessionOperations implements Closeabl
CasserMappingProperty p6 = MappingUtil.resolveMappingProperty(getter6);
CasserMappingProperty p7 = MappingUtil.resolveMappingProperty(getter7);
return new SelectOperation<Tuple7<V1, V2, V3, V4, V5, V6, V7>>(this,
new Tuple7.Mapper<V1, V2, V3, V4, V5, V6, V7>(p1, p2, p3, p4, p5, p6, p7),
new Tuple7.Mapper<V1, V2, V3, V4, V5, V6, V7>(getValueProvider(),
p1, p2, p3, p4, p5, p6, p7),
p1, p2, p3, p4, p5, p6, p7);
}

View file

@ -25,6 +25,8 @@ import casser.mapping.CasserEntityType;
import casser.mapping.CasserMappingEntity;
import casser.mapping.CasserMappingRepository;
import casser.mapping.ColumnValuePreparer;
import casser.mapping.ColumnValueProvider;
import casser.mapping.RowColumnValueProvider;
import casser.mapping.StatementColumnValuePreparer;
import com.datastax.driver.core.KeyspaceMetadata;
@ -71,8 +73,8 @@ public class SessionInitializer extends AbstractSessionOperations {
}
@Override
public CasserMappingRepository getRepository() {
return mappingRepository;
public ColumnValueProvider getValueProvider() {
return new RowColumnValueProvider(mappingRepository);
}
@Override

View file

@ -29,14 +29,12 @@ import casser.core.Filter;
import casser.core.dsl.Getter;
import casser.mapping.CasserMappingEntity;
import casser.mapping.CasserMappingProperty;
import casser.mapping.CasserMappingRepository;
import casser.mapping.ColumnValueProvider;
import casser.mapping.MappingUtil;
import casser.mapping.OrderingDirection;
import casser.mapping.RowColumnValueProvider;
import casser.support.CasserMappingException;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.querybuilder.BuiltStatement;
import com.datastax.driver.core.querybuilder.Ordering;
import com.datastax.driver.core.querybuilder.QueryBuilder;
@ -47,13 +45,13 @@ import com.datastax.driver.core.querybuilder.Select.Where;
public final class SelectOperation<E> extends AbstractFilterStreamOperation<E, SelectOperation<E>> {
protected final Function<ColumnValueProvider, E> rowMapper;
protected final Function<Row, E> rowMapper;
protected final CasserMappingProperty[] props;
protected List<Ordering> ordering = null;
protected Integer limit = null;
public SelectOperation(AbstractSessionOperations sessionOperations, Function<ColumnValueProvider, E> rowMapper, CasserMappingProperty... props) {
public SelectOperation(AbstractSessionOperations sessionOperations, Function<Row, E> rowMapper, CasserMappingProperty... props) {
super(sessionOperations);
this.rowMapper = rowMapper;
this.props = props;
@ -160,12 +158,10 @@ public final class SelectOperation<E> extends AbstractFilterStreamOperation<E, S
@Override
public Stream<E> transform(ResultSet resultSet) {
CasserMappingRepository repository = sessionOps.getRepository();
return StreamSupport.stream(
Spliterators.spliteratorUnknownSize(resultSet.iterator(), Spliterator.ORDERED)
, false).map(r -> new RowColumnValueProvider(repository, r)).map(rowMapper);
, false).map(rowMapper);
}

View file

@ -20,6 +20,8 @@ import java.util.function.Function;
import casser.mapping.CasserMappingProperty;
import casser.mapping.ColumnValueProvider;
import com.datastax.driver.core.Row;
public final class Tuple1<V1> {
public final V1 v1;
@ -28,17 +30,19 @@ public final class Tuple1<V1> {
this.v1 = v1;
}
public final static class Mapper<V1> implements Function<ColumnValueProvider, Tuple1<V1>> {
public final static class Mapper<V1> implements Function<Row, Tuple1<V1>> {
private final ColumnValueProvider provider;
private final CasserMappingProperty p1;
public Mapper(CasserMappingProperty p1) {
public Mapper(ColumnValueProvider provider, CasserMappingProperty p1) {
this.provider = provider;
this.p1 = p1;
}
@Override
public Tuple1<V1> apply(ColumnValueProvider provider) {
return new Tuple1<V1>(provider.getColumnValue(0, p1));
public Tuple1<V1> apply(Row row) {
return new Tuple1<V1>(provider.getColumnValue(row, 0, p1));
}
}

View file

@ -20,6 +20,8 @@ import java.util.function.Function;
import casser.mapping.CasserMappingProperty;
import casser.mapping.ColumnValueProvider;
import com.datastax.driver.core.Row;
public final class Tuple2<V1, V2> {
public final V1 v1;
@ -30,19 +32,23 @@ public final class Tuple2<V1, V2> {
this.v2 = v2;
}
public final static class Mapper<V1, V2> implements Function<ColumnValueProvider, Tuple2<V1, V2>> {
public final static class Mapper<V1, V2> implements Function<Row, Tuple2<V1, V2>> {
private final ColumnValueProvider provider;
private final CasserMappingProperty p1;
private final CasserMappingProperty p2;
public Mapper(CasserMappingProperty p1, CasserMappingProperty p2) {
public Mapper(ColumnValueProvider provider, CasserMappingProperty p1, CasserMappingProperty p2) {
this.provider = provider;
this.p1 = p1;
this.p2 = p2;
}
@Override
public Tuple2<V1, V2> apply(ColumnValueProvider provider) {
return new Tuple2<V1, V2>(provider.getColumnValue(0, p1), provider.getColumnValue(1, p2));
public Tuple2<V1, V2> apply(Row row) {
return new Tuple2<V1, V2>(
provider.getColumnValue(row, 0, p1),
provider.getColumnValue(row, 1, p2));
}
}

View file

@ -20,6 +20,8 @@ import java.util.function.Function;
import casser.mapping.CasserMappingProperty;
import casser.mapping.ColumnValueProvider;
import com.datastax.driver.core.Row;
public final class Tuple3<V1, V2, V3> {
public final V1 v1;
@ -32,24 +34,26 @@ public final class Tuple3<V1, V2, V3> {
this.v3 = v3;
}
public final static class Mapper<V1, V2, V3> implements Function<ColumnValueProvider, Tuple3<V1, V2, V3>> {
public final static class Mapper<V1, V2, V3> implements Function<Row, Tuple3<V1, V2, V3>> {
private final ColumnValueProvider provider;
private final CasserMappingProperty p1;
private final CasserMappingProperty p2;
private final CasserMappingProperty p3;
public Mapper(CasserMappingProperty p1, CasserMappingProperty p2, CasserMappingProperty p3) {
public Mapper(ColumnValueProvider provider, CasserMappingProperty p1, CasserMappingProperty p2, CasserMappingProperty p3) {
this.provider = provider;
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
}
@Override
public Tuple3<V1, V2, V3> apply(ColumnValueProvider provider) {
public Tuple3<V1, V2, V3> apply(Row row) {
return new Tuple3<V1, V2, V3>(
provider.getColumnValue(0, p1),
provider.getColumnValue(1, p2),
provider.getColumnValue(2, p3)
provider.getColumnValue(row, 0, p1),
provider.getColumnValue(row, 1, p2),
provider.getColumnValue(row, 2, p3)
);
}
}

View file

@ -20,6 +20,8 @@ import java.util.function.Function;
import casser.mapping.CasserMappingProperty;
import casser.mapping.ColumnValueProvider;
import com.datastax.driver.core.Row;
public final class Tuple4<V1, V2, V3, V4> {
public final V1 v1;
@ -34,19 +36,21 @@ public final class Tuple4<V1, V2, V3, V4> {
this.v4 = v4;
}
public final static class Mapper<V1, V2, V3, V4> implements Function<ColumnValueProvider, Tuple4<V1, V2, V3, V4>> {
public final static class Mapper<V1, V2, V3, V4> implements Function<Row, Tuple4<V1, V2, V3, V4>> {
private final ColumnValueProvider provider;
private final CasserMappingProperty p1;
private final CasserMappingProperty p2;
private final CasserMappingProperty p3;
private final CasserMappingProperty p4;
public Mapper(
public Mapper(ColumnValueProvider provider,
CasserMappingProperty p1,
CasserMappingProperty p2,
CasserMappingProperty p3,
CasserMappingProperty p4
) {
this.provider = provider;
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
@ -54,12 +58,12 @@ public final class Tuple4<V1, V2, V3, V4> {
}
@Override
public Tuple4<V1, V2, V3, V4> apply(ColumnValueProvider provider) {
public Tuple4<V1, V2, V3, V4> apply(Row row) {
return new Tuple4<V1, V2, V3, V4>(
provider.getColumnValue(0, p1),
provider.getColumnValue(1, p2),
provider.getColumnValue(2, p3),
provider.getColumnValue(3, p4)
provider.getColumnValue(row, 0, p1),
provider.getColumnValue(row, 1, p2),
provider.getColumnValue(row, 2, p3),
provider.getColumnValue(row, 3, p4)
);
}
}

View file

@ -20,6 +20,8 @@ import java.util.function.Function;
import casser.mapping.CasserMappingProperty;
import casser.mapping.ColumnValueProvider;
import com.datastax.driver.core.Row;
public final class Tuple5<V1, V2, V3, V4, V5> {
public final V1 v1;
@ -36,17 +38,19 @@ public final class Tuple5<V1, V2, V3, V4, V5> {
this.v5 = v5;
}
public final static class Mapper<V1, V2, V3, V4, V5> implements Function<ColumnValueProvider, Tuple5<V1, V2, V3, V4, V5>> {
public final static class Mapper<V1, V2, V3, V4, V5> implements Function<Row, Tuple5<V1, V2, V3, V4, V5>> {
private final ColumnValueProvider provider;
private final CasserMappingProperty p1, p2, p3, p4, p5;
public Mapper(
public Mapper(ColumnValueProvider provider,
CasserMappingProperty p1,
CasserMappingProperty p2,
CasserMappingProperty p3,
CasserMappingProperty p4,
CasserMappingProperty p5
) {
this.provider = provider;
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
@ -55,13 +59,13 @@ public final class Tuple5<V1, V2, V3, V4, V5> {
}
@Override
public Tuple5<V1, V2, V3, V4, V5> apply(ColumnValueProvider provider) {
public Tuple5<V1, V2, V3, V4, V5> apply(Row row) {
return new Tuple5<V1, V2, V3, V4, V5>(
provider.getColumnValue(0, p1),
provider.getColumnValue(1, p2),
provider.getColumnValue(2, p3),
provider.getColumnValue(3, p4),
provider.getColumnValue(4, p5)
provider.getColumnValue(row, 0, p1),
provider.getColumnValue(row, 1, p2),
provider.getColumnValue(row, 2, p3),
provider.getColumnValue(row, 3, p4),
provider.getColumnValue(row, 4, p5)
);
}
}

View file

@ -20,6 +20,8 @@ import java.util.function.Function;
import casser.mapping.CasserMappingProperty;
import casser.mapping.ColumnValueProvider;
import com.datastax.driver.core.Row;
public final class Tuple6<V1, V2, V3, V4, V5, V6> {
public final V1 v1;
@ -39,12 +41,13 @@ public final class Tuple6<V1, V2, V3, V4, V5, V6> {
}
public final static class Mapper<V1, V2, V3, V4, V5, V6> implements
Function<ColumnValueProvider,
Function<Row,
Tuple6<V1, V2, V3, V4, V5, V6>> {
private final ColumnValueProvider provider;
private final CasserMappingProperty p1, p2, p3, p4, p5, p6;
public Mapper(
public Mapper(ColumnValueProvider provider,
CasserMappingProperty p1,
CasserMappingProperty p2,
CasserMappingProperty p3,
@ -52,6 +55,7 @@ public final class Tuple6<V1, V2, V3, V4, V5, V6> {
CasserMappingProperty p5,
CasserMappingProperty p6
) {
this.provider = provider;
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
@ -61,14 +65,14 @@ public final class Tuple6<V1, V2, V3, V4, V5, V6> {
}
@Override
public Tuple6<V1, V2, V3, V4, V5, V6> apply(ColumnValueProvider provider) {
public Tuple6<V1, V2, V3, V4, V5, V6> apply(Row row) {
return new Tuple6<V1, V2, V3, V4, V5, V6>(
provider.getColumnValue(0, p1),
provider.getColumnValue(1, p2),
provider.getColumnValue(2, p3),
provider.getColumnValue(3, p4),
provider.getColumnValue(4, p5),
provider.getColumnValue(5, p6)
provider.getColumnValue(row, 0, p1),
provider.getColumnValue(row, 1, p2),
provider.getColumnValue(row, 2, p3),
provider.getColumnValue(row, 3, p4),
provider.getColumnValue(row, 4, p5),
provider.getColumnValue(row, 5, p6)
);
}
}

View file

@ -20,6 +20,8 @@ import java.util.function.Function;
import casser.mapping.CasserMappingProperty;
import casser.mapping.ColumnValueProvider;
import com.datastax.driver.core.Row;
public final class Tuple7<V1, V2, V3, V4, V5, V6, V7> {
public final V1 v1;
@ -41,12 +43,13 @@ public final class Tuple7<V1, V2, V3, V4, V5, V6, V7> {
}
public final static class Mapper<V1, V2, V3, V4, V5, V6, V7> implements
Function<ColumnValueProvider,
Function<Row,
Tuple7<V1, V2, V3, V4, V5, V6, V7>> {
private final ColumnValueProvider provider;
private final CasserMappingProperty p1, p2, p3, p4, p5, p6, p7;
public Mapper(
public Mapper(ColumnValueProvider provider,
CasserMappingProperty p1,
CasserMappingProperty p2,
CasserMappingProperty p3,
@ -55,6 +58,7 @@ public final class Tuple7<V1, V2, V3, V4, V5, V6, V7> {
CasserMappingProperty p6,
CasserMappingProperty p7
) {
this.provider = provider;
this.p1 = p1;
this.p2 = p2;
this.p3 = p3;
@ -65,15 +69,15 @@ public final class Tuple7<V1, V2, V3, V4, V5, V6, V7> {
}
@Override
public Tuple7<V1, V2, V3, V4, V5, V6, V7> apply(ColumnValueProvider provider) {
public Tuple7<V1, V2, V3, V4, V5, V6, V7> apply(Row row) {
return new Tuple7<V1, V2, V3, V4, V5, V6, V7>(
provider.getColumnValue(0, p1),
provider.getColumnValue(1, p2),
provider.getColumnValue(2, p3),
provider.getColumnValue(3, p4),
provider.getColumnValue(4, p5),
provider.getColumnValue(5, p6),
provider.getColumnValue(6, p7)
provider.getColumnValue(row, 0, p1),
provider.getColumnValue(row, 1, p2),
provider.getColumnValue(row, 2, p3),
provider.getColumnValue(row, 3, p4),
provider.getColumnValue(row, 4, p5),
provider.getColumnValue(row, 5, p6),
provider.getColumnValue(row, 6, p7)
);
}
}

View file

@ -15,8 +15,10 @@
*/
package casser.mapping;
import com.datastax.driver.core.Row;
public interface ColumnValueProvider {
<V> V getColumnValue(int columnIndex, CasserMappingProperty property);
<V> V getColumnValue(Row row, int columnIndex, CasserMappingProperty property);
}

View file

@ -30,22 +30,20 @@ import com.datastax.driver.core.Row;
public final class RowColumnValueProvider implements ColumnValueProvider {
private final CasserMappingRepository repository;
private final Row source;
private final ColumnDefinitions columnDefinitions;
public RowColumnValueProvider(CasserMappingRepository repository, Row source) {
public RowColumnValueProvider(CasserMappingRepository repository) {
this.repository = repository;
this.source = source;
this.columnDefinitions = source.getColumnDefinitions();
}
@Override
public <V> V getColumnValue(int columnIndex, CasserMappingProperty property) {
public <V> V getColumnValue(Row source, int columnIndex, CasserMappingProperty property) {
if (source.isNull(columnIndex)) {
return null;
}
ColumnDefinitions columnDefinitions = source.getColumnDefinitions();
DataType columnType = columnDefinitions.getType(columnIndex);
if (columnType.isCollection()) {