blob: a6320ed51432331d5f15deaad302b3c6e4f7ea30 [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 Jampanif5d263b2014-11-13 10:04:40 -08006import java.util.Set;
Madan Jampani08822c42014-11-04 17:17:46 -08007import java.util.concurrent.CompletableFuture;
8import java.util.concurrent.ExecutionException;
9
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080010import net.kuujo.copycat.Copycat;
Madan Jampani08822c42014-11-04 17:17:46 -080011
Madan Jampani12390c12014-11-12 00:35:56 -080012import org.onlab.onos.store.service.BatchReadRequest;
13import org.onlab.onos.store.service.BatchWriteRequest;
Madan Jampani08822c42014-11-04 17:17:46 -080014import org.onlab.onos.store.service.DatabaseException;
Madan Jampani12390c12014-11-12 00:35:56 -080015import org.onlab.onos.store.service.ReadResult;
16import org.onlab.onos.store.service.WriteResult;
Madan Jampani08822c42014-11-04 17:17:46 -080017
Madan Jampani686fa182014-11-04 23:16:27 -080018/**
19 * Client for interacting with the Copycat Raft cluster.
20 */
Madan Jampani08822c42014-11-04 17:17:46 -080021public class DatabaseClient {
22
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080023 private final Copycat copycat;
Madan Jampani08822c42014-11-04 17:17:46 -080024
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080025 public DatabaseClient(Copycat copycat) {
26 this.copycat = checkNotNull(copycat);
Madan Jampani08822c42014-11-04 17:17:46 -080027 }
28
Madan Jampani08822c42014-11-04 17:17:46 -080029 public boolean createTable(String tableName) {
30
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080031 CompletableFuture<Boolean> future = copycat.submit("createTable", tableName);
Madan Jampani08822c42014-11-04 17:17:46 -080032 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080033 return future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080034 } catch (InterruptedException | ExecutionException e) {
35 throw new DatabaseException(e);
36 }
37 }
38
Madan Jampanidef2c652014-11-12 13:50:10 -080039 public boolean createTable(String tableName, int ttlMillis) {
40
Madan Jampanif5d263b2014-11-13 10:04:40 -080041 CompletableFuture<Boolean> future = copycat.submit("createTableWithExpiration", tableName);
Madan Jampanidef2c652014-11-12 13:50:10 -080042 try {
43 return future.get();
44 } catch (InterruptedException | ExecutionException e) {
45 throw new DatabaseException(e);
46 }
47 }
48
Madan Jampani08822c42014-11-04 17:17:46 -080049 public void dropTable(String tableName) {
50
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080051 CompletableFuture<Void> future = copycat.submit("dropTable", tableName);
Madan Jampani08822c42014-11-04 17:17:46 -080052 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080053 future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080054 } catch (InterruptedException | ExecutionException e) {
55 throw new DatabaseException(e);
56 }
57 }
58
59 public void dropAllTables() {
60
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080061 CompletableFuture<Void> future = copycat.submit("dropAllTables");
Madan Jampani08822c42014-11-04 17:17:46 -080062 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080063 future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080064 } catch (InterruptedException | ExecutionException e) {
65 throw new DatabaseException(e);
66 }
67 }
68
Madan Jampanif5d263b2014-11-13 10:04:40 -080069 public Set<String> listTables() {
Madan Jampani08822c42014-11-04 17:17:46 -080070
Madan Jampanif5d263b2014-11-13 10:04:40 -080071 CompletableFuture<Set<String>> future = copycat.submit("listTables");
Madan Jampani08822c42014-11-04 17:17:46 -080072 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080073 return future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080074 } catch (InterruptedException | ExecutionException e) {
75 throw new DatabaseException(e);
76 }
77 }
78
Madan Jampani12390c12014-11-12 00:35:56 -080079 public List<ReadResult> batchRead(BatchReadRequest batchRequest) {
Madan Jampani08822c42014-11-04 17:17:46 -080080
Madan Jampani12390c12014-11-12 00:35:56 -080081 CompletableFuture<List<ReadResult>> future = copycat.submit("read", batchRequest);
Madan Jampani08822c42014-11-04 17:17:46 -080082 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080083 return future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080084 } catch (InterruptedException | ExecutionException e) {
85 throw new DatabaseException(e);
86 }
87 }
88
Madan Jampani12390c12014-11-12 00:35:56 -080089 public List<WriteResult> batchWrite(BatchWriteRequest batchRequest) {
Madan Jampani08822c42014-11-04 17:17:46 -080090
Madan Jampani12390c12014-11-12 00:35:56 -080091 CompletableFuture<List<WriteResult>> future = copycat.submit("write", batchRequest);
Madan Jampani08822c42014-11-04 17:17:46 -080092 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080093 return future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080094 } catch (InterruptedException | ExecutionException e) {
95 throw new DatabaseException(e);
96 }
97 }
98}