forked from infinispan/infinispan
/
IndexWorker.java
133 lines (112 loc) · 4.41 KB
/
IndexWorker.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package org.infinispan.query.impl.massindex;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.stream.Stream;
import org.infinispan.Cache;
import org.infinispan.commons.marshall.AbstractExternalizer;
import org.infinispan.compat.TypeConverter;
import org.infinispan.container.entries.CacheEntry;
import org.infinispan.context.Flag;
import org.infinispan.distexec.DistributedCallable;
import org.infinispan.factories.ComponentRegistry;
import org.infinispan.filter.AcceptAllKeyValueFilter;
import org.infinispan.filter.CacheFilters;
import org.infinispan.filter.KeyValueFilter;
import org.infinispan.interceptors.locking.ClusteringDependentLogic;
import org.infinispan.metadata.Metadata;
import org.infinispan.query.impl.externalizers.ExternalizerIds;
/**
* Base class for mass indexer tasks.
*
* @author gustavonalle
* @since 7.1
*/
public class IndexWorker implements DistributedCallable<Object, Object, Void> {
protected Cache<Object, Object> cache;
protected TypeConverter typeConverter;
protected final Class<?> entity;
private final boolean flush;
private final boolean clean;
private final boolean primaryOwner;
protected IndexUpdater indexUpdater;
private ClusteringDependentLogic clusteringDependentLogic;
public IndexWorker(Class<?> entity, boolean flush, boolean clean, boolean primaryOwner) {
this.entity = entity;
this.flush = flush;
this.clean = clean;
this.primaryOwner = primaryOwner;
}
@Override
public void setEnvironment(Cache<Object, Object> cache, Set<Object> inputKeys) {
this.cache = cache;
this.indexUpdater = new IndexUpdater(cache);
ComponentRegistry componentRegistry = cache.getAdvancedCache().getComponentRegistry();
this.clusteringDependentLogic = componentRegistry.getComponent(ClusteringDependentLogic.class);
this.typeConverter = componentRegistry.getComponent(TypeConverter.class);
}
protected void preIndex() {
if (clean) indexUpdater.purge(entity);
}
protected void postIndex() {
indexUpdater.waitForAsyncCompletion();
if (flush) indexUpdater.flush(entity);
}
private KeyValueFilter getFilter() {
return primaryOwner ? new PrimaryOwnersKeyValueFilter() : AcceptAllKeyValueFilter.getInstance();
}
private Object extractValue(Object wrappedValue) {
if (typeConverter != null) {
return typeConverter.unboxValue(wrappedValue);
}
return wrappedValue;
}
@Override
@SuppressWarnings("unchecked")
public Void call() throws Exception {
preIndex();
KeyValueFilter filter = getFilter();
try (Stream<CacheEntry<Object, Object>> stream = cache.getAdvancedCache().withFlags(Flag.CACHE_MODE_LOCAL)
.cacheEntrySet().stream()) {
Iterator<CacheEntry<Object, Object>> iterator = stream.filter(CacheFilters.predicate(filter)).iterator();
while (iterator.hasNext()) {
CacheEntry<Object, Object> next = iterator.next();
Object value = extractValue(next.getValue());
if (value != null && value.getClass().equals(entity))
indexUpdater.updateIndex(next.getKey(), value);
}
}
postIndex();
return null;
}
private class PrimaryOwnersKeyValueFilter implements KeyValueFilter {
@Override
public boolean accept(Object key, Object value, Metadata metadata) {
return clusteringDependentLogic.localNodeIsPrimaryOwner(key);
}
}
public static class Externalizer extends AbstractExternalizer<IndexWorker> {
@Override
public Set<Class<? extends IndexWorker>> getTypeClasses() {
return Collections.singleton(IndexWorker.class);
}
@Override
public void writeObject(ObjectOutput output, IndexWorker worker) throws IOException {
output.writeObject(worker.entity);
output.writeBoolean(worker.flush);
output.writeBoolean(worker.clean);
output.writeBoolean(worker.primaryOwner);
}
@Override
public IndexWorker readObject(ObjectInput input) throws IOException, ClassNotFoundException {
return new IndexWorker((Class<?>) input.readObject(), input.readBoolean(), input.readBoolean(), input.readBoolean());
}
@Override
public Integer getId() {
return ExternalizerIds.INDEX_WORKER;
}
}
}