Skip to content

Commit a4ec441

Browse files
committed
Move histogram serialization to library
1 parent 23ee3b6 commit a4ec441

File tree

3 files changed

+93
-53
lines changed

3 files changed

+93
-53
lines changed

libs/exponential-histogram/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ apply plugin: 'elasticsearch.build'
1313

1414
dependencies {
1515
api project(':libs:core')
16+
api project(':libs:x-content')
1617
api "org.apache.lucene:lucene-core:${versions.lucene}"
1718

1819
testImplementation(project(":test:framework"))
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.exponentialhistogram;
11+
12+
import org.elasticsearch.xcontent.XContentBuilder;
13+
14+
import java.io.IOException;
15+
16+
/**
17+
* Handles the serialization of an {@link ExponentialHistogram} to XContent.
18+
*/
19+
public class ExponentialHistogramXContent {
20+
21+
public static final String SCALE_FIELD = "scale";
22+
public static final String ZERO_FIELD = "zero";
23+
public static final String ZERO_COUNT_FIELD = "count";
24+
public static final String ZERO_THRESHOLD_FIELD = "threshold";
25+
public static final String POSITIVE_FIELD = "positive";
26+
public static final String NEGATIVE_FIELD = "negative";
27+
public static final String BUCKET_INDICES_FIELD = "indices";
28+
public static final String BUCKET_COUNTS_FIELD = "counts";
29+
30+
/**
31+
* Serializes an {@link ExponentialHistogram} to the provided {@link XContentBuilder}.
32+
* @param builder the XContentBuilder to write to
33+
* @param histogram the ExponentialHistogram to serialize
34+
* @throws IOException if the XContentBuilder throws an IOException
35+
*/
36+
public static void serialize(XContentBuilder builder, ExponentialHistogram histogram) throws IOException {
37+
builder.startObject();
38+
39+
builder.field(SCALE_FIELD, histogram.scale());
40+
double zeroThreshold = histogram.zeroBucket().zeroThreshold();
41+
long zeroCount = histogram.zeroBucket().count();
42+
43+
if (zeroCount != 0 || zeroThreshold != 0) {
44+
builder.startObject(ZERO_FIELD);
45+
if (zeroCount != 0) {
46+
builder.field(ZERO_COUNT_FIELD, zeroCount);
47+
}
48+
if (zeroThreshold != 0) {
49+
builder.field(ZERO_THRESHOLD_FIELD, zeroThreshold);
50+
}
51+
builder.endObject();
52+
}
53+
54+
writeBuckets(builder, POSITIVE_FIELD, histogram.positiveBuckets());
55+
writeBuckets(builder, NEGATIVE_FIELD, histogram.negativeBuckets());
56+
57+
builder.endObject();
58+
}
59+
60+
private static void writeBuckets(XContentBuilder b, String fieldName, ExponentialHistogram.Buckets buckets) throws IOException {
61+
if (buckets.iterator().hasNext() == false) {
62+
return;
63+
}
64+
b.startObject(fieldName);
65+
BucketIterator it = buckets.iterator();
66+
b.startArray(BUCKET_INDICES_FIELD);
67+
while (it.hasNext()) {
68+
b.value(it.peekIndex());
69+
it.advance();
70+
}
71+
b.endArray();
72+
it = buckets.iterator();
73+
b.startArray(BUCKET_COUNTS_FIELD);
74+
while (it.hasNext()) {
75+
b.value(it.peekCount());
76+
it.advance();
77+
}
78+
b.endArray();
79+
b.endObject();
80+
}
81+
82+
}

x-pack/plugin/mapper-exponential-histogram/src/main/java/org/elasticsearch/xpack/exponentialhistogram/ExponentialHistogramFieldMapper.java

Lines changed: 10 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
import org.elasticsearch.common.Explicit;
2020
import org.elasticsearch.common.io.stream.BytesStreamOutput;
2121
import org.elasticsearch.common.util.FeatureFlag;
22-
import org.elasticsearch.exponentialhistogram.BucketIterator;
2322
import org.elasticsearch.exponentialhistogram.ExponentialHistogram;
23+
import org.elasticsearch.exponentialhistogram.ExponentialHistogramXContent;
2424
import org.elasticsearch.index.fielddata.FieldDataContext;
2525
import org.elasticsearch.index.fielddata.IndexFieldData;
2626
import org.elasticsearch.index.mapper.CompositeSyntheticFieldLoader;
@@ -92,15 +92,15 @@ public class ExponentialHistogramFieldMapper extends FieldMapper {
9292

9393
public static final String CONTENT_TYPE = "exponential_histogram";
9494

95-
public static final ParseField SCALE_FIELD = new ParseField("scale");
96-
public static final ParseField ZERO_FIELD = new ParseField("zero");
97-
public static final ParseField ZERO_COUNT_FIELD = new ParseField("count");
98-
public static final ParseField ZERO_THRESHOLD_FIELD = new ParseField("threshold");
95+
public static final ParseField SCALE_FIELD = new ParseField(ExponentialHistogramXContent.SCALE_FIELD);
96+
public static final ParseField ZERO_FIELD = new ParseField(ExponentialHistogramXContent.ZERO_FIELD);
97+
public static final ParseField ZERO_COUNT_FIELD = new ParseField(ExponentialHistogramXContent.ZERO_COUNT_FIELD);
98+
public static final ParseField ZERO_THRESHOLD_FIELD = new ParseField(ExponentialHistogramXContent.ZERO_THRESHOLD_FIELD);
9999

100-
public static final ParseField POSITIVE_FIELD = new ParseField("positive");
101-
public static final ParseField NEGATIVE_FIELD = new ParseField("negative");
102-
public static final ParseField BUCKET_INDICES_FIELD = new ParseField("indices");
103-
public static final ParseField BUCKET_COUNTS_FIELD = new ParseField("counts");
100+
public static final ParseField POSITIVE_FIELD = new ParseField(ExponentialHistogramXContent.POSITIVE_FIELD);
101+
public static final ParseField NEGATIVE_FIELD = new ParseField(ExponentialHistogramXContent.NEGATIVE_FIELD);
102+
public static final ParseField BUCKET_INDICES_FIELD = new ParseField(ExponentialHistogramXContent.BUCKET_INDICES_FIELD);
103+
public static final ParseField BUCKET_COUNTS_FIELD = new ParseField(ExponentialHistogramXContent.BUCKET_COUNTS_FIELD);
104104

105105
private static ExponentialHistogramFieldMapper toType(FieldMapper in) {
106106
return (ExponentialHistogramFieldMapper) in;
@@ -625,50 +625,7 @@ public void write(XContentBuilder b) throws IOException {
625625
}
626626

627627
histogram.reset(zeroThreshold, valueCount, binaryValue);
628-
629-
b.startObject();
630-
631-
b.field(SCALE_FIELD.getPreferredName(), histogram.scale());
632-
double zeroThreshold = histogram.zeroBucket().zeroThreshold();
633-
long zeroCount = histogram.zeroBucket().count();
634-
635-
if (zeroCount != 0 || zeroThreshold != 0) {
636-
b.startObject(ZERO_FIELD.getPreferredName());
637-
if (zeroCount != 0) {
638-
b.field(ZERO_COUNT_FIELD.getPreferredName(), zeroCount);
639-
}
640-
if (zeroThreshold != 0) {
641-
b.field(ZERO_THRESHOLD_FIELD.getPreferredName(), zeroThreshold);
642-
}
643-
b.endObject();
644-
}
645-
646-
writeBuckets(b, POSITIVE_FIELD.getPreferredName(), histogram.positiveBuckets());
647-
writeBuckets(b, NEGATIVE_FIELD.getPreferredName(), histogram.negativeBuckets());
648-
649-
b.endObject();
650-
}
651-
652-
private static void writeBuckets(XContentBuilder b, String fieldName, ExponentialHistogram.Buckets buckets) throws IOException {
653-
if (buckets.iterator().hasNext() == false) {
654-
return;
655-
}
656-
b.startObject(fieldName);
657-
BucketIterator it = buckets.iterator();
658-
b.startArray(BUCKET_INDICES_FIELD.getPreferredName());
659-
while (it.hasNext()) {
660-
b.value(it.peekIndex());
661-
it.advance();
662-
}
663-
b.endArray();
664-
it = buckets.iterator();
665-
b.startArray(BUCKET_COUNTS_FIELD.getPreferredName());
666-
while (it.hasNext()) {
667-
b.value(it.peekCount());
668-
it.advance();
669-
}
670-
b.endArray();
671-
b.endObject();
628+
ExponentialHistogramXContent.serialize(b, histogram);
672629
}
673630

674631
@Override

0 commit comments

Comments
 (0)