io.druid.query.BaseQuery Maven / Gradle / Ivy
/*
* Licensed to Metamarkets Group Inc. (Metamarkets) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Metamarkets 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 io.druid.query;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Ordering;
import io.druid.guice.annotations.ExtensionPoint;
import io.druid.query.spec.QuerySegmentSpec;
import org.joda.time.Duration;
import org.joda.time.Interval;
import java.util.List;
import java.util.Map;
/**
*/
@ExtensionPoint
public abstract class BaseQuery> implements Query
{
public static void checkInterrupted()
{
if (Thread.interrupted()) {
throw new QueryInterruptedException(new InterruptedException());
}
}
public static final String QUERYID = "queryId";
private final DataSource dataSource;
private final boolean descending;
private final Map context;
private final QuerySegmentSpec querySegmentSpec;
private volatile Duration duration;
public BaseQuery(
DataSource dataSource,
QuerySegmentSpec querySegmentSpec,
boolean descending,
Map context
)
{
Preconditions.checkNotNull(dataSource, "dataSource can't be null");
Preconditions.checkNotNull(querySegmentSpec, "querySegmentSpec can't be null");
this.dataSource = dataSource;
this.context = context;
this.querySegmentSpec = querySegmentSpec;
this.descending = descending;
}
@JsonProperty
@Override
public DataSource getDataSource()
{
return dataSource;
}
@JsonProperty
@Override
public boolean isDescending()
{
return descending;
}
@JsonProperty("intervals")
public QuerySegmentSpec getQuerySegmentSpec()
{
return querySegmentSpec;
}
@Override
public QueryRunner getRunner(QuerySegmentWalker walker, Map context)
{
return querySegmentSpec.lookup(this, walker);
}
@Override
public List getIntervals()
{
return querySegmentSpec.getIntervals();
}
@Override
public Duration getDuration()
{
if (duration == null) {
Duration totalDuration = new Duration(0);
for (Interval interval : querySegmentSpec.getIntervals()) {
if (interval != null) {
totalDuration = totalDuration.plus(interval.toDuration());
}
}
duration = totalDuration;
}
return duration;
}
@Override
@JsonProperty
public Map getContext()
{
return context;
}
@Override
public ContextType getContextValue(String key)
{
return context == null ? null : (ContextType) context.get(key);
}
@Override
public ContextType getContextValue(String key, ContextType defaultValue)
{
ContextType retVal = getContextValue(key);
return retVal == null ? defaultValue : retVal;
}
@Override
public boolean getContextBoolean(String key, boolean defaultValue)
{
return QueryContexts.parseBoolean(this, key, defaultValue);
}
/**
* @deprecated use {@link #computeOverriddenContext(Map, Map) computeOverriddenContext(getContext(), overrides))}
* instead. This method may be removed in the next minor or major version of Druid.
*/
@Deprecated
protected Map computeOverridenContext(final Map overrides)
{
return computeOverriddenContext(getContext(), overrides);
}
protected static Map computeOverriddenContext(
final Map context,
final Map overrides
)
{
Map overridden = Maps.newTreeMap();
if (context != null) {
overridden.putAll(context);
}
overridden.putAll(overrides);
return overridden;
}
@Override
public Ordering getResultOrdering()
{
Ordering retVal = Ordering.natural();
return descending ? retVal.reverse() : retVal;
}
@Override
public String getId()
{
return (String) getContextValue(QUERYID);
}
@Override
public Query withId(String id)
{
return withOverriddenContext(ImmutableMap.of(QUERYID, id));
}
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
BaseQuery baseQuery = (BaseQuery) o;
if (descending != baseQuery.descending) {
return false;
}
if (context != null ? !context.equals(baseQuery.context) : baseQuery.context != null) {
return false;
}
if (dataSource != null ? !dataSource.equals(baseQuery.dataSource) : baseQuery.dataSource != null) {
return false;
}
if (duration != null ? !duration.equals(baseQuery.duration) : baseQuery.duration != null) {
return false;
}
if (querySegmentSpec != null
? !querySegmentSpec.equals(baseQuery.querySegmentSpec)
: baseQuery.querySegmentSpec != null) {
return false;
}
return true;
}
@Override
public int hashCode()
{
int result = dataSource != null ? dataSource.hashCode() : 0;
result = 31 * result + (descending ? 1 : 0);
result = 31 * result + (context != null ? context.hashCode() : 0);
result = 31 * result + (querySegmentSpec != null ? querySegmentSpec.hashCode() : 0);
result = 31 * result + (duration != null ? duration.hashCode() : 0);
return result;
}
}