Skip to content

Commit aeeacf3

Browse files
Implement AddTableToSchema (#111)
1 parent 020c9bb commit aeeacf3

26 files changed

+754
-118
lines changed

docs/protocol/whitefox-protocol-api.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -997,7 +997,13 @@ paths:
997997
content:
998998
application/json:
999999
schema:
1000-
$ref: "#/components/schemas/TableReference"
1000+
type: object
1001+
properties:
1002+
name:
1003+
type: string
1004+
description: public name of the table; can be different from the internal name used in `reference`
1005+
reference:
1006+
$ref: "#/components/schemas/TableReference"
10011007
responses:
10021008
"200":
10031009
description: table added to schema

server/src/main/java/io/whitefox/api/server/ShareV1ApiImpl.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package io.whitefox.api.server;
22

33
import io.whitefox.api.model.v1.generated.AddRecipientToShareRequest;
4+
import io.whitefox.api.model.v1.generated.AddTableToSchemaRequest;
45
import io.whitefox.api.model.v1.generated.CreateShareInput;
5-
import io.whitefox.api.model.v1.generated.TableReference;
66
import io.whitefox.api.server.v1.generated.ShareV1Api;
7+
import io.whitefox.core.*;
78
import io.whitefox.core.services.ShareService;
89
import jakarta.ws.rs.core.Response;
910

@@ -29,8 +30,19 @@ public Response addRecipientToShare(
2930
}
3031

3132
@Override
32-
public Response addTableToSchema(String share, String schema, TableReference tableReference) {
33-
return Response.status(501).build();
33+
public Response addTableToSchema(
34+
String share, String schema, AddTableToSchemaRequest addTableToSchemaRequest) {
35+
return wrapExceptions(
36+
() -> Response.status(Response.Status.CREATED)
37+
.entity(WhitefoxMappers.share2api(shareService.addTableToSchema(
38+
new ShareName(share),
39+
new SchemaName(schema),
40+
new SharedTableName(addTableToSchemaRequest.getName()),
41+
new ProviderName(addTableToSchemaRequest.getReference().getProviderName()),
42+
new InternalTableName(addTableToSchemaRequest.getReference().getName()),
43+
getRequestPrincipal())))
44+
.build(),
45+
exceptionToResponse);
3446
}
3547

3648
@Override
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.whitefox.core;
2+
3+
import io.whitefox.annotations.SkipCoverageGenerated;
4+
import java.util.Objects;
5+
6+
public class InternalTableName {
7+
private final String name;
8+
9+
public InternalTableName(String name) {
10+
this.name = name;
11+
}
12+
13+
@Override
14+
@SkipCoverageGenerated
15+
public boolean equals(Object o) {
16+
if (this == o) return true;
17+
if (o == null || getClass() != o.getClass()) return false;
18+
InternalTableName that = (InternalTableName) o;
19+
return Objects.equals(name, that.name);
20+
}
21+
22+
@Override
23+
@SkipCoverageGenerated
24+
public int hashCode() {
25+
return Objects.hash(name);
26+
}
27+
28+
public String name() {
29+
return name;
30+
}
31+
32+
@Override
33+
@SkipCoverageGenerated
34+
public String toString() {
35+
return name;
36+
}
37+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.whitefox.core;
2+
3+
import io.whitefox.annotations.SkipCoverageGenerated;
4+
import java.util.Objects;
5+
6+
public class ProviderName {
7+
private final String name;
8+
9+
public ProviderName(String share) {
10+
this.name = share;
11+
}
12+
13+
public String name() {
14+
return name;
15+
}
16+
17+
@Override
18+
@SkipCoverageGenerated
19+
public boolean equals(Object o) {
20+
if (this == o) return true;
21+
if (o == null || getClass() != o.getClass()) return false;
22+
ProviderName that = (ProviderName) o;
23+
return Objects.equals(name, that.name);
24+
}
25+
26+
@Override
27+
@SkipCoverageGenerated
28+
public int hashCode() {
29+
return Objects.hash(name);
30+
}
31+
32+
@Override
33+
@SkipCoverageGenerated
34+
public String toString() {
35+
return name;
36+
}
37+
}

server/src/main/java/io/whitefox/core/Schema.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
package io.whitefox.core;
22

33
import io.whitefox.annotations.SkipCoverageGenerated;
4-
import java.util.List;
5-
import java.util.Objects;
4+
import java.util.*;
5+
import java.util.function.Function;
6+
import java.util.stream.Collectors;
67

78
public final class Schema {
89
private final String name;
9-
private final List<SharedTable> sharedTables;
10+
private final Map<String, SharedTable> sharedTables;
1011
private final String share;
1112

12-
public Schema(String name, List<SharedTable> sharedTables, String share) {
13+
public Schema(String name, Collection<SharedTable> sharedTables, String share) {
14+
this(
15+
name,
16+
sharedTables.stream().collect(Collectors.toMap(SharedTable::name, Function.identity())),
17+
share);
18+
}
19+
20+
public Schema(String name, Map<String, SharedTable> sharedTables, String share) {
1321
this.name = name;
1422
this.sharedTables = sharedTables;
1523
this.share = share;
@@ -19,8 +27,8 @@ public String name() {
1927
return name;
2028
}
2129

22-
public List<SharedTable> tables() {
23-
return sharedTables;
30+
public Set<SharedTable> tables() {
31+
return Set.copyOf(sharedTables.values());
2432
}
2533

2634
public String share() {
@@ -50,4 +58,11 @@ public String toString() {
5058
return "Schema[" + "name=" + name + ", " + "tables=" + sharedTables + ", " + "share=" + share
5159
+ ']';
5260
}
61+
62+
public Schema addTable(InternalTable table, SharedTableName sharedTableName) {
63+
var newSharedTables = new HashMap<>(sharedTables);
64+
newSharedTables.put(
65+
sharedTableName.name(), new SharedTable(sharedTableName.name(), this.name, share, table));
66+
return new Schema(this.name, newSharedTables, share);
67+
}
5368
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.whitefox.core;
2+
3+
import io.whitefox.annotations.SkipCoverageGenerated;
4+
import java.util.Objects;
5+
6+
public class SchemaName {
7+
private final String name;
8+
9+
public SchemaName(String name) {
10+
this.name = name;
11+
}
12+
13+
public String name() {
14+
return name;
15+
}
16+
17+
@Override
18+
@SkipCoverageGenerated
19+
public boolean equals(Object o) {
20+
if (this == o) return true;
21+
if (o == null || getClass() != o.getClass()) return false;
22+
SchemaName that = (SchemaName) o;
23+
return Objects.equals(name, that.name);
24+
}
25+
26+
@Override
27+
@SkipCoverageGenerated
28+
public int hashCode() {
29+
return Objects.hash(name);
30+
}
31+
32+
@Override
33+
@SkipCoverageGenerated
34+
public String toString() {
35+
return name;
36+
}
37+
}

server/src/main/java/io/whitefox/core/Share.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ public Share addRecipients(List<Principal> recipients, Principal currentUser, lo
114114
owner);
115115
}
116116

117-
public Share addSchema(Schema schema, Principal currentUser, long now) {
117+
public Share upsertSchema(Schema schema, Principal currentUser, long now) {
118118
var newSchemas = new HashMap<>(schemas);
119119
newSchemas.put(schema.name(), schema);
120120
return new Share(
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package io.whitefox.core;
2+
3+
import io.whitefox.annotations.SkipCoverageGenerated;
4+
import java.util.Objects;
5+
6+
public final class ShareName {
7+
8+
private final String name;
9+
10+
public ShareName(String share) {
11+
this.name = share;
12+
}
13+
14+
public String name() {
15+
return name;
16+
}
17+
18+
@Override
19+
@SkipCoverageGenerated
20+
public boolean equals(Object o) {
21+
if (this == o) return true;
22+
if (o == null || getClass() != o.getClass()) return false;
23+
ShareName shareName = (ShareName) o;
24+
return Objects.equals(name, shareName.name);
25+
}
26+
27+
@Override
28+
@SkipCoverageGenerated
29+
public int hashCode() {
30+
return Objects.hash(name);
31+
}
32+
33+
@Override
34+
@SkipCoverageGenerated
35+
public String toString() {
36+
return name;
37+
}
38+
}

server/src/main/java/io/whitefox/core/SharedTable.java

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,21 @@
55

66
public final class SharedTable {
77
private final String name;
8-
private final String location;
98
private final String schema;
109
private final String share;
10+
private final InternalTable internalTable;
1111

12-
public SharedTable(String name, String location, String schema, String share) {
12+
public SharedTable(String name, String schema, String share, InternalTable internalTable) {
1313
this.name = name;
14-
this.location = location;
1514
this.schema = schema;
1615
this.share = share;
16+
this.internalTable = internalTable;
1717
}
1818

1919
public String name() {
2020
return name;
2121
}
2222

23-
public String location() {
24-
return location;
25-
}
26-
2723
public String schema() {
2824
return schema;
2925
}
@@ -32,31 +28,35 @@ public String share() {
3228
return share;
3329
}
3430

31+
public InternalTable internalTable() {
32+
return internalTable;
33+
}
34+
3535
@Override
3636
@SkipCoverageGenerated
37-
public boolean equals(Object obj) {
38-
if (obj == this) return true;
39-
if (obj == null || obj.getClass() != this.getClass()) return false;
40-
var that = (SharedTable) obj;
41-
return Objects.equals(this.name, that.name)
42-
&& Objects.equals(this.location, that.location)
43-
&& Objects.equals(this.schema, that.schema)
44-
&& Objects.equals(this.share, that.share);
37+
public boolean equals(Object o) {
38+
if (this == o) return true;
39+
if (o == null || getClass() != o.getClass()) return false;
40+
SharedTable that = (SharedTable) o;
41+
return Objects.equals(name, that.name)
42+
&& Objects.equals(schema, that.schema)
43+
&& Objects.equals(share, that.share)
44+
&& Objects.equals(internalTable, that.internalTable);
4545
}
4646

4747
@Override
4848
@SkipCoverageGenerated
4949
public int hashCode() {
50-
return Objects.hash(name, location, schema, share);
50+
return Objects.hash(name, schema, share, internalTable);
5151
}
5252

5353
@Override
5454
@SkipCoverageGenerated
5555
public String toString() {
56-
return "Table[" + "name="
57-
+ name + ", " + "location="
58-
+ location + ", " + "schema="
59-
+ schema + ", " + "share="
60-
+ share + ']';
56+
return "SharedTable{" + "name='"
57+
+ name + '\'' + ", schema='"
58+
+ schema + '\'' + ", share='"
59+
+ share + '\'' + ", internalTable="
60+
+ internalTable + '}';
6161
}
6262
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package io.whitefox.core;
2+
3+
import io.whitefox.annotations.SkipCoverageGenerated;
4+
import java.util.Objects;
5+
6+
public class SharedTableName {
7+
private final String name;
8+
9+
public SharedTableName(String name) {
10+
this.name = name;
11+
}
12+
13+
public String name() {
14+
return name;
15+
}
16+
17+
@Override
18+
@SkipCoverageGenerated
19+
public boolean equals(Object o) {
20+
if (this == o) return true;
21+
if (o == null || getClass() != o.getClass()) return false;
22+
SharedTableName that = (SharedTableName) o;
23+
return Objects.equals(name, that.name);
24+
}
25+
26+
@Override
27+
@SkipCoverageGenerated
28+
public int hashCode() {
29+
return Objects.hash(name);
30+
}
31+
32+
@Override
33+
@SkipCoverageGenerated
34+
public String toString() {
35+
return name;
36+
}
37+
}

0 commit comments

Comments
 (0)