From 916b46c0ce7b29ae51f067e214c8f159e88231c7 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Wed, 28 Mar 2018 03:50:35 +0200 Subject: [PATCH] [1.3.2] DefaultListener. --- .../ass1/jpa/listener/DefaultListener.java | 69 +++++++++++++++---- ass1-jpa/src/main/resources/META-INF/orm.xml | 13 ++-- 2 files changed, 63 insertions(+), 19 deletions(-) diff --git a/ass1-jpa/src/main/java/dst/ass1/jpa/listener/DefaultListener.java b/ass1-jpa/src/main/java/dst/ass1/jpa/listener/DefaultListener.java index bd3a7bf..6adf190 100644 --- a/ass1-jpa/src/main/java/dst/ass1/jpa/listener/DefaultListener.java +++ b/ass1-jpa/src/main/java/dst/ass1/jpa/listener/DefaultListener.java @@ -1,44 +1,83 @@ package dst.ass1.jpa.listener; +import javax.persistence.*; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLongArray; + public class DefaultListener { + public static final int LOAD_OPS = 0; + public static final int UPDATE_OPS = 1; + public static final int REMOVE_OPS = 2; + public static final int PERSIST_OPS = 3; + public static final int PERSIST_TIME = 4; + public static final int ATOMIC_ARRAY_SIZE = 5; - // TODO + private static final ConcurrentHashMap startTimes = new ConcurrentHashMap<>(); + private static AtomicLongArray data = new AtomicLongArray(ATOMIC_ARRAY_SIZE); public static int getLoadOperations() { - // TODO - return -1; + return (int) DefaultListener.data.get(LOAD_OPS); } public static int getUpdateOperations() { - // TODO - return -1; + return (int) DefaultListener.data.get(UPDATE_OPS); } public static int getRemoveOperations() { - // TODO - return -1; + return (int) DefaultListener.data.get(REMOVE_OPS); } public static int getPersistOperations() { - // TODO - return -1; + return (int) DefaultListener.data.get(PERSIST_OPS); } - public static long getOverallTimeToPersist() { - // TODO - return -1; + public static Integer getOverallTimeToPersist() { + return (int) DefaultListener.data.get(PERSIST_TIME); } public static double getAverageTimeToPersist() { - // TODO - return -1; + AtomicLongArray localdata = DefaultListener.data; + return (double) localdata.get(PERSIST_TIME) / localdata.get(PERSIST_OPS); } + /** * Clears the internal data structures that are used for storing the operations. */ public static void clear() { - // TODO + DefaultListener.data = new AtomicLongArray(DefaultListener.ATOMIC_ARRAY_SIZE); + } + + /** + * Track operations + times. + */ + @PostRemove + public void postRemove(Object obj) { + DefaultListener.data.incrementAndGet(REMOVE_OPS); + } + + @PostUpdate + public void postUpdate(Object obj) { + DefaultListener.data.incrementAndGet(UPDATE_OPS); + } + + @PostLoad + public void postLoad(Object obj) { + DefaultListener.data.incrementAndGet(LOAD_OPS); + } + + @PrePersist + public void prePersist(Object obj) { + startTimes.put(obj, System.currentTimeMillis()); + } + + @PostPersist + public void postPersist(Object obj) { + long startTime = startTimes.remove(obj); + + AtomicLongArray localdata = DefaultListener.data; + localdata.addAndGet(PERSIST_TIME, System.currentTimeMillis() - startTime); + localdata.incrementAndGet(PERSIST_OPS); } } diff --git a/ass1-jpa/src/main/resources/META-INF/orm.xml b/ass1-jpa/src/main/resources/META-INF/orm.xml index 1291bec..cfccf40 100644 --- a/ass1-jpa/src/main/resources/META-INF/orm.xml +++ b/ass1-jpa/src/main/resources/META-INF/orm.xml @@ -1,9 +1,14 @@ - - - + + + + + + + -- 2.43.0