Skip to content

Commit 46e3e09

Browse files
terryyylimTerence
andauthored
Refactor common module Feature ref (feast-dev#814)
Co-authored-by: Terence <[email protected]>
1 parent 7ce74c6 commit 46e3e09

6 files changed

Lines changed: 28 additions & 18 deletions

File tree

common/src/main/java/feast/common/models/Feature.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,32 @@ public class Feature {
2222

2323
/**
2424
* Accepts FeatureReference object and returns its reference in String
25+
* "featureset_name:feature_name".
26+
*
27+
* @param featureReference {@link FeatureReference}
28+
* @return String format of FeatureReference
29+
*/
30+
public static String getFeatureStringRef(FeatureReference featureReference) {
31+
String ref = featureReference.getName();
32+
if (!featureReference.getFeatureSet().isEmpty()) {
33+
ref = featureReference.getFeatureSet() + ":" + ref;
34+
}
35+
return ref;
36+
}
37+
38+
/**
39+
* Accepts FeatureReference object and returns its reference with project included in String, eg.
2540
* "project/featureset_name:feature_name".
2641
*
2742
* @param featureReference {@link FeatureReference}
28-
* @param ignoreProject Flag whether to return FeatureReference with project name
2943
* @return String format of FeatureReference
3044
*/
31-
public static String getFeatureStringRef(
32-
FeatureReference featureReference, boolean ignoreProject) {
45+
public static String getFeatureStringWithProjectRef(FeatureReference featureReference) {
3346
String ref = featureReference.getName();
3447
if (!featureReference.getFeatureSet().isEmpty()) {
3548
ref = featureReference.getFeatureSet() + ":" + ref;
3649
}
37-
if (!featureReference.getProject().isEmpty() && !ignoreProject) {
50+
if (!featureReference.getProject().isEmpty()) {
3851
ref = featureReference.getProject() + "/" + ref;
3952
}
4053
return ref;

common/src/test/java/feast/common/models/FeaturesTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,8 @@ public void shouldReturnFeatureStringRef() {
8888
.setName(featureSetSpec.getFeatures(0).getName())
8989
.build();
9090

91-
String actualFeatureStringRef = Feature.getFeatureStringRef(featureReference, false);
92-
String actualFeatureIgnoreProjectStringRef =
93-
Feature.getFeatureStringRef(featureReference, true);
91+
String actualFeatureStringRef = Feature.getFeatureStringWithProjectRef(featureReference);
92+
String actualFeatureIgnoreProjectStringRef = Feature.getFeatureStringRef(featureReference);
9493
String expectedFeatureStringRef = "project1/featureSetWithConstraints:feature1";
9594
String expectedFeatureIgnoreProjectStringRef = "featureSetWithConstraints:feature1";
9695

sdk/java/src/main/java/com/gojek/feast/FeastClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public List<Row> getOnlineFeatures(
151151
// Strip project from string Feature References from returned from serving
152152
FeatureReference featureRef =
153153
RequestUtil.parseFeatureRef(fieldName, true).build();
154-
stripFieldName = Feature.getFeatureStringRef(featureRef, true);
154+
stripFieldName = Feature.getFeatureStringRef(featureRef);
155155
row.set(
156156
stripFieldName,
157157
fieldValues.getFieldsMap().get(fieldName),

sdk/java/src/test/java/com/gojek/feast/RequestUtilTest.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ void renderFeatureRef_ShouldReturnFeatureRefString(
7676
.map(ref -> ref.toBuilder().clearProject().build())
7777
.collect(Collectors.toList());
7878
List<String> actual =
79-
input.stream()
80-
.map(ref -> Feature.getFeatureStringRef(ref, true))
81-
.collect(Collectors.toList());
79+
input.stream().map(ref -> Feature.getFeatureStringRef(ref)).collect(Collectors.toList());
8280
assertEquals(expected.size(), actual.size());
8381
for (int i = 0; i < expected.size(); i++) {
8482
assertEquals(expected.get(i), actual.get(i));

serving/src/main/java/feast/serving/service/OnlineServingService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ private static Map<String, Value> unpackValueMap(
175175
Collectors.toMap(
176176
featureRowField -> {
177177
FeatureReference featureRef = nameRefMap.get(featureRowField.getName());
178-
return Feature.getFeatureStringRef(featureRef, false);
178+
return Feature.getFeatureStringWithProjectRef(featureRef);
179179
},
180180
featureRowField -> {
181181
// drop feature values with an age outside feature set's max age.
@@ -188,7 +188,7 @@ private static Map<String, Value> unpackValueMap(
188188
// create empty values for features specified in request but not present in feature row.
189189
Set<String> missingFeatures =
190190
nameRefMap.values().stream()
191-
.map(ref -> Feature.getFeatureStringRef(ref, false))
191+
.map(ref -> Feature.getFeatureStringWithProjectRef(ref))
192192
.collect(Collectors.toSet());
193193
missingFeatures.removeAll(valueMap.keySet());
194194
missingFeatures.forEach(refString -> valueMap.put(refString, Value.newBuilder().build()));

serving/src/main/java/feast/serving/specs/CachedSpecService.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*/
1717
package feast.serving.specs;
1818

19-
import static feast.common.models.Feature.getFeatureStringRef;
19+
import static feast.common.models.Feature.getFeatureStringWithProjectRef;
2020
import static feast.common.models.FeatureSet.getFeatureSetStringRef;
2121
import static java.util.stream.Collectors.groupingBy;
2222

@@ -117,18 +117,18 @@ public List<FeatureSetRequest> getFeatureSets(List<FeatureReference> featureRefe
117117
featureReference -> {
118118
// map feature reference to coresponding feature set name
119119
String fsName =
120-
featureToFeatureSetMapping.get(getFeatureStringRef(featureReference, false));
120+
featureToFeatureSetMapping.get(getFeatureStringWithProjectRef(featureReference));
121121
if (fsName == null) {
122122
throw new SpecRetrievalException(
123123
String.format(
124124
"Unable to find Feature Set for the given Feature Reference: %s",
125-
getFeatureStringRef(featureReference, false)));
125+
getFeatureStringWithProjectRef(featureReference)));
126126
} else if (fsName == FEATURE_SET_CONFLICT_FLAG) {
127127
throw new SpecRetrievalException(
128128
String.format(
129129
"Given Feature Reference is amibigous as it matches multiple Feature Sets: %s."
130130
+ "Please specify a more specific Feature Reference (ie specify the project or feature set)",
131-
getFeatureStringRef(featureReference, false)));
131+
getFeatureStringWithProjectRef(featureReference)));
132132
}
133133
return Pair.of(fsName, featureReference);
134134
})
@@ -291,6 +291,6 @@ private Pair<String, String> generateFeatureToFeatureSetMapping(
291291
featureRef = featureRef.clearFeatureSet();
292292
}
293293
return Pair.of(
294-
getFeatureStringRef(featureRef.build(), false), getFeatureSetStringRef(featureSetSpec));
294+
getFeatureStringWithProjectRef(featureRef.build()), getFeatureSetStringRef(featureSetSpec));
295295
}
296296
}

0 commit comments

Comments
 (0)