|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright 2018-2019 The Feast Authors |
| 4 | + * |
| 5 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | + * you may not use this file except in compliance with the License. |
| 7 | + * You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package feast.ingestion.utils; |
| 18 | + |
| 19 | +import com.google.protobuf.InvalidProtocolBufferException; |
| 20 | +import com.google.protobuf.util.JsonFormat; |
| 21 | +import feast.proto.core.FeatureSetProto.FeatureSet; |
| 22 | +import feast.proto.core.FeatureSetProto.FeatureSetSpec; |
| 23 | +import feast.proto.core.StoreProto.Store; |
| 24 | +import feast.proto.core.StoreProto.Store.Subscription; |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.List; |
| 27 | +import java.util.regex.Pattern; |
| 28 | + |
| 29 | +/** |
| 30 | + * * Class copied and adapted from feast-ingestion SpecUtil. |
| 31 | + * |
| 32 | + * <p>TODO move common ingestion code into a shared project. |
| 33 | + */ |
| 34 | +public class SpecUtil { |
| 35 | + |
| 36 | + public static String getFeatureSetReference(FeatureSetSpec featureSetSpec) { |
| 37 | + return String.format("%s/%s", featureSetSpec.getProject(), featureSetSpec.getName()); |
| 38 | + } |
| 39 | + |
| 40 | + /** Get only feature set specs that matches the subscription */ |
| 41 | + public static List<FeatureSet> getSubscribedFeatureSets( |
| 42 | + List<Subscription> subscriptions, List<FeatureSet> featureSets) { |
| 43 | + List<FeatureSet> subscribed = new ArrayList<>(); |
| 44 | + for (FeatureSet featureSet : featureSets) { |
| 45 | + for (Subscription sub : subscriptions) { |
| 46 | + // If configuration missing, fail |
| 47 | + if (sub.getProject().isEmpty() || sub.getName().isEmpty()) { |
| 48 | + throw new IllegalArgumentException( |
| 49 | + String.format("Subscription is missing arguments: %s", sub.toString())); |
| 50 | + } |
| 51 | + |
| 52 | + // If all wildcards, subscribe to everything |
| 53 | + if (sub.getProject().equals("*") || sub.getName().equals("*")) { |
| 54 | + subscribed.add(featureSet); |
| 55 | + break; |
| 56 | + } |
| 57 | + |
| 58 | + // Match project name |
| 59 | + if (!featureSet.getSpec().getProject().equals(sub.getProject())) { |
| 60 | + continue; |
| 61 | + } |
| 62 | + |
| 63 | + // Convert wildcard to regex |
| 64 | + String subName = sub.getName(); |
| 65 | + if (!sub.getName().contains(".*")) { |
| 66 | + subName = subName.replace("*", ".*"); |
| 67 | + } |
| 68 | + |
| 69 | + // Match feature set name to pattern |
| 70 | + Pattern pattern = Pattern.compile(subName); |
| 71 | + if (!pattern.matcher(featureSet.getSpec().getName()).matches()) { |
| 72 | + continue; |
| 73 | + } |
| 74 | + subscribed.add(featureSet); |
| 75 | + } |
| 76 | + } |
| 77 | + return subscribed; |
| 78 | + } |
| 79 | + |
| 80 | + public static List<FeatureSet> parseFeatureSetSpecJsonList(List<String> jsonList) |
| 81 | + throws InvalidProtocolBufferException { |
| 82 | + List<FeatureSet> featureSets = new ArrayList<>(); |
| 83 | + for (String json : jsonList) { |
| 84 | + FeatureSetSpec.Builder builder = FeatureSetSpec.newBuilder(); |
| 85 | + JsonFormat.parser().merge(json, builder); |
| 86 | + featureSets.add(FeatureSet.newBuilder().setSpec(builder.build()).build()); |
| 87 | + } |
| 88 | + return featureSets; |
| 89 | + } |
| 90 | + |
| 91 | + public static List<Store> parseStoreJsonList(List<String> jsonList) |
| 92 | + throws InvalidProtocolBufferException { |
| 93 | + List<Store> stores = new ArrayList<>(); |
| 94 | + for (String json : jsonList) { |
| 95 | + Store.Builder builder = Store.newBuilder(); |
| 96 | + JsonFormat.parser().merge(json, builder); |
| 97 | + stores.add(builder.build()); |
| 98 | + } |
| 99 | + return stores; |
| 100 | + } |
| 101 | +} |
0 commit comments