|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.client.ml; |
| 21 | + |
| 22 | +import org.elasticsearch.client.Validatable; |
| 23 | +import org.elasticsearch.client.ValidationException; |
| 24 | +import org.elasticsearch.client.ml.job.config.AnalysisConfig; |
| 25 | +import org.elasticsearch.common.xcontent.ToXContentObject; |
| 26 | +import org.elasticsearch.common.xcontent.XContentBuilder; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.Map; |
| 31 | +import java.util.Objects; |
| 32 | +import java.util.Optional; |
| 33 | + |
| 34 | +/** |
| 35 | + * Request to estimate the model memory an analysis config is likely to need given supplied field cardinalities. |
| 36 | + */ |
| 37 | +public class EstimateModelMemoryRequest implements Validatable, ToXContentObject { |
| 38 | + |
| 39 | + public static final String ANALYSIS_CONFIG = "analysis_config"; |
| 40 | + public static final String OVERALL_CARDINALITY = "overall_cardinality"; |
| 41 | + public static final String MAX_BUCKET_CARDINALITY = "max_bucket_cardinality"; |
| 42 | + |
| 43 | + private final AnalysisConfig analysisConfig; |
| 44 | + private Map<String, Long> overallCardinality = Collections.emptyMap(); |
| 45 | + private Map<String, Long> maxBucketCardinality = Collections.emptyMap(); |
| 46 | + |
| 47 | + @Override |
| 48 | + public Optional<ValidationException> validate() { |
| 49 | + return Optional.empty(); |
| 50 | + } |
| 51 | + |
| 52 | + public EstimateModelMemoryRequest(AnalysisConfig analysisConfig) { |
| 53 | + this.analysisConfig = Objects.requireNonNull(analysisConfig); |
| 54 | + } |
| 55 | + |
| 56 | + public AnalysisConfig getAnalysisConfig() { |
| 57 | + return analysisConfig; |
| 58 | + } |
| 59 | + |
| 60 | + public Map<String, Long> getOverallCardinality() { |
| 61 | + return overallCardinality; |
| 62 | + } |
| 63 | + |
| 64 | + public void setOverallCardinality(Map<String, Long> overallCardinality) { |
| 65 | + this.overallCardinality = Collections.unmodifiableMap(overallCardinality); |
| 66 | + } |
| 67 | + |
| 68 | + public Map<String, Long> getMaxBucketCardinality() { |
| 69 | + return maxBucketCardinality; |
| 70 | + } |
| 71 | + |
| 72 | + public void setMaxBucketCardinality(Map<String, Long> maxBucketCardinality) { |
| 73 | + this.maxBucketCardinality = Collections.unmodifiableMap(maxBucketCardinality); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException { |
| 78 | + builder.startObject(); |
| 79 | + builder.field(ANALYSIS_CONFIG, analysisConfig); |
| 80 | + if (overallCardinality.isEmpty() == false) { |
| 81 | + builder.field(OVERALL_CARDINALITY, overallCardinality); |
| 82 | + } |
| 83 | + if (maxBucketCardinality.isEmpty() == false) { |
| 84 | + builder.field(MAX_BUCKET_CARDINALITY, maxBucketCardinality); |
| 85 | + } |
| 86 | + builder.endObject(); |
| 87 | + return builder; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public int hashCode() { |
| 92 | + return Objects.hash(analysisConfig, overallCardinality, maxBucketCardinality); |
| 93 | + } |
| 94 | + |
| 95 | + @Override |
| 96 | + public boolean equals(Object other) { |
| 97 | + if (this == other) { |
| 98 | + return true; |
| 99 | + } |
| 100 | + |
| 101 | + if (other == null || getClass() != other.getClass()) { |
| 102 | + return false; |
| 103 | + } |
| 104 | + |
| 105 | + EstimateModelMemoryRequest that = (EstimateModelMemoryRequest) other; |
| 106 | + return Objects.equals(analysisConfig, that.analysisConfig) && |
| 107 | + Objects.equals(overallCardinality, that.overallCardinality) && |
| 108 | + Objects.equals(maxBucketCardinality, that.maxBucketCardinality); |
| 109 | + } |
| 110 | +} |
0 commit comments