-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
QueryResult.java
291 lines (256 loc) · 9.6 KB
/
QueryResult.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* Copyright (c) 2008-2020, Hazelcast, Inc. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hazelcast.map.impl.query;
import com.hazelcast.map.QueryResultSizeExceededException;
import com.hazelcast.map.impl.MapDataSerializerHook;
import com.hazelcast.nio.ObjectDataInput;
import com.hazelcast.nio.ObjectDataOutput;
import com.hazelcast.nio.serialization.Data;
import com.hazelcast.projection.Projection;
import com.hazelcast.query.PagingPredicate;
import com.hazelcast.query.impl.QueryableEntry;
import com.hazelcast.spi.serialization.SerializationService;
import com.hazelcast.util.IterationType;
import com.hazelcast.util.SortingUtil;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
/**
* Represents a result of the query execution in the form of an iterable
* collection of {@link QueryResultRow rows}.
* <p>
* There are two modes of the result construction, the selection of the mode is
* controlled by the {@code orderAndLimitExpected} parameter:
* <ol>
* <li>When {@code orderAndLimitExpected} is {@code true}, this indicates that
* the call to the {@link #orderAndLimit} method is expected on behalf of the
* {@link PagingPredicate paging predicate} involved in the query. In this case,
* the intermediate result is represented as a collection of {@link
* QueryableEntry queryable entries} to allow the comparison of the items using
* the comparator of the paging predicate. After the call to {@link
* #completeConstruction}, all the queryable entries are converted to {@link
* QueryResultRow rows} and the result is ready to be provided to the client.
* <li>When {@code orderAndLimitExpected} is {@code false}, this indicates that
* no calls to the {@link #orderAndLimit} method are expected. In this case, the
* intermediate result is represented directly as a collection of {@link
* QueryResultRow rows} and no further conversion is performed.
* </ol>
*/
public class QueryResult implements Result<QueryResult>, Iterable<QueryResultRow> {
private List rows = new ArrayList();
private Collection<Integer> partitionIds;
private IterationType iterationType;
private final transient SerializationService serializationService;
private final transient long resultLimit;
private final transient boolean orderAndLimitExpected;
private final transient Projection projection;
private transient long resultSize;
/**
* Constructs an empty result for the purposes of deserialization.
*/
public QueryResult() {
serializationService = null;
orderAndLimitExpected = false;
resultLimit = Long.MAX_VALUE;
projection = null;
}
/**
* Constructs an empty result.
*
* @param iterationType the iteration type of the query for which
* this result is constructed for.
* @param projection the projection of the query for which this
* result is constructed for.
* @param serializationService the serialization service associated with
* the query for which this result is
* constructed for.
* @param resultLimit the upper limit on the number of items that
* can be {@link #add added} to this result.
* @param orderAndLimitExpected the flag to signal that the call to the
* {@link #orderAndLimit} method is expected,
* see the class javadoc for more details.
*/
public QueryResult(IterationType iterationType, Projection projection, SerializationService serializationService,
long resultLimit, boolean orderAndLimitExpected) {
this.iterationType = iterationType;
this.projection = projection;
this.serializationService = serializationService;
this.resultLimit = resultLimit;
this.orderAndLimitExpected = orderAndLimitExpected;
}
// for testing
IterationType getIterationType() {
return iterationType;
}
@Override
public Iterator<QueryResultRow> iterator() {
return rows.iterator();
}
/**
* @return the size of this result.
*/
public int size() {
return rows.size();
}
/**
* @return {@code true} if this result is empty, {@code false} otherwise.
**/
public boolean isEmpty() {
return rows.isEmpty();
}
/**
* Adds the given row into this result.
*
* @param row the row to add.
*/
public void addRow(QueryResultRow row) {
rows.add(row);
}
/**
* {@inheritDoc}
*
* @throws QueryResultSizeExceededException if the size of this result
* exceeds the result size limit.
*/
@Override
public void add(QueryableEntry entry) {
if (++resultSize > resultLimit) {
throw new QueryResultSizeExceededException();
}
rows.add(orderAndLimitExpected ? entry : convertEntryToRow(entry));
}
@Override
public QueryResult createSubResult() {
return new QueryResult(iterationType, projection, serializationService, resultLimit, orderAndLimitExpected);
}
@Override
public void orderAndLimit(PagingPredicate pagingPredicate, Map.Entry<Integer, Map.Entry> nearestAnchorEntry) {
rows = SortingUtil.getSortedSubList(rows, pagingPredicate, nearestAnchorEntry);
}
@Override
public void completeConstruction(Collection<Integer> partitionIds) {
setPartitionIds(partitionIds);
if (orderAndLimitExpected) {
for (ListIterator iterator = rows.listIterator(); iterator.hasNext(); ) {
iterator.set(convertEntryToRow((QueryableEntry) iterator.next()));
}
}
}
private Data getValueData(QueryableEntry entry) {
if (projection != null) {
return serializationService.toData(projection.transform(entry));
} else {
return entry.getValueData();
}
}
@Override
public Collection<Integer> getPartitionIds() {
return partitionIds;
}
@Override
public void combine(QueryResult result) {
Collection<Integer> otherPartitionIds = result.getPartitionIds();
if (otherPartitionIds == null) {
return;
}
if (partitionIds == null) {
partitionIds = new ArrayList<Integer>(otherPartitionIds.size());
}
partitionIds.addAll(otherPartitionIds);
rows.addAll(result.rows);
}
@Override
public void onCombineFinished() {
}
@Override
public void setPartitionIds(Collection<Integer> partitionIds) {
this.partitionIds = new ArrayList<Integer>(partitionIds);
}
/**
* @return the rows of this result.
*/
public List<QueryResultRow> getRows() {
return rows;
}
@Override
public int getFactoryId() {
return MapDataSerializerHook.F_ID;
}
@Override
public int getId() {
return MapDataSerializerHook.QUERY_RESULT;
}
@Override
public void writeData(ObjectDataOutput out) throws IOException {
int partitionSize = (partitionIds == null) ? 0 : partitionIds.size();
out.writeInt(partitionSize);
if (partitionSize > 0) {
for (Integer partitionId : partitionIds) {
out.writeInt(partitionId);
}
}
out.writeByte(iterationType.getId());
int resultSize = rows.size();
out.writeInt(resultSize);
if (resultSize > 0) {
for (QueryResultRow row : (List<QueryResultRow>) rows) {
row.writeData(out);
}
}
}
@Override
public void readData(ObjectDataInput in) throws IOException {
int partitionSize = in.readInt();
if (partitionSize > 0) {
partitionIds = new ArrayList<Integer>(partitionSize);
for (int i = 0; i < partitionSize; i++) {
partitionIds.add(in.readInt());
}
}
iterationType = IterationType.getById(in.readByte());
int resultSize = in.readInt();
if (resultSize > 0) {
for (int i = 0; i < resultSize; i++) {
QueryResultRow row = new QueryResultRow();
row.readData(in);
rows.add(row);
}
}
}
private QueryResultRow convertEntryToRow(QueryableEntry entry) {
Data key = null;
Data value = null;
switch (iterationType) {
case KEY:
key = entry.getKeyData();
break;
case VALUE:
value = getValueData(entry);
break;
case ENTRY:
key = entry.getKeyData();
value = entry.getValueData();
break;
default:
throw new IllegalStateException("Unknown iterationType:" + iterationType);
}
return new QueryResultRow(key, value);
}
}