jerryshao commented on code in PR #6751:
URL: https://github.com/apache/gravitino/pull/6751#discussion_r2021482788
##########
core/src/main/java/org/apache/gravitino/connector/PropertiesMetadata.java:
##########
@@ -102,10 +146,73 @@ default Object getOrDefault(Map<String, String>
properties, String propertyName)
* @return the default value object of the property.
*/
default Object getDefaultValue(String propertyName) {
+ PropertyEntry<?> propertyEntry = getPropertyEntry(propertyName);
+
+ return propertyEntry.getDefaultValue();
+ }
+
+ /**
+ * Get the property entry of the property. The non-prefix property entry
will be returned if
+ * exists, otherwise the longest prefix property entry will be returned.
+ *
+ * <p>For example, if there are two property prefix entries "foo." and
"foo.bar", and the property
+ * name is "foo.bar.baz", the entry for "foo.bar" will be returned. If the
property entry
+ * "foo.bar.baz" is defined, it will be returned instead.
+ *
+ * @param propertyName The name of the property.
+ * @return the property entry object of the property.
+ * @throws IllegalArgumentException if the property is not defined.
+ */
+ default PropertyEntry<?> getPropertyEntry(String propertyName) throws
IllegalArgumentException {
if (!containsProperty(propertyName)) {
throw new IllegalArgumentException("Property is not defined: " +
propertyName);
}
- return propertyEntries().get(propertyName).getDefaultValue();
+ try {
+ return getNonPrefixEntry(propertyName);
+ } catch (IllegalArgumentException e) {
+ return getPropertyPrefixEntry(propertyName);
+ }
+ }
+
+ /**
+ * Get the property prefix entry of the property. If there are multiple
property prefix entries
+ * matching the property name, the longest prefix entry will be returned.
+ *
+ * <p>For example, if there are two property prefix entries "foo." and
"foo.bar", and the property
+ * name is "foo.bar.baz", the entry for "foo.bar" will be returned.
+ *
+ * @param propertyName The name of the property.
+ * @return the property prefix entry object of the property.
+ * @throws IllegalArgumentException if the property prefix is not defined.
+ */
+ default PropertyEntry<?> getPropertyPrefixEntry(String propertyName)
+ throws IllegalArgumentException {
+ return propertyEntries().entrySet().stream()
+ .filter(e -> e.getValue().isPrefix() &&
propertyName.startsWith(e.getKey()))
+ // get the longest prefix property
+ .max(Map.Entry.comparingByKey(Comparator.comparingInt(String::length)))
+ .map(Map.Entry::getValue)
+ // should not happen since containsProperty check is done before
+ .orElseThrow(
+ () ->
+ new IllegalArgumentException(
+ "No matching prefix property found for: " + propertyName));
+ }
+
+ /**
+ * Get the non-prefix property entry of the property. That is, the property
entry that is not a
+ * prefix.
+ *
+ * @param propertyName The name of the property.
+ * @return the non-prefix property entry object of the property.
+ * @throws IllegalArgumentException if the property is not defined or is a
prefix.
+ */
+ default PropertyEntry<?> getNonPrefixEntry(String propertyName) throws
IllegalArgumentException {
Review Comment:
Better to use `Optional` instead of throw and catch an exception.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]