Skip to content

Commit

Permalink
[ML] adjusting for backport (#36117)
Browse files Browse the repository at this point in the history
  • Loading branch information
benwtrent committed Dec 4, 2018
1 parent ff6c194 commit f7f5484
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.util.CachedSupplier;
import org.elasticsearch.common.xcontent.ObjectParser;
import org.elasticsearch.common.xcontent.ToXContentObject;
import org.elasticsearch.common.xcontent.XContentBuilder;
Expand All @@ -30,6 +29,7 @@
import org.elasticsearch.xpack.core.ml.datafeed.extractor.ExtractorUtils;
import org.elasticsearch.xpack.core.ml.job.config.Job;
import org.elasticsearch.xpack.core.ml.job.messages.Messages;
import org.elasticsearch.xpack.core.ml.utils.CachedSupplier;
import org.elasticsearch.xpack.core.ml.utils.ExceptionsHelper;
import org.elasticsearch.xpack.core.ml.utils.MlStrings;
import org.elasticsearch.xpack.core.ml.utils.ToXContentParams;
Expand Down Expand Up @@ -256,7 +256,7 @@ public DatafeedConfig(StreamInput in) throws IOException {
} else {
this.types = null;
}
if (in.getVersion().before(Version.CURRENT)) {
if (in.getVersion().before(Version.V_6_6_0)) {
this.query = QUERY_TRANSFORMER.toMap(in.readNamedWriteable(QueryBuilder.class));
this.aggregations = AGG_TRANSFORMER.toMap(in.readOptionalWriteable(AggregatorFactories.Builder::new));
} else {
Expand Down Expand Up @@ -384,7 +384,7 @@ public void writeTo(StreamOutput out) throws IOException {
} else {
out.writeBoolean(false);
}
if (out.getVersion().before(Version.CURRENT)) {
if (out.getVersion().before(Version.V_6_6_0)) {
out.writeNamedWriteable(getParsedQuery());
out.writeOptionalWriteable(getParsedAggregations());
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.elasticsearch.xpack.core.ml.utils;

import java.util.function.Supplier;

/**
* A {@link Supplier} that caches its return value. This may be useful to make
* a {@link Supplier} idempotent or for performance reasons if always returning
* the same instance is acceptable.
*/
public final class CachedSupplier<T> implements Supplier<T> {

private Supplier<T> supplier;
private T result;
private boolean resultSet;

public CachedSupplier(Supplier<T> supplier) {
this.supplier = supplier;
}

@Override
public synchronized T get() {
if (resultSet == false) {
result = supplier.get();
resultSet = true;
}
return result;
}

}

0 comments on commit f7f5484

Please sign in to comment.