mirror of
https://github.com/ebean-orm/ebean.git
synced 2024-04-21 10:51:47 +00:00
Refactor tidy NodeUsageCollector, remove unused loadProperty and unnecessary WeakReference for State.listener
This commit is contained in:
@@ -874,9 +874,6 @@ public final class EntityBeanIntercept implements Serializable {
|
||||
}
|
||||
if (lazyLoadProperty == -1) {
|
||||
lazyLoadProperty = loadProperty;
|
||||
if (nodeUsageCollector != null) {
|
||||
nodeUsageCollector.setLoadProperty(getProperty(lazyLoadProperty));
|
||||
}
|
||||
loader.loadBean(this);
|
||||
if (lazyLoadFailure) {
|
||||
// failed when lazy loading this bean
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package io.ebean.bean;
|
||||
|
||||
import java.lang.ref.Cleaner;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
@@ -15,7 +14,8 @@ public final class NodeUsageCollector {
|
||||
private final static Cleaner cleaner = Cleaner.create();
|
||||
|
||||
public static final class State implements Runnable {
|
||||
private final WeakReference<NodeUsageListener> managerRef;
|
||||
|
||||
private final NodeUsageListener listener;
|
||||
/**
|
||||
* The properties used at this profile point.
|
||||
*/
|
||||
@@ -29,14 +29,9 @@ public final class NodeUsageCollector {
|
||||
*/
|
||||
private boolean modified;
|
||||
|
||||
/**
|
||||
* The property that cause a reference to lazy load.
|
||||
*/
|
||||
private String loadProperty;
|
||||
|
||||
private State(ObjectGraphNode node, WeakReference<NodeUsageListener> managerRef) {
|
||||
private State(ObjectGraphNode node, NodeUsageListener listener) {
|
||||
this.node = node;
|
||||
this.managerRef = managerRef;
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -46,10 +41,7 @@ public final class NodeUsageCollector {
|
||||
|
||||
@Override
|
||||
public void run() {
|
||||
NodeUsageListener manager = managerRef.get();
|
||||
if (manager != null) {
|
||||
manager.collectNodeUsage(this);
|
||||
}
|
||||
listener.collectNodeUsage(this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -84,8 +76,8 @@ public final class NodeUsageCollector {
|
||||
|
||||
private final State state;
|
||||
|
||||
public NodeUsageCollector(ObjectGraphNode node, WeakReference<NodeUsageListener> managerRef) {
|
||||
this.state = new State(node, managerRef);
|
||||
public NodeUsageCollector(ObjectGraphNode node, NodeUsageListener listener) {
|
||||
this.state = new State(node, listener);
|
||||
cleaner.register(this, state);
|
||||
}
|
||||
|
||||
@@ -110,13 +102,6 @@ public final class NodeUsageCollector {
|
||||
state.used.add(property);
|
||||
}
|
||||
|
||||
/**
|
||||
* The property that invoked a lazy load.
|
||||
*/
|
||||
public void setLoadProperty(String loadProperty) {
|
||||
state.loadProperty = loadProperty;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return state.toString();
|
||||
|
||||
@@ -3,24 +3,21 @@ package io.ebean.bean;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class NodeUsageCollectorTest {
|
||||
|
||||
private final Listener listener = new Listener();
|
||||
|
||||
private final ObjectGraphNode node = new ObjectGraphNode((ObjectGraphOrigin) null, "foo");
|
||||
|
||||
/**
|
||||
* Run this manually as we make explicit GC call here.
|
||||
* Run this manually as we make use of explicit GC call here which is dubious.
|
||||
*/
|
||||
@Disabled
|
||||
@Test
|
||||
void test() throws InterruptedException {
|
||||
WeakReference<NodeUsageListener> profilingListenerRef = new WeakReference<>(listener);
|
||||
|
||||
ObjectGraphNode node = new ObjectGraphNode((ObjectGraphOrigin)null, "foo");
|
||||
NodeUsageCollector c = new NodeUsageCollector(node, profilingListenerRef);
|
||||
NodeUsageCollector c = new NodeUsageCollector(node, listener);
|
||||
c.addUsed("a");
|
||||
c.addUsed("b");
|
||||
c = null;
|
||||
@@ -29,6 +26,7 @@ class NodeUsageCollectorTest {
|
||||
Thread.sleep(100);
|
||||
|
||||
assertThat(listener.collectCount).isEqualTo(1);
|
||||
assertThat(node).isNotNull();
|
||||
}
|
||||
|
||||
static class Listener implements NodeUsageListener {
|
||||
|
||||
+11
-1
@@ -1,6 +1,7 @@
|
||||
package io.ebeaninternal.server.autotune.service;
|
||||
|
||||
import io.ebean.bean.NodeUsageCollector;
|
||||
import io.ebean.bean.NodeUsageListener;
|
||||
import io.ebean.bean.ObjectGraphNode;
|
||||
import io.ebean.bean.ObjectGraphOrigin;
|
||||
import io.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
@@ -13,6 +14,15 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class ProfileOriginTest extends BaseTestCase {
|
||||
|
||||
static class Noop implements NodeUsageListener {
|
||||
@Override
|
||||
public void collectNodeUsage(NodeUsageCollector.State state) {
|
||||
// do nothing
|
||||
}
|
||||
}
|
||||
|
||||
private final NodeUsageListener listener = new Noop();
|
||||
|
||||
private final BeanDescriptor<Order> desc = getBeanDescriptor(Order.class);
|
||||
|
||||
@Test
|
||||
@@ -121,7 +131,7 @@ public class ProfileOriginTest extends BaseTestCase {
|
||||
|
||||
private NodeUsageCollector node(String path) {
|
||||
ObjectGraphNode node = new ObjectGraphNode((ObjectGraphOrigin)null, path);
|
||||
return new NodeUsageCollector(node, null);
|
||||
return new NodeUsageCollector(node, listener);
|
||||
}
|
||||
|
||||
// @Test
|
||||
|
||||
@@ -18,7 +18,6 @@ import io.ebeaninternal.server.core.SpiOrmQueryRequest;
|
||||
import io.ebeaninternal.server.deploy.*;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
import java.lang.ref.WeakReference;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
@@ -149,8 +148,6 @@ public final class CQuery<T> implements DbReadContext, CancelableQuery, SpiProfi
|
||||
|
||||
private final ProfilingListener profilingListener;
|
||||
|
||||
private final WeakReference<NodeUsageListener> profilingListenerRef;
|
||||
|
||||
private final Boolean readOnly;
|
||||
|
||||
private long profileOffset;
|
||||
@@ -189,7 +186,6 @@ public final class CQuery<T> implements DbReadContext, CancelableQuery, SpiProfi
|
||||
this.objectGraphNode = query.getParentNode();
|
||||
this.profilingListener = query.getProfilingListener();
|
||||
this.autoTuneProfiling = profilingListener != null;
|
||||
this.profilingListenerRef = autoTuneProfiling ? new WeakReference<>(profilingListener) : null;
|
||||
// set the generated sql back to the query
|
||||
// so its available to the user...
|
||||
query.setGeneratedSql(queryPlan.getSql());
|
||||
@@ -673,7 +669,7 @@ public final class CQuery<T> implements DbReadContext, CancelableQuery, SpiProfi
|
||||
@Override
|
||||
public void profileBean(EntityBeanIntercept ebi, String prefix) {
|
||||
ObjectGraphNode node = request.loadContext().getObjectGraphNode(prefix);
|
||||
ebi.setNodeUsageCollector(new NodeUsageCollector(node, profilingListenerRef));
|
||||
ebi.setNodeUsageCollector(new NodeUsageCollector(node, profilingListener));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in New Issue
Block a user