Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 1 | package org.onlab.onos.store.service.impl; |
| 2 | |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 3 | import static com.google.common.base.Preconditions.checkNotNull; |
| 4 | |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 5 | import java.util.List; |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 6 | import java.util.concurrent.CompletableFuture; |
| 7 | import java.util.concurrent.ExecutionException; |
| 8 | |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 9 | import net.kuujo.copycat.Copycat; |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 10 | |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame^] | 11 | import org.onlab.onos.store.service.BatchReadRequest; |
| 12 | import org.onlab.onos.store.service.BatchWriteRequest; |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 13 | import org.onlab.onos.store.service.DatabaseException; |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame^] | 14 | import org.onlab.onos.store.service.ReadResult; |
| 15 | import org.onlab.onos.store.service.WriteResult; |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 16 | |
Madan Jampani | 686fa18 | 2014-11-04 23:16:27 -0800 | [diff] [blame] | 17 | /** |
| 18 | * Client for interacting with the Copycat Raft cluster. |
| 19 | */ |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 20 | public class DatabaseClient { |
| 21 | |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 22 | private final Copycat copycat; |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 23 | |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 24 | public DatabaseClient(Copycat copycat) { |
| 25 | this.copycat = checkNotNull(copycat); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 26 | } |
| 27 | |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 28 | public boolean createTable(String tableName) { |
| 29 | |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 30 | CompletableFuture<Boolean> future = copycat.submit("createTable", tableName); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 31 | try { |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 32 | return future.get(); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 33 | } catch (InterruptedException | ExecutionException e) { |
| 34 | throw new DatabaseException(e); |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | public void dropTable(String tableName) { |
| 39 | |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 40 | CompletableFuture<Void> future = copycat.submit("dropTable", tableName); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 41 | try { |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 42 | future.get(); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 43 | } catch (InterruptedException | ExecutionException e) { |
| 44 | throw new DatabaseException(e); |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | public void dropAllTables() { |
| 49 | |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 50 | CompletableFuture<Void> future = copycat.submit("dropAllTables"); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 51 | try { |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 52 | future.get(); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 53 | } catch (InterruptedException | ExecutionException e) { |
| 54 | throw new DatabaseException(e); |
| 55 | } |
| 56 | } |
| 57 | |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 58 | public List<String> listTables() { |
| 59 | |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 60 | CompletableFuture<List<String>> future = copycat.submit("listTables"); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 61 | try { |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 62 | return future.get(); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 63 | } catch (InterruptedException | ExecutionException e) { |
| 64 | throw new DatabaseException(e); |
| 65 | } |
| 66 | } |
| 67 | |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame^] | 68 | public List<ReadResult> batchRead(BatchReadRequest batchRequest) { |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 69 | |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame^] | 70 | CompletableFuture<List<ReadResult>> future = copycat.submit("read", batchRequest); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 71 | try { |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 72 | return future.get(); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 73 | } catch (InterruptedException | ExecutionException e) { |
| 74 | throw new DatabaseException(e); |
| 75 | } |
| 76 | } |
| 77 | |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame^] | 78 | public List<WriteResult> batchWrite(BatchWriteRequest batchRequest) { |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 79 | |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame^] | 80 | CompletableFuture<List<WriteResult>> future = copycat.submit("write", batchRequest); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 81 | try { |
Yuta HIGUCHI | f846844 | 2014-11-11 10:09:20 -0800 | [diff] [blame] | 82 | return future.get(); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 83 | } catch (InterruptedException | ExecutionException e) { |
| 84 | throw new DatabaseException(e); |
| 85 | } |
| 86 | } |
| 87 | } |