|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.autotagging; |
| 10 | + |
| 11 | +import org.opensearch.ResourceNotFoundException; |
| 12 | +import org.opensearch.common.collect.Tuple; |
| 13 | + |
| 14 | +import java.util.HashMap; |
| 15 | +import java.util.Map; |
| 16 | + |
| 17 | +/** |
| 18 | + * Registry for managing auto-tagging attributes and feature types. |
| 19 | + * This class provides functionality to register and retrieve {@link Attribute} and {@link FeatureType} instances |
| 20 | + * used for auto-tagging. |
| 21 | + * |
| 22 | + * @opensearch.experimental |
| 23 | + */ |
| 24 | +public class AutoTaggingRegistry { |
| 25 | + public static final Map<Tuple<String, String>, FeatureType> featureTypesRegistryMap = new HashMap<>(); |
| 26 | + public static final Map<Tuple<String, String>, Attribute> attributeRegistryMap = new HashMap<>(); |
| 27 | + |
| 28 | + public static void registerFeatureType(FeatureType featureType) { |
| 29 | + if (featureType == null) { |
| 30 | + throw new IllegalStateException("Feature type is not initialized and can't be registered"); |
| 31 | + } |
| 32 | + featureTypesRegistryMap.put(new Tuple<>(featureType.getClass().getName(), featureType.getName()), featureType); |
| 33 | + } |
| 34 | + |
| 35 | + public static void registerAttribute(Attribute attribute) { |
| 36 | + if (attribute == null) { |
| 37 | + throw new IllegalStateException("Attribute is not initialized and can't be registered"); |
| 38 | + } |
| 39 | + attributeRegistryMap.put(new Tuple<>(attribute.getClass().getName(), attribute.getName()), attribute); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Retrieves the registered {@link FeatureType} instance based on class name and feature type name. |
| 44 | + * This method assumes that FeatureTypes are singletons, meaning that each unique |
| 45 | + * (className, featureTypeName) pair corresponds to a single, globally shared instance. |
| 46 | + * |
| 47 | + * @param className The name of the class associated with the feature type. |
| 48 | + * @param featureTypeName The name of the feature type. |
| 49 | + */ |
| 50 | + public static FeatureType getFeatureType(String className, String featureTypeName) { |
| 51 | + FeatureType featureType = featureTypesRegistryMap.get(new Tuple<>(className, featureTypeName)); |
| 52 | + if (featureType == null) { |
| 53 | + throw new ResourceNotFoundException( |
| 54 | + "Couldn't find a feature type with name: " |
| 55 | + + featureTypeName |
| 56 | + + " under the class: " |
| 57 | + + className |
| 58 | + + ". Make sure you have registered it." |
| 59 | + ); |
| 60 | + } |
| 61 | + return featureType; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Retrieves the registered {@link Attribute} instance based on the class name and attribute name. |
| 66 | + * This method assumes that Attributes are singletons, meaning that each unique |
| 67 | + * (className, attributeName) pair corresponds to a single, globally shared instance. |
| 68 | + * |
| 69 | + * @param className The name of the class associated with the attribute. |
| 70 | + * @param attributeName The name of the attribute. |
| 71 | + */ |
| 72 | + public static Attribute getAttribute(String className, String attributeName) { |
| 73 | + Attribute attribute = attributeRegistryMap.get(new Tuple<>(className, attributeName)); |
| 74 | + if (attribute == null) { |
| 75 | + throw new ResourceNotFoundException( |
| 76 | + "Couldn't find a attribute with name: " |
| 77 | + + attributeName |
| 78 | + + " under the class: " |
| 79 | + + className |
| 80 | + + ". Make sure you have registered it." |
| 81 | + ); |
| 82 | + } |
| 83 | + return attribute; |
| 84 | + } |
| 85 | +} |
0 commit comments