blob: d51263573b4eca3a87ac61904d5f9e8fb9828c79 [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;
Yuta HIGUCHI841c0b62014-11-13 20:27:14 -08006import java.util.Map;
Madan Jampanif5d263b2014-11-13 10:04:40 -08007import java.util.Set;
Madan Jampani08822c42014-11-04 17:17:46 -08008import java.util.concurrent.CompletableFuture;
9import java.util.concurrent.ExecutionException;
10
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080011import net.kuujo.copycat.Copycat;
Madan Jampani08822c42014-11-04 17:17:46 -080012
Madan Jampani12390c12014-11-12 00:35:56 -080013import org.onlab.onos.store.service.BatchReadRequest;
14import org.onlab.onos.store.service.BatchWriteRequest;
Madan Jampani08822c42014-11-04 17:17:46 -080015import org.onlab.onos.store.service.DatabaseException;
Madan Jampani12390c12014-11-12 00:35:56 -080016import org.onlab.onos.store.service.ReadResult;
Yuta HIGUCHI841c0b62014-11-13 20:27:14 -080017import org.onlab.onos.store.service.VersionedValue;
Madan Jampani12390c12014-11-12 00:35:56 -080018import org.onlab.onos.store.service.WriteResult;
Madan Jampani08822c42014-11-04 17:17:46 -080019
Madan Jampani686fa182014-11-04 23:16:27 -080020/**
21 * Client for interacting with the Copycat Raft cluster.
22 */
Madan Jampani08822c42014-11-04 17:17:46 -080023public class DatabaseClient {
24
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080025 private final Copycat copycat;
Madan Jampani08822c42014-11-04 17:17:46 -080026
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080027 public DatabaseClient(Copycat copycat) {
28 this.copycat = checkNotNull(copycat);
Madan Jampani08822c42014-11-04 17:17:46 -080029 }
30
Madan Jampani08822c42014-11-04 17:17:46 -080031 public boolean createTable(String tableName) {
32
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080033 CompletableFuture<Boolean> future = copycat.submit("createTable", tableName);
Madan Jampani08822c42014-11-04 17:17:46 -080034 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080035 return future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080036 } catch (InterruptedException | ExecutionException e) {
37 throw new DatabaseException(e);
38 }
39 }
40
Madan Jampanidef2c652014-11-12 13:50:10 -080041 public boolean createTable(String tableName, int ttlMillis) {
42
Madan Jampanif5d263b2014-11-13 10:04:40 -080043 CompletableFuture<Boolean> future = copycat.submit("createTableWithExpiration", tableName);
Madan Jampanidef2c652014-11-12 13:50:10 -080044 try {
45 return future.get();
46 } catch (InterruptedException | ExecutionException e) {
47 throw new DatabaseException(e);
48 }
49 }
50
Madan Jampani08822c42014-11-04 17:17:46 -080051 public void dropTable(String tableName) {
52
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080053 CompletableFuture<Void> future = copycat.submit("dropTable", tableName);
Madan Jampani08822c42014-11-04 17:17:46 -080054 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080055 future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080056 } catch (InterruptedException | ExecutionException e) {
57 throw new DatabaseException(e);
58 }
59 }
60
61 public void dropAllTables() {
62
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080063 CompletableFuture<Void> future = copycat.submit("dropAllTables");
Madan Jampani08822c42014-11-04 17:17:46 -080064 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080065 future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080066 } catch (InterruptedException | ExecutionException e) {
67 throw new DatabaseException(e);
68 }
69 }
70
Madan Jampanif5d263b2014-11-13 10:04:40 -080071 public Set<String> listTables() {
Madan Jampani08822c42014-11-04 17:17:46 -080072
Madan Jampanif5d263b2014-11-13 10:04:40 -080073 CompletableFuture<Set<String>> future = copycat.submit("listTables");
Madan Jampani08822c42014-11-04 17:17:46 -080074 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080075 return future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080076 } catch (InterruptedException | ExecutionException e) {
77 throw new DatabaseException(e);
78 }
79 }
80
Madan Jampani12390c12014-11-12 00:35:56 -080081 public List<ReadResult> batchRead(BatchReadRequest batchRequest) {
Madan Jampani08822c42014-11-04 17:17:46 -080082
Madan Jampani12390c12014-11-12 00:35:56 -080083 CompletableFuture<List<ReadResult>> future = copycat.submit("read", batchRequest);
Madan Jampani08822c42014-11-04 17:17:46 -080084 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080085 return future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080086 } catch (InterruptedException | ExecutionException e) {
87 throw new DatabaseException(e);
88 }
89 }
90
Madan Jampani12390c12014-11-12 00:35:56 -080091 public List<WriteResult> batchWrite(BatchWriteRequest batchRequest) {
Madan Jampani08822c42014-11-04 17:17:46 -080092
Madan Jampani12390c12014-11-12 00:35:56 -080093 CompletableFuture<List<WriteResult>> future = copycat.submit("write", batchRequest);
Madan Jampani08822c42014-11-04 17:17:46 -080094 try {
Yuta HIGUCHIf8468442014-11-11 10:09:20 -080095 return future.get();
Madan Jampani08822c42014-11-04 17:17:46 -080096 } catch (InterruptedException | ExecutionException e) {
97 throw new DatabaseException(e);
98 }
99 }
Yuta HIGUCHI841c0b62014-11-13 20:27:14 -0800100
101 public Map<String, VersionedValue> getAll(String tableName) {
102 CompletableFuture<Map<String, VersionedValue>> future = copycat.submit("getAll", tableName);
103 try {
104 return future.get();
105 } catch (InterruptedException | ExecutionException e) {
106 throw new DatabaseException(e);
107 }
108 }
Madan Jampani08822c42014-11-04 17:17:46 -0800109}