Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#206] feat(server, client): Add helper methods for NameIdentifier and Namespace creation (#240)

Jerry Shaob4f8f5872ded
diff --git a/api/src/main/java/com/datastrato/graviton/NameIdentifier.java b/api/src/main/java/com/datastrato/graviton/NameIdentifier.java
index f7509deb2d..bd5abfbb29 100644
--- a/api/src/main/java/com/datastrato/graviton/NameIdentifier.java
+++ b/api/src/main/java/com/datastrato/graviton/NameIdentifier.java
@@ -4,7 +4,7 @@
*/
package com.datastrato.graviton;
-import com.google.common.base.Preconditions;
+import com.datastrato.graviton.exceptions.IllegalNameIdentifierException;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import java.util.Arrays;
@@ -17,44 +17,158 @@ public class NameIdentifier {
private final String name;
+ /**
+ * Create the {@link NameIdentifier} with the given levels of names.
+ *
+ * @param names The names of the identifier
+ * @return The created {@link NameIdentifier}
+ */
public static NameIdentifier of(String... names) {
- Preconditions.checkArgument(names != null, "Cannot create a NameIdentifier with null names");
- Preconditions.checkArgument(names.length > 0, "Cannot create a NameIdentifier with no names");
+ check(names != null, "Cannot create a NameIdentifier with null names");
+ check(names.length > 0, "Cannot create a NameIdentifier with no names");
return new NameIdentifier(
Namespace.of(Arrays.copyOf(names, names.length - 1)), names[names.length - 1]);
}
+ /**
+ * Create the {@link NameIdentifier} with the given {@link Namespace} and name.
+ *
+ * @param namespace The namespace of the identifier
+ * @param name The name of the identifier
+ * @return The created {@link NameIdentifier}
+ */
public static NameIdentifier of(Namespace namespace, String name) {
return new NameIdentifier(namespace, name);
}
+ /**
+ * Create the metalake {@link NameIdentifier} with the given name.
+ *
+ * @param metalake The metalake name
+ * @return The created metalake {@link NameIdentifier}
+ */
+ public static NameIdentifier ofMetalake(String metalake) {
+ return NameIdentifier.of(metalake);
+ }
+
+ /**
+ * Create the catalog {@link NameIdentifier} with the given metalake and catalog name.
+ *
+ * @param metalake The metalake name
+ * @param catalog The catalog name
+ * @return The created catalog {@link NameIdentifier}
+ */
+ public static NameIdentifier ofCatalog(String metalake, String catalog) {
+ return NameIdentifier.of(metalake, catalog);
+ }
+
+ /**
+ * Create the schema {@link NameIdentifier} with the given metalake, catalog and schema name.
+ *
+ * @param metalake The metalake name
+ * @param catalog The catalog name
+ * @param schema The schema name
+ * @return The created schema {@link NameIdentifier}
+ */
+ public static NameIdentifier ofSchema(String metalake, String catalog, String schema) {
+ return NameIdentifier.of(metalake, catalog, schema);
+ }
+
+ /**
+ * Create the table {@link NameIdentifier} with the given metalake, catalog, schema and table
+ * name.
+ *
+ * @param metalake The metalake name
+ * @param catalog The catalog name
+ * @param schema The schema name
+ * @param table The table name
+ * @return The created table {@link NameIdentifier}
+ */
+ public static NameIdentifier ofTable(
+ String metalake, String catalog, String schema, String table) {
+ return NameIdentifier.of(metalake, catalog, schema, table);
+ }
+
+ /**
+ * Check the given {@link NameIdentifier} is a metalake identifier. Throw an {@link
+ * IllegalNameIdentifierException} if it's not.
+ *
+ * @param ident The metalake {@link NameIdentifier} to check.
+ */
+ public static void checkMetalake(NameIdentifier ident) {
+ // We don't have to check the name field of NameIdentifier, it's already checked when
+ // creating NameIdentifier object.
+ check(ident != null, "Metalake identifier must not be null");
+ Namespace.checkMetalake(ident.namespace);
+ }
+
+ /**
+ * Check the given {@link NameIdentifier} is a catalog identifier. Throw an {@link
+ * IllegalNameIdentifierException} if it's not.
+ *
+ * @param ident The catalog {@link NameIdentifier} to check.
+ */
+ public static void checkCatalog(NameIdentifier ident) {
+ check(ident != null, "Catalog identifier must not be null");
+ Namespace.checkCatalog(ident.namespace);
+ }
+
+ /**
+ * Check the given {@link NameIdentifier} is a schema identifier. Throw an {@link
+ * IllegalNameIdentifierException} if it's not.
+ *
+ * @param ident The schema {@link NameIdentifier} to check.
+ */
+ public static void checkSchema(NameIdentifier ident) {
+ check(ident != null, "Schema identifier must not be null");
+ Namespace.checkSchema(ident.namespace);
+ }
+
+ /**
+ * Check the given {@link NameIdentifier} is a table identifier. Throw an {@link
+ * IllegalNameIdentifierException} if it's not.
+ *
+ * @param ident The table {@link NameIdentifier} to check.
+ */
+ public static void checkTable(NameIdentifier ident) {
+ check(ident != null, "Table identifier must not be null");
+ Namespace.checkTable(ident.namespace);
+ }
+
+ /**
+ * Create a {@link NameIdentifier} from the given identifier string.
+ *
+ * @param identifier The identifier string
+ * @return The created {@link NameIdentifier}
+ */
public static NameIdentifier parse(String identifier) {
- Preconditions.checkArgument(
- identifier != null && !identifier.isEmpty(), "Cannot parse a null or empty identifier");
+ check(identifier != null && !identifier.isEmpty(), "Cannot parse a null or empty identifier");
Iterable<String> parts = DOT.split(identifier);
return NameIdentifier.of(Iterables.toArray(parts, String.class));
}
private NameIdentifier(Namespace namespace, String name) {
- Preconditions.checkArgument(
- namespace != null, "Cannot create a NameIdentifier with null namespace");
- Preconditions.checkArgument(
+ check(namespace != null, "Cannot create a NameIdentifier with null namespace");
+ check(
name != null && !name.isEmpty(), "Cannot create a NameIdentifier with null or empty name");
this.namespace = namespace;
this.name = name;
}
+ /** Check if the {@link NameIdentifier} has a namespace. */
public boolean hasNamespace() {
return !namespace.isEmpty();
}
+ /** Get the namespace of the {@link NameIdentifier}. */
public Namespace namespace() {
return namespace;
}
+ /** Get the name of the {@link NameIdentifier}. */
public String name() {
return name;
}
@@ -82,4 +196,10 @@ public class NameIdentifier {
return name;
}
}
+
+ private static void check(boolean condition, String message, Object... args) {
+ if (!condition) {
+ throw new IllegalNameIdentifierException(String.format(message, args));
+ }
+ }
}
diff --git a/api/src/main/java/com/datastrato/graviton/Namespace.java b/api/src/main/java/com/datastrato/graviton/Namespace.java
index a100bf6a31..2881cd87fb 100644
--- a/api/src/main/java/com/datastrato/graviton/Namespace.java
+++ b/api/src/main/java/com/datastrato/graviton/Namespace.java
@@ -4,8 +4,8 @@
*/
package com.datastrato.graviton;
+import com.datastrato.graviton.exceptions.IllegalNamespaceException;
import com.google.common.base.Joiner;
-import com.google.common.base.Preconditions;
import java.util.Arrays;
public class Namespace {
@@ -15,41 +15,150 @@ public class Namespace {
private final String[] levels;
+ /** Get an empty namespace. */
public static Namespace empty() {
return EMPTY;
}
+ /**
+ * Create a namespace with the given levels.
+ *
+ * @param levels The levels of the namespace
+ * @return A namespace with the given levels
+ */
public static Namespace of(String... levels) {
- Preconditions.checkArgument(levels != null, "Cannot create a namespace with null levels");
+ check(levels != null, "Cannot create a namespace with null levels");
if (levels.length == 0) {
return empty();
}
for (String level : levels) {
- Preconditions.checkArgument(
+ check(
level != null && !level.isEmpty(), "Cannot create a namespace with null or empty level");
}
return new Namespace(levels);
}
+ /** Create a namespace for metalake. */
+ public static Namespace ofMetalake() {
+ return empty();
+ }
+
+ /**
+ * Create a namespace for catalog.
+ *
+ * @param metalake The metalake name
+ * @return A namespace for catalog
+ */
+ public static Namespace ofCatalog(String metalake) {
+ return of(metalake);
+ }
+
+ /**
+ * Create a namespace for schema.
+ *
+ * @param metalake The metalake name
+ * @param catalog The catalog name
+ * @return A namespace for schema
+ */
+ public static Namespace ofSchema(String metalake, String catalog) {
+ return of(metalake, catalog);
+ }
+
+ /**
+ * Create a namespace for table.
+ *
+ * @param metalake The metalake name
+ * @param catalog The catalog name
+ * @param schema The schema name
+ * @return A namespace for table
+ */
+ public static Namespace ofTable(String metalake, String catalog, String schema) {
+ return of(metalake, catalog, schema);
+ }
+
+ /**
+ * Check if the given metalake namespace is legal, throw an {@link IllegalNamespaceException} if
+ * it's illegal.
+ *
+ * @param namespace The metalake namespace
+ */
+ public static void checkMetalake(Namespace namespace) {
+ check(
+ namespace != null && namespace.isEmpty(),
+ String.format(
+ "Metalake namespace must be non-null and empty, the input namespace is %s", namespace));
+ }
+
+ /**
+ * Check if the given catalog namespace is legal, throw an {@link IllegalNamespaceException} if
+ * it's illegal.
+ *
+ * @param namespace The catalog namespace
+ */
+ public static void checkCatalog(Namespace namespace) {
+ check(
+ namespace != null && namespace.length() == 1,
+ String.format(
+ "Catalog namespace must be non-null and have 1 level, the input namespace is %s",
+ namespace));
+ }
+
+ /**
+ * Check if the given schema namespace is legal, throw an {@link IllegalNamespaceException} if
+ * it's illegal.
+ *
+ * @param namespace The schema namespace
+ */
+ public static void checkSchema(Namespace namespace) {
+ check(
+ namespace != null && namespace.length() == 2,
+ String.format(
+ "Schema namespace must be non-null and have 2 levels, the input namespace is %s",
+ namespace));
+ }
+
+ /**
+ * Check if the given table namespace is legal, throw an {@link IllegalNamespaceException} if it's
+ * illegal.
+ *
+ * @param namespace The table namespace
+ */
+ public static void checkTable(Namespace namespace) {
+ check(
+ namespace != null && namespace.length() == 3,
+ String.format(
+ "Table namespace must be non-null and have 3 levels, the input namespace is %s",
+ namespace));
+ }
+
private Namespace(String[] levels) {
this.levels = levels;
}
+ /** Get the levels of the namespace. */
public String[] levels() {
return levels;
}
+ /**
+ * Get the level at the given position.
+ *
+ * @param pos The position of the level
+ * @return The level at the given position
+ */
public String level(int pos) {
- Preconditions.checkArgument(pos >= 0 && pos < levels.length, "Invalid level position");
+ check(pos >= 0 && pos < levels.length, "Invalid level position");
return levels[pos];
}
+ /** Get the length of the namespace. */
public int length() {
return levels.length;
}
+ /** Check if the namespace is empty. */
public boolean isEmpty() {
return levels.length == 0;
}
@@ -73,4 +182,10 @@ public class Namespace {
public String toString() {
return DOT.join(levels);
}
+
+ private static void check(boolean expression, String message, Object... args) {
+ if (!expression) {
+ throw new IllegalNamespaceException(String.format(message, args));
+ }
+ }
}
diff --git a/api/src/main/java/com/datastrato/graviton/exceptions/IllegalNameIdentifierException.java b/api/src/main/java/com/datastrato/graviton/exceptions/IllegalNameIdentifierException.java
new file mode 100644
index 0000000000..ffa1d75f54
--- /dev/null
+++ b/api/src/main/java/com/datastrato/graviton/exceptions/IllegalNameIdentifierException.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2023 Datastrato.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.graviton.exceptions;
+
+public class IllegalNameIdentifierException extends IllegalArgumentException {
+
+ public IllegalNameIdentifierException(String message) {
+ super(message);
+ }
+
+ public IllegalNameIdentifierException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public IllegalNameIdentifierException(Throwable cause) {
+ super(cause);
+ }
+
+ public IllegalNameIdentifierException() {
+ super();
+ }
+}
diff --git a/api/src/main/java/com/datastrato/graviton/exceptions/IllegalNamespaceException.java b/api/src/main/java/com/datastrato/graviton/exceptions/IllegalNamespaceException.java
new file mode 100644
index 0000000000..330dad2bba
--- /dev/null
+++ b/api/src/main/java/com/datastrato/graviton/exceptions/IllegalNamespaceException.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright 2023 Datastrato.
+ * This software is licensed under the Apache License version 2.
+ */
+package com.datastrato.graviton.exceptions;
+
+public class IllegalNamespaceException extends IllegalArgumentException {
+
+ public IllegalNamespaceException(String message) {
+ super(message);
+ }
+
+ public IllegalNamespaceException(String message, Throwable cause) {
+ super(message, cause);
+ }
+
+ public IllegalNamespaceException(Throwable cause) {
+ super(cause);
+ }
+
+ public IllegalNamespaceException() {
+ super();
+ }
+}
diff --git a/api/src/test/java/com/datastrato/graviton/TestNameIdentifier.java b/api/src/test/java/com/datastrato/graviton/TestNameIdentifier.java
index 41523b3748..41cda12e8d 100644
--- a/api/src/test/java/com/datastrato/graviton/TestNameIdentifier.java
+++ b/api/src/test/java/com/datastrato/graviton/TestNameIdentifier.java
@@ -10,6 +10,8 @@ import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
+import com.datastrato.graviton.exceptions.IllegalNameIdentifierException;
+import com.datastrato.graviton.exceptions.IllegalNamespaceException;
import org.junit.jupiter.api.Test;
public class TestNameIdentifier {
@@ -89,4 +91,39 @@ public class TestNameIdentifier {
assertEquals(id1.toString(), "a");
assertEquals(id2.toString(), "a.b.c");
}
+
+ @Test
+ public void testCheckNameIdentifier() {
+ // Test metalake
+ assertThrows(IllegalNameIdentifierException.class, () -> NameIdentifier.checkMetalake(null));
+ Throwable excep =
+ assertThrows(
+ IllegalNamespaceException.class,
+ () -> NameIdentifier.checkMetalake(NameIdentifier.of("a", "b", "c")));
+ assertTrue(excep.getMessage().contains("Metalake namespace must be non-null and empty"));
+
+ // test catalog
+ assertThrows(IllegalNameIdentifierException.class, () -> NameIdentifier.checkCatalog(null));
+ Throwable excep1 =
+ assertThrows(
+ IllegalNamespaceException.class,
+ () -> NameIdentifier.checkCatalog(NameIdentifier.of("a", "b", "c")));
+ assertTrue(excep1.getMessage().contains("Catalog namespace must be non-null and have 1 level"));
+
+ // test schema
+ assertThrows(IllegalNameIdentifierException.class, () -> NameIdentifier.checkSchema(null));
+ Throwable excep2 =
+ assertThrows(
+ IllegalNamespaceException.class,
+ () -> NameIdentifier.checkSchema(NameIdentifier.of("a", "b", "c", "d")));
+ assertTrue(excep2.getMessage().contains("Schema namespace must be non-null and have 2 levels"));
+
+ // test table
+ assertThrows(IllegalNameIdentifierException.class, () -> NameIdentifier.checkTable(null));
+ Throwable excep3 =
+ assertThrows(
+ IllegalNamespaceException.class,
+ () -> NameIdentifier.checkTable(NameIdentifier.of("a", "b", "c")));
+ assertTrue(excep3.getMessage().contains("Table namespace must be non-null and have 3 levels"));
+ }
}
diff --git a/api/src/test/java/com/datastrato/graviton/TestNamespace.java b/api/src/test/java/com/datastrato/graviton/TestNamespace.java
index 4d2ac4ea35..05704a293d 100644
--- a/api/src/test/java/com/datastrato/graviton/TestNamespace.java
+++ b/api/src/test/java/com/datastrato/graviton/TestNamespace.java
@@ -4,6 +4,7 @@
*/
package com.datastrato.graviton;
+import com.datastrato.graviton.exceptions.IllegalNamespaceException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
@@ -32,4 +33,40 @@ public class TestNamespace {
Assertions.assertThrows(IllegalArgumentException.class, () -> Namespace.of("a", null, "c"));
Assertions.assertThrows(IllegalArgumentException.class, () -> Namespace.of("a", "", "c"));
}
+
+ @Test
+ public void testCheckNamespace() {
+ // Test metalake
+ Assertions.assertThrows(IllegalNamespaceException.class, () -> Namespace.checkMetalake(null));
+ Throwable excep =
+ Assertions.assertThrows(
+ IllegalNamespaceException.class, () -> Namespace.checkMetalake(Namespace.of("a", "b")));
+ Assertions.assertTrue(
+ excep.getMessage().contains("Metalake namespace must be non-null and empty"));
+
+ // Test catalog
+ Assertions.assertThrows(IllegalNamespaceException.class, () -> Namespace.checkCatalog(null));
+ Throwable excep1 =
+ Assertions.assertThrows(
+ IllegalNamespaceException.class, () -> Namespace.checkCatalog(Namespace.of("a", "b")));
+ Assertions.assertTrue(
+ excep1.getMessage().contains("Catalog namespace must be non-null and have 1 level"));
+
+ // Test schema
+ Assertions.assertThrows(IllegalNamespaceException.class, () -> Namespace.checkSchema(null));
+ Throwable excep2 =
+ Assertions.assertThrows(
+ IllegalNamespaceException.class, () -> Namespace.checkSchema(Namespace.of("a")));
+ Assertions.assertTrue(
+ excep2.getMessage().contains("Schema namespace must be non-null and have 2 levels"));
+
+ // Test table
+ Assertions.assertThrows(IllegalNamespaceException.class, () -> Namespace.checkTable(null));
+ Throwable excep3 =
+ Assertions.assertThrows(
+ IllegalNamespaceException.class,
+ () -> Namespace.checkTable(Namespace.of("a", "b", "c", "d")));
+ Assertions.assertTrue(
+ excep3.getMessage().contains("Table namespace must be non-null and have 3 levels"));
+ }
}
diff --git a/client-java/src/main/java/com/datastrato/graviton/client/GravitonClient.java b/client-java/src/main/java/com/datastrato/graviton/client/GravitonClient.java
index f9665bffe2..638c097ca6 100644
--- a/client-java/src/main/java/com/datastrato/graviton/client/GravitonClient.java
+++ b/client-java/src/main/java/com/datastrato/graviton/client/GravitonClient.java
@@ -82,10 +82,7 @@ public class GravitonClient implements SupportsMetalakes, Closeable {
*/
@Override
public GravitonMetaLake loadMetalake(NameIdentifier ident) throws NoSuchMetalakeException {
- Preconditions.checkArgument(ident != null, "Metalake name identifier cannot be null");
- Preconditions.checkArgument(
- !ident.hasNamespace(),
- "Metalake name identifier is a top-level identifier, namespace should be empty");
+ NameIdentifier.checkMetalake(ident);
MetalakeResponse resp =
restClient.get(
@@ -112,10 +109,7 @@ public class GravitonClient implements SupportsMetalakes, Closeable {
public GravitonMetaLake createMetalake(
NameIdentifier ident, String comment, Map<String, String> properties)
throws MetalakeAlreadyExistsException {
- Preconditions.checkArgument(ident != null, "Metalake name identifier cannot be null");
- Preconditions.checkArgument(
- !ident.hasNamespace(),
- "Metalake name identifier is a top-level identifier, namespace should be empty");
+ NameIdentifier.checkMetalake(ident);
MetalakeCreateRequest req = new MetalakeCreateRequest(ident.name(), comment, properties);
req.validate();
@@ -144,10 +138,7 @@ public class GravitonClient implements SupportsMetalakes, Closeable {
@Override
public GravitonMetaLake alterMetalake(NameIdentifier ident, MetalakeChange... changes)
throws NoSuchMetalakeException, IllegalArgumentException {
- Preconditions.checkArgument(ident != null, "Metalake name identifier cannot be null");
- Preconditions.checkArgument(
- !ident.hasNamespace(),
- "Metalake name identifier is a top-level identifier, namespace should be empty");
+ NameIdentifier.checkMetalake(ident);
List<MetalakeUpdateRequest> reqs =
Arrays.stream(changes)
@@ -176,10 +167,7 @@ public class GravitonClient implements SupportsMetalakes, Closeable {
*/
@Override
public boolean dropMetalake(NameIdentifier ident) {
- Preconditions.checkArgument(ident != null, "Metalake name identifier cannot be null");
- Preconditions.checkArgument(
- !ident.hasNamespace(),
- "Metalake name identifier is a top-level identifier, namespace should be empty");
+ NameIdentifier.checkMetalake(ident);
try {
DropResponse resp =
diff --git a/client-java/src/main/java/com/datastrato/graviton/client/GravitonMetaLake.java b/client-java/src/main/java/com/datastrato/graviton/client/GravitonMetaLake.java
index b4bc57e570..47967e8f40 100644
--- a/client-java/src/main/java/com/datastrato/graviton/client/GravitonMetaLake.java
+++ b/client-java/src/main/java/com/datastrato/graviton/client/GravitonMetaLake.java
@@ -60,7 +60,7 @@ public class GravitonMetaLake extends MetalakeDTO implements SupportsCatalogs {
*/
@Override
public NameIdentifier[] listCatalogs(Namespace namespace) throws NoSuchMetalakeException {
- validateCatalogNamespace(namespace);
+ Namespace.checkCatalog(namespace);
EntityListResponse resp =
restClient.get(
@@ -82,7 +82,7 @@ public class GravitonMetaLake extends MetalakeDTO implements SupportsCatalogs {
*/
@Override
public Catalog loadCatalog(NameIdentifier ident) throws NoSuchCatalogException {
- validateCatalogIdentifier(ident);
+ NameIdentifier.checkCatalog(ident);
CatalogResponse resp =
restClient.get(
@@ -110,7 +110,7 @@ public class GravitonMetaLake extends MetalakeDTO implements SupportsCatalogs {
public Catalog createCatalog(
NameIdentifier ident, Catalog.Type type, String comment, Map<String, String> properties)
throws NoSuchMetalakeException, CatalogAlreadyExistsException {
- validateCatalogIdentifier(ident);
+ NameIdentifier.checkCatalog(ident);
CatalogCreateRequest req = new CatalogCreateRequest(ident.name(), type, comment, properties);
req.validate();
@@ -139,7 +139,7 @@ public class GravitonMetaLake extends MetalakeDTO implements SupportsCatalogs {
@Override
public Catalog alterCatalog(NameIdentifier ident, CatalogChange... changes)
throws NoSuchCatalogException, IllegalArgumentException {
- validateCatalogIdentifier(ident);
+ NameIdentifier.checkCatalog(ident);
List<CatalogUpdateRequest> reqs =
Arrays.stream(changes)
@@ -168,7 +168,7 @@ public class GravitonMetaLake extends MetalakeDTO implements SupportsCatalogs {
*/
@Override
public boolean dropCatalog(NameIdentifier ident) {
- validateCatalogIdentifier(ident);
+ NameIdentifier.checkCatalog(ident);
try {
DropResponse resp =
@@ -187,17 +187,6 @@ public class GravitonMetaLake extends MetalakeDTO implements SupportsCatalogs {
}
}
- private static void validateCatalogNamespace(Namespace ns) {
- Preconditions.checkArgument(
- ns != null && ns.length() == 1, "namespace must not be null and have exactly one level");
- }
-
- private static void validateCatalogIdentifier(NameIdentifier ident) {
- validateCatalogNamespace(ident.namespace());
- Preconditions.checkArgument(
- StringUtils.isNotBlank(ident.name()), "name must not be null or empty");
- }
-
static class Builder extends MetalakeDTO.Builder<Builder> {
private RESTClient restClient;
diff --git a/client-java/src/main/java/com/datastrato/graviton/client/RelationalCatalog.java b/client-java/src/main/java/com/datastrato/graviton/client/RelationalCatalog.java
index 46cc9da36b..9453acb120 100644
--- a/client-java/src/main/java/com/datastrato/graviton/client/RelationalCatalog.java
+++ b/client-java/src/main/java/com/datastrato/graviton/client/RelationalCatalog.java
@@ -39,7 +39,6 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
-import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -84,7 +83,7 @@ public class RelationalCatalog extends CatalogDTO implements TableCatalog, Suppo
*/
@Override
public NameIdentifier[] listTables(Namespace namespace) throws NoSuchSchemaException {
- validateTableNamespace(namespace);
+ Namespace.checkTable(namespace);
EntityListResponse resp =
restClient.get(
@@ -106,7 +105,7 @@ public class RelationalCatalog extends CatalogDTO implements TableCatalog, Suppo
*/
@Override
public Table loadTable(NameIdentifier ident) throws NoSuchTableException {
- validateTableIdentifier(ident);
+ NameIdentifier.checkTable(ident);
TableResponse resp =
restClient.get(
@@ -134,7 +133,7 @@ public class RelationalCatalog extends CatalogDTO implements TableCatalog, Suppo
public Table createTable(
NameIdentifier ident, Column[] columns, String comment, Map<String, String> properties)
throws NoSuchSchemaException, TableAlreadyExistsException {
- validateTableIdentifier(ident);
+ NameIdentifier.checkTable(ident);
TableCreateRequest req =
new TableCreateRequest(ident.name(), comment, (ColumnDTO[]) columns, properties);
@@ -164,7 +163,7 @@ public class RelationalCatalog extends CatalogDTO implements TableCatalog, Suppo
@Override
public Table alterTable(NameIdentifier ident, TableChange... changes)
throws NoSuchTableException, IllegalArgumentException {
- validateTableIdentifier(ident);
+ NameIdentifier.checkTable(ident);
List<TableUpdateRequest> reqs =
Arrays.stream(changes)
@@ -193,7 +192,7 @@ public class RelationalCatalog extends CatalogDTO implements TableCatalog, Suppo
*/
@Override
public boolean dropTable(NameIdentifier ident) {
- validateTableIdentifier(ident);
+ NameIdentifier.checkTable(ident);
try {
DropResponse resp =
@@ -220,7 +219,7 @@ public class RelationalCatalog extends CatalogDTO implements TableCatalog, Suppo
*/
@Override
public NameIdentifier[] listSchemas(Namespace namespace) throws NoSuchCatalogException {
- validateSchemaNamespace(namespace);
+ Namespace.checkSchema(namespace);
EntityListResponse resp =
restClient.get(
@@ -246,7 +245,7 @@ public class RelationalCatalog extends CatalogDTO implements TableCatalog, Suppo
@Override
public Schema createSchema(NameIdentifier ident, String comment, Map<String, String> metadata)
throws NoSuchCatalogException, SchemaAlreadyExistsException {
- validateSchemaIdentifier(ident);
+ NameIdentifier.checkSchema(ident);
SchemaCreateRequest req = new SchemaCreateRequest(ident.name(), comment, metadata);
req.validate();
@@ -272,7 +271,7 @@ public class RelationalCatalog extends CatalogDTO implements TableCatalog, Suppo
*/
@Override
public Schema loadSchema(NameIdentifier ident) throws NoSuchSchemaException {
- validateSchemaIdentifier(ident);
+ NameIdentifier.checkSchema(ident);
SchemaResponse resp =
restClient.get(
@@ -296,7 +295,7 @@ public class RelationalCatalog extends CatalogDTO implements TableCatalog, Suppo
@Override
public Schema alterSchema(NameIdentifier ident, SchemaChange... changes)
throws NoSuchSchemaException {
- validateSchemaIdentifier(ident);
+ NameIdentifier.checkSchema(ident);
List<SchemaUpdateRequest> reqs =
Arrays.stream(changes)
@@ -327,7 +326,7 @@ public class RelationalCatalog extends CatalogDTO implements TableCatalog, Suppo
*/
@Override
public boolean dropSchema(NameIdentifier ident, boolean cascade) throws NonEmptySchemaException {
- validateSchemaIdentifier(ident);
+ NameIdentifier.checkSchema(ident);
try {
DropResponse resp =
@@ -348,28 +347,6 @@ public class RelationalCatalog extends CatalogDTO implements TableCatalog, Suppo
}
}
- private static void validateSchemaNamespace(Namespace ns) {
- Preconditions.checkArgument(
- ns != null && ns.length() == 2,
- "Schema namespace must not be null and have exactly two level");
- }
-
- private static void validateSchemaIdentifier(NameIdentifier ident) {
- validateSchemaNamespace(ident.namespace());
- Preconditions.checkArgument(
- StringUtils.isNotBlank(ident.name()), "Schema name must not be null or empty");
- }
-
- private static void validateTableNamespace(Namespace ns) {
- validateSchemaIdentifier(NameIdentifier.of(ns.levels()));
- }
-
- private static void validateTableIdentifier(NameIdentifier ident) {
- validateTableNamespace(ident.namespace());
- Preconditions.checkArgument(
- StringUtils.isNotBlank(ident.name()), "Table name must not be null or empty");
- }
-
@VisibleForTesting
static String formatTableRequestPath(Namespace ns) {
Namespace schemaNs = Namespace.of(ns.level(0), ns.level(1));
diff --git a/client-java/src/test/java/com/datastrato/graviton/client/TestGravitonClient.java b/client-java/src/test/java/com/datastrato/graviton/client/TestGravitonClient.java
index ac6bf62de3..ea8edf57a8 100644
--- a/client-java/src/test/java/com/datastrato/graviton/client/TestGravitonClient.java
+++ b/client-java/src/test/java/com/datastrato/graviton/client/TestGravitonClient.java
@@ -113,7 +113,8 @@ public class TestGravitonClient extends TestBase {
Assertions.assertThrows(
IllegalArgumentException.class,
() -> client.loadMetalake(NameIdentifier.parse("mock.mock")));
- Assertions.assertTrue(excep1.getMessage().contains("namespace should be empty"));
+ Assertions.assertTrue(
+ excep1.getMessage().contains("Metalake namespace must be non-null and empty"));
// Test return unparsed system error
buildMockResource(Method.GET, "/api/metalakes/mock", null, null, HttpStatus.SC_CONFLICT);
@@ -209,7 +210,8 @@ public class TestGravitonClient extends TestBase {
Assertions.assertThrows(
IllegalArgumentException.class,
() -> client.alterMetalake(NameIdentifier.parse("mock.mock"), changes));
- Assertions.assertTrue(excep1.getMessage().contains("namespace should be empty"));
+ Assertions.assertTrue(
+ excep1.getMessage().contains("Metalake namespace must be non-null and empty"));
}
@Test
@@ -233,6 +235,7 @@ public class TestGravitonClient extends TestBase {
Assertions.assertThrows(
IllegalArgumentException.class,
() -> client.dropMetalake(NameIdentifier.parse("mock.mock")));
- Assertions.assertTrue(excep1.getMessage().contains("namespace should be empty"));
+ Assertions.assertTrue(
+ excep1.getMessage().contains("Metalake namespace must be non-null and empty"));
}
}
diff --git a/server/src/main/java/com/datastrato/graviton/server/web/rest/CatalogOperations.java b/server/src/main/java/com/datastrato/graviton/server/web/rest/CatalogOperations.java
index 630c081e69..0d77202b69 100644
--- a/server/src/main/java/com/datastrato/graviton/server/web/rest/CatalogOperations.java
+++ b/server/src/main/java/com/datastrato/graviton/server/web/rest/CatalogOperations.java
@@ -16,6 +16,8 @@ import com.datastrato.graviton.dto.responses.CatalogResponse;
import com.datastrato.graviton.dto.responses.DropResponse;
import com.datastrato.graviton.dto.responses.EntityListResponse;
import com.datastrato.graviton.exceptions.CatalogAlreadyExistsException;
+import com.datastrato.graviton.exceptions.IllegalNameIdentifierException;
+import com.datastrato.graviton.exceptions.IllegalNamespaceException;
import com.datastrato.graviton.exceptions.NoSuchCatalogException;
import com.datastrato.graviton.exceptions.NoSuchMetalakeException;
import com.datastrato.graviton.server.web.Utils;
@@ -54,16 +56,15 @@ public class CatalogOperations {
@GET
@Produces("application/vnd.graviton.v1+json")
public Response listCatalogs(@PathParam("metalake") String metalake) {
- if (metalake == null || metalake.isEmpty()) {
- LOG.error("Metalake name is null or empty");
- return Utils.illegalArguments("Metalake name is illegal");
- }
-
try {
- Namespace metalakeNS = Namespace.of(metalake);
- NameIdentifier[] idents = manager.listCatalogs(metalakeNS);
+ Namespace catalogNS = Namespace.ofCatalog(metalake);
+ NameIdentifier[] idents = manager.listCatalogs(catalogNS);
return Utils.ok(new EntityListResponse(idents));
+ } catch (IllegalNamespaceException e) {
+ LOG.warn("Failed to list catalogs with invalid namespace", e);
+ return Utils.illegalArguments("Failed to list catalogs with invalid namespace", e);
+
} catch (NoSuchMetalakeException ex) {
LOG.error("Metalake {} does not exist, fail to list catalogs", metalake);
return Utils.notFound("Metalake " + metalake + " does not exist", ex);
@@ -78,11 +79,6 @@ public class CatalogOperations {
@Produces("application/vnd.graviton.v1+json")
public Response createCatalog(
@PathParam("metalake") String metalake, CatalogCreateRequest request) {
- if (metalake == null || metalake.isEmpty()) {
- LOG.error("Metalake name is null or empty");
- return Utils.illegalArguments("Metalake name is illegal");
- }
-
try {
request.validate();
} catch (IllegalArgumentException e) {
@@ -92,12 +88,16 @@ public class CatalogOperations {
}
try {
- NameIdentifier ident = NameIdentifier.of(metalake, request.getName());
+ NameIdentifier ident = NameIdentifier.ofCatalog(metalake, request.getName());
Catalog catalog =
manager.createCatalog(
ident, request.getType(), request.getComment(), request.getProperties());
return Utils.ok(new CatalogResponse(DTOConverters.toDTO(catalog)));
+ } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
+ LOG.warn("Failed to create catalog with invalid name identifier", e);
+ return Utils.illegalArguments("Failed to create catalog with invalid name identifier", e);
+
} catch (NoSuchMetalakeException ex) {
LOG.error("Metalake {} does not exist, fail to create catalog", metalake);
return Utils.notFound("Metalake " + metalake + " does not exist", ex);
@@ -119,21 +119,15 @@ public class CatalogOperations {
@Produces("application/vnd.graviton.v1+json")
public Response loadCatalog(
@PathParam("metalake") String metalakeName, @PathParam("catalog") String catalogName) {
- if (metalakeName == null || metalakeName.isEmpty()) {
- LOG.error("Metalake name is null or empty");
- return Utils.illegalArguments("Metalake name is illegal");
- }
-
- if (catalogName == null || catalogName.isEmpty()) {
- LOG.error("Catalog name is null or empty");
- return Utils.illegalArguments("Catalog name is illegal");
- }
-
try {
- NameIdentifier ident = NameIdentifier.of(metalakeName, catalogName);
+ NameIdentifier ident = NameIdentifier.ofCatalog(metalakeName, catalogName);
Catalog catalog = manager.loadCatalog(ident);
return Utils.ok(new CatalogResponse(DTOConverters.toDTO(catalog)));
+ } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
+ LOG.warn("Failed to load catalog with invalid name identifier", e);
+ return Utils.illegalArguments("Failed to load catalog with invalid name identifier", e);
+
} catch (NoSuchMetalakeException ex) {
LOG.error("Metalake {} does not exist, fail to load catalog {}", metalakeName, catalogName);
return Utils.notFound("Metalake " + metalakeName + " does not exist", ex);
@@ -158,16 +152,6 @@ public class CatalogOperations {
@PathParam("metalake") String metalakeName,
@PathParam("catalog") String catalogName,
CatalogUpdatesRequest request) {
- if (metalakeName == null || metalakeName.isEmpty()) {
- LOG.error("Metalake name is null or empty");
- return Utils.illegalArguments("Metalake name is illegal");
- }
-
- if (catalogName == null || catalogName.isEmpty()) {
- LOG.error("Catalog name is null or empty");
- return Utils.illegalArguments("Catalog name is illegal");
- }
-
try {
request.validate();
} catch (IllegalArgumentException e) {
@@ -177,7 +161,7 @@ public class CatalogOperations {
}
try {
- NameIdentifier ident = NameIdentifier.of(metalakeName, catalogName);
+ NameIdentifier ident = NameIdentifier.ofCatalog(metalakeName, catalogName);
CatalogChange[] changes =
request.getUpdates().stream()
.map(CatalogUpdateRequest::catalogChange)
@@ -186,6 +170,10 @@ public class CatalogOperations {
Catalog catalog = manager.alterCatalog(ident, changes);
return Utils.ok(new CatalogResponse(DTOConverters.toDTO(catalog)));
+ } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
+ LOG.warn("Failed to alter catalog with invalid name identifier", e);
+ return Utils.illegalArguments("Failed to alter catalog with invalid name identifier", e);
+
} catch (NoSuchCatalogException ex) {
LOG.error("Catalog {} does not exist under metalake {}", catalogName, metalakeName);
return Utils.notFound(
@@ -217,18 +205,8 @@ public class CatalogOperations {
@Produces("application/vnd.graviton.v1+json")
public Response dropCatalog(
@PathParam("metalake") String metalakeName, @PathParam("catalog") String catalogName) {
- if (metalakeName == null || metalakeName.isEmpty()) {
- LOG.error("Metalake name is null or empty");
- return Utils.illegalArguments("Metalake name is illegal");
- }
-
- if (catalogName == null || catalogName.isEmpty()) {
- LOG.error("Catalog name is null or empty");
- return Utils.illegalArguments("Catalog name is illegal");
- }
-
try {
- NameIdentifier ident = NameIdentifier.of(metalakeName, catalogName);
+ NameIdentifier ident = NameIdentifier.ofCatalog(metalakeName, catalogName);
boolean dropped = manager.dropCatalog(ident);
if (!dropped) {
LOG.warn("Failed to drop catalog {} under metalake {}", catalogName, metalakeName);
@@ -236,6 +214,10 @@ public class CatalogOperations {
return Utils.ok(new DropResponse(dropped));
+ } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
+ LOG.warn("Failed to drop catalog with invalid name identifier", e);
+ return Utils.illegalArguments("Failed to drop catalog with invalid name identifier", e);
+
} catch (Exception e) {
LOG.error("Failed to drop catalog {} under metalake {}", catalogName, metalakeName, e);
return Utils.internalError(
diff --git a/server/src/main/java/com/datastrato/graviton/server/web/rest/MetalakeOperations.java b/server/src/main/java/com/datastrato/graviton/server/web/rest/MetalakeOperations.java
index cb2e2779fb..15d45d12e2 100644
--- a/server/src/main/java/com/datastrato/graviton/server/web/rest/MetalakeOperations.java
+++ b/server/src/main/java/com/datastrato/graviton/server/web/rest/MetalakeOperations.java
@@ -13,6 +13,8 @@ import com.datastrato.graviton.dto.requests.MetalakeUpdatesRequest;
import com.datastrato.graviton.dto.responses.DropResponse;
import com.datastrato.graviton.dto.responses.MetalakeListResponse;
import com.datastrato.graviton.dto.responses.MetalakeResponse;
+import com.datastrato.graviton.exceptions.IllegalNameIdentifierException;
+import com.datastrato.graviton.exceptions.IllegalNamespaceException;
import com.datastrato.graviton.exceptions.MetalakeAlreadyExistsException;
import com.datastrato.graviton.exceptions.NoSuchMetalakeException;
import com.datastrato.graviton.meta.BaseMetalake;
@@ -79,11 +81,15 @@ public class MetalakeOperations {
}
try {
- NameIdentifier ident = NameIdentifier.parse(request.getName());
+ NameIdentifier ident = NameIdentifier.ofMetalake(request.getName());
BaseMetalake metalake =
manager.createMetalake(ident, request.getComment(), request.getProperties());
return Utils.ok(new MetalakeResponse(DTOConverters.toDTO(metalake)));
+ } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
+ LOG.warn("Failed to create metalake with invalid arguments", e);
+ return Utils.illegalArguments("Failed to create metalake with invalid arguments", e);
+
} catch (MetalakeAlreadyExistsException exception) {
LOG.warn("Metalake {} already exists", request.getName(), exception);
return Utils.alreadyExists("Metalake " + request.getName() + " already exists", exception);
@@ -98,16 +104,15 @@ public class MetalakeOperations {
@Path("{name}")
@Produces("application/vnd.graviton.v1+json")
public Response loadMetalake(@PathParam("name") String metalakeName) {
- if (metalakeName == null || metalakeName.isEmpty()) {
- LOG.error("Metalake name is null or empty");
- return Utils.illegalArguments("Metalake name is illegal");
- }
-
try {
- NameIdentifier identifier = NameIdentifier.parse(metalakeName);
+ NameIdentifier identifier = NameIdentifier.ofMetalake(metalakeName);
BaseMetalake metalake = manager.loadMetalake(identifier);
return Utils.ok(new MetalakeResponse(DTOConverters.toDTO(metalake)));
+ } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
+ LOG.warn("Failed to load metalake with invalid arguments", e);
+ return Utils.illegalArguments("Failed to load metalake with invalid arguments", e);
+
} catch (NoSuchMetalakeException e) {
LOG.warn("Metalake {} does not exist", metalakeName);
return Utils.notFound("Metalake " + metalakeName + " does not exist", e);
@@ -123,11 +128,6 @@ public class MetalakeOperations {
@Produces("application/vnd.graviton.v1+json")
public Response alterMetalake(
@PathParam("name") String metalakeName, MetalakeUpdatesRequest updatesRequest) {
- if (metalakeName == null || metalakeName.isEmpty()) {
- LOG.error("Metalake name is null or empty");
- return Utils.illegalArguments("Metalake name is illegal");
- }
-
try {
updatesRequest.validate();
} catch (Exception e) {
@@ -137,7 +137,7 @@ public class MetalakeOperations {
}
try {
- NameIdentifier identifier = NameIdentifier.parse(metalakeName);
+ NameIdentifier identifier = NameIdentifier.ofMetalake(metalakeName);
MetalakeChange[] changes =
updatesRequest.getUpdates().stream()
.map(MetalakeUpdateRequest::metalakeChange)
@@ -146,6 +146,10 @@ public class MetalakeOperations {
BaseMetalake updatedMetalake = manager.alterMetalake(identifier, changes);
return Utils.ok(new MetalakeResponse(DTOConverters.toDTO(updatedMetalake)));
+ } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
+ LOG.warn("Failed to alter metalake with invalid arguments", e);
+ return Utils.illegalArguments("Failed to alter metalake with invalid arguments", e);
+
} catch (NoSuchMetalakeException e) {
LOG.warn("Metalake {} does not exist", metalakeName);
return Utils.notFound("Metalake " + metalakeName + " does not exist", e);
@@ -165,13 +169,8 @@ public class MetalakeOperations {
@Path("{name}")
@Produces("application/vnd.graviton.v1+json")
public Response dropMetalake(@PathParam("name") String metalakeName) {
- if (metalakeName == null || metalakeName.isEmpty()) {
- LOG.error("Metalake name is null or empty");
- return Utils.illegalArguments("Metalake name is illegal");
- }
-
try {
- NameIdentifier identifier = NameIdentifier.parse(metalakeName);
+ NameIdentifier identifier = NameIdentifier.ofMetalake(metalakeName);
boolean dropped = manager.dropMetalake(identifier);
if (!dropped) {
LOG.warn("Failed to drop metalake by name {}", metalakeName);
@@ -179,6 +178,10 @@ public class MetalakeOperations {
return Utils.ok(new DropResponse(dropped));
+ } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
+ LOG.warn("Failed to drop metalake with invalid arguments", e);
+ return Utils.illegalArguments("Failed to drop metalake with invalid arguments", e);
+
} catch (Exception e) {
LOG.error("Failed to drop metalake {}", metalakeName, e);
return Utils.internalError("Failed to drop metalake " + metalakeName, e);
diff --git a/server/src/main/java/com/datastrato/graviton/server/web/rest/SchemaOperations.java b/server/src/main/java/com/datastrato/graviton/server/web/rest/SchemaOperations.java
index be2d305f61..32144411a4 100644
--- a/server/src/main/java/com/datastrato/graviton/server/web/rest/SchemaOperations.java
+++ b/server/src/main/java/com/datastrato/graviton/server/web/rest/SchemaOperations.java
@@ -13,6 +13,8 @@ import com.datastrato.graviton.dto.requests.SchemaUpdatesRequest;
import com.datastrato.graviton.dto.responses.DropResponse;
import com.datastrato.graviton.dto.responses.EntityListResponse;
import com.datastrato.graviton.dto.responses.SchemaResponse;
+import com.datastrato.graviton.exceptions.IllegalNameIdentifierException;
+import com.datastrato.graviton.exceptions.IllegalNamespaceException;
import com.datastrato.graviton.exceptions.NoSuchCatalogException;
import com.datastrato.graviton.exceptions.NoSuchSchemaException;
import com.datastrato.graviton.exceptions.NonEmptySchemaException;
@@ -20,7 +22,6 @@ import com.datastrato.graviton.exceptions.SchemaAlreadyExistsException;
import com.datastrato.graviton.rel.Schema;
import com.datastrato.graviton.rel.SchemaChange;
import com.datastrato.graviton.server.web.Utils;
-import com.google.common.base.Preconditions;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.Consumes;
@@ -36,7 +37,6 @@ import javax.ws.rs.QueryParam;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
-import org.eclipse.jetty.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -61,13 +61,13 @@ public class SchemaOperations {
public Response listSchemas(
@PathParam("metalake") String metalake, @PathParam("catalog") String catalog) {
try {
- Namespace ns = schemaNS(metalake, catalog);
- NameIdentifier[] idents = dispatcher.listSchemas(ns);
+ Namespace schemaNS = Namespace.ofSchema(metalake, catalog);
+ NameIdentifier[] idents = dispatcher.listSchemas(schemaNS);
return Utils.ok(new EntityListResponse(idents));
- } catch (IllegalArgumentException e) {
- LOG.error("Failed to validate arguments", e);
- return Utils.illegalArguments("Failed to validate arguments", e);
+ } catch (IllegalNamespaceException e) {
+ LOG.error("Failed to list schemas with invalid namespace", e);
+ return Utils.illegalArguments("Failed to list schemas with invalid namespace", e);
} catch (NoSuchCatalogException e) {
LOG.error("Catalog {} does not exist, fail to list schemas", catalog);
@@ -94,13 +94,13 @@ public class SchemaOperations {
}
try {
- NameIdentifier ident = schemaIdentifier(metalake, catalog, request.getName());
+ NameIdentifier ident = NameIdentifier.ofSchema(metalake, catalog, request.getName());
Schema schema = dispatcher.createSchema(ident, request.getComment(), request.getProperties());
return Utils.ok(new SchemaResponse(DTOConverters.toDTO(schema)));
- } catch (IllegalArgumentException e) {
- LOG.error("Failed to validate arguments", e);
- return Utils.illegalArguments("Failed to validate arguments", e);
+ } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
+ LOG.warn("Failed to create schema with invalid namespace", e);
+ return Utils.illegalArguments("Failed to create schema with invalid namespace", e);
} catch (NoSuchCatalogException e) {
LOG.error(
@@ -128,13 +128,13 @@ public class SchemaOperations {
@PathParam("catalog") String catalog,
@PathParam("schema") String schema) {
try {
- NameIdentifier ident = schemaIdentifier(metalake, catalog, schema);
+ NameIdentifier ident = NameIdentifier.ofSchema(metalake, catalog, schema);
Schema s = dispatcher.loadSchema(ident);
return Utils.ok(new SchemaResponse(DTOConverters.toDTO(s)));
- } catch (IllegalArgumentException e) {
- LOG.error("Failed to validate arguments", e);
- return Utils.illegalArguments("Failed to validate arguments", e);
+ } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
+ LOG.warn("Failed to load schema with invalid namespace", e);
+ return Utils.illegalArguments("Failed to load schema with invalid namespace", e);
} catch (NoSuchSchemaException e) {
LOG.error("Schema {} does not exist under catalog {}", schema, catalog);
@@ -154,14 +154,6 @@ public class SchemaOperations {
@PathParam("catalog") String catalog,
@PathParam("schema") String schema,
SchemaUpdatesRequest request) {
- NameIdentifier ident;
- try {
- ident = schemaIdentifier(metalake, catalog, schema);
- } catch (IllegalArgumentException e) {
- LOG.error("Failed to validate arguments", e);
- return Utils.illegalArguments("Failed to validate arguments", e);
- }
-
try {
request.validate();
} catch (IllegalArgumentException e) {
@@ -175,18 +167,21 @@ public class SchemaOperations {
.map(SchemaUpdateRequest::schemaChange)
.toArray(SchemaChange[]::new);
try {
+ NameIdentifier ident = NameIdentifier.ofSchema(metalake, catalog, schema);
Schema s = dispatcher.alterSchema(ident, changes);
return Utils.ok(new SchemaResponse(DTOConverters.toDTO(s)));
+ } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
+ LOG.warn("Failed to alter schema with invalid namespace", e);
+ return Utils.illegalArguments("Failed to alter schema with invalid namespace", e);
+
} catch (NoSuchSchemaException e) {
- LOG.error("Schema {} does not exist under namespace {}", schema, ident.namespace());
- return Utils.notFound(
- "Schema " + schema + " does not exist under namespace " + ident.namespace(), e);
+ LOG.error("Schema {} does not exist under catalog {}", schema, catalog);
+ return Utils.notFound("Schema " + schema + " does not exist under catalog " + catalog, e);
} catch (Exception e) {
- LOG.error("Fail to alter schema {} under namespace {}", schema, ident.namespace(), e);
- return Utils.internalError(
- "Fail to alter schema " + schema + " under namespace " + ident.namespace(), e);
+ LOG.error("Fail to alter schema {} under catalog {}", schema, catalog, e);
+ return Utils.internalError("Fail to alter schema " + schema + " under catalog " + catalog, e);
}
}
@@ -199,7 +194,7 @@ public class SchemaOperations {
@PathParam("schema") String schema,
@DefaultValue("false") @QueryParam("cascade") boolean cascade) {
try {
- NameIdentifier ident = NameIdentifier.of(metalake, catalog, schema);
+ NameIdentifier ident = NameIdentifier.ofSchema(metalake, catalog, schema);
boolean dropped = dispatcher.dropSchema(ident, cascade);
if (!dropped) {
LOG.warn("Fail to drop schema {} under namespace {}", schema, ident.namespace());
@@ -207,10 +202,9 @@ public class SchemaOperations {
return Utils.ok(new DropResponse(dropped));
- } catch (IllegalArgumentException e) {
- LOG.error("Failed to validate arguments {} {} {}", metalake, catalog, schema, e);
- return Utils.illegalArguments(
- "Failed to validate arguments " + metalake + " " + catalog + " " + schema, e);
+ } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
+ LOG.warn("Failed to drop schema with invalid namespace", e);
+ return Utils.illegalArguments("Failed to drop schema with invalid namespace", e);
} catch (NonEmptySchemaException e) {
LOG.error(
@@ -229,24 +223,4 @@ public class SchemaOperations {
return Utils.internalError("Fail to drop schema " + schema + " under catalog " + catalog, e);
}
}
-
- private static Namespace schemaNS(String metalake, String catalog) {
- Preconditions.checkArgument(
- StringUtil.isNotBlank(metalake), "metalake name: %s is illegal", metalake);
- Preconditions.checkArgument(
- StringUtil.isNotBlank(catalog), "catalog name: %s is illegal", catalog);
-
- return Namespace.of(metalake, catalog);
- }
-
- private static NameIdentifier schemaIdentifier(String metalake, String catalog, String schema) {
- Preconditions.checkArgument(
- StringUtil.isNotBlank(metalake), "metalake name: %s is illegal", metalake);
- Preconditions.checkArgument(
- StringUtil.isNotBlank(catalog), "catalog name: %s is illegal", catalog);
- Preconditions.checkArgument(
- StringUtil.isNotBlank(schema), "schema name: %s is illegal", schema);
-
- return NameIdentifier.of(metalake, catalog, schema);
- }
}
diff --git a/server/src/main/java/com/datastrato/graviton/server/web/rest/TableOperations.java b/server/src/main/java/com/datastrato/graviton/server/web/rest/TableOperations.java
index 32e928e9ba..c1a276bd13 100644
--- a/server/src/main/java/com/datastrato/graviton/server/web/rest/TableOperations.java
+++ b/server/src/main/java/com/datastrato/graviton/server/web/rest/TableOperations.java
@@ -13,13 +13,14 @@ import com.datastrato.graviton.dto.requests.TableUpdatesRequest;
import com.datastrato.graviton.dto.responses.DropResponse;
import com.datastrato.graviton.dto.responses.EntityListResponse;
import com.datastrato.graviton.dto.responses.TableResponse;
+import com.datastrato.graviton.exceptions.IllegalNameIdentifierException;
+import com.datastrato.graviton.exceptions.IllegalNamespaceException;
import com.datastrato.graviton.exceptions.NoSuchSchemaException;
import com.datastrato.graviton.exceptions.NoSuchTableException;
import com.datastrato.graviton.exceptions.TableAlreadyExistsException;
import com.datastrato.graviton.rel.Table;
import com.datastrato.graviton.rel.TableChange;
import com.datastrato.graviton.server.web.Utils;
-import com.google.common.base.Preconditions;
import javax.inject.Inject;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DELETE;
@@ -31,7 +32,6 @@ import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.Response;
-import org.eclipse.jetty.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -56,13 +56,13 @@ public class TableOperations {
@PathParam("catalog") String catalog,
@PathParam("schema") String schema) {
try {
- Namespace ns = tableNS(metalake, catalog, schema);
- NameIdentifier[] idents = dispatcher.listTables(ns);
+ Namespace tableNS = Namespace.ofTable(metalake, catalog, schema);
+ NameIdentifier[] idents = dispatcher.listTables(tableNS);
return Utils.ok(new EntityListResponse(idents));
- } catch (IllegalArgumentException e) {
- LOG.error("Failed to validate arguments", e);
- return Utils.illegalArguments("Failed to validate arguments", e);
+ } catch (IllegalNamespaceException e) {
+ LOG.error("Failed to list tables with invalid arguments", e);
+ return Utils.illegalArguments("Failed to list tables with invalid arguments", e);
} catch (NoSuchSchemaException e) {
LOG.error("Schema {} does not exist, fail to list tables", schema);
@@ -89,15 +89,16 @@ public class TableOperations {
}
try {
- NameIdentifier ident = tableIdentifier(metalake, catalog, schema, request.getName());
+ NameIdentifier ident = NameIdentifier.ofTable(metalake, catalog, schema, request.getName());
Table table =
dispatcher.createTable(
ident, request.getColumns(), request.getComment(), request.getProperties());
return Utils.ok(new TableResponse(DTOConverters.toDTO(table)));
- } catch (IllegalArgumentException e) {
- LOG.error("Failed to validate arguments", e);
- return Utils.illegalArguments("Failed to validate arguments", e);
+ } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
+ LOG.warn("Failed to create table {} with invalid arguments", request.getName(), e);
+ return Utils.illegalArguments(
+ "Failed to create table " + request.getName() + " with invalid arguments", e);
} catch (NoSuchSchemaException e) {
LOG.error("Schema {} does not exist, fail to create table {}", schema, request.getName());
@@ -125,13 +126,13 @@ public class TableOperations {
@PathParam("schema") String schema,
@PathParam("table") String table) {
try {
- NameIdentifier ident = tableIdentifier(metalake, catalog, schema, table);
+ NameIdentifier ident = NameIdentifier.ofTable(metalake, catalog, schema, table);
Table t = dispatcher.loadTable(ident);
return Utils.ok(new TableResponse(DTOConverters.toDTO(t)));
- } catch (IllegalArgumentException e) {
- LOG.error("Failed to validate arguments", e);
- return Utils.illegalArguments("Failed to validate arguments", e);
+ } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
+ LOG.warn("Failed to load table {} with invalid arguments", table, e);
+ return Utils.illegalArguments("Failed to load table " + table + " with invalid arguments", e);
} catch (NoSuchTableException e) {
LOG.error("Table {} does not exist under schema {}", table, schema);
@@ -160,7 +161,7 @@ public class TableOperations {
}
try {
- NameIdentifier ident = tableIdentifier(metalake, catalog, schema, table);
+ NameIdentifier ident = NameIdentifier.ofTable(metalake, catalog, schema, table);
TableChange[] changes =
request.getUpdates().stream()
.map(TableUpdateRequest::tableChange)
@@ -168,9 +169,10 @@ public class TableOperations {
Table t = dispatcher.alterTable(ident, changes);
return Utils.ok(new TableResponse(DTOConverters.toDTO(t)));
- } catch (IllegalArgumentException e) {
- LOG.error("Failed to validate arguments", e);
- return Utils.illegalArguments("Failed to validate arguments", e);
+ } catch (IllegalNameIdentifierException | IllegalNamespaceException e) {
+ LOG.warn("Failed to alter table {} with invalid arguments", table, e);
+ return Utils.illegalArguments(
+ "Failed to alter table " + table + " with invalid arguments", e);
} catch (NoSuchTableException e) {
LOG.error("Table {} does not exist under schema {}", table, schema);
@@ -191,7 +193,7 @@ public class TableOperations {
@PathParam("schema") String schema,
@PathParam("table") String table) {
try {
- NameIdentifier ident = tableIdentifier(metalake, catalog, schema, table);
+ NameIdentifier ident = NameIdentifier.ofTable(metalake, catalog, schema, table);
boolean dropped = dispatcher.dropTable(ident);
if (!dropped) {
LOG.warn("Failed to drop table {} under schema {}", table, schema);
@@ -199,31 +201,13 @@ public class TableOperations {
return Utils.ok(new DropResponse(dropped));
- } catch (IllegalArgumentException e) {
- LOG.error("Failed to validate arguments", e);
- return Utils.illegalArguments("Failed to validate arguments", e);
+ } catch (IllegalNamespaceException | IllegalNameIdentifierException e) {
+ LOG.warn("Failed to drop table {} with invalid arguments", table, e);
+ return Utils.illegalArguments("Failed to drop table " + table + " with invalid arguments", e);
} catch (Exception e) {
LOG.error("Failed to drop table {} under schema {}", table, schema, e);
return Utils.internalError("Failed to drop table " + table + " under schema " + schema, e);
}
}
-
- private static Namespace tableNS(String metalake, String catalog, String schema) {
- Preconditions.checkArgument(
- StringUtil.isNotBlank(metalake), "metalake name: %s is illegal", metalake);
- Preconditions.checkArgument(
- StringUtil.isNotBlank(catalog), "catalog name: %s is illegal", catalog);
- Preconditions.checkArgument(
- StringUtil.isNotBlank(schema), "schema name: %s is illegal", schema);
-
- return Namespace.of(metalake, catalog, schema);
- }
-
- private static NameIdentifier tableIdentifier(
- String metalake, String catalog, String schema, String table) {
- Namespace ns = tableNS(metalake, catalog, schema);
- Preconditions.checkArgument(StringUtil.isNotBlank(table), "table name: %s is illegal", table);
- return NameIdentifier.of(ns, table);
- }
}