Brian O'Connor | abafb50 | 2014-12-02 22:26:20 -0800 | [diff] [blame^] | 1 | package org.onosproject.store.service; |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 2 | |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 3 | import java.util.List; |
| 4 | |
Madan Jampani | f5d263b | 2014-11-13 10:04:40 -0800 | [diff] [blame] | 5 | import com.google.common.base.MoreObjects; |
Madan Jampani | 7aad233 | 2014-11-12 01:57:07 -0800 | [diff] [blame] | 6 | import com.google.common.collect.ImmutableList; |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 7 | import com.google.common.collect.Lists; |
| 8 | |
| 9 | /** |
| 10 | * Collection of write requests to be submitted as one batch. |
| 11 | */ |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 12 | public final class BatchWriteRequest { |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 13 | |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 14 | private final List<WriteRequest> writeRequests; |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 15 | |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 16 | /** |
| 17 | * Creates a new BatchWriteRequest object from the specified list of write requests. |
| 18 | * @param writeRequests write requests. |
| 19 | * @return BatchWriteRequest object. |
| 20 | */ |
| 21 | public static BatchWriteRequest create(List<WriteRequest> writeRequests) { |
| 22 | return new BatchWriteRequest(writeRequests); |
| 23 | } |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 24 | |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 25 | private BatchWriteRequest(List<WriteRequest> writeRequests) { |
Madan Jampani | 7aad233 | 2014-11-12 01:57:07 -0800 | [diff] [blame] | 26 | this.writeRequests = ImmutableList.copyOf(writeRequests); |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 27 | } |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 28 | |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 29 | /** |
| 30 | * Returns the requests in this batch as a list. |
| 31 | * @return list of write requests |
| 32 | */ |
| 33 | public List<WriteRequest> getAsList() { |
| 34 | return writeRequests; |
| 35 | } |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 36 | |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 37 | /** |
| 38 | * Returns the number of requests in this batch. |
| 39 | * @return size of request batch. |
| 40 | */ |
| 41 | public int batchSize() { |
| 42 | return writeRequests.size(); |
| 43 | } |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 44 | |
Madan Jampani | f5d263b | 2014-11-13 10:04:40 -0800 | [diff] [blame] | 45 | @Override |
| 46 | public String toString() { |
| 47 | return MoreObjects.toStringHelper(getClass()) |
| 48 | .add("writeRequests", writeRequests) |
| 49 | .toString(); |
| 50 | } |
| 51 | |
Yuta HIGUCHI | 3524229 | 2014-11-12 18:53:15 -0800 | [diff] [blame] | 52 | public static Builder newBuilder() { |
| 53 | return new Builder(); |
| 54 | } |
| 55 | |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 56 | /** |
| 57 | * Builder for BatchWriteRequest. |
| 58 | */ |
| 59 | public static class Builder { |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 60 | |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 61 | private final List<WriteRequest> writeRequests = Lists.newLinkedList(); |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 62 | |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 63 | public Builder put(String tableName, String key, byte[] value) { |
| 64 | writeRequests.add(WriteRequest.put(tableName, key, value)); |
| 65 | return this; |
| 66 | } |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 67 | |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 68 | public Builder putIfAbsent(String tableName, String key, byte[] value) { |
| 69 | writeRequests.add(WriteRequest.putIfAbsent(tableName, key, value)); |
| 70 | return this; |
| 71 | } |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 72 | |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 73 | public Builder putIfValueMatches(String tableName, String key, byte[] oldValue, byte[] newValue) { |
| 74 | writeRequests.add(WriteRequest.putIfValueMatches(tableName, key, oldValue, newValue)); |
| 75 | return this; |
| 76 | } |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 77 | |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 78 | public Builder putIfVersionMatches(String tableName, String key, byte[] value, long version) { |
| 79 | writeRequests.add(WriteRequest.putIfVersionMatches(tableName, key, value, version)); |
| 80 | return this; |
| 81 | } |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 82 | |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 83 | public Builder remove(String tableName, String key) { |
| 84 | writeRequests.add(WriteRequest.remove(tableName, key)); |
| 85 | return this; |
| 86 | } |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 87 | |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 88 | public Builder removeIfVersionMatches(String tableName, String key, long version) { |
| 89 | writeRequests.add(WriteRequest.removeIfVersionMatches(tableName, key, version)); |
| 90 | return this; |
| 91 | } |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 92 | |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 93 | public Builder removeIfValueMatches(String tableName, String key, byte[] value) { |
| 94 | writeRequests.add(WriteRequest.removeIfValueMatches(tableName, key, value)); |
| 95 | return this; |
| 96 | } |
Madan Jampani | 12390c1 | 2014-11-12 00:35:56 -0800 | [diff] [blame] | 97 | |
Madan Jampani | 23af4fc | 2014-11-12 00:54:18 -0800 | [diff] [blame] | 98 | public BatchWriteRequest build() { |
| 99 | return new BatchWriteRequest(writeRequests); |
| 100 | } |
| 101 | } |
| 102 | } |