Skip to content

Commit

Permalink
[#32] feat(core): add Entity Operation interface and Entity name iden…
Browse files Browse the repository at this point in the history
…tifier (#33)

### What changes were proposed in this pull request?

This PR is a preconditional PR to support REST API for Graviton. This PR
defines:

1. Entity's name identifier to distinguish between entities.
2. Entity operation interfaces. We will later on implement this
interface to manipulate the entities.

### Why are the changes needed?

This PR is a preconditional PR to support REST API for Graviton.

Fix: #32 

### Does this PR introduce _any_ user-facing change?

N/A

### How was this patch tested?

New UTs to cover the test.
  • Loading branch information
jerryshao authored May 30, 2023
1 parent 8f3d1d6 commit 399feb8
Show file tree
Hide file tree
Showing 17 changed files with 343 additions and 18 deletions.
45 changes: 45 additions & 0 deletions core/src/main/java/com/datastrato/graviton/EntityOperations.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.datastrato.graviton;

import com.datastrato.graviton.schema.Entity;
import com.datastrato.graviton.schema.HasIdentifier;
import com.datastrato.graviton.schema.NameIdentifier;

public interface EntityOperations<T extends Entity & HasIdentifier> {

/**
* Creates the entity.
*
* @param t the entity to create.
*/
default void create(T t) {
throw new UnsupportedOperationException("Not implemented yet");
}

/**
* Gets the entity by name identifier.
*
* @param nameIdentifier the name identifier of the entity.
* @return the entity.
*/
default T get(NameIdentifier nameIdentifier) {
throw new UnsupportedOperationException("Not implemented yet");
}

/**
* Updates the entity.
*
* @param t the entity to update.
*/
default void update(T t) {
throw new UnsupportedOperationException("Not implemented yet");
}

/**
* Deletes the entity by name identifier.
*
* @param nameIdentifier the name identifier of the entity.
*/
default void delete(NameIdentifier nameIdentifier) {
throw new UnsupportedOperationException("Not implemented yet");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
@Getter
@EqualsAndHashCode
@ToString
public final class Column implements Entity, Auditable {
public final class Column implements Entity, Auditable, HasIdentifier {

public static final Field ID =
Field.required("id", Integer.class, "The unique identifier of the column");
Expand Down Expand Up @@ -88,6 +88,11 @@ public AuditInfo auditInfo() {
return auditInfo;
}

@Override
public String name() {
return name;
}

public static class Builder {
private final Column column;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.fasterxml.jackson.annotation.JsonTypeInfo;

/** Interface for entities that have extra info. */
public interface hasExtraInfo {
public interface HasExtraInfo {

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({@JsonSubTypes.Type(value = VirtualTableInfo.class, name = "VIRTUAL")})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.datastrato.graviton.schema;

public interface HasIdentifier {

/**
* Return the name of the entity.
*
* @return A String with the name of the entity.
*/
String name();

/**
* Returns the name identifier of the entity.
*
* @return NameIdentifier of the entity.
*/
default NameIdentifier nameIdentifier(Namespace namespace) {
return NameIdentifier.of(namespace, name());
}

// TODO. Returns a binary compact unique identifier of the entity. @Jerry
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@Getter
@EqualsAndHashCode
@ToString
public class Lakehouse implements Entity, Auditable {
public class Lakehouse implements Entity, Auditable, HasIdentifier {

public static final Field ID =
Field.required("id", Long.class, "The unique identifier of the lakehouse");
Expand Down Expand Up @@ -61,6 +61,11 @@ public AuditInfo auditInfo() {
return auditInfo;
}

@Override
public String name() {
return name;
}

public static class Builder {
private final Lakehouse lakehouse;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.datastrato.graviton.schema;

import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
import java.util.Arrays;

public class NameIdentifier {

private static final Splitter DOT = Splitter.on('.');

private final Namespace namespace;

private final String name;

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");

return new NameIdentifier(
Namespace.of(Arrays.copyOf(names, names.length - 1)), names[names.length - 1]);
}

public static NameIdentifier of(Namespace namespace, String name) {
return new NameIdentifier(namespace, name);
}

public static NameIdentifier parse(String identifier) {
Preconditions.checkArgument(
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(
name != null && !name.isEmpty(), "Cannot create a NameIdentifier with null or empty name");

this.namespace = namespace;
this.name = name;
}

public boolean hasNamespace() {
return !namespace.isEmpty();
}

public Namespace namespace() {
return namespace;
}

public String name() {
return name;
}

@Override
public boolean equals(Object other) {
if (other == null || !(other instanceof NameIdentifier)) {
return false;
}

NameIdentifier otherNameIdentifier = (NameIdentifier) other;
return namespace.equals(otherNameIdentifier.namespace) && name.equals(otherNameIdentifier.name);
}

@Override
public int hashCode() {
return Arrays.hashCode(new int[] {namespace.hashCode(), name.hashCode()});
}

@Override
public String toString() {
if (hasNamespace()) {
return namespace.toString() + "." + name;
} else {
return name;
}
}
}
72 changes: 72 additions & 0 deletions core/src/main/java/com/datastrato/graviton/schema/Namespace.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.datastrato.graviton.schema;

import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;
import java.util.Arrays;

public class Namespace {

private static final Namespace EMPTY = new Namespace(new String[0]);
private static final Joiner DOT = Joiner.on('.');

private final String[] levels;

public static Namespace empty() {
return EMPTY;
}

public static Namespace of(String... levels) {
Preconditions.checkArgument(levels != null, "Cannot create a namespace with null levels");
if (levels.length == 0) {
return empty();
}

for (String level : levels) {
Preconditions.checkArgument(
level != null && !level.isEmpty(), "Cannot create a namespace with null or empty level");
}

return new Namespace(levels);
}

private Namespace(String[] levels) {
this.levels = levels;
}

public String[] levels() {
return levels;
}

public String level(int pos) {
Preconditions.checkArgument(pos >= 0 && pos < levels.length, "Invalid level position");
return levels[pos];
}

public int length() {
return levels.length;
}

public boolean isEmpty() {
return levels.length == 0;
}

@Override
public boolean equals(Object other) {
if (other == null || !(other instanceof Namespace)) {
return false;
}

Namespace otherNamespace = (Namespace) other;
return Arrays.equals(levels, otherNamespace.levels);
}

@Override
public int hashCode() {
return Arrays.hashCode(levels);
}

@Override
public String toString() {
return DOT.join(levels);
}
}
15 changes: 10 additions & 5 deletions core/src/main/java/com/datastrato/graviton/schema/Table.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Getter
@EqualsAndHashCode
@ToString
public class Table implements Entity, Auditable, hasExtraInfo {
public class Table implements Entity, Auditable, HasExtraInfo, HasIdentifier {
public enum TableType {
VIRTUAL("VIRTUAL"),
VIEW("VIEW"),
Expand Down Expand Up @@ -45,7 +45,7 @@ public String getTypeStr() {
public static final Field AUDIT_INFO =
Field.required("audit_info", AuditInfo.class, "The audit info of the table");
public static final Field EXTRA_INFO =
Field.required("extra_info", hasExtraInfo.ExtraInfo.class, "The extra info of the table");
Field.required("extra_info", HasExtraInfo.ExtraInfo.class, "The extra info of the table");

@JsonProperty("id")
private Long id;
Expand Down Expand Up @@ -74,7 +74,7 @@ public String getTypeStr() {
private AuditInfo auditInfo;

@JsonProperty("extra_info")
private hasExtraInfo.ExtraInfo extraInfo;
private HasExtraInfo.ExtraInfo extraInfo;

@JsonProperty("columns")
private List<Column> columns;
Expand Down Expand Up @@ -103,10 +103,15 @@ public AuditInfo auditInfo() {
}

@Override
public hasExtraInfo.ExtraInfo extraInfo() {
public HasExtraInfo.ExtraInfo extraInfo() {
return extraInfo;
}

@Override
public String name() {
return name;
}

public static class Builder {
private final Table table;

Expand Down Expand Up @@ -154,7 +159,7 @@ public Builder withAuditInfo(AuditInfo auditInfo) {
return this;
}

public Builder withExtraInfo(hasExtraInfo.ExtraInfo extraInfo) {
public Builder withExtraInfo(HasExtraInfo.ExtraInfo extraInfo) {
table.extraInfo = extraInfo;
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@Getter
@EqualsAndHashCode
@ToString
public final class Tenant implements Entity, Auditable {
public final class Tenant implements Entity, Auditable, HasIdentifier {

public static final Field ID =
Field.required("id", Integer.class, "The unique identifier of the tenant");
Expand Down Expand Up @@ -59,6 +59,11 @@ public AuditInfo auditInfo() {
return auditInfo;
}

@Override
public String name() {
return name;
}

public static class Builder {
private final Tenant tenant;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
@Getter
@EqualsAndHashCode
@ToString
public final class VirtualTableInfo implements hasExtraInfo.ExtraInfo {
public final class VirtualTableInfo implements HasExtraInfo.ExtraInfo {

public static final Field CONNECTION_ID =
Field.required("connection_id", Integer.class, "The unique identifier of the connection");
Expand Down
7 changes: 6 additions & 1 deletion core/src/main/java/com/datastrato/graviton/schema/Zone.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
@Getter
@EqualsAndHashCode
@ToString
public class Zone implements Entity, Auditable {
public class Zone implements Entity, Auditable, HasIdentifier {

public static final Field ID =
Field.required("id", Long.class, "The unique identifier of the zone");
Expand Down Expand Up @@ -66,6 +66,11 @@ public AuditInfo auditInfo() {
return auditInfo;
}

@Override
public String name() {
return name;
}

public static class Builder {
private final Zone zone;

Expand Down
Loading

0 comments on commit 399feb8

Please sign in to comment.