-
Notifications
You must be signed in to change notification settings - Fork 612
/
ClusteredQueryCommand.java
183 lines (154 loc) · 6.09 KB
/
ClusteredQueryCommand.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package org.infinispan.query.clustered;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import org.infinispan.Cache;
import org.infinispan.commands.ReplicableCommand;
import org.infinispan.commands.remote.BaseRpcCommand;
import org.infinispan.commons.marshall.MarshallUtil;
import org.infinispan.query.QueryDefinition;
import org.infinispan.query.clustered.commandworkers.ClusteredQueryCommandWorker;
import org.infinispan.query.impl.CommandInitializer;
import org.infinispan.query.impl.CustomQueryCommand;
import org.infinispan.query.impl.ModuleCommandIds;
import org.infinispan.util.ByteString;
/**
* Encapsulates all rpc calls for distributed queries actions
*
* @author Israel Lacerra <israeldl@gmail.com>
* @since 5.1
*/
public class ClusteredQueryCommand extends BaseRpcCommand implements ReplicableCommand, CustomQueryCommand {
public static final byte COMMAND_ID = ModuleCommandIds.CLUSTERED_QUERY;
private static final Integer ZERO = Integer.valueOf(0);
private ClusteredQueryCommandType commandType;
private QueryDefinition queryDefinition;
// local instance (set only when command arrives on target node)
private Cache<?, ?> cache;
// identifies the query
private UUID lazyQueryId;
// for retrieve keys on a lazy query
private Integer docIndex = ZERO;
private ClusteredQueryCommand(ClusteredQueryCommandType type, String cacheName) {
super(ByteString.fromString(cacheName));
commandType = type;
}
/**
* For CommandFactory only. To create a ClusteredQueryCommand, use createLazyIterator(),
* destroyLazyQuery(), getResultSize() or retrieveKeyFromLazyQuery()
*/
public ClusteredQueryCommand(ByteString cacheName) {
super(cacheName);
}
@Override
public void fetchExecutionContext(CommandInitializer ci) {
this.cache = ci.getCacheManager().getCache(cacheName.toString());
}
public static ClusteredQueryCommand createLazyIterator(QueryDefinition queryDefinition, Cache<?, ?> cache, UUID id) {
ClusteredQueryCommand clQuery = new ClusteredQueryCommand(ClusteredQueryCommandType.CREATE_LAZY_ITERATOR, cache.getName());
clQuery.queryDefinition = queryDefinition;
clQuery.lazyQueryId = id;
return clQuery;
}
public static ClusteredQueryCommand getResultSize(QueryDefinition queryDefinition, Cache<?, ?> cache) {
ClusteredQueryCommand clQuery = new ClusteredQueryCommand(ClusteredQueryCommandType.GET_RESULT_SIZE, cache.getName());
clQuery.queryDefinition = queryDefinition;
return clQuery;
}
public static ClusteredQueryCommand createEagerIterator(QueryDefinition queryDefinition, Cache<?, ?> cache) {
ClusteredQueryCommand clQuery = new ClusteredQueryCommand(ClusteredQueryCommandType.CREATE_EAGER_ITERATOR, cache.getName());
clQuery.queryDefinition = queryDefinition;
return clQuery;
}
public static ClusteredQueryCommand destroyLazyQuery(Cache<?, ?> cache, UUID id) {
ClusteredQueryCommand clQuery = new ClusteredQueryCommand(ClusteredQueryCommandType.DESTROY_LAZY_ITERATOR, cache.getName());
clQuery.lazyQueryId = id;
return clQuery;
}
public static ClusteredQueryCommand retrieveKeyFromLazyQuery(Cache<?, ?> cache, UUID id, int docIndex) {
ClusteredQueryCommand clQuery = new ClusteredQueryCommand(ClusteredQueryCommandType.GET_SOME_KEYS, cache.getName());
clQuery.lazyQueryId = id;
clQuery.docIndex = docIndex;
return clQuery;
}
public void initialize(Cache<?, ?> localInstance) {
setCache(localInstance);
}
public void setCache(Cache<?, ?> cache) {
this.cache = cache;
}
/**
* Invokes a query on a (remote) cache and returns results (list of keys).
*
* @return returns a <code>CompletableFuture</code> with a <code>List<Object></code>.
*/
@Override
public CompletableFuture<Object> invokeAsync() throws Throwable {
return CompletableFuture.completedFuture(perform(cache));
}
public QueryResponse perform(Cache<?, ?> cache) {
ClusteredQueryCommandWorker worker = commandType.getCommand(cache, queryDefinition, lazyQueryId, docIndex);
return worker.perform();
}
@Override
public byte getCommandId() {
return COMMAND_ID;
}
@Override
public void writeTo(ObjectOutput output) throws IOException {
MarshallUtil.marshallEnum(commandType, output);
output.writeObject(queryDefinition);
MarshallUtil.marshallUUID(lazyQueryId, output, true);
output.writeInt(docIndex);
}
@Override
public void readFrom(ObjectInput input) throws IOException, ClassNotFoundException {
commandType = MarshallUtil.unmarshallEnum(input, ClusteredQueryCommandType::valueOf);
queryDefinition = (QueryDefinition) input.readObject();
lazyQueryId = MarshallUtil.unmarshallUUID(input, true);
docIndex = input.readInt();
}
@Override
public String toString() {
return "ClusteredQuery{ cache=" + getCacheName() + '}';
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((cacheName == null) ? 0 : cacheName.hashCode());
result = prime * result + ((queryDefinition == null) ? 0 : queryDefinition.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (!(obj instanceof ClusteredQueryCommand))
return false;
ClusteredQueryCommand other = (ClusteredQueryCommand) obj;
if (cacheName == null) {
if (other.cacheName != null)
return false;
} else if (!cacheName.equals(other.cacheName))
return false;
if (queryDefinition == null) {
if (other.queryDefinition != null)
return false;
} else if (!queryDefinition.equals(other.queryDefinition))
return false;
return true;
}
@Override
public boolean isReturnValueExpected() {
return true;
}
@Override
public boolean canBlock() {
return true;
}
}