-
Notifications
You must be signed in to change notification settings - Fork 387
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[#32] feat(core): add Entity Operation interface and Entity name iden…
…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
Showing
17 changed files
with
343 additions
and
18 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
core/src/main/java/com/datastrato/graviton/EntityOperations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
core/src/main/java/com/datastrato/graviton/schema/HasIdentifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
core/src/main/java/com/datastrato/graviton/schema/NameIdentifier.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
72
core/src/main/java/com/datastrato/graviton/schema/Namespace.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.