blob: c749197e01d36d3096ad2e89d0c55d9afb2bbe7f [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 Jampani08822c42014-11-04 17:17:46 -080011import org.onlab.onos.store.service.DatabaseException;
12import org.onlab.onos.store.service.ReadRequest;
13import org.onlab.onos.store.service.WriteRequest;
14
Madan Jampani686fa182014-11-04 23:16:27 -080015/**
16 * Client for interacting with the Copycat Raft cluster.
17 */
Madan Jampani08822c42014-11-04 17:17:46 -080018public class DatabaseClient {
19
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080020 private final Copycat copycat;
Madan Jampani08822c42014-11-04 17:17:46 -080021
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080022 public DatabaseClient(Copycat copycat) {
23 this.copycat = checkNotNull(copycat);
Madan Jampani08822c42014-11-04 17:17:46 -080024 }
25
Madan Jampani08822c42014-11-04 17:17:46 -080026 public boolean createTable(String tableName) {
27
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080028 CompletableFuture<Boolean> future = copycat.submit("createTable", tableName);
Madan Jampani08822c42014-11-04 17:17:46 -080029 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080030 return future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080031 } catch (InterruptedException | ExecutionException e) {
32 throw new DatabaseException(e);
33 }
34 }
35
36 public void dropTable(String tableName) {
37
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080038 CompletableFuture<Void> future = copycat.submit("dropTable", tableName);
Madan Jampani08822c42014-11-04 17:17:46 -080039 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080040 future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080041 } catch (InterruptedException | ExecutionException e) {
42 throw new DatabaseException(e);
43 }
44 }
45
46 public void dropAllTables() {
47
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080048 CompletableFuture<Void> future = copycat.submit("dropAllTables");
Madan Jampani08822c42014-11-04 17:17:46 -080049 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080050 future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080051 } catch (InterruptedException | ExecutionException e) {
52 throw new DatabaseException(e);
53 }
54 }
55
Madan Jampani08822c42014-11-04 17:17:46 -080056 public List<String> listTables() {
57
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080058 CompletableFuture<List<String>> future = copycat.submit("listTables");
Madan Jampani08822c42014-11-04 17:17:46 -080059 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080060 return future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080061 } catch (InterruptedException | ExecutionException e) {
62 throw new DatabaseException(e);
63 }
64 }
65
Madan Jampani08822c42014-11-04 17:17:46 -080066 public List<InternalReadResult> batchRead(List<ReadRequest> requests) {
67
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080068 CompletableFuture<List<InternalReadResult>> future = copycat.submit("read", requests);
Madan Jampani08822c42014-11-04 17:17:46 -080069 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080070 return future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080071 } catch (InterruptedException | ExecutionException e) {
72 throw new DatabaseException(e);
73 }
74 }
75
Madan Jampani08822c42014-11-04 17:17:46 -080076 public List<InternalWriteResult> batchWrite(List<WriteRequest> requests) {
77
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080078 CompletableFuture<List<InternalWriteResult>> future = copycat.submit("write", requests);
Madan Jampani08822c42014-11-04 17:17:46 -080079 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080080 return future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080081 } catch (InterruptedException | ExecutionException e) {
82 throw new DatabaseException(e);
83 }
84 }
85}