/
JPAContinuousQueryProtobufCacheEventFilterConverter.java
153 lines (132 loc) · 6.22 KB
/
JPAContinuousQueryProtobufCacheEventFilterConverter.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package org.infinispan.query.remote.impl.filter;
import org.infinispan.Cache;
import org.infinispan.commons.CacheException;
import org.infinispan.commons.io.UnsignedNumeric;
import org.infinispan.commons.marshall.AbstractExternalizer;
import org.infinispan.configuration.cache.Configuration;
import org.infinispan.factories.ComponentRegistry;
import org.infinispan.metadata.Metadata;
import org.infinispan.notifications.cachelistener.filter.EventType;
import org.infinispan.objectfilter.Matcher;
import org.infinispan.objectfilter.ObjectFilter;
import org.infinispan.protostream.ProtobufUtil;
import org.infinispan.protostream.SerializationContext;
import org.infinispan.query.continuous.impl.JPAContinuousQueryCacheEventFilterConverter;
import org.infinispan.query.remote.client.ContinuousQueryResult;
import org.infinispan.query.remote.impl.CompatibilityReflectionMatcher;
import org.infinispan.query.remote.impl.ExternalizerIds;
import org.infinispan.query.remote.impl.ProtobufMetadataManagerImpl;
import org.infinispan.query.remote.impl.indexing.ProtobufValueWrapper;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* @author anistor@redhat.com
* @since 8.0
*/
public final class JPAContinuousQueryProtobufCacheEventFilterConverter extends JPAContinuousQueryCacheEventFilterConverter<Object, Object, Object> {
private SerializationContext serCtx;
private boolean usesValueWrapper;
private boolean isCompatMode;
public JPAContinuousQueryProtobufCacheEventFilterConverter(String jpaQuery, Map<String, Object> namedParameters, Class<? extends Matcher> matcherImplClass) {
super(jpaQuery, namedParameters, matcherImplClass);
}
@Override
protected void injectDependencies(ComponentRegistry componentRegistry, Cache c) {
serCtx = ProtobufMetadataManagerImpl.getSerializationContextInternal(c.getCacheManager());
Configuration cfg = c.getCacheConfiguration();
isCompatMode = cfg.compatibility().enabled();
usesValueWrapper = cfg.indexing().index().isEnabled() && !isCompatMode;
if (isCompatMode) {
matcherImplClass = CompatibilityReflectionMatcher.class;
}
super.injectDependencies(componentRegistry, c);
}
@Override
public Object filterAndConvert(Object key, Object oldValue, Metadata oldMetadata, Object newValue, Metadata newMetadata, EventType eventType) {
if (usesValueWrapper) {
oldValue = oldValue != null ? ((ProtobufValueWrapper) oldValue).getBinary() : null;
newValue = newValue != null ? ((ProtobufValueWrapper) newValue).getBinary() : null;
}
if (eventType.isExpired()) {
oldValue = newValue; // expired events have the expired value as newValue
newValue = null;
}
ObjectFilter objectFilter = getObjectFilter();
ObjectFilter.FilterResult f1 = oldValue == null ? null : objectFilter.filter(oldValue);
ObjectFilter.FilterResult f2 = newValue == null ? null : objectFilter.filter(newValue);
if (f1 == null && f2 != null) {
return makeFilterResult(true, key, f2.getProjection() == null ? newValue : null, f2.getProjection());
} else if (f1 != null && f2 == null) {
return makeFilterResult(false, key, null, null);
} else {
return null;
}
}
protected Object makeFilterResult(boolean isJoining, Object key, Object value, Object[] projection) {
try {
if (isCompatMode) {
key = ProtobufUtil.toWrappedByteArray(serCtx, key);
if (value != null) {
value = ProtobufUtil.toWrappedByteArray(serCtx, value);
}
}
Object result = new ContinuousQueryResult(isJoining, (byte[]) key, (byte[]) value, projection);
if (!isCompatMode) {
result = ProtobufUtil.toWrappedByteArray(serCtx, result);
}
return result;
} catch (IOException e) {
throw new CacheException(e);
}
}
@Override
public String toString() {
return "JPAContinuousQueryProtobufCacheEventFilterConverter{jpaQuery='" + jpaQuery + "'}";
}
public static final class Externalizer extends AbstractExternalizer<JPAContinuousQueryProtobufCacheEventFilterConverter> {
@Override
public void writeObject(ObjectOutput output, JPAContinuousQueryProtobufCacheEventFilterConverter filterAndConverter) throws IOException {
output.writeUTF(filterAndConverter.jpaQuery);
Map<String, Object> namedParameters = filterAndConverter.namedParameters;
if (namedParameters != null) {
UnsignedNumeric.writeUnsignedInt(output, namedParameters.size());
for (Map.Entry<String, Object> e : namedParameters.entrySet()) {
output.writeUTF(e.getKey());
output.writeObject(e.getValue());
}
} else {
UnsignedNumeric.writeUnsignedInt(output, 0);
}
output.writeObject(filterAndConverter.matcherImplClass);
}
@Override
public JPAContinuousQueryProtobufCacheEventFilterConverter readObject(ObjectInput input) throws IOException, ClassNotFoundException {
String jpaQuery = input.readUTF();
int paramsSize = UnsignedNumeric.readUnsignedInt(input);
Map<String, Object> namedParameters = null;
if (paramsSize != 0) {
namedParameters = new HashMap<>(paramsSize);
for (int i = 0; i < paramsSize; i++) {
String paramName = input.readUTF();
Object paramValue = input.readObject();
namedParameters.put(paramName, paramValue);
}
}
Class<? extends Matcher> matcherImplClass = (Class<? extends Matcher>) input.readObject();
return new JPAContinuousQueryProtobufCacheEventFilterConverter(jpaQuery, namedParameters, matcherImplClass);
}
@Override
public Integer getId() {
return ExternalizerIds.JPA_CONTINUOUS_QUERY_CACHE_EVENT_FILTER_CONVERTER;
}
@Override
public Set<Class<? extends JPAContinuousQueryProtobufCacheEventFilterConverter>> getTypeClasses() {
return Collections.singleton(JPAContinuousQueryProtobufCacheEventFilterConverter.class);
}
}
}