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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
|
/*
* Copyright 2006 Amazon Technologies, Inc. or its affiliates.
* Amazon, Amazon.com and Carbonado are trademarks or registered trademarks
* of Amazon Technologies, Inc. or its affiliates. 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.amazon.carbonado.qe;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.amazon.carbonado.FetchException;
import com.amazon.carbonado.RepositoryException;
import com.amazon.carbonado.Storable;
import com.amazon.carbonado.Storage;
import com.amazon.carbonado.SupportException;
import com.amazon.carbonado.filter.Filter;
import com.amazon.carbonado.filter.PropertyFilter;
import com.amazon.carbonado.filter.RelOp;
import com.amazon.carbonado.info.ChainedProperty;
import com.amazon.carbonado.info.OrderedProperty;
import com.amazon.carbonado.info.StorableIndex;
import com.amazon.carbonado.info.StorableProperty;
/**
* Analyzes a simple query specification and determines which index is best
* suited for its execution. Query filters passed to this analyzer cannot
* contain any 'or' operations.
*
* <p>IndexedQueryAnalyzer is sharable and thread-safe. An instance for a
* particular Storable type can be cached, avoiding repeated construction
* cost. In addition, the analyzer caches learned foreign indexes.
*
* @author Brian S O'Neill
* @see UnionQueryAnalyzer
*/
public class IndexedQueryAnalyzer<S extends Storable> {
final Class<S> mType;
final RepositoryAccess mRepoAccess;
// Growable cache which maps join properties to lists of usable foreign indexes.
private Map<ChainedProperty<S>, ForeignIndexes<S>> mForeignIndexCache;
/**
* @param type type of storable being queried
* @param access repository access for examing available indexes
* @throws IllegalArgumentException if type or indexProvider is null
*/
public IndexedQueryAnalyzer(Class<S> type, RepositoryAccess access) {
if (type == null || access == null) {
throw new IllegalArgumentException();
}
mType = type;
mRepoAccess = access;
}
public Class<S> getStorableType() {
return mType;
}
/**
* @param filter optional filter which which must be {@link Filter#isBound
* bound} and cannot contain any logical 'or' operations.
* @param ordering optional properties which define desired ordering
* @param hints optional query hints
* @throws IllegalArgumentException if filter is not supported
*/
public Result analyze(Filter<S> filter, OrderingList<S> ordering, QueryHints hints)
throws SupportException, RepositoryException
{
if (filter != null && !filter.isBound()) {
throw new IllegalArgumentException("Filter must be bound");
}
// First find best local index.
CompositeScore<S> bestLocalScore = null;
StorableIndex<S> bestLocalIndex = null;
final Comparator<CompositeScore<?>> fullComparator = CompositeScore.fullComparator(hints);
Collection<StorableIndex<S>> localIndexes = indexesFor(getStorableType());
if (localIndexes != null) {
for (StorableIndex<S> index : localIndexes) {
CompositeScore<S> candidateScore =
CompositeScore.evaluate(index, filter, ordering);
if (bestLocalScore == null
|| fullComparator.compare(candidateScore, bestLocalScore) < 0)
{
bestLocalScore = candidateScore;
bestLocalIndex = index;
}
}
}
// Now try to find best foreign index.
if (bestLocalScore != null && bestLocalScore.getFilteringScore().isKeyMatch()) {
// Don't bother checking foreign indexes. The local one is perfect.
return new Result(filter, bestLocalScore, bestLocalIndex, null, null, hints);
}
CompositeScore<?> bestForeignScore = null;
StorableIndex<?> bestForeignIndex = null;
ChainedProperty<S> bestForeignProperty = null;
for (PropertyFilter<S> propFilter : PropertyFilterList.get(filter)) {
ChainedProperty<S> chainedProp = propFilter.getChainedProperty();
if (chainedProp.getChainCount() == 0) {
// Cannot possibly be a join, so move on.
continue;
}
ForeignIndexes<S> foreignIndexes = getForeignIndexes(chainedProp);
if (foreignIndexes == null) {
continue;
}
for (StorableIndex<?> index : foreignIndexes.mIndexes) {
CompositeScore<S> candidateScore = CompositeScore.evaluate
(foreignIndexes.getVirtualIndex(index),
index.isUnique(),
index.isClustered(),
filter,
ordering);
if (bestForeignScore == null
|| fullComparator.compare(candidateScore, bestForeignScore) < 0)
{
bestForeignScore = candidateScore;
bestForeignIndex = index;
bestForeignProperty = foreignIndexes.mProperty;
}
}
}
// Check if foreign index is better than local index.
if (bestLocalScore != null && bestForeignScore != null) {
// When comparing local index to foreign index, use a slightly less
// discriminating comparator, to prevent foreign indexes from
// looking too good.
Comparator<CompositeScore<?>> comp = CompositeScore.localForeignComparator();
if (comp.compare(bestForeignScore, bestLocalScore) < 0) {
// Foreign is better.
bestLocalScore = null;
} else {
// Local is better.
bestForeignScore = null;
}
}
CompositeScore bestScore;
if (bestLocalScore != null) {
bestScore = bestLocalScore;
bestForeignIndex = null;
bestForeignProperty = null;
} else {
bestScore = bestForeignScore;
bestLocalIndex = null;
}
return new Result
(filter, bestScore, bestLocalIndex, bestForeignIndex, bestForeignProperty, hints);
}
/**
* @return null if no foreign indexes for property
*/
private synchronized ForeignIndexes<S> getForeignIndexes(ChainedProperty<S> chainedProp)
throws SupportException, RepositoryException
{
// Remove the last property as it is expected to be a simple storable
// property instead of a joined Storable.
chainedProp = chainedProp.trim();
ForeignIndexes<S> foreignIndexes = null;
if (mForeignIndexCache != null) {
foreignIndexes = mForeignIndexCache.get(chainedProp);
if (foreignIndexes != null || mForeignIndexCache.containsKey(chainedProp)) {
return foreignIndexes;
}
}
// Check if property chain is properly joined and indexed along the way.
evaluate: {
if (!isProperJoin(chainedProp.getPrimeProperty())) {
break evaluate;
}
if (chainedProp.isOuterJoin(0)) {
// Outer joins cannot be optimized via foreign indexes.
break evaluate;
}
int count = chainedProp.getChainCount();
for (int i=0; i<count; i++) {
if (!isProperJoin(chainedProp.getChainedProperty(i))) {
break evaluate;
}
if (chainedProp.isOuterJoin(i + 1)) {
// Outer joins cannot be optimized via foreign indexes.
break evaluate;
}
}
// All foreign indexes are available for use.
Class foreignType = chainedProp.getLastProperty().getType();
Collection<StorableIndex<?>> indexes = indexesFor(foreignType);
foreignIndexes = new ForeignIndexes<S>(chainedProp, indexes);
}
if (mForeignIndexCache == null) {
mForeignIndexCache = Collections.synchronizedMap
(new HashMap<ChainedProperty<S>, ForeignIndexes<S>>());
}
mForeignIndexCache.put(chainedProp, foreignIndexes);
return foreignIndexes;
}
/**
* Checks if the property is a join and its internal properties are fully
* indexed.
*/
private boolean isProperJoin(StorableProperty<?> property)
throws SupportException, RepositoryException
{
if (!property.isJoin() || property.isQuery()) {
return false;
}
// Make up a filter over the join's internal properties and then search
// for an index that filters with no remainder.
Filter<?> filter = Filter.getOpenFilter(property.getEnclosingType());
int count = property.getJoinElementCount();
for (int i=0; i<count; i++) {
filter = filter.and(property.getInternalJoinElement(i).getName(), RelOp.EQ);
}
// Java generics are letting me down. I cannot use proper specification
// because compiler gets confused with all the wildcards.
Collection indexes = indexesFor(filter.getStorableType());
if (indexes != null) {
for (Object index : indexes) {
FilteringScore score = FilteringScore.evaluate((StorableIndex) index, filter);
if (score.getRemainderCount() == 0) {
return true;
}
}
}
return false;
}
private <T extends Storable> Collection<StorableIndex<T>> indexesFor(Class<T> type)
throws SupportException, RepositoryException
{
return mRepoAccess.storageAccessFor(type).getAllIndexes();
}
public class Result {
private final Filter<S> mFilter;
private final CompositeScore<S> mScore;
private final StorableIndex<S> mLocalIndex;
private final StorableIndex<?> mForeignIndex;
private final ChainedProperty<S> mForeignProperty;
private final QueryHints mHints;
Result(Filter<S> filter,
CompositeScore<S> score,
StorableIndex<S> localIndex,
StorableIndex<?> foreignIndex,
ChainedProperty<S> foreignProperty,
QueryHints hints)
{
mFilter = filter;
mScore = score;
mLocalIndex = localIndex;
mForeignIndex = foreignIndex;
mForeignProperty = foreignProperty;
mHints = hints;
}
/**
* Returns true if the selected index does anything at all to filter
* results or to order them. If not, a filtered and sorted full scan
* makes more sense.
*/
public boolean handlesAnything() {
return mScore.getFilteringScore().hasAnyMatches() == true
|| mScore.getOrderingScore().getHandledCount() > 0;
}
/**
* Returns combined handled and remainder filter for this result.
*/
public Filter<S> getFilter() {
return mFilter;
}
/**
* Returns combined handled and remainder orderings for this result.
*/
public OrderingList<S> getOrdering() {
return mScore.getOrderingScore().getHandledOrdering().concat(getRemainderOrdering());
}
/**
* Returns the score on how well the selected index performs the
* desired filtering and ordering.
*/
public CompositeScore<S> getCompositeScore() {
return mScore;
}
/**
* Remainder filter which overrides that in composite score.
*/
public Filter<S> getRemainderFilter() {
return mScore.getFilteringScore().getRemainderFilter();
}
/**
* Remainder orderings which override that in composite score.
*/
public OrderingList<S> getRemainderOrdering() {
return mScore.getOrderingScore().getRemainderOrdering();
}
/**
* Returns the local index that was selected, or null if a foreign
* index was selected.
*/
public StorableIndex<S> getLocalIndex() {
return mLocalIndex;
}
/**
* Returns the foreign index that was selected, or null if a local
* index was selected. If a foreign index has been selected, then a
* {@link JoinedQueryExecutor} is needed.
*/
public StorableIndex<?> getForeignIndex() {
return mForeignIndex;
}
/**
* Returns the simple or chained property that maps to the selected
* foreign index. Returns null if foreign index was not selected. This
* property corresponds to the "targetToSourceProperty" of {@link
* JoinedQueryExecutor}.
*/
public ChainedProperty<S> getForeignProperty() {
return mForeignProperty;
}
/**
* Returns true if local or foreign index is clustered. Scans of
* clustered indexes are generally faster.
*/
public boolean isIndexClustered() {
return (mLocalIndex != null && mLocalIndex.isClustered())
|| (mForeignIndex != null && mForeignIndex.isClustered());
}
/**
* Returns true if the given result uses the same index as this, and in
* the same way. The only allowed differences are in the remainder
* filter and orderings.
*/
public boolean canMergeRemainder(Result other) {
if (this == other || (!handlesAnything() && !other.handlesAnything())) {
return true;
}
if (equals(getLocalIndex(), other.getLocalIndex())
&& equals(getForeignIndex(), other.getForeignIndex())
&& equals(getForeignProperty(), other.getForeignProperty()))
{
return getCompositeScore().canMergeRemainder(other.getCompositeScore());
}
return false;
}
/**
* Merges the remainder filter and orderings of this result with the
* one given, returning a new result. Call canMergeRemainder first to
* verify if the merge makes any sense.
*/
public Result mergeRemainder(Result other) {
if (this == other) {
return this;
}
// Assuming canMergeRemainder returned true, each handled filter
// and the combined filter are all identical. This is just a safeguard.
Filter<S> handledFilter =
orFilters(getCompositeScore().getFilteringScore().getHandledFilter(),
other.getCompositeScore().getFilteringScore().getHandledFilter());
Filter<S> remainderFilter =
orFilters(getRemainderFilter(), other.getRemainderFilter());
OrderingList<S> remainderOrdering =
getRemainderOrdering().concat(other.getRemainderOrdering()).reduce();
Filter<S> filter = andFilters(handledFilter, remainderFilter);
CompositeScore<S> score = mScore
.withRemainderFilter(remainderFilter)
.withRemainderOrdering(remainderOrdering);
return new Result(filter, score, mLocalIndex, mForeignIndex, mForeignProperty, mHints);
}
/**
* Merges the remainder filter of this result with the given filter,
* returning a new result. If handlesAnything return true, then it
* doesn't usually make sense to call this method.
*/
public Result mergeRemainderFilter(Filter<S> filter) {
return withRemainderFilter(orFilters(getRemainderFilter(), filter));
}
private Filter<S> andFilters(Filter<S> a, Filter<S> b) {
if (a == null) {
return b;
}
if (b == null) {
return a;
}
return a.and(b).reduce();
}
private Filter<S> orFilters(Filter<S> a, Filter<S> b) {
if (a == null) {
return b;
}
if (b == null) {
return a;
}
return a.or(b).reduce();
}
/**
* Returns a new result with the remainder filter replaced.
*/
public Result withRemainderFilter(Filter<S> remainderFilter) {
CompositeScore<S> score = mScore.withRemainderFilter(remainderFilter);
return new Result(mFilter, score, mLocalIndex,
mForeignIndex, mForeignProperty, mHints);
}
/**
* Returns a new result with the remainder ordering replaced.
*/
public Result withRemainderOrdering(OrderingList<S> remainderOrdering) {
CompositeScore<S> score = mScore.withRemainderOrdering(remainderOrdering);
return new Result(mFilter, score, mLocalIndex,
mForeignIndex, mForeignProperty, mHints);
}
/**
* Creates a QueryExecutor based on this result.
*/
public QueryExecutor<S> createExecutor()
throws SupportException, FetchException, RepositoryException
{
StorableIndex<S> localIndex = getLocalIndex();
StorageAccess<S> localAccess = mRepoAccess.storageAccessFor(getStorableType());
if (localIndex != null) {
Storage<S> delegate = localAccess.storageDelegate(localIndex);
if (delegate != null) {
return new DelegatedQueryExecutor<S>(delegate, getFilter(), getOrdering());
}
}
Filter<S> remainderFilter = getRemainderFilter();
QueryExecutor<S> executor;
if (!handlesAnything()) {
executor = new FullScanQueryExecutor<S>(localAccess);
} else if (localIndex == null) {
// Use foreign executor.
return JoinedQueryExecutor.build
(mRepoAccess, getForeignProperty(), getFilter(), getOrdering(), mHints);
} else {
CompositeScore<S> score = getCompositeScore();
FilteringScore<S> fScore = score.getFilteringScore();
if (fScore.isKeyMatch()) {
executor = new KeyQueryExecutor<S>(localAccess, localIndex, fScore);
} else {
IndexedQueryExecutor ixExecutor =
new IndexedQueryExecutor<S>(localAccess, localIndex, score);
executor = ixExecutor;
if (ixExecutor.getCoveringFilter() != null) {
remainderFilter = fScore.getCoveringRemainderFilter();
}
}
}
if (remainderFilter != null) {
executor = new FilteredQueryExecutor<S>(executor, remainderFilter);
}
OrderingList<S> remainderOrdering = getRemainderOrdering();
if (remainderOrdering.size() > 0) {
executor = new SortedQueryExecutor<S>
(localAccess,
executor,
getCompositeScore().getOrderingScore().getHandledOrdering(),
remainderOrdering);
}
return executor;
}
@Override
public String toString() {
return "IndexedQueryAnalyzer.Result {score="
+ getCompositeScore() + ", localIndex="
+ getLocalIndex() + ", foreignIndex="
+ getForeignIndex() + ", foreignProperty="
+ getForeignProperty() + ", remainderFilter="
+ getRemainderFilter() + ", remainderOrdering="
+ getRemainderOrdering() + '}';
}
private boolean equals(Object a, Object b) {
return a == null ? (b == null) : (a.equals(b));
}
}
private static class ForeignIndexes<S extends Storable> {
final ChainedProperty<S> mProperty;
final List<StorableIndex<?>> mIndexes;
// Cache of virtual indexes.
private final Map<StorableIndex<?>, OrderedProperty<S>[]> mVirtualIndexMap;
/**
* @param property type of property must be a joined Storable
*/
ForeignIndexes(ChainedProperty<S> property, Collection<StorableIndex<?>> indexes) {
mProperty = property;
if (indexes == null || indexes.size() == 0) {
mIndexes = Collections.emptyList();
} else {
mIndexes = new ArrayList<StorableIndex<?>>(indexes);
}
mVirtualIndexMap = new HashMap<StorableIndex<?>, OrderedProperty<S>[]>();
}
/**
* Prepends local chained property with names of index elements,
* producing a virtual index on local storable. This allows
* CompositeScore to evaluate it.
*/
synchronized OrderedProperty<S>[] getVirtualIndex(StorableIndex<?> index) {
OrderedProperty<S>[] virtualProps = mVirtualIndexMap.get(index);
if (virtualProps != null) {
return virtualProps;
}
OrderedProperty<?>[] realProps = index.getOrderedProperties();
virtualProps = new OrderedProperty[realProps.length];
for (int i=realProps.length; --i>=0; ) {
OrderedProperty<?> realProp = realProps[i];
ChainedProperty<S> virtualChained =
mProperty.append(realProp.getChainedProperty());
virtualProps[i] = OrderedProperty.get(virtualChained, realProp.getDirection());
}
mVirtualIndexMap.put(index, virtualProps);
return virtualProps;
}
}
}
|