From 9cfdd92506ef353411313d7396b4de9b1be32af6 Mon Sep 17 00:00:00 2001 From: Alexander Date: Wed, 13 Apr 2022 16:54:27 +0300 Subject: [PATCH] feature: add event store improvements --- .../java/com/eventsourcing/es/EventStore.java | 23 +++++++++++++------ 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/eventsourcing/es/EventStore.java b/src/main/java/com/eventsourcing/es/EventStore.java index 5ce8024..32f2078 100644 --- a/src/main/java/com/eventsourcing/es/EventStore.java +++ b/src/main/java/com/eventsourcing/es/EventStore.java @@ -9,7 +9,6 @@ import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; -import java.lang.reflect.InvocationTargetException; import java.util.*; @Repository @@ -17,7 +16,8 @@ import java.util.*; @Slf4j public class EventStore implements EventStoreDB { - // private final JdbcTemplate jdbcTemplate; + public static final int SNAPSHOT_FREQUENCY = 3; + private final NamedParameterJdbcTemplate jdbcTemplate; @Override @@ -83,7 +83,7 @@ public class EventStore implements EventStoreDB { } this.saveEvents(aggregate.getChanges()); - if (aggregate.getVersion() % 3 == 0) { + if (aggregate.getVersion() % SNAPSHOT_FREQUENCY == 0) { this.saveSnapshot(aggregate); } @@ -120,9 +120,8 @@ public class EventStore implements EventStoreDB { private T getAggregate(final String aggregateId, final Class aggregateType) { try { return aggregateType.getConstructor(String.class).newInstance(aggregateId); - } catch (InstantiationException | IllegalAccessException | InvocationTargetException | - NoSuchMethodException e) { - throw new RuntimeException(e); + } catch (Exception ex) { + throw new RuntimeException(ex); } } @@ -150,11 +149,21 @@ public class EventStore implements EventStoreDB { if (aggregate.getVersion() == 0) throw new AggregateNotFoundException(aggregateId); + log.info("(load) loaded aggregate: {}", aggregate); return aggregate; } @Override public Boolean exists(String aggregateId) { - return null; + try { + final var id = jdbcTemplate.queryForObject("SELECT aggregate_id FROM events WHERE e e.aggregate_id = :aggregate_id", Map.of("aggregate_id", aggregateId), String.class); + log.info("aggregate exists id: {}", id); + return true; + } catch (Exception ex) { + if (!(ex instanceof EmptyResultDataAccessException)) { + throw new RuntimeException("exists", ex); + } + return false; + } } }