2121import static com .google .common .base .Preconditions .checkArgument ;
2222
2323import org .apache .polaris .core .entity .PolarisEntityCore ;
24+ import org .apache .polaris .core .entity .PolarisEntityType ;
25+ import org .apache .polaris .persistence .nosql .api .index .IndexKey ;
26+ import org .apache .polaris .persistence .nosql .coretypes .ObjBase ;
27+ import org .apache .polaris .persistence .nosql .coretypes .acl .GrantsObj ;
2428
2529/**
2630 * Represents the triplet of catalog-ID, entity-ID and type-code plus a reverse-or-key marker.
27- * String representations of this type are used as ACL names and "role" names.
31+ *
32+ * <p>This is intended to construct {@link IndexKey}s for {@link GrantsObj#acls}, which contain the
33+ * "reversed" and "directed" mappings.
34+ *
35+ * <ul>
36+ * <li>"Reversed" ({@code reverseOrKey == true}, role encoding {@code 'r'}) means <em>from</em> a
37+ * grantee or securable.
38+ * <li>"Directed" ({@code reverseOrKey == false}, role encoding {@code 'd'}) means <em>to</em> a
39+ * grantee (no use case for <em>to</em> securable).
40+ * </ul>
41+ *
42+ * <p>String representations of this type are used as ACL names and "role" names.
43+ *
44+ * @param reverseOrKey
45+ * @param catalogId catalog id
46+ * @param id entity id (aka {@link ObjBase#stableId()}
47+ * @param typeCode {@link PolarisEntityType#getCode() entity type code}
2848 */
2949public record GrantTriplet (boolean reverseOrKey , long catalogId , long id , int typeCode ) {
50+
51+ /**
52+ * Constructs a new {@link GrantTriplet} instance for the given entity, with {@link
53+ * #reverseOrKey()} set to {@code true}.
54+ */
55+ public static GrantTriplet forEntity (PolarisEntityCore entity ) {
56+ return new GrantTriplet (true , entity .getCatalogId (), entity .getId (), entity .getTypeCode ());
57+ }
58+
59+ /** Convert to a "directed" grant-triplet, having {@link #reverseOrKey()} set to {@code false}. */
60+ public GrantTriplet asDirected () {
61+ return new GrantTriplet (false , catalogId , id , typeCode );
62+ }
63+
64+ /**
65+ * Constructs the role name, the encoded string representation, for this triplet in the pattern
66+ * {@code [r|d] "/" catalogId "/" id "/" typeCode}
67+ */
68+ public String toRoleName () {
69+ return (reverseOrKey ? "r/" : "d/" ) + catalogId + "/" + id + "/" + typeCode ;
70+ }
71+
72+ /**
73+ * Parses a {@link GrantTriplet#toRoleName()}, expecting exactly the pattern {@code [r|d] "/"
74+ * catalogId "/" id "/" typeCode}.
75+ */
3076 public static GrantTriplet fromRoleName (String roleName ) {
3177 var c0 = roleName .charAt (0 );
3278 checkArgument (roleName .charAt (1 ) == '/' && (c0 == 'r' || c0 == 'd' ));
@@ -42,16 +88,4 @@ public static GrantTriplet fromRoleName(String roleName) {
4288
4389 return new GrantTriplet (reversed , catalogId , id , typeCode );
4490 }
45-
46- public static GrantTriplet forEntity (PolarisEntityCore entity ) {
47- return new GrantTriplet (true , entity .getCatalogId (), entity .getId (), entity .getTypeCode ());
48- }
49-
50- public GrantTriplet asDirected () {
51- return new GrantTriplet (false , catalogId , id , typeCode );
52- }
53-
54- public String toRoleName () {
55- return (reverseOrKey ? "r/" : "d/" ) + catalogId + "/" + id + "/" + typeCode ;
56- }
5791}
0 commit comments