Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 1 | package org.onlab.onos.store.service; |
| 2 | |
| 3 | import static com.google.common.base.Preconditions.checkArgument; |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 4 | import static com.google.common.base.Preconditions.checkNotNull; |
| 5 | import static org.onlab.onos.store.service.WriteRequest.Type.*; |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 6 | |
| 7 | import java.util.Objects; |
| 8 | |
Yuta HIGUCHI | 5e1cfe0 | 2014-11-04 21:22:45 -0800 | [diff] [blame] | 9 | import com.google.common.base.MoreObjects; |
| 10 | |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 11 | /** |
| 12 | * Database write request. |
| 13 | */ |
| 14 | public class WriteRequest { |
| 15 | |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 16 | public static final int ANY_VERSION = -1; |
| 17 | |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 18 | private final String tableName; |
| 19 | private final String key; |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 20 | |
| 21 | private final Type type; |
| 22 | |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 23 | private final byte[] newValue; |
| 24 | private final long previousVersion; |
| 25 | private final byte[] oldValue; |
| 26 | |
Yuta HIGUCHI | 47eb0d4 | 2014-11-05 14:31:20 -0800 | [diff] [blame] | 27 | /** |
| 28 | * Creates a write request, which will |
| 29 | * put the specified value to the table regardless of the previous value. |
| 30 | * |
| 31 | * @param tableName name of the table |
| 32 | * @param key key in the table |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 33 | * @param newValue value to write, must not be null |
Yuta HIGUCHI | 47eb0d4 | 2014-11-05 14:31:20 -0800 | [diff] [blame] | 34 | * @return WriteRequest |
| 35 | */ |
| 36 | public static WriteRequest put(String tableName, String key, |
| 37 | byte[] newValue) { |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 38 | return new WriteRequest(PUT, tableName, key, |
| 39 | checkNotNull(newValue), ANY_VERSION, null); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 40 | } |
| 41 | |
Yuta HIGUCHI | 47eb0d4 | 2014-11-05 14:31:20 -0800 | [diff] [blame] | 42 | /** |
| 43 | * Creates a write request, which will |
| 44 | * put the specified value to the table if the previous version matches. |
| 45 | * |
| 46 | * @param tableName name of the table |
| 47 | * @param key key in the table |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 48 | * @param newValue value to write, must not be null |
Yuta HIGUCHI | 47eb0d4 | 2014-11-05 14:31:20 -0800 | [diff] [blame] | 49 | * @param previousVersion previous version expected |
| 50 | * @return WriteRequest |
| 51 | */ |
| 52 | public static WriteRequest putIfVersionMatches(String tableName, String key, |
| 53 | byte[] newValue, |
| 54 | long previousVersion) { |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 55 | checkArgument(previousVersion >= 0); |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 56 | return new WriteRequest(PUT_IF_VERSION, tableName, key, |
| 57 | checkNotNull(newValue), previousVersion, null); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 58 | } |
| 59 | |
Yuta HIGUCHI | 47eb0d4 | 2014-11-05 14:31:20 -0800 | [diff] [blame] | 60 | /** |
| 61 | * Creates a write request, which will |
| 62 | * put the specified value to the table if the previous value matches. |
| 63 | * |
| 64 | * @param tableName name of the table |
| 65 | * @param key key in the table |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 66 | * @param newValue value to write, must not be null |
| 67 | * @param oldValue previous value expected, must not be null |
Yuta HIGUCHI | 47eb0d4 | 2014-11-05 14:31:20 -0800 | [diff] [blame] | 68 | * @return WriteRequest |
| 69 | */ |
| 70 | public static WriteRequest putIfValueMatches(String tableName, String key, |
| 71 | byte[] newValue, |
| 72 | byte[] oldValue) { |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 73 | return new WriteRequest(PUT_IF_VALUE, tableName, key, |
| 74 | checkNotNull(newValue), ANY_VERSION, |
| 75 | checkNotNull(oldValue)); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 76 | } |
| 77 | |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 78 | /** |
| 79 | * Creates a write request, which will |
| 80 | * put the specified value to the table if the previous value does not exist. |
| 81 | * |
| 82 | * @param tableName name of the table |
| 83 | * @param key key in the table |
| 84 | * @param newValue value to write, must not be null |
| 85 | * @return WriteRequest |
| 86 | */ |
| 87 | public static WriteRequest putIfAbsent(String tableName, String key, |
| 88 | byte[] newValue) { |
| 89 | return new WriteRequest(PUT_IF_ABSENT, tableName, key, |
| 90 | checkNotNull(newValue), ANY_VERSION, null); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Creates a write request, which will |
| 95 | * remove the specified entry from the table regardless of the previous value. |
| 96 | * |
| 97 | * @param tableName name of the table |
| 98 | * @param key key in the table |
| 99 | * @return WriteRequest |
| 100 | */ |
| 101 | public static WriteRequest remove(String tableName, String key) { |
| 102 | return new WriteRequest(REMOVE, tableName, key, |
| 103 | null, ANY_VERSION, null); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Creates a write request, which will |
| 108 | * remove the specified entry from the table if the previous version matches. |
| 109 | * |
| 110 | * @param tableName name of the table |
| 111 | * @param key key in the table |
| 112 | * @param previousVersion previous version expected |
| 113 | * @return WriteRequest |
| 114 | */ |
| 115 | public static WriteRequest remove(String tableName, String key, |
| 116 | long previousVersion) { |
| 117 | return new WriteRequest(REMOVE_IF_VALUE, tableName, key, |
| 118 | null, previousVersion, null); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Creates a write request, which will |
| 123 | * remove the specified entry from the table if the previous value matches. |
| 124 | * |
| 125 | * @param tableName name of the table |
| 126 | * @param key key in the table |
| 127 | * @param oldValue previous value expected, must not be null |
| 128 | * @return WriteRequest |
| 129 | */ |
| 130 | public static WriteRequest remove(String tableName, String key, |
| 131 | byte[] oldValue) { |
| 132 | return new WriteRequest(Type.REMOVE_IF_VALUE, tableName, key, |
| 133 | null, ANY_VERSION, checkNotNull(oldValue)); |
| 134 | } |
| 135 | |
| 136 | public enum Type { |
| 137 | PUT, |
| 138 | PUT_IF_VERSION, |
| 139 | PUT_IF_VALUE, |
| 140 | PUT_IF_ABSENT, |
| 141 | REMOVE, |
| 142 | REMOVE_IF_VERSION, |
| 143 | REMOVE_IF_VALUE, |
| 144 | } |
Yuta HIGUCHI | 47eb0d4 | 2014-11-05 14:31:20 -0800 | [diff] [blame] | 145 | |
Yuta HIGUCHI | 3a3ac96 | 2014-11-04 18:05:08 -0800 | [diff] [blame] | 146 | // hidden constructor |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 147 | protected WriteRequest(Type type, String tableName, String key, |
| 148 | byte[] newValue, |
| 149 | long previousVersion, byte[] oldValue) { |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 150 | |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 151 | checkNotNull(tableName); |
| 152 | checkNotNull(key); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 153 | |
| 154 | this.tableName = tableName; |
| 155 | this.key = key; |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 156 | this.type = type; |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 157 | this.newValue = newValue; |
| 158 | this.previousVersion = previousVersion; |
| 159 | this.oldValue = oldValue; |
| 160 | } |
| 161 | |
| 162 | public String tableName() { |
| 163 | return tableName; |
| 164 | } |
| 165 | |
| 166 | public String key() { |
| 167 | return key; |
| 168 | } |
| 169 | |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 170 | public WriteRequest.Type type() { |
| 171 | return type; |
| 172 | } |
| 173 | |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 174 | public byte[] newValue() { |
| 175 | return newValue; |
| 176 | } |
| 177 | |
| 178 | public long previousVersion() { |
| 179 | return previousVersion; |
| 180 | } |
| 181 | |
| 182 | public byte[] oldValue() { |
| 183 | return oldValue; |
| 184 | } |
| 185 | |
| 186 | @Override |
| 187 | public String toString() { |
Yuta HIGUCHI | 5e1cfe0 | 2014-11-04 21:22:45 -0800 | [diff] [blame] | 188 | return MoreObjects.toStringHelper(getClass()) |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 189 | .add("type", type) |
Yuta HIGUCHI | 5e1cfe0 | 2014-11-04 21:22:45 -0800 | [diff] [blame] | 190 | .add("tableName", tableName) |
| 191 | .add("key", key) |
| 192 | .add("newValue", newValue) |
| 193 | .add("previousVersion", previousVersion) |
| 194 | .add("oldValue", oldValue) |
| 195 | .toString(); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 198 | // TODO: revisit hashCode, equals condition |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 199 | @Override |
| 200 | public int hashCode() { |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 201 | return Objects.hash(type, key, tableName, previousVersion); |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | @Override |
| 205 | public boolean equals(Object obj) { |
| 206 | if (this == obj) { |
| 207 | return true; |
| 208 | } |
| 209 | if (obj == null) { |
| 210 | return false; |
| 211 | } |
| 212 | if (getClass() != obj.getClass()) { |
| 213 | return false; |
| 214 | } |
| 215 | WriteRequest other = (WriteRequest) obj; |
Yuta HIGUCHI | 361664e | 2014-11-06 17:28:47 -0800 | [diff] [blame^] | 216 | return Objects.equals(this.type, other.type) && |
| 217 | Objects.equals(this.key, other.key) && |
Madan Jampani | 08822c4 | 2014-11-04 17:17:46 -0800 | [diff] [blame] | 218 | Objects.equals(this.tableName, other.tableName) && |
| 219 | Objects.equals(this.previousVersion, other.previousVersion); |
| 220 | } |
| 221 | } |