| /* |
| * 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 org.apache.druid.query.filter; |
| |
| import com.fasterxml.jackson.annotation.JsonInclude; |
| import com.fasterxml.jackson.annotation.JsonProperty; |
| import com.google.common.annotations.VisibleForTesting; |
| import com.google.common.base.Preconditions; |
| import com.google.common.collect.ImmutableSet; |
| import com.google.common.collect.RangeSet; |
| import org.apache.druid.java.util.common.StringUtils; |
| import org.apache.druid.query.extraction.ExtractionFn; |
| import org.apache.druid.query.search.SearchQuerySpec; |
| import org.apache.druid.segment.filter.SearchQueryFilter; |
| |
| import javax.annotation.Nullable; |
| import java.nio.ByteBuffer; |
| import java.util.Objects; |
| import java.util.Set; |
| |
| /** |
| */ |
| public class SearchQueryDimFilter implements DimFilter |
| { |
| private final String dimension; |
| private final SearchQuerySpec query; |
| @Nullable |
| private final ExtractionFn extractionFn; |
| @Nullable |
| private final FilterTuning filterTuning; |
| |
| public SearchQueryDimFilter( |
| @JsonProperty("dimension") String dimension, |
| @JsonProperty("query") SearchQuerySpec query, |
| @JsonProperty("extractionFn") @Nullable ExtractionFn extractionFn, |
| @JsonProperty("filterTuning") @Nullable FilterTuning filterTuning |
| ) |
| { |
| Preconditions.checkArgument(dimension != null, "dimension must not be null"); |
| Preconditions.checkArgument(query != null, "query must not be null"); |
| |
| this.dimension = dimension; |
| this.query = query; |
| this.extractionFn = extractionFn; |
| this.filterTuning = filterTuning; |
| } |
| |
| @VisibleForTesting |
| public SearchQueryDimFilter( |
| String dimension, |
| SearchQuerySpec query, |
| @Nullable ExtractionFn extractionFn |
| ) |
| { |
| this(dimension, query, extractionFn, null); |
| } |
| |
| @JsonProperty |
| public String getDimension() |
| { |
| return dimension; |
| } |
| |
| @JsonProperty |
| public SearchQuerySpec getQuery() |
| { |
| return query; |
| } |
| |
| @Nullable |
| @JsonProperty |
| public ExtractionFn getExtractionFn() |
| { |
| return extractionFn; |
| } |
| |
| @Nullable |
| @JsonInclude(JsonInclude.Include.NON_NULL) |
| @JsonProperty |
| public FilterTuning getFilterTuning() |
| { |
| return filterTuning; |
| } |
| |
| @Override |
| public byte[] getCacheKey() |
| { |
| final byte[] dimensionBytes = StringUtils.toUtf8(dimension); |
| final byte[] queryBytes = query.getCacheKey(); |
| byte[] extractionFnBytes = extractionFn == null ? new byte[0] : extractionFn.getCacheKey(); |
| |
| return ByteBuffer.allocate(3 + dimensionBytes.length + queryBytes.length + extractionFnBytes.length) |
| .put(DimFilterUtils.SEARCH_QUERY_TYPE_ID) |
| .put(dimensionBytes) |
| .put(DimFilterUtils.STRING_SEPARATOR) |
| .put(queryBytes) |
| .put(DimFilterUtils.STRING_SEPARATOR) |
| .put(extractionFnBytes) |
| .array(); |
| } |
| |
| @Override |
| public DimFilter optimize() |
| { |
| return this; |
| } |
| |
| @Override |
| public Filter toFilter() |
| { |
| return new SearchQueryFilter(dimension, query, extractionFn, filterTuning); |
| } |
| |
| @Override |
| public RangeSet<String> getDimensionRangeSet(String dimension) |
| { |
| return null; |
| } |
| |
| @Override |
| public Set<String> getRequiredColumns() |
| { |
| return ImmutableSet.of(dimension); |
| } |
| |
| @Override |
| public String toString() |
| { |
| return "SearchQueryDimFilter{" + |
| "dimension='" + dimension + '\'' + |
| ", query=" + query + |
| ", extractionFn='" + extractionFn + '\'' + |
| ", filterTuning=" + filterTuning + |
| '}'; |
| } |
| |
| @Override |
| public boolean equals(Object o) |
| { |
| if (this == o) { |
| return true; |
| } |
| if (o == null || getClass() != o.getClass()) { |
| return false; |
| } |
| SearchQueryDimFilter that = (SearchQueryDimFilter) o; |
| return dimension.equals(that.dimension) && |
| query.equals(that.query) && |
| Objects.equals(extractionFn, that.extractionFn) && |
| Objects.equals(filterTuning, that.filterTuning); |
| } |
| |
| @Override |
| public int hashCode() |
| { |
| return Objects.hash(dimension, query, extractionFn, filterTuning); |
| } |
| } |