blob: 0021eb7bd51335cbafcb2c9f89ada6fc94703b55 [file] [log] [blame]
Madan Jampani08822c42014-11-04 17:17:46 -08001package org.onlab.onos.store.service.impl;
2
Yuta HIGUCHIf8468442014-11-11 10:09:20 -08003import static com.google.common.base.Preconditions.checkNotNull;
4
Madan Jampani08822c42014-11-04 17:17:46 -08005import java.util.List;
Madan Jampani08822c42014-11-04 17:17:46 -08006import java.util.concurrent.CompletableFuture;
7import java.util.concurrent.ExecutionException;
8
Yuta HIGUCHIf8468442014-11-11 10:09:20 -08009import net.kuujo.copycat.Copycat;
Madan Jampani08822c42014-11-04 17:17:46 -080010
Madan Jampani12390c12014-11-12 00:35:56 -080011import org.onlab.onos.store.service.BatchReadRequest;
12import org.onlab.onos.store.service.BatchWriteRequest;
Madan Jampani08822c42014-11-04 17:17:46 -080013import org.onlab.onos.store.service.DatabaseException;
Madan Jampani12390c12014-11-12 00:35:56 -080014import org.onlab.onos.store.service.ReadResult;
15import org.onlab.onos.store.service.WriteResult;
Madan Jampani08822c42014-11-04 17:17:46 -080016
Madan Jampani686fa182014-11-04 23:16:27 -080017/**
18 * Client for interacting with the Copycat Raft cluster.
19 */
Madan Jampani08822c42014-11-04 17:17:46 -080020public class DatabaseClient {
21
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080022 private final Copycat copycat;
Madan Jampani08822c42014-11-04 17:17:46 -080023
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080024 public DatabaseClient(Copycat copycat) {
25 this.copycat = checkNotNull(copycat);
Madan Jampani08822c42014-11-04 17:17:46 -080026 }
27
Madan Jampani08822c42014-11-04 17:17:46 -080028 public boolean createTable(String tableName) {
29
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080030 CompletableFuture<Boolean> future = copycat.submit("createTable", tableName);
Madan Jampani08822c42014-11-04 17:17:46 -080031 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080032 return future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080033 } catch (InterruptedException | ExecutionException e) {
34 throw new DatabaseException(e);
35 }
36 }
37
Madan Jampanidef2c652014-11-12 13:50:10 -080038 public boolean createTable(String tableName, int ttlMillis) {
39
40 CompletableFuture<Boolean> future = copycat.submit("createTable", tableName, ttlMillis);
41 try {
42 return future.get();
43 } catch (InterruptedException | ExecutionException e) {
44 throw new DatabaseException(e);
45 }
46 }
47
Madan Jampani08822c42014-11-04 17:17:46 -080048 public void dropTable(String tableName) {
49
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080050 CompletableFuture<Void> future = copycat.submit("dropTable", tableName);
Madan Jampani08822c42014-11-04 17:17:46 -080051 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080052 future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080053 } catch (InterruptedException | ExecutionException e) {
54 throw new DatabaseException(e);
55 }
56 }
57
58 public void dropAllTables() {
59
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080060 CompletableFuture<Void> future = copycat.submit("dropAllTables");
Madan Jampani08822c42014-11-04 17:17:46 -080061 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080062 future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080063 } catch (InterruptedException | ExecutionException e) {
64 throw new DatabaseException(e);
65 }
66 }
67
Madan Jampani08822c42014-11-04 17:17:46 -080068 public List<String> listTables() {
69
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080070 CompletableFuture<List<String>> future = copycat.submit("listTables");
Madan Jampani08822c42014-11-04 17:17:46 -080071 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080072 return future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080073 } catch (InterruptedException | ExecutionException e) {
74 throw new DatabaseException(e);
75 }
76 }
77
Madan Jampani12390c12014-11-12 00:35:56 -080078 public List<ReadResult> batchRead(BatchReadRequest batchRequest) {
Madan Jampani08822c42014-11-04 17:17:46 -080079
Madan Jampani12390c12014-11-12 00:35:56 -080080 CompletableFuture<List<ReadResult>> future = copycat.submit("read", batchRequest);
Madan Jampani08822c42014-11-04 17:17:46 -080081 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080082 return future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080083 } catch (InterruptedException | ExecutionException e) {
84 throw new DatabaseException(e);
85 }
86 }
87
Madan Jampani12390c12014-11-12 00:35:56 -080088 public List<WriteResult> batchWrite(BatchWriteRequest batchRequest) {
Madan Jampani08822c42014-11-04 17:17:46 -080089
Madan Jampani12390c12014-11-12 00:35:56 -080090 CompletableFuture<List<WriteResult>> future = copycat.submit("write", batchRequest);
Madan Jampani08822c42014-11-04 17:17:46 -080091 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080092 return future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080093 } catch (InterruptedException | ExecutionException e) {
94 throw new DatabaseException(e);
95 }
96 }
97}