You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@polaris.apache.org by di...@apache.org on 2025/12/08 14:07:17 UTC
(polaris) branch main updated: NoSQL: Add metastore types and mappings (#3207)
This is an automated email from the ASF dual-hosted git repository.
dimas pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/polaris.git
The following commit(s) were added to refs/heads/main by this push:
new 90cff771f NoSQL: Add metastore types and mappings (#3207)
90cff771f is described below
commit 90cff771f64fb0c0970311ed6d0eb303afe5c7e1
Author: Robert Stupp <sn...@snazy.de>
AuthorDate: Mon Dec 8 15:07:14 2025 +0100
NoSQL: Add metastore types and mappings (#3207)
Add the NoSQL specific metastore persistence types including the mapping from and to `*Polaris*Entity`.
---
bom/build.gradle.kts | 1 +
gradle/projects.main.properties | 1 +
.../persistence/metastore-types/build.gradle.kts | 54 ++
.../persistence/nosql/coretypes/ContainerObj.java | 51 ++
.../persistence/nosql/coretypes/ObjBase.java | 110 ++++
.../persistence/nosql/coretypes/acl/AclObj.java | 67 +++
.../persistence/nosql/coretypes/acl/GrantsObj.java | 39 ++
.../nosql/coretypes/catalog/CatalogBaseObj.java | 28 ++
.../nosql/coretypes/catalog/CatalogGrantsObj.java | 57 +++
.../nosql/coretypes/catalog/CatalogObj.java | 75 +++
.../nosql/coretypes/catalog/CatalogRoleObj.java | 53 ++
.../nosql/coretypes/catalog/CatalogRolesObj.java | 69 +++
.../nosql/coretypes/catalog/CatalogStateObj.java | 109 ++++
.../nosql/coretypes/catalog/CatalogStatus.java | 51 ++
.../nosql/coretypes/catalog/CatalogStorageObj.java | 55 ++
.../nosql/coretypes/catalog/CatalogType.java | 37 ++
.../nosql/coretypes/catalog/CatalogsObj.java | 68 +++
.../nosql/coretypes/catalog/EntityIdSet.java | 54 ++
.../coretypes/catalog/EntityIdSetSerializer.java | 92 ++++
.../nosql/coretypes/catalog/FileObj.java | 55 ++
.../nosql/coretypes/changes/Change.java | 36 ++
.../nosql/coretypes/changes/ChangeAdd.java | 37 ++
.../nosql/coretypes/changes/ChangeRemove.java | 37 ++
.../nosql/coretypes/changes/ChangeRename.java | 37 ++
.../nosql/coretypes/changes/ChangeRenameBase.java | 26 +
.../nosql/coretypes/changes/ChangeSerializer.java | 97 ++++
.../nosql/coretypes/changes/ChangeType.java | 55 ++
.../coretypes/changes/ChangeTypeIdResolver.java | 81 +++
.../nosql/coretypes/changes/ChangeUpdate.java | 37 ++
.../nosql/coretypes/changes/GenericChange.java | 91 ++++
.../nosql/coretypes/content/ContentObj.java | 42 ++
.../nosql/coretypes/content/GenericTableObj.java | 75 +++
.../coretypes/content/IcebergTableLikeObj.java | 32 ++
.../nosql/coretypes/content/IcebergTableObj.java | 54 ++
.../nosql/coretypes/content/IcebergViewObj.java | 54 ++
.../nosql/coretypes/content/LocalNamespaceObj.java | 55 ++
.../nosql/coretypes/content/NamespaceObj.java | 33 ++
.../content/PolicyAttachableContentObj.java | 31 ++
.../nosql/coretypes/content/PolicyObj.java | 86 ++++
.../nosql/coretypes/content/TableLikeObj.java | 40 ++
.../mapping/BaseCatalogContentMapping.java | 56 +++
.../coretypes/mapping/BaseCatalogMapping.java | 67 +++
.../nosql/coretypes/mapping/BaseMapping.java | 314 ++++++++++++
.../nosql/coretypes/mapping/CatalogMapping.java | 87 ++++
.../coretypes/mapping/CatalogRoleMapping.java | 43 ++
.../nosql/coretypes/mapping/EntityObjMappings.java | 343 +++++++++++++
.../nosql/coretypes/mapping/FileMapping.java | 36 ++
.../nosql/coretypes/mapping/NamespaceMapping.java | 48 ++
.../nosql/coretypes/mapping/PolicyMapping.java | 90 ++++
.../nosql/coretypes/mapping/PrincipalMapping.java | 85 ++++
.../coretypes/mapping/PrincipalRoleMapping.java | 75 +++
.../nosql/coretypes/mapping/RootMapping.java | 38 ++
.../nosql/coretypes/mapping/TableLikeMapping.java | 117 +++++
.../nosql/coretypes/mapping/TaskMapping.java | 144 ++++++
.../nosql/coretypes/principals/PrincipalObj.java | 103 ++++
.../coretypes/principals/PrincipalRoleObj.java | 66 +++
.../coretypes/principals/PrincipalRolesObj.java | 68 +++
.../nosql/coretypes/principals/PrincipalsObj.java | 76 +++
.../nosql/coretypes/realm/ImmediateTaskObj.java | 97 ++++
.../nosql/coretypes/realm/ImmediateTasksObj.java | 68 +++
.../nosql/coretypes/realm/PolicyMapping.java | 52 ++
.../coretypes/realm/PolicyMappingSerializer.java | 114 +++++
.../nosql/coretypes/realm/PolicyMappingsObj.java | 212 ++++++++
.../nosql/coretypes/realm/RealmGrantsObj.java | 57 +++
.../persistence/nosql/coretypes/realm/RootObj.java | 61 +++
.../nosql/coretypes/refs/References.java | 60 +++
...pache.polaris.persistence.nosql.api.obj.ObjType | 45 ++
.../nosql/coretypes/catalog/TestEntityIdSet.java | 97 ++++
.../nosql/coretypes/changes/TestChanges.java | 81 +++
.../nosql/coretypes/mapping/BaseTestMapping.java | 553 +++++++++++++++++++++
.../coretypes/mapping/TestCatalogMapping.java | 130 +++++
.../coretypes/mapping/TestCatalogRoleMapping.java | 83 ++++
.../coretypes/mapping/TestEntityObjMappings.java | 135 +++++
.../nosql/coretypes/mapping/TestFileMapping.java | 72 +++
.../coretypes/mapping/TestNamespaceMapping.java | 84 ++++
.../nosql/coretypes/mapping/TestPolicyMapping.java | 133 +++++
.../coretypes/mapping/TestPrincipalMapping.java | 233 +++++++++
.../mapping/TestPrincipalRoleMapping.java | 91 ++++
.../nosql/coretypes/mapping/TestRootMapping.java | 74 +++
.../coretypes/mapping/TestTableLikeMapping.java | 160 ++++++
.../nosql/coretypes/mapping/TestTaskMapping.java | 150 ++++++
.../nosql/coretypes/realm/TestPolicyMapping.java | 85 ++++
.../src/test/resources/logback-test.xml | 34 ++
83 files changed, 6979 insertions(+)
diff --git a/bom/build.gradle.kts b/bom/build.gradle.kts
index 8daafeeae..eb73c1d86 100644
--- a/bom/build.gradle.kts
+++ b/bom/build.gradle.kts
@@ -62,6 +62,7 @@ dependencies {
api(project(":polaris-persistence-nosql-api"))
api(project(":polaris-persistence-nosql-impl"))
api(project(":polaris-persistence-nosql-benchmark"))
+ api(project(":polaris-persistence-nosql-metastore-types"))
api(project(":polaris-persistence-nosql-correctness"))
api(project(":polaris-persistence-nosql-cdi-common"))
api(project(":polaris-persistence-nosql-cdi-quarkus"))
diff --git a/gradle/projects.main.properties b/gradle/projects.main.properties
index 454c704de..3706c6f13 100644
--- a/gradle/projects.main.properties
+++ b/gradle/projects.main.properties
@@ -77,6 +77,7 @@ polaris-persistence-nosql-realms-store-nosql=persistence/nosql/realms/store-nosq
polaris-persistence-nosql-api=persistence/nosql/persistence/api
polaris-persistence-nosql-impl=persistence/nosql/persistence/impl
polaris-persistence-nosql-benchmark=persistence/nosql/persistence/benchmark
+polaris-persistence-nosql-metastore-types=persistence/nosql/persistence/metastore-types
polaris-persistence-nosql-correctness=persistence/nosql/persistence/correctness
polaris-persistence-nosql-cdi-common=persistence/nosql/persistence/cdi/common
polaris-persistence-nosql-cdi-quarkus=persistence/nosql/persistence/cdi/quarkus
diff --git a/persistence/nosql/persistence/metastore-types/build.gradle.kts b/persistence/nosql/persistence/metastore-types/build.gradle.kts
new file mode 100644
index 000000000..08b5e79d8
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/build.gradle.kts
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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.
+ */
+
+plugins {
+ id("org.kordamp.gradle.jandex")
+ id("polaris-server")
+}
+
+description = "Polaris NoSQL persistence core types"
+
+dependencies {
+ implementation(project(":polaris-core"))
+ implementation(project(":polaris-persistence-nosql-api"))
+ implementation(project(":polaris-idgen-api"))
+ implementation(project(":polaris-persistence-nosql-authz-api"))
+ implementation(project(":polaris-persistence-nosql-varint"))
+
+ implementation(libs.guava)
+ implementation(libs.slf4j.api)
+
+ implementation(platform(libs.jackson.bom))
+ implementation("com.fasterxml.jackson.core:jackson-annotations")
+ implementation("com.fasterxml.jackson.core:jackson-core")
+ implementation("com.fasterxml.jackson.core:jackson-databind")
+ implementation("com.fasterxml.jackson.dataformat:jackson-dataformat-smile")
+
+ compileOnly(libs.smallrye.config.core)
+ compileOnly(platform(libs.quarkus.bom))
+ compileOnly("io.quarkus:quarkus-core")
+
+ compileOnly(project(":polaris-immutables"))
+ annotationProcessor(project(":polaris-immutables", configuration = "processor"))
+
+ compileOnly(libs.jakarta.annotation.api)
+ compileOnly(libs.jakarta.validation.api)
+ compileOnly(libs.jakarta.inject.api)
+ compileOnly(libs.jakarta.enterprise.cdi.api)
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/ContainerObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/ContainerObj.java
new file mode 100644
index 000000000..c9716c693
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/ContainerObj.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes;
+
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+import org.apache.polaris.persistence.nosql.api.index.IndexContainer;
+import org.apache.polaris.persistence.nosql.api.index.IndexKey;
+import org.apache.polaris.persistence.nosql.api.obj.BaseCommitObj;
+import org.apache.polaris.persistence.nosql.api.obj.ObjRef;
+
+public interface ContainerObj extends BaseCommitObj {
+ /** Holds the mapping of fully qualified names to object references. */
+ IndexContainer<ObjRef> nameToObjRef();
+
+ /**
+ * Contains the mapping of {@linkplain ObjBase#stableId() stable IDs} to fully qualified names.
+ *
+ * <p>This index is meant as a temporary construct during the transition from numeric ID-based
+ * parent-child relationships and references to using fully qualified names as the primary
+ * identifier on all APIs (catalogs, catalog state, principals, etc.).
+ */
+ IndexContainer<IndexKey> stableIdToName();
+
+ interface Builder<O extends ContainerObj, B extends ContainerObj.Builder<O, B>>
+ extends BaseCommitObj.Builder<O, B> {
+ @CanIgnoreReturnValue
+ B from(ContainerObj container);
+
+ @CanIgnoreReturnValue
+ B nameToObjRef(IndexContainer<ObjRef> nameToObjRef);
+
+ @CanIgnoreReturnValue
+ B stableIdToName(IndexContainer<IndexKey> stableIdToName);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/ObjBase.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/ObjBase.java
new file mode 100644
index 000000000..36627d74e
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/ObjBase.java
@@ -0,0 +1,110 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+import java.time.Instant;
+import java.util.Map;
+import org.apache.polaris.persistence.nosql.api.obj.Obj;
+import org.immutables.value.Value;
+
+public interface ObjBase extends Obj {
+
+ String name();
+
+ /**
+ * The <em>stable</em> ID for this object, remains the same for each update of the logical object.
+ * Raw persistence model {@link #id()} are unique for each persisted "version", because persisted
+ * objects are immutable.
+ *
+ * <p>This value is constant throughout the lifetime of the object (assigned once but never
+ * changed).
+ */
+ long stableId();
+
+ @JsonInclude(JsonInclude.Include.NON_DEFAULT)
+ @Value.Default
+ default long parentStableId() {
+ return 0L;
+ }
+
+ @Value.Default
+ default int entityVersion() {
+ return 1;
+ }
+
+ Instant createTimestamp();
+
+ Instant updateTimestamp();
+
+ @JsonInclude(JsonInclude.Include.NON_EMPTY)
+ Map<String, String> properties();
+
+ @JsonInclude(JsonInclude.Include.NON_EMPTY)
+ Map<String, String> internalProperties();
+
+ interface Builder<O extends ObjBase, B extends Builder<O, B>> extends Obj.Builder<O, B> {
+
+ @CanIgnoreReturnValue
+ B from(ObjBase from);
+
+ @CanIgnoreReturnValue
+ B stableId(long stableId);
+
+ @CanIgnoreReturnValue
+ B entityVersion(int entityVersion);
+
+ @CanIgnoreReturnValue
+ B name(String name);
+
+ @CanIgnoreReturnValue
+ B parentStableId(long parentStableId);
+
+ @CanIgnoreReturnValue
+ B createTimestamp(Instant createTimestamp);
+
+ @CanIgnoreReturnValue
+ B updateTimestamp(Instant updateTimestamp);
+
+ @CanIgnoreReturnValue
+ B putProperty(String key, String value);
+
+ @CanIgnoreReturnValue
+ B putProperty(Map.Entry<String, ? extends String> entry);
+
+ @CanIgnoreReturnValue
+ B properties(Map<String, ? extends String> entries);
+
+ @CanIgnoreReturnValue
+ B putAllProperties(Map<String, ? extends String> entries);
+
+ @CanIgnoreReturnValue
+ B putInternalProperty(String key, String value);
+
+ @CanIgnoreReturnValue
+ B putInternalProperty(Map.Entry<String, ? extends String> entry);
+
+ @CanIgnoreReturnValue
+ B internalProperties(Map<String, ? extends String> entries);
+
+ @CanIgnoreReturnValue
+ B putAllInternalProperties(Map<String, ? extends String> entries);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/acl/AclObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/acl/AclObj.java
new file mode 100644
index 000000000..e16af0221
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/acl/AclObj.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.acl;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import java.util.OptionalInt;
+import org.apache.polaris.core.entity.PolarisBaseEntity;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.obj.AbstractObjType;
+import org.apache.polaris.persistence.nosql.api.obj.Obj;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.authz.api.Acl;
+import org.apache.polaris.persistence.nosql.coretypes.ObjBase;
+
+/** */
+@PolarisImmutable
+@JsonSerialize(as = ImmutableAclObj.class)
+@JsonDeserialize(as = ImmutableAclObj.class)
+public interface AclObj extends Obj {
+
+ ObjType TYPE = new AclObjType();
+
+ /**
+ * Refers to the {@linkplain PolarisBaseEntity#getId() entity id} / {@linkplain ObjBase#stableId()
+ * stable ID}.
+ */
+ long securableId();
+
+ /** Refers to {@link org.apache.polaris.core.entity.PolarisEntityType}. */
+ @JsonInclude(JsonInclude.Include.NON_ABSENT)
+ OptionalInt securableTypeCode();
+
+ Acl acl();
+
+ @Override
+ default ObjType type() {
+ return TYPE;
+ }
+
+ static ImmutableAclObj.Builder builder() {
+ return ImmutableAclObj.builder();
+ }
+
+ final class AclObjType extends AbstractObjType<AclObj> {
+ public AclObjType() {
+ super("acl", "ACL", AclObj.class);
+ }
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/acl/GrantsObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/acl/GrantsObj.java
new file mode 100644
index 000000000..fc8a982ef
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/acl/GrantsObj.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.acl;
+
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+import org.apache.polaris.persistence.nosql.api.index.IndexContainer;
+import org.apache.polaris.persistence.nosql.api.obj.BaseCommitObj;
+import org.apache.polaris.persistence.nosql.api.obj.ObjRef;
+
+public interface GrantsObj extends BaseCommitObj {
+
+ /** Index of securable keys to {@link AclObj}s. */
+ IndexContainer<ObjRef> acls();
+
+ interface Builder<O extends GrantsObj, B extends BaseCommitObj.Builder<O, B>>
+ extends BaseCommitObj.Builder<O, B> {
+ @CanIgnoreReturnValue
+ B from(GrantsObj container);
+
+ @CanIgnoreReturnValue
+ B acls(IndexContainer<ObjRef> acls);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogBaseObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogBaseObj.java
new file mode 100644
index 000000000..f52e5a121
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogBaseObj.java
@@ -0,0 +1,28 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.catalog;
+
+import org.apache.polaris.persistence.nosql.coretypes.ObjBase;
+
+/** Objects that are contained in a catalog. */
+public interface CatalogBaseObj extends ObjBase {
+
+ interface Builder<O extends CatalogBaseObj, B extends Builder<O, B>>
+ extends ObjBase.Builder<O, B> {}
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogGrantsObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogGrantsObj.java
new file mode 100644
index 000000000..145d759da
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogGrantsObj.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.catalog;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.obj.AbstractObjType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.acl.GrantsObj;
+import org.apache.polaris.persistence.nosql.coretypes.realm.RealmGrantsObj;
+
+/**
+ * Maintains the state of all catalog grants. The current version of this object is maintained via
+ * the reference name pattern {@value RealmGrantsObj#REALM_GRANTS_REF_NAME}, where {@code %d} is to
+ * be replaced with the catalog's {@linkplain CatalogObj#stableId() stable ID}.
+ */
+@PolarisImmutable
+@JsonSerialize(as = ImmutableCatalogGrantsObj.class)
+@JsonDeserialize(as = ImmutableCatalogGrantsObj.class)
+public interface CatalogGrantsObj extends GrantsObj {
+
+ ObjType TYPE = new CatalogGrantsObjType();
+
+ @Override
+ default ObjType type() {
+ return TYPE;
+ }
+
+ static ImmutableCatalogGrantsObj.Builder builder() {
+ return ImmutableCatalogGrantsObj.builder();
+ }
+
+ final class CatalogGrantsObjType extends AbstractObjType<CatalogGrantsObj> {
+ public CatalogGrantsObjType() {
+ super("cat-gts", "Catalog Grants", CatalogGrantsObj.class);
+ }
+ }
+
+ interface Builder extends GrantsObj.Builder<CatalogGrantsObj, Builder> {}
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogObj.java
new file mode 100644
index 000000000..4e7bf226a
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogObj.java
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.catalog;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+import java.util.Optional;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.obj.AbstractObjType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+
+@PolarisImmutable
+@JsonSerialize(as = ImmutableCatalogObj.class)
+@JsonDeserialize(as = ImmutableCatalogObj.class)
+public interface CatalogObj extends CatalogStorageObj {
+ ObjType TYPE = new CatalogObjType();
+
+ CatalogType catalogType();
+
+ @JsonInclude(JsonInclude.Include.NON_ABSENT)
+ Optional<String> defaultBaseLocation();
+
+ CatalogStatus status();
+
+ @Override
+ default ObjType type() {
+ return TYPE;
+ }
+
+ static Builder builder() {
+ return ImmutableCatalogObj.builder();
+ }
+
+ final class CatalogObjType extends AbstractObjType<CatalogObj> {
+ public CatalogObjType() {
+ super("cat", "Catalog", CatalogObj.class);
+ }
+ }
+
+ @SuppressWarnings("unused")
+ interface Builder extends CatalogStorageObj.Builder<CatalogObj, Builder> {
+ @CanIgnoreReturnValue
+ Builder from(CatalogObj from);
+
+ @CanIgnoreReturnValue
+ Builder status(CatalogStatus status);
+
+ @CanIgnoreReturnValue
+ Builder catalogType(CatalogType catalogType);
+
+ @CanIgnoreReturnValue
+ Builder defaultBaseLocation(String defaultBaseLocation);
+
+ @CanIgnoreReturnValue
+ Builder defaultBaseLocation(Optional<String> defaultBaseLocation);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogRoleObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogRoleObj.java
new file mode 100644
index 000000000..da1f83c6a
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogRoleObj.java
@@ -0,0 +1,53 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.catalog;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.obj.AbstractObjType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+
+@PolarisImmutable
+@JsonSerialize(as = ImmutableCatalogRoleObj.class)
+@JsonDeserialize(as = ImmutableCatalogRoleObj.class)
+public interface CatalogRoleObj extends CatalogBaseObj {
+ ObjType TYPE = new CatalogRoleObjType();
+
+ @Override
+ default ObjType type() {
+ return TYPE;
+ }
+
+ static Builder builder() {
+ return ImmutableCatalogRoleObj.builder();
+ }
+
+ final class CatalogRoleObjType extends AbstractObjType<CatalogRoleObj> {
+ public CatalogRoleObjType() {
+ super("cat-r", "Catalog Role", CatalogRoleObj.class);
+ }
+ }
+
+ interface Builder extends CatalogBaseObj.Builder<CatalogRoleObj, Builder> {
+ @CanIgnoreReturnValue
+ Builder from(CatalogRoleObj from);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogRolesObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogRolesObj.java
new file mode 100644
index 000000000..099a932f9
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogRolesObj.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.catalog;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.index.IndexContainer;
+import org.apache.polaris.persistence.nosql.api.index.IndexKey;
+import org.apache.polaris.persistence.nosql.api.obj.AbstractObjType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjRef;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.ContainerObj;
+
+/**
+ * Maintains the state of all catalog roles. The current version of this object is maintained via
+ * the reference name pattern {@value #CATALOG_ROLES_REF_NAME_PATTERN}, where {@code %d} is to be
+ * replaced with the catalog's {@linkplain CatalogObj#stableId() stable ID}.
+ */
+@PolarisImmutable
+@JsonSerialize(as = ImmutableCatalogRolesObj.class)
+@JsonDeserialize(as = ImmutableCatalogRolesObj.class)
+public interface CatalogRolesObj extends ContainerObj {
+
+ String CATALOG_ROLES_REF_NAME_PATTERN = "cat/%d/roles";
+
+ ObjType TYPE = new CatalogRolesObjType();
+
+ /** Mapping of catalog role names to catalog role objects. */
+ @Override
+ IndexContainer<ObjRef> nameToObjRef();
+
+ // overridden only for posterity, no technical reason
+ @Override
+ IndexContainer<IndexKey> stableIdToName();
+
+ @Override
+ default ObjType type() {
+ return TYPE;
+ }
+
+ static ImmutableCatalogRolesObj.Builder builder() {
+ return ImmutableCatalogRolesObj.builder();
+ }
+
+ final class CatalogRolesObjType extends AbstractObjType<CatalogRolesObj> {
+ public CatalogRolesObjType() {
+ super("cat-rls", "Catalog Roles", CatalogRolesObj.class);
+ }
+ }
+
+ interface Builder extends ContainerObj.Builder<CatalogRolesObj, Builder> {}
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogStateObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogStateObj.java
new file mode 100644
index 000000000..fecd77813
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogStateObj.java
@@ -0,0 +1,109 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.catalog;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import java.util.Optional;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.index.IndexContainer;
+import org.apache.polaris.persistence.nosql.api.index.IndexKey;
+import org.apache.polaris.persistence.nosql.api.obj.AbstractObjType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjRef;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.ContainerObj;
+import org.apache.polaris.persistence.nosql.coretypes.ObjBase;
+import org.apache.polaris.persistence.nosql.coretypes.changes.Change;
+
+/**
+ * Holds the state of all catalog entities. The current version of this object is maintained via the
+ * reference name pattern {@value #CATALOG_STATE_REF_NAME_PATTERN}, where {@code %d} is to be
+ * replaced with the catalog's {@linkplain CatalogObj#stableId() stable ID}.
+ */
+@PolarisImmutable
+@JsonSerialize(as = ImmutableCatalogStateObj.class)
+@JsonDeserialize(as = ImmutableCatalogStateObj.class)
+public interface CatalogStateObj extends ContainerObj {
+
+ String CATALOG_STATE_REF_NAME_PATTERN = "cat/%d/heads/main";
+
+ ObjType TYPE = new CatalogStatesObjType();
+
+ /**
+ * Mapping of all entity names to catalog entities like namespaces, tables or views, which are
+ * accessible from this commit.
+ *
+ * <p>Think of this index as a {@code Map<}{@link IndexKey IndexKey}{@code ,}{@link ObjRef}{@code
+ * >}.
+ *
+ * <p>This index represents the current state of the catalog without any information about the
+ * change(s) that led to this state. Change information is maintained {@linkplain #changes()
+ * separately}.
+ *
+ * <p>Maintaining the {@linkplain #changes() changes} separately has the advantage that the
+ * storage needed for this index is not "wasted" with usually unneeded change information.
+ */
+ @Override
+ IndexContainer<ObjRef> nameToObjRef();
+
+ // overridden only for posterity, no technical reason
+ @Override
+ IndexContainer<IndexKey> stableIdToName();
+
+ /**
+ * Contains detailed information about the changes performed in this particular commit.
+ *
+ * <p>The index here is used to provide literally only the changes, rather a logical
+ * representation than an exact 1:1 mapping of a {@link IndexKey} to some change.
+ *
+ * <p>There is no guarantee that all technically changed keys are mentioned individually in this
+ * index. Whether a change is contained depends on functional, not technical requirements.
+ *
+ * <p>Changes that affect many entities (bulk updates) may also be only recorded once. For
+ * example, renaming a namespace with implicit rename of the contained entities might only be
+ * mentioned once for the rename of the namespace, not for all contained entities.
+ */
+ @JsonInclude(JsonInclude.Include.NON_ABSENT)
+ Optional<IndexContainer<Change>> changes();
+
+ /**
+ * Index of base-locations to {@link ObjBase#stableId() entity-IDs}. There can be multiple IDs for
+ * a single location if overlapping locations are allowed.
+ */
+ @JsonInclude(JsonInclude.Include.NON_ABSENT)
+ Optional<IndexContainer<EntityIdSet>> locations();
+
+ @Override
+ default ObjType type() {
+ return TYPE;
+ }
+
+ static ImmutableCatalogStateObj.Builder builder() {
+ return ImmutableCatalogStateObj.builder();
+ }
+
+ final class CatalogStatesObjType extends AbstractObjType<CatalogStateObj> {
+ public CatalogStatesObjType() {
+ super("cat-st", "Catalog States", CatalogStateObj.class);
+ }
+ }
+
+ interface Builder extends ContainerObj.Builder<CatalogStateObj, Builder> {}
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogStatus.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogStatus.java
new file mode 100644
index 000000000..cdaf4149d
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogStatus.java
@@ -0,0 +1,51 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.catalog;
+
+public enum CatalogStatus {
+ /**
+ * The initial state of a catalog is "created", which means that the catalog name is reserved, but
+ * the catalog is not yet usable. This state can transition to {@link #ACTIVE} or {@link
+ * #INACTIVE} or the catalog can be directly deleted.
+ */
+ CREATED,
+ /**
+ * When a catalog is fully set up, its state is "active". This state can only transition to {@link
+ * #INACTIVE}.
+ */
+ ACTIVE,
+ /**
+ * An {@link #ACTIVE} catalog can be put into "inactive" state, which means that the catalog
+ * cannot be used, but it can be put back into {@link #ACTIVE} state.
+ */
+ INACTIVE,
+ /**
+ * An {@link #INACTIVE} catalog can be put into "purging" state, which means that the catalog's
+ * data is being purged from the persistence database. This is next to the final and terminal
+ * state {@link #PURGED} of a catalog. Once all data of the catalog has been purged, it must at
+ * least be set into {@link #PURGED} status or be entirely removed.
+ */
+ PURGING,
+ /**
+ * "Purged" is the terminal state of every catalog. A purged catalog can be safely deleted. The
+ * difference between a "purged" catalog and a non-existing (deleted) catalog is that the name of
+ * a purged catalog name cannot be (re)used.
+ */
+ PURGED,
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogStorageObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogStorageObj.java
new file mode 100644
index 000000000..a6111117e
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogStorageObj.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.catalog;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+import java.util.Optional;
+import org.apache.polaris.core.storage.PolarisStorageConfigurationInfo;
+import org.apache.polaris.persistence.nosql.coretypes.ObjBase;
+
+/** Objects that hold storage configuration information. */
+public interface CatalogStorageObj extends ObjBase {
+
+ @JsonInclude(JsonInclude.Include.NON_ABSENT)
+ Optional<PolarisStorageConfigurationInfo> storageConfigurationInfo();
+
+ @JsonInclude(JsonInclude.Include.NON_ABSENT)
+ Optional<String> storageIntegrationIdentifier();
+
+ interface Builder<O extends CatalogStorageObj, B extends Builder<O, B>>
+ extends ObjBase.Builder<O, B> {
+
+ @CanIgnoreReturnValue
+ B from(CatalogStorageObj from);
+
+ @CanIgnoreReturnValue
+ B storageConfigurationInfo(PolarisStorageConfigurationInfo storageConfigurationInfo);
+
+ @CanIgnoreReturnValue
+ B storageConfigurationInfo(
+ Optional<? extends PolarisStorageConfigurationInfo> storageConfigurationInfo);
+
+ @CanIgnoreReturnValue
+ B storageIntegrationIdentifier(String storageIntegrationIdentifier);
+
+ @CanIgnoreReturnValue
+ B storageIntegrationIdentifier(Optional<String> storageIntegrationIdentifier);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogType.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogType.java
new file mode 100644
index 000000000..f51ebfce0
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogType.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.catalog;
+
+import com.fasterxml.jackson.annotation.JsonValue;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.immutables.value.Value;
+
+@PolarisImmutable
+@JsonSerialize(as = ImmutableCatalogType.class)
+@JsonDeserialize(as = ImmutableCatalogType.class)
+public interface CatalogType {
+ @JsonValue
+ @Value.Parameter
+ String name();
+
+ CatalogType INTERNAL = ImmutableCatalogType.of("INTERNAL");
+ CatalogType EXTERNAL = ImmutableCatalogType.of("EXTERNAL");
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogsObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogsObj.java
new file mode 100644
index 000000000..99001d39c
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/CatalogsObj.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.catalog;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.index.IndexContainer;
+import org.apache.polaris.persistence.nosql.api.index.IndexKey;
+import org.apache.polaris.persistence.nosql.api.obj.AbstractObjType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjRef;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.ContainerObj;
+
+/**
+ * Maintains the mapping of all catalogs by name to {@link CatalogObj}s. The current version of this
+ * object is maintained via the reference {@value #CATALOGS_REF_NAME}.
+ */
+@PolarisImmutable
+@JsonSerialize(as = ImmutableCatalogsObj.class)
+@JsonDeserialize(as = ImmutableCatalogsObj.class)
+public interface CatalogsObj extends ContainerObj {
+
+ String CATALOGS_REF_NAME = "catalogs";
+
+ ObjType TYPE = new CatalogsObjType();
+
+ /** Mapping of catalog names to catalog objects. */
+ @Override
+ IndexContainer<ObjRef> nameToObjRef();
+
+ // overridden only for posterity, no technical reason
+ @Override
+ IndexContainer<IndexKey> stableIdToName();
+
+ @Override
+ default ObjType type() {
+ return TYPE;
+ }
+
+ static ImmutableCatalogsObj.Builder builder() {
+ return ImmutableCatalogsObj.builder();
+ }
+
+ final class CatalogsObjType extends AbstractObjType<CatalogsObj> {
+ public CatalogsObjType() {
+ super("cats", "Catalogs", CatalogsObj.class);
+ }
+ }
+
+ interface Builder extends ContainerObj.Builder<CatalogsObj, Builder> {}
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/EntityIdSet.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/EntityIdSet.java
new file mode 100644
index 000000000..9b5408993
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/EntityIdSet.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.catalog;
+
+import java.util.Set;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.index.IndexValueSerializer;
+import org.immutables.value.Value;
+
+/**
+ * Container for a set of positive long values.
+ *
+ * <p>An empty {@code EntityIdSet} and {@code EntityIdSet==null} are treated the same, both are
+ * deserialized as {@code null}.
+ */
+@PolarisImmutable
+public interface EntityIdSet {
+ IndexValueSerializer<EntityIdSet> ENTITY_ID_SET_SERIALIZER = new EntityIdSetSerializer();
+
+ @Value.Parameter
+ Set<Long> entityIds();
+
+ @Value.Check
+ default void check() {
+ entityIds()
+ .forEach(
+ id -> {
+ if (id < 0) {
+ throw new IllegalStateException("Entity ID must not be negative: " + id);
+ }
+ });
+ }
+
+ static EntityIdSet entityIdSet(Set<Long> entityIds) {
+ return ImmutableEntityIdSet.of(entityIds);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/EntityIdSetSerializer.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/EntityIdSetSerializer.java
new file mode 100644
index 000000000..148b929e1
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/EntityIdSetSerializer.java
@@ -0,0 +1,92 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.catalog;
+
+import static org.apache.polaris.persistence.varint.VarInt.putVarInt;
+import static org.apache.polaris.persistence.varint.VarInt.readVarInt;
+import static org.apache.polaris.persistence.varint.VarInt.readVarLong;
+import static org.apache.polaris.persistence.varint.VarInt.skipVarInt;
+import static org.apache.polaris.persistence.varint.VarInt.varIntLen;
+
+import jakarta.annotation.Nonnull;
+import jakarta.annotation.Nullable;
+import java.nio.ByteBuffer;
+import org.apache.polaris.persistence.nosql.api.index.IndexValueSerializer;
+
+final class EntityIdSetSerializer implements IndexValueSerializer<EntityIdSet> {
+ private static final byte[] NULL;
+
+ static {
+ var buffer = putVarInt(ByteBuffer.allocate(1), 0).flip();
+ NULL = new byte[buffer.remaining()];
+ buffer.get(NULL);
+ }
+
+ @Nonnull
+ @Override
+ public ByteBuffer serialize(@Nullable EntityIdSet value, @Nonnull ByteBuffer target) {
+ if (value == null) {
+ return target.put(NULL);
+ }
+ var longs = value.entityIds();
+ var num = longs.size();
+ putVarInt(target, num);
+ for (var l : longs) {
+ putVarInt(target, l);
+ }
+ return target;
+ }
+
+ @Override
+ public int serializedSize(@Nullable EntityIdSet value) {
+ if (value == null || value.entityIds().isEmpty()) {
+ return NULL.length;
+ }
+ var longs = value.entityIds();
+ var num = longs.size();
+ var size = varIntLen(num);
+ for (var l : longs) {
+ size += varIntLen(l);
+ }
+ return size;
+ }
+
+ @Nullable
+ @Override
+ public EntityIdSet deserialize(@Nonnull ByteBuffer buffer) {
+ var num = readVarInt(buffer);
+ if (num == 0) {
+ return null;
+ }
+ var b = ImmutableEntityIdSet.builder();
+ for (int i = 0; i < num; i++) {
+ b.addEntityId(readVarLong(buffer));
+ }
+ return b.build();
+ }
+
+ @Override
+ public void skip(@Nonnull ByteBuffer buffer) {
+ var num = readVarInt(buffer);
+ for (int i = 0; i < num; i++) {
+ skipVarInt(buffer);
+ }
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/FileObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/FileObj.java
new file mode 100644
index 000000000..838fcfbb8
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/catalog/FileObj.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.catalog;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.obj.AbstractObjType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.ObjBase;
+
+@PolarisImmutable
+@JsonSerialize(as = ImmutableFileObj.class)
+@JsonDeserialize(as = ImmutableFileObj.class)
+public interface FileObj extends ObjBase {
+ ObjType TYPE = new FileObjType();
+
+ @Override
+ default ObjType type() {
+ return TYPE;
+ }
+
+ static Builder builder() {
+ return ImmutableFileObj.builder();
+ }
+
+ final class FileObjType extends AbstractObjType<FileObj> {
+ public FileObjType() {
+ super("file", "File", FileObj.class);
+ }
+ }
+
+ interface Builder extends ObjBase.Builder<FileObj, Builder> {
+
+ @CanIgnoreReturnValue
+ Builder from(FileObj from);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/Change.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/Change.java
new file mode 100644
index 000000000..99db92126
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/Change.java
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.changes;
+
+import com.fasterxml.jackson.annotation.JsonIgnore;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver;
+import org.apache.polaris.persistence.nosql.api.index.IndexValueSerializer;
+import org.immutables.value.Value;
+
+@JsonTypeIdResolver(ChangeTypeIdResolver.class)
+@JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, property = "type", visible = true)
+public interface Change {
+ IndexValueSerializer<Change> CHANGE_SERIALIZER = new ChangeSerializer();
+
+ @Value.Redacted
+ @JsonIgnore
+ // must use 'get*' here, otherwise the property won't be properly "wired" to be the type info
+ ChangeType getType();
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeAdd.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeAdd.java
new file mode 100644
index 000000000..a20094df7
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeAdd.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.changes;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import org.apache.polaris.immutables.PolarisImmutable;
+
+@PolarisImmutable
+@JsonSerialize(as = ImmutableChangeAdd.class)
+@JsonDeserialize(as = ImmutableChangeAdd.class)
+public interface ChangeAdd extends Change {
+ @Override
+ default ChangeType getType() {
+ return ChangeType.ADD;
+ }
+
+ static ImmutableChangeAdd.Builder builder() {
+ return ImmutableChangeAdd.builder();
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeRemove.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeRemove.java
new file mode 100644
index 000000000..489d9982b
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeRemove.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.changes;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import org.apache.polaris.immutables.PolarisImmutable;
+
+@PolarisImmutable
+@JsonSerialize(as = ImmutableChangeRemove.class)
+@JsonDeserialize(as = ImmutableChangeRemove.class)
+public interface ChangeRemove extends Change {
+ @Override
+ default ChangeType getType() {
+ return ChangeType.REMOVE;
+ }
+
+ static ImmutableChangeRemove.Builder builder() {
+ return ImmutableChangeRemove.builder();
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeRename.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeRename.java
new file mode 100644
index 000000000..0ddbce342
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeRename.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.changes;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import org.apache.polaris.immutables.PolarisImmutable;
+
+@PolarisImmutable
+@JsonSerialize(as = ImmutableChangeRename.class)
+@JsonDeserialize(as = ImmutableChangeRename.class)
+public interface ChangeRename extends ChangeRenameBase {
+ @Override
+ default ChangeType getType() {
+ return ChangeType.RENAME;
+ }
+
+ static ImmutableChangeRename.Builder builder() {
+ return ImmutableChangeRename.builder();
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeRenameBase.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeRenameBase.java
new file mode 100644
index 000000000..24b639d6c
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeRenameBase.java
@@ -0,0 +1,26 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.changes;
+
+import org.apache.polaris.persistence.nosql.api.index.IndexKey;
+
+public interface ChangeRenameBase extends Change {
+
+ IndexKey renameFrom();
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeSerializer.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeSerializer.java
new file mode 100644
index 000000000..3ed209d76
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeSerializer.java
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.changes;
+
+import static org.apache.polaris.persistence.varint.VarInt.putVarInt;
+import static org.apache.polaris.persistence.varint.VarInt.readVarInt;
+import static org.apache.polaris.persistence.varint.VarInt.varIntLen;
+
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.util.ByteBufferBackedInputStream;
+import com.fasterxml.jackson.dataformat.smile.databind.SmileMapper;
+import com.google.common.io.CountingOutputStream;
+import com.google.common.primitives.Ints;
+import jakarta.annotation.Nonnull;
+import jakarta.annotation.Nullable;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.ByteBuffer;
+import org.apache.polaris.persistence.nosql.api.index.IndexValueSerializer;
+import org.apache.polaris.persistence.nosql.api.obj.ObjRef;
+
+/**
+ * Index element value serializer for {@link Change} objects.
+ *
+ * <p>Delegates to the rather "expensive" and "verbose" Jackson/Smile serialization, in contrast to
+ * the space-optimized {@link ObjRef#OBJ_REF_SERIALIZER}. The reason for that implementation choice
+ * is that change serialization needs to be rather flexible, but also because space efficiency is
+ * not really a concern for the set of changes that have been done within a commit - there is
+ * usually just one changed entity per commit.
+ */
+final class ChangeSerializer implements IndexValueSerializer<Change> {
+ static ObjectMapper MAPPER =
+ new SmileMapper()
+ .findAndRegisterModules()
+ .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
+
+ @Override
+ public int serializedSize(@Nullable Change value) {
+ try (var out = new CountingOutputStream(OutputStream.nullOutputStream())) {
+ MAPPER.writeValue(out, value);
+ var size = out.getCount();
+ size += varIntLen(size);
+ return Ints.checkedCast(size);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Nonnull
+ @Override
+ public ByteBuffer serialize(@Nullable Change value, @Nonnull ByteBuffer target) {
+ try {
+ var bytes = MAPPER.writeValueAsBytes(value);
+ putVarInt(target, bytes.length);
+ target.put(bytes);
+ return target;
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Nullable
+ @Override
+ public Change deserialize(@Nonnull ByteBuffer buffer) {
+ try {
+ var len = readVarInt(buffer);
+ var readBuf = buffer.duplicate().limit(buffer.position() + len);
+ buffer.position(buffer.position() + len);
+ return MAPPER.readValue(new ByteBufferBackedInputStream(readBuf), Change.class);
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ @Override
+ public void skip(@Nonnull ByteBuffer buffer) {
+ var len = readVarInt(buffer);
+ buffer.position(buffer.position() + len);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeType.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeType.java
new file mode 100644
index 000000000..a56041346
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeType.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.changes;
+
+import com.fasterxml.jackson.annotation.JsonValue;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import java.util.Map;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.immutables.value.Value;
+
+@PolarisImmutable
+@JsonDeserialize(as = ImmutableChangeType.class)
+@JsonSerialize(as = ImmutableChangeType.class)
+public interface ChangeType {
+ ChangeType ADD = valueOf("add");
+ ChangeType UPDATE = valueOf("update");
+ ChangeType REMOVE = valueOf("remove");
+ ChangeType RENAME = valueOf("rename");
+
+ Map<String, Class<? extends Change>> TYPE_MAP =
+ Map.of(
+ ADD.name(),
+ ChangeAdd.class,
+ UPDATE.name(),
+ ChangeUpdate.class,
+ REMOVE.name(),
+ ChangeRemove.class,
+ RENAME.name(),
+ ChangeRename.class);
+
+ @JsonValue
+ @Value.Parameter
+ String name();
+
+ static ChangeType valueOf(String name) {
+ return ImmutableChangeType.of(name);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeTypeIdResolver.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeTypeIdResolver.java
new file mode 100644
index 000000000..f3c82c4c5
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeTypeIdResolver.java
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.changes;
+
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import com.fasterxml.jackson.databind.DatabindContext;
+import com.fasterxml.jackson.databind.JavaType;
+import com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase;
+import java.util.Locale;
+
+final class ChangeTypeIdResolver extends TypeIdResolverBase {
+
+ private JavaType baseType;
+
+ public ChangeTypeIdResolver() {}
+
+ @Override
+ public void init(JavaType bt) {
+ baseType = bt;
+ }
+
+ @Override
+ public String idFromValue(Object value) {
+ return getId(value);
+ }
+
+ @Override
+ public String idFromValueAndType(Object value, Class<?> suggestedType) {
+ return getId(value);
+ }
+
+ @Override
+ public JsonTypeInfo.Id getMechanism() {
+ return JsonTypeInfo.Id.CUSTOM;
+ }
+
+ private String getId(Object value) {
+ if (value instanceof Change change) {
+ return change.getType().name();
+ }
+
+ return null;
+ }
+
+ @Override
+ public JavaType typeFromId(DatabindContext context, String id) {
+ var idLower = id.toLowerCase(Locale.ROOT);
+ var asType = ChangeType.TYPE_MAP.get(idLower);
+ if (asType == null) {
+ return context.constructSpecializedType(baseType, GenericChange.class);
+ }
+ if (baseType.getRawClass().isAssignableFrom(asType)) {
+ return context.constructSpecializedType(baseType, asType);
+ }
+
+ // This is rather a "test-only" code path, but it might happen in real life as well, when
+ // calling the ObjectMapper with a "too specific" type and not just Change.class.
+ // So we can get here, for example, if the baseType (induced by the type passed to
+ // ObjectMapper), is GenericChange.class, but the type is a "well known" type like
+ // ChangeRename.class.
+ @SuppressWarnings("unchecked")
+ var concrete = (Class<? extends Change>) baseType.getRawClass();
+ return context.constructSpecializedType(baseType, concrete);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeUpdate.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeUpdate.java
new file mode 100644
index 000000000..84bd2148b
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/ChangeUpdate.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.changes;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import org.apache.polaris.immutables.PolarisImmutable;
+
+@PolarisImmutable
+@JsonSerialize(as = ImmutableChangeUpdate.class)
+@JsonDeserialize(as = ImmutableChangeUpdate.class)
+public interface ChangeUpdate extends Change {
+ @Override
+ default ChangeType getType() {
+ return ChangeType.UPDATE;
+ }
+
+ static ImmutableChangeUpdate.Builder builder() {
+ return ImmutableChangeUpdate.builder();
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/GenericChange.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/GenericChange.java
new file mode 100644
index 000000000..fe3f7754c
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/changes/GenericChange.java
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.changes;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.annotation.JsonUnwrapped;
+import com.fasterxml.jackson.core.JsonGenerator;
+import com.fasterxml.jackson.core.JsonParser;
+import com.fasterxml.jackson.databind.DeserializationContext;
+import com.fasterxml.jackson.databind.JsonDeserializer;
+import com.fasterxml.jackson.databind.JsonSerializer;
+import com.fasterxml.jackson.databind.SerializerProvider;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
+import java.io.IOException;
+import java.util.Map;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.immutables.value.Value;
+
+/**
+ * "Generic" change, only to be used as a fallback when hitting an unknown change type during
+ * deserialization.
+ */
+@PolarisImmutable
+@JsonSerialize(using = GenericChange.GenericChangeInfoSerializer.class)
+@JsonDeserialize(using = GenericChange.GenericChangeInfoDeserializer.class)
+public interface GenericChange extends Change {
+ @Override
+ @Value.Parameter
+ ChangeType getType();
+
+ @JsonInclude(JsonInclude.Include.NON_EMPTY)
+ @JsonUnwrapped
+ @Value.Parameter
+ Map<String, Object> getAttributes();
+
+ final class GenericChangeInfoSerializer extends JsonSerializer<GenericChange> {
+
+ @Override
+ public void serializeWithType(
+ GenericChange value,
+ JsonGenerator gen,
+ SerializerProvider serializers,
+ TypeSerializer typeSer)
+ throws IOException {
+ gen.writeStartObject();
+ gen.writeStringField("type", value.getType().name());
+ var attributes = value.getAttributes();
+ if (attributes != null) {
+ for (var entry : attributes.entrySet()) {
+ gen.writeFieldName(entry.getKey());
+ gen.writeObject(entry.getValue());
+ }
+ }
+ gen.writeEndObject();
+ }
+
+ @Override
+ public void serialize(GenericChange value, JsonGenerator gen, SerializerProvider serializers) {
+ throw new UnsupportedOperationException();
+ }
+ }
+
+ final class GenericChangeInfoDeserializer extends JsonDeserializer<GenericChange> {
+
+ @SuppressWarnings("unchecked")
+ @Override
+ public GenericChange deserialize(JsonParser p, DeserializationContext ctxt) throws IOException {
+ var all = p.readValueAs(Map.class);
+ var type = (String) all.remove("type");
+ return ImmutableGenericChange.of(ChangeType.valueOf(type), all);
+ }
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/ContentObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/ContentObj.java
new file mode 100644
index 000000000..8fdeb2701
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/ContentObj.java
@@ -0,0 +1,42 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.content;
+
+import com.fasterxml.jackson.annotation.JsonFormat;
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+import java.time.Instant;
+import java.util.Optional;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogBaseObj;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogStorageObj;
+
+/** Base for all catalog content objects, including namespaces, tables, views, etc. */
+public interface ContentObj extends CatalogStorageObj, CatalogBaseObj {
+
+ @JsonInclude(JsonInclude.Include.NON_ABSENT)
+ @JsonFormat(shape = JsonFormat.Shape.NUMBER)
+ Optional<Instant> lastNotificationTimestamp();
+
+ interface Builder<O extends ContentObj, B extends Builder<O, B>>
+ extends CatalogStorageObj.Builder<O, B>, CatalogBaseObj.Builder<O, B> {
+
+ @CanIgnoreReturnValue
+ B from(ContentObj from);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/GenericTableObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/GenericTableObj.java
new file mode 100644
index 000000000..68079c944
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/GenericTableObj.java
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.content;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+import java.util.Optional;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.obj.AbstractObjType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+
+@PolarisImmutable
+@JsonSerialize(as = ImmutableGenericTableObj.class)
+@JsonDeserialize(as = ImmutableGenericTableObj.class)
+public interface GenericTableObj extends TableLikeObj {
+ ObjType TYPE = new GenericTableObjType();
+
+ @Override
+ default ObjType type() {
+ return TYPE;
+ }
+
+ @JsonInclude(JsonInclude.Include.NON_ABSENT)
+ Optional<String> format();
+
+ @JsonInclude(JsonInclude.Include.NON_ABSENT)
+ Optional<String> doc();
+
+ static Builder builder() {
+ return ImmutableGenericTableObj.builder();
+ }
+
+ final class GenericTableObjType extends AbstractObjType<GenericTableObj> {
+ public GenericTableObjType() {
+ super("gen-tab", "GenericTable", GenericTableObj.class);
+ }
+ }
+
+ @SuppressWarnings("unused")
+ interface Builder extends TableLikeObj.Builder<GenericTableObj, Builder> {
+
+ @CanIgnoreReturnValue
+ Builder from(GenericTableObj from);
+
+ @CanIgnoreReturnValue
+ Builder format(String format);
+
+ @CanIgnoreReturnValue
+ Builder format(Optional<String> format);
+
+ @CanIgnoreReturnValue
+ Builder doc(String doc);
+
+ @CanIgnoreReturnValue
+ Builder doc(Optional<String> doc);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/IcebergTableLikeObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/IcebergTableLikeObj.java
new file mode 100644
index 000000000..0ac986b8a
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/IcebergTableLikeObj.java
@@ -0,0 +1,32 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.content;
+
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+
+/** Base for all Iceberg specific catalog content objects like Iceberg tables and Iceberg views. */
+public interface IcebergTableLikeObj extends TableLikeObj {
+
+ interface Builder<O extends IcebergTableLikeObj, B extends Builder<O, B>>
+ extends TableLikeObj.Builder<O, B> {
+
+ @CanIgnoreReturnValue
+ B from(IcebergTableLikeObj from);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/IcebergTableObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/IcebergTableObj.java
new file mode 100644
index 000000000..e74884611
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/IcebergTableObj.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.content;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.obj.AbstractObjType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+
+@PolarisImmutable
+@JsonSerialize(as = ImmutableIcebergTableObj.class)
+@JsonDeserialize(as = ImmutableIcebergTableObj.class)
+public interface IcebergTableObj extends IcebergTableLikeObj {
+ ObjType TYPE = new IcebergTableObjType();
+
+ @Override
+ default ObjType type() {
+ return TYPE;
+ }
+
+ static Builder builder() {
+ return ImmutableIcebergTableObj.builder();
+ }
+
+ final class IcebergTableObjType extends AbstractObjType<IcebergTableObj> {
+ public IcebergTableObjType() {
+ super("i-t", "Iceberg Table", IcebergTableObj.class);
+ }
+ }
+
+ interface Builder extends IcebergTableLikeObj.Builder<IcebergTableObj, Builder> {
+
+ @CanIgnoreReturnValue
+ Builder from(IcebergTableObj from);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/IcebergViewObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/IcebergViewObj.java
new file mode 100644
index 000000000..094b2c3b7
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/IcebergViewObj.java
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.content;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.obj.AbstractObjType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+
+@PolarisImmutable
+@JsonSerialize(as = ImmutableIcebergViewObj.class)
+@JsonDeserialize(as = ImmutableIcebergViewObj.class)
+public interface IcebergViewObj extends IcebergTableLikeObj {
+ ObjType TYPE = new IcebergViewObjType();
+
+ @Override
+ default ObjType type() {
+ return TYPE;
+ }
+
+ static Builder builder() {
+ return ImmutableIcebergViewObj.builder();
+ }
+
+ final class IcebergViewObjType extends AbstractObjType<IcebergViewObj> {
+ public IcebergViewObjType() {
+ super("i-v", "Iceberg View", IcebergViewObj.class);
+ }
+ }
+
+ interface Builder extends IcebergTableLikeObj.Builder<IcebergViewObj, Builder> {
+
+ @CanIgnoreReturnValue
+ Builder from(IcebergViewObj from);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/LocalNamespaceObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/LocalNamespaceObj.java
new file mode 100644
index 000000000..b89245d49
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/LocalNamespaceObj.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.content;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.obj.AbstractObjType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+
+/** Locally managed namespace. */
+@PolarisImmutable
+@JsonSerialize(as = ImmutableLocalNamespaceObj.class)
+@JsonDeserialize(as = ImmutableLocalNamespaceObj.class)
+public interface LocalNamespaceObj extends NamespaceObj {
+ ObjType TYPE = new LocalNamespaceObjType();
+
+ @Override
+ default ObjType type() {
+ return TYPE;
+ }
+
+ static Builder builder() {
+ return ImmutableLocalNamespaceObj.builder();
+ }
+
+ final class LocalNamespaceObjType extends AbstractObjType<LocalNamespaceObj> {
+ public LocalNamespaceObjType() {
+ super("ns-l", "Namespace (local)", LocalNamespaceObj.class);
+ }
+ }
+
+ interface Builder extends NamespaceObj.Builder<LocalNamespaceObj, Builder> {
+
+ @CanIgnoreReturnValue
+ Builder from(LocalNamespaceObj from);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/NamespaceObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/NamespaceObj.java
new file mode 100644
index 000000000..324a1cc45
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/NamespaceObj.java
@@ -0,0 +1,33 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.content;
+
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+
+/** Base for all namespaces (think: folder). */
+public interface NamespaceObj extends PolicyAttachableContentObj {
+
+ interface Builder<
+ O extends PolicyAttachableContentObj, B extends PolicyAttachableContentObj.Builder<O, B>>
+ extends PolicyAttachableContentObj.Builder<O, B> {
+
+ @CanIgnoreReturnValue
+ B from(NamespaceObj from);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/PolicyAttachableContentObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/PolicyAttachableContentObj.java
new file mode 100644
index 000000000..907626570
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/PolicyAttachableContentObj.java
@@ -0,0 +1,31 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.content;
+
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+
+public interface PolicyAttachableContentObj extends ContentObj {
+
+ interface Builder<O extends PolicyAttachableContentObj, B extends Builder<O, B>>
+ extends ContentObj.Builder<O, B> {
+
+ @CanIgnoreReturnValue
+ B from(PolicyAttachableContentObj from);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/PolicyObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/PolicyObj.java
new file mode 100644
index 000000000..ff38aee4e
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/PolicyObj.java
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.content;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+import java.util.Optional;
+import java.util.OptionalInt;
+import org.apache.polaris.core.policy.PolicyType;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.obj.AbstractObjType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+
+@PolarisImmutable
+@JsonSerialize(as = ImmutablePolicyObj.class)
+@JsonDeserialize(as = ImmutablePolicyObj.class)
+public interface PolicyObj extends ContentObj {
+ ObjType TYPE = new CatalogPolicyObjType();
+
+ @Override
+ default ObjType type() {
+ return TYPE;
+ }
+
+ PolicyType policyType();
+
+ Optional<String> description();
+
+ Optional<String> content();
+
+ OptionalInt policyVersion();
+
+ static Builder builder() {
+ return ImmutablePolicyObj.builder();
+ }
+
+ final class CatalogPolicyObjType extends AbstractObjType<PolicyObj> {
+ public CatalogPolicyObjType() {
+ super("pol", "Policy", PolicyObj.class);
+ }
+ }
+
+ @SuppressWarnings("unused")
+ interface Builder extends ContentObj.Builder<PolicyObj, Builder> {
+ @CanIgnoreReturnValue
+ Builder from(PolicyObj from);
+
+ @CanIgnoreReturnValue
+ Builder policyType(PolicyType policyType);
+
+ @CanIgnoreReturnValue
+ Builder description(String description);
+
+ @CanIgnoreReturnValue
+ Builder description(Optional<String> description);
+
+ @CanIgnoreReturnValue
+ Builder content(String content);
+
+ @CanIgnoreReturnValue
+ Builder content(Optional<String> content);
+
+ @CanIgnoreReturnValue
+ Builder policyVersion(int policyVersion);
+
+ @CanIgnoreReturnValue
+ Builder policyVersion(OptionalInt policyVersion);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/TableLikeObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/TableLikeObj.java
new file mode 100644
index 000000000..9e3bbce27
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/content/TableLikeObj.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.content;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+import java.util.Optional;
+
+/** Base for all catalog content objects, including namespaces, tables, views, etc. */
+public interface TableLikeObj extends PolicyAttachableContentObj {
+
+ @JsonInclude(JsonInclude.Include.NON_ABSENT)
+ Optional<String> metadataLocation();
+
+ interface Builder<O extends TableLikeObj, B extends Builder<O, B>>
+ extends PolicyAttachableContentObj.Builder<O, B> {
+
+ @CanIgnoreReturnValue
+ B from(TableLikeObj from);
+
+ @CanIgnoreReturnValue
+ B metadataLocation(String metadataLocation);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/BaseCatalogContentMapping.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/BaseCatalogContentMapping.java
new file mode 100644
index 000000000..cf0570e10
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/BaseCatalogContentMapping.java
@@ -0,0 +1,56 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import jakarta.annotation.Nonnull;
+import java.util.Map;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogBaseObj;
+import org.apache.polaris.persistence.nosql.coretypes.content.ContentObj;
+
+/** Base class for mapping objects that are catalog content, aka tables, views and policies. */
+abstract class BaseCatalogContentMapping<
+ O extends ContentObj & CatalogBaseObj,
+ B extends ContentObj.Builder<O, B> & CatalogBaseObj.Builder<O, B>>
+ extends BaseCatalogMapping<O, B> {
+ BaseCatalogContentMapping(
+ @Nonnull ObjType objType,
+ @Nonnull ObjType containerObjType,
+ @Nonnull String refName,
+ @Nonnull PolarisEntityType entityType) {
+ super(objType, containerObjType, refName, entityType);
+ }
+
+ BaseCatalogContentMapping(
+ @Nonnull Class<? extends ContentObj> baseObjTypeClass,
+ @Nonnull Map<PolarisEntitySubType, ObjType> subTypes,
+ @Nonnull ObjType containerObjType,
+ @Nonnull String refName,
+ @Nonnull PolarisEntityType entityType) {
+ super(baseObjTypeClass, subTypes, containerObjType, refName, entityType);
+ }
+
+ @Override
+ public boolean catalogContent() {
+ return true;
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/BaseCatalogMapping.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/BaseCatalogMapping.java
new file mode 100644
index 000000000..61e24bbac
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/BaseCatalogMapping.java
@@ -0,0 +1,67 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import static java.lang.String.format;
+
+import jakarta.annotation.Nonnull;
+import java.util.Map;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogBaseObj;
+
+/** Base class for mapping objects contained in a catalog. */
+abstract class BaseCatalogMapping<O extends CatalogBaseObj, B extends CatalogBaseObj.Builder<O, B>>
+ extends BaseMapping<O, B> {
+ BaseCatalogMapping(
+ @Nonnull ObjType objType,
+ @Nonnull ObjType containerObjType,
+ @Nonnull String refName,
+ @Nonnull PolarisEntityType entityType) {
+ super(objType, containerObjType, refName, entityType);
+ }
+
+ BaseCatalogMapping(
+ @Nonnull Class<? extends CatalogBaseObj> baseObjTypeClass,
+ @Nonnull Map<PolarisEntitySubType, ObjType> subTypes,
+ @Nonnull ObjType containerObjType,
+ @Nonnull String refName,
+ @Nonnull PolarisEntityType entityType) {
+ super(baseObjTypeClass, subTypes, containerObjType, refName, entityType);
+ }
+
+ @Override
+ public void checkCatalogId(long catalogId) {
+ EntityObjMappings.checkCatalogId(catalogId);
+ }
+
+ @Override
+ public @Nonnull String refNameForCatalog(long catalogId) {
+ checkCatalogId(catalogId);
+ return format(refName, catalogId);
+ }
+
+ @Override
+ public long fixCatalogId(long catalogId) {
+ checkCatalogId(catalogId);
+ return catalogId;
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/BaseMapping.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/BaseMapping.java
new file mode 100644
index 000000000..651f0822b
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/BaseMapping.java
@@ -0,0 +1,314 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
+import static java.lang.String.format;
+import static org.apache.polaris.core.entity.PolarisEntitySubType.ANY_SUBTYPE;
+
+import jakarta.annotation.Nonnull;
+import jakarta.annotation.Nullable;
+import java.time.Instant;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import org.apache.polaris.core.entity.PolarisBaseEntity;
+import org.apache.polaris.core.entity.PolarisEntityConstants;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.entity.PolarisPrincipalSecrets;
+import org.apache.polaris.core.storage.PolarisStorageConfigurationInfo;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.ContainerObj;
+import org.apache.polaris.persistence.nosql.coretypes.ObjBase;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogStorageObj;
+
+public abstract class BaseMapping<O extends ObjBase, B extends ObjBase.Builder<O, B>> {
+
+ private final Class<? extends ObjBase> baseObjTypeClass;
+ final ObjType objType;
+ final Map<PolarisEntitySubType, ObjType> subTypes;
+ final Map<ObjType, PolarisEntitySubType> subTypeByObjType;
+ final ObjType containerObjType;
+ final String refName;
+ final PolarisEntityType entityType;
+
+ @SuppressWarnings("unchecked")
+ BaseMapping(
+ @Nonnull ObjType objType,
+ @Nullable ObjType containerObjType,
+ @Nullable String refName,
+ @Nonnull PolarisEntityType entityType) {
+ this.baseObjTypeClass = (Class<? extends ObjBase>) objType.targetClass();
+ this.objType = objType;
+ this.subTypes = Map.of(PolarisEntitySubType.NULL_SUBTYPE, objType);
+ this.subTypeByObjType = Map.of(objType, PolarisEntitySubType.NULL_SUBTYPE);
+ this.containerObjType = containerObjType;
+ this.refName = refName;
+ this.entityType = entityType;
+ }
+
+ BaseMapping(
+ @Nonnull Class<? extends ObjBase> baseObjTypeClass,
+ @Nonnull Map<PolarisEntitySubType, ObjType> subTypes,
+ @Nonnull ObjType containerObjType,
+ @Nonnull String refName,
+ @Nonnull PolarisEntityType entityType) {
+ this.baseObjTypeClass = baseObjTypeClass;
+ this.objType = null;
+ this.subTypes = subTypes;
+ this.subTypeByObjType =
+ subTypes.entrySet().stream()
+ .collect(
+ Collectors.toMap(
+ Map.Entry::getValue,
+ Map.Entry::getKey,
+ (a, b) -> {
+ throw new IllegalStateException(format("Duplicate key %s", a));
+ }));
+ this.containerObjType = containerObjType;
+ this.refName = refName;
+ this.entityType = entityType;
+ }
+
+ /** Check whether the given subtype is valid. */
+ public void validateSubType(@Nonnull PolarisEntitySubType subType) {
+ objTypeForSubType(subType);
+ }
+
+ /** Retrieve the {@link ObjType} for the given {@link PolarisEntitySubType}. */
+ public @Nonnull ObjType objTypeForSubType(@Nonnull PolarisEntitySubType subType) {
+ checkArgument(
+ subType != ANY_SUBTYPE,
+ "Unresolvable subtype ANY_SUBTYPE for exact match for %s",
+ entityType);
+ var objType = subTypes.get(subType);
+ checkArgument(objType != null, "Invalid subType %s for %s", subType, entityType);
+ return objType;
+ }
+
+ /**
+ * Retrieve the {@link Class} used to filter objects for the given {@link PolarisEntitySubType}.
+ */
+ public @Nonnull Class<? extends ObjBase> objTypeClassForSubTypeForFiltering(
+ @Nonnull PolarisEntitySubType subType) {
+ if (subType == ANY_SUBTYPE) {
+ if (objType != null) {
+ return cast(objType.targetClass());
+ }
+ checkState(baseObjTypeClass != null, "No baseObjTypeClass for %s", entityType);
+ return baseObjTypeClass;
+ }
+
+ var objType = subTypes.get(subType);
+ checkState(objType != null, "Invalid subType %s for %s", subType, entityType);
+ return cast(objType.targetClass());
+ }
+
+ /** Checks whether the given catalog ID is valid for this mapping. */
+ public void checkCatalogId(long catalogId) {
+ checkArgument(
+ catalogId == 0L, "Catalog ID must be 0L, but is %s for %s", catalogId, entityType);
+ }
+
+ /**
+ * Returns the persistence reference name to manage entities for this mapping for the given
+ * catalog ID.
+ */
+ public @Nonnull String refNameForCatalog(long catalogId) {
+ checkArgument(
+ catalogId == 0L, "Catalog ID must be 0L, but is %s for %s", catalogId, entityType);
+ return refName;
+ }
+
+ public @Nonnull PolarisEntityType entityType() {
+ return entityType;
+ }
+
+ /**
+ * Returns whether this mapping refers to catalog content, aka tables, views and policies, called
+ * from {@link #mapToObj(PolarisBaseEntity, Optional)}
+ */
+ public boolean catalogContent() {
+ return false;
+ }
+
+ public @Nullable ObjType containerObjType() {
+ return containerObjType;
+ }
+
+ @SuppressWarnings("unchecked")
+ public @Nonnull <C extends ContainerObj> Class<C> containerObjTypeClass() {
+ return (Class<C>) containerObjType.targetClass();
+ }
+
+ /** Type specific mapping from a {@link PolarisBaseEntity} to an {@link ObjBase.Builder}. */
+ public abstract B newObjBuilder(@Nonnull PolarisEntitySubType subType);
+
+ /**
+ * Type specific mapping from a {@link PolarisBaseEntity} to an {@link ObjBase.Builder}.
+ *
+ * <p>Implementations <em>remove</em> properties from {@code properties} and {@code
+ * internalProperties} maps that have been set on the type-safe attributes of the {@link
+ * ObjBase.Builder}.
+ */
+ void mapToObjTypeSpecific(
+ B baseBuilder,
+ @Nonnull PolarisBaseEntity entity,
+ Optional<PolarisPrincipalSecrets> principalSecrets,
+ Map<String, String> properties,
+ Map<String, String> internalProperties) {}
+
+ static void mapToObjStorageSpecific(
+ CatalogStorageObj.Builder<?, ?> catalogObjBaseBuilder,
+ HashMap<String, String> internalProperties) {
+ catalogObjBaseBuilder.storageConfigurationInfo(
+ Optional.ofNullable(
+ internalProperties.remove(
+ PolarisEntityConstants.getStorageConfigInfoPropertyName()))
+ .map(PolarisStorageConfigurationInfo::deserialize));
+ catalogObjBaseBuilder.storageIntegrationIdentifier(
+ Optional.ofNullable(
+ internalProperties.remove(
+ PolarisEntityConstants.getStorageIntegrationIdentifierPropertyName())));
+ }
+
+ /**
+ * Produces a builder for the {@link ObjBase} from the given {@link PolarisBaseEntity} with the
+ * type-specific attributes populated.
+ */
+ public final B mapToObj(
+ @Nonnull PolarisBaseEntity entity, Optional<PolarisPrincipalSecrets> principalSecrets) {
+ var properties = new HashMap<>(entity.getPropertiesAsMap());
+ var internalProperties = new HashMap<>(entity.getInternalPropertiesAsMap());
+ validateSubType(entity.getSubType());
+
+ B baseBuilder = newObjBuilder(entity.getSubType());
+ mapToObjTypeSpecific(baseBuilder, entity, principalSecrets, properties, internalProperties);
+
+ if (baseBuilder instanceof CatalogStorageObj.Builder<?, ?> catalogObjBaseBuilder) {
+ mapToObjStorageSpecific(catalogObjBaseBuilder, internalProperties);
+ }
+
+ baseBuilder
+ .name(entity.getName())
+ .entityVersion(entity.getEntityVersion())
+ .stableId(entity.getId())
+ .parentStableId(entity.getParentId())
+ .createTimestamp(Instant.ofEpochMilli(entity.getCreateTimestamp()))
+ .updateTimestamp(Instant.ofEpochMilli(entity.getLastUpdateTimestamp()));
+
+ properties.entrySet().stream()
+ .filter(e -> e.getValue() != null)
+ .forEach(baseBuilder::putProperty);
+ internalProperties.entrySet().stream()
+ .filter(e -> e.getValue() != null)
+ .forEach(baseBuilder::putInternalProperty);
+
+ return baseBuilder;
+ }
+
+ /**
+ * Type specific mapping from an {@link ObjBase} to a {@link PolarisBaseEntity}, called from
+ * {@link #mapToEntity(ObjBase, long)}.
+ */
+ void mapToEntityTypeSpecific(
+ O o,
+ HashMap<String, String> properties,
+ HashMap<String, String> internalProperties,
+ PolarisEntitySubType subType) {}
+
+ static void mapToEntityStorageSpecific(
+ CatalogStorageObj catalogStorageObj, HashMap<String, String> internalProperties) {
+ catalogStorageObj
+ .storageConfigurationInfo()
+ .ifPresent(
+ polarisStorageConfigInfo ->
+ internalProperties.put(
+ PolarisEntityConstants.getStorageConfigInfoPropertyName(),
+ polarisStorageConfigInfo.serialize()));
+ catalogStorageObj
+ .storageIntegrationIdentifier()
+ .ifPresent(
+ ident ->
+ internalProperties.put(
+ PolarisEntityConstants.getStorageIntegrationIdentifierPropertyName(), ident));
+ }
+
+ /** Maps an {@link ObjBase} to a {@link PolarisBaseEntity} for the given catalog ID. */
+ public final PolarisBaseEntity mapToEntity(ObjBase o, long catalogId) {
+ var properties = new HashMap<>(o.properties());
+ var internalProperties = new HashMap<>(o.internalProperties());
+ checkCatalogId(catalogId);
+ var subType = subTypeByObjType.get(o.type());
+ checkState(subType != null, "Invalid no subtype for objType %s for %s", o.type(), entityType);
+
+ mapToEntityTypeSpecific(cast(o), properties, internalProperties, subType);
+
+ if (o instanceof CatalogStorageObj catalogStorageObj) {
+ mapToEntityStorageSpecific(catalogStorageObj, internalProperties);
+ }
+
+ return new PolarisBaseEntity.Builder()
+ .catalogId(catalogId)
+ .id(o.stableId())
+ .typeCode(entityType.getCode())
+ .subTypeCode(subType.getCode())
+ .parentId(o.parentStableId())
+ .name(o.name())
+ .propertiesAsMap(properties)
+ .internalPropertiesAsMap(internalProperties)
+ .createTimestamp(o.createTimestamp().toEpochMilli())
+ .lastUpdateTimestamp(o.updateTimestamp().toEpochMilli())
+ .entityVersion(o.entityVersion())
+ .build();
+ }
+
+ @SuppressWarnings("unchecked")
+ static <R> R cast(Object r) {
+ return (R) r;
+ }
+
+ @Override
+ public String toString() {
+ return "BaseMapping{"
+ + "entityType="
+ + entityType
+ + ", subTypes="
+ + subTypes.keySet()
+ + ", objType="
+ + objType
+ + '}';
+ }
+
+ /**
+ * Returns {@code 0L} for non-catalog-related types, otherwise the checked catalog ID.
+ *
+ * <p>At least some tests use a non-{@code 0} catalog ID for non-catalog entity types.
+ *
+ * <p>Could do the "fix" in {@link #refNameForCatalog(long)}, but the goal should be to fix the
+ * call sites and get rid of this workaround.
+ */
+ public long fixCatalogId(long catalogId) {
+ return 0;
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/CatalogMapping.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/CatalogMapping.java
new file mode 100644
index 000000000..ab85f94f2
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/CatalogMapping.java
@@ -0,0 +1,87 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import static org.apache.polaris.core.entity.CatalogEntity.CATALOG_TYPE_PROPERTY;
+import static org.apache.polaris.core.entity.CatalogEntity.DEFAULT_BASE_LOCATION_KEY;
+import static org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogsObj.CATALOGS_REF_NAME;
+
+import jakarta.annotation.Nonnull;
+import java.util.HashMap;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Optional;
+import org.apache.polaris.core.entity.PolarisBaseEntity;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.entity.PolarisPrincipalSecrets;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogObj;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogStatus;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogType;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogsObj;
+
+final class CatalogMapping extends BaseMapping<CatalogObj, CatalogObj.Builder> {
+ CatalogMapping() {
+ super(CatalogObj.TYPE, CatalogsObj.TYPE, CATALOGS_REF_NAME, PolarisEntityType.CATALOG);
+ }
+
+ @Override
+ public CatalogObj.Builder newObjBuilder(@Nonnull PolarisEntitySubType subType) {
+ return CatalogObj.builder();
+ }
+
+ @Override
+ void mapToObjTypeSpecific(
+ CatalogObj.Builder baseBuilder,
+ @Nonnull PolarisBaseEntity entity,
+ Optional<PolarisPrincipalSecrets> principalSecrets,
+ Map<String, String> properties,
+ Map<String, String> internalProperties) {
+ super.mapToObjTypeSpecific(
+ baseBuilder, entity, principalSecrets, properties, internalProperties);
+ var defaultBaseLocation = properties.remove(DEFAULT_BASE_LOCATION_KEY);
+ var catalogTypeString = internalProperties.remove(CATALOG_TYPE_PROPERTY);
+ if (catalogTypeString == null) {
+ catalogTypeString = CatalogType.INTERNAL.name();
+ }
+ var catalogType =
+ switch (catalogTypeString.toUpperCase(Locale.ROOT)) {
+ case "INTERNAL" -> CatalogType.INTERNAL;
+ case "EXTERNAL" -> CatalogType.EXTERNAL;
+ default ->
+ throw new IllegalArgumentException("Invalid catalog type " + catalogTypeString);
+ };
+ baseBuilder
+ .catalogType(catalogType)
+ .defaultBaseLocation(Optional.ofNullable(defaultBaseLocation))
+ .status(CatalogStatus.ACTIVE);
+ }
+
+ @Override
+ void mapToEntityTypeSpecific(
+ CatalogObj o,
+ HashMap<String, String> properties,
+ HashMap<String, String> internalProperties,
+ PolarisEntitySubType subType) {
+ super.mapToEntityTypeSpecific(o, properties, internalProperties, subType);
+ internalProperties.put(CATALOG_TYPE_PROPERTY, o.catalogType().name());
+ o.defaultBaseLocation().ifPresent(v -> properties.put(DEFAULT_BASE_LOCATION_KEY, v));
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/CatalogRoleMapping.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/CatalogRoleMapping.java
new file mode 100644
index 000000000..cbf76a07f
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/CatalogRoleMapping.java
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import static org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogRolesObj.CATALOG_ROLES_REF_NAME_PATTERN;
+
+import jakarta.annotation.Nonnull;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogRoleObj;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogRolesObj;
+
+final class CatalogRoleMapping extends BaseCatalogMapping<CatalogRoleObj, CatalogRoleObj.Builder> {
+ CatalogRoleMapping() {
+ super(
+ CatalogRoleObj.TYPE,
+ CatalogRolesObj.TYPE,
+ CATALOG_ROLES_REF_NAME_PATTERN,
+ PolarisEntityType.CATALOG_ROLE);
+ }
+
+ @Override
+ public CatalogRoleObj.Builder newObjBuilder(@Nonnull PolarisEntitySubType subType) {
+ return CatalogRoleObj.builder();
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/EntityObjMappings.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/EntityObjMappings.java
new file mode 100644
index 000000000..26c7e6bfc
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/EntityObjMappings.java
@@ -0,0 +1,343 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
+import static java.util.function.Function.identity;
+
+import jakarta.annotation.Nonnull;
+import jakarta.annotation.Nullable;
+import java.util.Arrays;
+import java.util.EnumMap;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.OptionalLong;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.polaris.core.entity.EntityNameLookupRecord;
+import org.apache.polaris.core.entity.PolarisBaseEntity;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.entity.PolarisPrincipalSecrets;
+import org.apache.polaris.persistence.nosql.api.obj.Obj;
+import org.apache.polaris.persistence.nosql.api.obj.ObjRef;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjTypes;
+import org.apache.polaris.persistence.nosql.coretypes.ContainerObj;
+import org.apache.polaris.persistence.nosql.coretypes.ObjBase;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogObj;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogStateObj;
+import org.apache.polaris.persistence.nosql.coretypes.principals.PrincipalObj;
+import org.apache.polaris.persistence.nosql.coretypes.principals.PrincipalsObj;
+
+public final class EntityObjMappings {
+
+ private static final List<BaseMapping<?, ?>> MAPPINGS =
+ List.of(
+ new RootMapping(),
+ //
+ new CatalogMapping(),
+ new PrincipalMapping(),
+ new PrincipalRoleMapping(),
+ new TaskMapping(),
+ new FileMapping(),
+ //
+ new CatalogRoleMapping(),
+ new NamespaceMapping<>(),
+ new TableLikeMapping<>(),
+ new PolicyMapping());
+
+ private static final Map<PolarisEntityType, BaseMapping<?, ?>> BY_ENTITY_TYPE =
+ MAPPINGS.stream()
+ .collect(
+ Collectors.toMap(
+ BaseMapping::entityType,
+ identity(),
+ (a, b) -> {
+ throw new IllegalStateException(String.format("Duplicate key %s", a));
+ },
+ () -> new EnumMap<>(PolarisEntityType.class)));
+
+ private static final BaseMapping<?, ?>[] BY_ENTITY_TYPE_CODE;
+
+ /**
+ * Contains mappings from all obj-type {@link Class}es to the {@link BaseMapping} referencing it
+ * either via {@link BaseMapping#objType} or {@link BaseMapping#subTypes}.
+ */
+ private static final Map<Class<? extends Obj>, BaseMapping<?, ?>> BY_OBJ_TYPE_TARGET_CLASS;
+
+ static {
+ var missingEntityTypes = new HashSet<>(Set.of(PolarisEntityType.values()));
+ missingEntityTypes.remove(PolarisEntityType.NULL_TYPE);
+ missingEntityTypes.removeAll(BY_ENTITY_TYPE.keySet());
+ checkState(
+ missingEntityTypes.isEmpty(),
+ "Missing TypeMapping for some PolarisEntityType %s",
+ missingEntityTypes);
+
+ var byEntityTypeCode =
+ new BaseMapping<?, ?>
+ [Arrays.stream(PolarisEntityType.values())
+ .mapToInt(PolarisEntityType::getCode)
+ .max()
+ .orElseThrow()
+ + 1];
+
+ var byObjTypeTargetClass = new HashMap<Class<? extends Obj>, BaseMapping<?, ?>>();
+ for (var mapping : MAPPINGS) {
+ if (mapping.objType != null) {
+ byObjTypeTargetClass.put(mapping.objType.targetClass(), mapping);
+ } else {
+ for (var subType : mapping.subTypes.values()) {
+ byObjTypeTargetClass.put(subType.targetClass(), mapping);
+ }
+ }
+ var existing = byEntityTypeCode[mapping.entityType().getCode()];
+ checkState(existing == null, "Duplicate entity type codes for %s and %s", mapping, existing);
+ byEntityTypeCode[mapping.entityType().getCode()] = mapping;
+ }
+ BY_OBJ_TYPE_TARGET_CLASS = byObjTypeTargetClass;
+ BY_ENTITY_TYPE_CODE = byEntityTypeCode;
+ }
+
+ private EntityObjMappings() {}
+
+ /**
+ * Produces a builder for the {@link ObjBase} from the given {@link PolarisBaseEntity} with the
+ * type-specific attributes populated.
+ */
+ @Nonnull
+ public static <O extends ObjBase, B extends ObjBase.Builder<O, B>> B mapToObj(
+ @Nonnull PolarisBaseEntity entity, Optional<PolarisPrincipalSecrets> principalSecrets) {
+ @SuppressWarnings("unchecked")
+ var mapping = (BaseMapping<O, B>) byEntityType(entity.getType());
+ return mapping.mapToObj(entity, principalSecrets);
+ }
+
+ /** Maps an {@link ObjBase} to a {@link PolarisBaseEntity} for the given catalog ID. */
+ @Nonnull
+ public static PolarisBaseEntity mapToEntity(@Nonnull ObjBase objBase, long catalogId) {
+ var entityTypeAndSubType = entityTypeAndSubType(objBase.type());
+ return entityTypeAndSubType.typeMapping().mapToEntity(objBase, catalogId);
+ }
+
+ /** Maps an {@link ObjBase} to an {@link EntityNameLookupRecord} for the given catalog ID. */
+ @Nonnull
+ public static EntityNameLookupRecord mapToEntityNameLookupRecord(
+ @Nonnull ObjBase objBase, long catalogId) {
+
+ var entityTypeAndSubType = entityTypeAndSubType(objBase.type());
+ entityTypeAndSubType.typeMapping().checkCatalogId(catalogId);
+
+ return new EntityNameLookupRecord(
+ catalogId,
+ objBase.stableId(),
+ objBase.parentStableId(),
+ objBase.name(),
+ entityTypeAndSubType.entityType().getCode(),
+ entityTypeAndSubType.subType().getCode());
+ }
+
+ // TODO move to MutationAttempt in the follow-up change
+ @Nullable
+ public static String entitySubTypeCodeFromObjType(ObjRef objRef) {
+ if (objRef != null) {
+ var objType = ObjTypes.objTypeById(objRef.type());
+ var entityTypeAndSubType = entityTypeAndSubType(objType);
+ return Integer.toString(entityTypeAndSubType.subType().getCode());
+ }
+ return null;
+ }
+
+ /**
+ * Extract the PolarisPrincipalSecrets from the given object, if it is a {@link PrincipalObj} and
+ * has secrets.
+ */
+ // TODO move to MutationAttempt in the follow-up change
+ public static @Nonnull Optional<PolarisPrincipalSecrets> maybeObjToPolarisPrincipalSecrets(
+ @Nonnull ObjBase obj) {
+ if (obj instanceof PrincipalObj principalObj && principalObj.clientId().isPresent()) {
+ return Optional.of(principalObjToPolarisPrincipalSecrets(principalObj));
+ }
+ return Optional.empty();
+ }
+
+ /**
+ * Produces a {@link PolarisPrincipalSecrets} from the given {@link PrincipalObj} assuming a
+ * {@code null} {@link PolarisPrincipalSecrets}.
+ */
+ public static @Nonnull PolarisPrincipalSecrets principalObjToPolarisPrincipalSecrets(
+ @Nonnull PrincipalObj principalObj) {
+ return principalObjToPolarisPrincipalSecrets(principalObj, null);
+ }
+
+ /**
+ * Produces a {@link PolarisPrincipalSecrets} from the given {@link PrincipalObj} and the given
+ * {@link PolarisPrincipalSecrets} if not {@code null}.
+ */
+ public static @Nonnull PolarisPrincipalSecrets principalObjToPolarisPrincipalSecrets(
+ @Nonnull PrincipalObj principalObj, @Nullable PolarisPrincipalSecrets newPrincipalSecrets) {
+ return new PolarisPrincipalSecrets(
+ principalObj.stableId(),
+ principalObj.clientId().orElse(null),
+ newPrincipalSecrets != null ? newPrincipalSecrets.getMainSecret() : null,
+ newPrincipalSecrets != null ? newPrincipalSecrets.getSecondarySecret() : null,
+ principalObj.secretSalt().orElse(null),
+ principalObj.mainSecretHash().orElse(null),
+ principalObj.secondarySecretHash().orElse(null));
+ }
+
+ /** Retrieve the {@link BaseMapping} for the given {@link PolarisEntityType}. */
+ public static @Nonnull BaseMapping<?, ?> byEntityType(@Nonnull PolarisEntityType entityType) {
+ var mapping = BY_ENTITY_TYPE.get(entityType);
+ checkArgument(mapping != null, "No type mapping for entity type %s", entityType);
+ return mapping;
+ }
+
+ /** Retrieve the {@link BaseMapping} for a {@link PolarisEntityType} by their {@code int} code. */
+ public static @Nonnull BaseMapping<?, ?> byEntityTypeCode(int entityType) {
+ checkArgument(
+ entityType > 0 && entityType < BY_ENTITY_TYPE_CODE.length,
+ "No type mapping for entity type code %s",
+ entityType);
+ var mapping = BY_ENTITY_TYPE_CODE[entityType];
+ checkArgument(mapping != null, "No type mapping for entity type code %s", entityType);
+ return mapping;
+ }
+
+ public record EntityTypeAndSubType(
+ @Nonnull PolarisEntityType entityType,
+ @Nonnull PolarisEntitySubType subType,
+ @Nonnull BaseMapping<?, ?> typeMapping) {}
+
+ /**
+ * Retrieve the {@link PolarisEntityType}, {@link PolarisEntitySubType} and the corresponding
+ * {@link BaseMapping} for the given {@link ObjType}.
+ */
+ public static @Nonnull EntityTypeAndSubType entityTypeAndSubType(@Nonnull ObjType objType) {
+ var mapping = BY_OBJ_TYPE_TARGET_CLASS.get(objType.targetClass());
+ checkArgument(
+ mapping != null,
+ "No TypeMapping for %s (%s)",
+ objType.name(),
+ objType.targetClass().getSimpleName());
+ var subType = mapping.subTypeByObjType.get(objType);
+ checkArgument(
+ subType != null,
+ "No subType mapping in existing TypeMapping for %s (%s)",
+ objType.name(),
+ objType.targetClass().getSimpleName());
+ return new EntityTypeAndSubType(mapping.entityType, subType, mapping);
+ }
+
+ /** Checks whether the entity type defines catalog-content, aka tables, views and policies. */
+ public static boolean isCatalogContent(int entityTypeCode) {
+ return byEntityTypeCode(entityTypeCode).catalogContent();
+ }
+
+ /** Checks whether the entity type defines catalog-content, aka tables, views and policies. */
+ public static boolean isCatalogContent(@Nonnull PolarisEntityType entityType) {
+ return byEntityType(entityType).catalogContent();
+ }
+
+ /**
+ * Returns the object type for a Polaris entity type and subtype that can be used for
+ * querying/filtering, allowing {@link PolarisEntitySubType#ANY_SUBTYPE} as the subtype.
+ */
+ public static @Nonnull Class<? extends ObjBase> objTypeForPolarisTypeForFiltering(
+ @Nonnull PolarisEntityType entityType, @Nonnull PolarisEntitySubType subType) {
+ return byEntityType(entityType).objTypeClassForSubTypeForFiltering(subType);
+ }
+
+ /**
+ * Returns the "exact" object type for a Polaris entity type and subtype, <em>not</em> allowing
+ * {@link PolarisEntitySubType#ANY_SUBTYPE} as the subtype.
+ */
+ public static @Nonnull ObjType objTypeForPolarisType(
+ @Nonnull PolarisEntityType entityType, @Nonnull PolarisEntitySubType subType) {
+ return byEntityType(entityType).objTypeForSubType(subType);
+ }
+
+ /**
+ * Retrieve the objet type that {@linkplain ContainerObj contains} the given entity-type.
+ *
+ * <p>For example, all catalog content is contained in a {@link CatalogStateObj}, all principals
+ * in a {@link PrincipalsObj}.
+ */
+ public static @Nonnull Class<? extends ContainerObj> containerTypeForEntityType(
+ @Nonnull PolarisEntityType entityType) {
+ var containerObjType = byEntityType(entityType).containerObjType;
+ checkArgument(containerObjType != null, "Not a container managed ObjType for %s", entityType);
+ @SuppressWarnings("unchecked")
+ var clazz = (Class<? extends ContainerObj>) containerObjType.targetClass();
+ return clazz;
+ }
+
+ /** Return the {@link PolarisEntityType} for the given entity type code, never {@code null}. */
+ public static @Nonnull PolarisEntityType typeFromCode(int entityTypeCode) {
+ return byEntityTypeCode(entityTypeCode).entityType;
+ }
+
+ /**
+ * Yields the given object, if it matches the given entity type, otherwise returns an empty
+ * optional.
+ */
+ public static <C extends ObjBase> @Nonnull Optional<C> filterIsEntityType(
+ @Nonnull C objBase, int entityTypeCode) {
+ return filterIsEntityType(objBase, typeFromCode(entityTypeCode));
+ }
+
+ /**
+ * Yields the given object, if it matches the given entity type, otherwise returns an empty
+ * optional.
+ */
+ public static <C extends ObjBase> @Nonnull Optional<C> filterIsEntityType(
+ @Nonnull C objBase, @Nonnull PolarisEntityType entityType) {
+ return objTypeForPolarisTypeForFiltering(entityType, PolarisEntitySubType.ANY_SUBTYPE)
+ .isInstance(objBase)
+ ? Optional.of(objBase)
+ : Optional.empty();
+ }
+
+ public static @Nonnull String referenceName(
+ @Nonnull PolarisEntityType entityType, @Nonnull Optional<CatalogObj> catalog) {
+ var catalogStableId = catalog.map(ObjBase::stableId).orElse(0L);
+ return referenceName(entityType, catalogStableId);
+ }
+
+ public static @Nonnull String referenceName(
+ @Nonnull PolarisEntityType entityType, @Nonnull OptionalLong catalogId) {
+ return referenceName(entityType, catalogId.orElse(0L));
+ }
+
+ public static @Nonnull String referenceName(
+ @Nonnull PolarisEntityType entityType, long catalogId) {
+ return BY_ENTITY_TYPE.get(entityType).refNameForCatalog(catalogId);
+ }
+
+ /** Checks that the given catalog ID is valid, aka positive. */
+ public static void checkCatalogId(long catalogId) {
+ checkArgument(catalogId > 0L, "Invalid catalog ID %s", catalogId);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/FileMapping.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/FileMapping.java
new file mode 100644
index 000000000..c2adfb66c
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/FileMapping.java
@@ -0,0 +1,36 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import jakarta.annotation.Nonnull;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.FileObj;
+
+final class FileMapping extends BaseMapping<FileObj, FileObj.Builder> {
+ FileMapping() {
+ super(FileObj.TYPE, null, null, PolarisEntityType.FILE);
+ }
+
+ @Override
+ public FileObj.Builder newObjBuilder(@Nonnull PolarisEntitySubType subType) {
+ return FileObj.builder();
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/NamespaceMapping.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/NamespaceMapping.java
new file mode 100644
index 000000000..7e7a1a633
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/NamespaceMapping.java
@@ -0,0 +1,48 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import static org.apache.polaris.core.entity.PolarisEntitySubType.NULL_SUBTYPE;
+import static org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogStateObj.CATALOG_STATE_REF_NAME_PATTERN;
+
+import jakarta.annotation.Nonnull;
+import java.util.Map;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogStateObj;
+import org.apache.polaris.persistence.nosql.coretypes.content.LocalNamespaceObj;
+import org.apache.polaris.persistence.nosql.coretypes.content.NamespaceObj;
+
+final class NamespaceMapping<O extends NamespaceObj, B extends NamespaceObj.Builder<O, B>>
+ extends BaseCatalogContentMapping<O, B> {
+ NamespaceMapping() {
+ super(
+ NamespaceObj.class,
+ Map.of(NULL_SUBTYPE, LocalNamespaceObj.TYPE),
+ CatalogStateObj.TYPE,
+ CATALOG_STATE_REF_NAME_PATTERN,
+ PolarisEntityType.NAMESPACE);
+ }
+
+ @Override
+ public B newObjBuilder(@Nonnull PolarisEntitySubType subType) {
+ return cast(LocalNamespaceObj.builder());
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/PolicyMapping.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/PolicyMapping.java
new file mode 100644
index 000000000..6c9b8d0c7
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/PolicyMapping.java
@@ -0,0 +1,90 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import static org.apache.polaris.core.policy.PolicyEntity.POLICY_CONTENT_KEY;
+import static org.apache.polaris.core.policy.PolicyEntity.POLICY_DESCRIPTION_KEY;
+import static org.apache.polaris.core.policy.PolicyEntity.POLICY_TYPE_CODE_KEY;
+import static org.apache.polaris.core.policy.PolicyEntity.POLICY_VERSION_KEY;
+import static org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogStateObj.CATALOG_STATE_REF_NAME_PATTERN;
+
+import jakarta.annotation.Nonnull;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.OptionalInt;
+import org.apache.polaris.core.entity.PolarisBaseEntity;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.entity.PolarisPrincipalSecrets;
+import org.apache.polaris.core.policy.PolicyType;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogStateObj;
+import org.apache.polaris.persistence.nosql.coretypes.content.PolicyObj;
+
+final class PolicyMapping extends BaseCatalogContentMapping<PolicyObj, PolicyObj.Builder> {
+ PolicyMapping() {
+ super(
+ PolicyObj.TYPE,
+ CatalogStateObj.TYPE,
+ CATALOG_STATE_REF_NAME_PATTERN,
+ PolarisEntityType.POLICY);
+ }
+
+ @Override
+ public PolicyObj.Builder newObjBuilder(@Nonnull PolarisEntitySubType subType) {
+ return PolicyObj.builder();
+ }
+
+ @Override
+ void mapToObjTypeSpecific(
+ PolicyObj.Builder baseBuilder,
+ @Nonnull PolarisBaseEntity entity,
+ Optional<PolarisPrincipalSecrets> principalSecrets,
+ Map<String, String> properties,
+ Map<String, String> internalProperties) {
+ super.mapToObjTypeSpecific(
+ baseBuilder, entity, principalSecrets, properties, internalProperties);
+ var policyTypeCode = properties.remove(POLICY_TYPE_CODE_KEY);
+ var description = properties.remove(POLICY_DESCRIPTION_KEY);
+ var content = properties.remove(POLICY_CONTENT_KEY);
+ var version = properties.remove(POLICY_VERSION_KEY);
+
+ baseBuilder
+ .policyType(
+ PolicyType.fromCode(Integer.parseInt(policyTypeCode != null ? policyTypeCode : "0")))
+ .description(Optional.ofNullable(description))
+ .content(Optional.ofNullable(content))
+ .policyVersion(
+ version != null ? OptionalInt.of(Integer.parseInt(version)) : OptionalInt.empty());
+ }
+
+ @Override
+ void mapToEntityTypeSpecific(
+ PolicyObj o,
+ HashMap<String, String> properties,
+ HashMap<String, String> internalProperties,
+ PolarisEntitySubType subType) {
+ super.mapToEntityTypeSpecific(o, properties, internalProperties, subType);
+ properties.put(POLICY_TYPE_CODE_KEY, Integer.toString(o.policyType().getCode()));
+ o.description().ifPresent(v -> properties.put(POLICY_DESCRIPTION_KEY, v));
+ o.content().ifPresent(v -> properties.put(POLICY_CONTENT_KEY, v));
+ o.policyVersion().ifPresent(v -> properties.put(POLICY_VERSION_KEY, Integer.toString(v)));
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/PrincipalMapping.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/PrincipalMapping.java
new file mode 100644
index 000000000..441441e1f
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/PrincipalMapping.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import static org.apache.polaris.core.entity.PolarisEntityConstants.PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE;
+import static org.apache.polaris.persistence.nosql.coretypes.principals.PrincipalsObj.PRINCIPALS_REF_NAME;
+
+import jakarta.annotation.Nonnull;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import org.apache.polaris.core.entity.PolarisBaseEntity;
+import org.apache.polaris.core.entity.PolarisEntityConstants;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.entity.PolarisPrincipalSecrets;
+import org.apache.polaris.persistence.nosql.coretypes.principals.PrincipalObj;
+import org.apache.polaris.persistence.nosql.coretypes.principals.PrincipalsObj;
+
+final class PrincipalMapping extends BaseMapping<PrincipalObj, PrincipalObj.Builder> {
+ PrincipalMapping() {
+ super(PrincipalObj.TYPE, PrincipalsObj.TYPE, PRINCIPALS_REF_NAME, PolarisEntityType.PRINCIPAL);
+ }
+
+ @Override
+ public PrincipalObj.Builder newObjBuilder(@Nonnull PolarisEntitySubType subType) {
+ return PrincipalObj.builder();
+ }
+
+ @Override
+ void mapToObjTypeSpecific(
+ PrincipalObj.Builder baseBuilder,
+ @Nonnull PolarisBaseEntity entity,
+ Optional<PolarisPrincipalSecrets> principalSecrets,
+ Map<String, String> properties,
+ Map<String, String> internalProperties) {
+ super.mapToObjTypeSpecific(
+ baseBuilder, entity, principalSecrets, properties, internalProperties);
+ var credentialRotationRequired =
+ internalProperties.remove(PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE);
+ internalProperties.remove(PolarisEntityConstants.getClientIdPropertyName());
+
+ var principalObjBuilder =
+ baseBuilder.credentialRotationRequired(credentialRotationRequired != null);
+ principalSecrets.ifPresent(
+ secrets ->
+ principalObjBuilder
+ .clientId(secrets.getPrincipalClientId())
+ .mainSecretHash(secrets.getMainSecretHash())
+ .secondarySecretHash(secrets.getSecondarySecretHash())
+ .secretSalt(secrets.getSecretSalt()));
+ }
+
+ @Override
+ void mapToEntityTypeSpecific(
+ PrincipalObj o,
+ HashMap<String, String> properties,
+ HashMap<String, String> internalProperties,
+ PolarisEntitySubType subType) {
+ super.mapToEntityTypeSpecific(o, properties, internalProperties, subType);
+ if (o.credentialRotationRequired()) {
+ internalProperties.put(PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE, "true");
+ }
+ o.clientId()
+ .ifPresent(
+ v -> internalProperties.put(PolarisEntityConstants.getClientIdPropertyName(), v));
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/PrincipalRoleMapping.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/PrincipalRoleMapping.java
new file mode 100644
index 000000000..aa813439d
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/PrincipalRoleMapping.java
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import static org.apache.polaris.persistence.nosql.coretypes.principals.PrincipalRolesObj.PRINCIPAL_ROLES_REF_NAME;
+
+import jakarta.annotation.Nonnull;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import org.apache.polaris.core.entity.PolarisBaseEntity;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.entity.PolarisPrincipalSecrets;
+import org.apache.polaris.core.entity.table.federated.FederatedEntities;
+import org.apache.polaris.persistence.nosql.coretypes.principals.PrincipalRoleObj;
+import org.apache.polaris.persistence.nosql.coretypes.principals.PrincipalRolesObj;
+
+final class PrincipalRoleMapping extends BaseMapping<PrincipalRoleObj, PrincipalRoleObj.Builder> {
+ PrincipalRoleMapping() {
+ super(
+ PrincipalRoleObj.TYPE,
+ PrincipalRolesObj.TYPE,
+ PRINCIPAL_ROLES_REF_NAME,
+ PolarisEntityType.PRINCIPAL_ROLE);
+ }
+
+ @Override
+ public PrincipalRoleObj.Builder newObjBuilder(@Nonnull PolarisEntitySubType subType) {
+ return PrincipalRoleObj.builder();
+ }
+
+ @Override
+ void mapToObjTypeSpecific(
+ PrincipalRoleObj.Builder baseBuilder,
+ @Nonnull PolarisBaseEntity entity,
+ Optional<PolarisPrincipalSecrets> principalSecrets,
+ Map<String, String> properties,
+ Map<String, String> internalProperties) {
+ super.mapToObjTypeSpecific(
+ baseBuilder, entity, principalSecrets, properties, internalProperties);
+ var federated =
+ Boolean.parseBoolean(internalProperties.remove(FederatedEntities.FEDERATED_ENTITY));
+ baseBuilder.federated(federated);
+ }
+
+ @Override
+ void mapToEntityTypeSpecific(
+ PrincipalRoleObj o,
+ HashMap<String, String> properties,
+ HashMap<String, String> internalProperties,
+ PolarisEntitySubType subType) {
+ super.mapToEntityTypeSpecific(o, properties, internalProperties, subType);
+ if (o.federated()) {
+ internalProperties.put(FederatedEntities.FEDERATED_ENTITY, "true");
+ }
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/RootMapping.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/RootMapping.java
new file mode 100644
index 000000000..918d652ef
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/RootMapping.java
@@ -0,0 +1,38 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import static org.apache.polaris.persistence.nosql.coretypes.realm.RootObj.ROOT_REF_NAME;
+
+import jakarta.annotation.Nonnull;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.persistence.nosql.coretypes.realm.RootObj;
+
+final class RootMapping extends BaseMapping<RootObj, RootObj.Builder> {
+ RootMapping() {
+ super(RootObj.TYPE, null, ROOT_REF_NAME, PolarisEntityType.ROOT);
+ }
+
+ @Override
+ public RootObj.Builder newObjBuilder(@Nonnull PolarisEntitySubType subType) {
+ return RootObj.builder();
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TableLikeMapping.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TableLikeMapping.java
new file mode 100644
index 000000000..9d779bd77
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TableLikeMapping.java
@@ -0,0 +1,117 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import static org.apache.polaris.core.entity.PolarisEntitySubType.GENERIC_TABLE;
+import static org.apache.polaris.core.entity.PolarisEntitySubType.ICEBERG_TABLE;
+import static org.apache.polaris.core.entity.PolarisEntitySubType.ICEBERG_VIEW;
+import static org.apache.polaris.core.entity.table.IcebergTableLikeEntity.METADATA_LOCATION_KEY;
+import static org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogStateObj.CATALOG_STATE_REF_NAME_PATTERN;
+
+import jakarta.annotation.Nonnull;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import org.apache.polaris.core.entity.PolarisBaseEntity;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.entity.PolarisPrincipalSecrets;
+import org.apache.polaris.core.entity.table.GenericTableEntity;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogStateObj;
+import org.apache.polaris.persistence.nosql.coretypes.content.GenericTableObj;
+import org.apache.polaris.persistence.nosql.coretypes.content.IcebergTableObj;
+import org.apache.polaris.persistence.nosql.coretypes.content.IcebergViewObj;
+import org.apache.polaris.persistence.nosql.coretypes.content.TableLikeObj;
+
+final class TableLikeMapping<O extends TableLikeObj, B extends TableLikeObj.Builder<O, B>>
+ extends BaseCatalogContentMapping<O, B> {
+ TableLikeMapping() {
+ super(
+ TableLikeObj.class,
+ Map.of(
+ ICEBERG_TABLE, IcebergTableObj.TYPE,
+ ICEBERG_VIEW, IcebergViewObj.TYPE,
+ GENERIC_TABLE, GenericTableObj.TYPE),
+ CatalogStateObj.TYPE,
+ CATALOG_STATE_REF_NAME_PATTERN,
+ PolarisEntityType.TABLE_LIKE);
+ }
+
+ @Override
+ public B newObjBuilder(@Nonnull PolarisEntitySubType subType) {
+ return cast(
+ switch (subType) {
+ case ICEBERG_TABLE -> IcebergTableObj.builder();
+ case ICEBERG_VIEW -> IcebergViewObj.builder();
+ case GENERIC_TABLE -> GenericTableObj.builder();
+ default -> throw new IllegalArgumentException("Unknown or invalid subtype");
+ });
+ }
+
+ @Override
+ void mapToObjTypeSpecific(
+ B baseBuilder,
+ @Nonnull PolarisBaseEntity entity,
+ Optional<PolarisPrincipalSecrets> principalSecrets,
+ Map<String, String> properties,
+ Map<String, String> internalProperties) {
+ super.mapToObjTypeSpecific(
+ baseBuilder, entity, principalSecrets, properties, internalProperties);
+ switch (baseBuilder) {
+ case IcebergTableObj.Builder ignored -> {}
+ case IcebergViewObj.Builder ignored -> {}
+ case GenericTableObj.Builder genericTableObjBuilder ->
+ genericTableObjBuilder
+ .format(Optional.ofNullable(internalProperties.remove(GenericTableEntity.FORMAT_KEY)))
+ .doc(Optional.ofNullable(internalProperties.remove(GenericTableEntity.DOC_KEY)));
+ default -> throw new IllegalArgumentException("Unknown or invalid subtype");
+ }
+
+ Optional.ofNullable(internalProperties.remove(METADATA_LOCATION_KEY))
+ .ifPresent(baseBuilder::metadataLocation);
+ }
+
+ @Override
+ void mapToEntityTypeSpecific(
+ O o,
+ HashMap<String, String> properties,
+ HashMap<String, String> internalProperties,
+ PolarisEntitySubType subType) {
+ super.mapToEntityTypeSpecific(o, properties, internalProperties, subType);
+ o.metadataLocation().ifPresent(v -> internalProperties.put(METADATA_LOCATION_KEY, v));
+ switch (o) {
+ case IcebergTableObj ignored -> {}
+ case IcebergViewObj ignored -> {}
+ case GenericTableObj genericTableObj -> {
+ genericTableObj
+ .format()
+ .ifPresent(v -> internalProperties.put(GenericTableEntity.FORMAT_KEY, v));
+ genericTableObj.doc().ifPresent(v -> internalProperties.put(GenericTableEntity.DOC_KEY, v));
+ }
+ default ->
+ throw new IllegalStateException(
+ "Cannot map "
+ + o.type().targetClass().getSimpleName()
+ + " ("
+ + o.type().name()
+ + ") to a PolarisEntity");
+ }
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TaskMapping.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TaskMapping.java
new file mode 100644
index 000000000..0721c670d
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TaskMapping.java
@@ -0,0 +1,144 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import static org.apache.polaris.persistence.nosql.coretypes.realm.ImmediateTasksObj.IMMEDIATE_TASKS_REF_NAME;
+
+import jakarta.annotation.Nonnull;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.DataInputStream;
+import java.io.DataOutputStream;
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.time.Instant;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.zip.GZIPInputStream;
+import java.util.zip.GZIPOutputStream;
+import org.apache.polaris.core.entity.AsyncTaskType;
+import org.apache.polaris.core.entity.PolarisBaseEntity;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.entity.PolarisPrincipalSecrets;
+import org.apache.polaris.core.entity.PolarisTaskConstants;
+import org.apache.polaris.persistence.nosql.coretypes.realm.ImmediateTaskObj;
+import org.apache.polaris.persistence.nosql.coretypes.realm.ImmediateTasksObj;
+
+final class TaskMapping extends BaseMapping<ImmediateTaskObj, ImmediateTaskObj.Builder> {
+ TaskMapping() {
+ super(
+ ImmediateTaskObj.TYPE,
+ ImmediateTasksObj.TYPE,
+ IMMEDIATE_TASKS_REF_NAME,
+ PolarisEntityType.TASK);
+ }
+
+ @Override
+ public ImmediateTaskObj.Builder newObjBuilder(@Nonnull PolarisEntitySubType subType) {
+ return ImmediateTaskObj.builder();
+ }
+
+ @Override
+ void mapToObjTypeSpecific(
+ ImmediateTaskObj.Builder b,
+ @Nonnull PolarisBaseEntity entity,
+ Optional<PolarisPrincipalSecrets> principalSecrets,
+ Map<String, String> properties,
+ Map<String, String> internalProperties) {
+ super.mapToObjTypeSpecific(b, entity, principalSecrets, properties, internalProperties);
+ var taskTypeCode = properties.remove(PolarisTaskConstants.TASK_TYPE);
+ var lastAttemptExecutorId = properties.remove(PolarisTaskConstants.LAST_ATTEMPT_EXECUTOR_ID);
+ if (lastAttemptExecutorId != null) {
+ b.lastAttemptExecutorId(lastAttemptExecutorId);
+ }
+ var lastAttemptStartTime = properties.remove(PolarisTaskConstants.LAST_ATTEMPT_START_TIME);
+ if (lastAttemptStartTime != null) {
+ b.lastAttemptStartTime(Instant.ofEpochMilli(Long.parseLong(lastAttemptStartTime)));
+ }
+ var attemptCount = properties.remove(PolarisTaskConstants.ATTEMPT_COUNT);
+ if (attemptCount != null) {
+ b.attemptCount(Integer.parseInt(attemptCount));
+ }
+ b.taskType(
+ Optional.ofNullable(
+ taskTypeCode != null
+ ? AsyncTaskType.fromTypeCode(Integer.parseInt(taskTypeCode))
+ : null))
+ .serializedEntity(
+ Optional.ofNullable(properties.remove("data"))
+ .map(TaskMapping::serializeStringCompressed));
+ }
+
+ @Override
+ void mapToEntityTypeSpecific(
+ ImmediateTaskObj o,
+ HashMap<String, String> properties,
+ HashMap<String, String> internalProperties,
+ PolarisEntitySubType subType) {
+ super.mapToEntityTypeSpecific(o, properties, internalProperties, subType);
+ o.serializedEntity()
+ .map(TaskMapping::deserializeStringCompressed)
+ .ifPresent(s -> properties.put("data", s));
+ o.taskType()
+ .ifPresent(
+ v -> properties.put(PolarisTaskConstants.TASK_TYPE, Integer.toString(v.typeCode())));
+ o.lastAttemptExecutorId()
+ .ifPresent(v -> properties.put(PolarisTaskConstants.LAST_ATTEMPT_EXECUTOR_ID, v));
+ o.lastAttemptStartTime()
+ .ifPresent(
+ v ->
+ properties.put(
+ PolarisTaskConstants.LAST_ATTEMPT_START_TIME, Long.toString(v.toEpochMilli())));
+ o.attemptCount()
+ .ifPresent(v -> properties.put(PolarisTaskConstants.ATTEMPT_COUNT, Integer.toString(v)));
+ }
+
+ /**
+ * Compress a string, for task-entity data, which can be really huge, like full Iceberg
+ * manifest-files as base64 encoded in JSON and such.
+ */
+ static ByteBuffer serializeStringCompressed(String entityAsJson) {
+ try (var byteArrayOutputStream = new ByteArrayOutputStream()) {
+ try (var gzip = new GZIPOutputStream(byteArrayOutputStream);
+ var out = new DataOutputStream(gzip)) {
+ out.writeUTF(entityAsJson);
+ }
+ return ByteBuffer.wrap(byteArrayOutputStream.toByteArray());
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ /**
+ * Decompress a string, for task-entity data, which can be really huge, like full Iceberg
+ * manifest-files as base64 encoded in JSON and such.
+ */
+ static String deserializeStringCompressed(ByteBuffer bytes) {
+ var byteArray = new byte[bytes.remaining()];
+ bytes.duplicate().get(byteArray);
+ try (var in = new DataInputStream(new GZIPInputStream(new ByteArrayInputStream(byteArray)))) {
+ return in.readUTF();
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/principals/PrincipalObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/principals/PrincipalObj.java
new file mode 100644
index 000000000..d0fcfcf10
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/principals/PrincipalObj.java
@@ -0,0 +1,103 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.principals;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+import java.util.Optional;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.obj.AbstractObjType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.ObjBase;
+import org.immutables.value.Value;
+
+@PolarisImmutable
+@JsonSerialize(as = ImmutablePrincipalObj.class)
+@JsonDeserialize(as = ImmutablePrincipalObj.class)
+public interface PrincipalObj extends ObjBase {
+ @JsonInclude(JsonInclude.Include.NON_ABSENT)
+ Optional<String> clientId();
+
+ @JsonInclude(JsonInclude.Include.NON_ABSENT)
+ Optional<String> mainSecretHash();
+
+ @JsonInclude(JsonInclude.Include.NON_ABSENT)
+ Optional<String> secondarySecretHash();
+
+ @JsonInclude(JsonInclude.Include.NON_ABSENT)
+ Optional<String> secretSalt();
+
+ @JsonInclude(JsonInclude.Include.NON_DEFAULT)
+ @Value.Default
+ default boolean credentialRotationRequired() {
+ return false;
+ }
+
+ ObjType TYPE = new PrincipalObjType();
+
+ @Override
+ default ObjType type() {
+ return TYPE;
+ }
+
+ static Builder builder() {
+ return ImmutablePrincipalObj.builder();
+ }
+
+ final class PrincipalObjType extends AbstractObjType<PrincipalObj> {
+ public PrincipalObjType() {
+ super("pr", "Principal", PrincipalObj.class);
+ }
+ }
+
+ interface Builder extends ObjBase.Builder<PrincipalObj, Builder> {
+
+ @CanIgnoreReturnValue
+ Builder from(PrincipalObj from);
+
+ @CanIgnoreReturnValue
+ Builder mainSecretHash(String mainSecretHash);
+
+ @CanIgnoreReturnValue
+ Builder mainSecretHash(Optional<String> mainSecretHash);
+
+ @CanIgnoreReturnValue
+ Builder secondarySecretHash(String secondarySecretHash);
+
+ @CanIgnoreReturnValue
+ Builder secondarySecretHash(Optional<String> secondarySecretHash);
+
+ @CanIgnoreReturnValue
+ Builder secretSalt(String secretSalt);
+
+ @CanIgnoreReturnValue
+ Builder secretSalt(Optional<String> secretSalt);
+
+ @CanIgnoreReturnValue
+ Builder clientId(String clientId);
+
+ @CanIgnoreReturnValue
+ Builder clientId(Optional<String> clientId);
+
+ @CanIgnoreReturnValue
+ Builder credentialRotationRequired(boolean credentialRotationRequired);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/principals/PrincipalRoleObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/principals/PrincipalRoleObj.java
new file mode 100644
index 000000000..6df45d130
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/principals/PrincipalRoleObj.java
@@ -0,0 +1,66 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.principals;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.obj.AbstractObjType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.ObjBase;
+import org.immutables.value.Value;
+
+@PolarisImmutable
+@JsonSerialize(as = ImmutablePrincipalRoleObj.class)
+@JsonDeserialize(as = ImmutablePrincipalRoleObj.class)
+public interface PrincipalRoleObj extends ObjBase {
+ ObjType TYPE = new PrincipalRoleObjType();
+
+ @JsonInclude(JsonInclude.Include.NON_DEFAULT)
+ @Value.Default
+ default boolean federated() {
+ return false;
+ }
+
+ @Override
+ default ObjType type() {
+ return TYPE;
+ }
+
+ static Builder builder() {
+ return ImmutablePrincipalRoleObj.builder();
+ }
+
+ final class PrincipalRoleObjType extends AbstractObjType<PrincipalRoleObj> {
+ public PrincipalRoleObjType() {
+ super("pr-r", "Principal Role", PrincipalRoleObj.class);
+ }
+ }
+
+ interface Builder extends ObjBase.Builder<PrincipalRoleObj, Builder> {
+
+ @CanIgnoreReturnValue
+ Builder from(PrincipalRoleObj from);
+
+ @CanIgnoreReturnValue
+ Builder federated(boolean federated);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/principals/PrincipalRolesObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/principals/PrincipalRolesObj.java
new file mode 100644
index 000000000..d94edfc61
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/principals/PrincipalRolesObj.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.principals;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.index.IndexContainer;
+import org.apache.polaris.persistence.nosql.api.index.IndexKey;
+import org.apache.polaris.persistence.nosql.api.obj.AbstractObjType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjRef;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.ContainerObj;
+
+/**
+ * Maintains the mapping of all principal roles to {@link PrincipalRoleObj}s. The current version of
+ * this object is maintained via the reference {@value #PRINCIPAL_ROLES_REF_NAME}.
+ */
+@PolarisImmutable
+@JsonSerialize(as = ImmutablePrincipalRolesObj.class)
+@JsonDeserialize(as = ImmutablePrincipalRolesObj.class)
+public interface PrincipalRolesObj extends ContainerObj {
+
+ String PRINCIPAL_ROLES_REF_NAME = "principal-roles";
+
+ ObjType TYPE = new PrincipalRolesObjType();
+
+ /** Mapping of principal role names to principal role objects. */
+ @Override
+ IndexContainer<ObjRef> nameToObjRef();
+
+ // overridden only for posterity, no technical reason
+ @Override
+ IndexContainer<IndexKey> stableIdToName();
+
+ static ImmutablePrincipalRolesObj.Builder builder() {
+ return ImmutablePrincipalRolesObj.builder();
+ }
+
+ @Override
+ default ObjType type() {
+ return TYPE;
+ }
+
+ final class PrincipalRolesObjType extends AbstractObjType<PrincipalRolesObj> {
+ public PrincipalRolesObjType() {
+ super("pr-rls", "Principal Roles", PrincipalRolesObj.class);
+ }
+ }
+
+ interface Builder extends ContainerObj.Builder<PrincipalRolesObj, Builder> {}
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/principals/PrincipalsObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/principals/PrincipalsObj.java
new file mode 100644
index 000000000..57b0ec30a
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/principals/PrincipalsObj.java
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.principals;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.index.IndexContainer;
+import org.apache.polaris.persistence.nosql.api.index.IndexKey;
+import org.apache.polaris.persistence.nosql.api.obj.AbstractObjType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjRef;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.ContainerObj;
+
+/**
+ * Maintains the mapping of all principals by name to {@link PrincipalObj}s. The current version of
+ * this object is maintained via the reference {@value #PRINCIPALS_REF_NAME}.
+ */
+@PolarisImmutable
+@JsonSerialize(as = ImmutablePrincipalsObj.class)
+@JsonDeserialize(as = ImmutablePrincipalsObj.class)
+public interface PrincipalsObj extends ContainerObj {
+
+ String PRINCIPALS_REF_NAME = "principals";
+
+ ObjType TYPE = new PrincipalsObjType();
+
+ /** Mapping of principal role names to principal objects. */
+ @Override
+ IndexContainer<ObjRef> nameToObjRef();
+
+ // overridden only for posterity, no technical reason
+ @Override
+ IndexContainer<IndexKey> stableIdToName();
+
+ /** Mapping of principal client ID to principal objects. */
+ IndexContainer<ObjRef> byClientId();
+
+ static ImmutablePrincipalsObj.Builder builder() {
+ return ImmutablePrincipalsObj.builder();
+ }
+
+ @Override
+ default ObjType type() {
+ return TYPE;
+ }
+
+ final class PrincipalsObjType extends AbstractObjType<PrincipalsObj> {
+ public PrincipalsObjType() {
+ super("prs", "Principals", PrincipalsObj.class);
+ }
+ }
+
+ interface Builder extends ContainerObj.Builder<PrincipalsObj, Builder> {
+
+ @CanIgnoreReturnValue
+ Builder byClientId(IndexContainer<ObjRef> byClientId);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/realm/ImmediateTaskObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/realm/ImmediateTaskObj.java
new file mode 100644
index 000000000..bbc5c7433
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/realm/ImmediateTaskObj.java
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.realm;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+import java.nio.ByteBuffer;
+import java.time.Instant;
+import java.util.Optional;
+import java.util.OptionalInt;
+import org.apache.polaris.core.entity.AsyncTaskType;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.obj.AbstractObjType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.ObjBase;
+
+@PolarisImmutable
+@JsonSerialize(as = ImmutableImmediateTaskObj.class)
+@JsonDeserialize(as = ImmutableImmediateTaskObj.class)
+public interface ImmediateTaskObj extends ObjBase {
+
+ ObjType TYPE = new ImmediateTaskObjType();
+
+ @Override
+ default ObjType type() {
+ return TYPE;
+ }
+
+ Optional<AsyncTaskType> taskType();
+
+ @JsonInclude(JsonInclude.Include.NON_ABSENT)
+ Optional<ByteBuffer> serializedEntity();
+
+ @JsonInclude(JsonInclude.Include.NON_ABSENT)
+ Optional<String> lastAttemptExecutorId();
+
+ @JsonInclude(JsonInclude.Include.NON_ABSENT)
+ Optional<Instant> lastAttemptStartTime();
+
+ @JsonInclude(JsonInclude.Include.NON_ABSENT)
+ OptionalInt attemptCount();
+
+ static Builder builder() {
+ return ImmutableImmediateTaskObj.builder();
+ }
+
+ final class ImmediateTaskObjType extends AbstractObjType<ImmediateTaskObj> {
+ public ImmediateTaskObjType() {
+ super("tsk", "Task", ImmediateTaskObj.class);
+ }
+ }
+
+ @SuppressWarnings("unused")
+ interface Builder extends ObjBase.Builder<ImmediateTaskObj, Builder> {
+ @CanIgnoreReturnValue
+ Builder from(ImmediateTaskObj from);
+
+ @CanIgnoreReturnValue
+ Builder taskType(AsyncTaskType taskType);
+
+ @CanIgnoreReturnValue
+ Builder taskType(Optional<? extends AsyncTaskType> taskType);
+
+ @CanIgnoreReturnValue
+ Builder serializedEntity(ByteBuffer serializedEntity);
+
+ @CanIgnoreReturnValue
+ Builder serializedEntity(Optional<? extends ByteBuffer> serializedEntity);
+
+ @CanIgnoreReturnValue
+ Builder lastAttemptExecutorId(String lastAttemptExecutorId);
+
+ @CanIgnoreReturnValue
+ Builder lastAttemptStartTime(Instant lastAttemptStartTime);
+
+ @CanIgnoreReturnValue
+ Builder attemptCount(int attemptCount);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/realm/ImmediateTasksObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/realm/ImmediateTasksObj.java
new file mode 100644
index 000000000..6ff22ddf6
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/realm/ImmediateTasksObj.java
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.realm;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.index.IndexContainer;
+import org.apache.polaris.persistence.nosql.api.index.IndexKey;
+import org.apache.polaris.persistence.nosql.api.obj.AbstractObjType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjRef;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.ContainerObj;
+
+/**
+ * Maintains all {@link ImmediateTaskObj}. The current version of this object is maintained via the
+ * reference {@value #IMMEDIATE_TASKS_REF_NAME}.
+ */
+@PolarisImmutable
+@JsonSerialize(as = ImmutableImmediateTasksObj.class)
+@JsonDeserialize(as = ImmutableImmediateTasksObj.class)
+public interface ImmediateTasksObj extends ContainerObj {
+
+ String IMMEDIATE_TASKS_REF_NAME = "immediate-tasks";
+
+ ObjType TYPE = new ImmediateTasksObjType();
+
+ // overridden only for posterity, no technical reason
+ @Override
+ IndexContainer<ObjRef> nameToObjRef();
+
+ // overridden only for posterity, no technical reason
+ @Override
+ IndexContainer<IndexKey> stableIdToName();
+
+ static ImmutableImmediateTasksObj.Builder builder() {
+ return ImmutableImmediateTasksObj.builder();
+ }
+
+ @Override
+ default ObjType type() {
+ return TYPE;
+ }
+
+ final class ImmediateTasksObjType extends AbstractObjType<ImmediateTasksObj> {
+ public ImmediateTasksObjType() {
+ super("itasks", "Immediate Tasks", ImmediateTasksObj.class);
+ }
+ }
+
+ interface Builder extends ContainerObj.Builder<ImmediateTasksObj, Builder> {}
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/realm/PolicyMapping.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/realm/PolicyMapping.java
new file mode 100644
index 000000000..4ac88041e
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/realm/PolicyMapping.java
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.realm;
+
+import com.fasterxml.jackson.annotation.JsonInclude;
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import java.util.Map;
+import java.util.Optional;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.index.IndexValueSerializer;
+import org.apache.polaris.persistence.nosql.api.obj.ObjRef;
+
+/**
+ * Polaris policy external mappings and parameters.
+ *
+ * <p>{@code null} values are deserialized as an "empty" policy mapping.
+ */
+@PolarisImmutable
+@JsonSerialize(as = ImmutablePolicyMapping.class)
+@JsonDeserialize(as = ImmutablePolicyMapping.class)
+public interface PolicyMapping {
+ @JsonInclude(JsonInclude.Include.NON_EMPTY)
+ Map<String, String> parameters();
+
+ @JsonInclude(JsonInclude.Include.NON_ABSENT)
+ Optional<ObjRef> externalMapping();
+
+ static ImmutablePolicyMapping.Builder builder() {
+ return ImmutablePolicyMapping.builder();
+ }
+
+ PolicyMapping EMPTY = builder().parameters(Map.of()).build();
+
+ IndexValueSerializer<PolicyMapping> POLICY_MAPPING_SERIALIZER = new PolicyMappingSerializer();
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/realm/PolicyMappingSerializer.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/realm/PolicyMappingSerializer.java
new file mode 100644
index 000000000..7db99e4b2
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/realm/PolicyMappingSerializer.java
@@ -0,0 +1,114 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.realm;
+
+import static org.apache.polaris.persistence.nosql.api.obj.ObjRef.OBJ_REF_SERIALIZER;
+import static org.apache.polaris.persistence.varint.VarInt.putVarInt;
+import static org.apache.polaris.persistence.varint.VarInt.readVarInt;
+import static org.apache.polaris.persistence.varint.VarInt.varIntLen;
+
+import jakarta.annotation.Nonnull;
+import jakarta.annotation.Nullable;
+import java.nio.ByteBuffer;
+import java.util.Map;
+import org.apache.polaris.persistence.nosql.api.index.IndexKey;
+import org.apache.polaris.persistence.nosql.api.index.IndexValueSerializer;
+
+final class PolicyMappingSerializer implements IndexValueSerializer<PolicyMapping> {
+ @Override
+ public int serializedSize(@Nullable PolicyMapping value) {
+ if (value == null) {
+ value = PolicyMapping.EMPTY;
+ }
+
+ var len = 0;
+
+ var params = value.parameters();
+ len += varIntLen(params.size());
+ for (Map.Entry<String, String> e : params.entrySet()) {
+ len += IndexKey.key(e.getKey()).serializedSize();
+ len += IndexKey.key(e.getValue()).serializedSize();
+ }
+
+ var ext = value.externalMapping();
+ len++;
+ if (ext.isPresent()) {
+ len += OBJ_REF_SERIALIZER.serializedSize(ext.get());
+ }
+
+ return len;
+ }
+
+ @Override
+ @Nonnull
+ public ByteBuffer serialize(@Nullable PolicyMapping value, @Nonnull ByteBuffer target) {
+ if (value == null) {
+ value = PolicyMapping.EMPTY;
+ }
+
+ var params = value.parameters();
+ putVarInt(target, params.size());
+ params.forEach(
+ (k, v) -> {
+ IndexKey.key(k).serialize(target);
+ IndexKey.key(v).serialize(target);
+ });
+
+ var ext = value.externalMapping();
+ if (ext.isPresent()) {
+ target.put((byte) 1);
+ OBJ_REF_SERIALIZER.serialize(ext.get(), target);
+ } else {
+ target.put((byte) 0);
+ }
+
+ return target;
+ }
+
+ @Override
+ public PolicyMapping deserialize(@Nonnull ByteBuffer buffer) {
+ var builder = PolicyMapping.builder();
+ var num = readVarInt(buffer);
+ for (int i = 0; i < num; i++) {
+ var k = IndexKey.deserializeKey(buffer).toString();
+ var v = IndexKey.deserializeKey(buffer).toString();
+ builder.putParameter(k, v);
+ }
+
+ if (buffer.get() == 1) {
+ builder.externalMapping(OBJ_REF_SERIALIZER.deserialize(buffer));
+ }
+
+ return builder.build();
+ }
+
+ @Override
+ public void skip(@Nonnull ByteBuffer buffer) {
+ var num = readVarInt(buffer);
+ for (int i = 0; i < num; i++) {
+ IndexKey.skip(buffer);
+ IndexKey.skip(buffer);
+ }
+
+ if (buffer.get() == 1) {
+ OBJ_REF_SERIALIZER.skip(buffer);
+ }
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/realm/PolicyMappingsObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/realm/PolicyMappingsObj.java
new file mode 100644
index 000000000..3312d903a
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/realm/PolicyMappingsObj.java
@@ -0,0 +1,212 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.realm;
+
+import static org.apache.polaris.persistence.varint.VarInt.putVarInt;
+import static org.apache.polaris.persistence.varint.VarInt.readVarInt;
+import static org.apache.polaris.persistence.varint.VarInt.readVarLong;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+import java.nio.ByteBuffer;
+import org.apache.polaris.core.policy.PolarisPolicyMappingRecord;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.index.IndexContainer;
+import org.apache.polaris.persistence.nosql.api.index.IndexKey;
+import org.apache.polaris.persistence.nosql.api.obj.AbstractObjType;
+import org.apache.polaris.persistence.nosql.api.obj.BaseCommitObj;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+
+@PolarisImmutable
+@JsonSerialize(as = ImmutablePolicyMappingsObj.class)
+@JsonDeserialize(as = ImmutablePolicyMappingsObj.class)
+public interface PolicyMappingsObj extends BaseCommitObj {
+
+ String POLICY_MAPPINGS_REF_NAME = "policy-mappings";
+
+ ObjType TYPE = new PolicyMappingsObjType();
+
+ IndexContainer<PolicyMapping> policyMappings();
+
+ @Override
+ default ObjType type() {
+ return TYPE;
+ }
+
+ static ImmutablePolicyMappingsObj.Builder builder() {
+ return ImmutablePolicyMappingsObj.builder();
+ }
+
+ final class PolicyMappingsObjType extends AbstractObjType<PolicyMappingsObj> {
+ public PolicyMappingsObjType() {
+ super("polmap", "PolicyMappings", PolicyMappingsObj.class);
+ }
+ }
+
+ interface Builder extends BaseCommitObj.Builder<PolicyMappingsObj, Builder> {
+ @CanIgnoreReturnValue
+ Builder from(PolicyMappingsObj container);
+
+ @CanIgnoreReturnValue
+ Builder policyMappings(IndexContainer<PolicyMapping> policyMappings);
+ }
+
+ // - key by entity:
+ // - E/entityCatalogId/entityId/policyType/policyCatalogId/policyId
+ // VALUE: PolicyMapping(parameters)
+ // - key by policy:
+ // - P/policyType/policyCatalogId/policyId/entityCatalogId/entityId
+ // VALUE: (empty)
+
+ interface PolicyMappingKey {
+ static PolicyMappingKey fromIndexKey(IndexKey key) {
+ var buffer = key.asByteBuffer();
+ var type = buffer.get();
+ return switch (type) {
+ case 'E' -> KeyByEntity.fromBuffer(buffer);
+ case 'P' -> KeyByPolicy.fromBuffer(buffer);
+ default ->
+ throw new IllegalArgumentException("Invalid policy mapping key type: " + (char) type);
+ };
+ }
+
+ long policyCatalogId();
+
+ long policyId();
+
+ PolicyMappingKey reverse();
+
+ IndexKey toIndexKey();
+
+ PolarisPolicyMappingRecord toMappingRecord(PolicyMapping value);
+ }
+
+ record KeyByEntity(
+ long entityCatalogId, long entityId, int policyType, long policyCatalogId, long policyId)
+ implements PolicyMappingKey {
+ static KeyByEntity fromBuffer(ByteBuffer buffer) {
+ var entityCatalogId = readVarLong(buffer);
+ var entityId = readVarLong(buffer);
+ var policyType = readVarInt(buffer);
+ var policyCatalogId = readVarLong(buffer);
+ var policyId = readVarLong(buffer);
+ return new KeyByEntity(entityCatalogId, entityId, policyType, policyCatalogId, policyId);
+ }
+
+ @Override
+ public PolicyMappingKey reverse() {
+ return new KeyByPolicy(policyCatalogId, policyId, policyType, entityCatalogId, entityId);
+ }
+
+ @Override
+ public IndexKey toIndexKey() {
+ var buffer = ByteBuffer.allocate(1 + 5 * 9);
+ buffer.put((byte) 'E');
+ putVarInt(buffer, entityCatalogId);
+ putVarInt(buffer, entityId);
+ putVarInt(buffer, policyType);
+ putVarInt(buffer, policyCatalogId);
+ putVarInt(buffer, policyId);
+ buffer.flip();
+ return IndexKey.key(buffer);
+ }
+
+ public IndexKey toEntityPartialIndexKey() {
+ var buffer = ByteBuffer.allocate(1 + 2 * 9);
+ buffer.put((byte) 'E');
+ putVarInt(buffer, entityCatalogId);
+ putVarInt(buffer, entityId);
+ buffer.flip();
+ return IndexKey.key(buffer);
+ }
+
+ public IndexKey toPolicyTypePartialIndexKey() {
+ var buffer = ByteBuffer.allocate(1 + 3 * 9);
+ buffer.put((byte) 'E');
+ putVarInt(buffer, entityCatalogId);
+ putVarInt(buffer, entityId);
+ putVarInt(buffer, policyType);
+ buffer.flip();
+ return IndexKey.key(buffer);
+ }
+
+ @Override
+ public PolarisPolicyMappingRecord toMappingRecord(PolicyMapping value) {
+ return new PolarisPolicyMappingRecord(
+ entityCatalogId, entityId, policyCatalogId, policyId, policyType, value.parameters());
+ }
+ }
+
+ record KeyByPolicy(
+ long policyCatalogId, long policyId, int policyType, long entityCatalogId, long entityId)
+ implements PolicyMappingKey {
+ static KeyByPolicy fromBuffer(ByteBuffer buffer) {
+ var policyCatalogId = readVarLong(buffer);
+ var policyId = readVarLong(buffer);
+ var policyType = readVarInt(buffer);
+ var entityCatalogId = readVarLong(buffer);
+ var entityId = readVarLong(buffer);
+ return new KeyByPolicy(policyCatalogId, policyId, policyType, entityCatalogId, entityId);
+ }
+
+ @Override
+ public PolicyMappingKey reverse() {
+ return new KeyByEntity(entityCatalogId, entityId, policyType, policyCatalogId, policyId);
+ }
+
+ @Override
+ public IndexKey toIndexKey() {
+ var buffer = ByteBuffer.allocate(1 + 5 * 9);
+ buffer.put((byte) 'P');
+ putVarInt(buffer, policyCatalogId);
+ putVarInt(buffer, policyId);
+ putVarInt(buffer, policyType);
+ putVarInt(buffer, entityCatalogId);
+ putVarInt(buffer, entityId);
+ buffer.flip();
+ return IndexKey.key(buffer);
+ }
+
+ public IndexKey toPolicyPartialIndexKey() {
+ var buffer = ByteBuffer.allocate(1 + 2 * 9);
+ buffer.put((byte) 'P');
+ putVarInt(buffer, policyCatalogId);
+ putVarInt(buffer, policyId);
+ buffer.flip();
+ return IndexKey.key(buffer);
+ }
+
+ public IndexKey toPolicyWithTypePartialIndexKey() {
+ var buffer = ByteBuffer.allocate(1 + 3 * 9);
+ buffer.put((byte) 'P');
+ putVarInt(buffer, policyCatalogId);
+ putVarInt(buffer, policyId);
+ putVarInt(buffer, policyType);
+ buffer.flip();
+ return IndexKey.key(buffer);
+ }
+
+ @Override
+ public PolarisPolicyMappingRecord toMappingRecord(PolicyMapping value) {
+ return new PolarisPolicyMappingRecord(
+ entityCatalogId, entityId, policyCatalogId, policyId, policyType, value.parameters());
+ }
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/realm/RealmGrantsObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/realm/RealmGrantsObj.java
new file mode 100644
index 000000000..e951752f3
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/realm/RealmGrantsObj.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.realm;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.obj.AbstractObjType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.acl.GrantsObj;
+
+/**
+ * Maintains the state of all realm grants. The current version of this object is maintained via the
+ * reference name {@value #REALM_GRANTS_REF_NAME}.
+ */
+@PolarisImmutable
+@JsonSerialize(as = ImmutableRealmGrantsObj.class)
+@JsonDeserialize(as = ImmutableRealmGrantsObj.class)
+public interface RealmGrantsObj extends GrantsObj {
+
+ String REALM_GRANTS_REF_NAME = "grants";
+
+ ObjType TYPE = new RealmGrantsObjType();
+
+ @Override
+ default ObjType type() {
+ return TYPE;
+ }
+
+ static ImmutableRealmGrantsObj.Builder builder() {
+ return ImmutableRealmGrantsObj.builder();
+ }
+
+ final class RealmGrantsObjType extends AbstractObjType<RealmGrantsObj> {
+ public RealmGrantsObjType() {
+ super("gts", "Realm Grants", RealmGrantsObj.class);
+ }
+ }
+
+ interface Builder extends GrantsObj.Builder<RealmGrantsObj, Builder> {}
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/realm/RootObj.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/realm/RootObj.java
new file mode 100644
index 000000000..f6016db57
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/realm/RootObj.java
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.realm;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import com.google.errorprone.annotations.CanIgnoreReturnValue;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.apache.polaris.persistence.nosql.api.obj.AbstractObjType;
+import org.apache.polaris.persistence.nosql.api.obj.BaseCommitObj;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.ObjBase;
+
+// TODO this "root" is a singleton in the realm - is this really necessary?
+@PolarisImmutable
+@JsonSerialize(as = ImmutableRootObj.class)
+@JsonDeserialize(as = ImmutableRootObj.class)
+public interface RootObj extends BaseCommitObj, ObjBase {
+
+ String ROOT_REF_NAME = "root";
+
+ ObjType TYPE = new RootObjType();
+
+ @Override
+ default ObjType type() {
+ return TYPE;
+ }
+
+ static Builder builder() {
+ return ImmutableRootObj.builder();
+ }
+
+ final class RootObjType extends AbstractObjType<RootObj> {
+ public RootObjType() {
+ super("root", "Root", RootObj.class);
+ }
+ }
+
+ interface Builder
+ extends BaseCommitObj.Builder<RootObj, Builder>, ObjBase.Builder<RootObj, Builder> {
+
+ @CanIgnoreReturnValue
+ Builder from(RootObj from);
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/refs/References.java b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/refs/References.java
new file mode 100644
index 000000000..80b9d9e5d
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/java/org/apache/polaris/persistence/nosql/coretypes/refs/References.java
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.refs;
+
+import static java.lang.String.format;
+import static org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogRolesObj.CATALOG_ROLES_REF_NAME_PATTERN;
+import static org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogStateObj.CATALOG_STATE_REF_NAME_PATTERN;
+import static org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogsObj.CATALOGS_REF_NAME;
+import static org.apache.polaris.persistence.nosql.coretypes.principals.PrincipalRolesObj.PRINCIPAL_ROLES_REF_NAME;
+import static org.apache.polaris.persistence.nosql.coretypes.principals.PrincipalsObj.PRINCIPALS_REF_NAME;
+import static org.apache.polaris.persistence.nosql.coretypes.realm.ImmediateTasksObj.IMMEDIATE_TASKS_REF_NAME;
+import static org.apache.polaris.persistence.nosql.coretypes.realm.PolicyMappingsObj.POLICY_MAPPINGS_REF_NAME;
+import static org.apache.polaris.persistence.nosql.coretypes.realm.RealmGrantsObj.REALM_GRANTS_REF_NAME;
+import static org.apache.polaris.persistence.nosql.coretypes.realm.RootObj.ROOT_REF_NAME;
+
+import java.util.Set;
+import java.util.stream.Collectors;
+
+public final class References {
+ private References() {}
+
+ private static final Set<String> REALM_REFERENCE_NAMES =
+ Set.of(
+ ROOT_REF_NAME,
+ CATALOGS_REF_NAME,
+ PRINCIPALS_REF_NAME,
+ PRINCIPAL_ROLES_REF_NAME,
+ REALM_GRANTS_REF_NAME,
+ IMMEDIATE_TASKS_REF_NAME,
+ POLICY_MAPPINGS_REF_NAME);
+
+ private static final Set<String> CATALOG_REFERENCE_PATTERNS =
+ Set.of(CATALOG_ROLES_REF_NAME_PATTERN, CATALOG_STATE_REF_NAME_PATTERN);
+
+ public static Set<String> realmReferenceNames() {
+ return REALM_REFERENCE_NAMES;
+ }
+
+ public static Set<String> catalogReferenceNames(long catalogStableId) {
+ return CATALOG_REFERENCE_PATTERNS.stream()
+ .map(refNamePattern -> format(refNamePattern, catalogStableId))
+ .collect(Collectors.toSet());
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/main/resources/META-INF/services/org.apache.polaris.persistence.nosql.api.obj.ObjType b/persistence/nosql/persistence/metastore-types/src/main/resources/META-INF/services/org.apache.polaris.persistence.nosql.api.obj.ObjType
new file mode 100644
index 000000000..fb2736968
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/main/resources/META-INF/services/org.apache.polaris.persistence.nosql.api.obj.ObjType
@@ -0,0 +1,45 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you 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
+#
+# http://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.
+#
+
+org.apache.polaris.persistence.nosql.coretypes.acl.AclObj$AclObjType
+
+org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogObj$CatalogObjType
+org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogRoleObj$CatalogRoleObjType
+org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogRolesObj$CatalogRolesObjType
+org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogStateObj$CatalogStatesObjType
+org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogsObj$CatalogsObjType
+org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogGrantsObj$CatalogGrantsObjType
+org.apache.polaris.persistence.nosql.coretypes.catalog.FileObj$FileObjType
+
+org.apache.polaris.persistence.nosql.coretypes.content.IcebergTableObj$IcebergTableObjType
+org.apache.polaris.persistence.nosql.coretypes.content.IcebergViewObj$IcebergViewObjType
+org.apache.polaris.persistence.nosql.coretypes.content.GenericTableObj$GenericTableObjType
+org.apache.polaris.persistence.nosql.coretypes.content.LocalNamespaceObj$LocalNamespaceObjType
+org.apache.polaris.persistence.nosql.coretypes.content.PolicyObj$CatalogPolicyObjType
+
+org.apache.polaris.persistence.nosql.coretypes.principals.PrincipalObj$PrincipalObjType
+org.apache.polaris.persistence.nosql.coretypes.principals.PrincipalRoleObj$PrincipalRoleObjType
+org.apache.polaris.persistence.nosql.coretypes.principals.PrincipalRolesObj$PrincipalRolesObjType
+org.apache.polaris.persistence.nosql.coretypes.principals.PrincipalsObj$PrincipalsObjType
+
+org.apache.polaris.persistence.nosql.coretypes.realm.RootObj$RootObjType
+org.apache.polaris.persistence.nosql.coretypes.realm.ImmediateTaskObj$ImmediateTaskObjType
+org.apache.polaris.persistence.nosql.coretypes.realm.ImmediateTasksObj$ImmediateTasksObjType
+org.apache.polaris.persistence.nosql.coretypes.realm.RealmGrantsObj$RealmGrantsObjType
+org.apache.polaris.persistence.nosql.coretypes.realm.PolicyMappingsObj$PolicyMappingsObjType
diff --git a/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/catalog/TestEntityIdSet.java b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/catalog/TestEntityIdSet.java
new file mode 100644
index 000000000..f5fe15b29
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/catalog/TestEntityIdSet.java
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.catalog;
+
+import static java.lang.Long.MAX_VALUE;
+import static org.apache.polaris.persistence.nosql.coretypes.catalog.EntityIdSet.ENTITY_ID_SET_SERIALIZER;
+
+import java.nio.ByteBuffer;
+import java.util.Set;
+import java.util.stream.Collectors;
+import java.util.stream.LongStream;
+import java.util.stream.Stream;
+import org.assertj.core.api.SoftAssertions;
+import org.assertj.core.api.junit.jupiter.InjectSoftAssertions;
+import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+@ExtendWith(SoftAssertionsExtension.class)
+public class TestEntityIdSet {
+ @InjectSoftAssertions protected SoftAssertions soft;
+
+ @Test
+ public void negativeIds() {
+ soft.assertThatIllegalStateException().isThrownBy(() -> EntityIdSet.entityIdSet(Set.of(-1L)));
+ soft.assertThatIllegalStateException()
+ .isThrownBy(() -> EntityIdSet.entityIdSet(Set.of(Long.MIN_VALUE, 0L)));
+ soft.assertThatIllegalStateException()
+ .isThrownBy(() -> EntityIdSet.entityIdSet(Set.of(0L, 1L, -1L)));
+ }
+
+ @ParameterizedTest
+ @MethodSource
+ public void entityIdSetSerialization(EntityIdSet objIds) {
+ var serSize = ENTITY_ID_SET_SERIALIZER.serializedSize(objIds);
+ var buffer = ByteBuffer.allocate(serSize + 10);
+ ENTITY_ID_SET_SERIALIZER.serialize(objIds, buffer);
+ soft.assertThat(buffer)
+ .extracting(ByteBuffer::position, ByteBuffer::remaining)
+ .containsExactly(serSize, 10);
+ buffer.put(new byte[10]);
+ soft.assertThat(buffer)
+ .extracting(ByteBuffer::position, ByteBuffer::remaining)
+ .containsExactly(serSize + 10, 0);
+
+ buffer.flip();
+
+ soft.assertThat(buffer)
+ .extracting(ByteBuffer::position, ByteBuffer::remaining)
+ .containsExactly(0, serSize + 10);
+
+ var skip = buffer.duplicate();
+ ENTITY_ID_SET_SERIALIZER.skip(skip);
+ soft.assertThat(skip)
+ .extracting(ByteBuffer::position, ByteBuffer::remaining)
+ .containsExactly(serSize, 10);
+
+ var deser = buffer.duplicate();
+ var deserialized = ENTITY_ID_SET_SERIALIZER.deserialize(deser);
+ soft.assertThat(deser)
+ .extracting(ByteBuffer::position, ByteBuffer::remaining)
+ .containsExactly(serSize, 10);
+ soft.assertThat(deserialized)
+ .isEqualTo(objIds == null || objIds.entityIds().isEmpty() ? null : objIds);
+ }
+
+ static Stream<EntityIdSet> entityIdSetSerialization() {
+ return Stream.of(
+ null,
+ EntityIdSet.entityIdSet(Set.of()),
+ EntityIdSet.entityIdSet(Set.of(0L)),
+ EntityIdSet.entityIdSet(Set.of(1L)),
+ EntityIdSet.entityIdSet(Set.of(1L, 2L, 3L)),
+ EntityIdSet.entityIdSet(LongStream.range(0, 10000).boxed().collect(Collectors.toSet())),
+ EntityIdSet.entityIdSet(
+ LongStream.range(MAX_VALUE - 1000, MAX_VALUE).boxed().collect(Collectors.toSet())));
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/changes/TestChanges.java b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/changes/TestChanges.java
new file mode 100644
index 000000000..b72201ddb
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/changes/TestChanges.java
@@ -0,0 +1,81 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.changes;
+
+import static org.apache.polaris.persistence.nosql.api.index.IndexKey.key;
+import static org.apache.polaris.persistence.nosql.coretypes.changes.Change.CHANGE_SERIALIZER;
+
+import java.nio.ByteBuffer;
+import java.util.stream.Stream;
+import org.assertj.core.api.SoftAssertions;
+import org.assertj.core.api.junit.jupiter.InjectSoftAssertions;
+import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+@ExtendWith(SoftAssertionsExtension.class)
+public class TestChanges {
+ @InjectSoftAssertions protected SoftAssertions soft;
+
+ @ParameterizedTest
+ @MethodSource
+ public void changeSerialization(Change mapping) {
+ var serSize = CHANGE_SERIALIZER.serializedSize(mapping);
+ var buffer = ByteBuffer.allocate(serSize + 10);
+ CHANGE_SERIALIZER.serialize(mapping, buffer);
+ soft.assertThat(buffer)
+ .extracting(ByteBuffer::position, ByteBuffer::remaining)
+ .containsExactly(serSize, 10);
+ buffer.put(new byte[10]);
+ soft.assertThat(buffer)
+ .extracting(ByteBuffer::position, ByteBuffer::remaining)
+ .containsExactly(serSize + 10, 0);
+
+ buffer.flip();
+
+ soft.assertThat(buffer)
+ .extracting(ByteBuffer::position, ByteBuffer::remaining)
+ .containsExactly(0, serSize + 10);
+
+ var skip = buffer.duplicate();
+ CHANGE_SERIALIZER.skip(skip);
+ soft.assertThat(skip)
+ .extracting(ByteBuffer::position, ByteBuffer::remaining)
+ .containsExactly(serSize, 10);
+
+ var deser = buffer.duplicate();
+ var deserialized = CHANGE_SERIALIZER.deserialize(deser);
+ soft.assertThat(deser)
+ .extracting(ByteBuffer::position, ByteBuffer::remaining)
+ .containsExactly(serSize, 10);
+ soft.assertThat(deserialized).isEqualTo(mapping);
+ }
+
+ static Stream<Change> changeSerialization() {
+ return Stream.of(
+ null,
+ ChangeAdd.builder().build(),
+ ChangeRemove.builder().build(),
+ ChangeRename.builder().renameFrom(key("foo")).build(),
+ ChangeRename.builder().renameFrom(key(42L)).build(),
+ ChangeUpdate.builder().build());
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/BaseTestMapping.java b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/BaseTestMapping.java
new file mode 100644
index 000000000..9397e2b68
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/BaseTestMapping.java
@@ -0,0 +1,553 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import static java.lang.String.format;
+import static org.apache.polaris.core.entity.PolarisEntityConstants.getStorageConfigInfoPropertyName;
+import static org.apache.polaris.core.entity.PolarisEntityConstants.getStorageIntegrationIdentifierPropertyName;
+import static org.apache.polaris.persistence.nosql.api.obj.ObjSerializationHelper.contextualReader;
+import static org.apache.polaris.persistence.nosql.coretypes.mapping.BaseTestMapping.MappingSample.entityWithInternalProperties;
+import static org.apache.polaris.persistence.nosql.coretypes.mapping.BaseTestMapping.MappingSample.entityWithInternalProperty;
+import static org.assertj.core.api.Assertions.assertThatCode;
+import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.io.IOException;
+import java.time.Instant;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Optional;
+import java.util.OptionalLong;
+import java.util.function.Supplier;
+import java.util.stream.LongStream;
+import java.util.stream.Stream;
+import org.apache.polaris.core.entity.EntityNameLookupRecord;
+import org.apache.polaris.core.entity.PolarisBaseEntity;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.storage.aws.AwsStorageConfigurationInfo;
+import org.apache.polaris.persistence.nosql.api.obj.BaseCommitObj;
+import org.apache.polaris.persistence.nosql.api.obj.ObjRef;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.ObjBase;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogBaseObj;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogStorageObj;
+import org.apache.polaris.persistence.nosql.coretypes.content.ContentObj;
+import org.assertj.core.api.SoftAssertions;
+import org.assertj.core.api.junit.jupiter.InjectSoftAssertions;
+import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.params.Parameter;
+import org.junit.jupiter.params.ParameterizedClass;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+@ParameterizedClass
+@ExtendWith(SoftAssertionsExtension.class)
+public abstract class BaseTestMapping {
+ @InjectSoftAssertions public SoftAssertions soft;
+
+ @Parameter static BaseTestParameter parameter;
+
+ public static ObjectMapper objectMapper;
+
+ @BeforeAll
+ public static void beforeAll() {
+ objectMapper = new ObjectMapper().findAndRegisterModules();
+ }
+
+ public BaseMapping<?, ?> mapping;
+
+ @BeforeEach
+ public void setUp() {
+ mapping = EntityObjMappings.byEntityType(parameter.entityType);
+ }
+
+ public abstract Class<? extends ObjBase> baseObjClass();
+
+ public abstract ObjType containerObjType();
+
+ public abstract String refName();
+
+ public abstract boolean isCatalogRelated();
+
+ public abstract boolean isCatalogContent();
+
+ public abstract boolean isWithStorage();
+
+ @Test
+ public void containerAndPoly() {
+ if (containerObjType() != null) {
+ soft.assertThat(mapping.containerObjType).isSameAs(containerObjType());
+ soft.assertThat(EntityObjMappings.containerTypeForEntityType(mapping.entityType()))
+ .isSameAs(containerObjType().targetClass());
+ soft.assertThat(mapping.objTypeForSubType(parameter.subType)).isSameAs(parameter.objType);
+ } else {
+ soft.assertThat(mapping.objType).isSameAs(parameter.objType);
+ soft.assertThatIllegalArgumentException()
+ .isThrownBy(() -> EntityObjMappings.containerTypeForEntityType(mapping.entityType()));
+ }
+ if (baseObjClass() != null) {
+ if (containerObjType() != null) {
+ soft.assertThat(mapping.objTypeForSubType(parameter.subType).targetClass())
+ .isAssignableTo(baseObjClass());
+ soft.assertThat(mapping.objType).isNull();
+ } else {
+ soft.assertThat(mapping.objType).isNotNull();
+ soft.assertThat(mapping.objType.targetClass()).isAssignableTo(baseObjClass());
+ }
+ } else {
+ soft.assertThat(mapping.objType).isNotNull();
+ }
+ }
+
+ @Test
+ public void catalogStorage() {
+ if (isWithStorage()) {
+ soft.assertThat(parameter.objType.targetClass()).isAssignableTo(CatalogStorageObj.class);
+ }
+ if (isCatalogContent()) {
+ soft.assertThat(parameter.objType.targetClass()).isAssignableTo(ContentObj.class);
+ soft.assertThat(mapping).isInstanceOf(BaseCatalogContentMapping.class);
+ }
+ if (isCatalogRelated()) {
+ soft.assertThat(parameter.objType.targetClass()).isAssignableTo(CatalogBaseObj.class);
+ soft.assertThat(mapping).isInstanceOf(BaseCatalogMapping.class);
+ soft.assertThat(LongStream.of(1, 42, 1000))
+ .allSatisfy(
+ c -> assertThatCode(() -> mapping.checkCatalogId(c)).doesNotThrowAnyException());
+ soft.assertThat(LongStream.of(0, -1, -42, -1000))
+ .allSatisfy(
+ c ->
+ assertThatIllegalArgumentException().isThrownBy(() -> mapping.checkCatalogId(c)));
+ } else {
+ soft.assertThatCode(() -> mapping.checkCatalogId(0)).doesNotThrowAnyException();
+ soft.assertThat(LongStream.of(1, 42, 1000, -1, -42, -1000))
+ .allSatisfy(
+ c ->
+ assertThatIllegalArgumentException().isThrownBy(() -> mapping.checkCatalogId(c)));
+ }
+ }
+
+ @Test
+ public void basics() {
+ soft.assertThat(mapping.entityType()).isEqualTo(parameter.entityType);
+ soft.assertThat(mapping.catalogContent()).isEqualTo(isCatalogContent());
+
+ soft.assertThat(EntityObjMappings.isCatalogContent(parameter.entityType))
+ .isEqualTo(isCatalogContent());
+ soft.assertThat(EntityObjMappings.isCatalogContent(parameter.entityType.getCode()))
+ .isEqualTo(isCatalogContent());
+
+ soft.assertThatCode(() -> mapping.validateSubType(parameter.subType))
+ .doesNotThrowAnyException();
+ }
+
+ @Test
+ public void referenceName() {
+ soft.assertThat(mapping.refName).isEqualTo(refName());
+
+ if (isCatalogRelated()) {
+ soft.assertThat(mapping.refNameForCatalog(1)).isEqualTo(format(refName(), 1L));
+ soft.assertThatIllegalArgumentException().isThrownBy(() -> mapping.refNameForCatalog(0));
+
+ // Static via EntityObjMappings
+ soft.assertThat(EntityObjMappings.referenceName(mapping.entityType(), 1))
+ .isEqualTo(format(refName(), 1L));
+ soft.assertThat(EntityObjMappings.referenceName(mapping.entityType(), OptionalLong.of(1)))
+ .isEqualTo(format(refName(), 1L));
+ soft.assertThatIllegalArgumentException()
+ .isThrownBy(() -> EntityObjMappings.referenceName(mapping.entityType(), 0));
+ soft.assertThatIllegalArgumentException()
+ .isThrownBy(
+ () -> EntityObjMappings.referenceName(mapping.entityType(), OptionalLong.of(0)));
+ soft.assertThatIllegalArgumentException()
+ .isThrownBy(
+ () -> EntityObjMappings.referenceName(mapping.entityType(), OptionalLong.empty()));
+ } else {
+ soft.assertThat(mapping.refNameForCatalog(0)).isEqualTo(refName());
+ soft.assertThatIllegalArgumentException().isThrownBy(() -> mapping.refNameForCatalog(1));
+
+ // Static via EntityObjMappings
+ soft.assertThat(EntityObjMappings.referenceName(mapping.entityType(), 0))
+ .isEqualTo(refName());
+ soft.assertThat(EntityObjMappings.referenceName(mapping.entityType(), OptionalLong.of(0)))
+ .isEqualTo(refName());
+ soft.assertThat(EntityObjMappings.referenceName(mapping.entityType(), OptionalLong.empty()))
+ .isEqualTo(refName());
+ soft.assertThatIllegalArgumentException()
+ .isThrownBy(() -> EntityObjMappings.referenceName(mapping.entityType(), 1));
+ soft.assertThatIllegalArgumentException()
+ .isThrownBy(
+ () -> EntityObjMappings.referenceName(mapping.entityType(), OptionalLong.of(1)));
+ }
+ }
+
+ @ParameterizedTest
+ @MethodSource("objEntityMapping")
+ public void objEntityMapping(MappingSample sample) throws IOException {
+ var catalogId = sample.entity().getCatalogId();
+ soft.assertThatCode(() -> mapping.checkCatalogId(catalogId)).doesNotThrowAnyException();
+
+ var entity = mapping.mapToEntity(sample.obj(), catalogId);
+ var entityToObjBuilder = mapping.mapToObj(entity, Optional.empty()).id(sample.obj().id());
+ if (sample.obj() instanceof BaseCommitObj baseCommitObj) {
+ //noinspection RedundantClassCall
+ BaseCommitObj.Builder.class
+ .cast(entityToObjBuilder)
+ .seq(baseCommitObj.seq())
+ .tail(baseCommitObj.tail())
+ .build();
+ }
+ var entityToObj = entityToObjBuilder.build();
+
+ var objectWriter = objectMapper.writer().withView(Object.class);
+ var objectReader =
+ contextualReader(
+ objectMapper,
+ sample.obj().type(),
+ sample.obj().id(),
+ sample.obj().numParts(),
+ null,
+ 0L);
+
+ soft.assertThat(entityToObj).isEqualTo(sample.obj());
+ entityEquals(entity, sample.entity());
+
+ // serialization roundtrip
+ var objAsJson = objectWriter.writeValueAsString(entityToObj);
+ var reserialized =
+ objectReader.readValue(
+ objAsJson, mapping.objTypeForSubType(entity.getSubType()).targetClass());
+ soft.assertThat(reserialized)
+ .isEqualTo(entityToObj)
+ .isInstanceOf(mapping.objTypeForSubType(entity.getSubType()).targetClass());
+
+ // Via EntityObjMapping
+ entity = EntityObjMappings.mapToEntity(sample.obj(), catalogId);
+ entityToObjBuilder = EntityObjMappings.mapToObj(entity, Optional.empty()).id(sample.obj().id());
+ if (sample.obj() instanceof BaseCommitObj baseCommitObj) {
+ //noinspection RedundantClassCall
+ BaseCommitObj.Builder.class
+ .cast(entityToObjBuilder)
+ .seq(baseCommitObj.seq())
+ .tail(baseCommitObj.tail())
+ .build();
+ }
+ entityToObj = entityToObjBuilder.build();
+
+ soft.assertThat(entityToObj).isEqualTo(sample.obj());
+ entityEquals(entity, sample.entity());
+
+ // serialization roundtrip
+ objAsJson = objectWriter.writeValueAsString(entityToObj);
+ reserialized =
+ objectReader.readValue(
+ objAsJson, mapping.objTypeForSubType(entity.getSubType()).targetClass());
+ soft.assertThat(reserialized)
+ .isEqualTo(entityToObj)
+ .isInstanceOf(mapping.objTypeForSubType(entity.getSubType()).targetClass());
+
+ //
+ var entityNameLookupRecord =
+ EntityObjMappings.mapToEntityNameLookupRecord(sample.obj(), catalogId);
+ soft.assertThat(entityNameLookupRecord)
+ .extracting(
+ EntityNameLookupRecord::getCatalogId,
+ EntityNameLookupRecord::getParentId,
+ EntityNameLookupRecord::getType,
+ EntityNameLookupRecord::getTypeCode,
+ EntityNameLookupRecord::getSubType,
+ EntityNameLookupRecord::getSubTypeCode,
+ EntityNameLookupRecord::getName,
+ EntityNameLookupRecord::getId)
+ .containsExactly(
+ catalogId,
+ entity.getParentId(),
+ entity.getType(),
+ entity.getTypeCode(),
+ entity.getSubType(),
+ entity.getSubTypeCode(),
+ entity.getName(),
+ entity.getId());
+
+ var entitySubType =
+ EntityObjMappings.entitySubTypeCodeFromObjType(
+ ObjRef.objRef(sample.obj().type(), sample.obj().id()));
+ soft.assertThat(entitySubType).isEqualTo(Integer.toString(sample.entity().getSubTypeCode()));
+ }
+
+ public void entityEquals(PolarisBaseEntity a, PolarisBaseEntity b) {
+ soft.assertThat(a.getClass()).isEqualTo(b.getClass());
+ soft.assertThat(a)
+ .extracting(
+ PolarisBaseEntity::getType,
+ PolarisBaseEntity::getTypeCode,
+ PolarisBaseEntity::getSubType,
+ PolarisBaseEntity::getSubTypeCode,
+ PolarisBaseEntity::getCreateTimestamp,
+ PolarisBaseEntity::getLastUpdateTimestamp,
+ PolarisBaseEntity::getGrantRecordsVersion,
+ PolarisBaseEntity::getEntityVersion,
+ PolarisBaseEntity::getParentId,
+ PolarisBaseEntity::getPropertiesAsMap,
+ PolarisBaseEntity::getInternalPropertiesAsMap)
+ .containsExactly(
+ b.getType(),
+ b.getTypeCode(),
+ b.getSubType(),
+ b.getSubTypeCode(),
+ b.getCreateTimestamp(),
+ b.getLastUpdateTimestamp(),
+ b.getGrantRecordsVersion(),
+ b.getEntityVersion(),
+ b.getParentId(),
+ b.getPropertiesAsMap(),
+ b.getInternalPropertiesAsMap());
+ }
+
+ static Stream<MappingSample> objEntityMapping() {
+ var entity = buildWithDefaults(parameter.entityBuilder()).build();
+ var obj = buildWithDefaults(parameter.objBuilder()).build();
+ return parameter
+ .typeVariations(new MappingSample(obj, entity))
+ .flatMap(
+ base ->
+ Stream.of(
+ //
+ base.varyCreatedTimestamp(parameter.objBuilder()),
+ base.varyUpdatedTimestamp(parameter.objBuilder()),
+ base.varyProperties(parameter.objBuilder()),
+ base.varyInternalProperties(parameter.objBuilder()),
+ base.varyEntityVersion(parameter.objBuilder()),
+ base.varyParentId(parameter.objBuilder()),
+ //
+ base.varyCreatedTimestamp(parameter.objBuilder())
+ .varyUpdatedTimestamp(parameter.objBuilder())
+ .varyProperties(parameter.objBuilder())
+ .varyInternalProperties(parameter.objBuilder())
+ .varyEntityVersion(parameter.objBuilder())
+ .varyParentId(parameter.objBuilder())
+ //
+ ));
+ }
+
+ protected PolarisBaseEntity.Builder entityBuilder(
+ PolarisEntitySubType subType, long entityId, String name) {
+ return new PolarisBaseEntity.Builder()
+ .typeCode(parameter.entityType.getCode())
+ .subTypeCode(subType.getCode())
+ .id(entityId)
+ .name(name);
+ }
+
+ public record MappingSample(ObjBase obj, PolarisBaseEntity entity) {
+ public MappingSample varyCreatedTimestamp(ObjBase.Builder<?, ?> builder) {
+ return new MappingSample(
+ builder.from(obj).createTimestamp(Instant.ofEpochSecond(888888888L)).build(),
+ new PolarisBaseEntity.Builder(entity)
+ .createTimestamp(Instant.ofEpochSecond(888888888L).toEpochMilli())
+ .build());
+ }
+
+ public MappingSample varyUpdatedTimestamp(ObjBase.Builder<?, ?> builder) {
+ return new MappingSample(
+ builder.from(obj).updateTimestamp(Instant.ofEpochSecond(9999999999L)).build(),
+ new PolarisBaseEntity.Builder(entity)
+ .lastUpdateTimestamp(Instant.ofEpochSecond(9999999999L).toEpochMilli())
+ .build());
+ }
+
+ public static PolarisBaseEntity entityWithProperty(
+ PolarisBaseEntity entity, String key, String value) {
+ var newProps = new HashMap<>(entity.getPropertiesAsMap());
+ newProps.put(key, value);
+ return new PolarisBaseEntity.Builder(entity).propertiesAsMap(newProps).build();
+ }
+
+ public static PolarisBaseEntity entityWithProperties(
+ PolarisBaseEntity entity, Map<String, String> properties) {
+ var newProps = new HashMap<>(entity.getPropertiesAsMap());
+ newProps.putAll(properties);
+ return new PolarisBaseEntity.Builder(entity).propertiesAsMap(newProps).build();
+ }
+
+ public static PolarisBaseEntity entityWithInternalProperty(
+ PolarisBaseEntity entity, String key, String value) {
+ var newProps = new HashMap<>(entity.getInternalPropertiesAsMap());
+ newProps.put(key, value);
+ return new PolarisBaseEntity.Builder(entity).internalPropertiesAsMap(newProps).build();
+ }
+
+ public static PolarisBaseEntity entityWithInternalProperties(
+ PolarisBaseEntity entity, Map<String, String> properties) {
+ var newProps = new HashMap<>(entity.getInternalPropertiesAsMap());
+ newProps.putAll(properties);
+ return new PolarisBaseEntity.Builder(entity).internalPropertiesAsMap(newProps).build();
+ }
+
+ public MappingSample varyProperties(ObjBase.Builder<?, ?> builder) {
+ return new MappingSample(
+ builder.from(obj).putProperty("foo", "bar").putProperty("baz", "meep").build(),
+ entityWithProperty(entityWithProperty(entity, "foo", "bar"), "baz", "meep"));
+ }
+
+ public MappingSample varyInternalProperties(ObjBase.Builder<?, ?> builder) {
+ return new MappingSample(
+ builder
+ .from(obj)
+ .putInternalProperty("foo", "bar")
+ .putInternalProperty("baz", "meep")
+ .build(),
+ entityWithInternalProperty(
+ entityWithInternalProperty(entity, "foo", "bar"), "baz", "meep"));
+ }
+
+ public MappingSample varyEntityVersion(ObjBase.Builder<?, ?> builder) {
+ return new MappingSample(
+ builder.from(obj).entityVersion(6666).build(),
+ new PolarisBaseEntity.Builder(entity).entityVersion(6666).build());
+ }
+
+ public MappingSample varyParentId(ObjBase.Builder<?, ?> builder) {
+ return new MappingSample(
+ builder.from(obj).parentStableId(6666).build(),
+ new PolarisBaseEntity.Builder(entity).parentId(6666).build());
+ }
+ }
+
+ public abstract static class BaseTestParameter {
+ public final PolarisEntityType entityType;
+ public final PolarisEntitySubType subType;
+ public final ObjType objType;
+
+ public BaseTestParameter(
+ PolarisEntityType entityType, PolarisEntitySubType subType, ObjType objType) {
+ this.entityType = entityType;
+ this.subType = subType;
+ this.objType = objType;
+ }
+
+ @Override
+ public String toString() {
+ return subType.name() + " / " + objType.name() + " / " + objType.id();
+ }
+
+ public abstract ObjBase.Builder<?, ?> objBuilder();
+
+ public PolarisBaseEntity.Builder entityBuilder() {
+ return new PolarisBaseEntity.Builder()
+ .typeCode(entityType.getCode())
+ .subTypeCode(subType.getCode());
+ }
+
+ /** Collect type-specific test case variations. */
+ public Stream<MappingSample> typeVariations(MappingSample base) {
+ return Stream.of(base);
+ }
+
+ /** Expands variations with varying storage information. */
+ public Stream<MappingSample> storageVariations(
+ Supplier<CatalogStorageObj.Builder<?, ?>> builderSupplier, MappingSample base) {
+ var storageConfigurationInfo =
+ AwsStorageConfigurationInfo.builder()
+ .addAllowedLocation("s3://bucket/allowed")
+ .endpoint("https://s3.amazonaws.com")
+ .endpointInternal("https://s3.internal.amazonaws.com")
+ .region("us-east-1")
+ .roleARN("arn:aws:iam::123456789012:role/PolarisS3Access")
+ .stsEndpoint("https://sts.amazonaws.com")
+ .stsUnavailable(true)
+ .pathStyleAccess(true)
+ .userARN("arn:aws:iam::123456789012:user/PolarisS3Access")
+ .externalId("external-id")
+ .build();
+ return Stream.of(
+ base,
+ //
+ new MappingSample(
+ builderSupplier
+ .get()
+ .from(base.obj())
+ .storageIntegrationIdentifier("storageIntegId")
+ .build(),
+ entityWithInternalProperty(
+ base.entity(), getStorageIntegrationIdentifierPropertyName(), "storageIntegId")),
+ new MappingSample(
+ builderSupplier
+ .get()
+ .from(base.obj())
+ .storageConfigurationInfo(storageConfigurationInfo)
+ .build(),
+ entityWithInternalProperty(
+ base.entity(),
+ getStorageConfigInfoPropertyName(),
+ storageConfigurationInfo.serialize())),
+ //
+ new MappingSample(
+ builderSupplier
+ .get()
+ .from(base.obj())
+ .storageConfigurationInfo(storageConfigurationInfo)
+ .storageIntegrationIdentifier("storageIntegId")
+ .build(),
+ entityWithInternalProperties(
+ base.entity(),
+ Map.of(
+ getStorageConfigInfoPropertyName(),
+ storageConfigurationInfo.serialize(),
+ getStorageIntegrationIdentifierPropertyName(),
+ "storageIntegId")))
+ //
+ );
+ }
+ }
+
+ public static PolarisBaseEntity.Builder buildWithDefaults(
+ PolarisBaseEntity.Builder entityBuilder) {
+ var props = entityBuilder.build().getPropertiesAsMap();
+ var intProps = entityBuilder.build().getInternalPropertiesAsMap();
+ entityBuilder
+ .id(0L)
+ .name("root")
+ .createTimestamp(Instant.ofEpochSecond(1234567890L).toEpochMilli())
+ .lastUpdateTimestamp(Instant.ofEpochSecond(1299999999L).toEpochMilli())
+ .propertiesAsMap(props)
+ .internalPropertiesAsMap(intProps);
+
+ return entityBuilder;
+ }
+
+ public static <B extends ObjBase.Builder<?, ?>> B buildWithDefaults(B builder) {
+ if (builder instanceof BaseCommitObj.Builder<?, ?> commitBuilder) {
+ commitBuilder.seq(42).tail(new long[0]);
+ }
+ builder
+ .id(42L)
+ .stableId(0L)
+ .name("root")
+ .createTimestamp(Instant.ofEpochSecond(1234567890L))
+ .updateTimestamp(Instant.ofEpochSecond(1299999999L));
+ return builder;
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestCatalogMapping.java b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestCatalogMapping.java
new file mode 100644
index 000000000..0fcbd6797
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestCatalogMapping.java
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import static org.apache.polaris.core.entity.CatalogEntity.CATALOG_TYPE_PROPERTY;
+import static org.apache.polaris.core.entity.CatalogEntity.DEFAULT_BASE_LOCATION_KEY;
+import static org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogsObj.CATALOGS_REF_NAME;
+import static org.apache.polaris.persistence.nosql.coretypes.mapping.BaseTestMapping.MappingSample.entityWithInternalProperty;
+import static org.apache.polaris.persistence.nosql.coretypes.mapping.BaseTestMapping.MappingSample.entityWithProperty;
+
+import java.util.Map;
+import java.util.stream.Stream;
+import org.apache.polaris.core.entity.PolarisBaseEntity;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.ObjBase;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogObj;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogStatus;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogType;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogsObj;
+import org.junit.jupiter.params.provider.MethodSource;
+
+@MethodSource("parameters")
+public class TestCatalogMapping extends BaseTestMapping {
+ @Override
+ public ObjType containerObjType() {
+ return CatalogsObj.TYPE;
+ }
+
+ @Override
+ public Class<? extends ObjBase> baseObjClass() {
+ return null;
+ }
+
+ @Override
+ public String refName() {
+ return CATALOGS_REF_NAME;
+ }
+
+ @Override
+ public boolean isCatalogContent() {
+ return false;
+ }
+
+ @Override
+ public boolean isCatalogRelated() {
+ return false;
+ }
+
+ @Override
+ public boolean isWithStorage() {
+ return true;
+ }
+
+ static Stream<BaseTestParameter> parameters() {
+ return Stream.of(
+ new BaseTestParameter(
+ PolarisEntityType.CATALOG, PolarisEntitySubType.NULL_SUBTYPE, CatalogObj.TYPE) {
+ @Override
+ public CatalogObj.Builder objBuilder() {
+ return CatalogObj.builder()
+ .catalogType(CatalogType.INTERNAL)
+ .status(CatalogStatus.ACTIVE);
+ }
+
+ @Override
+ public PolarisBaseEntity.Builder entityBuilder() {
+ return super.entityBuilder()
+ .internalPropertiesAsMap(Map.of(CATALOG_TYPE_PROPERTY, "INTERNAL"));
+ }
+
+ @Override
+ public Stream<MappingSample> typeVariations(MappingSample base) {
+ return storageVariations(this::objBuilder, base)
+ .flatMap(
+ b ->
+ Stream.of(
+ b,
+ //
+ new MappingSample(
+ objBuilder()
+ .from(b.obj())
+ .catalogType(CatalogType.EXTERNAL)
+ .build(),
+ entityWithInternalProperty(
+ b.entity(), CATALOG_TYPE_PROPERTY, "EXTERNAL")),
+ //
+ new MappingSample(
+ objBuilder()
+ .from(b.obj())
+ .defaultBaseLocation("s3://foo/bar/baz/")
+ .build(),
+ entityWithProperty(
+ b.entity(), DEFAULT_BASE_LOCATION_KEY, "s3://foo/bar/baz/")),
+ //
+ new MappingSample(
+ objBuilder()
+ .from(b.obj())
+ .catalogType(CatalogType.EXTERNAL)
+ .defaultBaseLocation("s3://foo/bar/baz/")
+ .build(),
+ entityWithInternalProperty(
+ entityWithProperty(
+ b.entity(), DEFAULT_BASE_LOCATION_KEY, "s3://foo/bar/baz/"),
+ CATALOG_TYPE_PROPERTY,
+ "EXTERNAL"))
+ //
+ ));
+ }
+ });
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestCatalogRoleMapping.java b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestCatalogRoleMapping.java
new file mode 100644
index 000000000..333c4a430
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestCatalogRoleMapping.java
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import static org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogRolesObj.CATALOG_ROLES_REF_NAME_PATTERN;
+
+import java.util.stream.Stream;
+import org.apache.polaris.core.entity.PolarisBaseEntity;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.ObjBase;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogRoleObj;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogRolesObj;
+import org.junit.jupiter.params.provider.MethodSource;
+
+@MethodSource("parameters")
+public class TestCatalogRoleMapping extends BaseTestMapping {
+ @Override
+ public ObjType containerObjType() {
+ return CatalogRolesObj.TYPE;
+ }
+
+ @Override
+ public Class<? extends ObjBase> baseObjClass() {
+ return null;
+ }
+
+ @Override
+ public String refName() {
+ return CATALOG_ROLES_REF_NAME_PATTERN;
+ }
+
+ @Override
+ public boolean isCatalogContent() {
+ return false;
+ }
+
+ @Override
+ public boolean isCatalogRelated() {
+ return true;
+ }
+
+ @Override
+ public boolean isWithStorage() {
+ return false;
+ }
+
+ static Stream<BaseTestParameter> parameters() {
+ return Stream.of(
+ new BaseTestParameter(
+ PolarisEntityType.CATALOG_ROLE,
+ PolarisEntitySubType.NULL_SUBTYPE,
+ CatalogRoleObj.TYPE) {
+ @Override
+ public ObjBase.Builder<?, ?> objBuilder() {
+ return CatalogRoleObj.builder();
+ }
+
+ @Override
+ public PolarisBaseEntity.Builder entityBuilder() {
+ return super.entityBuilder().catalogId(123L);
+ }
+ });
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestEntityObjMappings.java b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestEntityObjMappings.java
new file mode 100644
index 000000000..7687c7ec2
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestEntityObjMappings.java
@@ -0,0 +1,135 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import java.util.Arrays;
+import java.util.stream.Stream;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.assertj.core.api.SoftAssertions;
+import org.assertj.core.api.junit.jupiter.InjectSoftAssertions;
+import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+@ExtendWith(SoftAssertionsExtension.class)
+public class TestEntityObjMappings {
+ @InjectSoftAssertions protected SoftAssertions soft;
+
+ @Test
+ public void checkCatalogId() {
+ soft.assertThatCode(() -> EntityObjMappings.checkCatalogId(1)).doesNotThrowAnyException();
+ soft.assertThatCode(() -> EntityObjMappings.checkCatalogId(Long.MAX_VALUE))
+ .doesNotThrowAnyException();
+ soft.assertThatIllegalArgumentException().isThrownBy(() -> EntityObjMappings.checkCatalogId(0));
+ soft.assertThatIllegalArgumentException()
+ .isThrownBy(() -> EntityObjMappings.checkCatalogId(-1));
+ soft.assertThatIllegalArgumentException()
+ .isThrownBy(() -> EntityObjMappings.checkCatalogId(Long.MIN_VALUE));
+ }
+
+ @Test
+ public void invalidTypes() {
+ soft.assertThatIllegalArgumentException()
+ .isThrownBy(
+ () ->
+ EntityObjMappings.objTypeForPolarisType(
+ PolarisEntityType.TABLE_LIKE, PolarisEntitySubType.NULL_SUBTYPE))
+ .withMessage("Invalid subType NULL_SUBTYPE for TABLE_LIKE");
+ soft.assertThatIllegalArgumentException()
+ .isThrownBy(
+ () ->
+ EntityObjMappings.objTypeForPolarisType(
+ PolarisEntityType.TABLE_LIKE, PolarisEntitySubType.NULL_SUBTYPE))
+ .withMessage("Invalid subType NULL_SUBTYPE for TABLE_LIKE");
+ soft.assertThatIllegalArgumentException()
+ .isThrownBy(() -> EntityObjMappings.byEntityType(PolarisEntityType.NULL_TYPE))
+ .withMessage("No type mapping for entity type NULL_TYPE");
+ soft.assertThatIllegalArgumentException()
+ .isThrownBy(() -> EntityObjMappings.byEntityTypeCode(PolarisEntityType.NULL_TYPE.getCode()))
+ .withMessage("No type mapping for entity type code 0");
+ soft.assertThatIllegalArgumentException()
+ .isThrownBy(() -> EntityObjMappings.byEntityTypeCode(666))
+ .withMessage("No type mapping for entity type code 666");
+ soft.assertThatIllegalArgumentException()
+ .isThrownBy(() -> EntityObjMappings.byEntityTypeCode(-42))
+ .withMessage("No type mapping for entity type code -42");
+ }
+
+ @ParameterizedTest
+ @MethodSource("entityObjMapping")
+ public void entityObjMapping(PolarisEntityType entityType, PolarisEntitySubType subType) {
+ soft.assertThat(EntityObjMappings.byEntityType(entityType)).isNotNull();
+ soft.assertThat(EntityObjMappings.typeFromCode(entityType.getCode())).isSameAs(entityType);
+ soft.assertThat(EntityObjMappings.byEntityTypeCode(entityType.getCode()))
+ .isNotNull()
+ .isSameAs(EntityObjMappings.byEntityType(entityType));
+
+ soft.assertThatIllegalArgumentException()
+ .isThrownBy(
+ () ->
+ EntityObjMappings.objTypeForPolarisType(
+ entityType, PolarisEntitySubType.ANY_SUBTYPE))
+ .withMessage("Unresolvable subtype ANY_SUBTYPE for exact match for %s", entityType);
+ soft.assertThat(
+ EntityObjMappings.objTypeForPolarisTypeForFiltering(
+ entityType, PolarisEntitySubType.ANY_SUBTYPE))
+ .isNotNull();
+ soft.assertThat(EntityObjMappings.objTypeForPolarisTypeForFiltering(entityType, subType))
+ .isNotNull();
+
+ var objType = EntityObjMappings.objTypeForPolarisType(entityType, subType);
+ soft.assertThat(objType).isNotNull();
+ var entityTypeAndSubType = EntityObjMappings.entityTypeAndSubType(objType);
+ soft.assertThat(entityTypeAndSubType)
+ .isNotNull()
+ .extracting(
+ EntityObjMappings.EntityTypeAndSubType::entityType,
+ EntityObjMappings.EntityTypeAndSubType::subType)
+ .containsExactlyInAnyOrder(entityType, subType);
+ soft.assertThat(entityTypeAndSubType.typeMapping())
+ .extracting(BaseMapping::entityType)
+ .isEqualTo(entityType);
+ soft.assertThatCode(() -> entityTypeAndSubType.typeMapping().validateSubType(subType))
+ .doesNotThrowAnyException();
+ soft.assertThat(entityTypeAndSubType.typeMapping().objTypeForSubType(subType))
+ .isEqualTo(objType);
+ }
+
+ static Stream<Arguments> entityObjMapping() {
+ return Arrays.stream(PolarisEntityType.values())
+ .filter(t -> t != PolarisEntityType.NULL_TYPE)
+ .flatMap(
+ t -> {
+ var subTypes =
+ Arrays.stream(PolarisEntitySubType.values())
+ .filter(s -> s.getParentType() == t)
+ .toList();
+ if (subTypes.isEmpty()) {
+ return Stream.of(Arguments.of(t, PolarisEntitySubType.NULL_SUBTYPE));
+ } else {
+ return subTypes.stream().map(s -> Arguments.of(t, s));
+ }
+ });
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestFileMapping.java b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestFileMapping.java
new file mode 100644
index 000000000..a8ddc932c
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestFileMapping.java
@@ -0,0 +1,72 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import java.util.stream.Stream;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.ObjBase;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.FileObj;
+import org.junit.jupiter.params.provider.MethodSource;
+
+@MethodSource("parameters")
+public class TestFileMapping extends BaseTestMapping {
+ @Override
+ public ObjType containerObjType() {
+ return null;
+ }
+
+ @Override
+ public Class<? extends ObjBase> baseObjClass() {
+ return null;
+ }
+
+ @Override
+ public String refName() {
+ return null;
+ }
+
+ @Override
+ public boolean isCatalogContent() {
+ return false;
+ }
+
+ @Override
+ public boolean isCatalogRelated() {
+ return false;
+ }
+
+ @Override
+ public boolean isWithStorage() {
+ return false;
+ }
+
+ static Stream<BaseTestParameter> parameters() {
+ return Stream.of(
+ new BaseTestParameter(
+ PolarisEntityType.FILE, PolarisEntitySubType.NULL_SUBTYPE, FileObj.TYPE) {
+ @Override
+ public ObjBase.Builder<?, ?> objBuilder() {
+ return FileObj.builder();
+ }
+ });
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestNamespaceMapping.java b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestNamespaceMapping.java
new file mode 100644
index 000000000..77c91d962
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestNamespaceMapping.java
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import static org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogStateObj.CATALOG_STATE_REF_NAME_PATTERN;
+
+import java.util.stream.Stream;
+import org.apache.polaris.core.entity.PolarisBaseEntity;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.ObjBase;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogStateObj;
+import org.apache.polaris.persistence.nosql.coretypes.content.LocalNamespaceObj;
+import org.apache.polaris.persistence.nosql.coretypes.content.NamespaceObj;
+import org.junit.jupiter.params.provider.MethodSource;
+
+@MethodSource("parameters")
+public class TestNamespaceMapping extends BaseTestMapping {
+ @Override
+ public ObjType containerObjType() {
+ return CatalogStateObj.TYPE;
+ }
+
+ @Override
+ public Class<? extends ObjBase> baseObjClass() {
+ return NamespaceObj.class;
+ }
+
+ @Override
+ public String refName() {
+ return CATALOG_STATE_REF_NAME_PATTERN;
+ }
+
+ @Override
+ public boolean isCatalogContent() {
+ return true;
+ }
+
+ @Override
+ public boolean isCatalogRelated() {
+ return true;
+ }
+
+ @Override
+ public boolean isWithStorage() {
+ return false;
+ }
+
+ static Stream<BaseTestParameter> parameters() {
+ return Stream.of(
+ new BaseTestParameter(
+ PolarisEntityType.NAMESPACE,
+ PolarisEntitySubType.NULL_SUBTYPE,
+ LocalNamespaceObj.TYPE) {
+ @Override
+ public ObjBase.Builder<?, ?> objBuilder() {
+ return LocalNamespaceObj.builder();
+ }
+
+ @Override
+ public PolarisBaseEntity.Builder entityBuilder() {
+ return super.entityBuilder().catalogId(123L);
+ }
+ });
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestPolicyMapping.java b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestPolicyMapping.java
new file mode 100644
index 000000000..3b4c96726
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestPolicyMapping.java
@@ -0,0 +1,133 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import static org.apache.polaris.core.policy.PolicyEntity.POLICY_CONTENT_KEY;
+import static org.apache.polaris.core.policy.PolicyEntity.POLICY_DESCRIPTION_KEY;
+import static org.apache.polaris.core.policy.PolicyEntity.POLICY_TYPE_CODE_KEY;
+import static org.apache.polaris.core.policy.PolicyEntity.POLICY_VERSION_KEY;
+import static org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogStateObj.CATALOG_STATE_REF_NAME_PATTERN;
+import static org.apache.polaris.persistence.nosql.coretypes.mapping.BaseTestMapping.MappingSample.entityWithProperties;
+import static org.apache.polaris.persistence.nosql.coretypes.mapping.BaseTestMapping.MappingSample.entityWithProperty;
+
+import java.util.Map;
+import java.util.stream.Stream;
+import org.apache.polaris.core.entity.PolarisBaseEntity;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.policy.PolicyType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.ObjBase;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogStateObj;
+import org.apache.polaris.persistence.nosql.coretypes.content.PolicyObj;
+import org.junit.jupiter.params.provider.MethodSource;
+
+@MethodSource("parameters")
+public class TestPolicyMapping extends BaseTestMapping {
+ @Override
+ public ObjType containerObjType() {
+ return CatalogStateObj.TYPE;
+ }
+
+ @Override
+ public Class<? extends ObjBase> baseObjClass() {
+ return null;
+ }
+
+ @Override
+ public String refName() {
+ return CATALOG_STATE_REF_NAME_PATTERN;
+ }
+
+ @Override
+ public boolean isCatalogContent() {
+ return true;
+ }
+
+ @Override
+ public boolean isCatalogRelated() {
+ return true;
+ }
+
+ @Override
+ public boolean isWithStorage() {
+ return false;
+ }
+
+ static Stream<BaseTestParameter> parameters() {
+ return Stream.of(
+ new BaseTestParameter(
+ PolarisEntityType.POLICY, PolarisEntitySubType.NULL_SUBTYPE, PolicyObj.TYPE) {
+ @Override
+ public PolicyObj.Builder objBuilder() {
+ return PolicyObj.builder().policyType(PolicyType.fromCode(0));
+ }
+
+ @Override
+ public PolarisBaseEntity.Builder entityBuilder() {
+ return super.entityBuilder()
+ .catalogId(123L)
+ .propertiesAsMap(Map.of(POLICY_TYPE_CODE_KEY, "0"));
+ }
+
+ @Override
+ public Stream<MappingSample> typeVariations(MappingSample base) {
+ return Stream.of(
+ base,
+ //
+ new MappingSample(
+ objBuilder().from(base.obj()).policyType(PolicyType.fromCode(1)).build(),
+ entityWithProperty(base.entity(), POLICY_TYPE_CODE_KEY, "1")),
+ new MappingSample(
+ objBuilder().from(base.obj()).description("description").build(),
+ entityWithProperty(base.entity(), POLICY_DESCRIPTION_KEY, "description")),
+ new MappingSample(
+ objBuilder().from(base.obj()).policyVersion(42).build(),
+ entityWithProperty(base.entity(), POLICY_VERSION_KEY, "42")),
+ new MappingSample(
+ objBuilder().from(base.obj()).content("content-key").build(),
+ entityWithProperty(base.entity(), POLICY_CONTENT_KEY, "content-key")),
+ //
+ new MappingSample(
+ objBuilder()
+ .from(base.obj())
+ .policyType(PolicyType.fromCode(1))
+ .description("description")
+ .policyVersion(42)
+ .content("content-key")
+ .content("content-key")
+ .build(),
+ entityWithProperties(
+ base.entity(),
+ Map.of(
+ POLICY_TYPE_CODE_KEY,
+ "1",
+ POLICY_DESCRIPTION_KEY,
+ "description",
+ POLICY_VERSION_KEY,
+ "42",
+ POLICY_CONTENT_KEY,
+ "content-key")))
+ //
+ );
+ }
+ });
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestPrincipalMapping.java b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestPrincipalMapping.java
new file mode 100644
index 000000000..9bdfa4eb5
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestPrincipalMapping.java
@@ -0,0 +1,233 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import static org.apache.polaris.core.entity.PolarisEntityConstants.PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE;
+import static org.apache.polaris.persistence.nosql.coretypes.mapping.BaseTestMapping.MappingSample.entityWithInternalProperty;
+import static org.apache.polaris.persistence.nosql.coretypes.principals.PrincipalsObj.PRINCIPALS_REF_NAME;
+
+import java.util.Optional;
+import java.util.stream.Stream;
+import org.apache.polaris.core.DigestUtils;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.entity.PolarisPrincipalSecrets;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.ObjBase;
+import org.apache.polaris.persistence.nosql.coretypes.principals.PrincipalObj;
+import org.apache.polaris.persistence.nosql.coretypes.principals.PrincipalsObj;
+import org.apache.polaris.persistence.nosql.coretypes.realm.RootObj;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
+
+@MethodSource("parameters")
+public class TestPrincipalMapping extends BaseTestMapping {
+ @Override
+ public ObjType containerObjType() {
+ return PrincipalsObj.TYPE;
+ }
+
+ @Override
+ public Class<? extends ObjBase> baseObjClass() {
+ return null;
+ }
+
+ @Override
+ public String refName() {
+ return PRINCIPALS_REF_NAME;
+ }
+
+ @Override
+ public boolean isCatalogContent() {
+ return false;
+ }
+
+ @Override
+ public boolean isCatalogRelated() {
+ return false;
+ }
+
+ @Override
+ public boolean isWithStorage() {
+ return false;
+ }
+
+ @Test
+ public void noSecrets() {
+ var noPrincipal = buildWithDefaults(RootObj.builder()).build();
+ soft.assertThat(EntityObjMappings.maybeObjToPolarisPrincipalSecrets(noPrincipal)).isEmpty();
+ var noClientId = buildWithDefaults(PrincipalObj.builder()).build();
+ soft.assertThat(EntityObjMappings.maybeObjToPolarisPrincipalSecrets(noClientId)).isEmpty();
+ }
+
+ @Test
+ public void mappingWithSecrets() {
+ var obj =
+ buildWithDefaults(PrincipalObj.builder())
+ .clientId("client-id")
+ .secretSalt("1234")
+ .mainSecretHash(DigestUtils.sha256Hex("1st-secret" + ":" + "1234"))
+ .secondarySecretHash(DigestUtils.sha256Hex("1st-secret" + ":" + "1234"))
+ .build();
+ var entity = EntityObjMappings.mapToEntity(obj, 0);
+ var entityAsObj =
+ EntityObjMappings.mapToObj(
+ entity,
+ Optional.of(
+ new PolarisPrincipalSecrets(
+ obj.stableId(),
+ "client-id",
+ // secrets are not persisted
+ null,
+ null,
+ obj.secretSalt().orElseThrow(),
+ obj.mainSecretHash().orElseThrow(),
+ obj.secondarySecretHash().orElseThrow())))
+ .id(obj.id())
+ .build();
+ soft.assertThat(entityAsObj).isEqualTo(obj);
+
+ entityAsObj =
+ EntityObjMappings.mapToObj(
+ entity,
+ Optional.of(
+ new PolarisPrincipalSecrets(
+ obj.stableId(),
+ "client-id",
+ "1st-secret",
+ "2nd-secret",
+ obj.secretSalt().orElseThrow(),
+ obj.mainSecretHash().orElseThrow(),
+ obj.secondarySecretHash().orElseThrow())))
+ .id(obj.id())
+ .build();
+ soft.assertThat(entityAsObj).isEqualTo(obj);
+ }
+
+ @ParameterizedTest
+ @MethodSource("secrets")
+ public void principalObjToPolarisPrincipalSecrets(
+ PrincipalObj obj, PolarisPrincipalSecrets expected) {
+ var principalSecrets = EntityObjMappings.maybeObjToPolarisPrincipalSecrets(obj).orElseThrow();
+ soft.assertThat(principalSecrets)
+ .extracting(
+ PolarisPrincipalSecrets::getPrincipalId,
+ PolarisPrincipalSecrets::getPrincipalClientId,
+ PolarisPrincipalSecrets::getMainSecret,
+ PolarisPrincipalSecrets::getSecondarySecret,
+ PolarisPrincipalSecrets::getSecretSalt,
+ PolarisPrincipalSecrets::getMainSecretHash,
+ PolarisPrincipalSecrets::getSecondarySecretHash)
+ .containsExactly(
+ expected.getPrincipalId(),
+ expected.getPrincipalClientId(),
+ // secrets are not persisted
+ null, // expected.getMainSecret(),
+ null, // expected.getSecondarySecret(),
+ expected.getSecretSalt(),
+ expected.getMainSecretHash(),
+ expected.getSecondarySecretHash());
+
+ principalSecrets = EntityObjMappings.principalObjToPolarisPrincipalSecrets(obj);
+ soft.assertThat(principalSecrets)
+ .extracting(
+ PolarisPrincipalSecrets::getPrincipalId,
+ PolarisPrincipalSecrets::getPrincipalClientId,
+ PolarisPrincipalSecrets::getMainSecret,
+ PolarisPrincipalSecrets::getSecondarySecret,
+ PolarisPrincipalSecrets::getSecretSalt,
+ PolarisPrincipalSecrets::getMainSecretHash,
+ PolarisPrincipalSecrets::getSecondarySecretHash)
+ .containsExactly(
+ expected.getPrincipalId(),
+ expected.getPrincipalClientId(),
+ // secrets are not persisted
+ null, // expected.getMainSecret(),
+ null, // expected.getSecondarySecret(),
+ expected.getSecretSalt(),
+ expected.getMainSecretHash(),
+ expected.getSecondarySecretHash());
+
+ principalSecrets = EntityObjMappings.principalObjToPolarisPrincipalSecrets(obj, expected);
+ soft.assertThat(principalSecrets)
+ .extracting(
+ PolarisPrincipalSecrets::getPrincipalId,
+ PolarisPrincipalSecrets::getPrincipalClientId,
+ PolarisPrincipalSecrets::getMainSecret,
+ PolarisPrincipalSecrets::getSecondarySecret,
+ PolarisPrincipalSecrets::getSecretSalt,
+ PolarisPrincipalSecrets::getMainSecretHash,
+ PolarisPrincipalSecrets::getSecondarySecretHash)
+ .containsExactly(
+ expected.getPrincipalId(),
+ expected.getPrincipalClientId(),
+ expected.getMainSecret(),
+ expected.getSecondarySecret(),
+ expected.getSecretSalt(),
+ expected.getMainSecretHash(),
+ expected.getSecondarySecretHash());
+ }
+
+ static Stream<Arguments> secrets() {
+ var salt = "1234";
+ return Stream.of(
+ Arguments.of(
+ buildWithDefaults(PrincipalObj.builder())
+ .clientId("client-id")
+ .secretSalt(salt)
+ .build(),
+ new PolarisPrincipalSecrets(0L, "client-id", null, null, salt, null, null)),
+ Arguments.of(
+ buildWithDefaults(PrincipalObj.builder())
+ .clientId("client-id")
+ .secretSalt(salt)
+ .mainSecretHash(DigestUtils.sha256Hex("1st-secret" + ":" + salt))
+ .secondarySecretHash(DigestUtils.sha256Hex("2nd-secret" + ":" + salt))
+ .build(),
+ new PolarisPrincipalSecrets(
+ 0L, "client-id", "1st-secret", "2nd-secret", salt, null, null)));
+ }
+
+ static Stream<BaseTestParameter> parameters() {
+ return Stream.of(
+ new BaseTestParameter(
+ PolarisEntityType.PRINCIPAL, PolarisEntitySubType.NULL_SUBTYPE, PrincipalObj.TYPE) {
+ @Override
+ public PrincipalObj.Builder objBuilder() {
+ return PrincipalObj.builder();
+ }
+
+ @Override
+ public Stream<MappingSample> typeVariations(MappingSample base) {
+ return Stream.of(
+ base,
+ //
+ new MappingSample(
+ objBuilder().from(base.obj()).credentialRotationRequired(true).build(),
+ entityWithInternalProperty(
+ base.entity(), PRINCIPAL_CREDENTIAL_ROTATION_REQUIRED_STATE, "true"))
+ //
+ );
+ }
+ });
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestPrincipalRoleMapping.java b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestPrincipalRoleMapping.java
new file mode 100644
index 000000000..674e75bf0
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestPrincipalRoleMapping.java
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import static org.apache.polaris.core.entity.table.federated.FederatedEntities.FEDERATED_ENTITY;
+import static org.apache.polaris.persistence.nosql.coretypes.mapping.BaseTestMapping.MappingSample.entityWithInternalProperty;
+import static org.apache.polaris.persistence.nosql.coretypes.principals.PrincipalRolesObj.PRINCIPAL_ROLES_REF_NAME;
+
+import java.util.stream.Stream;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.ObjBase;
+import org.apache.polaris.persistence.nosql.coretypes.principals.PrincipalRoleObj;
+import org.apache.polaris.persistence.nosql.coretypes.principals.PrincipalRolesObj;
+import org.junit.jupiter.params.provider.MethodSource;
+
+@MethodSource("parameters")
+public class TestPrincipalRoleMapping extends BaseTestMapping {
+ @Override
+ public ObjType containerObjType() {
+ return PrincipalRolesObj.TYPE;
+ }
+
+ @Override
+ public Class<? extends ObjBase> baseObjClass() {
+ return null;
+ }
+
+ @Override
+ public String refName() {
+ return PRINCIPAL_ROLES_REF_NAME;
+ }
+
+ @Override
+ public boolean isCatalogContent() {
+ return false;
+ }
+
+ @Override
+ public boolean isCatalogRelated() {
+ return false;
+ }
+
+ @Override
+ public boolean isWithStorage() {
+ return false;
+ }
+
+ static Stream<BaseTestParameter> parameters() {
+ return Stream.of(
+ new BaseTestParameter(
+ PolarisEntityType.PRINCIPAL_ROLE,
+ PolarisEntitySubType.NULL_SUBTYPE,
+ PrincipalRoleObj.TYPE) {
+ @Override
+ public PrincipalRoleObj.Builder objBuilder() {
+ return PrincipalRoleObj.builder();
+ }
+
+ @Override
+ public Stream<MappingSample> typeVariations(MappingSample base) {
+ return Stream.of(
+ base,
+ //
+ new MappingSample(
+ objBuilder().from(base.obj()).federated(true).build(),
+ entityWithInternalProperty(base.entity(), FEDERATED_ENTITY, "true"))
+ //
+ );
+ }
+ });
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestRootMapping.java b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestRootMapping.java
new file mode 100644
index 000000000..11ae7d0ec
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestRootMapping.java
@@ -0,0 +1,74 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import static org.apache.polaris.persistence.nosql.coretypes.realm.RootObj.ROOT_REF_NAME;
+
+import java.util.stream.Stream;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.ObjBase;
+import org.apache.polaris.persistence.nosql.coretypes.realm.RootObj;
+import org.junit.jupiter.params.provider.MethodSource;
+
+@MethodSource("parameters")
+public class TestRootMapping extends BaseTestMapping {
+ @Override
+ public ObjType containerObjType() {
+ return null;
+ }
+
+ @Override
+ public Class<? extends ObjBase> baseObjClass() {
+ return null;
+ }
+
+ @Override
+ public String refName() {
+ return ROOT_REF_NAME;
+ }
+
+ @Override
+ public boolean isCatalogContent() {
+ return false;
+ }
+
+ @Override
+ public boolean isCatalogRelated() {
+ return false;
+ }
+
+ @Override
+ public boolean isWithStorage() {
+ return false;
+ }
+
+ static Stream<BaseTestParameter> parameters() {
+ return Stream.of(
+ new BaseTestParameter(
+ PolarisEntityType.ROOT, PolarisEntitySubType.NULL_SUBTYPE, RootObj.TYPE) {
+ @Override
+ public ObjBase.Builder<?, ?> objBuilder() {
+ return RootObj.builder();
+ }
+ });
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestTableLikeMapping.java b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestTableLikeMapping.java
new file mode 100644
index 000000000..386453f24
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestTableLikeMapping.java
@@ -0,0 +1,160 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import static org.apache.polaris.core.entity.table.GenericTableEntity.DOC_KEY;
+import static org.apache.polaris.core.entity.table.GenericTableEntity.FORMAT_KEY;
+import static org.apache.polaris.core.entity.table.IcebergTableLikeEntity.METADATA_LOCATION_KEY;
+import static org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogStateObj.CATALOG_STATE_REF_NAME_PATTERN;
+import static org.apache.polaris.persistence.nosql.coretypes.mapping.BaseTestMapping.MappingSample.entityWithInternalProperties;
+import static org.apache.polaris.persistence.nosql.coretypes.mapping.BaseTestMapping.MappingSample.entityWithInternalProperty;
+
+import java.util.Map;
+import java.util.stream.Stream;
+import org.apache.polaris.core.entity.PolarisBaseEntity;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.ObjBase;
+import org.apache.polaris.persistence.nosql.coretypes.catalog.CatalogStateObj;
+import org.apache.polaris.persistence.nosql.coretypes.content.GenericTableObj;
+import org.apache.polaris.persistence.nosql.coretypes.content.IcebergTableObj;
+import org.apache.polaris.persistence.nosql.coretypes.content.IcebergViewObj;
+import org.apache.polaris.persistence.nosql.coretypes.content.TableLikeObj;
+import org.junit.jupiter.params.provider.MethodSource;
+
+@MethodSource("parameters")
+public class TestTableLikeMapping extends BaseTestMapping {
+
+ @Override
+ public ObjType containerObjType() {
+ return CatalogStateObj.TYPE;
+ }
+
+ @Override
+ public Class<? extends ObjBase> baseObjClass() {
+ return TableLikeObj.class;
+ }
+
+ @Override
+ public String refName() {
+ return CATALOG_STATE_REF_NAME_PATTERN;
+ }
+
+ @Override
+ public boolean isCatalogContent() {
+ return true;
+ }
+
+ @Override
+ public boolean isCatalogRelated() {
+ return true;
+ }
+
+ @Override
+ public boolean isWithStorage() {
+ return false;
+ }
+
+ abstract static class TableLikeParameter extends BaseTestParameter {
+ TableLikeParameter(PolarisEntitySubType subType, ObjType objType) {
+ super(PolarisEntityType.TABLE_LIKE, subType, objType);
+ }
+
+ @Override
+ public PolarisBaseEntity.Builder entityBuilder() {
+ return super.entityBuilder().catalogId(123L);
+ }
+ }
+
+ // TODO PARENT_NAMESPACE_KEY
+
+ static Stream<BaseTestParameter> parameters() {
+ return Stream.of(
+ new TableLikeParameter(PolarisEntitySubType.ICEBERG_TABLE, IcebergTableObj.TYPE) {
+ @Override
+ public IcebergTableObj.Builder objBuilder() {
+ return IcebergTableObj.builder();
+ }
+
+ @Override
+ public Stream<MappingSample> typeVariations(MappingSample base) {
+ return storageVariations(this::objBuilder, base);
+ }
+ },
+ new TableLikeParameter(PolarisEntitySubType.ICEBERG_VIEW, IcebergViewObj.TYPE) {
+ @Override
+ public IcebergViewObj.Builder objBuilder() {
+ return IcebergViewObj.builder();
+ }
+
+ @Override
+ public Stream<MappingSample> typeVariations(MappingSample base) {
+ return storageVariations(this::objBuilder, base);
+ }
+ },
+ new TableLikeParameter(PolarisEntitySubType.GENERIC_TABLE, GenericTableObj.TYPE) {
+ @Override
+ public GenericTableObj.Builder objBuilder() {
+ return GenericTableObj.builder();
+ }
+
+ @Override
+ public Stream<MappingSample> typeVariations(MappingSample base) {
+ return storageVariations(this::objBuilder, base)
+ .flatMap(
+ b ->
+ Stream.of(
+ b,
+ //
+ new MappingSample(
+ objBuilder()
+ .from(b.obj())
+ .metadataLocation("s3://foo/bar/here.json")
+ .build(),
+ entityWithInternalProperty(
+ b.entity(), METADATA_LOCATION_KEY, "s3://foo/bar/here.json")),
+ new MappingSample(
+ objBuilder().from(b.obj()).doc("doc").build(),
+ entityWithInternalProperty(b.entity(), DOC_KEY, "doc")),
+ new MappingSample(
+ objBuilder().from(b.obj()).format("format").build(),
+ entityWithInternalProperty(b.entity(), FORMAT_KEY, "format")),
+ //
+ new MappingSample(
+ objBuilder()
+ .from(b.obj())
+ .metadataLocation("s3://foo/bar/here.json")
+ .doc("doc")
+ .format("format")
+ .build(),
+ entityWithInternalProperties(
+ b.entity(),
+ Map.of(
+ METADATA_LOCATION_KEY,
+ "s3://foo/bar/here.json",
+ DOC_KEY,
+ "doc",
+ FORMAT_KEY,
+ "format")))));
+ }
+ });
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestTaskMapping.java b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestTaskMapping.java
new file mode 100644
index 000000000..7444b2000
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/mapping/TestTaskMapping.java
@@ -0,0 +1,150 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.mapping;
+
+import static org.apache.polaris.core.entity.PolarisTaskConstants.ATTEMPT_COUNT;
+import static org.apache.polaris.core.entity.PolarisTaskConstants.LAST_ATTEMPT_EXECUTOR_ID;
+import static org.apache.polaris.core.entity.PolarisTaskConstants.LAST_ATTEMPT_START_TIME;
+import static org.apache.polaris.core.entity.PolarisTaskConstants.TASK_DATA;
+import static org.apache.polaris.core.entity.PolarisTaskConstants.TASK_TYPE;
+import static org.apache.polaris.persistence.nosql.coretypes.mapping.BaseTestMapping.MappingSample.entityWithProperties;
+import static org.apache.polaris.persistence.nosql.coretypes.mapping.BaseTestMapping.MappingSample.entityWithProperty;
+import static org.apache.polaris.persistence.nosql.coretypes.mapping.TaskMapping.serializeStringCompressed;
+import static org.apache.polaris.persistence.nosql.coretypes.realm.ImmediateTasksObj.IMMEDIATE_TASKS_REF_NAME;
+
+import java.time.Instant;
+import java.util.Map;
+import java.util.stream.Stream;
+import org.apache.polaris.core.entity.AsyncTaskType;
+import org.apache.polaris.core.entity.PolarisEntitySubType;
+import org.apache.polaris.core.entity.PolarisEntityType;
+import org.apache.polaris.core.persistence.PolarisObjectMapperUtil;
+import org.apache.polaris.persistence.nosql.api.obj.ObjType;
+import org.apache.polaris.persistence.nosql.coretypes.ObjBase;
+import org.apache.polaris.persistence.nosql.coretypes.realm.ImmediateTaskObj;
+import org.apache.polaris.persistence.nosql.coretypes.realm.ImmediateTasksObj;
+import org.junit.jupiter.params.provider.MethodSource;
+
+@MethodSource("parameters")
+public class TestTaskMapping extends BaseTestMapping {
+ @Override
+ public ObjType containerObjType() {
+ return ImmediateTasksObj.TYPE;
+ }
+
+ @Override
+ public Class<? extends ObjBase> baseObjClass() {
+ return null;
+ }
+
+ @Override
+ public String refName() {
+ return IMMEDIATE_TASKS_REF_NAME;
+ }
+
+ @Override
+ public boolean isCatalogContent() {
+ return false;
+ }
+
+ @Override
+ public boolean isCatalogRelated() {
+ return false;
+ }
+
+ @Override
+ public boolean isWithStorage() {
+ return false;
+ }
+
+ static Stream<BaseTestParameter> parameters() {
+ var taskData = Map.of("task-foo", "bar", "xyz", "abc");
+ return Stream.of(
+ new BaseTestParameter(
+ PolarisEntityType.TASK, PolarisEntitySubType.NULL_SUBTYPE, ImmediateTaskObj.TYPE) {
+ @Override
+ public ImmediateTaskObj.Builder objBuilder() {
+ return ImmediateTaskObj.builder();
+ }
+
+ @Override
+ public Stream<MappingSample> typeVariations(MappingSample base) {
+ return Stream.of(
+ base,
+ //
+ new MappingSample(
+ objBuilder()
+ .from(base.obj())
+ .taskType(AsyncTaskType.MANIFEST_FILE_CLEANUP)
+ .build(),
+ entityWithProperty(
+ base.entity(),
+ TASK_TYPE,
+ PolarisObjectMapperUtil.serialize(AsyncTaskType.MANIFEST_FILE_CLEANUP))),
+ new MappingSample(
+ objBuilder()
+ .from(base.obj())
+ .serializedEntity(
+ serializeStringCompressed(PolarisObjectMapperUtil.serialize(taskData)))
+ .build(),
+ entityWithProperty(
+ base.entity(), TASK_DATA, PolarisObjectMapperUtil.serialize(taskData))),
+ new MappingSample(
+ objBuilder().from(base.obj()).lastAttemptExecutorId("executor-id").build(),
+ entityWithProperty(base.entity(), LAST_ATTEMPT_EXECUTOR_ID, "executor-id")),
+ new MappingSample(
+ objBuilder().from(base.obj()).attemptCount(123).build(),
+ entityWithProperty(base.entity(), ATTEMPT_COUNT, "123")),
+ new MappingSample(
+ objBuilder()
+ .from(base.obj())
+ .lastAttemptStartTime(Instant.ofEpochMilli(1234567890))
+ .build(),
+ entityWithProperty(base.entity(), LAST_ATTEMPT_START_TIME, "1234567890")),
+ //
+ new MappingSample(
+ objBuilder()
+ .from(base.obj())
+ .taskType(AsyncTaskType.MANIFEST_FILE_CLEANUP)
+ .serializedEntity(
+ serializeStringCompressed(PolarisObjectMapperUtil.serialize(taskData)))
+ .lastAttemptExecutorId("executor-id")
+ .attemptCount(123)
+ .lastAttemptStartTime(Instant.ofEpochMilli(1234567890))
+ .build(),
+ entityWithProperties(
+ base.entity(),
+ Map.of(
+ TASK_TYPE,
+ PolarisObjectMapperUtil.serialize(AsyncTaskType.MANIFEST_FILE_CLEANUP),
+ TASK_DATA,
+ PolarisObjectMapperUtil.serialize(taskData),
+ LAST_ATTEMPT_EXECUTOR_ID,
+ "executor-id",
+ ATTEMPT_COUNT,
+ "123",
+ LAST_ATTEMPT_START_TIME,
+ "1234567890")))
+ //
+ );
+ }
+ });
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/realm/TestPolicyMapping.java b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/realm/TestPolicyMapping.java
new file mode 100644
index 000000000..f32716a13
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/test/java/org/apache/polaris/persistence/nosql/coretypes/realm/TestPolicyMapping.java
@@ -0,0 +1,85 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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
+ *
+ * http://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 org.apache.polaris.persistence.nosql.coretypes.realm;
+
+import static org.apache.polaris.persistence.nosql.coretypes.realm.PolicyMapping.POLICY_MAPPING_SERIALIZER;
+
+import java.nio.ByteBuffer;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.IntStream;
+import java.util.stream.Stream;
+import org.assertj.core.api.SoftAssertions;
+import org.assertj.core.api.junit.jupiter.InjectSoftAssertions;
+import org.assertj.core.api.junit.jupiter.SoftAssertionsExtension;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.MethodSource;
+
+@ExtendWith(SoftAssertionsExtension.class)
+public class TestPolicyMapping {
+ @InjectSoftAssertions protected SoftAssertions soft;
+
+ @ParameterizedTest
+ @MethodSource
+ public void policyMappingSerialization(PolicyMapping mapping) {
+ var serSize = POLICY_MAPPING_SERIALIZER.serializedSize(mapping);
+ var buffer = ByteBuffer.allocate(serSize + 10);
+ POLICY_MAPPING_SERIALIZER.serialize(mapping, buffer);
+ soft.assertThat(buffer)
+ .extracting(ByteBuffer::position, ByteBuffer::remaining)
+ .containsExactly(serSize, 10);
+ buffer.put(new byte[10]);
+ soft.assertThat(buffer)
+ .extracting(ByteBuffer::position, ByteBuffer::remaining)
+ .containsExactly(serSize + 10, 0);
+
+ buffer.flip();
+
+ soft.assertThat(buffer)
+ .extracting(ByteBuffer::position, ByteBuffer::remaining)
+ .containsExactly(0, serSize + 10);
+
+ var skip = buffer.duplicate();
+ POLICY_MAPPING_SERIALIZER.skip(skip);
+ soft.assertThat(skip)
+ .extracting(ByteBuffer::position, ByteBuffer::remaining)
+ .containsExactly(serSize, 10);
+
+ var deser = buffer.duplicate();
+ var deserialized = POLICY_MAPPING_SERIALIZER.deserialize(deser);
+ soft.assertThat(deser)
+ .extracting(ByteBuffer::position, ByteBuffer::remaining)
+ .containsExactly(serSize, 10);
+ soft.assertThat(deserialized).isEqualTo(mapping != null ? mapping : PolicyMapping.EMPTY);
+ }
+
+ static Stream<PolicyMapping> policyMappingSerialization() {
+ return Stream.of(
+ null,
+ PolicyMapping.EMPTY,
+ PolicyMapping.builder().parameters(Map.of("a", "b")).build(),
+ PolicyMapping.builder()
+ .parameters(
+ IntStream.range(0, 50)
+ .boxed()
+ .collect(Collectors.toMap(v -> "k" + v, v -> "v" + v)))
+ .build());
+ }
+}
diff --git a/persistence/nosql/persistence/metastore-types/src/test/resources/logback-test.xml b/persistence/nosql/persistence/metastore-types/src/test/resources/logback-test.xml
new file mode 100644
index 000000000..1de5ba06e
--- /dev/null
+++ b/persistence/nosql/persistence/metastore-types/src/test/resources/logback-test.xml
@@ -0,0 +1,34 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+<!--
+
+ Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you 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
+
+ http://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.
+
+-->
+<configuration debug="false">
+ <contextListener class="ch.qos.logback.classic.jul.LevelChangePropagator"/>
+ <appender name="console" class="ch.qos.logback.core.ConsoleAppender">
+ <encoder>
+ <pattern>%date{ISO8601} [%thread] %-5level %logger{36} - %msg%n</pattern>
+ </encoder>
+ </appender>
+ <root level="DEBUG">
+ <appender-ref ref="console"/>
+ </root>
+ <logger name="org.jboss" level="INFO"/>
+ <logger name="org.apache.polaris" level="DEBUG"/>
+</configuration>