Skip to content

Commit

Permalink
Implement AddTableToSchema (#111)
Browse files Browse the repository at this point in the history
  • Loading branch information
agilelab-tmnd1991 authored Oct 27, 2023
1 parent 020c9bb commit aeeacf3
Show file tree
Hide file tree
Showing 26 changed files with 754 additions and 118 deletions.
8 changes: 7 additions & 1 deletion docs/protocol/whitefox-protocol-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -997,7 +997,13 @@ paths:
content:
application/json:
schema:
$ref: "#/components/schemas/TableReference"
type: object
properties:
name:
type: string
description: public name of the table; can be different from the internal name used in `reference`
reference:
$ref: "#/components/schemas/TableReference"
responses:
"200":
description: table added to schema
Expand Down
18 changes: 15 additions & 3 deletions server/src/main/java/io/whitefox/api/server/ShareV1ApiImpl.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package io.whitefox.api.server;

import io.whitefox.api.model.v1.generated.AddRecipientToShareRequest;
import io.whitefox.api.model.v1.generated.AddTableToSchemaRequest;
import io.whitefox.api.model.v1.generated.CreateShareInput;
import io.whitefox.api.model.v1.generated.TableReference;
import io.whitefox.api.server.v1.generated.ShareV1Api;
import io.whitefox.core.*;
import io.whitefox.core.services.ShareService;
import jakarta.ws.rs.core.Response;

Expand All @@ -29,8 +30,19 @@ public Response addRecipientToShare(
}

@Override
public Response addTableToSchema(String share, String schema, TableReference tableReference) {
return Response.status(501).build();
public Response addTableToSchema(
String share, String schema, AddTableToSchemaRequest addTableToSchemaRequest) {
return wrapExceptions(
() -> Response.status(Response.Status.CREATED)
.entity(WhitefoxMappers.share2api(shareService.addTableToSchema(
new ShareName(share),
new SchemaName(schema),
new SharedTableName(addTableToSchemaRequest.getName()),
new ProviderName(addTableToSchemaRequest.getReference().getProviderName()),
new InternalTableName(addTableToSchemaRequest.getReference().getName()),
getRequestPrincipal())))
.build(),
exceptionToResponse);
}

@Override
Expand Down
37 changes: 37 additions & 0 deletions server/src/main/java/io/whitefox/core/InternalTableName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.whitefox.core;

import io.whitefox.annotations.SkipCoverageGenerated;
import java.util.Objects;

public class InternalTableName {
private final String name;

public InternalTableName(String name) {
this.name = name;
}

@Override
@SkipCoverageGenerated
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
InternalTableName that = (InternalTableName) o;
return Objects.equals(name, that.name);
}

@Override
@SkipCoverageGenerated
public int hashCode() {
return Objects.hash(name);
}

public String name() {
return name;
}

@Override
@SkipCoverageGenerated
public String toString() {
return name;
}
}
37 changes: 37 additions & 0 deletions server/src/main/java/io/whitefox/core/ProviderName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.whitefox.core;

import io.whitefox.annotations.SkipCoverageGenerated;
import java.util.Objects;

public class ProviderName {
private final String name;

public ProviderName(String share) {
this.name = share;
}

public String name() {
return name;
}

@Override
@SkipCoverageGenerated
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ProviderName that = (ProviderName) o;
return Objects.equals(name, that.name);
}

@Override
@SkipCoverageGenerated
public int hashCode() {
return Objects.hash(name);
}

@Override
@SkipCoverageGenerated
public String toString() {
return name;
}
}
27 changes: 21 additions & 6 deletions server/src/main/java/io/whitefox/core/Schema.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
package io.whitefox.core;

import io.whitefox.annotations.SkipCoverageGenerated;
import java.util.List;
import java.util.Objects;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

