Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Common module #801

Merged
merged 10 commits into from
Jun 22, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions common/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
### Scratch files ###
woop marked this conversation as resolved.
Show resolved Hide resolved
scratch*

### Local Environment ###
*local*.env

### Gradle ###
.gradle
**/build/
!gradle/wrapper/gradle-wrapper.jar

### STS ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr
/out/

### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/

## Feast Temporary Files ##
/temp/
47 changes: 47 additions & 0 deletions common/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright 2018-2020 The Feast Authors
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ https://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
~
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<artifactId>feast-parent</artifactId>
<groupId>dev.feast</groupId>
<version>${revision}</version>
</parent>

<name>Feast Common</name>
<description>Feast common module with functionality that can be reused</description>
<artifactId>feast-common</artifactId>

<dependencies>
<dependency>
<groupId>dev.feast</groupId>
<artifactId>datatypes-java</artifactId>
<version>${project.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
211 changes: 211 additions & 0 deletions common/src/main/java/feast/common/function/StringUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright 2018-2020 The Feast Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package feast.common.function;

import feast.proto.core.FeatureSetProto.FeatureSetSpec;
import feast.proto.core.FeatureSetReferenceProto.FeatureSetReference;
import feast.proto.core.StoreProto;
import feast.proto.serving.ServingAPIProto.FeatureReference;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class StringUtils {
Copy link
Collaborator

@pyalex pyalex Jun 18, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather have those functions grouped by domain. Also name "StringUtils" doesn't bring clarity here - you can call "utils" almost anything of course :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was expecting this haha, had a hard time thinking for a name so pushed for a review to have you guys help brainstorm 😂

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By domain you mean FeatureSet, Store etc?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My trigger words are

  • Info
  • Spec
  • Util
  • Helper
  • Metadata

Sometimes unavoidable, but we should pay attention when/why they are introduced.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By domain you mean FeatureSet, Store etc?

exactly

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated classes based on domain.


private static String PROJECT_DEFAULT_NAME = "default";

// FeatureSet-related functionality
/**
* Accepts FeatureSetSpec object and returns its reference in String "project/featureset_name".
*
* @param featureSetSpec
* @return String format of FeatureSetReference
*/
public static String getFeatureSetStringRef(FeatureSetSpec featureSetSpec) {
return String.format("%s/%s", featureSetSpec.getProject(), featureSetSpec.getName());
}

/**
* Accepts FeatureSetReference object and returns its reference in String
* "project/featureset_name".
*
* @param featureSetReference
* @return String format of FeatureSetReference
*/
public static String getFeatureSetStringRef(FeatureSetReference featureSetReference) {
return String.format("%s/%s", featureSetReference.getProject(), featureSetReference.getName());
}

// Feature-related functionality
/**
* Accepts FeatureReference object and returns its reference in String
* "project/featureset_name:feature_name".
*
* @param featureReference
* @return String format of FeatureReference
*/
public static String getFeatureStringRef(
FeatureReference featureReference, boolean ignoreProject) {
String ref = featureReference.getName();
if (!featureReference.getFeatureSet().isEmpty()) {
ref = featureReference.getFeatureSet() + ":" + ref;
}
if (!featureReference.getProject().isEmpty() && !ignoreProject) {
ref = featureReference.getProject() + "/" + ref;
}
return ref;
}

// Subscription-related functionality
/**
* Accepts a comma-delimited Subscriptions that is string-formatted and converts it to a list of
* Subscription class objects.
*
* @param subscriptions String formatted Subscriptions, comma delimited.
* @param exclude flag to determine if subscriptions with exclusion flag should be returned
* @return List of Subscription class objects
*/
public static List<StoreProto.Store.Subscription> getSubscriptionsByStr(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe better name "parseSubscriptionFrom"? similarly to protobuf api https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/Parser

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed function to follow protobuf api convention.

String subscriptions, boolean exclude) {
return Arrays.stream(subscriptions.split(","))
.map(subscriptionStr -> convertStringToSubscription(subscriptionStr, exclude))
.collect(Collectors.toList());
}

/**
* Accepts a Subscription class object and returns it in string format
*
* @param sub Subscription class to be converted to string format
* @return String formatted Subscription class
*/
public static String convertSubscriptionToString(StoreProto.Store.Subscription sub) {
if (sub.getName().isEmpty() || sub.getProject().isEmpty()) {
throw new IllegalArgumentException(
String.format("Missing arguments in subscription string: %s", sub.toString()));
}

return String.format("%s:%s:%s", sub.getProject(), sub.getName(), sub.getExclude());
}

/**
* Accepts a exclude parameter to determine whether to return subscriptions that are excluded.
*
* @param sub String formatted Subscription to be converted to Subscription class
* @param exclude flag to determine if subscriptions with exclusion flag should be returned
* @return Subscription class with its respective attributes
*/
public static StoreProto.Store.Subscription convertStringToSubscription(
String sub, boolean exclude) {
if (sub.equals("")) {
return StoreProto.Store.Subscription.newBuilder().build();
}
String[] split = sub.split(":");
if (split.length == 2) {
// Backward compatibility check
return StoreProto.Store.Subscription.newBuilder()
.setProject(split[0])
.setName(split[1])
.setExclude(false)
.build();
}

if (split.length == 3) {
// If exclusion flag is set to true
if (exclude && Boolean.parseBoolean(split[2])) {
return StoreProto.Store.Subscription.newBuilder().build();
}
}
return StoreProto.Store.Subscription.newBuilder()
.setProject(split[0])
.setName(split[1])
.setExclude(Boolean.parseBoolean(split[2]))
.build();
}

/**
* The current use of this function is to determine whether a FeatureRow is subscribed to a
* Featureset.
*
* @param subscriptions List of Subscriptions available in Store
* @param projectName Project name used for matching Subscription's Project
* @param featureSetName Featureset name used for matching Subscription's Featureset
* @return boolean flag to signify if FeatureRow is subscribed to Featureset
*/
public static boolean isSubscribedToFeatureSet(
List<StoreProto.Store.Subscription> subscriptions,
String projectName,
String featureSetName) {
// Case 1: Highest priority check, to exclude all matching subscriptions with excluded flag =
// true
for (StoreProto.Store.Subscription sub : subscriptions) {
// If configuration missing, fail
if (sub.getProject().isEmpty() || sub.getName().isEmpty()) {
throw new IllegalArgumentException(
String.format("Subscription is missing arguments: %s", sub.toString()));
}

String subName = sub.getName();
String subProject = sub.getProject();
if (!sub.getName().contains(".*")) {
subName = subName.replace("*", ".*");
}
if (!sub.getProject().contains(".*")) {
subProject = subProject.replace("*", ".*");
}

// Match feature set name to pattern
Pattern patternName = Pattern.compile(subName);
Pattern patternProject = Pattern.compile(subProject);

// SubCase: Project name and feature set name matches and excluded flag is true
if (patternProject.matcher(projectName).matches()
&& patternName.matcher(featureSetName).matches()
&& sub.getExclude()) {
return false;
}
}

// Case 2: Featureset is not excluded, check if it is included in the current subscriptions
// filteredSubscriptions only contain subscriptions with excluded flag = false
List<StoreProto.Store.Subscription> filteredSubscriptions =
subscriptions.stream().filter(sub -> !sub.getExclude()).collect(Collectors.toList());

for (StoreProto.Store.Subscription filteredSub : filteredSubscriptions) {
// Convert wildcard to regex
String subName = filteredSub.getName();
String subProject = filteredSub.getProject();
if (!filteredSub.getName().contains(".*")) {
subName = subName.replace("*", ".*");
}
if (!filteredSub.getProject().contains(".*")) {
subProject = subProject.replace("*", ".*");
}

// Match feature set name to pattern
Pattern patternName = Pattern.compile(subName);
Pattern patternProject = Pattern.compile(subProject);

// SubCase: Project name and feature set name matches
if (patternProject.matcher(projectName).matches()
&& patternName.matcher(featureSetName).matches()) {
return true;
}
}
return false;
}
}
Loading