1
|
package eu.dnetlib.data.resultSet;
|
2
|
|
3
|
import java.util.UUID;
|
4
|
|
5
|
import javax.annotation.PostConstruct;
|
6
|
|
7
|
import org.apache.commons.logging.Log;
|
8
|
import org.apache.commons.logging.LogFactory;
|
9
|
import org.springframework.beans.factory.annotation.Required;
|
10
|
|
11
|
import com.google.common.cache.Cache;
|
12
|
import com.google.common.cache.CacheBuilder;
|
13
|
import com.google.common.cache.RemovalListener;
|
14
|
import com.google.common.cache.RemovalNotification;
|
15
|
|
16
|
import eu.dnetlib.rmi.soap.exceptions.ResultSetException;
|
17
|
|
18
|
public class ResultSetRegistry {
|
19
|
|
20
|
private static final Log log = LogFactory.getLog(ResultSetRegistry.class);
|
21
|
|
22
|
private CacheBuilder<String, ResultSetIterator<?>> cacheBuilder;
|
23
|
private Cache<String, ResultSetIterator<?>> cache;
|
24
|
|
25
|
@PostConstruct
|
26
|
public void initCache() {
|
27
|
cache = cacheBuilder.removalListener(new RemovalListener<String, ResultSetIterator<?>>() {
|
28
|
|
29
|
@Override
|
30
|
public void onRemoval(final RemovalNotification<String, ResultSetIterator<?>> notification) {
|
31
|
log.info("Deleting old resultset: " + notification.getKey() + " - finished: " + notification.getValue().isFinished());
|
32
|
}
|
33
|
}).build();
|
34
|
}
|
35
|
|
36
|
public ResultSetIterator<?> findIterator(final String rsId) throws ResultSetException {
|
37
|
|
38
|
final ResultSetIterator<?> iter = cache.getIfPresent(rsId);
|
39
|
if (iter != null) {
|
40
|
return iter;
|
41
|
} else {
|
42
|
log.error("Resultset not found, id = " + rsId);
|
43
|
throw new ResultSetException("ResultSet not found, id = " + rsId);
|
44
|
}
|
45
|
}
|
46
|
|
47
|
public String registerIterator(final ResultSetIterator<?> iter) {
|
48
|
final String rsId = "rs-" + UUID.randomUUID();
|
49
|
|
50
|
cache.put(rsId, iter);
|
51
|
|
52
|
return rsId;
|
53
|
}
|
54
|
|
55
|
public CacheBuilder<String, ResultSetIterator<?>> getCacheBuilder() {
|
56
|
return cacheBuilder;
|
57
|
}
|
58
|
|
59
|
@Required
|
60
|
public void setCacheBuilder(final CacheBuilder<String, ResultSetIterator<?>> cacheBuilder) {
|
61
|
this.cacheBuilder = cacheBuilder;
|
62
|
}
|
63
|
|
64
|
}
|