From 7ac9288eb8d1182dcd4c0b0009e897c7c3eee103 Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Fri, 25 Aug 2017 16:08:31 -0400 Subject: [PATCH] Added update(draft). --- pom.xml | 2 +- .../java/net/helenus/core/HelenusSession.java | 57 +++++++++++++++++- .../net/helenus/core/reflect/Drafted.java | 4 ++ .../core/simple/SimpleUserTest.java | 59 +++++++++++++++---- .../helenus/test/unit/core/dsl/Account.java | 5 ++ 5 files changed, 111 insertions(+), 16 deletions(-) diff --git a/pom.xml b/pom.xml index d924183..e4586cb 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ 4.0.0 net.helenus helenus-core - 2.0.21-SNAPSHOT + 2.0.23-SNAPSHOT jar helenus diff --git a/src/main/java/net/helenus/core/HelenusSession.java b/src/main/java/net/helenus/core/HelenusSession.java index a5e70cf..7557611 100644 --- a/src/main/java/net/helenus/core/HelenusSession.java +++ b/src/main/java/net/helenus/core/HelenusSession.java @@ -23,23 +23,27 @@ import com.google.common.cache.Cache; import com.google.common.cache.CacheBuilder; import java.io.Closeable; import java.io.PrintStream; -import java.util.Map; -import java.util.Objects; -import java.util.Set; +import java.lang.reflect.Method; +import java.util.*; import java.util.concurrent.Executor; import java.util.concurrent.TimeUnit; import java.util.function.Function; import net.helenus.core.operation.*; import net.helenus.core.reflect.Drafted; +import net.helenus.core.reflect.DslExportable; import net.helenus.core.reflect.HelenusPropertyNode; import net.helenus.mapping.HelenusEntity; +import net.helenus.mapping.HelenusProperty; import net.helenus.mapping.MappingUtil; import net.helenus.mapping.value.*; +import net.helenus.support.DslPropertyException; import net.helenus.support.Fun; import net.helenus.support.Fun.Tuple1; import net.helenus.support.Fun.Tuple2; import net.helenus.support.Fun.Tuple6; +import static net.helenus.core.Query.eq; + public final class HelenusSession extends AbstractSessionOperations implements Closeable { private final int MAX_CACHE_SIZE = 10000; @@ -385,6 +389,53 @@ public final class HelenusSession extends AbstractSessionOperations implements C return new UpdateOperation(this); } + public UpdateOperation update(Drafted draft) { + UpdateOperation update = new UpdateOperation(this); + Map map = draft.toMap(); + HelenusEntity entity = draft.getEntity(); + Set mutatedProperties = draft.mutated(); + + // Add all the mutated values contained in the draft. + entity.getOrderedProperties().forEach(property -> { + switch (property.getColumnType()) { + case PARTITION_KEY: + case CLUSTERING_COLUMN: + break; + default: + String propertyName = property.getPropertyName(); + if (mutatedProperties.contains(propertyName)) { + Object value = map.get(propertyName); + Getter getter = new Getter() { + @Override + public Object get() { + throw new DslPropertyException(new HelenusPropertyNode(property, Optional.empty())); + } + }; + update.set(getter, value); + } + } + }); + + // Add the partition and clustering keys if they were in the draft (normally the case). + entity.getOrderedProperties().forEach(property -> { + switch (property.getColumnType()) { + case PARTITION_KEY: + case CLUSTERING_COLUMN: + String propertyName = property.getPropertyName(); + Object value = map.get(propertyName); + Getter getter = new Getter() { + @Override + public Object get() { + throw new DslPropertyException(new HelenusPropertyNode(property, Optional.empty())); + } + }; + update.where(getter, eq(value)); + } + }); + + return update; + } + public UpdateOperation update(Getter getter, V v) { Objects.requireNonNull(getter, "field is empty"); Objects.requireNonNull(v, "value is empty"); diff --git a/src/main/java/net/helenus/core/reflect/Drafted.java b/src/main/java/net/helenus/core/reflect/Drafted.java index b0a187d..e714658 100644 --- a/src/main/java/net/helenus/core/reflect/Drafted.java +++ b/src/main/java/net/helenus/core/reflect/Drafted.java @@ -1,9 +1,13 @@ package net.helenus.core.reflect; +import net.helenus.mapping.HelenusEntity; + import java.util.Set; public interface Drafted extends MapExportable { + HelenusEntity getEntity(); + Set mutated(); T build(); diff --git a/src/test/java/net/helenus/test/integration/core/simple/SimpleUserTest.java b/src/test/java/net/helenus/test/integration/core/simple/SimpleUserTest.java index 5e4c43b..564958d 100644 --- a/src/test/java/net/helenus/test/integration/core/simple/SimpleUserTest.java +++ b/src/test/java/net/helenus/test/integration/core/simple/SimpleUserTest.java @@ -20,12 +20,19 @@ import static net.helenus.core.Query.eq; import net.helenus.core.Helenus; import net.helenus.core.HelenusSession; import net.helenus.core.Operator; +import net.helenus.core.reflect.Drafted; +import net.helenus.mapping.HelenusEntity; import net.helenus.support.Fun; import net.helenus.test.integration.build.AbstractEmbeddedCassandraTest; import org.junit.Assert; import org.junit.BeforeClass; import org.junit.Test; +import java.util.HashMap; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; + public class SimpleUserTest extends AbstractEmbeddedCassandraTest { @@ -85,13 +92,13 @@ public class SimpleUserTest extends AbstractEmbeddedCassandraTest { // select row and map to entity User actual = - session - .selectAll(User.class) - .mapTo(User.class) - .where(user::id, eq(100L)) - .sync() - .findFirst() - .get(); + session + .selectAll(User.class) + .mapTo(User.class) + .where(user::id, eq(100L)) + .sync() + .findFirst() + .get(); assertUsers(newUser, actual); // select as object @@ -150,7 +157,7 @@ public class SimpleUserTest extends AbstractEmbeddedCassandraTest { name = (String) session - .select() + .select() .column(user::name) .where(user::id, eq(100L)) .sync() @@ -162,11 +169,39 @@ public class SimpleUserTest extends AbstractEmbeddedCassandraTest { // UPDATE + session.update(new Drafted() { + + @Override + public HelenusEntity getEntity() { return Helenus.entity(User.class); } + + @Override + public Map toMap() { + HashMap map = new HashMap(); + map.put("name", "joeseph"); + map.put("age", Integer.valueOf(45)); + map.put("id", 100L); + return map; + } + + @Override + public Set mutated() { + Set set = new HashSet(); + set.add("name"); + set.add("age"); + return set; + } + + @Override + public User build() { + return null; + } + }).sync(); + session - .update(user::name, "albert") - .set(user::age, 35) - .where(user::id, Operator.EQ, 100L) - .sync(); + .update(user::name, "albert") + .set(user::age, 35) + .where(user::id, Operator.EQ, 100L) + .sync(); long cnt = session.count(user).where(user::id, Operator.EQ, 100L).sync(); Assert.assertEquals(1L, cnt); diff --git a/src/test/java/net/helenus/test/unit/core/dsl/Account.java b/src/test/java/net/helenus/test/unit/core/dsl/Account.java index 57bfdc0..03779f5 100644 --- a/src/test/java/net/helenus/test/unit/core/dsl/Account.java +++ b/src/test/java/net/helenus/test/unit/core/dsl/Account.java @@ -19,6 +19,7 @@ import java.util.Date; import java.util.Map; import java.util.Set; import net.helenus.core.reflect.Drafted; +import net.helenus.mapping.HelenusEntity; import net.helenus.mapping.annotation.*; @Table @@ -40,6 +41,10 @@ public interface Account { } class Draft implements Drafted { //TODO + + @Override + public HelenusEntity getEntity() { return null; } + @Override public Set mutated() { return null;