public final class Schema {
private final String name;
private final List<SharedTable> sharedTables;
private final Map<String, SharedTable> sharedTables;
private final String share;

public Schema(String name, List<SharedTable> sharedTables, String share) {
public Schema(String name, Collection<SharedTable> sharedTables, String share) {
this(
name,
sharedTables.stream().collect(Collectors.toMap(SharedTable::name, Function.identity())),
share);
}

public Schema(String name, Map<String, SharedTable> sharedTables, String share) {
this.name = name;
this.sharedTables = sharedTables;
this.share = share;
Expand All @@ -19,8 +27,8 @@ public String name() {
return name;
}

public List<SharedTable> tables() {
return sharedTables;
public Set<SharedTable> tables() {
return Set.copyOf(sharedTables.values());
}

public String share() {
Expand Down Expand Up @@ -50,4 +58,11 @@ public String toString() {
return "Schema[" + "name=" + name + ", " + "tables=" + sharedTables + ", " + "share=" + share
+ ']';
}

public Schema addTable(InternalTable table, SharedTableName sharedTableName) {
var newSharedTables = new HashMap<>(sharedTables);
newSharedTables.put(
sharedTableName.name(), new SharedTable(sharedTableName.name(), this.name, share, table));
return new Schema(this.name, newSharedTables, share);
}
}
37 changes: 37 additions & 0 deletions server/src/main/java/io/whitefox/core/SchemaName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.whitefox.core;

import io.whitefox.annotations.SkipCoverageGenerated;
import java.util.Objects;

public class SchemaName {
private final String name;

public SchemaName(String name) {
this.name = name;
}

public String name() {
return name;
}

@Override
@SkipCoverageGenerated
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SchemaName that = (SchemaName) o;
return Objects.equals(name, that.name);
}

@Override
@SkipCoverageGenerated
public int hashCode() {
return Objects.hash(name);
}

@Override
@SkipCoverageGenerated
public String toString() {
return name;
}
}
2 changes: 1 addition & 1 deletion server/src/main/java/io/whitefox/core/Share.java
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public Share addRecipients(List<Principal> recipients, Principal currentUser, lo
owner);
}

public Share addSchema(Schema schema, Principal currentUser, long now) {
public Share upsertSchema(Schema schema, Principal currentUser, long now) {
var newSchemas = new HashMap<>(schemas);
newSchemas.put(schema.name(), schema);
return new Share(
Expand Down
38 changes: 38 additions & 0 deletions server/src/main/java/io/whitefox/core/ShareName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.whitefox.core;

import io.whitefox.annotations.SkipCoverageGenerated;
import java.util.Objects;

public final class ShareName {

private final String name;

public ShareName(String share) {
this.name = share;
}

public String name() {
return name;
}

@Override
@SkipCoverageGenerated
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ShareName shareName = (ShareName) o;
return Objects.equals(name, shareName.name);
}

@Override
@SkipCoverageGenerated
public int hashCode() {
return Objects.hash(name);
}

@Override
@SkipCoverageGenerated
public String toString() {
return name;
}
}
42 changes: 21 additions & 21 deletions server/src/main/java/io/whitefox/core/SharedTable.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,21 @@

public final class SharedTable {
private final String name;
private final String location;
private final String schema;
private final String share;
private final InternalTable internalTable;

public SharedTable(String name, String location, String schema, String share) {
public SharedTable(String name, String schema, String share, InternalTable internalTable) {
this.name = name;
this.location = location;
this.schema = schema;
this.share = share;
this.internalTable = internalTable;
}

public String name() {
return name;
}

public String location() {
return location;
}

public String schema() {
return schema;
}
Expand All @@ -32,31 +28,35 @@ public String share() {
return share;
}

public InternalTable internalTable() {
return internalTable;
}

@Override
@SkipCoverageGenerated
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj == null || obj.getClass() != this.getClass()) return false;
var that = (SharedTable) obj;
return Objects.equals(this.name, that.name)
&& Objects.equals(this.location, that.location)
&& Objects.equals(this.schema, that.schema)
&& Objects.equals(this.share, that.share);
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SharedTable that = (SharedTable) o;
return Objects.equals(name, that.name)
&& Objects.equals(schema, that.schema)
&& Objects.equals(share, that.share)
&& Objects.equals(internalTable, that.internalTable);
}

@Override
@SkipCoverageGenerated
public int hashCode() {
return Objects.hash(name, location, schema, share);
return Objects.hash(name, schema, share, internalTable);
}

@Override
@SkipCoverageGenerated
public String toString() {
return "Table[" + "name="
+ name + ", " + "location="
+ location + ", " + "schema="
+ schema + ", " + "share="
+ share + ']';
return "SharedTable{" + "name='"
+ name + '\'' + ", schema='"
+ schema + '\'' + ", share='"
+ share + '\'' + ", internalTable="
+ internalTable + '}';
}
}
37 changes: 37 additions & 0 deletions server/src/main/java/io/whitefox/core/SharedTableName.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package io.whitefox.core;

import io.whitefox.annotations.SkipCoverageGenerated;
import java.util.Objects;

public class SharedTableName {
private final String name;

public SharedTableName(String name) {
this.name = name;
}

public String name() {
return name;
}

@Override
@SkipCoverageGenerated
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
SharedTableName that = (SharedTableName) o;
return Objects.equals(name, that.name);
}

@Override
@SkipCoverageGenerated
public int hashCode() {
return Objects.hash(name);
}

@Override
@SkipCoverageGenerated
public String toString() {
return name;
}
}
Loading

0 comments on commit aeeacf3

Please sign in to comment.