Skip to content

Commit 41563cd

Browse files
committed
Add PolicyEntity
1 parent c93717d commit 41563cd

File tree

3 files changed

+167
-1
lines changed

3 files changed

+167
-1
lines changed

polaris-core/src/main/java/org/apache/polaris/core/entity/PolarisEntityType.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ public enum PolarisEntityType {
3434
// generic table is either a view or a real table
3535
TABLE_LIKE(7, NAMESPACE, false, false),
3636
TASK(8, ROOT, false, false),
37-
FILE(9, TABLE_LIKE, false, false);
37+
FILE(9, TABLE_LIKE, false, false),
38+
POLICY(10, NAMESPACE, false, false);
3839

3940
// to efficiently map a code to its corresponding entity type, use a reverse array which
4041
// is initialized below
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.polaris.core.entity;
20+
21+
import com.fasterxml.jackson.annotation.JsonIgnore;
22+
import com.google.common.base.Preconditions;
23+
import org.apache.iceberg.catalog.Namespace;
24+
import org.apache.iceberg.rest.RESTUtil;
25+
import org.apache.polaris.core.policy.PolicyType;
26+
27+
public class PolicyEntity extends PolarisEntity {
28+
29+
public static final String POLICY_TYPE_CODE_KEY = "policy-type-code";
30+
public static final String POLICY_DESCRIPTION_KEY = "policy-description";
31+
public static final String POLICY_VERSION_KEY = "policy-version";
32+
public static final String POLICY_CONTENT_KEY = "policy-content";
33+
34+
PolicyEntity(PolarisBaseEntity sourceEntity) {
35+
super(sourceEntity);
36+
}
37+
38+
public static PolicyEntity of(PolarisBaseEntity sourceEntity) {
39+
if (sourceEntity != null) {
40+
return new PolicyEntity(sourceEntity);
41+
}
42+
43+
return null;
44+
}
45+
46+
@JsonIgnore
47+
public PolicyType getPolicyType() {
48+
return PolicyType.fromCode(getPolicyTypeCode());
49+
}
50+
51+
@JsonIgnore
52+
public int getPolicyTypeCode() {
53+
String policyTypeCode = getPropertiesAsMap().get(POLICY_TYPE_CODE_KEY);
54+
if (policyTypeCode != null) {
55+
return Integer.parseInt(policyTypeCode);
56+
}
57+
58+
return -1;
59+
}
60+
61+
@JsonIgnore
62+
public String getDescription() {
63+
return getPropertiesAsMap().get(POLICY_DESCRIPTION_KEY);
64+
}
65+
66+
@JsonIgnore
67+
public String getContent() {
68+
return getPropertiesAsMap().get(POLICY_CONTENT_KEY);
69+
}
70+
71+
@JsonIgnore
72+
public String getPolicyVersion() {
73+
return getPropertiesAsMap().get(POLICY_VERSION_KEY);
74+
}
75+
76+
public static class Builder extends PolarisEntity.BaseBuilder<PolicyEntity, Builder> {
77+
public Builder(Namespace namespace, String policyName) {
78+
super();
79+
setType(PolarisEntityType.POLICY);
80+
setParentNamespace(namespace);
81+
setName(policyName);
82+
setPolicyVersion(0);
83+
}
84+
85+
public Builder(PolicyEntity original) {
86+
super(original);
87+
}
88+
89+
@Override
90+
public PolicyEntity build() {
91+
Preconditions.checkArgument(
92+
properties.get(POLICY_TYPE_CODE_KEY) != null, "Policy type must be specified");
93+
94+
return new PolicyEntity(buildBase());
95+
}
96+
97+
public Builder setParentNamespace(Namespace namespace) {
98+
if (namespace != null && !namespace.isEmpty()) {
99+
internalProperties.put(
100+
NamespaceEntity.PARENT_NAMESPACE_KEY, RESTUtil.encodeNamespace(namespace));
101+
}
102+
return this;
103+
}
104+
105+
public Builder setPolicyType(PolicyType policyType) {
106+
properties.put(POLICY_TYPE_CODE_KEY, Integer.toString(policyType.getCode()));
107+
return this;
108+
}
109+
110+
public Builder setDescription(String description) {
111+
properties.put(POLICY_DESCRIPTION_KEY, description);
112+
return this;
113+
}
114+
115+
public Builder setPolicyVersion(long version) {
116+
properties.put(POLICY_VERSION_KEY, Long.toString(version));
117+
return this;
118+
}
119+
120+
public Builder setContent(String content) {
121+
properties.put(POLICY_CONTENT_KEY, content);
122+
return this;
123+
}
124+
}
125+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.polaris.core.entity;
20+
21+
import org.apache.iceberg.catalog.Namespace;
22+
import org.apache.polaris.core.policy.PredefinedPolicyType;
23+
import org.assertj.core.api.Assertions;
24+
import org.junit.jupiter.api.Test;
25+
26+
public class PolicyEntityTest {
27+
28+
@Test
29+
public void testBuildPolicyEntity() {
30+
PolicyEntity entity =
31+
new PolicyEntity.Builder(Namespace.of("NS1"), "testPolicy")
32+
.setPolicyType(PredefinedPolicyType.DATA_COMPACTION)
33+
.setContent("test_content")
34+
.setPolicyVersion(0)
35+
.build();
36+
Assertions.assertThat(entity.getPolicyType()).isEqualTo(PredefinedPolicyType.DATA_COMPACTION);
37+
Assertions.assertThat(entity.getPolicyTypeCode()).isEqualTo(0);
38+
Assertions.assertThat(entity.getContent()).isEqualTo("test_content");
39+
}
40+
}

0 commit comments

Comments
 (0)