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