SearchResultImpl.java
8.86 KB
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
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.RepositoryException
* javax.jcr.query.Row
* javax.jcr.query.RowIterator
* org.apache.sling.api.resource.Resource
* org.slf4j.Logger
*/
package com.day.cq.search.impl.result;
import com.day.cq.search.facets.Facet;
import com.day.cq.search.impl.builder.QueryImpl;
import com.day.cq.search.impl.result.HitImpl;
import com.day.cq.search.impl.result.ResultPageImpl;
import com.day.cq.search.result.Hit;
import com.day.cq.search.result.ResultPage;
import com.day.cq.search.result.SearchResult;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.query.Row;
import javax.jcr.query.RowIterator;
import org.apache.sling.api.resource.Resource;
import org.slf4j.Logger;
public class SearchResultImpl
implements SearchResult {
private final Logger log;
private final QueryImpl query;
private final long executionTime;
private boolean guessedTotal;
private long totalMatches;
private final boolean hasMore;
private final long startIndex;
private final long hitsPerPage;
private final List<Hit> hits;
private List<ResultPage> resultPages;
private Map<String, Facet> facets;
public SearchResultImpl(QueryImpl query, RowIterator rows, long startIndex, long hitsPerPage, long startTime, QueryImpl.Optional<QueryImpl.FilteringRowIterator> filteringIterator, Logger log) {
this.log = log;
this.query = query;
this.hitsPerPage = hitsPerPage;
try {
rows.skip(startIndex);
}
catch (NoSuchElementException e) {
// empty catch block
}
this.startIndex = rows.getPosition();
ArrayList<HitImpl> hits = new ArrayList<HitImpl>();
for (long i = 0; (hitsPerPage <= 0 || i < hitsPerPage) && rows.hasNext(); ++i) {
hits.add(new HitImpl(query, rows.nextRow(), this.startIndex + i));
}
this.hits = Collections.unmodifiableList(hits);
this.guessedTotal = false;
long size = rows.getSize();
if (size >= 0) {
this.totalMatches = size;
this.hasMore = false;
} else {
this.guessedTotal = true;
this.totalMatches = rows.getPosition();
long maxTotal = query.getMaxTotal();
if (maxTotal >= 0) {
while (this.totalMatches < maxTotal && rows.hasNext()) {
++this.totalMatches;
rows.nextRow();
}
}
this.hasMore = rows.hasNext();
log.debug(">> total is at least {} or more", (Object)this.totalMatches);
}
QueryImpl.FilteringRowIterator filterIter = filteringIterator.get();
if (filterIter != null) {
log.debug("filtering overhead was {} ms", (Object)filterIter.getFilteringTime());
}
this.executionTime = System.currentTimeMillis() - startTime;
log.debug("entire query execution took {} ms", (Object)this.executionTime);
}
@Override
public long getTotalMatches() {
return this.totalMatches;
}
@Override
public boolean hasMore() {
return this.hasMore;
}
@Override
public long getStartIndex() {
return this.startIndex;
}
@Override
public long getHitsPerPage() {
return this.hitsPerPage;
}
@Override
public List<Hit> getHits() {
return this.hits;
}
@Override
public List<ResultPage> getResultPages() {
if (this.resultPages == null) {
ArrayList<ResultPageImpl> resultPages = new ArrayList<ResultPageImpl>();
if (this.hitsPerPage <= 0) {
if (this.startIndex == 0) {
resultPages.add(new ResultPageImpl(0, 0, true));
} else {
resultPages.add(new ResultPageImpl(0, 0, false));
resultPages.add(new ResultPageImpl(1, this.startIndex, true));
}
} else if (this.startIndex % this.hitsPerPage != 0) {
int i = 0;
if (this.startIndex > 0) {
resultPages.add(new ResultPageImpl(i++, 0, false));
}
resultPages.add(new ResultPageImpl(i++, this.startIndex, true));
if (this.guessedTotal || this.totalMatches > this.startIndex + this.hitsPerPage) {
resultPages.add(new ResultPageImpl(i++, this.startIndex + this.hitsPerPage, false));
}
} else {
long maxPage = this.totalMatches / this.hitsPerPage;
if (this.totalMatches % this.hitsPerPage > 0) {
++maxPage;
}
if (this.guessedTotal) {
++maxPage;
}
long currentPageIndex = this.startIndex / this.hitsPerPage;
maxPage = Math.min(maxPage, currentPageIndex + 10);
for (long i = minPage = Math.max((long)0, (long)(currentPageIndex - 10)); i < maxPage; ++i) {
resultPages.add(new ResultPageImpl(i, i * this.hitsPerPage, i == currentPageIndex));
}
}
this.resultPages = Collections.unmodifiableList(resultPages);
}
return this.resultPages;
}
@Override
public ResultPage getPreviousPage() {
ResultPage previous = null;
for (ResultPage p : this.getResultPages()) {
if (p.isCurrentPage()) break;
previous = p;
}
return previous;
}
@Override
public ResultPage getNextPage() {
Iterator<ResultPage> it = this.getResultPages().iterator();
while (it.hasNext()) {
ResultPage p = it.next();
if (!p.isCurrentPage() || !it.hasNext()) continue;
return it.next();
}
return null;
}
public static String formatExecutionTime(long time) {
double seconds = (double)time / 1000.0;
NumberFormat format = NumberFormat.getNumberInstance();
format.setMinimumFractionDigits(2);
format.setMaximumFractionDigits(2);
return format.format(seconds);
}
@Override
public String getExecutionTime() {
return SearchResultImpl.formatExecutionTime(this.executionTime);
}
@Override
public long getExecutionTimeMillis() {
return this.executionTime;
}
@Override
public Map<String, Facet> getFacets() throws RepositoryException {
if (this.facets == null) {
this.facets = this.query.extractFacets();
}
return this.facets;
}
@Override
public String getQueryStatement() {
return this.query.getStatement();
}
@Override
public String getFilteringPredicates() {
return this.query.getFilteringPredicates();
}
@Override
public Iterator<Node> getNodes() {
return new HitBasedNodeIterator(this.hits.iterator());
}
@Override
public Iterator<Resource> getResources() {
return new HitBasedResourceIterator(this.hits.iterator());
}
private class HitBasedResourceIterator
implements Iterator<Resource> {
private Iterator<Hit> hits;
public HitBasedResourceIterator(Iterator<Hit> hits) {
this.hits = hits;
}
@Override
public boolean hasNext() {
return this.hits.hasNext();
}
@Override
public Resource next() {
try {
return this.hits.next().getResource();
}
catch (RepositoryException e) {
SearchResultImpl.this.log.error("Could not get resource behind search result hit", (Throwable)e);
return null;
}
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove not allowed for resource iterator on search result");
}
}
private class HitBasedNodeIterator
implements Iterator<Node> {
private Iterator<Hit> hits;
public HitBasedNodeIterator(Iterator<Hit> hits) {
this.hits = hits;
}
@Override
public boolean hasNext() {
return this.hits.hasNext();
}
@Override
public Node next() {
try {
return this.hits.next().getNode();
}
catch (RepositoryException e) {
SearchResultImpl.this.log.error("Could not get node behind search result hit", (Throwable)e);
return null;
}
}
@Override
public void remove() {
throw new UnsupportedOperationException("remove not allowed for node iterator on search result");
}
}
}