Skip to content

Commit bd729bb

Browse files
authored
feat(sync): Initial insert message support (#81)
1 parent ead7dd9 commit bd729bb

File tree

16 files changed

+245
-20
lines changed

16 files changed

+245
-20
lines changed

lib/src/main/java/io/cloudquery/internal/servers/plugin/v3/PluginServer.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.protobuf.ByteString;
44
import io.cloudquery.plugin.BackendOptions;
5+
import io.cloudquery.plugin.NewClientOptions;
56
import io.cloudquery.plugin.Plugin;
67
import io.cloudquery.plugin.v3.PluginGrpc.PluginImplBase;
78
import io.cloudquery.plugin.v3.Write;
@@ -41,9 +42,15 @@ public void getVersion(
4142
public void init(
4243
io.cloudquery.plugin.v3.Init.Request request,
4344
StreamObserver<io.cloudquery.plugin.v3.Init.Response> responseObserver) {
44-
plugin.init();
45-
responseObserver.onNext(io.cloudquery.plugin.v3.Init.Response.newBuilder().build());
46-
responseObserver.onCompleted();
45+
try {
46+
plugin.init(
47+
request.getSpec().toStringUtf8(),
48+
NewClientOptions.builder().noConnection(request.getNoConnection()).build());
49+
responseObserver.onNext(io.cloudquery.plugin.v3.Init.Response.newBuilder().build());
50+
responseObserver.onCompleted();
51+
} catch (Exception e) {
52+
responseObserver.onError(e);
53+
}
4754
}
4855

4956
@Override

lib/src/main/java/io/cloudquery/memdb/MemDB.java

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
package io.cloudquery.memdb;
22

33
import io.cloudquery.plugin.BackendOptions;
4+
import io.cloudquery.plugin.ClientNotInitializedException;
5+
import io.cloudquery.plugin.NewClientOptions;
46
import io.cloudquery.plugin.Plugin;
7+
import io.cloudquery.plugin.TableOutputStream;
58
import io.cloudquery.scheduler.Scheduler;
9+
import io.cloudquery.schema.ClientMeta;
610
import io.cloudquery.schema.Column;
11+
import io.cloudquery.schema.Resource;
712
import io.cloudquery.schema.SchemaException;
813
import io.cloudquery.schema.Table;
14+
import io.cloudquery.schema.TableResolver;
915
import io.grpc.stub.StreamObserver;
1016
import java.util.List;
1117
import org.apache.arrow.vector.types.pojo.ArrowType.Utf8;
@@ -15,26 +21,44 @@ public class MemDB extends Plugin {
1521
List.of(
1622
Table.builder()
1723
.name("table1")
18-
.columns(List.of(Column.builder().name("name1").type(new Utf8()).build()))
24+
.resolver(
25+
new TableResolver() {
26+
@Override
27+
public void resolve(
28+
ClientMeta clientMeta, Resource parent, TableOutputStream stream) {
29+
stream.write(Table1Data.builder().name("name1").build());
30+
stream.write(Table1Data.builder().name("name2").build());
31+
}
32+
})
33+
.columns(List.of(Column.builder().name("name").type(new Utf8()).build()))
1934
.build(),
2035
Table.builder()
2136
.name("table2")
22-
.columns(List.of(Column.builder().name("name1").type(new Utf8()).build()))
37+
.resolver(
38+
new TableResolver() {
39+
@Override
40+
public void resolve(
41+
ClientMeta clientMeta, Resource parent, TableOutputStream stream) {
42+
stream.write(Table2Data.builder().id("id1").build());
43+
stream.write(Table2Data.builder().id("id2").build());
44+
}
45+
})
46+
.columns(List.of(Column.builder().name("id").type(new Utf8()).build()))
2347
.build());
2448

49+
private Spec spec;
50+
2551
public MemDB() {
2652
super("memdb", "0.0.1");
2753
}
2854

29-
@Override
30-
public void init() {
31-
// do nothing
32-
}
33-
3455
@Override
3556
public List<Table> tables(
3657
List<String> includeList, List<String> skipList, boolean skipDependentTables)
37-
throws SchemaException {
58+
throws SchemaException, ClientNotInitializedException {
59+
if (this.client == null) {
60+
throw new ClientNotInitializedException();
61+
}
3862
return Table.filterDFS(allTables, includeList, skipList, skipDependentTables);
3963
}
4064

@@ -46,13 +70,19 @@ public void sync(
4670
boolean deterministicCqId,
4771
BackendOptions backendOptions,
4872
StreamObserver<io.cloudquery.plugin.v3.Sync.Response> syncStream)
49-
throws SchemaException {
73+
throws SchemaException, ClientNotInitializedException {
74+
if (this.client == null) {
75+
throw new ClientNotInitializedException();
76+
}
77+
5078
List<Table> filtered = Table.filterDFS(allTables, includeList, skipList, skipDependentTables);
5179
Scheduler.builder()
80+
.client(client)
5281
.tables(filtered)
5382
.syncStream(syncStream)
5483
.deterministicCqId(deterministicCqId)
5584
.logger(getLogger())
85+
.concurrency(this.spec.getConcurrency())
5686
.build()
5787
.sync();
5888
}
@@ -69,6 +99,17 @@ public void write() {
6999

70100
@Override
71101
public void close() {
72-
// do nothing
102+
if (this.client != null) {
103+
((MemDBClient) this.client).close();
104+
}
105+
}
106+
107+
@Override
108+
public ClientMeta newClient(String spec, NewClientOptions options) throws Exception {
109+
if (options.isNoConnection()) {
110+
return null;
111+
}
112+
this.spec = Spec.fromJSON(spec);
113+
return new MemDBClient();
73114
}
74115
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package io.cloudquery.memdb;
2+
3+
import io.cloudquery.schema.ClientMeta;
4+
5+
public class MemDBClient implements ClientMeta {
6+
private static final String id = "memdb";
7+
8+
public MemDBClient() {}
9+
10+
@Override
11+
public String getId() {
12+
return id;
13+
}
14+
15+
public void close() {
16+
// do nothing
17+
}
18+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.cloudquery.memdb;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.databind.JsonMappingException;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import lombok.Getter;
7+
import lombok.Setter;
8+
9+
@Getter
10+
@Setter
11+
public class Spec {
12+
public static Spec fromJSON(String json) throws JsonMappingException, JsonProcessingException {
13+
ObjectMapper objectMapper = new ObjectMapper();
14+
Spec spec = objectMapper.readValue(json, Spec.class);
15+
if (spec.getConcurrency() == 0) {
16+
spec.setConcurrency(10000);
17+
}
18+
return spec;
19+
}
20+
21+
private int concurrency;
22+
23+
public Spec() {}
24+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.cloudquery.memdb;
2+
3+
import lombok.Builder;
4+
import lombok.Getter;
5+
6+
@Builder
7+
@Getter
8+
public class Table1Data {
9+
private String name;
10+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.cloudquery.memdb;
2+
3+
import lombok.Builder;
4+
import lombok.Getter;
5+
6+
@Builder
7+
@Getter
8+
public class Table2Data {
9+
private String id;
10+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package io.cloudquery.plugin;
2+
3+
public class ClientNotInitializedException extends Exception {
4+
5+
public ClientNotInitializedException() {}
6+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package io.cloudquery.plugin;
2+
3+
import lombok.Builder;
4+
import lombok.Getter;
5+
6+
@Builder
7+
@Getter
8+
public class NewClientOptions {
9+
private final boolean noConnection;
10+
}

lib/src/main/java/io/cloudquery/plugin/Plugin.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package io.cloudquery.plugin;
22

3+
import io.cloudquery.schema.ClientMeta;
34
import io.cloudquery.schema.SchemaException;
45
import io.cloudquery.schema.Table;
56
import io.grpc.stub.StreamObserver;
@@ -16,12 +17,17 @@ public abstract class Plugin {
1617
@NonNull protected final String name;
1718
@NonNull protected final String version;
1819
@Setter protected Logger logger;
20+
protected ClientMeta client;
1921

20-
public abstract void init();
22+
public void init(String spec, NewClientOptions options) throws Exception {
23+
client = newClient(spec, options);
24+
}
25+
26+
public abstract ClientMeta newClient(String spec, NewClientOptions options) throws Exception;
2127

2228
public abstract List<Table> tables(
2329
List<String> includeList, List<String> skipList, boolean skipDependentTables)
24-
throws SchemaException;
30+
throws SchemaException, ClientNotInitializedException;
2531

2632
public abstract void sync(
2733
List<String> includeList,
@@ -30,7 +36,7 @@ public abstract void sync(
3036
boolean deterministicCqId,
3137
BackendOptions backendOptions,
3238
StreamObserver<io.cloudquery.plugin.v3.Sync.Response> syncStream)
33-
throws SchemaException;
39+
throws SchemaException, ClientNotInitializedException;
3440

3541
public abstract void read();
3642

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package io.cloudquery.plugin;
2+
3+
public interface TableOutputStream {
4+
public void write(Object data);
5+
}

0 commit comments

Comments
 (0)