use of org.locationtech.geowave.core.store.api.IndexFieldMapper in project geowave by locationtech.
the class OptimalExpressionQuery method determineBestIndices.
@SuppressWarnings({ "rawtypes", "unchecked" })
public List<Pair<Index, List<InternalDataAdapter<?>>>> determineBestIndices(final BaseQueryOptions baseOptions, final InternalDataAdapter<?>[] adapters, final AdapterIndexMappingStore adapterIndexMappingStore, final IndexStore indexStore, final DataStatisticsStore statisticsStore) {
final Map<Index, List<InternalDataAdapter<?>>> bestIndices = Maps.newHashMap();
final Set<String> referencedFields = Sets.newHashSet();
filter.addReferencedFields(referencedFields);
for (final InternalDataAdapter<?> adapter : adapters) {
if (!adapterMatchesFilter(adapter, referencedFields)) {
continue;
}
final AdapterToIndexMapping[] adapterIndices = adapterIndexMappingStore.getIndicesForAdapter(adapter.getAdapterId());
final Map<Index, FilterConstraints<?>> indexConstraints = Maps.newHashMap();
Index bestIndex = null;
for (final AdapterToIndexMapping mapping : adapterIndices) {
if ((baseOptions.getIndexName() != null) && !baseOptions.getIndexName().equals(mapping.getIndexName())) {
continue;
}
final Index index = mapping.getIndex(indexStore);
if (indexFilter != null && !indexFilter.test(index)) {
continue;
}
if ((bestIndex == null) || ((bestIndex instanceof AttributeIndex) && !(index instanceof AttributeIndex))) {
bestIndex = index;
}
final Set<String> indexedFields = Sets.newHashSet();
final Class<? extends Comparable> filterClass;
if ((index instanceof CustomIndex) && (((CustomIndex<?, ?>) index).getCustomIndexStrategy() instanceof TextIndexStrategy)) {
final TextIndexStrategy<?> indexStrategy = (TextIndexStrategy<?>) ((CustomIndex<?, ?>) index).getCustomIndexStrategy();
if (!(indexStrategy.getEntryConverter() instanceof AdapterFieldTextIndexEntryConverter)) {
continue;
}
indexedFields.add(((AdapterFieldTextIndexEntryConverter<?>) indexStrategy.getEntryConverter()).getFieldName());
filterClass = String.class;
} else {
for (final IndexFieldMapper<?, ?> mapper : mapping.getIndexFieldMappers()) {
for (final String adapterField : mapper.getAdapterFields()) {
indexedFields.add(adapterField);
}
}
// Remove any fields that are part of the common index model, but not used in the index
// strategy. They shouldn't be considered when trying to find a best match. In the future
// it may be useful to consider an index that has extra common index dimensions that
// contain filtered fields over one that only matches indexed dimensions. For example, if
// I have a spatial index, and a spatial index that stores time, it should pick the one
// that stores time if I supply a temporal constraint, even though it isn't part of the
// index strategy.
final int modelDimensions = index.getIndexModel().getDimensions().length;
final int strategyDimensions = index.getIndexStrategy().getOrderedDimensionDefinitions().length;
for (int i = modelDimensions - 1; i >= strategyDimensions; i--) {
final IndexFieldMapper<?, ?> mapper = mapping.getMapperForIndexField(index.getIndexModel().getDimensions()[i].getFieldName());
for (final String adapterField : mapper.getAdapterFields()) {
indexedFields.remove(adapterField);
}
}
filterClass = Double.class;
}
if (referencedFields.containsAll(indexedFields)) {
final FilterConstraints<?> constraints = filter.getConstraints(filterClass, statisticsStore, adapter, mapping, index, indexedFields);
if (constraints.constrainsAllFields(indexedFields)) {
indexConstraints.put(index, constraints);
}
}
}
if (indexConstraints.size() == 1) {
final Entry<Index, FilterConstraints<?>> bestEntry = indexConstraints.entrySet().iterator().next();
bestIndex = bestEntry.getKey();
constraintCache.put(adapter.getTypeName(), bestEntry.getValue());
} else if (indexConstraints.size() > 1) {
// determine which constraint is the best
double bestCardinality = Double.MAX_VALUE;
Index bestConstrainedIndex = null;
for (final Entry<Index, FilterConstraints<?>> entry : indexConstraints.entrySet()) {
final QueryRanges ranges = entry.getValue().getQueryRanges(baseOptions, statisticsStore);
if (ranges.isEmpty()) {
continue;
}
// TODO: A future optimization would be to add a default numeric histogram for any numeric
// index dimensions and just use the index data ranges to determine cardinality rather
// than decomposing query ranges.
final StatisticId<RowRangeHistogramValue> statisticId = IndexStatistic.generateStatisticId(entry.getKey().getName(), RowRangeHistogramStatistic.STATS_TYPE, Statistic.INTERNAL_TAG);
final RowRangeHistogramStatistic histogram = (RowRangeHistogramStatistic) statisticsStore.getStatisticById(statisticId);
final double cardinality = DataStoreUtils.cardinality(statisticsStore, histogram, adapter, bestConstrainedIndex, ranges);
if ((bestConstrainedIndex == null) || (cardinality < bestCardinality)) {
bestConstrainedIndex = entry.getKey();
bestCardinality = cardinality;
}
}
if (bestConstrainedIndex != null) {
bestIndex = bestConstrainedIndex;
constraintCache.put(adapter.getTypeName(), indexConstraints.get(bestIndex));
}
}
if (bestIndex == null) {
continue;
}
if (!bestIndices.containsKey(bestIndex)) {
bestIndices.put(bestIndex, Lists.newArrayList());
}
bestIndices.get(bestIndex).add(adapter);
}
return bestIndices.entrySet().stream().map(e -> Pair.of(e.getKey(), e.getValue())).collect(Collectors.toList());
}
use of org.locationtech.geowave.core.store.api.IndexFieldMapper in project geowave by locationtech.
the class ExpressionQueryFilter method accept.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public boolean accept(final CommonIndexModel indexModel, final IndexedPersistenceEncoding<?> persistenceEncoding) {
if ((filter != null) && (indexModel != null) && (adapter != null) && (indexMapping != null)) {
final Map<String, Object> fieldValues = Maps.newHashMap();
if (!referencedFieldsInitialized) {
initReferencedFields();
}
final PersistentDataset<?> commonData = persistenceEncoding.getCommonData();
PersistentDataset<Object> adapterExtendedValues = null;
for (final String field : referencedFields) {
if (fieldValues.containsKey(field)) {
continue;
}
if (fieldToIndexFieldMap.containsKey(field)) {
final IndexFieldMapper<?, ?> mapper = fieldToIndexFieldMap.get(field);
final Object indexValue = commonData.getValue(mapper.indexFieldName());
((IndexFieldMapper) mapper).toAdapter(indexValue, new MapRowBuilder(fieldValues));
} else {
final Object value = commonData.getValue(field);
if (value != null) {
fieldValues.put(field, value);
} else {
if (adapterExtendedValues == null) {
adapterExtendedValues = new MultiFieldPersistentDataset<>();
if (persistenceEncoding instanceof AbstractAdapterPersistenceEncoding) {
((AbstractAdapterPersistenceEncoding) persistenceEncoding).convertUnknownValues(adapter, indexModel);
final PersistentDataset<Object> existingExtValues = ((AbstractAdapterPersistenceEncoding) persistenceEncoding).getAdapterExtendedData();
if (persistenceEncoding.isAsync()) {
return false;
}
if (existingExtValues != null) {
adapterExtendedValues.addValues(existingExtValues.getValues());
}
}
}
fieldValues.put(field, adapterExtendedValues.getValue(field));
}
}
}
return filter.evaluate(fieldValues);
}
return true;
}
use of org.locationtech.geowave.core.store.api.IndexFieldMapper in project geowave by locationtech.
the class BaseDataStoreUtils method mapAdapterToIndex.
@SuppressWarnings({ "unchecked", "rawtypes" })
public static AdapterToIndexMapping mapAdapterToIndex(final InternalDataAdapter<?> adapter, final Index index) {
// Build up a list of index field mappers
final Map<String, IndexFieldMapper<?, ?>> mappers = Maps.newHashMap();
// Get index model dimensions
final NumericDimensionField<?>[] dimensions = index.getIndexModel().getDimensions();
// Map dimensions to index fields
final Map<String, List<NumericDimensionField>> indexFields = Arrays.stream(dimensions).collect(Collectors.groupingBy(dim -> dim.getFieldName(), Collectors.mapping(dim -> dim, Collectors.toList())));
// Get adapter fields
final FieldDescriptor<?>[] adapterFields = adapter.getFieldDescriptors();
for (final Entry<String, List<NumericDimensionField>> indexField : indexFields.entrySet()) {
// Get the hints used by all dimensions of the field
final Set<IndexDimensionHint> dimensionHints = Sets.newHashSet();
indexField.getValue().forEach(dim -> dimensionHints.addAll(dim.getDimensionHints()));
final Class<?> indexFieldClass = indexField.getValue().get(0).getFieldClass();
final String indexFieldName = indexField.getKey();
final IndexFieldOptions indexFieldOptions = indexField.getValue().get(0).getIndexFieldOptions();
// Get available mappers for the field class
final List<IndexFieldMapper<?, ?>> availableMappers = IndexFieldMapperRegistry.instance().getAvailableMappers(indexFieldClass);
if (availableMappers.size() == 0) {
throw new IllegalArgumentException("No index field mappers were found for the type: " + indexFieldClass.getName());
}
final List<FieldDescriptor<?>> hintedFields;
if (index instanceof AttributeIndex) {
// Only check the field that is set for the attribute index
hintedFields = Lists.newArrayList(adapter.getFieldDescriptor(((AttributeIndex) index).getAttributeName()));
} else {
// Get any adapter fields that have been hinted for this field
hintedFields = Arrays.stream(adapterFields).filter(field -> dimensionHints.stream().anyMatch(field.indexHints()::contains)).collect(Collectors.toList());
}
if (hintedFields.size() > 0) {
final Class<?> hintedFieldClass = hintedFields.get(0).bindingClass();
for (int i = 1; i < hintedFields.size(); i++) {
if (!hintedFieldClass.equals(hintedFields.get(i).bindingClass())) {
throw new IllegalArgumentException("All hinted fields must be of the same type.");
}
}
boolean mapperFound = false;
// Find a mapper that matches
for (final IndexFieldMapper<?, ?> mapper : availableMappers) {
if (mapper.isCompatibleWith(hintedFieldClass) && (mapper.adapterFieldCount() == hintedFields.size())) {
mapper.init(indexField.getKey(), (List) hintedFields, indexFieldOptions);
mappers.put(indexField.getKey(), mapper);
mapperFound = true;
break;
}
}
if (!mapperFound) {
throw new IllegalArgumentException("No registered index field mappers were found for the type: " + hintedFieldClass.getName() + "[" + hintedFields.size() + "] -> " + indexFieldClass.getName());
}
} else {
// Attempt to infer the field to use
// See if there are any suggested fields
boolean mapperFound = false;
for (final IndexFieldMapper<?, ?> mapper : availableMappers) {
final Set<String> suggestedFieldNames = mapper.getLowerCaseSuggestedFieldNames();
final List<FieldDescriptor<?>> matchingFields = Arrays.stream(adapterFields).filter(field -> mapper.isCompatibleWith(field.bindingClass()) && suggestedFieldNames.contains(field.fieldName().toLowerCase())).collect(Collectors.toList());
if (matchingFields.size() >= mapper.adapterFieldCount()) {
mapperFound = true;
mapper.init(indexFieldName, (List) matchingFields.stream().limit(mapper.adapterFieldCount()).collect(Collectors.toList()), indexFieldOptions);
mappers.put(indexFieldName, mapper);
break;
}
}
// See if a direct mapper is available
if (!mapperFound) {
for (final FieldDescriptor<?> fieldDescriptor : adapterFields) {
if (fieldDescriptor.bindingClass().equals(indexFieldClass)) {
final Optional<IndexFieldMapper<?, ?>> matchingMapper = availableMappers.stream().filter(mapper -> mapper.isCompatibleWith(fieldDescriptor.bindingClass()) && (mapper.adapterFieldCount() == 1)).findFirst();
if (matchingMapper.isPresent()) {
final IndexFieldMapper<?, ?> mapper = matchingMapper.get();
mapperFound = true;
mapper.init(indexFieldName, (List) Lists.newArrayList(fieldDescriptor), indexFieldOptions);
mappers.put(indexFieldName, mapper);
break;
}
}
}
}
// Check other mappers
if (!mapperFound) {
for (final IndexFieldMapper<?, ?> mapper : availableMappers) {
final List<FieldDescriptor<?>> matchingFields = Arrays.stream(adapterFields).filter(field -> mapper.isCompatibleWith(field.bindingClass())).collect(Collectors.toList());
if (matchingFields.size() >= mapper.adapterFieldCount()) {
mapperFound = true;
mapper.init(indexFieldName, (List) matchingFields.stream().limit(mapper.adapterFieldCount()).collect(Collectors.toList()), indexFieldOptions);
mappers.put(indexFieldName, mapper);
break;
}
}
}
if (!mapperFound) {
throw new IllegalArgumentException("No suitable index field mapper could be found for the index field " + indexFieldName);
}
}
}
return new AdapterToIndexMapping(adapter.getAdapterId(), index.getName(), mappers.values().stream().collect(Collectors.toList()));
}
Aggregations