Skip to content

Commit

Permalink
Aggregations Refactor: Refactor Histogram and Date Histogram Aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
colings86 committed Nov 16, 2015
1 parent 43d96d4 commit ce3719f
Show file tree
Hide file tree
Showing 15 changed files with 753 additions and 277 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
package org.elasticsearch.common.rounding;

import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Streamable;

import java.io.IOException;
import java.util.Objects;

/**
* A strategy for rounding long values.
Expand Down Expand Up @@ -61,6 +63,12 @@ public final long round(long value) {
*/
public abstract long nextRoundingValue(long value);

@Override
public abstract boolean equals(Object obj);

@Override
public abstract int hashCode();

/**
* Rounding strategy which is based on an interval
*
Expand All @@ -70,6 +78,8 @@ public static class Interval extends Rounding {

final static byte ID = 0;

public static final ParseField INTERVAL_FIELD = new ParseField("interval");

private long interval;

public Interval() { // for serialization
Expand Down Expand Up @@ -126,12 +136,31 @@ public void readFrom(StreamInput in) throws IOException {
public void writeTo(StreamOutput out) throws IOException {
out.writeVLong(interval);
}

@Override
public int hashCode() {
return Objects.hash(interval);
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Interval other = (Interval) obj;
return Objects.equals(interval, other.interval);
}
}

public static class FactorRounding extends Rounding {

final static byte ID = 7;

public static final ParseField FACTOR_FIELD = new ParseField("factor");

private Rounding rounding;

private float factor;
Expand Down Expand Up @@ -166,7 +195,7 @@ public long nextRoundingValue(long value) {

@Override
public void readFrom(StreamInput in) throws IOException {
rounding = (TimeZoneRounding) Rounding.Streams.read(in);
rounding = Rounding.Streams.read(in);
factor = in.readFloat();
}

Expand All @@ -175,12 +204,32 @@ public void writeTo(StreamOutput out) throws IOException {
Rounding.Streams.write(rounding, out);
out.writeFloat(factor);
}

@Override
public int hashCode() {
return Objects.hash(rounding, factor);
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
FactorRounding other = (FactorRounding) obj;
return Objects.equals(rounding, other.rounding)
&& Objects.equals(factor, other.factor);
}
}

public static class OffsetRounding extends Rounding {

final static byte ID = 8;

public static final ParseField OFFSET_FIELD = new ParseField("offset");

private Rounding rounding;

private long offset;
Expand Down Expand Up @@ -224,6 +273,24 @@ public void writeTo(StreamOutput out) throws IOException {
Rounding.Streams.write(rounding, out);
out.writeLong(offset);
}

@Override
public int hashCode() {
return Objects.hash(rounding, offset);
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
OffsetRounding other = (OffsetRounding) obj;
return Objects.equals(rounding, other.rounding)
&& Objects.equals(offset, other.offset);
}
}

public static class Streams {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

package org.elasticsearch.common.rounding;

import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.unit.TimeValue;
Expand All @@ -27,10 +28,13 @@
import org.joda.time.DurationField;

import java.io.IOException;
import java.util.Objects;

/**
*/
public abstract class TimeZoneRounding extends Rounding {
public static final ParseField INTERVAL_FIELD = new ParseField("interval");
public static final ParseField TIME_ZONE_FIELD = new ParseField("time_zone");

public static Builder builder(DateTimeUnit unit) {
return new Builder(unit);
Expand Down Expand Up @@ -157,6 +161,24 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeByte(unit.id());
out.writeString(timeZone.getID());
}

@Override
public int hashCode() {
return Objects.hash(unit, timeZone);
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
TimeUnitRounding other = (TimeUnitRounding) obj;
return Objects.equals(unit, other.unit)
&& Objects.equals(timeZone, other.timeZone);
}
}

static class TimeIntervalRounding extends TimeZoneRounding {
Expand Down Expand Up @@ -214,5 +236,23 @@ public void writeTo(StreamOutput out) throws IOException {
out.writeVLong(interval);
out.writeString(timeZone.getID());
}

@Override
public int hashCode() {
return Objects.hash(interval, timeZone);
}

@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
TimeIntervalRounding other = (TimeIntervalRounding) obj;
return Objects.equals(interval, other.interval)
&& Objects.equals(timeZone, other.timeZone);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ protected XContentBuilder doInternalXContent(XContentBuilder builder, Params par
}

if (extendedBoundsMin != null || extendedBoundsMax != null) {
builder.startObject(DateHistogramParser.EXTENDED_BOUNDS.getPreferredName());
builder.startObject(ExtendedBounds.EXTENDED_BOUNDS_FIELD.getPreferredName());
if (extendedBoundsMin != null) {
builder.field("min", extendedBoundsMin);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,16 @@

package org.elasticsearch.search.aggregations.bucket.histogram;

import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;

import java.io.IOException;

/**
* The interval the date histogram is based on.
*/
public class DateHistogramInterval {
public class DateHistogramInterval implements Writeable<DateHistogramInterval> {

public static final DateHistogramInterval SECOND = new DateHistogramInterval("1s");
public static final DateHistogramInterval MINUTE = new DateHistogramInterval("1m");
Expand All @@ -33,6 +39,10 @@ public class DateHistogramInterval {
public static final DateHistogramInterval QUARTER = new DateHistogramInterval("1q");
public static final DateHistogramInterval YEAR = new DateHistogramInterval("1y");

public static final DateHistogramInterval readFromStream(StreamInput in) throws IOException {
return SECOND.readFrom(in);
}

public static DateHistogramInterval seconds(int sec) {
return new DateHistogramInterval(sec + "s");
}
Expand Down Expand Up @@ -63,4 +73,14 @@ public DateHistogramInterval(String expression) {
public String toString() {
return expression;
}

@Override
public DateHistogramInterval readFrom(StreamInput in) throws IOException {
return new DateHistogramInterval(in.readString());
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(expression);
}
}
Loading

0 comments on commit ce3719f

Please sign in to comment.