Skip to content

Commit 21742f8

Browse files
authored
Merge pull request #14 from weiguoz/clean_code
Build SQLFlow client by Builder pattern
2 parents 2749470 + 0edc400 commit 21742f8

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

src/main/java/org/sqlflow/client/SQLFlow.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,6 @@
2020
import proto.Sqlflow.Session;
2121

2222
public interface SQLFlow {
23-
/**
24-
* Open a channel to the SQLFlow server. The serverUrl argument always ends with a port.
25-
*
26-
* @param serverUrl an address the SQLFlow server exposed.
27-
* <p>Example: "localhost:50051"
28-
*/
29-
void init(String serverUrl);
30-
3123
/**
3224
* Submit a task to SQLFlow server. This method return immediately.
3325
*

src/main/java/org/sqlflow/client/impl/SQLFlowImpl.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515

1616
package org.sqlflow.client.impl;
1717

18-
import com.google.common.annotations.VisibleForTesting;
1918
import io.grpc.ManagedChannel;
2019
import io.grpc.ManagedChannelBuilder;
2120
import io.grpc.StatusRuntimeException;
@@ -32,14 +31,8 @@ public class SQLFlowImpl implements SQLFlow {
3231
private ManagedChannel channel;
3332
private SQLFlowGrpc.SQLFlowBlockingStub blockingStub;
3433

35-
public void init(String serverUrl) {
36-
this.channel = ManagedChannelBuilder.forTarget(serverUrl).usePlaintext().build();
37-
blockingStub = SQLFlowGrpc.newBlockingStub(channel);
38-
}
39-
40-
@VisibleForTesting
41-
public SQLFlowImpl(ManagedChannel channel) {
42-
this.channel = channel;
34+
private SQLFlowImpl(Builder builder) {
35+
this.channel = builder.channel;
4336
blockingStub = SQLFlowGrpc.newBlockingStub(channel);
4437
}
4538

@@ -80,4 +73,31 @@ public void release() throws InterruptedException {
8073
throw e;
8174
}
8275
}
76+
77+
public static class Builder {
78+
private ManagedChannel channel;
79+
80+
public static Builder newInstance() {
81+
return new Builder();
82+
}
83+
84+
public Builder withChannel(ManagedChannel channel) {
85+
this.channel = channel;
86+
return this;
87+
}
88+
89+
/**
90+
* Open a channel to the SQLFlow server. The serverUrl argument always ends with a port.
91+
*
92+
* @param serverUrl an address the SQLFlow server exposed.
93+
* <p>Example: "localhost:50051"
94+
*/
95+
public Builder forTarget(String serverUrl) {
96+
return withChannel(ManagedChannelBuilder.forTarget(serverUrl).usePlaintext().build());
97+
}
98+
99+
public SQLFlow build() {
100+
return new SQLFlowImpl(this);
101+
}
102+
}
83103
}

src/test/java/org/sqlflow/client/SQLFlowTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public void setUp() throws Exception {
8686

8787
ManagedChannel channel =
8888
grpcCleanup.register(InProcessChannelBuilder.forName(serverName).directExecutor().build());
89-
client = new SQLFlowImpl(channel);
89+
client = SQLFlowImpl.Builder.newInstance().withChannel(channel).build();
9090
}
9191

9292
@Test
@@ -98,7 +98,7 @@ public void testSubmit() {
9898
Session session =
9999
Session.newBuilder()
100100
.setUserId(userId)
101-
.setDbConnStr("mysql://root@[email protected]:3306/iris")
101+
.setDbConnStr("mysql://root:[email protected]:3306/iris")
102102
.build();
103103
String jobId = client.submit(session, sql);
104104
assertEquals(mockJobId(userId, sql), jobId);

0 commit comments

Comments
 (0)