Commit 86ea7355 authored by Matija Obreza's avatar Matija Obreza

Bulk add items to shared BlockingQueue

parent ec50cbc1
......@@ -16,6 +16,9 @@
package org.genesys2.server.component.aspect;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import javax.annotation.Resource;
......@@ -26,6 +29,7 @@ import org.aspectj.lang.annotation.Aspect;
import org.genesys2.server.component.elastic.ElasticReindex;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.model.genesys.AccessionRef;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -59,6 +63,24 @@ public class ExtraReindexingAspect {
}
}
/**
* After AccessionIdRepository#save
*
* @param joinPoint the join point
* @param result the result
*/
@AfterReturning(value = "execution(* org.genesys2.server.persistence.AccessionRefRepository.save(..))", returning = "result")
public void afterPersistAccessionRef(final JoinPoint joinPoint, final Object result) {
LOG.trace("accessionRefRepository#save {} {}", joinPoint.toLongString(), joinPoint.getTarget());
try {
scheduleReindexing(result);
} catch (Throwable e) {
LOG.error(e.getMessage(), e);
}
}
/**
* Flatten collections (if Iterable) and check each object class if indexed
*
......@@ -72,13 +94,32 @@ public class ExtraReindexingAspect {
if (toReindex instanceof Iterable<?>) {
// Iterate and test
Iterable<?> it = (Iterable<?>) toReindex;
for (Object obj : it) {
scheduleReindexing(obj);
}
List<ElasticReindex> list = new ArrayList<>();
it.forEach((item) -> addNotNull(list, forReindexing(item)));
elasticReindexQueue.addAll(list);
} else {
addNotNull(elasticReindexQueue, forReindexing(toReindex));
}
}
private <T> void addNotNull(Collection<T> destination, T element) {
if (element != null) {
destination.add(element);
}
}
private ElasticReindex forReindexing(Object toReindex) {
if (toReindex instanceof AccessionId){
AccessionId accessionId = (AccessionId) toReindex;
LOG.trace("Scheduling reindexing of {} {}", Accession.class.getName(), accessionId.getId());
elasticReindexQueue.add(new ElasticReindex(Accession.class.getName(), accessionId.getId()));
return new ElasticReindex(Accession.class.getName(), accessionId.getId());
} else if (toReindex instanceof AccessionRef){
AccessionRef accessionRef = (AccessionRef) toReindex;
if (accessionRef.getAccession() != null) {
LOG.trace("Scheduling reindexing of {} {}", Accession.class.getName(), accessionRef.getAccession().getId());
return new ElasticReindex(Accession.class.getName(), accessionRef.getAccession().getId());
}
}
return null;
}
}
......@@ -18,8 +18,11 @@ package org.genesys2.server.component.elastic;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
......@@ -129,6 +132,7 @@ public class ElasticJPAListener {
}
}
/**
* Flatten collections (if Iterable) and check each object class if indexed
*
......@@ -142,23 +146,35 @@ public class ElasticJPAListener {
if (toReindex instanceof Iterable<?>) {
// Iterate and test
Iterable<?> it = (Iterable<?>) toReindex;
for (Object obj : it) {
scheduleReindexing(obj);
}
List<ElasticReindex> list = new ArrayList<>();
it.forEach((item) -> addNotNull(list, forReindexing(item)));
elasticReindexQueue.addAll(list);
} else {
Class<?> clazz = toReindex.getClass();
if (isIndexed(clazz)) {
LOG.trace("Scheduling reindexing of {} {}", clazz.getName(), toReindex);
if (toReindex instanceof BasicModel) {
BasicModel entity = (BasicModel) toReindex;
elasticReindexQueue.add(new ElasticReindex(clazz.getName(), entity.getId()));
} else {
LOG.warn("Don't know how to index {}. Not a BasicModel.", clazz.getName());
}
addNotNull(elasticReindexQueue, forReindexing(toReindex));
}
}
private <T> void addNotNull(Collection<T> destination, T element) {
if (element != null) {
destination.add(element);
}
}
private ElasticReindex forReindexing(Object toReindex) {
Class<?> clazz = toReindex.getClass();
if (isIndexed(clazz)) {
LOG.trace("Scheduling reindexing of {} {}", clazz.getName(), toReindex);
if (toReindex instanceof BasicModel) {
BasicModel entity = (BasicModel) toReindex;
return new ElasticReindex(clazz.getName(), entity.getId());
} else {
LOG.warn("Don't know how to index {}. Not a BasicModel.", clazz.getName());
}
}
return null;
}
private boolean isIndexed(Class<?> clazz) {
if (ignoredClasses.contains(clazz)) {
return false;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment