/
QueryList.java
321 lines (276 loc) · 10.3 KB
/
QueryList.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You 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.baidu.hugegraph.backend.page;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import com.baidu.hugegraph.HugeGraph;
import com.baidu.hugegraph.backend.id.Id;
import com.baidu.hugegraph.backend.query.IdQuery;
import com.baidu.hugegraph.backend.query.Query;
import com.baidu.hugegraph.backend.query.QueryResults;
import com.baidu.hugegraph.backend.store.BackendEntry;
import com.baidu.hugegraph.config.CoreOptions;
import com.baidu.hugegraph.util.Bytes;
import com.baidu.hugegraph.util.CollectionUtil;
import com.baidu.hugegraph.util.E;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
public final class QueryList {
private final HugeGraph graph;
private final Query parent;
// The size of each page fetched by the inner page
private final Function<Query, QueryResults> fetcher;
private final List<QueryHolder> queries;
public QueryList(HugeGraph graph, Query parent,
Function<Query, QueryResults> fetcher) {
this.graph = graph;
this.parent = parent;
this.fetcher = fetcher;
this.queries = new ArrayList<>();
}
protected Query parent() {
return this.parent;
}
protected Function<Query, QueryResults> fetcher() {
return this.fetcher;
}
public void add(IdHolderList holders) {
// IdHolderList is results of one index query, the query is flattened
if (!this.parent.paging()) {
for (QueryHolder q : this.queries) {
if (q instanceof IndexQuery) {
((IndexQuery) q).merge(holders);
return;
}
}
}
this.queries.add(new IndexQuery(holders));
}
public void add(Query query) {
// TODO: maybe need do deduplicate(for -> flatten)
this.queries.add(new OptimizedQuery(query));
}
public int total() {
int total = 0;
for (QueryHolder q : this.queries) {
total += q.total();
}
return total;
}
public boolean empty() {
return this.queries.isEmpty();
}
public QueryResults fetch() {
assert !this.queries.isEmpty();
if (this.parent.paging()) {
int pageSize = this.graph.configuration()
.get(CoreOptions.QUERY_PAGE_SIZE);
PageEntryIterator iterator = new PageEntryIterator(this, pageSize);
/*
* NOTE: PageEntryIterator query will change every fetch time.
* TODO: sort results by input ids in each page.
*/
return iterator.results();
}
// Fetch all results once
return QueryResults.flatMap(this.queries.iterator(), q -> q.iterator());
}
protected PageIterator fetchNext(PageInfo pageInfo, long pageSize) {
QueryHolder query = null;
int offset = pageInfo.offset();
int current = 0;
for (QueryHolder q : this.queries) {
if (current + q.total() > offset) {
query = q;
break;
}
current += q.total();
}
E.checkNotNull(query, "query");
assert offset >= current;
return query.iterator(offset - current, pageInfo.page(), pageSize);
}
private static Set<Id> limit(Set<Id> ids, Query query) {
long fromIndex = query.offset();
E.checkArgument(fromIndex <= Integer.MAX_VALUE,
"Offset must be <= 0x7fffffff, but got '%s'",
fromIndex);
if (query.offset() >= ids.size()) {
return ImmutableSet.of();
}
if (query.nolimit() && query.offset() == 0) {
return ids;
}
long toIndex = query.total();
if (query.nolimit() || toIndex > ids.size()) {
toIndex = ids.size();
}
assert fromIndex < ids.size();
assert toIndex <= ids.size();
return CollectionUtil.subSet(ids, (int) fromIndex, (int) toIndex);
}
/**
* A container that can generate queries
*/
private interface QueryHolder {
/**
* For non-paging situation
* @return BackendEntry iterator
*/
public QueryResults iterator();
/**
* For paging situation
* @param index position IdHolder(Query)
* @param page set query page
* @param pageSize set query page size
* @return BackendEntry iterator with page
*/
public PageIterator iterator(int index, String page, long pageSize);
public int total();
}
/**
* Generate queries from tx.optimizeQuery()
*/
private class OptimizedQuery implements QueryHolder {
private final Query query;
public OptimizedQuery(Query query) {
this.query = query;
}
@Override
public QueryResults iterator() {
return fetcher().apply(this.query);
}
@Override
public PageIterator iterator(int index, String page, long pageSize) {
assert index == 0;
Query query = this.query.copy();
query.page(page);
// Not set limit to pageSize due to PageEntryIterator.remaining
if (this.query.nolimit()) {
query.limit(pageSize);
}
QueryResults rs = fetcher().apply(query);
// Must iterate all entries before get the next page
return new PageIterator(rs.list().iterator(), rs.queries(),
PageInfo.pageState(rs.iterator()));
}
@Override
public int total() {
return 1;
}
}
/**
* Generate queries from tx.indexQuery()
*/
private class IndexQuery implements QueryHolder {
// An IdHolder each sub-query
private final IdHolderList holders;
// To skip the offset in parent query
private long offsetToSkip;
public IndexQuery(IdHolderList holders) {
this.holders = holders;
this.offsetToSkip = this.holders.needSkipOffset() ?
parent().offset() : -1L;
}
public void merge(IdHolderList holders) {
E.checkState(holders.sameParameters(this.holders),
"Can't merge IdHolderList with different parameters");
this.holders.addAll(holders);
}
@Override
public QueryResults iterator() {
if (this.holders.size() == 1) {
return this.each(this.holders.get(0));
}
return QueryResults.flatMap(this.holders.iterator(), this::each);
}
private QueryResults each(IdHolder holder) {
Set<Id> ids = holder.ids();
Query parent = parent();
if (this.offsetToSkip > 0L) {
// Skip offset when needed
this.offsetToSkip -= ids.size();
ids = limit(ids, parent);
} else if (!parent.nolimit() && ids.size() > parent.total()) {
/*
* Avoid too many ids in one time query,
* Assume it will get one result by each id
*/
ids = CollectionUtil.subSet(ids, 0, (int) parent.total());
}
if (ids.isEmpty()) {
return null;
}
IdQuery query = new IdQuery(parent, ids);
return fetcher().apply(query);
}
@Override
public PageIterator iterator(int index, String page, long pageSize) {
IdHolder holder = this.holders.get(index);
PageIds pageIds = holder.fetchNext(page, pageSize);
if (pageIds.empty()) {
return PageIterator.EMPTY;
}
IdQuery query = new IdQuery(parent(), pageIds.ids());
QueryResults rs = fetcher().apply(query);
return new PageIterator(rs.iterator(), rs.queries(),
pageIds.pageState());
}
@Override
public int total() {
return this.holders.size();
}
}
public static class PageIterator {
public static final PageIterator EMPTY = new PageIterator(
QueryResults.emptyIterator(),
ImmutableList.of(Query.NONE),
PageState.EMPTY);
private final Iterator<BackendEntry> iterator;
private final List<Query> queries;
private final PageState pageState;
public PageIterator(Iterator<BackendEntry> iterator,
List<Query> queries, PageState pageState) {
this.iterator = iterator;
this.queries = queries;
this.pageState = pageState;
}
public Iterator<BackendEntry> get() {
return this.iterator;
}
public boolean hasNextPage() {
return !Bytes.equals(this.pageState.position(),
PageState.EMPTY_BYTES);
}
public Query query() {
E.checkState(this.queries.size() == 1,
"Expect queries size 1, but got: %s", this.queries);
return this.queries.get(0);
}
public String page() {
return this.pageState.toString();
}
public long total() {
return this.pageState.total();
}
}
}