Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 1 | package org.onlab.onos.store.service.impl; |
| 2 | |
| 3 | import java.util.Arrays; |
| 4 | import java.util.List; |
| 5 | import java.util.UUID; |
| 6 | import java.util.concurrent.CompletableFuture; |
| 7 | import java.util.concurrent.ExecutionException; |
| 8 | |
| 9 | import net.kuujo.copycat.protocol.Response.Status; |
| 10 | import net.kuujo.copycat.protocol.SubmitRequest; |
| 11 | import net.kuujo.copycat.protocol.SubmitResponse; |
| 12 | import net.kuujo.copycat.spi.protocol.ProtocolClient; |
| 13 | |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 14 | import org.onlab.onos.store.service.DatabaseException; |
| 15 | import org.onlab.onos.store.service.ReadRequest; |
| 16 | import org.onlab.onos.store.service.WriteRequest; |
| 17 | |
Madan Jampani | 686fa18 | 2014-11-04 23:16:27 -0800 | [diff] [blame] | 18 | /** |
| 19 | * Client for interacting with the Copycat Raft cluster. |
| 20 | */ |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 21 | public class DatabaseClient { |
| 22 | |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 23 | private final ProtocolClient client; |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 24 | |
Madan Jampani | 9b19a82 | 2014-11-04 21:37:13 -0800 | [diff] [blame] | 25 | public DatabaseClient(ProtocolClient client) { |
| 26 | this.client = client; |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | private static String nextId() { |
| 30 | return UUID.randomUUID().toString(); |
| 31 | } |
| 32 | |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 33 | public boolean createTable(String tableName) { |
| 34 | |
| 35 | SubmitRequest request = |
| 36 | new SubmitRequest( |
| 37 | nextId(), |
| 38 | "createTable", |
| 39 | Arrays.asList(tableName)); |
| 40 | CompletableFuture<SubmitResponse> future = client.submit(request); |
| 41 | try { |
| 42 | return (boolean) future.get().result(); |
| 43 | } catch (InterruptedException | ExecutionException e) { |
| 44 | throw new DatabaseException(e); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | public void dropTable(String tableName) { |
| 49 | |
| 50 | SubmitRequest request = |
| 51 | new SubmitRequest( |
| 52 | nextId(), |
| 53 | "dropTable", |
| 54 | Arrays.asList(tableName)); |
| 55 | CompletableFuture<SubmitResponse> future = client.submit(request); |
| 56 | try { |
| 57 | if (future.get().status() == Status.OK) { |
| 58 | throw new DatabaseException(future.get().toString()); |
| 59 | } |
| 60 | |
| 61 | } catch (InterruptedException | ExecutionException e) { |
| 62 | throw new DatabaseException(e); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public void dropAllTables() { |
| 67 | |
| 68 | SubmitRequest request = |
| 69 | new SubmitRequest( |
| 70 | nextId(), |
| 71 | "dropAllTables", |
| 72 | Arrays.asList()); |
| 73 | CompletableFuture<SubmitResponse> future = client.submit(request); |
| 74 | try { |
| 75 | if (future.get().status() != Status.OK) { |
| 76 | throw new DatabaseException(future.get().toString()); |
| 77 | } |
| 78 | } catch (InterruptedException | ExecutionException e) { |
| 79 | throw new DatabaseException(e); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | @SuppressWarnings("unchecked") |
| 84 | public List<String> listTables() { |
| 85 | |
| 86 | SubmitRequest request = |
| 87 | new SubmitRequest( |
| 88 | nextId(), |
| 89 | "listTables", |
| 90 | Arrays.asList()); |
| 91 | CompletableFuture<SubmitResponse> future = client.submit(request); |
| 92 | try { |
| 93 | return (List<String>) future.get().result(); |
| 94 | } catch (InterruptedException | ExecutionException e) { |
| 95 | throw new DatabaseException(e); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | @SuppressWarnings("unchecked") |
| 100 | public List<InternalReadResult> batchRead(List<ReadRequest> requests) { |
| 101 | |
| 102 | SubmitRequest request = new SubmitRequest( |
| 103 | nextId(), |
| 104 | "read", |
| 105 | Arrays.asList(requests)); |
| 106 | |
| 107 | CompletableFuture<SubmitResponse> future = client.submit(request); |
| 108 | try { |
| 109 | List<InternalReadResult> internalReadResults = (List<InternalReadResult>) future.get().result(); |
| 110 | return internalReadResults; |
| 111 | } catch (InterruptedException | ExecutionException e) { |
| 112 | throw new DatabaseException(e); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | @SuppressWarnings("unchecked") |
| 117 | public List<InternalWriteResult> batchWrite(List<WriteRequest> requests) { |
| 118 | |
| 119 | SubmitRequest request = new SubmitRequest( |
| 120 | nextId(), |
| 121 | "write", |
| 122 | Arrays.asList(requests)); |
| 123 | |
| 124 | CompletableFuture<SubmitResponse> future = client.submit(request); |
| 125 | try { |
| 126 | List<InternalWriteResult> internalWriteResults = (List<InternalWriteResult>) future.get().result(); |
| 127 | return internalWriteResults; |
| 128 | } catch (InterruptedException | ExecutionException e) { |
| 129 | throw new DatabaseException(e); |
| 130 | } |
| 131 | } |
| 132 | } |