|
| 1 | +package sqlancer.feldera; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 4 | +import com.fasterxml.jackson.databind.node.ObjectNode; |
| 5 | +import sqlancer.SQLancerDBConnection; |
| 6 | +import sqlancer.feldera.client.FelderaClient; |
| 7 | +import sqlancer.feldera.client.FelderaPipeline; |
| 8 | + |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | +import java.util.Map; |
| 12 | + |
| 13 | +public class FelderaConnection implements SQLancerDBConnection { |
| 14 | + private final FelderaClient client; |
| 15 | + private final String pipelineName; |
| 16 | + private final List<String> inserts; |
| 17 | + private boolean ready = false; |
| 18 | + private String ddl; |
| 19 | + |
| 20 | + public FelderaConnection(String url, String pipelineName) { |
| 21 | + this.client = new FelderaClient(url); |
| 22 | + this.pipelineName = pipelineName; |
| 23 | + this.inserts = new ArrayList<>(); |
| 24 | + this.ddl = ""; |
| 25 | + } |
| 26 | + |
| 27 | + public void prepare() throws Exception { |
| 28 | + if (!ready) { |
| 29 | + ObjectMapper mapper = new ObjectMapper(); |
| 30 | + ObjectNode node = mapper.createObjectNode(); |
| 31 | + |
| 32 | + node.put("name", this.pipelineName); |
| 33 | + node.put("description", "sqlancerTest"); |
| 34 | + String ddlWithInserts = ddl + "--" + String.join("\n--", inserts); |
| 35 | + node.put("program_code", ddlWithInserts); |
| 36 | + node.putObject("runtime_config"); |
| 37 | + node.putObject("program_config"); |
| 38 | + |
| 39 | + this.client.createPipeline(pipelineName, node.toString()); |
| 40 | + this.client.start(this.pipelineName); |
| 41 | + |
| 42 | + for (String insert : this.inserts) { |
| 43 | + this.client.exec(this.pipelineName, insert); |
| 44 | + } |
| 45 | + |
| 46 | + ready = true; |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public String getPipelineName() { |
| 51 | + return this.pipelineName; |
| 52 | + } |
| 53 | + |
| 54 | + public FelderaClient getClient() { |
| 55 | + return this.client; |
| 56 | + } |
| 57 | + |
| 58 | + @Override |
| 59 | + public String getDatabaseVersion() throws Exception { |
| 60 | + int x = this.client.getPipeline(this.pipelineName).getVersion(); |
| 61 | + return Integer.toString(x); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public void close() throws Exception { |
| 66 | + if (ready) { |
| 67 | + this.client.shutdown(pipelineName); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public FelderaPipeline get() throws Exception { |
| 72 | + return this.client.getPipeline(pipelineName); |
| 73 | + } |
| 74 | + |
| 75 | + public void buffer(String query) throws Exception { |
| 76 | + if (query.startsWith("INSERT")) { |
| 77 | + this.inserts.add(query); |
| 78 | + } else { |
| 79 | + this.ddl += query; |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + public Map<String, Object> execute(String query) throws Exception { |
| 84 | + return this.client.exec(this.pipelineName, query); |
| 85 | + } |
| 86 | + |
| 87 | + public void shutdown() throws Exception { |
| 88 | + this.client.shutdown(this.pipelineName); |
| 89 | + } |
| 90 | +} |
0 commit comments