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 { |
Yuta HIGUCHI | b2ab091 | 2014-11-10 11:44:40 -0800 | [diff] [blame^] | 42 | final SubmitResponse submitResponse = future.get(); |
| 43 | if (submitResponse.status() == Status.OK) { |
| 44 | return (boolean) submitResponse.result(); |
| 45 | } else { |
| 46 | throw new DatabaseException(submitResponse.error()); |
| 47 | } |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 48 | } catch (InterruptedException | ExecutionException e) { |
| 49 | throw new DatabaseException(e); |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | public void dropTable(String tableName) { |
| 54 | |
| 55 | SubmitRequest request = |
| 56 | new SubmitRequest( |
| 57 | nextId(), |
| 58 | "dropTable", |
| 59 | Arrays.asList(tableName)); |
| 60 | CompletableFuture<SubmitResponse> future = client.submit(request); |
| 61 | try { |
Yuta HIGUCHI | b2ab091 | 2014-11-10 11:44:40 -0800 | [diff] [blame^] | 62 | if (future.get().status() != Status.OK) { |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 63 | throw new DatabaseException(future.get().toString()); |
| 64 | } |
| 65 | |
| 66 | } catch (InterruptedException | ExecutionException e) { |
| 67 | throw new DatabaseException(e); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | public void dropAllTables() { |
| 72 | |
| 73 | SubmitRequest request = |
| 74 | new SubmitRequest( |
| 75 | nextId(), |
| 76 | "dropAllTables", |
| 77 | Arrays.asList()); |
| 78 | CompletableFuture<SubmitResponse> future = client.submit(request); |
| 79 | try { |
| 80 | if (future.get().status() != Status.OK) { |
| 81 | throw new DatabaseException(future.get().toString()); |
| 82 | } |
| 83 | } catch (InterruptedException | ExecutionException e) { |
| 84 | throw new DatabaseException(e); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | @SuppressWarnings("unchecked") |
| 89 | public List<String> listTables() { |
| 90 | |
| 91 | SubmitRequest request = |
| 92 | new SubmitRequest( |
| 93 | nextId(), |
| 94 | "listTables", |
| 95 | Arrays.asList()); |
| 96 | CompletableFuture<SubmitResponse> future = client.submit(request); |
| 97 | try { |
Yuta HIGUCHI | b2ab091 | 2014-11-10 11:44:40 -0800 | [diff] [blame^] | 98 | final SubmitResponse submitResponse = future.get(); |
| 99 | if (submitResponse.status() == Status.OK) { |
| 100 | return (List<String>) submitResponse.result(); |
| 101 | } else { |
| 102 | throw new DatabaseException(submitResponse.error()); |
| 103 | } |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 104 | } catch (InterruptedException | ExecutionException e) { |
| 105 | throw new DatabaseException(e); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | @SuppressWarnings("unchecked") |
| 110 | public List<InternalReadResult> batchRead(List<ReadRequest> requests) { |
| 111 | |
| 112 | SubmitRequest request = new SubmitRequest( |
| 113 | nextId(), |
| 114 | "read", |
| 115 | Arrays.asList(requests)); |
| 116 | |
| 117 | CompletableFuture<SubmitResponse> future = client.submit(request); |
| 118 | try { |
Yuta HIGUCHI | b2ab091 | 2014-11-10 11:44:40 -0800 | [diff] [blame^] | 119 | final SubmitResponse submitResponse = future.get(); |
| 120 | if (submitResponse.status() == Status.OK) { |
| 121 | return (List<InternalReadResult>) submitResponse.result(); |
| 122 | } else { |
| 123 | throw new DatabaseException(submitResponse.error()); |
| 124 | } |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 125 | } catch (InterruptedException | ExecutionException e) { |
| 126 | throw new DatabaseException(e); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | @SuppressWarnings("unchecked") |
| 131 | public List<InternalWriteResult> batchWrite(List<WriteRequest> requests) { |
| 132 | |
| 133 | SubmitRequest request = new SubmitRequest( |
| 134 | nextId(), |
| 135 | "write", |
| 136 | Arrays.asList(requests)); |
| 137 | |
| 138 | CompletableFuture<SubmitResponse> future = client.submit(request); |
| 139 | try { |
Yuta HIGUCHI | b2ab091 | 2014-11-10 11:44:40 -0800 | [diff] [blame^] | 140 | final SubmitResponse submitResponse = future.get(); |
| 141 | if (submitResponse.status() == Status.OK) { |
| 142 | return (List<InternalWriteResult>) submitResponse.result(); |
| 143 | } else { |
| 144 | throw new DatabaseException(submitResponse.error()); |
| 145 | } |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 146 | } catch (InterruptedException | ExecutionException e) { |
| 147 | throw new DatabaseException(e); |
| 148 | } |
| 149 | } |
| 150 | } |